system.c revision 276630
195584Sanholt/* 2145132Sanholt * Copyright (c) 1988, 1993 3145132Sanholt * The Regents of the University of California. All rights reserved. 4139749Simp * 595584Sanholt * Redistribution and use in source and binary forms, with or without 695584Sanholt * modification, are permitted provided that the following conditions 795584Sanholt * are met: 895584Sanholt * 1. Redistributions of source code must retain the above copyright 995584Sanholt * notice, this list of conditions and the following disclaimer. 1095584Sanholt * 2. Redistributions in binary form must reproduce the above copyright 1195584Sanholt * notice, this list of conditions and the following disclaimer in the 1295584Sanholt * documentation and/or other materials provided with the distribution. 1395584Sanholt * 3. Neither the name of the University nor the names of its contributors 1495584Sanholt * may be used to endorse or promote products derived from this software 1595584Sanholt * without specific prior written permission. 1695584Sanholt * 1795584Sanholt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1895584Sanholt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1995584Sanholt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2095584Sanholt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2195584Sanholt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2295584Sanholt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2395584Sanholt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2495584Sanholt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2595584Sanholt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2695584Sanholt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2795584Sanholt * SUCH DAMAGE. 2895584Sanholt */ 2995584Sanholt 3095584Sanholt#if defined(LIBC_SCCS) && !defined(lint) 3195584Sanholtstatic char sccsid[] = "@(#)system.c 8.1 (Berkeley) 6/4/93"; 3295584Sanholt#endif /* LIBC_SCCS and not lint */ 3395584Sanholt#include <sys/cdefs.h> 34152909Sanholt__FBSDID("$FreeBSD: head/lib/libc/stdlib/system.c 276630 2015-01-03 18:38:46Z kib $"); 35152909Sanholt 36152909Sanholt#include "namespace.h" 3795584Sanholt#include <sys/types.h> 38112015Sanholt#include <sys/wait.h> 3995746Sanholt#include <signal.h> 4095584Sanholt#include <stdlib.h> 41145132Sanholt#include <stddef.h> 4295584Sanholt#include <string.h> 43145132Sanholt#include <unistd.h> 44145132Sanholt#include <paths.h> 45145132Sanholt#include <errno.h> 46145132Sanholt#include "un-namespace.h" 4795584Sanholt#include "libc_private.h" 48182080Srnoland 49145132Sanholt#pragma weak system 50183573Srnolandint 51183573Srnolandsystem(const char *command) 52183573Srnoland{ 53145132Sanholt 54183573Srnoland return (((int (*)(const char *)) 55189130Srnoland __libc_interposing[INTERPOS_system])(command)); 56183573Srnoland} 57183573Srnoland 58183573Srnolandint 59183573Srnoland__libc_system(const char *command) 60183573Srnoland{ 61183573Srnoland pid_t pid, savedpid; 62183573Srnoland int pstat; 63183573Srnoland struct sigaction ign, intact, quitact; 64183573Srnoland sigset_t newsigblock, oldsigblock; 65183573Srnoland 66145132Sanholt if (!command) /* just checking... */ 67183573Srnoland return(1); 68183573Srnoland 69145132Sanholt (void)sigemptyset(&newsigblock); 70183573Srnoland (void)sigaddset(&newsigblock, SIGCHLD); 71183573Srnoland (void)sigaddset(&newsigblock, SIGINT); 72183573Srnoland (void)sigaddset(&newsigblock, SIGQUIT); 73183573Srnoland (void)_sigprocmask(SIG_BLOCK, &newsigblock, &oldsigblock); 74183573Srnoland switch(pid = vfork()) { 75183573Srnoland case -1: /* error */ 76145132Sanholt (void)_sigprocmask(SIG_SETMASK, &oldsigblock, NULL); 77145132Sanholt return (-1); 78145132Sanholt case 0: /* child */ 79189563Srnoland /* 80145132Sanholt * Restore original signal dispositions and exec the command. 81189563Srnoland */ 82145132Sanholt (void)_sigprocmask(SIG_SETMASK, &oldsigblock, NULL); 83145132Sanholt execl(_PATH_BSHELL, "sh", "-c", command, (char *)NULL); 84145132Sanholt _exit(127); 85189563Srnoland } 86145132Sanholt /* 87189563Srnoland * If we are running means that the child has either completed 88145132Sanholt * its execve, or has failed. 89183833Srnoland * Block SIGINT/QUIT because sh -c handles it and wait for 90183604Srnoland * it to clean up. 91183604Srnoland */ 92145132Sanholt memset(&ign, 0, sizeof(ign)); 93183573Srnoland ign.sa_handler = SIG_IGN; 94189563Srnoland (void)sigemptyset(&ign.sa_mask); 95145132Sanholt (void)_sigaction(SIGINT, &ign, &intact); 96145132Sanholt (void)_sigaction(SIGQUIT, &ign, &quitact); 97189130Srnoland savedpid = pid; 98189130Srnoland do { 99189130Srnoland pid = _wait4(savedpid, &pstat, 0, (struct rusage *)0); 100189130Srnoland } while (pid == -1 && errno == EINTR); 101189130Srnoland (void)_sigaction(SIGINT, &intact, NULL); 102183573Srnoland (void)_sigaction(SIGQUIT, &quitact, NULL); 103189563Srnoland (void)_sigprocmask(SIG_SETMASK, &oldsigblock, NULL); 104183573Srnoland return(pid == -1 ? -1 : pstat); 105189563Srnoland} 106183573Srnoland 107183573Srnoland__weak_reference(__libc_system, __system); 108189563Srnoland__weak_reference(__libc_system, _system); 109183573Srnoland