10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 53235Sraf * Common Development and Distribution License (the "License"). 63235Sraf * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 211219Sraf 220Sstevel@tonic-gate /* 235891Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */ 280Sstevel@tonic-gate /* All Rights Reserved */ 290Sstevel@tonic-gate 306812Sraf #pragma ident "%Z%%M% %I% %E% SMI" 316812Sraf 326812Sraf #include "lint.h" 330Sstevel@tonic-gate #include "mtlib.h" 340Sstevel@tonic-gate #include <sys/types.h> 350Sstevel@tonic-gate #include <sys/wait.h> 360Sstevel@tonic-gate #include <signal.h> 370Sstevel@tonic-gate #include <stdlib.h> 380Sstevel@tonic-gate #include <wait.h> 390Sstevel@tonic-gate #include <sys/stat.h> 400Sstevel@tonic-gate #include <unistd.h> 410Sstevel@tonic-gate #include <memory.h> 423235Sraf #include <thread.h> 430Sstevel@tonic-gate #include <pthread.h> 440Sstevel@tonic-gate #include <errno.h> 450Sstevel@tonic-gate #include <synch.h> 460Sstevel@tonic-gate #include <spawn.h> 471219Sraf #include "libc.h" 480Sstevel@tonic-gate 49*6879Sraf extern const char **_environ; 500Sstevel@tonic-gate 510Sstevel@tonic-gate extern int __xpg4; /* defined in _xpg4.c; 0 if not xpg4-compiled program */ 523235Sraf extern const sigset_t maskset; /* all maskable signals */ 530Sstevel@tonic-gate 540Sstevel@tonic-gate static mutex_t sys_lock = DEFAULTMUTEX; /* protects the following */ 550Sstevel@tonic-gate static uint_t sys_count = 0; /* number of threads in system() */ 563235Sraf static struct sigaction sys_ibuf; /* saved SIGINT sigaction */ 573235Sraf static struct sigaction sys_qbuf; /* saved SIGQUIT sigaction */ 583235Sraf static struct sigaction ignore = {0, {SIG_IGN}, {0}}; 593235Sraf 603235Sraf /* 613235Sraf * Things needed by the cancellation cleanup handler. 623235Sraf */ 633235Sraf typedef struct { 643235Sraf sigset_t savemask; /* saved signal mask */ 653235Sraf pid_t pid; /* if nonzero, the child's pid */ 663235Sraf } cleanup_t; 673235Sraf 683235Sraf /* 693235Sraf * Daemon thread whose sole function is to reap an abandoned child. 703235Sraf * Also invoked from pclose() (see port/stdio/popen.c). 713235Sraf */ 723235Sraf void * 733235Sraf reapchild(void *arg) 743235Sraf { 753235Sraf pid_t pid = (pid_t)(uintptr_t)arg; 765891Sraf int cancel_state; 773235Sraf 785891Sraf (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state); 793235Sraf while (waitpid(pid, NULL, 0) == -1) { 803235Sraf if (errno != EINTR) 813235Sraf break; 823235Sraf } 835891Sraf (void) pthread_setcancelstate(cancel_state, NULL); 843235Sraf return (NULL); 853235Sraf } 860Sstevel@tonic-gate 870Sstevel@tonic-gate /* 880Sstevel@tonic-gate * Cancellation cleanup handler. 893235Sraf * If we were cancelled in waitpid(), create a daemon thread to 903235Sraf * reap our abandoned child. No other thread can do this for us. 913235Sraf * It would be better if there were a system call to disinherit 923235Sraf * a child process (give it to init, just as though we exited). 930Sstevel@tonic-gate */ 940Sstevel@tonic-gate static void 950Sstevel@tonic-gate cleanup(void *arg) 960Sstevel@tonic-gate { 973235Sraf cleanup_t *cup = arg; 983235Sraf 993235Sraf if (cup->pid != 0) { /* we were cancelled; abandoning our pid */ 1003235Sraf (void) thr_sigsetmask(SIG_SETMASK, &maskset, NULL); 1013235Sraf (void) thr_create(NULL, 0, 1023235Sraf reapchild, (void *)(uintptr_t)cup->pid, 1033235Sraf THR_DAEMON, NULL); 1043235Sraf } 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate lmutex_lock(&sys_lock); 1070Sstevel@tonic-gate if (--sys_count == 0) { /* leaving system() */ 1080Sstevel@tonic-gate /* 1093235Sraf * There are no remaining threads in system(), so 1103235Sraf * restore the SIGINT and SIGQUIT signal actions. 1110Sstevel@tonic-gate */ 1120Sstevel@tonic-gate (void) sigaction(SIGINT, &sys_ibuf, NULL); 1130Sstevel@tonic-gate (void) sigaction(SIGQUIT, &sys_qbuf, NULL); 1140Sstevel@tonic-gate } 1150Sstevel@tonic-gate lmutex_unlock(&sys_lock); 1163235Sraf 1173235Sraf (void) thr_sigsetmask(SIG_SETMASK, &cup->savemask, NULL); 1180Sstevel@tonic-gate } 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate int 1210Sstevel@tonic-gate system(const char *cmd) 1220Sstevel@tonic-gate { 1233235Sraf cleanup_t cu; 1240Sstevel@tonic-gate pid_t w; 1250Sstevel@tonic-gate int status; 1260Sstevel@tonic-gate int error; 1270Sstevel@tonic-gate sigset_t mask; 1280Sstevel@tonic-gate struct stat64 buf; 1290Sstevel@tonic-gate const char *shpath; 1303235Sraf char *argv[4]; 1310Sstevel@tonic-gate posix_spawnattr_t attr; 1320Sstevel@tonic-gate static const char *sun_path = "/bin/sh"; 1330Sstevel@tonic-gate static const char *xpg4_path = "/usr/xpg4/bin/sh"; 1340Sstevel@tonic-gate static const char *shell = "sh"; 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate shpath = __xpg4? xpg4_path : sun_path; 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate if (cmd == NULL) { 1390Sstevel@tonic-gate if (stat64(shpath, &buf) != 0) { 1400Sstevel@tonic-gate return (0); 1410Sstevel@tonic-gate } else if (getuid() == buf.st_uid) { 1420Sstevel@tonic-gate /* exec for user */ 1430Sstevel@tonic-gate if ((buf.st_mode & 0100) == 0) 1440Sstevel@tonic-gate return (0); 1450Sstevel@tonic-gate } else if (getgid() == buf.st_gid) { 1460Sstevel@tonic-gate /* exec for group */ 1470Sstevel@tonic-gate if ((buf.st_mode & 0010) == 0) 1480Sstevel@tonic-gate return (0); 1490Sstevel@tonic-gate } else if ((buf.st_mode & 0001) == 0) { /* exec for others */ 1500Sstevel@tonic-gate return (0); 1510Sstevel@tonic-gate } 1520Sstevel@tonic-gate return (1); 1530Sstevel@tonic-gate } 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate /* 1560Sstevel@tonic-gate * Initialize the posix_spawn() attributes structure. 1573235Sraf * The setting of POSIX_SPAWN_WAITPID_NP ensures that no 1583235Sraf * wait-for-multiple wait() operation will reap our child 1593235Sraf * and that the child will not be automatically reaped due 1603235Sraf * to the disposition of SIGCHLD being set to be ignored. 1613235Sraf * Only a specific wait for the specific pid will be able 1623235Sraf * to reap the child. Since no other thread knows the pid 1633235Sraf * of our child, this should be safe enough. 1640Sstevel@tonic-gate */ 1653235Sraf error = posix_spawnattr_init(&attr); 1663235Sraf if (error == 0) 1673235Sraf error = posix_spawnattr_setflags(&attr, 1683235Sraf POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF | 1693235Sraf POSIX_SPAWN_NOSIGCHLD_NP | POSIX_SPAWN_WAITPID_NP); 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate /* 1723235Sraf * The POSIX spec for system() requires us to block SIGCHLD, 1733235Sraf * the rationale being that the process's signal handler for 1743235Sraf * SIGCHLD, if any, should not be called when our child exits. 1753235Sraf * This doesn't work for a multithreaded process because some 1763235Sraf * other thread could receive the SIGCHLD. 1773235Sraf * 1783235Sraf * The above setting of POSIX_SPAWN_NOSIGCHLD_NP ensures that no 1793235Sraf * SIGCHLD signal will be posted for our child when it exits, so 1803235Sraf * we don't have to block SIGCHLD to meet the intent of the spec. 1813235Sraf * We block SIGCHLD anyway, just because the spec requires it. 1820Sstevel@tonic-gate */ 1830Sstevel@tonic-gate (void) sigemptyset(&mask); 1840Sstevel@tonic-gate (void) sigaddset(&mask, SIGCHLD); 1853235Sraf (void) thr_sigsetmask(SIG_BLOCK, &mask, &cu.savemask); 1860Sstevel@tonic-gate /* 1870Sstevel@tonic-gate * Tell posix_spawn() to restore the signal mask in the child. 1880Sstevel@tonic-gate */ 1890Sstevel@tonic-gate if (error == 0) 1903235Sraf error = posix_spawnattr_setsigmask(&attr, &cu.savemask); 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate /* 1930Sstevel@tonic-gate * We are required to set the disposition of SIGINT and SIGQUIT 1940Sstevel@tonic-gate * to be ignored for the duration of the system() operation. 1950Sstevel@tonic-gate * 1960Sstevel@tonic-gate * We allow more than one thread to call system() concurrently by 1970Sstevel@tonic-gate * keeping a count of such threads. The signal actions are set 1980Sstevel@tonic-gate * to SIG_IGN when the first thread calls system(). They are 1990Sstevel@tonic-gate * restored in cleanup() when the last thread exits system(). 2000Sstevel@tonic-gate * 2010Sstevel@tonic-gate * However, system() is still MT-unsafe because sigaction() has 2020Sstevel@tonic-gate * a process-wide effect and some other thread may also be 2030Sstevel@tonic-gate * setting the signal actions for SIGINT or SIGQUIT. 2040Sstevel@tonic-gate */ 2050Sstevel@tonic-gate lmutex_lock(&sys_lock); 2060Sstevel@tonic-gate if (sys_count++ == 0) { 2073235Sraf (void) sigaction(SIGINT, &ignore, &sys_ibuf); 2083235Sraf (void) sigaction(SIGQUIT, &ignore, &sys_qbuf); 2090Sstevel@tonic-gate } 2100Sstevel@tonic-gate lmutex_unlock(&sys_lock); 2110Sstevel@tonic-gate 2120Sstevel@tonic-gate /* 2130Sstevel@tonic-gate * If SIGINT and SIGQUIT were not already SIG_IGN, tell 2140Sstevel@tonic-gate * posix_spawn() to make them SIG_DFL in the child, 2150Sstevel@tonic-gate * else leave them as SIG_IGN in the child. 2160Sstevel@tonic-gate */ 2170Sstevel@tonic-gate (void) sigemptyset(&mask); 2180Sstevel@tonic-gate if (sys_ibuf.sa_handler != SIG_IGN) 2190Sstevel@tonic-gate (void) sigaddset(&mask, SIGINT); 2200Sstevel@tonic-gate if (sys_qbuf.sa_handler != SIG_IGN) 2210Sstevel@tonic-gate (void) sigaddset(&mask, SIGQUIT); 2220Sstevel@tonic-gate if (error == 0) 2230Sstevel@tonic-gate error = posix_spawnattr_setsigdefault(&attr, &mask); 2240Sstevel@tonic-gate 2253235Sraf argv[0] = (char *)shell; 2263235Sraf argv[1] = "-c"; 2273235Sraf argv[2] = (char *)cmd; 2283235Sraf argv[3] = NULL; 2290Sstevel@tonic-gate if (error == 0) 2303235Sraf error = posix_spawn(&cu.pid, shpath, NULL, &attr, 231*6879Sraf (char *const *)argv, (char *const *)_environ); 2320Sstevel@tonic-gate 2330Sstevel@tonic-gate (void) posix_spawnattr_destroy(&attr); 2340Sstevel@tonic-gate 2350Sstevel@tonic-gate if (error) { 2360Sstevel@tonic-gate errno = error; 2370Sstevel@tonic-gate status = -1; 2380Sstevel@tonic-gate } else { 2391219Sraf /* 2405891Sraf * system() is a cancellation point and so is waitpid(). 2411219Sraf */ 2423235Sraf pthread_cleanup_push(cleanup, &cu); 2430Sstevel@tonic-gate do { 2445891Sraf w = waitpid(cu.pid, &status, 0); 2450Sstevel@tonic-gate } while (w == -1 && errno == EINTR); 2460Sstevel@tonic-gate pthread_cleanup_pop(0); 2470Sstevel@tonic-gate if (w == -1) 2480Sstevel@tonic-gate status = -1; 2490Sstevel@tonic-gate } 2503235Sraf error = errno; 2513235Sraf cu.pid = 0; 2523235Sraf cleanup(&cu); 2533235Sraf errno = error; 2540Sstevel@tonic-gate 2550Sstevel@tonic-gate return (status); 2560Sstevel@tonic-gate } 257