relauncher.cc 7.0 KB

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