relauncher.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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(),
  48. relauncher_args.begin(), 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. base::FileHandleMappingVector fd_map;
  73. fd_map.push_back(
  74. std::make_pair(pipe_write_fd.get(), internal::kRelauncherSyncFD));
  75. #endif
  76. base::LaunchOptions options;
  77. #if defined(OS_POSIX)
  78. options.fds_to_remap = &fd_map;
  79. base::Process process = base::LaunchProcess(relaunch_argv, options);
  80. #elif defined(OS_WIN)
  81. base::Process process = base::LaunchProcess(
  82. internal::ArgvToCommandLineString(relaunch_argv), options);
  83. #endif
  84. if (!process.IsValid()) {
  85. LOG(ERROR) << "base::LaunchProcess failed";
  86. return false;
  87. }
  88. // The relauncher process is now starting up, or has started up. The
  89. // original parent process continues.
  90. #if defined(OS_WIN)
  91. // Synchronize with the relauncher process.
  92. StringType name = internal::GetWaitEventName(process.Pid());
  93. HANDLE wait_event = ::CreateEventW(NULL, TRUE, FALSE, name.c_str());
  94. if (wait_event != NULL) {
  95. WaitForSingleObject(wait_event, 1000);
  96. CloseHandle(wait_event);
  97. }
  98. #elif defined(OS_POSIX)
  99. pipe_write_fd.reset(); // close(pipe_fds[1]);
  100. // Synchronize with the relauncher process.
  101. char read_char;
  102. int read_result = HANDLE_EINTR(read(pipe_read_fd.get(), &read_char, 1));
  103. if (read_result != 1) {
  104. if (read_result < 0) {
  105. PLOG(ERROR) << "read";
  106. } else {
  107. LOG(ERROR) << "read: unexpected result " << read_result;
  108. }
  109. return false;
  110. }
  111. // Since a byte has been successfully read from the relauncher process, it's
  112. // guaranteed to have set up its kqueue monitoring this process for exit.
  113. // It's safe to exit now.
  114. #endif
  115. return true;
  116. }
  117. int RelauncherMain(const content::MainFunctionParams& main_parameters) {
  118. const StringVector& argv = atom::AtomCommandLine::argv();
  119. if (argv.size() < 4 || argv[1] != internal::kRelauncherTypeArg) {
  120. LOG(ERROR) << "relauncher process invoked with unexpected arguments";
  121. return 1;
  122. }
  123. internal::RelauncherSynchronizeWithParent();
  124. // Figure out what to execute, what arguments to pass it, and whether to
  125. // start it in the background.
  126. bool in_relauncher_args = false;
  127. StringType relaunch_executable;
  128. StringVector relauncher_args;
  129. StringVector launch_argv;
  130. for (size_t argv_index = 2; argv_index < argv.size(); ++argv_index) {
  131. const StringType& arg(argv[argv_index]);
  132. if (!in_relauncher_args) {
  133. if (arg == internal::kRelauncherArgSeparator) {
  134. in_relauncher_args = true;
  135. } else {
  136. relauncher_args.push_back(arg);
  137. }
  138. } else {
  139. launch_argv.push_back(arg);
  140. }
  141. }
  142. if (launch_argv.empty()) {
  143. LOG(ERROR) << "nothing to relaunch";
  144. return 1;
  145. }
  146. if (internal::LaunchProgram(relauncher_args, launch_argv) != 0) {
  147. LOG(ERROR) << "failed to launch program";
  148. return 1;
  149. }
  150. // The application should have relaunched (or is in the process of
  151. // relaunching). From this point on, only clean-up tasks should occur, and
  152. // failures are tolerable.
  153. return 0;
  154. }
  155. } // namespace relauncher