Update strings for turning off Smart Lock.
[chromium-blink-merge.git] / base / process / process_iterator_freebsd.cc
blob5b1e2ab09dbf7f3574c8fa710a5c79dcba540353
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"
7 #include <sys/types.h>
8 #include <sys/sysctl.h>
9 #include <unistd.h>
11 #include "base/logging.h"
12 #include "base/strings/string_util.h"
14 namespace base {
16 ProcessIterator::ProcessIterator(const ProcessFilter* filter)
17 : index_of_kinfo_proc_(),
18 filter_(filter) {
20 int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_UID, getuid() };
22 bool done = false;
23 int try_num = 1;
24 const int max_tries = 10;
26 do {
27 size_t len = 0;
28 if (sysctl(mib, arraysize(mib), NULL, &len, NULL, 0) < 0) {
29 LOG(ERROR) << "failed to get the size needed for the process list";
30 kinfo_procs_.resize(0);
31 done = true;
32 } else {
33 size_t num_of_kinfo_proc = len / sizeof(struct kinfo_proc);
34 // Leave some spare room for process table growth (more could show up
35 // between when we check and now)
36 num_of_kinfo_proc += 16;
37 kinfo_procs_.resize(num_of_kinfo_proc);
38 len = num_of_kinfo_proc * sizeof(struct kinfo_proc);
39 if (sysctl(mib, arraysize(mib), &kinfo_procs_[0], &len, NULL, 0) <0) {
40 // If we get a mem error, it just means we need a bigger buffer, so
41 // loop around again. Anything else is a real error and give up.
42 if (errno != ENOMEM) {
43 LOG(ERROR) << "failed to get the process list";
44 kinfo_procs_.resize(0);
45 done = true;
47 } else {
48 // Got the list, just make sure we're sized exactly right
49 size_t num_of_kinfo_proc = len / sizeof(struct kinfo_proc);
50 kinfo_procs_.resize(num_of_kinfo_proc);
51 done = true;
54 } while (!done && (try_num++ < max_tries));
56 if (!done) {
57 LOG(ERROR) << "failed to collect the process list in a few tries";
58 kinfo_procs_.resize(0);
62 ProcessIterator::~ProcessIterator() {
65 bool ProcessIterator::CheckForNextProcess() {
66 std::string data;
68 for (; index_of_kinfo_proc_ < kinfo_procs_.size(); ++index_of_kinfo_proc_) {
69 size_t length;
70 struct kinfo_proc kinfo = kinfo_procs_[index_of_kinfo_proc_];
71 int mib[] = { CTL_KERN, KERN_PROC_ARGS, kinfo.ki_pid };
73 if ((kinfo.ki_pid > 0) && (kinfo.ki_stat == SZOMB))
74 continue;
76 length = 0;
77 if (sysctl(mib, arraysize(mib), NULL, &length, NULL, 0) < 0) {
78 LOG(ERROR) << "failed to figure out the buffer size for a command line";
79 continue;
82 data.resize(length);
84 if (sysctl(mib, arraysize(mib), &data[0], &length, NULL, 0) < 0) {
85 LOG(ERROR) << "failed to fetch a commandline";
86 continue;
89 std::string delimiters;
90 delimiters.push_back('\0');
91 Tokenize(data, delimiters, &entry_.cmd_line_args_);
93 size_t exec_name_end = data.find('\0');
94 if (exec_name_end == std::string::npos) {
95 LOG(ERROR) << "command line data didn't match expected format";
96 continue;
99 entry_.pid_ = kinfo.ki_pid;
100 entry_.ppid_ = kinfo.ki_ppid;
101 entry_.gid_ = kinfo.ki_pgid;
103 size_t last_slash = data.rfind('/', exec_name_end);
104 if (last_slash == std::string::npos) {
105 entry_.exe_file_.assign(data, 0, exec_name_end);
106 } else {
107 entry_.exe_file_.assign(data, last_slash + 1,
108 exec_name_end - last_slash - 1);
111 // Start w/ the next entry next time through
112 ++index_of_kinfo_proc_;
114 return true;
116 return false;
119 bool NamedProcessIterator::IncludeEntry() {
120 if (executable_name_ != entry().exe_file())
121 return false;
123 return ProcessIterator::IncludeEntry();
126 } // namespace base