Merge branch 'vendor/MDOCML'
[dragonfly.git] / contrib / opie / popen.c
CommitLineData
984263bc
MD
1/* popen.c: A "safe" pipe open routine.
2
3%%% portions-copyright-cmetz-96
4Portions of this software are Copyright 1996-1999 by Craig Metz, All Rights
5Reserved. The Inner Net License Version 2 applies to these portions of
6the software.
7You should have received a copy of the license with this software. If
8you didn't get a copy, you may request one from <license@inner.net>.
9
10Portions of this software are Copyright 1995 by Randall Atkinson and Dan
11McDonald, All Rights Reserved. All Rights under this copyright are assigned
12to the U.S. Naval Research Laboratory (NRL). The NRL Copyright Notice and
13License Agreement applies to this software.
14
15 History:
16
17 Modified by cmetz for OPIE 2.31. Merged in some 4.4BSD-Lite fixes.
18 Modified by cmetz for OPIE 2.2. Use FUNCTION declaration et al.
19 Removed useless string. ifdef around some headers.
20 Modified at NRL for OPIE 2.1. Optimized for only one pipe at a time.
21 Added minimal version of sigprocmask(). Moved some pid_t
22 dancing to the config headers.
23 Modified at NRL for OPIE 2.0.
24 Originally from BSD.
25
26*/
27/*
28 * Copyright (c) 1988, 1993, 1994
29 * The Regents of the University of California. All rights reserved.
30 *
31 * This code is derived from software written by Ken Arnold and
32 * published in UNIX Review, Vol. 6, No. 8.
33 *
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
36 * are met:
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
61 *
62 */
63
64#include "opie_cfg.h"
65
66#include <sys/types.h>
67#include <sys/wait.h>
68#if HAVE_SIGNAL_H
69#include <signal.h>
70#endif /* HAVE_SIGNAL_H */
71#if HAVE_SYS_SIGNAL_H
72#include <sys/signal.h>
73#endif /* HAVE_SYS_SIGNAL_H */
74#if HAVE_UNISTD_H
75#include <unistd.h>
76#endif /* HAVE_UNISTD_H */
77#include <stdio.h>
78#if HAVE_STDLIB_H
79#include <stdlib.h>
80#endif /* HAVE_STDLIB_H */
81#if HAVE_STRING_H
82#include <string.h>
83#endif /* HAVE_STRING_H */
84
85#include "opie.h"
86
87char **ftpglob __P((register char *));
88char **copyblk __P((char **));
89VOIDRET blkfree __P((char **));
90
91/*
92 * Special version of popen which avoids call to shell. This ensures noone
93 * may create a pipe to a hidden program as a side effect of a list or dir
94 * command.
95 */
96static pid_t child_pid = -1;
97static int pipe_fd;
98
99extern char **environ;
100
101FILE *ftpd_popen FUNCTION((program, type), char *program AND char *type)
102{
103 char *cp;
104 FILE *iop;
105 int argc, gargc, pdes[2];
106 char **pop, *argv[100], *gargv[1000], *vv[2];
107
108 if ((*type != 'r' && *type != 'w') || type[1])
109 return (NULL);
110
111 if (pipe(pdes) < 0)
112 return (NULL);
113
114 /* break up string into pieces */
115 for (argc = 0, cp = program;; cp = NULL)
116 if (!(argv[argc++] = strtok(cp, " \t\n")))
117 break;
118
119 /* glob each piece */
120 gargv[0] = argv[0];
121 for (gargc = argc = 1; argv[argc]; argc++) {
122 if (!(pop = (char **) ftpglob(argv[argc]))) {
123 /* globbing failed */
124 vv[0] = argv[argc];
125 vv[1] = NULL;
126 pop = (char **) copyblk(vv);
127 }
128 argv[argc] = (char *) pop; /* save to free later */
129 while (*pop && gargc < 1000)
130 gargv[gargc++] = *pop++;
131 }
132 gargv[gargc] = NULL;
133
134 iop = NULL;
135 switch (child_pid = fork()) {
136 case -1: /* error */
137 close(pdes[0]);
138 close(pdes[1]);
139 goto pfree;
140 /* NOTREACHED */
141 case 0: /* child */
142 if (*type == 'r') {
143 if (pdes[1] != 1) {
144 dup2(pdes[1], 1);
145 dup2(pdes[1], 2); /* stderr, too! */
146 close(pdes[1]);
147 }
148 close(pdes[0]);
149 } else {
150 if (pdes[0] != 0) {
151 dup2(pdes[0], 0);
152 close(pdes[0]);
153 }
154 close(pdes[1]);
155 }
156 environ = NULL;
157 execv(gargv[0], gargv);
158 _exit(1);
159 }
160
161 /* parent; assume fdopen can't fail... */
162 if (*type == 'r') {
163 iop = fdopen(pipe_fd = pdes[0], type);
164 close(pdes[1]);
165 } else {
166 iop = fdopen(pipe_fd = pdes[1], type);
167 close(pdes[0]);
168 }
169
170pfree: for (argc = 1; argv[argc] != NULL; argc++) {
171 blkfree((char **) argv[argc]);
172 free((char *) argv[argc]);
173 }
174 return (iop);
175}
176
177int ftpd_pclose FUNCTION((iop), FILE *iop)
178{
179 int status;
180 pid_t pid;
181 sigset_t omask, mask;
182
183 sigemptyset(&mask);
184 sigaddset(&mask, SIGINT);
185 sigaddset(&mask, SIGQUIT);
186 sigaddset(&mask, SIGHUP);
187
188 /* pclose returns -1 if stream is not associated with a `popened' command,
189 or, if already `pclosed'. */
190 if ((child_pid < 0) || (fileno(iop) != pipe_fd))
191 return (-1);
192
193 fclose(iop);
194 sigprocmask(SIG_BLOCK, &mask, &omask);
195
196 while ((pid = wait(&status)) != child_pid && (pid != -1));
197 sigprocmask(SIG_SETMASK, &omask, NULL);
198
199 child_pid = -1;
200 pipe_fd = -1;
201
202#if defined(WEXITSTATUS) && defined(WIFEXITED)
203 if ((pid > 0) && WIFEXITED(status))
204 return WEXITSTATUS(status);
205
206 return -1;
207#else /* defined(WEXITSTATUS) && defined(WIFEXITED) */
208 return (pid == -1 ? -1 : status.w_status);
209#endif /* defined(WEXITSTATUS) && defined(WIFEXITED) */
210}