Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | d3acb67 | 2013-05-31 15:38:01 | [diff] [blame] | 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> |
avi | beced7c | 2015-12-24 06:47:59 | [diff] [blame] | 8 | #include <stddef.h> |
[email protected] | d3acb67 | 2013-05-31 15:38:01 | [diff] [blame] | 9 | #include <sys/sysctl.h> |
| 10 | |
| 11 | #include "base/logging.h" |
brettw | 37fd9d7d | 2015-06-24 02:07:16 | [diff] [blame] | 12 | #include "base/strings/string_split.h" |
[email protected] | d1a5a2f | 2013-06-10 21:17:40 | [diff] [blame] | 13 | #include "base/strings/string_util.h" |
[email protected] | d3acb67 | 2013-05-31 15:38:01 | [diff] [blame] | 14 | |
| 15 | namespace base { |
| 16 | |
| 17 | ProcessIterator::ProcessIterator(const ProcessFilter* filter) |
Lei Zhang | 6f418e0 | 2023-02-08 21:55:43 | [diff] [blame] | 18 | : filter_(filter) { |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 19 | int mib[] = { |
| 20 | CTL_KERN, KERN_PROC, KERN_PROC_UID, getuid(), sizeof(struct kinfo_proc), |
| 21 | 0}; |
[email protected] | d3acb67 | 2013-05-31 15:38:01 | [diff] [blame] | 22 | |
| 23 | bool done = false; |
| 24 | int try_num = 1; |
| 25 | const int max_tries = 10; |
| 26 | |
| 27 | do { |
| 28 | size_t len = 0; |
Daniel Cheng | f45f4760 | 2022-02-28 22:38:32 | [diff] [blame] | 29 | if (sysctl(mib, std::size(mib), NULL, &len, NULL, 0) < 0) { |
[email protected] | d3acb67 | 2013-05-31 15:38:01 | [diff] [blame] | 30 | 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 Cheng | f45f4760 | 2022-02-28 22:38:32 | [diff] [blame] | 40 | if (sysctl(mib, std::size(mib), &kinfo_procs_[0], &len, NULL, 0) < 0) { |
[email protected] | d3acb67 | 2013-05-31 15:38:01 | [diff] [blame] | 41 | // 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 Zhang | 6f418e0 | 2023-02-08 21:55:43 | [diff] [blame] | 63 | ProcessIterator::~ProcessIterator() = default; |
[email protected] | d3acb67 | 2013-05-31 15:38:01 | [diff] [blame] | 64 | |
| 65 | bool 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 Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 71 | if ((kinfo.p_pid > 0) && (kinfo.p_stat == SZOMB)) { |
[email protected] | d3acb67 | 2013-05-31 15:38:01 | [diff] [blame] | 72 | continue; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 73 | } |
[email protected] | d3acb67 | 2013-05-31 15:38:01 | [diff] [blame] | 74 | |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 75 | int mib[] = {CTL_KERN, KERN_PROC_ARGS, kinfo.p_pid}; |
[email protected] | d3acb67 | 2013-05-31 15:38:01 | [diff] [blame] | 76 | |
| 77 | // Find out what size buffer we need. |
| 78 | size_t data_len = 0; |
Daniel Cheng | f45f4760 | 2022-02-28 22:38:32 | [diff] [blame] | 79 | if (sysctl(mib, std::size(mib), NULL, &data_len, NULL, 0) < 0) { |
[email protected] | d3acb67 | 2013-05-31 15:38:01 | [diff] [blame] | 80 | DVPLOG(1) << "failed to figure out the buffer size for a commandline"; |
| 81 | continue; |
| 82 | } |
| 83 | |
| 84 | data.resize(data_len); |
Daniel Cheng | f45f4760 | 2022-02-28 22:38:32 | [diff] [blame] | 85 | if (sysctl(mib, std::size(mib), &data[0], &data_len, NULL, 0) < 0) { |
[email protected] | d3acb67 | 2013-05-31 15:38:01 | [diff] [blame] | 86 | 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 Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 96 | entry_.cmd_line_args_ = |
| 97 | SplitString(data, delimiters, KEEP_WHITESPACE, SPLIT_WANT_NONEMPTY); |
[email protected] | d3acb67 | 2013-05-31 15:38:01 | [diff] [blame] | 98 | |
| 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 Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 112 | if (last_slash == std::string::npos) { |
[email protected] | d3acb67 | 2013-05-31 15:38:01 | [diff] [blame] | 113 | entry_.exe_file_.assign(data, 0, exec_name_end); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 114 | } else { |
[email protected] | d3acb67 | 2013-05-31 15:38:01 | [diff] [blame] | 115 | entry_.exe_file_.assign(data, last_slash + 1, |
| 116 | exec_name_end - last_slash - 1); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 117 | } |
[email protected] | d3acb67 | 2013-05-31 15:38:01 | [diff] [blame] | 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 | |
| 126 | bool NamedProcessIterator::IncludeEntry() { |
| 127 | return (executable_name_ == entry().exe_file() && |
| 128 | ProcessIterator::IncludeEntry()); |
| 129 | } |
| 130 | |
| 131 | } // namespace base |