popen.c revision 1.14 1 1.14 lukem /* $NetBSD: popen.c,v 1.14 1999/05/17 15:14:54 lukem Exp $ */
2 1.5 cgd
3 1.1 cgd /*
4 1.3 deraadt * Copyright (c) 1988, 1993, 1994
5 1.3 deraadt * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software written by Ken Arnold and
8 1.1 cgd * published in UNIX Review, Vol. 6, No. 8.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.1 cgd * 3. All advertising materials mentioning features or use of this software
19 1.1 cgd * must display the following acknowledgement:
20 1.1 cgd * This product includes software developed by the University of
21 1.1 cgd * California, Berkeley and its contributors.
22 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.1 cgd * may be used to endorse or promote products derived from this software
24 1.1 cgd * without specific prior written permission.
25 1.1 cgd *
26 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 cgd * SUCH DAMAGE.
37 1.1 cgd *
38 1.1 cgd */
39 1.1 cgd
40 1.8 christos #include <sys/cdefs.h>
41 1.1 cgd #ifndef lint
42 1.5 cgd #if 0
43 1.3 deraadt static char sccsid[] = "@(#)popen.c 8.3 (Berkeley) 4/6/94";
44 1.5 cgd #else
45 1.14 lukem __RCSID("$NetBSD: popen.c,v 1.14 1999/05/17 15:14:54 lukem Exp $");
46 1.5 cgd #endif
47 1.1 cgd #endif /* not lint */
48 1.1 cgd
49 1.1 cgd #include <sys/types.h>
50 1.1 cgd #include <sys/wait.h>
51 1.3 deraadt
52 1.3 deraadt #include <errno.h>
53 1.3 deraadt #include <glob.h>
54 1.1 cgd #include <signal.h>
55 1.1 cgd #include <stdio.h>
56 1.1 cgd #include <stdlib.h>
57 1.1 cgd #include <string.h>
58 1.14 lukem #include <syslog.h>
59 1.3 deraadt #include <unistd.h>
60 1.13 explorer
61 1.13 explorer #ifdef KERBEROS5
62 1.13 explorer #include <krb5.h>
63 1.13 explorer #endif
64 1.3 deraadt
65 1.3 deraadt #include "extern.h"
66 1.1 cgd
67 1.8 christos #define INCR 100
68 1.1 cgd /*
69 1.9 lukem * Special version of popen which avoids call to shell. This ensures no-one
70 1.1 cgd * may create a pipe to a hidden program as a side effect of a list or dir
71 1.1 cgd * command.
72 1.9 lukem * If stderrfd != -1, then send stderr of a read command there,
73 1.9 lukem * otherwise close stderr.
74 1.1 cgd */
75 1.1 cgd static int *pids;
76 1.1 cgd static int fds;
77 1.1 cgd
78 1.14 lukem extern int ls_main __P((int, char *[]));
79 1.14 lukem
80 1.1 cgd FILE *
81 1.9 lukem ftpd_popen(program, type, stderrfd)
82 1.1 cgd char *program, *type;
83 1.9 lukem int stderrfd;
84 1.1 cgd {
85 1.3 deraadt char *cp;
86 1.8 christos FILE *iop = NULL;
87 1.14 lukem int argc, gargc, pdes[2], pid, isls;
88 1.8 christos char **pop, **np;
89 1.8 christos char **argv = NULL, **gargv = NULL;
90 1.8 christos size_t nargc = 100, ngargc = 100;
91 1.8 christos #ifdef __GNUC__
92 1.8 christos (void) &iop;
93 1.8 christos (void) &gargc;
94 1.8 christos (void) &gargv;
95 1.8 christos (void) &argv;
96 1.8 christos #endif
97 1.14 lukem isls = 0;
98 1.1 cgd
99 1.6 lukem if ((*type != 'r' && *type != 'w') || type[1])
100 1.3 deraadt return (NULL);
101 1.1 cgd
102 1.1 cgd if (!pids) {
103 1.1 cgd if ((fds = getdtablesize()) <= 0)
104 1.3 deraadt return (NULL);
105 1.1 cgd if ((pids = (int *)malloc((u_int)(fds * sizeof(int)))) == NULL)
106 1.3 deraadt return (NULL);
107 1.3 deraadt memset(pids, 0, fds * sizeof(int));
108 1.1 cgd }
109 1.1 cgd if (pipe(pdes) < 0)
110 1.3 deraadt return (NULL);
111 1.1 cgd
112 1.8 christos if ((argv = malloc(nargc * sizeof(char *))) == NULL)
113 1.8 christos return NULL;
114 1.8 christos
115 1.8 christos #define CHECKMORE(c, v, n) \
116 1.8 christos if (c >= n + 2) { \
117 1.8 christos n += INCR; \
118 1.8 christos if ((np = realloc(v, n * sizeof(char *))) == NULL) \
119 1.8 christos goto pfree; \
120 1.8 christos else \
121 1.8 christos v = np; \
122 1.8 christos }
123 1.8 christos
124 1.1 cgd /* break up string into pieces */
125 1.8 christos for (argc = 0, cp = program;; cp = NULL) {
126 1.8 christos CHECKMORE(argc, argv, nargc)
127 1.1 cgd if (!(argv[argc++] = strtok(cp, " \t\n")))
128 1.1 cgd break;
129 1.8 christos }
130 1.8 christos
131 1.8 christos if ((gargv = malloc(ngargc * sizeof(char *))) == NULL)
132 1.8 christos goto pfree;
133 1.1 cgd
134 1.1 cgd /* glob each piece */
135 1.1 cgd gargv[0] = argv[0];
136 1.1 cgd for (gargc = argc = 1; argv[argc]; argc++) {
137 1.3 deraadt glob_t gl;
138 1.10 kleink int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE;
139 1.3 deraadt
140 1.3 deraadt memset(&gl, 0, sizeof(gl));
141 1.8 christos if (glob(argv[argc], flags, NULL, &gl)) {
142 1.8 christos CHECKMORE(gargc, gargv, ngargc)
143 1.8 christos if ((gargv[gargc++] = strdup(argv[argc])) == NULL)
144 1.8 christos goto pfree;
145 1.8 christos }
146 1.3 deraadt else
147 1.8 christos for (pop = gl.gl_pathv; *pop; pop++) {
148 1.8 christos CHECKMORE(gargc, gargv, ngargc)
149 1.8 christos if ((gargv[gargc++] = strdup(*pop)) == NULL)
150 1.8 christos goto pfree;
151 1.8 christos }
152 1.3 deraadt globfree(&gl);
153 1.1 cgd }
154 1.1 cgd gargv[gargc] = NULL;
155 1.1 cgd
156 1.14 lukem isls = (strcmp(gargv[0], "/bin/ls") == 0);
157 1.14 lukem
158 1.14 lukem pid = isls ? fork() : vfork();
159 1.14 lukem switch (pid) {
160 1.1 cgd case -1: /* error */
161 1.1 cgd (void)close(pdes[0]);
162 1.1 cgd (void)close(pdes[1]);
163 1.1 cgd goto pfree;
164 1.1 cgd /* NOTREACHED */
165 1.1 cgd case 0: /* child */
166 1.1 cgd if (*type == 'r') {
167 1.3 deraadt if (pdes[1] != STDOUT_FILENO) {
168 1.3 deraadt dup2(pdes[1], STDOUT_FILENO);
169 1.1 cgd (void)close(pdes[1]);
170 1.1 cgd }
171 1.9 lukem if (stderrfd == -1)
172 1.7 lukem (void)close(STDERR_FILENO);
173 1.9 lukem else
174 1.9 lukem dup2(stderrfd, STDERR_FILENO);
175 1.1 cgd (void)close(pdes[0]);
176 1.1 cgd } else {
177 1.3 deraadt if (pdes[0] != STDIN_FILENO) {
178 1.3 deraadt dup2(pdes[0], STDIN_FILENO);
179 1.1 cgd (void)close(pdes[0]);
180 1.1 cgd }
181 1.1 cgd (void)close(pdes[1]);
182 1.14 lukem }
183 1.14 lukem if (isls) { /* use internal ls */
184 1.14 lukem optreset = optind = optopt = 1;
185 1.14 lukem closelog();
186 1.14 lukem exit(ls_main(gargc, gargv));
187 1.1 cgd }
188 1.1 cgd execv(gargv[0], gargv);
189 1.1 cgd _exit(1);
190 1.1 cgd }
191 1.1 cgd /* parent; assume fdopen can't fail... */
192 1.1 cgd if (*type == 'r') {
193 1.1 cgd iop = fdopen(pdes[0], type);
194 1.1 cgd (void)close(pdes[1]);
195 1.1 cgd } else {
196 1.1 cgd iop = fdopen(pdes[1], type);
197 1.1 cgd (void)close(pdes[0]);
198 1.1 cgd }
199 1.1 cgd pids[fileno(iop)] = pid;
200 1.1 cgd
201 1.8 christos pfree: if (gargv) {
202 1.8 christos for (argc = 1; argc < gargc; argc++)
203 1.8 christos free(gargv[argc]);
204 1.8 christos free(gargv);
205 1.8 christos }
206 1.8 christos if (argv)
207 1.8 christos free(argv);
208 1.3 deraadt
209 1.3 deraadt return (iop);
210 1.1 cgd }
211 1.1 cgd
212 1.3 deraadt int
213 1.1 cgd ftpd_pclose(iop)
214 1.1 cgd FILE *iop;
215 1.1 cgd {
216 1.6 lukem int fdes, status;
217 1.3 deraadt pid_t pid;
218 1.4 mycroft sigset_t sigset, osigset;
219 1.1 cgd
220 1.1 cgd /*
221 1.1 cgd * pclose returns -1 if stream is not associated with a
222 1.1 cgd * `popened' command, or, if already `pclosed'.
223 1.1 cgd */
224 1.1 cgd if (pids == 0 || pids[fdes = fileno(iop)] == 0)
225 1.3 deraadt return (-1);
226 1.1 cgd (void)fclose(iop);
227 1.4 mycroft sigemptyset(&sigset);
228 1.4 mycroft sigaddset(&sigset, SIGINT);
229 1.4 mycroft sigaddset(&sigset, SIGQUIT);
230 1.4 mycroft sigaddset(&sigset, SIGHUP);
231 1.4 mycroft sigprocmask(SIG_BLOCK, &sigset, &osigset);
232 1.3 deraadt while ((pid = waitpid(pids[fdes], &status, 0)) < 0 && errno == EINTR)
233 1.3 deraadt continue;
234 1.4 mycroft sigprocmask(SIG_SETMASK, &osigset, NULL);
235 1.1 cgd pids[fdes] = 0;
236 1.3 deraadt if (pid < 0)
237 1.3 deraadt return (pid);
238 1.3 deraadt if (WIFEXITED(status))
239 1.3 deraadt return (WEXITSTATUS(status));
240 1.3 deraadt return (1);
241 1.1 cgd }
242