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/
10 #include "un-namespace.h"
11 #include "libc_private.h"
13 +#include <crt_externs.h>
14 +#define environ (*_NSGetEnviron())
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 */
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};
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 */
48 + if (!command) { /* just checking... */
49 + if (access(_PATH_BSHELL, F_OK) == -1) /* if no sh or no access */
55 - if (!command) /* just checking... */
57 + if ((err = posix_spawnattr_init(&attr)) != 0) {
61 + (void)sigemptyset(&defaultsig);
64 + pthread_mutex_lock(&__systemfn_mutex);
65 +#endif /* __DARWIN_UNIX03 */
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);
72 (void)_sigaction(SIGINT, &ign, &intact);
73 + if (intact.sa_handler != SIG_IGN) {
74 + sigaddset(&defaultsig, SIGINT);
75 + flags |= POSIX_SPAWN_SETSIGDEF;
77 (void)_sigaction(SIGQUIT, &ign, &quitact);
78 + if (quitact.sa_handler != SIG_IGN) {
79 + sigaddset(&defaultsig, SIGQUIT);
80 + flags |= POSIX_SPAWN_SETSIGDEF;
82 (void)sigemptyset(&newsigblock);
83 (void)sigaddset(&newsigblock, SIGCHLD);
84 (void)_sigprocmask(SIG_BLOCK, &newsigblock, &oldsigblock);
85 - switch(pid = fork()) {
86 - case -1: /* error */
90 - * Restore original signal dispositions and exec the command.
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);
97 - default: /* parent */
98 + (void)posix_spawnattr_setsigmask(&attr, &oldsigblock);
99 + if (flags & POSIX_SPAWN_SETSIGDEF) {
100 + (void)posix_spawnattr_setsigdefault(&attr, &defaultsig);
102 + (void)posix_spawnattr_setflags(&attr, flags);
104 + err = posix_spawn(&pid, _PATH_BSHELL, NULL, &attr, (char *const *)argv, environ);
105 + (void)posix_spawnattr_destroy(&attr);
109 pid = _wait4(savedpid, &pstat, 0, (struct rusage *)0);
110 } while (pid == -1 && errno == EINTR);
112 + if (pid == -1) pstat = -1;
113 + } else if (err == ENOMEM || err == EAGAIN) { /* as if fork failed */
116 + pstat = W_EXITCODE(127, 0); /* couldn't exec shell */
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);
124 + pthread_mutex_unlock(&__systemfn_mutex);
125 +#endif /* __DARWIN_UNIX03 */
129 __weak_reference(__system, system);