process.h 8.7 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. #include "build/chromeos_buildflags.h"
  12. #if defined(OS_WIN)
  13. #include "base/win/scoped_handle.h"
  14. #endif
  15. #if defined(OS_FUCHSIA)
  16. #include <lib/zx/process.h>
  17. #endif
  18. #if defined(OS_APPLE)
  19. #include "base/feature_list.h"
  20. #include "base/process/port_provider_mac.h"
  21. #endif
  22. namespace base {
  23. #if defined(OS_APPLE)
  24. extern const Feature kMacAllowBackgroundingProcesses;
  25. #endif
  26. // Provides a move-only encapsulation of a process.
  27. //
  28. // This object is not tied to the lifetime of the underlying process: the
  29. // process may be killed and this object may still around, and it will still
  30. // claim to be valid. The actual behavior in that case is OS dependent like so:
  31. //
  32. // Windows: The underlying ProcessHandle will be valid after the process dies
  33. // and can be used to gather some information about that process, but most
  34. // methods will obviously fail.
  35. //
  36. // POSIX: The underlying ProcessHandle is not guaranteed to remain valid after
  37. // the process dies, and it may be reused by the system, which means that it may
  38. // end up pointing to the wrong process.
  39. class BASE_EXPORT Process {
  40. public:
  41. // On Windows, this takes ownership of |handle|. On POSIX, this does not take
  42. // ownership of |handle|.
  43. explicit Process(ProcessHandle handle = kNullProcessHandle);
  44. Process(Process&& other);
  45. // The destructor does not terminate the process.
  46. ~Process();
  47. Process& operator=(Process&& other);
  48. // Returns an object for the current process.
  49. static Process Current();
  50. // Returns a Process for the given |pid|.
  51. static Process Open(ProcessId pid);
  52. // Returns a Process for the given |pid|. On Windows the handle is opened
  53. // with more access rights and must only be used by trusted code (can read the
  54. // address space and duplicate handles).
  55. static Process OpenWithExtraPrivileges(ProcessId pid);
  56. #if defined(OS_WIN)
  57. // Returns a Process for the given |pid|, using some |desired_access|.
  58. // See ::OpenProcess documentation for valid |desired_access|.
  59. static Process OpenWithAccess(ProcessId pid, DWORD desired_access);
  60. #endif
  61. // Creates an object from a |handle| owned by someone else.
  62. // Don't use this for new code. It is only intended to ease the migration to
  63. // a strict ownership model.
  64. // TODO(rvargas) crbug.com/417532: Remove this code.
  65. static Process DeprecatedGetProcessFromHandle(ProcessHandle handle);
  66. // Returns true if processes can be backgrounded.
  67. static bool CanBackgroundProcesses();
  68. // Terminates the current process immediately with |exit_code|.
  69. [[noreturn]] static void TerminateCurrentProcessImmediately(int exit_code);
  70. // Returns true if this objects represents a valid process.
  71. bool IsValid() const;
  72. // Returns a handle for this process. There is no guarantee about when that
  73. // handle becomes invalid because this object retains ownership.
  74. ProcessHandle Handle() const;
  75. // Returns a second object that represents this process.
  76. Process Duplicate() const;
  77. // Get the PID for this process.
  78. ProcessId Pid() const;
  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. // On Android, works only if |this| is the current process, as security
  84. // features prevent an application from getting data about other processes,
  85. // even if they belong to us. Otherwise, returns Time().
  86. Time CreationTime() const;
  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_APPLE)
  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_APPLE)
  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) || BUILDFLAG(IS_LACROS)
  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) || BUILDFLAG(IS_LACROS)
  195. } // namespace base
  196. #endif // BASE_PROCESS_PROCESS_H_