xref: /illumos-gate/usr/src/lib/fm/topo/libtopo/common/topo_file.c (revision cd61ae21816e53b94bc1673f3f1aa651fc3115e8)
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 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <limits.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <sys/param.h>
33 #include <topo_error.h>
34 #include <topo_tree.h>
35 #include <topo_subr.h>
36 #include <topo_file.h>
37 
38 /*
39  * topo_file.c
40  *
41  *	This file hides the details of any file manipulation to
42  *	establish topology for a given enumerator.
43  */
44 
45 #define	TOPO_DEFAULT_FILE	"maps/%s-%s-topology.xml"
46 #define	TOPO_COMMON_FILE	"maps/%s-topology.xml"
47 
48 static void
49 topo_file_unload(topo_file_t *tfp)
50 {
51 
52 	if (tfp == NULL)
53 		return;
54 
55 	if (tfp->tf_filenm != NULL)
56 		topo_mod_strfree(tfp->tf_mod, tfp->tf_filenm);
57 
58 	if (tfp->tf_tmap != NULL)
59 		tf_info_free(tfp->tf_mod, tfp->tf_tmap);
60 
61 	topo_mod_free(tfp->tf_mod, tfp, sizeof (topo_file_t));
62 }
63 
64 int
65 topo_file_load(topo_mod_t *mod, tnode_t *node, const char *name,
66     const char *scheme, int pmap)
67 {
68 	topo_file_t *tfp;
69 	char fp[MAXNAMELEN];
70 
71 	if ((tfp = topo_mod_zalloc(mod, sizeof (topo_file_t))) == NULL)
72 		return (topo_mod_seterrno(mod, ETOPO_NOMEM));
73 
74 	tfp->tf_mod = mod;
75 
76 	if (name != NULL)
77 		(void) snprintf(fp, MAXNAMELEN, TOPO_DEFAULT_FILE, name,
78 		    scheme);
79 	else
80 		(void) snprintf(fp, MAXNAMELEN, TOPO_COMMON_FILE, scheme);
81 
82 	if ((tfp->tf_filenm = topo_search_path(mod, mod->tm_rootdir, fp))
83 	    == NULL) {
84 		topo_file_unload(tfp);
85 		return (topo_mod_seterrno(mod, ETOPO_MOD_NOENT));
86 	}
87 
88 	if ((tfp->tf_tmap = topo_xml_read(mod, tfp->tf_filenm, scheme))
89 	    == NULL) {
90 		topo_dprintf(mod->tm_hdl, TOPO_DBG_ERR,
91 		    "failed to load topology file %s: "
92 		    "%s\n", tfp->tf_filenm, topo_strerror(ETOPO_MOD_XRD));
93 		topo_file_unload(tfp);
94 		return (topo_mod_seterrno(mod, ETOPO_MOD_XRD));
95 	}
96 
97 	if (pmap)
98 		tfp->tf_tmap->tf_flags |= TF_PROPMAP;
99 
100 	if (topo_xml_enum(mod, tfp->tf_tmap, node) < 0) {
101 		topo_dprintf(mod->tm_hdl, TOPO_DBG_ERR,
102 		    "Failed to enumerate topology: %s\n",
103 		    topo_strerror(ETOPO_MOD_XENUM));
104 		topo_file_unload(tfp);
105 		return (topo_mod_seterrno(mod, ETOPO_MOD_XENUM));
106 	}
107 
108 	topo_file_unload(tfp);
109 
110 	return (0);
111 }
112