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"
8 #include <sys/sysctl.h>
12 #include "base/logging.h"
13 #include "base/strings/string_split.h"
14 #include "base/strings/string_util.h"
18 ProcessIterator::ProcessIterator(const ProcessFilter
* filter
)
19 : index_of_kinfo_proc_(0),
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
25 int mib
[] = { CTL_KERN
, KERN_PROC
, KERN_PROC_UID
, geteuid() };
27 // Since more processes could start between when we get the size and when
28 // we get the list, we do a loop to keep trying until we get it.
31 const int max_tries
= 10;
33 // Get the size of the buffer
35 if (sysctl(mib
, arraysize(mib
), NULL
, &len
, NULL
, 0) < 0) {
36 DLOG(ERROR
) << "failed to get the size needed for the process list";
37 kinfo_procs_
.resize(0);
40 size_t num_of_kinfo_proc
= len
/ sizeof(struct kinfo_proc
);
41 // Leave some spare room for process table growth (more could show up
42 // between when we check and now)
43 num_of_kinfo_proc
+= 16;
44 kinfo_procs_
.resize(num_of_kinfo_proc
);
45 len
= num_of_kinfo_proc
* sizeof(struct kinfo_proc
);
46 // Load the list of processes
47 if (sysctl(mib
, arraysize(mib
), &kinfo_procs_
[0], &len
, NULL
, 0) < 0) {
48 // If we get a mem error, it just means we need a bigger buffer, so
49 // loop around again. Anything else is a real error and give up.
50 if (errno
!= ENOMEM
) {
51 DLOG(ERROR
) << "failed to get the process list";
52 kinfo_procs_
.resize(0);
56 // Got the list, just make sure we're sized exactly right
57 size_t num_of_kinfo_proc
= len
/ sizeof(struct kinfo_proc
);
58 kinfo_procs_
.resize(num_of_kinfo_proc
);
62 } while (!done
&& (try_num
++ < max_tries
));
65 DLOG(ERROR
) << "failed to collect the process list in a few tries";
66 kinfo_procs_
.resize(0);
70 ProcessIterator::~ProcessIterator() {
73 bool ProcessIterator::CheckForNextProcess() {
75 for (; index_of_kinfo_proc_
< kinfo_procs_
.size(); ++index_of_kinfo_proc_
) {
76 kinfo_proc
& kinfo
= kinfo_procs_
[index_of_kinfo_proc_
];
78 // Skip processes just awaiting collection
79 if ((kinfo
.kp_proc
.p_pid
> 0) && (kinfo
.kp_proc
.p_stat
== SZOMB
))
82 int mib
[] = { CTL_KERN
, KERN_PROCARGS
, kinfo
.kp_proc
.p_pid
};
84 // Find out what size buffer we need.
86 if (sysctl(mib
, arraysize(mib
), NULL
, &data_len
, NULL
, 0) < 0) {
87 DVPLOG(1) << "failed to figure out the buffer size for a commandline";
91 data
.resize(data_len
);
92 if (sysctl(mib
, arraysize(mib
), &data
[0], &data_len
, NULL
, 0) < 0) {
93 DVPLOG(1) << "failed to fetch a commandline";
97 // |data| contains all the command line parameters of the process, separated
98 // by blocks of one or more null characters. We tokenize |data| into a
99 // vector of strings using '\0' as a delimiter and populate
100 // |entry_.cmd_line_args_|.
101 std::string delimiters
;
102 delimiters
.push_back('\0');
103 entry_
.cmd_line_args_
= SplitString(data
, delimiters
,
104 KEEP_WHITESPACE
, SPLIT_WANT_NONEMPTY
);
106 // |data| starts with the full executable path followed by a null character.
107 // We search for the first instance of '\0' and extract everything before it
108 // to populate |entry_.exe_file_|.
109 size_t exec_name_end
= data
.find('\0');
110 if (exec_name_end
== std::string::npos
) {
111 DLOG(ERROR
) << "command line data didn't match expected format";
115 entry_
.pid_
= kinfo
.kp_proc
.p_pid
;
116 entry_
.ppid_
= kinfo
.kp_eproc
.e_ppid
;
117 entry_
.gid_
= kinfo
.kp_eproc
.e_pgid
;
118 size_t last_slash
= data
.rfind('/', exec_name_end
);
119 if (last_slash
== std::string::npos
)
120 entry_
.exe_file_
.assign(data
, 0, exec_name_end
);
122 entry_
.exe_file_
.assign(data
, last_slash
+ 1,
123 exec_name_end
- last_slash
- 1);
124 // Start w/ the next entry next time through
125 ++index_of_kinfo_proc_
;
132 bool NamedProcessIterator::IncludeEntry() {
133 return (executable_name_
== entry().exe_file() &&
134 ProcessIterator::IncludeEntry());