5304 syseventd: datalink notify thread hangs up when terminated
[unleashed.git] / usr / src / cmd / syseventd / modules / datalink_mod / datalink_mod.c
blob3982a5b583e81ce90d634b55876c6769b37acc30
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 * Copyright 2016 Nexenta Systems, Inc.
29 * datalink syseventd module.
31 * The purpose of this module is to identify all datalink related events,
32 * and react accordingly.
35 #include <errno.h>
36 #include <sys/sysevent/eventdefs.h>
37 #include <string.h>
38 #include <libnvpair.h>
39 #include <librcm.h>
40 #include <libsysevent.h>
41 #include "sysevent_signal.h"
43 extern void syseventd_err_print(char *, ...);
45 struct event_list {
46 nvlist_t *ev;
47 struct event_list *next;
50 static rcm_handle_t *rcm_hdl = NULL;
51 static boolean_t dl_exiting;
52 static thread_t dl_notify_tid;
53 static mutex_t dl_mx;
54 static cond_t dl_cv;
55 static struct event_list *dl_events;
57 /* ARGSUSED */
58 static void *
59 datalink_notify_thread(void *arg)
61 struct event_list *tmp_events, *ep;
63 (void) mutex_lock(&dl_mx);
65 while (! dl_exiting || dl_events != NULL) {
66 if (dl_events == NULL) {
67 (void) cond_wait(&dl_cv, &dl_mx);
68 continue;
71 tmp_events = dl_events;
72 dl_events = NULL;
74 (void) mutex_unlock(&dl_mx);
76 while (tmp_events != NULL) {
77 struct sigaction cbuf, dfl;
80 * Ignore SIGCLD for the
81 * duration of the rcm_notify_event call.
83 (void) memset(&dfl, 0, sizeof (dfl));
84 dfl.sa_handler = SIG_IGN;
85 (void) sigaction(SIGCHLD, &dfl, &cbuf);
88 * Send the PHYSLINK_NEW event to network_rcm to update
89 * the network devices cache accordingly.
91 if (rcm_notify_event(rcm_hdl, RCM_RESOURCE_PHYSLINK_NEW,
92 0, tmp_events->ev, NULL) != RCM_SUCCESS)
93 syseventd_err_print("datalink_mod: Can not "
94 "notify event: %s\n", strerror(errno));
96 (void) sigaction(SIGCHLD, &cbuf, NULL);
97 ep = tmp_events;
98 tmp_events = tmp_events->next;
99 nvlist_free(ep->ev);
100 free(ep);
103 (void) mutex_lock(&dl_mx);
106 (void) mutex_unlock(&dl_mx);
108 return (NULL);
111 /*ARGSUSED*/
112 static int
113 datalink_deliver_event(sysevent_t *ev, int unused)
115 const char *class = sysevent_get_class_name(ev);
116 const char *subclass = sysevent_get_subclass_name(ev);
117 nvlist_t *nvl;
118 struct event_list *newp, **elpp;
120 if (strcmp(class, EC_DATALINK) != 0 ||
121 strcmp(subclass, ESC_DATALINK_PHYS_ADD) != 0) {
122 return (0);
125 if (sysevent_get_attr_list(ev, &nvl) != 0)
126 return (EINVAL);
129 * rcm_notify_event() needs to be called asynchronously otherwise when
130 * sysevent queue is full, deadlock will happen.
132 if ((newp = malloc(sizeof (struct event_list))) == NULL)
133 return (ENOMEM);
135 newp->ev = nvl;
136 newp->next = NULL;
139 * queue up at the end of the event list and signal notify_thread to
140 * process it.
142 (void) mutex_lock(&dl_mx);
143 elpp = &dl_events;
144 while (*elpp != NULL)
145 elpp = &(*elpp)->next;
146 *elpp = newp;
147 (void) cond_signal(&dl_cv);
148 (void) mutex_unlock(&dl_mx);
150 return (0);
153 static struct slm_mod_ops datalink_mod_ops = {
154 SE_MAJOR_VERSION,
155 SE_MINOR_VERSION,
156 SE_MAX_RETRY_LIMIT,
157 datalink_deliver_event
160 struct slm_mod_ops *
161 slm_init()
163 dl_events = NULL;
164 dl_exiting = B_FALSE;
166 if (rcm_alloc_handle(NULL, 0, NULL, &rcm_hdl) != RCM_SUCCESS)
167 return (NULL);
169 (void) mutex_init(&dl_mx, USYNC_THREAD, NULL);
170 (void) cond_init(&dl_cv, USYNC_THREAD, NULL);
172 if (thr_create(NULL, 0, datalink_notify_thread, NULL, 0,
173 &dl_notify_tid) != 0) {
174 (void) rcm_free_handle(rcm_hdl);
175 (void) mutex_destroy(&dl_mx);
176 (void) cond_destroy(&dl_cv);
177 return (NULL);
180 return (&datalink_mod_ops);
183 void
184 slm_fini()
186 (void) mutex_lock(&dl_mx);
187 dl_exiting = B_TRUE;
188 (void) cond_signal(&dl_cv);
189 (void) mutex_unlock(&dl_mx);
190 (void) thr_join(dl_notify_tid, NULL, NULL);
192 (void) mutex_destroy(&dl_mx);
193 (void) cond_destroy(&dl_cv);
194 (void) rcm_free_handle(rcm_hdl);
195 rcm_hdl = NULL;