blob: 3d8e3d746c49fdaf785ea6c1ea998abbac6bee63 [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>
32#include <unordered_set>
Jeff Sharkey32ebb732017-03-27 16:18:50 -060033
34#include <android-base/file.h>
Jeff Sharkey32ebb732017-03-27 16:18:50 -060035#include <android-base/logging.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070036#include <android-base/parseint.h>
37#include <android-base/stringprintf.h>
38#include <android-base/strings.h>
San Mehat586536c2010-02-16 17:12:00 -080039
40#include "Process.h"
41
Jeff Sharkey32ebb732017-03-27 16:18:50 -060042using android::base::StringPrintf;
43
Jeff Sharkey3472e522017-10-06 18:02:53 -060044namespace android {
45namespace vold {
San Mehat586536c2010-02-16 17:12:00 -080046
Jeff Sharkey3472e522017-10-06 18:02:53 -060047static bool checkMaps(const std::string& path, const std::string& prefix) {
48 bool found = false;
Suren Baghdasaryan28af26a2019-03-26 10:00:05 -070049 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);
Jeff Sharkey3472e522017-10-06 18:02:53 -060058 std::string::size_type pos = line.find('/');
59 if (pos != std::string::npos) {
60 line = line.substr(pos);
Elliott Hughes32a5b9a2017-12-20 12:38:47 -080061 if (android::base::StartsWith(line, prefix)) {
Jeff Sharkey3472e522017-10-06 18:02:53 -060062 LOG(WARNING) << "Found map " << path << " referencing " << line;
63 found = true;
Suren Baghdasaryan28af26a2019-03-26 10:00:05 -070064 break;
San Mehat586536c2010-02-16 17:12:00 -080065 }
San Mehat586536c2010-02-16 17:12:00 -080066 }
67 }
Suren Baghdasaryan28af26a2019-03-26 10:00:05 -070068 free(buf);
69
Jeff Sharkey3472e522017-10-06 18:02:53 -060070 return found;
San Mehat586536c2010-02-16 17:12:00 -080071}
72
Jeff Sharkey3472e522017-10-06 18:02:53 -060073static bool checkSymlink(const std::string& path, const std::string& prefix) {
74 std::string res;
75 if (android::base::Readlink(path, &res)) {
Elliott Hughes32a5b9a2017-12-20 12:38:47 -080076 if (android::base::StartsWith(res, prefix)) {
Jeff Sharkey3472e522017-10-06 18:02:53 -060077 LOG(WARNING) << "Found symlink " << path << " referencing " << res;
78 return true;
San Mehat586536c2010-02-16 17:12:00 -080079 }
80 }
Jeff Sharkey3472e522017-10-06 18:02:53 -060081 return false;
San Mehat586536c2010-02-16 17:12:00 -080082}
83
Jeff Sharkey3472e522017-10-06 18:02:53 -060084int KillProcessesWithOpenFiles(const std::string& prefix, int signal) {
85 std::unordered_set<pid_t> pids;
San Mehat586536c2010-02-16 17:12:00 -080086
Jeff Sharkey3472e522017-10-06 18:02:53 -060087 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;
San Mehat586536c2010-02-16 17:12:00 -080091 }
92
Jeff Sharkey3472e522017-10-06 18:02:53 -060093 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;
Jeff Sharkey32ebb732017-03-27 16:18:50 -060099
Jeff Sharkey3472e522017-10-06 18:02:53 -0600100 // 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);
San Mehat586536c2010-02-16 17:12:00 -0800107
Jeff Sharkey3472e522017-10-06 18:02:53 -0600108 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;
San Mehat586536c2010-02-16 17:12:00 -0800112 } else {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600113 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 }
San Mehat586536c2010-02-16 17:12:00 -0800118 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700119
Jeff Sharkey3472e522017-10-06 18:02:53 -0600120 if (found) {
121 pids.insert(pid);
San Mehat586536c2010-02-16 17:12:00 -0800122 }
123 }
Jeff Sharkey3472e522017-10-06 18:02:53 -0600124 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();
San Mehat586536c2010-02-16 17:12:00 -0800131}
Jeff Sharkey3472e522017-10-06 18:02:53 -0600132
133} // namespace vold
134} // namespace android