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