xref: /freebsd/usr.bin/diff/pr.c (revision 253f15c016ca699906f78b8e522a3f7ed675929b)
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 <errno.h>
32 #include <fcntl.h>
33 #include <paths.h>
34 #include <signal.h>
35 #include <spawn.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 extern char **environ;
47 
48 struct pr *
49 start_pr(char *file1, char *file2)
50 {
51 	int pfd[2];
52 	pid_t pid;
53 	char *header;
54 	struct pr *pr;
55 	posix_spawn_file_actions_t fa;
56 	posix_spawnattr_t sa;
57 	int error;
58 
59 	pr = xcalloc(1, sizeof(*pr));
60 
61 	xasprintf(&header, "%s %s %s", diffargs, file1, file2);
62 	signal(SIGPIPE, SIG_IGN);
63 	fflush(stdout);
64 	if (pipe(pfd) == -1)
65 		err(2, "pipe");
66 
67 	if ((error = posix_spawnattr_init(&sa)) != 0)
68 		errc(2, error, "posix_spawnattr_init");
69 	if ((error = posix_spawn_file_actions_init(&fa)) != 0)
70 		errc(2, error, "posix_spawn_file_actions_init");
71 
72 	posix_spawnattr_setprocdescp_np(&sa, &pr->procd, 0);
73 
74 	if (pfd[0] != STDIN_FILENO) {
75 		posix_spawn_file_actions_adddup2(&fa, pfd[0], STDIN_FILENO);
76 		posix_spawn_file_actions_addclose(&fa, pfd[0]);
77 	}
78 	posix_spawn_file_actions_addclose(&fa, pfd[1]);
79 
80 	char *argv[] = { __DECONST(char *, _PATH_PR),
81 	    __DECONST(char *, "-h"), header, NULL };
82 	error = posix_spawn(&pid, _PATH_PR, &fa, &sa, argv, environ);
83 	if (error != 0)
84 		errc(2, error, "could not spawn pr");
85 
86 	posix_spawn_file_actions_destroy(&fa);
87 	posix_spawnattr_destroy(&sa);
88 
89 	/* parent */
90 	if (pfd[1] == STDOUT_FILENO) {
91 		pr->ostdout = STDOUT_FILENO;
92 	} else {
93 		if ((pr->ostdout = dup(STDOUT_FILENO)) < 0 ||
94 		    dup2(pfd[1], STDOUT_FILENO) < 0) {
95 			err(2, "stdout");
96 		}
97 		close(pfd[1]);
98 	}
99 	close(pfd[0]);
100 	free(header);
101 	return (pr);
102 }
103 
104 /* close the pipe to pr and restore stdout */
105 void
106 stop_pr(struct pr *pr)
107 {
108 	int wstatus;
109 
110 	if (pr == NULL)
111 		return;
112 
113 	fflush(stdout);
114 	if (pr->ostdout != STDOUT_FILENO) {
115 		dup2(pr->ostdout, STDOUT_FILENO);
116 		close(pr->ostdout);
117 	}
118 	while (pdwait(pr->procd, &wstatus, WEXITED, NULL, NULL) == -1) {
119 		if (errno != EINTR)
120 			err(2, "pdwait");
121 	}
122 	close(pr->procd);
123 	free(pr);
124 	if (WIFEXITED(wstatus) && WEXITSTATUS(wstatus) != 0)
125 		errx(2, "pr exited abnormally");
126 	else if (WIFSIGNALED(wstatus))
127 		errx(2, "pr killed by signal %d",
128 		    WTERMSIG(wstatus));
129 }
130