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