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

Side by Side Diff: handler/main.cc

Issue 1432563003: win: crashpad_handler should create its own pipe name in ephemeral mode (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: It's futile to pick and choose errors, retry on all Created 5 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
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 <getopt.h> 15 #include <getopt.h>
16 #include <stdlib.h> 16 #include <stdlib.h>
17 17
18 #include <map> 18 #include <map>
19 #include <string> 19 #include <string>
20 20
21 #include "base/files/file_path.h" 21 #include "base/files/file_path.h"
22 #include "base/files/scoped_file.h" 22 #include "base/files/scoped_file.h"
23 #include "base/logging.h" 23 #include "base/logging.h"
24 #include "base/memory/scoped_ptr.h" 24 #include "base/memory/scoped_ptr.h"
25 #include "base/strings/utf_string_conversions.h"
25 #include "build/build_config.h" 26 #include "build/build_config.h"
26 #include "client/crash_report_database.h" 27 #include "client/crash_report_database.h"
27 #include "client/crashpad_client.h" 28 #include "client/crashpad_client.h"
28 #include "tools/tool_support.h" 29 #include "tools/tool_support.h"
29 #include "handler/crash_report_upload_thread.h" 30 #include "handler/crash_report_upload_thread.h"
31 #include "util/file/file_io.h"
30 #include "util/stdlib/map_insert.h" 32 #include "util/stdlib/map_insert.h"
31 #include "util/stdlib/string_number_conversion.h" 33 #include "util/stdlib/string_number_conversion.h"
32 #include "util/string/split_string.h" 34 #include "util/string/split_string.h"
33 #include "util/synchronization/semaphore.h" 35 #include "util/synchronization/semaphore.h"
34 36
35 #if defined(OS_MACOSX) 37 #if defined(OS_MACOSX)
36 #include <libgen.h> 38 #include <libgen.h>
37 #include "base/mac/scoped_mach_port.h" 39 #include "base/mac/scoped_mach_port.h"
38 #include "handler/mac/crash_report_exception_handler.h" 40 #include "handler/mac/crash_report_exception_handler.h"
39 #include "handler/mac/exception_handler_server.h" 41 #include "handler/mac/exception_handler_server.h"
(...skipping 13 matching lines...) Expand all
53 "Usage: %" PRFilePath " [OPTION]...\n" 55 "Usage: %" PRFilePath " [OPTION]...\n"
54 "Crashpad's exception handler server.\n" 56 "Crashpad's exception handler server.\n"
55 "\n" 57 "\n"
56 " --annotation=KEY=VALUE set a process annotation in each crash report\n" 58 " --annotation=KEY=VALUE set a process annotation in each crash report\n"
57 " --database=PATH store the crash report database at PATH\n" 59 " --database=PATH store the crash report database at PATH\n"
58 #if defined(OS_MACOSX) 60 #if defined(OS_MACOSX)
59 " --handshake-fd=FD establish communication with the client over FD\n " 61 " --handshake-fd=FD establish communication with the client over FD\n "
60 " --reset-own-crash-exception-port-to-system-default\n" 62 " --reset-own-crash-exception-port-to-system-default\n"
61 " reset the server's exception handler to default\n " 63 " reset the server's exception handler to default\n "
62 #elif defined(OS_WIN) 64 #elif defined(OS_WIN)
63 " --persistent continue running after all clients exit\n" 65 " --handshake-handle=H create a new pipe and send its name via H\n"
64 " --pipe-name=PIPE communicate with the client over PIPE\n" 66 " --pipe-name=PIPE communicate with the client over PIPE\n"
65 #endif // OS_MACOSX 67 #endif // OS_MACOSX
66 " --url=URL send crash reports to this Breakpad server URL,\n " 68 " --url=URL send crash reports to this Breakpad server URL,\n "
67 " only if uploads are enabled for the database\n" 69 " only if uploads are enabled for the database\n"
68 " --help display this help and exit\n" 70 " --help display this help and exit\n"
69 " --version output version information and exit\n", 71 " --version output version information and exit\n",
70 me.value().c_str()); 72 me.value().c_str());
71 ToolSupport::UsageTail(me); 73 ToolSupport::UsageTail(me);
72 } 74 }
73 75
74 int HandlerMain(int argc, char* argv[]) { 76 int HandlerMain(int argc, char* argv[]) {
75 const base::FilePath argv0( 77 const base::FilePath argv0(
76 ToolSupport::CommandLineArgumentToFilePathStringType(argv[0])); 78 ToolSupport::CommandLineArgumentToFilePathStringType(argv[0]));
77 const base::FilePath me(argv0.BaseName()); 79 const base::FilePath me(argv0.BaseName());
78 80
79 enum OptionFlags { 81 enum OptionFlags {
80 // Long options without short equivalents. 82 // Long options without short equivalents.
81 kOptionLastChar = 255, 83 kOptionLastChar = 255,
82 kOptionAnnotation, 84 kOptionAnnotation,
83 kOptionDatabase, 85 kOptionDatabase,
84 #if defined(OS_MACOSX) 86 #if defined(OS_MACOSX)
85 kOptionHandshakeFD, 87 kOptionHandshakeFD,
86 kOptionResetOwnCrashExceptionPortToSystemDefault, 88 kOptionResetOwnCrashExceptionPortToSystemDefault,
87 #elif defined(OS_WIN) 89 #elif defined(OS_WIN)
88 kOptionPersistent, 90 kOptionHandshakeHandle,
89 kOptionPipeName, 91 kOptionPipeName,
90 #endif // OS_MACOSX 92 #endif // OS_MACOSX
91 kOptionURL, 93 kOptionURL,
92 94
93 // Standard options. 95 // Standard options.
94 kOptionHelp = -2, 96 kOptionHelp = -2,
95 kOptionVersion = -3, 97 kOptionVersion = -3,
96 }; 98 };
97 99
98 struct { 100 struct {
99 std::map<std::string, std::string> annotations; 101 std::map<std::string, std::string> annotations;
100 std::string url; 102 std::string url;
101 const char* database; 103 const char* database;
102 #if defined(OS_MACOSX) 104 #if defined(OS_MACOSX)
103 int handshake_fd; 105 int handshake_fd;
104 bool reset_own_crash_exception_port_to_system_default; 106 bool reset_own_crash_exception_port_to_system_default;
105 #elif defined(OS_WIN) 107 #elif defined(OS_WIN)
106 bool persistent; 108 HANDLE handshake_handle;
107 std::string pipe_name; 109 std::string pipe_name;
108 #endif // OS_MACOSX 110 #endif // OS_MACOSX
109 } options = {}; 111 } options = {};
110 #if defined(OS_MACOSX) 112 #if defined(OS_MACOSX)
111 options.handshake_fd = -1; 113 options.handshake_fd = -1;
112 options.reset_own_crash_exception_port_to_system_default = false; 114 #elif defined(OS_WIN)
115 options.handshake_handle = INVALID_HANDLE_VALUE;
113 #endif 116 #endif
114 117
115 const option long_options[] = { 118 const option long_options[] = {
116 {"annotation", required_argument, nullptr, kOptionAnnotation}, 119 {"annotation", required_argument, nullptr, kOptionAnnotation},
117 {"database", required_argument, nullptr, kOptionDatabase}, 120 {"database", required_argument, nullptr, kOptionDatabase},
118 #if defined(OS_MACOSX) 121 #if defined(OS_MACOSX)
119 {"handshake-fd", required_argument, nullptr, kOptionHandshakeFD}, 122 {"handshake-fd", required_argument, nullptr, kOptionHandshakeFD},
120 {"reset-own-crash-exception-port-to-system-default", 123 {"reset-own-crash-exception-port-to-system-default",
121 no_argument, 124 no_argument,
122 nullptr, 125 nullptr,
123 kOptionResetOwnCrashExceptionPortToSystemDefault}, 126 kOptionResetOwnCrashExceptionPortToSystemDefault},
124 #elif defined(OS_WIN) 127 #elif defined(OS_WIN)
125 {"persistent", no_argument, nullptr, kOptionPersistent}, 128 {"handshake-handle", required_argument, nullptr, kOptionHandshakeHandle},
126 {"pipe-name", required_argument, nullptr, kOptionPipeName}, 129 {"pipe-name", required_argument, nullptr, kOptionPipeName},
127 #endif // OS_MACOSX 130 #endif // OS_MACOSX
128 {"url", required_argument, nullptr, kOptionURL}, 131 {"url", required_argument, nullptr, kOptionURL},
129 {"help", no_argument, nullptr, kOptionHelp}, 132 {"help", no_argument, nullptr, kOptionHelp},
130 {"version", no_argument, nullptr, kOptionVersion}, 133 {"version", no_argument, nullptr, kOptionVersion},
131 {nullptr, 0, nullptr, 0}, 134 {nullptr, 0, nullptr, 0},
132 }; 135 };
133 136
134 int opt; 137 int opt;
135 while ((opt = getopt_long(argc, argv, "", long_options, nullptr)) != -1) { 138 while ((opt = getopt_long(argc, argv, "", long_options, nullptr)) != -1) {
(...skipping 24 matching lines...) Expand all
160 "--handshake-fd requires a file descriptor"); 163 "--handshake-fd requires a file descriptor");
161 return EXIT_FAILURE; 164 return EXIT_FAILURE;
162 } 165 }
163 break; 166 break;
164 } 167 }
165 case kOptionResetOwnCrashExceptionPortToSystemDefault: { 168 case kOptionResetOwnCrashExceptionPortToSystemDefault: {
166 options.reset_own_crash_exception_port_to_system_default = true; 169 options.reset_own_crash_exception_port_to_system_default = true;
167 break; 170 break;
168 } 171 }
169 #elif defined(OS_WIN) 172 #elif defined(OS_WIN)
170 case kOptionPersistent: { 173 case kOptionHandshakeHandle: {
171 options.persistent = true; 174 // According to
175 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa384203,
176 // HANDLEs are always 32 bits. This construction is used to read a full
177 // 32-bit number presented by the client, which uses 0x%x format, and
178 // sign-extend it.
179 unsigned int handle_uint;
180 if (!StringToNumber(optarg, &handle_uint) ||
181 (options.handshake_handle = reinterpret_cast<HANDLE>(
182 static_cast<int>(handle_uint))) == INVALID_HANDLE_VALUE) {
183 ToolSupport::UsageHint(me, "--handshake-handle requires a HANDLE");
184 return EXIT_FAILURE;
185 }
172 break; 186 break;
173 } 187 }
174 case kOptionPipeName: { 188 case kOptionPipeName: {
175 options.pipe_name = optarg; 189 options.pipe_name = optarg;
176 break; 190 break;
177 } 191 }
178 #endif // OS_MACOSX 192 #endif // OS_MACOSX
179 case kOptionURL: { 193 case kOptionURL: {
180 options.url = optarg; 194 options.url = optarg;
181 break; 195 break;
(...skipping 14 matching lines...) Expand all
196 } 210 }
197 argc -= optind; 211 argc -= optind;
198 argv += optind; 212 argv += optind;
199 213
200 #if defined(OS_MACOSX) 214 #if defined(OS_MACOSX)
201 if (options.handshake_fd < 0) { 215 if (options.handshake_fd < 0) {
202 ToolSupport::UsageHint(me, "--handshake-fd is required"); 216 ToolSupport::UsageHint(me, "--handshake-fd is required");
203 return EXIT_FAILURE; 217 return EXIT_FAILURE;
204 } 218 }
205 #elif defined(OS_WIN) 219 #elif defined(OS_WIN)
206 if (options.pipe_name.empty()) { 220 if (options.handshake_handle == INVALID_HANDLE_VALUE &&
207 ToolSupport::UsageHint(me, "--pipe-name is required"); 221 options.pipe_name.empty()) {
222 ToolSupport::UsageHint(me, "--handshake-handle or --pipe-name is required");
208 return EXIT_FAILURE; 223 return EXIT_FAILURE;
209 } 224 }
210 #endif 225 if (options.handshake_handle != INVALID_HANDLE_VALUE &&
226 !options.pipe_name.empty()) {
227 ToolSupport::UsageHint(
228 me, "--handshake-handle and --pipe-name are incompatible");
229 return EXIT_FAILURE;
230 }
231 #endif // OS_MACOSX
211 232
212 if (!options.database) { 233 if (!options.database) {
213 ToolSupport::UsageHint(me, "--database is required"); 234 ToolSupport::UsageHint(me, "--database is required");
214 return EXIT_FAILURE; 235 return EXIT_FAILURE;
215 } 236 }
216 237
217 if (argc) { 238 if (argc) {
218 ToolSupport::UsageHint(me, nullptr); 239 ToolSupport::UsageHint(me, nullptr);
219 return EXIT_FAILURE; 240 return EXIT_FAILURE;
220 } 241 }
221 242
222 #if defined(OS_MACOSX) 243 #if defined(OS_MACOSX)
223 CloseStdinAndStdout(); 244 CloseStdinAndStdout();
224 245
225 if (options.reset_own_crash_exception_port_to_system_default) { 246 if (options.reset_own_crash_exception_port_to_system_default) {
226 CrashpadClient::UseSystemDefaultHandler(); 247 CrashpadClient::UseSystemDefaultHandler();
227 } 248 }
228 249
229 base::mac::ScopedMachReceiveRight receive_right( 250 base::mac::ScopedMachReceiveRight receive_right(
230 ChildPortHandshake::RunServerForFD( 251 ChildPortHandshake::RunServerForFD(
231 base::ScopedFD(options.handshake_fd), 252 base::ScopedFD(options.handshake_fd),
232 ChildPortHandshake::PortRightType::kReceiveRight)); 253 ChildPortHandshake::PortRightType::kReceiveRight));
233 if (!receive_right.is_valid()) { 254 if (!receive_right.is_valid()) {
234 return EXIT_FAILURE; 255 return EXIT_FAILURE;
235 } 256 }
236 257
237 ExceptionHandlerServer exception_handler_server(receive_right.Pass()); 258 ExceptionHandlerServer exception_handler_server(receive_right.Pass());
238 #elif defined(OS_WIN) 259 #elif defined(OS_WIN)
239 ExceptionHandlerServer exception_handler_server(options.pipe_name, 260 ExceptionHandlerServer exception_handler_server(!options.pipe_name.empty());
240 options.persistent); 261
262 std::string pipe_name;
263 if (!options.pipe_name.empty()) {
264 exception_handler_server.SetPipeName(base::UTF8ToUTF16(options.pipe_name));
265 } else if (options.handshake_handle != INVALID_HANDLE_VALUE) {
266 std::wstring pipe_name = exception_handler_server.CreatePipe();
267
268 uint32_t pipe_name_length = static_cast<uint32_t>(pipe_name.size());
269 if (!LoggingWriteFile(options.handshake_handle,
270 &pipe_name_length,
271 sizeof(pipe_name_length))) {
272 return EXIT_FAILURE;
273 }
274 if (!LoggingWriteFile(options.handshake_handle,
275 pipe_name.c_str(),
276 pipe_name.size() * sizeof(pipe_name[0]))) {
277 return EXIT_FAILURE;
278 }
279 }
241 #endif // OS_MACOSX 280 #endif // OS_MACOSX
242 281
243 scoped_ptr<CrashReportDatabase> database(CrashReportDatabase::Initialize( 282 scoped_ptr<CrashReportDatabase> database(CrashReportDatabase::Initialize(
244 base::FilePath(ToolSupport::CommandLineArgumentToFilePathStringType( 283 base::FilePath(ToolSupport::CommandLineArgumentToFilePathStringType(
245 options.database)))); 284 options.database))));
246 if (!database) { 285 if (!database) {
247 return EXIT_FAILURE; 286 return EXIT_FAILURE;
248 } 287 }
249 288
250 CrashReportUploadThread upload_thread(database.get(), options.url); 289 CrashReportUploadThread upload_thread(database.get(), options.url);
(...skipping 14 matching lines...) Expand all
265 304
266 #if defined(OS_MACOSX) 305 #if defined(OS_MACOSX)
267 int main(int argc, char* argv[]) { 306 int main(int argc, char* argv[]) {
268 return crashpad::HandlerMain(argc, argv); 307 return crashpad::HandlerMain(argc, argv);
269 } 308 }
270 #elif defined(OS_WIN) 309 #elif defined(OS_WIN)
271 int wmain(int argc, wchar_t* argv[]) { 310 int wmain(int argc, wchar_t* argv[]) {
272 return crashpad::ToolSupport::Wmain(argc, argv, crashpad::HandlerMain); 311 return crashpad::ToolSupport::Wmain(argc, argv, crashpad::HandlerMain);
273 } 312 }
274 #endif // OS_MACOSX 313 #endif // OS_MACOSX
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698