summaryrefslogtreecommitdiff
path: root/usr/src/lib/libc/port/stdio/system.c
blob: bc7e412d52ca6aac8d104a6d2228c6cb78001aa2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1988 AT&T	*/
/*	  All Rights Reserved  	*/

#include "lint.h"
#include "mtlib.h"
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <stdlib.h>
#include <wait.h>
#include <sys/stat.h>
#include <unistd.h>
#include <memory.h>
#include <thread.h>
#include <pthread.h>
#include <errno.h>
#include <synch.h>
#include <spawn.h>
#include <paths.h>
#include "libc.h"

extern const char **_environ;

extern int __xpg4;	/* defined in _xpg4.c; 0 if not xpg4-compiled program */
extern const sigset_t maskset;		/* all maskable signals */

static mutex_t sys_lock = DEFAULTMUTEX;	/* protects the following */
static uint_t sys_count = 0;		/* number of threads in system() */
static struct sigaction sys_ibuf;	/* saved SIGINT sigaction */
static struct sigaction sys_qbuf;	/* saved SIGQUIT sigaction */
static struct sigaction ignore = {0, {SIG_IGN}, {0}};

/*
 * Things needed by the cancellation cleanup handler.
 */
typedef struct {
	sigset_t	savemask;	/* saved signal mask */
	pid_t		pid;		/* if nonzero, the child's pid */
} cleanup_t;

/*
 * Daemon thread whose sole function is to reap an abandoned child.
 * Also invoked from pclose() (see port/stdio/popen.c).
 */
void *
reapchild(void *arg)
{
	pid_t pid = (pid_t)(uintptr_t)arg;
	int cancel_state;

	(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
	while (waitpid(pid, NULL, 0) == -1) {
		if (errno != EINTR)
			break;
	}
	(void) pthread_setcancelstate(cancel_state, NULL);
	return (NULL);
}

/*
 * Cancellation cleanup handler.
 * If we were cancelled in waitpid(), create a daemon thread to
 * reap our abandoned child.  No other thread can do this for us.
 * It would be better if there were a system call to disinherit
 * a child process (give it to init, just as though we exited).
 */
static void
cleanup(void *arg)
{
	cleanup_t *cup = arg;

	if (cup->pid != 0) {	/* we were cancelled; abandoning our pid */
		(void) thr_sigsetmask(SIG_SETMASK, &maskset, NULL);
		(void) thr_create(NULL, 0,
		    reapchild, (void *)(uintptr_t)cup->pid,
		    THR_DAEMON, NULL);
	}

	lmutex_lock(&sys_lock);
	if (--sys_count == 0) {		/* leaving system() */
		/*
		 * There are no remaining threads in system(), so
		 * restore the SIGINT and SIGQUIT signal actions.
		 */
		(void) sigaction(SIGINT, &sys_ibuf, NULL);
		(void) sigaction(SIGQUIT, &sys_qbuf, NULL);
	}
	lmutex_unlock(&sys_lock);

	(void) thr_sigsetmask(SIG_SETMASK, &cup->savemask, NULL);
}

int
system(const char *cmd)
{
	cleanup_t cu;
	pid_t w;
	int status;
	int error;
	sigset_t mask;
	struct stat64 buf;
	const char *shpath = _PATH_BSHELL;
	char *argv[4];
	posix_spawnattr_t attr;
	static const char *shell = "sh";

	if (cmd == NULL) {
		if (stat64(shpath, &buf) != 0) {
			return (0);
		} else if (getuid() == buf.st_uid) {
			/* exec for user */
			if ((buf.st_mode & 0100) == 0)
				return (0);
		} else if (getgid() == buf.st_gid) {
			/* exec for group */
			if ((buf.st_mode & 0010) == 0)
				return (0);
		} else if ((buf.st_mode & 0001) == 0) {	/* exec for others */
			return (0);
		}
		return (1);
	}

	/*
	 * Initialize the posix_spawn() attributes structure.
	 *
	 * The setting of POSIX_SPAWN_WAITPID_NP ensures that no
	 * wait-for-multiple wait() operation will reap our child
	 * and that the child will not be automatically reaped due
	 * to the disposition of SIGCHLD being set to be ignored.
	 * Only a specific wait for the specific pid will be able
	 * to reap the child.  Since no other thread knows the pid
	 * of our child, this should be safe enough.
	 *
	 * The POSIX_SPAWN_NOEXECERR_NP flag tells posix_spawn() not
	 * to fail if the shell cannot be executed, but rather cause
	 * a child to be created that simply performs _exit(127).
	 * This is in order to satisfy the Posix requirement on system():
	 *	The system function shall behave as if a child process were
	 *	created using fork(), and the child process invoked the sh
	 *	utility using execl().  If some error prevents the command
	 *	language interpreter from executing after the child process
	 *	is created, the return value from system() shall be as if
	 *	the command language interpreter had terminated using
	 *	exit(127) or _exit(127).
	 */
	error = posix_spawnattr_init(&attr);
	if (error == 0)
		error = posix_spawnattr_setflags(&attr,
		    POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF |
		    POSIX_SPAWN_NOSIGCHLD_NP | POSIX_SPAWN_WAITPID_NP |
		    POSIX_SPAWN_NOEXECERR_NP);

	/*
	 * The POSIX spec for system() requires us to block SIGCHLD,
	 * the rationale being that the process's signal handler for
	 * SIGCHLD, if any, should not be called when our child exits.
	 * This doesn't work for a multithreaded process because some
	 * other thread could receive the SIGCHLD.
	 *
	 * The above setting of POSIX_SPAWN_NOSIGCHLD_NP ensures that no
	 * SIGCHLD signal will be posted for our child when it exits, so
	 * we don't have to block SIGCHLD to meet the intent of the spec.
	 * We block SIGCHLD anyway, just because the spec requires it.
	 */
	(void) sigemptyset(&mask);
	(void) sigaddset(&mask, SIGCHLD);
	(void) thr_sigsetmask(SIG_BLOCK, &mask, &cu.savemask);
	/*
	 * Tell posix_spawn() to restore the signal mask in the child.
	 */
	if (error == 0)
		error = posix_spawnattr_setsigmask(&attr, &cu.savemask);

	/*
	 * We are required to set the disposition of SIGINT and SIGQUIT
	 * to be ignored for the duration of the system() operation.
	 *
	 * We allow more than one thread to call system() concurrently by
	 * keeping a count of such threads.  The signal actions are set
	 * to SIG_IGN when the first thread calls system().  They are
	 * restored in cleanup() when the last thread exits system().
	 *
	 * However, system() is still MT-unsafe because sigaction() has
	 * a process-wide effect and some other thread may also be
	 * setting the signal actions for SIGINT or SIGQUIT.
	 */
	lmutex_lock(&sys_lock);
	if (sys_count++ == 0) {
		(void) sigaction(SIGINT, &ignore, &sys_ibuf);
		(void) sigaction(SIGQUIT, &ignore, &sys_qbuf);
	}
	lmutex_unlock(&sys_lock);

	/*
	 * If SIGINT and SIGQUIT were not already SIG_IGN, tell
	 * posix_spawn() to make them SIG_DFL in the child,
	 * else leave them as SIG_IGN in the child.
	 */
	(void) sigemptyset(&mask);
	if (sys_ibuf.sa_handler != SIG_IGN)
		(void) sigaddset(&mask, SIGINT);
	if (sys_qbuf.sa_handler != SIG_IGN)
		(void) sigaddset(&mask, SIGQUIT);
	if (error == 0)
		error = posix_spawnattr_setsigdefault(&attr, &mask);

	argv[0] = (char *)shell;
	argv[1] = "-c";
	argv[2] = (char *)cmd;
	argv[3] = NULL;
	if (error == 0)
		error = posix_spawn(&cu.pid, shpath, NULL, &attr,
		    (char *const *)argv, (char *const *)_environ);

	(void) posix_spawnattr_destroy(&attr);

	if (error) {
		errno = error;
		status = -1;
	} else {
		/*
		 * system() is a cancellation point and so is waitpid().
		 */
		pthread_cleanup_push(cleanup, &cu);
		do {
			w = waitpid(cu.pid, &status, 0);
		} while (w == -1 && errno == EINTR);
		pthread_cleanup_pop(0);
		if (w == -1)
			status = -1;
	}
	error = errno;
	cu.pid = 0;
	cleanup(&cu);
	errno = error;

	return (status);
}