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