system.c revision 84417
11573Srgrimes/* 21573Srgrimes * Copyright (c) 1988, 1993 31573Srgrimes * The Regents of the University of California. All rights reserved. 41573Srgrimes * 51573Srgrimes * Redistribution and use in source and binary forms, with or without 61573Srgrimes * modification, are permitted provided that the following conditions 71573Srgrimes * are met: 81573Srgrimes * 1. Redistributions of source code must retain the above copyright 91573Srgrimes * notice, this list of conditions and the following disclaimer. 101573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111573Srgrimes * notice, this list of conditions and the following disclaimer in the 121573Srgrimes * documentation and/or other materials provided with the distribution. 131573Srgrimes * 3. All advertising materials mentioning features or use of this software 141573Srgrimes * must display the following acknowledgement: 151573Srgrimes * This product includes software developed by the University of 161573Srgrimes * California, Berkeley and its contributors. 171573Srgrimes * 4. Neither the name of the University nor the names of its contributors 181573Srgrimes * may be used to endorse or promote products derived from this software 191573Srgrimes * without specific prior written permission. 201573Srgrimes * 211573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241573Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311573Srgrimes * SUCH DAMAGE. 3255837Sjasone * 3355837Sjasone * $FreeBSD: head/lib/libc/stdlib/system.c 84417 2001-10-03 11:01:39Z alfred $ 341573Srgrimes */ 351573Srgrimes 361573Srgrimes#if defined(LIBC_SCCS) && !defined(lint) 371573Srgrimesstatic char sccsid[] = "@(#)system.c 8.1 (Berkeley) 6/4/93"; 381573Srgrimes#endif /* LIBC_SCCS and not lint */ 391573Srgrimes 4071579Sdeischen#include "namespace.h" 411573Srgrimes#include <sys/types.h> 421573Srgrimes#include <sys/wait.h> 431573Srgrimes#include <signal.h> 441573Srgrimes#include <stdlib.h> 451573Srgrimes#include <stddef.h> 461573Srgrimes#include <unistd.h> 471573Srgrimes#include <paths.h> 4816117Sjraynard#include <errno.h> 4971579Sdeischen#include "un-namespace.h" 5071579Sdeischen#include "libc_private.h" 511573Srgrimes 5255837Sjasoneint 5355837Sjasone__system(command) 541573Srgrimes const char *command; 551573Srgrimes{ 5684417Salfred pid_t pid, savedpid; 5716117Sjraynard int pstat; 5816117Sjraynard struct sigaction ign, intact, quitact; 5916117Sjraynard sigset_t newsigblock, oldsigblock; 601573Srgrimes 611573Srgrimes if (!command) /* just checking... */ 621573Srgrimes return(1); 631573Srgrimes 6416117Sjraynard /* 6516117Sjraynard * Ignore SIGINT and SIGQUIT, block SIGCHLD. Remember to save 6616117Sjraynard * existing signal dispositions. 6716117Sjraynard */ 6816117Sjraynard ign.sa_handler = SIG_IGN; 6916117Sjraynard (void)sigemptyset(&ign.sa_mask); 7016117Sjraynard ign.sa_flags = 0; 7171579Sdeischen (void)_sigaction(SIGINT, &ign, &intact); 7271579Sdeischen (void)_sigaction(SIGQUIT, &ign, &quitact); 7316117Sjraynard (void)sigemptyset(&newsigblock); 7416228Sjraynard (void)sigaddset(&newsigblock, SIGCHLD); 7571579Sdeischen (void)_sigprocmask(SIG_BLOCK, &newsigblock, &oldsigblock); 7616117Sjraynard switch(pid = fork()) { 771573Srgrimes case -1: /* error */ 7816117Sjraynard break; 791573Srgrimes case 0: /* child */ 8016117Sjraynard /* 8116117Sjraynard * Restore original signal dispositions and exec the command. 8216117Sjraynard */ 8371579Sdeischen (void)_sigaction(SIGINT, &intact, NULL); 8471579Sdeischen (void)_sigaction(SIGQUIT, &quitact, NULL); 8571579Sdeischen (void)_sigprocmask(SIG_SETMASK, &oldsigblock, NULL); 861573Srgrimes execl(_PATH_BSHELL, "sh", "-c", command, (char *)NULL); 871573Srgrimes _exit(127); 8816117Sjraynard default: /* parent */ 8984417Salfred savedpid = pid; 9016117Sjraynard do { 9184417Salfred pid = _wait4(savedpid, &pstat, 0, (struct rusage *)0); 9216117Sjraynard } while (pid == -1 && errno == EINTR); 9316117Sjraynard break; 941573Srgrimes } 9571579Sdeischen (void)_sigaction(SIGINT, &intact, NULL); 9671579Sdeischen (void)_sigaction(SIGQUIT, &quitact, NULL); 9771579Sdeischen (void)_sigprocmask(SIG_SETMASK, &oldsigblock, NULL); 9816117Sjraynard return(pid == -1 ? -1 : pstat); 991573Srgrimes} 10055837Sjasone 10156698Sjasone__weak_reference(__system, system); 10271579Sdeischen__weak_reference(__system, _system); 103