xref: /freebsd/lib/libc/stdlib/system.c (revision bc531a96c9b28b1cabcd5deb0c9f8f6d815cfebc)
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 	char *argv[] = { "sh", "-c", __DECONST(char *, command), NULL };
64 	extern char **environ;
65 	int pstat = -1, serrno = 0;
66 	pid_t pid;
67 
68 	if (command == NULL)			/* just checking... */
69 		return (eaccess(_PATH_BSHELL, X_OK) == 0);
70 
71 	/*
72 	 * If we are the first concurrent instance, ignore SIGINT and
73 	 * SIGQUIT.  Block SIGCHLD regardless of concurrency, since on
74 	 * FreeBSD, sigprocmask() is equivalent to pthread_sigmask().
75 	 */
76 	if (__isthreaded)
77 		_SPINLOCK(&lock);
78 	if (concurrent++ == 0) {
79 		memset(&ign, 0, sizeof(ign));
80 		ign.sa_handler = SIG_IGN;
81 		sigemptyset(&ign.sa_mask);
82 		(void)__libc_sigaction(SIGINT, &ign, &ointact);
83 		(void)__libc_sigaction(SIGQUIT, &ign, &oquitact);
84 	}
85 	sigemptyset(&sigblock);
86 	sigaddset(&sigblock, SIGCHLD);
87 	(void)__libc_sigprocmask(SIG_BLOCK, &sigblock, &osigblock);
88 	if (__isthreaded)
89 		_SPINUNLOCK(&lock);
90 
91 	/*
92 	 * Fork the child process.
93 	 */
94 	if ((pid = fork()) < 0) {		/* error */
95 		serrno = errno;
96 	} else if (pid == 0) {			/* child */
97 		/*
98 		 * Restore original signal dispositions.
99 		 */
100 		(void)__libc_sigaction(SIGINT, &ointact, NULL);
101 		(void)__libc_sigaction(SIGQUIT,  &oquitact, NULL);
102 		(void)__sys_sigprocmask(SIG_SETMASK, &osigblock, NULL);
103 		/*
104 		 * Exec the command.
105 		 */
106 		_execve(_PATH_BSHELL, argv, environ);
107 		_exit(127);
108 	} else {				/* parent */
109 		/*
110 		 * Wait for the child to terminate.
111 		 */
112 		while (_wait4(pid, &pstat, 0, NULL) < 0) {
113 			if (errno != EINTR) {
114 				serrno = errno;
115 				break;
116 			}
117 		}
118 	}
119 
120 	/*
121 	 * If we are the last concurrent instance, restore original signal
122 	 * dispositions.  Unblock SIGCHLD, unless it was already blocked.
123 	 */
124 	if (__isthreaded)
125 		_SPINLOCK(&lock);
126 	if (--concurrent == 0) {
127 		(void)__libc_sigaction(SIGINT, &ointact, NULL);
128 		(void)__libc_sigaction(SIGQUIT,  &oquitact, NULL);
129 	}
130 	if (!sigismember(&osigblock, SIGCHLD))
131 		(void)__libc_sigprocmask(SIG_UNBLOCK, &sigblock, NULL);
132 	if (__isthreaded)
133 		_SPINUNLOCK(&lock);
134 	if (serrno != 0)
135 		errno = serrno;
136 	return (pstat);
137 }
138 
139 __weak_reference(__libc_system, __system);
140 __weak_reference(__libc_system, _system);
141