blob: 6e65ebfd6cf45ec8b966e3c70418f6729afe3fed [file] [log] [blame]
evanm@google.com0c9f1eb2008-10-15 05:49:16 +09001// Copyright (c) 2008 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
pvalchev@google.com1d919db2010-03-10 16:46:43 +09005#include "base/process.h"
6
7#include <sys/types.h>
8#include <sys/time.h>
davemoore@chromium.orgad59fcb2009-10-30 02:43:44 +09009#include <sys/resource.h>
10
evanm@google.com0c9f1eb2008-10-15 05:49:16 +090011#include "base/process_util.h"
erg@google.com6e67c1d2010-07-29 02:25:28 +090012#include "base/logging.h"
evanm@google.com0c9f1eb2008-10-15 05:49:16 +090013
brettw@google.comc60d9892008-11-14 12:25:15 +090014namespace base {
15
erg@google.comd00c64e2011-01-26 02:29:39 +090016// static
17Process Process::Current() {
18 return Process(GetCurrentProcessHandle());
19}
20
21ProcessId Process::pid() const {
22 if (process_ == 0)
23 return 0;
24
25 return GetProcId(process_);
26}
27
28bool Process::is_current() const {
29 return process_ == GetCurrentProcessHandle();
30}
31
brettw@google.comc60d9892008-11-14 12:25:15 +090032void Process::Close() {
33 process_ = 0;
davemoore@chromium.orgad59fcb2009-10-30 02:43:44 +090034 // if the process wasn't terminated (so we waited) or the state
thomasvl@chromium.org570d7442009-02-06 04:22:01 +090035 // wasn't already collected w/ a wait from process_utils, we're gonna
36 // end up w/ a zombie when it does finally exit.
brettw@google.comc60d9892008-11-14 12:25:15 +090037}
38
brettw@google.com67cbe882008-11-14 12:48:25 +090039void Process::Terminate(int result_code) {
thomasvl@chromium.org570d7442009-02-06 04:22:01 +090040 // result_code isn't supportable.
41 if (!process_)
42 return;
agl@chromium.org65d43102009-04-28 10:37:23 +090043 // We don't wait here. It's the responsibility of other code to reap the
44 // child.
45 KillProcess(process_, result_code, false);
brettw@google.comc60d9892008-11-14 12:25:15 +090046}
47
davemoore@chromium.orgad59fcb2009-10-30 02:43:44 +090048#if !defined(OS_LINUX)
brettw@google.comba3b5a32008-11-12 07:35:19 +090049bool Process::IsProcessBackgrounded() const {
davemoore@chromium.orgad59fcb2009-10-30 02:43:44 +090050 // See SetProcessBackgrounded().
evanm@google.com0c9f1eb2008-10-15 05:49:16 +090051 return false;
52}
53
54bool Process::SetProcessBackgrounded(bool value) {
davemoore@chromium.orgad59fcb2009-10-30 02:43:44 +090055 // POSIX only allows lowering the priority of a process, so if we
56 // were to lower it we wouldn't be able to raise it back to its initial
57 // priority.
58 return false;
evanm@google.com0c9f1eb2008-10-15 05:49:16 +090059}
davemoore@chromium.orgad59fcb2009-10-30 02:43:44 +090060#endif
evanm@google.com0c9f1eb2008-10-15 05:49:16 +090061
davemoore@chromium.orgad59fcb2009-10-30 02:43:44 +090062int Process::GetPriority() const {
63 DCHECK(process_);
64 return getpriority(PRIO_PROCESS, process_);
65}
66
brettw@google.comc60d9892008-11-14 12:25:15 +090067} // namspace base