xref: /freebsd/usr.sbin/autofs/popen.c (revision ba8d15d3a8b10be9f3a0cb86a60246f225a36736)
1 /*
2  * Copyright (c) 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 2014 The FreeBSD Foundation
5  * All rights reserved.
6  *
7  * This code is derived from software written by Ken Arnold and
8  * published in UNIX Review, Vol. 6, No. 8.
9  *
10  * Portions of this software were developed by Edward Tomasz Napierala
11  * under sponsorship from the FreeBSD Foundation.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  */
38 
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #include <sys/param.h>
43 #include <sys/queue.h>
44 #include <sys/wait.h>
45 
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <unistd.h>
49 #include <stdarg.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <paths.h>
54 
55 #include "common.h"
56 
57 extern char **environ;
58 
59 struct pid {
60 	SLIST_ENTRY(pid) next;
61 	FILE *outfp;
62 	pid_t pid;
63 	char *command;
64 };
65 static SLIST_HEAD(, pid) pidlist = SLIST_HEAD_INITIALIZER(pidlist);
66 
67 #define	ARGV_LEN	42
68 
69 /*
70  * Replacement for popen(3), without stdin (which we do not use), but with
71  * stderr, proper logging, and improved command line arguments passing.
72  * Error handling is built in - if it returns, then it succeeded.
73  */
74 FILE *
75 auto_popen(const char *argv0, ...)
76 {
77 	va_list ap;
78 	struct pid *cur, *p;
79 	pid_t pid;
80 	int error, i, nullfd, outfds[2];
81 	char *arg, *argv[ARGV_LEN], *command;
82 
83 	nullfd = open(_PATH_DEVNULL, O_RDWR, 0);
84 	if (nullfd < 0)
85 		log_err(1, "cannot open %s", _PATH_DEVNULL);
86 
87 	error = pipe(outfds);
88 	if (error != 0)
89 		log_err(1, "pipe");
90 
91 	cur = malloc(sizeof(struct pid));
92 	if (cur == NULL)
93 		log_err(1, "malloc");
94 
95 	argv[0] = checked_strdup(argv0);
96 	command = argv[0];
97 
98 	va_start(ap, argv0);
99 	for (i = 1;; i++) {
100 		if (i >= ARGV_LEN)
101 			log_errx(1, "too many arguments to auto_popen");
102 		arg = va_arg(ap, char *);
103 		argv[i] = arg;
104 		if (arg == NULL)
105 			break;
106 
107 		command = concat(command, ' ', arg);
108 	}
109 	va_end(ap);
110 
111 	cur->command = checked_strdup(command);
112 
113 	switch (pid = fork()) {
114 	case -1:			/* Error. */
115 		log_err(1, "fork");
116 		/* NOTREACHED */
117 	case 0:				/* Child. */
118 		dup2(nullfd, STDIN_FILENO);
119 		dup2(outfds[1], STDOUT_FILENO);
120 
121 		close(nullfd);
122 		close(outfds[0]);
123 		close(outfds[1]);
124 
125 		SLIST_FOREACH(p, &pidlist, next)
126 			close(fileno(p->outfp));
127 		execvp(argv[0], argv);
128 		log_err(1, "failed to execute %s", argv[0]);
129 		/* NOTREACHED */
130 	}
131 
132 	log_debugx("executing \"%s\" as pid %d", command, pid);
133 
134 	/* Parent; assume fdopen cannot fail. */
135 	cur->outfp = fdopen(outfds[0], "r");
136 	close(nullfd);
137 	close(outfds[1]);
138 
139 	/* Link into list of file descriptors. */
140 	cur->pid = pid;
141 	SLIST_INSERT_HEAD(&pidlist, cur, next);
142 
143 	return (cur->outfp);
144 }
145 
146 int
147 auto_pclose(FILE *iop)
148 {
149 	struct pid *cur, *last = NULL;
150 	int status;
151 	pid_t pid;
152 
153 	/*
154 	 * Find the appropriate file pointer and remove it from the list.
155 	 */
156 	SLIST_FOREACH(cur, &pidlist, next) {
157 		if (cur->outfp == iop)
158 			break;
159 		last = cur;
160 	}
161 	if (cur == NULL) {
162 		return (-1);
163 	}
164 	if (last == NULL)
165 		SLIST_REMOVE_HEAD(&pidlist, next);
166 	else
167 		SLIST_REMOVE_AFTER(last, next);
168 
169 	fclose(cur->outfp);
170 
171 	do {
172 		pid = wait4(cur->pid, &status, 0, NULL);
173 	} while (pid == -1 && errno == EINTR);
174 
175 	if (WIFSIGNALED(status)) {
176 		log_warnx("\"%s\", pid %d, terminated with signal %d",
177 		    cur->command, pid, WTERMSIG(status));
178 		return (status);
179 	}
180 
181 	if (WEXITSTATUS(status) != 0) {
182 		log_warnx("\"%s\", pid %d, terminated with exit status %d",
183 		    cur->command, pid, WEXITSTATUS(status));
184 		return (status);
185 	}
186 
187 	log_debugx("\"%s\", pid %d, terminated gracefully", cur->command, pid);
188 
189 	free(cur->command);
190 	free(cur);
191 
192 	return (pid == -1 ? -1 : status);
193 }
194