pr.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*-
  2. * Copyright (c) 2017 Baptiste Daroussin <bapt@FreeBSD.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer
  10. * in this position and unchanged.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <sys/procdesc.h>
  27. #include <sys/wait.h>
  28. #include <err.h>
  29. #include <paths.h>
  30. #include <signal.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <unistd.h>
  34. #include "pr.h"
  35. #include "diff.h"
  36. #include "xmalloc.h"
  37. #define _PATH_PR "/usr/bin/pr"
  38. struct pr *
  39. start_pr(char *file1, char *file2)
  40. {
  41. int pfd[2];
  42. int pr_pd;
  43. pid_t pid;
  44. char *header;
  45. struct pr *pr;
  46. pr = xcalloc(1, sizeof(*pr));
  47. xasprintf(&header, "%s %s %s", diffargs, file1, file2);
  48. signal(SIGPIPE, SIG_IGN);
  49. fflush(stdout);
  50. rewind(stdout);
  51. if (pipe(pfd) == -1)
  52. err(2, "pipe");
  53. switch ((pid = pdfork(&pr_pd, PD_CLOEXEC))) {
  54. case -1:
  55. status |= 2;
  56. free(header);
  57. err(2, "No more processes");
  58. case 0:
  59. /* child */
  60. if (pfd[0] != STDIN_FILENO) {
  61. dup2(pfd[0], STDIN_FILENO);
  62. close(pfd[0]);
  63. }
  64. close(pfd[1]);
  65. execl(_PATH_PR, _PATH_PR, "-h", header, (char *)0);
  66. _exit(127);
  67. default:
  68. /* parent */
  69. if (pfd[1] != STDOUT_FILENO) {
  70. pr->ostdout = dup(STDOUT_FILENO);
  71. dup2(pfd[1], STDOUT_FILENO);
  72. close(pfd[1]);
  73. }
  74. close(pfd[0]);
  75. rewind(stdout);
  76. free(header);
  77. pr->kq = kqueue();
  78. if (pr->kq == -1)
  79. err(2, "kqueue");
  80. pr->e = xmalloc(sizeof(struct kevent));
  81. EV_SET(pr->e, pr_pd, EVFILT_PROCDESC, EV_ADD, NOTE_EXIT, 0,
  82. NULL);
  83. if (kevent(pr->kq, pr->e, 1, NULL, 0, NULL) == -1)
  84. err(2, "kevent");
  85. }
  86. return (pr);
  87. }
  88. /* close the pipe to pr and restore stdout */
  89. void
  90. stop_pr(struct pr *pr)
  91. {
  92. int wstatus;
  93. if (pr == NULL)
  94. return;
  95. fflush(stdout);
  96. if (pr->ostdout != STDOUT_FILENO) {
  97. close(STDOUT_FILENO);
  98. dup2(pr->ostdout, STDOUT_FILENO);
  99. close(pr->ostdout);
  100. }
  101. if (kevent(pr->kq, NULL, 0, pr->e, 1, NULL) == -1)
  102. err(2, "kevent");
  103. wstatus = pr->e[0].data;
  104. close(pr->kq);
  105. free(pr);
  106. if (WIFEXITED(wstatus) && WEXITSTATUS(wstatus) != 0)
  107. errx(2, "pr exited abnormally");
  108. else if (WIFSIGNALED(wstatus))
  109. errx(2, "pr killed by signal %d",
  110. WTERMSIG(wstatus));
  111. }