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