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
46struct pr *
47start_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			close(pfd[1]);
85			}
86			close(pfd[0]);
87			rewind(stdout);
88			free(header);
89			pr->kq = kqueue();
90			if (pr->kq == -1)
91				err(2, "kqueue");
92			pr->e = xmalloc(sizeof(struct kevent));
93			EV_SET(pr->e, pr_pd, EVFILT_PROCDESC, EV_ADD, NOTE_EXIT, 0,
94			    NULL);
95			if (kevent(pr->kq, pr->e, 1, NULL, 0, NULL) == -1)
96				err(2, "kevent");
97	}
98	return (pr);
99}
100
101/* close the pipe to pr and restore stdout */
102void
103stop_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		close(STDOUT_FILENO);
113		dup2(pr->ostdout, STDOUT_FILENO);
114		close(pr->ostdout);
115	}
116	if (kevent(pr->kq, NULL, 0, pr->e, 1, NULL) == -1)
117		err(2, "kevent");
118	wstatus = pr->e[0].data;
119	close(pr->kq);
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