OLD | NEW |
(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 = sysctl(mib, arraysize(mib), NULL, &args_size_estimate, NULL, 0); |
| 47 if (rv != 0) { |
| 48 return false; |
| 49 } |
| 50 |
| 51 args_size = args_size_estimate + 1; |
| 52 args.resize(args_size); |
| 53 rv = sysctl(mib, arraysize(mib), &args[0], &args_size, NULL, 0); |
| 54 if (rv != 0) { |
| 55 return false; |
| 56 } |
| 57 } while (args_size == args_size_estimate + 1 && tries--); |
| 58 |
| 59 if (args_size == args_size_estimate + 1) { |
| 60 return false; |
| 61 } |
| 62 |
| 63 // KERN_PROCARGS2 needs to at least contain argc. |
| 64 if (args_size < sizeof(int)) { |
| 65 return false; |
| 66 } |
| 67 args.resize(args_size); |
| 68 |
| 69 // Get argc. |
| 70 int argc; |
| 71 memcpy(&argc, &args[0], sizeof(argc)); |
| 72 |
| 73 // Find the end of the executable path. |
| 74 size_t start_pos = sizeof(argc); |
| 75 size_t nul_pos = args.find('\0', start_pos); |
| 76 if (nul_pos == std::string::npos) { |
| 77 return false; |
| 78 } |
| 79 |
| 80 // Find the beginning of the string area. |
| 81 start_pos = args.find_first_not_of('\0', nul_pos); |
| 82 if (start_pos == std::string::npos) { |
| 83 return false; |
| 84 } |
| 85 |
| 86 std::vector<std::string> local_argv; |
| 87 while (argc-- && nul_pos != std::string::npos) { |
| 88 nul_pos = args.find('\0', start_pos); |
| 89 local_argv.push_back(args.substr(start_pos, nul_pos - start_pos)); |
| 90 start_pos = nul_pos + 1; |
| 91 } |
| 92 |
| 93 if (argc >= 0) { |
| 94 // Not every argument was recovered. |
| 95 return false; |
| 96 } |
| 97 |
| 98 argv->swap(local_argv); |
| 99 return true; |
| 100 } |
| 101 |
| 102 } // namespace crashpad |
OLD | NEW |