system.c revision 84417
150764Smarkm/*
250764Smarkm * Copyright (c) 1988, 1993
350764Smarkm *	The Regents of the University of California.  All rights reserved.
450764Smarkm *
550764Smarkm * Redistribution and use in source and binary forms, with or without
650764Smarkm * modification, are permitted provided that the following conditions
750764Smarkm * are met:
850764Smarkm * 1. Redistributions of source code must retain the above copyright
950764Smarkm *    notice, this list of conditions and the following disclaimer.
1050764Smarkm * 2. Redistributions in binary form must reproduce the above copyright
1150764Smarkm *    notice, this list of conditions and the following disclaimer in the
1250764Smarkm *    documentation and/or other materials provided with the distribution.
1350764Smarkm * 3. All advertising materials mentioning features or use of this software
1450764Smarkm *    must display the following acknowledgement:
1550764Smarkm *	This product includes software developed by the University of
1650764Smarkm *	California, Berkeley and its contributors.
1750764Smarkm * 4. Neither the name of the University nor the names of its contributors
1850764Smarkm *    may be used to endorse or promote products derived from this software
1950764Smarkm *    without specific prior written permission.
2050764Smarkm *
2150764Smarkm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2250764Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2350764Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2450764Smarkm * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2550764Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2650764Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2750764Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2850764Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2950764Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3050764Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3150764Smarkm * SUCH DAMAGE.
3250764Smarkm *
3350764Smarkm * $FreeBSD: head/lib/libc/stdlib/system.c 84417 2001-10-03 11:01:39Z alfred $
3450764Smarkm */
3550764Smarkm
3650764Smarkm#if defined(LIBC_SCCS) && !defined(lint)
3750764Smarkmstatic char sccsid[] = "@(#)system.c	8.1 (Berkeley) 6/4/93";
3850764Smarkm#endif /* LIBC_SCCS and not lint */
3950764Smarkm
4050764Smarkm#include "namespace.h"
4150764Smarkm#include <sys/types.h>
4250764Smarkm#include <sys/wait.h>
4350764Smarkm#include <signal.h>
4450764Smarkm#include <stdlib.h>
4550764Smarkm#include <stddef.h>
4650764Smarkm#include <unistd.h>
4750764Smarkm#include <paths.h>
4850764Smarkm#include <errno.h>
4950764Smarkm#include "un-namespace.h"
5050764Smarkm#include "libc_private.h"
5150764Smarkm
5250764Smarkmint
5350764Smarkm__system(command)
5450764Smarkm	const char *command;
5550764Smarkm{
5650764Smarkm	pid_t pid, savedpid;
5750764Smarkm	int pstat;
5850764Smarkm	struct sigaction ign, intact, quitact;
5950764Smarkm	sigset_t newsigblock, oldsigblock;
6050764Smarkm
6150764Smarkm	if (!command)		/* just checking... */
6250764Smarkm		return(1);
6350764Smarkm
6450764Smarkm	/*
6550764Smarkm	 * Ignore SIGINT and SIGQUIT, block SIGCHLD. Remember to save
6650764Smarkm	 * existing signal dispositions.
6750764Smarkm	 */
6850764Smarkm	ign.sa_handler = SIG_IGN;
6950764Smarkm	(void)sigemptyset(&ign.sa_mask);
7050764Smarkm	ign.sa_flags = 0;
7150764Smarkm	(void)_sigaction(SIGINT, &ign, &intact);
7250764Smarkm	(void)_sigaction(SIGQUIT, &ign, &quitact);
7350764Smarkm	(void)sigemptyset(&newsigblock);
74	(void)sigaddset(&newsigblock, SIGCHLD);
75	(void)_sigprocmask(SIG_BLOCK, &newsigblock, &oldsigblock);
76	switch(pid = fork()) {
77	case -1:			/* error */
78		break;
79	case 0:				/* child */
80		/*
81		 * Restore original signal dispositions and exec the command.
82		 */
83		(void)_sigaction(SIGINT, &intact, NULL);
84		(void)_sigaction(SIGQUIT,  &quitact, NULL);
85		(void)_sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
86		execl(_PATH_BSHELL, "sh", "-c", command, (char *)NULL);
87		_exit(127);
88	default:			/* parent */
89		savedpid = pid;
90		do {
91			pid = _wait4(savedpid, &pstat, 0, (struct rusage *)0);
92		} while (pid == -1 && errno == EINTR);
93		break;
94	}
95	(void)_sigaction(SIGINT, &intact, NULL);
96	(void)_sigaction(SIGQUIT,  &quitact, NULL);
97	(void)_sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
98	return(pid == -1 ? -1 : pstat);
99}
100
101__weak_reference(__system, system);
102__weak_reference(__system, _system);
103