The repository formerly known as dotfiles
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.

rec.c 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* Copyright (C) 2017 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. #include <sys/types.h>
  17. #include <err.h>
  18. #include <fcntl.h>
  19. #include <stdlib.h>
  20. #include <sys/event.h>
  21. #include <sys/wait.h>
  22. #include <sysexits.h>
  23. #include <unistd.h>
  24. static void watch(int kq, char *path) {
  25. int fd = open(path, O_CLOEXEC);
  26. if (fd < 0) err(EX_NOINPUT, "%s", path);
  27. struct kevent event;
  28. EV_SET(
  29. &event,
  30. fd,
  31. EVFILT_VNODE,
  32. EV_ADD | EV_CLEAR,
  33. NOTE_WRITE | NOTE_DELETE,
  34. 0,
  35. path
  36. );
  37. int nevents = kevent(kq, &event, 1, NULL, 0, NULL);
  38. if (nevents < 0) err(EX_OSERR, "kevent");
  39. }
  40. static void exec(char *const argv[]) {
  41. pid_t pid = fork();
  42. if (pid < 0) err(EX_OSERR, "fork");
  43. if (!pid) {
  44. execvp(*argv, argv);
  45. err(EX_NOINPUT, "%s", *argv);
  46. }
  47. int status;
  48. pid = wait(&status);
  49. if (pid < 0) err(EX_OSERR, "wait");
  50. if (WIFEXITED(status)) {
  51. warnx("exit %d\n", WEXITSTATUS(status));
  52. } else if (WIFSIGNALED(status)) {
  53. warnx("signal %d\n", WTERMSIG(status));
  54. } else {
  55. warnx("status %d\n", status);
  56. }
  57. }
  58. int main(int argc, char *argv[]) {
  59. if (argc < 3) return EX_USAGE;
  60. int kq = kqueue();
  61. if (kq < 0) err(EX_OSERR, "kqueue");
  62. int i;
  63. for (i = 1; i < argc - 1; ++i) {
  64. if (argv[i][0] == '-') {
  65. i++;
  66. break;
  67. }
  68. watch(kq, argv[i]);
  69. }
  70. exec(&argv[i]);
  71. for (;;) {
  72. struct kevent event;
  73. int nevents = kevent(kq, NULL, 0, &event, 1, NULL);
  74. if (nevents < 0) err(EX_OSERR, "kevent");
  75. if (event.fflags & NOTE_DELETE) {
  76. close(event.ident);
  77. sleep(1);
  78. watch(kq, (char *)event.udata);
  79. }
  80. exec(&argv[i]);
  81. }
  82. }