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