xref: /illumos-gate/usr/src/cmd/picl/plugins/common/piclevent/picl_slm.c (revision e9610e3e86cbfb5fd6797a438c65b493250b4219)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <unistd.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <errno.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate #include <door.h>
327c478bd9Sstevel@tonic-gate #include <fcntl.h>
337c478bd9Sstevel@tonic-gate #include <syslog.h>
347c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
357c478bd9Sstevel@tonic-gate #include <libsysevent.h>
367c478bd9Sstevel@tonic-gate #include <picl.h>
377c478bd9Sstevel@tonic-gate #include <pthread.h>
387c478bd9Sstevel@tonic-gate #include "piclevent.h"
397c478bd9Sstevel@tonic-gate #include <sys/sysevent/eventdefs.h>
407c478bd9Sstevel@tonic-gate #include <sys/sysevent/dr.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #define	PICLSLM_DOOR_FAILED	gettext("PICL SLM door create failed\n")
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  * syseventd event handler
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate static	int	piclslm_debug = 0;
487c478bd9Sstevel@tonic-gate static	int	piclslm_deliver_event(sysevent_t *ev, int flag);
497c478bd9Sstevel@tonic-gate static	int	door_fd = -1;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate typedef struct nvlist_queue {
527c478bd9Sstevel@tonic-gate 	char			*nvq_item;	/* packed nvlist */
537c478bd9Sstevel@tonic-gate 	size_t			nvq_sz;		/* buf size */
547c478bd9Sstevel@tonic-gate 	struct nvlist_queue	*nvq_next;
557c478bd9Sstevel@tonic-gate } nvlist_queue_t;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate static nvlist_queue_t	*nvq_head;
587c478bd9Sstevel@tonic-gate static nvlist_queue_t	*nvq_tail;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate static mutex_t	nvq_lock;
617c478bd9Sstevel@tonic-gate static cond_t	nvq_cv;
627c478bd9Sstevel@tonic-gate static thread_t	piclslm_deliver_thr_id;
637c478bd9Sstevel@tonic-gate static int	cleanup;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate static struct slm_mod_ops piclslm_mod_ops = {
667c478bd9Sstevel@tonic-gate 	SE_MAJOR_VERSION, SE_MINOR_VERSION, SE_MAX_RETRY_LIMIT,
677c478bd9Sstevel@tonic-gate 	piclslm_deliver_event};
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate static void
init_queue(void)717c478bd9Sstevel@tonic-gate init_queue(void)
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate 	nvq_head = NULL;
747c478bd9Sstevel@tonic-gate 	nvq_tail = NULL;
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate static int
add_to_queue(char * nvl,size_t sz)787c478bd9Sstevel@tonic-gate add_to_queue(char *nvl, size_t sz)
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate 	nvlist_queue_t	*new_nvq;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	new_nvq = malloc(sizeof (*new_nvq));
837c478bd9Sstevel@tonic-gate 	if (new_nvq == NULL)
847c478bd9Sstevel@tonic-gate 		return (-1);
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	new_nvq->nvq_item = nvl;
877c478bd9Sstevel@tonic-gate 	new_nvq->nvq_sz = sz;
887c478bd9Sstevel@tonic-gate 	new_nvq->nvq_next = NULL;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	if (nvq_head == NULL)
917c478bd9Sstevel@tonic-gate 		nvq_head = new_nvq;
927c478bd9Sstevel@tonic-gate 	else
937c478bd9Sstevel@tonic-gate 		nvq_tail->nvq_next = new_nvq;
947c478bd9Sstevel@tonic-gate 	nvq_tail = new_nvq;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	return (0);
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate static nvlist_queue_t *
remove_from_queue(void)1007c478bd9Sstevel@tonic-gate remove_from_queue(void)
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate 	nvlist_queue_t	*nvqp;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	if (nvq_head == NULL)
1057c478bd9Sstevel@tonic-gate 		return (NULL);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	nvqp = nvq_head;
1087c478bd9Sstevel@tonic-gate 	nvq_head = nvq_head->nvq_next;
1097c478bd9Sstevel@tonic-gate 	if (nvq_head == NULL)
1107c478bd9Sstevel@tonic-gate 		nvq_tail = NULL;
1117c478bd9Sstevel@tonic-gate 	return (nvqp);
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate static void
free_nvqueue(nvlist_queue_t * nvqp)1157c478bd9Sstevel@tonic-gate free_nvqueue(nvlist_queue_t *nvqp)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate 	free(nvqp->nvq_item);
1187c478bd9Sstevel@tonic-gate 	free(nvqp);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate  * deliver the event to the plugin if the door exists
1237c478bd9Sstevel@tonic-gate  */
1247c478bd9Sstevel@tonic-gate static void
post_piclevent(char * pack_buf,size_t nvl_size)1257c478bd9Sstevel@tonic-gate post_piclevent(char *pack_buf, size_t nvl_size)
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate 	door_arg_t		darg;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	darg.data_ptr = pack_buf;
1307c478bd9Sstevel@tonic-gate 	darg.data_size = nvl_size;
1317c478bd9Sstevel@tonic-gate 	darg.desc_ptr = NULL;
1327c478bd9Sstevel@tonic-gate 	darg.desc_num = 0;
1337c478bd9Sstevel@tonic-gate 	darg.rbuf = NULL;
1347c478bd9Sstevel@tonic-gate 	darg.rsize = 0;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	if (door_fd < 0 || door_call(door_fd, &darg) < 0) {
1377c478bd9Sstevel@tonic-gate 		if (door_fd >= 0) {
1387c478bd9Sstevel@tonic-gate 			if (errno != EBADF) {
1397c478bd9Sstevel@tonic-gate 				return;
1407c478bd9Sstevel@tonic-gate 			}
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 			/*
1437c478bd9Sstevel@tonic-gate 			 * It's not a valid door file descriptor.
1447c478bd9Sstevel@tonic-gate 			 * Close and reopen the door and try again
1457c478bd9Sstevel@tonic-gate 			 * as "picld" may have restarted.
1467c478bd9Sstevel@tonic-gate 			 */
1477c478bd9Sstevel@tonic-gate 			(void) close(door_fd);
1487c478bd9Sstevel@tonic-gate 		}
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 		door_fd = open(PICLEVENT_DOOR, O_RDONLY);
1517c478bd9Sstevel@tonic-gate 		if (piclslm_debug)
1527c478bd9Sstevel@tonic-gate 			syslog(LOG_INFO,
1537c478bd9Sstevel@tonic-gate 			    "picl_slm: opened door %s door_fd: %d\n",
1547c478bd9Sstevel@tonic-gate 			    PICLEVENT_DOOR, door_fd);
1557c478bd9Sstevel@tonic-gate 		if (door_fd < 0 || door_call(door_fd, &darg) < 0) {
1567c478bd9Sstevel@tonic-gate 			return;
1577c478bd9Sstevel@tonic-gate 		}
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 	if (piclslm_debug)
1607c478bd9Sstevel@tonic-gate 		syslog(LOG_INFO,
1617c478bd9Sstevel@tonic-gate 		    "picl_slm: sent sysevent door:%d pack_buf:%p size:0x%x\n",
1627c478bd9Sstevel@tonic-gate 		    door_fd, pack_buf, nvl_size);
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1667c478bd9Sstevel@tonic-gate static void *
piclslm_deliver_thr(void * args)1677c478bd9Sstevel@tonic-gate piclslm_deliver_thr(void *args)
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate 	nvlist_queue_t		*nvqp;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	for (;;) {
1727c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&nvq_lock);
1737c478bd9Sstevel@tonic-gate 		while (nvq_head == NULL && cleanup == 0) {
1747c478bd9Sstevel@tonic-gate 			(void) cond_wait(&nvq_cv, &nvq_lock);
1757c478bd9Sstevel@tonic-gate 		}
1767c478bd9Sstevel@tonic-gate 		nvqp = remove_from_queue();
1777c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&nvq_lock);
1787c478bd9Sstevel@tonic-gate 		while (nvqp) {
1797c478bd9Sstevel@tonic-gate 			post_piclevent(nvqp->nvq_item, nvqp->nvq_sz);
1807c478bd9Sstevel@tonic-gate 			free_nvqueue(nvqp);
1817c478bd9Sstevel@tonic-gate 			(void) mutex_lock(&nvq_lock);
1827c478bd9Sstevel@tonic-gate 			nvqp = remove_from_queue();
1837c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&nvq_lock);
1847c478bd9Sstevel@tonic-gate 		}
1857c478bd9Sstevel@tonic-gate 		if (cleanup)
1867c478bd9Sstevel@tonic-gate 			return (NULL);
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate /*
1927c478bd9Sstevel@tonic-gate  * returns 0 if arguments successfully added to nvl, EINVAL if arguments missing
1937c478bd9Sstevel@tonic-gate  * from ev and EAGAIN if nvlist_add_string() fails
1947c478bd9Sstevel@tonic-gate  */
1957c478bd9Sstevel@tonic-gate static int
piclslm_add_ec_devfs_args(nvlist_t * nvl,sysevent_t * ev)1967c478bd9Sstevel@tonic-gate piclslm_add_ec_devfs_args(nvlist_t *nvl, sysevent_t *ev)
1977c478bd9Sstevel@tonic-gate {
1987c478bd9Sstevel@tonic-gate 	sysevent_value_t se_val;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	if (sysevent_lookup_attr(ev, DEVFS_PATHNAME, SE_DATA_TYPE_STRING,
2017c478bd9Sstevel@tonic-gate 	    &se_val) != 0 || se_val.value.sv_string == NULL) {
2027c478bd9Sstevel@tonic-gate 		return (EINVAL);
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate 	if (nvlist_add_string(nvl, PICLEVENTARG_DEVFS_PATH,
2057c478bd9Sstevel@tonic-gate 	    se_val.value.sv_string)) {
2067c478bd9Sstevel@tonic-gate 		return (EAGAIN);
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 	return (0);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate /*
2127c478bd9Sstevel@tonic-gate  * returns 0 if arguments successfully added to nvl, EINVAL if arguments missing
2137c478bd9Sstevel@tonic-gate  * from ev and EAGAIN if nvlist_add_string() fails
2147c478bd9Sstevel@tonic-gate  */
2157c478bd9Sstevel@tonic-gate static int
piclslm_add_ec_dr_args(nvlist_t * nvl,sysevent_t * ev)2167c478bd9Sstevel@tonic-gate piclslm_add_ec_dr_args(nvlist_t *nvl, sysevent_t *ev)
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate 	sysevent_value_t se_val;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	if (sysevent_lookup_attr(ev, DR_AP_ID, SE_DATA_TYPE_STRING,
2217c478bd9Sstevel@tonic-gate 	    &se_val) != 0 || se_val.value.sv_string == NULL) {
2227c478bd9Sstevel@tonic-gate 		return (EINVAL);
2237c478bd9Sstevel@tonic-gate 	}
2247c478bd9Sstevel@tonic-gate 	if (nvlist_add_string(nvl, PICLEVENTARG_AP_ID,
2257c478bd9Sstevel@tonic-gate 	    se_val.value.sv_string)) {
2267c478bd9Sstevel@tonic-gate 		return (EAGAIN);
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 	if (sysevent_lookup_attr(ev, DR_HINT, SE_DATA_TYPE_STRING,
2297c478bd9Sstevel@tonic-gate 	    &se_val) != 0 || se_val.value.sv_string == NULL) {
2307c478bd9Sstevel@tonic-gate 		if (nvlist_add_string(nvl, PICLEVENTARG_HINT, ""))
2317c478bd9Sstevel@tonic-gate 			return (EAGAIN);
2327c478bd9Sstevel@tonic-gate 	} else {
2337c478bd9Sstevel@tonic-gate 		if (nvlist_add_string(nvl, PICLEVENTARG_HINT,
2347c478bd9Sstevel@tonic-gate 		    se_val.value.sv_string))
2357c478bd9Sstevel@tonic-gate 			return (EAGAIN);
2367c478bd9Sstevel@tonic-gate 	}
2377c478bd9Sstevel@tonic-gate 	return (0);
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate /*
2417c478bd9Sstevel@tonic-gate  * returns 0 if arguments successfully added to nvl, EINVAL if arguments missing
2427c478bd9Sstevel@tonic-gate  * from ev and EAGAIN if nvlist_add_string() fails
2437c478bd9Sstevel@tonic-gate  */
2447c478bd9Sstevel@tonic-gate static int
piclslm_add_ec_dr_req_args(nvlist_t * nvl,sysevent_t * ev)2457c478bd9Sstevel@tonic-gate piclslm_add_ec_dr_req_args(nvlist_t *nvl, sysevent_t *ev)
2467c478bd9Sstevel@tonic-gate {
2477c478bd9Sstevel@tonic-gate 	nvlist_t *nvlist = NULL;
2487c478bd9Sstevel@tonic-gate 	char *ap_id = NULL;
2497c478bd9Sstevel@tonic-gate 	char *dr_req = NULL;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	if (sysevent_get_attr_list(ev, &nvlist)) {
2527c478bd9Sstevel@tonic-gate 		return (EAGAIN);
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	if (nvlist_lookup_string(nvlist, DR_AP_ID, &ap_id) != 0 ||
2567c478bd9Sstevel@tonic-gate 	    ap_id == NULL) {
2577c478bd9Sstevel@tonic-gate 		nvlist_free(nvlist);
2587c478bd9Sstevel@tonic-gate 		return (EINVAL);
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	if (nvlist_add_string(nvl, PICLEVENTARG_AP_ID, ap_id)) {
2627c478bd9Sstevel@tonic-gate 		nvlist_free(nvlist);
2637c478bd9Sstevel@tonic-gate 		return (EAGAIN);
2647c478bd9Sstevel@tonic-gate 	}
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	dr_req = NULL;
2677c478bd9Sstevel@tonic-gate 	if (nvlist_lookup_string(nvlist, DR_REQ_TYPE, &dr_req) != 0)
2687c478bd9Sstevel@tonic-gate 		dr_req = "";
2697c478bd9Sstevel@tonic-gate 
270*e9610e3eSToomas Soome 	if (nvlist_add_string(nvl, PICLEVENTARG_DR_REQ_TYPE, dr_req)) {
2717c478bd9Sstevel@tonic-gate 		nvlist_free(nvlist);
2727c478bd9Sstevel@tonic-gate 		return (EAGAIN);
2737c478bd9Sstevel@tonic-gate 	}
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	if (piclslm_debug)
2767c478bd9Sstevel@tonic-gate 		syslog(LOG_DEBUG, "piclevent: dr_req_type = %s on %s\n",
2777c478bd9Sstevel@tonic-gate 		    (dr_req ? dr_req : "Investigate"), ap_id);
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	nvlist_free(nvlist);
2807c478bd9Sstevel@tonic-gate 	return (0);
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate /*
2847c478bd9Sstevel@tonic-gate  * piclslm_deliver_event - called by syseventd to deliver an event buffer.
2857c478bd9Sstevel@tonic-gate  *			The event buffer is subsequently delivered to
2867c478bd9Sstevel@tonic-gate  *			picld.  If picld, is not responding to the
2877c478bd9Sstevel@tonic-gate  *			delivery attempt, we will ignore it.
2887c478bd9Sstevel@tonic-gate  */
2897c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2907c478bd9Sstevel@tonic-gate static int
piclslm_deliver_event(sysevent_t * ev,int flag)2917c478bd9Sstevel@tonic-gate piclslm_deliver_event(sysevent_t *ev, int flag)
2927c478bd9Sstevel@tonic-gate {
2937c478bd9Sstevel@tonic-gate 	sysevent_t	*dupev;
2947c478bd9Sstevel@tonic-gate 	nvlist_t	*nvl;
2957c478bd9Sstevel@tonic-gate 	char		*ec;
2967c478bd9Sstevel@tonic-gate 	char		*esc;
2977c478bd9Sstevel@tonic-gate 	char		*ename;
2987c478bd9Sstevel@tonic-gate 	int		retval;
2997c478bd9Sstevel@tonic-gate 	char		*pack_buf;
3007c478bd9Sstevel@tonic-gate 	size_t		nvl_size;
3017c478bd9Sstevel@tonic-gate 	int		rval;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	/*
3047c478bd9Sstevel@tonic-gate 	 * Filter out uninteresting events
3057c478bd9Sstevel@tonic-gate 	 */
3067c478bd9Sstevel@tonic-gate 	ec = sysevent_get_class_name(ev);
3077c478bd9Sstevel@tonic-gate 	esc = sysevent_get_subclass_name(ev);
3087c478bd9Sstevel@tonic-gate 	if (piclslm_debug)
3097c478bd9Sstevel@tonic-gate 		syslog(LOG_INFO,
3107c478bd9Sstevel@tonic-gate 		    "picl_slm: got sysevent  ev:%p class:%s subclass:%s\n",
3117c478bd9Sstevel@tonic-gate 		    ev, (ec) ? ec : "NULL", (esc) ? esc : "NULL");
3127c478bd9Sstevel@tonic-gate 	if ((ec == NULL) || (esc == NULL)) {
3137c478bd9Sstevel@tonic-gate 		return (0);
3147c478bd9Sstevel@tonic-gate 	} else if (strcmp(ec, EC_DEVFS) == 0) {
3157c478bd9Sstevel@tonic-gate 		if (strcmp(esc, ESC_DEVFS_DEVI_ADD) == 0)
3167c478bd9Sstevel@tonic-gate 			ename = strdup(PICLEVENT_SYSEVENT_DEVICE_ADDED);
3177c478bd9Sstevel@tonic-gate 		else if (strcmp(esc, ESC_DEVFS_DEVI_REMOVE) == 0)
3187c478bd9Sstevel@tonic-gate 			ename = strdup(PICLEVENT_SYSEVENT_DEVICE_REMOVED);
3197c478bd9Sstevel@tonic-gate 		else
3207c478bd9Sstevel@tonic-gate 			return (0);
3217c478bd9Sstevel@tonic-gate 	} else if (strcmp(ec, EC_DR) == 0) {
3227c478bd9Sstevel@tonic-gate 		if (strcmp(esc, ESC_DR_AP_STATE_CHANGE) == 0)
3237c478bd9Sstevel@tonic-gate 			ename = strdup(PICLEVENT_DR_AP_STATE_CHANGE);
3247c478bd9Sstevel@tonic-gate 		else if (strcmp(esc, ESC_DR_REQ) == 0)
3257c478bd9Sstevel@tonic-gate 			ename = strdup(PICLEVENT_DR_REQ);
3267c478bd9Sstevel@tonic-gate 		else
3277c478bd9Sstevel@tonic-gate 			return (0);
3287c478bd9Sstevel@tonic-gate 	} else {
3297c478bd9Sstevel@tonic-gate 		return (0);
3307c478bd9Sstevel@tonic-gate 	}
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	if (ename == NULL)
3337c478bd9Sstevel@tonic-gate 		return (EAGAIN);
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	/*
3367c478bd9Sstevel@tonic-gate 	 * Make a copy to expand attribute list
3377c478bd9Sstevel@tonic-gate 	 */
3387c478bd9Sstevel@tonic-gate 	dupev = sysevent_dup(ev);
3397c478bd9Sstevel@tonic-gate 	if (dupev == NULL) {
3407c478bd9Sstevel@tonic-gate 		free(ename);
3417c478bd9Sstevel@tonic-gate 		return (EAGAIN);
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 
344*e9610e3eSToomas Soome 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME_TYPE, 0)) {
3457c478bd9Sstevel@tonic-gate 		free(ename);
3467c478bd9Sstevel@tonic-gate 		sysevent_free(dupev);
3477c478bd9Sstevel@tonic-gate 		return (EAGAIN);
3487c478bd9Sstevel@tonic-gate 	}
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	if (strcmp(ec, EC_DEVFS) == 0) {
3517c478bd9Sstevel@tonic-gate 		rval = piclslm_add_ec_devfs_args(nvl, dupev);
3527c478bd9Sstevel@tonic-gate 	} else if (strcmp(ec, EC_DR) == 0) {
3537c478bd9Sstevel@tonic-gate 		if (strcmp(esc, ESC_DR_REQ) == 0) {
3547c478bd9Sstevel@tonic-gate 			rval = piclslm_add_ec_dr_req_args(nvl, dupev);
3557c478bd9Sstevel@tonic-gate 		} else {
3567c478bd9Sstevel@tonic-gate 			rval = piclslm_add_ec_dr_args(nvl, dupev);
3577c478bd9Sstevel@tonic-gate 		}
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	if (rval != 0) {
3617c478bd9Sstevel@tonic-gate 		free(ename);
3627c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
3637c478bd9Sstevel@tonic-gate 		sysevent_free(dupev);
3647c478bd9Sstevel@tonic-gate 		return ((rval == EAGAIN) ? EAGAIN : 0);
3657c478bd9Sstevel@tonic-gate 	}
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	pack_buf = NULL;
3687c478bd9Sstevel@tonic-gate 	if (nvlist_add_string(nvl, PICLEVENTARG_EVENT_NAME, ename) ||
3697c478bd9Sstevel@tonic-gate 	    nvlist_add_string(nvl, PICLEVENTARG_DATA_TYPE,
3707c478bd9Sstevel@tonic-gate 	    PICLEVENTARG_PICLEVENT_DATA) ||
371*e9610e3eSToomas Soome 	    nvlist_pack(nvl, &pack_buf, &nvl_size, NV_ENCODE_NATIVE, 0)) {
3727c478bd9Sstevel@tonic-gate 		free(ename);
3737c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
3747c478bd9Sstevel@tonic-gate 		sysevent_free(dupev);
3757c478bd9Sstevel@tonic-gate 		return (EAGAIN);
3767c478bd9Sstevel@tonic-gate 	}
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	/*
3797c478bd9Sstevel@tonic-gate 	 * Add nvlist_t to queue
3807c478bd9Sstevel@tonic-gate 	 */
3817c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&nvq_lock);
3827c478bd9Sstevel@tonic-gate 	retval = add_to_queue(pack_buf, nvl_size);
3837c478bd9Sstevel@tonic-gate 	(void) cond_signal(&nvq_cv);
3847c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&nvq_lock);
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	nvlist_free(nvl);
3877c478bd9Sstevel@tonic-gate 	sysevent_free(dupev);
3887c478bd9Sstevel@tonic-gate 	free(ename);
3897c478bd9Sstevel@tonic-gate 	return (retval < 0 ? EAGAIN : 0);
3907c478bd9Sstevel@tonic-gate }
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate struct slm_mod_ops *
slm_init(void)3937c478bd9Sstevel@tonic-gate slm_init(void)
3947c478bd9Sstevel@tonic-gate {
3957c478bd9Sstevel@tonic-gate 	cleanup = 0;
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	init_queue();
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	(void) mutex_init(&nvq_lock, USYNC_THREAD, NULL);
4007c478bd9Sstevel@tonic-gate 	(void) cond_init(&nvq_cv, USYNC_THREAD, NULL);
4017c478bd9Sstevel@tonic-gate 
402*e9610e3eSToomas Soome 	if (thr_create(NULL, 0, piclslm_deliver_thr,
4037c478bd9Sstevel@tonic-gate 	    NULL, THR_BOUND, &piclslm_deliver_thr_id) != 0) {
4047c478bd9Sstevel@tonic-gate 		(void) mutex_destroy(&nvq_lock);
4057c478bd9Sstevel@tonic-gate 		(void) cond_destroy(&nvq_cv);
4067c478bd9Sstevel@tonic-gate 		return (NULL);
4077c478bd9Sstevel@tonic-gate 	}
4087c478bd9Sstevel@tonic-gate 	return (&piclslm_mod_ops);
4097c478bd9Sstevel@tonic-gate }
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate void
slm_fini(void)4127c478bd9Sstevel@tonic-gate slm_fini(void)
4137c478bd9Sstevel@tonic-gate {
4147c478bd9Sstevel@tonic-gate 	/*
4157c478bd9Sstevel@tonic-gate 	 * Wait for all events to be sent
4167c478bd9Sstevel@tonic-gate 	 */
4177c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&nvq_lock);
4187c478bd9Sstevel@tonic-gate 	cleanup = 1;
4197c478bd9Sstevel@tonic-gate 	(void) cond_signal(&nvq_cv);
4207c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&nvq_lock);
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	/* Wait for delivery thread to exit */
4237c478bd9Sstevel@tonic-gate 	(void) thr_join(piclslm_deliver_thr_id, NULL, NULL);
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	(void) mutex_destroy(&nvq_lock);
4267c478bd9Sstevel@tonic-gate 	(void) cond_destroy(&nvq_cv);
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	if (door_fd >= 0)
4297c478bd9Sstevel@tonic-gate 		(void) close(door_fd);
4307c478bd9Sstevel@tonic-gate 	door_fd = -1;
4317c478bd9Sstevel@tonic-gate }
432