OLD | NEW |
1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with 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 | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 #include "client/crashpad_client.h" | 15 #include "client/crashpad_client.h" |
16 | 16 |
17 #include <mach/mach.h> | 17 #include <mach/mach.h> |
18 #include <sys/wait.h> | 18 #include <sys/wait.h> |
19 #include <unistd.h> | 19 #include <unistd.h> |
20 | 20 |
21 #include "base/logging.h" | 21 #include "base/logging.h" |
22 #include "base/posix/eintr_wrapper.h" | 22 #include "base/posix/eintr_wrapper.h" |
23 #include "base/strings/stringprintf.h" | 23 #include "base/strings/stringprintf.h" |
24 #include "client/crashpad_client.h" | 24 #include "client/crashpad_client.h" |
25 #include "util/mach/child_port_handshake.h" | 25 #include "util/mach/child_port_handshake.h" |
26 #include "util/mach/exception_ports.h" | 26 #include "util/mach/exception_ports.h" |
27 #include "util/mach/mach_extensions.h" | 27 #include "util/mach/mach_extensions.h" |
28 #include "util/posix/close_multiple.h" | 28 #include "util/posix/close_multiple.h" |
29 | 29 |
| 30 namespace { |
| 31 |
| 32 std::string FormatArgumentString(const std::string& name, |
| 33 const std::string& value) { |
| 34 return base::StringPrintf("--%s=%s", name.c_str(), value.c_str()); |
| 35 } |
| 36 |
| 37 std::string FormatArgumentInt(const std::string& name, int value) { |
| 38 return base::StringPrintf("--%s=%d", name.c_str(), value); |
| 39 } |
| 40 |
| 41 } // namespace |
| 42 |
30 namespace crashpad { | 43 namespace crashpad { |
31 | 44 |
32 CrashpadClient::CrashpadClient() | 45 CrashpadClient::CrashpadClient() |
33 : exception_port_() { | 46 : exception_port_() { |
34 } | 47 } |
35 | 48 |
36 CrashpadClient::~CrashpadClient() { | 49 CrashpadClient::~CrashpadClient() { |
37 } | 50 } |
38 | 51 |
39 bool CrashpadClient::StartHandler( | 52 bool CrashpadClient::StartHandler( |
40 const base::FilePath& handler, | 53 const base::FilePath& handler, |
41 const std::vector<std::string>& handler_arguments) { | 54 const base::FilePath& database, |
| 55 const std::string& url, |
| 56 const std::map<std::string, std::string>& annotations, |
| 57 const std::vector<std::string>& arguments) { |
42 DCHECK_EQ(exception_port_, kMachPortNull); | 58 DCHECK_EQ(exception_port_, kMachPortNull); |
43 | 59 |
44 // Set up the arguments for execve() first. These aren’t needed until execve() | 60 // Set up the arguments for execve() first. These aren’t needed until execve() |
45 // is called, but it’s dangerous to do this in a child process after fork(). | 61 // is called, but it’s dangerous to do this in a child process after fork(). |
46 ChildPortHandshake child_port_handshake; | 62 ChildPortHandshake child_port_handshake; |
47 int handshake_fd = child_port_handshake.ReadPipeFD(); | 63 int handshake_fd = child_port_handshake.ReadPipeFD(); |
48 std::string handshake_fd_arg = | |
49 base::StringPrintf("--handshake-fd=%d", handshake_fd); | |
50 | 64 |
51 const std::string& handler_s = handler.value(); | 65 // Use handler as argv[0], followed by arguments directed by this method’s |
52 const char* const handler_c = handler_s.c_str(); | 66 // parameters and a --handshake-fd argument. |arguments| are added first so |
| 67 // that if it erroneously contains an argument such as --url, the actual |url| |
| 68 // argument passed to this method will supersede it. In normal command-line |
| 69 // processing, the last parameter wins in the case of a conflict. |
| 70 std::vector<std::string> argv(1, handler.value()); |
| 71 argv.reserve(1 + arguments.size() + 2 + annotations.size() + 1); |
| 72 for (const std::string& argument : arguments) { |
| 73 argv.push_back(argument); |
| 74 } |
| 75 if (!database.value().empty()) { |
| 76 argv.push_back(FormatArgumentString("database", database.value())); |
| 77 } |
| 78 if (!url.empty()) { |
| 79 argv.push_back(FormatArgumentString("url", url)); |
| 80 } |
| 81 for (const auto& kv : annotations) { |
| 82 argv.push_back( |
| 83 FormatArgumentString("annotation", kv.first + '=' + kv.second)); |
| 84 } |
| 85 argv.push_back(FormatArgumentInt("handshake-fd", handshake_fd)); |
53 | 86 |
54 // Use handler as argv[0], followed by handler_arguments, handshake_fd_arg, | 87 // argv_c contains const char* pointers and is terminated by nullptr. argv |
55 // and a nullptr terminator. | 88 // is required because the pointers in argv_c need to point somewhere, and |
56 std::vector<const char*> argv(1, handler_c); | 89 // they can’t point to temporaries such as those returned by |
57 argv.reserve(1 + handler_arguments.size() + 1 + 1); | 90 // FormatArgumentString(). |
58 for (const std::string& handler_argument : handler_arguments) { | 91 std::vector<const char*> argv_c; |
59 argv.push_back(handler_argument.c_str()); | 92 argv_c.reserve(argv.size() + 1); |
| 93 for (const std::string& argument : argv) { |
| 94 argv_c.push_back(argument.c_str()); |
60 } | 95 } |
61 argv.push_back(handshake_fd_arg.c_str()); | 96 argv_c.push_back(nullptr); |
62 argv.push_back(nullptr); | |
63 | 97 |
64 // Double-fork(). The three processes involved are parent, child, and | 98 // Double-fork(). The three processes involved are parent, child, and |
65 // grandchild. The grandchild will become the handler process. The child exits | 99 // grandchild. The grandchild will become the handler process. The child exits |
66 // immediately after spawning the grandchild, so the grandchild becomes an | 100 // immediately after spawning the grandchild, so the grandchild becomes an |
67 // orphan and its parent process ID becomes 1. This relieves the parent and | 101 // orphan and its parent process ID becomes 1. This relieves the parent and |
68 // child of the responsibility for reaping the grandchild with waitpid() or | 102 // child of the responsibility for reaping the grandchild with waitpid() or |
69 // similar. The handler process is expected to outlive the parent process, so | 103 // similar. The handler process is expected to outlive the parent process, so |
70 // the parent shouldn’t be concerned with reaping it. This approach means that | 104 // the parent shouldn’t be concerned with reaping it. This approach means that |
71 // accidental early termination of the handler process will not result in a | 105 // accidental early termination of the handler process will not result in a |
72 // zombie process. | 106 // zombie process. |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 // Child process. | 139 // Child process. |
106 | 140 |
107 // _exit() instead of exit(), because fork() was called. | 141 // _exit() instead of exit(), because fork() was called. |
108 _exit(EXIT_SUCCESS); | 142 _exit(EXIT_SUCCESS); |
109 } | 143 } |
110 | 144 |
111 // Grandchild process. | 145 // Grandchild process. |
112 | 146 |
113 CloseMultipleNowOrOnExec(STDERR_FILENO + 1, handshake_fd); | 147 CloseMultipleNowOrOnExec(STDERR_FILENO + 1, handshake_fd); |
114 | 148 |
115 // &argv[0] is a pointer to a pointer to const char data, but because of how | 149 // &argv_c[0] is a pointer to a pointer to const char data, but because of |
116 // C (not C++) works, execvp() wants a pointer to a const pointer to char | 150 // how C (not C++) works, execvp() wants a pointer to a const pointer to |
117 // data. It modifies neither the data nor the pointers, so the const_cast is | 151 // char data. It modifies neither the data nor the pointers, so the |
118 // safe. | 152 // const_cast is safe. |
119 execvp(handler_c, const_cast<char* const*>(&argv[0])); | 153 execvp(handler.value().c_str(), const_cast<char* const*>(&argv_c[0])); |
120 PLOG(FATAL) << "execvp " << handler_s; | 154 PLOG(FATAL) << "execvp " << handler.value(); |
121 } | 155 } |
122 | 156 |
123 // Parent process. | 157 // Parent process. |
124 | 158 |
125 // waitpid() for the child, so that it does not become a zombie process. The | 159 // waitpid() for the child, so that it does not become a zombie process. The |
126 // child normally exits quickly. | 160 // child normally exits quickly. |
127 int status; | 161 int status; |
128 pid_t wait_pid = HANDLE_EINTR(waitpid(pid, &status, 0)); | 162 pid_t wait_pid = HANDLE_EINTR(waitpid(pid, &status, 0)); |
129 PCHECK(wait_pid != -1) << "waitpid"; | 163 PCHECK(wait_pid != -1) << "waitpid"; |
130 DCHECK_EQ(wait_pid, pid); | 164 DCHECK_EQ(wait_pid, pid); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 exception_port_, | 217 exception_port_, |
184 EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES, | 218 EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES, |
185 MACHINE_THREAD_STATE)) { | 219 MACHINE_THREAD_STATE)) { |
186 return false; | 220 return false; |
187 } | 221 } |
188 | 222 |
189 return true; | 223 return true; |
190 } | 224 } |
191 | 225 |
192 } // namespace crashpad | 226 } // namespace crashpad |
OLD | NEW |