1 /* popen.c: A "safe" pipe open routine.
3 %%% portions-copyright-cmetz-96
4 Portions of this software are Copyright 1996-1999 by Craig Metz, All Rights
5 Reserved. The Inner Net License Version 2 applies to these portions of
7 You should have received a copy of the license with this software. If
8 you didn't get a copy, you may request one from <license@inner.net>.
10 Portions of this software are Copyright 1995 by Randall Atkinson and Dan
11 McDonald, All Rights Reserved. All Rights under this copyright are assigned
12 to the U.S. Naval Research Laboratory (NRL). The NRL Copyright Notice and
13 License Agreement applies to this software.
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.
28 * Copyright (c) 1988, 1993, 1994
29 * The Regents of the University of California. All rights reserved.
31 * This code is derived from software written by Ken Arnold and
32 * published in UNIX Review, Vol. 6, No. 8.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
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.
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
66 #include <sys/types.h>
70 #endif /* HAVE_SIGNAL_H */
72 #include <sys/signal.h>
73 #endif /* HAVE_SYS_SIGNAL_H */
76 #endif /* HAVE_UNISTD_H */
80 #endif /* HAVE_STDLIB_H */
83 #endif /* HAVE_STRING_H */
87 char **ftpglob
__P((register char *));
88 char **copyblk
__P((char **));
89 VOIDRET blkfree
__P((char **));
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
96 static pid_t child_pid
= -1;
99 extern char **environ
;
101 FILE *ftpd_popen
FUNCTION((program
, type
), char *program AND
char *type
)
105 int argc
, gargc
, pdes
[2];
106 char **pop
, *argv
[100], *gargv
[1000], *vv
[2];
108 if ((*type
!= 'r' && *type
!= 'w') || type
[1])
114 /* break up string into pieces */
115 for (argc
= 0, cp
= program
;; cp
= NULL
)
116 if (!(argv
[argc
++] = strtok(cp
, " \t\n")))
119 /* glob each piece */
121 for (gargc
= argc
= 1; argv
[argc
]; argc
++) {
122 if (!(pop
= (char **) ftpglob(argv
[argc
]))) {
123 /* globbing failed */
126 pop
= (char **) copyblk(vv
);
128 argv
[argc
] = (char *) pop
; /* save to free later */
129 while (*pop
&& gargc
< 1000)
130 gargv
[gargc
++] = *pop
++;
135 switch (child_pid
= fork()) {
145 dup2(pdes
[1], 2); /* stderr, too! */
157 execv(gargv
[0], gargv
);
161 /* parent; assume fdopen can't fail... */
163 iop
= fdopen(pipe_fd
= pdes
[0], type
);
166 iop
= fdopen(pipe_fd
= pdes
[1], type
);
170 pfree
: for (argc
= 1; argv
[argc
] != NULL
; argc
++) {
171 blkfree((char **) argv
[argc
]);
172 free((char *) argv
[argc
]);
177 int ftpd_pclose
FUNCTION((iop
), FILE *iop
)
181 sigset_t omask
, mask
;
184 sigaddset(&mask
, SIGINT
);
185 sigaddset(&mask
, SIGQUIT
);
186 sigaddset(&mask
, SIGHUP
);
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
))
194 sigprocmask(SIG_BLOCK
, &mask
, &omask
);
196 while ((pid
= wait(&status
)) != child_pid
&& (pid
!= -1));
197 sigprocmask(SIG_SETMASK
, &omask
, NULL
);
202 #if defined(WEXITSTATUS) && defined(WIFEXITED)
203 if ((pid
> 0) && WIFEXITED(status
))
204 return WEXITSTATUS(status
);
207 #else /* defined(WEXITSTATUS) && defined(WIFEXITED) */
208 return (pid
== -1 ? -1 : status
.w_status
);
209 #endif /* defined(WEXITSTATUS) && defined(WIFEXITED) */