Don't assume that uid_t is int
[chromium-blink-merge.git] / base / process / process_iterator_mac.cc
blobd9136f48b039c393f9e76273243df340765cf71a
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.
5 #include "base/process/process_iterator.h"
7 #include <errno.h>
8 #include <sys/sysctl.h>
9 #include <sys/types.h>
10 #include <unistd.h>
12 #include "base/logging.h"
13 #include "base/strings/string_split.h"
14 #include "base/strings/string_util.h"
16 namespace base {
18 ProcessIterator::ProcessIterator(const ProcessFilter* filter)
19 : index_of_kinfo_proc_(0),
20 filter_(filter) {
21 // Get a snapshot of all of my processes (yes, as we loop it can go stale, but
22 // but trying to find where we were in a constantly changing list is basically
23 // impossible.
25 int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_UID,
26 static_cast<int>(geteuid()) };
28 // Since more processes could start between when we get the size and when
29 // we get the list, we do a loop to keep trying until we get it.
30 bool done = false;
31 int try_num = 1;
32 const int max_tries = 10;
33 do {
34 // Get the size of the buffer
35 size_t len = 0;
36 if (sysctl(mib, arraysize(mib), NULL, &len, NULL, 0) < 0) {
37 DLOG(ERROR) << "failed to get the size needed for the process list";
38 kinfo_procs_.resize(0);
39 done = true;
40 } else {
41 size_t num_of_kinfo_proc = len / sizeof(struct kinfo_proc);
42 // Leave some spare room for process table growth (more could show up
43 // between when we check and now)
44 num_of_kinfo_proc += 16;
45 kinfo_procs_.resize(num_of_kinfo_proc);
46 len = num_of_kinfo_proc * sizeof(struct kinfo_proc);
47 // Load the list of processes
48 if (sysctl(mib, arraysize(mib), &kinfo_procs_[0], &len, NULL, 0) < 0) {
49 // If we get a mem error, it just means we need a bigger buffer, so
50 // loop around again. Anything else is a real error and give up.
51 if (errno != ENOMEM) {
52 DLOG(ERROR) << "failed to get the process list";
53 kinfo_procs_.resize(0);
54 done = true;
56 } else {
57 // Got the list, just make sure we're sized exactly right
58 size_t num_of_kinfo_proc = len / sizeof(struct kinfo_proc);
59 kinfo_procs_.resize(num_of_kinfo_proc);
60 done = true;
63 } while (!done && (try_num++ < max_tries));
65 if (!done) {
66 DLOG(ERROR) << "failed to collect the process list in a few tries";
67 kinfo_procs_.resize(0);
71 ProcessIterator::~ProcessIterator() {
74 bool ProcessIterator::CheckForNextProcess() {
75 std::string data;
76 for (; index_of_kinfo_proc_ < kinfo_procs_.size(); ++index_of_kinfo_proc_) {
77 kinfo_proc& kinfo = kinfo_procs_[index_of_kinfo_proc_];
79 // Skip processes just awaiting collection
80 if ((kinfo.kp_proc.p_pid > 0) && (kinfo.kp_proc.p_stat == SZOMB))
81 continue;
83 int mib[] = { CTL_KERN, KERN_PROCARGS, kinfo.kp_proc.p_pid };
85 // Find out what size buffer we need.
86 size_t data_len = 0;
87 if (sysctl(mib, arraysize(mib), NULL, &data_len, NULL, 0) < 0) {
88 DVPLOG(1) << "failed to figure out the buffer size for a commandline";
89 continue;
92 data.resize(data_len);
93 if (sysctl(mib, arraysize(mib), &data[0], &data_len, NULL, 0) < 0) {
94 DVPLOG(1) << "failed to fetch a commandline";
95 continue;
98 // |data| contains all the command line parameters of the process, separated
99 // by blocks of one or more null characters. We tokenize |data| into a
100 // vector of strings using '\0' as a delimiter and populate
101 // |entry_.cmd_line_args_|.
102 std::string delimiters;
103 delimiters.push_back('\0');
104 entry_.cmd_line_args_ = SplitString(data, delimiters,
105 KEEP_WHITESPACE, SPLIT_WANT_NONEMPTY);
107 // |data| starts with the full executable path followed by a null character.
108 // We search for the first instance of '\0' and extract everything before it
109 // to populate |entry_.exe_file_|.
110 size_t exec_name_end = data.find('\0');
111 if (exec_name_end == std::string::npos) {
112 DLOG(ERROR) << "command line data didn't match expected format";
113 continue;
116 entry_.pid_ = kinfo.kp_proc.p_pid;
117 entry_.ppid_ = kinfo.kp_eproc.e_ppid;
118 entry_.gid_ = kinfo.kp_eproc.e_pgid;
119 size_t last_slash = data.rfind('/', exec_name_end);
120 if (last_slash == std::string::npos)
121 entry_.exe_file_.assign(data, 0, exec_name_end);
122 else
123 entry_.exe_file_.assign(data, last_slash + 1,
124 exec_name_end - last_slash - 1);
125 // Start w/ the next entry next time through
126 ++index_of_kinfo_proc_;
127 // Done
128 return true;
130 return false;
133 bool NamedProcessIterator::IncludeEntry() {
134 return (executable_name_ == entry().exe_file() &&
135 ProcessIterator::IncludeEntry());
138 } // namespace base