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_util.h"
17 ProcessIterator::ProcessIterator(const ProcessFilter
* filter
)
18 : index_of_kinfo_proc_(0),
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
24 int mib
[] = { CTL_KERN
, KERN_PROC
, KERN_PROC_UID
, geteuid() };
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.
30 const int max_tries
= 10;
32 // Get the size of the buffer
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);
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);
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
);
61 } while (!done
&& (try_num
++ < max_tries
));
64 DLOG(ERROR
) << "failed to collect the process list in a few tries";
65 kinfo_procs_
.resize(0);
69 ProcessIterator::~ProcessIterator() {
72 bool ProcessIterator::CheckForNextProcess() {
74 for (; index_of_kinfo_proc_
< kinfo_procs_
.size(); ++index_of_kinfo_proc_
) {
75 kinfo_proc
& kinfo
= kinfo_procs_
[index_of_kinfo_proc_
];
77 // Skip processes just awaiting collection
78 if ((kinfo
.kp_proc
.p_pid
> 0) && (kinfo
.kp_proc
.p_stat
== SZOMB
))
81 int mib
[] = { CTL_KERN
, KERN_PROCARGS
, kinfo
.kp_proc
.p_pid
};
83 // Find out what size buffer we need.
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";
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";
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_
);
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";
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
);
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_
;
130 bool NamedProcessIterator::IncludeEntry() {
131 return (executable_name_
== entry().exe_file() &&
132 ProcessIterator::IncludeEntry());