1 /* $NetBSD: popen.c,v 1.35 2009/03/15 07:48:36 lukem Exp $ */
4 * Copyright (c) 1999-2009 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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c) 1988, 1993, 1994
34 * The Regents of the University of California. All rights reserved.
36 * This code is derived from software written by Ken Arnold and
37 * published in UNIX Review, Vol. 6, No. 8.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 #include <sys/cdefs.h>
68 static char sccsid
[] = "@(#)popen.c 8.3 (Berkeley) 4/6/94";
70 __RCSID("$NetBSD: popen.c,v 1.35 2009/03/15 07:48:36 lukem Exp $");
74 #include <sys/types.h>
75 #include <sys/param.h>
84 #include <stringlist.h>
89 #include <krb5/krb5.h>
96 * Special version of popen which avoids call to shell. This ensures no-one
97 * may create a pipe to a hidden program as a side effect of a list or dir
99 * If stderrfd != -1, then send stderr of a read command there,
100 * otherwise close stderr.
105 extern int ls_main(int, char *[]);
108 ftpd_popen(const char *argv
[], const char *ptype
, int stderrfd
)
111 int argc
, pdes
[2], pid
;
118 if ((*ptype
!= 'r' && *ptype
!= 'w') || ptype
[1])
122 if ((fds
= getdtablesize()) <= 0)
124 if ((pids
= (int *)malloc((unsigned int)(fds
* sizeof(int)))) == NULL
)
126 memset(pids
, 0, fds
* sizeof(int));
131 if ((sl
= sl_init()) == NULL
)
134 /* glob each piece */
135 if (sl_add(sl
, ftpd_strdup(argv
[0])) == -1)
137 for (argc
= 1; argv
[argc
]; argc
++) {
139 int flags
= GLOB_BRACE
|GLOB_NOCHECK
|GLOB_TILDE
|GLOB_LIMIT
;
141 memset(&gl
, 0, sizeof(gl
));
142 if (glob(argv
[argc
], flags
, NULL
, &gl
)) {
143 if (sl_add(sl
, ftpd_strdup(argv
[argc
])) == -1) {
148 for (pop
= gl
.gl_pathv
; *pop
; pop
++) {
149 if (sl_add(sl
, ftpd_strdup(*pop
)) == -1) {
157 if (sl_add(sl
, NULL
) == -1)
160 #ifndef NO_INTERNAL_LS
161 isls
= (strcmp(sl
->sl_str
[0], INTERNAL_LS
) == 0);
164 pid
= isls
? fork() : vfork();
167 (void)close(pdes
[0]);
168 (void)close(pdes
[1]);
173 if (pdes
[1] != STDOUT_FILENO
) {
174 dup2(pdes
[1], STDOUT_FILENO
);
175 (void)close(pdes
[1]);
178 (void)close(STDERR_FILENO
);
180 dup2(stderrfd
, STDERR_FILENO
);
181 (void)close(pdes
[0]);
183 if (pdes
[0] != STDIN_FILENO
) {
184 dup2(pdes
[0], STDIN_FILENO
);
185 (void)close(pdes
[0]);
187 (void)close(pdes
[1]);
189 #ifndef NO_INTERNAL_LS
190 if (isls
) { /* use internal ls */
191 optreset
= optind
= optopt
= 1;
193 exit(ls_main(sl
->sl_cur
- 1, sl
->sl_str
));
197 execv(sl
->sl_str
[0], sl
->sl_str
);
200 /* parent; assume fdopen can't fail... */
202 iop
= fdopen(pdes
[0], ptype
);
203 (void)close(pdes
[1]);
205 iop
= fdopen(pdes
[1], ptype
);
206 (void)close(pdes
[0]);
208 pids
[fileno(iop
)] = pid
;
217 ftpd_pclose(FILE *iop
)
221 sigset_t nsigset
, osigset
;
224 * pclose returns -1 if stream is not associated with a
225 * `popened' command, or, if already `pclosed'.
227 if (pids
== 0 || pids
[fdes
= fileno(iop
)] == 0)
230 sigemptyset(&nsigset
);
231 sigaddset(&nsigset
, SIGINT
);
232 sigaddset(&nsigset
, SIGQUIT
);
233 sigaddset(&nsigset
, SIGHUP
);
234 sigprocmask(SIG_BLOCK
, &nsigset
, &osigset
);
235 while ((pid
= waitpid(pids
[fdes
], &status
, 0)) < 0 && errno
== EINTR
)
237 sigprocmask(SIG_SETMASK
, &osigset
, NULL
);
241 if (WIFEXITED(status
))
242 return (WEXITSTATUS(status
));