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 /*
23e73e5762Skd  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * PICL plug-in that listens to sysevent and posts picl events
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <ctype.h>
347c478bd9Sstevel@tonic-gate #include <limits.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
367c478bd9Sstevel@tonic-gate #include <assert.h>
377c478bd9Sstevel@tonic-gate #include <alloca.h>
387c478bd9Sstevel@tonic-gate #include <unistd.h>
397c478bd9Sstevel@tonic-gate #include <stropts.h>
407c478bd9Sstevel@tonic-gate #include <syslog.h>
417c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
427c478bd9Sstevel@tonic-gate #include <sys/time.h>
437c478bd9Sstevel@tonic-gate #include <fcntl.h>
447c478bd9Sstevel@tonic-gate #include <picl.h>
457c478bd9Sstevel@tonic-gate #include <picltree.h>
467c478bd9Sstevel@tonic-gate #include <sys/types.h>
477c478bd9Sstevel@tonic-gate #include <sys/sysinfo.h>
487c478bd9Sstevel@tonic-gate #include <dirent.h>
497c478bd9Sstevel@tonic-gate #include <libintl.h>
507c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
517c478bd9Sstevel@tonic-gate #include <sys/stat.h>
527c478bd9Sstevel@tonic-gate #include <libsysevent.h>
537c478bd9Sstevel@tonic-gate #include <libnvpair.h>
547c478bd9Sstevel@tonic-gate #include "piclevent.h"
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate  * Plugin registration entry points
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate static void	eventplugin_register(void);
607c478bd9Sstevel@tonic-gate static void	eventplugin_init(void);
617c478bd9Sstevel@tonic-gate static void	eventplugin_fini(void);
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #pragma	init(eventplugin_register)
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate static picld_plugin_reg_t  my_reg_info = {
667c478bd9Sstevel@tonic-gate 	PICLD_PLUGIN_VERSION_1,
677c478bd9Sstevel@tonic-gate 	PICLD_PLUGIN_CRITICAL,
687c478bd9Sstevel@tonic-gate 	"SUNW_piclevent plugin for sysevents",
697c478bd9Sstevel@tonic-gate 	eventplugin_init,
707c478bd9Sstevel@tonic-gate 	eventplugin_fini
717c478bd9Sstevel@tonic-gate };
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * Log message texts
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate #define	EVT_THR_FAILED		gettext("Event thread create failed!\n")
777c478bd9Sstevel@tonic-gate #define	EVT_OPEN_FAILED		gettext("PICL SLM door create failed\n")
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate static	int		door_id = -1;
807c478bd9Sstevel@tonic-gate #define	SUNW_PICLEVENT_PLUGIN_DEBUG	"SUNW_PICLEVENT_PLUGIN_DEBUG"
817c478bd9Sstevel@tonic-gate static	int		piclevent_debug = 0;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate  * completion handler for the posted picl event
867c478bd9Sstevel@tonic-gate  */
877c478bd9Sstevel@tonic-gate /*ARGSUSED*/
887c478bd9Sstevel@tonic-gate static void
piclevent_completion_handler(char * ename,void * earg,size_t size)897c478bd9Sstevel@tonic-gate piclevent_completion_handler(char *ename, void *earg, size_t size)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	free(earg);
927c478bd9Sstevel@tonic-gate 	free(ename);
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * This function posts the incoming piclevent
977c478bd9Sstevel@tonic-gate  * It packs the nvlist and posts it to PICL
987c478bd9Sstevel@tonic-gate  */
997c478bd9Sstevel@tonic-gate static void
parse_piclevent(nvlist_t * nvlp)1007c478bd9Sstevel@tonic-gate parse_piclevent(nvlist_t *nvlp)
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate 	char		*enval;
1037c478bd9Sstevel@tonic-gate 	char		*ename;
1047c478bd9Sstevel@tonic-gate 	size_t		nvl_size;
1057c478bd9Sstevel@tonic-gate 	char		*packed_nvl;
1067c478bd9Sstevel@tonic-gate 	int		err;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	if (nvlist_lookup_string(nvlp, PICLEVENTARG_EVENT_NAME, &enval))
1097c478bd9Sstevel@tonic-gate 		return;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	packed_nvl = NULL;
112*e9610e3eSToomas Soome 	if (nvlist_pack(nvlp, &packed_nvl, &nvl_size, NV_ENCODE_NATIVE, 0))
1137c478bd9Sstevel@tonic-gate 		return;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	ename = strdup(enval);
1167c478bd9Sstevel@tonic-gate 	if (ename == NULL) {
1177c478bd9Sstevel@tonic-gate 		free(packed_nvl);
1187c478bd9Sstevel@tonic-gate 		return;
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	if (piclevent_debug) {
1227c478bd9Sstevel@tonic-gate 		syslog(LOG_INFO, "piclevent: posting ename:%s packed_nvl:%p "
1237c478bd9Sstevel@tonic-gate 		    "nvl_size:0x%x\n", ename, packed_nvl, nvl_size);
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 	err = ptree_post_event(ename, packed_nvl, nvl_size,
1267c478bd9Sstevel@tonic-gate 	    piclevent_completion_handler);
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS) {
1297c478bd9Sstevel@tonic-gate 		if (piclevent_debug)
1307c478bd9Sstevel@tonic-gate 			syslog(LOG_INFO,
1317c478bd9Sstevel@tonic-gate 			    "piclevent: posting ename:%s failed err:%d\n",
1327c478bd9Sstevel@tonic-gate 			    ename, err);
1337c478bd9Sstevel@tonic-gate 		free(ename);
1347c478bd9Sstevel@tonic-gate 		free(packed_nvl);
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate  * This is the PICL SLM door handler. It parses the event tuple received
1407c478bd9Sstevel@tonic-gate  * and posts an event to refresh the PICL tree.
1417c478bd9Sstevel@tonic-gate  */
1427c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1437c478bd9Sstevel@tonic-gate static void
event_handler(void * cookie,char * argp,size_t asize,door_desc_t * dp,uint_t n_desc)1447c478bd9Sstevel@tonic-gate event_handler(void *cookie, char *argp, size_t asize,
1457c478bd9Sstevel@tonic-gate     door_desc_t *dp, uint_t n_desc)
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate 	door_cred_t		cred;
1487c478bd9Sstevel@tonic-gate 	nvlist_t		*nvlp;
1497c478bd9Sstevel@tonic-gate 	char			*dtype;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	if (piclevent_debug)
1527c478bd9Sstevel@tonic-gate 		syslog(LOG_INFO,
1537c478bd9Sstevel@tonic-gate 		    "piclevent: got SLM event cookie:%p evarg:%p size:0x%x\n",
1547c478bd9Sstevel@tonic-gate 		    cookie, argp, asize);
1557c478bd9Sstevel@tonic-gate 	if ((door_id < 0) || (argp == NULL) || (door_cred(&cred) < 0) ||
1567c478bd9Sstevel@tonic-gate 	    (cred.dc_euid != 0))
1577c478bd9Sstevel@tonic-gate 		(void) door_return(argp, 0, NULL, 0);
1587c478bd9Sstevel@tonic-gate 
159*e9610e3eSToomas Soome 	if (nvlist_unpack(argp, asize, &nvlp, 0))
1607c478bd9Sstevel@tonic-gate 		(void) door_return(argp, 0, NULL, 0);
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	if (nvlist_lookup_string(nvlp, PICLEVENTARG_DATA_TYPE, &dtype)) {
1637c478bd9Sstevel@tonic-gate 		nvlist_free(nvlp);
1647c478bd9Sstevel@tonic-gate 		(void) door_return(argp, 0, NULL, 0);
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	if (strcmp(dtype, PICLEVENTARG_PICLEVENT_DATA) == 0)
1687c478bd9Sstevel@tonic-gate 		parse_piclevent(nvlp);
1697c478bd9Sstevel@tonic-gate 	/*
1707c478bd9Sstevel@tonic-gate 	 * ignore other event data types
1717c478bd9Sstevel@tonic-gate 	 */
1727c478bd9Sstevel@tonic-gate 	nvlist_free(nvlp);
1737c478bd9Sstevel@tonic-gate 	(void) door_return(argp, 0, NULL, 0);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate  * Create the slm to picl plugin door
1787c478bd9Sstevel@tonic-gate  */
1797c478bd9Sstevel@tonic-gate static int
setup_door(void)1807c478bd9Sstevel@tonic-gate setup_door(void)
1817c478bd9Sstevel@tonic-gate {
1827c478bd9Sstevel@tonic-gate 	struct stat	stbuf;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	/*
1857c478bd9Sstevel@tonic-gate 	 * Create the door
1867c478bd9Sstevel@tonic-gate 	 */
1877c478bd9Sstevel@tonic-gate 	door_id = door_create(event_handler, PICLEVENT_DOOR_COOKIE,
188e73e5762Skd 	    DOOR_REFUSE_DESC | DOOR_NO_CANCEL);
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	if (door_id < 0)
1917c478bd9Sstevel@tonic-gate 		return (-1);
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	if (stat(PICLEVENT_DOOR, &stbuf) < 0) {
1947c478bd9Sstevel@tonic-gate 		int newfd;
1957c478bd9Sstevel@tonic-gate 		if ((newfd = creat(PICLEVENT_DOOR, 0444)) < 0) {
1967c478bd9Sstevel@tonic-gate 			(void) door_revoke(door_id);
1977c478bd9Sstevel@tonic-gate 			door_id = -1;
1987c478bd9Sstevel@tonic-gate 			return (-1);
1997c478bd9Sstevel@tonic-gate 		}
2007c478bd9Sstevel@tonic-gate 		(void) close(newfd);
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	if (fattach(door_id, PICLEVENT_DOOR) < 0) {
2047c478bd9Sstevel@tonic-gate 		if ((errno != EBUSY) || (fdetach(PICLEVENT_DOOR) < 0) ||
2057c478bd9Sstevel@tonic-gate 		    (fattach(door_id, PICLEVENT_DOOR) < 0)) {
2067c478bd9Sstevel@tonic-gate 			(void) door_revoke(door_id);
2077c478bd9Sstevel@tonic-gate 			door_id = -1;
2087c478bd9Sstevel@tonic-gate 			return (-1);
2097c478bd9Sstevel@tonic-gate 		}
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	return (0);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate /*
2177c478bd9Sstevel@tonic-gate  * This function is executed as part of .init when the plugin is
2187c478bd9Sstevel@tonic-gate  * dlopen()ed
2197c478bd9Sstevel@tonic-gate  */
2207c478bd9Sstevel@tonic-gate static void
eventplugin_register(void)2217c478bd9Sstevel@tonic-gate eventplugin_register(void)
2227c478bd9Sstevel@tonic-gate {
2237c478bd9Sstevel@tonic-gate 	if (getenv(SUNW_PICLEVENT_PLUGIN_DEBUG))
2247c478bd9Sstevel@tonic-gate 		piclevent_debug = 1;
2257c478bd9Sstevel@tonic-gate 	(void) picld_plugin_register(&my_reg_info);
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate /*
2297c478bd9Sstevel@tonic-gate  * This function is the init entry point of the plugin.
2307c478bd9Sstevel@tonic-gate  * It creates the slm to picl plugin door.
2317c478bd9Sstevel@tonic-gate  */
2327c478bd9Sstevel@tonic-gate static void
eventplugin_init(void)2337c478bd9Sstevel@tonic-gate eventplugin_init(void)
2347c478bd9Sstevel@tonic-gate {
2357c478bd9Sstevel@tonic-gate 	if (setup_door() < 0) {
2367c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, EVT_OPEN_FAILED);
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate /*
2417c478bd9Sstevel@tonic-gate  * This function is the fini entry point of the plugin
2427c478bd9Sstevel@tonic-gate  */
2437c478bd9Sstevel@tonic-gate static void
eventplugin_fini(void)2447c478bd9Sstevel@tonic-gate eventplugin_fini(void)
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate 	if (door_id >= 0) {
2477c478bd9Sstevel@tonic-gate 		(void) door_revoke(door_id);
2487c478bd9Sstevel@tonic-gate 		door_id = -1;
2497c478bd9Sstevel@tonic-gate 	}
2507c478bd9Sstevel@tonic-gate }
251