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