atom_main.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 "atom/app/atom_main.h"
  5. #include <cstdlib>
  6. #include <memory>
  7. #include <vector>
  8. #if defined(OS_WIN)
  9. #include <windows.h> // windows.h must be included first
  10. #include <atlbase.h> // ensures that ATL statics like `_AtlWinModule` are initialized (it's an issue in static debug build)
  11. #include <shellapi.h>
  12. #include <shellscalingapi.h>
  13. #include <tchar.h>
  14. #include "atom/app/atom_main_delegate.h"
  15. #include "atom/app/command_line_args.h"
  16. #include "atom/common/crash_reporter/win/crash_service_main.h"
  17. #include "base/environment.h"
  18. #include "base/process/launch.h"
  19. #include "base/strings/utf_string_conversions.h"
  20. #include "base/win/windows_version.h"
  21. #include "content/public/app/sandbox_helper_win.h"
  22. #include "sandbox/win/src/sandbox_types.h"
  23. #elif defined(OS_LINUX) // defined(OS_WIN)
  24. #include <unistd.h>
  25. #include <cstdio>
  26. #include "atom/app/atom_main_delegate.h" // NOLINT
  27. #include "content/public/app/content_main.h"
  28. #else // defined(OS_LINUX)
  29. #include <mach-o/dyld.h>
  30. #include <unistd.h>
  31. #include <cstdio>
  32. #include "atom/app/atom_library_main.h"
  33. #endif // defined(OS_MACOSX)
  34. #include "atom/app/node_main.h"
  35. #include "atom/common/atom_command_line.h"
  36. #include "base/at_exit.h"
  37. #include "base/i18n/icu_util.h"
  38. #include "electron/buildflags/buildflags.h"
  39. #if defined(HELPER_EXECUTABLE) && !defined(MAS_BUILD)
  40. #include "sandbox/mac/seatbelt_exec.h" // nogncheck
  41. #endif
  42. namespace {
  43. #if BUILDFLAG(ENABLE_RUN_AS_NODE)
  44. const char kRunAsNode[] = "ELECTRON_RUN_AS_NODE";
  45. #endif
  46. ALLOW_UNUSED_TYPE bool IsEnvSet(const char* name) {
  47. #if defined(OS_WIN)
  48. size_t required_size;
  49. getenv_s(&required_size, nullptr, 0, name);
  50. return required_size != 0;
  51. #else
  52. char* indicator = getenv(name);
  53. return indicator && indicator[0] != '\0';
  54. #endif
  55. }
  56. #if defined(OS_POSIX)
  57. void FixStdioStreams() {
  58. // libuv may mark stdin/stdout/stderr as close-on-exec, which interferes
  59. // with chromium's subprocess spawning. As a workaround, we detect if these
  60. // streams are closed on startup, and reopen them as /dev/null if necessary.
  61. // Otherwise, an unrelated file descriptor will be assigned as stdout/stderr
  62. // which may cause various errors when attempting to write to them.
  63. //
  64. // For details see https://github.com/libuv/libuv/issues/2062
  65. struct stat st;
  66. if (fstat(STDIN_FILENO, &st) < 0 && errno == EBADF)
  67. ignore_result(freopen("/dev/null", "r", stdin));
  68. if (fstat(STDOUT_FILENO, &st) < 0 && errno == EBADF)
  69. ignore_result(freopen("/dev/null", "w", stdout));
  70. if (fstat(STDERR_FILENO, &st) < 0 && errno == EBADF)
  71. ignore_result(freopen("/dev/null", "w", stderr));
  72. }
  73. #endif
  74. } // namespace
  75. #if defined(OS_WIN)
  76. int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE, wchar_t* cmd, int) {
  77. struct Arguments {
  78. int argc = 0;
  79. wchar_t** argv = ::CommandLineToArgvW(::GetCommandLineW(), &argc);
  80. ~Arguments() { LocalFree(argv); }
  81. } arguments;
  82. if (!arguments.argv)
  83. return -1;
  84. #ifdef _DEBUG
  85. // Don't display assert dialog boxes in CI test runs
  86. static const char* kCI = "ELECTRON_CI";
  87. bool is_ci = IsEnvSet(kCI);
  88. if (!is_ci) {
  89. for (int i = 0; i < arguments.argc; ++i) {
  90. if (!_wcsicmp(arguments.argv[i], L"--ci")) {
  91. is_ci = true;
  92. _putenv_s(kCI, "1"); // set flag for child processes
  93. break;
  94. }
  95. }
  96. }
  97. if (is_ci) {
  98. _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG | _CRTDBG_MODE_FILE);
  99. _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
  100. _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG | _CRTDBG_MODE_FILE);
  101. _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
  102. _set_error_mode(_OUT_TO_STDERR);
  103. }
  104. #endif
  105. #if BUILDFLAG(ENABLE_RUN_AS_NODE)
  106. bool run_as_node = IsEnvSet(kRunAsNode);
  107. #else
  108. bool run_as_node = false;
  109. #endif
  110. // Make sure the output is printed to console.
  111. if (run_as_node || !IsEnvSet("ELECTRON_NO_ATTACH_CONSOLE"))
  112. base::RouteStdioToConsole(false);
  113. #ifndef DEBUG
  114. // Chromium has its own TLS subsystem which supports automatic destruction
  115. // of thread-local data, and also depends on memory allocation routines
  116. // provided by the CRT. The problem is that the auto-destruction mechanism
  117. // uses a hidden feature of the OS loader which calls a callback on thread
  118. // exit, but only after all loaded DLLs have been detached. Since the CRT is
  119. // also a DLL, it happens that by the time Chromium's `OnThreadExit` function
  120. // is called, the heap functions, though still in memory, no longer perform
  121. // their duties, and when Chromium calls `free` on its buffer, it triggers
  122. // an access violation error.
  123. // We work around this problem by invoking Chromium's `OnThreadExit` in time
  124. // from within the CRT's atexit facility, ensuring the heap functions are
  125. // still active. The second invocation from the OS loader will be a no-op.
  126. extern void NTAPI OnThreadExit(PVOID module, DWORD reason, PVOID reserved);
  127. atexit([]() { OnThreadExit(nullptr, DLL_THREAD_DETACH, nullptr); });
  128. #endif
  129. #if BUILDFLAG(ENABLE_RUN_AS_NODE)
  130. if (run_as_node) {
  131. std::vector<char*> argv(arguments.argc);
  132. std::transform(
  133. arguments.argv, arguments.argv + arguments.argc, argv.begin(),
  134. [](auto& a) { return _strdup(base::WideToUTF8(a).c_str()); });
  135. base::AtExitManager atexit_manager;
  136. base::i18n::InitializeICU();
  137. auto ret = atom::NodeMain(argv.size(), argv.data());
  138. std::for_each(argv.begin(), argv.end(), free);
  139. return ret;
  140. }
  141. #endif
  142. if (IsEnvSet("ELECTRON_INTERNAL_CRASH_SERVICE")) {
  143. return crash_service::Main(cmd);
  144. }
  145. if (!atom::CheckCommandLineArguments(arguments.argc, arguments.argv))
  146. return -1;
  147. sandbox::SandboxInterfaceInfo sandbox_info = {0};
  148. content::InitializeSandboxInfo(&sandbox_info);
  149. atom::AtomMainDelegate delegate;
  150. content::ContentMainParams params(&delegate);
  151. params.instance = instance;
  152. params.sandbox_info = &sandbox_info;
  153. atom::AtomCommandLine::Init(arguments.argc, arguments.argv);
  154. return content::ContentMain(params);
  155. }
  156. #elif defined(OS_LINUX) // defined(OS_WIN)
  157. int main(int argc, char* argv[]) {
  158. FixStdioStreams();
  159. #if BUILDFLAG(ENABLE_RUN_AS_NODE)
  160. if (IsEnvSet(kRunAsNode)) {
  161. base::i18n::InitializeICU();
  162. base::AtExitManager atexit_manager;
  163. return atom::NodeMain(argc, argv);
  164. }
  165. #endif
  166. atom::AtomMainDelegate delegate;
  167. content::ContentMainParams params(&delegate);
  168. params.argc = argc;
  169. params.argv = const_cast<const char**>(argv);
  170. atom::AtomCommandLine::Init(argc, argv);
  171. return content::ContentMain(params);
  172. }
  173. #else // defined(OS_LINUX)
  174. int main(int argc, char* argv[]) {
  175. FixStdioStreams();
  176. #if BUILDFLAG(ENABLE_RUN_AS_NODE)
  177. if (IsEnvSet(kRunAsNode)) {
  178. return AtomInitializeICUandStartNode(argc, argv);
  179. }
  180. #endif
  181. #if defined(HELPER_EXECUTABLE) && !defined(MAS_BUILD)
  182. uint32_t exec_path_size = 0;
  183. int rv = _NSGetExecutablePath(NULL, &exec_path_size);
  184. if (rv != -1) {
  185. fprintf(stderr, "_NSGetExecutablePath: get length failed\n");
  186. abort();
  187. }
  188. std::unique_ptr<char[]> exec_path(new char[exec_path_size]);
  189. rv = _NSGetExecutablePath(exec_path.get(), &exec_path_size);
  190. if (rv != 0) {
  191. fprintf(stderr, "_NSGetExecutablePath: get path failed\n");
  192. abort();
  193. }
  194. sandbox::SeatbeltExecServer::CreateFromArgumentsResult seatbelt =
  195. sandbox::SeatbeltExecServer::CreateFromArguments(exec_path.get(), argc,
  196. argv);
  197. if (seatbelt.sandbox_required) {
  198. if (!seatbelt.server) {
  199. fprintf(stderr, "Failed to create seatbelt sandbox server.\n");
  200. abort();
  201. }
  202. if (!seatbelt.server->InitializeSandbox()) {
  203. fprintf(stderr, "Failed to initialize sandbox.\n");
  204. abort();
  205. }
  206. }
  207. #endif // defined(HELPER_EXECUTABLE) && !defined(MAS_BUILD)
  208. return AtomMain(argc, argv);
  209. }
  210. #endif // defined(OS_MACOSX)