fmd_dr.c revision d9638e547d8811f2c689977f8dd2a353938b61fd
17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
227c478bdstevel@tonic-gate/*
23d9638e5mws * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
277c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bdstevel@tonic-gate
297c478bdstevel@tonic-gate/*
307c478bdstevel@tonic-gate * FMD Dynamic Reconfiguration (DR) Event Handling
317c478bdstevel@tonic-gate *
327c478bdstevel@tonic-gate * Fault manager scheme plug-ins must track characteristics of individual
337c478bdstevel@tonic-gate * pieces of hardware.  As these components can be added or removed by a DR
347c478bdstevel@tonic-gate * operation, we need to provide a means by which plug-ins can determine when
357c478bdstevel@tonic-gate * they need to re-examine the current configuration.  We provide a simple
367c478bdstevel@tonic-gate * mechanism whereby this task can be implemented using lazy evaluation: a
377c478bdstevel@tonic-gate * simple 64-bit generation counter is maintained and incremented on *any* DR.
387c478bdstevel@tonic-gate * Schemes can store the generation number in scheme-specific data structures,
397c478bdstevel@tonic-gate * and then revalidate their contents if the current generation number has
407c478bdstevel@tonic-gate * changed since the resource information was cached.  This method saves time,
417c478bdstevel@tonic-gate * avoids the complexity of direct participation in DR, avoids the need for
427c478bdstevel@tonic-gate * resource-specific processing of DR events, and is relatively easy to port
437c478bdstevel@tonic-gate * to other systems that support dynamic reconfiguration.
447c478bdstevel@tonic-gate */
457c478bdstevel@tonic-gate
467c478bdstevel@tonic-gate#include <sys/types.h>
477c478bdstevel@tonic-gate#include <sys/sysevent/dr.h>
487c478bdstevel@tonic-gate#include <sys/sysevent/eventdefs.h>
497c478bdstevel@tonic-gate
507c478bdstevel@tonic-gate#include <stdio.h>
51d9638e5mws#include <unistd.h>
527c478bdstevel@tonic-gate#include <libsysevent.h>
537c478bdstevel@tonic-gate
547c478bdstevel@tonic-gate#undef MUTEX_HELD
557c478bdstevel@tonic-gate#undef RW_READ_HELD
567c478bdstevel@tonic-gate#undef RW_WRITE_HELD
577c478bdstevel@tonic-gate
587c478bdstevel@tonic-gate#include <fmd_error.h>
597c478bdstevel@tonic-gate#include <fmd_subr.h>
607c478bdstevel@tonic-gate#include <fmd.h>
617c478bdstevel@tonic-gate
627c478bdstevel@tonic-gatestatic void
637c478bdstevel@tonic-gatefmd_dr_event(sysevent_t *sep)
647c478bdstevel@tonic-gate{
657c478bdstevel@tonic-gate	uint64_t gen;
667c478bdstevel@tonic-gate
677c478bdstevel@tonic-gate	(void) pthread_mutex_lock(&fmd.d_stats_lock);
687c478bdstevel@tonic-gate	gen = fmd.d_stats->ds_dr_gen.fmds_value.ui64++;
697c478bdstevel@tonic-gate	(void) pthread_mutex_unlock(&fmd.d_stats_lock);
707c478bdstevel@tonic-gate
717c478bdstevel@tonic-gate	TRACE((FMD_DBG_XPRT, "dr event %p, gen=%llu", (void *)sep, gen));
727c478bdstevel@tonic-gate}
737c478bdstevel@tonic-gate
747c478bdstevel@tonic-gatevoid
757c478bdstevel@tonic-gatefmd_dr_init(void)
767c478bdstevel@tonic-gate{
777c478bdstevel@tonic-gate	const char *subclass = ESC_DR_AP_STATE_CHANGE;
787c478bdstevel@tonic-gate
79d9638e5mws	if (geteuid() != 0)
80d9638e5mws		return; /* legacy sysevent mechanism is still root-only */
817c478bdstevel@tonic-gate
827c478bdstevel@tonic-gate	if ((fmd.d_dr_hdl = sysevent_bind_handle(fmd_dr_event)) == NULL)
837c478bdstevel@tonic-gate		fmd_error(EFMD_EXIT, "failed to bind handle for DR sysevent");
847c478bdstevel@tonic-gate
857c478bdstevel@tonic-gate	if (sysevent_subscribe_event(fmd.d_dr_hdl, EC_DR, &subclass, 1) == -1)
867c478bdstevel@tonic-gate		fmd_error(EFMD_EXIT, "failed to subscribe for DR sysevent");
877c478bdstevel@tonic-gate}
887c478bdstevel@tonic-gate
897c478bdstevel@tonic-gatevoid
907c478bdstevel@tonic-gatefmd_dr_fini(void)
917c478bdstevel@tonic-gate{
927c478bdstevel@tonic-gate	if (fmd.d_dr_hdl != NULL) {
937c478bdstevel@tonic-gate		sysevent_unsubscribe_event(fmd.d_dr_hdl, EC_DR);
947c478bdstevel@tonic-gate		sysevent_unbind_handle(fmd.d_dr_hdl);
957c478bdstevel@tonic-gate	}
967c478bdstevel@tonic-gate}
97