electron_main_mac.cc 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. namespace {
  17. [[maybe_unused]] bool IsEnvSet(const char* name) {
  18. char* indicator = getenv(name);
  19. return indicator && indicator[0] != '\0';
  20. }
  21. } // namespace
  22. int main(int argc, char* argv[]) {
  23. partition_alloc::EarlyMallocZoneRegistration();
  24. FixStdioStreams();
  25. #if BUILDFLAG(ENABLE_RUN_AS_NODE)
  26. if (electron::fuses::IsRunAsNodeEnabled() &&
  27. IsEnvSet("ELECTRON_RUN_AS_NODE")) {
  28. return ElectronInitializeICUandStartNode(argc, argv);
  29. }
  30. #endif
  31. #if defined(HELPER_EXECUTABLE) && !defined(MAS_BUILD)
  32. uint32_t exec_path_size = 0;
  33. int rv = _NSGetExecutablePath(NULL, &exec_path_size);
  34. if (rv != -1) {
  35. fprintf(stderr, "_NSGetExecutablePath: get length failed\n");
  36. abort();
  37. }
  38. auto exec_path = std::make_unique<char[]>(exec_path_size);
  39. rv = _NSGetExecutablePath(exec_path.get(), &exec_path_size);
  40. if (rv != 0) {
  41. fprintf(stderr, "_NSGetExecutablePath: get path failed\n");
  42. abort();
  43. }
  44. sandbox::SeatbeltExecServer::CreateFromArgumentsResult seatbelt =
  45. sandbox::SeatbeltExecServer::CreateFromArguments(exec_path.get(), argc,
  46. argv);
  47. if (seatbelt.sandbox_required) {
  48. if (!seatbelt.server) {
  49. fprintf(stderr, "Failed to create seatbelt sandbox server.\n");
  50. abort();
  51. }
  52. if (!seatbelt.server->InitializeSandbox()) {
  53. fprintf(stderr, "Failed to initialize sandbox.\n");
  54. abort();
  55. }
  56. }
  57. #endif // defined(HELPER_EXECUTABLE) && !defined(MAS_BUILD)
  58. return ElectronMain(argc, argv);
  59. }