relauncher.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // Copyright (c) 2016 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/browser/relauncher.h"
  5. #if BUILDFLAG(IS_WIN)
  6. #include <windows.h>
  7. #endif
  8. #include "base/files/file_util.h"
  9. #include "base/files/scoped_file.h"
  10. #include "base/logging.h"
  11. #include "base/path_service.h"
  12. #include "base/process/launch.h"
  13. #include "content/public/common/content_paths.h"
  14. #include "content/public/common/main_function_params.h"
  15. #include "shell/common/electron_command_line.h"
  16. #if BUILDFLAG(IS_POSIX)
  17. #include "base/posix/eintr_wrapper.h"
  18. #endif
  19. namespace {
  20. // The argument separating arguments intended for the relauncher process from
  21. // those intended for the relaunched process. "---" is chosen instead of "--"
  22. // because CommandLine interprets "--" as meaning "end of switches", but
  23. // for many purposes, the relauncher process' CommandLine ought to interpret
  24. // arguments intended for the relaunched process, to get the correct settings
  25. // for such things as logging and the user-data-dir in case it affects crash
  26. // reporting.
  27. constexpr base::CommandLine::CharType kRelauncherArgSeparator[] =
  28. FILE_PATH_LITERAL("---");
  29. // The "type" argument identifying a relauncher process ("--type=relauncher").
  30. constexpr base::CommandLine::CharType kRelauncherTypeArg[] =
  31. FILE_PATH_LITERAL("--type=relauncher");
  32. } // namespace
  33. namespace relauncher {
  34. namespace internal {
  35. #if BUILDFLAG(IS_POSIX)
  36. const int kRelauncherSyncFD = STDERR_FILENO + 1;
  37. #endif
  38. } // namespace internal
  39. bool RelaunchApp(const StringVector& argv) {
  40. // Use the currently-running application's helper process. The automatic
  41. // update feature is careful to leave the currently-running version alone,
  42. // so this is safe even if the relaunch is the result of an update having
  43. // been applied. In fact, it's safer than using the updated version of the
  44. // helper process, because there's no guarantee that the updated version's
  45. // relauncher implementation will be compatible with the running version's.
  46. base::FilePath child_path;
  47. if (!base::PathService::Get(content::CHILD_PROCESS_EXE, &child_path)) {
  48. LOG(ERROR) << "No CHILD_PROCESS_EXE";
  49. return false;
  50. }
  51. StringVector relauncher_args;
  52. return RelaunchAppWithHelper(child_path, relauncher_args, argv);
  53. }
  54. bool RelaunchAppWithHelper(const base::FilePath& helper,
  55. const StringVector& relauncher_args,
  56. const StringVector& argv) {
  57. StringVector relaunch_argv;
  58. relaunch_argv.push_back(helper.value());
  59. relaunch_argv.push_back(kRelauncherTypeArg);
  60. // Relauncher process has its own --type=relauncher which
  61. // is not recognized by the service_manager, explicitly set
  62. // the sandbox type to avoid CHECK failure in
  63. // service_manager::SandboxTypeFromCommandLine
  64. relaunch_argv.push_back(FILE_PATH_LITERAL("--no-sandbox"));
  65. relaunch_argv.insert(relaunch_argv.end(), relauncher_args.begin(),
  66. relauncher_args.end());
  67. relaunch_argv.push_back(kRelauncherArgSeparator);
  68. relaunch_argv.insert(relaunch_argv.end(), argv.begin(), argv.end());
  69. #if BUILDFLAG(IS_POSIX)
  70. int pipe_fds[2];
  71. if (HANDLE_EINTR(pipe(pipe_fds)) != 0) {
  72. PLOG(ERROR) << "pipe";
  73. return false;
  74. }
  75. // The parent process will only use pipe_read_fd as the read side of the
  76. // pipe. It can close the write side as soon as the relauncher process has
  77. // forked off. The relauncher process will only use pipe_write_fd as the
  78. // write side of the pipe. In that process, the read side will be closed by
  79. // base::LaunchApp because it won't be present in fd_map, and the write side
  80. // will be remapped to kRelauncherSyncFD by fd_map.
  81. base::ScopedFD pipe_read_fd(pipe_fds[0]);
  82. base::ScopedFD pipe_write_fd(pipe_fds[1]);
  83. // Make sure kRelauncherSyncFD is a safe value. base::LaunchProcess will
  84. // preserve these three FDs in forked processes, so kRelauncherSyncFD should
  85. // not conflict with them.
  86. static_assert(internal::kRelauncherSyncFD != STDIN_FILENO &&
  87. internal::kRelauncherSyncFD != STDOUT_FILENO &&
  88. internal::kRelauncherSyncFD != STDERR_FILENO,
  89. "kRelauncherSyncFD must not conflict with stdio fds");
  90. #endif
  91. base::LaunchOptions options;
  92. #if BUILDFLAG(IS_POSIX)
  93. options.fds_to_remap.emplace_back(pipe_write_fd.get(),
  94. internal::kRelauncherSyncFD);
  95. base::Process process = base::LaunchProcess(relaunch_argv, options);
  96. #elif BUILDFLAG(IS_WIN)
  97. base::Process process = base::LaunchProcess(
  98. internal::ArgvToCommandLineString(relaunch_argv), options);
  99. #endif
  100. if (!process.IsValid()) {
  101. LOG(ERROR) << "base::LaunchProcess failed";
  102. return false;
  103. }
  104. // The relauncher process is now starting up, or has started up. The
  105. // original parent process continues.
  106. #if BUILDFLAG(IS_WIN)
  107. // Synchronize with the relauncher process.
  108. StringType name = internal::GetWaitEventName(process.Pid());
  109. HANDLE wait_event = ::CreateEventW(nullptr, TRUE, FALSE, name.c_str());
  110. if (wait_event != nullptr) {
  111. WaitForSingleObject(wait_event, 1000);
  112. CloseHandle(wait_event);
  113. }
  114. #elif BUILDFLAG(IS_POSIX)
  115. pipe_write_fd.reset(); // close(pipe_fds[1]);
  116. // Synchronize with the relauncher process.
  117. char read_char;
  118. int read_result = HANDLE_EINTR(read(pipe_read_fd.get(), &read_char, 1));
  119. if (read_result != 1) {
  120. if (read_result < 0) {
  121. PLOG(ERROR) << "read";
  122. } else {
  123. LOG(ERROR) << "read: unexpected result " << read_result;
  124. }
  125. return false;
  126. }
  127. // Since a byte has been successfully read from the relauncher process, it's
  128. // guaranteed to have set up its kqueue monitoring this process for exit.
  129. // It's safe to exit now.
  130. #endif
  131. return true;
  132. }
  133. int RelauncherMain(const content::MainFunctionParams& main_parameters) {
  134. const StringVector& argv = electron::ElectronCommandLine::argv();
  135. if (argv.size() < 4 || argv[1] != kRelauncherTypeArg) {
  136. LOG(ERROR) << "relauncher process invoked with unexpected arguments";
  137. return 1;
  138. }
  139. internal::RelauncherSynchronizeWithParent();
  140. // Figure out what to execute, what arguments to pass it, and whether to
  141. // start it in the background.
  142. bool in_relauncher_args = false;
  143. StringVector relauncher_args;
  144. StringVector launch_argv;
  145. for (size_t argv_index = 2; argv_index < argv.size(); ++argv_index) {
  146. const StringType& arg(argv[argv_index]);
  147. if (!in_relauncher_args) {
  148. if (arg == kRelauncherArgSeparator) {
  149. in_relauncher_args = true;
  150. } else {
  151. relauncher_args.push_back(arg);
  152. }
  153. } else {
  154. launch_argv.push_back(arg);
  155. }
  156. }
  157. if (launch_argv.empty()) {
  158. LOG(ERROR) << "nothing to relaunch";
  159. return 1;
  160. }
  161. if (internal::LaunchProgram(relauncher_args, launch_argv) != 0) {
  162. LOG(ERROR) << "failed to launch program";
  163. return 1;
  164. }
  165. // The application should have relaunched (or is in the process of
  166. // relaunching). From this point on, only clean-up tasks should occur, and
  167. // failures are tolerable.
  168. return 0;
  169. }
  170. } // namespace relauncher