San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 17 | #include <ctype.h> |
| 18 | #include <dirent.h> |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 19 | #include <errno.h> |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 20 | #include <fcntl.h> |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 21 | #include <fts.h> |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 22 | #include <poll.h> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 23 | #include <pwd.h> |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 24 | #include <signal.h> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <unistd.h> |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 30 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 31 | #include <fstream> |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 32 | #include <mntent.h> |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 33 | #include <unordered_set> |
Jeff Sharkey | 32ebb73 | 2017-03-27 16:18:50 -0600 | [diff] [blame] | 34 | |
| 35 | #include <android-base/file.h> |
Jeff Sharkey | 32ebb73 | 2017-03-27 16:18:50 -0600 | [diff] [blame] | 36 | #include <android-base/logging.h> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 37 | #include <android-base/parseint.h> |
| 38 | #include <android-base/stringprintf.h> |
| 39 | #include <android-base/strings.h> |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 40 | |
| 41 | #include "Process.h" |
Zim | 7527300 | 2021-03-04 12:21:24 +0000 | [diff] [blame] | 42 | #include "Utils.h" |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 43 | |
Jeff Sharkey | 32ebb73 | 2017-03-27 16:18:50 -0600 | [diff] [blame] | 44 | using android::base::StringPrintf; |
| 45 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 46 | namespace android { |
| 47 | namespace vold { |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 48 | |
Omar Eissa | fedaddb | 2025-03-12 16:09:16 +0000 | [diff] [blame] | 49 | static bool checkMaps(const std::string& path, const std::vector<std::string>& prefixes) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 50 | bool found = false; |
Suren Baghdasaryan | 28af26a | 2019-03-26 10:00:05 -0700 | [diff] [blame] | 51 | auto file = std::unique_ptr<FILE, decltype(&fclose)>{fopen(path.c_str(), "re"), fclose}; |
| 52 | if (!file) { |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | char* buf = nullptr; |
| 57 | size_t len = 0; |
| 58 | while (getline(&buf, &len, file.get()) != -1) { |
| 59 | std::string line(buf); |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 60 | std::string::size_type pos = line.find('/'); |
| 61 | if (pos != std::string::npos) { |
| 62 | line = line.substr(pos); |
Omar Eissa | fedaddb | 2025-03-12 16:09:16 +0000 | [diff] [blame] | 63 | for (const auto& prefix : prefixes) { |
| 64 | if (android::base::StartsWith(line, prefix)) { |
| 65 | LOG(WARNING) << "Found map " << path << " referencing " << line; |
| 66 | found = true; |
| 67 | break; |
| 68 | } |
| 69 | } |
| 70 | if (found) { |
Suren Baghdasaryan | 28af26a | 2019-03-26 10:00:05 -0700 | [diff] [blame] | 71 | break; |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 72 | } |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 73 | } |
| 74 | } |
Suren Baghdasaryan | 28af26a | 2019-03-26 10:00:05 -0700 | [diff] [blame] | 75 | free(buf); |
| 76 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 77 | return found; |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Omar Eissa | fedaddb | 2025-03-12 16:09:16 +0000 | [diff] [blame] | 80 | static bool checkSymlink(const std::string& path, const std::vector<std::string>& prefixes) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 81 | std::string res; |
| 82 | if (android::base::Readlink(path, &res)) { |
Omar Eissa | fedaddb | 2025-03-12 16:09:16 +0000 | [diff] [blame] | 83 | for (const auto& prefix : prefixes) { |
| 84 | if (android::base::StartsWith(res, prefix)) { |
| 85 | LOG(WARNING) << "Found symlink " << path << " referencing " << res; |
| 86 | return true; |
| 87 | } |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 88 | } |
| 89 | } |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 90 | return false; |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 93 | // TODO: Refactor the code with KillProcessesWithOpenFiles(). |
Ricky Wai | 2335637 | 2021-04-30 09:53:07 +0100 | [diff] [blame] | 94 | int KillProcessesWithTmpfsMounts(const std::string& prefix, int signal) { |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 95 | std::unordered_set<pid_t> pids; |
| 96 | |
| 97 | auto proc_d = std::unique_ptr<DIR, int (*)(DIR*)>(opendir("/proc"), closedir); |
| 98 | if (!proc_d) { |
| 99 | PLOG(ERROR) << "Failed to open proc"; |
| 100 | return -1; |
| 101 | } |
| 102 | |
| 103 | struct dirent* proc_de; |
| 104 | while ((proc_de = readdir(proc_d.get())) != nullptr) { |
| 105 | // We only care about valid PIDs |
| 106 | pid_t pid; |
| 107 | if (proc_de->d_type != DT_DIR) continue; |
| 108 | if (!android::base::ParseInt(proc_de->d_name, &pid)) continue; |
| 109 | |
| 110 | // Look for references to prefix |
| 111 | std::string mounts_file(StringPrintf("/proc/%d/mounts", pid)); |
| 112 | auto fp = std::unique_ptr<FILE, int (*)(FILE*)>( |
| 113 | setmntent(mounts_file.c_str(), "r"), endmntent); |
| 114 | if (!fp) { |
| 115 | PLOG(WARNING) << "Failed to open " << mounts_file; |
| 116 | continue; |
| 117 | } |
| 118 | |
| 119 | // Check if obb directory is mounted, and get all packages of mounted app data directory. |
| 120 | mntent* mentry; |
| 121 | while ((mentry = getmntent(fp.get())) != nullptr) { |
Ricky Wai | 2335637 | 2021-04-30 09:53:07 +0100 | [diff] [blame] | 122 | if (mentry->mnt_fsname != nullptr && strncmp(mentry->mnt_fsname, "tmpfs", 5) == 0 |
| 123 | && android::base::StartsWith(mentry->mnt_dir, prefix)) { |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 124 | pids.insert(pid); |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | if (signal != 0) { |
| 130 | for (const auto& pid : pids) { |
| 131 | LOG(WARNING) << "Killing pid "<< pid << " with signal " << strsignal(signal) << |
| 132 | " because it has a mount with prefix " << prefix; |
| 133 | kill(pid, signal); |
| 134 | } |
| 135 | } |
| 136 | return pids.size(); |
| 137 | } |
| 138 | |
Omar Eissa | fedaddb | 2025-03-12 16:09:16 +0000 | [diff] [blame] | 139 | int KillProcessesWithOpenFiles(const std::vector<std::string>& prefixes, int signal, |
| 140 | bool killFuseDaemon) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 141 | std::unordered_set<pid_t> pids; |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 142 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 143 | auto proc_d = std::unique_ptr<DIR, int (*)(DIR*)>(opendir("/proc"), closedir); |
| 144 | if (!proc_d) { |
| 145 | PLOG(ERROR) << "Failed to open proc"; |
| 146 | return -1; |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 149 | struct dirent* proc_de; |
| 150 | while ((proc_de = readdir(proc_d.get())) != nullptr) { |
| 151 | // We only care about valid PIDs |
| 152 | pid_t pid; |
| 153 | if (proc_de->d_type != DT_DIR) continue; |
| 154 | if (!android::base::ParseInt(proc_de->d_name, &pid)) continue; |
Jeff Sharkey | 32ebb73 | 2017-03-27 16:18:50 -0600 | [diff] [blame] | 155 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 156 | // Look for references to prefix |
| 157 | bool found = false; |
| 158 | auto path = StringPrintf("/proc/%d", pid); |
Omar Eissa | fedaddb | 2025-03-12 16:09:16 +0000 | [diff] [blame] | 159 | found |= checkMaps(path + "/maps", prefixes); |
| 160 | found |= checkSymlink(path + "/cwd", prefixes); |
| 161 | found |= checkSymlink(path + "/root", prefixes); |
| 162 | found |= checkSymlink(path + "/exe", prefixes); |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 163 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 164 | auto fd_path = path + "/fd"; |
| 165 | auto fd_d = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(fd_path.c_str()), closedir); |
| 166 | if (!fd_d) { |
| 167 | PLOG(WARNING) << "Failed to open " << fd_path; |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 168 | } else { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 169 | struct dirent* fd_de; |
| 170 | while ((fd_de = readdir(fd_d.get())) != nullptr) { |
| 171 | if (fd_de->d_type != DT_LNK) continue; |
Omar Eissa | fedaddb | 2025-03-12 16:09:16 +0000 | [diff] [blame] | 172 | found |= checkSymlink(fd_path + "/" + fd_de->d_name, prefixes); |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 173 | } |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 174 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 175 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 176 | if (found) { |
Zim | 7527300 | 2021-03-04 12:21:24 +0000 | [diff] [blame] | 177 | if (!IsFuseDaemon(pid) || killFuseDaemon) { |
| 178 | pids.insert(pid); |
| 179 | } else { |
| 180 | LOG(WARNING) << "Found FUSE daemon with open file. Skipping..."; |
| 181 | } |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 182 | } |
| 183 | } |
Jahdiel Alvarez | d7c4296 | 2023-10-25 17:16:38 -0700 | [diff] [blame] | 184 | int totalKilledPids = pids.size(); |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 185 | if (signal != 0) { |
| 186 | for (const auto& pid : pids) { |
Eric Biggers | c78ae60 | 2021-05-05 12:11:33 -0700 | [diff] [blame] | 187 | std::string comm; |
| 188 | android::base::ReadFileToString(StringPrintf("/proc/%d/comm", pid), &comm); |
| 189 | comm = android::base::Trim(comm); |
| 190 | |
| 191 | std::string exe; |
| 192 | android::base::Readlink(StringPrintf("/proc/%d/exe", pid), &exe); |
| 193 | |
| 194 | LOG(WARNING) << "Sending " << strsignal(signal) << " to pid " << pid << " (" << comm |
| 195 | << ", " << exe << ")"; |
Jahdiel Alvarez | d7c4296 | 2023-10-25 17:16:38 -0700 | [diff] [blame] | 196 | if (kill(pid, signal) < 0) { |
| 197 | if (errno == ESRCH) { |
| 198 | totalKilledPids--; |
| 199 | LOG(WARNING) << "The target pid " << pid << " was already killed"; |
| 200 | continue; |
| 201 | } |
| 202 | LOG(ERROR) << "Unable to send signal " << strsignal(signal) << " to pid " << pid; |
| 203 | } |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 204 | } |
| 205 | } |
Jahdiel Alvarez | d7c4296 | 2023-10-25 17:16:38 -0700 | [diff] [blame] | 206 | return totalKilledPids; |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 207 | } |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 208 | |
Omar Eissa | fedaddb | 2025-03-12 16:09:16 +0000 | [diff] [blame] | 209 | int KillProcessesWithOpenFiles(const std::string& prefix, int signal, bool killFuseDaemon) { |
| 210 | return KillProcessesWithOpenFiles(std::vector<std::string>(1, prefix), signal, |
| 211 | killFuseDaemon); |
| 212 | } |
| 213 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 214 | } // namespace vold |
| 215 | } // namespace android |