6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * CDDL HEADER START
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * The contents of this file are subject to the terms of the
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * Common Development and Distribution License (the "License").
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * You may not use this file except in compliance with the License.
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * or http://www.opensolaris.org/os/licensing.
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * See the License for the specific language governing permissions
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * and limitations under the License.
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * When distributing Covered Code, include this CDDL HEADER in each
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * If applicable, add the following below this CDDL HEADER, with the
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * fields enclosed by brackets "[]" replaced with your own identifying
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * information: Portions Copyright [yyyy] [name of copyright owner]
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * CDDL HEADER END
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * Use is subject to license terms.
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * sysevent_events.c - this file contains routines to retrieve sysevents
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * from the system and package them for high level processing.
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * At present, we only handle EC_DEV_ADD/EC_DEV_REMOVE sysevents of
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * subclass ESC_NETWORK. These signify hotplug addition/removal.
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * For EC_DEV_ADD, we:
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * - extract the driver/instance sysevent attributes
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * - combine these to get interface name and create associated NCUs
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * at the link/IP level if required
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * - enable those instances
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * For EC_DEV_REMOVE, we:
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * - disable the associated link/IP NCUs
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey char *event_class = sysevent_get_class_name(ev);
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey char *event_subclass = sysevent_get_subclass_name(ev);
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey nlog(LOG_DEBUG, "sysevent_handler: event %s/%s", event_class,
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey /* Make sure sysevent is of expected class/subclass */
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey if ((strcmp(event_class, EC_DEV_ADD) != 0 &&
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey strcmp(event_class, EC_DEV_REMOVE) != 0) ||
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey strcmp(event_subclass, ESC_NETWORK) != 0) {
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey nlog(LOG_ERR, "sysevent_handler: unexpected sysevent "
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey "class/subclass %s/%s", event_class, event_subclass);
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey link_added = (strcmp(event_class, EC_DEV_ADD) == 0);
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * Retrieve driver name and instance attributes, and combine to
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * get interface name.
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey if (sysevent_get_attr_list(ev, &attr_list) != 0) {
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey nlog(LOG_ERR, "sysevent_handler: sysevent_get_attr_list: %m");
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey if (nvlist_lookup_string(attr_list, DEV_DRIVER_NAME, &driver) != 0 ||
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey nvlist_lookup_int32(attr_list, DEV_INSTANCE, &instance) != 0) {
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey nlog(LOG_ERR, "sysevent_handler: nvlist_lookup "
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey "of attributes failed: %m");
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey (void) snprintf(if_name, LIFNAMSIZ, "%s%d", driver, instance);
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey /* Ignore sysevent events for other zones */
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey if (!nwamd_link_belongs_to_this_zone(if_name))
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey /* Create event for link */
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey link_event = nwamd_event_init_link_action(if_name,
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey link_added ? NWAM_ACTION_ADD : NWAM_ACTION_REMOVE);
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey/* ARGSUSED0 */
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey sysevent_handle = sysevent_bind_handle(sysevent_handler);
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * Subscribe to ESC_NETWORK subclass of EC_DEV_ADD and EC_DEV_REMOVE
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * events. As a result, we get sysevent notification of hotplug
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * add/remove events, which we handle above in sysevent_handler().
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey if (sysevent_subscribe_event(sysevent_handle, EC_DEV_ADD, &subclass, 1)
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey sysevent_subscribe_event(sysevent_handle, EC_DEV_REMOVE, &subclass,
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey pfail("sysevent_subscribe_event: %s", strerror(errno));
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * We can't initialize in the main thread because we may need to wait until
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * svc:/system/sysevent:default finishes starting up. So we create a thread to
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey * initialize in.
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey pfail("nwamd_sysevents_init: pthread_attr_init failed: %s",
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey rc = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey pfail("nwamd_sysevents_init: pthread_attr_setdetachstate "
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey rc = pthread_create(NULL, &attr, sysevent_initialization, NULL);
6ba597c56d749c61b4f783157f63196d7b2445f0Anurag S. Maskey pfail("nwamd_sysevents_init: couldn't start sysevent init "