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