blob: a5028f27f09c5caafd6a9b12f343efd7d53d7a22 [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;
49 std::ifstream infile(path);
50 std::string line;
51 while (std::getline(infile, line)) {
52 std::string::size_type pos = line.find('/');
53 if (pos != std::string::npos) {
54 line = line.substr(pos);
Elliott Hughes32a5b9a2017-12-20 12:38:47 -080055 if (android::base::StartsWith(line, prefix)) {
Jeff Sharkey3472e522017-10-06 18:02:53 -060056 LOG(WARNING) << "Found map " << path << " referencing " << line;
57 found = true;
San Mehat586536c2010-02-16 17:12:00 -080058 }
San Mehat586536c2010-02-16 17:12:00 -080059 }
60 }
Jeff Sharkey3472e522017-10-06 18:02:53 -060061 return found;
San Mehat586536c2010-02-16 17:12:00 -080062}
63
Jeff Sharkey3472e522017-10-06 18:02:53 -060064static bool checkSymlink(const std::string& path, const std::string& prefix) {
65 std::string res;
66 if (android::base::Readlink(path, &res)) {
Elliott Hughes32a5b9a2017-12-20 12:38:47 -080067 if (android::base::StartsWith(res, prefix)) {
Jeff Sharkey3472e522017-10-06 18:02:53 -060068 LOG(WARNING) << "Found symlink " << path << " referencing " << res;
69 return true;
San Mehat586536c2010-02-16 17:12:00 -080070 }
71 }
Jeff Sharkey3472e522017-10-06 18:02:53 -060072 return false;
San Mehat586536c2010-02-16 17:12:00 -080073}
74
Jeff Sharkey3472e522017-10-06 18:02:53 -060075int KillProcessesWithOpenFiles(const std::string& prefix, int signal) {
76 std::unordered_set<pid_t> pids;
San Mehat586536c2010-02-16 17:12:00 -080077
Jeff Sharkey3472e522017-10-06 18:02:53 -060078 auto proc_d = std::unique_ptr<DIR, int (*)(DIR*)>(opendir("/proc"), closedir);
79 if (!proc_d) {
80 PLOG(ERROR) << "Failed to open proc";
81 return -1;
San Mehat586536c2010-02-16 17:12:00 -080082 }
83
Jeff Sharkey3472e522017-10-06 18:02:53 -060084 struct dirent* proc_de;
85 while ((proc_de = readdir(proc_d.get())) != nullptr) {
86 // We only care about valid PIDs
87 pid_t pid;
88 if (proc_de->d_type != DT_DIR) continue;
89 if (!android::base::ParseInt(proc_de->d_name, &pid)) continue;
Jeff Sharkey32ebb732017-03-27 16:18:50 -060090
Jeff Sharkey3472e522017-10-06 18:02:53 -060091 // Look for references to prefix
92 bool found = false;
93 auto path = StringPrintf("/proc/%d", pid);
94 found |= checkMaps(path + "/maps", prefix);
95 found |= checkSymlink(path + "/cwd", prefix);
96 found |= checkSymlink(path + "/root", prefix);
97 found |= checkSymlink(path + "/exe", prefix);
San Mehat586536c2010-02-16 17:12:00 -080098
Jeff Sharkey3472e522017-10-06 18:02:53 -060099 auto fd_path = path + "/fd";
100 auto fd_d = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(fd_path.c_str()), closedir);
101 if (!fd_d) {
102 PLOG(WARNING) << "Failed to open " << fd_path;
San Mehat586536c2010-02-16 17:12:00 -0800103 } else {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600104 struct dirent* fd_de;
105 while ((fd_de = readdir(fd_d.get())) != nullptr) {
106 if (fd_de->d_type != DT_LNK) continue;
107 found |= checkSymlink(fd_path + "/" + fd_de->d_name, prefix);
108 }
San Mehat586536c2010-02-16 17:12:00 -0800109 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700110
Jeff Sharkey3472e522017-10-06 18:02:53 -0600111 if (found) {
112 pids.insert(pid);
San Mehat586536c2010-02-16 17:12:00 -0800113 }
114 }
Jeff Sharkey3472e522017-10-06 18:02:53 -0600115 if (signal != 0) {
116 for (const auto& pid : pids) {
117 LOG(WARNING) << "Sending " << strsignal(signal) << " to " << pid;
118 kill(pid, signal);
119 }
120 }
121 return pids.size();
San Mehat586536c2010-02-16 17:12:00 -0800122}
Jeff Sharkey3472e522017-10-06 18:02:53 -0600123
124} // namespace vold
125} // namespace android