dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / picl / plugins / common / piclevent / picl_slm.c
blobf6bbea770d52cbcf6e4326ad228c8e56e107bf96
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 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <door.h>
33 #include <fcntl.h>
34 #include <syslog.h>
35 #include <sys/sunddi.h>
36 #include <libsysevent.h>
37 #include <picl.h>
38 #include <pthread.h>
39 #include "piclevent.h"
40 #include <sys/sysevent/eventdefs.h>
41 #include <sys/sysevent/dr.h>
43 #define PICLSLM_DOOR_FAILED gettext("PICL SLM door create failed\n")
46 * syseventd event handler
48 static int piclslm_debug = 0;
49 static int piclslm_deliver_event(sysevent_t *ev, int flag);
50 static int door_fd = -1;
52 typedef struct nvlist_queue {
53 char *nvq_item; /* packed nvlist */
54 size_t nvq_sz; /* buf size */
55 struct nvlist_queue *nvq_next;
56 } nvlist_queue_t;
58 static nvlist_queue_t *nvq_head;
59 static nvlist_queue_t *nvq_tail;
61 static mutex_t nvq_lock;
62 static cond_t nvq_cv;
63 static thread_t piclslm_deliver_thr_id;
64 static int cleanup;
66 static struct slm_mod_ops piclslm_mod_ops = {
67 SE_MAJOR_VERSION, SE_MINOR_VERSION, SE_MAX_RETRY_LIMIT,
68 piclslm_deliver_event};
71 static void
72 init_queue(void)
74 nvq_head = NULL;
75 nvq_tail = NULL;
78 static int
79 add_to_queue(char *nvl, size_t sz)
81 nvlist_queue_t *new_nvq;
83 new_nvq = malloc(sizeof (*new_nvq));
84 if (new_nvq == NULL)
85 return (-1);
87 new_nvq->nvq_item = nvl;
88 new_nvq->nvq_sz = sz;
89 new_nvq->nvq_next = NULL;
91 if (nvq_head == NULL)
92 nvq_head = new_nvq;
93 else
94 nvq_tail->nvq_next = new_nvq;
95 nvq_tail = new_nvq;
97 return (0);
100 static nvlist_queue_t *
101 remove_from_queue(void)
103 nvlist_queue_t *nvqp;
105 if (nvq_head == NULL)
106 return (NULL);
108 nvqp = nvq_head;
109 nvq_head = nvq_head->nvq_next;
110 if (nvq_head == NULL)
111 nvq_tail = NULL;
112 return (nvqp);
115 static void
116 free_nvqueue(nvlist_queue_t *nvqp)
118 free(nvqp->nvq_item);
119 free(nvqp);
123 * deliver the event to the plugin if the door exists
125 static void
126 post_piclevent(char *pack_buf, size_t nvl_size)
128 door_arg_t darg;
130 darg.data_ptr = pack_buf;
131 darg.data_size = nvl_size;
132 darg.desc_ptr = NULL;
133 darg.desc_num = 0;
134 darg.rbuf = NULL;
135 darg.rsize = 0;
137 if (door_fd < 0 || door_call(door_fd, &darg) < 0) {
138 if (door_fd >= 0) {
139 if (errno != EBADF) {
140 return;
144 * It's not a valid door file descriptor.
145 * Close and reopen the door and try again
146 * as "picld" may have restarted.
148 (void) close(door_fd);
151 door_fd = open(PICLEVENT_DOOR, O_RDONLY);
152 if (piclslm_debug)
153 syslog(LOG_INFO,
154 "picl_slm: opened door %s door_fd: %d\n",
155 PICLEVENT_DOOR, door_fd);
156 if (door_fd < 0 || door_call(door_fd, &darg) < 0) {
157 return;
160 if (piclslm_debug)
161 syslog(LOG_INFO,
162 "picl_slm: sent sysevent door:%d pack_buf:%p size:0x%x\n",
163 door_fd, pack_buf, nvl_size);
166 /*ARGSUSED*/
167 static void *
168 piclslm_deliver_thr(void *args)
170 nvlist_queue_t *nvqp;
172 for (;;) {
173 (void) mutex_lock(&nvq_lock);
174 while (nvq_head == NULL && cleanup == 0) {
175 (void) cond_wait(&nvq_cv, &nvq_lock);
177 nvqp = remove_from_queue();
178 (void) mutex_unlock(&nvq_lock);
179 while (nvqp) {
180 post_piclevent(nvqp->nvq_item, nvqp->nvq_sz);
181 free_nvqueue(nvqp);
182 (void) mutex_lock(&nvq_lock);
183 nvqp = remove_from_queue();
184 (void) mutex_unlock(&nvq_lock);
186 if (cleanup)
187 return (NULL);
189 /*NOTREACHED*/
193 * returns 0 if arguments successfully added to nvl, EINVAL if arguments missing
194 * from ev and EAGAIN if nvlist_add_string() fails
196 static int
197 piclslm_add_ec_devfs_args(nvlist_t *nvl, sysevent_t *ev)
199 sysevent_value_t se_val;
201 if (sysevent_lookup_attr(ev, DEVFS_PATHNAME, SE_DATA_TYPE_STRING,
202 &se_val) != 0 || se_val.value.sv_string == NULL) {
203 return (EINVAL);
205 if (nvlist_add_string(nvl, PICLEVENTARG_DEVFS_PATH,
206 se_val.value.sv_string)) {
207 return (EAGAIN);
209 return (0);
213 * returns 0 if arguments successfully added to nvl, EINVAL if arguments missing
214 * from ev and EAGAIN if nvlist_add_string() fails
216 static int
217 piclslm_add_ec_dr_args(nvlist_t *nvl, sysevent_t *ev)
219 sysevent_value_t se_val;
221 if (sysevent_lookup_attr(ev, DR_AP_ID, SE_DATA_TYPE_STRING,
222 &se_val) != 0 || se_val.value.sv_string == NULL) {
223 return (EINVAL);
225 if (nvlist_add_string(nvl, PICLEVENTARG_AP_ID,
226 se_val.value.sv_string)) {
227 return (EAGAIN);
229 if (sysevent_lookup_attr(ev, DR_HINT, SE_DATA_TYPE_STRING,
230 &se_val) != 0 || se_val.value.sv_string == NULL) {
231 if (nvlist_add_string(nvl, PICLEVENTARG_HINT, ""))
232 return (EAGAIN);
233 } else {
234 if (nvlist_add_string(nvl, PICLEVENTARG_HINT,
235 se_val.value.sv_string))
236 return (EAGAIN);
238 return (0);
242 * returns 0 if arguments successfully added to nvl, EINVAL if arguments missing
243 * from ev and EAGAIN if nvlist_add_string() fails
245 static int
246 piclslm_add_ec_dr_req_args(nvlist_t *nvl, sysevent_t *ev)
248 nvlist_t *nvlist = NULL;
249 char *ap_id = NULL;
250 char *dr_req = NULL;
252 if (sysevent_get_attr_list(ev, &nvlist)) {
253 return (EAGAIN);
256 if (nvlist_lookup_string(nvlist, DR_AP_ID, &ap_id) != 0 ||
257 ap_id == NULL) {
258 nvlist_free(nvlist);
259 return (EINVAL);
262 if (nvlist_add_string(nvl, PICLEVENTARG_AP_ID, ap_id)) {
263 nvlist_free(nvlist);
264 return (EAGAIN);
267 dr_req = NULL;
268 if (nvlist_lookup_string(nvlist, DR_REQ_TYPE, &dr_req) != 0)
269 dr_req = "";
271 if (nvlist_add_string(nvl, PICLEVENTARG_DR_REQ_TYPE,
272 dr_req)) {
273 nvlist_free(nvlist);
274 return (EAGAIN);
277 if (piclslm_debug)
278 syslog(LOG_DEBUG, "piclevent: dr_req_type = %s on %s\n",
279 (dr_req ? dr_req : "Investigate"), ap_id);
281 nvlist_free(nvlist);
282 return (0);
286 * piclslm_deliver_event - called by syseventd to deliver an event buffer.
287 * The event buffer is subsequently delivered to
288 * picld. If picld, is not responding to the
289 * delivery attempt, we will ignore it.
291 /*ARGSUSED*/
292 static int
293 piclslm_deliver_event(sysevent_t *ev, int flag)
295 sysevent_t *dupev;
296 nvlist_t *nvl;
297 char *ec;
298 char *esc;
299 char *ename;
300 int retval;
301 char *pack_buf;
302 size_t nvl_size;
303 int rval;
306 * Filter out uninteresting events
308 ec = sysevent_get_class_name(ev);
309 esc = sysevent_get_subclass_name(ev);
310 if (piclslm_debug)
311 syslog(LOG_INFO,
312 "picl_slm: got sysevent ev:%p class:%s subclass:%s\n",
313 ev, (ec) ? ec : "NULL", (esc) ? esc : "NULL");
314 if ((ec == NULL) || (esc == NULL)) {
315 return (0);
316 } else if (strcmp(ec, EC_DEVFS) == 0) {
317 if (strcmp(esc, ESC_DEVFS_DEVI_ADD) == 0)
318 ename = strdup(PICLEVENT_SYSEVENT_DEVICE_ADDED);
319 else if (strcmp(esc, ESC_DEVFS_DEVI_REMOVE) == 0)
320 ename = strdup(PICLEVENT_SYSEVENT_DEVICE_REMOVED);
321 else
322 return (0);
323 } else if (strcmp(ec, EC_DR) == 0) {
324 if (strcmp(esc, ESC_DR_AP_STATE_CHANGE) == 0)
325 ename = strdup(PICLEVENT_DR_AP_STATE_CHANGE);
326 else if (strcmp(esc, ESC_DR_REQ) == 0)
327 ename = strdup(PICLEVENT_DR_REQ);
328 else
329 return (0);
330 } else {
331 return (0);
334 if (ename == NULL)
335 return (EAGAIN);
338 * Make a copy to expand attribute list
340 dupev = sysevent_dup(ev);
341 if (dupev == NULL) {
342 free(ename);
343 return (EAGAIN);
346 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME_TYPE, 0)) {
347 free(ename);
348 sysevent_free(dupev);
349 return (EAGAIN);
352 if (strcmp(ec, EC_DEVFS) == 0) {
353 rval = piclslm_add_ec_devfs_args(nvl, dupev);
354 } else if (strcmp(ec, EC_DR) == 0) {
355 if (strcmp(esc, ESC_DR_REQ) == 0) {
356 rval = piclslm_add_ec_dr_req_args(nvl, dupev);
357 } else {
358 rval = piclslm_add_ec_dr_args(nvl, dupev);
362 if (rval != 0) {
363 free(ename);
364 nvlist_free(nvl);
365 sysevent_free(dupev);
366 return ((rval == EAGAIN) ? EAGAIN : 0);
369 pack_buf = NULL;
370 if (nvlist_add_string(nvl, PICLEVENTARG_EVENT_NAME, ename) ||
371 nvlist_add_string(nvl, PICLEVENTARG_DATA_TYPE,
372 PICLEVENTARG_PICLEVENT_DATA) ||
373 nvlist_pack(nvl, &pack_buf, &nvl_size, NV_ENCODE_NATIVE, 0)) {
374 free(ename);
375 nvlist_free(nvl);
376 sysevent_free(dupev);
377 return (EAGAIN);
381 * Add nvlist_t to queue
383 (void) mutex_lock(&nvq_lock);
384 retval = add_to_queue(pack_buf, nvl_size);
385 (void) cond_signal(&nvq_cv);
386 (void) mutex_unlock(&nvq_lock);
388 nvlist_free(nvl);
389 sysevent_free(dupev);
390 free(ename);
391 return (retval < 0 ? EAGAIN : 0);
394 struct slm_mod_ops *
395 slm_init(void)
397 cleanup = 0;
399 init_queue();
401 (void) mutex_init(&nvq_lock, USYNC_THREAD, NULL);
402 (void) cond_init(&nvq_cv, USYNC_THREAD, NULL);
404 if (thr_create(NULL, 0, piclslm_deliver_thr,
405 NULL, THR_BOUND, &piclslm_deliver_thr_id) != 0) {
406 (void) mutex_destroy(&nvq_lock);
407 (void) cond_destroy(&nvq_cv);
408 return (NULL);
410 return (&piclslm_mod_ops);
413 void
414 slm_fini(void)
417 * Wait for all events to be sent
419 (void) mutex_lock(&nvq_lock);
420 cleanup = 1;
421 (void) cond_signal(&nvq_cv);
422 (void) mutex_unlock(&nvq_lock);
424 /* Wait for delivery thread to exit */
425 (void) thr_join(piclslm_deliver_thr_id, NULL, NULL);
427 (void) mutex_destroy(&nvq_lock);
428 (void) cond_destroy(&nvq_cv);
430 if (door_fd >= 0)
431 (void) close(door_fd);
432 door_fd = -1;