IRC client
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sandman.m 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* Copyright (C) 2018 Curtis McEnroe <june@causal.agency>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU Affero General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #import <Cocoa/Cocoa.h>
  17. #import <err.h>
  18. #import <signal.h>
  19. #import <stdlib.h>
  20. #import <sysexits.h>
  21. #import <unistd.h>
  22. void handler(int sig) {
  23. (void)sig;
  24. int status;
  25. pid_t pid = wait(&status);
  26. if (pid < 0) _exit(EX_OSERR);
  27. if (WIFSIGNALED(status)) {
  28. _exit(128 + WTERMSIG(status));
  29. } else {
  30. _exit(WEXITSTATUS(status));
  31. }
  32. }
  33. int main(int argc, char *argv[]) {
  34. if (argc < 2) return EX_USAGE;
  35. sigset_t mask;
  36. sigemptyset(&mask);
  37. struct sigaction sa = {
  38. .sa_handler = handler,
  39. .sa_mask = mask,
  40. .sa_flags = SA_NOCLDSTOP | SA_RESTART,
  41. };
  42. sigaction(SIGCHLD, &sa, NULL);
  43. pid_t pid = fork();
  44. if (pid < 0) err(EX_OSERR, "fork");
  45. if (!pid) {
  46. execvp(argv[1], &argv[1]);
  47. err(EX_NOINPUT, "%s", argv[1]);
  48. }
  49. [
  50. [[NSWorkspace sharedWorkspace] notificationCenter]
  51. addObserverForName: NSWorkspaceWillSleepNotification
  52. object: nil
  53. queue: nil
  54. usingBlock: ^(NSNotification *note) {
  55. (void)note;
  56. int error = kill(pid, SIGHUP);
  57. if (error) err(EX_UNAVAILABLE, "kill %d", pid);
  58. }
  59. ];
  60. [[NSApplication sharedApplication] run];
  61. }