xref: /freebsd/usr.bin/diff/pr.c (revision fd45b686f9d92f583366c75b22c04c7ee49709c0)
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 #include <sys/procdesc.h>
29 #include <sys/wait.h>
30 
31 #include <err.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 
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 		pr->kq = kqueue();
87 		if (pr->kq == -1)
88 			err(2, "kqueue");
89 		pr->e = xmalloc(sizeof(struct kevent));
90 		EV_SET(pr->e, pr_pd, EVFILT_PROCDESC, EV_ADD, NOTE_EXIT, 0,
91 		    NULL);
92 		if (kevent(pr->kq, pr->e, 1, NULL, 0, NULL) == -1)
93 			err(2, "kevent");
94 	}
95 	return (pr);
96 }
97 
98 /* close the pipe to pr and restore stdout */
99 void
100 stop_pr(struct pr *pr)
101 {
102 	int wstatus;
103 
104 	if (pr == NULL)
105 		return;
106 
107 	fflush(stdout);
108 	if (pr->ostdout != STDOUT_FILENO) {
109 		close(STDOUT_FILENO);
110 		dup2(pr->ostdout, STDOUT_FILENO);
111 		close(pr->ostdout);
112 	}
113 	if (kevent(pr->kq, NULL, 0, pr->e, 1, NULL) == -1)
114 		err(2, "kevent");
115 	wstatus = pr->e[0].data;
116 	close(pr->kq);
117 	free(pr);
118 	if (WIFEXITED(wstatus) && WEXITSTATUS(wstatus) != 0)
119 		errx(2, "pr exited abnormally");
120 	else if (WIFSIGNALED(wstatus))
121 		errx(2, "pr killed by signal %d",
122 		    WTERMSIG(wstatus));
123 }
124