system.c revision 251672
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. 13251672Semaste * 3. Neither the name of the University nor the names of its contributors 141573Srgrimes * may be used to endorse or promote products derived from this software 151573Srgrimes * without specific prior written permission. 161573Srgrimes * 171573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201573Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271573Srgrimes * SUCH DAMAGE. 281573Srgrimes */ 291573Srgrimes 301573Srgrimes#if defined(LIBC_SCCS) && !defined(lint) 311573Srgrimesstatic char sccsid[] = "@(#)system.c 8.1 (Berkeley) 6/4/93"; 321573Srgrimes#endif /* LIBC_SCCS and not lint */ 3392986Sobrien#include <sys/cdefs.h> 3492986Sobrien__FBSDID("$FreeBSD: head/lib/libc/stdlib/system.c 251672 2013-06-13 00:19:30Z emaste $"); 351573Srgrimes 3671579Sdeischen#include "namespace.h" 371573Srgrimes#include <sys/types.h> 381573Srgrimes#include <sys/wait.h> 391573Srgrimes#include <signal.h> 401573Srgrimes#include <stdlib.h> 411573Srgrimes#include <stddef.h> 421573Srgrimes#include <unistd.h> 431573Srgrimes#include <paths.h> 4416117Sjraynard#include <errno.h> 4571579Sdeischen#include "un-namespace.h" 4671579Sdeischen#include "libc_private.h" 471573Srgrimes 4855837Sjasoneint 49200150Sed__system(const char *command) 501573Srgrimes{ 5184417Salfred pid_t pid, savedpid; 5216117Sjraynard int pstat; 5316117Sjraynard struct sigaction ign, intact, quitact; 5416117Sjraynard sigset_t newsigblock, oldsigblock; 551573Srgrimes 561573Srgrimes if (!command) /* just checking... */ 571573Srgrimes return(1); 581573Srgrimes 5916117Sjraynard /* 6016117Sjraynard * Ignore SIGINT and SIGQUIT, block SIGCHLD. Remember to save 6116117Sjraynard * existing signal dispositions. 6216117Sjraynard */ 6316117Sjraynard ign.sa_handler = SIG_IGN; 6416117Sjraynard (void)sigemptyset(&ign.sa_mask); 6516117Sjraynard ign.sa_flags = 0; 6671579Sdeischen (void)_sigaction(SIGINT, &ign, &intact); 6771579Sdeischen (void)_sigaction(SIGQUIT, &ign, &quitact); 6816117Sjraynard (void)sigemptyset(&newsigblock); 6916228Sjraynard (void)sigaddset(&newsigblock, SIGCHLD); 7071579Sdeischen (void)_sigprocmask(SIG_BLOCK, &newsigblock, &oldsigblock); 7116117Sjraynard switch(pid = fork()) { 721573Srgrimes case -1: /* error */ 7316117Sjraynard break; 741573Srgrimes case 0: /* child */ 7516117Sjraynard /* 7616117Sjraynard * Restore original signal dispositions and exec the command. 7716117Sjraynard */ 7871579Sdeischen (void)_sigaction(SIGINT, &intact, NULL); 7971579Sdeischen (void)_sigaction(SIGQUIT, &quitact, NULL); 8071579Sdeischen (void)_sigprocmask(SIG_SETMASK, &oldsigblock, NULL); 811573Srgrimes execl(_PATH_BSHELL, "sh", "-c", command, (char *)NULL); 821573Srgrimes _exit(127); 8316117Sjraynard default: /* parent */ 8484417Salfred savedpid = pid; 8516117Sjraynard do { 8684417Salfred pid = _wait4(savedpid, &pstat, 0, (struct rusage *)0); 8716117Sjraynard } while (pid == -1 && errno == EINTR); 8816117Sjraynard break; 891573Srgrimes } 9071579Sdeischen (void)_sigaction(SIGINT, &intact, NULL); 9171579Sdeischen (void)_sigaction(SIGQUIT, &quitact, NULL); 9271579Sdeischen (void)_sigprocmask(SIG_SETMASK, &oldsigblock, NULL); 9316117Sjraynard return(pid == -1 ? -1 : pstat); 941573Srgrimes} 9555837Sjasone 9656698Sjasone__weak_reference(__system, system); 9771579Sdeischen__weak_reference(__system, _system); 98