]> git.saurik.com Git - apple/libc.git/blame - gen/FreeBSD/popen.c
Libc-1272.250.1.tar.gz
[apple/libc.git] / gen / FreeBSD / popen.c
CommitLineData
e9ce8d39
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.
e9ce8d39
A
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
ad3c9f2a
A
33#ifdef VARIANT_DARWINEXTSN
34#define _DARWIN_UNLIMITED_STREAMS
35#endif /* VARIANT_DARWINEXTSN */
36
9385eb3d
A
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>
1f2f436a 41__FBSDID("$FreeBSD: src/lib/libc/gen/popen.c,v 1.21 2009/05/27 19:28:04 ed Exp $");
9385eb3d
A
42
43#include "namespace.h"
e9ce8d39 44#include <sys/param.h>
1f2f436a 45#include <sys/queue.h>
e9ce8d39 46#include <sys/wait.h>
ad3c9f2a
A
47#include <sys/socket.h>
48#include <wchar.h> /* fwide() */
e9ce8d39
A
49#include <signal.h>
50#include <errno.h>
51#include <unistd.h>
52#include <stdio.h>
53#include <stdlib.h>
54#include <string.h>
55#include <paths.h>
9385eb3d 56#include <pthread.h>
ad3c9f2a 57#include <spawn.h>
9385eb3d
A
58#include "un-namespace.h"
59#include "libc_private.h"
3b2a1fe8 60
ad3c9f2a
A
61#include <crt_externs.h>
62#define environ (*_NSGetEnviron())
63
64/* Our queue.h doesn't have SLIST_REMOVE_AFTER in it yet
65 * <rdar://problem/7431558> API: Add SLIST_REMOVE_AFTER to sys/queue.h (from FreeBSD)
66 */
67#ifndef SLIST_REMOVE_AFTER
68#define SLIST_REMOVE_AFTER(elm, field) do { \
69 SLIST_NEXT(elm, field) = \
70 SLIST_NEXT(SLIST_NEXT(elm, field), field); \
71} while (0)
72#endif
e9ce8d39 73
ad3c9f2a 74/* 3516149 - store file descriptor and use that to close to prevent blocking */
1f2f436a
A
75struct pid {
76 SLIST_ENTRY(pid) next;
e9ce8d39 77 FILE *fp;
ad3c9f2a 78 int fd;
e9ce8d39 79 pid_t pid;
1f2f436a 80};
ad3c9f2a
A
81#define pidlist __popen_pidlist
82#define pidlist_mutex __popen_pidlist_mutex
83#ifndef BUILDING_VARIANT
84__private_extern__ SLIST_HEAD(, pid) pidlist = SLIST_HEAD_INITIALIZER(pidlist);
85__private_extern__ pthread_mutex_t pidlist_mutex = PTHREAD_MUTEX_INITIALIZER;
86#else /* BUILDING_VARIANT */
87extern SLIST_HEAD(, pid) pidlist;
88extern pthread_mutex_t pidlist_mutex;
89#endif /* !BUILDING_VARIANT */
9385eb3d
A
90
91#define THREAD_LOCK() if (__isthreaded) _pthread_mutex_lock(&pidlist_mutex)
92#define THREAD_UNLOCK() if (__isthreaded) _pthread_mutex_unlock(&pidlist_mutex)
93
e9ce8d39
A
94FILE *
95popen(command, type)
96 const char *command, *type;
97{
98 struct pid *cur;
99 FILE *iop;
ad3c9f2a 100 int pdes[2], pid, twoway, other;
3b2a1fe8
A
101 char *argv[4];
102 struct pid *p;
ad3c9f2a
A
103 posix_spawn_file_actions_t file_actions;
104 int err;
e9ce8d39 105
ad3c9f2a
A
106 if (type == NULL) {
107 errno = EINVAL;
108 return (NULL);
109 }
110 if (strcmp(type, "r+") == 0) {
e9ce8d39
A
111 twoway = 1;
112 type = "r+";
ad3c9f2a
A
113 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pdes) < 0)
114 return (NULL);
e9ce8d39
A
115 } else {
116 twoway = 0;
ad3c9f2a
A
117 if ((*type != 'r' && *type != 'w') || type[1]) {
118 errno = EINVAL;
119 return (NULL);
120 }
121 if (pipe(pdes) < 0)
e9ce8d39
A
122 return (NULL);
123 }
124
ad3c9f2a
A
125 /* fdopen can now fail */
126 if (*type == 'r') {
127 iop = fdopen(pdes[0], type);
128 other = pdes[1];
129 } else {
130 iop = fdopen(pdes[1], type);
131 other = pdes[0];
132 }
133 if (iop == NULL) {
9385eb3d
A
134 (void)_close(pdes[0]);
135 (void)_close(pdes[1]);
e9ce8d39 136 return (NULL);
3b2a1fe8
A
137 }
138
ad3c9f2a
A
139 if ((cur = malloc(sizeof(struct pid))) == NULL) {
140 (void)fclose(iop);
141 (void)_close(other);
142 return (NULL);
143 }
144
145 if ((err = posix_spawn_file_actions_init(&file_actions)) != 0) {
146 (void)fclose(iop);
147 (void)_close(other);
148 free(cur);
149 errno = err;
150 return (NULL);
151 }
152 if (*type == 'r') {
153 /*
154 * The dup2() to STDIN_FILENO is repeated to avoid
155 * writing to pdes[1], which might corrupt the
156 * parent's copy. This isn't good enough in
157 * general, since the _exit() is no return, so
158 * the compiler is free to corrupt all the local
159 * variables.
160 */
161 (void)posix_spawn_file_actions_addclose(&file_actions, pdes[0]);
162 if (pdes[1] != STDOUT_FILENO) {
163 (void)posix_spawn_file_actions_adddup2(&file_actions, pdes[1], STDOUT_FILENO);
164 (void)posix_spawn_file_actions_addclose(&file_actions, pdes[1]);
165 if (twoway)
166 (void)posix_spawn_file_actions_adddup2(&file_actions, STDOUT_FILENO, STDIN_FILENO);
167 } else if (twoway && (pdes[1] != STDIN_FILENO))
168 (void)posix_spawn_file_actions_adddup2(&file_actions, pdes[1], STDIN_FILENO);
169 } else {
170 if (pdes[0] != STDIN_FILENO) {
171 (void)posix_spawn_file_actions_adddup2(&file_actions, pdes[0], STDIN_FILENO);
172 (void)posix_spawn_file_actions_addclose(&file_actions, pdes[0]);
173 }
174 (void)posix_spawn_file_actions_addclose(&file_actions, pdes[1]);
175 }
176 SLIST_FOREACH(p, &pidlist, next)
177 (void)posix_spawn_file_actions_addclose(&file_actions, p->fd);
178
3b2a1fe8
A
179 argv[0] = "sh";
180 argv[1] = "-c";
181 argv[2] = (char *)command;
182 argv[3] = NULL;
e9ce8d39 183
ad3c9f2a
A
184 err = posix_spawn(&pid, _PATH_BSHELL, &file_actions, NULL, argv, environ);
185 posix_spawn_file_actions_destroy(&file_actions);
186
187 if (err == ENOMEM || err == EAGAIN) { /* as if fork failed */
188 (void)fclose(iop);
189 (void)_close(other);
3b2a1fe8 190 free(cur);
ad3c9f2a 191 errno = err;
e9ce8d39 192 return (NULL);
ad3c9f2a
A
193 } else if (err != 0) { /* couldn't exec the shell */
194 pid = -1;
e9ce8d39
A
195 }
196
e9ce8d39 197 if (*type == 'r') {
ad3c9f2a 198 cur->fd = pdes[0];
9385eb3d 199 (void)_close(pdes[1]);
e9ce8d39 200 } else {
ad3c9f2a 201 cur->fd = pdes[1];
9385eb3d 202 (void)_close(pdes[0]);
e9ce8d39
A
203 }
204
205 /* Link into list of file descriptors. */
206 cur->fp = iop;
9385eb3d
A
207 cur->pid = pid;
208 THREAD_LOCK();
1f2f436a 209 SLIST_INSERT_HEAD(&pidlist, cur, next);
9385eb3d 210 THREAD_UNLOCK();
ad3c9f2a 211 fwide(iop, -1); /* byte stream */
e9ce8d39
A
212 return (iop);
213}
214
ad3c9f2a 215#ifndef BUILDING_VARIANT
e9ce8d39
A
216/*
217 * pclose --
218 * Pclose returns -1 if stream is not associated with a `popened' command,
219 * if already `pclosed', or waitpid returns an error.
220 */
221int
222pclose(iop)
223 FILE *iop;
224{
1f2f436a 225 struct pid *cur, *last = NULL;
e9ce8d39
A
226 int pstat;
227 pid_t pid;
228
9385eb3d
A
229 /*
230 * Find the appropriate file pointer and remove it from the list.
231 */
232 THREAD_LOCK();
1f2f436a 233 SLIST_FOREACH(cur, &pidlist, next) {
e9ce8d39
A
234 if (cur->fp == iop)
235 break;
1f2f436a
A
236 last = cur;
237 }
9385eb3d
A
238 if (cur == NULL) {
239 THREAD_UNLOCK();
e9ce8d39 240 return (-1);
9385eb3d
A
241 }
242 if (last == NULL)
1f2f436a 243 SLIST_REMOVE_HEAD(&pidlist, next);
9385eb3d 244 else
1f2f436a 245 SLIST_REMOVE_AFTER(last, next);
9385eb3d 246 THREAD_UNLOCK();
e9ce8d39
A
247
248 (void)fclose(iop);
249
ad3c9f2a
A
250 if (cur->pid < 0) {
251 free(cur);
252 return W_EXITCODE(127, 0);
253 }
e9ce8d39 254 do {
9385eb3d 255 pid = _wait4(cur->pid, &pstat, 0, (struct rusage *)0);
e9ce8d39
A
256 } while (pid == -1 && errno == EINTR);
257
e9ce8d39 258 free(cur);
9385eb3d 259
e9ce8d39
A
260 return (pid == -1 ? -1 : pstat);
261}
ad3c9f2a 262#endif /* !BUILDING_VARIANT */