relauncher.cc 6.4 KB

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