1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate #include <unistd.h>
30*0Sstevel@tonic-gate #include <stdlib.h>
31*0Sstevel@tonic-gate #include <errno.h>
32*0Sstevel@tonic-gate #include <string.h>
33*0Sstevel@tonic-gate #include <door.h>
34*0Sstevel@tonic-gate #include <fcntl.h>
35*0Sstevel@tonic-gate #include <syslog.h>
36*0Sstevel@tonic-gate #include <sys/sunddi.h>
37*0Sstevel@tonic-gate #include <libsysevent.h>
38*0Sstevel@tonic-gate #include <picl.h>
39*0Sstevel@tonic-gate #include <pthread.h>
40*0Sstevel@tonic-gate #include "piclevent.h"
41*0Sstevel@tonic-gate #include <sys/sysevent/eventdefs.h>
42*0Sstevel@tonic-gate #include <sys/sysevent/dr.h>
43*0Sstevel@tonic-gate
44*0Sstevel@tonic-gate #define PICLSLM_DOOR_FAILED gettext("PICL SLM door create failed\n")
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gate /*
47*0Sstevel@tonic-gate * syseventd event handler
48*0Sstevel@tonic-gate */
49*0Sstevel@tonic-gate static int piclslm_debug = 0;
50*0Sstevel@tonic-gate static int piclslm_deliver_event(sysevent_t *ev, int flag);
51*0Sstevel@tonic-gate static int door_fd = -1;
52*0Sstevel@tonic-gate
53*0Sstevel@tonic-gate typedef struct nvlist_queue {
54*0Sstevel@tonic-gate char *nvq_item; /* packed nvlist */
55*0Sstevel@tonic-gate size_t nvq_sz; /* buf size */
56*0Sstevel@tonic-gate struct nvlist_queue *nvq_next;
57*0Sstevel@tonic-gate } nvlist_queue_t;
58*0Sstevel@tonic-gate
59*0Sstevel@tonic-gate static nvlist_queue_t *nvq_head;
60*0Sstevel@tonic-gate static nvlist_queue_t *nvq_tail;
61*0Sstevel@tonic-gate
62*0Sstevel@tonic-gate static mutex_t nvq_lock;
63*0Sstevel@tonic-gate static cond_t nvq_cv;
64*0Sstevel@tonic-gate static thread_t piclslm_deliver_thr_id;
65*0Sstevel@tonic-gate static int cleanup;
66*0Sstevel@tonic-gate
67*0Sstevel@tonic-gate static struct slm_mod_ops piclslm_mod_ops = {
68*0Sstevel@tonic-gate SE_MAJOR_VERSION, SE_MINOR_VERSION, SE_MAX_RETRY_LIMIT,
69*0Sstevel@tonic-gate piclslm_deliver_event};
70*0Sstevel@tonic-gate
71*0Sstevel@tonic-gate
72*0Sstevel@tonic-gate static void
init_queue(void)73*0Sstevel@tonic-gate init_queue(void)
74*0Sstevel@tonic-gate {
75*0Sstevel@tonic-gate nvq_head = NULL;
76*0Sstevel@tonic-gate nvq_tail = NULL;
77*0Sstevel@tonic-gate }
78*0Sstevel@tonic-gate
79*0Sstevel@tonic-gate static int
add_to_queue(char * nvl,size_t sz)80*0Sstevel@tonic-gate add_to_queue(char *nvl, size_t sz)
81*0Sstevel@tonic-gate {
82*0Sstevel@tonic-gate nvlist_queue_t *new_nvq;
83*0Sstevel@tonic-gate
84*0Sstevel@tonic-gate new_nvq = malloc(sizeof (*new_nvq));
85*0Sstevel@tonic-gate if (new_nvq == NULL)
86*0Sstevel@tonic-gate return (-1);
87*0Sstevel@tonic-gate
88*0Sstevel@tonic-gate new_nvq->nvq_item = nvl;
89*0Sstevel@tonic-gate new_nvq->nvq_sz = sz;
90*0Sstevel@tonic-gate new_nvq->nvq_next = NULL;
91*0Sstevel@tonic-gate
92*0Sstevel@tonic-gate if (nvq_head == NULL)
93*0Sstevel@tonic-gate nvq_head = new_nvq;
94*0Sstevel@tonic-gate else
95*0Sstevel@tonic-gate nvq_tail->nvq_next = new_nvq;
96*0Sstevel@tonic-gate nvq_tail = new_nvq;
97*0Sstevel@tonic-gate
98*0Sstevel@tonic-gate return (0);
99*0Sstevel@tonic-gate }
100*0Sstevel@tonic-gate
101*0Sstevel@tonic-gate static nvlist_queue_t *
remove_from_queue(void)102*0Sstevel@tonic-gate remove_from_queue(void)
103*0Sstevel@tonic-gate {
104*0Sstevel@tonic-gate nvlist_queue_t *nvqp;
105*0Sstevel@tonic-gate
106*0Sstevel@tonic-gate if (nvq_head == NULL)
107*0Sstevel@tonic-gate return (NULL);
108*0Sstevel@tonic-gate
109*0Sstevel@tonic-gate nvqp = nvq_head;
110*0Sstevel@tonic-gate nvq_head = nvq_head->nvq_next;
111*0Sstevel@tonic-gate if (nvq_head == NULL)
112*0Sstevel@tonic-gate nvq_tail = NULL;
113*0Sstevel@tonic-gate return (nvqp);
114*0Sstevel@tonic-gate }
115*0Sstevel@tonic-gate
116*0Sstevel@tonic-gate static void
free_nvqueue(nvlist_queue_t * nvqp)117*0Sstevel@tonic-gate free_nvqueue(nvlist_queue_t *nvqp)
118*0Sstevel@tonic-gate {
119*0Sstevel@tonic-gate free(nvqp->nvq_item);
120*0Sstevel@tonic-gate free(nvqp);
121*0Sstevel@tonic-gate }
122*0Sstevel@tonic-gate
123*0Sstevel@tonic-gate /*
124*0Sstevel@tonic-gate * deliver the event to the plugin if the door exists
125*0Sstevel@tonic-gate */
126*0Sstevel@tonic-gate static void
post_piclevent(char * pack_buf,size_t nvl_size)127*0Sstevel@tonic-gate post_piclevent(char *pack_buf, size_t nvl_size)
128*0Sstevel@tonic-gate {
129*0Sstevel@tonic-gate door_arg_t darg;
130*0Sstevel@tonic-gate
131*0Sstevel@tonic-gate darg.data_ptr = pack_buf;
132*0Sstevel@tonic-gate darg.data_size = nvl_size;
133*0Sstevel@tonic-gate darg.desc_ptr = NULL;
134*0Sstevel@tonic-gate darg.desc_num = 0;
135*0Sstevel@tonic-gate darg.rbuf = NULL;
136*0Sstevel@tonic-gate darg.rsize = 0;
137*0Sstevel@tonic-gate
138*0Sstevel@tonic-gate if (door_fd < 0 || door_call(door_fd, &darg) < 0) {
139*0Sstevel@tonic-gate if (door_fd >= 0) {
140*0Sstevel@tonic-gate if (errno != EBADF) {
141*0Sstevel@tonic-gate return;
142*0Sstevel@tonic-gate }
143*0Sstevel@tonic-gate
144*0Sstevel@tonic-gate /*
145*0Sstevel@tonic-gate * It's not a valid door file descriptor.
146*0Sstevel@tonic-gate * Close and reopen the door and try again
147*0Sstevel@tonic-gate * as "picld" may have restarted.
148*0Sstevel@tonic-gate */
149*0Sstevel@tonic-gate (void) close(door_fd);
150*0Sstevel@tonic-gate }
151*0Sstevel@tonic-gate
152*0Sstevel@tonic-gate door_fd = open(PICLEVENT_DOOR, O_RDONLY);
153*0Sstevel@tonic-gate if (piclslm_debug)
154*0Sstevel@tonic-gate syslog(LOG_INFO,
155*0Sstevel@tonic-gate "picl_slm: opened door %s door_fd: %d\n",
156*0Sstevel@tonic-gate PICLEVENT_DOOR, door_fd);
157*0Sstevel@tonic-gate if (door_fd < 0 || door_call(door_fd, &darg) < 0) {
158*0Sstevel@tonic-gate return;
159*0Sstevel@tonic-gate }
160*0Sstevel@tonic-gate }
161*0Sstevel@tonic-gate if (piclslm_debug)
162*0Sstevel@tonic-gate syslog(LOG_INFO,
163*0Sstevel@tonic-gate "picl_slm: sent sysevent door:%d pack_buf:%p size:0x%x\n",
164*0Sstevel@tonic-gate door_fd, pack_buf, nvl_size);
165*0Sstevel@tonic-gate }
166*0Sstevel@tonic-gate
167*0Sstevel@tonic-gate /*ARGSUSED*/
168*0Sstevel@tonic-gate static void *
piclslm_deliver_thr(void * args)169*0Sstevel@tonic-gate piclslm_deliver_thr(void *args)
170*0Sstevel@tonic-gate {
171*0Sstevel@tonic-gate nvlist_queue_t *nvqp;
172*0Sstevel@tonic-gate
173*0Sstevel@tonic-gate for (;;) {
174*0Sstevel@tonic-gate (void) mutex_lock(&nvq_lock);
175*0Sstevel@tonic-gate while (nvq_head == NULL && cleanup == 0) {
176*0Sstevel@tonic-gate (void) cond_wait(&nvq_cv, &nvq_lock);
177*0Sstevel@tonic-gate }
178*0Sstevel@tonic-gate nvqp = remove_from_queue();
179*0Sstevel@tonic-gate (void) mutex_unlock(&nvq_lock);
180*0Sstevel@tonic-gate while (nvqp) {
181*0Sstevel@tonic-gate post_piclevent(nvqp->nvq_item, nvqp->nvq_sz);
182*0Sstevel@tonic-gate free_nvqueue(nvqp);
183*0Sstevel@tonic-gate (void) mutex_lock(&nvq_lock);
184*0Sstevel@tonic-gate nvqp = remove_from_queue();
185*0Sstevel@tonic-gate (void) mutex_unlock(&nvq_lock);
186*0Sstevel@tonic-gate }
187*0Sstevel@tonic-gate if (cleanup)
188*0Sstevel@tonic-gate return (NULL);
189*0Sstevel@tonic-gate }
190*0Sstevel@tonic-gate /*NOTREACHED*/
191*0Sstevel@tonic-gate }
192*0Sstevel@tonic-gate
193*0Sstevel@tonic-gate /*
194*0Sstevel@tonic-gate * returns 0 if arguments successfully added to nvl, EINVAL if arguments missing
195*0Sstevel@tonic-gate * from ev and EAGAIN if nvlist_add_string() fails
196*0Sstevel@tonic-gate */
197*0Sstevel@tonic-gate static int
piclslm_add_ec_devfs_args(nvlist_t * nvl,sysevent_t * ev)198*0Sstevel@tonic-gate piclslm_add_ec_devfs_args(nvlist_t *nvl, sysevent_t *ev)
199*0Sstevel@tonic-gate {
200*0Sstevel@tonic-gate sysevent_value_t se_val;
201*0Sstevel@tonic-gate
202*0Sstevel@tonic-gate if (sysevent_lookup_attr(ev, DEVFS_PATHNAME, SE_DATA_TYPE_STRING,
203*0Sstevel@tonic-gate &se_val) != 0 || se_val.value.sv_string == NULL) {
204*0Sstevel@tonic-gate return (EINVAL);
205*0Sstevel@tonic-gate }
206*0Sstevel@tonic-gate if (nvlist_add_string(nvl, PICLEVENTARG_DEVFS_PATH,
207*0Sstevel@tonic-gate se_val.value.sv_string)) {
208*0Sstevel@tonic-gate return (EAGAIN);
209*0Sstevel@tonic-gate }
210*0Sstevel@tonic-gate return (0);
211*0Sstevel@tonic-gate }
212*0Sstevel@tonic-gate
213*0Sstevel@tonic-gate /*
214*0Sstevel@tonic-gate * returns 0 if arguments successfully added to nvl, EINVAL if arguments missing
215*0Sstevel@tonic-gate * from ev and EAGAIN if nvlist_add_string() fails
216*0Sstevel@tonic-gate */
217*0Sstevel@tonic-gate static int
piclslm_add_ec_dr_args(nvlist_t * nvl,sysevent_t * ev)218*0Sstevel@tonic-gate piclslm_add_ec_dr_args(nvlist_t *nvl, sysevent_t *ev)
219*0Sstevel@tonic-gate {
220*0Sstevel@tonic-gate sysevent_value_t se_val;
221*0Sstevel@tonic-gate
222*0Sstevel@tonic-gate if (sysevent_lookup_attr(ev, DR_AP_ID, SE_DATA_TYPE_STRING,
223*0Sstevel@tonic-gate &se_val) != 0 || se_val.value.sv_string == NULL) {
224*0Sstevel@tonic-gate return (EINVAL);
225*0Sstevel@tonic-gate }
226*0Sstevel@tonic-gate if (nvlist_add_string(nvl, PICLEVENTARG_AP_ID,
227*0Sstevel@tonic-gate se_val.value.sv_string)) {
228*0Sstevel@tonic-gate return (EAGAIN);
229*0Sstevel@tonic-gate }
230*0Sstevel@tonic-gate if (sysevent_lookup_attr(ev, DR_HINT, SE_DATA_TYPE_STRING,
231*0Sstevel@tonic-gate &se_val) != 0 || se_val.value.sv_string == NULL) {
232*0Sstevel@tonic-gate if (nvlist_add_string(nvl, PICLEVENTARG_HINT, ""))
233*0Sstevel@tonic-gate return (EAGAIN);
234*0Sstevel@tonic-gate } else {
235*0Sstevel@tonic-gate if (nvlist_add_string(nvl, PICLEVENTARG_HINT,
236*0Sstevel@tonic-gate se_val.value.sv_string))
237*0Sstevel@tonic-gate return (EAGAIN);
238*0Sstevel@tonic-gate }
239*0Sstevel@tonic-gate return (0);
240*0Sstevel@tonic-gate }
241*0Sstevel@tonic-gate
242*0Sstevel@tonic-gate /*
243*0Sstevel@tonic-gate * returns 0 if arguments successfully added to nvl, EINVAL if arguments missing
244*0Sstevel@tonic-gate * from ev and EAGAIN if nvlist_add_string() fails
245*0Sstevel@tonic-gate */
246*0Sstevel@tonic-gate static int
piclslm_add_ec_dr_req_args(nvlist_t * nvl,sysevent_t * ev)247*0Sstevel@tonic-gate piclslm_add_ec_dr_req_args(nvlist_t *nvl, sysevent_t *ev)
248*0Sstevel@tonic-gate {
249*0Sstevel@tonic-gate nvlist_t *nvlist = NULL;
250*0Sstevel@tonic-gate char *ap_id = NULL;
251*0Sstevel@tonic-gate char *dr_req = NULL;
252*0Sstevel@tonic-gate
253*0Sstevel@tonic-gate if (sysevent_get_attr_list(ev, &nvlist)) {
254*0Sstevel@tonic-gate return (EAGAIN);
255*0Sstevel@tonic-gate }
256*0Sstevel@tonic-gate
257*0Sstevel@tonic-gate if (nvlist_lookup_string(nvlist, DR_AP_ID, &ap_id) != 0 ||
258*0Sstevel@tonic-gate ap_id == NULL) {
259*0Sstevel@tonic-gate nvlist_free(nvlist);
260*0Sstevel@tonic-gate return (EINVAL);
261*0Sstevel@tonic-gate }
262*0Sstevel@tonic-gate
263*0Sstevel@tonic-gate if (nvlist_add_string(nvl, PICLEVENTARG_AP_ID, ap_id)) {
264*0Sstevel@tonic-gate nvlist_free(nvlist);
265*0Sstevel@tonic-gate return (EAGAIN);
266*0Sstevel@tonic-gate }
267*0Sstevel@tonic-gate
268*0Sstevel@tonic-gate dr_req = NULL;
269*0Sstevel@tonic-gate if (nvlist_lookup_string(nvlist, DR_REQ_TYPE, &dr_req) != 0)
270*0Sstevel@tonic-gate dr_req = "";
271*0Sstevel@tonic-gate
272*0Sstevel@tonic-gate if (nvlist_add_string(nvl, PICLEVENTARG_DR_REQ_TYPE,
273*0Sstevel@tonic-gate dr_req)) {
274*0Sstevel@tonic-gate nvlist_free(nvlist);
275*0Sstevel@tonic-gate return (EAGAIN);
276*0Sstevel@tonic-gate }
277*0Sstevel@tonic-gate
278*0Sstevel@tonic-gate if (piclslm_debug)
279*0Sstevel@tonic-gate syslog(LOG_DEBUG, "piclevent: dr_req_type = %s on %s\n",
280*0Sstevel@tonic-gate (dr_req ? dr_req : "Investigate"), ap_id);
281*0Sstevel@tonic-gate
282*0Sstevel@tonic-gate nvlist_free(nvlist);
283*0Sstevel@tonic-gate return (0);
284*0Sstevel@tonic-gate }
285*0Sstevel@tonic-gate
286*0Sstevel@tonic-gate /*
287*0Sstevel@tonic-gate * piclslm_deliver_event - called by syseventd to deliver an event buffer.
288*0Sstevel@tonic-gate * The event buffer is subsequently delivered to
289*0Sstevel@tonic-gate * picld. If picld, is not responding to the
290*0Sstevel@tonic-gate * delivery attempt, we will ignore it.
291*0Sstevel@tonic-gate */
292*0Sstevel@tonic-gate /*ARGSUSED*/
293*0Sstevel@tonic-gate static int
piclslm_deliver_event(sysevent_t * ev,int flag)294*0Sstevel@tonic-gate piclslm_deliver_event(sysevent_t *ev, int flag)
295*0Sstevel@tonic-gate {
296*0Sstevel@tonic-gate sysevent_t *dupev;
297*0Sstevel@tonic-gate nvlist_t *nvl;
298*0Sstevel@tonic-gate char *ec;
299*0Sstevel@tonic-gate char *esc;
300*0Sstevel@tonic-gate char *ename;
301*0Sstevel@tonic-gate int retval;
302*0Sstevel@tonic-gate char *pack_buf;
303*0Sstevel@tonic-gate size_t nvl_size;
304*0Sstevel@tonic-gate int rval;
305*0Sstevel@tonic-gate
306*0Sstevel@tonic-gate /*
307*0Sstevel@tonic-gate * Filter out uninteresting events
308*0Sstevel@tonic-gate */
309*0Sstevel@tonic-gate ec = sysevent_get_class_name(ev);
310*0Sstevel@tonic-gate esc = sysevent_get_subclass_name(ev);
311*0Sstevel@tonic-gate if (piclslm_debug)
312*0Sstevel@tonic-gate syslog(LOG_INFO,
313*0Sstevel@tonic-gate "picl_slm: got sysevent ev:%p class:%s subclass:%s\n",
314*0Sstevel@tonic-gate ev, (ec) ? ec : "NULL", (esc) ? esc : "NULL");
315*0Sstevel@tonic-gate if ((ec == NULL) || (esc == NULL)) {
316*0Sstevel@tonic-gate return (0);
317*0Sstevel@tonic-gate } else if (strcmp(ec, EC_DEVFS) == 0) {
318*0Sstevel@tonic-gate if (strcmp(esc, ESC_DEVFS_DEVI_ADD) == 0)
319*0Sstevel@tonic-gate ename = strdup(PICLEVENT_SYSEVENT_DEVICE_ADDED);
320*0Sstevel@tonic-gate else if (strcmp(esc, ESC_DEVFS_DEVI_REMOVE) == 0)
321*0Sstevel@tonic-gate ename = strdup(PICLEVENT_SYSEVENT_DEVICE_REMOVED);
322*0Sstevel@tonic-gate else
323*0Sstevel@tonic-gate return (0);
324*0Sstevel@tonic-gate } else if (strcmp(ec, EC_DR) == 0) {
325*0Sstevel@tonic-gate if (strcmp(esc, ESC_DR_AP_STATE_CHANGE) == 0)
326*0Sstevel@tonic-gate ename = strdup(PICLEVENT_DR_AP_STATE_CHANGE);
327*0Sstevel@tonic-gate else if (strcmp(esc, ESC_DR_REQ) == 0)
328*0Sstevel@tonic-gate ename = strdup(PICLEVENT_DR_REQ);
329*0Sstevel@tonic-gate else
330*0Sstevel@tonic-gate return (0);
331*0Sstevel@tonic-gate } else {
332*0Sstevel@tonic-gate return (0);
333*0Sstevel@tonic-gate }
334*0Sstevel@tonic-gate
335*0Sstevel@tonic-gate if (ename == NULL)
336*0Sstevel@tonic-gate return (EAGAIN);
337*0Sstevel@tonic-gate
338*0Sstevel@tonic-gate /*
339*0Sstevel@tonic-gate * Make a copy to expand attribute list
340*0Sstevel@tonic-gate */
341*0Sstevel@tonic-gate dupev = sysevent_dup(ev);
342*0Sstevel@tonic-gate if (dupev == NULL) {
343*0Sstevel@tonic-gate free(ename);
344*0Sstevel@tonic-gate return (EAGAIN);
345*0Sstevel@tonic-gate }
346*0Sstevel@tonic-gate
347*0Sstevel@tonic-gate if (nvlist_alloc(&nvl, NV_UNIQUE_NAME_TYPE, NULL)) {
348*0Sstevel@tonic-gate free(ename);
349*0Sstevel@tonic-gate sysevent_free(dupev);
350*0Sstevel@tonic-gate return (EAGAIN);
351*0Sstevel@tonic-gate }
352*0Sstevel@tonic-gate
353*0Sstevel@tonic-gate if (strcmp(ec, EC_DEVFS) == 0) {
354*0Sstevel@tonic-gate rval = piclslm_add_ec_devfs_args(nvl, dupev);
355*0Sstevel@tonic-gate } else if (strcmp(ec, EC_DR) == 0) {
356*0Sstevel@tonic-gate if (strcmp(esc, ESC_DR_REQ) == 0) {
357*0Sstevel@tonic-gate rval = piclslm_add_ec_dr_req_args(nvl, dupev);
358*0Sstevel@tonic-gate } else {
359*0Sstevel@tonic-gate rval = piclslm_add_ec_dr_args(nvl, dupev);
360*0Sstevel@tonic-gate }
361*0Sstevel@tonic-gate }
362*0Sstevel@tonic-gate
363*0Sstevel@tonic-gate if (rval != 0) {
364*0Sstevel@tonic-gate free(ename);
365*0Sstevel@tonic-gate nvlist_free(nvl);
366*0Sstevel@tonic-gate sysevent_free(dupev);
367*0Sstevel@tonic-gate return ((rval == EAGAIN) ? EAGAIN : 0);
368*0Sstevel@tonic-gate }
369*0Sstevel@tonic-gate
370*0Sstevel@tonic-gate pack_buf = NULL;
371*0Sstevel@tonic-gate if (nvlist_add_string(nvl, PICLEVENTARG_EVENT_NAME, ename) ||
372*0Sstevel@tonic-gate nvlist_add_string(nvl, PICLEVENTARG_DATA_TYPE,
373*0Sstevel@tonic-gate PICLEVENTARG_PICLEVENT_DATA) ||
374*0Sstevel@tonic-gate nvlist_pack(nvl, &pack_buf, &nvl_size, NV_ENCODE_NATIVE, NULL)) {
375*0Sstevel@tonic-gate free(ename);
376*0Sstevel@tonic-gate nvlist_free(nvl);
377*0Sstevel@tonic-gate sysevent_free(dupev);
378*0Sstevel@tonic-gate return (EAGAIN);
379*0Sstevel@tonic-gate }
380*0Sstevel@tonic-gate
381*0Sstevel@tonic-gate /*
382*0Sstevel@tonic-gate * Add nvlist_t to queue
383*0Sstevel@tonic-gate */
384*0Sstevel@tonic-gate (void) mutex_lock(&nvq_lock);
385*0Sstevel@tonic-gate retval = add_to_queue(pack_buf, nvl_size);
386*0Sstevel@tonic-gate (void) cond_signal(&nvq_cv);
387*0Sstevel@tonic-gate (void) mutex_unlock(&nvq_lock);
388*0Sstevel@tonic-gate
389*0Sstevel@tonic-gate nvlist_free(nvl);
390*0Sstevel@tonic-gate sysevent_free(dupev);
391*0Sstevel@tonic-gate free(ename);
392*0Sstevel@tonic-gate return (retval < 0 ? EAGAIN : 0);
393*0Sstevel@tonic-gate }
394*0Sstevel@tonic-gate
395*0Sstevel@tonic-gate struct slm_mod_ops *
slm_init(void)396*0Sstevel@tonic-gate slm_init(void)
397*0Sstevel@tonic-gate {
398*0Sstevel@tonic-gate cleanup = 0;
399*0Sstevel@tonic-gate
400*0Sstevel@tonic-gate init_queue();
401*0Sstevel@tonic-gate
402*0Sstevel@tonic-gate (void) mutex_init(&nvq_lock, USYNC_THREAD, NULL);
403*0Sstevel@tonic-gate (void) cond_init(&nvq_cv, USYNC_THREAD, NULL);
404*0Sstevel@tonic-gate
405*0Sstevel@tonic-gate if (thr_create(NULL, NULL, piclslm_deliver_thr,
406*0Sstevel@tonic-gate NULL, THR_BOUND, &piclslm_deliver_thr_id) != 0) {
407*0Sstevel@tonic-gate (void) mutex_destroy(&nvq_lock);
408*0Sstevel@tonic-gate (void) cond_destroy(&nvq_cv);
409*0Sstevel@tonic-gate return (NULL);
410*0Sstevel@tonic-gate }
411*0Sstevel@tonic-gate return (&piclslm_mod_ops);
412*0Sstevel@tonic-gate }
413*0Sstevel@tonic-gate
414*0Sstevel@tonic-gate void
slm_fini(void)415*0Sstevel@tonic-gate slm_fini(void)
416*0Sstevel@tonic-gate {
417*0Sstevel@tonic-gate /*
418*0Sstevel@tonic-gate * Wait for all events to be sent
419*0Sstevel@tonic-gate */
420*0Sstevel@tonic-gate (void) mutex_lock(&nvq_lock);
421*0Sstevel@tonic-gate cleanup = 1;
422*0Sstevel@tonic-gate (void) cond_signal(&nvq_cv);
423*0Sstevel@tonic-gate (void) mutex_unlock(&nvq_lock);
424*0Sstevel@tonic-gate
425*0Sstevel@tonic-gate /* Wait for delivery thread to exit */
426*0Sstevel@tonic-gate (void) thr_join(piclslm_deliver_thr_id, NULL, NULL);
427*0Sstevel@tonic-gate
428*0Sstevel@tonic-gate (void) mutex_destroy(&nvq_lock);
429*0Sstevel@tonic-gate (void) cond_destroy(&nvq_cv);
430*0Sstevel@tonic-gate
431*0Sstevel@tonic-gate if (door_fd >= 0)
432*0Sstevel@tonic-gate (void) close(door_fd);
433*0Sstevel@tonic-gate door_fd = -1;
434*0Sstevel@tonic-gate }
435