xref: /titanic_50/usr/src/cmd/fm/fmd/common/fmd_topo.c (revision fba5460f9ff1a4800cb5456354cb51962a5c092a)
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  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * FMD Topology Handling
30  *
31  * Fault manager scheme and module plug-ins may need access to the latest
32  * libtopo snapshot.  Upon fmd initialization, a snapshot is taken and
33  * made available via fmd_fmri_topology() and fmd_hdl_topology().  Each
34  * of these routines returns a libtopo snapshot handle back to the caller.
35  * New snapshots are taken if and when a DR event causes the DR generation
36  * number to increase.  The current snapshot is retained to assure consistency
37  * for modules still using older snapshots and the latest snapshot handle is
38  * returned to the caller.
39  */
40 
41 #include <fmd_alloc.h>
42 #include <fmd_error.h>
43 #include <fmd_subr.h>
44 #include <fmd_topo.h>
45 #include <fmd.h>
46 
47 #include <string.h>
48 #include <unistd.h>
49 #include <sys/types.h>
50 #include <fm/fmd_fmri.h>
51 #include <fm/libtopo.h>
52 
53 static void
54 fmd_topo_update(void)
55 {
56 	int err;
57 	topo_hdl_t *tp;
58 	fmd_topo_t *ftp;
59 	char *id;
60 	const char *name;
61 
62 	ASSERT(MUTEX_HELD(&fmd.d_topo_lock));
63 
64 	name = fmd.d_rootdir != NULL &&
65 	    *fmd.d_rootdir != '\0' ? fmd.d_rootdir : NULL;
66 
67 	/*
68 	 * Update the topology snapshot.
69 	 */
70 	if ((tp = topo_open(TOPO_VERSION, name, &err)) == NULL)
71 		fmd_panic("failed to open topology library: %s",
72 		    topo_strerror(err));
73 
74 	if ((id = topo_snap_hold(tp, NULL, &err)) == NULL)
75 		fmd_panic("failed to get topology snapshot: %s",
76 		    topo_strerror(err));
77 
78 	topo_hdl_strfree(tp, id);
79 
80 	ftp = fmd_alloc(sizeof (fmd_topo_t), FMD_SLEEP);
81 	ftp->ft_hdl = tp;
82 	fmd.d_stats->ds_topo_gen.fmds_value.ui64++;
83 	fmd_list_prepend(&fmd.d_topo_list, ftp);
84 
85 }
86 
87 topo_hdl_t *
88 fmd_topo_handle(int version)
89 {
90 	uint64_t curgen;
91 	fmd_topo_t *ftp;
92 
93 	if (version != TOPO_VERSION)
94 		return (NULL);
95 
96 	(void) pthread_mutex_lock(&fmd.d_topo_lock);
97 	if ((curgen = fmd_fmri_get_drgen()) >
98 	    fmd.d_stats->ds_topo_drgen.fmds_value.ui64) {
99 		fmd.d_stats->ds_topo_drgen.fmds_value.ui64 = curgen;
100 		fmd_topo_update();
101 	}
102 	ftp = fmd_list_next(&fmd.d_topo_list);
103 	(void) pthread_mutex_unlock(&fmd.d_topo_lock);
104 
105 	return ((topo_hdl_t *)ftp->ft_hdl);
106 }
107 
108 void
109 fmd_topo_init(void)
110 {
111 	(void) pthread_mutex_lock(&fmd.d_topo_lock);
112 	fmd_topo_update();
113 	(void) pthread_mutex_unlock(&fmd.d_topo_lock);
114 }
115 
116 void
117 fmd_topo_fini(void)
118 {
119 	fmd_topo_t *ftp;
120 
121 	(void) pthread_mutex_lock(&fmd.d_topo_lock);
122 	while ((ftp = fmd_list_next(&fmd.d_topo_list)) != NULL) {
123 		fmd_list_delete(&fmd.d_topo_list, ftp);
124 		topo_close(ftp->ft_hdl);
125 		fmd_free(ftp, sizeof (fmd_topo_t));
126 	}
127 	(void) pthread_mutex_unlock(&fmd.d_topo_lock);
128 }
129