blob: 62d51a22d703075388c39c885410fb0f2a4a2a70 [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"
Zim75273002021-03-04 12:21:24 +000042#include "Utils.h"
San Mehat586536c2010-02-16 17:12:00 -080043
Jeff Sharkey32ebb732017-03-27 16:18:50 -060044using android::base::StringPrintf;
45
Jeff Sharkey3472e522017-10-06 18:02:53 -060046namespace android {
47namespace vold {
San Mehat586536c2010-02-16 17:12:00 -080048
Jeff Sharkey3472e522017-10-06 18:02:53 -060049static bool checkMaps(const std::string& path, const std::string& prefix) {
50 bool found = false;
Suren Baghdasaryan28af26a2019-03-26 10:00:05 -070051 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 Sharkey3472e522017-10-06 18:02:53 -060060 std::string::size_type pos = line.find('/');
61 if (pos != std::string::npos) {
62 line = line.substr(pos);
Elliott Hughes32a5b9a2017-12-20 12:38:47 -080063 if (android::base::StartsWith(line, prefix)) {
Jeff Sharkey3472e522017-10-06 18:02:53 -060064 LOG(WARNING) << "Found map " << path << " referencing " << line;
65 found = true;
Suren Baghdasaryan28af26a2019-03-26 10:00:05 -070066 break;
San Mehat586536c2010-02-16 17:12:00 -080067 }
San Mehat586536c2010-02-16 17:12:00 -080068 }
69 }
Suren Baghdasaryan28af26a2019-03-26 10:00:05 -070070 free(buf);
71
Jeff Sharkey3472e522017-10-06 18:02:53 -060072 return found;
San Mehat586536c2010-02-16 17:12:00 -080073}
74
Jeff Sharkey3472e522017-10-06 18:02:53 -060075static bool checkSymlink(const std::string& path, const std::string& prefix) {
76 std::string res;
77 if (android::base::Readlink(path, &res)) {
Elliott Hughes32a5b9a2017-12-20 12:38:47 -080078 if (android::base::StartsWith(res, prefix)) {
Jeff Sharkey3472e522017-10-06 18:02:53 -060079 LOG(WARNING) << "Found symlink " << path << " referencing " << res;
80 return true;
San Mehat586536c2010-02-16 17:12:00 -080081 }
82 }
Jeff Sharkey3472e522017-10-06 18:02:53 -060083 return false;
San Mehat586536c2010-02-16 17:12:00 -080084}
85
Ricky Wai07e64a42020-02-11 14:31:24 +000086// TODO: Refactor the code with KillProcessesWithOpenFiles().
87int KillProcessesWithMounts(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 (android::base::StartsWith(mentry->mnt_dir, prefix)) {
116 pids.insert(pid);
117 break;
118 }
119 }
120 }
121 if (signal != 0) {
122 for (const auto& pid : pids) {
123 LOG(WARNING) << "Killing pid "<< pid << " with signal " << strsignal(signal) <<
124 " because it has a mount with prefix " << prefix;
125 kill(pid, signal);
126 }
127 }
128 return pids.size();
129}
130
Zim75273002021-03-04 12:21:24 +0000131int KillProcessesWithOpenFiles(const std::string& prefix, int signal, bool killFuseDaemon) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600132 std::unordered_set<pid_t> pids;
San Mehat586536c2010-02-16 17:12:00 -0800133
Jeff Sharkey3472e522017-10-06 18:02:53 -0600134 auto proc_d = std::unique_ptr<DIR, int (*)(DIR*)>(opendir("/proc"), closedir);
135 if (!proc_d) {
136 PLOG(ERROR) << "Failed to open proc";
137 return -1;
San Mehat586536c2010-02-16 17:12:00 -0800138 }
139
Jeff Sharkey3472e522017-10-06 18:02:53 -0600140 struct dirent* proc_de;
141 while ((proc_de = readdir(proc_d.get())) != nullptr) {
142 // We only care about valid PIDs
143 pid_t pid;
144 if (proc_de->d_type != DT_DIR) continue;
145 if (!android::base::ParseInt(proc_de->d_name, &pid)) continue;
Jeff Sharkey32ebb732017-03-27 16:18:50 -0600146
Jeff Sharkey3472e522017-10-06 18:02:53 -0600147 // Look for references to prefix
148 bool found = false;
149 auto path = StringPrintf("/proc/%d", pid);
150 found |= checkMaps(path + "/maps", prefix);
151 found |= checkSymlink(path + "/cwd", prefix);
152 found |= checkSymlink(path + "/root", prefix);
153 found |= checkSymlink(path + "/exe", prefix);
San Mehat586536c2010-02-16 17:12:00 -0800154
Jeff Sharkey3472e522017-10-06 18:02:53 -0600155 auto fd_path = path + "/fd";
156 auto fd_d = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(fd_path.c_str()), closedir);
157 if (!fd_d) {
158 PLOG(WARNING) << "Failed to open " << fd_path;
San Mehat586536c2010-02-16 17:12:00 -0800159 } else {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600160 struct dirent* fd_de;
161 while ((fd_de = readdir(fd_d.get())) != nullptr) {
162 if (fd_de->d_type != DT_LNK) continue;
163 found |= checkSymlink(fd_path + "/" + fd_de->d_name, prefix);
164 }
San Mehat586536c2010-02-16 17:12:00 -0800165 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700166
Jeff Sharkey3472e522017-10-06 18:02:53 -0600167 if (found) {
Zim75273002021-03-04 12:21:24 +0000168 if (!IsFuseDaemon(pid) || killFuseDaemon) {
169 pids.insert(pid);
170 } else {
171 LOG(WARNING) << "Found FUSE daemon with open file. Skipping...";
172 }
San Mehat586536c2010-02-16 17:12:00 -0800173 }
174 }
Jeff Sharkey3472e522017-10-06 18:02:53 -0600175 if (signal != 0) {
176 for (const auto& pid : pids) {
177 LOG(WARNING) << "Sending " << strsignal(signal) << " to " << pid;
178 kill(pid, signal);
179 }
180 }
181 return pids.size();
San Mehat586536c2010-02-16 17:12:00 -0800182}
Jeff Sharkey3472e522017-10-06 18:02:53 -0600183
184} // namespace vold
185} // namespace android