atom_main.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Copyright (c) 2013 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/app/atom_main.h"
  5. #include <algorithm>
  6. #include <cstdlib>
  7. #include <memory>
  8. #include <vector>
  9. #if defined(OS_WIN)
  10. #include <windows.h> // windows.h must be included first
  11. #include <atlbase.h> // ensures that ATL statics like `_AtlWinModule` are initialized (it's an issue in static debug build)
  12. #include <shellapi.h>
  13. #include <shellscalingapi.h>
  14. #include <tchar.h>
  15. #include "base/environment.h"
  16. #include "base/process/launch.h"
  17. #include "base/strings/utf_string_conversions.h"
  18. #include "base/win/windows_version.h"
  19. #include "content/public/app/sandbox_helper_win.h"
  20. #include "sandbox/win/src/sandbox_types.h"
  21. #include "shell/app/atom_main_delegate.h"
  22. #include "shell/app/command_line_args.h"
  23. #include "shell/common/crash_reporter/win/crash_service_main.h"
  24. #elif defined(OS_LINUX) // defined(OS_WIN)
  25. #include <unistd.h>
  26. #include <cstdio>
  27. #include "content/public/app/content_main.h"
  28. #include "shell/app/atom_main_delegate.h" // NOLINT
  29. #else // defined(OS_LINUX)
  30. #include <mach-o/dyld.h>
  31. #include <unistd.h>
  32. #include <cstdio>
  33. #include "shell/app/atom_library_main.h"
  34. #endif // defined(OS_MACOSX)
  35. #include "base/at_exit.h"
  36. #include "base/i18n/icu_util.h"
  37. #include "electron/buildflags/buildflags.h"
  38. #include "shell/app/node_main.h"
  39. #include "shell/common/atom_command_line.h"
  40. #include "shell/common/atom_constants.h"
  41. #if defined(HELPER_EXECUTABLE) && !defined(MAS_BUILD)
  42. #include "sandbox/mac/seatbelt_exec.h" // nogncheck
  43. #endif
  44. namespace {
  45. ALLOW_UNUSED_TYPE bool IsEnvSet(const char* name) {
  46. #if defined(OS_WIN)
  47. size_t required_size;
  48. getenv_s(&required_size, nullptr, 0, name);
  49. return required_size != 0;
  50. #else
  51. char* indicator = getenv(name);
  52. return indicator && indicator[0] != '\0';
  53. #endif
  54. }
  55. #if defined(OS_POSIX)
  56. void FixStdioStreams() {
  57. // libuv may mark stdin/stdout/stderr as close-on-exec, which interferes
  58. // with chromium's subprocess spawning. As a workaround, we detect if these
  59. // streams are closed on startup, and reopen them as /dev/null if necessary.
  60. // Otherwise, an unrelated file descriptor will be assigned as stdout/stderr
  61. // which may cause various errors when attempting to write to them.
  62. //
  63. // For details see https://github.com/libuv/libuv/issues/2062
  64. struct stat st;
  65. if (fstat(STDIN_FILENO, &st) < 0 && errno == EBADF)
  66. ignore_result(freopen("/dev/null", "r", stdin));
  67. if (fstat(STDOUT_FILENO, &st) < 0 && errno == EBADF)
  68. ignore_result(freopen("/dev/null", "w", stdout));
  69. if (fstat(STDERR_FILENO, &st) < 0 && errno == EBADF)
  70. ignore_result(freopen("/dev/null", "w", stderr));
  71. }
  72. #endif
  73. } // namespace
  74. #if defined(OS_WIN)
  75. namespace crash_reporter {
  76. extern const char kCrashpadProcess[];
  77. }
  78. int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE, wchar_t* cmd, int) {
  79. struct Arguments {
  80. int argc = 0;
  81. wchar_t** argv = ::CommandLineToArgvW(::GetCommandLineW(), &argc);
  82. ~Arguments() { LocalFree(argv); }
  83. } arguments;
  84. if (!arguments.argv)
  85. return -1;
  86. #ifdef _DEBUG
  87. // Don't display assert dialog boxes in CI test runs
  88. static const char* kCI = "CI";
  89. if (IsEnvSet(kCI)) {
  90. _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG | _CRTDBG_MODE_FILE);
  91. _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
  92. _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG | _CRTDBG_MODE_FILE);
  93. _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
  94. _set_error_mode(_OUT_TO_STDERR);
  95. }
  96. #endif
  97. #if BUILDFLAG(ENABLE_RUN_AS_NODE)
  98. bool run_as_node = IsEnvSet(electron::kRunAsNode);
  99. #else
  100. bool run_as_node = false;
  101. #endif
  102. // Make sure the output is printed to console.
  103. if (run_as_node || !IsEnvSet("ELECTRON_NO_ATTACH_CONSOLE"))
  104. base::RouteStdioToConsole(false);
  105. std::vector<char*> argv(arguments.argc);
  106. std::transform(arguments.argv, arguments.argv + arguments.argc, argv.begin(),
  107. [](auto& a) { return _strdup(base::WideToUTF8(a).c_str()); });
  108. #if BUILDFLAG(ENABLE_RUN_AS_NODE)
  109. if (run_as_node) {
  110. base::AtExitManager atexit_manager;
  111. base::i18n::InitializeICU();
  112. auto ret = electron::NodeMain(argv.size(), argv.data());
  113. std::for_each(argv.begin(), argv.end(), free);
  114. return ret;
  115. }
  116. #endif
  117. base::CommandLine::Init(argv.size(), argv.data());
  118. const base::CommandLine& cmd_line = *base::CommandLine::ForCurrentProcess();
  119. if (cmd_line.GetSwitchValueASCII("type") ==
  120. crash_reporter::kCrashpadProcess) {
  121. return crash_service::Main(&argv);
  122. }
  123. if (!electron::CheckCommandLineArguments(arguments.argc, arguments.argv))
  124. return -1;
  125. sandbox::SandboxInterfaceInfo sandbox_info = {0};
  126. content::InitializeSandboxInfo(&sandbox_info);
  127. electron::AtomMainDelegate delegate;
  128. content::ContentMainParams params(&delegate);
  129. params.instance = instance;
  130. params.sandbox_info = &sandbox_info;
  131. electron::AtomCommandLine::Init(arguments.argc, arguments.argv);
  132. return content::ContentMain(params);
  133. }
  134. #elif defined(OS_LINUX) // defined(OS_WIN)
  135. int main(int argc, char* argv[]) {
  136. FixStdioStreams();
  137. #if BUILDFLAG(ENABLE_RUN_AS_NODE)
  138. if (IsEnvSet(electron::kRunAsNode)) {
  139. base::i18n::InitializeICU();
  140. base::AtExitManager atexit_manager;
  141. return electron::NodeMain(argc, argv);
  142. }
  143. #endif
  144. electron::AtomMainDelegate delegate;
  145. content::ContentMainParams params(&delegate);
  146. params.argc = argc;
  147. params.argv = const_cast<const char**>(argv);
  148. electron::AtomCommandLine::Init(argc, argv);
  149. return content::ContentMain(params);
  150. }
  151. #else // defined(OS_LINUX)
  152. int main(int argc, char* argv[]) {
  153. FixStdioStreams();
  154. #if BUILDFLAG(ENABLE_RUN_AS_NODE)
  155. if (IsEnvSet(electron::kRunAsNode)) {
  156. return AtomInitializeICUandStartNode(argc, argv);
  157. }
  158. #endif
  159. #if defined(HELPER_EXECUTABLE) && !defined(MAS_BUILD)
  160. uint32_t exec_path_size = 0;
  161. int rv = _NSGetExecutablePath(NULL, &exec_path_size);
  162. if (rv != -1) {
  163. fprintf(stderr, "_NSGetExecutablePath: get length failed\n");
  164. abort();
  165. }
  166. std::unique_ptr<char[]> exec_path(new char[exec_path_size]);
  167. rv = _NSGetExecutablePath(exec_path.get(), &exec_path_size);
  168. if (rv != 0) {
  169. fprintf(stderr, "_NSGetExecutablePath: get path failed\n");
  170. abort();
  171. }
  172. sandbox::SeatbeltExecServer::CreateFromArgumentsResult seatbelt =
  173. sandbox::SeatbeltExecServer::CreateFromArguments(exec_path.get(), argc,
  174. argv);
  175. if (seatbelt.sandbox_required) {
  176. if (!seatbelt.server) {
  177. fprintf(stderr, "Failed to create seatbelt sandbox server.\n");
  178. abort();
  179. }
  180. if (!seatbelt.server->InitializeSandbox()) {
  181. fprintf(stderr, "Failed to initialize sandbox.\n");
  182. abort();
  183. }
  184. }
  185. #endif // defined(HELPER_EXECUTABLE) && !defined(MAS_BUILD)
  186. return AtomMain(argc, argv);
  187. }
  188. #endif // defined(OS_MACOSX)