electron_main_mac.cc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright (c) 2022 Slack Technologies, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include <cstdlib>
  5. #include <memory>
  6. #include "electron/fuses.h"
  7. #include "shell/app/electron_library_main.h"
  8. #include "shell/app/uv_stdio_fix.h"
  9. #if defined(HELPER_EXECUTABLE) && !IS_MAS_BUILD()
  10. #include <mach-o/dyld.h>
  11. #include <cstdio>
  12. #include "sandbox/mac/seatbelt_exec.h" // nogncheck
  13. #endif
  14. extern "C" {
  15. // abort_report_np() records the message in a special section that both the
  16. // system CrashReporter and Crashpad collect in crash reports. Using a Crashpad
  17. // Annotation would be preferable, but this executable cannot depend on
  18. // Crashpad directly.
  19. void abort_report_np(const char* fmt, ...);
  20. }
  21. namespace {
  22. bool IsEnvSet(const char* name) {
  23. char* indicator = getenv(name);
  24. return indicator && indicator[0] != '\0';
  25. }
  26. #if defined(HELPER_EXECUTABLE) && !IS_MAS_BUILD()
  27. [[noreturn]] void FatalError(const char* format, ...) {
  28. va_list valist;
  29. va_start(valist, format);
  30. char message[4096];
  31. if (vsnprintf(message, sizeof(message), format, valist) >= 0) {
  32. fputs(message, stderr);
  33. abort_report_np("%s", message);
  34. }
  35. va_end(valist);
  36. abort();
  37. }
  38. #endif
  39. } // namespace
  40. int main(int argc, char* argv[]) {
  41. FixStdioStreams();
  42. if (electron::fuses::IsRunAsNodeEnabled() &&
  43. IsEnvSet("ELECTRON_RUN_AS_NODE")) {
  44. return ElectronInitializeICUandStartNode(argc, argv);
  45. }
  46. #if defined(HELPER_EXECUTABLE) && !IS_MAS_BUILD()
  47. uint32_t exec_path_size = 0;
  48. int rv = _NSGetExecutablePath(nullptr, &exec_path_size);
  49. if (rv != -1) {
  50. FatalError("_NSGetExecutablePath: get length failed.");
  51. }
  52. auto exec_path = std::make_unique<char[]>(exec_path_size);
  53. rv = _NSGetExecutablePath(exec_path.get(), &exec_path_size);
  54. if (rv != 0) {
  55. FatalError("_NSGetExecutablePath: get path failed.");
  56. }
  57. sandbox::SeatbeltExecServer::CreateFromArgumentsResult seatbelt =
  58. sandbox::SeatbeltExecServer::CreateFromArguments(exec_path.get(), argc,
  59. argv);
  60. if (seatbelt.sandbox_required) {
  61. if (!seatbelt.server) {
  62. FatalError("Failed to create seatbelt sandbox server.");
  63. }
  64. if (!seatbelt.server->InitializeSandbox()) {
  65. FatalError("Failed to initialize sandbox.");
  66. }
  67. }
  68. #endif // defined(HELPER_EXECUTABLE) && !IS_MAS_BUILD
  69. return ElectronMain(argc, argv);
  70. }