process.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // Copyright 2011 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. #ifndef BASE_PROCESS_PROCESS_H_
  5. #define BASE_PROCESS_PROCESS_H_
  6. #include "base/base_export.h"
  7. #include "base/macros.h"
  8. #include "base/process/process_handle.h"
  9. #include "base/time/time.h"
  10. #include "build/build_config.h"
  11. #if defined(OS_WIN)
  12. #include "base/win/scoped_handle.h"
  13. #endif
  14. #if defined(OS_FUCHSIA)
  15. #include <lib/zx/process.h>
  16. #endif
  17. #if defined(OS_MACOSX)
  18. #include "base/feature_list.h"
  19. #include "base/process/port_provider_mac.h"
  20. #endif
  21. namespace base {
  22. #if defined(OS_MACOSX)
  23. extern const Feature kMacAllowBackgroundingProcesses;
  24. #endif
  25. // Provides a move-only encapsulation of a process.
  26. //
  27. // This object is not tied to the lifetime of the underlying process: the
  28. // process may be killed and this object may still around, and it will still
  29. // claim to be valid. The actual behavior in that case is OS dependent like so:
  30. //
  31. // Windows: The underlying ProcessHandle will be valid after the process dies
  32. // and can be used to gather some information about that process, but most
  33. // methods will obviously fail.
  34. //
  35. // POSIX: The underlying ProcessHandle is not guaranteed to remain valid after
  36. // the process dies, and it may be reused by the system, which means that it may
  37. // end up pointing to the wrong process.
  38. class BASE_EXPORT Process {
  39. public:
  40. // On Windows, this takes ownership of |handle|. On POSIX, this does not take
  41. // ownership of |handle|.
  42. explicit Process(ProcessHandle handle = kNullProcessHandle);
  43. Process(Process&& other);
  44. // The destructor does not terminate the process.
  45. ~Process();
  46. Process& operator=(Process&& other);
  47. // Returns an object for the current process.
  48. static Process Current();
  49. // Returns a Process for the given |pid|.
  50. static Process Open(ProcessId pid);
  51. // Returns a Process for the given |pid|. On Windows the handle is opened
  52. // with more access rights and must only be used by trusted code (can read the
  53. // address space and duplicate handles).
  54. static Process OpenWithExtraPrivileges(ProcessId pid);
  55. #if defined(OS_WIN)
  56. // Returns a Process for the given |pid|, using some |desired_access|.
  57. // See ::OpenProcess documentation for valid |desired_access|.
  58. static Process OpenWithAccess(ProcessId pid, DWORD desired_access);
  59. #endif
  60. // Creates an object from a |handle| owned by someone else.
  61. // Don't use this for new code. It is only intended to ease the migration to
  62. // a strict ownership model.
  63. // TODO(rvargas) crbug.com/417532: Remove this code.
  64. static Process DeprecatedGetProcessFromHandle(ProcessHandle handle);
  65. // Returns true if processes can be backgrounded.
  66. static bool CanBackgroundProcesses();
  67. // Terminates the current process immediately with |exit_code|.
  68. [[noreturn]] static void TerminateCurrentProcessImmediately(int exit_code);
  69. // Returns true if this objects represents a valid process.
  70. bool IsValid() const;
  71. // Returns a handle for this process. There is no guarantee about when that
  72. // handle becomes invalid because this object retains ownership.
  73. ProcessHandle Handle() const;
  74. // Returns a second object that represents this process.
  75. Process Duplicate() const;
  76. // Get the PID for this process.
  77. ProcessId Pid() const;
  78. #if !defined(OS_ANDROID)
  79. // Get the creation time for this process. Since the Pid can be reused after a
  80. // process dies, it is useful to use both the Pid and the creation time to
  81. // uniquely identify a process.
  82. //
  83. // Not available on Android because /proc/stat/ cannot be accessed on O+.
  84. // https://issuetracker.google.com/issues/37140047
  85. Time CreationTime() const;
  86. #endif // !defined(OS_ANDROID)
  87. // Returns true if this process is the current process.
  88. bool is_current() const;
  89. // Close the process handle. This will not terminate the process.
  90. void Close();
  91. // Returns true if this process is still running. This is only safe on Windows
  92. // (and maybe Fuchsia?), because the ProcessHandle will keep the zombie
  93. // process information available until itself has been released. But on Posix,
  94. // the OS may reuse the ProcessId.
  95. #if defined(OS_WIN)
  96. bool IsRunning() const {
  97. return !WaitForExitWithTimeout(base::TimeDelta(), nullptr);
  98. }
  99. #endif
  100. // Terminates the process with extreme prejudice. The given |exit_code| will
  101. // be the exit code of the process. If |wait| is true, this method will wait
  102. // for up to one minute for the process to actually terminate.
  103. // Returns true if the process terminates within the allowed time.
  104. // NOTE: On POSIX |exit_code| is ignored.
  105. bool Terminate(int exit_code, bool wait) const;
  106. #if defined(OS_WIN)
  107. enum class WaitExitStatus {
  108. PROCESS_EXITED,
  109. STOP_EVENT_SIGNALED,
  110. FAILED,
  111. };
  112. // Waits for the process to exit, or the specified |stop_event_handle| to be
  113. // set. Returns value indicating which event was set. The given |exit_code|
  114. // will be the exit code of the process.
  115. WaitExitStatus WaitForExitOrEvent(
  116. const base::win::ScopedHandle& stop_event_handle,
  117. int* exit_code) const;
  118. #endif // OS_WIN
  119. // Waits for the process to exit. Returns true on success.
  120. // On POSIX, if the process has been signaled then |exit_code| is set to -1.
  121. // On Linux this must be a child process, however on Mac and Windows it can be
  122. // any process.
  123. // NOTE: |exit_code| is optional, nullptr can be passed if the exit code is
  124. // not required.
  125. bool WaitForExit(int* exit_code) const;
  126. // Same as WaitForExit() but only waits for up to |timeout|.
  127. // NOTE: |exit_code| is optional, nullptr can be passed if the exit code
  128. // is not required.
  129. bool WaitForExitWithTimeout(TimeDelta timeout, int* exit_code) const;
  130. // Indicates that the process has exited with the specified |exit_code|.
  131. // This should be called if process exit is observed outside of this class.
  132. // (i.e. Not because Terminate or WaitForExit, above, was called.)
  133. // Note that nothing prevents this being called multiple times for a dead
  134. // process though that should be avoided.
  135. void Exited(int exit_code) const;
  136. #if defined(OS_MACOSX)
  137. // The Mac needs a Mach port in order to manipulate a process's priority,
  138. // and there's no good way to get that from base given the pid. These Mac
  139. // variants of the IsProcessBackgrounded and SetProcessBackgrounded API take
  140. // a port provider for this reason. See crbug.com/460102
  141. //
  142. // A process is backgrounded when its task priority is
  143. // |TASK_BACKGROUND_APPLICATION|.
  144. //
  145. // Returns true if the port_provider can locate a task port for the process
  146. // and it is backgrounded. If port_provider is null, returns false.
  147. bool IsProcessBackgrounded(PortProvider* port_provider) const;
  148. // Set the process as backgrounded. If value is
  149. // true, the priority of the associated task will be set to
  150. // TASK_BACKGROUND_APPLICATION. If value is false, the
  151. // priority of the process will be set to TASK_FOREGROUND_APPLICATION.
  152. //
  153. // Returns true if the priority was changed, false otherwise. If
  154. // |port_provider| is null, this is a no-op and it returns false.
  155. bool SetProcessBackgrounded(PortProvider* port_provider, bool value);
  156. #else
  157. // A process is backgrounded when it's priority is lower than normal.
  158. // Return true if this process is backgrounded, false otherwise.
  159. bool IsProcessBackgrounded() const;
  160. // Set a process as backgrounded. If value is true, the priority of the
  161. // process will be lowered. If value is false, the priority of the process
  162. // will be made "normal" - equivalent to default process priority.
  163. // Returns true if the priority was changed, false otherwise.
  164. bool SetProcessBackgrounded(bool value);
  165. #endif // defined(OS_MACOSX)
  166. // Returns an integer representing the priority of a process. The meaning
  167. // of this value is OS dependent.
  168. int GetPriority() const;
  169. #if defined(OS_CHROMEOS)
  170. // Get the PID in its PID namespace.
  171. // If the process is not in a PID namespace or /proc/<pid>/status does not
  172. // report NSpid, kNullProcessId is returned.
  173. ProcessId GetPidInNamespace() const;
  174. #endif
  175. private:
  176. #if defined(OS_WIN)
  177. win::ScopedHandle process_;
  178. #elif defined(OS_FUCHSIA)
  179. zx::process process_;
  180. #else
  181. ProcessHandle process_;
  182. #endif
  183. #if defined(OS_WIN) || defined(OS_FUCHSIA)
  184. bool is_current_process_;
  185. #endif
  186. DISALLOW_COPY_AND_ASSIGN(Process);
  187. };
  188. #if defined(OS_CHROMEOS)
  189. // Exposed for testing.
  190. // Given the contents of the /proc/<pid>/cgroup file, determine whether the
  191. // process is backgrounded or not.
  192. BASE_EXPORT bool IsProcessBackgroundedCGroup(
  193. const StringPiece& cgroup_contents);
  194. #endif // defined(OS_CHROMEOS)
  195. } // namespace base
  196. #endif // BASE_PROCESS_PROCESS_H_