xref: /illumos-gate/usr/src/uts/common/io/sysevent.c (revision 280798ba)
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
519397407SSherry Moore  * Common Development and Distribution License (the "License").
619397407SSherry Moore  * 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 /*
22f6e214c7SGavin Maltby  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Sysevent Driver for GPEC
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/param.h>
327c478bd9Sstevel@tonic-gate #include <sys/cred.h>
337c478bd9Sstevel@tonic-gate #include <sys/file.h>
347c478bd9Sstevel@tonic-gate #include <sys/stat.h>
357c478bd9Sstevel@tonic-gate #include <sys/conf.h>
367c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
377c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
387c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
397c478bd9Sstevel@tonic-gate #include <sys/open.h>		/* OTYP_CHR definition */
407c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>	/* L_BITSMINOR definition */
417c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
427c478bd9Sstevel@tonic-gate #include <sys/sysevent.h>
437c478bd9Sstevel@tonic-gate #include <sys/sysevent_impl.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate static dev_info_t *sysevent_devi;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /* Definitions for binding handle array */
487c478bd9Sstevel@tonic-gate static ulong_t sysevent_bitmap_initial = 1;	/* index 0 indicates error */
497c478bd9Sstevel@tonic-gate static ulong_t *sysevent_minor_bitmap = &sysevent_bitmap_initial;
507c478bd9Sstevel@tonic-gate static size_t sysevent_minor_bits = BT_NBIPUL;
517c478bd9Sstevel@tonic-gate static kmutex_t sysevent_minor_mutex;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate  * evchan_ctl acts as a container for the binding handle
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate typedef struct evchan_ctl {
577c478bd9Sstevel@tonic-gate 	evchan_t *chp;
587c478bd9Sstevel@tonic-gate } evchan_ctl_t;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate static void *evchan_ctlp;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /*
637c478bd9Sstevel@tonic-gate  * Check if it's a null terminated array - to avoid DoS attack
647c478bd9Sstevel@tonic-gate  * It is supposed that string points to an array with
657c478bd9Sstevel@tonic-gate  * a minimum length of len. len must be strlen + 1.
667c478bd9Sstevel@tonic-gate  * Checks for printable characters are already done in library.
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate static int
sysevent_isstrend(char * string,size_t len)697c478bd9Sstevel@tonic-gate sysevent_isstrend(char *string, size_t len)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	/* Return 0 if string has length of zero */
727c478bd9Sstevel@tonic-gate 	if (len > 0) {
737c478bd9Sstevel@tonic-gate 		return (string[len - 1] == '\0' ? 1 : 0);
747c478bd9Sstevel@tonic-gate 	} else {
757c478bd9Sstevel@tonic-gate 		return (0);
767c478bd9Sstevel@tonic-gate 	}
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate  * Following sysevent_minor_* routines map
817c478bd9Sstevel@tonic-gate  * a binding handle (evchan_t *) to a minor number
827c478bd9Sstevel@tonic-gate  * Has to be called w/ locks held.
837c478bd9Sstevel@tonic-gate  */
847c478bd9Sstevel@tonic-gate static ulong_t *
sysevent_minor_alloc(void)857c478bd9Sstevel@tonic-gate sysevent_minor_alloc(void)
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate 	ulong_t *bhst = sysevent_minor_bitmap;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	/* Increase bitmap by one BT_NBIPUL */
907c478bd9Sstevel@tonic-gate 	if (sysevent_minor_bits + BT_NBIPUL > SYSEVENT_MINOR_MAX) {
917c478bd9Sstevel@tonic-gate 		return ((ulong_t *)NULL);
927c478bd9Sstevel@tonic-gate 	}
937c478bd9Sstevel@tonic-gate 	sysevent_minor_bitmap = kmem_zalloc(
947c478bd9Sstevel@tonic-gate 	    BT_SIZEOFMAP(sysevent_minor_bits + BT_NBIPUL), KM_SLEEP);
957c478bd9Sstevel@tonic-gate 	bcopy(bhst, sysevent_minor_bitmap, BT_SIZEOFMAP(sysevent_minor_bits));
967c478bd9Sstevel@tonic-gate 	if (bhst != &sysevent_bitmap_initial)
977c478bd9Sstevel@tonic-gate 		kmem_free(bhst, BT_SIZEOFMAP(sysevent_minor_bits));
987c478bd9Sstevel@tonic-gate 	sysevent_minor_bits += BT_NBIPUL;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	return (sysevent_minor_bitmap);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate static void
sysevent_minor_free(ulong_t * bitmap)1047c478bd9Sstevel@tonic-gate sysevent_minor_free(ulong_t *bitmap)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate 	if (bitmap != &sysevent_bitmap_initial)
1077c478bd9Sstevel@tonic-gate 		kmem_free(bitmap, BT_SIZEOFMAP(sysevent_minor_bits));
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate static index_t
sysevent_minor_get(void)1117c478bd9Sstevel@tonic-gate sysevent_minor_get(void)
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate 	index_t idx;
1147c478bd9Sstevel@tonic-gate 	ulong_t *bhst;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	/* Search for an available index */
1177c478bd9Sstevel@tonic-gate 	mutex_enter(&sysevent_minor_mutex);
1187c478bd9Sstevel@tonic-gate 	if ((idx = bt_availbit(sysevent_minor_bitmap,
1197c478bd9Sstevel@tonic-gate 	    sysevent_minor_bits)) == -1) {
1207c478bd9Sstevel@tonic-gate 		/* All busy - allocate additional binding handle bitmap space */
1217c478bd9Sstevel@tonic-gate 		if ((bhst = sysevent_minor_alloc()) == NULL) {
1227c478bd9Sstevel@tonic-gate 			/* Reached our maximum of id's == SHRT_MAX */
1237c478bd9Sstevel@tonic-gate 			mutex_exit(&sysevent_minor_mutex);
1247c478bd9Sstevel@tonic-gate 			return (0);
1257c478bd9Sstevel@tonic-gate 		} else {
1267c478bd9Sstevel@tonic-gate 			sysevent_minor_bitmap = bhst;
1277c478bd9Sstevel@tonic-gate 		}
1287c478bd9Sstevel@tonic-gate 		idx = bt_availbit(sysevent_minor_bitmap, sysevent_minor_bits);
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 	BT_SET(sysevent_minor_bitmap, idx);
1317c478bd9Sstevel@tonic-gate 	mutex_exit(&sysevent_minor_mutex);
1327c478bd9Sstevel@tonic-gate 	return (idx);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate static void
sysevent_minor_rele(index_t idx)1367c478bd9Sstevel@tonic-gate sysevent_minor_rele(index_t idx)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate 	mutex_enter(&sysevent_minor_mutex);
1397c478bd9Sstevel@tonic-gate 	ASSERT(BT_TEST(sysevent_minor_bitmap, idx) == 1);
1407c478bd9Sstevel@tonic-gate 	BT_CLEAR(sysevent_minor_bitmap, idx);
1417c478bd9Sstevel@tonic-gate 	mutex_exit(&sysevent_minor_mutex);
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate static void
sysevent_minor_init(void)1457c478bd9Sstevel@tonic-gate sysevent_minor_init(void)
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate 	mutex_init(&sysevent_minor_mutex, NULL, MUTEX_DEFAULT, NULL);
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /* ARGSUSED */
1517c478bd9Sstevel@tonic-gate static int
sysevent_publish(dev_t dev,int * rvalp,void * arg,int flag,cred_t * cr)1527c478bd9Sstevel@tonic-gate sysevent_publish(dev_t dev, int *rvalp, void *arg, int flag, cred_t *cr)
1537c478bd9Sstevel@tonic-gate {
1547c478bd9Sstevel@tonic-gate 	int km_flags;
1557c478bd9Sstevel@tonic-gate 	sev_publish_args_t uargs;
1567c478bd9Sstevel@tonic-gate 	sysevent_impl_t *ev;
1577c478bd9Sstevel@tonic-gate 	evchan_ctl_t *ctl;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	ctl = ddi_get_soft_state(evchan_ctlp, getminor(dev));
1607c478bd9Sstevel@tonic-gate 	if (ctl == NULL || ctl->chp == NULL)
1617c478bd9Sstevel@tonic-gate 		return (ENXIO);
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	if (copyin(arg, &uargs, sizeof (sev_publish_args_t)) != 0)
1647c478bd9Sstevel@tonic-gate 		return (EFAULT);
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	/*
1677c478bd9Sstevel@tonic-gate 	 * This limits the size of an event
1687c478bd9Sstevel@tonic-gate 	 */
1697c478bd9Sstevel@tonic-gate 	if (uargs.ev.len > MAX_EV_SIZE_LEN)
1707c478bd9Sstevel@tonic-gate 		return (EOVERFLOW);
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	/*
1737c478bd9Sstevel@tonic-gate 	 * Check for valid uargs.flags
1747c478bd9Sstevel@tonic-gate 	 */
1757c478bd9Sstevel@tonic-gate 	if (uargs.flags & ~(EVCH_NOSLEEP | EVCH_SLEEP | EVCH_QWAIT))
1767c478bd9Sstevel@tonic-gate 		return (EINVAL);
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	/*
1797c478bd9Sstevel@tonic-gate 	 * Check that at least one of EVCH_NOSLEEP or EVCH_SLEEP is
1807c478bd9Sstevel@tonic-gate 	 * specified
1817c478bd9Sstevel@tonic-gate 	 */
1827c478bd9Sstevel@tonic-gate 	km_flags = uargs.flags & (EVCH_NOSLEEP | EVCH_SLEEP);
1837c478bd9Sstevel@tonic-gate 	if (km_flags != EVCH_NOSLEEP && km_flags != EVCH_SLEEP)
1847c478bd9Sstevel@tonic-gate 		return (EINVAL);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	ev = evch_usrallocev(uargs.ev.len, uargs.flags);
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	if (copyin((void *)(uintptr_t)uargs.ev.name, ev, uargs.ev.len) != 0) {
1897c478bd9Sstevel@tonic-gate 		evch_usrfreeev(ev);
1907c478bd9Sstevel@tonic-gate 		return (EFAULT);
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	return (evch_usrpostevent(ctl->chp, ev, uargs.flags));
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	/* Event will be freed internally */
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate /*
1997c478bd9Sstevel@tonic-gate  * sysevent_chan_open - used to open a channel in the GPEC channel layer
2007c478bd9Sstevel@tonic-gate  */
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate /* ARGSUSED */
2037c478bd9Sstevel@tonic-gate static int
sysevent_chan_open(dev_t dev,int * rvalp,void * arg,int flag,cred_t * cr)2047c478bd9Sstevel@tonic-gate sysevent_chan_open(dev_t dev, int *rvalp, void *arg, int flag, cred_t *cr)
2057c478bd9Sstevel@tonic-gate {
2067c478bd9Sstevel@tonic-gate 	sev_bind_args_t uargs;
2077c478bd9Sstevel@tonic-gate 	evchan_ctl_t *ctl;
2087c478bd9Sstevel@tonic-gate 	char *chan_name;
2097c478bd9Sstevel@tonic-gate 	int ec;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	ctl = ddi_get_soft_state(evchan_ctlp, getminor(dev));
2127c478bd9Sstevel@tonic-gate 	if (ctl == NULL) {
2137c478bd9Sstevel@tonic-gate 		return (ENXIO);
2147c478bd9Sstevel@tonic-gate 	}
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	if (copyin(arg, &uargs, sizeof (sev_bind_args_t)) != 0)
2177c478bd9Sstevel@tonic-gate 		return (EFAULT);
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	if (uargs.chan_name.len > MAX_CHNAME_LEN)
2207c478bd9Sstevel@tonic-gate 		return (EINVAL);
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	chan_name = kmem_alloc(uargs.chan_name.len, KM_SLEEP);
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	if (copyin((void *)(uintptr_t)uargs.chan_name.name, chan_name,
2257c478bd9Sstevel@tonic-gate 	    uargs.chan_name.len) != 0) {
2267c478bd9Sstevel@tonic-gate 		kmem_free(chan_name, uargs.chan_name.len);
2277c478bd9Sstevel@tonic-gate 		return (EFAULT);
2287c478bd9Sstevel@tonic-gate 	}
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	if (!sysevent_isstrend(chan_name, uargs.chan_name.len)) {
2317c478bd9Sstevel@tonic-gate 		kmem_free(chan_name, uargs.chan_name.len);
2327c478bd9Sstevel@tonic-gate 		return (EINVAL);
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	/*
2367c478bd9Sstevel@tonic-gate 	 * Check of uargs.flags and uargs.perms just to avoid DoS attacks.
2377c478bd9Sstevel@tonic-gate 	 * libsysevent does this carefully
2387c478bd9Sstevel@tonic-gate 	 */
2397c478bd9Sstevel@tonic-gate 	ctl->chp = evch_usrchanopen((const char *)chan_name,
2407c478bd9Sstevel@tonic-gate 	    uargs.flags & EVCH_B_FLAGS, &ec);
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	kmem_free(chan_name, uargs.chan_name.len);
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	if (ec != 0) {
2457c478bd9Sstevel@tonic-gate 		return (ec);
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	return (0);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate /* ARGSUSED */
2527c478bd9Sstevel@tonic-gate static int
sysevent_chan_control(dev_t dev,int * rvalp,void * arg,int flag,cred_t * cr)2537c478bd9Sstevel@tonic-gate sysevent_chan_control(dev_t dev, int *rvalp, void *arg, int flag, cred_t *cr)
2547c478bd9Sstevel@tonic-gate {
2557c478bd9Sstevel@tonic-gate 	sev_control_args_t uargs;
2567c478bd9Sstevel@tonic-gate 	evchan_ctl_t *ctl;
2577c478bd9Sstevel@tonic-gate 	int rc;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	ctl = ddi_get_soft_state(evchan_ctlp, getminor(dev));
2607c478bd9Sstevel@tonic-gate 	if (ctl == NULL || ctl->chp == NULL)
2617c478bd9Sstevel@tonic-gate 		return (ENXIO);
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	if (copyin(arg, &uargs, sizeof (sev_control_args_t)) != 0)
2647c478bd9Sstevel@tonic-gate 		return (EFAULT);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	switch (uargs.cmd) {
2677c478bd9Sstevel@tonic-gate 	case EVCH_GET_CHAN_LEN:
2687c478bd9Sstevel@tonic-gate 	case EVCH_GET_CHAN_LEN_MAX:
2697c478bd9Sstevel@tonic-gate 		rc = evch_usrcontrol_get(ctl->chp, uargs.cmd, &uargs.value);
2707c478bd9Sstevel@tonic-gate 		if (rc == 0) {
2717c478bd9Sstevel@tonic-gate 			if (copyout((void *)&uargs, arg,
2727c478bd9Sstevel@tonic-gate 			    sizeof (sev_control_args_t)) != 0) {
2737c478bd9Sstevel@tonic-gate 				rc = EFAULT;
2747c478bd9Sstevel@tonic-gate 			}
2757c478bd9Sstevel@tonic-gate 		}
2767c478bd9Sstevel@tonic-gate 		break;
2777c478bd9Sstevel@tonic-gate 	case EVCH_SET_CHAN_LEN:
2787c478bd9Sstevel@tonic-gate 		rc = evch_usrcontrol_set(ctl->chp, uargs.cmd, uargs.value);
2797c478bd9Sstevel@tonic-gate 		break;
2807c478bd9Sstevel@tonic-gate 	default:
2817c478bd9Sstevel@tonic-gate 		rc = EINVAL;
2827c478bd9Sstevel@tonic-gate 	}
2837c478bd9Sstevel@tonic-gate 	return (rc);
2847c478bd9Sstevel@tonic-gate }
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate /* ARGSUSED */
2877c478bd9Sstevel@tonic-gate static int
sysevent_subscribe(dev_t dev,int * rvalp,void * arg,int flag,cred_t * cr)2887c478bd9Sstevel@tonic-gate sysevent_subscribe(dev_t dev, int *rvalp, void *arg, int flag, cred_t *cr)
2897c478bd9Sstevel@tonic-gate {
2907c478bd9Sstevel@tonic-gate 	sev_subscribe_args_t uargs;
2917c478bd9Sstevel@tonic-gate 	char *sid;
2927c478bd9Sstevel@tonic-gate 	char *class_info = NULL;
2937c478bd9Sstevel@tonic-gate 	evchan_ctl_t *ctl;
2947c478bd9Sstevel@tonic-gate 	int rc;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	ctl = ddi_get_soft_state(evchan_ctlp, getminor(dev));
2977c478bd9Sstevel@tonic-gate 	if (ctl == NULL || ctl->chp == NULL)
2987c478bd9Sstevel@tonic-gate 		return (ENXIO);
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	if (copyin(arg, &uargs, sizeof (sev_subscribe_args_t)) != 0)
3017c478bd9Sstevel@tonic-gate 		return (EFAULT);
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	if (uargs.sid.len > MAX_SUBID_LEN ||
3047c478bd9Sstevel@tonic-gate 	    uargs.class_info.len > MAX_CLASS_LEN)
3057c478bd9Sstevel@tonic-gate 		return (EINVAL);
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	sid = kmem_alloc(uargs.sid.len, KM_SLEEP);
3087c478bd9Sstevel@tonic-gate 	if (copyin((void *)(uintptr_t)uargs.sid.name,
3097c478bd9Sstevel@tonic-gate 	    sid, uargs.sid.len) != 0) {
3107c478bd9Sstevel@tonic-gate 		kmem_free(sid, uargs.sid.len);
3117c478bd9Sstevel@tonic-gate 		return (EFAULT);
3127c478bd9Sstevel@tonic-gate 	}
3137c478bd9Sstevel@tonic-gate 	if (!sysevent_isstrend(sid, uargs.sid.len)) {
3147c478bd9Sstevel@tonic-gate 		kmem_free(sid, uargs.sid.len);
3157c478bd9Sstevel@tonic-gate 		return (EINVAL);
3167c478bd9Sstevel@tonic-gate 	}
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	/* If class string empty then class EC_ALL is assumed */
3197c478bd9Sstevel@tonic-gate 	if (uargs.class_info.len != 0) {
3207c478bd9Sstevel@tonic-gate 		class_info = kmem_alloc(uargs.class_info.len, KM_SLEEP);
3217c478bd9Sstevel@tonic-gate 		if (copyin((void *)(uintptr_t)uargs.class_info.name, class_info,
3227c478bd9Sstevel@tonic-gate 		    uargs.class_info.len) != 0) {
3237c478bd9Sstevel@tonic-gate 			kmem_free(class_info, uargs.class_info.len);
3247c478bd9Sstevel@tonic-gate 			kmem_free(sid, uargs.sid.len);
3257c478bd9Sstevel@tonic-gate 			return (EFAULT);
3267c478bd9Sstevel@tonic-gate 		}
3277c478bd9Sstevel@tonic-gate 		if (!sysevent_isstrend(class_info, uargs.class_info.len)) {
3287c478bd9Sstevel@tonic-gate 			kmem_free(class_info, uargs.class_info.len);
3297c478bd9Sstevel@tonic-gate 			kmem_free(sid, uargs.sid.len);
3307c478bd9Sstevel@tonic-gate 			return (EINVAL);
3317c478bd9Sstevel@tonic-gate 		}
3327c478bd9Sstevel@tonic-gate 	}
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	/*
3357c478bd9Sstevel@tonic-gate 	 * Check of uargs.flags just to avoid DoS attacks
3367c478bd9Sstevel@tonic-gate 	 * libsysevent does this carefully.
3377c478bd9Sstevel@tonic-gate 	 */
3387c478bd9Sstevel@tonic-gate 	rc = evch_usrsubscribe(ctl->chp, sid, class_info,
3397c478bd9Sstevel@tonic-gate 	    (int)uargs.door_desc, uargs.flags);
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	kmem_free(class_info, uargs.class_info.len);
3427c478bd9Sstevel@tonic-gate 	kmem_free(sid, uargs.sid.len);
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	return (rc);
3457c478bd9Sstevel@tonic-gate }
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate /* ARGSUSED */
3487c478bd9Sstevel@tonic-gate static int
sysevent_unsubscribe(dev_t dev,int * rvalp,void * arg,int flag,cred_t * cr)3497c478bd9Sstevel@tonic-gate sysevent_unsubscribe(dev_t dev, int *rvalp, void *arg, int flag, cred_t *cr)
3507c478bd9Sstevel@tonic-gate {
3517c478bd9Sstevel@tonic-gate 	sev_unsubscribe_args_t uargs;
3527c478bd9Sstevel@tonic-gate 	char *sid;
3537c478bd9Sstevel@tonic-gate 	evchan_ctl_t *ctl;
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	if (copyin(arg, &uargs, sizeof (sev_unsubscribe_args_t)) != 0)
3567c478bd9Sstevel@tonic-gate 		return (EFAULT);
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	ctl = ddi_get_soft_state(evchan_ctlp, getminor(dev));
3597c478bd9Sstevel@tonic-gate 	if (ctl == NULL || ctl->chp == NULL)
3607c478bd9Sstevel@tonic-gate 		return (ENXIO);
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	if (uargs.sid.len > MAX_SUBID_LEN)
3637c478bd9Sstevel@tonic-gate 		return (EINVAL);
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	/* Unsubscribe for all */
3667c478bd9Sstevel@tonic-gate 	if (uargs.sid.len == 0) {
3677c478bd9Sstevel@tonic-gate 		evch_usrunsubscribe(ctl->chp, NULL, 0);
3687c478bd9Sstevel@tonic-gate 		return (0);
3697c478bd9Sstevel@tonic-gate 	}
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	sid = kmem_alloc(uargs.sid.len, KM_SLEEP);
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	if (copyin((void *)(uintptr_t)uargs.sid.name,
3747c478bd9Sstevel@tonic-gate 	    sid, uargs.sid.len) != 0) {
3757c478bd9Sstevel@tonic-gate 		kmem_free(sid, uargs.sid.len);
3767c478bd9Sstevel@tonic-gate 		return (EFAULT);
3777c478bd9Sstevel@tonic-gate 	}
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	evch_usrunsubscribe(ctl->chp, sid, 0);
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	kmem_free(sid, uargs.sid.len);
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	return (0);
3847c478bd9Sstevel@tonic-gate }
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate /* ARGSUSED */
3877c478bd9Sstevel@tonic-gate static int
sysevent_channames(dev_t dev,int * rvalp,void * arg,int flag,cred_t * cr)3887c478bd9Sstevel@tonic-gate sysevent_channames(dev_t dev, int *rvalp, void *arg, int flag, cred_t *cr)
3897c478bd9Sstevel@tonic-gate {
3907c478bd9Sstevel@tonic-gate 	sev_chandata_args_t uargs;
3917c478bd9Sstevel@tonic-gate 	char *buf;
3927c478bd9Sstevel@tonic-gate 	int len;
3937c478bd9Sstevel@tonic-gate 	int rc = 0;
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	if (copyin(arg, &uargs, sizeof (sev_chandata_args_t)) != 0)
3967c478bd9Sstevel@tonic-gate 		return (EFAULT);
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	if (uargs.out_data.len == 0 || uargs.out_data.len > EVCH_MAX_DATA_SIZE)
3997c478bd9Sstevel@tonic-gate 		return (EINVAL);
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	buf = kmem_alloc(uargs.out_data.len, KM_SLEEP);
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	if ((len = evch_usrgetchnames(buf, uargs.out_data.len)) == -1) {
4047c478bd9Sstevel@tonic-gate 		rc = EOVERFLOW;
4057c478bd9Sstevel@tonic-gate 	}
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	if (rc == 0) {
4087c478bd9Sstevel@tonic-gate 		ASSERT(len <= uargs.out_data.len);
4097c478bd9Sstevel@tonic-gate 		if (copyout(buf,
4107c478bd9Sstevel@tonic-gate 		    (void *)(uintptr_t)uargs.out_data.name, len) != 0) {
4117c478bd9Sstevel@tonic-gate 			rc = EFAULT;
4127c478bd9Sstevel@tonic-gate 		}
4137c478bd9Sstevel@tonic-gate 	}
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	kmem_free(buf, uargs.out_data.len);
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	return (rc);
4187c478bd9Sstevel@tonic-gate }
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate /* ARGSUSED */
4217c478bd9Sstevel@tonic-gate static int
sysevent_chandata(dev_t dev,int * rvalp,void * arg,int flag,cred_t * cr)4227c478bd9Sstevel@tonic-gate sysevent_chandata(dev_t dev, int *rvalp, void *arg, int flag, cred_t *cr)
4237c478bd9Sstevel@tonic-gate {
4247c478bd9Sstevel@tonic-gate 	sev_chandata_args_t uargs;
4257c478bd9Sstevel@tonic-gate 	char *channel;
4267c478bd9Sstevel@tonic-gate 	char *buf;
4277c478bd9Sstevel@tonic-gate 	int len;
4287c478bd9Sstevel@tonic-gate 	int rc = 0;
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	if (copyin(arg, &uargs, sizeof (sev_chandata_args_t)) != 0)
4317c478bd9Sstevel@tonic-gate 		return (EFAULT);
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	if (uargs.in_data.len > MAX_CHNAME_LEN ||
4347c478bd9Sstevel@tonic-gate 	    uargs.out_data.len > EVCH_MAX_DATA_SIZE)
4357c478bd9Sstevel@tonic-gate 		return (EINVAL);
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	channel = kmem_alloc(uargs.in_data.len, KM_SLEEP);
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	if (copyin((void *)(uintptr_t)uargs.in_data.name, channel,
4407c478bd9Sstevel@tonic-gate 	    uargs.in_data.len) != 0) {
4417c478bd9Sstevel@tonic-gate 		kmem_free(channel, uargs.in_data.len);
4427c478bd9Sstevel@tonic-gate 		return (EFAULT);
4437c478bd9Sstevel@tonic-gate 	}
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	if (!sysevent_isstrend(channel, uargs.in_data.len)) {
4467c478bd9Sstevel@tonic-gate 		kmem_free(channel, uargs.in_data.len);
4477c478bd9Sstevel@tonic-gate 		return (EINVAL);
4487c478bd9Sstevel@tonic-gate 	}
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	buf = kmem_alloc(uargs.out_data.len, KM_SLEEP);
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	len = evch_usrgetchdata(channel, buf, uargs.out_data.len);
4537c478bd9Sstevel@tonic-gate 	if (len == 0) {
4547c478bd9Sstevel@tonic-gate 		rc = EOVERFLOW;
4557c478bd9Sstevel@tonic-gate 	} else if (len == -1) {
4567c478bd9Sstevel@tonic-gate 		rc = ENOENT;
4577c478bd9Sstevel@tonic-gate 	}
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	if (rc == 0) {
4607c478bd9Sstevel@tonic-gate 		ASSERT(len <= uargs.out_data.len);
4617c478bd9Sstevel@tonic-gate 		if (copyout(buf,
4627c478bd9Sstevel@tonic-gate 		    (void *)(uintptr_t)uargs.out_data.name, len) != 0) {
4637c478bd9Sstevel@tonic-gate 			rc = EFAULT;
4647c478bd9Sstevel@tonic-gate 		}
4657c478bd9Sstevel@tonic-gate 	}
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	kmem_free(buf, uargs.out_data.len);
4687c478bd9Sstevel@tonic-gate 	kmem_free(channel, uargs.in_data.len);
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	return (rc);
4717c478bd9Sstevel@tonic-gate }
4727c478bd9Sstevel@tonic-gate 
473f6e214c7SGavin Maltby /* ARGSUSED */
474f6e214c7SGavin Maltby static int
sysevent_setpropnvl(dev_t dev,int * rvalp,void * arg,int flag,cred_t * cr)475f6e214c7SGavin Maltby sysevent_setpropnvl(dev_t dev, int *rvalp, void *arg, int flag, cred_t *cr)
476f6e214c7SGavin Maltby {
477f6e214c7SGavin Maltby 	sev_propnvl_args_t uargs;
478f6e214c7SGavin Maltby 	nvlist_t *nvl = NULL;
479f6e214c7SGavin Maltby 	evchan_ctl_t *ctl;
480f6e214c7SGavin Maltby 	size_t bufsz;
481f6e214c7SGavin Maltby 	char *buf;
482f6e214c7SGavin Maltby 
483f6e214c7SGavin Maltby 	ctl = ddi_get_soft_state(evchan_ctlp, getminor(dev));
484f6e214c7SGavin Maltby 	if (ctl == NULL || ctl->chp == NULL)
485f6e214c7SGavin Maltby 		return (ENXIO);
486f6e214c7SGavin Maltby 
487f6e214c7SGavin Maltby 	if (copyin(arg, &uargs, sizeof (uargs)) != 0)
488f6e214c7SGavin Maltby 		return (EFAULT);
489f6e214c7SGavin Maltby 
490f6e214c7SGavin Maltby 	if (uargs.packednvl.name != 0) {
491f6e214c7SGavin Maltby 		bufsz = uargs.packednvl.len;
492f6e214c7SGavin Maltby 
493f6e214c7SGavin Maltby 		if (bufsz == 0)
494f6e214c7SGavin Maltby 			return (EINVAL);
495f6e214c7SGavin Maltby 
496f6e214c7SGavin Maltby 		if (bufsz > EVCH_MAX_DATA_SIZE)
497f6e214c7SGavin Maltby 			return (EOVERFLOW);
498f6e214c7SGavin Maltby 
499f6e214c7SGavin Maltby 		buf = kmem_alloc(bufsz, KM_SLEEP);
500f6e214c7SGavin Maltby 
501f6e214c7SGavin Maltby 		if (copyin((void *)(uintptr_t)uargs.packednvl.name, buf,
502f6e214c7SGavin Maltby 		    bufsz) != 0 ||
503f6e214c7SGavin Maltby 		    nvlist_unpack(buf, bufsz, &nvl, KM_SLEEP) != 0) {
504f6e214c7SGavin Maltby 			kmem_free(buf, bufsz);
505f6e214c7SGavin Maltby 			return (EFAULT);
506f6e214c7SGavin Maltby 		}
507f6e214c7SGavin Maltby 
508f6e214c7SGavin Maltby 		kmem_free(buf, bufsz);
509f6e214c7SGavin Maltby 
510f6e214c7SGavin Maltby 		if (nvl == NULL)
511f6e214c7SGavin Maltby 			return (EINVAL);
512f6e214c7SGavin Maltby 	}
513f6e214c7SGavin Maltby 
514f6e214c7SGavin Maltby 	evch_usrsetpropnvl(ctl->chp, nvl);
515f6e214c7SGavin Maltby 	return (0);
516f6e214c7SGavin Maltby }
517f6e214c7SGavin Maltby 
518f6e214c7SGavin Maltby /* ARGSUSED */
519f6e214c7SGavin Maltby static int
sysevent_getpropnvl(dev_t dev,int * rvalp,void * arg,int flag,cred_t * cr)520f6e214c7SGavin Maltby sysevent_getpropnvl(dev_t dev, int *rvalp, void *arg, int flag, cred_t *cr)
521f6e214c7SGavin Maltby {
522f6e214c7SGavin Maltby 	sev_propnvl_args_t uargs;
523f6e214c7SGavin Maltby 	size_t reqsz, avlsz;
524f6e214c7SGavin Maltby 	evchan_ctl_t *ctl;
525f6e214c7SGavin Maltby 	nvlist_t *nvl;
526f6e214c7SGavin Maltby 	int64_t gen;
527f6e214c7SGavin Maltby 	int rc;
528f6e214c7SGavin Maltby 
529f6e214c7SGavin Maltby 	ctl = ddi_get_soft_state(evchan_ctlp, getminor(dev));
530f6e214c7SGavin Maltby 
531f6e214c7SGavin Maltby 	if (ctl == NULL || ctl->chp == NULL)
532f6e214c7SGavin Maltby 		return (ENXIO);
533f6e214c7SGavin Maltby 
534f6e214c7SGavin Maltby 	if (copyin(arg, &uargs, sizeof (uargs)) != 0)
535f6e214c7SGavin Maltby 		return (EFAULT);
536f6e214c7SGavin Maltby 
537f6e214c7SGavin Maltby 	if ((rc = evch_usrgetpropnvl(ctl->chp, &nvl, &gen)) != 0)
538f6e214c7SGavin Maltby 		return (rc);
539f6e214c7SGavin Maltby 
540f6e214c7SGavin Maltby 	if (nvl != NULL) {
541f6e214c7SGavin Maltby 		avlsz = uargs.packednvl.len;
542f6e214c7SGavin Maltby 
543f6e214c7SGavin Maltby 		if (nvlist_size(nvl, &reqsz, NV_ENCODE_NATIVE) != 0) {
544f6e214c7SGavin Maltby 			nvlist_free(nvl);
545f6e214c7SGavin Maltby 			return (EINVAL);
546f6e214c7SGavin Maltby 		}
547f6e214c7SGavin Maltby 
548f6e214c7SGavin Maltby 		if (reqsz > EVCH_MAX_DATA_SIZE) {
549f6e214c7SGavin Maltby 			nvlist_free(nvl);
550f6e214c7SGavin Maltby 			return (E2BIG);
551f6e214c7SGavin Maltby 		}
552f6e214c7SGavin Maltby 
553f6e214c7SGavin Maltby 		if (reqsz <= avlsz) {
554f6e214c7SGavin Maltby 			char *buf = kmem_alloc(reqsz, KM_SLEEP);
555f6e214c7SGavin Maltby 
556f6e214c7SGavin Maltby 			if (nvlist_pack(nvl, &buf, &reqsz,
557f6e214c7SGavin Maltby 			    NV_ENCODE_NATIVE, 0) != 0 || copyout(buf,
558f6e214c7SGavin Maltby 			    (void *)(uintptr_t)uargs.packednvl.name,
559f6e214c7SGavin Maltby 			    reqsz) != 0) {
560f6e214c7SGavin Maltby 				kmem_free(buf, reqsz);
561f6e214c7SGavin Maltby 				nvlist_free(nvl);
562f6e214c7SGavin Maltby 				return (EFAULT);
563f6e214c7SGavin Maltby 			}
564f6e214c7SGavin Maltby 			kmem_free(buf, reqsz);
565f6e214c7SGavin Maltby 			rc = 0;
566f6e214c7SGavin Maltby 		} else {
567f6e214c7SGavin Maltby 			rc = EOVERFLOW;
568f6e214c7SGavin Maltby 		}
569f6e214c7SGavin Maltby 		uargs.packednvl.len = (uint32_t)reqsz;
570f6e214c7SGavin Maltby 		nvlist_free(nvl);
571f6e214c7SGavin Maltby 	} else {
572f6e214c7SGavin Maltby 		uargs.packednvl.len = 0;
573f6e214c7SGavin Maltby 		rc = 0;
574f6e214c7SGavin Maltby 	}
575f6e214c7SGavin Maltby 
576f6e214c7SGavin Maltby 	uargs.generation = gen;
577f6e214c7SGavin Maltby 	if (copyout((void *)&uargs, arg, sizeof (uargs)) != 0)
578f6e214c7SGavin Maltby 		rc = EFAULT;
579f6e214c7SGavin Maltby 
580f6e214c7SGavin Maltby 	return (rc);
581f6e214c7SGavin Maltby }
582f6e214c7SGavin Maltby 
5837c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5847c478bd9Sstevel@tonic-gate static int
sysevent_ioctl(dev_t dev,int cmd,intptr_t arg,int flag,cred_t * cr,int * rvalp)5857c478bd9Sstevel@tonic-gate sysevent_ioctl(dev_t dev, int cmd, intptr_t arg,
5867c478bd9Sstevel@tonic-gate     int flag, cred_t *cr, int *rvalp)
5877c478bd9Sstevel@tonic-gate {
5887c478bd9Sstevel@tonic-gate 	int rc;
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	switch (cmd) {
5917c478bd9Sstevel@tonic-gate 	case SEV_PUBLISH:
5927c478bd9Sstevel@tonic-gate 		rc = sysevent_publish(dev, rvalp, (void *)arg, flag, cr);
5937c478bd9Sstevel@tonic-gate 		break;
5947c478bd9Sstevel@tonic-gate 	case SEV_CHAN_OPEN:
5957c478bd9Sstevel@tonic-gate 		rc = sysevent_chan_open(dev, rvalp, (void *)arg, flag, cr);
5967c478bd9Sstevel@tonic-gate 		break;
5977c478bd9Sstevel@tonic-gate 	case SEV_CHAN_CONTROL:
5987c478bd9Sstevel@tonic-gate 		rc = sysevent_chan_control(dev, rvalp, (void *)arg, flag, cr);
5997c478bd9Sstevel@tonic-gate 		break;
6007c478bd9Sstevel@tonic-gate 	case SEV_SUBSCRIBE:
6017c478bd9Sstevel@tonic-gate 		rc = sysevent_subscribe(dev, rvalp, (void *)arg, flag, cr);
6027c478bd9Sstevel@tonic-gate 		break;
6037c478bd9Sstevel@tonic-gate 	case SEV_UNSUBSCRIBE:
6047c478bd9Sstevel@tonic-gate 		rc = sysevent_unsubscribe(dev, rvalp, (void *)arg, flag, cr);
6057c478bd9Sstevel@tonic-gate 		break;
6067c478bd9Sstevel@tonic-gate 	case SEV_CHANNAMES:
6077c478bd9Sstevel@tonic-gate 		rc = sysevent_channames(dev, rvalp, (void *)arg, flag, cr);
6087c478bd9Sstevel@tonic-gate 		break;
6097c478bd9Sstevel@tonic-gate 	case SEV_CHANDATA:
6107c478bd9Sstevel@tonic-gate 		rc = sysevent_chandata(dev, rvalp, (void *)arg, flag, cr);
6117c478bd9Sstevel@tonic-gate 		break;
612f6e214c7SGavin Maltby 	case SEV_SETPROPNVL:
613f6e214c7SGavin Maltby 		rc = sysevent_setpropnvl(dev, rvalp, (void *)arg, flag, cr);
614f6e214c7SGavin Maltby 		break;
615f6e214c7SGavin Maltby 	case SEV_GETPROPNVL:
616f6e214c7SGavin Maltby 		rc = sysevent_getpropnvl(dev, rvalp, (void *)arg, flag, cr);
617f6e214c7SGavin Maltby 		break;
6187c478bd9Sstevel@tonic-gate 	default:
6197c478bd9Sstevel@tonic-gate 		rc = EINVAL;
6207c478bd9Sstevel@tonic-gate 	}
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	return (rc);
6237c478bd9Sstevel@tonic-gate }
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6267c478bd9Sstevel@tonic-gate static int
sysevent_open(dev_t * devp,int flag,int otyp,cred_t * cr)6277c478bd9Sstevel@tonic-gate sysevent_open(dev_t *devp, int flag, int otyp, cred_t *cr)
6287c478bd9Sstevel@tonic-gate {
6297c478bd9Sstevel@tonic-gate 	int minor;
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
6327c478bd9Sstevel@tonic-gate 		return (EINVAL);
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	if (getminor(*devp) != 0)
6357c478bd9Sstevel@tonic-gate 		return (ENXIO);
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	minor = sysevent_minor_get();
6387c478bd9Sstevel@tonic-gate 	if (minor == 0)
6397c478bd9Sstevel@tonic-gate 		/* All minors are busy */
6407c478bd9Sstevel@tonic-gate 		return (EBUSY);
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(evchan_ctlp, minor)
6437c478bd9Sstevel@tonic-gate 	    != DDI_SUCCESS) {
6447c478bd9Sstevel@tonic-gate 		sysevent_minor_rele(minor);
6457c478bd9Sstevel@tonic-gate 		return (ENOMEM);
6467c478bd9Sstevel@tonic-gate 	}
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	*devp = makedevice(getmajor(*devp), minor);
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	return (0);
6517c478bd9Sstevel@tonic-gate }
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6547c478bd9Sstevel@tonic-gate static int
sysevent_close(dev_t dev,int flag,int otyp,cred_t * cr)6557c478bd9Sstevel@tonic-gate sysevent_close(dev_t dev, int flag, int otyp, cred_t *cr)
6567c478bd9Sstevel@tonic-gate {
6577c478bd9Sstevel@tonic-gate 	int minor = (int)getminor(dev);
6587c478bd9Sstevel@tonic-gate 	evchan_ctl_t *ctl;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
6617c478bd9Sstevel@tonic-gate 		return (EINVAL);
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	ctl = ddi_get_soft_state(evchan_ctlp, minor);
6647c478bd9Sstevel@tonic-gate 	if (ctl == NULL) {
6657c478bd9Sstevel@tonic-gate 		return (ENXIO);
6667c478bd9Sstevel@tonic-gate 	}
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	if (ctl->chp) {
6697c478bd9Sstevel@tonic-gate 		/* Release all non-persistant subscriptions */
6707c478bd9Sstevel@tonic-gate 		evch_usrunsubscribe(ctl->chp, NULL, EVCH_SUB_KEEP);
6717c478bd9Sstevel@tonic-gate 		evch_usrchanclose(ctl->chp);
6727c478bd9Sstevel@tonic-gate 	}
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	ddi_soft_state_free(evchan_ctlp, minor);
6757c478bd9Sstevel@tonic-gate 	sysevent_minor_rele(minor);
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	return (0);
6787c478bd9Sstevel@tonic-gate }
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate /* ARGSUSED */
6817c478bd9Sstevel@tonic-gate static int
sysevent_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)6827c478bd9Sstevel@tonic-gate sysevent_info(dev_info_t *dip, ddi_info_cmd_t infocmd,
6837c478bd9Sstevel@tonic-gate     void *arg, void **result)
6847c478bd9Sstevel@tonic-gate {
6857c478bd9Sstevel@tonic-gate 	switch (infocmd) {
6867c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
6877c478bd9Sstevel@tonic-gate 		*result = sysevent_devi;
6887c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
6897c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
6907c478bd9Sstevel@tonic-gate 		*result = 0;
6917c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
6927c478bd9Sstevel@tonic-gate 	}
6937c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
6947c478bd9Sstevel@tonic-gate }
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate /* ARGSUSED */
6977c478bd9Sstevel@tonic-gate static int
sysevent_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)6987c478bd9Sstevel@tonic-gate sysevent_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
6997c478bd9Sstevel@tonic-gate {
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	if (cmd != DDI_ATTACH) {
7027c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7037c478bd9Sstevel@tonic-gate 	}
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "sysevent", S_IFCHR,
706*280798baSToomas Soome 	    0, DDI_PSEUDO, 0) == DDI_FAILURE) {
7077c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
7087c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7097c478bd9Sstevel@tonic-gate 	}
7107c478bd9Sstevel@tonic-gate 	sysevent_devi = devi;
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	sysevent_minor_init();
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
7157c478bd9Sstevel@tonic-gate }
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate static int
sysevent_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)7187c478bd9Sstevel@tonic-gate sysevent_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
7197c478bd9Sstevel@tonic-gate {
7207c478bd9Sstevel@tonic-gate 	if (cmd != DDI_DETACH) {
7217c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7227c478bd9Sstevel@tonic-gate 	}
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	sysevent_minor_free(sysevent_minor_bitmap);
7257c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
7267c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
7277c478bd9Sstevel@tonic-gate }
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate static struct cb_ops sysevent_cb_ops = {
730*280798baSToomas Soome 	.cb_open = sysevent_open,
731*280798baSToomas Soome 	.cb_close = sysevent_close,
732*280798baSToomas Soome 	.cb_strategy = nodev,
733*280798baSToomas Soome 	.cb_print = nodev,
734*280798baSToomas Soome 	.cb_dump = nodev,
735*280798baSToomas Soome 	.cb_read = nodev,
736*280798baSToomas Soome 	.cb_write = nodev,
737*280798baSToomas Soome 	.cb_ioctl = sysevent_ioctl,
738*280798baSToomas Soome 	.cb_devmap = nodev,
739*280798baSToomas Soome 	.cb_mmap = nodev,
740*280798baSToomas Soome 	.cb_segmap = nodev,
741*280798baSToomas Soome 	.cb_chpoll = nochpoll,
742*280798baSToomas Soome 	.cb_prop_op = ddi_prop_op,
743*280798baSToomas Soome 	.cb_str = NULL,
744*280798baSToomas Soome 	.cb_flag = D_NEW | D_MP,
745*280798baSToomas Soome 	.cb_rev = CB_REV,
746*280798baSToomas Soome 	.cb_aread = NULL,
747*280798baSToomas Soome 	.cb_awrite = NULL
7487c478bd9Sstevel@tonic-gate };
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate static struct dev_ops sysevent_ops = {
7517c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
7527c478bd9Sstevel@tonic-gate 	0,			/* refcnt  */
7537c478bd9Sstevel@tonic-gate 	sysevent_info,		/* info */
7547c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
7557c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
7567c478bd9Sstevel@tonic-gate 	sysevent_attach,	/* attach */
7577c478bd9Sstevel@tonic-gate 	sysevent_detach,	/* detach */
7587c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
7597c478bd9Sstevel@tonic-gate 	&sysevent_cb_ops,	/* driver operations */
760*280798baSToomas Soome 	NULL,			/* no bus operations */
76119397407SSherry Moore 	nulldev,		/* power */
76219397407SSherry Moore 	ddi_quiesce_not_needed,		/* quiesce */
7637c478bd9Sstevel@tonic-gate };
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
76619397407SSherry Moore 	&mod_driverops, "sysevent driver", &sysevent_ops
7677c478bd9Sstevel@tonic-gate };
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
7707c478bd9Sstevel@tonic-gate 	MODREV_1, &modldrv, NULL
7717c478bd9Sstevel@tonic-gate };
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate int
_init(void)7747c478bd9Sstevel@tonic-gate _init(void)
7757c478bd9Sstevel@tonic-gate {
7767c478bd9Sstevel@tonic-gate 	int s;
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	s = ddi_soft_state_init(&evchan_ctlp, sizeof (evchan_ctl_t), 1);
7797c478bd9Sstevel@tonic-gate 	if (s != 0)
7807c478bd9Sstevel@tonic-gate 		return (s);
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	if ((s = mod_install(&modlinkage)) != 0)
7837c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&evchan_ctlp);
7847c478bd9Sstevel@tonic-gate 	return (s);
7857c478bd9Sstevel@tonic-gate }
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate int
_fini(void)7887c478bd9Sstevel@tonic-gate _fini(void)
7897c478bd9Sstevel@tonic-gate {
7907c478bd9Sstevel@tonic-gate 	int s;
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	if ((s = mod_remove(&modlinkage)) != 0)
7937c478bd9Sstevel@tonic-gate 		return (s);
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	ddi_soft_state_fini(&evchan_ctlp);
7967c478bd9Sstevel@tonic-gate 	return (s);
7977c478bd9Sstevel@tonic-gate }
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)8007c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
8017c478bd9Sstevel@tonic-gate {
8027c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
8037c478bd9Sstevel@tonic-gate }
804