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