blob: 74fdfd8df7c4d699ed0efcb692338c8654df88ac [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
11#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)
Lei Zhang6f418e02023-02-08 21:55:4318 : filter_(filter) {
Peter Kasting134ef9af2024-12-28 02:30:0919 int mib[] = {
20 CTL_KERN, KERN_PROC, KERN_PROC_UID, getuid(), sizeof(struct kinfo_proc),
21 0};
[email protected]d3acb672013-05-31 15:38:0122
23 bool done = false;
24 int try_num = 1;
25 const int max_tries = 10;
26
27 do {
28 size_t len = 0;
Daniel Chengf45f47602022-02-28 22:38:3229 if (sysctl(mib, std::size(mib), NULL, &len, NULL, 0) < 0) {
[email protected]d3acb672013-05-31 15:38:0130 DLOG(ERROR) << "failed to get the size needed for the process list";
31 kinfo_procs_.resize(0);
32 done = true;
33 } else {
34 size_t num_of_kinfo_proc = len / sizeof(struct kinfo_proc);
35 // Leave some spare room for process table growth (more could show up
36 // between when we check and now)
37 num_of_kinfo_proc += 16;
38 kinfo_procs_.resize(num_of_kinfo_proc);
39 len = num_of_kinfo_proc * sizeof(struct kinfo_proc);
Daniel Chengf45f47602022-02-28 22:38:3240 if (sysctl(mib, std::size(mib), &kinfo_procs_[0], &len, NULL, 0) < 0) {
[email protected]d3acb672013-05-31 15:38:0141 // If we get a mem error, it just means we need a bigger buffer, so
42 // loop around again. Anything else is a real error and give up.
43 if (errno != ENOMEM) {
44 DLOG(ERROR) << "failed to get the process list";
45 kinfo_procs_.resize(0);
46 done = true;
47 }
48 } else {
49 // Got the list, just make sure we're sized exactly right
50 size_t num_of_kinfo_proc = len / sizeof(struct kinfo_proc);
51 kinfo_procs_.resize(num_of_kinfo_proc);
52 done = true;
53 }
54 }
55 } while (!done && (try_num++ < max_tries));
56
57 if (!done) {
58 DLOG(ERROR) << "failed to collect the process list in a few tries";
59 kinfo_procs_.resize(0);
60 }
61}
62
Lei Zhang6f418e02023-02-08 21:55:4363ProcessIterator::~ProcessIterator() = default;
[email protected]d3acb672013-05-31 15:38:0164
65bool ProcessIterator::CheckForNextProcess() {
66 std::string data;
67 for (; index_of_kinfo_proc_ < kinfo_procs_.size(); ++index_of_kinfo_proc_) {
68 kinfo_proc& kinfo = kinfo_procs_[index_of_kinfo_proc_];
69
70 // Skip processes just awaiting collection
Peter Kasting134ef9af2024-12-28 02:30:0971 if ((kinfo.p_pid > 0) && (kinfo.p_stat == SZOMB)) {
[email protected]d3acb672013-05-31 15:38:0172 continue;
Peter Kasting134ef9af2024-12-28 02:30:0973 }
[email protected]d3acb672013-05-31 15:38:0174
Peter Kasting134ef9af2024-12-28 02:30:0975 int mib[] = {CTL_KERN, KERN_PROC_ARGS, kinfo.p_pid};
[email protected]d3acb672013-05-31 15:38:0176
77 // Find out what size buffer we need.
78 size_t data_len = 0;
Daniel Chengf45f47602022-02-28 22:38:3279 if (sysctl(mib, std::size(mib), NULL, &data_len, NULL, 0) < 0) {
[email protected]d3acb672013-05-31 15:38:0180 DVPLOG(1) << "failed to figure out the buffer size for a commandline";
81 continue;
82 }
83
84 data.resize(data_len);
Daniel Chengf45f47602022-02-28 22:38:3285 if (sysctl(mib, std::size(mib), &data[0], &data_len, NULL, 0) < 0) {
[email protected]d3acb672013-05-31 15:38:0186 DVPLOG(1) << "failed to fetch a commandline";
87 continue;
88 }
89
90 // |data| contains all the command line parameters of the process, separated
91 // by blocks of one or more null characters. We tokenize |data| into a
92 // vector of strings using '\0' as a delimiter and populate
93 // |entry_.cmd_line_args_|.
94 std::string delimiters;
95 delimiters.push_back('\0');
Peter Kasting134ef9af2024-12-28 02:30:0996 entry_.cmd_line_args_ =
97 SplitString(data, delimiters, KEEP_WHITESPACE, SPLIT_WANT_NONEMPTY);
[email protected]d3acb672013-05-31 15:38:0198
99 // |data| starts with the full executable path followed by a null character.
100 // We search for the first instance of '\0' and extract everything before it
101 // to populate |entry_.exe_file_|.
102 size_t exec_name_end = data.find('\0');
103 if (exec_name_end == std::string::npos) {
104 DLOG(ERROR) << "command line data didn't match expected format";
105 continue;
106 }
107
108 entry_.pid_ = kinfo.p_pid;
109 entry_.ppid_ = kinfo.p_ppid;
110 entry_.gid_ = kinfo.p__pgid;
111 size_t last_slash = data.rfind('/', exec_name_end);
Peter Kasting134ef9af2024-12-28 02:30:09112 if (last_slash == std::string::npos) {
[email protected]d3acb672013-05-31 15:38:01113 entry_.exe_file_.assign(data, 0, exec_name_end);
Peter Kasting134ef9af2024-12-28 02:30:09114 } else {
[email protected]d3acb672013-05-31 15:38:01115 entry_.exe_file_.assign(data, last_slash + 1,
116 exec_name_end - last_slash - 1);
Peter Kasting134ef9af2024-12-28 02:30:09117 }
[email protected]d3acb672013-05-31 15:38:01118 // 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