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