blob: e7978fa8d07c34cab8d6939f7f3e818f6dbe5c63 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2013 The Chromium Authors
[email protected]d3acb672013-05-31 15:38:012// 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>
avibeced7c2015-12-24 06:47:598#include <stddef.h>
[email protected]d3acb672013-05-31 15:38:019#include <sys/sysctl.h>
10
[email protected]d3acb672013-05-31 15:38:0111#include "base/logging.h"
brettw37fd9d7d2015-06-24 02:07:1612#include "base/strings/string_split.h"
[email protected]d1a5a2f2013-06-10 21:17:4013#include "base/strings/string_util.h"
[email protected]d3acb672013-05-31 15:38:0114
15namespace base {
16
17ProcessIterator::ProcessIterator(const ProcessFilter* filter)
18 : index_of_kinfo_proc_(),
19 filter_(filter) {
20
21 int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_UID, getuid(),
22 sizeof(struct kinfo_proc), 0 };
23
24 bool done = false;
25 int try_num = 1;
26 const int max_tries = 10;
27
28 do {
29 size_t len = 0;
Daniel Chengf45f47602022-02-28 22:38:3230 if (sysctl(mib, std::size(mib), NULL, &len, NULL, 0) < 0) {
[email protected]d3acb672013-05-31 15:38:0131 DLOG(ERROR) << "failed to get the size needed for the process list";
32 kinfo_procs_.resize(0);
33 done = true;
34 } else {
35 size_t num_of_kinfo_proc = len / sizeof(struct kinfo_proc);
36 // Leave some spare room for process table growth (more could show up
37 // between when we check and now)
38 num_of_kinfo_proc += 16;
39 kinfo_procs_.resize(num_of_kinfo_proc);
40 len = num_of_kinfo_proc * sizeof(struct kinfo_proc);
Daniel Chengf45f47602022-02-28 22:38:3241 if (sysctl(mib, std::size(mib), &kinfo_procs_[0], &len, NULL, 0) < 0) {
[email protected]d3acb672013-05-31 15:38:0142 // If we get a mem error, it just means we need a bigger buffer, so
43 // loop around again. Anything else is a real error and give up.
44 if (errno != ENOMEM) {
45 DLOG(ERROR) << "failed to get the process list";
46 kinfo_procs_.resize(0);
47 done = true;
48 }
49 } else {
50 // Got the list, just make sure we're sized exactly right
51 size_t num_of_kinfo_proc = len / sizeof(struct kinfo_proc);
52 kinfo_procs_.resize(num_of_kinfo_proc);
53 done = true;
54 }
55 }
56 } while (!done && (try_num++ < max_tries));
57
58 if (!done) {
59 DLOG(ERROR) << "failed to collect the process list in a few tries";
60 kinfo_procs_.resize(0);
61 }
62}
63
64ProcessIterator::~ProcessIterator() {
65}
66
67bool ProcessIterator::CheckForNextProcess() {
68 std::string data;
69 for (; index_of_kinfo_proc_ < kinfo_procs_.size(); ++index_of_kinfo_proc_) {
70 kinfo_proc& kinfo = kinfo_procs_[index_of_kinfo_proc_];
71
72 // Skip processes just awaiting collection
73 if ((kinfo.p_pid > 0) && (kinfo.p_stat == SZOMB))
74 continue;
75
76 int mib[] = { CTL_KERN, KERN_PROC_ARGS, kinfo.p_pid };
77
78 // Find out what size buffer we need.
79 size_t data_len = 0;
Daniel Chengf45f47602022-02-28 22:38:3280 if (sysctl(mib, std::size(mib), NULL, &data_len, NULL, 0) < 0) {
[email protected]d3acb672013-05-31 15:38:0181 DVPLOG(1) << "failed to figure out the buffer size for a commandline";
82 continue;
83 }
84
85 data.resize(data_len);
Daniel Chengf45f47602022-02-28 22:38:3286 if (sysctl(mib, std::size(mib), &data[0], &data_len, NULL, 0) < 0) {
[email protected]d3acb672013-05-31 15:38:0187 DVPLOG(1) << "failed to fetch a commandline";
88 continue;
89 }
90
91 // |data| contains all the command line parameters of the process, separated
92 // by blocks of one or more null characters. We tokenize |data| into a
93 // vector of strings using '\0' as a delimiter and populate
94 // |entry_.cmd_line_args_|.
95 std::string delimiters;
96 delimiters.push_back('\0');
brettw37fd9d7d2015-06-24 02:07:1697 entry_.cmd_line_args_ = SplitString(data, delimiters, KEEP_WHITESPACE,
98 SPLIT_WANT_NONEMPTY);
[email protected]d3acb672013-05-31 15:38:0199
100 // |data| starts with the full executable path followed by a null character.
101 // We search for the first instance of '\0' and extract everything before it
102 // to populate |entry_.exe_file_|.
103 size_t exec_name_end = data.find('\0');
104 if (exec_name_end == std::string::npos) {
105 DLOG(ERROR) << "command line data didn't match expected format";
106 continue;
107 }
108
109 entry_.pid_ = kinfo.p_pid;
110 entry_.ppid_ = kinfo.p_ppid;
111 entry_.gid_ = kinfo.p__pgid;
112 size_t last_slash = data.rfind('/', exec_name_end);
113 if (last_slash == std::string::npos)
114 entry_.exe_file_.assign(data, 0, exec_name_end);
115 else
116 entry_.exe_file_.assign(data, last_slash + 1,
117 exec_name_end - last_slash - 1);
118 // Start w/ the next entry next time through
119 ++index_of_kinfo_proc_;
120 // Done
121 return true;
122 }
123 return false;
124}
125
126bool NamedProcessIterator::IncludeEntry() {
127 return (executable_name_ == entry().exe_file() &&
128 ProcessIterator::IncludeEntry());
129}
130
131} // namespace base