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 <getopt.h> |
| 16 #include <libgen.h> |
| 17 #include <stdlib.h> |
| 18 |
| 19 #include <string> |
| 20 |
| 21 #include "tools/tool_support.h" |
| 22 #include "handler/mac/exception_handler_server.h" |
| 23 #include "util/mach/child_port_handshake.h" |
| 24 #include "util/posix/close_stdio.h" |
| 25 #include "util/stdlib/string_number_conversion.h" |
| 26 |
| 27 namespace crashpad { |
| 28 namespace { |
| 29 |
| 30 void Usage(const std::string& me) { |
| 31 fprintf(stderr, |
| 32 "Usage: %s [OPTION]...\n" |
| 33 "Crashpad's exception handler server.\n" |
| 34 "\n" |
| 35 " -h, --handshake-fd=FD establish communication with the client over FD\n" |
| 36 " --help display this help and exit\n" |
| 37 " --version output version information and exit\n", |
| 38 me.c_str()); |
| 39 ToolSupport::UsageTail(me); |
| 40 } |
| 41 |
| 42 int HandlerMain(int argc, char* argv[]) { |
| 43 const std::string me(basename(argv[0])); |
| 44 |
| 45 enum OptionFlags { |
| 46 // “Short” (single-character) options. |
| 47 kOptionHandshakeFD = 'h', |
| 48 |
| 49 // Long options without short equivalents. |
| 50 kOptionLastChar = 255, |
| 51 |
| 52 // Standard options. |
| 53 kOptionHelp = -2, |
| 54 kOptionVersion = -3, |
| 55 }; |
| 56 |
| 57 struct { |
| 58 int handshake_fd; |
| 59 } options = {}; |
| 60 options.handshake_fd = -1; |
| 61 |
| 62 const struct option long_options[] = { |
| 63 {"handshake-fd", required_argument, nullptr, kOptionHandshakeFD}, |
| 64 {"help", no_argument, nullptr, kOptionHelp}, |
| 65 {"version", no_argument, nullptr, kOptionVersion}, |
| 66 {nullptr, 0, nullptr, 0}, |
| 67 }; |
| 68 |
| 69 int opt; |
| 70 while ((opt = getopt_long(argc, argv, "h:", long_options, nullptr)) != -1) { |
| 71 switch (opt) { |
| 72 case kOptionHandshakeFD: |
| 73 if (!StringToNumber(optarg, &options.handshake_fd) || |
| 74 options.handshake_fd < 0) { |
| 75 ToolSupport::UsageHint(me, "-h requires a file descriptor"); |
| 76 return EXIT_FAILURE; |
| 77 } |
| 78 break; |
| 79 case kOptionHelp: |
| 80 Usage(me); |
| 81 return EXIT_SUCCESS; |
| 82 case kOptionVersion: |
| 83 ToolSupport::Version(me); |
| 84 return EXIT_SUCCESS; |
| 85 default: |
| 86 ToolSupport::UsageHint(me, nullptr); |
| 87 return EXIT_FAILURE; |
| 88 } |
| 89 } |
| 90 argc -= optind; |
| 91 argv += optind; |
| 92 |
| 93 if (options.handshake_fd < 0) { |
| 94 ToolSupport::UsageHint(me, "-h is required"); |
| 95 return EXIT_FAILURE; |
| 96 } |
| 97 |
| 98 if (argc) { |
| 99 ToolSupport::UsageHint(me, nullptr); |
| 100 return EXIT_FAILURE; |
| 101 } |
| 102 |
| 103 CloseStdinAndStdout(); |
| 104 |
| 105 ExceptionHandlerServer exception_handler_server; |
| 106 |
| 107 ChildPortHandshake::RunClient(options.handshake_fd, |
| 108 exception_handler_server.receive_port(), |
| 109 MACH_MSG_TYPE_MAKE_SEND); |
| 110 |
| 111 exception_handler_server.Run(); |
| 112 |
| 113 return EXIT_SUCCESS; |
| 114 } |
| 115 |
| 116 } // namespace |
| 117 } // namespace crashpad |
| 118 |
| 119 int main(int argc, char* argv[]) { |
| 120 return crashpad::HandlerMain(argc, argv); |
| 121 } |
OLD | NEW |