xref: /titanic_44/usr/src/cmd/cmd-inet/lib/nwamd/sysevent_events.c (revision a60349c89adffc0902b2353230891d8e7f2b24d9)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <assert.h>
28 #include <errno.h>
29 #include <libsysevent.h>
30 #include <sys/sysevent/eventdefs.h>
31 #include <sys/sysevent/dev.h>
32 #include <sys/types.h>
33 #include <libnvpair.h>
34 #include <string.h>
35 #include <unistd.h>
36 
37 #include "events.h"
38 #include "ncp.h"
39 #include "ncu.h"
40 #include "objects.h"
41 #include "util.h"
42 
43 /*
44  * sysevent_events.c - this file contains routines to retrieve sysevents
45  * from the system and package them for high level processing.
46  */
47 
48 static sysevent_handle_t *sysevent_handle;
49 
50 /*
51  * At present, we only handle EC_DEV_ADD/EC_DEV_REMOVE sysevents of
52  * subclass ESC_NETWORK.  These signify hotplug addition/removal.
53  * For EC_DEV_ADD, we:
54  *      - extract the driver/instance sysevent attributes
55  *      - combine these to get interface name and create associated NCUs
56  *      at the link/IP level if required
57  *      - enable those instances
58  * For EC_DEV_REMOVE, we:
59  *      - disable the associated link/IP NCUs
60  */
61 static void
62 sysevent_handler(sysevent_t *ev)
63 {
64 	int32_t instance;
65 	char *driver;
66 	char if_name[LIFNAMSIZ];
67 	boolean_t link_added;
68 	nvlist_t *attr_list;
69 	char *event_class = sysevent_get_class_name(ev);
70 	char *event_subclass = sysevent_get_subclass_name(ev);
71 	nwamd_event_t link_event = NULL;
72 
73 	nlog(LOG_DEBUG, "sysevent_handler: event %s/%s", event_class,
74 	    event_subclass);
75 
76 	/* Make sure sysevent is of expected class/subclass */
77 	if ((strcmp(event_class, EC_DEV_ADD) != 0 &&
78 	    strcmp(event_class, EC_DEV_REMOVE) != 0) ||
79 	    strcmp(event_subclass, ESC_NETWORK) != 0) {
80 		nlog(LOG_ERR, "sysevent_handler: unexpected sysevent "
81 		    "class/subclass %s/%s", event_class, event_subclass);
82 		return;
83 	}
84 
85 	link_added = (strcmp(event_class, EC_DEV_ADD) == 0);
86 
87 	/*
88 	 * Retrieve driver name and instance attributes, and combine to
89 	 * get interface name.
90 	 */
91 	if (sysevent_get_attr_list(ev, &attr_list) != 0) {
92 		nlog(LOG_ERR, "sysevent_handler: sysevent_get_attr_list: %m");
93 		return;
94 	}
95 	if (nvlist_lookup_string(attr_list, DEV_DRIVER_NAME, &driver) != 0 ||
96 	    nvlist_lookup_int32(attr_list, DEV_INSTANCE, &instance) != 0) {
97 		nlog(LOG_ERR, "sysevent_handler: nvlist_lookup "
98 		    "of attributes failed: %m");
99 		nvlist_free(attr_list);
100 		return;
101 	}
102 	(void) snprintf(if_name, LIFNAMSIZ, "%s%d", driver, instance);
103 	nvlist_free(attr_list);
104 
105 	/* Ignore sysevent events for other zones */
106 	if (!nwamd_link_belongs_to_this_zone(if_name))
107 		return;
108 
109 	/* Create event for link */
110 	link_event = nwamd_event_init_link_action(if_name,
111 	    link_added ? NWAM_ACTION_ADD : NWAM_ACTION_REMOVE);
112 	if (link_event != NULL)
113 		nwamd_event_enqueue(link_event);
114 }
115 
116 /* ARGSUSED0 */
117 static void *
118 sysevent_initialization(void *arg)
119 {
120 	const char *subclass = ESC_NETWORK;
121 
122 	do {
123 		nwamd_escalate();
124 		sysevent_handle = sysevent_bind_handle(sysevent_handler);
125 		nwamd_deescalate();
126 
127 		(void) sleep(1);
128 	} while (sysevent_handle == NULL);
129 
130 	/*
131 	 * Subscribe to ESC_NETWORK subclass of EC_DEV_ADD and EC_DEV_REMOVE
132 	 * events.  As a result,  we get sysevent notification of hotplug
133 	 * add/remove events,  which we handle above in sysevent_handler().
134 	 */
135 	if (sysevent_subscribe_event(sysevent_handle, EC_DEV_ADD, &subclass, 1)
136 	    != 0 ||
137 	    sysevent_subscribe_event(sysevent_handle, EC_DEV_REMOVE, &subclass,
138 	    1) != 0)
139 		pfail("sysevent_subscribe_event: %s", strerror(errno));
140 
141 	return (NULL);
142 }
143 
144 /*
145  * We can't initialize in the main thread because we may need to wait until
146  * svc:/system/sysevent:default finishes starting up.  So we create a thread to
147  * initialize in.
148  */
149 void
150 nwamd_sysevent_events_init(void)
151 {
152 	int rc;
153 	pthread_attr_t attr;
154 
155 	rc = pthread_attr_init(&attr);
156 	if (rc != 0) {
157 		pfail("nwamd_sysevents_init: pthread_attr_init failed: %s",
158 		    strerror(rc));
159 	}
160 
161 	rc = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
162 	if (rc != 0) {
163 		pfail("nwamd_sysevents_init: pthread_attr_setdetachstate "
164 		    "failed: %s", strerror(rc));
165 	}
166 
167 	rc = pthread_create(NULL, &attr, sysevent_initialization, NULL);
168 	if (rc != 0) {
169 		pfail("nwamd_sysevents_init: couldn't start sysevent init "
170 		    "thread: %s", strerror(rc));
171 	}
172 
173 	(void) pthread_attr_destroy(&attr);
174 }
175 
176 void
177 nwamd_sysevent_events_fini(void)
178 {
179 	if (sysevent_handle != NULL) {
180 		nwamd_escalate();
181 		sysevent_unbind_handle(sysevent_handle);
182 		nwamd_deescalate();
183 	}
184 	sysevent_handle = NULL;
185 }
186