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