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 "base/files/file_util.h"
8 #include "base/logging.h"
9 #include "base/process/internal_linux.h"
10 #include "base/strings/string_split.h"
11 #include "base/strings/string_util.h"
12 #include "base/threading/thread_restrictions.h"
18 // Reads the |field_num|th field from |proc_stats|.
19 // Returns an empty string on failure.
20 // This version only handles VM_COMM and VM_STATE, which are the only fields
22 std::string
GetProcStatsFieldAsString(
23 const std::vector
<std::string
>& proc_stats
,
24 internal::ProcStatsFields field_num
) {
25 if (field_num
< internal::VM_COMM
|| field_num
> internal::VM_STATE
) {
30 if (proc_stats
.size() > static_cast<size_t>(field_num
))
31 return proc_stats
[field_num
];
37 // Reads /proc/<pid>/cmdline and populates |proc_cmd_line_args| with the command
38 // line arguments. Returns true if successful.
39 // Note: /proc/<pid>/cmdline contains command line arguments separated by single
40 // null characters. We tokenize it into a vector of strings using '\0' as a
42 bool GetProcCmdline(pid_t pid
, std::vector
<std::string
>* proc_cmd_line_args
) {
43 // Synchronously reading files in /proc is safe.
44 ThreadRestrictions::ScopedAllowIO allow_io
;
46 FilePath cmd_line_file
= internal::GetProcPidDir(pid
).Append("cmdline");
48 if (!ReadFileToString(cmd_line_file
, &cmd_line
))
50 std::string delimiters
;
51 delimiters
.push_back('\0');
52 *proc_cmd_line_args
= SplitString(cmd_line
, delimiters
, KEEP_WHITESPACE
,
59 ProcessIterator::ProcessIterator(const ProcessFilter
* filter
)
61 procfs_dir_
= opendir(internal::kProcDir
);
64 ProcessIterator::~ProcessIterator() {
66 closedir(procfs_dir_
);
71 bool ProcessIterator::CheckForNextProcess() {
72 // TODO(port): skip processes owned by different UID
74 pid_t pid
= kNullProcessId
;
75 std::vector
<std::string
> cmd_line_args
;
76 std::string stats_data
;
77 std::vector
<std::string
> proc_stats
;
79 // Arbitrarily guess that there will never be more than 200 non-process
80 // files in /proc. Hardy has 53 and Lucid has 61.
82 const int kSkipLimit
= 200;
83 while (skipped
< kSkipLimit
) {
84 dirent
* slot
= readdir(procfs_dir_
);
85 // all done looking through /proc?
89 // If not a process, keep looking for one.
90 pid
= internal::ProcDirSlotToPid(slot
->d_name
);
96 if (!GetProcCmdline(pid
, &cmd_line_args
))
99 if (!internal::ReadProcStats(pid
, &stats_data
))
101 if (!internal::ParseProcStats(stats_data
, &proc_stats
))
104 std::string runstate
=
105 GetProcStatsFieldAsString(proc_stats
, internal::VM_STATE
);
106 if (runstate
.size() != 1) {
111 // Is the process in 'Zombie' state, i.e. dead but waiting to be reaped?
112 // Allowed values: D R S T Z
113 if (runstate
[0] != 'Z')
116 // Nope, it's a zombie; somebody isn't cleaning up after their children.
117 // (e.g. WaitForProcessesToExit doesn't clean up after dead children yet.)
118 // There could be a lot of zombies, can't really decrement i here.
120 if (skipped
>= kSkipLimit
) {
126 entry_
.ppid_
= GetProcStatsFieldAsInt64(proc_stats
, internal::VM_PPID
);
127 entry_
.gid_
= GetProcStatsFieldAsInt64(proc_stats
, internal::VM_PGRP
);
128 entry_
.cmd_line_args_
.assign(cmd_line_args
.begin(), cmd_line_args
.end());
129 entry_
.exe_file_
= GetProcessExecutablePath(pid
).BaseName().value();
133 bool NamedProcessIterator::IncludeEntry() {
134 if (executable_name_
!= entry().exe_file())
136 return ProcessIterator::IncludeEntry();