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 * $Id: popen.c,v 1.6 2005/02/06 06:57:30 cpressey Exp $
40 * @(#)popen.c 8.3 (Berkeley) 5/3/95
42 * A modified version of the standard popen()/pclose() functions
43 * which adds a third function, pgetpid(), which allows the program
44 * which used popen() to obtain the pid of the process on the other
48 #include <sys/param.h>
50 #include <sys/types.h>
64 extern char **environ;
73 aura_popen(const char *fmt, const char *type, ...)
82 volatile enum { READ, WRITE, TWO_WAY } pipedir;
85 * Lite2 introduced two-way popen() pipes using socketpair().
86 * FreeBSD's pipe() is bidirectional, so we use that.
88 if (strchr(type, '+')) {
95 else if (*type == 'w')
104 if ((cur = malloc(sizeof(struct pid))) == NULL) {
110 va_start(args, type);
111 vasprintf(&command, fmt, args);
119 switch (pid = vfork()) {
120 case -1: /* Error. */
128 if (pipedir == READ || pipedir == TWO_WAY) {
130 * The dup2() to STDIN_FILENO is repeated to avoid
131 * writing to pdes[1], which might corrupt the
132 * parent's copy. This isn't good enough in
133 * general, since the _exit() is no return, so
134 * the compiler is free to corrupt all the local
138 if (pdes[1] != STDOUT_FILENO) {
139 dup2(pdes[1], STDOUT_FILENO);
141 if (pipedir == TWO_WAY)
142 dup2(STDOUT_FILENO, STDIN_FILENO);
143 } else if (pipedir == TWO_WAY &&
144 (pdes[1] != STDIN_FILENO)) {
145 dup2(pdes[1], STDIN_FILENO);
148 if (pdes[0] != STDIN_FILENO) {
149 dup2(pdes[0], STDIN_FILENO);
154 for (p = pidlist; p; p = p->next) {
155 close(fileno(p->fp));
157 execve(_PATH_BSHELL, __DECONST(char **, argv), environ);
162 /* Parent; assume fdopen can't fail. */
163 if (pipedir == READ || pipedir == TWO_WAY) {
164 iop = fdopen(pdes[0], type);
167 iop = fdopen(pdes[1], type);
171 /* Link into list of file descriptors. */
183 * Pclose returns -1 if stream is not associated with a `popened' command,
184 * if already `pclosed', or waitpid returns an error.
187 aura_pclose(FILE *iop)
189 struct pid *cur, *last;
193 /* Find the appropriate file pointer. */
194 for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next) {
205 pid = wait4(cur->pid, &pstat, 0, (struct rusage *)0);
206 } while (pid == -1 && errno == EINTR);
208 /* Remove the entry from the linked list. */
212 last->next = cur->next;
215 return (pid == -1 ? -1 : pstat);
219 aura_pgetpid(FILE *iop)
223 /* Find the appropriate file pointer. */
224 for (cur = pidlist; cur; cur = cur->next)
234 * 1 if all went well.
235 * 0 if an error occurred, in which case err is set to:
236 * AURA_PGETS_TIMEOUT: select() timed out.
237 * AURA_PGETS_SELECT_ERR: a select() error occurred.
238 * AURA_PGETS_EOF: end of file condition on pipe.
239 * AURA_PGETS_FGETS_ERR: a fgets() error occurred.
242 aura_pgets(FILE *p, char *buf, size_t len, long msec, int *err)
249 tv.tv_sec = msec / 1000;
250 tv.tv_usec = (msec % 1000) * 1000;
251 tvp = (msec < 0 ? NULL : &tv);
254 FD_SET(fileno(p), &r);
260 *err = AURA_PGETS_EOF;
264 *err = AURA_PGETS_FGETS_ERR;
268 n = select(fileno(p) + 1, &r, NULL, NULL, tvp);
271 *err = AURA_PGETS_TIMEOUT;
274 *err = AURA_PGETS_SELECT_ERR;
277 /* Data came in; read it. */
278 if (fgets(buf, len, p) == NULL) {
280 *err = AURA_PGETS_EOF;
282 *err = AURA_PGETS_FGETS_ERR;