evanm@google.com | 0c9f1eb | 2008-10-15 05:49:16 +0900 | [diff] [blame] | 1 | // 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.com | 1d919db | 2010-03-10 16:46:43 +0900 | [diff] [blame] | 5 | #include "base/process.h" |
| 6 | |
| 7 | #include <sys/types.h> |
| 8 | #include <sys/time.h> |
davemoore@chromium.org | ad59fcb | 2009-10-30 02:43:44 +0900 | [diff] [blame] | 9 | #include <sys/resource.h> |
| 10 | |
evanm@google.com | 0c9f1eb | 2008-10-15 05:49:16 +0900 | [diff] [blame] | 11 | #include "base/process_util.h" |
erg@google.com | 6e67c1d | 2010-07-29 02:25:28 +0900 | [diff] [blame] | 12 | #include "base/logging.h" |
evanm@google.com | 0c9f1eb | 2008-10-15 05:49:16 +0900 | [diff] [blame] | 13 | |
brettw@google.com | c60d989 | 2008-11-14 12:25:15 +0900 | [diff] [blame] | 14 | namespace base { |
| 15 | |
erg@google.com | d00c64e | 2011-01-26 02:29:39 +0900 | [diff] [blame] | 16 | // static |
| 17 | Process Process::Current() { |
| 18 | return Process(GetCurrentProcessHandle()); |
| 19 | } |
| 20 | |
| 21 | ProcessId Process::pid() const { |
| 22 | if (process_ == 0) |
| 23 | return 0; |
| 24 | |
| 25 | return GetProcId(process_); |
| 26 | } |
| 27 | |
| 28 | bool Process::is_current() const { |
| 29 | return process_ == GetCurrentProcessHandle(); |
| 30 | } |
| 31 | |
brettw@google.com | c60d989 | 2008-11-14 12:25:15 +0900 | [diff] [blame] | 32 | void Process::Close() { |
| 33 | process_ = 0; |
davemoore@chromium.org | ad59fcb | 2009-10-30 02:43:44 +0900 | [diff] [blame] | 34 | // if the process wasn't terminated (so we waited) or the state |
thomasvl@chromium.org | 570d744 | 2009-02-06 04:22:01 +0900 | [diff] [blame] | 35 | // 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.com | c60d989 | 2008-11-14 12:25:15 +0900 | [diff] [blame] | 37 | } |
| 38 | |
brettw@google.com | 67cbe88 | 2008-11-14 12:48:25 +0900 | [diff] [blame] | 39 | void Process::Terminate(int result_code) { |
thomasvl@chromium.org | 570d744 | 2009-02-06 04:22:01 +0900 | [diff] [blame] | 40 | // result_code isn't supportable. |
| 41 | if (!process_) |
| 42 | return; |
agl@chromium.org | 65d4310 | 2009-04-28 10:37:23 +0900 | [diff] [blame] | 43 | // 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.com | c60d989 | 2008-11-14 12:25:15 +0900 | [diff] [blame] | 46 | } |
| 47 | |
davemoore@chromium.org | ad59fcb | 2009-10-30 02:43:44 +0900 | [diff] [blame] | 48 | #if !defined(OS_LINUX) |
brettw@google.com | ba3b5a3 | 2008-11-12 07:35:19 +0900 | [diff] [blame] | 49 | bool Process::IsProcessBackgrounded() const { |
davemoore@chromium.org | ad59fcb | 2009-10-30 02:43:44 +0900 | [diff] [blame] | 50 | // See SetProcessBackgrounded(). |
evanm@google.com | 0c9f1eb | 2008-10-15 05:49:16 +0900 | [diff] [blame] | 51 | return false; |
| 52 | } |
| 53 | |
| 54 | bool Process::SetProcessBackgrounded(bool value) { |
davemoore@chromium.org | ad59fcb | 2009-10-30 02:43:44 +0900 | [diff] [blame] | 55 | // 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.com | 0c9f1eb | 2008-10-15 05:49:16 +0900 | [diff] [blame] | 59 | } |
davemoore@chromium.org | ad59fcb | 2009-10-30 02:43:44 +0900 | [diff] [blame] | 60 | #endif |
evanm@google.com | 0c9f1eb | 2008-10-15 05:49:16 +0900 | [diff] [blame] | 61 | |
davemoore@chromium.org | ad59fcb | 2009-10-30 02:43:44 +0900 | [diff] [blame] | 62 | int Process::GetPriority() const { |
| 63 | DCHECK(process_); |
| 64 | return getpriority(PRIO_PROCESS, process_); |
| 65 | } |
| 66 | |
brettw@google.com | c60d989 | 2008-11-14 12:25:15 +0900 | [diff] [blame] | 67 | } // namspace base |