popen.c revision 1.19 1 /* $NetBSD: popen.c,v 1.19 1998/02/03 18:23:49 perry 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.19 1998/02/03 18:23:49 perry 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_UNIX, 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 return (NULL);
101
102 switch (pid = vfork()) {
103 case -1: /* Error. */
104 (void)close(pdes[0]);
105 (void)close(pdes[1]);
106 free(cur);
107 return (NULL);
108 /* NOTREACHED */
109 case 0: /* Child. */
110 if (*type == 'r') {
111 if (pdes[1] != STDOUT_FILENO) {
112 (void)dup2(pdes[1], STDOUT_FILENO);
113 (void)close(pdes[1]);
114 pdes[1] = STDOUT_FILENO;
115 }
116 (void) close(pdes[0]);
117 if (twoway && (pdes[1] != STDIN_FILENO))
118 (void)dup2(pdes[1], STDIN_FILENO);
119 } else {
120 if (pdes[0] != STDIN_FILENO) {
121 (void)dup2(pdes[0], STDIN_FILENO);
122 (void)close(pdes[0]);
123 }
124 (void)close(pdes[1]);
125 }
126
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));
132
133 execl(_PATH_BSHELL, "sh", "-c", command, NULL);
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)close(pdes[1]);
142 } else {
143 iop = fdopen(pdes[1], type);
144 (void)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 */
161 int
162 pclose(iop)
163 FILE *iop;
164 {
165 struct pid *cur, *last;
166 int pstat;
167 pid_t pid;
168
169 /* Find the appropriate file pointer. */
170 for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next)
171 if (cur->fp == iop)
172 break;
173 if (cur == NULL)
174 return (-1);
175
176 (void)fclose(iop);
177
178 do {
179 pid = waitpid(cur->pid, &pstat, 0);
180 } while (pid == -1 && errno == EINTR);
181
182 /* Remove the entry from the linked list. */
183 if (last == NULL)
184 pidlist = cur->next;
185 else
186 last->next = cur->next;
187 free(cur);
188
189 return (pid == -1 ? -1 : pstat);
190 }
191