Print this page
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/lib/libc/port/stdio/system.c
+++ new/usr/src/lib/libc/port/stdio/system.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 * Copyright 2016 Joyent, Inc.
26 26 */
27 27
28 28 /* Copyright (c) 1988 AT&T */
29 29 /* All Rights Reserved */
30 30
31 31 #include "lint.h"
32 32 #include "mtlib.h"
33 33 #include <sys/types.h>
34 34 #include <sys/wait.h>
35 35 #include <signal.h>
36 36 #include <stdlib.h>
37 37 #include <wait.h>
38 38 #include <sys/stat.h>
39 39 #include <unistd.h>
40 40 #include <memory.h>
41 41 #include <thread.h>
42 42 #include <pthread.h>
43 43 #include <errno.h>
44 44 #include <synch.h>
45 45 #include <spawn.h>
46 46 #include <paths.h>
47 47 #include <zone.h>
48 48 #include "libc.h"
49 49
50 50 extern const char **_environ;
51 51
52 52 extern int __xpg4; /* defined in _xpg4.c; 0 if not xpg4-compiled program */
53 53 extern const sigset_t maskset; /* all maskable signals */
54 54
55 55 static mutex_t sys_lock = DEFAULTMUTEX; /* protects the following */
56 56 static uint_t sys_count = 0; /* number of threads in system() */
57 57 static struct sigaction sys_ibuf; /* saved SIGINT sigaction */
58 58 static struct sigaction sys_qbuf; /* saved SIGQUIT sigaction */
59 59 static struct sigaction ignore = {0, {SIG_IGN}, {0}};
60 60
61 61 /*
62 62 * Things needed by the cancellation cleanup handler.
63 63 */
64 64 typedef struct {
65 65 sigset_t savemask; /* saved signal mask */
66 66 pid_t pid; /* if nonzero, the child's pid */
67 67 } cleanup_t;
68 68
69 69 /*
70 70 * Daemon thread whose sole function is to reap an abandoned child.
71 71 * Also invoked from pclose() (see port/stdio/popen.c).
72 72 */
73 73 void *
74 74 reapchild(void *arg)
75 75 {
76 76 pid_t pid = (pid_t)(uintptr_t)arg;
77 77 int cancel_state;
78 78
79 79 (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
80 80 while (waitpid(pid, NULL, 0) == -1) {
81 81 if (errno != EINTR)
82 82 break;
83 83 }
84 84 (void) pthread_setcancelstate(cancel_state, NULL);
85 85 return (NULL);
86 86 }
87 87
88 88 /*
89 89 * Cancellation cleanup handler.
90 90 * If we were cancelled in waitpid(), create a daemon thread to
91 91 * reap our abandoned child. No other thread can do this for us.
92 92 * It would be better if there were a system call to disinherit
93 93 * a child process (give it to init, just as though we exited).
94 94 */
95 95 static void
96 96 cleanup(void *arg)
97 97 {
98 98 cleanup_t *cup = arg;
99 99
100 100 if (cup->pid != 0) { /* we were cancelled; abandoning our pid */
101 101 (void) thr_sigsetmask(SIG_SETMASK, &maskset, NULL);
102 102 (void) thr_create(NULL, 0,
103 103 reapchild, (void *)(uintptr_t)cup->pid,
104 104 THR_DAEMON, NULL);
105 105 }
106 106
107 107 lmutex_lock(&sys_lock);
108 108 if (--sys_count == 0) { /* leaving system() */
109 109 /*
110 110 * There are no remaining threads in system(), so
111 111 * restore the SIGINT and SIGQUIT signal actions.
112 112 */
113 113 (void) sigaction(SIGINT, &sys_ibuf, NULL);
114 114 (void) sigaction(SIGQUIT, &sys_qbuf, NULL);
115 115 }
116 116 lmutex_unlock(&sys_lock);
117 117
118 118 (void) thr_sigsetmask(SIG_SETMASK, &cup->savemask, NULL);
119 119 }
120 120
121 121 int
122 122 system(const char *cmd)
123 123 {
124 124 cleanup_t cu;
125 125 pid_t w;
126 126 int status;
127 127 int error;
128 128 sigset_t mask;
129 129 struct stat64 buf;
130 130 char shpath[MAXPATHLEN];
131 131 const char *zroot = zone_get_nroot();
132 132 char *argv[4];
133 133 posix_spawnattr_t attr;
134 134 static const char *shell = "sh";
135 135
136 136 /*
137 137 * If executing in brand use native root.
138 138 */
139 139 (void) snprintf(shpath, sizeof (shpath), "%s%s",
140 140 zroot != NULL ? zroot : "", _PATH_BSHELL);
141 141
142 142 if (cmd == NULL) {
143 143 if (stat64(shpath, &buf) != 0) {
144 144 return (0);
145 145 } else if (getuid() == buf.st_uid) {
146 146 /* exec for user */
147 147 if ((buf.st_mode & 0100) == 0)
148 148 return (0);
149 149 } else if (getgid() == buf.st_gid) {
150 150 /* exec for group */
151 151 if ((buf.st_mode & 0010) == 0)
152 152 return (0);
153 153 } else if ((buf.st_mode & 0001) == 0) { /* exec for others */
154 154 return (0);
155 155 }
156 156 return (1);
157 157 }
158 158
159 159 /*
160 160 * Initialize the posix_spawn() attributes structure.
161 161 *
162 162 * The setting of POSIX_SPAWN_WAITPID_NP ensures that no
163 163 * wait-for-multiple wait() operation will reap our child
164 164 * and that the child will not be automatically reaped due
165 165 * to the disposition of SIGCHLD being set to be ignored.
166 166 * Only a specific wait for the specific pid will be able
167 167 * to reap the child. Since no other thread knows the pid
168 168 * of our child, this should be safe enough.
169 169 *
170 170 * The POSIX_SPAWN_NOEXECERR_NP flag tells posix_spawn() not
171 171 * to fail if the shell cannot be executed, but rather cause
172 172 * a child to be created that simply performs _exit(127).
173 173 * This is in order to satisfy the Posix requirement on system():
174 174 * The system function shall behave as if a child process were
175 175 * created using fork(), and the child process invoked the sh
176 176 * utility using execl(). If some error prevents the command
177 177 * language interpreter from executing after the child process
178 178 * is created, the return value from system() shall be as if
179 179 * the command language interpreter had terminated using
180 180 * exit(127) or _exit(127).
181 181 */
182 182 error = posix_spawnattr_init(&attr);
183 183 if (error == 0)
184 184 error = posix_spawnattr_setflags(&attr,
185 185 POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF |
186 186 POSIX_SPAWN_NOSIGCHLD_NP | POSIX_SPAWN_WAITPID_NP |
187 187 POSIX_SPAWN_NOEXECERR_NP);
188 188
189 189 /*
190 190 * The POSIX spec for system() requires us to block SIGCHLD,
191 191 * the rationale being that the process's signal handler for
192 192 * SIGCHLD, if any, should not be called when our child exits.
193 193 * This doesn't work for a multithreaded process because some
194 194 * other thread could receive the SIGCHLD.
195 195 *
196 196 * The above setting of POSIX_SPAWN_NOSIGCHLD_NP ensures that no
197 197 * SIGCHLD signal will be posted for our child when it exits, so
198 198 * we don't have to block SIGCHLD to meet the intent of the spec.
199 199 * We block SIGCHLD anyway, just because the spec requires it.
200 200 */
201 201 (void) sigemptyset(&mask);
202 202 (void) sigaddset(&mask, SIGCHLD);
203 203 (void) thr_sigsetmask(SIG_BLOCK, &mask, &cu.savemask);
204 204 /*
205 205 * Tell posix_spawn() to restore the signal mask in the child.
206 206 */
207 207 if (error == 0)
208 208 error = posix_spawnattr_setsigmask(&attr, &cu.savemask);
209 209
210 210 /*
211 211 * We are required to set the disposition of SIGINT and SIGQUIT
212 212 * to be ignored for the duration of the system() operation.
213 213 *
214 214 * We allow more than one thread to call system() concurrently by
215 215 * keeping a count of such threads. The signal actions are set
216 216 * to SIG_IGN when the first thread calls system(). They are
217 217 * restored in cleanup() when the last thread exits system().
218 218 *
219 219 * However, system() is still MT-unsafe because sigaction() has
220 220 * a process-wide effect and some other thread may also be
221 221 * setting the signal actions for SIGINT or SIGQUIT.
222 222 */
223 223 lmutex_lock(&sys_lock);
224 224 if (sys_count++ == 0) {
225 225 (void) sigaction(SIGINT, &ignore, &sys_ibuf);
226 226 (void) sigaction(SIGQUIT, &ignore, &sys_qbuf);
227 227 }
228 228 lmutex_unlock(&sys_lock);
229 229
230 230 /*
231 231 * If SIGINT and SIGQUIT were not already SIG_IGN, tell
232 232 * posix_spawn() to make them SIG_DFL in the child,
233 233 * else leave them as SIG_IGN in the child.
234 234 */
235 235 (void) sigemptyset(&mask);
236 236 if (sys_ibuf.sa_handler != SIG_IGN)
237 237 (void) sigaddset(&mask, SIGINT);
238 238 if (sys_qbuf.sa_handler != SIG_IGN)
239 239 (void) sigaddset(&mask, SIGQUIT);
240 240 if (error == 0)
241 241 error = posix_spawnattr_setsigdefault(&attr, &mask);
242 242
243 243 argv[0] = (char *)shell;
244 244 argv[1] = "-c";
245 245 argv[2] = (char *)cmd;
246 246 argv[3] = NULL;
247 247 if (error == 0)
248 248 error = posix_spawn(&cu.pid, shpath, NULL, &attr,
249 249 (char *const *)argv, (char *const *)_environ);
250 250
251 251 (void) posix_spawnattr_destroy(&attr);
252 252
253 253 if (error) {
254 254 errno = error;
255 255 status = -1;
256 256 } else {
257 257 /*
258 258 * system() is a cancellation point and so is waitpid().
259 259 */
260 260 pthread_cleanup_push(cleanup, &cu);
261 261 do {
262 262 w = waitpid(cu.pid, &status, 0);
263 263 } while (w == -1 && errno == EINTR);
264 264 pthread_cleanup_pop(0);
265 265 if (w == -1)
266 266 status = -1;
267 267 }
268 268 error = errno;
269 269 cu.pid = 0;
270 270 cleanup(&cu);
271 271 errno = error;
272 272
273 273 return (status);
274 274 }
|
↓ open down ↓ |
274 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX