popen.c revision 1.16 1 /* $NetBSD: popen.c,v 1.16 1998/02/02 02:41:28 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.16 1998/02/02 02:41:28 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 #ifdef __GNUC__
80 (void) &cur;
81 #endif
82
83 if (strchr(type, '+')) {
84 twoway = 1;
85 type = "r+";
86 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pdes) < 0)
87 return (NULL);
88 } else {
89 twoway = 0;
90 if (*type != 'r' && *type != 'w' || type[1] ||
91 (pipe(pdes) < 0)) {
92 errno = EINVAL;
93 return (NULL);
94 }
95 }
96
97 if ((cur = malloc(sizeof(struct pid))) == NULL)
98 return (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 if (pdes[1] != STDOUT_FILENO) {
110 (void)dup2(pdes[1], STDOUT_FILENO);
111 (void)close(pdes[1]);
112 pdes[1] = STDOUT_FILENO;
113 }
114 (void) close(pdes[0]);
115 if (twoway && (pdes[1] != STDIN_FILENO))
116 (void)dup2(pdes[1], STDIN_FILENO);
117 } else {
118 if (pdes[0] != STDIN_FILENO) {
119 (void)dup2(pdes[0], STDIN_FILENO);
120 (void)close(pdes[0]);
121 }
122 (void)close(pdes[1]);
123 }
124
125 /* POSIX.2 B.3.2.2 "popen() shall ensure that any streams
126 from previous popen() calls that remain open in the
127 parent process are closed in the new child process. */
128 for (old = pidlist; old; old = old->next)
129 close(fileno(old->fp));
130
131 execl(_PATH_BSHELL, "sh", "-c", program, NULL);
132 _exit(127);
133 /* NOTREACHED */
134 }
135
136 /* Parent; assume fdopen can't fail. */
137 if (*type == 'r') {
138 iop = fdopen(pdes[0], type);
139 (void)close(pdes[1]);
140 } else {
141 iop = fdopen(pdes[1], type);
142 (void)close(pdes[0]);
143 }
144
145 /* Link into list of file descriptors. */
146 cur->fp = iop;
147 cur->pid = pid;
148 cur->next = pidlist;
149 pidlist = cur;
150
151 return (iop);
152 }
153
154 /*
155 * pclose --
156 * Pclose returns -1 if stream is not associated with a `popened' command,
157 * if already `pclosed', or waitpid returns an error.
158 */
159 int
160 pclose(iop)
161 FILE *iop;
162 {
163 register struct pid *cur, *last;
164 int pstat;
165 pid_t pid;
166
167 /* Find the appropriate file pointer. */
168 for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next)
169 if (cur->fp == iop)
170 break;
171 if (cur == NULL)
172 return (-1);
173
174 (void)fclose(iop);
175
176 do {
177 pid = waitpid(cur->pid, &pstat, 0);
178 } while (pid == -1 && errno == EINTR);
179
180 /* Remove the entry from the linked list. */
181 if (last == NULL)
182 pidlist = cur->next;
183 else
184 last->next = cur->next;
185 free(cur);
186
187 return (pid == -1 ? -1 : pstat);
188 }
189