relauncher_mac.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 <sys/event.h>
  6. #include <sys/time.h>
  7. #include <sys/types.h>
  8. #include <unistd.h>
  9. #include "base/files/file_util.h"
  10. #include "base/logging.h"
  11. #include "base/mac/mac_logging.h"
  12. #include "base/posix/eintr_wrapper.h"
  13. #include "base/process/launch.h"
  14. #include "base/strings/sys_string_conversions.h"
  15. namespace relauncher {
  16. namespace internal {
  17. void RelauncherSynchronizeWithParent() {
  18. base::ScopedFD relauncher_sync_fd(kRelauncherSyncFD);
  19. int parent_pid = getppid();
  20. // PID 1 identifies init. launchd, that is. launchd never starts the
  21. // relauncher process directly, having this parent_pid means that the parent
  22. // already exited and launchd "inherited" the relauncher as its child.
  23. // There's no reason to synchronize with launchd.
  24. if (parent_pid == 1) {
  25. LOG(ERROR) << "unexpected parent_pid";
  26. return;
  27. }
  28. // Set up a kqueue to monitor the parent process for exit.
  29. base::ScopedFD kq(kqueue());
  30. if (!kq.is_valid()) {
  31. PLOG(ERROR) << "kqueue";
  32. return;
  33. }
  34. struct kevent change = {0};
  35. EV_SET(&change, parent_pid, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, NULL);
  36. if (kevent(kq.get(), &change, 1, nullptr, 0, nullptr) == -1) {
  37. PLOG(ERROR) << "kevent (add)";
  38. return;
  39. }
  40. // Write a '\0' character to the pipe.
  41. if (HANDLE_EINTR(write(relauncher_sync_fd.get(), "", 1)) != 1) {
  42. PLOG(ERROR) << "write";
  43. return;
  44. }
  45. // Up until now, the parent process was blocked in a read waiting for the
  46. // write above to complete. The parent process is now free to exit. Wait for
  47. // that to happen.
  48. struct kevent event;
  49. int events = kevent(kq.get(), nullptr, 0, &event, 1, nullptr);
  50. if (events != 1) {
  51. if (events < 0) {
  52. PLOG(ERROR) << "kevent (monitor)";
  53. } else {
  54. LOG(ERROR) << "kevent (monitor): unexpected result " << events;
  55. }
  56. return;
  57. }
  58. if (event.filter != EVFILT_PROC || event.fflags != NOTE_EXIT ||
  59. event.ident != static_cast<uintptr_t>(parent_pid)) {
  60. LOG(ERROR) << "kevent (monitor): unexpected event, filter " << event.filter
  61. << ", fflags " << event.fflags << ", ident " << event.ident;
  62. return;
  63. }
  64. }
  65. int LaunchProgram(const StringVector& relauncher_args,
  66. const StringVector& argv) {
  67. // Redirect the stdout of child process to /dev/null, otherwise after
  68. // relaunch the child process will raise exception when writing to stdout.
  69. base::ScopedFD devnull(HANDLE_EINTR(open("/dev/null", O_WRONLY)));
  70. base::LaunchOptions options;
  71. options.new_process_group = true; // detach
  72. options.fds_to_remap.push_back(std::make_pair(devnull.get(), STDERR_FILENO));
  73. options.fds_to_remap.push_back(std::make_pair(devnull.get(), STDOUT_FILENO));
  74. base::Process process = base::LaunchProcess(argv, options);
  75. return process.IsValid() ? 0 : 1;
  76. }
  77. } // namespace internal
  78. } // namespace relauncher