xref: /freebsd/usr.bin/diff/pr.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/procdesc.h>
31 #include <sys/wait.h>
32 
33 #include <err.h>
34 #include <paths.h>
35 #include <signal.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 struct pr *
47 start_pr(char *file1, char *file2)
48 {
49 	int pfd[2];
50 	int pr_pd;
51 	pid_t pid;
52 	char *header;
53 	struct pr *pr;
54 
55 	pr = xcalloc(1, sizeof(*pr));
56 
57 	xasprintf(&header, "%s %s %s", diffargs, file1, file2);
58 	signal(SIGPIPE, SIG_IGN);
59 	fflush(stdout);
60 	rewind(stdout);
61 	if (pipe(pfd) == -1)
62 		err(2, "pipe");
63 	switch ((pid = pdfork(&pr_pd, PD_CLOEXEC))) {
64 	case -1:
65 		status |= 2;
66 		free(header);
67 		err(2, "No more processes");
68 	case 0:
69 		/* child */
70 		if (pfd[0] != STDIN_FILENO) {
71 			dup2(pfd[0], STDIN_FILENO);
72 			close(pfd[0]);
73 		}
74 		close(pfd[1]);
75 		execl(_PATH_PR, _PATH_PR, "-h", header, (char *)0);
76 		_exit(127);
77 	default:
78 
79 		/* parent */
80 		if (pfd[1] != STDOUT_FILENO) {
81 			pr->ostdout = dup(STDOUT_FILENO);
82 			dup2(pfd[1], STDOUT_FILENO);
83 			close(pfd[1]);
84 		}
85 		close(pfd[0]);
86 		rewind(stdout);
87 		free(header);
88 		pr->kq = kqueue();
89 		if (pr->kq == -1)
90 			err(2, "kqueue");
91 		pr->e = xmalloc(sizeof(struct kevent));
92 		EV_SET(pr->e, pr_pd, EVFILT_PROCDESC, EV_ADD, NOTE_EXIT, 0,
93 		    NULL);
94 		if (kevent(pr->kq, pr->e, 1, NULL, 0, NULL) == -1)
95 			err(2, "kevent");
96 	}
97 	return (pr);
98 }
99 
100 /* close the pipe to pr and restore stdout */
101 void
102 stop_pr(struct pr *pr)
103 {
104 	int wstatus;
105 
106 	if (pr == NULL)
107 		return;
108 
109 	fflush(stdout);
110 	if (pr->ostdout != STDOUT_FILENO) {
111 		close(STDOUT_FILENO);
112 		dup2(pr->ostdout, STDOUT_FILENO);
113 		close(pr->ostdout);
114 	}
115 	if (kevent(pr->kq, NULL, 0, pr->e, 1, NULL) == -1)
116 		err(2, "kevent");
117 	wstatus = pr->e[0].data;
118 	close(pr->kq);
119 	free(pr);
120 	if (WIFEXITED(wstatus) && WEXITSTATUS(wstatus) != 0)
121 		errx(2, "pr exited abnormally");
122 	else if (WIFSIGNALED(wstatus))
123 		errx(2, "pr killed by signal %d",
124 		    WTERMSIG(wstatus));
125 }
126