| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/process/process_iterator.h" | |
| 6 | |
| 7 #include "base/files/file_util.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/process/internal_linux.h" | |
| 10 #include "base/strings/string_split.h" | |
| 11 #include "base/strings/string_util.h" | |
| 12 #include "base/threading/thread_restrictions.h" | |
| 13 | |
| 14 namespace base { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 // Reads the |field_num|th field from |proc_stats|. | |
| 19 // Returns an empty string on failure. | |
| 20 // This version only handles VM_COMM and VM_STATE, which are the only fields | |
| 21 // that are strings. | |
| 22 std::string GetProcStatsFieldAsString( | |
| 23 const std::vector<std::string>& proc_stats, | |
| 24 internal::ProcStatsFields field_num) { | |
| 25 if (field_num < internal::VM_COMM || field_num > internal::VM_STATE) { | |
| 26 NOTREACHED(); | |
| 27 return std::string(); | |
| 28 } | |
| 29 | |
| 30 if (proc_stats.size() > static_cast<size_t>(field_num)) | |
| 31 return proc_stats[field_num]; | |
| 32 | |
| 33 NOTREACHED(); | |
| 34 return 0; | |
| 35 } | |
| 36 | |
| 37 // Reads /proc/<pid>/cmdline and populates |proc_cmd_line_args| with the command | |
| 38 // line arguments. Returns true if successful. | |
| 39 // Note: /proc/<pid>/cmdline contains command line arguments separated by single | |
| 40 // null characters. We tokenize it into a vector of strings using '\0' as a | |
| 41 // delimiter. | |
| 42 bool GetProcCmdline(pid_t pid, std::vector<std::string>* proc_cmd_line_args) { | |
| 43 // Synchronously reading files in /proc is safe. | |
| 44 ThreadRestrictions::ScopedAllowIO allow_io; | |
| 45 | |
| 46 FilePath cmd_line_file = internal::GetProcPidDir(pid).Append("cmdline"); | |
| 47 std::string cmd_line; | |
| 48 if (!ReadFileToString(cmd_line_file, &cmd_line)) | |
| 49 return false; | |
| 50 std::string delimiters; | |
| 51 delimiters.push_back('\0'); | |
| 52 *proc_cmd_line_args = | |
| 53 SplitString(cmd_line, delimiters, KEEP_WHITESPACE, SPLIT_WANT_NONEMPTY); | |
| 54 return true; | |
| 55 } | |
| 56 | |
| 57 } // namespace | |
| 58 | |
| 59 ProcessIterator::ProcessIterator(const ProcessFilter* filter) | |
| 60 : filter_(filter) { | |
| 61 procfs_dir_ = opendir(internal::kProcDir); | |
| 62 } | |
| 63 | |
| 64 ProcessIterator::~ProcessIterator() { | |
| 65 if (procfs_dir_) { | |
| 66 closedir(procfs_dir_); | |
| 67 procfs_dir_ = NULL; | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 bool ProcessIterator::CheckForNextProcess() { | |
| 72 // TODO(port): skip processes owned by different UID | |
| 73 | |
| 74 pid_t pid = kNullProcessId; | |
| 75 std::vector<std::string> cmd_line_args; | |
| 76 std::string stats_data; | |
| 77 std::vector<std::string> proc_stats; | |
| 78 | |
| 79 // Arbitrarily guess that there will never be more than 200 non-process | |
| 80 // files in /proc. Hardy has 53 and Lucid has 61. | |
| 81 int skipped = 0; | |
| 82 const int kSkipLimit = 200; | |
| 83 while (skipped < kSkipLimit) { | |
| 84 dirent* slot = readdir(procfs_dir_); | |
| 85 // all done looking through /proc? | |
| 86 if (!slot) | |
| 87 return false; | |
| 88 | |
| 89 // If not a process, keep looking for one. | |
| 90 pid = internal::ProcDirSlotToPid(slot->d_name); | |
| 91 if (!pid) { | |
| 92 skipped++; | |
| 93 continue; | |
| 94 } | |
| 95 | |
| 96 if (!GetProcCmdline(pid, &cmd_line_args)) | |
| 97 continue; | |
| 98 | |
| 99 if (!internal::ReadProcStats(pid, &stats_data)) | |
| 100 continue; | |
| 101 if (!internal::ParseProcStats(stats_data, &proc_stats)) | |
| 102 continue; | |
| 103 | |
| 104 std::string runstate = | |
| 105 GetProcStatsFieldAsString(proc_stats, internal::VM_STATE); | |
| 106 if (runstate.size() != 1) { | |
| 107 NOTREACHED(); | |
| 108 continue; | |
| 109 } | |
| 110 | |
| 111 // Is the process in 'Zombie' state, i.e. dead but waiting to be reaped? | |
| 112 // Allowed values: D R S T Z | |
| 113 if (runstate[0] != 'Z') | |
| 114 break; | |
| 115 | |
| 116 // Nope, it's a zombie; somebody isn't cleaning up after their children. | |
| 117 // (e.g. WaitForProcessesToExit doesn't clean up after dead children yet.) | |
| 118 // There could be a lot of zombies, can't really decrement i here. | |
| 119 } | |
| 120 if (skipped >= kSkipLimit) { | |
| 121 NOTREACHED(); | |
| 122 return false; | |
| 123 } | |
| 124 | |
| 125 entry_.pid_ = pid; | |
| 126 entry_.ppid_ = GetProcStatsFieldAsInt64(proc_stats, internal::VM_PPID); | |
| 127 entry_.gid_ = GetProcStatsFieldAsInt64(proc_stats, internal::VM_PGRP); | |
| 128 entry_.cmd_line_args_.assign(cmd_line_args.begin(), cmd_line_args.end()); | |
| 129 entry_.exe_file_ = GetProcessExecutablePath(pid).BaseName().value(); | |
| 130 return true; | |
| 131 } | |
| 132 | |
| 133 bool NamedProcessIterator::IncludeEntry() { | |
| 134 if (executable_name_ != entry().exe_file()) | |
| 135 return false; | |
| 136 return ProcessIterator::IncludeEntry(); | |
| 137 } | |
| 138 | |
| 139 } // namespace base | |
| OLD | NEW |