blob: 277d6a395c4a0a7413646bef4cc345e0ea4beda6 [file] [log] [blame]
San Mehat586536c2010-02-16 17:12:00 -08001/*
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 Crowley14c8c072018-09-18 13:30:21 -070017#include <ctype.h>
18#include <dirent.h>
San Mehat586536c2010-02-16 17:12:00 -080019#include <errno.h>
San Mehat586536c2010-02-16 17:12:00 -080020#include <fcntl.h>
Jeff Sharkey3472e522017-10-06 18:02:53 -060021#include <fts.h>
San Mehat586536c2010-02-16 17:12:00 -080022#include <poll.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070023#include <pwd.h>
San Mehat586536c2010-02-16 17:12:00 -080024#include <signal.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <sys/stat.h>
29#include <unistd.h>
San Mehat586536c2010-02-16 17:12:00 -080030
Jeff Sharkey3472e522017-10-06 18:02:53 -060031#include <fstream>
Ricky Wai07e64a42020-02-11 14:31:24 +000032#include <mntent.h>
Jeff Sharkey3472e522017-10-06 18:02:53 -060033#include <unordered_set>
Jeff Sharkey32ebb732017-03-27 16:18:50 -060034
35#include <android-base/file.h>
Jeff Sharkey32ebb732017-03-27 16:18:50 -060036#include <android-base/logging.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070037#include <android-base/parseint.h>
38#include <android-base/stringprintf.h>
39#include <android-base/strings.h>
San Mehat586536c2010-02-16 17:12:00 -080040
41#include "Process.h"
42
Jeff Sharkey32ebb732017-03-27 16:18:50 -060043using android::base::StringPrintf;
44
Jeff Sharkey3472e522017-10-06 18:02:53 -060045namespace android {
46namespace vold {
San Mehat586536c2010-02-16 17:12:00 -080047
Jeff Sharkey3472e522017-10-06 18:02:53 -060048static bool checkMaps(const std::string& path, const std::string& prefix) {
49 bool found = false;
Suren Baghdasaryan28af26a2019-03-26 10:00:05 -070050 auto file = std::unique_ptr<FILE, decltype(&fclose)>{fopen(path.c_str(), "re"), fclose};
51 if (!file) {
52 return false;
53 }
54
55 char* buf = nullptr;
56 size_t len = 0;
57 while (getline(&buf, &len, file.get()) != -1) {
58 std::string line(buf);
Jeff Sharkey3472e522017-10-06 18:02:53 -060059 std::string::size_type pos = line.find('/');
60 if (pos != std::string::npos) {
61 line = line.substr(pos);
Elliott Hughes32a5b9a2017-12-20 12:38:47 -080062 if (android::base::StartsWith(line, prefix)) {
Jeff Sharkey3472e522017-10-06 18:02:53 -060063 LOG(WARNING) << "Found map " << path << " referencing " << line;
64 found = true;
Suren Baghdasaryan28af26a2019-03-26 10:00:05 -070065 break;
San Mehat586536c2010-02-16 17:12:00 -080066 }
San Mehat586536c2010-02-16 17:12:00 -080067 }
68 }
Suren Baghdasaryan28af26a2019-03-26 10:00:05 -070069 free(buf);
70
Jeff Sharkey3472e522017-10-06 18:02:53 -060071 return found;
San Mehat586536c2010-02-16 17:12:00 -080072}
73
Jeff Sharkey3472e522017-10-06 18:02:53 -060074static bool checkSymlink(const std::string& path, const std::string& prefix) {
75 std::string res;
76 if (android::base::Readlink(path, &res)) {
Elliott Hughes32a5b9a2017-12-20 12:38:47 -080077 if (android::base::StartsWith(res, prefix)) {
Jeff Sharkey3472e522017-10-06 18:02:53 -060078 LOG(WARNING) << "Found symlink " << path << " referencing " << res;
79 return true;
San Mehat586536c2010-02-16 17:12:00 -080080 }
81 }
Jeff Sharkey3472e522017-10-06 18:02:53 -060082 return false;
San Mehat586536c2010-02-16 17:12:00 -080083}
84
Ricky Wai07e64a42020-02-11 14:31:24 +000085// TODO: Refactor the code with KillProcessesWithOpenFiles().
86int KillProcessesWithMounts(const std::string& prefix, int signal) {
87 std::unordered_set<pid_t> pids;
88
89 auto proc_d = std::unique_ptr<DIR, int (*)(DIR*)>(opendir("/proc"), closedir);
90 if (!proc_d) {
91 PLOG(ERROR) << "Failed to open proc";
92 return -1;
93 }
94
95 struct dirent* proc_de;
96 while ((proc_de = readdir(proc_d.get())) != nullptr) {
97 // We only care about valid PIDs
98 pid_t pid;
99 if (proc_de->d_type != DT_DIR) continue;
100 if (!android::base::ParseInt(proc_de->d_name, &pid)) continue;
101
102 // Look for references to prefix
103 std::string mounts_file(StringPrintf("/proc/%d/mounts", pid));
104 auto fp = std::unique_ptr<FILE, int (*)(FILE*)>(
105 setmntent(mounts_file.c_str(), "r"), endmntent);
106 if (!fp) {
107 PLOG(WARNING) << "Failed to open " << mounts_file;
108 continue;
109 }
110
111 // Check if obb directory is mounted, and get all packages of mounted app data directory.
112 mntent* mentry;
113 while ((mentry = getmntent(fp.get())) != nullptr) {
114 if (android::base::StartsWith(mentry->mnt_dir, prefix)) {
115 pids.insert(pid);
116 break;
117 }
118 }
119 }
120 if (signal != 0) {
121 for (const auto& pid : pids) {
122 LOG(WARNING) << "Killing pid "<< pid << " with signal " << strsignal(signal) <<
123 " because it has a mount with prefix " << prefix;
124 kill(pid, signal);
125 }
126 }
127 return pids.size();
128}
129
Jeff Sharkey3472e522017-10-06 18:02:53 -0600130int KillProcessesWithOpenFiles(const std::string& prefix, int signal) {
131 std::unordered_set<pid_t> pids;
San Mehat586536c2010-02-16 17:12:00 -0800132
Jeff Sharkey3472e522017-10-06 18:02:53 -0600133 auto proc_d = std::unique_ptr<DIR, int (*)(DIR*)>(opendir("/proc"), closedir);
134 if (!proc_d) {
135 PLOG(ERROR) << "Failed to open proc";
136 return -1;
San Mehat586536c2010-02-16 17:12:00 -0800137 }
138
Jeff Sharkey3472e522017-10-06 18:02:53 -0600139 struct dirent* proc_de;
140 while ((proc_de = readdir(proc_d.get())) != nullptr) {
141 // We only care about valid PIDs
142 pid_t pid;
143 if (proc_de->d_type != DT_DIR) continue;
144 if (!android::base::ParseInt(proc_de->d_name, &pid)) continue;
Jeff Sharkey32ebb732017-03-27 16:18:50 -0600145
Jeff Sharkey3472e522017-10-06 18:02:53 -0600146 // Look for references to prefix
147 bool found = false;
148 auto path = StringPrintf("/proc/%d", pid);
149 found |= checkMaps(path + "/maps", prefix);
150 found |= checkSymlink(path + "/cwd", prefix);
151 found |= checkSymlink(path + "/root", prefix);
152 found |= checkSymlink(path + "/exe", prefix);
San Mehat586536c2010-02-16 17:12:00 -0800153
Jeff Sharkey3472e522017-10-06 18:02:53 -0600154 auto fd_path = path + "/fd";
155 auto fd_d = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(fd_path.c_str()), closedir);
156 if (!fd_d) {
157 PLOG(WARNING) << "Failed to open " << fd_path;
San Mehat586536c2010-02-16 17:12:00 -0800158 } else {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600159 struct dirent* fd_de;
160 while ((fd_de = readdir(fd_d.get())) != nullptr) {
161 if (fd_de->d_type != DT_LNK) continue;
162 found |= checkSymlink(fd_path + "/" + fd_de->d_name, prefix);
163 }
San Mehat586536c2010-02-16 17:12:00 -0800164 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700165
Jeff Sharkey3472e522017-10-06 18:02:53 -0600166 if (found) {
167 pids.insert(pid);
San Mehat586536c2010-02-16 17:12:00 -0800168 }
169 }
Jeff Sharkey3472e522017-10-06 18:02:53 -0600170 if (signal != 0) {
171 for (const auto& pid : pids) {
172 LOG(WARNING) << "Sending " << strsignal(signal) << " to " << pid;
173 kill(pid, signal);
174 }
175 }
176 return pids.size();
San Mehat586536c2010-02-16 17:12:00 -0800177}
Jeff Sharkey3472e522017-10-06 18:02:53 -0600178
179} // namespace vold
180} // namespace android