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
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]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
35 #include <sys/sunddi.h>
36 #include <libsysevent.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
;
58 static nvlist_queue_t
*nvq_head
;
59 static nvlist_queue_t
*nvq_tail
;
61 static mutex_t nvq_lock
;
63 static thread_t piclslm_deliver_thr_id
;
66 static struct slm_mod_ops piclslm_mod_ops
= {
67 SE_MAJOR_VERSION
, SE_MINOR_VERSION
, SE_MAX_RETRY_LIMIT
,
68 piclslm_deliver_event
};
79 add_to_queue(char *nvl
, size_t sz
)
81 nvlist_queue_t
*new_nvq
;
83 new_nvq
= malloc(sizeof (*new_nvq
));
87 new_nvq
->nvq_item
= nvl
;
89 new_nvq
->nvq_next
= NULL
;
94 nvq_tail
->nvq_next
= new_nvq
;
100 static nvlist_queue_t
*
101 remove_from_queue(void)
103 nvlist_queue_t
*nvqp
;
105 if (nvq_head
== NULL
)
109 nvq_head
= nvq_head
->nvq_next
;
110 if (nvq_head
== NULL
)
116 free_nvqueue(nvlist_queue_t
*nvqp
)
118 free(nvqp
->nvq_item
);
123 * deliver the event to the plugin if the door exists
126 post_piclevent(char *pack_buf
, size_t nvl_size
)
130 darg
.data_ptr
= pack_buf
;
131 darg
.data_size
= nvl_size
;
132 darg
.desc_ptr
= NULL
;
137 if (door_fd
< 0 || door_call(door_fd
, &darg
) < 0) {
139 if (errno
!= EBADF
) {
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
);
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) {
162 "picl_slm: sent sysevent door:%d pack_buf:%p size:0x%x\n",
163 door_fd
, pack_buf
, nvl_size
);
168 piclslm_deliver_thr(void *args
)
170 nvlist_queue_t
*nvqp
;
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
);
180 post_piclevent(nvqp
->nvq_item
, nvqp
->nvq_sz
);
182 (void) mutex_lock(&nvq_lock
);
183 nvqp
= remove_from_queue();
184 (void) mutex_unlock(&nvq_lock
);
193 * returns 0 if arguments successfully added to nvl, EINVAL if arguments missing
194 * from ev and EAGAIN if nvlist_add_string() fails
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
) {
205 if (nvlist_add_string(nvl
, PICLEVENTARG_DEVFS_PATH
,
206 se_val
.value
.sv_string
)) {
213 * returns 0 if arguments successfully added to nvl, EINVAL if arguments missing
214 * from ev and EAGAIN if nvlist_add_string() fails
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
) {
225 if (nvlist_add_string(nvl
, PICLEVENTARG_AP_ID
,
226 se_val
.value
.sv_string
)) {
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
, ""))
234 if (nvlist_add_string(nvl
, PICLEVENTARG_HINT
,
235 se_val
.value
.sv_string
))
242 * returns 0 if arguments successfully added to nvl, EINVAL if arguments missing
243 * from ev and EAGAIN if nvlist_add_string() fails
246 piclslm_add_ec_dr_req_args(nvlist_t
*nvl
, sysevent_t
*ev
)
248 nvlist_t
*nvlist
= NULL
;
252 if (sysevent_get_attr_list(ev
, &nvlist
)) {
256 if (nvlist_lookup_string(nvlist
, DR_AP_ID
, &ap_id
) != 0 ||
262 if (nvlist_add_string(nvl
, PICLEVENTARG_AP_ID
, ap_id
)) {
268 if (nvlist_lookup_string(nvlist
, DR_REQ_TYPE
, &dr_req
) != 0)
271 if (nvlist_add_string(nvl
, PICLEVENTARG_DR_REQ_TYPE
,
278 syslog(LOG_DEBUG
, "piclevent: dr_req_type = %s on %s\n",
279 (dr_req
? dr_req
: "Investigate"), ap_id
);
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.
293 piclslm_deliver_event(sysevent_t
*ev
, int flag
)
306 * Filter out uninteresting events
308 ec
= sysevent_get_class_name(ev
);
309 esc
= sysevent_get_subclass_name(ev
);
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
)) {
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
);
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
);
338 * Make a copy to expand attribute list
340 dupev
= sysevent_dup(ev
);
346 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME_TYPE
, 0)) {
348 sysevent_free(dupev
);
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
);
358 rval
= piclslm_add_ec_dr_args(nvl
, dupev
);
365 sysevent_free(dupev
);
366 return ((rval
== EAGAIN
) ? EAGAIN
: 0);
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)) {
376 sysevent_free(dupev
);
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
);
389 sysevent_free(dupev
);
391 return (retval
< 0 ? EAGAIN
: 0);
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
);
410 return (&piclslm_mod_ops
);
417 * Wait for all events to be sent
419 (void) mutex_lock(&nvq_lock
);
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
);
431 (void) close(door_fd
);