xref: /netbsd-src/libexec/ftpd/popen.c (revision dc306354b0b29af51801a7632f1e95265a68cd81)
1 /*	$NetBSD: popen.c,v 1.12 1998/12/28 04:54:01 lukem Exp $	*/
2 
3 /*
4  * Copyright (c) 1988, 1993, 1994
5  *	The Regents of the University of California.  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  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
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/cdefs.h>
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)popen.c	8.3 (Berkeley) 4/6/94";
44 #else
45 __RCSID("$NetBSD: popen.c,v 1.12 1998/12/28 04:54:01 lukem Exp $");
46 #endif
47 #endif /* not lint */
48 
49 #include <sys/types.h>
50 #include <sys/wait.h>
51 
52 #include <errno.h>
53 #include <glob.h>
54 #include <signal.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59 
60 #include "extern.h"
61 
62 #define INCR	100
63 /*
64  * Special version of popen which avoids call to shell.  This ensures no-one
65  * may create a pipe to a hidden program as a side effect of a list or dir
66  * command.
67  * If stderrfd != -1, then send stderr of a read command there,
68  * otherwise close stderr.
69  */
70 static int *pids;
71 static int fds;
72 
73 FILE *
74 ftpd_popen(program, type, stderrfd)
75 	char *program, *type;
76 	int stderrfd;
77 {
78 	char *cp;
79 	FILE *iop = NULL;
80 	int argc, gargc, pdes[2], pid;
81 	char **pop, **np;
82 	char **argv = NULL, **gargv = NULL;
83 	size_t nargc = 100, ngargc = 100;
84 #ifdef __GNUC__
85 	(void) &iop;
86 	(void) &gargc;
87 	(void) &gargv;
88 	(void) &argv;
89 #endif
90 
91 	if ((*type != 'r' && *type != 'w') || type[1])
92 		return (NULL);
93 
94 	if (!pids) {
95 		if ((fds = getdtablesize()) <= 0)
96 			return (NULL);
97 		if ((pids = (int *)malloc((u_int)(fds * sizeof(int)))) == NULL)
98 			return (NULL);
99 		memset(pids, 0, fds * sizeof(int));
100 	}
101 	if (pipe(pdes) < 0)
102 		return (NULL);
103 
104 	if ((argv = malloc(nargc * sizeof(char *))) == NULL)
105 		return NULL;
106 
107 #define CHECKMORE(c, v, n) \
108 	if (c >= n + 2) { \
109 		n += INCR; \
110 		if ((np = realloc(v, n * sizeof(char *))) == NULL) \
111 			goto pfree; \
112 		else \
113 			v = np; \
114 	}
115 
116 	/* break up string into pieces */
117 	for (argc = 0, cp = program;; cp = NULL) {
118 		CHECKMORE(argc, argv, nargc)
119 		if (!(argv[argc++] = strtok(cp, " \t\n")))
120 			break;
121 	}
122 
123 	if ((gargv = malloc(ngargc * sizeof(char *))) == NULL)
124 		goto pfree;
125 
126 	/* glob each piece */
127 	gargv[0] = argv[0];
128 	for (gargc = argc = 1; argv[argc]; argc++) {
129 		glob_t gl;
130 		int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE;
131 
132 		memset(&gl, 0, sizeof(gl));
133 		if (glob(argv[argc], flags, NULL, &gl)) {
134 			CHECKMORE(gargc, gargv, ngargc)
135 			if ((gargv[gargc++] = strdup(argv[argc])) == NULL)
136 				goto pfree;
137 		}
138 		else
139 			for (pop = gl.gl_pathv; *pop; pop++) {
140 				CHECKMORE(gargc, gargv, ngargc)
141 				if ((gargv[gargc++] = strdup(*pop)) == NULL)
142 					goto pfree;
143 			}
144 		globfree(&gl);
145 	}
146 	gargv[gargc] = NULL;
147 
148 	switch(pid = vfork()) {
149 	case -1:			/* error */
150 		(void)close(pdes[0]);
151 		(void)close(pdes[1]);
152 		goto pfree;
153 		/* NOTREACHED */
154 	case 0:				/* child */
155 		if (*type == 'r') {
156 			if (pdes[1] != STDOUT_FILENO) {
157 				dup2(pdes[1], STDOUT_FILENO);
158 				(void)close(pdes[1]);
159 			}
160 			if (stderrfd == -1)
161 				(void)close(STDERR_FILENO);
162 			else
163 				dup2(stderrfd, STDERR_FILENO);
164 			(void)close(pdes[0]);
165 		} else {
166 			if (pdes[0] != STDIN_FILENO) {
167 				dup2(pdes[0], STDIN_FILENO);
168 				(void)close(pdes[0]);
169 			}
170 			(void)close(pdes[1]);
171 		}
172 		execv(gargv[0], gargv);
173 		_exit(1);
174 	}
175 	/* parent; assume fdopen can't fail...  */
176 	if (*type == 'r') {
177 		iop = fdopen(pdes[0], type);
178 		(void)close(pdes[1]);
179 	} else {
180 		iop = fdopen(pdes[1], type);
181 		(void)close(pdes[0]);
182 	}
183 	pids[fileno(iop)] = pid;
184 
185 pfree:	if (gargv) {
186 		for (argc = 1; argc < gargc; argc++)
187 			free(gargv[argc]);
188 		free(gargv);
189 	}
190 	if (argv)
191 		free(argv);
192 
193 	return (iop);
194 }
195 
196 int
197 ftpd_pclose(iop)
198 	FILE *iop;
199 {
200 	int fdes, status;
201 	pid_t pid;
202 	sigset_t sigset, osigset;
203 
204 	/*
205 	 * pclose returns -1 if stream is not associated with a
206 	 * `popened' command, or, if already `pclosed'.
207 	 */
208 	if (pids == 0 || pids[fdes = fileno(iop)] == 0)
209 		return (-1);
210 	(void)fclose(iop);
211 	sigemptyset(&sigset);
212 	sigaddset(&sigset, SIGINT);
213 	sigaddset(&sigset, SIGQUIT);
214 	sigaddset(&sigset, SIGHUP);
215 	sigprocmask(SIG_BLOCK, &sigset, &osigset);
216 	while ((pid = waitpid(pids[fdes], &status, 0)) < 0 && errno == EINTR)
217 		continue;
218 	sigprocmask(SIG_SETMASK, &osigset, NULL);
219 	pids[fdes] = 0;
220 	if (pid < 0)
221 		return (pid);
222 	if (WIFEXITED(status))
223 		return (WEXITSTATUS(status));
224 	return (1);
225 }
226