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
5*3235Sraf  * Common Development and Distribution License (the "License").
6*3235Sraf  * 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 /*
231219Sraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
300Sstevel@tonic-gate /*	  All Rights Reserved  	*/
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include "synonyms.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>
42*3235Sraf #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 
490Sstevel@tonic-gate extern const char **environ;
500Sstevel@tonic-gate 
510Sstevel@tonic-gate extern int __xpg4;	/* defined in _xpg4.c; 0 if not xpg4-compiled program */
52*3235Sraf 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() */
56*3235Sraf static struct sigaction sys_ibuf;	/* saved SIGINT sigaction */
57*3235Sraf static struct sigaction sys_qbuf;	/* saved SIGQUIT sigaction */
58*3235Sraf static struct sigaction ignore = {0, {SIG_IGN}, {0}};
59*3235Sraf 
60*3235Sraf /*
61*3235Sraf  * Things needed by the cancellation cleanup handler.
62*3235Sraf  */
63*3235Sraf typedef struct {
64*3235Sraf 	sigset_t	savemask;	/* saved signal mask */
65*3235Sraf 	pid_t		pid;		/* if nonzero, the child's pid */
66*3235Sraf } cleanup_t;
67*3235Sraf 
68*3235Sraf /*
69*3235Sraf  * Daemon thread whose sole function is to reap an abandoned child.
70*3235Sraf  * Also invoked from pclose() (see port/stdio/popen.c).
71*3235Sraf  */
72*3235Sraf void *
73*3235Sraf reapchild(void *arg)
74*3235Sraf {
75*3235Sraf 	pid_t pid = (pid_t)(uintptr_t)arg;
76*3235Sraf 
77*3235Sraf 	while (waitpid(pid, NULL, 0) == -1) {
78*3235Sraf 		if (errno != EINTR)
79*3235Sraf 			break;
80*3235Sraf 	}
81*3235Sraf 	return (NULL);
82*3235Sraf }
830Sstevel@tonic-gate 
840Sstevel@tonic-gate /*
850Sstevel@tonic-gate  * Cancellation cleanup handler.
86*3235Sraf  * If we were cancelled in waitpid(), create a daemon thread to
87*3235Sraf  * reap our abandoned child.  No other thread can do this for us.
88*3235Sraf  * It would be better if there were a system call to disinherit
89*3235Sraf  * a child process (give it to init, just as though we exited).
900Sstevel@tonic-gate  */
910Sstevel@tonic-gate static void
920Sstevel@tonic-gate cleanup(void *arg)
930Sstevel@tonic-gate {
94*3235Sraf 	cleanup_t *cup = arg;
95*3235Sraf 
96*3235Sraf 	if (cup->pid != 0) {	/* we were cancelled; abandoning our pid */
97*3235Sraf 		(void) thr_sigsetmask(SIG_SETMASK, &maskset, NULL);
98*3235Sraf 		(void) thr_create(NULL, 0,
99*3235Sraf 		    reapchild, (void *)(uintptr_t)cup->pid,
100*3235Sraf 		    THR_DAEMON, NULL);
101*3235Sraf 	}
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	lmutex_lock(&sys_lock);
1040Sstevel@tonic-gate 	if (--sys_count == 0) {		/* leaving system() */
1050Sstevel@tonic-gate 		/*
106*3235Sraf 		 * There are no remaining threads in system(), so
107*3235Sraf 		 * restore the SIGINT and SIGQUIT signal actions.
1080Sstevel@tonic-gate 		 */
1090Sstevel@tonic-gate 		(void) sigaction(SIGINT, &sys_ibuf, NULL);
1100Sstevel@tonic-gate 		(void) sigaction(SIGQUIT, &sys_qbuf, NULL);
1110Sstevel@tonic-gate 	}
1120Sstevel@tonic-gate 	lmutex_unlock(&sys_lock);
113*3235Sraf 
114*3235Sraf 	(void) thr_sigsetmask(SIG_SETMASK, &cup->savemask, NULL);
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate int
1180Sstevel@tonic-gate system(const char *cmd)
1190Sstevel@tonic-gate {
120*3235Sraf 	cleanup_t cu;
1210Sstevel@tonic-gate 	pid_t w;
1220Sstevel@tonic-gate 	int status;
1230Sstevel@tonic-gate 	int error;
1240Sstevel@tonic-gate 	sigset_t mask;
1250Sstevel@tonic-gate 	struct stat64 buf;
1260Sstevel@tonic-gate 	const char *shpath;
127*3235Sraf 	char *argv[4];
1280Sstevel@tonic-gate 	posix_spawnattr_t attr;
1290Sstevel@tonic-gate 	static const char *sun_path = "/bin/sh";
1300Sstevel@tonic-gate 	static const char *xpg4_path = "/usr/xpg4/bin/sh";
1310Sstevel@tonic-gate 	static const char *shell = "sh";
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	shpath = __xpg4? xpg4_path : sun_path;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	if (cmd == NULL) {
1360Sstevel@tonic-gate 		if (stat64(shpath, &buf) != 0) {
1370Sstevel@tonic-gate 			return (0);
1380Sstevel@tonic-gate 		} else if (getuid() == buf.st_uid) {
1390Sstevel@tonic-gate 			/* exec for user */
1400Sstevel@tonic-gate 			if ((buf.st_mode & 0100) == 0)
1410Sstevel@tonic-gate 				return (0);
1420Sstevel@tonic-gate 		} else if (getgid() == buf.st_gid) {
1430Sstevel@tonic-gate 			/* exec for group */
1440Sstevel@tonic-gate 			if ((buf.st_mode & 0010) == 0)
1450Sstevel@tonic-gate 				return (0);
1460Sstevel@tonic-gate 		} else if ((buf.st_mode & 0001) == 0) {	/* exec for others */
1470Sstevel@tonic-gate 			return (0);
1480Sstevel@tonic-gate 		}
1490Sstevel@tonic-gate 		return (1);
1500Sstevel@tonic-gate 	}
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	/*
1530Sstevel@tonic-gate 	 * Initialize the posix_spawn() attributes structure.
154*3235Sraf 	 * The setting of POSIX_SPAWN_WAITPID_NP ensures that no
155*3235Sraf 	 * wait-for-multiple wait() operation will reap our child
156*3235Sraf 	 * and that the child will not be automatically reaped due
157*3235Sraf 	 * to the disposition of SIGCHLD being set to be ignored.
158*3235Sraf 	 * Only a specific wait for the specific pid will be able
159*3235Sraf 	 * to reap the child.  Since no other thread knows the pid
160*3235Sraf 	 * of our child, this should be safe enough.
1610Sstevel@tonic-gate 	 */
162*3235Sraf 	error = posix_spawnattr_init(&attr);
163*3235Sraf 	if (error == 0)
164*3235Sraf 		error = posix_spawnattr_setflags(&attr,
165*3235Sraf 		    POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF |
166*3235Sraf 		    POSIX_SPAWN_NOSIGCHLD_NP | POSIX_SPAWN_WAITPID_NP);
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	/*
169*3235Sraf 	 * The POSIX spec for system() requires us to block SIGCHLD,
170*3235Sraf 	 * the rationale being that the process's signal handler for
171*3235Sraf 	 * SIGCHLD, if any, should not be called when our child exits.
172*3235Sraf 	 * This doesn't work for a multithreaded process because some
173*3235Sraf 	 * other thread could receive the SIGCHLD.
174*3235Sraf 	 *
175*3235Sraf 	 * The above setting of POSIX_SPAWN_NOSIGCHLD_NP ensures that no
176*3235Sraf 	 * SIGCHLD signal will be posted for our child when it exits, so
177*3235Sraf 	 * we don't have to block SIGCHLD to meet the intent of the spec.
178*3235Sraf 	 * We block SIGCHLD anyway, just because the spec requires it.
1790Sstevel@tonic-gate 	 */
1800Sstevel@tonic-gate 	(void) sigemptyset(&mask);
1810Sstevel@tonic-gate 	(void) sigaddset(&mask, SIGCHLD);
182*3235Sraf 	(void) thr_sigsetmask(SIG_BLOCK, &mask, &cu.savemask);
1830Sstevel@tonic-gate 	/*
1840Sstevel@tonic-gate 	 * Tell posix_spawn() to restore the signal mask in the child.
1850Sstevel@tonic-gate 	 */
1860Sstevel@tonic-gate 	if (error == 0)
187*3235Sraf 		error = posix_spawnattr_setsigmask(&attr, &cu.savemask);
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	/*
1900Sstevel@tonic-gate 	 * We are required to set the disposition of SIGINT and SIGQUIT
1910Sstevel@tonic-gate 	 * to be ignored for the duration of the system() operation.
1920Sstevel@tonic-gate 	 *
1930Sstevel@tonic-gate 	 * We allow more than one thread to call system() concurrently by
1940Sstevel@tonic-gate 	 * keeping a count of such threads.  The signal actions are set
1950Sstevel@tonic-gate 	 * to SIG_IGN when the first thread calls system().  They are
1960Sstevel@tonic-gate 	 * restored in cleanup() when the last thread exits system().
1970Sstevel@tonic-gate 	 *
1980Sstevel@tonic-gate 	 * However, system() is still MT-unsafe because sigaction() has
1990Sstevel@tonic-gate 	 * a process-wide effect and some other thread may also be
2000Sstevel@tonic-gate 	 * setting the signal actions for SIGINT or SIGQUIT.
2010Sstevel@tonic-gate 	 */
2020Sstevel@tonic-gate 	lmutex_lock(&sys_lock);
2030Sstevel@tonic-gate 	if (sys_count++ == 0) {
204*3235Sraf 		(void) sigaction(SIGINT, &ignore, &sys_ibuf);
205*3235Sraf 		(void) sigaction(SIGQUIT, &ignore, &sys_qbuf);
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate 	lmutex_unlock(&sys_lock);
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	/*
2100Sstevel@tonic-gate 	 * If SIGINT and SIGQUIT were not already SIG_IGN, tell
2110Sstevel@tonic-gate 	 * posix_spawn() to make them SIG_DFL in the child,
2120Sstevel@tonic-gate 	 * else leave them as SIG_IGN in the child.
2130Sstevel@tonic-gate 	 */
2140Sstevel@tonic-gate 	(void) sigemptyset(&mask);
2150Sstevel@tonic-gate 	if (sys_ibuf.sa_handler != SIG_IGN)
2160Sstevel@tonic-gate 		(void) sigaddset(&mask, SIGINT);
2170Sstevel@tonic-gate 	if (sys_qbuf.sa_handler != SIG_IGN)
2180Sstevel@tonic-gate 		(void) sigaddset(&mask, SIGQUIT);
2190Sstevel@tonic-gate 	if (error == 0)
2200Sstevel@tonic-gate 		error = posix_spawnattr_setsigdefault(&attr, &mask);
2210Sstevel@tonic-gate 
222*3235Sraf 	argv[0] = (char *)shell;
223*3235Sraf 	argv[1] = "-c";
224*3235Sraf 	argv[2] = (char *)cmd;
225*3235Sraf 	argv[3] = NULL;
2260Sstevel@tonic-gate 	if (error == 0)
227*3235Sraf 		error = posix_spawn(&cu.pid, shpath, NULL, &attr,
228*3235Sraf 			(char *const *)argv, (char *const *)environ);
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	(void) posix_spawnattr_destroy(&attr);
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	if (error) {
2330Sstevel@tonic-gate 		errno = error;
2340Sstevel@tonic-gate 		status = -1;
2350Sstevel@tonic-gate 	} else {
2361219Sraf 		/*
2371219Sraf 		 * system() is a cancellation point.
2381219Sraf 		 * Call waitpid_cancel() rather than _waitpid() to make
2391219Sraf 		 * sure that we actually perform the cancellation logic.
2401219Sraf 		 */
241*3235Sraf 		pthread_cleanup_push(cleanup, &cu);
2420Sstevel@tonic-gate 		do {
243*3235Sraf 			w = waitpid_cancel(cu.pid, &status, 0);
2440Sstevel@tonic-gate 		} while (w == -1 && errno == EINTR);
2450Sstevel@tonic-gate 		pthread_cleanup_pop(0);
2460Sstevel@tonic-gate 		if (w == -1)
2470Sstevel@tonic-gate 			status = -1;
2480Sstevel@tonic-gate 	}
249*3235Sraf 	error = errno;
250*3235Sraf 	cu.pid = 0;
251*3235Sraf 	cleanup(&cu);
252*3235Sraf 	errno = error;
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	return (status);
2550Sstevel@tonic-gate }
256