| 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 <errno.h> | |
| 8 #include <sys/sysctl.h> | |
| 9 | |
| 10 #include "base/logging.h" | |
| 11 #include "base/strings/string_split.h" | |
| 12 #include "base/strings/string_util.h" | |
| 13 | |
| 14 namespace base { | |
| 15 | |
| 16 ProcessIterator::ProcessIterator(const ProcessFilter* filter) | |
| 17 : index_of_kinfo_proc_(), | |
| 18 filter_(filter) { | |
| 19 | |
| 20 int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_UID, getuid(), | |
| 21 sizeof(struct kinfo_proc), 0 }; | |
| 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 DLOG(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 DLOG(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 DLOG(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 for (; index_of_kinfo_proc_ < kinfo_procs_.size(); ++index_of_kinfo_proc_) { | |
| 69 kinfo_proc& kinfo = kinfo_procs_[index_of_kinfo_proc_]; | |
| 70 | |
| 71 // Skip processes just awaiting collection | |
| 72 if ((kinfo.p_pid > 0) && (kinfo.p_stat == SZOMB)) | |
| 73 continue; | |
| 74 | |
| 75 int mib[] = { CTL_KERN, KERN_PROC_ARGS, kinfo.p_pid }; | |
| 76 | |
| 77 // Find out what size buffer we need. | |
| 78 size_t data_len = 0; | |
| 79 if (sysctl(mib, arraysize(mib), NULL, &data_len, NULL, 0) < 0) { | |
| 80 DVPLOG(1) << "failed to figure out the buffer size for a commandline"; | |
| 81 continue; | |
| 82 } | |
| 83 | |
| 84 data.resize(data_len); | |
| 85 if (sysctl(mib, arraysize(mib), &data[0], &data_len, NULL, 0) < 0) { | |
| 86 DVPLOG(1) << "failed to fetch a commandline"; | |
| 87 continue; | |
| 88 } | |
| 89 | |
| 90 // |data| contains all the command line parameters of the process, separated | |
| 91 // by blocks of one or more null characters. We tokenize |data| into a | |
| 92 // vector of strings using '\0' as a delimiter and populate | |
| 93 // |entry_.cmd_line_args_|. | |
| 94 std::string delimiters; | |
| 95 delimiters.push_back('\0'); | |
| 96 entry_.cmd_line_args_ = | |
| 97 SplitString(data, delimiters, KEEP_WHITESPACE, SPLIT_WANT_NONEMPTY); | |
| 98 | |
| 99 // |data| starts with the full executable path followed by a null character. | |
| 100 // We search for the first instance of '\0' and extract everything before it | |
| 101 // to populate |entry_.exe_file_|. | |
| 102 size_t exec_name_end = data.find('\0'); | |
| 103 if (exec_name_end == std::string::npos) { | |
| 104 DLOG(ERROR) << "command line data didn't match expected format"; | |
| 105 continue; | |
| 106 } | |
| 107 | |
| 108 entry_.pid_ = kinfo.p_pid; | |
| 109 entry_.ppid_ = kinfo.p_ppid; | |
| 110 entry_.gid_ = kinfo.p__pgid; | |
| 111 size_t last_slash = data.rfind('/', exec_name_end); | |
| 112 if (last_slash == std::string::npos) | |
| 113 entry_.exe_file_.assign(data, 0, exec_name_end); | |
| 114 else | |
| 115 entry_.exe_file_.assign(data, last_slash + 1, | |
| 116 exec_name_end - last_slash - 1); | |
| 117 // Start w/ the next entry next time through | |
| 118 ++index_of_kinfo_proc_; | |
| 119 // Done | |
| 120 return true; | |
| 121 } | |
| 122 return false; | |
| 123 } | |
| 124 | |
| 125 bool NamedProcessIterator::IncludeEntry() { | |
| 126 return (executable_name_ == entry().exe_file() && | |
| 127 ProcessIterator::IncludeEntry()); | |
| 128 } | |
| 129 | |
| 130 } // namespace base | |
| OLD | NEW |