xref: /freebsd/usr.sbin/autofs/popen.c (revision 5608fd23c27fa1e8ee595d7b678cbfd35d657fbe)
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  * $FreeBSD$
38  */
39 
40 #include <sys/param.h>
41 #include <sys/queue.h>
42 #include <sys/wait.h>
43 
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <unistd.h>
47 #include <stdarg.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <paths.h>
52 
53 #include "common.h"
54 
55 extern char **environ;
56 
57 struct pid {
58 	SLIST_ENTRY(pid) next;
59 	FILE *outfp;
60 	pid_t pid;
61 	char *command;
62 };
63 static SLIST_HEAD(, pid) pidlist = SLIST_HEAD_INITIALIZER(pidlist);
64 
65 #define	ARGV_LEN	42
66 
67 /*
68  * Replacement for popen(3), without stdin (which we do not use), but with
69  * stderr, proper logging, and improved command line arguments passing.
70  * Error handling is built in - if it returns, then it succeeded.
71  */
72 FILE *
73 auto_popen(const char *argv0, ...)
74 {
75 	va_list ap;
76 	struct pid *cur, *p;
77 	pid_t pid;
78 	int error, i, nullfd, outfds[2];
79 	char *arg, *argv[ARGV_LEN], *command;
80 
81 	nullfd = open(_PATH_DEVNULL, O_RDWR, 0);
82 	if (nullfd < 0)
83 		log_err(1, "cannot open %s", _PATH_DEVNULL);
84 
85 	error = pipe(outfds);
86 	if (error != 0)
87 		log_err(1, "pipe");
88 
89 	cur = malloc(sizeof(struct pid));
90 	if (cur == NULL)
91 		log_err(1, "malloc");
92 
93 	argv[0] = checked_strdup(argv0);
94 	command = argv[0];
95 
96 	va_start(ap, argv0);
97 	for (i = 1;; i++) {
98 		if (i >= ARGV_LEN)
99 			log_errx(1, "too many arguments to auto_popen");
100 		arg = va_arg(ap, char *);
101 		argv[i] = arg;
102 		if (arg == NULL)
103 			break;
104 
105 		command = separated_concat(command, arg, ' ');
106 	}
107 	va_end(ap);
108 
109 	cur->command = checked_strdup(command);
110 
111 	switch (pid = fork()) {
112 	case -1:			/* Error. */
113 		log_err(1, "fork");
114 		/* NOTREACHED */
115 	case 0:				/* Child. */
116 		dup2(nullfd, STDIN_FILENO);
117 		dup2(outfds[1], STDOUT_FILENO);
118 
119 		close(nullfd);
120 		close(outfds[0]);
121 		close(outfds[1]);
122 
123 		SLIST_FOREACH(p, &pidlist, next)
124 			close(fileno(p->outfp));
125 		execvp(argv[0], argv);
126 		log_err(1, "failed to execute %s", argv[0]);
127 		/* NOTREACHED */
128 	}
129 
130 	log_debugx("executing \"%s\" as pid %d", command, pid);
131 
132 	/* Parent; assume fdopen cannot fail. */
133 	cur->outfp = fdopen(outfds[0], "r");
134 	close(nullfd);
135 	close(outfds[1]);
136 
137 	/* Link into list of file descriptors. */
138 	cur->pid = pid;
139 	SLIST_INSERT_HEAD(&pidlist, cur, next);
140 
141 	return (cur->outfp);
142 }
143 
144 int
145 auto_pclose(FILE *iop)
146 {
147 	struct pid *cur, *last = NULL;
148 	int status;
149 	pid_t pid;
150 
151 	/*
152 	 * Find the appropriate file pointer and remove it from the list.
153 	 */
154 	SLIST_FOREACH(cur, &pidlist, next) {
155 		if (cur->outfp == iop)
156 			break;
157 		last = cur;
158 	}
159 	if (cur == NULL) {
160 		return (-1);
161 	}
162 	if (last == NULL)
163 		SLIST_REMOVE_HEAD(&pidlist, next);
164 	else
165 		SLIST_REMOVE_AFTER(last, next);
166 
167 	fclose(cur->outfp);
168 
169 	do {
170 		pid = wait4(cur->pid, &status, 0, NULL);
171 	} while (pid == -1 && errno == EINTR);
172 
173 	if (WIFSIGNALED(status)) {
174 		log_warnx("\"%s\", pid %d, terminated with signal %d",
175 		    cur->command, pid, WTERMSIG(status));
176 		return (status);
177 	}
178 
179 	if (WEXITSTATUS(status) != 0) {
180 		log_warnx("\"%s\", pid %d, terminated with exit status %d",
181 		    cur->command, pid, WEXITSTATUS(status));
182 		return (status);
183 	}
184 
185 	log_debugx("\"%s\", pid %d, terminated gracefully", cur->command, pid);
186 
187 	free(cur->command);
188 	free(cur);
189 
190 	return (pid == -1 ? -1 : status);
191 }
192