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 27 #include <sys/procdesc.h> 28 #include <sys/wait.h> 29 30 #include <err.h> 31 #include <errno.h> 32 #include <paths.h> 33 #include <signal.h> 34 #include <stdio.h> 35 #include <stdlib.h> 36 #include <unistd.h> 37 38 #include "pr.h" 39 #include "diff.h" 40 #include "xmalloc.h" 41 42 #define _PATH_PR "/usr/bin/pr" 43 44 struct pr * 45 start_pr(char *file1, char *file2) 46 { 47 int pfd[2]; 48 int pr_pd; 49 pid_t pid; 50 char *header; 51 struct pr *pr; 52 53 pr = xcalloc(1, sizeof(*pr)); 54 55 xasprintf(&header, "%s %s %s", diffargs, file1, file2); 56 signal(SIGPIPE, SIG_IGN); 57 fflush(stdout); 58 rewind(stdout); 59 if (pipe(pfd) == -1) 60 err(2, "pipe"); 61 switch ((pid = pdfork(&pr_pd, PD_CLOEXEC))) { 62 case -1: 63 status |= 2; 64 free(header); 65 err(2, "No more processes"); 66 case 0: 67 /* child */ 68 if (pfd[0] != STDIN_FILENO) { 69 dup2(pfd[0], STDIN_FILENO); 70 close(pfd[0]); 71 } 72 close(pfd[1]); 73 execl(_PATH_PR, _PATH_PR, "-h", header, (char *)0); 74 _exit(127); 75 default: 76 pr->pidfd = pr_pd; 77 /* parent */ 78 if (pfd[1] != STDOUT_FILENO) { 79 pr->ostdout = dup(STDOUT_FILENO); 80 dup2(pfd[1], STDOUT_FILENO); 81 close(pfd[1]); 82 } 83 close(pfd[0]); 84 rewind(stdout); 85 free(header); 86 } 87 return (pr); 88 } 89 90 /* close the pipe to pr and restore stdout */ 91 void 92 stop_pr(struct pr *pr) 93 { 94 int wstatus; 95 96 if (pr == NULL) 97 return; 98 99 fflush(stdout); 100 if (pr->ostdout != STDOUT_FILENO) { 101 close(STDOUT_FILENO); 102 dup2(pr->ostdout, STDOUT_FILENO); 103 close(pr->ostdout); 104 } 105 while (pdwait(pr->pidfd, &wstatus, WEXITED, NULL, NULL) == -1) { 106 if (errno != EINTR) 107 err(2, "pdwait"); 108 } 109 free(pr); 110 if (WIFEXITED(wstatus) && WEXITSTATUS(wstatus) != 0) 111 errx(2, "pr exited abnormally"); 112 else if (WIFSIGNALED(wstatus)) 113 errx(2, "pr killed by signal %d", 114 WTERMSIG(wstatus)); 115 } 116