17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5bc9ec910Snakanon  * Common Development and Distribution License (the "License").
6bc9ec910Snakanon  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22999620f6SJerry Gilliam  * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #include <unistd.h>
267c478bd9Sstevel@tonic-gate #include <stdlib.h>
277c478bd9Sstevel@tonic-gate #include <string.h>
287c478bd9Sstevel@tonic-gate #include <strings.h>
297c478bd9Sstevel@tonic-gate #include <limits.h>
307c478bd9Sstevel@tonic-gate #include <thread.h>
317c478bd9Sstevel@tonic-gate #include <wait.h>
327c478bd9Sstevel@tonic-gate #include <synch.h>
337c478bd9Sstevel@tonic-gate #include <syslog.h>
347c478bd9Sstevel@tonic-gate #include <libintl.h>
357c478bd9Sstevel@tonic-gate #include <sys/stat.h>
367c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <libsysevent.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include "sysevent_signal.h"
417c478bd9Sstevel@tonic-gate #include "../devfsadm/devfsadm.h"
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  * SLM for devfsadmd device configuration daemon
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate extern char *root_dir;
487c478bd9Sstevel@tonic-gate extern void syseventd_print();
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate sysevent_handle_t *sysevent_hp;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /* Alternate root declarations during install */
537c478bd9Sstevel@tonic-gate static int use_alt_root = 0;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate static int devfsadmdeliver_event(sysevent_t *ev, int flag);
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate static struct slm_mod_ops devfsadm_mod_ops = {
587c478bd9Sstevel@tonic-gate 	SE_MAJOR_VERSION, SE_MINOR_VERSION, 10, devfsadmdeliver_event};
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate typedef struct ev_queue {
617c478bd9Sstevel@tonic-gate 	struct ev_queue *evq_next;
627c478bd9Sstevel@tonic-gate 	sysevent_t	*evq_ev;
637c478bd9Sstevel@tonic-gate } ev_queue_t;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate static mutex_t evq_lock;
667c478bd9Sstevel@tonic-gate static cond_t evq_cv;
677c478bd9Sstevel@tonic-gate static ev_queue_t *eventq_head;
687c478bd9Sstevel@tonic-gate static ev_queue_t *eventq_tail;
697c478bd9Sstevel@tonic-gate 
70999620f6SJerry Gilliam #define	DELIVERY_FAILED	\
71999620f6SJerry Gilliam 	gettext("devfsadmd not responding, /dev may not be current")
72999620f6SJerry Gilliam 
73999620f6SJerry Gilliam #define	DELIVERY_RESUMED \
74999620f6SJerry Gilliam 	gettext("devfsadmd now responding again")
75999620f6SJerry Gilliam 
76999620f6SJerry Gilliam /*
77999620f6SJerry Gilliam  * Retry error recovery when attempting to send an event to devfsadmd
78999620f6SJerry Gilliam  */
79999620f6SJerry Gilliam #define	RETRY_DAEMON_RESTART	0
80999620f6SJerry Gilliam #define	RETRY_MSG_THRESHOLD	60
81999620f6SJerry Gilliam #define	RETRY_DAEMON_INTERVAL	60
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate static int
system1(const char * s_path,const char * s)847c478bd9Sstevel@tonic-gate system1(const char *s_path, const char *s)
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate 	struct sigaction cbuf, ibuf, qbuf, ignore, dfl;
877c478bd9Sstevel@tonic-gate 	sigset_t mask, savemask;
887c478bd9Sstevel@tonic-gate 	struct stat st;
897c478bd9Sstevel@tonic-gate 	pid_t pid;
907c478bd9Sstevel@tonic-gate 	int status, w;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	/* Check the requested command */
937c478bd9Sstevel@tonic-gate 	if (s == NULL) {
947c478bd9Sstevel@tonic-gate 		errno = EINVAL;
957c478bd9Sstevel@tonic-gate 		return (-1);
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	/* Check the ability to execute devfsadmd from this process */
997c478bd9Sstevel@tonic-gate 	if (stat(s_path, &st) < 0) {
1007c478bd9Sstevel@tonic-gate 		return (-1);
1017c478bd9Sstevel@tonic-gate 	}
1027c478bd9Sstevel@tonic-gate 	if (((geteuid() == st.st_uid) && ((st.st_mode & S_IXUSR) == 0)) ||
103999620f6SJerry Gilliam 	    ((getegid() == st.st_gid) && ((st.st_mode & S_IXGRP) == 0)) ||
104999620f6SJerry Gilliam 	    ((st.st_mode & S_IXOTH) == 0)) {
1057c478bd9Sstevel@tonic-gate 		errno = EPERM;
1067c478bd9Sstevel@tonic-gate 		return (-1);
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	/*
1107c478bd9Sstevel@tonic-gate 	 * Block SIGCHLD and set up a default handler for the duration of the
1117c478bd9Sstevel@tonic-gate 	 * system1 call.
1127c478bd9Sstevel@tonic-gate 	 */
1137c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&mask);
1147c478bd9Sstevel@tonic-gate 	(void) sigaddset(&mask, SIGCHLD);
1157c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &mask, &savemask);
1167c478bd9Sstevel@tonic-gate 	(void) memset(&dfl, 0, sizeof (dfl));
1177c478bd9Sstevel@tonic-gate 	dfl.sa_handler = SIG_DFL;
1187c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGCHLD, &dfl, &cbuf);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	/* Fork off the child process (using fork1(), because it's MT-safe) */
1217c478bd9Sstevel@tonic-gate 	switch (pid = fork1()) {
1227c478bd9Sstevel@tonic-gate 		case -1:
1237c478bd9Sstevel@tonic-gate 			/* Error */
1247c478bd9Sstevel@tonic-gate 			(void) sigaction(SIGCHLD, &cbuf, NULL);
1257c478bd9Sstevel@tonic-gate 			(void) sigprocmask(SIG_SETMASK, &savemask, NULL);
1267c478bd9Sstevel@tonic-gate 			return (-1);
1277c478bd9Sstevel@tonic-gate 		case 0:
1287c478bd9Sstevel@tonic-gate 			/* Set-up an initial signal mask for the child */
1297c478bd9Sstevel@tonic-gate 			(void) sigemptyset(&mask);
1307c478bd9Sstevel@tonic-gate 			(void) sigprocmask(SIG_SETMASK, &mask, NULL);
131bc9ec910Snakanon 			closefrom(3);
1327c478bd9Sstevel@tonic-gate 			(void) execl(s_path, s, (char *)0);
1337c478bd9Sstevel@tonic-gate 			_exit(-1);
1347c478bd9Sstevel@tonic-gate 			break;
1357c478bd9Sstevel@tonic-gate 		default:
1367c478bd9Sstevel@tonic-gate 			/* Parent */
1377c478bd9Sstevel@tonic-gate 			break;
1387c478bd9Sstevel@tonic-gate 	}
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	(void) memset(&ignore, 0, sizeof (ignore));
1417c478bd9Sstevel@tonic-gate 	ignore.sa_handler = SIG_IGN;
1427c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGINT, &ignore, &ibuf);
1437c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGQUIT, &ignore, &qbuf);
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	do {
1467c478bd9Sstevel@tonic-gate 		w = waitpid(pid, &status, 0);
1477c478bd9Sstevel@tonic-gate 	} while (w == -1 && errno == EINTR);
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGINT, &ibuf, NULL);
1507c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGQUIT, &qbuf, NULL);
1517c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGCHLD, &cbuf, NULL);
1527c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &savemask, NULL);
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	return ((w == -1)? w: status);
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate /*
1587c478bd9Sstevel@tonic-gate  * devfsadmdeliver_event - called by syseventd to deliver an event buffer.
1597c478bd9Sstevel@tonic-gate  *			The event buffer is subsequently delivered to
1607c478bd9Sstevel@tonic-gate  *			devfsadmd.  If devfsadmd, is not responding to the
1617c478bd9Sstevel@tonic-gate  *			delivery attempt, we will try to startup the
1627c478bd9Sstevel@tonic-gate  *			daemon.  MT protection is provided by syseventd
1637c478bd9Sstevel@tonic-gate  *			and the client lock.  This insures sequential
1647c478bd9Sstevel@tonic-gate  *			event delivery and protection from re-entrance.
1657c478bd9Sstevel@tonic-gate  */
1667c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1677c478bd9Sstevel@tonic-gate static int
devfsadmdeliver_event(sysevent_t * ev,int flag)1687c478bd9Sstevel@tonic-gate devfsadmdeliver_event(sysevent_t *ev, int flag)
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	int ev_size;
1717c478bd9Sstevel@tonic-gate 	ev_queue_t *new_evq;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	/* Not initialized */
1747c478bd9Sstevel@tonic-gate 	if (sysevent_hp == NULL) {
1757c478bd9Sstevel@tonic-gate 		return (0);
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	/* Quick return for uninteresting events */
1797c478bd9Sstevel@tonic-gate 	if (strcmp(sysevent_get_class_name(ev), EC_DEVFS) != 0) {
1807c478bd9Sstevel@tonic-gate 		return (0);
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	/* Queue event for delivery to devfsadmd */
1847c478bd9Sstevel@tonic-gate 	new_evq = (ev_queue_t *)calloc(1, sizeof (ev_queue_t));
1857c478bd9Sstevel@tonic-gate 	if (new_evq == NULL) {
1867c478bd9Sstevel@tonic-gate 		return (EAGAIN);
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	ev_size = sysevent_get_size(ev);
1907c478bd9Sstevel@tonic-gate 	new_evq->evq_ev = (sysevent_t *)malloc(ev_size);
1917c478bd9Sstevel@tonic-gate 	if (new_evq->evq_ev == NULL) {
1927c478bd9Sstevel@tonic-gate 		free(new_evq);
1937c478bd9Sstevel@tonic-gate 		return (EAGAIN);
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 	bcopy(ev, new_evq->evq_ev, ev_size);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&evq_lock);
1987c478bd9Sstevel@tonic-gate 	if (eventq_head == NULL) {
1997c478bd9Sstevel@tonic-gate 		eventq_head = new_evq;
2007c478bd9Sstevel@tonic-gate 	} else {
2017c478bd9Sstevel@tonic-gate 		eventq_tail->evq_next = new_evq;
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 	eventq_tail = new_evq;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	(void) cond_signal(&evq_cv);
2067c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&evq_lock);
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	return (0);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate static int cleanup;
2127c478bd9Sstevel@tonic-gate thread_t deliver_thr_id;
2137c478bd9Sstevel@tonic-gate 
214*5b784b07SToomas Soome void *
devfsadmd_deliver_thr(void * arg __unused)215*5b784b07SToomas Soome devfsadmd_deliver_thr(void *arg __unused)
2167c478bd9Sstevel@tonic-gate {
217999620f6SJerry Gilliam 	int retry = 0;
218999620f6SJerry Gilliam 	int msg_emitted = 0;
2197c478bd9Sstevel@tonic-gate 	ev_queue_t *evqp;
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&evq_lock);
2227c478bd9Sstevel@tonic-gate 	for (;;) {
2237c478bd9Sstevel@tonic-gate 		while (eventq_head == NULL) {
2247c478bd9Sstevel@tonic-gate 			(void) cond_wait(&evq_cv, &evq_lock);
2257c478bd9Sstevel@tonic-gate 			if (cleanup && eventq_head == NULL) {
2267c478bd9Sstevel@tonic-gate 				(void) cond_signal(&evq_cv);
2277c478bd9Sstevel@tonic-gate 				(void) mutex_unlock(&evq_lock);
228*5b784b07SToomas Soome 				return (NULL);
2297c478bd9Sstevel@tonic-gate 			}
2307c478bd9Sstevel@tonic-gate 		}
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 		/* Send events on to devfsadmd */
2337c478bd9Sstevel@tonic-gate 		evqp = eventq_head;
2347c478bd9Sstevel@tonic-gate 		while (evqp) {
2357c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&evq_lock);
2367c478bd9Sstevel@tonic-gate 			retry = 0;
2377c478bd9Sstevel@tonic-gate 			while (sysevent_send_event(sysevent_hp,
2387c478bd9Sstevel@tonic-gate 			    evqp->evq_ev) != 0) {
2397c478bd9Sstevel@tonic-gate 				/*
240999620f6SJerry Gilliam 				 * Invoke devfsadm to handle node creation
241999620f6SJerry Gilliam 				 * but not for an alternate root.
2427c478bd9Sstevel@tonic-gate 				 */
243999620f6SJerry Gilliam 				if (use_alt_root != 0)
2447c478bd9Sstevel@tonic-gate 					break;
245999620f6SJerry Gilliam 				/*
246999620f6SJerry Gilliam 				 * daemon unresponsive -
247999620f6SJerry Gilliam 				 * restart daemon and retry once more
248999620f6SJerry Gilliam 				 */
249999620f6SJerry Gilliam 				if ((errno == EBADF || errno == ENOENT) &&
250999620f6SJerry Gilliam 				    (retry == RETRY_DAEMON_RESTART) ||
251999620f6SJerry Gilliam 				    ((retry % RETRY_DAEMON_INTERVAL) == 0)) {
252999620f6SJerry Gilliam 					(void) system1(
253999620f6SJerry Gilliam 					    DEVFSADMD_START_PATH,
254999620f6SJerry Gilliam 					    DEVFSADMD_START);
255999620f6SJerry Gilliam 				}
256999620f6SJerry Gilliam 				if (retry == RETRY_MSG_THRESHOLD) {
257999620f6SJerry Gilliam 					syslog(LOG_ERR, DELIVERY_FAILED);
258999620f6SJerry Gilliam 					msg_emitted = 1;
2597c478bd9Sstevel@tonic-gate 				}
260999620f6SJerry Gilliam 				(void) sleep(1);
261999620f6SJerry Gilliam 				++retry;
262999620f6SJerry Gilliam 				continue;
2637c478bd9Sstevel@tonic-gate 			}
264999620f6SJerry Gilliam 
265999620f6SJerry Gilliam 			/*
266999620f6SJerry Gilliam 			 * Event delivered: remove from queue
267999620f6SJerry Gilliam 			 * and reset delivery retry state.
268999620f6SJerry Gilliam 			 */
269999620f6SJerry Gilliam 			if (msg_emitted) {
270999620f6SJerry Gilliam 				syslog(LOG_ERR, DELIVERY_RESUMED);
271999620f6SJerry Gilliam 				msg_emitted = 0;
272999620f6SJerry Gilliam 			}
273999620f6SJerry Gilliam 			retry = 0;
2747c478bd9Sstevel@tonic-gate 			(void) mutex_lock(&evq_lock);
2757c478bd9Sstevel@tonic-gate 			if (eventq_head != NULL) {
2767c478bd9Sstevel@tonic-gate 				eventq_head = eventq_head->evq_next;
2777c478bd9Sstevel@tonic-gate 				if (eventq_head == NULL)
2787c478bd9Sstevel@tonic-gate 					eventq_tail = NULL;
2797c478bd9Sstevel@tonic-gate 			}
2807c478bd9Sstevel@tonic-gate 			free(evqp->evq_ev);
2817c478bd9Sstevel@tonic-gate 			free(evqp);
2827c478bd9Sstevel@tonic-gate 			evqp = eventq_head;
2837c478bd9Sstevel@tonic-gate 		}
2847c478bd9Sstevel@tonic-gate 		if (cleanup) {
2857c478bd9Sstevel@tonic-gate 			(void) cond_signal(&evq_cv);
2867c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&evq_lock);
287*5b784b07SToomas Soome 			return (NULL);
2887c478bd9Sstevel@tonic-gate 		}
2897c478bd9Sstevel@tonic-gate 	}
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate struct slm_mod_ops *
slm_init()2957c478bd9Sstevel@tonic-gate slm_init()
2967c478bd9Sstevel@tonic-gate {
2977c478bd9Sstevel@tonic-gate 	char alt_door[MAXPATHLEN];
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	if (strcmp(root_dir, "") == 0) {
3007c478bd9Sstevel@tonic-gate 		/* Initialize the private sysevent handle */
3017c478bd9Sstevel@tonic-gate 		sysevent_hp = sysevent_open_channel_alt(DEVFSADM_SERVICE_DOOR);
3027c478bd9Sstevel@tonic-gate 	} else {
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 		/* Try alternate door during install time */
3057c478bd9Sstevel@tonic-gate 		if (snprintf(alt_door, MAXPATHLEN, "%s%s", "/tmp",
3067c478bd9Sstevel@tonic-gate 		    DEVFSADM_SERVICE_DOOR) >= MAXPATHLEN)
3077c478bd9Sstevel@tonic-gate 			return (NULL);
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 		sysevent_hp = sysevent_open_channel_alt(alt_door);
3107c478bd9Sstevel@tonic-gate 		use_alt_root = 1;
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate 	if (sysevent_hp == NULL) {
3137c478bd9Sstevel@tonic-gate 		syseventd_print(0, "Unable to allocate sysevent handle"
3147c478bd9Sstevel@tonic-gate 		    " for devfsadm module\n");
3157c478bd9Sstevel@tonic-gate 		return (NULL);
3167c478bd9Sstevel@tonic-gate 	}
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	if (sysevent_bind_publisher(sysevent_hp) != 0) {
3197c478bd9Sstevel@tonic-gate 		if (errno == EBUSY) {
3207c478bd9Sstevel@tonic-gate 			sysevent_cleanup_publishers(sysevent_hp);
3217c478bd9Sstevel@tonic-gate 			if (sysevent_bind_publisher(sysevent_hp) != 0) {
3227c478bd9Sstevel@tonic-gate 				(void) sysevent_close_channel(sysevent_hp);
3237c478bd9Sstevel@tonic-gate 				return (NULL);
3247c478bd9Sstevel@tonic-gate 			}
3257c478bd9Sstevel@tonic-gate 		}
3267c478bd9Sstevel@tonic-gate 	}
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	sysevent_cleanup_subscribers(sysevent_hp);
3297c478bd9Sstevel@tonic-gate 	cleanup = 0;
3307c478bd9Sstevel@tonic-gate 	eventq_head = NULL;
3317c478bd9Sstevel@tonic-gate 	eventq_tail = NULL;
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	(void) mutex_init(&evq_lock, USYNC_THREAD, NULL);
3347c478bd9Sstevel@tonic-gate 	(void) cond_init(&evq_cv, USYNC_THREAD, NULL);
3357c478bd9Sstevel@tonic-gate 
336*5b784b07SToomas Soome 	if (thr_create(NULL, 0, devfsadmd_deliver_thr,
3377c478bd9Sstevel@tonic-gate 	    NULL, THR_BOUND, &deliver_thr_id) != 0) {
3387c478bd9Sstevel@tonic-gate 		(void) mutex_destroy(&evq_lock);
3397c478bd9Sstevel@tonic-gate 		(void) cond_destroy(&evq_cv);
3407c478bd9Sstevel@tonic-gate 		sysevent_close_channel(sysevent_hp);
3417c478bd9Sstevel@tonic-gate 		return (NULL);
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	return (&devfsadm_mod_ops);
3457c478bd9Sstevel@tonic-gate }
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate void
slm_fini()3487c478bd9Sstevel@tonic-gate slm_fini()
3497c478bd9Sstevel@tonic-gate {
3507c478bd9Sstevel@tonic-gate 	/* Wait for all events to be flushed out to devfsadmd */
3517c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&evq_lock);
3527c478bd9Sstevel@tonic-gate 	cleanup = 1;
3537c478bd9Sstevel@tonic-gate 	(void) cond_signal(&evq_cv);
3547c478bd9Sstevel@tonic-gate 	(void) cond_wait(&evq_cv, &evq_lock);
3557c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&evq_lock);
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	/* Wait for delivery thread to exit */
3587c478bd9Sstevel@tonic-gate 	(void) thr_join(deliver_thr_id, NULL, NULL);
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	(void) mutex_destroy(&evq_lock);
3617c478bd9Sstevel@tonic-gate 	(void) cond_destroy(&evq_cv);
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	sysevent_close_channel(sysevent_hp);
3647c478bd9Sstevel@tonic-gate 	sysevent_hp = NULL;
3657c478bd9Sstevel@tonic-gate }
366