xref: /freebsd/lib/libc/stdlib/system.c (revision 32cd3ee5901ea33d41ff550e5f40ce743c8d4165)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1988, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include "namespace.h"
33 #include <sys/types.h>
34 #include <sys/wait.h>
35 
36 #include <errno.h>
37 #include <paths.h>
38 #include <signal.h>
39 #include <stddef.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include "un-namespace.h"
44 #include "libc_private.h"
45 #include "spinlock.h"
46 
47 #pragma weak system
48 int
49 system(const char *command)
50 {
51 	return (((int (*)(const char *))
52 	    __libc_interposing[INTERPOS_system])(command));
53 }
54 
55 int
56 __libc_system(const char *command)
57 {
58 	static spinlock_t lock = _SPINLOCK_INITIALIZER;
59 	static volatile unsigned long concurrent;
60 	static struct sigaction ointact, oquitact;
61 	struct sigaction ign;
62 	sigset_t sigblock, osigblock;
63 	int pstat = -1, serrno = 0;
64 	pid_t pid;
65 
66 	if (command == NULL)			/* just checking... */
67 		return (eaccess(_PATH_BSHELL, X_OK) == 0);
68 
69 	/*
70 	 * If we are the first concurrent instance, ignore SIGINT and
71 	 * SIGQUIT.  Block SIGCHLD regardless of concurrency, since on
72 	 * FreeBSD, sigprocmask() is equivalent to pthread_sigmask().
73 	 */
74 	if (__isthreaded)
75 		_SPINLOCK(&lock);
76 	if (concurrent++ == 0) {
77 		memset(&ign, 0, sizeof(ign));
78 		ign.sa_handler = SIG_IGN;
79 		sigemptyset(&ign.sa_mask);
80 		(void)__libc_sigaction(SIGINT, &ign, &ointact);
81 		(void)__libc_sigaction(SIGQUIT, &ign, &oquitact);
82 	}
83 	sigemptyset(&sigblock);
84 	sigaddset(&sigblock, SIGCHLD);
85 	(void)__libc_sigprocmask(SIG_BLOCK, &sigblock, &osigblock);
86 	if (__isthreaded)
87 		_SPINUNLOCK(&lock);
88 
89 	/*
90 	 * Fork the child process.
91 	 */
92 	if ((pid = fork()) < 0) {		/* error */
93 		serrno = errno;
94 	} else if (pid == 0) {			/* child */
95 		/*
96 		 * Restore original signal dispositions.
97 		 */
98 		(void)__libc_sigaction(SIGINT, &ointact, NULL);
99 		(void)__libc_sigaction(SIGQUIT,  &oquitact, NULL);
100 		(void)__sys_sigprocmask(SIG_SETMASK, &osigblock, NULL);
101 		/*
102 		 * Exec the command.
103 		 */
104 		execl(_PATH_BSHELL, "sh", "-c", command, NULL);
105 		_exit(127);
106 	} else {				/* parent */
107 		/*
108 		 * Wait for the child to terminate.
109 		 */
110 		while (_wait4(pid, &pstat, 0, NULL) < 0) {
111 			if (errno != EINTR) {
112 				serrno = errno;
113 				break;
114 			}
115 		}
116 	}
117 
118 	/*
119 	 * If we are the last concurrent instance, restore original signal
120 	 * dispositions.  Unblock SIGCHLD, unless it was already blocked.
121 	 */
122 	if (__isthreaded)
123 		_SPINLOCK(&lock);
124 	if (--concurrent == 0) {
125 		(void)__libc_sigaction(SIGINT, &ointact, NULL);
126 		(void)__libc_sigaction(SIGQUIT,  &oquitact, NULL);
127 	}
128 	if (!sigismember(&osigblock, SIGCHLD))
129 		(void)__libc_sigprocmask(SIG_UNBLOCK, &sigblock, NULL);
130 	if (__isthreaded)
131 		_SPINUNLOCK(&lock);
132 	if (serrno != 0)
133 		errno = serrno;
134 	return (pstat);
135 }
136 
137 __weak_reference(__libc_system, __system);
138 __weak_reference(__libc_system, _system);
139