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
) {
17 CHECK_NE(handle
, GetCurrentProcessHandle());
20 Process::Process(RValue other
)
21 : process_(other
.object
->process_
) {
22 other
.object
->Close();
25 Process
& Process::operator=(RValue other
) {
26 if (this != other
.object
) {
27 process_
= other
.object
->process_
;
28 other
.object
->Close();
34 Process
Process::Current() {
36 process
.process_
= GetCurrentProcessHandle();
37 return process
.Pass();
40 #if !defined(OS_LINUX)
42 bool Process::CanBackgroundProcesses() {
45 #endif // !defined(OS_LINUX)
47 bool Process::IsValid() const {
48 return process_
!= kNullProcessHandle
;
51 ProcessHandle
Process::Handle() const {
55 Process
Process::Duplicate() const {
59 return Process(process_
);
62 ProcessId
Process::pid() const {
64 return GetProcId(process_
);
67 bool Process::is_current() const {
68 return process_
== GetCurrentProcessHandle();
71 void Process::Close() {
72 process_
= kNullProcessHandle
;
73 // if the process wasn't terminated (so we waited) or the state
74 // wasn't already collected w/ a wait from process_utils, we're gonna
75 // end up w/ a zombie when it does finally exit.
78 void Process::Terminate(int result_code
) {
79 // result_code isn't supportable.
81 // We don't wait here. It's the responsibility of other code to reap the
83 KillProcess(process_
, result_code
, false);
86 #if !defined(OS_LINUX)
87 bool Process::IsProcessBackgrounded() const {
88 // See SetProcessBackgrounded().
93 bool Process::SetProcessBackgrounded(bool value
) {
94 // POSIX only allows lowering the priority of a process, so if we
95 // were to lower it we wouldn't be able to raise it back to its initial
100 #endif // !defined(OS_LINUX)
102 int Process::GetPriority() const {
104 return getpriority(PRIO_PROCESS
, process_
);