electron_main_mac.cc 2.5 KB

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