relauncher_mac.cc 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 <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 ||
  59. event.fflags != NOTE_EXIT ||
  60. event.ident != static_cast<uintptr_t>(parent_pid)) {
  61. LOG(ERROR) << "kevent (monitor): unexpected event, filter " << event.filter
  62. << ", fflags " << event.fflags << ", ident " << event.ident;
  63. return;
  64. }
  65. }
  66. int LaunchProgram(const StringVector& relauncher_args,
  67. const StringVector& argv) {
  68. // Redirect the stdout of child process to /dev/null, otherwise after
  69. // relaunch the child process will raise exception when writing to stdout.
  70. base::ScopedFD devnull(HANDLE_EINTR(open("/dev/null", O_WRONLY)));
  71. base::FileHandleMappingVector no_stdout;
  72. no_stdout.push_back(std::make_pair(devnull.get(), STDERR_FILENO));
  73. no_stdout.push_back(std::make_pair(devnull.get(), STDOUT_FILENO));
  74. base::LaunchOptions options;
  75. options.new_process_group = true; // detach
  76. options.fds_to_remap = &no_stdout;
  77. base::Process process = base::LaunchProcess(argv, options);
  78. return process.IsValid() ? 0 : 1;
  79. }
  80. } // namespace internal
  81. } // namespace relauncher