system.c revision 16117
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. 321573Srgrimes */ 331573Srgrimes 341573Srgrimes#if defined(LIBC_SCCS) && !defined(lint) 351573Srgrimesstatic char sccsid[] = "@(#)system.c 8.1 (Berkeley) 6/4/93"; 361573Srgrimes#endif /* LIBC_SCCS and not lint */ 371573Srgrimes 381573Srgrimes#include <sys/types.h> 391573Srgrimes#include <sys/wait.h> 401573Srgrimes#include <signal.h> 411573Srgrimes#include <stdlib.h> 421573Srgrimes#include <stddef.h> 431573Srgrimes#include <unistd.h> 441573Srgrimes#include <paths.h> 4516117Sjraynard#include <errno.h> 461573Srgrimes 4716117Sjraynardint system(command) 481573Srgrimes const char *command; 491573Srgrimes{ 501573Srgrimes pid_t pid; 5116117Sjraynard int pstat; 5216117Sjraynard struct sigaction ign, intact, quitact; 5316117Sjraynard sigset_t newsigblock, oldsigblock; 541573Srgrimes 551573Srgrimes if (!command) /* just checking... */ 561573Srgrimes return(1); 571573Srgrimes 5816117Sjraynard /* 5916117Sjraynard * Ignore SIGINT and SIGQUIT, block SIGCHLD. Remember to save 6016117Sjraynard * existing signal dispositions. 6116117Sjraynard */ 6216117Sjraynard ign.sa_handler = SIG_IGN; 6316117Sjraynard (void)sigemptyset(&ign.sa_mask); 6416117Sjraynard ign.sa_flags = 0; 6516117Sjraynard (void)sigaction(SIGINT, &ign, &intact); 6616117Sjraynard (void)sigaction(SIGQUIT, &ign, &quitact); 6716117Sjraynard (void)sigemptyset(&newsigblock); 6816117Sjraynard sigprocmask(SIG_BLOCK, &newsigblock, &oldsigblock); 6916117Sjraynard switch(pid = fork()) { 701573Srgrimes case -1: /* error */ 7116117Sjraynard break; 721573Srgrimes case 0: /* child */ 7316117Sjraynard /* 7416117Sjraynard * Restore original signal dispositions and exec the command. 7516117Sjraynard */ 7616117Sjraynard (void)sigaction(SIGINT, &intact, NULL); 7716117Sjraynard (void)sigaction(SIGQUIT, &quitact, NULL); 7816117Sjraynard (void)sigprocmask(SIG_SETMASK, &oldsigblock, NULL); 791573Srgrimes execl(_PATH_BSHELL, "sh", "-c", command, (char *)NULL); 801573Srgrimes _exit(127); 8116117Sjraynard default: /* parent */ 8216117Sjraynard do { 8316117Sjraynard pid = waitpid(pid, &pstat, 0); 8416117Sjraynard } while (pid == -1 && errno == EINTR); 8516117Sjraynard break; 861573Srgrimes } 8716117Sjraynard (void)sigaction(SIGINT, &intact, NULL); 8816117Sjraynard (void)sigaction(SIGQUIT, &quitact, NULL); 8916117Sjraynard (void)sigprocmask(SIG_SETMASK, &oldsigblock, NULL); 9016117Sjraynard return(pid == -1 ? -1 : pstat); 911573Srgrimes} 92