| 6 | #define BASE_PROCESS_PROCESS_H_ |
| 7 |
| 8 | #include "base/base_export.h" |
| 9 | #include "base/macros.h" |
| 10 | #include "base/process/process_handle.h" |
| 11 | #include "base/time/time.h" |
| 12 | #include "build/build_config.h" |
| 13 |
| 14 | #if defined(OS_WIN) |
| 15 | #include "base/win/scoped_handle.h" |
| 16 | #endif |
| 17 |
| 18 | #if defined(OS_FUCHSIA) |
| 19 | #include <lib/zx/process.h> |
| 20 | #endif |
| 21 |
| 22 | #if defined(OS_MACOSX) |
| 23 | #include "base/feature_list.h" |
| 24 | #include "base/process/port_provider_mac.h" |
| 25 | #endif |
| 26 |
| 27 | namespace base { |
| 28 |
| 29 | #if defined(OS_MACOSX) |
| 30 | extern const Feature kMacAllowBackgroundingProcesses; |
| 31 | #endif |
| 32 |
| 33 | // 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 | // |
| 43 | // POSIX: The underlying ProcessHandle is not guaranteed to remain valid after |
| 44 | // the process dies, and it may be reused by the system, which means that it may |
| 45 | // end up pointing to the wrong process. |
| 46 | class BASE_EXPORT Process { |
| 47 | public: |
| 48 | // On Windows, this takes ownership of |handle|. On POSIX, this does not take |
| 49 | // ownership of |handle|. |
| 50 | explicit Process(ProcessHandle handle = kNullProcessHandle); |
| 51 |
| 52 | Process(Process&& other); |
| 53 |
| 54 | // The destructor does not terminate the process. |
| 55 | ~Process(); |
| 56 |
| 57 | Process& operator=(Process&& other); |
| 58 |
| 59 | // Returns an object for the current process. |
| 60 | static Process Current(); |
| 61 |
| 62 | // Returns a Process for the given |pid|. |
| 63 | static Process Open(ProcessId pid); |
| 64 |
| 65 | // 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). |
| 68 | static Process OpenWithExtraPrivileges(ProcessId pid); |
| 69 |
| 70 | #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 |
| 76 | // 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 |
| 82 | // Returns true if processes can be backgrounded. |
| 83 | static bool CanBackgroundProcesses(); |
| 84 |
| 85 | // Terminates the current process immediately with |exit_code|. |
| 86 | [[noreturn]] static void TerminateCurrentProcessImmediately(int exit_code); |
| 87 |
| 88 | // 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; |
| 97 |
| 98 | // Get the PID for this process. |
| 99 | ProcessId Pid() const; |
| 100 |
| 101 | #if !defined(OS_ANDROID) |
| 102 | // Get the creation time for this process. Since the Pid can be reused after a |
| 103 | // process dies, it is useful to use both the Pid and the creation time to |
| 104 | // uniquely identify a process. |
| 105 | // |
| 106 | // Not available on Android because /proc/stat/ cannot be accessed on O+. |
| 107 | // https://issuetracker.google.com/issues/37140047 |
| 108 | Time CreationTime() const; |
| 109 | #endif // !defined(OS_ANDROID) |
| 110 |
| 111 | // Returns true if this process is the current process. |
| 112 | bool is_current() const; |
| 113 |
| 114 | // Close the process handle. This will not terminate the process. |
| 115 | void Close(); |
| 116 |
| 117 | // Returns true if this process is still running. This is only safe on Windows |
| 118 | // (and maybe Fuchsia?), because the ProcessHandle will keep the zombie |
| 119 | // process information available until itself has been released. But on Posix, |
| 120 | // the OS may reuse the ProcessId. |
| 121 | #if defined(OS_WIN) |
| 122 | bool IsRunning() const { |
| 123 | return !WaitForExitWithTimeout(base::TimeDelta(), nullptr); |
| 124 | } |
| 125 | #endif |
| 126 |
| 127 | // Terminates the process with extreme prejudice. The given |exit_code| will |
| 128 | // be the exit code of the process. If |wait| is true, this method will wait |
| 129 | // for up to one minute for the process to actually terminate. |
| 130 | // Returns true if the process terminates within the allowed time. |
| 131 | // NOTE: On POSIX |exit_code| is ignored. |
| 132 | bool Terminate(int exit_code, bool wait) const; |
| 133 |
| 134 | #if defined(OS_WIN) |
| 135 | enum class WaitExitStatus { |
| 136 | PROCESS_EXITED, |
| 137 | STOP_EVENT_SIGNALED, |
| 138 | FAILED, |
| 139 | }; |
| 140 |
| 141 | // Waits for the process to exit, or the specified |stop_event_handle| to be |
| 142 | // set. Returns value indicating which event was set. The given |exit_code| |
| 143 | // will be the exit code of the process. |
| 144 | WaitExitStatus WaitForExitOrEvent( |
| 145 | const base::win::ScopedHandle& stop_event_handle, |
| 146 | int* exit_code) const; |
| 147 | #endif // OS_WIN |
| 148 |
| 149 | // Waits for the process to exit. Returns true on success. |
| 150 | // On POSIX, if the process has been signaled then |exit_code| is set to -1. |
| 151 | // On Linux this must be a child process, however on Mac and Windows it can be |
| 152 | // any process. |
| 153 | // NOTE: |exit_code| is optional, nullptr can be passed if the exit code is |
| 154 | // not required. |
| 155 | bool WaitForExit(int* exit_code) const; |
| 156 |
| 157 | // Same as WaitForExit() but only waits for up to |timeout|. |
| 158 | // NOTE: |exit_code| is optional, nullptr can be passed if the exit code |
| 159 | // is not required. |
| 160 | bool WaitForExitWithTimeout(TimeDelta timeout, int* exit_code) const; |
| 161 |
| 162 | // Indicates that the process has exited with the specified |exit_code|. |
| 163 | // This should be called if process exit is observed outside of this class. |
| 164 | // (i.e. Not because Terminate or WaitForExit, above, was called.) |
| 165 | // Note that nothing prevents this being called multiple times for a dead |
| 166 | // process though that should be avoided. |
| 167 | void Exited(int exit_code) const; |
| 168 |
| 169 | #if defined(OS_MACOSX) |
| 170 | // The Mac needs a Mach port in order to manipulate a process's priority, |
| 171 | // and there's no good way to get that from base given the pid. These Mac |
| 172 | // variants of the IsProcessBackgrounded and SetProcessBackgrounded API take |
| 173 | // a port provider for this reason. See crbug.com/460102 |
| 174 | // |
| 175 | // A process is backgrounded when its task priority is |
| 176 | // |TASK_BACKGROUND_APPLICATION|. |
| 177 | // |
| 178 | // Returns true if the port_provider can locate a task port for the process |
| 179 | // and it is backgrounded. If port_provider is null, returns false. |
| 180 | bool IsProcessBackgrounded(PortProvider* port_provider) const; |
| 181 |
| 182 | // Set the process as backgrounded. If value is |
| 183 | // true, the priority of the associated task will be set to |
| 184 | // TASK_BACKGROUND_APPLICATION. If value is false, the |
| 185 | // priority of the process will be set to TASK_FOREGROUND_APPLICATION. |
| 186 | // |
| 187 | // Returns true if the priority was changed, false otherwise. If |
| 188 | // |port_provider| is null, this is a no-op and it returns false. |
| 189 | bool SetProcessBackgrounded(PortProvider* port_provider, bool value); |
| 190 | #else |
| 191 | // A process is backgrounded when it's priority is lower than normal. |
| 192 | // Return true if this process is backgrounded, false otherwise. |
| 193 | bool IsProcessBackgrounded() const; |
| 194 |
| 195 | // Set a process as backgrounded. If value is true, the priority of the |
| 196 | // process will be lowered. If value is false, the priority of the process |
| 197 | // will be made "normal" - equivalent to default process priority. |
| 198 | // Returns true if the priority was changed, false otherwise. |
| 199 | bool SetProcessBackgrounded(bool value); |
| 200 | #endif // defined(OS_MACOSX) |
| 201 | // Returns an integer representing the priority of a process. The meaning |
| 202 | // of this value is OS dependent. |
| 203 | int GetPriority() const; |
| 204 |
| 205 | #if defined(OS_CHROMEOS) |
| 206 | // Get the PID in its PID namespace. |
| 207 | // If the process is not in a PID namespace or /proc/<pid>/status does not |
| 208 | // report NSpid, kNullProcessId is returned. |
| 209 | ProcessId GetPidInNamespace() const; |
| 210 | #endif |
| 211 |
| 212 | private: |
| 213 | #if defined(OS_WIN) |
| 214 | win::ScopedHandle process_; |
| 215 | #elif defined(OS_FUCHSIA) |
| 216 | zx::process process_; |
| 217 | #else |
| 218 | ProcessHandle process_; |
| 219 | #endif |
| 220 |
| 221 | #if defined(OS_WIN) || defined(OS_FUCHSIA) |
| 222 | bool is_current_process_; |
| 223 | #endif |
| 224 |
| 225 | DISALLOW_COPY_AND_ASSIGN(Process); |
| 226 | }; |
| 227 |
| 228 | #if defined(OS_CHROMEOS) |
| 229 | // Exposed for testing. |
| 230 | // Given the contents of the /proc/<pid>/cgroup file, determine whether the |
| 231 | // process is backgrounded or not. |
| 232 | BASE_EXPORT bool IsProcessBackgroundedCGroup( |
| 233 | const StringPiece& cgroup_contents); |
| 234 | #endif // defined(OS_CHROMEOS) |
| 235 |
| 236 | } // namespace base |
| 237 |
| 238 | #endif // BASE_PROCESS_PROCESS_H_ |
| 239 |