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_util.h"
11 #include "base/threading/thread_restrictions.h"
17 // Reads the |field_num|th field from |proc_stats|.
18 // Returns an empty string on failure.
19 // This version only handles VM_COMM and VM_STATE, which are the only fields
21 std::string
GetProcStatsFieldAsString(
22 const std::vector
<std::string
>& proc_stats
,
23 internal::ProcStatsFields field_num
) {
24 if (field_num
< internal::VM_COMM
|| field_num
> internal::VM_STATE
) {
29 if (proc_stats
.size() > static_cast<size_t>(field_num
))
30 return proc_stats
[field_num
];
36 // Reads /proc/<pid>/cmdline and populates |proc_cmd_line_args| with the command
37 // line arguments. Returns true if successful.
38 // Note: /proc/<pid>/cmdline contains command line arguments separated by single
39 // null characters. We tokenize it into a vector of strings using '\0' as a
41 bool GetProcCmdline(pid_t pid
, std::vector
<std::string
>* proc_cmd_line_args
) {
42 // Synchronously reading files in /proc is safe.
43 ThreadRestrictions::ScopedAllowIO allow_io
;
45 FilePath cmd_line_file
= internal::GetProcPidDir(pid
).Append("cmdline");
47 if (!ReadFileToString(cmd_line_file
, &cmd_line
))
49 std::string delimiters
;
50 delimiters
.push_back('\0');
51 Tokenize(cmd_line
, delimiters
, proc_cmd_line_args
);
57 ProcessIterator::ProcessIterator(const ProcessFilter
* filter
)
59 procfs_dir_
= opendir(internal::kProcDir
);
62 ProcessIterator::~ProcessIterator() {
64 closedir(procfs_dir_
);
69 bool ProcessIterator::CheckForNextProcess() {
70 // TODO(port): skip processes owned by different UID
72 pid_t pid
= kNullProcessId
;
73 std::vector
<std::string
> cmd_line_args
;
74 std::string stats_data
;
75 std::vector
<std::string
> proc_stats
;
77 // Arbitrarily guess that there will never be more than 200 non-process
78 // files in /proc. Hardy has 53 and Lucid has 61.
80 const int kSkipLimit
= 200;
81 while (skipped
< kSkipLimit
) {
82 dirent
* slot
= readdir(procfs_dir_
);
83 // all done looking through /proc?
87 // If not a process, keep looking for one.
88 pid
= internal::ProcDirSlotToPid(slot
->d_name
);
94 if (!GetProcCmdline(pid
, &cmd_line_args
))
97 if (!internal::ReadProcStats(pid
, &stats_data
))
99 if (!internal::ParseProcStats(stats_data
, &proc_stats
))
102 std::string runstate
=
103 GetProcStatsFieldAsString(proc_stats
, internal::VM_STATE
);
104 if (runstate
.size() != 1) {
109 // Is the process in 'Zombie' state, i.e. dead but waiting to be reaped?
110 // Allowed values: D R S T Z
111 if (runstate
[0] != 'Z')
114 // Nope, it's a zombie; somebody isn't cleaning up after their children.
115 // (e.g. WaitForProcessesToExit doesn't clean up after dead children yet.)
116 // There could be a lot of zombies, can't really decrement i here.
118 if (skipped
>= kSkipLimit
) {
124 entry_
.ppid_
= GetProcStatsFieldAsInt64(proc_stats
, internal::VM_PPID
);
125 entry_
.gid_
= GetProcStatsFieldAsInt64(proc_stats
, internal::VM_PGRP
);
126 entry_
.cmd_line_args_
.assign(cmd_line_args
.begin(), cmd_line_args
.end());
127 entry_
.exe_file_
= GetProcessExecutablePath(pid
).BaseName().value();
131 bool NamedProcessIterator::IncludeEntry() {
132 if (executable_name_
!= entry().exe_file())
134 return ProcessIterator::IncludeEntry();