electron_main_mac.cc 2.0 KB

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