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
17 #include <ctype.h>
18 #include <dirent.h>
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <fts.h>
22 #include <poll.h>
23 #include <pwd.h>
24 #include <signal.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30
31 #include <fstream>
32 #include <mntent.h>
33 #include <unordered_set>
34
35 #include <android-base/file.h>
36 #include <android-base/logging.h>
37 #include <android-base/parseint.h>
38 #include <android-base/stringprintf.h>
39 #include <android-base/strings.h>
40
41 #include "Process.h"
42 #include "Utils.h"
43
44 using android::base::StringPrintf;
45
46 namespace android {
47 namespace vold {
48
checkMaps(const std::string & path,const std::string & prefix)49 static bool checkMaps(const std::string& path, const std::string& prefix) {
50 bool found = false;
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);
60 std::string::size_type pos = line.find('/');
61 if (pos != std::string::npos) {
62 line = line.substr(pos);
63 if (android::base::StartsWith(line, prefix)) {
64 LOG(WARNING) << "Found map " << path << " referencing " << line;
65 found = true;
66 break;
67 }
68 }
69 }
70 free(buf);
71
72 return found;
73 }
74
checkSymlink(const std::string & path,const std::string & prefix)75 static bool checkSymlink(const std::string& path, const std::string& prefix) {
76 std::string res;
77 if (android::base::Readlink(path, &res)) {
78 if (android::base::StartsWith(res, prefix)) {
79 LOG(WARNING) << "Found symlink " << path << " referencing " << res;
80 return true;
81 }
82 }
83 return false;
84 }
85
86 // TODO: Refactor the code with KillProcessesWithOpenFiles().
KillProcessesWithTmpfsMounts(const std::string & prefix,int signal)87 int KillProcessesWithTmpfsMounts(const std::string& prefix, int signal) {
88 std::unordered_set<pid_t> pids;
89
90 auto proc_d = std::unique_ptr<DIR, int (*)(DIR*)>(opendir("/proc"), closedir);
91 if (!proc_d) {
92 PLOG(ERROR) << "Failed to open proc";
93 return -1;
94 }
95
96 struct dirent* proc_de;
97 while ((proc_de = readdir(proc_d.get())) != nullptr) {
98 // We only care about valid PIDs
99 pid_t pid;
100 if (proc_de->d_type != DT_DIR) continue;
101 if (!android::base::ParseInt(proc_de->d_name, &pid)) continue;
102
103 // Look for references to prefix
104 std::string mounts_file(StringPrintf("/proc/%d/mounts", pid));
105 auto fp = std::unique_ptr<FILE, int (*)(FILE*)>(
106 setmntent(mounts_file.c_str(), "r"), endmntent);
107 if (!fp) {
108 PLOG(WARNING) << "Failed to open " << mounts_file;
109 continue;
110 }
111
112 // Check if obb directory is mounted, and get all packages of mounted app data directory.
113 mntent* mentry;
114 while ((mentry = getmntent(fp.get())) != nullptr) {
115 if (mentry->mnt_fsname != nullptr && strncmp(mentry->mnt_fsname, "tmpfs", 5) == 0
116 && android::base::StartsWith(mentry->mnt_dir, prefix)) {
117 pids.insert(pid);
118 break;
119 }
120 }
121 }
122 if (signal != 0) {
123 for (const auto& pid : pids) {
124 LOG(WARNING) << "Killing pid "<< pid << " with signal " << strsignal(signal) <<
125 " because it has a mount with prefix " << prefix;
126 kill(pid, signal);
127 }
128 }
129 return pids.size();
130 }
131
KillProcessesWithOpenFiles(const std::string & prefix,int signal,bool killFuseDaemon)132 int KillProcessesWithOpenFiles(const std::string& prefix, int signal, bool killFuseDaemon) {
133 std::unordered_set<pid_t> pids;
134
135 auto proc_d = std::unique_ptr<DIR, int (*)(DIR*)>(opendir("/proc"), closedir);
136 if (!proc_d) {
137 PLOG(ERROR) << "Failed to open proc";
138 return -1;
139 }
140
141 struct dirent* proc_de;
142 while ((proc_de = readdir(proc_d.get())) != nullptr) {
143 // We only care about valid PIDs
144 pid_t pid;
145 if (proc_de->d_type != DT_DIR) continue;
146 if (!android::base::ParseInt(proc_de->d_name, &pid)) continue;
147
148 // Look for references to prefix
149 bool found = false;
150 auto path = StringPrintf("/proc/%d", pid);
151 found |= checkMaps(path + "/maps", prefix);
152 found |= checkSymlink(path + "/cwd", prefix);
153 found |= checkSymlink(path + "/root", prefix);
154 found |= checkSymlink(path + "/exe", prefix);
155
156 auto fd_path = path + "/fd";
157 auto fd_d = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(fd_path.c_str()), closedir);
158 if (!fd_d) {
159 PLOG(WARNING) << "Failed to open " << fd_path;
160 } else {
161 struct dirent* fd_de;
162 while ((fd_de = readdir(fd_d.get())) != nullptr) {
163 if (fd_de->d_type != DT_LNK) continue;
164 found |= checkSymlink(fd_path + "/" + fd_de->d_name, prefix);
165 }
166 }
167
168 if (found) {
169 if (!IsFuseDaemon(pid) || killFuseDaemon) {
170 pids.insert(pid);
171 } else {
172 LOG(WARNING) << "Found FUSE daemon with open file. Skipping...";
173 }
174 }
175 }
176 if (signal != 0) {
177 for (const auto& pid : pids) {
178 std::string comm;
179 android::base::ReadFileToString(StringPrintf("/proc/%d/comm", pid), &comm);
180 comm = android::base::Trim(comm);
181
182 std::string exe;
183 android::base::Readlink(StringPrintf("/proc/%d/exe", pid), &exe);
184
185 LOG(WARNING) << "Sending " << strsignal(signal) << " to pid " << pid << " (" << comm
186 << ", " << exe << ")";
187 kill(pid, signal);
188 }
189 }
190 return pids.size();
191 }
192
193 } // namespace vold
194 } // namespace android
195