system.c revision 277317
1169695Skan/* 2169695Skan * Copyright (c) 1988, 1993 3169695Skan * The Regents of the University of California. All rights reserved. 4169695Skan * 5169695Skan * Redistribution and use in source and binary forms, with or without 6169695Skan * modification, are permitted provided that the following conditions 7169695Skan * are met: 8169695Skan * 1. Redistributions of source code must retain the above copyright 9169695Skan * notice, this list of conditions and the following disclaimer. 10169695Skan * 2. Redistributions in binary form must reproduce the above copyright 11169695Skan * notice, this list of conditions and the following disclaimer in the 12169695Skan * documentation and/or other materials provided with the distribution. 13169695Skan * 3. Neither the name of the University nor the names of its contributors 14169695Skan * may be used to endorse or promote products derived from this software 15169695Skan * without specific prior written permission. 16169695Skan * 17169695Skan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18169695Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19169695Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20169695Skan * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21169695Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22169695Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23169695Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24169695Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25169695Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26169695Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27169695Skan * SUCH DAMAGE. 28169695Skan */ 29169695Skan 30169695Skan#if defined(LIBC_SCCS) && !defined(lint) 31169695Skanstatic char sccsid[] = "@(#)system.c 8.1 (Berkeley) 6/4/93"; 32169695Skan#endif /* LIBC_SCCS and not lint */ 33169695Skan#include <sys/cdefs.h> 34169695Skan__FBSDID("$FreeBSD: stable/10/lib/libc/stdlib/system.c 277317 2015-01-18 11:54:20Z kib $"); 35169695Skan 36169695Skan#include "namespace.h" 37169695Skan#include <sys/types.h> 38169695Skan#include <sys/wait.h> 39169695Skan#include <signal.h> 40169695Skan#include <stdlib.h> 41169695Skan#include <stddef.h> 42169695Skan#include <string.h> 43169695Skan#include <unistd.h> 44169695Skan#include <paths.h> 45169695Skan#include <errno.h> 46169695Skan#include "un-namespace.h" 47169695Skan#include "libc_private.h" 48169695Skan 49169695Skan#pragma weak system 50169695Skanint 51169695Skansystem(const char *command) 52169695Skan{ 53169695Skan 54169695Skan return (((int (*)(const char *)) 55169695Skan __libc_interposing[INTERPOS_system])(command)); 56169695Skan} 57169695Skan 58169695Skanint 59169695Skan__libc_system(const char *command) 60169695Skan{ 61169695Skan pid_t pid, savedpid; 62169695Skan int pstat; 63169695Skan struct sigaction ign, intact, quitact; 64169695Skan sigset_t newsigblock, oldsigblock; 65169695Skan 66169695Skan if (!command) /* just checking... */ 67169695Skan return(1); 68169695Skan 69169695Skan (void)sigemptyset(&newsigblock); 70169695Skan (void)sigaddset(&newsigblock, SIGCHLD); 71169695Skan (void)sigaddset(&newsigblock, SIGINT); 72169695Skan (void)sigaddset(&newsigblock, SIGQUIT); 73169695Skan (void)_sigprocmask(SIG_BLOCK, &newsigblock, &oldsigblock); 74169695Skan switch(pid = vfork()) { 75169695Skan case -1: /* error */ 76169695Skan (void)_sigprocmask(SIG_SETMASK, &oldsigblock, NULL); 77169695Skan return (-1); 78169695Skan case 0: /* child */ 79169695Skan /* 80169695Skan * Restore original signal dispositions and exec the command. 81169695Skan */ 82169695Skan (void)_sigprocmask(SIG_SETMASK, &oldsigblock, NULL); 83169695Skan execl(_PATH_BSHELL, "sh", "-c", command, (char *)NULL); 84169695Skan _exit(127); 85169695Skan } 86169695Skan /* 87169695Skan * If we are running means that the child has either completed 88169695Skan * its execve, or has failed. 89169695Skan * Block SIGINT/QUIT because sh -c handles it and wait for 90169695Skan * it to clean up. 91169695Skan */ 92169695Skan memset(&ign, 0, sizeof(ign)); 93169695Skan ign.sa_handler = SIG_IGN; 94169695Skan (void)sigemptyset(&ign.sa_mask); 95169695Skan (void)_sigaction(SIGINT, &ign, &intact); 96169695Skan (void)_sigaction(SIGQUIT, &ign, &quitact); 97169695Skan savedpid = pid; 98169695Skan do { 99169695Skan pid = _wait4(savedpid, &pstat, 0, (struct rusage *)0); 100169695Skan } while (pid == -1 && errno == EINTR); 101169695Skan (void)_sigaction(SIGINT, &intact, NULL); 102169695Skan (void)_sigaction(SIGQUIT, &quitact, NULL); 103169695Skan (void)_sigprocmask(SIG_SETMASK, &oldsigblock, NULL); 104169695Skan return(pid == -1 ? -1 : pstat); 105169695Skan} 106169695Skan 107169695Skan__weak_reference(__libc_system, __system); 108169695Skan__weak_reference(__libc_system, _system); 109169695Skan