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

Side by Side Diff: util/posix/process_util_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: Address review feedback 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
« no previous file with comments | « util/posix/process_util.h ('k') | util/posix/process_util_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_util.h"
16
17 #include <string.h>
18 #include <sys/sysctl.h>
19 #include <sys/types.h>
20
21 #include "base/basictypes.h"
22
23 namespace crashpad {
24
25 bool ProcessArgumentsForPID(pid_t pid, std::vector<std::string>* argv) {
26 // The format of KERN_PROCARGS2 is explained in 10.9.2 adv_cmds-153/ps/print.c
27 // getproclline(). It is an int (argc) followed by the executable’s string
28 // area. The string area consists of NUL-terminated strings, beginning with
29 // the executable path, and then starting on an aligned boundary, all of the
30 // elements of argv, envp, and applev.
31
32 // It is possible for a process to exec() in between the two sysctl() calls
33 // below. If that happens, and the string area of the new program is larger
34 // than that of the old one, args_size_estimate will be too small. To detect
35 // this situation, the second sysctl() attempts to fetch args_size_estimate +
36 // 1 bytes, expecting to only receive args_size_estimate. If it gets the extra
37 // byte, it indicates that the string area has grown, and the sysctl() pair
38 // will be retried a limited number of times.
39
40 size_t args_size_estimate;
41 size_t args_size;
42 std::string args;
43 int tries = 3;
44 do {
45 int mib[] = {CTL_KERN, KERN_PROCARGS2, pid};
46 int rv =
47 sysctl(mib, arraysize(mib), nullptr, &args_size_estimate, nullptr, 0);
48 if (rv != 0) {
49 return false;
50 }
51
52 args_size = args_size_estimate + 1;
53 args.resize(args_size);
54 rv = sysctl(mib, arraysize(mib), &args[0], &args_size, nullptr, 0);
55 if (rv != 0) {
56 return false;
57 }
58 } while (args_size == args_size_estimate + 1 && tries--);
59
60 if (args_size == args_size_estimate + 1) {
61 return false;
62 }
63
64 // KERN_PROCARGS2 needs to at least contain argc.
65 if (args_size < sizeof(int)) {
66 return false;
67 }
68 args.resize(args_size);
69
70 // Get argc.
71 int argc;
72 memcpy(&argc, &args[0], sizeof(argc));
73
74 // Find the end of the executable path.
75 size_t start_pos = sizeof(argc);
76 size_t nul_pos = args.find('\0', start_pos);
77 if (nul_pos == std::string::npos) {
78 return false;
79 }
80
81 // Find the beginning of the string area.
82 start_pos = args.find_first_not_of('\0', nul_pos);
83 if (start_pos == std::string::npos) {
84 return false;
85 }
86
87 std::vector<std::string> local_argv;
88 while (argc-- && nul_pos != std::string::npos) {
89 nul_pos = args.find('\0', start_pos);
90 local_argv.push_back(args.substr(start_pos, nul_pos - start_pos));
91 start_pos = nul_pos + 1;
92 }
93
94 if (argc >= 0) {
95 // Not every argument was recovered.
96 return false;
97 }
98
99 argv->swap(local_argv);
100 return true;
101 }
102
103 } // namespace crashpad
OLDNEW
« no previous file with comments | « util/posix/process_util.h ('k') | util/posix/process_util_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698