relauncher_mac.cc 2.9 KB

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