2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
6 * This code is derived from software written by Ken Arnold and
7 * published in UNIX Review, Vol. 6, No. 8.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * $FreeBSD: src/lib/libc/gen/popen.c,v 1.14 2000/01/27 23:06:19 jasone Exp $
38 * $DragonFly: src/contrib/bsdinstaller-1.1.6/src/lib/libaura/popen.c,v 1.1.1.1 2008/03/12 22:15:54 dave Exp $
39 * $Id: popen.c,v 1.6 2005/02/06 06:57:30 cpressey Exp $
41 * @(#)popen.c 8.3 (Berkeley) 5/3/95
43 * A modified version of the standard popen()/pclose() functions
44 * which adds a third function, pgetpid(), which allows the program
45 * which used popen() to obtain the pid of the process on the other
49 #include <sys/param.h>
51 #include <sys/types.h>
65 extern char **environ;
74 aura_popen(const char *fmt, const char *type, ...)
83 volatile enum { READ, WRITE, TWO_WAY } pipedir;
86 * Lite2 introduced two-way popen() pipes using socketpair().
87 * FreeBSD's pipe() is bidirectional, so we use that.
89 if (strchr(type, '+')) {
96 else if (*type == 'w')
105 if ((cur = malloc(sizeof(struct pid))) == NULL) {
111 va_start(args, type);
112 vasprintf(&command, fmt, args);
120 switch (pid = vfork()) {
121 case -1: /* Error. */
129 if (pipedir == READ || pipedir == TWO_WAY) {
131 * The dup2() to STDIN_FILENO is repeated to avoid
132 * writing to pdes[1], which might corrupt the
133 * parent's copy. This isn't good enough in
134 * general, since the _exit() is no return, so
135 * the compiler is free to corrupt all the local
139 if (pdes[1] != STDOUT_FILENO) {
140 dup2(pdes[1], STDOUT_FILENO);
142 if (pipedir == TWO_WAY)
143 dup2(STDOUT_FILENO, STDIN_FILENO);
144 } else if (pipedir == TWO_WAY &&
145 (pdes[1] != STDIN_FILENO)) {
146 dup2(pdes[1], STDIN_FILENO);
149 if (pdes[0] != STDIN_FILENO) {
150 dup2(pdes[0], STDIN_FILENO);
155 for (p = pidlist; p; p = p->next) {
156 close(fileno(p->fp));
158 execve(_PATH_BSHELL, __DECONST(char **, argv), environ);
163 /* Parent; assume fdopen can't fail. */
164 if (pipedir == READ || pipedir == TWO_WAY) {
165 iop = fdopen(pdes[0], type);
168 iop = fdopen(pdes[1], type);
172 /* Link into list of file descriptors. */
184 * Pclose returns -1 if stream is not associated with a `popened' command,
185 * if already `pclosed', or waitpid returns an error.
188 aura_pclose(FILE *iop)
190 struct pid *cur, *last;
194 /* Find the appropriate file pointer. */
195 for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next) {
206 pid = wait4(cur->pid, &pstat, 0, (struct rusage *)0);
207 } while (pid == -1 && errno == EINTR);
209 /* Remove the entry from the linked list. */
213 last->next = cur->next;
216 return (pid == -1 ? -1 : pstat);
220 aura_pgetpid(FILE *iop)
222 struct pid *cur, *last;
224 /* Find the appropriate file pointer. */
225 for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next)
235 * 1 if all went well.
236 * 0 if an error occurred, in which case err is set to:
237 * AURA_PGETS_TIMEOUT: select() timed out.
238 * AURA_PGETS_SELECT_ERR: a select() error occurred.
239 * AURA_PGETS_EOF: end of file condition on pipe.
240 * AURA_PGETS_FGETS_ERR: a fgets() error occurred.
243 aura_pgets(FILE *p, char *buf, size_t len, long msec, int *err)
250 tv.tv_sec = msec / 1000;
251 tv.tv_usec = (msec % 1000) * 1000;
252 tvp = (msec < 0 ? NULL : &tv);
255 FD_SET(fileno(p), &r);
261 *err = AURA_PGETS_EOF;
265 *err = AURA_PGETS_FGETS_ERR;
269 n = select(fileno(p) + 1, &r, NULL, NULL, tvp);
272 *err = AURA_PGETS_TIMEOUT;
275 *err = AURA_PGETS_SELECT_ERR;
278 /* Data came in; read it. */
279 if (fgets(buf, len, p) == NULL) {
281 *err = AURA_PGETS_EOF;
283 *err = AURA_PGETS_FGETS_ERR;