atom_main.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 <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 "atom/app/atom_main_delegate.h"
  16. #include "atom/app/command_line_args.h"
  17. #include "atom/common/crash_reporter/win/crash_service_main.h"
  18. #include "base/environment.h"
  19. #include "base/process/launch.h"
  20. #include "base/strings/utf_string_conversions.h"
  21. #include "base/win/windows_version.h"
  22. #include "content/public/app/sandbox_helper_win.h"
  23. #include "sandbox/win/src/sandbox_types.h"
  24. #elif defined(OS_LINUX) // defined(OS_WIN)
  25. #include <unistd.h>
  26. #include <cstdio>
  27. #include "atom/app/atom_main_delegate.h" // NOLINT
  28. #include "content/public/app/content_main.h"
  29. #else // defined(OS_LINUX)
  30. #include <mach-o/dyld.h>
  31. #include <unistd.h>
  32. #include <cstdio>
  33. #include "atom/app/atom_library_main.h"
  34. #endif // defined(OS_MACOSX)
  35. #include "atom/app/node_main.h"
  36. #include "atom/common/atom_command_line.h"
  37. #include "atom/common/atom_constants.h"
  38. #include "base/at_exit.h"
  39. #include "base/i18n/icu_util.h"
  40. #include "electron/buildflags/buildflags.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 = "ELECTRON_CI";
  89. bool is_ci = IsEnvSet(kCI);
  90. if (!is_ci) {
  91. for (int i = 0; i < arguments.argc; ++i) {
  92. if (!_wcsicmp(arguments.argv[i], L"--ci")) {
  93. is_ci = true;
  94. _putenv_s(kCI, "1"); // set flag for child processes
  95. break;
  96. }
  97. }
  98. }
  99. if (is_ci) {
  100. _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG | _CRTDBG_MODE_FILE);
  101. _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
  102. _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG | _CRTDBG_MODE_FILE);
  103. _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
  104. _set_error_mode(_OUT_TO_STDERR);
  105. }
  106. #endif
  107. #if BUILDFLAG(ENABLE_RUN_AS_NODE)
  108. bool run_as_node = IsEnvSet(atom::kRunAsNode);
  109. #else
  110. bool run_as_node = false;
  111. #endif
  112. // Make sure the output is printed to console.
  113. if (run_as_node || !IsEnvSet("ELECTRON_NO_ATTACH_CONSOLE"))
  114. base::RouteStdioToConsole(false);
  115. std::vector<char*> argv(arguments.argc);
  116. std::transform(arguments.argv, arguments.argv + arguments.argc, argv.begin(),
  117. [](auto& a) { return _strdup(base::WideToUTF8(a).c_str()); });
  118. #if BUILDFLAG(ENABLE_RUN_AS_NODE)
  119. if (run_as_node) {
  120. base::AtExitManager atexit_manager;
  121. base::i18n::InitializeICU();
  122. auto ret = atom::NodeMain(argv.size(), argv.data());
  123. std::for_each(argv.begin(), argv.end(), free);
  124. return ret;
  125. }
  126. #endif
  127. base::CommandLine::Init(argv.size(), argv.data());
  128. const base::CommandLine& cmd_line = *base::CommandLine::ForCurrentProcess();
  129. if (cmd_line.GetSwitchValueASCII("type") ==
  130. crash_reporter::kCrashpadProcess) {
  131. return crash_service::Main(&argv);
  132. }
  133. if (!atom::CheckCommandLineArguments(arguments.argc, arguments.argv))
  134. return -1;
  135. sandbox::SandboxInterfaceInfo sandbox_info = {0};
  136. content::InitializeSandboxInfo(&sandbox_info);
  137. atom::AtomMainDelegate delegate;
  138. content::ContentMainParams params(&delegate);
  139. params.instance = instance;
  140. params.sandbox_info = &sandbox_info;
  141. atom::AtomCommandLine::Init(arguments.argc, arguments.argv);
  142. return content::ContentMain(params);
  143. }
  144. #elif defined(OS_LINUX) // defined(OS_WIN)
  145. int main(int argc, char* argv[]) {
  146. FixStdioStreams();
  147. #if BUILDFLAG(ENABLE_RUN_AS_NODE)
  148. if (IsEnvSet(atom::kRunAsNode)) {
  149. base::i18n::InitializeICU();
  150. base::AtExitManager atexit_manager;
  151. return atom::NodeMain(argc, argv);
  152. }
  153. #endif
  154. atom::AtomMainDelegate delegate;
  155. content::ContentMainParams params(&delegate);
  156. params.argc = argc;
  157. params.argv = const_cast<const char**>(argv);
  158. atom::AtomCommandLine::Init(argc, argv);
  159. return content::ContentMain(params);
  160. }
  161. #else // defined(OS_LINUX)
  162. int main(int argc, char* argv[]) {
  163. FixStdioStreams();
  164. #if BUILDFLAG(ENABLE_RUN_AS_NODE)
  165. if (IsEnvSet(atom::kRunAsNode)) {
  166. return AtomInitializeICUandStartNode(argc, argv);
  167. }
  168. #endif
  169. #if defined(HELPER_EXECUTABLE) && !defined(MAS_BUILD)
  170. uint32_t exec_path_size = 0;
  171. int rv = _NSGetExecutablePath(NULL, &exec_path_size);
  172. if (rv != -1) {
  173. fprintf(stderr, "_NSGetExecutablePath: get length failed\n");
  174. abort();
  175. }
  176. std::unique_ptr<char[]> exec_path(new char[exec_path_size]);
  177. rv = _NSGetExecutablePath(exec_path.get(), &exec_path_size);
  178. if (rv != 0) {
  179. fprintf(stderr, "_NSGetExecutablePath: get path failed\n");
  180. abort();
  181. }
  182. sandbox::SeatbeltExecServer::CreateFromArgumentsResult seatbelt =
  183. sandbox::SeatbeltExecServer::CreateFromArguments(exec_path.get(), argc,
  184. argv);
  185. if (seatbelt.sandbox_required) {
  186. if (!seatbelt.server) {
  187. fprintf(stderr, "Failed to create seatbelt sandbox server.\n");
  188. abort();
  189. }
  190. if (!seatbelt.server->InitializeSandbox()) {
  191. fprintf(stderr, "Failed to initialize sandbox.\n");
  192. abort();
  193. }
  194. }
  195. #endif // defined(HELPER_EXECUTABLE) && !defined(MAS_BUILD)
  196. return AtomMain(argc, argv);
  197. }
  198. #endif // defined(OS_MACOSX)