2 * Copyright (c) 2017 Baptiste Daroussin <bapt@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/procdesc.h>
44 #define _PATH_PR "/usr/bin/pr"
47 start_pr(char *file1, char *file2)
55 pr = xcalloc(1, sizeof(*pr));
57 xasprintf(&header, "%s %s %s", diffargs, file1, file2);
58 signal(SIGPIPE, SIG_IGN);
63 switch ((pid = pdfork(&pr_pd, PD_CLOEXEC))) {
67 err(2, "No more processes");
70 if (pfd[0] != STDIN_FILENO) {
71 dup2(pfd[0], STDIN_FILENO);
75 execl(_PATH_PR, _PATH_PR, "-h", header, (char *)0);
80 if (pfd[1] != STDOUT_FILENO) {
81 pr->ostdout = dup(STDOUT_FILENO);
82 dup2(pfd[1], STDOUT_FILENO);
92 pr->e = xmalloc(sizeof(struct kevent));
93 EV_SET(pr->e, pr_pd, EVFILT_PROCDESC, EV_ADD, NOTE_EXIT, 0,
95 if (kevent(pr->kq, pr->e, 1, NULL, 0, NULL) == -1)
101 /* close the pipe to pr and restore stdout */
103 stop_pr(struct pr *pr)
111 if (pr->ostdout != STDOUT_FILENO) {
112 close(STDOUT_FILENO);
113 dup2(pr->ostdout, STDOUT_FILENO);
116 if (kevent(pr->kq, NULL, 0, pr->e, 1, NULL) == -1)
118 wstatus = pr->e[0].data;
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",