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 #include "lint.h"
310Sstevel@tonic-gate #include "mtlib.h"
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <sys/wait.h>
340Sstevel@tonic-gate #include <signal.h>
350Sstevel@tonic-gate #include <stdlib.h>
360Sstevel@tonic-gate #include <wait.h>
370Sstevel@tonic-gate #include <sys/stat.h>
380Sstevel@tonic-gate #include <unistd.h>
390Sstevel@tonic-gate #include <memory.h>
403235Sraf #include <thread.h>
410Sstevel@tonic-gate #include <pthread.h>
420Sstevel@tonic-gate #include <errno.h>
430Sstevel@tonic-gate #include <synch.h>
440Sstevel@tonic-gate #include <spawn.h>
451219Sraf #include "libc.h"
460Sstevel@tonic-gate
476879Sraf extern const char **_environ;
480Sstevel@tonic-gate
490Sstevel@tonic-gate extern int __xpg4; /* defined in _xpg4.c; 0 if not xpg4-compiled program */
503235Sraf extern const sigset_t maskset; /* all maskable signals */
510Sstevel@tonic-gate
520Sstevel@tonic-gate static mutex_t sys_lock = DEFAULTMUTEX; /* protects the following */
530Sstevel@tonic-gate static uint_t sys_count = 0; /* number of threads in system() */
543235Sraf static struct sigaction sys_ibuf; /* saved SIGINT sigaction */
553235Sraf static struct sigaction sys_qbuf; /* saved SIGQUIT sigaction */
563235Sraf static struct sigaction ignore = {0, {SIG_IGN}, {0}};
573235Sraf
583235Sraf /*
593235Sraf * Things needed by the cancellation cleanup handler.
603235Sraf */
613235Sraf typedef struct {
623235Sraf sigset_t savemask; /* saved signal mask */
633235Sraf pid_t pid; /* if nonzero, the child's pid */
643235Sraf } cleanup_t;
653235Sraf
663235Sraf /*
673235Sraf * Daemon thread whose sole function is to reap an abandoned child.
683235Sraf * Also invoked from pclose() (see port/stdio/popen.c).
693235Sraf */
703235Sraf void *
reapchild(void * arg)713235Sraf reapchild(void *arg)
723235Sraf {
733235Sraf pid_t pid = (pid_t)(uintptr_t)arg;
745891Sraf int cancel_state;
753235Sraf
765891Sraf (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
773235Sraf while (waitpid(pid, NULL, 0) == -1) {
783235Sraf if (errno != EINTR)
793235Sraf break;
803235Sraf }
815891Sraf (void) pthread_setcancelstate(cancel_state, NULL);
823235Sraf return (NULL);
833235Sraf }
840Sstevel@tonic-gate
850Sstevel@tonic-gate /*
860Sstevel@tonic-gate * Cancellation cleanup handler.
873235Sraf * If we were cancelled in waitpid(), create a daemon thread to
883235Sraf * reap our abandoned child. No other thread can do this for us.
893235Sraf * It would be better if there were a system call to disinherit
903235Sraf * a child process (give it to init, just as though we exited).
910Sstevel@tonic-gate */
920Sstevel@tonic-gate static void
cleanup(void * arg)930Sstevel@tonic-gate cleanup(void *arg)
940Sstevel@tonic-gate {
953235Sraf cleanup_t *cup = arg;
963235Sraf
973235Sraf if (cup->pid != 0) { /* we were cancelled; abandoning our pid */
983235Sraf (void) thr_sigsetmask(SIG_SETMASK, &maskset, NULL);
993235Sraf (void) thr_create(NULL, 0,
1003235Sraf reapchild, (void *)(uintptr_t)cup->pid,
1013235Sraf THR_DAEMON, NULL);
1023235Sraf }
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate lmutex_lock(&sys_lock);
1050Sstevel@tonic-gate if (--sys_count == 0) { /* leaving system() */
1060Sstevel@tonic-gate /*
1073235Sraf * There are no remaining threads in system(), so
1083235Sraf * restore the SIGINT and SIGQUIT signal actions.
1090Sstevel@tonic-gate */
1100Sstevel@tonic-gate (void) sigaction(SIGINT, &sys_ibuf, NULL);
1110Sstevel@tonic-gate (void) sigaction(SIGQUIT, &sys_qbuf, NULL);
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate lmutex_unlock(&sys_lock);
1143235Sraf
1153235Sraf (void) thr_sigsetmask(SIG_SETMASK, &cup->savemask, NULL);
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate int
system(const char * cmd)1190Sstevel@tonic-gate system(const char *cmd)
1200Sstevel@tonic-gate {
1213235Sraf cleanup_t cu;
1220Sstevel@tonic-gate pid_t w;
1230Sstevel@tonic-gate int status;
1240Sstevel@tonic-gate int error;
1250Sstevel@tonic-gate sigset_t mask;
1260Sstevel@tonic-gate struct stat64 buf;
1270Sstevel@tonic-gate const char *shpath;
1283235Sraf char *argv[4];
1290Sstevel@tonic-gate posix_spawnattr_t attr;
1300Sstevel@tonic-gate static const char *sun_path = "/bin/sh";
1310Sstevel@tonic-gate static const char *xpg4_path = "/usr/xpg4/bin/sh";
1320Sstevel@tonic-gate static const char *shell = "sh";
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate shpath = __xpg4? xpg4_path : sun_path;
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate if (cmd == NULL) {
1370Sstevel@tonic-gate if (stat64(shpath, &buf) != 0) {
1380Sstevel@tonic-gate return (0);
1390Sstevel@tonic-gate } else if (getuid() == buf.st_uid) {
1400Sstevel@tonic-gate /* exec for user */
1410Sstevel@tonic-gate if ((buf.st_mode & 0100) == 0)
1420Sstevel@tonic-gate return (0);
1430Sstevel@tonic-gate } else if (getgid() == buf.st_gid) {
1440Sstevel@tonic-gate /* exec for group */
1450Sstevel@tonic-gate if ((buf.st_mode & 0010) == 0)
1460Sstevel@tonic-gate return (0);
1470Sstevel@tonic-gate } else if ((buf.st_mode & 0001) == 0) { /* exec for others */
1480Sstevel@tonic-gate return (0);
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate return (1);
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate /*
1540Sstevel@tonic-gate * Initialize the posix_spawn() attributes structure.
155*7635SRoger.Faulkner@Sun.COM *
1563235Sraf * The setting of POSIX_SPAWN_WAITPID_NP ensures that no
1573235Sraf * wait-for-multiple wait() operation will reap our child
1583235Sraf * and that the child will not be automatically reaped due
1593235Sraf * to the disposition of SIGCHLD being set to be ignored.
1603235Sraf * Only a specific wait for the specific pid will be able
1613235Sraf * to reap the child. Since no other thread knows the pid
1623235Sraf * of our child, this should be safe enough.
163*7635SRoger.Faulkner@Sun.COM *
164*7635SRoger.Faulkner@Sun.COM * The POSIX_SPAWN_NOEXECERR_NP flag tells posix_spawn() not
165*7635SRoger.Faulkner@Sun.COM * to fail if the shell cannot be executed, but rather cause
166*7635SRoger.Faulkner@Sun.COM * a child to be created that simply performs _exit(127).
167*7635SRoger.Faulkner@Sun.COM * This is in order to satisfy the Posix requirement on system():
168*7635SRoger.Faulkner@Sun.COM * The system function shall behave as if a child process were
169*7635SRoger.Faulkner@Sun.COM * created using fork(), and the child process invoked the sh
170*7635SRoger.Faulkner@Sun.COM * utility using execl(). If some error prevents the command
171*7635SRoger.Faulkner@Sun.COM * language interpreter from executing after the child process
172*7635SRoger.Faulkner@Sun.COM * is created, the return value from system() shall be as if
173*7635SRoger.Faulkner@Sun.COM * the command language interpreter had terminated using
174*7635SRoger.Faulkner@Sun.COM * exit(127) or _exit(127).
1750Sstevel@tonic-gate */
1763235Sraf error = posix_spawnattr_init(&attr);
1773235Sraf if (error == 0)
1783235Sraf error = posix_spawnattr_setflags(&attr,
1793235Sraf POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF |
180*7635SRoger.Faulkner@Sun.COM POSIX_SPAWN_NOSIGCHLD_NP | POSIX_SPAWN_WAITPID_NP |
181*7635SRoger.Faulkner@Sun.COM POSIX_SPAWN_NOEXECERR_NP);
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate /*
1843235Sraf * The POSIX spec for system() requires us to block SIGCHLD,
1853235Sraf * the rationale being that the process's signal handler for
1863235Sraf * SIGCHLD, if any, should not be called when our child exits.
1873235Sraf * This doesn't work for a multithreaded process because some
1883235Sraf * other thread could receive the SIGCHLD.
1893235Sraf *
1903235Sraf * The above setting of POSIX_SPAWN_NOSIGCHLD_NP ensures that no
1913235Sraf * SIGCHLD signal will be posted for our child when it exits, so
1923235Sraf * we don't have to block SIGCHLD to meet the intent of the spec.
1933235Sraf * We block SIGCHLD anyway, just because the spec requires it.
1940Sstevel@tonic-gate */
1950Sstevel@tonic-gate (void) sigemptyset(&mask);
1960Sstevel@tonic-gate (void) sigaddset(&mask, SIGCHLD);
1973235Sraf (void) thr_sigsetmask(SIG_BLOCK, &mask, &cu.savemask);
1980Sstevel@tonic-gate /*
1990Sstevel@tonic-gate * Tell posix_spawn() to restore the signal mask in the child.
2000Sstevel@tonic-gate */
2010Sstevel@tonic-gate if (error == 0)
2023235Sraf error = posix_spawnattr_setsigmask(&attr, &cu.savemask);
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate /*
2050Sstevel@tonic-gate * We are required to set the disposition of SIGINT and SIGQUIT
2060Sstevel@tonic-gate * to be ignored for the duration of the system() operation.
2070Sstevel@tonic-gate *
2080Sstevel@tonic-gate * We allow more than one thread to call system() concurrently by
2090Sstevel@tonic-gate * keeping a count of such threads. The signal actions are set
2100Sstevel@tonic-gate * to SIG_IGN when the first thread calls system(). They are
2110Sstevel@tonic-gate * restored in cleanup() when the last thread exits system().
2120Sstevel@tonic-gate *
2130Sstevel@tonic-gate * However, system() is still MT-unsafe because sigaction() has
2140Sstevel@tonic-gate * a process-wide effect and some other thread may also be
2150Sstevel@tonic-gate * setting the signal actions for SIGINT or SIGQUIT.
2160Sstevel@tonic-gate */
2170Sstevel@tonic-gate lmutex_lock(&sys_lock);
2180Sstevel@tonic-gate if (sys_count++ == 0) {
2193235Sraf (void) sigaction(SIGINT, &ignore, &sys_ibuf);
2203235Sraf (void) sigaction(SIGQUIT, &ignore, &sys_qbuf);
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate lmutex_unlock(&sys_lock);
2230Sstevel@tonic-gate
2240Sstevel@tonic-gate /*
2250Sstevel@tonic-gate * If SIGINT and SIGQUIT were not already SIG_IGN, tell
2260Sstevel@tonic-gate * posix_spawn() to make them SIG_DFL in the child,
2270Sstevel@tonic-gate * else leave them as SIG_IGN in the child.
2280Sstevel@tonic-gate */
2290Sstevel@tonic-gate (void) sigemptyset(&mask);
2300Sstevel@tonic-gate if (sys_ibuf.sa_handler != SIG_IGN)
2310Sstevel@tonic-gate (void) sigaddset(&mask, SIGINT);
2320Sstevel@tonic-gate if (sys_qbuf.sa_handler != SIG_IGN)
2330Sstevel@tonic-gate (void) sigaddset(&mask, SIGQUIT);
2340Sstevel@tonic-gate if (error == 0)
2350Sstevel@tonic-gate error = posix_spawnattr_setsigdefault(&attr, &mask);
2360Sstevel@tonic-gate
2373235Sraf argv[0] = (char *)shell;
2383235Sraf argv[1] = "-c";
2393235Sraf argv[2] = (char *)cmd;
2403235Sraf argv[3] = NULL;
2410Sstevel@tonic-gate if (error == 0)
2423235Sraf error = posix_spawn(&cu.pid, shpath, NULL, &attr,
2436879Sraf (char *const *)argv, (char *const *)_environ);
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate (void) posix_spawnattr_destroy(&attr);
2460Sstevel@tonic-gate
2470Sstevel@tonic-gate if (error) {
2480Sstevel@tonic-gate errno = error;
2490Sstevel@tonic-gate status = -1;
2500Sstevel@tonic-gate } else {
2511219Sraf /*
2525891Sraf * system() is a cancellation point and so is waitpid().
2531219Sraf */
2543235Sraf pthread_cleanup_push(cleanup, &cu);
2550Sstevel@tonic-gate do {
2565891Sraf w = waitpid(cu.pid, &status, 0);
2570Sstevel@tonic-gate } while (w == -1 && errno == EINTR);
2580Sstevel@tonic-gate pthread_cleanup_pop(0);
2590Sstevel@tonic-gate if (w == -1)
2600Sstevel@tonic-gate status = -1;
2610Sstevel@tonic-gate }
2623235Sraf error = errno;
2633235Sraf cu.pid = 0;
2643235Sraf cleanup(&cu);
2653235Sraf errno = error;
2660Sstevel@tonic-gate
2670Sstevel@tonic-gate return (status);
2680Sstevel@tonic-gate }
269