process_iterator.h 4.7 KB

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