Print this page
8158 Want named threads API
9857 proc manpages should have LIBRARY section
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/svc/startd/wait.c
+++ new/usr/src/cmd/svc/startd/wait.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
↓ open down ↓ |
13 lines elided |
↑ open up ↑ |
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 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 - * Copyright 2012, Joyent, Inc. All rights reserved.
24 + *
25 + * Copyright 2018 Joyent, Inc.
25 26 */
26 27
27 28 /*
28 29 * wait.c - asynchronous monitoring of "wait registered" start methods
29 30 *
30 31 * Use event ports to poll on the set of fds representing the /proc/[pid]/psinfo
31 32 * files. If one of these fds returns an event, then we inform the restarter
32 33 * that it has stopped.
33 34 *
34 35 * The wait_info_list holds the series of processes currently being monitored
35 36 * for exit. The wi_fd member, which contains the file descriptor of the psinfo
36 37 * file being polled upon ("event ported upon"), will be set to -1 if the file
37 38 * descriptor is inactive (already closed or not yet opened).
38 39 */
39 40
40 41 #ifdef _FILE_OFFSET_BITS
41 42 #undef _FILE_OFFSET_BITS
42 43 #endif /* _FILE_OFFSET_BITS */
43 44
44 45 #include <sys/resource.h>
45 46 #include <sys/stat.h>
46 47 #include <sys/types.h>
47 48 #include <sys/uio.h>
48 49 #include <sys/wait.h>
49 50
50 51 #include <assert.h>
51 52 #include <errno.h>
52 53 #include <fcntl.h>
53 54 #include <libuutil.h>
54 55 #include <poll.h>
55 56 #include <port.h>
56 57 #include <pthread.h>
57 58 #include <procfs.h>
58 59 #include <string.h>
59 60 #include <stropts.h>
60 61 #include <unistd.h>
61 62
62 63 #include "startd.h"
63 64
64 65 #define WAIT_FILES 262144 /* reasonably high maximum */
65 66
66 67 static int port_fd;
67 68 static scf_handle_t *wait_hndl;
68 69 static struct rlimit init_fd_rlimit;
69 70
70 71 static uu_list_pool_t *wait_info_pool;
71 72 static uu_list_t *wait_info_list;
72 73
73 74 static pthread_mutex_t wait_info_lock;
74 75
75 76 /*
76 77 * void wait_remove(wait_info_t *, int)
77 78 * Remove the given wait_info structure from our list, performing various
78 79 * cleanup operations along the way. If the direct flag is false (meaning
79 80 * that we are being called with from restarter instance list context) and
80 81 * the instance should not be ignored, then notify the restarter that the
81 82 * associated instance has exited. If the wi_ignore flag is true then it
82 83 * means that the stop was initiated from within svc.startd, rather than
83 84 * from outside it.
84 85 *
85 86 * Since we may no longer be the startd that started this process, we only are
86 87 * concerned with a waitpid(3C) failure if the wi_parent field is non-zero.
87 88 */
88 89 static void
89 90 wait_remove(wait_info_t *wi, int direct)
90 91 {
91 92 int status;
92 93 stop_cause_t cause = RSTOP_EXIT;
93 94
94 95 if (waitpid(wi->wi_pid, &status, 0) == -1) {
95 96 if (wi->wi_parent)
96 97 log_framework(LOG_INFO,
97 98 "instance %s waitpid failure: %s\n", wi->wi_fmri,
98 99 strerror(errno));
99 100 } else {
100 101 if (WEXITSTATUS(status) != 0) {
101 102 log_framework(LOG_NOTICE,
102 103 "instance %s exited with status %d\n", wi->wi_fmri,
103 104 WEXITSTATUS(status));
104 105 if (WEXITSTATUS(status) == SMF_EXIT_ERR_CONFIG)
105 106 cause = RSTOP_ERR_CFG;
106 107 else
107 108 cause = RSTOP_ERR_EXIT;
108 109 }
109 110 }
110 111
111 112 MUTEX_LOCK(&wait_info_lock);
112 113 if (wi->wi_fd != -1) {
113 114 startd_close(wi->wi_fd);
114 115 wi->wi_fd = -1;
115 116 }
116 117 uu_list_remove(wait_info_list, wi);
117 118 MUTEX_UNLOCK(&wait_info_lock);
118 119
119 120 /*
120 121 * Make an attempt to clear out any utmpx record associated with this
121 122 * PID.
122 123 */
123 124 utmpx_mark_dead(wi->wi_pid, status, B_FALSE);
124 125
125 126 if (!direct && !wi->wi_ignore) {
126 127 /*
127 128 * Bind wait_hndl lazily.
128 129 */
129 130 if (wait_hndl == NULL) {
130 131 for (wait_hndl =
131 132 libscf_handle_create_bound(SCF_VERSION);
132 133 wait_hndl == NULL;
133 134 wait_hndl =
134 135 libscf_handle_create_bound(SCF_VERSION)) {
135 136 log_error(LOG_INFO, "[wait_remove] Unable to "
136 137 "bind a new repository handle: %s\n",
137 138 scf_strerror(scf_error()));
138 139 (void) sleep(2);
139 140 }
140 141 }
141 142
142 143 log_framework(LOG_DEBUG,
143 144 "wait_remove requesting stop of %s\n", wi->wi_fmri);
144 145 (void) stop_instance_fmri(wait_hndl, wi->wi_fmri, cause);
145 146 }
146 147
147 148 uu_list_node_fini(wi, &wi->wi_link, wait_info_pool);
148 149 startd_free(wi, sizeof (wait_info_t));
149 150 }
150 151
151 152 /*
152 153 * void wait_ignore_by_fmri(const char *)
153 154 * wait_ignore_by_fmri is called when svc.startd is going to stop the
154 155 * instance. Since we need to wait on the process and close the utmpx record,
155 156 * we're going to set the wi_ignore flag, so that when the process exits we
156 157 * clean up, but don't tell the restarter to stop it.
157 158 */
158 159 void
159 160 wait_ignore_by_fmri(const char *fmri)
160 161 {
161 162 wait_info_t *wi;
162 163
163 164 MUTEX_LOCK(&wait_info_lock);
164 165
165 166 for (wi = uu_list_first(wait_info_list); wi != NULL;
166 167 wi = uu_list_next(wait_info_list, wi)) {
167 168 if (strcmp(wi->wi_fmri, fmri) == 0)
168 169 break;
169 170 }
170 171
171 172 if (wi != NULL) {
172 173 wi->wi_ignore = 1;
173 174 }
174 175
175 176 MUTEX_UNLOCK(&wait_info_lock);
176 177 }
177 178
178 179 /*
179 180 * int wait_register(pid_t, char *, int, int)
180 181 * wait_register is called after we have called fork(2), and know which pid we
181 182 * wish to monitor. However, since the child may have already exited by the
182 183 * time we are called, we must handle the error cases from open(2)
183 184 * appropriately. The am_parent flag is recorded to handle waitpid(2)
184 185 * behaviour on removal; similarly, the direct flag is passed through to a
185 186 * potential call to wait_remove() to govern its behaviour in different
186 187 * contexts.
187 188 *
188 189 * Returns 0 if registration successful, 1 if child pid did not exist, and -1
189 190 * if a different error occurred.
190 191 */
191 192 int
192 193 wait_register(pid_t pid, const char *inst_fmri, int am_parent, int direct)
193 194 {
194 195 char *fname = uu_msprintf("/proc/%ld/psinfo", pid);
195 196 int fd;
196 197 wait_info_t *wi;
197 198
198 199 assert(pid != 0);
199 200
200 201 if (fname == NULL)
201 202 return (-1);
202 203
203 204 wi = startd_alloc(sizeof (wait_info_t));
204 205
205 206 uu_list_node_init(wi, &wi->wi_link, wait_info_pool);
206 207
207 208 wi->wi_fd = -1;
208 209 wi->wi_pid = pid;
209 210 wi->wi_fmri = inst_fmri;
210 211 wi->wi_parent = am_parent;
211 212 wi->wi_ignore = 0;
212 213
213 214 MUTEX_LOCK(&wait_info_lock);
214 215 (void) uu_list_insert_before(wait_info_list, NULL, wi);
215 216 MUTEX_UNLOCK(&wait_info_lock);
216 217
217 218 if ((fd = open(fname, O_RDONLY)) == -1) {
218 219 if (errno == ENOENT) {
219 220 /*
220 221 * Child has already exited.
221 222 */
222 223 wait_remove(wi, direct);
223 224 uu_free(fname);
224 225 return (1);
225 226 } else {
226 227 log_error(LOG_WARNING,
227 228 "open %s failed; not monitoring %s: %s\n", fname,
228 229 inst_fmri, strerror(errno));
229 230 uu_free(fname);
230 231 return (-1);
231 232 }
232 233 }
233 234
234 235 uu_free(fname);
235 236
236 237 wi->wi_fd = fd;
237 238
238 239 if (port_associate(port_fd, PORT_SOURCE_FD, fd, 0, wi)) {
239 240 log_error(LOG_WARNING,
240 241 "initial port_association of %d / %s failed: %s\n", fd,
241 242 inst_fmri, strerror(errno));
242 243 return (-1);
243 244 }
244 245
↓ open down ↓ |
210 lines elided |
↑ open up ↑ |
245 246 log_framework(LOG_DEBUG, "monitoring PID %ld on fd %d (%s)\n", pid, fd,
246 247 inst_fmri);
247 248
248 249 return (0);
249 250 }
250 251
251 252 /*ARGSUSED*/
252 253 void *
253 254 wait_thread(void *args)
254 255 {
256 + (void) pthread_setname_np(pthread_self(), "wait");
257 +
255 258 for (;;) {
256 259 port_event_t pe;
257 260 int fd;
258 261 wait_info_t *wi;
259 262
260 263 if (port_get(port_fd, &pe, NULL) != 0) {
261 264 if (errno == EINTR)
262 265 continue;
263 266 else {
264 267 log_error(LOG_WARNING,
265 268 "port_get() failed with %s\n",
266 269 strerror(errno));
267 270 bad_error("port_get", errno);
268 271 }
269 272 }
270 273
271 274 fd = pe.portev_object;
272 275 wi = pe.portev_user;
273 276 assert(wi != NULL);
274 277 assert(fd == wi->wi_fd);
275 278
276 279 if ((pe.portev_events & POLLHUP) == POLLHUP) {
277 280 psinfo_t psi;
278 281
279 282 if (lseek(fd, 0, SEEK_SET) != 0 ||
280 283 read(fd, &psi, sizeof (psinfo_t)) !=
281 284 sizeof (psinfo_t)) {
282 285 log_framework(LOG_WARNING,
283 286 "couldn't get psinfo data for %s (%s); "
284 287 "assuming failed\n", wi->wi_fmri,
285 288 strerror(errno));
286 289 goto err_remove;
287 290 }
288 291
289 292 if (psi.pr_nlwp != 0 ||
290 293 psi.pr_nzomb != 0 ||
291 294 psi.pr_lwp.pr_lwpid != 0) {
292 295 /*
293 296 * We have determined, in accordance with the
294 297 * definition in proc(4), this process is not a
295 298 * zombie. Reassociate.
296 299 */
297 300 if (port_associate(port_fd, PORT_SOURCE_FD, fd,
298 301 0, wi))
299 302 log_error(LOG_WARNING,
300 303 "port_association of %d / %s "
301 304 "failed\n", fd, wi->wi_fmri);
302 305 continue;
303 306 }
304 307 } else if (
305 308 (pe.portev_events & POLLERR) == 0) {
306 309 if (port_associate(port_fd, PORT_SOURCE_FD, fd, 0, wi))
307 310 log_error(LOG_WARNING,
308 311 "port_association of %d / %s "
309 312 "failed\n", fd, wi->wi_fmri);
310 313 continue;
311 314 }
312 315
313 316 err_remove:
314 317 wait_remove(wi, 0);
315 318 }
316 319
317 320 /*LINTED E_FUNC_HAS_NO_RETURN_STMT*/
318 321 }
319 322
320 323 void
321 324 wait_prefork()
322 325 {
323 326 MUTEX_LOCK(&wait_info_lock);
324 327 }
325 328
326 329 void
327 330 wait_postfork(pid_t pid)
328 331 {
329 332 wait_info_t *wi;
330 333
331 334 MUTEX_UNLOCK(&wait_info_lock);
332 335
333 336 if (pid != 0)
334 337 return;
335 338
336 339 /*
337 340 * Close all of the child's wait-related fds. The wait_thread() is
338 341 * gone, so no need to worry about returning events. We always exec(2)
339 342 * after a fork request, so we needn't free the list elements
340 343 * themselves.
341 344 */
342 345
343 346 for (wi = uu_list_first(wait_info_list);
344 347 wi != NULL;
345 348 wi = uu_list_next(wait_info_list, wi)) {
346 349 if (wi->wi_fd != -1)
347 350 startd_close(wi->wi_fd);
348 351 }
349 352
350 353 startd_close(port_fd);
351 354
352 355 (void) setrlimit(RLIMIT_NOFILE, &init_fd_rlimit);
353 356 }
354 357
355 358 void
356 359 wait_init()
357 360 {
358 361 struct rlimit fd_new;
359 362
360 363 (void) getrlimit(RLIMIT_NOFILE, &init_fd_rlimit);
361 364 (void) getrlimit(RLIMIT_NOFILE, &fd_new);
362 365
363 366 fd_new.rlim_max = fd_new.rlim_cur = WAIT_FILES;
364 367
365 368 (void) setrlimit(RLIMIT_NOFILE, &fd_new);
366 369
367 370 if ((port_fd = port_create()) == -1)
368 371 uu_die("wait_init couldn't port_create");
369 372
370 373 wait_info_pool = uu_list_pool_create("wait_info", sizeof (wait_info_t),
371 374 offsetof(wait_info_t, wi_link), NULL, UU_LIST_POOL_DEBUG);
372 375 if (wait_info_pool == NULL)
373 376 uu_die("wait_init couldn't create wait_info_pool");
374 377
375 378 wait_info_list = uu_list_create(wait_info_pool, wait_info_list, 0);
376 379 if (wait_info_list == NULL)
377 380 uu_die("wait_init couldn't create wait_info_list");
378 381
379 382 (void) pthread_mutex_init(&wait_info_lock, &mutex_attrs);
380 383 }
↓ open down ↓ |
116 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX