relauncher.cc 6.1 KB

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