electron_main_mac.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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) && !IS_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. bool IsEnvSet(const char* name) {
  25. char* indicator = getenv(name);
  26. return indicator && indicator[0] != '\0';
  27. }
  28. #if defined(HELPER_EXECUTABLE) && !IS_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 (electron::fuses::IsRunAsNodeEnabled() &&
  46. IsEnvSet("ELECTRON_RUN_AS_NODE")) {
  47. return ElectronInitializeICUandStartNode(argc, argv);
  48. }
  49. #if defined(HELPER_EXECUTABLE) && !IS_MAS_BUILD()
  50. uint32_t exec_path_size = 0;
  51. int rv = _NSGetExecutablePath(NULL, &exec_path_size);
  52. if (rv != -1) {
  53. FatalError("_NSGetExecutablePath: get length failed.");
  54. }
  55. auto exec_path = std::make_unique<char[]>(exec_path_size);
  56. rv = _NSGetExecutablePath(exec_path.get(), &exec_path_size);
  57. if (rv != 0) {
  58. FatalError("_NSGetExecutablePath: get path failed.");
  59. }
  60. sandbox::SeatbeltExecServer::CreateFromArgumentsResult seatbelt =
  61. sandbox::SeatbeltExecServer::CreateFromArguments(exec_path.get(), argc,
  62. argv);
  63. if (seatbelt.sandbox_required) {
  64. if (!seatbelt.server) {
  65. FatalError("Failed to create seatbelt sandbox server.");
  66. }
  67. if (!seatbelt.server->InitializeSandbox()) {
  68. FatalError("Failed to initialize sandbox.");
  69. }
  70. }
  71. #endif // defined(HELPER_EXECUTABLE) && !IS_MAS_BUILD
  72. return ElectronMain(argc, argv);
  73. }