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