1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* 28 * PICL plug-in that listens to sysevent and posts picl events 29 */ 30 31 #include <stdio.h> 32 #include <string.h> 33 #include <ctype.h> 34 #include <limits.h> 35 #include <stdlib.h> 36 #include <assert.h> 37 #include <alloca.h> 38 #include <unistd.h> 39 #include <stropts.h> 40 #include <syslog.h> 41 #include <libdevinfo.h> 42 #include <sys/time.h> 43 #include <fcntl.h> 44 #include <picl.h> 45 #include <picltree.h> 46 #include <sys/types.h> 47 #include <sys/sysinfo.h> 48 #include <dirent.h> 49 #include <libintl.h> 50 #include <sys/sunddi.h> 51 #include <sys/stat.h> 52 #include <libsysevent.h> 53 #include <libnvpair.h> 54 #include "piclevent.h" 55 56 /* 57 * Plugin registration entry points 58 */ 59 static void eventplugin_register(void); 60 static void eventplugin_init(void); 61 static void eventplugin_fini(void); 62 63 #pragma init(eventplugin_register) 64 65 static picld_plugin_reg_t my_reg_info = { 66 PICLD_PLUGIN_VERSION_1, 67 PICLD_PLUGIN_CRITICAL, 68 "SUNW_piclevent plugin for sysevents", 69 eventplugin_init, 70 eventplugin_fini 71 }; 72 73 /* 74 * Log message texts 75 */ 76 #define EVT_THR_FAILED gettext("Event thread create failed!\n") 77 #define EVT_OPEN_FAILED gettext("PICL SLM door create failed\n") 78 79 static int door_id = -1; 80 #define SUNW_PICLEVENT_PLUGIN_DEBUG "SUNW_PICLEVENT_PLUGIN_DEBUG" 81 static int piclevent_debug = 0; 82 83 84 /* 85 * completion handler for the posted picl event 86 */ 87 /*ARGSUSED*/ 88 static void 89 piclevent_completion_handler(char *ename, void *earg, size_t size) 90 { 91 free(earg); 92 free(ename); 93 } 94 95 /* 96 * This function posts the incoming piclevent 97 * It packs the nvlist and posts it to PICL 98 */ 99 static void 100 parse_piclevent(nvlist_t *nvlp) 101 { 102 char *enval; 103 char *ename; 104 size_t nvl_size; 105 char *packed_nvl; 106 int err; 107 108 if (nvlist_lookup_string(nvlp, PICLEVENTARG_EVENT_NAME, &enval)) 109 return; 110 111 packed_nvl = NULL; 112 if (nvlist_pack(nvlp, &packed_nvl, &nvl_size, NV_ENCODE_NATIVE, 0)) 113 return; 114 115 ename = strdup(enval); 116 if (ename == NULL) { 117 free(packed_nvl); 118 return; 119 } 120 121 if (piclevent_debug) { 122 syslog(LOG_INFO, "piclevent: posting ename:%s packed_nvl:%p " 123 "nvl_size:0x%x\n", ename, packed_nvl, nvl_size); 124 } 125 err = ptree_post_event(ename, packed_nvl, nvl_size, 126 piclevent_completion_handler); 127 128 if (err != PICL_SUCCESS) { 129 if (piclevent_debug) 130 syslog(LOG_INFO, 131 "piclevent: posting ename:%s failed err:%d\n", 132 ename, err); 133 free(ename); 134 free(packed_nvl); 135 } 136 } 137 138 /* 139 * This is the PICL SLM door handler. It parses the event tuple received 140 * and posts an event to refresh the PICL tree. 141 */ 142 /*ARGSUSED*/ 143 static void 144 event_handler(void *cookie, char *argp, size_t asize, 145 door_desc_t *dp, uint_t n_desc) 146 { 147 door_cred_t cred; 148 nvlist_t *nvlp; 149 char *dtype; 150 151 if (piclevent_debug) 152 syslog(LOG_INFO, 153 "piclevent: got SLM event cookie:%p evarg:%p size:0x%x\n", 154 cookie, argp, asize); 155 if ((door_id < 0) || (argp == NULL) || (door_cred(&cred) < 0) || 156 (cred.dc_euid != 0)) 157 (void) door_return(argp, 0, NULL, 0); 158 159 if (nvlist_unpack(argp, asize, &nvlp, 0)) 160 (void) door_return(argp, 0, NULL, 0); 161 162 if (nvlist_lookup_string(nvlp, PICLEVENTARG_DATA_TYPE, &dtype)) { 163 nvlist_free(nvlp); 164 (void) door_return(argp, 0, NULL, 0); 165 } 166 167 if (strcmp(dtype, PICLEVENTARG_PICLEVENT_DATA) == 0) 168 parse_piclevent(nvlp); 169 /* 170 * ignore other event data types 171 */ 172 nvlist_free(nvlp); 173 (void) door_return(argp, 0, NULL, 0); 174 } 175 176 /* 177 * Create the slm to picl plugin door 178 */ 179 static int 180 setup_door(void) 181 { 182 struct stat stbuf; 183 184 /* 185 * Create the door 186 */ 187 door_id = door_create(event_handler, PICLEVENT_DOOR_COOKIE, 188 DOOR_REFUSE_DESC | DOOR_NO_CANCEL); 189 190 if (door_id < 0) 191 return (-1); 192 193 if (stat(PICLEVENT_DOOR, &stbuf) < 0) { 194 int newfd; 195 if ((newfd = creat(PICLEVENT_DOOR, 0444)) < 0) { 196 (void) door_revoke(door_id); 197 door_id = -1; 198 return (-1); 199 } 200 (void) close(newfd); 201 } 202 203 if (fattach(door_id, PICLEVENT_DOOR) < 0) { 204 if ((errno != EBUSY) || (fdetach(PICLEVENT_DOOR) < 0) || 205 (fattach(door_id, PICLEVENT_DOOR) < 0)) { 206 (void) door_revoke(door_id); 207 door_id = -1; 208 return (-1); 209 } 210 } 211 212 return (0); 213 } 214 215 216 /* 217 * This function is executed as part of .init when the plugin is 218 * dlopen()ed 219 */ 220 static void 221 eventplugin_register(void) 222 { 223 if (getenv(SUNW_PICLEVENT_PLUGIN_DEBUG)) 224 piclevent_debug = 1; 225 (void) picld_plugin_register(&my_reg_info); 226 } 227 228 /* 229 * This function is the init entry point of the plugin. 230 * It creates the slm to picl plugin door. 231 */ 232 static void 233 eventplugin_init(void) 234 { 235 if (setup_door() < 0) { 236 syslog(LOG_ERR, EVT_OPEN_FAILED); 237 } 238 } 239 240 /* 241 * This function is the fini entry point of the plugin 242 */ 243 static void 244 eventplugin_fini(void) 245 { 246 if (door_id >= 0) { 247 (void) door_revoke(door_id); 248 door_id = -1; 249 } 250 } 251