1 /* $NetBSD: popen.c,v 1.27 2001/12/01 10:25:30 lukem Exp $ */
4 * Copyright (c) 1999-2001 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
40 * Copyright (c) 1988, 1993, 1994
41 * The Regents of the University of California. All rights reserved.
43 * This code is derived from software written by Ken Arnold and
44 * published in UNIX Review, Vol. 6, No. 8.
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
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.
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
76 #include "lukemftpd.h"
82 * Special version of popen which avoids call to shell. This ensures no-one
83 * may create a pipe to a hidden program as a side effect of a list or dir
85 * If stderrfd != -1, then send stderr of a read command there,
86 * otherwise close stderr.
91 extern int ls_main(int, char *[]);
94 ftpd_popen(char *argv[], const char *ptype, int stderrfd)
97 int argc, pdes[2], pid, isls;
103 if ((*ptype != 'r' && *ptype != 'w') || ptype[1])
107 if ((fds = getdtablesize()) <= 0)
109 if ((pids = (int *)malloc((u_int)(fds * sizeof(int)))) == NULL)
111 memset(pids, 0, fds * sizeof(int));
116 if ((sl = sl_init()) == NULL)
119 /* glob each piece */
120 if (sl_add(sl, xstrdup(argv[0])) == -1)
122 for (argc = 1; argv[argc]; argc++) {
124 int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE|GLOB_LIMIT;
126 memset(&gl, 0, sizeof(gl));
127 if (glob(argv[argc], flags, NULL, &gl)) {
128 if (sl_add(sl, xstrdup(argv[argc])) == -1) {
133 for (pop = gl.gl_pathv; *pop; pop++) {
134 if (sl_add(sl, xstrdup(*pop)) == -1) {
142 if (sl_add(sl, NULL) == -1)
145 #ifndef NO_INTERNAL_LS
146 isls = (strcmp(sl->sl_str[0], INTERNAL_LS) == 0);
150 pid = isls ? fork() : vfork();
156 (void)close(pdes[0]);
157 (void)close(pdes[1]);
162 if (pdes[1] != STDOUT_FILENO) {
163 dup2(pdes[1], STDOUT_FILENO);
164 (void)close(pdes[1]);
167 (void)close(STDERR_FILENO);
169 dup2(stderrfd, STDERR_FILENO);
170 (void)close(pdes[0]);
172 if (pdes[0] != STDIN_FILENO) {
173 dup2(pdes[0], STDIN_FILENO);
174 (void)close(pdes[0]);
176 (void)close(pdes[1]);
178 #ifndef NO_INTERNAL_LS
179 if (isls) { /* use internal ls */
185 exit(ls_main(sl->sl_cur - 1, sl->sl_str));
189 execv(sl->sl_str[0], sl->sl_str);
192 /* parent; assume fdopen can't fail... */
194 iop = fdopen(pdes[0], ptype);
195 (void)close(pdes[1]);
197 iop = fdopen(pdes[1], ptype);
198 (void)close(pdes[0]);
200 pids[fileno(iop)] = pid;
209 ftpd_pclose(FILE *iop)
213 sigset_t sigset, osigset;
216 * pclose returns -1 if stream is not associated with a
217 * `popened' command, or, if already `pclosed'.
219 if (pids == 0 || pids[fdes = fileno(iop)] == 0)
222 sigemptyset(&sigset);
223 sigaddset(&sigset, SIGINT);
224 sigaddset(&sigset, SIGQUIT);
225 sigaddset(&sigset, SIGHUP);
226 sigprocmask(SIG_BLOCK, &sigset, &osigset);
227 while ((pid = waitpid(pids[fdes], &status, 0)) < 0 && errno == EINTR)
229 sigprocmask(SIG_SETMASK, &osigset, NULL);
233 if (WIFEXITED(status))
234 return (WEXITSTATUS(status));