dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / picl / plugins / common / piclevent / piclevent.c
blob1185248e6d1ec4e66ad80d42a6a63b5c77f88bf1
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.
28 * PICL plug-in that listens to sysevent and posts picl events
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"
57 * Plugin registration entry points
59 static void eventplugin_register(void);
60 static void eventplugin_init(void);
61 static void eventplugin_fini(void);
63 #pragma init(eventplugin_register)
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
74 * Log message texts
76 #define EVT_THR_FAILED gettext("Event thread create failed!\n")
77 #define EVT_OPEN_FAILED gettext("PICL SLM door create failed\n")
79 static int door_id = -1;
80 #define SUNW_PICLEVENT_PLUGIN_DEBUG "SUNW_PICLEVENT_PLUGIN_DEBUG"
81 static int piclevent_debug = 0;
85 * completion handler for the posted picl event
87 /*ARGSUSED*/
88 static void
89 piclevent_completion_handler(char *ename, void *earg, size_t size)
91 free(earg);
92 free(ename);
96 * This function posts the incoming piclevent
97 * It packs the nvlist and posts it to PICL
99 static void
100 parse_piclevent(nvlist_t *nvlp)
102 char *enval;
103 char *ename;
104 size_t nvl_size;
105 char *packed_nvl;
106 int err;
108 if (nvlist_lookup_string(nvlp, PICLEVENTARG_EVENT_NAME, &enval))
109 return;
111 packed_nvl = NULL;
112 if (nvlist_pack(nvlp, &packed_nvl, &nvl_size, NV_ENCODE_NATIVE, 0))
113 return;
115 ename = strdup(enval);
116 if (ename == NULL) {
117 free(packed_nvl);
118 return;
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);
125 err = ptree_post_event(ename, packed_nvl, nvl_size,
126 piclevent_completion_handler);
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);
139 * This is the PICL SLM door handler. It parses the event tuple received
140 * and posts an event to refresh the PICL tree.
142 /*ARGSUSED*/
143 static void
144 event_handler(void *cookie, char *argp, size_t asize,
145 door_desc_t *dp, uint_t n_desc)
147 door_cred_t cred;
148 nvlist_t *nvlp;
149 char *dtype;
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);
159 if (nvlist_unpack(argp, asize, &nvlp, 0))
160 (void) door_return(argp, 0, NULL, 0);
162 if (nvlist_lookup_string(nvlp, PICLEVENTARG_DATA_TYPE, &dtype)) {
163 nvlist_free(nvlp);
164 (void) door_return(argp, 0, NULL, 0);
167 if (strcmp(dtype, PICLEVENTARG_PICLEVENT_DATA) == 0)
168 parse_piclevent(nvlp);
170 * ignore other event data types
172 nvlist_free(nvlp);
173 (void) door_return(argp, 0, NULL, 0);
177 * Create the slm to picl plugin door
179 static int
180 setup_door(void)
182 struct stat stbuf;
185 * Create the door
187 door_id = door_create(event_handler, PICLEVENT_DOOR_COOKIE,
188 DOOR_REFUSE_DESC | DOOR_NO_CANCEL);
190 if (door_id < 0)
191 return (-1);
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);
200 (void) close(newfd);
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);
212 return (0);
217 * This function is executed as part of .init when the plugin is
218 * dlopen()ed
220 static void
221 eventplugin_register(void)
223 if (getenv(SUNW_PICLEVENT_PLUGIN_DEBUG))
224 piclevent_debug = 1;
225 (void) picld_plugin_register(&my_reg_info);
229 * This function is the init entry point of the plugin.
230 * It creates the slm to picl plugin door.
232 static void
233 eventplugin_init(void)
235 if (setup_door() < 0) {
236 syslog(LOG_ERR, EVT_OPEN_FAILED);
241 * This function is the fini entry point of the plugin
243 static void
244 eventplugin_fini(void)
246 if (door_id >= 0) {
247 (void) door_revoke(door_id);
248 door_id = -1;