2 * Copyright (c) 2008 Voltaire, Inc. All rights reserved.
3 * Copyright (c) 2007 Lawrence Livermore National Lab
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
41 #include <sys/types.h>
48 #include <complib/cl_nodenamemap.h>
49 #include <complib/cl_math.h>
51 #define PARSE_NODE_MAP_BUFLEN 256
53 static int parse_node_map_wrap(const char *file_name,
54 int (*create)(void *, uint64_t, char *),
57 unsigned int linebuflen);
59 static int map_name(void *cxt, uint64_t guid, char *p)
62 name_map_item_t *item;
68 item = malloc(sizeof(*item));
72 item->name = strdup(p);
73 cl_qmap_insert(map, item->guid, (cl_map_item_t *) item);
77 nn_map_t *open_node_name_map(const char *node_name_map)
80 char linebuf[PARSE_NODE_MAP_BUFLEN + 1];
83 #ifdef HAVE_DEFAULT_NODENAME_MAP
85 node_name_map = HAVE_DEFAULT_NODENAME_MAP;
86 if (stat(node_name_map, &buf))
90 #endif /* HAVE_DEFAULT_NODENAME_MAP */
93 map = malloc(sizeof(*map));
98 memset(linebuf, '\0', PARSE_NODE_MAP_BUFLEN + 1);
99 if (parse_node_map_wrap(node_name_map, map_name, map,
100 linebuf, PARSE_NODE_MAP_BUFLEN)) {
103 "WARNING failed to parse node name map "
107 "WARNING failed line: \"%s\"\n",
112 "WARNING failed to open node name map "
114 node_name_map, strerror(errno));
115 close_node_name_map(map);
122 void close_node_name_map(nn_map_t * map)
124 name_map_item_t *item = NULL;
129 item = (name_map_item_t *) cl_qmap_head(map);
130 while (item != (name_map_item_t *) cl_qmap_end(map)) {
131 item = (name_map_item_t *) cl_qmap_remove(map, item->guid);
134 item = (name_map_item_t *) cl_qmap_head(map);
139 char *remap_node_name(nn_map_t * map, uint64_t target_guid, char *nodedesc)
142 name_map_item_t *item = NULL;
147 item = (name_map_item_t *) cl_qmap_get(map, target_guid);
148 if (item != (name_map_item_t *) cl_qmap_end(map))
149 rc = strdup(item->name);
153 rc = strdup(clean_nodedesc(nodedesc));
157 char *clean_nodedesc(char *nodedesc)
162 while (nodedesc[i]) {
163 if (!isprint(nodedesc[i]))
171 static int parse_node_map_wrap(const char *file_name,
172 int (*create) (void *, uint64_t, char *),
175 unsigned int linebuflen)
177 char line[PARSE_NODE_MAP_BUFLEN];
180 if (!(f = fopen(file_name, "r")))
183 while (fgets(line, sizeof(line), f)) {
190 if (*p == '\0' || *p == '\n' || *p == '#')
193 guid = strtoull(p, &e, 0);
194 if (e == p || (!isspace(*e) && *e != '#' && *e != '\0')) {
198 memcpy(linebuf, line,
199 MIN(PARSE_NODE_MAP_BUFLEN, linebuflen));
200 e = strpbrk(linebuf, "\n");
211 e = strpbrk(p, "\n");
215 if (create(cxt, guid, p)) {
225 int parse_node_map(const char *file_name,
226 int (*create) (void *, uint64_t, char *), void *cxt)
228 return parse_node_map_wrap(file_name, create, cxt, NULL, 0);