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
580ab886dSwesolows  * Common Development and Distribution License (the "License").
680ab886dSwesolows  * 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  */
2180ab886dSwesolows 
227c478bd9Sstevel@tonic-gate /*
23*f6e214c7SGavin Maltby  * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <stdio.h>
277c478bd9Sstevel@tonic-gate #include <fcntl.h>
287c478bd9Sstevel@tonic-gate #include <errno.h>
297c478bd9Sstevel@tonic-gate #include <door.h>
307c478bd9Sstevel@tonic-gate #include <unistd.h>
317c478bd9Sstevel@tonic-gate #include <stddef.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <strings.h>
357c478bd9Sstevel@tonic-gate #include <synch.h>
367c478bd9Sstevel@tonic-gate #include <pthread.h>
37*f6e214c7SGavin Maltby #include <signal.h>
387c478bd9Sstevel@tonic-gate #include <thread.h>
397c478bd9Sstevel@tonic-gate #include <libnvpair.h>
407c478bd9Sstevel@tonic-gate #include <assert.h>
417c478bd9Sstevel@tonic-gate #include <sys/stat.h>
427c478bd9Sstevel@tonic-gate #include <sys/types.h>
437c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
447c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
457c478bd9Sstevel@tonic-gate #include <sys/sysevent.h>
467c478bd9Sstevel@tonic-gate #include <sys/sysevent_impl.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #include "libsysevent.h"
497c478bd9Sstevel@tonic-gate #include "libsysevent_impl.h"
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * libsysevent - The system event framework library
537c478bd9Sstevel@tonic-gate  *
547c478bd9Sstevel@tonic-gate  *		This library provides routines to help with marshalling
557c478bd9Sstevel@tonic-gate  *		and unmarshalling of data contained in a sysevent event
567c478bd9Sstevel@tonic-gate  *		buffer.
577c478bd9Sstevel@tonic-gate  */
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #define	SE_ENCODE_METHOD	NV_ENCODE_NATIVE
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #define	dprint	if (libsysevent_debug) (void) printf
627c478bd9Sstevel@tonic-gate static int libsysevent_debug = 0;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate static sysevent_t *se_unpack(sysevent_t *);
657c478bd9Sstevel@tonic-gate static int cleanup_id(sysevent_handle_t *shp, uint32_t id, int type);
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * The following routines allow system event publication to the sysevent
697c478bd9Sstevel@tonic-gate  * framework.
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate  * sysevent_alloc - allocate a sysevent buffer
747c478bd9Sstevel@tonic-gate  */
757c478bd9Sstevel@tonic-gate static sysevent_t *
767c478bd9Sstevel@tonic-gate sysevent_alloc(char *class, int class_sz, char *subclass, int subclass_sz,
777c478bd9Sstevel@tonic-gate 	char *pub, int pub_sz, nvlist_t *attr_list)
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate 	int payload_sz;
807c478bd9Sstevel@tonic-gate 	int aligned_class_sz, aligned_subclass_sz, aligned_pub_sz;
817c478bd9Sstevel@tonic-gate 	size_t nvlist_sz = 0;
827c478bd9Sstevel@tonic-gate 	char *attr;
837c478bd9Sstevel@tonic-gate 	uint64_t attr_offset;
847c478bd9Sstevel@tonic-gate 	sysevent_t *ev;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	if (attr_list != NULL) {
877c478bd9Sstevel@tonic-gate 		if (nvlist_size(attr_list, &nvlist_sz, SE_ENCODE_METHOD)
887c478bd9Sstevel@tonic-gate 		    != 0) {
897c478bd9Sstevel@tonic-gate 			return (NULL);
907c478bd9Sstevel@tonic-gate 		}
917c478bd9Sstevel@tonic-gate 	}
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	/*
947c478bd9Sstevel@tonic-gate 	 * Calculate and reserve space for the class, subclass and
957c478bd9Sstevel@tonic-gate 	 * publisher strings in the event buffer
967c478bd9Sstevel@tonic-gate 	 */
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	/* String sizes must be 64-bit aligned in the event buffer */
997c478bd9Sstevel@tonic-gate 	aligned_class_sz = SE_ALIGN(class_sz);
1007c478bd9Sstevel@tonic-gate 	aligned_subclass_sz = SE_ALIGN(subclass_sz);
1017c478bd9Sstevel@tonic-gate 	aligned_pub_sz = SE_ALIGN(pub_sz);
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	payload_sz = (aligned_class_sz - sizeof (uint64_t)) +
1047c478bd9Sstevel@tonic-gate 	    (aligned_subclass_sz - sizeof (uint64_t)) +
1057c478bd9Sstevel@tonic-gate 	    (aligned_pub_sz - sizeof (uint64_t)) - sizeof (uint64_t) +
1067c478bd9Sstevel@tonic-gate 	    nvlist_sz;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	/*
1097c478bd9Sstevel@tonic-gate 	 * Allocate event buffer plus additional payload overhead.
1107c478bd9Sstevel@tonic-gate 	 */
1117c478bd9Sstevel@tonic-gate 	ev = calloc(1, sizeof (sysevent_impl_t) + payload_sz);
1127c478bd9Sstevel@tonic-gate 	if (ev == NULL) {
1137c478bd9Sstevel@tonic-gate 		return (NULL);
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	/* Initialize the event buffer data */
1177c478bd9Sstevel@tonic-gate 	SE_VERSION(ev) = SYS_EVENT_VERSION;
1187c478bd9Sstevel@tonic-gate 	(void) bcopy(class, SE_CLASS_NAME(ev), class_sz);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	SE_SUBCLASS_OFF(ev) = SE_ALIGN(offsetof(sysevent_impl_t, se_class_name))
1217c478bd9Sstevel@tonic-gate 		+ aligned_class_sz;
1227c478bd9Sstevel@tonic-gate 	(void) bcopy(subclass, SE_SUBCLASS_NAME(ev), subclass_sz);
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	SE_PUB_OFF(ev) = SE_SUBCLASS_OFF(ev) + aligned_subclass_sz;
1257c478bd9Sstevel@tonic-gate 	(void) bcopy(pub, SE_PUB_NAME(ev), pub_sz);
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	SE_PAYLOAD_SZ(ev) = payload_sz;
1287c478bd9Sstevel@tonic-gate 	SE_ATTR_PTR(ev) = (uint64_t)0;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	/* Check for attribute list */
1317c478bd9Sstevel@tonic-gate 	if (attr_list == NULL) {
1327c478bd9Sstevel@tonic-gate 		return (ev);
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	/* Copy attribute data to contiguous memory */
1367c478bd9Sstevel@tonic-gate 	SE_FLAG(ev) = SE_PACKED_BUF;
1377c478bd9Sstevel@tonic-gate 	attr_offset = SE_ATTR_OFF(ev);
1387c478bd9Sstevel@tonic-gate 	attr = (char *)((caddr_t)ev + attr_offset);
1397c478bd9Sstevel@tonic-gate 	if (nvlist_pack(attr_list, &attr, &nvlist_sz, SE_ENCODE_METHOD,
1407c478bd9Sstevel@tonic-gate 	    0) != 0) {
1417c478bd9Sstevel@tonic-gate 		free(ev);
1427c478bd9Sstevel@tonic-gate 		return (NULL);
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	return (ev);
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate /*
1497c478bd9Sstevel@tonic-gate  * sysevent_post_event - generate a system event via the sysevent framework
1507c478bd9Sstevel@tonic-gate  */
1517c478bd9Sstevel@tonic-gate int
1527c478bd9Sstevel@tonic-gate sysevent_post_event(char *class, char *subclass, char *vendor, char *pub_name,
1537c478bd9Sstevel@tonic-gate 	nvlist_t *attr_list, sysevent_id_t *eid)
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate 	int error;
1567c478bd9Sstevel@tonic-gate 	sysevent_t *ev;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	ev = sysevent_alloc_event(class, subclass, vendor, pub_name, attr_list);
1597c478bd9Sstevel@tonic-gate 	if (ev == NULL) {
1607c478bd9Sstevel@tonic-gate 		return (-1);
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	error = modctl(MODEVENTS, (uintptr_t)MODEVENTS_POST_EVENT,
16449b225e1SGavin Maltby 	    (uintptr_t)ev, (uintptr_t)SE_SIZE(ev), (uintptr_t)eid, 0);
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	sysevent_free(ev);
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	if (error) {
1697c478bd9Sstevel@tonic-gate 		errno = EIO;
1707c478bd9Sstevel@tonic-gate 		return (-1);
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	return (0);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate  * The following routines are used to free or duplicate a
1787c478bd9Sstevel@tonic-gate  * sysevent event buffer.
1797c478bd9Sstevel@tonic-gate  */
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate  * sysevent_dup - Allocate and copy an event buffer
1837c478bd9Sstevel@tonic-gate  *	Copies both packed and unpacked to unpacked sysevent.
1847c478bd9Sstevel@tonic-gate  */
1857c478bd9Sstevel@tonic-gate sysevent_t *
1867c478bd9Sstevel@tonic-gate sysevent_dup(sysevent_t *ev)
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate 	nvlist_t *nvl, *cnvl = NULL;
1897c478bd9Sstevel@tonic-gate 	uint64_t attr_offset;
1907c478bd9Sstevel@tonic-gate 	sysevent_t *copy;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	if (SE_FLAG(ev) == SE_PACKED_BUF)
1937c478bd9Sstevel@tonic-gate 		return (se_unpack(ev));
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	/* Copy event header information */
1967c478bd9Sstevel@tonic-gate 	attr_offset = SE_ATTR_OFF(ev);
1977c478bd9Sstevel@tonic-gate 	copy = calloc(1, attr_offset);
1987c478bd9Sstevel@tonic-gate 	if (copy == NULL)
1997c478bd9Sstevel@tonic-gate 		return (NULL);
2007c478bd9Sstevel@tonic-gate 	bcopy(ev, copy, attr_offset);
2017c478bd9Sstevel@tonic-gate 
20280ab886dSwesolows 	nvl = (nvlist_t *)(uintptr_t)SE_ATTR_PTR(ev);
2037c478bd9Sstevel@tonic-gate 	if (nvl && nvlist_dup(nvl, &cnvl, 0) != 0) {
2047c478bd9Sstevel@tonic-gate 		free(copy);
2057c478bd9Sstevel@tonic-gate 		return (NULL);
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	SE_ATTR_PTR(copy) = (uintptr_t)cnvl;
2097c478bd9Sstevel@tonic-gate 	SE_FLAG(copy) = 0;	/* unpacked */
2107c478bd9Sstevel@tonic-gate 	return (copy);
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate /*
2147c478bd9Sstevel@tonic-gate  * sysevent_free - Free memory allocated for an event buffer
2157c478bd9Sstevel@tonic-gate  */
2167c478bd9Sstevel@tonic-gate void
2177c478bd9Sstevel@tonic-gate sysevent_free(sysevent_t *ev)
2187c478bd9Sstevel@tonic-gate {
21980ab886dSwesolows 	nvlist_t *attr_list = (nvlist_t *)(uintptr_t)SE_ATTR_PTR(ev);
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	if (attr_list)
2227c478bd9Sstevel@tonic-gate 		nvlist_free(attr_list);
2237c478bd9Sstevel@tonic-gate 	free(ev);
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate /*
2277c478bd9Sstevel@tonic-gate  * The following routines are used to extract attribute data from a sysevent
2287c478bd9Sstevel@tonic-gate  * handle.
2297c478bd9Sstevel@tonic-gate  */
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate /*
2327c478bd9Sstevel@tonic-gate  * sysevent_get_attr_list - allocate and return an attribute associated with
2337c478bd9Sstevel@tonic-gate  *			the given sysevent buffer.
2347c478bd9Sstevel@tonic-gate  */
2357c478bd9Sstevel@tonic-gate int
2367c478bd9Sstevel@tonic-gate sysevent_get_attr_list(sysevent_t *ev, nvlist_t **nvlist)
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate 	int error;
2397c478bd9Sstevel@tonic-gate 	caddr_t attr;
2407c478bd9Sstevel@tonic-gate 	size_t attr_len;
2417c478bd9Sstevel@tonic-gate 	uint64_t attr_offset;
2427c478bd9Sstevel@tonic-gate 	nvlist_t *nvl;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	*nvlist = NULL;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	/* Duplicate attribute for an unpacked sysevent buffer */
2477c478bd9Sstevel@tonic-gate 	if (SE_FLAG(ev) != SE_PACKED_BUF) {
24880ab886dSwesolows 		nvl = (nvlist_t *)(uintptr_t)SE_ATTR_PTR(ev);
2497c478bd9Sstevel@tonic-gate 		if (nvl == NULL) {
2507c478bd9Sstevel@tonic-gate 			return (0);
2517c478bd9Sstevel@tonic-gate 		}
2527c478bd9Sstevel@tonic-gate 		if ((error = nvlist_dup(nvl, nvlist, 0)) != 0) {
2537c478bd9Sstevel@tonic-gate 			if (error == ENOMEM) {
2547c478bd9Sstevel@tonic-gate 				errno = error;
2557c478bd9Sstevel@tonic-gate 			} else {
2567c478bd9Sstevel@tonic-gate 				errno = EINVAL;
2577c478bd9Sstevel@tonic-gate 			}
2587c478bd9Sstevel@tonic-gate 			return (-1);
2597c478bd9Sstevel@tonic-gate 		}
2607c478bd9Sstevel@tonic-gate 		return (0);
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	attr_offset = SE_ATTR_OFF(ev);
2647c478bd9Sstevel@tonic-gate 	if (SE_SIZE(ev) == attr_offset) {
2657c478bd9Sstevel@tonic-gate 		return (0);
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	/* unpack nvlist */
2697c478bd9Sstevel@tonic-gate 	attr = (caddr_t)ev + attr_offset;
2707c478bd9Sstevel@tonic-gate 	attr_len = SE_SIZE(ev) - attr_offset;
2717c478bd9Sstevel@tonic-gate 	if ((error = nvlist_unpack(attr, attr_len, nvlist, 0)) != 0) {
2727c478bd9Sstevel@tonic-gate 		if (error == ENOMEM) {
2737c478bd9Sstevel@tonic-gate 			errno = error;
2747c478bd9Sstevel@tonic-gate 		} else 	{
2757c478bd9Sstevel@tonic-gate 			errno = EINVAL;
2767c478bd9Sstevel@tonic-gate 		}
2777c478bd9Sstevel@tonic-gate 		return (-1);
2787c478bd9Sstevel@tonic-gate 	}
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	return (0);
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate /*
2847c478bd9Sstevel@tonic-gate  * sysevent_attr_name - Get name of attribute
2857c478bd9Sstevel@tonic-gate  */
2867c478bd9Sstevel@tonic-gate char *
2877c478bd9Sstevel@tonic-gate sysevent_attr_name(sysevent_attr_t *attr)
2887c478bd9Sstevel@tonic-gate {
2897c478bd9Sstevel@tonic-gate 	if (attr == NULL) {
2907c478bd9Sstevel@tonic-gate 		errno = EINVAL;
2917c478bd9Sstevel@tonic-gate 		return (NULL);
2927c478bd9Sstevel@tonic-gate 	}
2937c478bd9Sstevel@tonic-gate 	return (nvpair_name((nvpair_t *)attr));
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate /*
2977c478bd9Sstevel@tonic-gate  * sysevent_attr_value - Get attribute value data and type
2987c478bd9Sstevel@tonic-gate  */
2997c478bd9Sstevel@tonic-gate int
3007c478bd9Sstevel@tonic-gate sysevent_attr_value(sysevent_attr_t *attr, sysevent_value_t *se_value)
3017c478bd9Sstevel@tonic-gate {
3027c478bd9Sstevel@tonic-gate 	nvpair_t *nvp = attr;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	if (nvp == NULL)
3057c478bd9Sstevel@tonic-gate 		return (EINVAL);
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	/* Convert DATA_TYPE_* to SE_DATA_TYPE_* */
3087c478bd9Sstevel@tonic-gate 	switch (nvpair_type(nvp)) {
3097c478bd9Sstevel@tonic-gate 	case DATA_TYPE_BYTE:
3107c478bd9Sstevel@tonic-gate 		se_value->value_type = SE_DATA_TYPE_BYTE;
3117c478bd9Sstevel@tonic-gate 		(void) nvpair_value_byte(nvp, &se_value->value.sv_byte);
3127c478bd9Sstevel@tonic-gate 		break;
3137c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT16:
3147c478bd9Sstevel@tonic-gate 		se_value->value_type = SE_DATA_TYPE_INT16;
3157c478bd9Sstevel@tonic-gate 		(void) nvpair_value_int16(nvp, &se_value->value.sv_int16);
3167c478bd9Sstevel@tonic-gate 		break;
3177c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT16:
3187c478bd9Sstevel@tonic-gate 		se_value->value_type = SE_DATA_TYPE_UINT16;
3197c478bd9Sstevel@tonic-gate 		(void) nvpair_value_uint16(nvp, &se_value->value.sv_uint16);
3207c478bd9Sstevel@tonic-gate 		break;
3217c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT32:
3227c478bd9Sstevel@tonic-gate 		se_value->value_type = SE_DATA_TYPE_INT32;
3237c478bd9Sstevel@tonic-gate 		(void) nvpair_value_int32(nvp, &se_value->value.sv_int32);
3247c478bd9Sstevel@tonic-gate 		break;
3257c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT32:
3267c478bd9Sstevel@tonic-gate 		se_value->value_type = SE_DATA_TYPE_UINT32;
3277c478bd9Sstevel@tonic-gate 		(void) nvpair_value_uint32(nvp, &se_value->value.sv_uint32);
3287c478bd9Sstevel@tonic-gate 		break;
3297c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT64:
3307c478bd9Sstevel@tonic-gate 		se_value->value_type = SE_DATA_TYPE_INT64;
3317c478bd9Sstevel@tonic-gate 		(void) nvpair_value_int64(nvp, &se_value->value.sv_int64);
3327c478bd9Sstevel@tonic-gate 		break;
3337c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT64:
3347c478bd9Sstevel@tonic-gate 		se_value->value_type = SE_DATA_TYPE_UINT64;
3357c478bd9Sstevel@tonic-gate 		(void) nvpair_value_uint64(nvp, &se_value->value.sv_uint64);
3367c478bd9Sstevel@tonic-gate 		break;
3377c478bd9Sstevel@tonic-gate 	case DATA_TYPE_STRING:
3387c478bd9Sstevel@tonic-gate 		se_value->value_type = SE_DATA_TYPE_STRING;
3397c478bd9Sstevel@tonic-gate 		(void) nvpair_value_string(nvp, &se_value->value.sv_string);
3407c478bd9Sstevel@tonic-gate 		break;
3417c478bd9Sstevel@tonic-gate 	case DATA_TYPE_BYTE_ARRAY:
3427c478bd9Sstevel@tonic-gate 		se_value->value_type = SE_DATA_TYPE_BYTES;
3437c478bd9Sstevel@tonic-gate 		(void) nvpair_value_byte_array(nvp,
3447c478bd9Sstevel@tonic-gate 		    &se_value->value.sv_bytes.data,
3457c478bd9Sstevel@tonic-gate 		    (uint_t *)&se_value->value.sv_bytes.size);
3467c478bd9Sstevel@tonic-gate 		break;
3477c478bd9Sstevel@tonic-gate 	case DATA_TYPE_HRTIME:
3487c478bd9Sstevel@tonic-gate 		se_value->value_type = SE_DATA_TYPE_TIME;
3497c478bd9Sstevel@tonic-gate 		(void) nvpair_value_hrtime(nvp, &se_value->value.sv_time);
3507c478bd9Sstevel@tonic-gate 		break;
3517c478bd9Sstevel@tonic-gate 	default:
3527c478bd9Sstevel@tonic-gate 		return (ENOTSUP);
3537c478bd9Sstevel@tonic-gate 	}
3547c478bd9Sstevel@tonic-gate 	return (0);
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate /*
3587c478bd9Sstevel@tonic-gate  * sysevent_attr_next - Get next attribute in event attribute list
3597c478bd9Sstevel@tonic-gate  */
3607c478bd9Sstevel@tonic-gate sysevent_attr_t *
3617c478bd9Sstevel@tonic-gate sysevent_attr_next(sysevent_t *ev, sysevent_attr_t *attr)
3627c478bd9Sstevel@tonic-gate {
3637c478bd9Sstevel@tonic-gate 	nvlist_t *nvl;
3647c478bd9Sstevel@tonic-gate 	nvpair_t *nvp = attr;
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	/* all user visible sysevent_t's are unpacked */
3677c478bd9Sstevel@tonic-gate 	assert(SE_FLAG(ev) != SE_PACKED_BUF);
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	if (SE_ATTR_PTR(ev) == (uint64_t)0) {
3707c478bd9Sstevel@tonic-gate 		return (NULL);
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 
37380ab886dSwesolows 	nvl = (nvlist_t *)(uintptr_t)SE_ATTR_PTR(ev);
3747c478bd9Sstevel@tonic-gate 	return (nvlist_next_nvpair(nvl, nvp));
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate /*
3787c478bd9Sstevel@tonic-gate  * sysevent_lookup_attr - Lookup attribute by name and datatype.
3797c478bd9Sstevel@tonic-gate  */
3807c478bd9Sstevel@tonic-gate int
3817c478bd9Sstevel@tonic-gate sysevent_lookup_attr(sysevent_t *ev, char *name, int datatype,
3827c478bd9Sstevel@tonic-gate 	sysevent_value_t *se_value)
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate 	nvpair_t *nvp;
3857c478bd9Sstevel@tonic-gate 	nvlist_t *nvl;
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	assert(SE_FLAG(ev) != SE_PACKED_BUF);
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	if (SE_ATTR_PTR(ev) == (uint64_t)0) {
3907c478bd9Sstevel@tonic-gate 		return (ENOENT);
3917c478bd9Sstevel@tonic-gate 	}
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	/*
3947c478bd9Sstevel@tonic-gate 	 * sysevent matches on both name and datatype
3957c478bd9Sstevel@tonic-gate 	 * nvlist_look mataches name only. So we walk
3967c478bd9Sstevel@tonic-gate 	 * nvlist manually here.
3977c478bd9Sstevel@tonic-gate 	 */
39880ab886dSwesolows 	nvl = (nvlist_t *)(uintptr_t)SE_ATTR_PTR(ev);
3997c478bd9Sstevel@tonic-gate 	nvp = nvlist_next_nvpair(nvl, NULL);
4007c478bd9Sstevel@tonic-gate 	while (nvp) {
4017c478bd9Sstevel@tonic-gate 		if ((strcmp(name, nvpair_name(nvp)) == 0) &&
4027c478bd9Sstevel@tonic-gate 		    (sysevent_attr_value(nvp, se_value) == 0) &&
4037c478bd9Sstevel@tonic-gate 		    (se_value->value_type == datatype))
4047c478bd9Sstevel@tonic-gate 			return (0);
4057c478bd9Sstevel@tonic-gate 		nvp = nvlist_next_nvpair(nvl, nvp);
4067c478bd9Sstevel@tonic-gate 	}
4077c478bd9Sstevel@tonic-gate 	return (ENOENT);
4087c478bd9Sstevel@tonic-gate }
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate /* Routines to extract event header information */
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate /*
4137c478bd9Sstevel@tonic-gate  * sysevent_get_class - Get class id
4147c478bd9Sstevel@tonic-gate  */
4157c478bd9Sstevel@tonic-gate int
4167c478bd9Sstevel@tonic-gate sysevent_get_class(sysevent_t *ev)
4177c478bd9Sstevel@tonic-gate {
4187c478bd9Sstevel@tonic-gate 	return (SE_CLASS(ev));
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate /*
4227c478bd9Sstevel@tonic-gate  * sysevent_get_subclass - Get subclass id
4237c478bd9Sstevel@tonic-gate  */
4247c478bd9Sstevel@tonic-gate int
4257c478bd9Sstevel@tonic-gate sysevent_get_subclass(sysevent_t *ev)
4267c478bd9Sstevel@tonic-gate {
4277c478bd9Sstevel@tonic-gate 	return (SE_SUBCLASS(ev));
4287c478bd9Sstevel@tonic-gate }
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate /*
4317c478bd9Sstevel@tonic-gate  * sysevent_get_class_name - Get class name string
4327c478bd9Sstevel@tonic-gate  */
4337c478bd9Sstevel@tonic-gate char *
4347c478bd9Sstevel@tonic-gate sysevent_get_class_name(sysevent_t *ev)
4357c478bd9Sstevel@tonic-gate {
4367c478bd9Sstevel@tonic-gate 	return (SE_CLASS_NAME(ev));
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate typedef enum {
4407c478bd9Sstevel@tonic-gate 	PUB_VEND,
4417c478bd9Sstevel@tonic-gate 	PUB_KEYWD,
4427c478bd9Sstevel@tonic-gate 	PUB_NAME,
4437c478bd9Sstevel@tonic-gate 	PUB_PID
4447c478bd9Sstevel@tonic-gate } se_pub_id_t;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate /*
4477c478bd9Sstevel@tonic-gate  * sysevent_get_pub - Get publisher name string
4487c478bd9Sstevel@tonic-gate  */
4497c478bd9Sstevel@tonic-gate char *
4507c478bd9Sstevel@tonic-gate sysevent_get_pub(sysevent_t *ev)
4517c478bd9Sstevel@tonic-gate {
4527c478bd9Sstevel@tonic-gate 	return (SE_PUB_NAME(ev));
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate /*
4567c478bd9Sstevel@tonic-gate  * Get the requested string pointed by the token.
4577c478bd9Sstevel@tonic-gate  *
4587c478bd9Sstevel@tonic-gate  * Return NULL if not found or for insufficient memory.
4597c478bd9Sstevel@tonic-gate  */
4607c478bd9Sstevel@tonic-gate static char *
4617c478bd9Sstevel@tonic-gate parse_pub_id(sysevent_t *ev, se_pub_id_t token)
4627c478bd9Sstevel@tonic-gate {
4637c478bd9Sstevel@tonic-gate 	int i;
4647c478bd9Sstevel@tonic-gate 	char *pub_id, *pub_element, *str, *next;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	next = pub_id = strdup(sysevent_get_pub(ev));
4677c478bd9Sstevel@tonic-gate 	for (i = 0; i <= token; ++i) {
4687c478bd9Sstevel@tonic-gate 		str = strtok_r(next, ":", &next);
4697c478bd9Sstevel@tonic-gate 		if (str == NULL) {
4707c478bd9Sstevel@tonic-gate 			free(pub_id);
4717c478bd9Sstevel@tonic-gate 			return (NULL);
4727c478bd9Sstevel@tonic-gate 		}
4737c478bd9Sstevel@tonic-gate 	}
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	pub_element = strdup(str);
4767c478bd9Sstevel@tonic-gate 	free(pub_id);
4777c478bd9Sstevel@tonic-gate 	return (pub_element);
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate /*
4817c478bd9Sstevel@tonic-gate  * Return a pointer to the string following the token
4827c478bd9Sstevel@tonic-gate  *
4837c478bd9Sstevel@tonic-gate  * Note: This is a dedicated function for parsing
4847c478bd9Sstevel@tonic-gate  * publisher strings and not for general purpose.
4857c478bd9Sstevel@tonic-gate  */
4867c478bd9Sstevel@tonic-gate static const char *
4877c478bd9Sstevel@tonic-gate pub_idx(const char *pstr, int token)
4887c478bd9Sstevel@tonic-gate {
4897c478bd9Sstevel@tonic-gate 	int i;
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	for (i = 1; i <= token; i++) {
4927c478bd9Sstevel@tonic-gate 		if ((pstr = index(pstr, ':')) == NULL)
4937c478bd9Sstevel@tonic-gate 			return (NULL);
4947c478bd9Sstevel@tonic-gate 		pstr++;
4957c478bd9Sstevel@tonic-gate 	}
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	/* String might be empty */
4987c478bd9Sstevel@tonic-gate 	if (pstr) {
4997c478bd9Sstevel@tonic-gate 		if (*pstr == '\0' || *pstr == ':')
5007c478bd9Sstevel@tonic-gate 			return (NULL);
5017c478bd9Sstevel@tonic-gate 	}
5027c478bd9Sstevel@tonic-gate 	return (pstr);
5037c478bd9Sstevel@tonic-gate }
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate char *
5067c478bd9Sstevel@tonic-gate sysevent_get_vendor_name(sysevent_t *ev)
5077c478bd9Sstevel@tonic-gate {
5087c478bd9Sstevel@tonic-gate 	return (parse_pub_id(ev, PUB_VEND));
5097c478bd9Sstevel@tonic-gate }
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate char *
5127c478bd9Sstevel@tonic-gate sysevent_get_pub_name(sysevent_t *ev)
5137c478bd9Sstevel@tonic-gate {
5147c478bd9Sstevel@tonic-gate 	return (parse_pub_id(ev, PUB_NAME));
5157c478bd9Sstevel@tonic-gate }
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate /*
5187c478bd9Sstevel@tonic-gate  * Provide the pid encoded in the publisher string
5197c478bd9Sstevel@tonic-gate  * w/o allocating any resouces.
5207c478bd9Sstevel@tonic-gate  */
5217c478bd9Sstevel@tonic-gate void
5227c478bd9Sstevel@tonic-gate sysevent_get_pid(sysevent_t *ev, pid_t *pid)
5237c478bd9Sstevel@tonic-gate {
5247c478bd9Sstevel@tonic-gate 	const char *part_str;
5257c478bd9Sstevel@tonic-gate 	const char *pub_str = sysevent_get_pub(ev);
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	*pid = (pid_t)SE_KERN_PID;
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	part_str = pub_idx(pub_str, PUB_KEYWD);
5307c478bd9Sstevel@tonic-gate 	if (part_str != NULL && strstr(part_str, SE_KERN_PUB) != NULL)
5317c478bd9Sstevel@tonic-gate 		return;
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	if ((part_str = pub_idx(pub_str, PUB_PID)) == NULL)
5347c478bd9Sstevel@tonic-gate 		return;
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	*pid = (pid_t)atoi(part_str);
5377c478bd9Sstevel@tonic-gate }
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate /*
5407c478bd9Sstevel@tonic-gate  * sysevent_get_subclass_name - Get subclass name string
5417c478bd9Sstevel@tonic-gate  */
5427c478bd9Sstevel@tonic-gate char *
5437c478bd9Sstevel@tonic-gate sysevent_get_subclass_name(sysevent_t *ev)
5447c478bd9Sstevel@tonic-gate {
5457c478bd9Sstevel@tonic-gate 	return (SE_SUBCLASS_NAME(ev));
5467c478bd9Sstevel@tonic-gate }
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate /*
5497c478bd9Sstevel@tonic-gate  * sysevent_get_seq - Get event sequence id
5507c478bd9Sstevel@tonic-gate  */
5517c478bd9Sstevel@tonic-gate uint64_t
5527c478bd9Sstevel@tonic-gate sysevent_get_seq(sysevent_t *ev)
5537c478bd9Sstevel@tonic-gate {
5547c478bd9Sstevel@tonic-gate 	return (SE_SEQ(ev));
5557c478bd9Sstevel@tonic-gate }
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate /*
5587c478bd9Sstevel@tonic-gate  * sysevent_get_time - Get event timestamp
5597c478bd9Sstevel@tonic-gate  */
5607c478bd9Sstevel@tonic-gate void
5617c478bd9Sstevel@tonic-gate sysevent_get_time(sysevent_t *ev, hrtime_t *etime)
5627c478bd9Sstevel@tonic-gate {
5637c478bd9Sstevel@tonic-gate 	*etime = SE_TIME(ev);
5647c478bd9Sstevel@tonic-gate }
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate /*
5677c478bd9Sstevel@tonic-gate  * sysevent_get_size - Get event buffer size
5687c478bd9Sstevel@tonic-gate  */
5697c478bd9Sstevel@tonic-gate size_t
5707c478bd9Sstevel@tonic-gate sysevent_get_size(sysevent_t *ev)
5717c478bd9Sstevel@tonic-gate {
5727c478bd9Sstevel@tonic-gate 	return ((size_t)SE_SIZE(ev));
5737c478bd9Sstevel@tonic-gate }
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate /*
5767c478bd9Sstevel@tonic-gate  * The following routines are used by devfsadm_mod.c to propagate event
5777c478bd9Sstevel@tonic-gate  * buffers to devfsadmd.  These routines will serve as the basis for
5787c478bd9Sstevel@tonic-gate  * event channel publication and subscription.
5797c478bd9Sstevel@tonic-gate  */
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate /*
5827c478bd9Sstevel@tonic-gate  * sysevent_alloc_event -
5837c478bd9Sstevel@tonic-gate  *	allocate a sysevent buffer for sending through an established event
5847c478bd9Sstevel@tonic-gate  *	channel.
5857c478bd9Sstevel@tonic-gate  */
5867c478bd9Sstevel@tonic-gate sysevent_t *
5877c478bd9Sstevel@tonic-gate sysevent_alloc_event(char *class, char *subclass, char *vendor, char *pub_name,
5887c478bd9Sstevel@tonic-gate 	nvlist_t *attr_list)
5897c478bd9Sstevel@tonic-gate {
5907c478bd9Sstevel@tonic-gate 	int class_sz, subclass_sz, pub_sz;
5917c478bd9Sstevel@tonic-gate 	char *pub_id;
5927c478bd9Sstevel@tonic-gate 	sysevent_t *ev;
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	if ((class == NULL) || (subclass == NULL) || (vendor == NULL) ||
5957c478bd9Sstevel@tonic-gate 	    (pub_name == NULL)) {
5967c478bd9Sstevel@tonic-gate 		errno = EINVAL;
5977c478bd9Sstevel@tonic-gate 		return (NULL);
5987c478bd9Sstevel@tonic-gate 	}
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	class_sz = strlen(class) + 1;
6017c478bd9Sstevel@tonic-gate 	subclass_sz = strlen(subclass) + 1;
6027c478bd9Sstevel@tonic-gate 	if ((class_sz > MAX_CLASS_LEN) ||
6037c478bd9Sstevel@tonic-gate 	    (subclass_sz > MAX_SUBCLASS_LEN)) {
6047c478bd9Sstevel@tonic-gate 		errno = EINVAL;
6057c478bd9Sstevel@tonic-gate 		return (NULL);
6067c478bd9Sstevel@tonic-gate 	}
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	/*
6097c478bd9Sstevel@tonic-gate 	 * Calculate the publisher size plus string seperators and maximum
6107c478bd9Sstevel@tonic-gate 	 * pid characters
6117c478bd9Sstevel@tonic-gate 	 */
6127c478bd9Sstevel@tonic-gate 	pub_sz = strlen(vendor) + sizeof (SE_USR_PUB) + strlen(pub_name) + 14;
6137c478bd9Sstevel@tonic-gate 	if (pub_sz > MAX_PUB_LEN) {
6147c478bd9Sstevel@tonic-gate 		errno = EINVAL;
6157c478bd9Sstevel@tonic-gate 		return (NULL);
6167c478bd9Sstevel@tonic-gate 	}
6177c478bd9Sstevel@tonic-gate 	pub_id = malloc(pub_sz);
6187c478bd9Sstevel@tonic-gate 	if (pub_id == NULL) {
6197c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
6207c478bd9Sstevel@tonic-gate 		return (NULL);
6217c478bd9Sstevel@tonic-gate 	}
6227c478bd9Sstevel@tonic-gate 	if (snprintf(pub_id, pub_sz, "%s:%s%s:%d", vendor, SE_USR_PUB,
6237c478bd9Sstevel@tonic-gate 	    pub_name, (int)getpid()) >= pub_sz) {
6247c478bd9Sstevel@tonic-gate 		free(pub_id);
6257c478bd9Sstevel@tonic-gate 		errno = EINVAL;
6267c478bd9Sstevel@tonic-gate 		return (NULL);
6277c478bd9Sstevel@tonic-gate 	}
6287c478bd9Sstevel@tonic-gate 	pub_sz = strlen(pub_id) + 1;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	ev = sysevent_alloc(class, class_sz, subclass, subclass_sz,
6317c478bd9Sstevel@tonic-gate 	    pub_id, pub_sz, attr_list);
6327c478bd9Sstevel@tonic-gate 	free(pub_id);
6337c478bd9Sstevel@tonic-gate 	if (ev == NULL) {
6347c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
6357c478bd9Sstevel@tonic-gate 		return (NULL);
6367c478bd9Sstevel@tonic-gate 	}
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	return (ev);
6397c478bd9Sstevel@tonic-gate }
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate /*
6427c478bd9Sstevel@tonic-gate  * se_unpack - unpack nvlist to a searchable list.
6437c478bd9Sstevel@tonic-gate  *	If already unpacked, will do a dup.
6447c478bd9Sstevel@tonic-gate  */
6457c478bd9Sstevel@tonic-gate static sysevent_t *
6467c478bd9Sstevel@tonic-gate se_unpack(sysevent_t *ev)
6477c478bd9Sstevel@tonic-gate {
6487c478bd9Sstevel@tonic-gate 	caddr_t attr;
6497c478bd9Sstevel@tonic-gate 	size_t attr_len;
6507c478bd9Sstevel@tonic-gate 	nvlist_t *attrp = NULL;
6517c478bd9Sstevel@tonic-gate 	uint64_t attr_offset;
6527c478bd9Sstevel@tonic-gate 	sysevent_t *copy;
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	assert(SE_FLAG(ev) == SE_PACKED_BUF);
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	/* Copy event header information */
6577c478bd9Sstevel@tonic-gate 	attr_offset = SE_ATTR_OFF(ev);
6587c478bd9Sstevel@tonic-gate 	copy = calloc(1, attr_offset);
6597c478bd9Sstevel@tonic-gate 	if (copy == NULL)
6607c478bd9Sstevel@tonic-gate 		return (NULL);
6617c478bd9Sstevel@tonic-gate 	bcopy(ev, copy, attr_offset);
6627c478bd9Sstevel@tonic-gate 	SE_FLAG(copy) = 0;	/* unpacked */
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	/* unpack nvlist */
6657c478bd9Sstevel@tonic-gate 	attr = (caddr_t)ev + attr_offset;
6667c478bd9Sstevel@tonic-gate 	attr_len = SE_SIZE(ev) - attr_offset;
6677c478bd9Sstevel@tonic-gate 	if (attr_len == 0) {
6687c478bd9Sstevel@tonic-gate 		return (copy);
6697c478bd9Sstevel@tonic-gate 	}
6707c478bd9Sstevel@tonic-gate 	if (nvlist_unpack(attr, attr_len, &attrp, 0) != 0) {
6717c478bd9Sstevel@tonic-gate 		free(copy);
6727c478bd9Sstevel@tonic-gate 		return (NULL);
6737c478bd9Sstevel@tonic-gate 	}
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	SE_ATTR_PTR(copy) = (uintptr_t)attrp;
6767c478bd9Sstevel@tonic-gate 	return (copy);
6777c478bd9Sstevel@tonic-gate }
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate /*
6807c478bd9Sstevel@tonic-gate  * se_print - Prints elements in an event buffer
6817c478bd9Sstevel@tonic-gate  */
6827c478bd9Sstevel@tonic-gate void
6837c478bd9Sstevel@tonic-gate se_print(FILE *fp, sysevent_t *ev)
6847c478bd9Sstevel@tonic-gate {
6857c478bd9Sstevel@tonic-gate 	char *vendor, *pub;
6867c478bd9Sstevel@tonic-gate 	pid_t pid;
6877c478bd9Sstevel@tonic-gate 	hrtime_t hrt;
6887c478bd9Sstevel@tonic-gate 	nvlist_t *attr_list = NULL;
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	(void) sysevent_get_time(ev, &hrt);
6917c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "received sysevent id = 0X%llx:%llx\n",
6927c478bd9Sstevel@tonic-gate 	    hrt, (longlong_t)sysevent_get_seq(ev));
6937c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "\tclass = %s\n", sysevent_get_class_name(ev));
6947c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "\tsubclass = %s\n", sysevent_get_subclass_name(ev));
6957c478bd9Sstevel@tonic-gate 	if ((vendor =  sysevent_get_vendor_name(ev)) != NULL) {
6967c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\tvendor = %s\n", vendor);
6977c478bd9Sstevel@tonic-gate 		free(vendor);
6987c478bd9Sstevel@tonic-gate 	}
6997c478bd9Sstevel@tonic-gate 	if ((pub = sysevent_get_pub_name(ev)) != NULL) {
7007c478bd9Sstevel@tonic-gate 		sysevent_get_pid(ev, &pid);
7017c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\tpublisher = %s:%d\n", pub, (int)pid);
7027c478bd9Sstevel@tonic-gate 		free(pub);
7037c478bd9Sstevel@tonic-gate 	}
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	if (sysevent_get_attr_list(ev, &attr_list) == 0 && attr_list != NULL) {
7067c478bd9Sstevel@tonic-gate 		nvlist_print(fp, attr_list);
7077c478bd9Sstevel@tonic-gate 		nvlist_free(attr_list);
7087c478bd9Sstevel@tonic-gate 	}
7097c478bd9Sstevel@tonic-gate }
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate /*
7127c478bd9Sstevel@tonic-gate  * The following routines are provided to support establishment and use
7137c478bd9Sstevel@tonic-gate  * of sysevent channels.  A sysevent channel is established between
7147c478bd9Sstevel@tonic-gate  * publishers and subscribers of sysevents for an agreed upon channel name.
7157c478bd9Sstevel@tonic-gate  * These routines currently support sysevent channels between user-level
7167c478bd9Sstevel@tonic-gate  * applications running on the same system.
7177c478bd9Sstevel@tonic-gate  *
7187c478bd9Sstevel@tonic-gate  * Sysevent channels may be created by a single publisher or subscriber process.
7197c478bd9Sstevel@tonic-gate  * Once established, up to MAX_SUBSRCIBERS subscribers may subscribe interest in
7207c478bd9Sstevel@tonic-gate  * receiving sysevent notifications on the named channel.  At present, only
7217c478bd9Sstevel@tonic-gate  * one publisher is allowed per sysevent channel.
7227c478bd9Sstevel@tonic-gate  *
7237c478bd9Sstevel@tonic-gate  * The registration information for each channel is kept in the kernel.  A
7247c478bd9Sstevel@tonic-gate  * kernel-based registration was chosen for persistence and reliability reasons.
7257c478bd9Sstevel@tonic-gate  * If either a publisher or a subscriber exits for any reason, the channel
7267c478bd9Sstevel@tonic-gate  * properties are maintained until all publishers and subscribers have exited.
7277c478bd9Sstevel@tonic-gate  * Additionally, an in-kernel registration allows the API to be extended to
7287c478bd9Sstevel@tonic-gate  * include kernel subscribers as well as userland subscribers in the future.
7297c478bd9Sstevel@tonic-gate  *
7307c478bd9Sstevel@tonic-gate  * To insure fast lookup of subscriptions, a cached copy of the registration
7317c478bd9Sstevel@tonic-gate  * is kept and maintained for the publisher process.  Updates are made
7327c478bd9Sstevel@tonic-gate  * everytime a change is made in the kernel.  Changes to the registration are
7337c478bd9Sstevel@tonic-gate  * expected to be infrequent.
7347c478bd9Sstevel@tonic-gate  *
7357c478bd9Sstevel@tonic-gate  * Channel communication between publisher and subscriber processes is
7367c478bd9Sstevel@tonic-gate  * implemented primarily via doors.  Each publisher creates a door for
7377c478bd9Sstevel@tonic-gate  * registration notifications and each subscriber creates a door for event
7387c478bd9Sstevel@tonic-gate  * delivery.
7397c478bd9Sstevel@tonic-gate  *
7407c478bd9Sstevel@tonic-gate  * Most of these routines are used by syseventd(1M), the sysevent publisher
7417c478bd9Sstevel@tonic-gate  * for the syseventd channel.  Processes wishing to receive sysevent
7427c478bd9Sstevel@tonic-gate  * notifications from syseventd may use a set of public
7437c478bd9Sstevel@tonic-gate  * APIs designed to subscribe to syseventd sysevents.  The subscription
7447c478bd9Sstevel@tonic-gate  * APIs are implemented in accordance with PSARC/2001/076.
7457c478bd9Sstevel@tonic-gate  *
7467c478bd9Sstevel@tonic-gate  */
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate /*
7497c478bd9Sstevel@tonic-gate  * Door handlers for the channel subscribers
7507c478bd9Sstevel@tonic-gate  */
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate /*
7537c478bd9Sstevel@tonic-gate  * subscriber_event_handler - generic event handling wrapper for subscribers
7547c478bd9Sstevel@tonic-gate  *			This handler is used to process incoming sysevent
7557c478bd9Sstevel@tonic-gate  *			notifications from channel publishers.
7567c478bd9Sstevel@tonic-gate  *			It is created as a seperate thread in each subscriber
7577c478bd9Sstevel@tonic-gate  *			process per subscription.
7587c478bd9Sstevel@tonic-gate  */
7597c478bd9Sstevel@tonic-gate static void
7607c478bd9Sstevel@tonic-gate subscriber_event_handler(sysevent_handle_t *shp)
7617c478bd9Sstevel@tonic-gate {
7627c478bd9Sstevel@tonic-gate 	subscriber_priv_t *sub_info;
7637c478bd9Sstevel@tonic-gate 	sysevent_queue_t *evqp;
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	sub_info = (subscriber_priv_t *)SH_PRIV_DATA(shp);
7667c478bd9Sstevel@tonic-gate 
767*f6e214c7SGavin Maltby 	/* See hack alert in sysevent_bind_subscriber_cmn */
768*f6e214c7SGavin Maltby 	if (sub_info->sp_handler_tid == NULL)
769*f6e214c7SGavin Maltby 		sub_info->sp_handler_tid = thr_self();
770*f6e214c7SGavin Maltby 
7717c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&sub_info->sp_qlock);
7727c478bd9Sstevel@tonic-gate 	for (;;) {
7737c478bd9Sstevel@tonic-gate 		while (sub_info->sp_evq_head == NULL && SH_BOUND(shp)) {
7747c478bd9Sstevel@tonic-gate 			(void) cond_wait(&sub_info->sp_cv, &sub_info->sp_qlock);
7757c478bd9Sstevel@tonic-gate 		}
7767c478bd9Sstevel@tonic-gate 		evqp = sub_info->sp_evq_head;
7777c478bd9Sstevel@tonic-gate 		while (evqp) {
7787c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&sub_info->sp_qlock);
7797c478bd9Sstevel@tonic-gate 			(void) sub_info->sp_func(evqp->sq_ev);
7807c478bd9Sstevel@tonic-gate 			(void) mutex_lock(&sub_info->sp_qlock);
7817c478bd9Sstevel@tonic-gate 			sub_info->sp_evq_head = sub_info->sp_evq_head->sq_next;
7827c478bd9Sstevel@tonic-gate 			free(evqp->sq_ev);
7837c478bd9Sstevel@tonic-gate 			free(evqp);
7847c478bd9Sstevel@tonic-gate 			evqp = sub_info->sp_evq_head;
7857c478bd9Sstevel@tonic-gate 		}
7867c478bd9Sstevel@tonic-gate 		if (!SH_BOUND(shp)) {
7877c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&sub_info->sp_qlock);
7887c478bd9Sstevel@tonic-gate 			return;
7897c478bd9Sstevel@tonic-gate 		}
7907c478bd9Sstevel@tonic-gate 	}
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
7937c478bd9Sstevel@tonic-gate }
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate /*
7967c478bd9Sstevel@tonic-gate  * Data structure used to communicate event subscription cache updates
7977c478bd9Sstevel@tonic-gate  * to publishers via a registration door
7987c478bd9Sstevel@tonic-gate  */
7997c478bd9Sstevel@tonic-gate struct reg_args {
8007c478bd9Sstevel@tonic-gate 	uint32_t ra_sub_id;
8017c478bd9Sstevel@tonic-gate 	uint32_t ra_op;
8027c478bd9Sstevel@tonic-gate 	uint64_t ra_buf_ptr;
8037c478bd9Sstevel@tonic-gate };
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate /*
8077c478bd9Sstevel@tonic-gate  * event_deliver_service - generic event delivery service routine.  This routine
8087c478bd9Sstevel@tonic-gate  *		is called in response to a door call to post an event.
8097c478bd9Sstevel@tonic-gate  *
8107c478bd9Sstevel@tonic-gate  */
81149b225e1SGavin Maltby /*ARGSUSED*/
8127c478bd9Sstevel@tonic-gate static void
8137c478bd9Sstevel@tonic-gate event_deliver_service(void *cookie, char *args, size_t alen,
8147c478bd9Sstevel@tonic-gate     door_desc_t *ddp, uint_t ndid)
8157c478bd9Sstevel@tonic-gate {
8167c478bd9Sstevel@tonic-gate 	int	ret = 0;
8177c478bd9Sstevel@tonic-gate 	subscriber_priv_t *sub_info;
8187c478bd9Sstevel@tonic-gate 	sysevent_handle_t *shp;
8197c478bd9Sstevel@tonic-gate 	sysevent_queue_t *new_eq;
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 	if (args == NULL || alen < sizeof (uint32_t)) {
8227c478bd9Sstevel@tonic-gate 		ret = EINVAL;
8237c478bd9Sstevel@tonic-gate 		goto return_from_door;
8247c478bd9Sstevel@tonic-gate 	}
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 	/* Publisher checking on subscriber */
8277c478bd9Sstevel@tonic-gate 	if (alen == sizeof (uint32_t)) {
8287c478bd9Sstevel@tonic-gate 		ret = 0;
8297c478bd9Sstevel@tonic-gate 		goto return_from_door;
8307c478bd9Sstevel@tonic-gate 	}
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	shp = (sysevent_handle_t *)cookie;
8337c478bd9Sstevel@tonic-gate 	if (shp == NULL) {
8347c478bd9Sstevel@tonic-gate 		ret = EBADF;
8357c478bd9Sstevel@tonic-gate 		goto return_from_door;
8367c478bd9Sstevel@tonic-gate 	}
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 	/*
8397c478bd9Sstevel@tonic-gate 	 * Mustn't block if we are trying to update the registration with
8407c478bd9Sstevel@tonic-gate 	 * the publisher
8417c478bd9Sstevel@tonic-gate 	 */
8427c478bd9Sstevel@tonic-gate 	if (mutex_trylock(SH_LOCK(shp)) != 0) {
8437c478bd9Sstevel@tonic-gate 		ret = EAGAIN;
8447c478bd9Sstevel@tonic-gate 		goto return_from_door;
8457c478bd9Sstevel@tonic-gate 	}
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	if (!SH_BOUND(shp)) {
8487c478bd9Sstevel@tonic-gate 		ret = EBADF;
8497c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(SH_LOCK(shp));
8507c478bd9Sstevel@tonic-gate 		goto return_from_door;
8517c478bd9Sstevel@tonic-gate 	}
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	sub_info = (subscriber_priv_t *)SH_PRIV_DATA(shp);
8547c478bd9Sstevel@tonic-gate 	if (sub_info == NULL) {
8557c478bd9Sstevel@tonic-gate 		ret = EBADF;
8567c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(SH_LOCK(shp));
8577c478bd9Sstevel@tonic-gate 		goto return_from_door;
8587c478bd9Sstevel@tonic-gate 	}
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	new_eq = (sysevent_queue_t *)calloc(1,
8617c478bd9Sstevel@tonic-gate 	    sizeof (sysevent_queue_t));
8627c478bd9Sstevel@tonic-gate 	if (new_eq == NULL) {
8637c478bd9Sstevel@tonic-gate 		ret = EAGAIN;
8647c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(SH_LOCK(shp));
8657c478bd9Sstevel@tonic-gate 		goto return_from_door;
8667c478bd9Sstevel@tonic-gate 	}
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	/*
8697c478bd9Sstevel@tonic-gate 	 * Allocate and copy the event buffer into the subscriber's
8707c478bd9Sstevel@tonic-gate 	 * address space
8717c478bd9Sstevel@tonic-gate 	 */
8727c478bd9Sstevel@tonic-gate 	new_eq->sq_ev = calloc(1, alen);
8737c478bd9Sstevel@tonic-gate 	if (new_eq->sq_ev == NULL) {
8747c478bd9Sstevel@tonic-gate 		free(new_eq);
8757c478bd9Sstevel@tonic-gate 		ret = EAGAIN;
8767c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(SH_LOCK(shp));
8777c478bd9Sstevel@tonic-gate 		goto return_from_door;
8787c478bd9Sstevel@tonic-gate 	}
8797c478bd9Sstevel@tonic-gate 	(void) bcopy(args, new_eq->sq_ev, alen);
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&sub_info->sp_qlock);
8827c478bd9Sstevel@tonic-gate 	if (sub_info->sp_evq_head == NULL) {
8837c478bd9Sstevel@tonic-gate 		sub_info->sp_evq_head = new_eq;
8847c478bd9Sstevel@tonic-gate 	} else {
8857c478bd9Sstevel@tonic-gate 		sub_info->sp_evq_tail->sq_next = new_eq;
8867c478bd9Sstevel@tonic-gate 	}
8877c478bd9Sstevel@tonic-gate 	sub_info->sp_evq_tail = new_eq;
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	(void) cond_signal(&sub_info->sp_cv);
8907c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&sub_info->sp_qlock);
8917c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(SH_LOCK(shp));
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate return_from_door:
8947c478bd9Sstevel@tonic-gate 	(void) door_return((void *)&ret, sizeof (ret), NULL, 0);
8957c478bd9Sstevel@tonic-gate 	(void) door_return(NULL, 0, NULL, 0);
8967c478bd9Sstevel@tonic-gate }
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate /*
8997c478bd9Sstevel@tonic-gate  * Sysevent subscription information is maintained in the kernel.  Updates
9007c478bd9Sstevel@tonic-gate  * to the in-kernel registration database is expected to be infrequent and
9017c478bd9Sstevel@tonic-gate  * offers consistency for publishers and subscribers that may come and go
9027c478bd9Sstevel@tonic-gate  * for a given channel.
9037c478bd9Sstevel@tonic-gate  *
9047c478bd9Sstevel@tonic-gate  * To expedite registration lookups by publishers, a cached copy of the
9057c478bd9Sstevel@tonic-gate  * kernel registration database is kept per-channel.  Caches are invalidated
9067c478bd9Sstevel@tonic-gate  * and refreshed upon state changes to the in-kernel registration database.
9077c478bd9Sstevel@tonic-gate  *
9087c478bd9Sstevel@tonic-gate  * To prevent stale subscriber data, publishers may remove subsriber
9097c478bd9Sstevel@tonic-gate  * registrations from the in-kernel registration database in the event
9107c478bd9Sstevel@tonic-gate  * that a particular subscribing process is unresponsive.
9117c478bd9Sstevel@tonic-gate  *
9127c478bd9Sstevel@tonic-gate  * The following routines provide a mechanism to update publisher and subscriber
9137c478bd9Sstevel@tonic-gate  * information for a specified channel.
9147c478bd9Sstevel@tonic-gate  */
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate /*
9177c478bd9Sstevel@tonic-gate  * clnt_deliver_event - Deliver an event through the consumer's event
9187c478bd9Sstevel@tonic-gate  *			delivery door
9197c478bd9Sstevel@tonic-gate  *
9207c478bd9Sstevel@tonic-gate  * Returns -1 if message not delivered. With errno set to cause of error.
9217c478bd9Sstevel@tonic-gate  * Returns 0 for success with the results returned in posting buffer.
9227c478bd9Sstevel@tonic-gate  */
9237c478bd9Sstevel@tonic-gate static int
9247c478bd9Sstevel@tonic-gate clnt_deliver_event(int service_door, void *data, size_t datalen,
9257c478bd9Sstevel@tonic-gate 	void *result, size_t rlen)
9267c478bd9Sstevel@tonic-gate {
9277c478bd9Sstevel@tonic-gate 	int error = 0;
9287c478bd9Sstevel@tonic-gate 	door_arg_t door_arg;
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	door_arg.rbuf = result;
9317c478bd9Sstevel@tonic-gate 	door_arg.rsize = rlen;
9327c478bd9Sstevel@tonic-gate 	door_arg.data_ptr = data;
9337c478bd9Sstevel@tonic-gate 	door_arg.data_size = datalen;
9347c478bd9Sstevel@tonic-gate 	door_arg.desc_ptr = NULL;
9357c478bd9Sstevel@tonic-gate 	door_arg.desc_num = 0;
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	/*
9387c478bd9Sstevel@tonic-gate 	 * Make door call
9397c478bd9Sstevel@tonic-gate 	 */
9407c478bd9Sstevel@tonic-gate 	while ((error = door_call(service_door, &door_arg)) != 0) {
9417c478bd9Sstevel@tonic-gate 		if (errno == EAGAIN || errno == EINTR) {
9427c478bd9Sstevel@tonic-gate 			continue;
9437c478bd9Sstevel@tonic-gate 		} else {
9447c478bd9Sstevel@tonic-gate 			error = errno;
9457c478bd9Sstevel@tonic-gate 			break;
9467c478bd9Sstevel@tonic-gate 		}
9477c478bd9Sstevel@tonic-gate 	}
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 	return (error);
9507c478bd9Sstevel@tonic-gate }
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate static int
9537c478bd9Sstevel@tonic-gate update_publisher_cache(subscriber_priv_t *sub_info, int update_op,
9547c478bd9Sstevel@tonic-gate 	uint32_t sub_id, size_t datasz, uchar_t *data)
9557c478bd9Sstevel@tonic-gate {
9567c478bd9Sstevel@tonic-gate 	int pub_fd;
9577c478bd9Sstevel@tonic-gate 	uint32_t result = 0;
9587c478bd9Sstevel@tonic-gate 	struct reg_args *rargs;
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	rargs = (struct reg_args *)calloc(1, sizeof (struct reg_args) +
9617c478bd9Sstevel@tonic-gate 	    datasz);
9627c478bd9Sstevel@tonic-gate 	if (rargs == NULL) {
9637c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
9647c478bd9Sstevel@tonic-gate 		return (-1);
9657c478bd9Sstevel@tonic-gate 	}
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 	rargs->ra_sub_id = sub_id;
9687c478bd9Sstevel@tonic-gate 	rargs->ra_op = update_op;
9697c478bd9Sstevel@tonic-gate 	bcopy(data, (char *)&rargs->ra_buf_ptr, datasz);
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	pub_fd = open(sub_info->sp_door_name, O_RDONLY);
9727c478bd9Sstevel@tonic-gate 	(void) clnt_deliver_event(pub_fd, (void *)rargs,
9737c478bd9Sstevel@tonic-gate 	    sizeof (struct reg_args) + datasz, &result, sizeof (result));
9747c478bd9Sstevel@tonic-gate 	(void) close(pub_fd);
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 	free(rargs);
9777c478bd9Sstevel@tonic-gate 	if (result != 0) {
9787c478bd9Sstevel@tonic-gate 		errno = result;
9797c478bd9Sstevel@tonic-gate 		return (-1);
9807c478bd9Sstevel@tonic-gate 	}
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 	return (0);
9837c478bd9Sstevel@tonic-gate }
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate /*
9877c478bd9Sstevel@tonic-gate  * update_kernel_registration - update the in-kernel registration for the
9887c478bd9Sstevel@tonic-gate  * given channel.
9897c478bd9Sstevel@tonic-gate  */
9907c478bd9Sstevel@tonic-gate static int
9917c478bd9Sstevel@tonic-gate update_kernel_registration(sysevent_handle_t *shp, int update_type,
9927c478bd9Sstevel@tonic-gate 	int update_op, uint32_t *sub_id, size_t datasz, uchar_t *data)
9937c478bd9Sstevel@tonic-gate {
9947c478bd9Sstevel@tonic-gate 	int error;
9957c478bd9Sstevel@tonic-gate 	char *channel_name = SH_CHANNEL_NAME(shp);
9967c478bd9Sstevel@tonic-gate 	se_pubsub_t udata;
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 	udata.ps_channel_name_len = strlen(channel_name) + 1;
9997c478bd9Sstevel@tonic-gate 	udata.ps_op = update_op;
10007c478bd9Sstevel@tonic-gate 	udata.ps_type = update_type;
10017c478bd9Sstevel@tonic-gate 	udata.ps_buflen = datasz;
10027c478bd9Sstevel@tonic-gate 	udata.ps_id = *sub_id;
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 	if ((error = modctl(MODEVENTS, (uintptr_t)MODEVENTS_REGISTER_EVENT,
10057c478bd9Sstevel@tonic-gate 	    (uintptr_t)channel_name, (uintptr_t)data, (uintptr_t)&udata, 0))
10067c478bd9Sstevel@tonic-gate 	    != 0) {
10077c478bd9Sstevel@tonic-gate 		return (error);
10087c478bd9Sstevel@tonic-gate 	}
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	*sub_id = udata.ps_id;
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 	return (error);
10137c478bd9Sstevel@tonic-gate }
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate /*
10167c478bd9Sstevel@tonic-gate  * get_kernel_registration - get the current subscriber registration for
10177c478bd9Sstevel@tonic-gate  * the given channel
10187c478bd9Sstevel@tonic-gate  */
10197c478bd9Sstevel@tonic-gate static nvlist_t *
10207c478bd9Sstevel@tonic-gate get_kernel_registration(char *channel_name, uint32_t class_id)
10217c478bd9Sstevel@tonic-gate {
10227c478bd9Sstevel@tonic-gate 	char *nvlbuf;
10237c478bd9Sstevel@tonic-gate 	nvlist_t *nvl;
10247c478bd9Sstevel@tonic-gate 	se_pubsub_t udata;
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 	nvlbuf = calloc(1, MAX_SUBSCRIPTION_SZ);
10277c478bd9Sstevel@tonic-gate 	if (nvlbuf == NULL) {
10287c478bd9Sstevel@tonic-gate 		return (NULL);
10297c478bd9Sstevel@tonic-gate 	}
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 	udata.ps_buflen = MAX_SUBSCRIPTION_SZ;
10327c478bd9Sstevel@tonic-gate 	udata.ps_channel_name_len = strlen(channel_name) + 1;
10337c478bd9Sstevel@tonic-gate 	udata.ps_id = class_id;
10347c478bd9Sstevel@tonic-gate 	udata.ps_op = SE_GET_REGISTRATION;
10357c478bd9Sstevel@tonic-gate 	udata.ps_type = PUBLISHER;
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 	if (modctl(MODEVENTS, (uintptr_t)MODEVENTS_REGISTER_EVENT,
10387c478bd9Sstevel@tonic-gate 	    (uintptr_t)channel_name, (uintptr_t)nvlbuf, (uintptr_t)&udata, 0)
10397c478bd9Sstevel@tonic-gate 	    != 0) {
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 		/* Need a bigger buffer to hold channel registration */
10427c478bd9Sstevel@tonic-gate 		if (errno == EAGAIN) {
10437c478bd9Sstevel@tonic-gate 			free(nvlbuf);
10447c478bd9Sstevel@tonic-gate 			nvlbuf = calloc(1, udata.ps_buflen);
10457c478bd9Sstevel@tonic-gate 			if (nvlbuf == NULL)
10467c478bd9Sstevel@tonic-gate 				return (NULL);
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 			/* Try again */
10497c478bd9Sstevel@tonic-gate 			if (modctl(MODEVENTS,
10507c478bd9Sstevel@tonic-gate 			    (uintptr_t)MODEVENTS_REGISTER_EVENT,
10517c478bd9Sstevel@tonic-gate 			    (uintptr_t)channel_name, (uintptr_t)nvlbuf,
10527c478bd9Sstevel@tonic-gate 			    (uintptr_t)&udata, 0) != 0) {
10537c478bd9Sstevel@tonic-gate 				free(nvlbuf);
10547c478bd9Sstevel@tonic-gate 				return (NULL);
10557c478bd9Sstevel@tonic-gate 			}
10567c478bd9Sstevel@tonic-gate 		} else {
10577c478bd9Sstevel@tonic-gate 			free(nvlbuf);
10587c478bd9Sstevel@tonic-gate 			return (NULL);
10597c478bd9Sstevel@tonic-gate 		}
10607c478bd9Sstevel@tonic-gate 	}
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 	if (nvlist_unpack(nvlbuf, udata.ps_buflen, &nvl, 0) != 0) {
10637c478bd9Sstevel@tonic-gate 		free(nvlbuf);
10647c478bd9Sstevel@tonic-gate 		return (NULL);
10657c478bd9Sstevel@tonic-gate 	}
10667c478bd9Sstevel@tonic-gate 	free(nvlbuf);
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	return (nvl);
10697c478bd9Sstevel@tonic-gate }
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate /*
10727c478bd9Sstevel@tonic-gate  * The following routines provide a mechanism for publishers to maintain
10737c478bd9Sstevel@tonic-gate  * subscriber information.
10747c478bd9Sstevel@tonic-gate  */
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate static void
10777c478bd9Sstevel@tonic-gate dealloc_subscribers(sysevent_handle_t *shp)
10787c478bd9Sstevel@tonic-gate {
10797c478bd9Sstevel@tonic-gate 	int i;
10807c478bd9Sstevel@tonic-gate 	subscriber_data_t *sub;
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	for (i = 1; i <= MAX_SUBSCRIBERS; ++i) {
10837c478bd9Sstevel@tonic-gate 		sub = SH_SUBSCRIBER(shp, i);
10847c478bd9Sstevel@tonic-gate 		if (sub != NULL) {
10857c478bd9Sstevel@tonic-gate 			free(sub->sd_door_name);
10867c478bd9Sstevel@tonic-gate 			free(sub);
10877c478bd9Sstevel@tonic-gate 		}
10887c478bd9Sstevel@tonic-gate 		SH_SUBSCRIBER(shp, i) = NULL;
10897c478bd9Sstevel@tonic-gate 	}
10907c478bd9Sstevel@tonic-gate }
10917c478bd9Sstevel@tonic-gate 
109249b225e1SGavin Maltby /*ARGSUSED*/
10937c478bd9Sstevel@tonic-gate static int
10947c478bd9Sstevel@tonic-gate alloc_subscriber(sysevent_handle_t *shp, uint32_t sub_id, int oflag)
10957c478bd9Sstevel@tonic-gate {
10967c478bd9Sstevel@tonic-gate 	subscriber_data_t *sub;
10977c478bd9Sstevel@tonic-gate 	char door_name[MAXPATHLEN];
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 	if (SH_SUBSCRIBER(shp, sub_id) != NULL) {
11007c478bd9Sstevel@tonic-gate 		return (0);
11017c478bd9Sstevel@tonic-gate 	}
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate 	/* Allocate and initialize the subscriber data */
11047c478bd9Sstevel@tonic-gate 	sub = (subscriber_data_t *)calloc(1,
11057c478bd9Sstevel@tonic-gate 	    sizeof (subscriber_data_t));
11067c478bd9Sstevel@tonic-gate 	if (sub == NULL) {
11077c478bd9Sstevel@tonic-gate 		return (-1);
11087c478bd9Sstevel@tonic-gate 	}
11097c478bd9Sstevel@tonic-gate 	if (snprintf(door_name, MAXPATHLEN, "%s/%d",
11107c478bd9Sstevel@tonic-gate 	    SH_CHANNEL_PATH(shp), sub_id) >= MAXPATHLEN) {
11117c478bd9Sstevel@tonic-gate 		free(sub);
11127c478bd9Sstevel@tonic-gate 		return (-1);
11137c478bd9Sstevel@tonic-gate 	}
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 	sub->sd_flag = ACTIVE;
11167c478bd9Sstevel@tonic-gate 	sub->sd_door_name = strdup(door_name);
11177c478bd9Sstevel@tonic-gate 	if (sub->sd_door_name == NULL) {
11187c478bd9Sstevel@tonic-gate 		free(sub);
11197c478bd9Sstevel@tonic-gate 		return (-1);
11207c478bd9Sstevel@tonic-gate 	}
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	SH_SUBSCRIBER(shp, sub_id) = sub;
11237c478bd9Sstevel@tonic-gate 	return (0);
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate }
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate /*
11287c478bd9Sstevel@tonic-gate  * The following routines are used to update and maintain the registration cache
11297c478bd9Sstevel@tonic-gate  * for a particular sysevent channel.
11307c478bd9Sstevel@tonic-gate  */
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate static uint32_t
11337c478bd9Sstevel@tonic-gate hash_func(const char *s)
11347c478bd9Sstevel@tonic-gate {
11357c478bd9Sstevel@tonic-gate 	uint32_t result = 0;
11367c478bd9Sstevel@tonic-gate 	uint_t g;
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 	while (*s != '\0') {
11397c478bd9Sstevel@tonic-gate 		result <<= 4;
11407c478bd9Sstevel@tonic-gate 		result += (uint32_t)*s++;
11417c478bd9Sstevel@tonic-gate 		g = result & 0xf0000000;
11427c478bd9Sstevel@tonic-gate 		if (g != 0) {
11437c478bd9Sstevel@tonic-gate 			result ^= g >> 24;
11447c478bd9Sstevel@tonic-gate 			result ^= g;
11457c478bd9Sstevel@tonic-gate 		}
11467c478bd9Sstevel@tonic-gate 	}
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 	return (result);
11497c478bd9Sstevel@tonic-gate }
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate subclass_lst_t *
11527c478bd9Sstevel@tonic-gate cache_find_subclass(class_lst_t *c_list, char *subclass)
11537c478bd9Sstevel@tonic-gate {
11547c478bd9Sstevel@tonic-gate 	subclass_lst_t *sc_list;
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate 	if (c_list == NULL)
11577c478bd9Sstevel@tonic-gate 		return (NULL);
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate 	sc_list = c_list->cl_subclass_list;
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 	while (sc_list != NULL) {
11627c478bd9Sstevel@tonic-gate 		if (strcmp(sc_list->sl_name, subclass) == 0) {
11637c478bd9Sstevel@tonic-gate 			return (sc_list);
11647c478bd9Sstevel@tonic-gate 		}
11657c478bd9Sstevel@tonic-gate 		sc_list = sc_list->sl_next;
11667c478bd9Sstevel@tonic-gate 	}
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 	return (NULL);
11697c478bd9Sstevel@tonic-gate }
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate static class_lst_t *
11737c478bd9Sstevel@tonic-gate cache_find_class(sysevent_handle_t *shp, char *class)
11747c478bd9Sstevel@tonic-gate {
11757c478bd9Sstevel@tonic-gate 	int index;
11767c478bd9Sstevel@tonic-gate 	class_lst_t *c_list;
11777c478bd9Sstevel@tonic-gate 	class_lst_t **class_hash = SH_CLASS_HASH(shp);
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 	if (strcmp(class, EC_ALL) == 0) {
11807c478bd9Sstevel@tonic-gate 		return (class_hash[0]);
11817c478bd9Sstevel@tonic-gate 	}
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate 	index = CLASS_HASH(class);
11847c478bd9Sstevel@tonic-gate 	c_list = class_hash[index];
11857c478bd9Sstevel@tonic-gate 	while (c_list != NULL) {
11867c478bd9Sstevel@tonic-gate 		if (strcmp(class, c_list->cl_name) == 0) {
11877c478bd9Sstevel@tonic-gate 			break;
11887c478bd9Sstevel@tonic-gate 		}
11897c478bd9Sstevel@tonic-gate 		c_list = c_list->cl_next;
11907c478bd9Sstevel@tonic-gate 	}
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 	return (c_list);
11937c478bd9Sstevel@tonic-gate }
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate static int
11967c478bd9Sstevel@tonic-gate cache_insert_subclass(class_lst_t *c_list, char **subclass_names,
11977c478bd9Sstevel@tonic-gate 	int subclass_num, uint32_t sub_id)
11987c478bd9Sstevel@tonic-gate {
11997c478bd9Sstevel@tonic-gate 	int i;
12007c478bd9Sstevel@tonic-gate 	subclass_lst_t *sc_list;
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 	for (i = 0; i < subclass_num; ++i) {
12037c478bd9Sstevel@tonic-gate 		if ((sc_list = cache_find_subclass(c_list, subclass_names[i]))
12047c478bd9Sstevel@tonic-gate 		    != NULL) {
12057c478bd9Sstevel@tonic-gate 			sc_list->sl_num[sub_id] = 1;
12067c478bd9Sstevel@tonic-gate 		} else {
12077c478bd9Sstevel@tonic-gate 			sc_list = (subclass_lst_t *)calloc(1,
12087c478bd9Sstevel@tonic-gate 			    sizeof (subclass_lst_t));
12097c478bd9Sstevel@tonic-gate 			if (sc_list == NULL)
12107c478bd9Sstevel@tonic-gate 				return (-1);
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 			sc_list->sl_name = strdup(subclass_names[i]);
12137c478bd9Sstevel@tonic-gate 			if (sc_list->sl_name == NULL) {
12147c478bd9Sstevel@tonic-gate 				free(sc_list);
12157c478bd9Sstevel@tonic-gate 				return (-1);
12167c478bd9Sstevel@tonic-gate 			}
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 			sc_list->sl_num[sub_id] = 1;
12197c478bd9Sstevel@tonic-gate 			sc_list->sl_next = c_list->cl_subclass_list;
12207c478bd9Sstevel@tonic-gate 			c_list->cl_subclass_list = sc_list;
12217c478bd9Sstevel@tonic-gate 		}
12227c478bd9Sstevel@tonic-gate 	}
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate 	return (0);
12257c478bd9Sstevel@tonic-gate }
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate static int
12287c478bd9Sstevel@tonic-gate cache_insert_class(sysevent_handle_t *shp, char *class,
12297c478bd9Sstevel@tonic-gate 	char **subclass_names, int subclass_num, uint32_t sub_id)
12307c478bd9Sstevel@tonic-gate {
12317c478bd9Sstevel@tonic-gate 	class_lst_t *c_list;
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 	if (strcmp(class, EC_ALL) == 0) {
12347c478bd9Sstevel@tonic-gate 		char *subclass_all = EC_SUB_ALL;
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 		(void) cache_insert_subclass(SH_CLASS_HASH(shp)[0],
12377c478bd9Sstevel@tonic-gate 		    (char **)&subclass_all, 1, sub_id);
12387c478bd9Sstevel@tonic-gate 		return (0);
12397c478bd9Sstevel@tonic-gate 	}
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate 	/* New class, add to the registration cache */
12427c478bd9Sstevel@tonic-gate 	if ((c_list = cache_find_class(shp, class)) == NULL) {
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate 		c_list = (class_lst_t *)calloc(1, sizeof (class_lst_t));
12457c478bd9Sstevel@tonic-gate 		if (c_list == NULL) {
12467c478bd9Sstevel@tonic-gate 			return (1);
12477c478bd9Sstevel@tonic-gate 		}
12487c478bd9Sstevel@tonic-gate 		c_list->cl_name = strdup(class);
12497c478bd9Sstevel@tonic-gate 		if (c_list->cl_name == NULL) {
12507c478bd9Sstevel@tonic-gate 			free(c_list);
12517c478bd9Sstevel@tonic-gate 			return (1);
12527c478bd9Sstevel@tonic-gate 		}
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 		c_list->cl_subclass_list = (subclass_lst_t *)
12557c478bd9Sstevel@tonic-gate 		    calloc(1, sizeof (subclass_lst_t));
12567c478bd9Sstevel@tonic-gate 		if (c_list->cl_subclass_list == NULL) {
12577c478bd9Sstevel@tonic-gate 			free(c_list->cl_name);
12587c478bd9Sstevel@tonic-gate 			free(c_list);
12597c478bd9Sstevel@tonic-gate 			return (1);
12607c478bd9Sstevel@tonic-gate 		}
12617c478bd9Sstevel@tonic-gate 		c_list->cl_subclass_list->sl_name = strdup(EC_SUB_ALL);
12627c478bd9Sstevel@tonic-gate 		if (c_list->cl_subclass_list->sl_name == NULL) {
12637c478bd9Sstevel@tonic-gate 			free(c_list->cl_subclass_list);
12647c478bd9Sstevel@tonic-gate 			free(c_list->cl_name);
12657c478bd9Sstevel@tonic-gate 			free(c_list);
12667c478bd9Sstevel@tonic-gate 			return (1);
12677c478bd9Sstevel@tonic-gate 		}
12687c478bd9Sstevel@tonic-gate 		c_list->cl_next = SH_CLASS_HASH(shp)[CLASS_HASH(class)];
12697c478bd9Sstevel@tonic-gate 		SH_CLASS_HASH(shp)[CLASS_HASH(class)] = c_list;
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 	}
12727c478bd9Sstevel@tonic-gate 
12737c478bd9Sstevel@tonic-gate 	/* Update the subclass list */
12747c478bd9Sstevel@tonic-gate 	if (cache_insert_subclass(c_list, subclass_names, subclass_num,
12757c478bd9Sstevel@tonic-gate 	    sub_id) != 0)
12767c478bd9Sstevel@tonic-gate 		return (1);
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 	return (0);
12797c478bd9Sstevel@tonic-gate }
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate static void
12827c478bd9Sstevel@tonic-gate cache_remove_all_class(sysevent_handle_t *shp, uint32_t sub_id)
12837c478bd9Sstevel@tonic-gate {
12847c478bd9Sstevel@tonic-gate 	int i;
12857c478bd9Sstevel@tonic-gate 	class_lst_t *c_list;
12867c478bd9Sstevel@tonic-gate 	subclass_lst_t *sc_list;
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate 	for (i = 0; i < CLASS_HASH_SZ + 1; ++i) {
12897c478bd9Sstevel@tonic-gate 		c_list = SH_CLASS_HASH(shp)[i];
12907c478bd9Sstevel@tonic-gate 		while (c_list != NULL) {
12917c478bd9Sstevel@tonic-gate 			sc_list = c_list->cl_subclass_list;
12927c478bd9Sstevel@tonic-gate 			while (sc_list != NULL) {
12937c478bd9Sstevel@tonic-gate 				sc_list->sl_num[sub_id] = 0;
12947c478bd9Sstevel@tonic-gate 				sc_list = sc_list->sl_next;
12957c478bd9Sstevel@tonic-gate 			}
12967c478bd9Sstevel@tonic-gate 			c_list = c_list->cl_next;
12977c478bd9Sstevel@tonic-gate 		}
12987c478bd9Sstevel@tonic-gate 	}
12997c478bd9Sstevel@tonic-gate }
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate static void
13027c478bd9Sstevel@tonic-gate cache_remove_class(sysevent_handle_t *shp, char *class, uint32_t sub_id)
13037c478bd9Sstevel@tonic-gate {
13047c478bd9Sstevel@tonic-gate 	class_lst_t *c_list;
13057c478bd9Sstevel@tonic-gate 	subclass_lst_t *sc_list;
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate 	if (strcmp(class, EC_ALL) == 0) {
13087c478bd9Sstevel@tonic-gate 		cache_remove_all_class(shp, sub_id);
13097c478bd9Sstevel@tonic-gate 		return;
13107c478bd9Sstevel@tonic-gate 	}
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate 	if ((c_list = cache_find_class(shp, class)) == NULL) {
13137c478bd9Sstevel@tonic-gate 		return;
13147c478bd9Sstevel@tonic-gate 	}
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 	sc_list = c_list->cl_subclass_list;
13177c478bd9Sstevel@tonic-gate 	while (sc_list != NULL) {
13187c478bd9Sstevel@tonic-gate 		sc_list->sl_num[sub_id] = 0;
13197c478bd9Sstevel@tonic-gate 		sc_list = sc_list->sl_next;
13207c478bd9Sstevel@tonic-gate 	}
13217c478bd9Sstevel@tonic-gate }
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate static void
13247c478bd9Sstevel@tonic-gate free_cached_registration(sysevent_handle_t *shp)
13257c478bd9Sstevel@tonic-gate {
13267c478bd9Sstevel@tonic-gate 	int i;
13277c478bd9Sstevel@tonic-gate 	class_lst_t *clist, *next_clist;
13287c478bd9Sstevel@tonic-gate 	subclass_lst_t *sc_list, *next_sc;
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate 	for (i = 0; i < CLASS_HASH_SZ + 1; i++) {
13317c478bd9Sstevel@tonic-gate 		clist = SH_CLASS_HASH(shp)[i];
13327c478bd9Sstevel@tonic-gate 		while (clist != NULL) {
13337c478bd9Sstevel@tonic-gate 			sc_list = clist->cl_subclass_list;
13347c478bd9Sstevel@tonic-gate 			while (sc_list != NULL) {
13357c478bd9Sstevel@tonic-gate 				free(sc_list->sl_name);
13367c478bd9Sstevel@tonic-gate 				next_sc = sc_list->sl_next;
13377c478bd9Sstevel@tonic-gate 				free(sc_list);
13387c478bd9Sstevel@tonic-gate 				sc_list = next_sc;
13397c478bd9Sstevel@tonic-gate 			}
13407c478bd9Sstevel@tonic-gate 			free(clist->cl_name);
13417c478bd9Sstevel@tonic-gate 			next_clist = clist->cl_next;
13427c478bd9Sstevel@tonic-gate 			free(clist);
13437c478bd9Sstevel@tonic-gate 			clist = next_clist;
13447c478bd9Sstevel@tonic-gate 		}
13457c478bd9Sstevel@tonic-gate 		SH_CLASS_HASH(shp)[i] = NULL;
13467c478bd9Sstevel@tonic-gate 	}
13477c478bd9Sstevel@tonic-gate }
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate static int
13507c478bd9Sstevel@tonic-gate create_cached_registration(sysevent_handle_t *shp,
13517c478bd9Sstevel@tonic-gate 	class_lst_t **class_hash)
13527c478bd9Sstevel@tonic-gate {
13537c478bd9Sstevel@tonic-gate 	int i, j, new_class;
13547c478bd9Sstevel@tonic-gate 	char *class_name;
13557c478bd9Sstevel@tonic-gate 	uint_t num_elem;
13567c478bd9Sstevel@tonic-gate 	uchar_t *subscribers;
13577c478bd9Sstevel@tonic-gate 	nvlist_t *nvl;
13587c478bd9Sstevel@tonic-gate 	nvpair_t *nvpair;
13597c478bd9Sstevel@tonic-gate 	class_lst_t *clist;
13607c478bd9Sstevel@tonic-gate 	subclass_lst_t *sc_list;
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate 	for (i = 0; i < CLASS_HASH_SZ + 1; ++i) {
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate 		if ((nvl = get_kernel_registration(SH_CHANNEL_NAME(shp), i))
13657c478bd9Sstevel@tonic-gate 		    == NULL) {
13667c478bd9Sstevel@tonic-gate 			if (errno == ENOENT) {
13677c478bd9Sstevel@tonic-gate 				class_hash[i] = NULL;
13687c478bd9Sstevel@tonic-gate 				continue;
13697c478bd9Sstevel@tonic-gate 			} else {
13707c478bd9Sstevel@tonic-gate 				goto create_failed;
13717c478bd9Sstevel@tonic-gate 			}
13727c478bd9Sstevel@tonic-gate 		}
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate 		nvpair = NULL;
13767c478bd9Sstevel@tonic-gate 		if ((nvpair = nvlist_next_nvpair(nvl, nvpair)) == NULL) {
13777c478bd9Sstevel@tonic-gate 			goto create_failed;
13787c478bd9Sstevel@tonic-gate 		}
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate 		new_class = 1;
13817c478bd9Sstevel@tonic-gate 		while (new_class) {
13827c478bd9Sstevel@tonic-gate 			/* Extract the class name from the nvpair */
13837c478bd9Sstevel@tonic-gate 			if (nvpair_value_string(nvpair, &class_name) != 0) {
13847c478bd9Sstevel@tonic-gate 				goto create_failed;
13857c478bd9Sstevel@tonic-gate 			}
13867c478bd9Sstevel@tonic-gate 			clist = (class_lst_t *)
13877c478bd9Sstevel@tonic-gate 			    calloc(1, sizeof (class_lst_t));
13887c478bd9Sstevel@tonic-gate 			if (clist == NULL) {
13897c478bd9Sstevel@tonic-gate 				goto create_failed;
13907c478bd9Sstevel@tonic-gate 			}
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate 			clist->cl_name = strdup(class_name);
13937c478bd9Sstevel@tonic-gate 			if (clist->cl_name == NULL) {
13947c478bd9Sstevel@tonic-gate 				free(clist);
13957c478bd9Sstevel@tonic-gate 				goto create_failed;
13967c478bd9Sstevel@tonic-gate 			}
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate 			/*
13997c478bd9Sstevel@tonic-gate 			 * Extract the subclass name and registration
14007c478bd9Sstevel@tonic-gate 			 * from the nvpair
14017c478bd9Sstevel@tonic-gate 			 */
14027c478bd9Sstevel@tonic-gate 			if ((nvpair = nvlist_next_nvpair(nvl, nvpair))
14037c478bd9Sstevel@tonic-gate 			    == NULL) {
14047c478bd9Sstevel@tonic-gate 				free(clist->cl_name);
14057c478bd9Sstevel@tonic-gate 				free(clist);
14067c478bd9Sstevel@tonic-gate 				goto create_failed;
14077c478bd9Sstevel@tonic-gate 			}
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate 			clist->cl_next = class_hash[i];
14107c478bd9Sstevel@tonic-gate 			class_hash[i] = clist;
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 			for (;;) {
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate 				sc_list = (subclass_lst_t *)calloc(1,
14157c478bd9Sstevel@tonic-gate 				    sizeof (subclass_lst_t));
14167c478bd9Sstevel@tonic-gate 				if (sc_list == NULL) {
14177c478bd9Sstevel@tonic-gate 					goto create_failed;
14187c478bd9Sstevel@tonic-gate 				}
14197c478bd9Sstevel@tonic-gate 
14207c478bd9Sstevel@tonic-gate 				sc_list->sl_next = clist->cl_subclass_list;
14217c478bd9Sstevel@tonic-gate 				clist->cl_subclass_list = sc_list;
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 				sc_list->sl_name = strdup(nvpair_name(nvpair));
14247c478bd9Sstevel@tonic-gate 				if (sc_list->sl_name == NULL) {
14257c478bd9Sstevel@tonic-gate 					goto create_failed;
14267c478bd9Sstevel@tonic-gate 				}
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate 				if (nvpair_value_byte_array(nvpair,
14297c478bd9Sstevel@tonic-gate 				    &subscribers, &num_elem) != 0) {
14307c478bd9Sstevel@tonic-gate 					goto create_failed;
14317c478bd9Sstevel@tonic-gate 				}
14327c478bd9Sstevel@tonic-gate 				bcopy(subscribers, (uchar_t *)sc_list->sl_num,
14337c478bd9Sstevel@tonic-gate 				    MAX_SUBSCRIBERS + 1);
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 				for (j = 1; j <= MAX_SUBSCRIBERS; ++j) {
14367c478bd9Sstevel@tonic-gate 					if (sc_list->sl_num[j] == 0)
14377c478bd9Sstevel@tonic-gate 						continue;
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 					if (alloc_subscriber(shp, j, 1) != 0) {
14407c478bd9Sstevel@tonic-gate 						goto create_failed;
14417c478bd9Sstevel@tonic-gate 					}
14427c478bd9Sstevel@tonic-gate 				}
14437c478bd9Sstevel@tonic-gate 
14447c478bd9Sstevel@tonic-gate 				/*
14457c478bd9Sstevel@tonic-gate 				 * Check next nvpair - either subclass or
14467c478bd9Sstevel@tonic-gate 				 * class
14477c478bd9Sstevel@tonic-gate 				 */
14487c478bd9Sstevel@tonic-gate 				if ((nvpair = nvlist_next_nvpair(nvl, nvpair))
14497c478bd9Sstevel@tonic-gate 				    == NULL) {
14507c478bd9Sstevel@tonic-gate 					new_class = 0;
14517c478bd9Sstevel@tonic-gate 					break;
14527c478bd9Sstevel@tonic-gate 				} else if (strcmp(nvpair_name(nvpair),
14537c478bd9Sstevel@tonic-gate 				    CLASS_NAME) == 0) {
14547c478bd9Sstevel@tonic-gate 					break;
14557c478bd9Sstevel@tonic-gate 				}
14567c478bd9Sstevel@tonic-gate 			}
14577c478bd9Sstevel@tonic-gate 		}
14587c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
14597c478bd9Sstevel@tonic-gate 	}
14607c478bd9Sstevel@tonic-gate 	return (0);
14617c478bd9Sstevel@tonic-gate 
14627c478bd9Sstevel@tonic-gate create_failed:
14637c478bd9Sstevel@tonic-gate 	dealloc_subscribers(shp);
14647c478bd9Sstevel@tonic-gate 	free_cached_registration(shp);
14657c478bd9Sstevel@tonic-gate 	if (nvl)
14667c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
14677c478bd9Sstevel@tonic-gate 	return (-1);
14687c478bd9Sstevel@tonic-gate 
14697c478bd9Sstevel@tonic-gate }
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate /*
14727c478bd9Sstevel@tonic-gate  * cache_update_service - generic event publisher service routine.  This routine
14737c478bd9Sstevel@tonic-gate  *		is called in response to a registration cache update.
14747c478bd9Sstevel@tonic-gate  *
14757c478bd9Sstevel@tonic-gate  */
147649b225e1SGavin Maltby /*ARGSUSED*/
14777c478bd9Sstevel@tonic-gate static void
14787c478bd9Sstevel@tonic-gate cache_update_service(void *cookie, char *args, size_t alen,
14797c478bd9Sstevel@tonic-gate     door_desc_t *ddp, uint_t ndid)
14807c478bd9Sstevel@tonic-gate {
14817c478bd9Sstevel@tonic-gate 	int ret = 0;
14827c478bd9Sstevel@tonic-gate 	uint_t num_elem;
14837c478bd9Sstevel@tonic-gate 	char *class, **event_list;
14847c478bd9Sstevel@tonic-gate 	size_t datalen;
14857c478bd9Sstevel@tonic-gate 	uint32_t sub_id;
14867c478bd9Sstevel@tonic-gate 	nvlist_t *nvl;
14877c478bd9Sstevel@tonic-gate 	nvpair_t *nvpair = NULL;
14887c478bd9Sstevel@tonic-gate 	struct reg_args *rargs;
14897c478bd9Sstevel@tonic-gate 	sysevent_handle_t *shp;
14907c478bd9Sstevel@tonic-gate 	subscriber_data_t *sub;
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate 	if (alen < sizeof (struct reg_args) || cookie == NULL) {
14937c478bd9Sstevel@tonic-gate 		ret = EINVAL;
14947c478bd9Sstevel@tonic-gate 		goto return_from_door;
14957c478bd9Sstevel@tonic-gate 	}
14967c478bd9Sstevel@tonic-gate 
149749b225e1SGavin Maltby 	/* LINTED: E_BAD_PTR_CAST_ALIGN */
14987c478bd9Sstevel@tonic-gate 	rargs = (struct reg_args *)args;
14997c478bd9Sstevel@tonic-gate 	shp = (sysevent_handle_t *)cookie;
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 	datalen = alen - sizeof (struct reg_args);
15027c478bd9Sstevel@tonic-gate 	sub_id = rargs->ra_sub_id;
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate 	(void) mutex_lock(SH_LOCK(shp));
15057c478bd9Sstevel@tonic-gate 
15067c478bd9Sstevel@tonic-gate 	switch (rargs->ra_op) {
15077c478bd9Sstevel@tonic-gate 	case SE_UNREGISTER:
15087c478bd9Sstevel@tonic-gate 		class = (char *)&rargs->ra_buf_ptr;
15097c478bd9Sstevel@tonic-gate 		cache_remove_class(shp, (char *)class,
15107c478bd9Sstevel@tonic-gate 		    sub_id);
15117c478bd9Sstevel@tonic-gate 		break;
15127c478bd9Sstevel@tonic-gate 	case SE_UNBIND_REGISTRATION:
15137c478bd9Sstevel@tonic-gate 
15147c478bd9Sstevel@tonic-gate 		sub = SH_SUBSCRIBER(shp, sub_id);
15157c478bd9Sstevel@tonic-gate 		if (sub == NULL)
15167c478bd9Sstevel@tonic-gate 			break;
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate 		free(sub->sd_door_name);
15197c478bd9Sstevel@tonic-gate 		free(sub);
15207c478bd9Sstevel@tonic-gate 		cache_remove_class(shp, EC_ALL, sub_id);
15217c478bd9Sstevel@tonic-gate 		SH_SUBSCRIBER(shp, sub_id) = NULL;
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 		break;
15247c478bd9Sstevel@tonic-gate 	case SE_BIND_REGISTRATION:
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate 		/* New subscriber */
15277c478bd9Sstevel@tonic-gate 		if (alloc_subscriber(shp, sub_id, 0) != 0) {
15287c478bd9Sstevel@tonic-gate 			ret = ENOMEM;
15297c478bd9Sstevel@tonic-gate 			break;
15307c478bd9Sstevel@tonic-gate 		}
15317c478bd9Sstevel@tonic-gate 		break;
15327c478bd9Sstevel@tonic-gate 	case SE_REGISTER:
15337c478bd9Sstevel@tonic-gate 
15347c478bd9Sstevel@tonic-gate 		if (SH_SUBSCRIBER(shp, sub_id) == NULL) {
15357c478bd9Sstevel@tonic-gate 			ret = EINVAL;
15367c478bd9Sstevel@tonic-gate 			break;
15377c478bd9Sstevel@tonic-gate 		}
15387c478bd9Sstevel@tonic-gate 		/* Get new registration data */
15397c478bd9Sstevel@tonic-gate 		if (nvlist_unpack((char *)&rargs->ra_buf_ptr, datalen,
15407c478bd9Sstevel@tonic-gate 		    &nvl, 0) != 0) {
15417c478bd9Sstevel@tonic-gate 			ret =  EFAULT;
15427c478bd9Sstevel@tonic-gate 			break;
15437c478bd9Sstevel@tonic-gate 		}
15447c478bd9Sstevel@tonic-gate 		if ((nvpair = nvlist_next_nvpair(nvl, nvpair)) == NULL) {
15457c478bd9Sstevel@tonic-gate 			nvlist_free(nvl);
15467c478bd9Sstevel@tonic-gate 			ret = EFAULT;
15477c478bd9Sstevel@tonic-gate 			break;
15487c478bd9Sstevel@tonic-gate 		}
15497c478bd9Sstevel@tonic-gate 		if (nvpair_value_string_array(nvpair, &event_list, &num_elem)
15507c478bd9Sstevel@tonic-gate 		    != 0) {
15517c478bd9Sstevel@tonic-gate 			nvlist_free(nvl);
15527c478bd9Sstevel@tonic-gate 			ret =  EFAULT;
15537c478bd9Sstevel@tonic-gate 			break;
15547c478bd9Sstevel@tonic-gate 		}
15557c478bd9Sstevel@tonic-gate 		class = nvpair_name(nvpair);
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 		ret = cache_insert_class(shp, class,
15587c478bd9Sstevel@tonic-gate 		    event_list, num_elem, sub_id);
15597c478bd9Sstevel@tonic-gate 		if (ret != 0) {
15607c478bd9Sstevel@tonic-gate 			cache_remove_class(shp, class, sub_id);
15617c478bd9Sstevel@tonic-gate 			nvlist_free(nvl);
15627c478bd9Sstevel@tonic-gate 			ret =  EFAULT;
15637c478bd9Sstevel@tonic-gate 			break;
15647c478bd9Sstevel@tonic-gate 		}
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate 		break;
15697c478bd9Sstevel@tonic-gate 	case SE_CLEANUP:
15707c478bd9Sstevel@tonic-gate 		/* Cleanup stale subscribers */
15717c478bd9Sstevel@tonic-gate 		sysevent_cleanup_subscribers(shp);
15727c478bd9Sstevel@tonic-gate 		break;
15737c478bd9Sstevel@tonic-gate 	default:
15747c478bd9Sstevel@tonic-gate 		ret =  EINVAL;
15757c478bd9Sstevel@tonic-gate 	}
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(SH_LOCK(shp));
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate return_from_door:
15807c478bd9Sstevel@tonic-gate 	(void) door_return((void *)&ret, sizeof (ret), NULL, 0);
15817c478bd9Sstevel@tonic-gate 	(void) door_return(NULL, 0, NULL, 0);
15827c478bd9Sstevel@tonic-gate }
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate /*
15857c478bd9Sstevel@tonic-gate  * sysevent_send_event -
15867c478bd9Sstevel@tonic-gate  * Send an event via the communication channel associated with the sysevent
15877c478bd9Sstevel@tonic-gate  * handle.  Event notifications are broadcast to all subscribers based upon
15887c478bd9Sstevel@tonic-gate  * the event class and subclass.  The handle must have been previously
15897c478bd9Sstevel@tonic-gate  * allocated and bound by
15907c478bd9Sstevel@tonic-gate  * sysevent_open_channel() and sysevent_bind_publisher()
15917c478bd9Sstevel@tonic-gate  */
15927c478bd9Sstevel@tonic-gate int
15937c478bd9Sstevel@tonic-gate sysevent_send_event(sysevent_handle_t *shp, sysevent_t *ev)
15947c478bd9Sstevel@tonic-gate {
15957c478bd9Sstevel@tonic-gate 	int i, error, sub_fd, result = 0;
15967c478bd9Sstevel@tonic-gate 	int deliver_error = 0;
15977c478bd9Sstevel@tonic-gate 	int subscribers_sent = 0;
15987c478bd9Sstevel@tonic-gate 	int want_resend, resend_cnt = 0;
15997c478bd9Sstevel@tonic-gate 	char *event_class, *event_subclass;
16007c478bd9Sstevel@tonic-gate 	uchar_t *all_class_subscribers, *all_subclass_subscribers;
16017c478bd9Sstevel@tonic-gate 	uchar_t *subclass_subscribers;
16027c478bd9Sstevel@tonic-gate 	subscriber_data_t *sub;
16037c478bd9Sstevel@tonic-gate 	subclass_lst_t *sc_lst;
16047c478bd9Sstevel@tonic-gate 
16057c478bd9Sstevel@tonic-gate 	/* Check for proper registration */
16067c478bd9Sstevel@tonic-gate 	event_class = sysevent_get_class_name(ev);
16077c478bd9Sstevel@tonic-gate 	event_subclass = sysevent_get_subclass_name(ev);
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 	(void) mutex_lock(SH_LOCK(shp));
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate send_event:
16127c478bd9Sstevel@tonic-gate 
16137c478bd9Sstevel@tonic-gate 	want_resend = 0;
16147c478bd9Sstevel@tonic-gate 	if (!SH_BOUND(shp)) {
16157c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(SH_LOCK(shp));
16167c478bd9Sstevel@tonic-gate 		errno = EINVAL;
16177c478bd9Sstevel@tonic-gate 		return (-1);
16187c478bd9Sstevel@tonic-gate 	}
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate 	/* Find all subscribers for this event class/subclass */
16217c478bd9Sstevel@tonic-gate 	sc_lst = cache_find_subclass(
16227c478bd9Sstevel@tonic-gate 	    cache_find_class(shp, EC_ALL), EC_SUB_ALL);
16237c478bd9Sstevel@tonic-gate 	all_class_subscribers = sc_lst->sl_num;
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate 	sc_lst = cache_find_subclass(
16267c478bd9Sstevel@tonic-gate 	    cache_find_class(shp, event_class), EC_SUB_ALL);
16277c478bd9Sstevel@tonic-gate 	if (sc_lst)
16287c478bd9Sstevel@tonic-gate 		all_subclass_subscribers = sc_lst->sl_num;
16297c478bd9Sstevel@tonic-gate 	else
16307c478bd9Sstevel@tonic-gate 		all_subclass_subscribers = NULL;
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate 	sc_lst = cache_find_subclass(
16337c478bd9Sstevel@tonic-gate 	    cache_find_class(shp, event_class), event_subclass);
16347c478bd9Sstevel@tonic-gate 	if (sc_lst)
16357c478bd9Sstevel@tonic-gate 		subclass_subscribers = sc_lst->sl_num;
16367c478bd9Sstevel@tonic-gate 	else
16377c478bd9Sstevel@tonic-gate 		subclass_subscribers = NULL;
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 	/* Send event buffer to all valid subscribers */
16407c478bd9Sstevel@tonic-gate 	for (i = 1; i <= MAX_SUBSCRIBERS; ++i) {
16417c478bd9Sstevel@tonic-gate 		if ((all_class_subscribers[i] |
16427c478bd9Sstevel@tonic-gate 		    (all_subclass_subscribers && all_subclass_subscribers[i]) |
16437c478bd9Sstevel@tonic-gate 		    (subclass_subscribers && subclass_subscribers[i])) == 0)
16447c478bd9Sstevel@tonic-gate 			continue;
16457c478bd9Sstevel@tonic-gate 
16467c478bd9Sstevel@tonic-gate 		sub = SH_SUBSCRIBER(shp, i);
16477c478bd9Sstevel@tonic-gate 		assert(sub != NULL);
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate 		/* Check for active subscriber */
16507c478bd9Sstevel@tonic-gate 		if (!(sub->sd_flag & ACTIVE)) {
16517c478bd9Sstevel@tonic-gate 			dprint("sysevent_send_event: subscriber %d inactive\n",
16527c478bd9Sstevel@tonic-gate 			    i);
16537c478bd9Sstevel@tonic-gate 			continue;
16547c478bd9Sstevel@tonic-gate 		}
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate 		/* Process only resend requests */
16577c478bd9Sstevel@tonic-gate 		if (resend_cnt > 0 && !(sub->sd_flag & SEND_AGAIN)) {
16587c478bd9Sstevel@tonic-gate 			continue;
16597c478bd9Sstevel@tonic-gate 		}
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate 		if ((sub_fd = open(sub->sd_door_name, O_RDONLY)) == -1) {
16627c478bd9Sstevel@tonic-gate 			dprint("sysevent_send_event: Failed to open "
16637c478bd9Sstevel@tonic-gate 			    "%s: %s\n", sub->sd_door_name, strerror(errno));
16647c478bd9Sstevel@tonic-gate 			continue;
16657c478bd9Sstevel@tonic-gate 		}
16667c478bd9Sstevel@tonic-gate 		result = 0;
16677c478bd9Sstevel@tonic-gate 		error = clnt_deliver_event(sub_fd, ev,
16687c478bd9Sstevel@tonic-gate 		    sysevent_get_size(ev), &result, sizeof (result));
16697c478bd9Sstevel@tonic-gate 
16707c478bd9Sstevel@tonic-gate 		(void) close(sub_fd);
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 		/* Successful door call */
16737c478bd9Sstevel@tonic-gate 		if (error == 0) {
16747c478bd9Sstevel@tonic-gate 			switch (result) {
16757c478bd9Sstevel@tonic-gate 			/* Subscriber requested EAGAIN */
16767c478bd9Sstevel@tonic-gate 			case EAGAIN:
16777c478bd9Sstevel@tonic-gate 				if (resend_cnt > SE_MAX_RETRY_LIMIT) {
16787c478bd9Sstevel@tonic-gate 					deliver_error = 1;
16797c478bd9Sstevel@tonic-gate 				} else {
16807c478bd9Sstevel@tonic-gate 					want_resend = 1;
16817c478bd9Sstevel@tonic-gate 					dprint("sysevent_send_event: resend "
16827c478bd9Sstevel@tonic-gate 					    "requested for %d\n", i);
16837c478bd9Sstevel@tonic-gate 					sub->sd_flag |= SEND_AGAIN;
16847c478bd9Sstevel@tonic-gate 				}
16857c478bd9Sstevel@tonic-gate 				break;
16867c478bd9Sstevel@tonic-gate 			/* Bad sysevent handle for subscriber */
16877c478bd9Sstevel@tonic-gate 			case EBADF:
16887c478bd9Sstevel@tonic-gate 			case EINVAL:
16897c478bd9Sstevel@tonic-gate 				dprint("sysevent_send_event: Bad sysevent "
16907c478bd9Sstevel@tonic-gate 				    "handle for %s", sub->sd_door_name);
16917c478bd9Sstevel@tonic-gate 				sub->sd_flag = 0;
16927c478bd9Sstevel@tonic-gate 				deliver_error = 1;
16937c478bd9Sstevel@tonic-gate 				break;
16947c478bd9Sstevel@tonic-gate 			/* Successful delivery */
16957c478bd9Sstevel@tonic-gate 			default:
16967c478bd9Sstevel@tonic-gate 				sub->sd_flag &= ~SEND_AGAIN;
16977c478bd9Sstevel@tonic-gate 				++subscribers_sent;
16987c478bd9Sstevel@tonic-gate 			}
16997c478bd9Sstevel@tonic-gate 		} else {
17007c478bd9Sstevel@tonic-gate 			dprint("sysevent_send_event: Failed door call "
17017c478bd9Sstevel@tonic-gate 			    "to %s: %s: %d\n", sub->sd_door_name,
17027c478bd9Sstevel@tonic-gate 			    strerror(errno), result);
17037c478bd9Sstevel@tonic-gate 			sub->sd_flag = 0;
17047c478bd9Sstevel@tonic-gate 			deliver_error = 1;
17057c478bd9Sstevel@tonic-gate 		}
17067c478bd9Sstevel@tonic-gate 	}
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate 	if (want_resend) {
17097c478bd9Sstevel@tonic-gate 		resend_cnt++;
17107c478bd9Sstevel@tonic-gate 		goto send_event;
17117c478bd9Sstevel@tonic-gate 	}
17127c478bd9Sstevel@tonic-gate 
17137c478bd9Sstevel@tonic-gate 	if (deliver_error) {
17147c478bd9Sstevel@tonic-gate 		sysevent_cleanup_subscribers(shp);
17157c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(SH_LOCK(shp));
17167c478bd9Sstevel@tonic-gate 		errno = EFAULT;
17177c478bd9Sstevel@tonic-gate 		return (-1);
17187c478bd9Sstevel@tonic-gate 	}
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(SH_LOCK(shp));
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate 	if (subscribers_sent == 0) {
17237c478bd9Sstevel@tonic-gate 		dprint("sysevent_send_event: No subscribers for %s:%s\n",
17247c478bd9Sstevel@tonic-gate 		    event_class, event_subclass);
17257c478bd9Sstevel@tonic-gate 		errno = ENOENT;
17267c478bd9Sstevel@tonic-gate 		return (-1);
17277c478bd9Sstevel@tonic-gate 	}
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate 	return (0);
17307c478bd9Sstevel@tonic-gate }
17317c478bd9Sstevel@tonic-gate 
17327c478bd9Sstevel@tonic-gate /*
17337c478bd9Sstevel@tonic-gate  * Common routine to establish an event channel through which an event
17347c478bd9Sstevel@tonic-gate  * publisher or subscriber may post or receive events.
17357c478bd9Sstevel@tonic-gate  */
17367c478bd9Sstevel@tonic-gate static sysevent_handle_t *
17377c478bd9Sstevel@tonic-gate sysevent_open_channel_common(const char *channel_path)
17387c478bd9Sstevel@tonic-gate {
17397c478bd9Sstevel@tonic-gate 	uint32_t sub_id = 0;
17407c478bd9Sstevel@tonic-gate 	char *begin_path;
17417c478bd9Sstevel@tonic-gate 	struct stat chan_stat;
17427c478bd9Sstevel@tonic-gate 	sysevent_handle_t *shp;
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate 	if (channel_path == NULL || strlen(channel_path) + 1 > MAXPATHLEN) {
17467c478bd9Sstevel@tonic-gate 		errno = EINVAL;
17477c478bd9Sstevel@tonic-gate 		return (NULL);
17487c478bd9Sstevel@tonic-gate 	}
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate 	if (mkdir(channel_path, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) < 0) {
17517c478bd9Sstevel@tonic-gate 		if (errno != EEXIST) {
17527c478bd9Sstevel@tonic-gate 			errno = EACCES;
17537c478bd9Sstevel@tonic-gate 			return (NULL);
17547c478bd9Sstevel@tonic-gate 		}
17557c478bd9Sstevel@tonic-gate 	}
17567c478bd9Sstevel@tonic-gate 
17577c478bd9Sstevel@tonic-gate 	/* Check channel file permissions */
17587c478bd9Sstevel@tonic-gate 	if (stat(channel_path, &chan_stat) != 0) {
17597c478bd9Sstevel@tonic-gate 		dprint("sysevent_open_channel: Invalid permissions for channel "
17607c478bd9Sstevel@tonic-gate 		    "%s\n", channel_path);
17617c478bd9Sstevel@tonic-gate 		errno = EACCES;
17627c478bd9Sstevel@tonic-gate 		return (NULL);
17637c478bd9Sstevel@tonic-gate 	} else if (chan_stat.st_uid != getuid() ||
17644bc0a2efScasper 	    !S_ISDIR(chan_stat.st_mode)) {
17657c478bd9Sstevel@tonic-gate 		dprint("sysevent_open_channel: Invalid "
17667c478bd9Sstevel@tonic-gate 		    "permissions for channel %s\n: %d:%d:%d", channel_path,
17677c478bd9Sstevel@tonic-gate 		    (int)chan_stat.st_uid, (int)chan_stat.st_gid,
17687c478bd9Sstevel@tonic-gate 		    (int)chan_stat.st_mode);
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate 		errno = EACCES;
17717c478bd9Sstevel@tonic-gate 		return (NULL);
17727c478bd9Sstevel@tonic-gate 	}
17737c478bd9Sstevel@tonic-gate 
17747c478bd9Sstevel@tonic-gate 	shp = calloc(1, sizeof (sysevent_impl_hdl_t));
17757c478bd9Sstevel@tonic-gate 	if (shp == NULL) {
17767c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
17777c478bd9Sstevel@tonic-gate 		return (NULL);
17787c478bd9Sstevel@tonic-gate 	}
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate 	SH_CHANNEL_NAME(shp) = NULL;
17817c478bd9Sstevel@tonic-gate 	SH_CHANNEL_PATH(shp) = strdup(channel_path);
17827c478bd9Sstevel@tonic-gate 	if (SH_CHANNEL_PATH(shp) == NULL) {
17837c478bd9Sstevel@tonic-gate 		free(shp);
17847c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
17857c478bd9Sstevel@tonic-gate 		return (NULL);
17867c478bd9Sstevel@tonic-gate 	}
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate 	/* Extract the channel name */
17897c478bd9Sstevel@tonic-gate 	begin_path = SH_CHANNEL_PATH(shp);
17907c478bd9Sstevel@tonic-gate 	while (*begin_path != '\0' &&
17917c478bd9Sstevel@tonic-gate 	    (begin_path = strpbrk(begin_path, "/")) != NULL) {
17927c478bd9Sstevel@tonic-gate 		++begin_path;
17937c478bd9Sstevel@tonic-gate 		SH_CHANNEL_NAME(shp) = begin_path;
17947c478bd9Sstevel@tonic-gate 	}
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate 	if (update_kernel_registration(shp, 0,
17977c478bd9Sstevel@tonic-gate 	    SE_OPEN_REGISTRATION, &sub_id, 0, NULL) != 0) {
17987c478bd9Sstevel@tonic-gate 		dprint("sysevent_open_channel: Failed for channel %s\n",
17997c478bd9Sstevel@tonic-gate 		    SH_CHANNEL_NAME(shp));
18007c478bd9Sstevel@tonic-gate 		free(SH_CHANNEL_PATH(shp));
18017c478bd9Sstevel@tonic-gate 		free(shp);
18027c478bd9Sstevel@tonic-gate 		errno = EFAULT;
18037c478bd9Sstevel@tonic-gate 		return (NULL);
18047c478bd9Sstevel@tonic-gate 	}
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate 	(void) mutex_init(SH_LOCK(shp), USYNC_THREAD, NULL);
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate 	return (shp);
18097c478bd9Sstevel@tonic-gate }
18107c478bd9Sstevel@tonic-gate 
18117c478bd9Sstevel@tonic-gate /*
18127c478bd9Sstevel@tonic-gate  * Establish a sysevent channel for publication and subscription
18137c478bd9Sstevel@tonic-gate  */
18147c478bd9Sstevel@tonic-gate sysevent_handle_t *
18157c478bd9Sstevel@tonic-gate sysevent_open_channel(const char *channel)
18167c478bd9Sstevel@tonic-gate {
18177c478bd9Sstevel@tonic-gate 	int var_run_mounted = 0;
18187c478bd9Sstevel@tonic-gate 	char full_channel[MAXPATHLEN + 1];
18197c478bd9Sstevel@tonic-gate 	FILE *fp;
18207c478bd9Sstevel@tonic-gate 	struct stat chan_stat;
18217c478bd9Sstevel@tonic-gate 	struct extmnttab m;
18227c478bd9Sstevel@tonic-gate 
18237c478bd9Sstevel@tonic-gate 	if (channel == NULL) {
18247c478bd9Sstevel@tonic-gate 		errno = EINVAL;
18257c478bd9Sstevel@tonic-gate 		return (NULL);
18267c478bd9Sstevel@tonic-gate 	}
18277c478bd9Sstevel@tonic-gate 
18287c478bd9Sstevel@tonic-gate 	/*
18297c478bd9Sstevel@tonic-gate 	 * Check that /var/run is mounted as tmpfs before allowing a channel
18307c478bd9Sstevel@tonic-gate 	 * to be opened.
18317c478bd9Sstevel@tonic-gate 	 */
1832004388ebScasper 	if ((fp = fopen(MNTTAB, "rF")) == NULL) {
18337c478bd9Sstevel@tonic-gate 		errno = EACCES;
18347c478bd9Sstevel@tonic-gate 		return (NULL);
18357c478bd9Sstevel@tonic-gate 	}
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate 	resetmnttab(fp);
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate 	while (getextmntent(fp, &m, sizeof (struct extmnttab)) == 0) {
18407c478bd9Sstevel@tonic-gate 		if (strcmp(m.mnt_mountp, "/var/run") == 0 &&
18417c478bd9Sstevel@tonic-gate 		    strcmp(m.mnt_fstype, "tmpfs") == 0) {
18427c478bd9Sstevel@tonic-gate 			var_run_mounted = 1;
18437c478bd9Sstevel@tonic-gate 			break;
18447c478bd9Sstevel@tonic-gate 		}
18457c478bd9Sstevel@tonic-gate 	}
18467c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate 	if (!var_run_mounted) {
18497c478bd9Sstevel@tonic-gate 		errno = EACCES;
18507c478bd9Sstevel@tonic-gate 		return (NULL);
18517c478bd9Sstevel@tonic-gate 	}
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate 	if (stat(CHAN_PATH, &chan_stat) < 0) {
18547c478bd9Sstevel@tonic-gate 		if (mkdir(CHAN_PATH,
18557c478bd9Sstevel@tonic-gate 		    S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) < 0) {
18567c478bd9Sstevel@tonic-gate 			dprint("sysevent_open_channel: Unable "
18577c478bd9Sstevel@tonic-gate 			    "to create channel directory %s:%s\n", CHAN_PATH,
18587c478bd9Sstevel@tonic-gate 			    strerror(errno));
18597c478bd9Sstevel@tonic-gate 			if (errno != EEXIST) {
18607c478bd9Sstevel@tonic-gate 				errno = EACCES;
18617c478bd9Sstevel@tonic-gate 				return (NULL);
18627c478bd9Sstevel@tonic-gate 			}
18637c478bd9Sstevel@tonic-gate 		}
18647c478bd9Sstevel@tonic-gate 	}
18657c478bd9Sstevel@tonic-gate 
18667c478bd9Sstevel@tonic-gate 	if (snprintf(full_channel, MAXPATHLEN, "%s/%s", CHAN_PATH, channel) >=
18677c478bd9Sstevel@tonic-gate 	    MAXPATHLEN) {
18687c478bd9Sstevel@tonic-gate 		errno = EINVAL;
18697c478bd9Sstevel@tonic-gate 		return (NULL);
18707c478bd9Sstevel@tonic-gate 	}
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate 	return (sysevent_open_channel_common(full_channel));
18737c478bd9Sstevel@tonic-gate }
18747c478bd9Sstevel@tonic-gate 
18757c478bd9Sstevel@tonic-gate /*
18767c478bd9Sstevel@tonic-gate  * Establish a sysevent channel for publication and subscription
18777c478bd9Sstevel@tonic-gate  * Full path to the channel determined by the caller
18787c478bd9Sstevel@tonic-gate  */
18797c478bd9Sstevel@tonic-gate sysevent_handle_t *
18807c478bd9Sstevel@tonic-gate sysevent_open_channel_alt(const char *channel_path)
18817c478bd9Sstevel@tonic-gate {
18827c478bd9Sstevel@tonic-gate 	return (sysevent_open_channel_common(channel_path));
18837c478bd9Sstevel@tonic-gate }
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate /*
18867c478bd9Sstevel@tonic-gate  * sysevent_close_channel - Clean up resources associated with a previously
18877c478bd9Sstevel@tonic-gate  *				opened sysevent channel
18887c478bd9Sstevel@tonic-gate  */
18897c478bd9Sstevel@tonic-gate void
18907c478bd9Sstevel@tonic-gate sysevent_close_channel(sysevent_handle_t *shp)
18917c478bd9Sstevel@tonic-gate {
18927c478bd9Sstevel@tonic-gate 	int error = errno;
18937c478bd9Sstevel@tonic-gate 	uint32_t sub_id = 0;
18947c478bd9Sstevel@tonic-gate 
18957c478bd9Sstevel@tonic-gate 	if (shp == NULL) {
18967c478bd9Sstevel@tonic-gate 		return;
18977c478bd9Sstevel@tonic-gate 	}
18987c478bd9Sstevel@tonic-gate 
18997c478bd9Sstevel@tonic-gate 	(void) mutex_lock(SH_LOCK(shp));
19007c478bd9Sstevel@tonic-gate 	if (SH_BOUND(shp)) {
19017c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(SH_LOCK(shp));
19027c478bd9Sstevel@tonic-gate 		if (SH_TYPE(shp) == PUBLISHER)
19037c478bd9Sstevel@tonic-gate 			sysevent_unbind_publisher(shp);
19047c478bd9Sstevel@tonic-gate 		else if (SH_TYPE(shp) == SUBSCRIBER)
19057c478bd9Sstevel@tonic-gate 			sysevent_unbind_subscriber(shp);
19067c478bd9Sstevel@tonic-gate 		(void) mutex_lock(SH_LOCK(shp));
19077c478bd9Sstevel@tonic-gate 	}
19087c478bd9Sstevel@tonic-gate 
19097c478bd9Sstevel@tonic-gate 	(void) update_kernel_registration(shp, 0,
19107c478bd9Sstevel@tonic-gate 	    SE_CLOSE_REGISTRATION, &sub_id, 0, NULL);
19117c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(SH_LOCK(shp));
19127c478bd9Sstevel@tonic-gate 
19137c478bd9Sstevel@tonic-gate 	free(SH_CHANNEL_PATH(shp));
19147c478bd9Sstevel@tonic-gate 	free(shp);
19157c478bd9Sstevel@tonic-gate 	errno = error;
19167c478bd9Sstevel@tonic-gate }
19177c478bd9Sstevel@tonic-gate 
19187c478bd9Sstevel@tonic-gate /*
19197c478bd9Sstevel@tonic-gate  * sysevent_bind_publisher - Bind an event publisher to an event channel
19207c478bd9Sstevel@tonic-gate  */
19217c478bd9Sstevel@tonic-gate int
19227c478bd9Sstevel@tonic-gate sysevent_bind_publisher(sysevent_handle_t *shp)
19237c478bd9Sstevel@tonic-gate {
19247c478bd9Sstevel@tonic-gate 	int error = 0;
19257c478bd9Sstevel@tonic-gate 	int fd = -1;
19267c478bd9Sstevel@tonic-gate 	char door_name[MAXPATHLEN];
19277c478bd9Sstevel@tonic-gate 	uint32_t pub_id;
19287c478bd9Sstevel@tonic-gate 	struct stat reg_stat;
19297c478bd9Sstevel@tonic-gate 	publisher_priv_t *pub;
19307c478bd9Sstevel@tonic-gate 
19317c478bd9Sstevel@tonic-gate 	if (shp == NULL) {
19327c478bd9Sstevel@tonic-gate 		errno = EINVAL;
19337c478bd9Sstevel@tonic-gate 		return (-1);
19347c478bd9Sstevel@tonic-gate 	}
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate 	(void) mutex_lock(SH_LOCK(shp));
19377c478bd9Sstevel@tonic-gate 	if (SH_BOUND(shp)) {
19387c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(SH_LOCK(shp));
19397c478bd9Sstevel@tonic-gate 		errno = EINVAL;
19407c478bd9Sstevel@tonic-gate 		return (-1);
19417c478bd9Sstevel@tonic-gate 	}
19427c478bd9Sstevel@tonic-gate 
19437c478bd9Sstevel@tonic-gate 	if ((pub = (publisher_priv_t *)calloc(1, sizeof (publisher_priv_t))) ==
19447c478bd9Sstevel@tonic-gate 	    NULL) {
19457c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(SH_LOCK(shp));
19467c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
19477c478bd9Sstevel@tonic-gate 		return (-1);
19487c478bd9Sstevel@tonic-gate 	}
19497c478bd9Sstevel@tonic-gate 	SH_PRIV_DATA(shp) = (void *)pub;
19507c478bd9Sstevel@tonic-gate 
19517c478bd9Sstevel@tonic-gate 	if (snprintf(door_name, MAXPATHLEN, "%s/%s",
19527c478bd9Sstevel@tonic-gate 	    SH_CHANNEL_PATH(shp), REG_DOOR) >= MAXPATHLEN) {
19537c478bd9Sstevel@tonic-gate 		free(pub);
19547c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(SH_LOCK(shp));
19557c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
19567c478bd9Sstevel@tonic-gate 		return (-1);
19577c478bd9Sstevel@tonic-gate 	}
19587c478bd9Sstevel@tonic-gate 	if ((SH_DOOR_NAME(shp) = strdup(door_name)) == NULL) {
19597c478bd9Sstevel@tonic-gate 		free(pub);
19607c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(SH_LOCK(shp));
19617c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
19627c478bd9Sstevel@tonic-gate 		return (-1);
19637c478bd9Sstevel@tonic-gate 	}
19647c478bd9Sstevel@tonic-gate 
19657c478bd9Sstevel@tonic-gate 	/* Only one publisher allowed per channel */
19667c478bd9Sstevel@tonic-gate 	if (stat(SH_DOOR_NAME(shp), &reg_stat) != 0) {
19677c478bd9Sstevel@tonic-gate 		if (errno != ENOENT) {
19687c478bd9Sstevel@tonic-gate 			error = EINVAL;
19697c478bd9Sstevel@tonic-gate 			goto fail;
19707c478bd9Sstevel@tonic-gate 		}
19717c478bd9Sstevel@tonic-gate 	}
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate 	/*
19747c478bd9Sstevel@tonic-gate 	 * Remove door file for robustness.
19757c478bd9Sstevel@tonic-gate 	 */
19767c478bd9Sstevel@tonic-gate 	if (unlink(SH_DOOR_NAME(shp)) != 0)
19777c478bd9Sstevel@tonic-gate 		dprint("sysevent_bind_publisher: Unlink of %s failed.\n",
19787c478bd9Sstevel@tonic-gate 		    SH_DOOR_NAME(shp));
19797c478bd9Sstevel@tonic-gate 
19807c478bd9Sstevel@tonic-gate 	/* Open channel registration door */
19817c478bd9Sstevel@tonic-gate 	fd = open(SH_DOOR_NAME(shp), O_CREAT|O_RDWR,
19827c478bd9Sstevel@tonic-gate 	    S_IREAD|S_IWRITE);
19837c478bd9Sstevel@tonic-gate 	if (fd == -1) {
19847c478bd9Sstevel@tonic-gate 		error = EINVAL;
19857c478bd9Sstevel@tonic-gate 		goto fail;
19867c478bd9Sstevel@tonic-gate 	}
19877c478bd9Sstevel@tonic-gate 
19887c478bd9Sstevel@tonic-gate 	/*
19897c478bd9Sstevel@tonic-gate 	 * Create the registration service for this publisher.
19907c478bd9Sstevel@tonic-gate 	 */
19917c478bd9Sstevel@tonic-gate 	if ((SH_DOOR_DESC(shp) = door_create(cache_update_service,
19927c478bd9Sstevel@tonic-gate 	    (void *)shp, DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) == -1) {
19937c478bd9Sstevel@tonic-gate 		dprint("sysevent_bind_publisher: door create failed: "
19947c478bd9Sstevel@tonic-gate 		    "%s\n", strerror(errno));
19957c478bd9Sstevel@tonic-gate 		error = EFAULT;
19967c478bd9Sstevel@tonic-gate 		goto fail;
19977c478bd9Sstevel@tonic-gate 	}
19987c478bd9Sstevel@tonic-gate 
19997c478bd9Sstevel@tonic-gate 	(void) fdetach(SH_DOOR_NAME(shp));
20007c478bd9Sstevel@tonic-gate 	if (fattach(SH_DOOR_DESC(shp), SH_DOOR_NAME(shp)) != 0) {
20017c478bd9Sstevel@tonic-gate 		dprint("sysevent_bind_publisher: unable to "
20027c478bd9Sstevel@tonic-gate 		    "bind event channel: fattach: %s\n",
20037c478bd9Sstevel@tonic-gate 		    SH_DOOR_NAME(shp));
20047c478bd9Sstevel@tonic-gate 		error = EACCES;
20057c478bd9Sstevel@tonic-gate 		goto fail;
20067c478bd9Sstevel@tonic-gate 	}
20077c478bd9Sstevel@tonic-gate 
20087c478bd9Sstevel@tonic-gate 	/* Bind this publisher in the kernel registration database */
20097c478bd9Sstevel@tonic-gate 	if (update_kernel_registration(shp, PUBLISHER,
20107c478bd9Sstevel@tonic-gate 	    SE_BIND_REGISTRATION, &pub_id, 0, NULL) != 0) {
20117c478bd9Sstevel@tonic-gate 		error = errno;
20127c478bd9Sstevel@tonic-gate 		goto fail;
20137c478bd9Sstevel@tonic-gate 	}
20147c478bd9Sstevel@tonic-gate 
20157c478bd9Sstevel@tonic-gate 	SH_ID(shp) = pub_id;
20167c478bd9Sstevel@tonic-gate 	SH_BOUND(shp) = 1;
20177c478bd9Sstevel@tonic-gate 	SH_TYPE(shp) = PUBLISHER;
20187c478bd9Sstevel@tonic-gate 
20197c478bd9Sstevel@tonic-gate 
20207c478bd9Sstevel@tonic-gate 	/* Create the subscription registration cache */
20217c478bd9Sstevel@tonic-gate 	if (create_cached_registration(shp, SH_CLASS_HASH(shp)) != 0) {
20227c478bd9Sstevel@tonic-gate 		(void) update_kernel_registration(shp,
20237c478bd9Sstevel@tonic-gate 		    PUBLISHER, SE_UNBIND_REGISTRATION, &pub_id, 0, NULL);
20247c478bd9Sstevel@tonic-gate 		error = EFAULT;
20257c478bd9Sstevel@tonic-gate 		goto fail;
20267c478bd9Sstevel@tonic-gate 	}
20277c478bd9Sstevel@tonic-gate 	(void) close(fd);
20287c478bd9Sstevel@tonic-gate 
20297c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(SH_LOCK(shp));
20307c478bd9Sstevel@tonic-gate 
20317c478bd9Sstevel@tonic-gate 	return (0);
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate fail:
20347c478bd9Sstevel@tonic-gate 	SH_BOUND(shp) = 0;
20357c478bd9Sstevel@tonic-gate 	(void) door_revoke(SH_DOOR_DESC(shp));
20367c478bd9Sstevel@tonic-gate 	(void) fdetach(SH_DOOR_NAME(shp));
20377c478bd9Sstevel@tonic-gate 	free(SH_DOOR_NAME(shp));
20387c478bd9Sstevel@tonic-gate 	free(pub);
20397c478bd9Sstevel@tonic-gate 	(void) close(fd);
20407c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(SH_LOCK(shp));
20417c478bd9Sstevel@tonic-gate 	errno = error;
20427c478bd9Sstevel@tonic-gate 	return (-1);
20437c478bd9Sstevel@tonic-gate }
20447c478bd9Sstevel@tonic-gate 
2045*f6e214c7SGavin Maltby static pthread_once_t xdoor_thrattr_once = PTHREAD_ONCE_INIT;
2046*f6e214c7SGavin Maltby static pthread_attr_t xdoor_thrattr;
2047*f6e214c7SGavin Maltby 
2048*f6e214c7SGavin Maltby static void
2049*f6e214c7SGavin Maltby xdoor_thrattr_init(void)
2050*f6e214c7SGavin Maltby {
2051*f6e214c7SGavin Maltby 	(void) pthread_attr_init(&xdoor_thrattr);
2052*f6e214c7SGavin Maltby 	(void) pthread_attr_setdetachstate(&xdoor_thrattr,
2053*f6e214c7SGavin Maltby 	    PTHREAD_CREATE_DETACHED);
2054*f6e214c7SGavin Maltby 	(void) pthread_attr_setscope(&xdoor_thrattr, PTHREAD_SCOPE_SYSTEM);
2055*f6e214c7SGavin Maltby }
2056*f6e214c7SGavin Maltby 
2057*f6e214c7SGavin Maltby static int
2058*f6e214c7SGavin Maltby xdoor_server_create(door_info_t *dip, void *(*startf)(void *),
2059*f6e214c7SGavin Maltby     void *startfarg, void *cookie)
2060*f6e214c7SGavin Maltby {
2061*f6e214c7SGavin Maltby 	struct sysevent_subattr_impl *xsa = cookie;
2062*f6e214c7SGavin Maltby 	pthread_attr_t *thrattr;
2063*f6e214c7SGavin Maltby 	sigset_t oset;
2064*f6e214c7SGavin Maltby 	int err;
2065*f6e214c7SGavin Maltby 
2066*f6e214c7SGavin Maltby 	if (xsa->xs_thrcreate) {
2067*f6e214c7SGavin Maltby 		return (xsa->xs_thrcreate(dip, startf, startfarg,
2068*f6e214c7SGavin Maltby 		    xsa->xs_thrcreate_cookie));
2069*f6e214c7SGavin Maltby 	}
2070*f6e214c7SGavin Maltby 
2071*f6e214c7SGavin Maltby 	if (xsa->xs_thrattr == NULL) {
2072*f6e214c7SGavin Maltby 		(void) pthread_once(&xdoor_thrattr_once, xdoor_thrattr_init);
2073*f6e214c7SGavin Maltby 		thrattr = &xdoor_thrattr;
2074*f6e214c7SGavin Maltby 	} else {
2075*f6e214c7SGavin Maltby 		thrattr = xsa->xs_thrattr;
2076*f6e214c7SGavin Maltby 	}
2077*f6e214c7SGavin Maltby 
2078*f6e214c7SGavin Maltby 	(void) pthread_sigmask(SIG_SETMASK, &xsa->xs_sigmask, &oset);
2079*f6e214c7SGavin Maltby 	err = pthread_create(NULL, thrattr, startf, startfarg);
2080*f6e214c7SGavin Maltby 	(void) pthread_sigmask(SIG_SETMASK, &oset, NULL);
2081*f6e214c7SGavin Maltby 
2082*f6e214c7SGavin Maltby 	return (err == 0 ? 1 : -1);
2083*f6e214c7SGavin Maltby }
2084*f6e214c7SGavin Maltby 
2085*f6e214c7SGavin Maltby static void
2086*f6e214c7SGavin Maltby xdoor_server_setup(void *cookie)
2087*f6e214c7SGavin Maltby {
2088*f6e214c7SGavin Maltby 	struct sysevent_subattr_impl *xsa = cookie;
2089*f6e214c7SGavin Maltby 
2090*f6e214c7SGavin Maltby 	if (xsa->xs_thrsetup) {
2091*f6e214c7SGavin Maltby 		xsa->xs_thrsetup(xsa->xs_thrsetup_cookie);
2092*f6e214c7SGavin Maltby 	} else {
2093*f6e214c7SGavin Maltby 		(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
2094*f6e214c7SGavin Maltby 		(void) pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
2095*f6e214c7SGavin Maltby 	}
2096*f6e214c7SGavin Maltby }
2097*f6e214c7SGavin Maltby 
2098*f6e214c7SGavin Maltby static int
2099*f6e214c7SGavin Maltby sysevent_bind_subscriber_cmn(sysevent_handle_t *shp,
2100*f6e214c7SGavin Maltby 	void (*event_handler)(sysevent_t *ev),
2101*f6e214c7SGavin Maltby 	sysevent_subattr_t *subattr)
21027c478bd9Sstevel@tonic-gate {
21037c478bd9Sstevel@tonic-gate 	int fd = -1;
21047c478bd9Sstevel@tonic-gate 	int error = 0;
21057c478bd9Sstevel@tonic-gate 	uint32_t sub_id = 0;
21067c478bd9Sstevel@tonic-gate 	char door_name[MAXPATHLEN];
21077c478bd9Sstevel@tonic-gate 	subscriber_priv_t *sub_info;
2108*f6e214c7SGavin Maltby 	int created;
2109*f6e214c7SGavin Maltby 	struct sysevent_subattr_impl *xsa =
2110*f6e214c7SGavin Maltby 	    (struct sysevent_subattr_impl *)subattr;
21117c478bd9Sstevel@tonic-gate 
21127c478bd9Sstevel@tonic-gate 	if (shp == NULL || event_handler == NULL) {
21137c478bd9Sstevel@tonic-gate 		errno = EINVAL;
21147c478bd9Sstevel@tonic-gate 		return (-1);
21157c478bd9Sstevel@tonic-gate 	}
21167c478bd9Sstevel@tonic-gate 
21177c478bd9Sstevel@tonic-gate 	(void) mutex_lock(SH_LOCK(shp));
21187c478bd9Sstevel@tonic-gate 	if (SH_BOUND(shp)) {
21197c478bd9Sstevel@tonic-gate 		errno = EINVAL;
21207c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(SH_LOCK(shp));
21217c478bd9Sstevel@tonic-gate 		return (-1);
21227c478bd9Sstevel@tonic-gate 	}
21237c478bd9Sstevel@tonic-gate 
21247c478bd9Sstevel@tonic-gate 	if ((sub_info = (subscriber_priv_t *)calloc(1,
21257c478bd9Sstevel@tonic-gate 	    sizeof (subscriber_priv_t))) == NULL) {
21267c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
21277c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(SH_LOCK(shp));
21287c478bd9Sstevel@tonic-gate 		return (-1);
21297c478bd9Sstevel@tonic-gate 	}
21307c478bd9Sstevel@tonic-gate 
21317c478bd9Sstevel@tonic-gate 	if (snprintf(door_name, MAXPATHLEN, "%s/%s",
21327c478bd9Sstevel@tonic-gate 	    SH_CHANNEL_PATH(shp), REG_DOOR) >= MAXPATHLEN) {
21337c478bd9Sstevel@tonic-gate 		free(sub_info);
21347c478bd9Sstevel@tonic-gate 		errno = EINVAL;
21357c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(SH_LOCK(shp));
21367c478bd9Sstevel@tonic-gate 		return (-1);
21377c478bd9Sstevel@tonic-gate 	}
21387c478bd9Sstevel@tonic-gate 
21397c478bd9Sstevel@tonic-gate 	if ((sub_info->sp_door_name = strdup(door_name)) == NULL) {
21407c478bd9Sstevel@tonic-gate 		free(sub_info);
21417c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
21427c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(SH_LOCK(shp));
21437c478bd9Sstevel@tonic-gate 		return (-1);
21447c478bd9Sstevel@tonic-gate 	}
21457c478bd9Sstevel@tonic-gate 	(void) cond_init(&sub_info->sp_cv, USYNC_THREAD, NULL);
21467c478bd9Sstevel@tonic-gate 	(void) mutex_init(&sub_info->sp_qlock, USYNC_THREAD, NULL);
21477c478bd9Sstevel@tonic-gate 	sub_info->sp_func = event_handler;
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate 	/* Update the in-kernel registration */
21507c478bd9Sstevel@tonic-gate 	if (update_kernel_registration(shp, SUBSCRIBER,
21517c478bd9Sstevel@tonic-gate 	    SE_BIND_REGISTRATION, &sub_id, 0, NULL) != 0) {
21527c478bd9Sstevel@tonic-gate 		error = errno;
21537c478bd9Sstevel@tonic-gate 		goto fail;
21547c478bd9Sstevel@tonic-gate 	}
21557c478bd9Sstevel@tonic-gate 	SH_ID(shp) = sub_id;
21567c478bd9Sstevel@tonic-gate 
21577c478bd9Sstevel@tonic-gate 	if (snprintf(door_name, MAXPATHLEN, "%s/%d",
21587c478bd9Sstevel@tonic-gate 	    SH_CHANNEL_PATH(shp), sub_id) >= MAXPATHLEN) {
21597c478bd9Sstevel@tonic-gate 		error = EINVAL;
21607c478bd9Sstevel@tonic-gate 		goto fail;
21617c478bd9Sstevel@tonic-gate 	}
21627c478bd9Sstevel@tonic-gate 	if ((SH_DOOR_NAME(shp) = strdup(door_name)) == NULL) {
21637c478bd9Sstevel@tonic-gate 		error = ENOMEM;
21647c478bd9Sstevel@tonic-gate 		goto fail;
21657c478bd9Sstevel@tonic-gate 	}
21667c478bd9Sstevel@tonic-gate 
21677c478bd9Sstevel@tonic-gate 	/*
21687c478bd9Sstevel@tonic-gate 	 * Remove door file for robustness.
21697c478bd9Sstevel@tonic-gate 	 */
21707c478bd9Sstevel@tonic-gate 	if (unlink(SH_DOOR_NAME(shp)) != 0)
21717c478bd9Sstevel@tonic-gate 		dprint("sysevent_bind_subscriber: Unlink of %s failed.\n",
21727c478bd9Sstevel@tonic-gate 		    SH_DOOR_NAME(shp));
21737c478bd9Sstevel@tonic-gate 
21747c478bd9Sstevel@tonic-gate 	fd = open(SH_DOOR_NAME(shp), O_CREAT|O_RDWR, S_IREAD|S_IWRITE);
21757c478bd9Sstevel@tonic-gate 	if (fd == -1) {
21767c478bd9Sstevel@tonic-gate 		error = EFAULT;
21777c478bd9Sstevel@tonic-gate 		goto fail;
21787c478bd9Sstevel@tonic-gate 	}
21797c478bd9Sstevel@tonic-gate 
21807c478bd9Sstevel@tonic-gate 	/*
21817c478bd9Sstevel@tonic-gate 	 * Create the sysevent door service for this client.
21827c478bd9Sstevel@tonic-gate 	 * syseventd will use this door service to propagate
21837c478bd9Sstevel@tonic-gate 	 * events to the client.
21847c478bd9Sstevel@tonic-gate 	 */
2185*f6e214c7SGavin Maltby 	if (subattr == NULL) {
2186*f6e214c7SGavin Maltby 		SH_DOOR_DESC(shp) = door_create(event_deliver_service,
2187*f6e214c7SGavin Maltby 		    (void *)shp, DOOR_REFUSE_DESC | DOOR_NO_CANCEL);
2188*f6e214c7SGavin Maltby 	} else {
2189*f6e214c7SGavin Maltby 		SH_DOOR_DESC(shp) = door_xcreate(event_deliver_service,
2190*f6e214c7SGavin Maltby 		    (void *)shp,
2191*f6e214c7SGavin Maltby 		    DOOR_REFUSE_DESC | DOOR_NO_CANCEL | DOOR_NO_DEPLETION_CB,
2192*f6e214c7SGavin Maltby 		    xdoor_server_create, xdoor_server_setup,
2193*f6e214c7SGavin Maltby 		    (void *)subattr, 1);
2194*f6e214c7SGavin Maltby 	}
2195*f6e214c7SGavin Maltby 
2196*f6e214c7SGavin Maltby 	if (SH_DOOR_DESC(shp) == -1) {
21977c478bd9Sstevel@tonic-gate 		dprint("sysevent_bind_subscriber: door create failed: "
21987c478bd9Sstevel@tonic-gate 		    "%s\n", strerror(errno));
21997c478bd9Sstevel@tonic-gate 		error = EFAULT;
22007c478bd9Sstevel@tonic-gate 		goto fail;
22017c478bd9Sstevel@tonic-gate 	}
22027c478bd9Sstevel@tonic-gate 
22037c478bd9Sstevel@tonic-gate 	(void) fdetach(SH_DOOR_NAME(shp));
22047c478bd9Sstevel@tonic-gate 	if (fattach(SH_DOOR_DESC(shp), SH_DOOR_NAME(shp)) != 0) {
22057c478bd9Sstevel@tonic-gate 		error = EFAULT;
22067c478bd9Sstevel@tonic-gate 		goto fail;
22077c478bd9Sstevel@tonic-gate 	}
22087c478bd9Sstevel@tonic-gate 	(void) close(fd);
22097c478bd9Sstevel@tonic-gate 
22107c478bd9Sstevel@tonic-gate 	if (update_publisher_cache(sub_info, SE_BIND_REGISTRATION,
22117c478bd9Sstevel@tonic-gate 	    sub_id, 0, NULL) != 0) {
22127c478bd9Sstevel@tonic-gate 		error = errno;
22137c478bd9Sstevel@tonic-gate 		(void) update_kernel_registration(shp, SUBSCRIBER,
22147c478bd9Sstevel@tonic-gate 		    SE_UNBIND_REGISTRATION, &sub_id, 0, NULL);
22157c478bd9Sstevel@tonic-gate 		goto fail;
22167c478bd9Sstevel@tonic-gate 	}
22177c478bd9Sstevel@tonic-gate 
22187c478bd9Sstevel@tonic-gate 	SH_BOUND(shp) = 1;
22197c478bd9Sstevel@tonic-gate 	SH_TYPE(shp) = SUBSCRIBER;
22207c478bd9Sstevel@tonic-gate 	SH_PRIV_DATA(shp) = (void *)sub_info;
22217c478bd9Sstevel@tonic-gate 
22227c478bd9Sstevel@tonic-gate 	/* Create an event handler thread */
2223*f6e214c7SGavin Maltby 	if (xsa == NULL || xsa->xs_thrcreate == NULL) {
2224*f6e214c7SGavin Maltby 		created = thr_create(NULL, NULL,
2225*f6e214c7SGavin Maltby 		    (void *(*)(void *))subscriber_event_handler,
2226*f6e214c7SGavin Maltby 		    shp, THR_BOUND, &sub_info->sp_handler_tid) == 0;
2227*f6e214c7SGavin Maltby 	} else {
2228*f6e214c7SGavin Maltby 		/*
2229*f6e214c7SGavin Maltby 		 * A terrible hack.  We will use the extended private
2230*f6e214c7SGavin Maltby 		 * door thread creation function the caller passed in to
2231*f6e214c7SGavin Maltby 		 * create the event handler thread.  That function will
2232*f6e214c7SGavin Maltby 		 * be called with our chosen thread start function and arg
2233*f6e214c7SGavin Maltby 		 * instead of the usual libc-provided ones, but that's ok
2234*f6e214c7SGavin Maltby 		 * as it is required to use them verbatim anyway.  We will
2235*f6e214c7SGavin Maltby 		 * pass a NULL door_info_t pointer to the function - so
2236*f6e214c7SGavin Maltby 		 * callers depending on this hack had better be prepared
2237*f6e214c7SGavin Maltby 		 * for that.  All this allow the caller to rubberstamp
2238*f6e214c7SGavin Maltby 		 * the created thread as it wishes.  But we don't get
2239*f6e214c7SGavin Maltby 		 * the created threadid with this, so we modify the
2240*f6e214c7SGavin Maltby 		 * thread start function to stash it.
2241*f6e214c7SGavin Maltby 		 */
2242*f6e214c7SGavin Maltby 
2243*f6e214c7SGavin Maltby 		created = xsa->xs_thrcreate(NULL,
2244*f6e214c7SGavin Maltby 		    (void *(*)(void *))subscriber_event_handler,
2245*f6e214c7SGavin Maltby 		    shp, xsa->xs_thrcreate_cookie) == 1;
2246*f6e214c7SGavin Maltby 	}
2247*f6e214c7SGavin Maltby 
2248*f6e214c7SGavin Maltby 	if (!created) {
22497c478bd9Sstevel@tonic-gate 		error = EFAULT;
22507c478bd9Sstevel@tonic-gate 		goto fail;
22517c478bd9Sstevel@tonic-gate 	}
22527c478bd9Sstevel@tonic-gate 
22537c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(SH_LOCK(shp));
22547c478bd9Sstevel@tonic-gate 
22557c478bd9Sstevel@tonic-gate 	return (0);
22567c478bd9Sstevel@tonic-gate 
22577c478bd9Sstevel@tonic-gate fail:
22587c478bd9Sstevel@tonic-gate 	(void) close(fd);
22597c478bd9Sstevel@tonic-gate 	(void) door_revoke(SH_DOOR_DESC(shp));
22607c478bd9Sstevel@tonic-gate 	(void) fdetach(SH_DOOR_NAME(shp));
22617c478bd9Sstevel@tonic-gate 	(void) cond_destroy(&sub_info->sp_cv);
22627c478bd9Sstevel@tonic-gate 	(void) mutex_destroy(&sub_info->sp_qlock);
22637c478bd9Sstevel@tonic-gate 	free(sub_info->sp_door_name);
22647c478bd9Sstevel@tonic-gate 	free(sub_info);
22657c478bd9Sstevel@tonic-gate 	if (SH_ID(shp)) {
22667c478bd9Sstevel@tonic-gate 		(void) update_kernel_registration(shp, SUBSCRIBER,
22677c478bd9Sstevel@tonic-gate 		    SE_UNBIND_REGISTRATION, &sub_id, 0, NULL);
22687c478bd9Sstevel@tonic-gate 		SH_ID(shp) = 0;
22697c478bd9Sstevel@tonic-gate 	}
22707c478bd9Sstevel@tonic-gate 	if (SH_BOUND(shp)) {
22717c478bd9Sstevel@tonic-gate 		(void) update_publisher_cache(sub_info, SE_UNBIND_REGISTRATION,
22727c478bd9Sstevel@tonic-gate 		    sub_id, 0, NULL);
22737c478bd9Sstevel@tonic-gate 		free(SH_DOOR_NAME(shp));
22747c478bd9Sstevel@tonic-gate 		SH_BOUND(shp) = 0;
22757c478bd9Sstevel@tonic-gate 	}
22767c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(SH_LOCK(shp));
22777c478bd9Sstevel@tonic-gate 
22787c478bd9Sstevel@tonic-gate 	errno = error;
22797c478bd9Sstevel@tonic-gate 
22807c478bd9Sstevel@tonic-gate 	return (-1);
22817c478bd9Sstevel@tonic-gate }
22827c478bd9Sstevel@tonic-gate 
22837c478bd9Sstevel@tonic-gate /*
2284*f6e214c7SGavin Maltby  * sysevent_bind_subscriber - Bind an event receiver to an event channel
2285*f6e214c7SGavin Maltby  */
2286*f6e214c7SGavin Maltby int
2287*f6e214c7SGavin Maltby sysevent_bind_subscriber(sysevent_handle_t *shp,
2288*f6e214c7SGavin Maltby 	void (*event_handler)(sysevent_t *ev))
2289*f6e214c7SGavin Maltby {
2290*f6e214c7SGavin Maltby 	return (sysevent_bind_subscriber_cmn(shp, event_handler, NULL));
2291*f6e214c7SGavin Maltby }
2292*f6e214c7SGavin Maltby 
2293*f6e214c7SGavin Maltby /*
2294*f6e214c7SGavin Maltby  * sysevent_bind_xsubscriber - Bind a subscriber using door_xcreate with
2295*f6e214c7SGavin Maltby  * attributes specified.
2296*f6e214c7SGavin Maltby  */
2297*f6e214c7SGavin Maltby int
2298*f6e214c7SGavin Maltby sysevent_bind_xsubscriber(sysevent_handle_t *shp,
2299*f6e214c7SGavin Maltby 	void (*event_handler)(sysevent_t *ev), sysevent_subattr_t *subattr)
2300*f6e214c7SGavin Maltby {
2301*f6e214c7SGavin Maltby 	return (sysevent_bind_subscriber_cmn(shp, event_handler, subattr));
2302*f6e214c7SGavin Maltby }
2303*f6e214c7SGavin Maltby 
2304*f6e214c7SGavin Maltby /*
23057c478bd9Sstevel@tonic-gate  * sysevent_register_event - register an event class and associated subclasses
23067c478bd9Sstevel@tonic-gate  *		for an event subscriber
23077c478bd9Sstevel@tonic-gate  */
23087c478bd9Sstevel@tonic-gate int
23097c478bd9Sstevel@tonic-gate sysevent_register_event(sysevent_handle_t *shp,
23107c478bd9Sstevel@tonic-gate 	const char *ev_class, const char **ev_subclass,
23117c478bd9Sstevel@tonic-gate 	int subclass_num)
23127c478bd9Sstevel@tonic-gate {
23137c478bd9Sstevel@tonic-gate 	int error;
23147c478bd9Sstevel@tonic-gate 	char *event_class = (char *)ev_class;
23157c478bd9Sstevel@tonic-gate 	char **event_subclass_list = (char **)ev_subclass;
23167c478bd9Sstevel@tonic-gate 	char *nvlbuf = NULL;
23177c478bd9Sstevel@tonic-gate 	size_t datalen;
23187c478bd9Sstevel@tonic-gate 	nvlist_t *nvl;
23197c478bd9Sstevel@tonic-gate 
23207c478bd9Sstevel@tonic-gate 	(void) mutex_lock(SH_LOCK(shp));
23217c478bd9Sstevel@tonic-gate 	if (event_class == NULL || event_subclass_list == NULL ||
23227c478bd9Sstevel@tonic-gate 	    event_subclass_list[0] == NULL || SH_BOUND(shp) != 1 ||
23237c478bd9Sstevel@tonic-gate 	    subclass_num <= 0) {
23247c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(SH_LOCK(shp));
23257c478bd9Sstevel@tonic-gate 		errno = EINVAL;
23267c478bd9Sstevel@tonic-gate 		return (-1);
23277c478bd9Sstevel@tonic-gate 	}
23287c478bd9Sstevel@tonic-gate 
23297c478bd9Sstevel@tonic-gate 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME_TYPE, 0) != 0) {
23307c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(SH_LOCK(shp));
23317c478bd9Sstevel@tonic-gate 		return (-1);
23327c478bd9Sstevel@tonic-gate 	}
23337c478bd9Sstevel@tonic-gate 	if (nvlist_add_string_array(nvl, event_class, event_subclass_list,
23347c478bd9Sstevel@tonic-gate 	    subclass_num) != 0) {
23357c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
23367c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(SH_LOCK(shp));
23377c478bd9Sstevel@tonic-gate 		return (-1);
23387c478bd9Sstevel@tonic-gate 	}
23397c478bd9Sstevel@tonic-gate 	if (nvlist_pack(nvl, &nvlbuf, &datalen, NV_ENCODE_NATIVE, 0) != 0) {
23407c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
23417c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(SH_LOCK(shp));
23427c478bd9Sstevel@tonic-gate 		return (-1);
23437c478bd9Sstevel@tonic-gate 	}
23447c478bd9Sstevel@tonic-gate 	nvlist_free(nvl);
23457c478bd9Sstevel@tonic-gate 
23467c478bd9Sstevel@tonic-gate 	/* Store new subscriber in in-kernel registration */
23477c478bd9Sstevel@tonic-gate 	if (update_kernel_registration(shp, SUBSCRIBER,
23487c478bd9Sstevel@tonic-gate 	    SE_REGISTER, &SH_ID(shp), datalen, (uchar_t *)nvlbuf)
23497c478bd9Sstevel@tonic-gate 	    != 0) {
23507c478bd9Sstevel@tonic-gate 		error = errno;
23517c478bd9Sstevel@tonic-gate 		free(nvlbuf);
23527c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(SH_LOCK(shp));
23537c478bd9Sstevel@tonic-gate 		errno = error;
23547c478bd9Sstevel@tonic-gate 		return (-1);
23557c478bd9Sstevel@tonic-gate 	}
23567c478bd9Sstevel@tonic-gate 	/* Update the publisher's cached registration */
23577c478bd9Sstevel@tonic-gate 	if (update_publisher_cache(
23587c478bd9Sstevel@tonic-gate 	    (subscriber_priv_t *)SH_PRIV_DATA(shp), SE_REGISTER,
23597c478bd9Sstevel@tonic-gate 	    SH_ID(shp), datalen, (uchar_t *)nvlbuf) != 0) {
23607c478bd9Sstevel@tonic-gate 		error = errno;
23617c478bd9Sstevel@tonic-gate 		free(nvlbuf);
23627c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(SH_LOCK(shp));
23637c478bd9Sstevel@tonic-gate 		errno = error;
23647c478bd9Sstevel@tonic-gate 		return (-1);
23657c478bd9Sstevel@tonic-gate 	}
23667c478bd9Sstevel@tonic-gate 
23677c478bd9Sstevel@tonic-gate 	free(nvlbuf);
23687c478bd9Sstevel@tonic-gate 
23697c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(SH_LOCK(shp));
23707c478bd9Sstevel@tonic-gate 
23717c478bd9Sstevel@tonic-gate 	return (0);
23727c478bd9Sstevel@tonic-gate }
23737c478bd9Sstevel@tonic-gate 
23747c478bd9Sstevel@tonic-gate /*
23757c478bd9Sstevel@tonic-gate  * sysevent_unregister_event - Unregister an event class and associated
23767c478bd9Sstevel@tonic-gate  *				subclasses for an event subscriber
23777c478bd9Sstevel@tonic-gate  */
23787c478bd9Sstevel@tonic-gate void
23797c478bd9Sstevel@tonic-gate sysevent_unregister_event(sysevent_handle_t *shp, const char *class)
23807c478bd9Sstevel@tonic-gate {
23817c478bd9Sstevel@tonic-gate 	size_t class_sz;
23827c478bd9Sstevel@tonic-gate 
23837c478bd9Sstevel@tonic-gate 	(void) mutex_lock(SH_LOCK(shp));
23847c478bd9Sstevel@tonic-gate 
23857c478bd9Sstevel@tonic-gate 	if (!SH_BOUND(shp)) {
23867c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(SH_LOCK(shp));
23877c478bd9Sstevel@tonic-gate 		return;
23887c478bd9Sstevel@tonic-gate 	}
23897c478bd9Sstevel@tonic-gate 
23907c478bd9Sstevel@tonic-gate 	/* Remove subscriber from in-kernel registration */
23917c478bd9Sstevel@tonic-gate 	class_sz = strlen(class) + 1;
23927c478bd9Sstevel@tonic-gate 	(void) update_kernel_registration(shp, SUBSCRIBER,
23937c478bd9Sstevel@tonic-gate 	    SE_UNREGISTER, &SH_ID(shp), class_sz, (uchar_t *)class);
23947c478bd9Sstevel@tonic-gate 	/* Update the publisher's cached registration */
23957c478bd9Sstevel@tonic-gate 	(void) update_publisher_cache(
23967c478bd9Sstevel@tonic-gate 	    (subscriber_priv_t *)SH_PRIV_DATA(shp), SE_UNREGISTER,
23977c478bd9Sstevel@tonic-gate 	    SH_ID(shp), class_sz, (uchar_t *)class);
23987c478bd9Sstevel@tonic-gate 
23997c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(SH_LOCK(shp));
24007c478bd9Sstevel@tonic-gate }
24017c478bd9Sstevel@tonic-gate 
24027c478bd9Sstevel@tonic-gate static int
24037c478bd9Sstevel@tonic-gate cleanup_id(sysevent_handle_t *shp, uint32_t id, int type)
24047c478bd9Sstevel@tonic-gate {
24057c478bd9Sstevel@tonic-gate 	dprint("cleanup_id: Cleaning up %s/%d\n", SH_CHANNEL_NAME(shp), id);
24067c478bd9Sstevel@tonic-gate 
24077c478bd9Sstevel@tonic-gate 	/* Remove registration from the kernel */
24087c478bd9Sstevel@tonic-gate 	if (update_kernel_registration(shp, type, SE_CLEANUP, &id,
24097c478bd9Sstevel@tonic-gate 	    0, NULL) != 0) {
24107c478bd9Sstevel@tonic-gate 		dprint("cleanup_id: Unable to clean "
24117c478bd9Sstevel@tonic-gate 		    "up %s/%d\n", SH_CHANNEL_NAME(shp), id);
24127c478bd9Sstevel@tonic-gate 		return (-1);
24137c478bd9Sstevel@tonic-gate 	}
24147c478bd9Sstevel@tonic-gate 
24157c478bd9Sstevel@tonic-gate 	return (0);
24167c478bd9Sstevel@tonic-gate }
24177c478bd9Sstevel@tonic-gate 
24187c478bd9Sstevel@tonic-gate /*
24197c478bd9Sstevel@tonic-gate  * sysevent_cleanup_subscribers: Allows the caller to cleanup resources
24207c478bd9Sstevel@tonic-gate  *		allocated to unresponsive subscribers.
24217c478bd9Sstevel@tonic-gate  */
24227c478bd9Sstevel@tonic-gate void
24237c478bd9Sstevel@tonic-gate sysevent_cleanup_subscribers(sysevent_handle_t *shp)
24247c478bd9Sstevel@tonic-gate {
24257c478bd9Sstevel@tonic-gate 	uint32_t ping, result;
24267c478bd9Sstevel@tonic-gate 	int i, error, sub_fd;
24277c478bd9Sstevel@tonic-gate 	subscriber_data_t *sub;
24287c478bd9Sstevel@tonic-gate 
24297c478bd9Sstevel@tonic-gate 	if (!SH_BOUND(shp)) {
24307c478bd9Sstevel@tonic-gate 		return;
24317c478bd9Sstevel@tonic-gate 	}
24327c478bd9Sstevel@tonic-gate 
24337c478bd9Sstevel@tonic-gate 	for (i = 1; i <= MAX_SUBSCRIBERS; ++i) {
24347c478bd9Sstevel@tonic-gate 
24357c478bd9Sstevel@tonic-gate 		sub = SH_SUBSCRIBER(shp, i);
24367c478bd9Sstevel@tonic-gate 		if (sub == NULL) {
24377c478bd9Sstevel@tonic-gate 			continue;
24387c478bd9Sstevel@tonic-gate 		}
24397c478bd9Sstevel@tonic-gate 
24407c478bd9Sstevel@tonic-gate 		if ((sub_fd = open(sub->sd_door_name, O_RDONLY)) == -1) {
24417c478bd9Sstevel@tonic-gate 			continue;
24427c478bd9Sstevel@tonic-gate 		}
24437c478bd9Sstevel@tonic-gate 		/* Check for valid and responsive subscriber */
24447c478bd9Sstevel@tonic-gate 		error = clnt_deliver_event(sub_fd, &ping,
24457c478bd9Sstevel@tonic-gate 		    sizeof (uint32_t), &result, sizeof (result));
24467c478bd9Sstevel@tonic-gate 		(void) close(sub_fd);
24477c478bd9Sstevel@tonic-gate 
24487c478bd9Sstevel@tonic-gate 		/* Only cleanup on EBADF (Invalid door descriptor) */
24497c478bd9Sstevel@tonic-gate 		if (error != EBADF)
24507c478bd9Sstevel@tonic-gate 			continue;
24517c478bd9Sstevel@tonic-gate 
24527c478bd9Sstevel@tonic-gate 		if (cleanup_id(shp, i, SUBSCRIBER) != 0)
24537c478bd9Sstevel@tonic-gate 			continue;
24547c478bd9Sstevel@tonic-gate 
24557c478bd9Sstevel@tonic-gate 		cache_remove_class(shp, EC_ALL, i);
24567c478bd9Sstevel@tonic-gate 
24577c478bd9Sstevel@tonic-gate 		free(sub->sd_door_name);
24587c478bd9Sstevel@tonic-gate 		free(sub);
24597c478bd9Sstevel@tonic-gate 		SH_SUBSCRIBER(shp, i) = NULL;
24607c478bd9Sstevel@tonic-gate 	}
24617c478bd9Sstevel@tonic-gate 
24627c478bd9Sstevel@tonic-gate }
24637c478bd9Sstevel@tonic-gate 
24647c478bd9Sstevel@tonic-gate /*
24657c478bd9Sstevel@tonic-gate  * sysevent_cleanup_publishers: Allows stale publisher handles to be deallocated
24667c478bd9Sstevel@tonic-gate  *		as needed.
24677c478bd9Sstevel@tonic-gate  */
24687c478bd9Sstevel@tonic-gate void
24697c478bd9Sstevel@tonic-gate sysevent_cleanup_publishers(sysevent_handle_t *shp)
24707c478bd9Sstevel@tonic-gate {
24717c478bd9Sstevel@tonic-gate 	(void) cleanup_id(shp, 1, PUBLISHER);
24727c478bd9Sstevel@tonic-gate }
24737c478bd9Sstevel@tonic-gate 
24747c478bd9Sstevel@tonic-gate /*
24757c478bd9Sstevel@tonic-gate  * sysevent_unbind_subscriber: Unbind the subscriber from the sysevent channel.
24767c478bd9Sstevel@tonic-gate  */
24777c478bd9Sstevel@tonic-gate void
24787c478bd9Sstevel@tonic-gate sysevent_unbind_subscriber(sysevent_handle_t *shp)
24797c478bd9Sstevel@tonic-gate {
24807c478bd9Sstevel@tonic-gate 	subscriber_priv_t *sub_info;
24817c478bd9Sstevel@tonic-gate 
24827c478bd9Sstevel@tonic-gate 	if (shp == NULL)
24837c478bd9Sstevel@tonic-gate 		return;
24847c478bd9Sstevel@tonic-gate 
24857c478bd9Sstevel@tonic-gate 	(void) mutex_lock(SH_LOCK(shp));
24867c478bd9Sstevel@tonic-gate 	if (SH_BOUND(shp) == 0) {
24877c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(SH_LOCK(shp));
24887c478bd9Sstevel@tonic-gate 		return;
24897c478bd9Sstevel@tonic-gate 	}
24907c478bd9Sstevel@tonic-gate 
24917c478bd9Sstevel@tonic-gate 	/* Update the in-kernel registration */
24927c478bd9Sstevel@tonic-gate 	(void) update_kernel_registration(shp, SUBSCRIBER,
24937c478bd9Sstevel@tonic-gate 	    SE_UNBIND_REGISTRATION, &SH_ID(shp), 0, NULL);
24947c478bd9Sstevel@tonic-gate 
24957c478bd9Sstevel@tonic-gate 	/* Update the sysevent channel publisher */
24967c478bd9Sstevel@tonic-gate 	sub_info = (subscriber_priv_t *)SH_PRIV_DATA(shp);
24977c478bd9Sstevel@tonic-gate 	(void) update_publisher_cache(sub_info, SE_UNBIND_REGISTRATION,
24987c478bd9Sstevel@tonic-gate 	    SH_ID(shp), 0, NULL);
24997c478bd9Sstevel@tonic-gate 
25007c478bd9Sstevel@tonic-gate 	/* Close down event delivery facilities */
25017c478bd9Sstevel@tonic-gate 	(void) door_revoke(SH_DOOR_DESC(shp));
25027c478bd9Sstevel@tonic-gate 	(void) fdetach(SH_DOOR_NAME(shp));
25037c478bd9Sstevel@tonic-gate 
25047c478bd9Sstevel@tonic-gate 	/*
25057c478bd9Sstevel@tonic-gate 	 * Release resources and wait for pending event delivery to
25067c478bd9Sstevel@tonic-gate 	 * complete.
25077c478bd9Sstevel@tonic-gate 	 */
25087c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&sub_info->sp_qlock);
25097c478bd9Sstevel@tonic-gate 	SH_BOUND(shp) = 0;
25107c478bd9Sstevel@tonic-gate 	/* Signal event handler and drain the subscriber's event queue */
25117c478bd9Sstevel@tonic-gate 	(void) cond_signal(&sub_info->sp_cv);
25127c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&sub_info->sp_qlock);
2513*f6e214c7SGavin Maltby 	if (sub_info->sp_handler_tid != NULL)
25147c478bd9Sstevel@tonic-gate 		(void) thr_join(sub_info->sp_handler_tid, NULL, NULL);
25157c478bd9Sstevel@tonic-gate 
25167c478bd9Sstevel@tonic-gate 	(void) cond_destroy(&sub_info->sp_cv);
25177c478bd9Sstevel@tonic-gate 	(void) mutex_destroy(&sub_info->sp_qlock);
25187c478bd9Sstevel@tonic-gate 	free(sub_info->sp_door_name);
25197c478bd9Sstevel@tonic-gate 	free(sub_info);
25207c478bd9Sstevel@tonic-gate 	free(SH_DOOR_NAME(shp));
25217c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(SH_LOCK(shp));
25227c478bd9Sstevel@tonic-gate }
25237c478bd9Sstevel@tonic-gate 
25247c478bd9Sstevel@tonic-gate /*
25257c478bd9Sstevel@tonic-gate  * sysevent_unbind_publisher: Unbind publisher from the sysevent channel.
25267c478bd9Sstevel@tonic-gate  */
25277c478bd9Sstevel@tonic-gate void
25287c478bd9Sstevel@tonic-gate sysevent_unbind_publisher(sysevent_handle_t *shp)
25297c478bd9Sstevel@tonic-gate {
25307c478bd9Sstevel@tonic-gate 	if (shp == NULL)
25317c478bd9Sstevel@tonic-gate 		return;
25327c478bd9Sstevel@tonic-gate 
25337c478bd9Sstevel@tonic-gate 	(void) mutex_lock(SH_LOCK(shp));
25347c478bd9Sstevel@tonic-gate 	if (SH_BOUND(shp) == 0) {
25357c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(SH_LOCK(shp));
25367c478bd9Sstevel@tonic-gate 		return;
25377c478bd9Sstevel@tonic-gate 	}
25387c478bd9Sstevel@tonic-gate 
25397c478bd9Sstevel@tonic-gate 	/* Close down the registration facilities */
25407c478bd9Sstevel@tonic-gate 	(void) door_revoke(SH_DOOR_DESC(shp));
25417c478bd9Sstevel@tonic-gate 	(void) fdetach(SH_DOOR_NAME(shp));
25427c478bd9Sstevel@tonic-gate 
25437c478bd9Sstevel@tonic-gate 	/* Update the in-kernel registration */
25447c478bd9Sstevel@tonic-gate 	(void) update_kernel_registration(shp, PUBLISHER,
25457c478bd9Sstevel@tonic-gate 	    SE_UNBIND_REGISTRATION, &SH_ID(shp), 0, NULL);
25467c478bd9Sstevel@tonic-gate 	SH_BOUND(shp) = 0;
25477c478bd9Sstevel@tonic-gate 
25487c478bd9Sstevel@tonic-gate 	/* Free resources associated with bind */
25497c478bd9Sstevel@tonic-gate 	free_cached_registration(shp);
25507c478bd9Sstevel@tonic-gate 	dealloc_subscribers(shp);
25517c478bd9Sstevel@tonic-gate 
25527c478bd9Sstevel@tonic-gate 	free(SH_PRIV_DATA(shp));
25537c478bd9Sstevel@tonic-gate 	free(SH_DOOR_NAME(shp));
25547c478bd9Sstevel@tonic-gate 	SH_ID(shp) = 0;
25557c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(SH_LOCK(shp));
25567c478bd9Sstevel@tonic-gate }
25577c478bd9Sstevel@tonic-gate 
25587c478bd9Sstevel@tonic-gate /*
25597c478bd9Sstevel@tonic-gate  * Evolving APIs to subscribe to syseventd(1M) system events.
25607c478bd9Sstevel@tonic-gate  */
25617c478bd9Sstevel@tonic-gate 
2562*f6e214c7SGavin Maltby static sysevent_handle_t *
2563*f6e214c7SGavin Maltby sysevent_bind_handle_cmn(void (*event_handler)(sysevent_t *ev),
2564*f6e214c7SGavin Maltby     sysevent_subattr_t *subattr)
25657c478bd9Sstevel@tonic-gate {
25667c478bd9Sstevel@tonic-gate 	sysevent_handle_t *shp;
25677c478bd9Sstevel@tonic-gate 
25687c478bd9Sstevel@tonic-gate 	if (getuid() != 0) {
25697c478bd9Sstevel@tonic-gate 		errno = EACCES;
25707c478bd9Sstevel@tonic-gate 		return (NULL);
25717c478bd9Sstevel@tonic-gate 	}
25727c478bd9Sstevel@tonic-gate 
25737c478bd9Sstevel@tonic-gate 	if (event_handler == NULL) {
25747c478bd9Sstevel@tonic-gate 		errno = EINVAL;
25757c478bd9Sstevel@tonic-gate 		return (NULL);
25767c478bd9Sstevel@tonic-gate 	}
25777c478bd9Sstevel@tonic-gate 
25787c478bd9Sstevel@tonic-gate 	if ((shp = sysevent_open_channel(SYSEVENTD_CHAN)) == NULL) {
25797c478bd9Sstevel@tonic-gate 		return (NULL);
25807c478bd9Sstevel@tonic-gate 	}
25817c478bd9Sstevel@tonic-gate 
2582*f6e214c7SGavin Maltby 	if (sysevent_bind_xsubscriber(shp, event_handler, subattr) != 0) {
25837c478bd9Sstevel@tonic-gate 		/*
25847c478bd9Sstevel@tonic-gate 		 * Ask syseventd to clean-up any stale subcribers and try to
25857c478bd9Sstevel@tonic-gate 		 * to bind again
25867c478bd9Sstevel@tonic-gate 		 */
25877c478bd9Sstevel@tonic-gate 		if (errno == EBUSY) {
25887c478bd9Sstevel@tonic-gate 			int pub_fd;
25897c478bd9Sstevel@tonic-gate 			char door_name[MAXPATHLEN];
25907c478bd9Sstevel@tonic-gate 			uint32_t result;
25917c478bd9Sstevel@tonic-gate 			struct reg_args rargs;
25927c478bd9Sstevel@tonic-gate 
25937c478bd9Sstevel@tonic-gate 			if (snprintf(door_name, MAXPATHLEN, "%s/%s",
25947c478bd9Sstevel@tonic-gate 			    SH_CHANNEL_PATH(shp), REG_DOOR) >= MAXPATHLEN) {
25957c478bd9Sstevel@tonic-gate 				sysevent_close_channel(shp);
25967c478bd9Sstevel@tonic-gate 				errno = EINVAL;
25977c478bd9Sstevel@tonic-gate 				return (NULL);
25987c478bd9Sstevel@tonic-gate 			}
25997c478bd9Sstevel@tonic-gate 
26007c478bd9Sstevel@tonic-gate 			rargs.ra_op = SE_CLEANUP;
26017c478bd9Sstevel@tonic-gate 			pub_fd = open(door_name, O_RDONLY);
26027c478bd9Sstevel@tonic-gate 			(void) clnt_deliver_event(pub_fd, (void *)&rargs,
26037c478bd9Sstevel@tonic-gate 			    sizeof (struct reg_args), &result, sizeof (result));
26047c478bd9Sstevel@tonic-gate 			(void) close(pub_fd);
26057c478bd9Sstevel@tonic-gate 
26067c478bd9Sstevel@tonic-gate 			/* Try to bind again */
2607*f6e214c7SGavin Maltby 			if (sysevent_bind_xsubscriber(shp, event_handler,
2608*f6e214c7SGavin Maltby 			    subattr) != 0) {
26097c478bd9Sstevel@tonic-gate 				sysevent_close_channel(shp);
26107c478bd9Sstevel@tonic-gate 				return (NULL);
26117c478bd9Sstevel@tonic-gate 			}
26127c478bd9Sstevel@tonic-gate 		} else {
26137c478bd9Sstevel@tonic-gate 			sysevent_close_channel(shp);
26147c478bd9Sstevel@tonic-gate 			return (NULL);
26157c478bd9Sstevel@tonic-gate 		}
26167c478bd9Sstevel@tonic-gate 	}
26177c478bd9Sstevel@tonic-gate 
26187c478bd9Sstevel@tonic-gate 	return (shp);
26197c478bd9Sstevel@tonic-gate }
26207c478bd9Sstevel@tonic-gate 
26217c478bd9Sstevel@tonic-gate /*
2622*f6e214c7SGavin Maltby  * sysevent_bind_handle - Bind application event handler for syseventd
2623*f6e214c7SGavin Maltby  *		subscription.
2624*f6e214c7SGavin Maltby  */
2625*f6e214c7SGavin Maltby sysevent_handle_t *
2626*f6e214c7SGavin Maltby sysevent_bind_handle(void (*event_handler)(sysevent_t *ev))
2627*f6e214c7SGavin Maltby {
2628*f6e214c7SGavin Maltby 	return (sysevent_bind_handle_cmn(event_handler, NULL));
2629*f6e214c7SGavin Maltby }
2630*f6e214c7SGavin Maltby 
2631*f6e214c7SGavin Maltby /*
2632*f6e214c7SGavin Maltby  * sysevent_bind_xhandle - Bind application event handler for syseventd
2633*f6e214c7SGavin Maltby  *		subscription, using door_xcreate and attributes as specified.
2634*f6e214c7SGavin Maltby  */
2635*f6e214c7SGavin Maltby sysevent_handle_t *
2636*f6e214c7SGavin Maltby sysevent_bind_xhandle(void (*event_handler)(sysevent_t *ev),
2637*f6e214c7SGavin Maltby     sysevent_subattr_t *subattr)
2638*f6e214c7SGavin Maltby {
2639*f6e214c7SGavin Maltby 	return (sysevent_bind_handle_cmn(event_handler, subattr));
2640*f6e214c7SGavin Maltby }
2641*f6e214c7SGavin Maltby 
2642*f6e214c7SGavin Maltby /*
26437c478bd9Sstevel@tonic-gate  * sysevent_unbind_handle - Unbind caller from syseventd subscriptions
26447c478bd9Sstevel@tonic-gate  */
26457c478bd9Sstevel@tonic-gate void
26467c478bd9Sstevel@tonic-gate sysevent_unbind_handle(sysevent_handle_t *shp)
26477c478bd9Sstevel@tonic-gate {
26487c478bd9Sstevel@tonic-gate 	sysevent_unbind_subscriber(shp);
26497c478bd9Sstevel@tonic-gate 	sysevent_close_channel(shp);
26507c478bd9Sstevel@tonic-gate }
26517c478bd9Sstevel@tonic-gate 
26527c478bd9Sstevel@tonic-gate /*
26537c478bd9Sstevel@tonic-gate  * sysevent_subscribe_event - Subscribe to system event notification from
26547c478bd9Sstevel@tonic-gate  *			syseventd(1M) for the class and subclasses specified.
26557c478bd9Sstevel@tonic-gate  */
26567c478bd9Sstevel@tonic-gate int
26577c478bd9Sstevel@tonic-gate sysevent_subscribe_event(sysevent_handle_t *shp, const char *event_class,
26587c478bd9Sstevel@tonic-gate 	const char **event_subclass_list, int num_subclasses)
26597c478bd9Sstevel@tonic-gate {
26607c478bd9Sstevel@tonic-gate 	return (sysevent_register_event(shp, event_class,
26617c478bd9Sstevel@tonic-gate 	    event_subclass_list, num_subclasses));
26627c478bd9Sstevel@tonic-gate }
26637c478bd9Sstevel@tonic-gate 
26647c478bd9Sstevel@tonic-gate void
26657c478bd9Sstevel@tonic-gate sysevent_unsubscribe_event(sysevent_handle_t *shp, const char *event_class)
26667c478bd9Sstevel@tonic-gate {
26677c478bd9Sstevel@tonic-gate 	sysevent_unregister_event(shp, event_class);
26687c478bd9Sstevel@tonic-gate }
2669