process_iterator_linux.cc revision 868fa2fe829687343ffae624259930155e16dbd8
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. 4 5#include "base/process/process_iterator.h" 6 7#include "base/file_util.h" 8#include "base/logging.h" 9#include "base/process/internal_linux.h" 10#include "base/process_util.h" 11#include "base/strings/string_util.h" 12#include "base/threading/thread_restrictions.h" 13 14namespace base { 15 16namespace { 17 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 21// that are strings. 22std::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) { 26 NOTREACHED(); 27 return std::string(); 28 } 29 30 if (proc_stats.size() > static_cast<size_t>(field_num)) 31 return proc_stats[field_num]; 32 33 NOTREACHED(); 34 return 0; 35} 36 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 41// delimiter. 42bool 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; 45 46 FilePath cmd_line_file = internal::GetProcPidDir(pid).Append("cmdline"); 47 std::string cmd_line; 48 if (!file_util::ReadFileToString(cmd_line_file, &cmd_line)) 49 return false; 50 std::string delimiters; 51 delimiters.push_back('\0'); 52 Tokenize(cmd_line, delimiters, proc_cmd_line_args); 53 return true; 54} 55 56} // namespace 57 58ProcessIterator::ProcessIterator(const ProcessFilter* filter) 59 : filter_(filter) { 60 procfs_dir_ = opendir(internal::kProcDir); 61} 62 63ProcessIterator::~ProcessIterator() { 64 if (procfs_dir_) { 65 closedir(procfs_dir_); 66 procfs_dir_ = NULL; 67 } 68} 69 70bool ProcessIterator::CheckForNextProcess() { 71 // TODO(port): skip processes owned by different UID 72 73 pid_t pid = kNullProcessId; 74 std::vector<std::string> cmd_line_args; 75 std::string stats_data; 76 std::vector<std::string> proc_stats; 77 78 // Arbitrarily guess that there will never be more than 200 non-process 79 // files in /proc. Hardy has 53 and Lucid has 61. 80 int skipped = 0; 81 const int kSkipLimit = 200; 82 while (skipped < kSkipLimit) { 83 dirent* slot = readdir(procfs_dir_); 84 // all done looking through /proc? 85 if (!slot) 86 return false; 87 88 // If not a process, keep looking for one. 89 pid = internal::ProcDirSlotToPid(slot->d_name); 90 if (!pid) { 91 skipped++; 92 continue; 93 } 94 95 if (!GetProcCmdline(pid, &cmd_line_args)) 96 continue; 97 98 if (!internal::ReadProcStats(pid, &stats_data)) 99 continue; 100 if (!internal::ParseProcStats(stats_data, &proc_stats)) 101 continue; 102 103 std::string runstate = 104 GetProcStatsFieldAsString(proc_stats, internal::VM_STATE); 105 if (runstate.size() != 1) { 106 NOTREACHED(); 107 continue; 108 } 109 110 // Is the process in 'Zombie' state, i.e. dead but waiting to be reaped? 111 // Allowed values: D R S T Z 112 if (runstate[0] != 'Z') 113 break; 114 115 // Nope, it's a zombie; somebody isn't cleaning up after their children. 116 // (e.g. WaitForProcessesToExit doesn't clean up after dead children yet.) 117 // There could be a lot of zombies, can't really decrement i here. 118 } 119 if (skipped >= kSkipLimit) { 120 NOTREACHED(); 121 return false; 122 } 123 124 entry_.pid_ = pid; 125 entry_.ppid_ = GetProcStatsFieldAsInt(proc_stats, internal::VM_PPID); 126 entry_.gid_ = GetProcStatsFieldAsInt(proc_stats, internal::VM_PGRP); 127 entry_.cmd_line_args_.assign(cmd_line_args.begin(), cmd_line_args.end()); 128 entry_.exe_file_ = GetProcessExecutablePath(pid).BaseName().value(); 129 return true; 130} 131 132bool NamedProcessIterator::IncludeEntry() { 133 if (executable_name_ != entry().exe_file()) 134 return false; 135 return ProcessIterator::IncludeEntry(); 136} 137 138} // namespace base 139