xref: /netbsd-src/lib/libc/gen/popen.c (revision dc306354b0b29af51801a7632f1e95265a68cd81)
1 /*	$NetBSD: popen.c,v 1.22 1998/07/18 05:04:35 lukem Exp $	*/
2 
3 /*
4  * Copyright (c) 1988, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software written by Ken Arnold and
8  * published in UNIX Review, Vol. 6, No. 8.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 #if defined(LIBC_SCCS) && !defined(lint)
41 #if 0
42 static char sccsid[] = "@(#)popen.c	8.3 (Berkeley) 5/3/95";
43 #else
44 __RCSID("$NetBSD: popen.c,v 1.22 1998/07/18 05:04:35 lukem Exp $");
45 #endif
46 #endif /* LIBC_SCCS and not lint */
47 
48 #include "namespace.h"
49 #include <sys/param.h>
50 #include <sys/wait.h>
51 #include <sys/socket.h>
52 
53 #include <signal.h>
54 #include <errno.h>
55 #include <unistd.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <paths.h>
60 
61 #ifdef __weak_alias
62 __weak_alias(popen,_popen);
63 __weak_alias(pclose,_pclose);
64 #endif
65 
66 static struct pid {
67 	struct pid *next;
68 	FILE *fp;
69 	pid_t pid;
70 } *pidlist;
71 
72 FILE *
73 popen(command, type)
74 	const char *command, *type;
75 {
76 	struct pid *cur, *old;
77 	FILE *iop;
78 	int pdes[2], pid, twoway;
79 
80 #ifdef __GNUC__
81 	/* This outrageous construct just to shut up a GCC warning. */
82 	(void) &cur; (void) &twoway; (void) &type;
83 #endif
84 
85 	if (strchr(type, '+')) {
86 		twoway = 1;
87 		type = "r+";
88 		if (socketpair(AF_LOCAL, SOCK_STREAM, 0, pdes) < 0)
89 			return (NULL);
90 	} else  {
91 		twoway = 0;
92 		if ((*type != 'r' && *type != 'w') || type[1] ||
93 		    (pipe(pdes) < 0)) {
94 			errno = EINVAL;
95 			return (NULL);
96 		}
97 	}
98 
99 	if ((cur = malloc(sizeof(struct pid))) == NULL) {
100 		(void)close(pdes[0]);
101 		(void)close(pdes[1]);
102 		return (NULL);
103 	}
104 
105 	switch (pid = vfork()) {
106 	case -1:			/* Error. */
107 		free(cur);
108 		(void)close(pdes[0]);
109 		(void)close(pdes[1]);
110 		return (NULL);
111 		/* NOTREACHED */
112 	case 0:				/* Child. */
113 		/* POSIX.2 B.3.2.2 "popen() shall ensure that any streams
114 		   from previous popen() calls that remain open in the
115 		   parent process are closed in the new child process. */
116 		for (old = pidlist; old; old = old->next)
117 			close(fileno(old->fp)); /* don't allow a flush */
118 
119 		if (*type == 'r') {
120 			(void)close(pdes[0]);
121 			if (pdes[1] != STDOUT_FILENO) {
122 				(void)dup2(pdes[1], STDOUT_FILENO);
123 				(void)close(pdes[1]);
124 			}
125 			if (twoway)
126 				(void)dup2(STDOUT_FILENO, STDIN_FILENO);
127 		} else {
128 			(void)close(pdes[1]);
129 			if (pdes[0] != STDIN_FILENO) {
130 				(void)dup2(pdes[0], STDIN_FILENO);
131 				(void)close(pdes[0]);
132 			}
133 		}
134 
135 		execl(_PATH_BSHELL, "sh", "-c", command, NULL);
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  */
163 int
164 pclose(iop)
165 	FILE *iop;
166 {
167 	struct pid *cur, *last;
168 	int pstat;
169 	pid_t pid;
170 
171 	/* Find the appropriate file pointer. */
172 	for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next)
173 		if (cur->fp == iop)
174 			break;
175 	if (cur == NULL)
176 		return (-1);
177 
178 	(void)fclose(iop);
179 
180 	do {
181 		pid = waitpid(cur->pid, &pstat, 0);
182 	} while (pid == -1 && errno == EINTR);
183 
184 	/* Remove the entry from the linked list. */
185 	if (last == NULL)
186 		pidlist = cur->next;
187 	else
188 		last->next = cur->next;
189 	free(cur);
190 
191 	return (pid == -1 ? -1 : pstat);
192 }
193