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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <unistd.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate #include <strings.h>
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <limits.h>
337c478bd9Sstevel@tonic-gate #include <thread.h>
347c478bd9Sstevel@tonic-gate #include <wait.h>
357c478bd9Sstevel@tonic-gate #include <synch.h>
367c478bd9Sstevel@tonic-gate #include <errno.h>
377c478bd9Sstevel@tonic-gate #include <locale.h>
387c478bd9Sstevel@tonic-gate #include <sys/stat.h>
397c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
407c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
417c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
427c478bd9Sstevel@tonic-gate #include <sys/sysevent.h>
437c478bd9Sstevel@tonic-gate #include <sys/sysevent_impl.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include <libsysevent.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #include "message_reg_mod.h"
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate  * SLM for sysevent event subscribers
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate extern char	*root_dir;
547c478bd9Sstevel@tonic-gate extern void	syseventd_print(int level, char *format, ...);
557c478bd9Sstevel@tonic-gate extern void	syseventd_err_print(char *format, ...);
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate sysevent_handle_t *sysevent_hp;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate typedef struct ev_queue {
607c478bd9Sstevel@tonic-gate 	struct ev_queue *evq_next;
617c478bd9Sstevel@tonic-gate 	sysevent_t	*evq_ev;
627c478bd9Sstevel@tonic-gate } ev_queue_t;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate static mutex_t	evq_lock;
657c478bd9Sstevel@tonic-gate static cond_t evq_cv;
667c478bd9Sstevel@tonic-gate static ev_queue_t *event_q = NULL;
677c478bd9Sstevel@tonic-gate static int cleanup;
687c478bd9Sstevel@tonic-gate static thread_t deliver_thr_id;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate static int
init_channel()717c478bd9Sstevel@tonic-gate init_channel()
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate 	/*
747c478bd9Sstevel@tonic-gate 	 * This functionality is not supported in the mini-root
757c478bd9Sstevel@tonic-gate 	 * environment, ie install.  If root_dir is set, implying
767c478bd9Sstevel@tonic-gate 	 * install, we quietly fail.
777c478bd9Sstevel@tonic-gate 	 */
787c478bd9Sstevel@tonic-gate 	if (strcmp(root_dir, "") != 0) {
797c478bd9Sstevel@tonic-gate 		return (EACCES);
807c478bd9Sstevel@tonic-gate 	}
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	/*
837c478bd9Sstevel@tonic-gate 	 * Initialize the private sysevent handle
847c478bd9Sstevel@tonic-gate 	 */
857c478bd9Sstevel@tonic-gate 	sysevent_hp = sysevent_open_channel(SYSEVENTD_CHAN);
867c478bd9Sstevel@tonic-gate 	if (sysevent_hp == NULL) {
877c478bd9Sstevel@tonic-gate 		if (errno == EACCES) {
887c478bd9Sstevel@tonic-gate 			syseventd_print(3, "sysevent_reg_mod: "
897c478bd9Sstevel@tonic-gate 			    "sysevent_open_channel failed with %s init "
907c478bd9Sstevel@tonic-gate 			    "deferred\n", strerror(errno));
917c478bd9Sstevel@tonic-gate 			return (errno);
927c478bd9Sstevel@tonic-gate 		} else {
937c478bd9Sstevel@tonic-gate 			syseventd_err_print(INIT_SUB_OPEN_CHAN_ERR,
947c478bd9Sstevel@tonic-gate 			    strerror(errno));
957c478bd9Sstevel@tonic-gate 			return (errno);
967c478bd9Sstevel@tonic-gate 		}
977c478bd9Sstevel@tonic-gate 	}
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	if (sysevent_bind_publisher(sysevent_hp) != 0) {
1007c478bd9Sstevel@tonic-gate 		/*
1017c478bd9Sstevel@tonic-gate 		 * Only one publisher allowed on the syseventd channel,
1027c478bd9Sstevel@tonic-gate 		 * cleanup previously allocated syseventd channel publishers
1037c478bd9Sstevel@tonic-gate 		 */
1047c478bd9Sstevel@tonic-gate 		if (errno == EBUSY) {
1057c478bd9Sstevel@tonic-gate 			sysevent_cleanup_publishers(sysevent_hp);
1067c478bd9Sstevel@tonic-gate 			if (sysevent_bind_publisher(sysevent_hp) == 0)
1077c478bd9Sstevel@tonic-gate 				return (0);
1087c478bd9Sstevel@tonic-gate 		}
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 		syseventd_err_print(INIT_SUB_BIND_PUB_ERR,
1117c478bd9Sstevel@tonic-gate 		    strerror(errno));
1127c478bd9Sstevel@tonic-gate 		sysevent_close_channel(sysevent_hp);
1137c478bd9Sstevel@tonic-gate 		sysevent_hp = NULL;
1147c478bd9Sstevel@tonic-gate 		return (errno);
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	return (0);
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate static int
deliver_event(sysevent_t * ev,int flag)1217c478bd9Sstevel@tonic-gate deliver_event(sysevent_t *ev, int flag)
1227c478bd9Sstevel@tonic-gate {
1237c478bd9Sstevel@tonic-gate 	int ret, ev_size;
1247c478bd9Sstevel@tonic-gate 	ev_queue_t *new_evq, *tmp_evq;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	/* Not initialized */
1277c478bd9Sstevel@tonic-gate 	if (sysevent_hp == NULL) {
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 		ret = init_channel();
1307c478bd9Sstevel@tonic-gate 		if (ret != 0) {
1317c478bd9Sstevel@tonic-gate 			if (ret == EBUSY && flag != SE_NO_RETRY) {
1327c478bd9Sstevel@tonic-gate 				return (EAGAIN);
1337c478bd9Sstevel@tonic-gate 			} else if (ret == EACCES) {
1347c478bd9Sstevel@tonic-gate 				return (0);
1357c478bd9Sstevel@tonic-gate 			} else {
1367c478bd9Sstevel@tonic-gate 				syseventd_err_print(INIT_SUB_OPEN_CHAN_ERR,
1377c478bd9Sstevel@tonic-gate 				    strerror(ret));
1387c478bd9Sstevel@tonic-gate 				return (0);
1397c478bd9Sstevel@tonic-gate 			}
1407c478bd9Sstevel@tonic-gate 		}
1417c478bd9Sstevel@tonic-gate 		/* Check for stale syseventd subscribers */
1427c478bd9Sstevel@tonic-gate 		sysevent_cleanup_subscribers(sysevent_hp);
1437c478bd9Sstevel@tonic-gate 		syseventd_print(3, "sysevent_reg_mod: init successful");
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	/* Queue event for delivery to all subscribers */
1477c478bd9Sstevel@tonic-gate 	new_evq = (ev_queue_t *)calloc(1, sizeof (ev_queue_t));
1487c478bd9Sstevel@tonic-gate 	if (new_evq == NULL) {
1497c478bd9Sstevel@tonic-gate 		return (EAGAIN);
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 	ev_size = sysevent_get_size(ev);
1527c478bd9Sstevel@tonic-gate 	new_evq->evq_ev = (sysevent_t *)malloc(ev_size);
1537c478bd9Sstevel@tonic-gate 	if (new_evq->evq_ev == NULL) {
1547c478bd9Sstevel@tonic-gate 		free(new_evq);
1557c478bd9Sstevel@tonic-gate 		return (EAGAIN);
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 	bcopy(ev, new_evq->evq_ev, ev_size);
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&evq_lock);
1607c478bd9Sstevel@tonic-gate 	if (event_q == NULL) {
1617c478bd9Sstevel@tonic-gate 		event_q = new_evq;
1627c478bd9Sstevel@tonic-gate 	} else {
1637c478bd9Sstevel@tonic-gate 		tmp_evq = event_q;
1647c478bd9Sstevel@tonic-gate 		while (tmp_evq->evq_next != NULL)
1657c478bd9Sstevel@tonic-gate 			tmp_evq = tmp_evq->evq_next;
1667c478bd9Sstevel@tonic-gate 		tmp_evq->evq_next = new_evq;
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 	syseventd_print(3, "sysevent_reg_mod: queue event 0X%llx\n",
1697c478bd9Sstevel@tonic-gate 	    sysevent_get_seq(ev));
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	(void) cond_signal(&evq_cv);
1727c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&evq_lock);
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	return (0);
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate 
177*5b784b07SToomas Soome void *
subscriber_deliver_thr(void * arg __unused)178*5b784b07SToomas Soome subscriber_deliver_thr(void *arg __unused)
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate 	ev_queue_t *evqp;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&evq_lock);
1837c478bd9Sstevel@tonic-gate 	for (;;) {
1847c478bd9Sstevel@tonic-gate 		while (event_q == NULL && cleanup == 0) {
1857c478bd9Sstevel@tonic-gate 			(void) cond_wait(&evq_cv, &evq_lock);
1867c478bd9Sstevel@tonic-gate 		}
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 		/* Send events on to all current subscribers */
1897c478bd9Sstevel@tonic-gate 		evqp = event_q;
1907c478bd9Sstevel@tonic-gate 		while (evqp) {
1917c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&evq_lock);
1927c478bd9Sstevel@tonic-gate 			syseventd_print(3, "sysevent_reg_mod: sending event "
1937c478bd9Sstevel@tonic-gate 			    "0X%llx\n", sysevent_get_seq(evqp->evq_ev));
1947c478bd9Sstevel@tonic-gate 			if (sysevent_send_event(sysevent_hp,
1957c478bd9Sstevel@tonic-gate 			    evqp->evq_ev) != 0) {
1967c478bd9Sstevel@tonic-gate 				syseventd_print(3, "sysevent_reg_mod: "
1977c478bd9Sstevel@tonic-gate 				    "failed to send event\n");
1987c478bd9Sstevel@tonic-gate 			}
1997c478bd9Sstevel@tonic-gate 			syseventd_print(3, "sysevent_reg_mod: event sent "
2007c478bd9Sstevel@tonic-gate 			    "0X%llx\n", sysevent_get_seq(evqp->evq_ev));
2017c478bd9Sstevel@tonic-gate 			(void) mutex_lock(&evq_lock);
2027c478bd9Sstevel@tonic-gate 			event_q = evqp->evq_next;
2037c478bd9Sstevel@tonic-gate 			free(evqp->evq_ev);
2047c478bd9Sstevel@tonic-gate 			free(evqp);
2057c478bd9Sstevel@tonic-gate 			evqp = event_q;
2067c478bd9Sstevel@tonic-gate 		}
2077c478bd9Sstevel@tonic-gate 		if (cleanup) {
2087c478bd9Sstevel@tonic-gate 			syseventd_print(3, "sysevent_reg_mod: deliver "
2097c478bd9Sstevel@tonic-gate 			    "thread exiting\n");
2107c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&evq_lock);
2117c478bd9Sstevel@tonic-gate 			(void) thr_exit(NULL);
2127c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
2137c478bd9Sstevel@tonic-gate 		}
2147c478bd9Sstevel@tonic-gate 	}
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate static struct slm_mod_ops sysevent_reg_mod_ops = {
2207c478bd9Sstevel@tonic-gate 	SE_MAJOR_VERSION, SE_MINOR_VERSION, SE_MAX_RETRY_LIMIT, deliver_event};
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate struct slm_mod_ops *
slm_init()2237c478bd9Sstevel@tonic-gate slm_init()
2247c478bd9Sstevel@tonic-gate {
2257c478bd9Sstevel@tonic-gate 	cleanup = 0;
2267c478bd9Sstevel@tonic-gate 	sysevent_hp = NULL;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	(void) init_channel();
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	(void) mutex_init(&evq_lock, USYNC_THREAD, NULL);
2317c478bd9Sstevel@tonic-gate 	(void) cond_init(&evq_cv, USYNC_THREAD, NULL);
2327c478bd9Sstevel@tonic-gate 
233c7cb3c8bSToomas Soome 	if (thr_create(NULL, 0, (void *(*)(void *))subscriber_deliver_thr,
2347c478bd9Sstevel@tonic-gate 	    NULL, 0, &deliver_thr_id) != 0) {
2357c478bd9Sstevel@tonic-gate 		syseventd_err_print(INIT_SUB_THR_CREATE_ERR, strerror(errno));
2367c478bd9Sstevel@tonic-gate 		return (NULL);
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	return (&sysevent_reg_mod_ops);
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate void
slm_fini()2437c478bd9Sstevel@tonic-gate slm_fini()
2447c478bd9Sstevel@tonic-gate {
2457c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&evq_lock);
2467c478bd9Sstevel@tonic-gate 	cleanup = 1;
2477c478bd9Sstevel@tonic-gate 	(void) cond_signal(&evq_cv);
2487c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&evq_lock);
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	/* Wait for delivery threads to exit */
2517c478bd9Sstevel@tonic-gate 	(void) thr_join(deliver_thr_id, NULL, NULL);
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	(void) mutex_destroy(&evq_lock);
2547c478bd9Sstevel@tonic-gate 	(void) cond_destroy(&evq_cv);
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	sysevent_close_channel(sysevent_hp);
2577c478bd9Sstevel@tonic-gate 	sysevent_hp = NULL;
2587c478bd9Sstevel@tonic-gate }
259