17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5657b1f3dSraf * Common Development and Distribution License (the "License"). 6657b1f3dSraf * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21e8031f0aSraf 227c478bd9Sstevel@tonic-gate /* 23a574db85Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 307257d1b4Sraf #include "lint.h" 317c478bd9Sstevel@tonic-gate #include "mtlib.h" 327c478bd9Sstevel@tonic-gate #include <sys/types.h> 337c478bd9Sstevel@tonic-gate #include <sys/wait.h> 347c478bd9Sstevel@tonic-gate #include <signal.h> 357c478bd9Sstevel@tonic-gate #include <stdlib.h> 367c478bd9Sstevel@tonic-gate #include <wait.h> 377c478bd9Sstevel@tonic-gate #include <sys/stat.h> 387c478bd9Sstevel@tonic-gate #include <unistd.h> 397c478bd9Sstevel@tonic-gate #include <memory.h> 40657b1f3dSraf #include <thread.h> 417c478bd9Sstevel@tonic-gate #include <pthread.h> 427c478bd9Sstevel@tonic-gate #include <errno.h> 437c478bd9Sstevel@tonic-gate #include <synch.h> 447c478bd9Sstevel@tonic-gate #include <spawn.h> 45*6a5408e6SRichard Lowe #include <paths.h> 46e8031f0aSraf #include "libc.h" 477c478bd9Sstevel@tonic-gate 4859f081edSraf extern const char **_environ; 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate extern int __xpg4; /* defined in _xpg4.c; 0 if not xpg4-compiled program */ 51657b1f3dSraf extern const sigset_t maskset; /* all maskable signals */ 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate static mutex_t sys_lock = DEFAULTMUTEX; /* protects the following */ 547c478bd9Sstevel@tonic-gate static uint_t sys_count = 0; /* number of threads in system() */ 55657b1f3dSraf static struct sigaction sys_ibuf; /* saved SIGINT sigaction */ 56657b1f3dSraf static struct sigaction sys_qbuf; /* saved SIGQUIT sigaction */ 57657b1f3dSraf static struct sigaction ignore = {0, {SIG_IGN}, {0}}; 58657b1f3dSraf 59657b1f3dSraf /* 60657b1f3dSraf * Things needed by the cancellation cleanup handler. 61657b1f3dSraf */ 62657b1f3dSraf typedef struct { 63657b1f3dSraf sigset_t savemask; /* saved signal mask */ 64657b1f3dSraf pid_t pid; /* if nonzero, the child's pid */ 65657b1f3dSraf } cleanup_t; 66657b1f3dSraf 67657b1f3dSraf /* 68657b1f3dSraf * Daemon thread whose sole function is to reap an abandoned child. 69657b1f3dSraf * Also invoked from pclose() (see port/stdio/popen.c). 70657b1f3dSraf */ 71657b1f3dSraf void * 72657b1f3dSraf reapchild(void *arg) 73657b1f3dSraf { 74657b1f3dSraf pid_t pid = (pid_t)(uintptr_t)arg; 75a574db85Sraf int cancel_state; 76657b1f3dSraf 77a574db85Sraf (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state); 78657b1f3dSraf while (waitpid(pid, NULL, 0) == -1) { 79657b1f3dSraf if (errno != EINTR) 80657b1f3dSraf break; 81657b1f3dSraf } 82a574db85Sraf (void) pthread_setcancelstate(cancel_state, NULL); 83657b1f3dSraf return (NULL); 84657b1f3dSraf } 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate /* 877c478bd9Sstevel@tonic-gate * Cancellation cleanup handler. 88657b1f3dSraf * If we were cancelled in waitpid(), create a daemon thread to 89657b1f3dSraf * reap our abandoned child. No other thread can do this for us. 90657b1f3dSraf * It would be better if there were a system call to disinherit 91657b1f3dSraf * a child process (give it to init, just as though we exited). 927c478bd9Sstevel@tonic-gate */ 937c478bd9Sstevel@tonic-gate static void 947c478bd9Sstevel@tonic-gate cleanup(void *arg) 957c478bd9Sstevel@tonic-gate { 96657b1f3dSraf cleanup_t *cup = arg; 97657b1f3dSraf 98657b1f3dSraf if (cup->pid != 0) { /* we were cancelled; abandoning our pid */ 99657b1f3dSraf (void) thr_sigsetmask(SIG_SETMASK, &maskset, NULL); 100657b1f3dSraf (void) thr_create(NULL, 0, 101657b1f3dSraf reapchild, (void *)(uintptr_t)cup->pid, 102657b1f3dSraf THR_DAEMON, NULL); 103657b1f3dSraf } 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate lmutex_lock(&sys_lock); 1067c478bd9Sstevel@tonic-gate if (--sys_count == 0) { /* leaving system() */ 1077c478bd9Sstevel@tonic-gate /* 108657b1f3dSraf * There are no remaining threads in system(), so 109657b1f3dSraf * restore the SIGINT and SIGQUIT signal actions. 1107c478bd9Sstevel@tonic-gate */ 1117c478bd9Sstevel@tonic-gate (void) sigaction(SIGINT, &sys_ibuf, NULL); 1127c478bd9Sstevel@tonic-gate (void) sigaction(SIGQUIT, &sys_qbuf, NULL); 1137c478bd9Sstevel@tonic-gate } 1147c478bd9Sstevel@tonic-gate lmutex_unlock(&sys_lock); 115657b1f3dSraf 116657b1f3dSraf (void) thr_sigsetmask(SIG_SETMASK, &cup->savemask, NULL); 1177c478bd9Sstevel@tonic-gate } 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate int 1207c478bd9Sstevel@tonic-gate system(const char *cmd) 1217c478bd9Sstevel@tonic-gate { 122657b1f3dSraf cleanup_t cu; 1237c478bd9Sstevel@tonic-gate pid_t w; 1247c478bd9Sstevel@tonic-gate int status; 1257c478bd9Sstevel@tonic-gate int error; 1267c478bd9Sstevel@tonic-gate sigset_t mask; 1277c478bd9Sstevel@tonic-gate struct stat64 buf; 128*6a5408e6SRichard Lowe const char *shpath = _PATH_BSHELL; 129657b1f3dSraf char *argv[4]; 1307c478bd9Sstevel@tonic-gate posix_spawnattr_t attr; 1317c478bd9Sstevel@tonic-gate static const char *shell = "sh"; 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate if (cmd == NULL) { 1347c478bd9Sstevel@tonic-gate if (stat64(shpath, &buf) != 0) { 1357c478bd9Sstevel@tonic-gate return (0); 1367c478bd9Sstevel@tonic-gate } else if (getuid() == buf.st_uid) { 1377c478bd9Sstevel@tonic-gate /* exec for user */ 1387c478bd9Sstevel@tonic-gate if ((buf.st_mode & 0100) == 0) 1397c478bd9Sstevel@tonic-gate return (0); 1407c478bd9Sstevel@tonic-gate } else if (getgid() == buf.st_gid) { 1417c478bd9Sstevel@tonic-gate /* exec for group */ 1427c478bd9Sstevel@tonic-gate if ((buf.st_mode & 0010) == 0) 1437c478bd9Sstevel@tonic-gate return (0); 1447c478bd9Sstevel@tonic-gate } else if ((buf.st_mode & 0001) == 0) { /* exec for others */ 1457c478bd9Sstevel@tonic-gate return (0); 1467c478bd9Sstevel@tonic-gate } 1477c478bd9Sstevel@tonic-gate return (1); 1487c478bd9Sstevel@tonic-gate } 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate /* 1517c478bd9Sstevel@tonic-gate * Initialize the posix_spawn() attributes structure. 152f9f6ed06SRoger A. Faulkner * 153657b1f3dSraf * The setting of POSIX_SPAWN_WAITPID_NP ensures that no 154657b1f3dSraf * wait-for-multiple wait() operation will reap our child 155657b1f3dSraf * and that the child will not be automatically reaped due 156657b1f3dSraf * to the disposition of SIGCHLD being set to be ignored. 157657b1f3dSraf * Only a specific wait for the specific pid will be able 158657b1f3dSraf * to reap the child. Since no other thread knows the pid 159657b1f3dSraf * of our child, this should be safe enough. 160f9f6ed06SRoger A. Faulkner * 161f9f6ed06SRoger A. Faulkner * The POSIX_SPAWN_NOEXECERR_NP flag tells posix_spawn() not 162f9f6ed06SRoger A. Faulkner * to fail if the shell cannot be executed, but rather cause 163f9f6ed06SRoger A. Faulkner * a child to be created that simply performs _exit(127). 164f9f6ed06SRoger A. Faulkner * This is in order to satisfy the Posix requirement on system(): 165f9f6ed06SRoger A. Faulkner * The system function shall behave as if a child process were 166f9f6ed06SRoger A. Faulkner * created using fork(), and the child process invoked the sh 167f9f6ed06SRoger A. Faulkner * utility using execl(). If some error prevents the command 168f9f6ed06SRoger A. Faulkner * language interpreter from executing after the child process 169f9f6ed06SRoger A. Faulkner * is created, the return value from system() shall be as if 170f9f6ed06SRoger A. Faulkner * the command language interpreter had terminated using 171f9f6ed06SRoger A. Faulkner * exit(127) or _exit(127). 1727c478bd9Sstevel@tonic-gate */ 173657b1f3dSraf error = posix_spawnattr_init(&attr); 174657b1f3dSraf if (error == 0) 1757c478bd9Sstevel@tonic-gate error = posix_spawnattr_setflags(&attr, 176657b1f3dSraf POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF | 177f9f6ed06SRoger A. Faulkner POSIX_SPAWN_NOSIGCHLD_NP | POSIX_SPAWN_WAITPID_NP | 178f9f6ed06SRoger A. Faulkner POSIX_SPAWN_NOEXECERR_NP); 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate /* 181657b1f3dSraf * The POSIX spec for system() requires us to block SIGCHLD, 182657b1f3dSraf * the rationale being that the process's signal handler for 183657b1f3dSraf * SIGCHLD, if any, should not be called when our child exits. 184657b1f3dSraf * This doesn't work for a multithreaded process because some 185657b1f3dSraf * other thread could receive the SIGCHLD. 186657b1f3dSraf * 187657b1f3dSraf * The above setting of POSIX_SPAWN_NOSIGCHLD_NP ensures that no 188657b1f3dSraf * SIGCHLD signal will be posted for our child when it exits, so 189657b1f3dSraf * we don't have to block SIGCHLD to meet the intent of the spec. 190657b1f3dSraf * We block SIGCHLD anyway, just because the spec requires it. 1917c478bd9Sstevel@tonic-gate */ 1927c478bd9Sstevel@tonic-gate (void) sigemptyset(&mask); 1937c478bd9Sstevel@tonic-gate (void) sigaddset(&mask, SIGCHLD); 194657b1f3dSraf (void) thr_sigsetmask(SIG_BLOCK, &mask, &cu.savemask); 1957c478bd9Sstevel@tonic-gate /* 1967c478bd9Sstevel@tonic-gate * Tell posix_spawn() to restore the signal mask in the child. 1977c478bd9Sstevel@tonic-gate */ 1987c478bd9Sstevel@tonic-gate if (error == 0) 199657b1f3dSraf error = posix_spawnattr_setsigmask(&attr, &cu.savemask); 2007c478bd9Sstevel@tonic-gate 2017c478bd9Sstevel@tonic-gate /* 2027c478bd9Sstevel@tonic-gate * We are required to set the disposition of SIGINT and SIGQUIT 2037c478bd9Sstevel@tonic-gate * to be ignored for the duration of the system() operation. 2047c478bd9Sstevel@tonic-gate * 2057c478bd9Sstevel@tonic-gate * We allow more than one thread to call system() concurrently by 2067c478bd9Sstevel@tonic-gate * keeping a count of such threads. The signal actions are set 2077c478bd9Sstevel@tonic-gate * to SIG_IGN when the first thread calls system(). They are 2087c478bd9Sstevel@tonic-gate * restored in cleanup() when the last thread exits system(). 2097c478bd9Sstevel@tonic-gate * 2107c478bd9Sstevel@tonic-gate * However, system() is still MT-unsafe because sigaction() has 2117c478bd9Sstevel@tonic-gate * a process-wide effect and some other thread may also be 2127c478bd9Sstevel@tonic-gate * setting the signal actions for SIGINT or SIGQUIT. 2137c478bd9Sstevel@tonic-gate */ 2147c478bd9Sstevel@tonic-gate lmutex_lock(&sys_lock); 2157c478bd9Sstevel@tonic-gate if (sys_count++ == 0) { 216657b1f3dSraf (void) sigaction(SIGINT, &ignore, &sys_ibuf); 217657b1f3dSraf (void) sigaction(SIGQUIT, &ignore, &sys_qbuf); 2187c478bd9Sstevel@tonic-gate } 2197c478bd9Sstevel@tonic-gate lmutex_unlock(&sys_lock); 2207c478bd9Sstevel@tonic-gate 2217c478bd9Sstevel@tonic-gate /* 2227c478bd9Sstevel@tonic-gate * If SIGINT and SIGQUIT were not already SIG_IGN, tell 2237c478bd9Sstevel@tonic-gate * posix_spawn() to make them SIG_DFL in the child, 2247c478bd9Sstevel@tonic-gate * else leave them as SIG_IGN in the child. 2257c478bd9Sstevel@tonic-gate */ 2267c478bd9Sstevel@tonic-gate (void) sigemptyset(&mask); 2277c478bd9Sstevel@tonic-gate if (sys_ibuf.sa_handler != SIG_IGN) 2287c478bd9Sstevel@tonic-gate (void) sigaddset(&mask, SIGINT); 2297c478bd9Sstevel@tonic-gate if (sys_qbuf.sa_handler != SIG_IGN) 2307c478bd9Sstevel@tonic-gate (void) sigaddset(&mask, SIGQUIT); 2317c478bd9Sstevel@tonic-gate if (error == 0) 2327c478bd9Sstevel@tonic-gate error = posix_spawnattr_setsigdefault(&attr, &mask); 2337c478bd9Sstevel@tonic-gate 234657b1f3dSraf argv[0] = (char *)shell; 235657b1f3dSraf argv[1] = "-c"; 236657b1f3dSraf argv[2] = (char *)cmd; 237657b1f3dSraf argv[3] = NULL; 2387c478bd9Sstevel@tonic-gate if (error == 0) 239657b1f3dSraf error = posix_spawn(&cu.pid, shpath, NULL, &attr, 24059f081edSraf (char *const *)argv, (char *const *)_environ); 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate (void) posix_spawnattr_destroy(&attr); 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gate if (error) { 2457c478bd9Sstevel@tonic-gate errno = error; 2467c478bd9Sstevel@tonic-gate status = -1; 2477c478bd9Sstevel@tonic-gate } else { 248e8031f0aSraf /* 249a574db85Sraf * system() is a cancellation point and so is waitpid(). 250e8031f0aSraf */ 251657b1f3dSraf pthread_cleanup_push(cleanup, &cu); 2527c478bd9Sstevel@tonic-gate do { 253a574db85Sraf w = waitpid(cu.pid, &status, 0); 2547c478bd9Sstevel@tonic-gate } while (w == -1 && errno == EINTR); 2557c478bd9Sstevel@tonic-gate pthread_cleanup_pop(0); 2567c478bd9Sstevel@tonic-gate if (w == -1) 2577c478bd9Sstevel@tonic-gate status = -1; 2587c478bd9Sstevel@tonic-gate } 259657b1f3dSraf error = errno; 260657b1f3dSraf cu.pid = 0; 261657b1f3dSraf cleanup(&cu); 262657b1f3dSraf errno = error; 2637c478bd9Sstevel@tonic-gate 2647c478bd9Sstevel@tonic-gate return (status); 2657c478bd9Sstevel@tonic-gate } 266