electron_main_mac.cc 2.4 KB

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