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