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 <fcntl.h> 33 #include <paths.h> 34 #include <signal.h> 35 #include <spawn.h> 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <unistd.h> 39 40 #include "pr.h" 41 #include "diff.h" 42 #include "xmalloc.h" 43 44 #define _PATH_PR "/usr/bin/pr" 45 46 extern char **environ; 47 48 struct pr * 49 start_pr(char *file1, char *file2) 50 { 51 int pfd[2]; 52 pid_t pid; 53 char *header; 54 struct pr *pr; 55 posix_spawn_file_actions_t fa; 56 posix_spawnattr_t sa; 57 int error; 58 59 pr = xcalloc(1, sizeof(*pr)); 60 61 xasprintf(&header, "%s %s %s", diffargs, file1, file2); 62 signal(SIGPIPE, SIG_IGN); 63 fflush(stdout); 64 if (pipe2(pfd, O_CLOEXEC) == -1) 65 err(2, "pipe"); 66 67 if ((error = posix_spawnattr_init(&sa)) != 0) 68 errc(2, error, "posix_spawnattr_init"); 69 if ((error = posix_spawn_file_actions_init(&fa)) != 0) 70 errc(2, error, "posix_spawn_file_actions_init"); 71 72 posix_spawnattr_setprocdescp_np(&sa, &pr->procd, 0); 73 74 if (pfd[0] != STDIN_FILENO) 75 posix_spawn_file_actions_adddup2(&fa, pfd[0], STDIN_FILENO); 76 77 char *argv[] = { __DECONST(char *, _PATH_PR), 78 __DECONST(char *, "-h"), header, NULL }; 79 error = posix_spawn(&pid, _PATH_PR, &fa, &sa, argv, environ); 80 if (error != 0) 81 errc(2, error, "could not spawn pr"); 82 83 posix_spawn_file_actions_destroy(&fa); 84 posix_spawnattr_destroy(&sa); 85 86 /* parent */ 87 if (pfd[1] == STDOUT_FILENO) { 88 pr->ostdout = STDOUT_FILENO; 89 } else { 90 if ((pr->ostdout = dup(STDOUT_FILENO)) < 0 || 91 dup2(pfd[1], STDOUT_FILENO) < 0) { 92 err(2, "stdout"); 93 } 94 close(pfd[1]); 95 } 96 close(pfd[0]); 97 free(header); 98 return (pr); 99 } 100 101 /* close the pipe to pr and restore stdout */ 102 void 103 stop_pr(struct pr *pr) 104 { 105 int wstatus; 106 107 if (pr == NULL) 108 return; 109 110 fflush(stdout); 111 if (pr->ostdout != STDOUT_FILENO) { 112 dup2(pr->ostdout, STDOUT_FILENO); 113 close(pr->ostdout); 114 } 115 while (pdwait(pr->procd, &wstatus, WEXITED, NULL, NULL) == -1) { 116 if (errno != EINTR) 117 err(2, "pdwait"); 118 } 119 close(pr->procd); 120 free(pr); 121 if (WIFEXITED(wstatus) && WEXITSTATUS(wstatus) != 0) 122 errx(2, "pr exited abnormally"); 123 else if (WIFSIGNALED(wstatus)) 124 errx(2, "pr killed by signal %d", 125 WTERMSIG(wstatus)); 126 } 127