system.c revision 1dc9e472e19acfe6dc7f41e429236e7eef7ceda1
1c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner/* $OpenBSD: system.c,v 1.8 2005/08/08 08:05:37 espie Exp $ */ 2c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner/* 3c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * Copyright (c) 1988 The Regents of the University of California. 4c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * All rights reserved. 5c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * 6c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * Redistribution and use in source and binary forms, with or without 7c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * modification, are permitted provided that the following conditions 8c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * are met: 9c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * 1. Redistributions of source code must retain the above copyright 10c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * notice, this list of conditions and the following disclaimer. 11c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * 2. Redistributions in binary form must reproduce the above copyright 12c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * notice, this list of conditions and the following disclaimer in the 13c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * documentation and/or other materials provided with the distribution. 14c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * 3. Neither the name of the University nor the names of its contributors 15c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * may be used to endorse or promote products derived from this software 16c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * without specific prior written permission. 17c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * 18c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * SUCH DAMAGE. 29c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner */ 30c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner 31c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner#include <sys/types.h> 32c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner#include <signal.h> 33c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner#include <stdlib.h> 34c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner#include <unistd.h> 35c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner#include <paths.h> 36c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner#include <sys/wait.h> 37c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner 38c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turnerextern char **environ; 39c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner 40650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughesint 41650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughessystem(const char *command) 42650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes{ 43650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes pid_t pid; 44c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner sig_t intsave, quitsave; 45650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes sigset_t mask, omask; 46c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner int pstat; 47650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes char *argp[] = {"sh", "-c", NULL, NULL}; 48650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes 49650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes if (!command) /* just checking... */ 50650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes return(1); 51650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes 52c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner argp[2] = (char *)command; 53650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes 54650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes sigemptyset(&mask); 55650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes sigaddset(&mask, SIGCHLD); 56650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes sigprocmask(SIG_BLOCK, &mask, &omask); 57650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes switch (pid = vfork()) { 58650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes case -1: /* error */ 59650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes sigprocmask(SIG_SETMASK, &omask, NULL); 60650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes return(-1); 61c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner case 0: /* child */ 62650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes sigprocmask(SIG_SETMASK, &omask, NULL); 63650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes execve(_PATH_BSHELL, argp, environ); 64650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes _exit(127); 65650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes } 66650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes 67650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes intsave = (sig_t) bsd_signal(SIGINT, SIG_IGN); 68650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes quitsave = (sig_t) bsd_signal(SIGQUIT, SIG_IGN); 69650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes pid = waitpid(pid, (int *)&pstat, 0); 70650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes sigprocmask(SIG_SETMASK, &omask, NULL); 71650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes (void)bsd_signal(SIGINT, intsave); 72650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes (void)bsd_signal(SIGQUIT, quitsave); 73650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes return (pid == -1 ? -1 : pstat); 74650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes} 75650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes