system.c (3311B)
1 /* 2 * Copyright (c) 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include <sys/types.h> 35 #include <sys/wait.h> 36 #include <signal.h> 37 #include <stdlib.h> 38 #include <stddef.h> 39 #include <unistd.h> 40 #include <errno.h> 41 42 int 43 system(command) 44 const char *command; 45 { 46 pid_t pid, savedpid; 47 int pstat; 48 // struct sigaction ign, intact, quitact; 49 // sigset_t newsigblock, oldsigblock; 50 51 if (!command) /* just checking... */ 52 return(1); 53 54 /* 55 * Ignore SIGINT and SIGQUIT, block SIGCHLD. Remember to save 56 * existing signal dispositions. 57 */ 58 // ign.sa_handler = SIG_IGN; 59 // (void)sigemptyset(&ign.sa_mask); 60 // ign.sa_flags = 0; 61 // (void)_sigaction(SIGINT, &ign, &intact); 62 // (void)_sigaction(SIGQUIT, &ign, &quitact); 63 // (void)sigemptyset(&newsigblock); 64 // (void)sigaddset(&newsigblock, SIGCHLD); 65 // (void)_sigprocmask(SIG_BLOCK, &newsigblock, &oldsigblock); 66 switch(pid = fork()) { 67 case -1: /* error */ 68 break; 69 case 0: /* child */ 70 /* 71 * Restore original signal dispositions and exec the command. 72 */ 73 // (void)_sigaction(SIGINT, &intact, NULL); 74 // (void)_sigaction(SIGQUIT, &quitact, NULL); 75 // (void)_sigprocmask(SIG_SETMASK, &oldsigblock, NULL); 76 execl("/bin/sh", "sh", "-c", command, (char *)NULL); 77 _exit(127); 78 default: /* parent */ 79 savedpid = pid; 80 do { 81 pid = waitpid(savedpid, &pstat, 0); 82 } while (pid == -1 && errno == EINTR); 83 break; 84 } 85 // (void)_sigaction(SIGINT, &intact, NULL); 86 // (void)_sigaction(SIGQUIT, &quitact, NULL); 87 // (void)_sigprocmask(SIG_SETMASK, &oldsigblock, NULL); 88 return(pid == -1 ? -1 : pstat); 89 }