summaryrefslogtreecommitdiff
path: root/usr/src/cmd/svc/startd/wait.c
blob: 12856ff639d020e0e7b06bda135b65fb69b28d09 (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
 * 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 2018 Joyent, Inc.
 */

/*
 * wait.c - asynchronous monitoring of "wait registered" start methods
 *
 * Use event ports to poll on the set of fds representing the /proc/[pid]/psinfo
 * files.  If one of these fds returns an event, then we inform the restarter
 * that it has stopped.
 *
 * The wait_info_list holds the series of processes currently being monitored
 * for exit.  The wi_fd member, which contains the file descriptor of the psinfo
 * file being polled upon ("event ported upon"), will be set to -1 if the file
 * descriptor is inactive (already closed or not yet opened).
 */

#ifdef _FILE_OFFSET_BITS
#undef _FILE_OFFSET_BITS
#endif /* _FILE_OFFSET_BITS */

#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/wait.h>

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <libuutil.h>
#include <poll.h>
#include <port.h>
#include <pthread.h>
#include <procfs.h>
#include <string.h>
#include <stropts.h>
#include <unistd.h>

#include "startd.h"

#define	WAIT_FILES	262144		/* reasonably high maximum */

static int port_fd;
static scf_handle_t *wait_hndl;
static struct rlimit init_fd_rlimit;

static uu_list_pool_t *wait_info_pool;
static uu_list_t *wait_info_list;

static pthread_mutex_t wait_info_lock;

/*
 * void wait_remove(wait_info_t *, int)
 *   Remove the given wait_info structure from our list, performing various
 *   cleanup operations along the way.  If the direct flag is false (meaning
 *   that we are being called with from restarter instance list context) and
 *   the instance should not be ignored, then notify the restarter that the
 *   associated instance has exited. If the wi_ignore flag is true then it
 *   means that the stop was initiated from within svc.startd, rather than
 *   from outside it.
 *
 *   Since we may no longer be the startd that started this process, we only are
 *   concerned with a waitpid(3C) failure if the wi_parent field is non-zero.
 */
static void
wait_remove(wait_info_t *wi, int direct)
{
	int status;
	stop_cause_t cause = RSTOP_EXIT;

	if (waitpid(wi->wi_pid, &status, 0) == -1) {
		if (wi->wi_parent)
			log_framework(LOG_INFO,
			    "instance %s waitpid failure: %s\n", wi->wi_fmri,
			    strerror(errno));
	} else {
		if (WEXITSTATUS(status) != 0) {
			log_framework(LOG_NOTICE,
			    "instance %s exited with status %d\n", wi->wi_fmri,
			    WEXITSTATUS(status));
			if (WEXITSTATUS(status) == SMF_EXIT_ERR_CONFIG)
				cause = RSTOP_ERR_CFG;
			else
				cause = RSTOP_ERR_EXIT;
		}
	}

	MUTEX_LOCK(&wait_info_lock);
	if (wi->wi_fd != -1) {
		startd_close(wi->wi_fd);
		wi->wi_fd = -1;
	}
	uu_list_remove(wait_info_list, wi);
	MUTEX_UNLOCK(&wait_info_lock);

	/*
	 * Make an attempt to clear out any utmpx record associated with this
	 * PID.
	 */
	utmpx_mark_dead(wi->wi_pid, status, B_FALSE);

	if (!direct && !wi->wi_ignore) {
		/*
		 * Bind wait_hndl lazily.
		 */
		if (wait_hndl == NULL) {
			for (wait_hndl =
			    libscf_handle_create_bound(SCF_VERSION);
			    wait_hndl == NULL;
			    wait_hndl =
			    libscf_handle_create_bound(SCF_VERSION)) {
				log_error(LOG_INFO, "[wait_remove] Unable to "
				    "bind a new repository handle: %s\n",
				    scf_strerror(scf_error()));
				(void) sleep(2);
			}
		}

		log_framework(LOG_DEBUG,
		    "wait_remove requesting stop of %s\n", wi->wi_fmri);
		(void) stop_instance_fmri(wait_hndl, wi->wi_fmri, cause);
	}

	uu_list_node_fini(wi, &wi->wi_link, wait_info_pool);
	startd_free(wi, sizeof (wait_info_t));
}

/*
 * void wait_ignore_by_fmri(const char *)
 *   wait_ignore_by_fmri is called when svc.startd is going to stop the
 *   instance. Since we need to wait on the process and close the utmpx record,
 *   we're going to set the wi_ignore flag, so that when the process exits we
 *   clean up, but don't tell the restarter to stop it.
 */
void
wait_ignore_by_fmri(const char *fmri)
{
	wait_info_t *wi;

	MUTEX_LOCK(&wait_info_lock);

	for (wi = uu_list_first(wait_info_list); wi != NULL;
	    wi = uu_list_next(wait_info_list, wi)) {
		if (strcmp(wi->wi_fmri, fmri) == 0)
			break;
	}

	if (wi != NULL) {
		wi->wi_ignore = 1;
	}

	MUTEX_UNLOCK(&wait_info_lock);
}

/*
 * int wait_register(pid_t, char *, int, int)
 *   wait_register is called after we have called fork(2), and know which pid we
 *   wish to monitor.  However, since the child may have already exited by the
 *   time we are called, we must handle the error cases from open(2)
 *   appropriately.  The am_parent flag is recorded to handle waitpid(2)
 *   behaviour on removal; similarly, the direct flag is passed through to a
 *   potential call to wait_remove() to govern its behaviour in different
 *   contexts.
 *
 *   Returns 0 if registration successful, 1 if child pid did not exist, and -1
 *   if a different error occurred.
 */
int
wait_register(pid_t pid, const char *inst_fmri, int am_parent, int direct)
{
	char *fname = uu_msprintf("/proc/%ld/psinfo", pid);
	int fd;
	wait_info_t *wi;

	assert(pid != 0);

	if (fname == NULL)
		return (-1);

	wi = startd_alloc(sizeof (wait_info_t));

	uu_list_node_init(wi, &wi->wi_link, wait_info_pool);

	wi->wi_fd = -1;
	wi->wi_pid = pid;
	wi->wi_fmri = inst_fmri;
	wi->wi_parent = am_parent;
	wi->wi_ignore = 0;

	MUTEX_LOCK(&wait_info_lock);
	(void) uu_list_insert_before(wait_info_list, NULL, wi);
	MUTEX_UNLOCK(&wait_info_lock);

	if ((fd = open(fname, O_RDONLY)) == -1) {
		if (errno == ENOENT) {
			/*
			 * Child has already exited.
			 */
			wait_remove(wi, direct);
			uu_free(fname);
			return (1);
		} else {
			log_error(LOG_WARNING,
			    "open %s failed; not monitoring %s: %s\n", fname,
			    inst_fmri, strerror(errno));
			uu_free(fname);
			return (-1);
		}
	}

	uu_free(fname);

	wi->wi_fd = fd;

	if (port_associate(port_fd, PORT_SOURCE_FD, fd, 0, wi)) {
		log_error(LOG_WARNING,
		    "initial port_association of %d / %s failed: %s\n", fd,
		    inst_fmri, strerror(errno));
		return (-1);
	}

	log_framework(LOG_DEBUG, "monitoring PID %ld on fd %d (%s)\n", pid, fd,
	    inst_fmri);

	return (0);
}

/*ARGSUSED*/
void *
wait_thread(void *args)
{
	(void) pthread_setname_np(pthread_self(), "wait");

	for (;;) {
		port_event_t pe;
		int fd;
		wait_info_t *wi;

		if (port_get(port_fd, &pe, NULL) != 0) {
			if (errno == EINTR)
				continue;
			else {
				log_error(LOG_WARNING,
				    "port_get() failed with %s\n",
				    strerror(errno));
				bad_error("port_get", errno);
			}
		}

		fd = pe.portev_object;
		wi = pe.portev_user;
		assert(wi != NULL);
		assert(fd == wi->wi_fd);

		if ((pe.portev_events & POLLHUP) == POLLHUP) {
			psinfo_t psi;

			if (lseek(fd, 0, SEEK_SET) != 0 ||
			    read(fd, &psi, sizeof (psinfo_t)) !=
			    sizeof (psinfo_t)) {
				log_framework(LOG_WARNING,
				    "couldn't get psinfo data for %s (%s); "
				    "assuming failed\n", wi->wi_fmri,
				    strerror(errno));
				goto err_remove;
			}

			if (psi.pr_nlwp != 0 ||
			    psi.pr_nzomb != 0 ||
			    psi.pr_lwp.pr_lwpid != 0) {
				/*
				 * We have determined, in accordance with the
				 * definition in proc(4), this process is not a
				 * zombie.  Reassociate.
				 */
				if (port_associate(port_fd, PORT_SOURCE_FD, fd,
				    0, wi))
					log_error(LOG_WARNING,
					    "port_association of %d / %s "
					    "failed\n", fd, wi->wi_fmri);
				continue;
			}
		} else if (
		    (pe.portev_events & POLLERR) == 0) {
			if (port_associate(port_fd, PORT_SOURCE_FD, fd, 0, wi))
				log_error(LOG_WARNING,
				    "port_association of %d / %s "
				    "failed\n", fd, wi->wi_fmri);
			continue;
		}

err_remove:
		wait_remove(wi, 0);
	}

	/*LINTED E_FUNC_HAS_NO_RETURN_STMT*/
}

void
wait_prefork()
{
	MUTEX_LOCK(&wait_info_lock);
}

void
wait_postfork(pid_t pid)
{
	wait_info_t *wi;

	MUTEX_UNLOCK(&wait_info_lock);

	if (pid != 0)
		return;

	/*
	 * Close all of the child's wait-related fds.  The wait_thread() is
	 * gone, so no need to worry about returning events.  We always exec(2)
	 * after a fork request, so we needn't free the list elements
	 * themselves.
	 */

	for (wi = uu_list_first(wait_info_list);
	    wi != NULL;
	    wi = uu_list_next(wait_info_list, wi)) {
		if (wi->wi_fd != -1)
			startd_close(wi->wi_fd);
	}

	startd_close(port_fd);

	(void) setrlimit(RLIMIT_NOFILE, &init_fd_rlimit);
}

void
wait_init()
{
	struct rlimit fd_new;

	(void) getrlimit(RLIMIT_NOFILE, &init_fd_rlimit);
	(void) getrlimit(RLIMIT_NOFILE, &fd_new);

	fd_new.rlim_max = fd_new.rlim_cur = WAIT_FILES;

	(void) setrlimit(RLIMIT_NOFILE, &fd_new);

	if ((port_fd = port_create()) == -1)
		uu_die("wait_init couldn't port_create");

	wait_info_pool = uu_list_pool_create("wait_info", sizeof (wait_info_t),
	    offsetof(wait_info_t, wi_link), NULL, UU_LIST_POOL_DEBUG);
	if (wait_info_pool == NULL)
		uu_die("wait_init couldn't create wait_info_pool");

	wait_info_list = uu_list_create(wait_info_pool, wait_info_list, 0);
	if (wait_info_list == NULL)
		uu_die("wait_init couldn't create wait_info_list");

	(void) pthread_mutex_init(&wait_info_lock, &mutex_attrs);
}