]> git.saurik.com Git - apple/libc.git/blob - stdlib/FreeBSD/system.c.patch
6691e7a42bae2fb766089ba5e9e7523a9c1b59dc
[apple/libc.git] / stdlib / FreeBSD / system.c.patch
1 --- system.c.bsdnew 2009-11-13 14:11:52.000000000 -0800
2 +++ system.c 2009-11-13 14:11:52.000000000 -0800
3 @@ -40,23 +40,61 @@ __FBSDID("$FreeBSD: src/lib/libc/stdlib/
4 #include <stdlib.h>
5 #include <stddef.h>
6 #include <unistd.h>
7 +#include <spawn.h>
8 #include <paths.h>
9 #include <errno.h>
10 #include "un-namespace.h"
11 #include "libc_private.h"
12
13 +#include <crt_externs.h>
14 +#define environ (*_NSGetEnviron())
15 +
16 +#if __DARWIN_UNIX03
17 +#include <pthread.h>
18 +
19 +static pthread_mutex_t __systemfn_mutex = PTHREAD_MUTEX_INITIALIZER;
20 +extern int __unix_conforming;
21 +#ifdef VARIANT_CANCELABLE
22 +extern void _pthread_testcancel(pthread_t thread, int isconforming);
23 +#endif /* VARIANT_CANCELABLE */
24 +#endif /* __DARWIN_UNIX03 */
25 +
26 int
27 __system(command)
28 const char *command;
29 {
30 pid_t pid, savedpid;
31 - int pstat;
32 + int pstat, err;
33 struct sigaction ign, intact, quitact;
34 - sigset_t newsigblock, oldsigblock;
35 + sigset_t newsigblock, oldsigblock, defaultsig;
36 + posix_spawnattr_t attr;
37 + short flags = POSIX_SPAWN_SETSIGMASK;
38 + const char *argv[] = {"sh", "-c", command, NULL};
39 +
40 +#if __DARWIN_UNIX03
41 + if (__unix_conforming == 0)
42 + __unix_conforming = 1;
43 +#ifdef VARIANT_CANCELABLE
44 + _pthread_testcancel(pthread_self(), 1);
45 +#endif /* VARIANT_CANCELABLE */
46 +#endif /* __DARWIN_UNIX03 */
47 +
48 + if (!command) { /* just checking... */
49 + if (access(_PATH_BSHELL, F_OK) == -1) /* if no sh or no access */
50 + return(0);
51 + else
52 + return(1);
53 + }
54
55 - if (!command) /* just checking... */
56 - return(1);
57 + if ((err = posix_spawnattr_init(&attr)) != 0) {
58 + errno = err;
59 + return -1;
60 + }
61 + (void)sigemptyset(&defaultsig);
62
63 +#if __DARWIN_UNIX03
64 + pthread_mutex_lock(&__systemfn_mutex);
65 +#endif /* __DARWIN_UNIX03 */
66 /*
67 * Ignore SIGINT and SIGQUIT, block SIGCHLD. Remember to save
68 * existing signal dispositions.
69 @@ -65,33 +103,45 @@ __system(command)
70 (void)sigemptyset(&ign.sa_mask);
71 ign.sa_flags = 0;
72 (void)_sigaction(SIGINT, &ign, &intact);
73 + if (intact.sa_handler != SIG_IGN) {
74 + sigaddset(&defaultsig, SIGINT);
75 + flags |= POSIX_SPAWN_SETSIGDEF;
76 + }
77 (void)_sigaction(SIGQUIT, &ign, &quitact);
78 + if (quitact.sa_handler != SIG_IGN) {
79 + sigaddset(&defaultsig, SIGQUIT);
80 + flags |= POSIX_SPAWN_SETSIGDEF;
81 + }
82 (void)sigemptyset(&newsigblock);
83 (void)sigaddset(&newsigblock, SIGCHLD);
84 (void)_sigprocmask(SIG_BLOCK, &newsigblock, &oldsigblock);
85 - switch(pid = fork()) {
86 - case -1: /* error */
87 - break;
88 - case 0: /* child */
89 - /*
90 - * Restore original signal dispositions and exec the command.
91 - */
92 - (void)_sigaction(SIGINT, &intact, NULL);
93 - (void)_sigaction(SIGQUIT, &quitact, NULL);
94 - (void)_sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
95 - execl(_PATH_BSHELL, "sh", "-c", command, (char *)NULL);
96 - _exit(127);
97 - default: /* parent */
98 + (void)posix_spawnattr_setsigmask(&attr, &oldsigblock);
99 + if (flags & POSIX_SPAWN_SETSIGDEF) {
100 + (void)posix_spawnattr_setsigdefault(&attr, &defaultsig);
101 + }
102 + (void)posix_spawnattr_setflags(&attr, flags);
103 +
104 + err = posix_spawn(&pid, _PATH_BSHELL, NULL, &attr, (char *const *)argv, environ);
105 + (void)posix_spawnattr_destroy(&attr);
106 + if (err == 0) {
107 savedpid = pid;
108 do {
109 pid = _wait4(savedpid, &pstat, 0, (struct rusage *)0);
110 } while (pid == -1 && errno == EINTR);
111 - break;
112 + if (pid == -1) pstat = -1;
113 + } else if (err == ENOMEM || err == EAGAIN) { /* as if fork failed */
114 + pstat = -1;
115 + } else {
116 + pstat = W_EXITCODE(127, 0); /* couldn't exec shell */
117 }
118 +
119 (void)_sigaction(SIGINT, &intact, NULL);
120 (void)_sigaction(SIGQUIT, &quitact, NULL);
121 (void)_sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
122 - return(pid == -1 ? -1 : pstat);
123 +#if __DARWIN_UNIX03
124 + pthread_mutex_unlock(&__systemfn_mutex);
125 +#endif /* __DARWIN_UNIX03 */
126 + return(pstat);
127 }
128
129 __weak_reference(__system, system);