Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Side by Side Diff: base/process/process_posix.cc

Issue 651253002: Enforce handle ownership in base::Process (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 ProcessObject::ProcessObject(ProcessHandle handle) : process_(handle) {
17 CHECK_NE(handle, GetCurrentProcessHandle());
18 }
19
20 ProcessObject::ProcessObject(RValue other)
21 : process_(other.object->process_) {
22 other.object->Close();
23 }
24
25 ProcessObject& ProcessObject::operator=(RValue other) {
26 if (this != other.object) {
27 process_ = other.object->process_;
28 other.object->Close();
29 }
30 return *this;
31 }
32
33 // static
34 ProcessObject ProcessObject::Current() {
35 ProcessObject process;
36 process.process_ = GetCurrentProcessHandle();
37 return process.Pass();
38 }
39
40 #if !defined(OS_LINUX)
41 // static
42 bool ProcessObject::CanBackgroundProcesses() {
43 return false;
44 }
45 #endif // !defined(OS_LINUX)
46
47 bool ProcessObject::IsValid() const {
48 return process_ != kNullProcessHandle;
49 }
50
51 ProcessHandle ProcessObject::Handle() const {
52 return process_;
53 }
54
55 ProcessObject ProcessObject::Duplicate() const {
56 if (is_current())
57 return Current();
58
59 return ProcessObject(process_);
60 }
61
62 ProcessId ProcessObject::pid() const {
63 DCHECK(IsValid());
64 return GetProcId(process_);
65 }
66
67 bool ProcessObject::is_current() const {
68 return process_ == GetCurrentProcessHandle();
69 }
70
71 void ProcessObject::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.
76 }
77
78 void ProcessObject::Terminate(int result_code) {
79 // result_code isn't supportable.
80 DCHECK(IsValid());
81 // We don't wait here. It's the responsibility of other code to reap the
82 // child.
83 KillProcess(process_, result_code, false);
84 }
85
86 #if !defined(OS_LINUX)
87 bool ProcessObject::IsProcessBackgrounded() const {
88 // See SetProcessBackgrounded().
89 DCHECK(IsValid());
90 return false;
91 }
92
93 bool ProcessObject::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
96 // priority.
97 DCHECK(IsValid());
98 return false;
99 }
100 #endif // !defined(OS_LINUX)
101
102 int ProcessObject::GetPriority() const {
103 DCHECK(IsValid());
104 return getpriority(PRIO_PROCESS, process_);
105 }
106
107 // -----------------------------------------------------------------------------
108 // TODO(rvargas) crbug.com/417532: Remove Process::*
109
16 // static 110 // static
17 Process Process::Current() { 111 Process Process::Current() {
18 return Process(GetCurrentProcessHandle()); 112 return Process(GetCurrentProcessHandle());
19 } 113 }
20 114
21 ProcessId Process::pid() const { 115 ProcessId Process::pid() const {
22 if (process_ == 0) 116 if (process_ == 0)
23 return 0; 117 return 0;
24 118
25 return GetProcId(process_); 119 return GetProcId(process_);
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 } 158 }
65 159
66 #endif 160 #endif
67 161
68 int Process::GetPriority() const { 162 int Process::GetPriority() const {
69 DCHECK(process_); 163 DCHECK(process_);
70 return getpriority(PRIO_PROCESS, process_); 164 return getpriority(PRIO_PROCESS, process_);
71 } 165 }
72 166
73 } // namspace base 167 } // namspace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698