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