electron_main_mac.cc 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 "base/allocator/early_zone_registration_mac.h"
  7. #include "electron/buildflags/buildflags.h"
  8. #include "electron/fuses.h"
  9. #include "shell/app/electron_library_main.h"
  10. #include "shell/app/uv_stdio_fix.h"
  11. #if defined(HELPER_EXECUTABLE) && !defined(MAS_BUILD)
  12. #include <mach-o/dyld.h>
  13. #include <cstdio>
  14. #include "sandbox/mac/seatbelt_exec.h" // nogncheck
  15. #endif
  16. extern "C" {
  17. // abort_report_np() records the message in a special section that both the
  18. // system CrashReporter and Crashpad collect in crash reports. Using a Crashpad
  19. // Annotation would be preferable, but this executable cannot depend on
  20. // Crashpad directly.
  21. void abort_report_np(const char* fmt, ...);
  22. }
  23. namespace {
  24. [[maybe_unused]] bool IsEnvSet(const char* name) {
  25. char* indicator = getenv(name);
  26. return indicator && indicator[0] != '\0';
  27. }
  28. #if defined(HELPER_EXECUTABLE) && !defined(MAS_BUILD)
  29. [[noreturn]] void FatalError(const char* format, ...) {
  30. va_list valist;
  31. va_start(valist, format);
  32. char message[4096];
  33. if (vsnprintf(message, sizeof(message), format, valist) >= 0) {
  34. fputs(message, stderr);
  35. abort_report_np("%s", message);
  36. }
  37. va_end(valist);
  38. abort();
  39. }
  40. #endif
  41. } // namespace
  42. int main(int argc, char* argv[]) {
  43. partition_alloc::EarlyMallocZoneRegistration();
  44. FixStdioStreams();
  45. #if BUILDFLAG(ENABLE_RUN_AS_NODE)
  46. if (electron::fuses::IsRunAsNodeEnabled() &&
  47. IsEnvSet("ELECTRON_RUN_AS_NODE")) {
  48. return ElectronInitializeICUandStartNode(argc, argv);
  49. }
  50. #endif
  51. #if defined(HELPER_EXECUTABLE) && !defined(MAS_BUILD)
  52. uint32_t exec_path_size = 0;
  53. int rv = _NSGetExecutablePath(NULL, &exec_path_size);
  54. if (rv != -1) {
  55. FatalError("_NSGetExecutablePath: get length failed.");
  56. }
  57. auto exec_path = std::make_unique<char[]>(exec_path_size);
  58. rv = _NSGetExecutablePath(exec_path.get(), &exec_path_size);
  59. if (rv != 0) {
  60. FatalError("_NSGetExecutablePath: get path failed.");
  61. }
  62. sandbox::SeatbeltExecServer::CreateFromArgumentsResult seatbelt =
  63. sandbox::SeatbeltExecServer::CreateFromArguments(exec_path.get(), argc,
  64. argv);
  65. if (seatbelt.sandbox_required) {
  66. if (!seatbelt.server) {
  67. FatalError("Failed to create seatbelt sandbox server.");
  68. }
  69. if (!seatbelt.server->InitializeSandbox()) {
  70. FatalError("Failed to initialize sandbox.");
  71. }
  72. }
  73. #endif // defined(HELPER_EXECUTABLE) && !defined(MAS_BUILD)
  74. return ElectronMain(argc, argv);
  75. }