OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/process/process.h" | 5 #include "base/process/process.h" |
6 | 6 |
7 #include <sys/resource.h> | 7 #include <sys/resource.h> |
8 #include <sys/time.h> | 8 #include <sys/time.h> |
9 #include <sys/types.h> | 9 #include <sys/types.h> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/process/kill.h" | 12 #include "base/process/kill.h" |
13 | 13 |
14 namespace base { | 14 namespace base { |
15 | 15 |
| 16 Process::Process(ProcessHandle handle) : process_(handle) { |
| 17 CHECK_NE(handle, GetCurrentProcessHandle()); |
| 18 } |
| 19 |
| 20 Process::Process(RValue other) |
| 21 : process_(other.object->process_) { |
| 22 other.object->Close(); |
| 23 } |
| 24 |
| 25 Process& Process::operator=(RValue other) { |
| 26 if (this != other.object) { |
| 27 process_ = other.object->process_; |
| 28 other.object->Close(); |
| 29 } |
| 30 return *this; |
| 31 } |
| 32 |
16 // static | 33 // static |
17 Process Process::Current() { | 34 Process Process::Current() { |
18 return Process(GetCurrentProcessHandle()); | 35 Process process; |
| 36 process.process_ = GetCurrentProcessHandle(); |
| 37 return process.Pass(); |
| 38 } |
| 39 |
| 40 #if !defined(OS_LINUX) |
| 41 // static |
| 42 bool Process::CanBackgroundProcesses() { |
| 43 return false; |
| 44 } |
| 45 #endif // !defined(OS_LINUX) |
| 46 |
| 47 bool Process::IsValid() const { |
| 48 return process_ != kNullProcessHandle; |
| 49 } |
| 50 |
| 51 ProcessHandle Process::Handle() const { |
| 52 return process_; |
| 53 } |
| 54 |
| 55 Process Process::Duplicate() const { |
| 56 if (is_current()) |
| 57 return Current(); |
| 58 |
| 59 return Process(process_); |
19 } | 60 } |
20 | 61 |
21 ProcessId Process::pid() const { | 62 ProcessId Process::pid() const { |
22 if (process_ == 0) | 63 DCHECK(IsValid()); |
23 return 0; | |
24 | |
25 return GetProcId(process_); | 64 return GetProcId(process_); |
26 } | 65 } |
27 | 66 |
28 bool Process::is_current() const { | 67 bool Process::is_current() const { |
29 return process_ == GetCurrentProcessHandle(); | 68 return process_ == GetCurrentProcessHandle(); |
30 } | 69 } |
31 | 70 |
32 void Process::Close() { | 71 void Process::Close() { |
33 process_ = 0; | 72 process_ = kNullProcessHandle; |
34 // if the process wasn't terminated (so we waited) or the state | 73 // if the process wasn't terminated (so we waited) or the state |
35 // wasn't already collected w/ a wait from process_utils, we're gonna | 74 // wasn't already collected w/ a wait from process_utils, we're gonna |
36 // end up w/ a zombie when it does finally exit. | 75 // end up w/ a zombie when it does finally exit. |
37 } | 76 } |
38 | 77 |
39 void Process::Terminate(int result_code) { | 78 void Process::Terminate(int result_code) { |
40 // result_code isn't supportable. | 79 // result_code isn't supportable. |
41 if (!process_) | 80 DCHECK(IsValid()); |
42 return; | |
43 // We don't wait here. It's the responsibility of other code to reap the | 81 // We don't wait here. It's the responsibility of other code to reap the |
44 // child. | 82 // child. |
45 KillProcess(process_, result_code, false); | 83 KillProcess(process_, result_code, false); |
46 } | 84 } |
47 | 85 |
48 #if !defined(OS_LINUX) | 86 #if !defined(OS_LINUX) |
49 bool Process::IsProcessBackgrounded() const { | 87 bool Process::IsProcessBackgrounded() const { |
50 // See SetProcessBackgrounded(). | 88 // See SetProcessBackgrounded(). |
| 89 DCHECK(IsValid()); |
51 return false; | 90 return false; |
52 } | 91 } |
53 | 92 |
54 bool Process::SetProcessBackgrounded(bool value) { | 93 bool Process::SetProcessBackgrounded(bool value) { |
55 // POSIX only allows lowering the priority of a process, so if we | 94 // 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 | 95 // were to lower it we wouldn't be able to raise it back to its initial |
57 // priority. | 96 // priority. |
| 97 DCHECK(IsValid()); |
58 return false; | 98 return false; |
59 } | 99 } |
60 | 100 #endif // !defined(OS_LINUX) |
61 // static | |
62 bool Process::CanBackgroundProcesses() { | |
63 return false; | |
64 } | |
65 | |
66 #endif | |
67 | 101 |
68 int Process::GetPriority() const { | 102 int Process::GetPriority() const { |
69 DCHECK(process_); | 103 DCHECK(IsValid()); |
70 return getpriority(PRIO_PROCESS, process_); | 104 return getpriority(PRIO_PROCESS, process_); |
71 } | 105 } |
72 | 106 |
73 } // namspace base | 107 } // namspace base |
OLD | NEW |