dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / fm / topo / libtopo / common / topo_file.c
blob028f9d1786bd98ed45fb4781fc80b7e4c7ddafc8
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
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>
39 * topo_file.c
41 * This file hides the details of any file manipulation to
42 * establish topology for a given enumerator.
45 #define TOPO_DEFAULT_FILE "maps/%s-%s-topology.xml"
46 #define TOPO_COMMON_FILE "maps/%s-topology.xml"
48 static void
49 topo_file_unload(topo_file_t *tfp)
52 if (tfp == NULL)
53 return;
55 if (tfp->tf_filenm != NULL)
56 topo_mod_strfree(tfp->tf_mod, tfp->tf_filenm);
58 if (tfp->tf_tmap != NULL)
59 tf_info_free(tfp->tf_mod, tfp->tf_tmap);
61 topo_mod_free(tfp->tf_mod, tfp, sizeof (topo_file_t));
64 int
65 topo_file_load(topo_mod_t *mod, tnode_t *node, const char *name,
66 const char *scheme, int pmap)
68 topo_file_t *tfp;
69 char fp[MAXNAMELEN];
71 if ((tfp = topo_mod_zalloc(mod, sizeof (topo_file_t))) == NULL)
72 return (topo_mod_seterrno(mod, ETOPO_NOMEM));
74 tfp->tf_mod = mod;
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);
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));
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));
97 if (pmap)
98 tfp->tf_tmap->tf_flags |= TF_PROPMAP;
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));
108 topo_file_unload(tfp);
110 return (0);