]> git.saurik.com Git - apple/libc.git/blame - gen/popen-fbsd.c
Libc-498.1.7.tar.gz
[apple/libc.git] / gen / popen-fbsd.c
CommitLineData
224c7076
A
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
37#if defined(LIBC_SCCS) && !defined(lint)
38static char sccsid[] = "@(#)popen.c 8.3 (Berkeley) 5/3/95";
39#endif /* LIBC_SCCS and not lint */
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD: src/lib/libc/gen/popen.c,v 1.18 2003/01/04 00:15:15 tjr Exp $");
42
43#include "namespace.h"
44#include <sys/param.h>
45#include <sys/wait.h>
46#include <sys/socket.h>
47#include <wchar.h> /* fwide() */
48#include <signal.h>
49#include <errno.h>
50#include <unistd.h>
51#include <stdio.h>
52#include <stdlib.h>
53#include <string.h>
54#include <paths.h>
55#include <pthread.h>
56#include "un-namespace.h"
57#include "libc_private.h"
58
59#include <crt_externs.h>
60#define environ (*_NSGetEnviron())
61
62/* 3516149 - store file descriptor and use that to close to prevent blocking */
63static struct pid {
64 struct pid *next;
65 FILE *fp;
66 int fd;
67 pid_t pid;
68} *pidlist;
69static pthread_mutex_t pidlist_mutex = PTHREAD_MUTEX_INITIALIZER;
70
71#define THREAD_LOCK() if (__isthreaded) _pthread_mutex_lock(&pidlist_mutex)
72#define THREAD_UNLOCK() if (__isthreaded) _pthread_mutex_unlock(&pidlist_mutex)
73
74FILE *
75popen(command, type)
76 const char *command, *type;
77{
78 struct pid *cur;
79 FILE *iop;
80 int pdes[2], pid, twoway;
81 char *argv[4];
82 struct pid *p;
83
84 if (type == NULL) {
85 errno = EINVAL;
86 return (NULL);
87 }
88 if (strcmp(type, "r+") == 0) {
89 twoway = 1;
90 type = "r+";
91 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pdes) < 0)
92 return (NULL);
93 } else {
94 twoway = 0;
95 if ((*type != 'r' && *type != 'w') || type[1]) {
96 errno = EINVAL;
97 return (NULL);
98 }
99 if (pipe(pdes) < 0)
100 return (NULL);
101 }
102
103 if ((cur = malloc(sizeof(struct pid))) == NULL) {
104 (void)_close(pdes[0]);
105 (void)_close(pdes[1]);
106 return (NULL);
107 }
108
109 argv[0] = "sh";
110 argv[1] = "-c";
111 argv[2] = (char *)command;
112 argv[3] = NULL;
113
114 THREAD_LOCK();
115 switch (pid = fork()) {
116 case -1: /* Error. */
117 THREAD_UNLOCK();
118 (void)_close(pdes[0]);
119 (void)_close(pdes[1]);
120 free(cur);
121 return (NULL);
122 /* NOTREACHED */
123 case 0: /* Child. */
124 if (*type == 'r') {
125 /*
126 * The _dup2() to STDIN_FILENO is repeated to avoid
127 * writing to pdes[1], which might corrupt the
128 * parent's copy. This isn't good enough in
129 * general, since the _exit() is no return, so
130 * the compiler is free to corrupt all the local
131 * variables.
132 */
133 (void)_close(pdes[0]);
134 if (pdes[1] != STDOUT_FILENO) {
135 (void)_dup2(pdes[1], STDOUT_FILENO);
136 (void)_close(pdes[1]);
137 if (twoway)
138 (void)_dup2(STDOUT_FILENO, STDIN_FILENO);
139 } else if (twoway && (pdes[1] != STDIN_FILENO))
140 (void)_dup2(pdes[1], STDIN_FILENO);
141 } else {
142 if (pdes[0] != STDIN_FILENO) {
143 (void)_dup2(pdes[0], STDIN_FILENO);
144 (void)_close(pdes[0]);
145 }
146 (void)_close(pdes[1]);
147 }
148 for (p = pidlist; p; p = p->next) {
149 (void)_close(p->fd);
150 }
151 _execve(_PATH_BSHELL, argv, environ);
152 _exit(127);
153 /* NOTREACHED */
154 }
155 THREAD_UNLOCK();
156
157 /* Parent; assume fdopen can't fail. */
158 if (*type == 'r') {
159 iop = fdopen(pdes[0], type);
160 cur->fd = pdes[0];
161 (void)_close(pdes[1]);
162 } else {
163 iop = fdopen(pdes[1], type);
164 cur->fd = pdes[1];
165 (void)_close(pdes[0]);
166 }
167
168 /* Link into list of file descriptors. */
169 cur->fp = iop;
170 cur->pid = pid;
171 THREAD_LOCK();
172 cur->next = pidlist;
173 pidlist = cur;
174 THREAD_UNLOCK();
175 fwide(iop, -1); /* byte stream */
176 return (iop);
177}
178
179/*
180 * pclose --
181 * Pclose returns -1 if stream is not associated with a `popened' command,
182 * if already `pclosed', or waitpid returns an error.
183 */
184int
185pclose(iop)
186 FILE *iop;
187{
188 struct pid *cur, *last;
189 int pstat;
190 pid_t pid;
191
192 /*
193 * Find the appropriate file pointer and remove it from the list.
194 */
195 THREAD_LOCK();
196 for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next)
197 if (cur->fp == iop)
198 break;
199 if (cur == NULL) {
200 THREAD_UNLOCK();
201 return (-1);
202 }
203 if (last == NULL)
204 pidlist = cur->next;
205 else
206 last->next = cur->next;
207 THREAD_UNLOCK();
208
209 (void)fclose(iop);
210
211 do {
212 pid = _wait4(cur->pid, &pstat, 0, (struct rusage *)0);
213 } while (pid == -1 && errno == EINTR);
214
215 free(cur);
216
217 return (pid == -1 ? -1 : pstat);
218}