2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 1988, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A/* Copyright (c) 1988 AT&T */
2N/A/* All Rights Reserved */
2N/A
2N/A#include "lint.h"
2N/A#include "mtlib.h"
2N/A#include <sys/types.h>
2N/A#include <sys/wait.h>
2N/A#include <signal.h>
2N/A#include <stdlib.h>
2N/A#include <wait.h>
2N/A#include <sys/stat.h>
2N/A#include <unistd.h>
2N/A#include <memory.h>
2N/A#include <thread.h>
2N/A#include <pthread.h>
2N/A#include <errno.h>
2N/A#include <synch.h>
2N/A#include <spawn.h>
2N/A#include "libc.h"
2N/A
2N/Aextern const char **_environ;
2N/A
2N/Aextern int __xpg4; /* defined in _xpg4.c; 0 if not xpg4-compiled program */
2N/Aextern const sigset_t maskset; /* all maskable signals */
2N/A
2N/Astatic mutex_t sys_lock = DEFAULTMUTEX; /* protects the following */
2N/Astatic uint_t sys_count = 0; /* number of threads in system() */
2N/Astatic struct sigaction sys_ibuf; /* saved SIGINT sigaction */
2N/Astatic struct sigaction sys_qbuf; /* saved SIGQUIT sigaction */
2N/Astatic struct sigaction ignore = {0, {SIG_IGN}, {0}};
2N/A
2N/A/*
2N/A * Things needed by the cancellation cleanup handler.
2N/A */
2N/Atypedef struct {
2N/A sigset_t savemask; /* saved signal mask */
2N/A pid_t pid; /* if nonzero, the child's pid */
2N/A} cleanup_t;
2N/A
2N/A/*
2N/A * Daemon thread whose sole function is to reap an abandoned child.
2N/A * Also invoked from pclose() (see port/stdio/popen.c).
2N/A */
2N/Avoid *
2N/Areapchild(void *arg)
2N/A{
2N/A pid_t pid = (pid_t)(uintptr_t)arg;
2N/A int cancel_state;
2N/A
2N/A (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
2N/A while (waitpid(pid, NULL, 0) == -1) {
2N/A if (errno != EINTR)
2N/A break;
2N/A }
2N/A (void) pthread_setcancelstate(cancel_state, NULL);
2N/A return (NULL);
2N/A}
2N/A
2N/A/*
2N/A * Cancellation cleanup handler.
2N/A * If we were cancelled in waitpid(), create a daemon thread to
2N/A * reap our abandoned child. No other thread can do this for us.
2N/A * It would be better if there were a system call to disinherit
2N/A * a child process (give it to init, just as though we exited).
2N/A */
2N/Astatic void
2N/Acleanup(void *arg)
2N/A{
2N/A cleanup_t *cup = arg;
2N/A
2N/A if (cup->pid != 0) { /* we were cancelled; abandoning our pid */
2N/A (void) thr_sigsetmask(SIG_SETMASK, &maskset, NULL);
2N/A (void) thr_create(NULL, 0,
2N/A reapchild, (void *)(uintptr_t)cup->pid,
2N/A THR_DAEMON, NULL);
2N/A }
2N/A
2N/A lmutex_lock(&sys_lock);
2N/A if (--sys_count == 0) { /* leaving system() */
2N/A /*
2N/A * There are no remaining threads in system(), so
2N/A * restore the SIGINT and SIGQUIT signal actions.
2N/A */
2N/A (void) sigaction(SIGINT, &sys_ibuf, NULL);
2N/A (void) sigaction(SIGQUIT, &sys_qbuf, NULL);
2N/A }
2N/A lmutex_unlock(&sys_lock);
2N/A
2N/A (void) thr_sigsetmask(SIG_SETMASK, &cup->savemask, NULL);
2N/A}
2N/A
2N/Aint
2N/Asystem(const char *cmd)
2N/A{
2N/A cleanup_t cu;
2N/A pid_t w;
2N/A int status;
2N/A int error;
2N/A sigset_t mask;
2N/A struct stat64 buf;
2N/A const char *shpath;
2N/A char *argv[4];
2N/A posix_spawnattr_t attr;
2N/A static const char *sun_path = "/bin/sh";
2N/A static const char *xpg4_path = "/usr/xpg4/bin/sh";
2N/A static const char *shell = "sh";
2N/A
2N/A shpath = __xpg4? xpg4_path : sun_path;
2N/A
2N/A if (cmd == NULL) {
2N/A if (stat64(shpath, &buf) != 0) {
2N/A return (0);
2N/A } else if (getuid() == buf.st_uid) {
2N/A /* exec for user */
2N/A if ((buf.st_mode & 0100) == 0)
2N/A return (0);
2N/A } else if (getgid() == buf.st_gid) {
2N/A /* exec for group */
2N/A if ((buf.st_mode & 0010) == 0)
2N/A return (0);
2N/A } else if ((buf.st_mode & 0001) == 0) { /* exec for others */
2N/A return (0);
2N/A }
2N/A return (1);
2N/A }
2N/A
2N/A /*
2N/A * Initialize the posix_spawn() attributes structure.
2N/A *
2N/A * The setting of POSIX_SPAWN_WAITPID_NP ensures that no
2N/A * wait-for-multiple wait() operation will reap our child
2N/A * and that the child will not be automatically reaped due
2N/A * to the disposition of SIGCHLD being set to be ignored.
2N/A * Only a specific wait for the specific pid will be able
2N/A * to reap the child. Since no other thread knows the pid
2N/A * of our child, this should be safe enough.
2N/A *
2N/A * The POSIX_SPAWN_NOEXECERR_NP flag tells posix_spawn() not
2N/A * to fail if the shell cannot be executed, but rather cause
2N/A * a child to be created that simply performs _exit(127).
2N/A * This is in order to satisfy the Posix requirement on system():
2N/A * The system function shall behave as if a child process were
2N/A * created using fork(), and the child process invoked the sh
2N/A * utility using execl(). If some error prevents the command
2N/A * language interpreter from executing after the child process
2N/A * is created, the return value from system() shall be as if
2N/A * the command language interpreter had terminated using
2N/A * exit(127) or _exit(127).
2N/A */
2N/A error = posix_spawnattr_init(&attr);
2N/A if (error == 0)
2N/A error = posix_spawnattr_setflags(&attr,
2N/A POSIX_SPAWN_RESETIDS |
2N/A POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF |
2N/A POSIX_SPAWN_NOSIGCHLD_NP | POSIX_SPAWN_WAITPID_NP |
2N/A POSIX_SPAWN_NOEXECERR_NP);
2N/A
2N/A /*
2N/A * The POSIX spec for system() requires us to block SIGCHLD,
2N/A * the rationale being that the process's signal handler for
2N/A * SIGCHLD, if any, should not be called when our child exits.
2N/A * This doesn't work for a multithreaded process because some
2N/A * other thread could receive the SIGCHLD.
2N/A *
2N/A * The above setting of POSIX_SPAWN_NOSIGCHLD_NP ensures that no
2N/A * SIGCHLD signal will be posted for our child when it exits, so
2N/A * we don't have to block SIGCHLD to meet the intent of the spec.
2N/A * We block SIGCHLD anyway, just because the spec requires it.
2N/A */
2N/A (void) sigemptyset(&mask);
2N/A (void) sigaddset(&mask, SIGCHLD);
2N/A (void) thr_sigsetmask(SIG_BLOCK, &mask, &cu.savemask);
2N/A /*
2N/A * Tell posix_spawn() to restore the signal mask in the child.
2N/A */
2N/A if (error == 0)
2N/A error = posix_spawnattr_setsigmask(&attr, &cu.savemask);
2N/A
2N/A /*
2N/A * We are required to set the disposition of SIGINT and SIGQUIT
2N/A * to be ignored for the duration of the system() operation.
2N/A *
2N/A * We allow more than one thread to call system() concurrently by
2N/A * keeping a count of such threads. The signal actions are set
2N/A * to SIG_IGN when the first thread calls system(). They are
2N/A * restored in cleanup() when the last thread exits system().
2N/A *
2N/A * However, system() is still MT-unsafe because sigaction() has
2N/A * a process-wide effect and some other thread may also be
2N/A * setting the signal actions for SIGINT or SIGQUIT.
2N/A */
2N/A lmutex_lock(&sys_lock);
2N/A if (sys_count++ == 0) {
2N/A (void) sigaction(SIGINT, &ignore, &sys_ibuf);
2N/A (void) sigaction(SIGQUIT, &ignore, &sys_qbuf);
2N/A }
2N/A lmutex_unlock(&sys_lock);
2N/A
2N/A /*
2N/A * If SIGINT and SIGQUIT were not already SIG_IGN, tell
2N/A * posix_spawn() to make them SIG_DFL in the child,
2N/A * else leave them as SIG_IGN in the child.
2N/A */
2N/A (void) sigemptyset(&mask);
2N/A if (sys_ibuf.sa_handler != SIG_IGN)
2N/A (void) sigaddset(&mask, SIGINT);
2N/A if (sys_qbuf.sa_handler != SIG_IGN)
2N/A (void) sigaddset(&mask, SIGQUIT);
2N/A if (error == 0)
2N/A error = posix_spawnattr_setsigdefault(&attr, &mask);
2N/A
2N/A argv[0] = (char *)shell;
2N/A argv[1] = "-c";
2N/A argv[2] = (char *)cmd;
2N/A argv[3] = NULL;
2N/A if (error == 0)
2N/A error = posix_spawn(&cu.pid, shpath, NULL, &attr,
2N/A (char *const *)argv, (char *const *)_environ);
2N/A
2N/A (void) posix_spawnattr_destroy(&attr);
2N/A
2N/A if (error) {
2N/A errno = error;
2N/A status = -1;
2N/A } else {
2N/A /*
2N/A * system() is a cancellation point and so is waitpid().
2N/A */
2N/A pthread_cleanup_push(cleanup, &cu);
2N/A do {
2N/A w = waitpid(cu.pid, &status, 0);
2N/A } while (w == -1 && errno == EINTR);
2N/A pthread_cleanup_pop(0);
2N/A if (w == -1)
2N/A status = -1;
2N/A }
2N/A error = errno;
2N/A cu.pid = 0;
2N/A cleanup(&cu);
2N/A errno = error;
2N/A
2N/A return (status);
2N/A}