8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / picl / plugins / common / piclevent / piclevent.c
blob8d1804e34004191d9f61ed2acd1a1a544f5b9b6f
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * PICL plug-in that listens to sysevent and posts picl events
33 #include <stdio.h>
34 #include <string.h>
35 #include <ctype.h>
36 #include <limits.h>
37 #include <stdlib.h>
38 #include <assert.h>
39 #include <alloca.h>
40 #include <unistd.h>
41 #include <stropts.h>
42 #include <syslog.h>
43 #include <libdevinfo.h>
44 #include <sys/time.h>
45 #include <fcntl.h>
46 #include <picl.h>
47 #include <picltree.h>
48 #include <sys/types.h>
49 #include <sys/sysinfo.h>
50 #include <dirent.h>
51 #include <libintl.h>
52 #include <sys/sunddi.h>
53 #include <sys/stat.h>
54 #include <libsysevent.h>
55 #include <libnvpair.h>
56 #include "piclevent.h"
59 * Plugin registration entry points
61 static void eventplugin_register(void);
62 static void eventplugin_init(void);
63 static void eventplugin_fini(void);
65 #pragma init(eventplugin_register)
67 static picld_plugin_reg_t my_reg_info = {
68 PICLD_PLUGIN_VERSION_1,
69 PICLD_PLUGIN_CRITICAL,
70 "SUNW_piclevent plugin for sysevents",
71 eventplugin_init,
72 eventplugin_fini
76 * Log message texts
78 #define EVT_THR_FAILED gettext("Event thread create failed!\n")
79 #define EVT_OPEN_FAILED gettext("PICL SLM door create failed\n")
81 static int door_id = -1;
82 #define SUNW_PICLEVENT_PLUGIN_DEBUG "SUNW_PICLEVENT_PLUGIN_DEBUG"
83 static int piclevent_debug = 0;
87 * completion handler for the posted picl event
89 /*ARGSUSED*/
90 static void
91 piclevent_completion_handler(char *ename, void *earg, size_t size)
93 free(earg);
94 free(ename);
98 * This function posts the incoming piclevent
99 * It packs the nvlist and posts it to PICL
101 static void
102 parse_piclevent(nvlist_t *nvlp)
104 char *enval;
105 char *ename;
106 size_t nvl_size;
107 char *packed_nvl;
108 int err;
110 if (nvlist_lookup_string(nvlp, PICLEVENTARG_EVENT_NAME, &enval))
111 return;
113 packed_nvl = NULL;
114 if (nvlist_pack(nvlp, &packed_nvl, &nvl_size, NV_ENCODE_NATIVE, NULL))
115 return;
117 ename = strdup(enval);
118 if (ename == NULL) {
119 free(packed_nvl);
120 return;
123 if (piclevent_debug) {
124 syslog(LOG_INFO, "piclevent: posting ename:%s packed_nvl:%p "
125 "nvl_size:0x%x\n", ename, packed_nvl, nvl_size);
127 err = ptree_post_event(ename, packed_nvl, nvl_size,
128 piclevent_completion_handler);
130 if (err != PICL_SUCCESS) {
131 if (piclevent_debug)
132 syslog(LOG_INFO,
133 "piclevent: posting ename:%s failed err:%d\n",
134 ename, err);
135 free(ename);
136 free(packed_nvl);
141 * This is the PICL SLM door handler. It parses the event tuple received
142 * and posts an event to refresh the PICL tree.
144 /*ARGSUSED*/
145 static void
146 event_handler(void *cookie, char *argp, size_t asize,
147 door_desc_t *dp, uint_t n_desc)
149 door_cred_t cred;
150 nvlist_t *nvlp;
151 char *dtype;
153 if (piclevent_debug)
154 syslog(LOG_INFO,
155 "piclevent: got SLM event cookie:%p evarg:%p size:0x%x\n",
156 cookie, argp, asize);
157 if ((door_id < 0) || (argp == NULL) || (door_cred(&cred) < 0) ||
158 (cred.dc_euid != 0))
159 (void) door_return(argp, 0, NULL, 0);
161 if (nvlist_unpack(argp, asize, &nvlp, NULL))
162 (void) door_return(argp, 0, NULL, 0);
164 if (nvlist_lookup_string(nvlp, PICLEVENTARG_DATA_TYPE, &dtype)) {
165 nvlist_free(nvlp);
166 (void) door_return(argp, 0, NULL, 0);
169 if (strcmp(dtype, PICLEVENTARG_PICLEVENT_DATA) == 0)
170 parse_piclevent(nvlp);
172 * ignore other event data types
174 nvlist_free(nvlp);
175 (void) door_return(argp, 0, NULL, 0);
179 * Create the slm to picl plugin door
181 static int
182 setup_door(void)
184 struct stat stbuf;
187 * Create the door
189 door_id = door_create(event_handler, PICLEVENT_DOOR_COOKIE,
190 DOOR_REFUSE_DESC | DOOR_NO_CANCEL);
192 if (door_id < 0)
193 return (-1);
195 if (stat(PICLEVENT_DOOR, &stbuf) < 0) {
196 int newfd;
197 if ((newfd = creat(PICLEVENT_DOOR, 0444)) < 0) {
198 (void) door_revoke(door_id);
199 door_id = -1;
200 return (-1);
202 (void) close(newfd);
205 if (fattach(door_id, PICLEVENT_DOOR) < 0) {
206 if ((errno != EBUSY) || (fdetach(PICLEVENT_DOOR) < 0) ||
207 (fattach(door_id, PICLEVENT_DOOR) < 0)) {
208 (void) door_revoke(door_id);
209 door_id = -1;
210 return (-1);
214 return (0);
219 * This function is executed as part of .init when the plugin is
220 * dlopen()ed
222 static void
223 eventplugin_register(void)
225 if (getenv(SUNW_PICLEVENT_PLUGIN_DEBUG))
226 piclevent_debug = 1;
227 (void) picld_plugin_register(&my_reg_info);
231 * This function is the init entry point of the plugin.
232 * It creates the slm to picl plugin door.
234 static void
235 eventplugin_init(void)
237 if (setup_door() < 0) {
238 syslog(LOG_ERR, EVT_OPEN_FAILED);
243 * This function is the fini entry point of the plugin
245 static void
246 eventplugin_fini(void)
248 if (door_id >= 0) {
249 (void) door_revoke(door_id);
250 door_id = -1;