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 " --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 // Long options without short equivalents. |
| 47 kOptionLastChar = 255, |
| 48 kOptionHandshakeFD, |
| 49 |
| 50 // Standard options. |
| 51 kOptionHelp = -2, |
| 52 kOptionVersion = -3, |
| 53 }; |
| 54 |
| 55 struct { |
| 56 int handshake_fd; |
| 57 } options = {}; |
| 58 options.handshake_fd = -1; |
| 59 |
| 60 const struct option long_options[] = { |
| 61 {"handshake-fd", required_argument, nullptr, kOptionHandshakeFD}, |
| 62 {"help", no_argument, nullptr, kOptionHelp}, |
| 63 {"version", no_argument, nullptr, kOptionVersion}, |
| 64 {nullptr, 0, nullptr, 0}, |
| 65 }; |
| 66 |
| 67 int opt; |
| 68 while ((opt = getopt_long(argc, argv, "", long_options, nullptr)) != -1) { |
| 69 switch (opt) { |
| 70 case kOptionHandshakeFD: |
| 71 if (!StringToNumber(optarg, &options.handshake_fd) || |
| 72 options.handshake_fd < 0) { |
| 73 ToolSupport::UsageHint(me, |
| 74 "--handshake-fd requires a file descriptor"); |
| 75 return EXIT_FAILURE; |
| 76 } |
| 77 break; |
| 78 case kOptionHelp: |
| 79 Usage(me); |
| 80 return EXIT_SUCCESS; |
| 81 case kOptionVersion: |
| 82 ToolSupport::Version(me); |
| 83 return EXIT_SUCCESS; |
| 84 default: |
| 85 ToolSupport::UsageHint(me, nullptr); |
| 86 return EXIT_FAILURE; |
| 87 } |
| 88 } |
| 89 argc -= optind; |
| 90 argv += optind; |
| 91 |
| 92 if (options.handshake_fd < 0) { |
| 93 ToolSupport::UsageHint(me, "--handshake-fd is required"); |
| 94 return EXIT_FAILURE; |
| 95 } |
| 96 |
| 97 if (argc) { |
| 98 ToolSupport::UsageHint(me, nullptr); |
| 99 return EXIT_FAILURE; |
| 100 } |
| 101 |
| 102 CloseStdinAndStdout(); |
| 103 |
| 104 ExceptionHandlerServer exception_handler_server; |
| 105 |
| 106 ChildPortHandshake::RunClient(options.handshake_fd, |
| 107 exception_handler_server.receive_port(), |
| 108 MACH_MSG_TYPE_MAKE_SEND); |
| 109 |
| 110 exception_handler_server.Run(); |
| 111 |
| 112 return EXIT_SUCCESS; |
| 113 } |
| 114 |
| 115 } // namespace |
| 116 } // namespace crashpad |
| 117 |
| 118 int main(int argc, char* argv[]) { |
| 119 return crashpad::HandlerMain(argc, argv); |
| 120 } |
OLD | NEW |