relauncher.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. relaunch_argv.insert(relaunch_argv.end(), relauncher_args.begin(),
  49. relauncher_args.end());
  50. relaunch_argv.push_back(internal::kRelauncherArgSeparator);
  51. relaunch_argv.insert(relaunch_argv.end(), argv.begin(), argv.end());
  52. #if defined(OS_POSIX)
  53. int pipe_fds[2];
  54. if (HANDLE_EINTR(pipe(pipe_fds)) != 0) {
  55. PLOG(ERROR) << "pipe";
  56. return false;
  57. }
  58. // The parent process will only use pipe_read_fd as the read side of the
  59. // pipe. It can close the write side as soon as the relauncher process has
  60. // forked off. The relauncher process will only use pipe_write_fd as the
  61. // write side of the pipe. In that process, the read side will be closed by
  62. // base::LaunchApp because it won't be present in fd_map, and the write side
  63. // will be remapped to kRelauncherSyncFD by fd_map.
  64. base::ScopedFD pipe_read_fd(pipe_fds[0]);
  65. base::ScopedFD pipe_write_fd(pipe_fds[1]);
  66. // Make sure kRelauncherSyncFD is a safe value. base::LaunchProcess will
  67. // preserve these three FDs in forked processes, so kRelauncherSyncFD should
  68. // not conflict with them.
  69. static_assert(internal::kRelauncherSyncFD != STDIN_FILENO &&
  70. internal::kRelauncherSyncFD != STDOUT_FILENO &&
  71. internal::kRelauncherSyncFD != STDERR_FILENO,
  72. "kRelauncherSyncFD must not conflict with stdio fds");
  73. #endif
  74. base::LaunchOptions options;
  75. #if defined(OS_POSIX)
  76. options.fds_to_remap.push_back(
  77. std::make_pair(pipe_write_fd.get(), internal::kRelauncherSyncFD));
  78. base::Process process = base::LaunchProcess(relaunch_argv, options);
  79. #elif defined(OS_WIN)
  80. base::Process process = base::LaunchProcess(
  81. internal::ArgvToCommandLineString(relaunch_argv), options);
  82. #endif
  83. if (!process.IsValid()) {
  84. LOG(ERROR) << "base::LaunchProcess failed";
  85. return false;
  86. }
  87. // The relauncher process is now starting up, or has started up. The
  88. // original parent process continues.
  89. #if defined(OS_WIN)
  90. // Synchronize with the relauncher process.
  91. StringType name = internal::GetWaitEventName(process.Pid());
  92. HANDLE wait_event = ::CreateEventW(NULL, TRUE, FALSE, name.c_str());
  93. if (wait_event != NULL) {
  94. WaitForSingleObject(wait_event, 1000);
  95. CloseHandle(wait_event);
  96. }
  97. #elif defined(OS_POSIX)
  98. pipe_write_fd.reset(); // close(pipe_fds[1]);
  99. // Synchronize with the relauncher process.
  100. char read_char;
  101. int read_result = HANDLE_EINTR(read(pipe_read_fd.get(), &read_char, 1));
  102. if (read_result != 1) {
  103. if (read_result < 0) {
  104. PLOG(ERROR) << "read";
  105. } else {
  106. LOG(ERROR) << "read: unexpected result " << read_result;
  107. }
  108. return false;
  109. }
  110. // Since a byte has been successfully read from the relauncher process, it's
  111. // guaranteed to have set up its kqueue monitoring this process for exit.
  112. // It's safe to exit now.
  113. #endif
  114. return true;
  115. }
  116. int RelauncherMain(const content::MainFunctionParams& main_parameters) {
  117. const StringVector& argv = electron::ElectronCommandLine::argv();
  118. if (argv.size() < 4 || argv[1] != internal::kRelauncherTypeArg) {
  119. LOG(ERROR) << "relauncher process invoked with unexpected arguments";
  120. return 1;
  121. }
  122. internal::RelauncherSynchronizeWithParent();
  123. // Figure out what to execute, what arguments to pass it, and whether to
  124. // start it in the background.
  125. bool in_relauncher_args = false;
  126. StringType relaunch_executable;
  127. StringVector relauncher_args;
  128. StringVector launch_argv;
  129. for (size_t argv_index = 2; argv_index < argv.size(); ++argv_index) {
  130. const StringType& arg(argv[argv_index]);
  131. if (!in_relauncher_args) {
  132. if (arg == internal::kRelauncherArgSeparator) {
  133. in_relauncher_args = true;
  134. } else {
  135. relauncher_args.push_back(arg);
  136. }
  137. } else {
  138. launch_argv.push_back(arg);
  139. }
  140. }
  141. if (launch_argv.empty()) {
  142. LOG(ERROR) << "nothing to relaunch";
  143. return 1;
  144. }
  145. if (internal::LaunchProgram(relauncher_args, launch_argv) != 0) {
  146. LOG(ERROR) << "failed to launch program";
  147. return 1;
  148. }
  149. // The application should have relaunched (or is in the process of
  150. // relaunching). From this point on, only clean-up tasks should occur, and
  151. // failures are tolerable.
  152. return 0;
  153. }
  154. } // namespace relauncher