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