Chromium Code Reviews| Index: handler/mac/main.cc |
| diff --git a/handler/mac/main.cc b/handler/mac/main.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1ddea9a7e2cf06499e9d29d4cf6089335b6cded3 |
| --- /dev/null |
| +++ b/handler/mac/main.cc |
| @@ -0,0 +1,121 @@ |
| +// Copyright 2014 The Crashpad Authors. All rights reserved. |
| +// |
| +// Licensed under the Apache License, Version 2.0 (the "License"); |
| +// you may not use this file except in compliance with the License. |
| +// You may obtain a copy of the License at |
| +// |
| +// http://www.apache.org/licenses/LICENSE-2.0 |
| +// |
| +// Unless required by applicable law or agreed to in writing, software |
| +// distributed under the License is distributed on an "AS IS" BASIS, |
| +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| +// See the License for the specific language governing permissions and |
| +// limitations under the License. |
| + |
| +#include <getopt.h> |
| +#include <libgen.h> |
| +#include <stdlib.h> |
| + |
| +#include <string> |
| + |
| +#include "tools/tool_support.h" |
| +#include "handler/mac/exception_handler_server.h" |
| +#include "util/mach/child_port_handshake.h" |
| +#include "util/posix/close_stdio.h" |
| +#include "util/stdlib/string_number_conversion.h" |
| + |
| +namespace crashpad { |
| +namespace { |
| + |
| +void Usage(const std::string& me) { |
| + fprintf(stderr, |
| +"Usage: %s [OPTION]...\n" |
| +"Crashpad's exception handler server.\n" |
| +"\n" |
| +" -h, --handshake-fd=FD establish communication with the client over FD\n" |
|
Robert Sesek
2014/12/29 16:17:08
Since this is never meant to be run by users, I'm
|
| +" --help display this help and exit\n" |
| +" --version output version information and exit\n", |
| + me.c_str()); |
| + ToolSupport::UsageTail(me); |
| +} |
| + |
| +int HandlerMain(int argc, char* argv[]) { |
| + const std::string me(basename(argv[0])); |
| + |
| + enum OptionFlags { |
| + // “Short” (single-character) options. |
| + kOptionHandshakeFD = 'h', |
| + |
| + // Long options without short equivalents. |
| + kOptionLastChar = 255, |
| + |
| + // Standard options. |
| + kOptionHelp = -2, |
| + kOptionVersion = -3, |
| + }; |
| + |
| + struct { |
| + int handshake_fd; |
| + } options = {}; |
| + options.handshake_fd = -1; |
| + |
| + const struct option long_options[] = { |
| + {"handshake-fd", required_argument, nullptr, kOptionHandshakeFD}, |
| + {"help", no_argument, nullptr, kOptionHelp}, |
| + {"version", no_argument, nullptr, kOptionVersion}, |
| + {nullptr, 0, nullptr, 0}, |
| + }; |
| + |
| + int opt; |
| + while ((opt = getopt_long(argc, argv, "h:", long_options, nullptr)) != -1) { |
| + switch (opt) { |
| + case kOptionHandshakeFD: |
| + if (!StringToNumber(optarg, &options.handshake_fd) || |
| + options.handshake_fd < 0) { |
| + ToolSupport::UsageHint(me, "-h requires a file descriptor"); |
| + return EXIT_FAILURE; |
| + } |
| + break; |
| + case kOptionHelp: |
| + Usage(me); |
| + return EXIT_SUCCESS; |
| + case kOptionVersion: |
| + ToolSupport::Version(me); |
| + return EXIT_SUCCESS; |
| + default: |
| + ToolSupport::UsageHint(me, nullptr); |
| + return EXIT_FAILURE; |
| + } |
| + } |
| + argc -= optind; |
| + argv += optind; |
| + |
| + if (options.handshake_fd < 0) { |
|
Robert Sesek
2014/12/29 16:17:08
This is done above on line 74, too. This one is ne
Mark Mentovai
2014/12/29 17:08:07
Robert Sesek wrote:
|
| + ToolSupport::UsageHint(me, "-h is required"); |
| + return EXIT_FAILURE; |
| + } |
| + |
| + if (argc) { |
| + ToolSupport::UsageHint(me, nullptr); |
| + return EXIT_FAILURE; |
| + } |
| + |
| + CloseStdinAndStdout(); |
| + |
| + ExceptionHandlerServer exception_handler_server; |
| + |
| + ChildPortHandshake::RunClient(options.handshake_fd, |
| + exception_handler_server.receive_port(), |
| + MACH_MSG_TYPE_MAKE_SEND); |
| + |
| + exception_handler_server.Run(); |
| + |
| + return EXIT_SUCCESS; |
| +} |
| + |
| +} // namespace |
| +} // namespace crashpad |
| + |
| +int main(int argc, char* argv[]) { |
| + return crashpad::HandlerMain(argc, argv); |
| +} |