xref: /dragonfly/lib/libc/stdlib/system.c (revision a32e3ba6)
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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#)system.c	8.1 (Berkeley) 6/4/93
30  * $FreeBSD: src/lib/libc/stdlib/system.c,v 1.11 2007/01/09 00:28:10 imp Exp $
31  */
32 
33 #include "namespace.h"
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 <paths.h>
41 #include <errno.h>
42 #include "un-namespace.h"
43 #include "libc_private.h"
44 
45 int __system(const char *);
46 
47 int
__system(const char * command)48 __system(const char *command)
49 {
50 	pid_t pid, savedpid;
51 	int pstat;
52 	struct sigaction ign, intact, quitact;
53 	sigset_t newsigblock, oldsigblock;
54 
55 	if (!command)		/* just checking... */
56 		return(1);
57 
58 	/*
59 	 * Ignore SIGINT and SIGQUIT, block SIGCHLD. Remember to save
60 	 * existing signal dispositions.
61 	 */
62 	ign.sa_handler = SIG_IGN;
63 	sigemptyset(&ign.sa_mask);
64 	ign.sa_flags = 0;
65 	_sigaction(SIGINT, &ign, &intact);
66 	_sigaction(SIGQUIT, &ign, &quitact);
67 	sigemptyset(&newsigblock);
68 	sigaddset(&newsigblock, SIGCHLD);
69 	_sigprocmask(SIG_BLOCK, &newsigblock, &oldsigblock);
70 	switch(pid = fork()) {
71 	case -1:			/* error */
72 		break;
73 	case 0:				/* child */
74 		/*
75 		 * Restore original signal dispositions and exec the command.
76 		 */
77 		_sigaction(SIGINT, &intact, NULL);
78 		_sigaction(SIGQUIT,  &quitact, NULL);
79 		_sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
80 		execl(_PATH_BSHELL, "sh", "-c", command, NULL);
81 		_exit(127);
82 	default:			/* parent */
83 		savedpid = pid;
84 		do {
85 			pid = _wait4(savedpid, &pstat, 0, NULL);
86 		} while (pid == -1 && errno == EINTR);
87 		break;
88 	}
89 	_sigaction(SIGINT, &intact, NULL);
90 	_sigaction(SIGQUIT,  &quitact, NULL);
91 	_sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
92 	return(pid == -1 ? -1 : pstat);
93 }
94 
95 __weak_reference(__system, system);
96 __weak_reference(__system, _system);
97