popen.c revision 1.11 1 /* $NetBSD: popen.c,v 1.11 1998/09/06 10:39:41 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.11 1998/09/06 10:39:41 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 <setjmp.h>
55 #include <signal.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
60
61 #include "extern.h"
62
63 #define INCR 100
64 /*
65 * Special version of popen which avoids call to shell. This ensures no-one
66 * may create a pipe to a hidden program as a side effect of a list or dir
67 * command.
68 * If stderrfd != -1, then send stderr of a read command there,
69 * otherwise close stderr.
70 */
71 static int *pids;
72 static int fds;
73
74 FILE *
75 ftpd_popen(program, type, stderrfd)
76 char *program, *type;
77 int stderrfd;
78 {
79 char *cp;
80 FILE *iop = NULL;
81 int argc, gargc, pdes[2], pid;
82 char **pop, **np;
83 char **argv = NULL, **gargv = NULL;
84 size_t nargc = 100, ngargc = 100;
85 #ifdef __GNUC__
86 (void) &iop;
87 (void) &gargc;
88 (void) &gargv;
89 (void) &argv;
90 #endif
91
92 if ((*type != 'r' && *type != 'w') || type[1])
93 return (NULL);
94
95 if (!pids) {
96 if ((fds = getdtablesize()) <= 0)
97 return (NULL);
98 if ((pids = (int *)malloc((u_int)(fds * sizeof(int)))) == NULL)
99 return (NULL);
100 memset(pids, 0, fds * sizeof(int));
101 }
102 if (pipe(pdes) < 0)
103 return (NULL);
104
105 if ((argv = malloc(nargc * sizeof(char *))) == NULL)
106 return NULL;
107
108 #define CHECKMORE(c, v, n) \
109 if (c >= n + 2) { \
110 n += INCR; \
111 if ((np = realloc(v, n * sizeof(char *))) == NULL) \
112 goto pfree; \
113 else \
114 v = np; \
115 }
116
117 /* break up string into pieces */
118 for (argc = 0, cp = program;; cp = NULL) {
119 CHECKMORE(argc, argv, nargc)
120 if (!(argv[argc++] = strtok(cp, " \t\n")))
121 break;
122 }
123
124 if ((gargv = malloc(ngargc * sizeof(char *))) == NULL)
125 goto pfree;
126
127 /* glob each piece */
128 gargv[0] = argv[0];
129 for (gargc = argc = 1; argv[argc]; argc++) {
130 glob_t gl;
131 int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE;
132
133 memset(&gl, 0, sizeof(gl));
134 if (glob(argv[argc], flags, NULL, &gl)) {
135 CHECKMORE(gargc, gargv, ngargc)
136 if ((gargv[gargc++] = strdup(argv[argc])) == NULL)
137 goto pfree;
138 }
139 else
140 for (pop = gl.gl_pathv; *pop; pop++) {
141 CHECKMORE(gargc, gargv, ngargc)
142 if ((gargv[gargc++] = strdup(*pop)) == NULL)
143 goto pfree;
144 }
145 globfree(&gl);
146 }
147 gargv[gargc] = NULL;
148
149 switch(pid = vfork()) {
150 case -1: /* error */
151 (void)close(pdes[0]);
152 (void)close(pdes[1]);
153 goto pfree;
154 /* NOTREACHED */
155 case 0: /* child */
156 if (*type == 'r') {
157 if (pdes[1] != STDOUT_FILENO) {
158 dup2(pdes[1], STDOUT_FILENO);
159 (void)close(pdes[1]);
160 }
161 if (stderrfd == -1)
162 (void)close(STDERR_FILENO);
163 else
164 dup2(stderrfd, STDERR_FILENO);
165 (void)close(pdes[0]);
166 } else {
167 if (pdes[0] != STDIN_FILENO) {
168 dup2(pdes[0], STDIN_FILENO);
169 (void)close(pdes[0]);
170 }
171 (void)close(pdes[1]);
172 }
173 execv(gargv[0], gargv);
174 _exit(1);
175 }
176 /* parent; assume fdopen can't fail... */
177 if (*type == 'r') {
178 iop = fdopen(pdes[0], type);
179 (void)close(pdes[1]);
180 } else {
181 iop = fdopen(pdes[1], type);
182 (void)close(pdes[0]);
183 }
184 pids[fileno(iop)] = pid;
185
186 pfree: if (gargv) {
187 for (argc = 1; argc < gargc; argc++)
188 free(gargv[argc]);
189 free(gargv);
190 }
191 if (argv)
192 free(argv);
193
194 return (iop);
195 }
196
197 int
198 ftpd_pclose(iop)
199 FILE *iop;
200 {
201 int fdes, status;
202 pid_t pid;
203 sigset_t sigset, osigset;
204
205 /*
206 * pclose returns -1 if stream is not associated with a
207 * `popened' command, or, if already `pclosed'.
208 */
209 if (pids == 0 || pids[fdes = fileno(iop)] == 0)
210 return (-1);
211 (void)fclose(iop);
212 sigemptyset(&sigset);
213 sigaddset(&sigset, SIGINT);
214 sigaddset(&sigset, SIGQUIT);
215 sigaddset(&sigset, SIGHUP);
216 sigprocmask(SIG_BLOCK, &sigset, &osigset);
217 while ((pid = waitpid(pids[fdes], &status, 0)) < 0 && errno == EINTR)
218 continue;
219 sigprocmask(SIG_SETMASK, &osigset, NULL);
220 pids[fdes] = 0;
221 if (pid < 0)
222 return (pid);
223 if (WIFEXITED(status))
224 return (WEXITSTATUS(status));
225 return (1);
226 }
227