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