Process.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #include <ctype.h>
  17. #include <dirent.h>
  18. #include <errno.h>
  19. #include <fcntl.h>
  20. #include <fts.h>
  21. #include <poll.h>
  22. #include <pwd.h>
  23. #include <signal.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <sys/stat.h>
  28. #include <unistd.h>
  29. #include <fstream>
  30. #include <unordered_set>
  31. #include <android-base/file.h>
  32. #include <android-base/logging.h>
  33. #include <android-base/parseint.h>
  34. #include <android-base/stringprintf.h>
  35. #include <android-base/strings.h>
  36. #include "Process.h"
  37. using android::base::StringPrintf;
  38. namespace android {
  39. namespace vold {
  40. static bool checkMaps(const std::string& path, const std::string& prefix) {
  41. bool found = false;
  42. auto file = std::unique_ptr<FILE, decltype(&fclose)>{fopen(path.c_str(), "re"), fclose};
  43. if (!file) {
  44. return false;
  45. }
  46. char* buf = nullptr;
  47. size_t len = 0;
  48. while (getline(&buf, &len, file.get()) != -1) {
  49. std::string line(buf);
  50. std::string::size_type pos = line.find('/');
  51. if (pos != std::string::npos) {
  52. line = line.substr(pos);
  53. if (android::base::StartsWith(line, prefix)) {
  54. LOG(WARNING) << "Found map " << path << " referencing " << line;
  55. found = true;
  56. break;
  57. }
  58. }
  59. }
  60. free(buf);
  61. return found;
  62. }
  63. static bool checkSymlink(const std::string& path, const std::string& prefix) {
  64. std::string res;
  65. if (android::base::Readlink(path, &res)) {
  66. if (android::base::StartsWith(res, prefix)) {
  67. LOG(WARNING) << "Found symlink " << path << " referencing " << res;
  68. return true;
  69. }
  70. }
  71. return false;
  72. }
  73. int KillProcessesWithOpenFiles(const std::string& prefix, int signal) {
  74. std::unordered_set<pid_t> pids;
  75. auto proc_d = std::unique_ptr<DIR, int (*)(DIR*)>(opendir("/proc"), closedir);
  76. if (!proc_d) {
  77. PLOG(ERROR) << "Failed to open proc";
  78. return -1;
  79. }
  80. struct dirent* proc_de;
  81. while ((proc_de = readdir(proc_d.get())) != nullptr) {
  82. // We only care about valid PIDs
  83. pid_t pid;
  84. if (proc_de->d_type != DT_DIR) continue;
  85. if (!android::base::ParseInt(proc_de->d_name, &pid)) continue;
  86. // Look for references to prefix
  87. bool found = false;
  88. auto path = StringPrintf("/proc/%d", pid);
  89. found |= checkMaps(path + "/maps", prefix);
  90. found |= checkSymlink(path + "/cwd", prefix);
  91. found |= checkSymlink(path + "/root", prefix);
  92. found |= checkSymlink(path + "/exe", prefix);
  93. auto fd_path = path + "/fd";
  94. auto fd_d = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(fd_path.c_str()), closedir);
  95. if (!fd_d) {
  96. PLOG(WARNING) << "Failed to open " << fd_path;
  97. } else {
  98. struct dirent* fd_de;
  99. while ((fd_de = readdir(fd_d.get())) != nullptr) {
  100. if (fd_de->d_type != DT_LNK) continue;
  101. found |= checkSymlink(fd_path + "/" + fd_de->d_name, prefix);
  102. }
  103. }
  104. if (found) {
  105. pids.insert(pid);
  106. }
  107. }
  108. if (signal != 0) {
  109. for (const auto& pid : pids) {
  110. LOG(WARNING) << "Sending " << strsignal(signal) << " to " << pid;
  111. kill(pid, signal);
  112. }
  113. }
  114. return pids.size();
  115. }
  116. } // namespace vold
  117. } // namespace android