blob: 3d8e3d746c49fdaf785ea6c1ea998abbac6bee63 [file] [log] [blame]
bigbiff7ba75002020-04-11 20:47:09 -04001/*
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 <unordered_set>
33
34#include <android-base/file.h>
35#include <android-base/logging.h>
36#include <android-base/parseint.h>
37#include <android-base/stringprintf.h>
38#include <android-base/strings.h>
39
40#include "Process.h"
41
42using android::base::StringPrintf;
43
44namespace android {
45namespace vold {
46
47static bool checkMaps(const std::string& path, const std::string& prefix) {
48 bool found = false;
49 auto file = std::unique_ptr<FILE, decltype(&fclose)>{fopen(path.c_str(), "re"), fclose};
50 if (!file) {
51 return false;
52 }
53
54 char* buf = nullptr;
55 size_t len = 0;
56 while (getline(&buf, &len, file.get()) != -1) {
57 std::string line(buf);
58 std::string::size_type pos = line.find('/');
59 if (pos != std::string::npos) {
60 line = line.substr(pos);
61 if (android::base::StartsWith(line, prefix)) {
62 LOG(WARNING) << "Found map " << path << " referencing " << line;
63 found = true;
64 break;
65 }
66 }
67 }
68 free(buf);
69
70 return found;
71}
72
73static bool checkSymlink(const std::string& path, const std::string& prefix) {
74 std::string res;
75 if (android::base::Readlink(path, &res)) {
76 if (android::base::StartsWith(res, prefix)) {
77 LOG(WARNING) << "Found symlink " << path << " referencing " << res;
78 return true;
79 }
80 }
81 return false;
82}
83
84int KillProcessesWithOpenFiles(const std::string& prefix, int signal) {
85 std::unordered_set<pid_t> pids;
86
87 auto proc_d = std::unique_ptr<DIR, int (*)(DIR*)>(opendir("/proc"), closedir);
88 if (!proc_d) {
89 PLOG(ERROR) << "Failed to open proc";
90 return -1;
91 }
92
93 struct dirent* proc_de;
94 while ((proc_de = readdir(proc_d.get())) != nullptr) {
95 // We only care about valid PIDs
96 pid_t pid;
97 if (proc_de->d_type != DT_DIR) continue;
98 if (!android::base::ParseInt(proc_de->d_name, &pid)) continue;
99
100 // Look for references to prefix
101 bool found = false;
102 auto path = StringPrintf("/proc/%d", pid);
103 found |= checkMaps(path + "/maps", prefix);
104 found |= checkSymlink(path + "/cwd", prefix);
105 found |= checkSymlink(path + "/root", prefix);
106 found |= checkSymlink(path + "/exe", prefix);
107
108 auto fd_path = path + "/fd";
109 auto fd_d = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(fd_path.c_str()), closedir);
110 if (!fd_d) {
111 PLOG(WARNING) << "Failed to open " << fd_path;
112 } else {
113 struct dirent* fd_de;
114 while ((fd_de = readdir(fd_d.get())) != nullptr) {
115 if (fd_de->d_type != DT_LNK) continue;
116 found |= checkSymlink(fd_path + "/" + fd_de->d_name, prefix);
117 }
118 }
119
120 if (found) {
121 pids.insert(pid);
122 }
123 }
124 if (signal != 0) {
125 for (const auto& pid : pids) {
126 LOG(WARNING) << "Sending " << strsignal(signal) << " to " << pid;
127 kill(pid, signal);
128 }
129 }
130 return pids.size();
131}
132
133} // namespace vold
134} // namespace android