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 <paths.h>
32#include <signal.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <unistd.h>
36
37#include "pr.h"
38#include "diff.h"
39#include "xmalloc.h"
40
41#define _PATH_PR "/usr/bin/pr"
42
43struct pr *
44start_pr(char *file1, char *file2)
45{
46	int pfd[2];
47	int pr_pd;
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	rewind(stdout);
58	if (pipe(pfd) == -1)
59		err(2, "pipe");
60	switch ((pid = pdfork(&pr_pd, PD_CLOEXEC))) {
61	case -1:
62		status |= 2;
63		free(header);
64		err(2, "No more processes");
65	case 0:
66		/* child */
67		if (pfd[0] != STDIN_FILENO) {
68			dup2(pfd[0], STDIN_FILENO);
69			close(pfd[0]);
70		}
71		close(pfd[1]);
72		execl(_PATH_PR, _PATH_PR, "-h", header, (char *)0);
73		_exit(127);
74	default:
75
76		/* parent */
77		if (pfd[1] != STDOUT_FILENO) {
78			pr->ostdout = dup(STDOUT_FILENO);
79			dup2(pfd[1], STDOUT_FILENO);
80			close(pfd[1]);
81		}
82		close(pfd[0]);
83		rewind(stdout);
84		free(header);
85		pr->kq = kqueue();
86		if (pr->kq == -1)
87			err(2, "kqueue");
88		pr->e = xmalloc(sizeof(struct kevent));
89		EV_SET(pr->e, pr_pd, EVFILT_PROCDESC, EV_ADD, NOTE_EXIT, 0,
90		    NULL);
91		if (kevent(pr->kq, pr->e, 1, NULL, 0, NULL) == -1)
92			err(2, "kevent");
93	}
94	return (pr);
95}
96
97/* close the pipe to pr and restore stdout */
98void
99stop_pr(struct pr *pr)
100{
101	int wstatus;
102
103	if (pr == NULL)
104		return;
105
106	fflush(stdout);
107	if (pr->ostdout != STDOUT_FILENO) {
108		close(STDOUT_FILENO);
109		dup2(pr->ostdout, STDOUT_FILENO);
110		close(pr->ostdout);
111	}
112	if (kevent(pr->kq, NULL, 0, pr->e, 1, NULL) == -1)
113		err(2, "kevent");
114	wstatus = pr->e[0].data;
115	close(pr->kq);
116	free(pr);
117	if (WIFEXITED(wstatus) && WEXITSTATUS(wstatus) != 0)
118		errx(2, "pr exited abnormally");
119	else if (WIFSIGNALED(wstatus))
120		errx(2, "pr killed by signal %d",
121		    WTERMSIG(wstatus));
122}
123