Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | /* |
2 | * Copyright (c) 1988 The Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * This code is derived from software written by Ken Arnold and | |
6 | * published in UNIX Review, Vol. 6, No. 8. | |
7 | * | |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * 1. Redistributions of source code must retain the above copyright | |
12 | * notice, this list of conditions and the following disclaimer. | |
13 | * 2. Redistributions in binary form must reproduce the above copyright | |
14 | * notice, this list of conditions and the following disclaimer in the | |
15 | * documentation and/or other materials provided with the distribution. | |
16 | * 3. All advertising materials mentioning features or use of this software | |
17 | * must display the following acknowledgement: | |
18 | * This product includes software developed by the University of | |
19 | * California, Berkeley and its contributors. | |
20 | * 4. Neither the name of the University nor the names of its contributors | |
21 | * may be used to endorse or promote products derived from this software | |
22 | * without specific prior written permission. | |
23 | * | |
24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
34 | * SUCH DAMAGE. | |
35 | * | |
36 | */ | |
37 | ||
38 | #ifndef lint | |
39 | static char sccsid[] = "@(#)popen.c 5.9 (Berkeley) 2/25/91"; | |
40 | #endif /* not lint */ | |
41 | ||
42 | #include <sys/types.h> | |
43 | #include <sys/wait.h> | |
44 | #include <signal.h> | |
45 | #include <unistd.h> | |
46 | #include <stdio.h> | |
47 | #include <stdlib.h> | |
48 | #include <string.h> | |
49 | ||
50 | /* | |
51 | * Special version of popen which avoids call to shell. This insures noone | |
52 | * may create a pipe to a hidden program as a side effect of a list or dir | |
53 | * command. | |
54 | */ | |
55 | static int *pids; | |
56 | static int fds; | |
57 | ||
58 | FILE * | |
59 | ftpd_popen(program, type) | |
60 | char *program, *type; | |
61 | { | |
62 | register char *cp; | |
63 | FILE *iop; | |
64 | int argc, gargc, pdes[2], pid; | |
65 | char **pop, *argv[100], *gargv[1000], *vv[2]; | |
66 | extern char **ftpglob(), **copyblk(); | |
67 | ||
68 | if (*type != 'r' && *type != 'w' || type[1]) | |
69 | return(NULL); | |
70 | ||
71 | if (!pids) { | |
72 | if ((fds = getdtablesize()) <= 0) | |
73 | return(NULL); | |
74 | if ((pids = (int *)malloc((u_int)(fds * sizeof(int)))) == NULL) | |
75 | return(NULL); | |
76 | bzero((char *)pids, fds * sizeof(int)); | |
77 | } | |
78 | if (pipe(pdes) < 0) | |
79 | return(NULL); | |
80 | ||
81 | /* break up string into pieces */ | |
82 | for (argc = 0, cp = program;; cp = NULL) | |
83 | if (!(argv[argc++] = strtok(cp, " \t\n"))) | |
84 | break; | |
85 | ||
86 | /* glob each piece */ | |
87 | gargv[0] = argv[0]; | |
88 | for (gargc = argc = 1; argv[argc]; argc++) { | |
89 | if (!(pop = ftpglob(argv[argc]))) { /* globbing failed */ | |
90 | vv[0] = argv[argc]; | |
91 | vv[1] = NULL; | |
92 | pop = copyblk(vv); | |
93 | } | |
94 | argv[argc] = (char *)pop; /* save to free later */ | |
95 | while (*pop && gargc < 1000) | |
96 | gargv[gargc++] = *pop++; | |
97 | } | |
98 | gargv[gargc] = NULL; | |
99 | ||
100 | iop = NULL; | |
101 | switch(pid = vfork()) { | |
102 | case -1: /* error */ | |
103 | (void)close(pdes[0]); | |
104 | (void)close(pdes[1]); | |
105 | goto pfree; | |
106 | /* NOTREACHED */ | |
107 | case 0: /* child */ | |
108 | if (*type == 'r') { | |
109 | if (pdes[1] != 1) { | |
110 | dup2(pdes[1], 1); | |
111 | dup2(pdes[1], 2); /* stderr, too! */ | |
112 | (void)close(pdes[1]); | |
113 | } | |
114 | (void)close(pdes[0]); | |
115 | } else { | |
116 | if (pdes[0] != 0) { | |
117 | dup2(pdes[0], 0); | |
118 | (void)close(pdes[0]); | |
119 | } | |
120 | (void)close(pdes[1]); | |
121 | } | |
122 | execv(gargv[0], gargv); | |
123 | _exit(1); | |
124 | } | |
125 | /* parent; assume fdopen can't fail... */ | |
126 | if (*type == 'r') { | |
127 | iop = fdopen(pdes[0], type); | |
128 | (void)close(pdes[1]); | |
129 | } else { | |
130 | iop = fdopen(pdes[1], type); | |
131 | (void)close(pdes[0]); | |
132 | } | |
133 | pids[fileno(iop)] = pid; | |
134 | ||
135 | pfree: for (argc = 1; argv[argc] != NULL; argc++) { | |
136 | blkfree((char **)argv[argc]); | |
137 | free((char *)argv[argc]); | |
138 | } | |
139 | return(iop); | |
140 | } | |
141 | ||
142 | ftpd_pclose(iop) | |
143 | FILE *iop; | |
144 | { | |
145 | register int fdes; | |
146 | int omask; | |
147 | union wait stat_loc; | |
148 | int pid; | |
149 | ||
150 | /* | |
151 | * pclose returns -1 if stream is not associated with a | |
152 | * `popened' command, or, if already `pclosed'. | |
153 | */ | |
154 | if (pids == 0 || pids[fdes = fileno(iop)] == 0) | |
155 | return(-1); | |
156 | (void)fclose(iop); | |
157 | omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP)); | |
158 | while ((pid = wait((int *)&stat_loc)) != pids[fdes] && pid != -1); | |
159 | (void)sigsetmask(omask); | |
160 | pids[fdes] = 0; | |
161 | return(pid == -1 ? -1 : stat_loc.w_status); | |
162 | } |