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