1 /* $NetBSD: popen.c,v 1.37 2010/03/20 18:23:30 christos 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.37 2010/03/20 18:23:30 christos 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 || gl
.gl_pathv
== NULL
) {
144 if (sl_add(sl
, ftpd_strdup(argv
[argc
])) == -1) {
149 for (pop
= gl
.gl_pathv
; *pop
; pop
++) {
150 if (sl_add(sl
, ftpd_strdup(*pop
)) == -1) {
158 if (sl_add(sl
, NULL
) == -1)
161 #ifndef NO_INTERNAL_LS
162 isls
= (strcmp(sl
->sl_str
[0], INTERNAL_LS
) == 0);
165 pid
= isls
? fork() : vfork();
168 (void)close(pdes
[0]);
169 (void)close(pdes
[1]);
174 if (pdes
[1] != STDOUT_FILENO
) {
175 dup2(pdes
[1], STDOUT_FILENO
);
176 (void)close(pdes
[1]);
179 (void)close(STDERR_FILENO
);
181 dup2(stderrfd
, STDERR_FILENO
);
182 (void)close(pdes
[0]);
184 if (pdes
[0] != STDIN_FILENO
) {
185 dup2(pdes
[0], STDIN_FILENO
);
186 (void)close(pdes
[0]);
188 (void)close(pdes
[1]);
190 #ifndef NO_INTERNAL_LS
191 if (isls
) { /* use internal ls */
192 optreset
= optind
= optopt
= 1;
194 exit(ls_main(sl
->sl_cur
- 1, sl
->sl_str
));
198 execv(sl
->sl_str
[0], sl
->sl_str
);
201 /* parent; assume fdopen can't fail... */
203 iop
= fdopen(pdes
[0], ptype
);
204 (void)close(pdes
[1]);
206 iop
= fdopen(pdes
[1], ptype
);
207 (void)close(pdes
[0]);
209 pids
[fileno(iop
)] = pid
;
218 ftpd_pclose(FILE *iop
)
222 sigset_t nsigset
, osigset
;
225 * pclose returns -1 if stream is not associated with a
226 * `popened' command, or, if already `pclosed'.
228 if (pids
== 0 || pids
[fdes
= fileno(iop
)] == 0)
231 sigemptyset(&nsigset
);
232 sigaddset(&nsigset
, SIGINT
);
233 sigaddset(&nsigset
, SIGQUIT
);
234 sigaddset(&nsigset
, SIGHUP
);
235 sigprocmask(SIG_BLOCK
, &nsigset
, &osigset
);
236 while ((pid
= waitpid(pids
[fdes
], &status
, 0)) < 0 && errno
== EINTR
)
238 sigprocmask(SIG_SETMASK
, &osigset
, NULL
);
242 if (WIFEXITED(status
))
243 return (WEXITSTATUS(status
));