| 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 <sys/types.h> | |
| 8 #include <sys/sysctl.h> | |
| 9 #include <unistd.h> | |
| 10 | |
| 11 #include "base/logging.h" | |
| 12 #include "base/strings/string_split.h" | |
| 13 #include "base/strings/string_util.h" | |
| 14 | |
| 15 namespace base { | |
| 16 | |
| 17 ProcessIterator::ProcessIterator(const ProcessFilter* filter) | |
| 18 : index_of_kinfo_proc_(), | |
| 19 filter_(filter) { | |
| 20 | |
| 21 int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_UID, getuid() }; | |
| 22 | |
| 23 bool done = false; | |
| 24 int try_num = 1; | |
| 25 const int max_tries = 10; | |
| 26 | |
| 27 do { | |
| 28 size_t len = 0; | |
| 29 if (sysctl(mib, arraysize(mib), NULL, &len, NULL, 0) < 0) { | |
| 30 LOG(ERROR) << "failed to get the size needed for the process list"; | |
| 31 kinfo_procs_.resize(0); | |
| 32 done = true; | |
| 33 } else { | |
| 34 size_t num_of_kinfo_proc = len / sizeof(struct kinfo_proc); | |
| 35 // Leave some spare room for process table growth (more could show up | |
| 36 // between when we check and now) | |
| 37 num_of_kinfo_proc += 16; | |
| 38 kinfo_procs_.resize(num_of_kinfo_proc); | |
| 39 len = num_of_kinfo_proc * sizeof(struct kinfo_proc); | |
| 40 if (sysctl(mib, arraysize(mib), &kinfo_procs_[0], &len, NULL, 0) <0) { | |
| 41 // If we get a mem error, it just means we need a bigger buffer, so | |
| 42 // loop around again. Anything else is a real error and give up. | |
| 43 if (errno != ENOMEM) { | |
| 44 LOG(ERROR) << "failed to get the process list"; | |
| 45 kinfo_procs_.resize(0); | |
| 46 done = true; | |
| 47 } | |
| 48 } else { | |
| 49 // Got the list, just make sure we're sized exactly right | |
| 50 size_t num_of_kinfo_proc = len / sizeof(struct kinfo_proc); | |
| 51 kinfo_procs_.resize(num_of_kinfo_proc); | |
| 52 done = true; | |
| 53 } | |
| 54 } | |
| 55 } while (!done && (try_num++ < max_tries)); | |
| 56 | |
| 57 if (!done) { | |
| 58 LOG(ERROR) << "failed to collect the process list in a few tries"; | |
| 59 kinfo_procs_.resize(0); | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 ProcessIterator::~ProcessIterator() { | |
| 64 } | |
| 65 | |
| 66 bool ProcessIterator::CheckForNextProcess() { | |
| 67 std::string data; | |
| 68 | |
| 69 for (; index_of_kinfo_proc_ < kinfo_procs_.size(); ++index_of_kinfo_proc_) { | |
| 70 size_t length; | |
| 71 struct kinfo_proc kinfo = kinfo_procs_[index_of_kinfo_proc_]; | |
| 72 int mib[] = { CTL_KERN, KERN_PROC_ARGS, kinfo.ki_pid }; | |
| 73 | |
| 74 if ((kinfo.ki_pid > 0) && (kinfo.ki_stat == SZOMB)) | |
| 75 continue; | |
| 76 | |
| 77 length = 0; | |
| 78 if (sysctl(mib, arraysize(mib), NULL, &length, NULL, 0) < 0) { | |
| 79 LOG(ERROR) << "failed to figure out the buffer size for a command line"; | |
| 80 continue; | |
| 81 } | |
| 82 | |
| 83 data.resize(length); | |
| 84 | |
| 85 if (sysctl(mib, arraysize(mib), &data[0], &length, NULL, 0) < 0) { | |
| 86 LOG(ERROR) << "failed to fetch a commandline"; | |
| 87 continue; | |
| 88 } | |
| 89 | |
| 90 std::string delimiters; | |
| 91 delimiters.push_back('\0'); | |
| 92 entry_.cmd_line_args_ = | |
| 93 SplitString(data, delimiters, KEEP_WHITESPACE, SPLIT_WANT_NONEMPTY); | |
| 94 | |
| 95 size_t exec_name_end = data.find('\0'); | |
| 96 if (exec_name_end == std::string::npos) { | |
| 97 LOG(ERROR) << "command line data didn't match expected format"; | |
| 98 continue; | |
| 99 } | |
| 100 | |
| 101 entry_.pid_ = kinfo.ki_pid; | |
| 102 entry_.ppid_ = kinfo.ki_ppid; | |
| 103 entry_.gid_ = kinfo.ki_pgid; | |
| 104 | |
| 105 size_t last_slash = data.rfind('/', exec_name_end); | |
| 106 if (last_slash == std::string::npos) { | |
| 107 entry_.exe_file_.assign(data, 0, exec_name_end); | |
| 108 } else { | |
| 109 entry_.exe_file_.assign(data, last_slash + 1, | |
| 110 exec_name_end - last_slash - 1); | |
| 111 } | |
| 112 | |
| 113 // Start w/ the next entry next time through | |
| 114 ++index_of_kinfo_proc_; | |
| 115 | |
| 116 return true; | |
| 117 } | |
| 118 return false; | |
| 119 } | |
| 120 | |
| 121 bool NamedProcessIterator::IncludeEntry() { | |
| 122 if (executable_name_ != entry().exe_file()) | |
| 123 return false; | |
| 124 | |
| 125 return ProcessIterator::IncludeEntry(); | |
| 126 } | |
| 127 | |
| 128 } // namespace base | |
| OLD | NEW |