]> git.saurik.com Git - apple/libc.git/blob - gen/FreeBSD/popen.c.patch
Libc-391.5.21.tar.gz
[apple/libc.git] / gen / FreeBSD / popen.c.patch
1 --- popen.c.orig 2003-05-20 15:21:02.000000000 -0700
2 +++ popen.c 2005-09-17 16:08:55.000000000 -0700
3 @@ -43,6 +43,7 @@
4 #include "namespace.h"
5 #include <sys/param.h>
6 #include <sys/wait.h>
7 +#include <sys/socket.h>
8
9 #include <signal.h>
10 #include <errno.h>
11 @@ -55,11 +56,14 @@
12 #include "un-namespace.h"
13 #include "libc_private.h"
14
15 -extern char **environ;
16 +#include <crt_externs.h>
17 +#define environ (*_NSGetEnviron())
18
19 +/* 3516149 - store file descriptor and use that to close to prevent blocking */
20 static struct pid {
21 struct pid *next;
22 FILE *fp;
23 + int fd;
24 pid_t pid;
25 } *pidlist;
26 static pthread_mutex_t pidlist_mutex = PTHREAD_MUTEX_INITIALIZER;
27 @@ -77,20 +81,18 @@
28 char *argv[4];
29 struct pid *p;
30
31 - /*
32 - * Lite2 introduced two-way popen() pipes using _socketpair().
33 - * FreeBSD's pipe() is bidirectional, so we use that.
34 - */
35 if (strchr(type, '+')) {
36 twoway = 1;
37 type = "r+";
38 + if (socketpair(AF_UNIX, SOCK_STREAM, 0, pdes) < 0)
39 + return (NULL);
40 } else {
41 twoway = 0;
42 if ((*type != 'r' && *type != 'w') || type[1])
43 return (NULL);
44 + if (pipe(pdes) < 0)
45 + return (NULL);
46 }
47 - if (pipe(pdes) < 0)
48 - return (NULL);
49
50 if ((cur = malloc(sizeof(struct pid))) == NULL) {
51 (void)_close(pdes[0]);
52 @@ -104,7 +106,7 @@
53 argv[3] = NULL;
54
55 THREAD_LOCK();
56 - switch (pid = vfork()) {
57 + switch (pid = fork()) {
58 case -1: /* Error. */
59 THREAD_UNLOCK();
60 (void)_close(pdes[0]);
61 @@ -138,7 +140,7 @@
62 (void)_close(pdes[1]);
63 }
64 for (p = pidlist; p; p = p->next) {
65 - (void)_close(fileno(p->fp));
66 + (void)_close(p->fd);
67 }
68 _execve(_PATH_BSHELL, argv, environ);
69 _exit(127);
70 @@ -149,9 +151,11 @@
71 /* Parent; assume fdopen can't fail. */
72 if (*type == 'r') {
73 iop = fdopen(pdes[0], type);
74 + cur->fd = pdes[0];
75 (void)_close(pdes[1]);
76 } else {
77 iop = fdopen(pdes[1], type);
78 + cur->fd = pdes[1];
79 (void)_close(pdes[0]);
80 }
81