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