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