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 // This file contains methods to iterate over processes on the system.
      6 
      7 #ifndef BASE_PROCESS_PROCESS_ITERATOR_H_
      8 #define BASE_PROCESS_PROCESS_ITERATOR_H_
      9 
     10 #include <stddef.h>
     11 
     12 #include <list>
     13 #include <string>
     14 #include <vector>
     15 
     16 #include "base/base_export.h"
     17 #include "base/files/file_path.h"
     18 #include "base/macros.h"
     19 #include "base/process/process.h"
     20 #include "build/build_config.h"
     21 
     22 #if defined(OS_WIN)
     23 #include <windows.h>
     24 #include <tlhelp32.h>
     25 #elif defined(OS_MACOSX) || defined(OS_OPENBSD)
     26 #include <sys/sysctl.h>
     27 #elif defined(OS_FREEBSD)
     28 #include <sys/user.h>
     29 #elif defined(OS_POSIX)
     30 #include <dirent.h>
     31 #endif
     32 
     33 namespace base {
     34 
     35 #if defined(OS_WIN)
     36 struct ProcessEntry : public PROCESSENTRY32 {
     37   ProcessId pid() const { return th32ProcessID; }
     38   ProcessId parent_pid() const { return th32ParentProcessID; }
     39   const wchar_t* exe_file() const { return szExeFile; }
     40 };
     41 #elif defined(OS_POSIX)
     42 struct BASE_EXPORT ProcessEntry {
     43   ProcessEntry();
     44   ~ProcessEntry();
     45 
     46   ProcessId pid() const { return pid_; }
     47   ProcessId parent_pid() const { return ppid_; }
     48   ProcessId gid() const { return gid_; }
     49   const char* exe_file() const { return exe_file_.c_str(); }
     50   const std::vector<std::string>& cmd_line_args() const {
     51     return cmd_line_args_;
     52   }
     53 
     54   ProcessId pid_;
     55   ProcessId ppid_;
     56   ProcessId gid_;
     57   std::string exe_file_;
     58   std::vector<std::string> cmd_line_args_;
     59 };
     60 #endif  // defined(OS_POSIX)
     61 
     62 // Used to filter processes by process ID.
     63 class ProcessFilter {
     64  public:
     65   // Returns true to indicate set-inclusion and false otherwise.  This method
     66   // should not have side-effects and should be idempotent.
     67   virtual bool Includes(const ProcessEntry& entry) const = 0;
     68 
     69  protected:
     70   virtual ~ProcessFilter() {}
     71 };
     72 
     73 // This class provides a way to iterate through a list of processes on the
     74 // current machine with a specified filter.
     75 // To use, create an instance and then call NextProcessEntry() until it returns
     76 // false.
     77 class BASE_EXPORT ProcessIterator {
     78  public:
     79   typedef std::list<ProcessEntry> ProcessEntries;
     80 
     81   explicit ProcessIterator(const ProcessFilter* filter);
     82   virtual ~ProcessIterator();
     83 
     84   // If there's another process that matches the given executable name,
     85   // returns a const pointer to the corresponding PROCESSENTRY32.
     86   // If there are no more matching processes, returns NULL.
     87   // The returned pointer will remain valid until NextProcessEntry()
     88   // is called again or this NamedProcessIterator goes out of scope.
     89   const ProcessEntry* NextProcessEntry();
     90 
     91   // Takes a snapshot of all the ProcessEntry found.
     92   ProcessEntries Snapshot();
     93 
     94  protected:
     95   virtual bool IncludeEntry();
     96   const ProcessEntry& entry() { return entry_; }
     97 
     98  private:
     99   // Determines whether there's another process (regardless of executable)
    100   // left in the list of all processes.  Returns true and sets entry_ to
    101   // that process's info if there is one, false otherwise.
    102   bool CheckForNextProcess();
    103 
    104   // Initializes a PROCESSENTRY32 data structure so that it's ready for
    105   // use with Process32First/Process32Next.
    106   void InitProcessEntry(ProcessEntry* entry);
    107 
    108 #if defined(OS_WIN)
    109   HANDLE snapshot_;
    110   bool started_iteration_;
    111 #elif defined(OS_MACOSX) || defined(OS_BSD)
    112   std::vector<kinfo_proc> kinfo_procs_;
    113   size_t index_of_kinfo_proc_;
    114 #elif defined(OS_POSIX)
    115   DIR* procfs_dir_;
    116 #endif
    117   ProcessEntry entry_;
    118   const ProcessFilter* filter_;
    119 
    120   DISALLOW_COPY_AND_ASSIGN(ProcessIterator);
    121 };
    122 
    123 // This class provides a way to iterate through the list of processes
    124 // on the current machine that were started from the given executable
    125 // name.  To use, create an instance and then call NextProcessEntry()
    126 // until it returns false.
    127 class BASE_EXPORT NamedProcessIterator : public ProcessIterator {
    128  public:
    129   NamedProcessIterator(const FilePath::StringType& executable_name,
    130                        const ProcessFilter* filter);
    131   ~NamedProcessIterator() override;
    132 
    133  protected:
    134   bool IncludeEntry() override;
    135 
    136  private:
    137   FilePath::StringType executable_name_;
    138 
    139   DISALLOW_COPY_AND_ASSIGN(NamedProcessIterator);
    140 };
    141 
    142 // Returns the number of processes on the machine that are running from the
    143 // given executable name.  If filter is non-null, then only processes selected
    144 // by the filter will be counted.
    145 BASE_EXPORT int GetProcessCount(const FilePath::StringType& executable_name,
    146                                 const ProcessFilter* filter);
    147 
    148 }  // namespace base
    149 
    150 #endif  // BASE_PROCESS_PROCESS_ITERATOR_H_
    151