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 *
start_pr(char * file1,char * file2)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 (pipe2(pfd, O_CLOEXEC) == -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 error = posix_spawn_file_actions_adddup2(&fa, pfd[0], STDIN_FILENO);
75 if (error != 0)
76 errc(2, error, "posix_spawn_file_actions_adddup2");
77
78 char *argv[] = { __DECONST(char *, _PATH_PR),
79 __DECONST(char *, "-h"), header, NULL };
80 error = posix_spawn(&pid, _PATH_PR, &fa, &sa, argv, environ);
81 if (error != 0)
82 errc(2, error, "could not spawn pr");
83
84 posix_spawn_file_actions_destroy(&fa);
85 posix_spawnattr_destroy(&sa);
86
87 /* parent */
88 if (pfd[1] == STDOUT_FILENO) {
89 pr->ostdout = STDOUT_FILENO;
90 } else {
91 if ((pr->ostdout = dup(STDOUT_FILENO)) < 0 ||
92 dup2(pfd[1], STDOUT_FILENO) < 0) {
93 err(2, "stdout");
94 }
95 close(pfd[1]);
96 }
97 close(pfd[0]);
98 free(header);
99 return (pr);
100 }
101
102 /* close the pipe to pr and restore stdout */
103 void
stop_pr(struct pr * pr)104 stop_pr(struct pr *pr)
105 {
106 int wstatus;
107
108 if (pr == NULL)
109 return;
110
111 fflush(stdout);
112 if (pr->ostdout != STDOUT_FILENO) {
113 dup2(pr->ostdout, STDOUT_FILENO);
114 close(pr->ostdout);
115 }
116 while (pdwait(pr->procd, &wstatus, WEXITED, NULL, NULL) == -1) {
117 if (errno != EINTR)
118 err(2, "pdwait");
119 }
120 close(pr->procd);
121 free(pr);
122 if (WIFEXITED(wstatus) && WEXITSTATUS(wstatus) != 0)
123 errx(2, "pr exited abnormally");
124 else if (WIFSIGNALED(wstatus))
125 errx(2, "pr killed by signal %d",
126 WTERMSIG(wstatus));
127 }
128