1 // Copyright (c) 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.
5 #include "base/process/process.h"
7 #include <sys/resource.h>
11 #include "base/logging.h"
12 #include "base/process/kill.h"
16 Process::Process(ProcessHandle handle
) : process_(handle
) {
19 Process::Process(RValue other
)
20 : process_(other
.object
->process_
) {
21 other
.object
->Close();
24 Process
& Process::operator=(RValue other
) {
25 if (this != other
.object
) {
26 process_
= other
.object
->process_
;
27 other
.object
->Close();
33 Process
Process::Current() {
34 return Process(GetCurrentProcessHandle());
38 Process
Process::Open(ProcessId pid
) {
39 if (pid
== GetCurrentProcId())
42 // On POSIX process handles are the same as PIDs.
47 Process
Process::OpenWithExtraPriviles(ProcessId pid
) {
48 // On POSIX there are no privileges to set.
53 Process
Process::DeprecatedGetProcessFromHandle(ProcessHandle handle
) {
54 DCHECK_NE(handle
, GetCurrentProcessHandle());
55 return Process(handle
);
58 #if !defined(OS_LINUX)
60 bool Process::CanBackgroundProcesses() {
63 #endif // !defined(OS_LINUX)
65 bool Process::IsValid() const {
66 return process_
!= kNullProcessHandle
;
69 ProcessHandle
Process::Handle() const {
73 Process
Process::Duplicate() const {
77 return Process(process_
);
80 ProcessId
Process::Pid() const {
82 return GetProcId(process_
);
85 bool Process::is_current() const {
86 return process_
== GetCurrentProcessHandle();
89 void Process::Close() {
90 process_
= kNullProcessHandle
;
91 // if the process wasn't terminated (so we waited) or the state
92 // wasn't already collected w/ a wait from process_utils, we're gonna
93 // end up w/ a zombie when it does finally exit.
96 void Process::Terminate(int result_code
) {
97 // result_code isn't supportable.
99 // We don't wait here. It's the responsibility of other code to reap the
101 KillProcess(process_
, result_code
, false);
104 bool Process::WaitForExit(int* exit_code
) {
105 // TODO(rvargas) crbug.com/417532: Remove this constant.
106 const int kNoTimeout
= -1;
107 return WaitForExitWithTimeout(TimeDelta::FromMilliseconds(kNoTimeout
),
111 bool Process::WaitForExitWithTimeout(TimeDelta timeout
, int* exit_code
) {
112 // TODO(rvargas) crbug.com/417532: Move the implementation here.
113 return base::WaitForExitCodeWithTimeout(Handle(), exit_code
, timeout
);
116 #if !defined(OS_LINUX)
117 bool Process::IsProcessBackgrounded() const {
118 // See SetProcessBackgrounded().
123 bool Process::SetProcessBackgrounded(bool value
) {
124 // POSIX only allows lowering the priority of a process, so if we
125 // were to lower it we wouldn't be able to raise it back to its initial
130 #endif // !defined(OS_LINUX)
132 int Process::GetPriority() const {
134 return getpriority(PRIO_PROCESS
, process_
);