atom_main.cc 6.5 KB

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