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