relauncher.cc 6.4 KB

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