relauncher.cc 6.4 KB

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