Home | History | Annotate | Download | only in process
      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/strings/string_util.h"
     11 #include "base/threading/thread_restrictions.h"
     12 
     13 namespace base {
     14 
     15 namespace {
     16 
     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
     20 // that are strings.
     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) {
     25     NOTREACHED();
     26     return std::string();
     27   }
     28 
     29   if (proc_stats.size() > static_cast<size_t>(field_num))
     30     return proc_stats[field_num];
     31 
     32   NOTREACHED();
     33   return 0;
     34 }
     35 
     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
     40 // delimiter.
     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;
     44 
     45   FilePath cmd_line_file = internal::GetProcPidDir(pid).Append("cmdline");
     46   std::string cmd_line;
     47   if (!ReadFileToString(cmd_line_file, &cmd_line))
     48     return false;
     49   std::string delimiters;
     50   delimiters.push_back('\0');
     51   Tokenize(cmd_line, delimiters, proc_cmd_line_args);
     52   return true;
     53 }
     54 
     55 }  // namespace
     56 
     57 ProcessIterator::ProcessIterator(const ProcessFilter* filter)
     58     : filter_(filter) {
     59   procfs_dir_ = opendir(internal::kProcDir);
     60 }
     61 
     62 ProcessIterator::~ProcessIterator() {
     63   if (procfs_dir_) {
     64     closedir(procfs_dir_);
     65     procfs_dir_ = NULL;
     66   }
     67 }
     68 
     69 bool ProcessIterator::CheckForNextProcess() {
     70   // TODO(port): skip processes owned by different UID
     71 
     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;
     76 
     77   // Arbitrarily guess that there will never be more than 200 non-process
     78   // files in /proc.  Hardy has 53 and Lucid has 61.
     79   int skipped = 0;
     80   const int kSkipLimit = 200;
     81   while (skipped < kSkipLimit) {
     82     dirent* slot = readdir(procfs_dir_);
     83     // all done looking through /proc?
     84     if (!slot)
     85       return false;
     86 
     87     // If not a process, keep looking for one.
     88     pid = internal::ProcDirSlotToPid(slot->d_name);
     89     if (!pid) {
     90       skipped++;
     91       continue;
     92     }
     93 
     94     if (!GetProcCmdline(pid, &cmd_line_args))
     95       continue;
     96 
     97     if (!internal::ReadProcStats(pid, &stats_data))
     98       continue;
     99     if (!internal::ParseProcStats(stats_data, &proc_stats))
    100       continue;
    101 
    102     std::string runstate =
    103         GetProcStatsFieldAsString(proc_stats, internal::VM_STATE);
    104     if (runstate.size() != 1) {
    105       NOTREACHED();
    106       continue;
    107     }
    108 
    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')
    112       break;
    113 
    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.
    117   }
    118   if (skipped >= kSkipLimit) {
    119     NOTREACHED();
    120     return false;
    121   }
    122 
    123   entry_.pid_ = pid;
    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();
    128   return true;
    129 }
    130 
    131 bool NamedProcessIterator::IncludeEntry() {
    132   if (executable_name_ != entry().exe_file())
    133     return false;
    134   return ProcessIterator::IncludeEntry();
    135 }
    136 
    137 }  // namespace base
    138