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

Side by Side Diff: util/posix/process_info_mac.cc

Issue 727973002: Move some parts of ProcessReader (in snapshot) to ProcessInfo (in util) (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Created 6 years, 1 month 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
(Empty)
1 // Copyright 2014 The Crashpad Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "util/posix/process_info.h"
16
17 #include <string.h>
18
19 #include "base/logging.h"
20 #include "base/mac/mach_logging.h"
21
22 namespace crashpad {
23
24 ProcessInfo::ProcessInfo() : kern_proc_info_(), initialized_() {
25 }
26
27 bool ProcessInfo::Initialize(pid_t pid) {
28 INITIALIZATION_STATE_SET_INITIALIZING(initialized_);
29
30 int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
31 size_t len = sizeof(kern_proc_info_);
32 if (sysctl(mib, arraysize(mib), &kern_proc_info_, &len, nullptr, 0) != 0) {
33 PLOG(ERROR) << "sysctl for pid " << pid;
34 return false;
35 }
36
37 // This sysctl does not return an error if the pid was not found. 10.9.5
38 // xnu-2422.115.4/bsd/kern/kern_sysctl.c sysctl_prochandle() calls
39 // xnu-2422.115.4/bsd/kern/kern_proc.c proc_iterate(), which provides no
40 // indication of whether anything was done. To catch this, check that the PID
41 // has changed from the 0 value it was given when initialized by the
42 // constructor.
43 if (kern_proc_info_.kp_proc.p_pid == 0) {
44 LOG(WARNING) << "pid " << pid << " not found";
45 return false;
46 }
47
48 DCHECK_EQ(kern_proc_info_.kp_proc.p_pid, pid);
49
50 INITIALIZATION_STATE_SET_VALID(initialized_);
51 return true;
52 }
53
54 bool ProcessInfo::InitializeFromTask(task_t task) {
55 pid_t pid;
56 kern_return_t kr = pid_for_task(task, &pid);
57 if (kr != KERN_SUCCESS) {
58 MACH_LOG(ERROR, kr) << "pid_for_task";
59 return false;
60 }
61
62 return Initialize(pid);
63 }
64
65 pid_t ProcessInfo::ProcessID() const {
66 INITIALIZATION_STATE_DCHECK_VALID(initialized_);
67 return kern_proc_info_.kp_proc.p_pid;
68 }
69
70 pid_t ProcessInfo::ParentProcessID() const {
71 INITIALIZATION_STATE_DCHECK_VALID(initialized_);
72 return kern_proc_info_.kp_eproc.e_ppid;
73 }
74
75 uid_t ProcessInfo::RealUserID() const {
76 INITIALIZATION_STATE_DCHECK_VALID(initialized_);
77 return kern_proc_info_.kp_eproc.e_pcred.p_ruid;
78 }
79
80 uid_t ProcessInfo::EffectiveUserID() const {
81 INITIALIZATION_STATE_DCHECK_VALID(initialized_);
82 return kern_proc_info_.kp_eproc.e_ucred.cr_uid;
83 }
84
85 uid_t ProcessInfo::SavedUserID() const {
86 INITIALIZATION_STATE_DCHECK_VALID(initialized_);
87 return kern_proc_info_.kp_eproc.e_pcred.p_svuid;
88 }
89
90 gid_t ProcessInfo::RealGroupID() const {
91 INITIALIZATION_STATE_DCHECK_VALID(initialized_);
92 return kern_proc_info_.kp_eproc.e_pcred.p_rgid;
93 }
94
95 gid_t ProcessInfo::EffectiveGroupID() const {
96 INITIALIZATION_STATE_DCHECK_VALID(initialized_);
97 return kern_proc_info_.kp_eproc.e_ucred.cr_gid;
98 }
99
100 gid_t ProcessInfo::SavedGroupID() const {
101 INITIALIZATION_STATE_DCHECK_VALID(initialized_);
102 return kern_proc_info_.kp_eproc.e_pcred.p_svgid;
103 }
104
105 std::set<gid_t> ProcessInfo::SupplementaryGroups() const {
106 INITIALIZATION_STATE_DCHECK_VALID(initialized_);
107
108 const short ngroups = kern_proc_info_.kp_eproc.e_ucred.cr_ngroups;
109 DCHECK_GE(ngroups, 0);
110 DCHECK_LT(static_cast<size_t>(ngroups),
111 arraysize(kern_proc_info_.kp_eproc.e_ucred.cr_groups));
112
113 const gid_t* groups = kern_proc_info_.kp_eproc.e_ucred.cr_groups;
114 return std::set<gid_t>(&groups[0], &groups[ngroups]);
115 }
116
117 std::set<gid_t> ProcessInfo::AllGroups() const {
118 INITIALIZATION_STATE_DCHECK_VALID(initialized_);
119
120 std::set<gid_t> all_groups = SupplementaryGroups();
121 all_groups.insert(RealGroupID());
122 all_groups.insert(EffectiveGroupID());
123 all_groups.insert(SavedGroupID());
124 return all_groups;
125 }
126
127 bool ProcessInfo::DidChangePrivileges() const {
128 INITIALIZATION_STATE_DCHECK_VALID(initialized_);
129 return kern_proc_info_.kp_proc.p_flag & P_SUGID;
130 }
131
132 bool ProcessInfo::Is64Bit() const {
133 INITIALIZATION_STATE_DCHECK_VALID(initialized_);
134 return kern_proc_info_.kp_proc.p_flag & P_LP64;
135 }
136
137 void ProcessInfo::StartTime(timeval* start_time) const {
138 INITIALIZATION_STATE_DCHECK_VALID(initialized_);
139 *start_time = kern_proc_info_.kp_proc.p_starttime;
140 }
141
142 bool ProcessInfo::Arguments(std::vector<std::string>* argv) const {
143 INITIALIZATION_STATE_DCHECK_VALID(initialized_);
144
145 // The format of KERN_PROCARGS2 is explained in 10.9.2 adv_cmds-153/ps/print.c
146 // getproclline(). It is an int (argc) followed by the executable’s string
147 // area. The string area consists of NUL-terminated strings, beginning with
148 // the executable path, and then starting on an aligned boundary, all of the
149 // elements of argv, envp, and applev.
150
151 // It is possible for a process to exec() in between the two sysctl() calls
152 // below. If that happens, and the string area of the new program is larger
153 // than that of the old one, args_size_estimate will be too small. To detect
154 // this situation, the second sysctl() attempts to fetch args_size_estimate +
155 // 1 bytes, expecting to only receive args_size_estimate. If it gets the extra
156 // byte, it indicates that the string area has grown, and the sysctl() pair
157 // will be retried a limited number of times.
158
159 size_t args_size_estimate;
160 size_t args_size;
161 std::string args;
162 int tries = 3;
163 const pid_t pid = ProcessID();
164 do {
165 int mib[] = {CTL_KERN, KERN_PROCARGS2, pid};
166 int rv =
167 sysctl(mib, arraysize(mib), nullptr, &args_size_estimate, nullptr, 0);
168 if (rv != 0) {
169 PLOG(ERROR) << "sysctl (size) for pid " << pid;
170 return false;
171 }
172
173 args_size = args_size_estimate + 1;
174 args.resize(args_size);
175 rv = sysctl(mib, arraysize(mib), &args[0], &args_size, nullptr, 0);
176 if (rv != 0) {
177 PLOG(ERROR) << "sysctl (data) for pid " << pid;
178 return false;
179 }
180 } while (args_size == args_size_estimate + 1 && tries--);
181
182 if (args_size == args_size_estimate + 1) {
183 LOG(ERROR) << "unexpected args_size";
184 return false;
185 }
186
187 // KERN_PROCARGS2 needs to at least contain argc.
188 if (args_size < sizeof(int)) {
189 LOG(ERROR) << "tiny args_size";
190 return false;
191 }
192 args.resize(args_size);
193
194 // Get argc.
195 int argc;
196 memcpy(&argc, &args[0], sizeof(argc));
197
198 // Find the end of the executable path.
199 size_t start_pos = sizeof(argc);
200 size_t nul_pos = args.find('\0', start_pos);
201 if (nul_pos == std::string::npos) {
202 LOG(ERROR) << "unterminated executable path";
203 return false;
204 }
205
206 // Find the beginning of the string area.
207 start_pos = args.find_first_not_of('\0', nul_pos);
208 if (start_pos == std::string::npos) {
209 LOG(ERROR) << "no string area";
210 return false;
211 }
212
213 std::vector<std::string> local_argv;
214 while (argc-- && nul_pos != std::string::npos) {
215 nul_pos = args.find('\0', start_pos);
216 local_argv.push_back(args.substr(start_pos, nul_pos - start_pos));
217 start_pos = nul_pos + 1;
218 }
219
220 if (argc >= 0) {
221 // Not every argument was recovered.
222 LOG(ERROR) << "did not recover all arguments";
223 return false;
224 }
225
226 argv->swap(local_argv);
227 return true;
228 }
229
230 } // namespace crashpad
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698