popen.c revision 90041
11590Srgrimes/*
287213Smarkm * Copyright (c) 1988, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
4265420Simp *
5263227Sjmmv * This code is derived from software written by Ken Arnold and
61590Srgrimes * published in UNIX Review, Vol. 6, No. 8.
71590Srgrimes *
8263227Sjmmv * Redistribution and use in source and binary forms, with or without
9263227Sjmmv * modification, are permitted provided that the following conditions
10263227Sjmmv * are met:
11263227Sjmmv * 1. Redistributions of source code must retain the above copyright
121590Srgrimes *    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#if defined(LIBC_SCCS) && !defined(lint)
38static char sccsid[] = "@(#)popen.c	8.3 (Berkeley) 5/3/95";
39#endif /* LIBC_SCCS and not lint */
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD: head/lib/libc/gen/popen.c 90041 2002-02-01 01:08:48Z obrien $");
42
43#include "namespace.h"
44#include <sys/param.h>
45#include <sys/wait.h>
46
47#include <signal.h>
48#include <errno.h>
49#include <unistd.h>
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
53#include <paths.h>
54#include "un-namespace.h"
55
56extern char **environ;
57
58static struct pid {
59	struct pid *next;
60	FILE *fp;
61	pid_t pid;
62} *pidlist;
63
64FILE *
65popen(command, type)
66	const char *command, *type;
67{
68	struct pid *cur;
69	FILE *iop;
70	int pdes[2], pid, twoway;
71	char *argv[4];
72	struct pid *p;
73
74	/*
75	 * Lite2 introduced two-way popen() pipes using _socketpair().
76	 * FreeBSD's pipe() is bidirectional, so we use that.
77	 */
78	if (strchr(type, '+')) {
79		twoway = 1;
80		type = "r+";
81	} else  {
82		twoway = 0;
83		if ((*type != 'r' && *type != 'w') || type[1])
84			return (NULL);
85	}
86	if (pipe(pdes) < 0)
87		return (NULL);
88
89	if ((cur = malloc(sizeof(struct pid))) == NULL) {
90		(void)_close(pdes[0]);
91		(void)_close(pdes[1]);
92		return (NULL);
93	}
94
95	argv[0] = "sh";
96	argv[1] = "-c";
97	argv[2] = (char *)command;
98	argv[3] = NULL;
99
100	switch (pid = vfork()) {
101	case -1:			/* Error. */
102		(void)_close(pdes[0]);
103		(void)_close(pdes[1]);
104		free(cur);
105		return (NULL);
106		/* NOTREACHED */
107	case 0:				/* Child. */
108		if (*type == 'r') {
109			/*
110			 * The _dup2() to STDIN_FILENO is repeated to avoid
111			 * writing to pdes[1], which might corrupt the
112			 * parent's copy.  This isn't good enough in
113			 * general, since the _exit() is no return, so
114			 * the compiler is free to corrupt all the local
115			 * variables.
116			 */
117			(void)_close(pdes[0]);
118			if (pdes[1] != STDOUT_FILENO) {
119				(void)_dup2(pdes[1], STDOUT_FILENO);
120				(void)_close(pdes[1]);
121				if (twoway)
122					(void)_dup2(STDOUT_FILENO, STDIN_FILENO);
123			} else if (twoway && (pdes[1] != STDIN_FILENO))
124				(void)_dup2(pdes[1], STDIN_FILENO);
125		} else {
126			if (pdes[0] != STDIN_FILENO) {
127				(void)_dup2(pdes[0], STDIN_FILENO);
128				(void)_close(pdes[0]);
129			}
130			(void)_close(pdes[1]);
131		}
132		for (p = pidlist; p; p = p->next) {
133			(void)_close(fileno(p->fp));
134		}
135		_execve(_PATH_BSHELL, argv, environ);
136		_exit(127);
137		/* NOTREACHED */
138	}
139
140	/* Parent; assume fdopen can't fail. */
141	if (*type == 'r') {
142		iop = fdopen(pdes[0], type);
143		(void)_close(pdes[1]);
144	} else {
145		iop = fdopen(pdes[1], type);
146		(void)_close(pdes[0]);
147	}
148
149	/* Link into list of file descriptors. */
150	cur->fp = iop;
151	cur->pid =  pid;
152	cur->next = pidlist;
153	pidlist = cur;
154
155	return (iop);
156}
157
158/*
159 * pclose --
160 *	Pclose returns -1 if stream is not associated with a `popened' command,
161 *	if already `pclosed', or waitpid returns an error.
162 */
163int
164pclose(iop)
165	FILE *iop;
166{
167	struct pid *cur, *last;
168	int omask;
169	int pstat;
170	pid_t pid;
171
172	/* Find the appropriate file pointer. */
173	for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next)
174		if (cur->fp == iop)
175			break;
176	if (cur == NULL)
177		return (-1);
178
179	(void)fclose(iop);
180
181	do {
182		pid = _wait4(cur->pid, &pstat, 0, (struct rusage *)0);
183	} while (pid == -1 && errno == EINTR);
184
185	/* Remove the entry from the linked list. */
186	if (last == NULL)
187		pidlist = cur->next;
188	else
189		last->next = cur->next;
190	free(cur);
191
192	return (pid == -1 ? -1 : pstat);
193}
194