• Home
  • History
  • Annotate
  • 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 namespace base {
19 
20 // Provides a move-only encapsulation of a process.
21 //
22 // This object is not tied to the lifetime of the underlying process: the
23 // process may be killed and this object may still around, and it will still
24 // claim to be valid. The actual behavior in that case is OS dependent like so:
25 //
26 // Windows: The underlying ProcessHandle will be valid after the process dies
27 // and can be used to gather some information about that process, but most
28 // methods will obviously fail.
29 //
30 // POSIX: The underlying PorcessHandle is not guaranteed to remain valid after
31 // the process dies, and it may be reused by the system, which means that it may
32 // end up pointing to the wrong process.
33 class BASE_EXPORT Process {
34  public:
35   explicit Process(ProcessHandle handle = kNullProcessHandle);
36 
37   Process(Process&& other);
38 
39   // The destructor does not terminate the process.
40   ~Process();
41 
42   Process& operator=(Process&& other);
43 
44   // Returns an object for the current process.
45   static Process Current();
46 
47   // Returns a Process for the given |pid|.
48   static Process Open(ProcessId pid);
49 
50   // Returns a Process for the given |pid|. On Windows the handle is opened
51   // with more access rights and must only be used by trusted code (can read the
52   // address space and duplicate handles).
53   static Process OpenWithExtraPrivileges(ProcessId pid);
54 
55 #if defined(OS_WIN)
56   // Returns a Process for the given |pid|, using some |desired_access|.
57   // See ::OpenProcess documentation for valid |desired_access|.
58   static Process OpenWithAccess(ProcessId pid, DWORD desired_access);
59 #endif
60 
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 
67   // Returns true if processes can be backgrounded.
68   static bool CanBackgroundProcesses();
69 
70   // Returns true if this objects represents a valid process.
71   bool IsValid() const;
72 
73   // Returns a handle for this process. There is no guarantee about when that
74   // handle becomes invalid because this object retains ownership.
75   ProcessHandle Handle() const;
76 
77   // Returns a second object that represents this process.
78   Process Duplicate() const;
79 
80   // Get the PID for this process.
81   ProcessId Pid() const;
82 
83   // Returns true if this process is the current process.
84   bool is_current() const;
85 
86   // Close the process handle. This will not terminate the process.
87   void Close();
88 
89   // Terminates the process with extreme prejudice. The given |exit_code| will
90   // be the exit code of the process. If |wait| is true, this method will wait
91   // for up to one minute for the process to actually terminate.
92   // Returns true if the process terminates within the allowed time.
93   // NOTE: On POSIX |exit_code| is ignored.
94   bool Terminate(int exit_code, bool wait) const;
95 
96   // Waits for the process to exit. Returns true on success.
97   // On POSIX, if the process has been signaled then |exit_code| is set to -1.
98   // On Linux this must be a child process, however on Mac and Windows it can be
99   // any process.
100   // NOTE: |exit_code| is optional, nullptr can be passed if the exit code is
101   // not required.
102   bool WaitForExit(int* exit_code);
103 
104   // Same as WaitForExit() but only waits for up to |timeout|.
105   // NOTE: |exit_code| is optional, nullptr can be passed if the exit code
106   // is not required.
107   bool WaitForExitWithTimeout(TimeDelta timeout, int* exit_code);
108 
109   // A process is backgrounded when it's priority is lower than normal.
110   // Return true if this process is backgrounded, false otherwise.
111   bool IsProcessBackgrounded() const;
112 
113   // Set a process as backgrounded. If value is true, the priority of the
114   // process will be lowered. If value is false, the priority of the process
115   // will be made "normal" - equivalent to default process priority.
116   // Returns true if the priority was changed, false otherwise.
117   bool SetProcessBackgrounded(bool value);
118 
119   // Returns an integer representing the priority of a process. The meaning
120   // of this value is OS dependent.
121   int GetPriority() const;
122 
123 #if defined(OS_CHROMEOS)
124   // Get the PID in its PID namespace.
125   // If the process is not in a PID namespace or /proc/<pid>/status does not
126   // report NSpid, kNullProcessId is returned.
127   ProcessId GetPidInNamespace() const;
128 #endif
129 
130  private:
131 #if defined(OS_WIN)
132   bool is_current_process_;
133   win::ScopedHandle process_;
134 #else
135   ProcessHandle process_;
136 #endif
137 
138   DISALLOW_COPY_AND_ASSIGN(Process);
139 };
140 
141 #if defined(OS_CHROMEOS)
142 // Exposed for testing.
143 // Given the contents of the /proc/<pid>/cgroup file, determine whether the
144 // process is backgrounded or not.
145 BASE_EXPORT bool IsProcessBackgroundedCGroup(
146     const StringPiece& cgroup_contents);
147 #endif  // defined(OS_CHROMEOS)
148 
149 }  // namespace base
150 
151 #endif  // BASE_PROCESS_PROCESS_H_
152