electron_main_mac.cc 1.9 KB

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