popen.c revision 1.26 1 /* $NetBSD: popen.c,v 1.26 2003/03/04 19:44:10 nathanw 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.26 2003/03/04 19:44:10 nathanw 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 <assert.h>
54 #include <errno.h>
55 #include <paths.h>
56 #include <signal.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61 #include "reentrant.h"
62
63 #ifdef __weak_alias
64 __weak_alias(popen,_popen)
65 __weak_alias(pclose,_pclose)
66 #endif
67
68 #ifdef _REENTRANT
69 extern rwlock_t __environ_lock;
70 #endif
71
72 static struct pid {
73 struct pid *next;
74 FILE *fp;
75 pid_t pid;
76 } *pidlist;
77
78 FILE *
79 popen(command, type)
80 const char *command, *type;
81 {
82 struct pid *cur, *old;
83 FILE *iop;
84 int pdes[2], pid, twoway, serrno;
85
86 _DIAGASSERT(command != NULL);
87 _DIAGASSERT(type != NULL);
88
89 #ifdef __GNUC__
90 /* This outrageous construct just to shut up a GCC warning. */
91 (void) &cur; (void) &twoway; (void) &type;
92 #endif
93
94 if (strchr(type, '+')) {
95 twoway = 1;
96 type = "r+";
97 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, pdes) < 0)
98 return (NULL);
99 } else {
100 twoway = 0;
101 if ((*type != 'r' && *type != 'w') || type[1] ||
102 (pipe(pdes) < 0)) {
103 errno = EINVAL;
104 return (NULL);
105 }
106 }
107
108 if ((cur = malloc(sizeof(struct pid))) == NULL) {
109 (void)close(pdes[0]);
110 (void)close(pdes[1]);
111 errno = ENOMEM;
112 return (NULL);
113 }
114
115 rwlock_rdlock(&__environ_lock);
116 switch (pid = vfork()) {
117 case -1: /* Error. */
118 serrno = errno;
119 rwlock_unlock(&__environ_lock);
120 free(cur);
121 (void)close(pdes[0]);
122 (void)close(pdes[1]);
123 errno = serrno;
124 return (NULL);
125 /* NOTREACHED */
126 case 0: /* Child. */
127 /* POSIX.2 B.3.2.2 "popen() shall ensure that any streams
128 from previous popen() calls that remain open in the
129 parent process are closed in the new child process. */
130 for (old = pidlist; old; old = old->next)
131 close(fileno(old->fp)); /* don't allow a flush */
132
133 if (*type == 'r') {
134 (void)close(pdes[0]);
135 if (pdes[1] != STDOUT_FILENO) {
136 (void)dup2(pdes[1], STDOUT_FILENO);
137 (void)close(pdes[1]);
138 }
139 if (twoway)
140 (void)dup2(STDOUT_FILENO, STDIN_FILENO);
141 } else {
142 (void)close(pdes[1]);
143 if (pdes[0] != STDIN_FILENO) {
144 (void)dup2(pdes[0], STDIN_FILENO);
145 (void)close(pdes[0]);
146 }
147 }
148
149 execl(_PATH_BSHELL, "sh", "-c", command, NULL);
150 _exit(127);
151 /* NOTREACHED */
152 }
153 rwlock_unlock(&__environ_lock);
154
155 /* Parent; assume fdopen can't fail. */
156 if (*type == 'r') {
157 iop = fdopen(pdes[0], type);
158 (void)close(pdes[1]);
159 } else {
160 iop = fdopen(pdes[1], type);
161 (void)close(pdes[0]);
162 }
163
164 /* Link into list of file descriptors. */
165 cur->fp = iop;
166 cur->pid = pid;
167 cur->next = pidlist;
168 pidlist = cur;
169
170 return (iop);
171 }
172
173 /*
174 * pclose --
175 * Pclose returns -1 if stream is not associated with a `popened' command,
176 * if already `pclosed', or waitpid returns an error.
177 */
178 int
179 pclose(iop)
180 FILE *iop;
181 {
182 struct pid *cur, *last;
183 int pstat;
184 pid_t pid;
185
186 _DIAGASSERT(iop != NULL);
187
188 /* Find the appropriate file pointer. */
189 for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next)
190 if (cur->fp == iop)
191 break;
192 if (cur == NULL)
193 return (-1);
194
195 (void)fclose(iop);
196
197 do {
198 pid = waitpid(cur->pid, &pstat, 0);
199 } while (pid == -1 && errno == EINTR);
200
201 /* Remove the entry from the linked list. */
202 if (last == NULL)
203 pidlist = cur->next;
204 else
205 last->next = cur->next;
206 free(cur);
207
208 return (pid == -1 ? -1 : pstat);
209 }
210