blob: 2b945015af3646afcfdde9e0bd094ea6f2c20301 [file] [log] [blame]
shrike8fbe9d32015-06-02 19:53:571// Copyright 2011 The Chromium Authors. All rights reserved.
[email protected]76bea672013-07-19 16:48:562// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
danakj0a448602015-03-10 00:31:165#ifndef BASE_PROCESS_PROCESS_H_
6#define BASE_PROCESS_PROCESS_H_
[email protected]76bea672013-07-19 16:48:567
8#include "base/base_export.h"
dcheng1a2fd6cd2016-06-07 21:39:129#include "base/macros.h"
[email protected]76bea672013-07-19 16:48:5610#include "base/process/process_handle.h"
rvargas126fd5822014-12-12 00:25:1411#include "base/time/time.h"
[email protected]76bea672013-07-19 16:48:5612#include "build/build_config.h"
13
rvargas079d1842014-10-17 22:32:1614#if defined(OS_WIN)
15#include "base/win/scoped_handle.h"
16#endif
17
Wez78b733132017-08-09 18:41:5918#if defined(OS_FUCHSIA)
Scott Grahamfe0e9f462017-09-18 21:25:0419#include "base/fuchsia/scoped_zx_handle.h"
Wez78b733132017-08-09 18:41:5920#endif
21
lgrey0d2bafc2016-11-07 16:28:3322#if defined(OS_MACOSX)
23#include "base/feature_list.h"
24#include "base/process/port_provider_mac.h"
25#endif
26
[email protected]76bea672013-07-19 16:48:5627namespace base {
28
lgrey0d2bafc2016-11-07 16:28:3329#if defined(OS_MACOSX)
30extern const Feature kMacAllowBackgroundingProcesses;
31#endif
32
rvargas079d1842014-10-17 22:32:1633// Provides a move-only encapsulation of a process.
34//
35// This object is not tied to the lifetime of the underlying process: the
36// process may be killed and this object may still around, and it will still
37// claim to be valid. The actual behavior in that case is OS dependent like so:
38//
39// Windows: The underlying ProcessHandle will be valid after the process dies
40// and can be used to gather some information about that process, but most
41// methods will obviously fail.
42//
Wez6fcf59f2017-08-02 21:09:0543// POSIX: The underlying ProcessHandle is not guaranteed to remain valid after
rvargas079d1842014-10-17 22:32:1644// the process dies, and it may be reused by the system, which means that it may
45// end up pointing to the wrong process.
[email protected]76bea672013-07-19 16:48:5646class BASE_EXPORT Process {
47 public:
erikchen7cef7d262017-09-11 22:56:5748 // On Windows, this takes ownership of |handle|. On POSIX, this does not take
49 // ownership of |handle|.
rvargas079d1842014-10-17 22:32:1650 explicit Process(ProcessHandle handle = kNullProcessHandle);
[email protected]76bea672013-07-19 16:48:5651
dchenge1b0277c2015-12-01 12:09:5252 Process(Process&& other);
[email protected]76bea672013-07-19 16:48:5653
rvargas079d1842014-10-17 22:32:1654 // The destructor does not terminate the process.
thakis3096dac2015-04-20 16:44:4855 ~Process();
rvargas079d1842014-10-17 22:32:1656
dchenge1b0277c2015-12-01 12:09:5257 Process& operator=(Process&& other);
rvargas079d1842014-10-17 22:32:1658
59 // Returns an object for the current process.
[email protected]76bea672013-07-19 16:48:5660 static Process Current();
61
rvargas6b039c372015-02-04 21:11:2962 // Returns a Process for the given |pid|.
63 static Process Open(ProcessId pid);
64
rvargas747ff242015-01-17 02:46:4765 // Returns a Process for the given |pid|. On Windows the handle is opened
66 // with more access rights and must only be used by trusted code (can read the
67 // address space and duplicate handles).
rvargas1c376a82015-03-16 23:03:5268 static Process OpenWithExtraPrivileges(ProcessId pid);
rvargas747ff242015-01-17 02:46:4769
rvargas17a407d2015-01-23 20:36:4470#if defined(OS_WIN)
71 // Returns a Process for the given |pid|, using some |desired_access|.
72 // See ::OpenProcess documentation for valid |desired_access|.
73 static Process OpenWithAccess(ProcessId pid, DWORD desired_access);
74#endif
75
rvargas5779b382014-11-18 20:44:1176 // Creates an object from a |handle| owned by someone else.
77 // Don't use this for new code. It is only intended to ease the migration to
78 // a strict ownership model.
79 // TODO(rvargas) crbug.com/417532: Remove this code.
80 static Process DeprecatedGetProcessFromHandle(ProcessHandle handle);
81
rvargas079d1842014-10-17 22:32:1682 // Returns true if processes can be backgrounded.
[email protected]76bea672013-07-19 16:48:5683 static bool CanBackgroundProcesses();
84
haraken940efb92017-02-08 05:58:1585 // Terminates the current process immediately with |exit_code|.
86 static void TerminateCurrentProcessImmediately(int exit_code);
87
rvargas079d1842014-10-17 22:32:1688 // Returns true if this objects represents a valid process.
89 bool IsValid() const;
90
91 // Returns a handle for this process. There is no guarantee about when that
92 // handle becomes invalid because this object retains ownership.
93 ProcessHandle Handle() const;
94
95 // Returns a second object that represents this process.
96 Process Duplicate() const;
[email protected]76bea672013-07-19 16:48:5697
98 // Get the PID for this process.
rvargas960db882015-01-24 00:27:2599 ProcessId Pid() const;
[email protected]76bea672013-07-19 16:48:56100
rvargas079d1842014-10-17 22:32:16101 // Returns true if this process is the current process.
[email protected]76bea672013-07-19 16:48:56102 bool is_current() const;
103
104 // Close the process handle. This will not terminate the process.
105 void Close();
106
Zijie He4dd88ae422017-09-20 01:30:18107 // Returns true if this process is still running. This is only safe on Windows
108 // (and maybe Fuchsia?), because the ProcessHandle will keep the zombie
109 // process information available until itself has been released. But on Posix,
110 // the OS may reuse the ProcessId.
111#if defined(OS_WIN)
112 bool IsRunning() const {
113 return !WaitForExitWithTimeout(base::TimeDelta(), nullptr);
114 }
115#endif
116
rvargas02ad7832015-04-02 01:36:06117 // Terminates the process with extreme prejudice. The given |exit_code| will
rvargaseedb763e2015-03-09 23:53:45118 // be the exit code of the process. If |wait| is true, this method will wait
119 // for up to one minute for the process to actually terminate.
120 // Returns true if the process terminates within the allowed time.
rvargas02ad7832015-04-02 01:36:06121 // NOTE: On POSIX |exit_code| is ignored.
122 bool Terminate(int exit_code, bool wait) const;
[email protected]76bea672013-07-19 16:48:56123
rvargas126fd5822014-12-12 00:25:14124 // Waits for the process to exit. Returns true on success.
125 // On POSIX, if the process has been signaled then |exit_code| is set to -1.
rvargas2f70a152015-02-24 00:28:11126 // On Linux this must be a child process, however on Mac and Windows it can be
127 // any process.
g.mehndirattfd19e232015-05-29 08:17:14128 // NOTE: |exit_code| is optional, nullptr can be passed if the exit code is
129 // not required.
jcivellif4462a352017-01-10 04:45:59130 bool WaitForExit(int* exit_code) const;
rvargas126fd5822014-12-12 00:25:14131
132 // Same as WaitForExit() but only waits for up to |timeout|.
g.mehndirattfd19e232015-05-29 08:17:14133 // NOTE: |exit_code| is optional, nullptr can be passed if the exit code
134 // is not required.
jcivellif4462a352017-01-10 04:45:59135 bool WaitForExitWithTimeout(TimeDelta timeout, int* exit_code) const;
rvargas126fd5822014-12-12 00:25:14136
Brian Whiteae2a8b9a2017-11-02 19:10:36137 // Indicates that the process has exited with the specified |exit_code|.
138 // This should be called if process exit is observed outside of this class.
139 // (i.e. Not because Terminate or WaitForExit, above, was called.)
140 // Note that nothing prevents this being called multiple times for a dead
141 // process though that should be avoided.
142 void Exited(int exit_code) const;
143
lgrey0d2bafc2016-11-07 16:28:33144#if defined(OS_MACOSX)
145 // The Mac needs a Mach port in order to manipulate a process's priority,
146 // and there's no good way to get that from base given the pid. These Mac
147 // variants of the IsProcessBackgrounded and SetProcessBackgrounded API take
148 // a port provider for this reason. See crbug.com/460102
149 //
150 // A process is backgrounded when its task priority is
151 // |TASK_BACKGROUND_APPLICATION|.
152 //
153 // Returns true if the port_provider can locate a task port for the process
154 // and it is backgrounded. If port_provider is null, returns false.
155 bool IsProcessBackgrounded(PortProvider* port_provider) const;
156
157 // Set the process as backgrounded. If value is
158 // true, the priority of the associated task will be set to
159 // TASK_BACKGROUND_APPLICATION. If value is false, the
160 // priority of the process will be set to TASK_FOREGROUND_APPLICATION.
161 //
162 // Returns true if the priority was changed, false otherwise. If
163 // |port_provider| is null, this is a no-op and it returns false.
164 bool SetProcessBackgrounded(PortProvider* port_provider, bool value);
lgrey0d2bafc2016-11-07 16:28:33165#else
[email protected]76bea672013-07-19 16:48:56166 // A process is backgrounded when it's priority is lower than normal.
167 // Return true if this process is backgrounded, false otherwise.
168 bool IsProcessBackgrounded() const;
169
rvargas079d1842014-10-17 22:32:16170 // Set a process as backgrounded. If value is true, the priority of the
171 // process will be lowered. If value is false, the priority of the process
172 // will be made "normal" - equivalent to default process priority.
[email protected]76bea672013-07-19 16:48:56173 // Returns true if the priority was changed, false otherwise.
174 bool SetProcessBackgrounded(bool value);
lgrey0d2bafc2016-11-07 16:28:33175#endif // defined(OS_MACOSX)
[email protected]76bea672013-07-19 16:48:56176 // Returns an integer representing the priority of a process. The meaning
177 // of this value is OS dependent.
178 int GetPriority() const;
179
nyad2c548b2015-12-09 03:22:32180#if defined(OS_CHROMEOS)
181 // Get the PID in its PID namespace.
182 // If the process is not in a PID namespace or /proc/<pid>/status does not
183 // report NSpid, kNullProcessId is returned.
184 ProcessId GetPidInNamespace() const;
185#endif
186
[email protected]76bea672013-07-19 16:48:56187 private:
rvargas079d1842014-10-17 22:32:16188#if defined(OS_WIN)
rvargas079d1842014-10-17 22:32:16189 win::ScopedHandle process_;
Wez78b733132017-08-09 18:41:59190#elif defined(OS_FUCHSIA)
Scott Grahamfe0e9f462017-09-18 21:25:04191 ScopedZxHandle process_;
rvargas079d1842014-10-17 22:32:16192#else
[email protected]76bea672013-07-19 16:48:56193 ProcessHandle process_;
rvargas079d1842014-10-17 22:32:16194#endif
dcheng1a2fd6cd2016-06-07 21:39:12195
scottmg297cc932017-05-24 03:45:58196#if defined(OS_WIN) || defined(OS_FUCHSIA)
197 bool is_current_process_;
198#endif
199
dcheng1a2fd6cd2016-06-07 21:39:12200 DISALLOW_COPY_AND_ASSIGN(Process);
[email protected]76bea672013-07-19 16:48:56201};
202
afakhry8b4796b2015-11-16 18:41:44203#if defined(OS_CHROMEOS)
204// Exposed for testing.
205// Given the contents of the /proc/<pid>/cgroup file, determine whether the
206// process is backgrounded or not.
207BASE_EXPORT bool IsProcessBackgroundedCGroup(
208 const StringPiece& cgroup_contents);
209#endif // defined(OS_CHROMEOS)
210
[email protected]76bea672013-07-19 16:48:56211} // namespace base
212
danakj0a448602015-03-10 00:31:16213#endif // BASE_PROCESS_PROCESS_H_