Blame complib/cl_nodenamemap.c

Packit 13e616
/*
Packit 13e616
 * Copyright (c) 2008 Voltaire, Inc. All rights reserved.
Packit 13e616
 * Copyright (c) 2007 Lawrence Livermore National Lab
Packit 13e616
 *
Packit 13e616
 * This software is available to you under a choice of one of two
Packit 13e616
 * licenses.  You may choose to be licensed under the terms of the GNU
Packit 13e616
 * General Public License (GPL) Version 2, available from the file
Packit 13e616
 * COPYING in the main directory of this source tree, or the
Packit 13e616
 * OpenIB.org BSD license below:
Packit 13e616
 *
Packit 13e616
 *     Redistribution and use in source and binary forms, with or
Packit 13e616
 *     without modification, are permitted provided that the following
Packit 13e616
 *     conditions are met:
Packit 13e616
 *
Packit 13e616
 *      - Redistributions of source code must retain the above
Packit 13e616
 *        copyright notice, this list of conditions and the following
Packit 13e616
 *        disclaimer.
Packit 13e616
 *
Packit 13e616
 *      - Redistributions in binary form must reproduce the above
Packit 13e616
 *        copyright notice, this list of conditions and the following
Packit 13e616
 *        disclaimer in the documentation and/or other materials
Packit 13e616
 *        provided with the distribution.
Packit 13e616
 *
Packit 13e616
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Packit 13e616
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Packit 13e616
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Packit 13e616
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
Packit 13e616
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
Packit 13e616
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
Packit 13e616
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
Packit 13e616
 * SOFTWARE.
Packit 13e616
 *
Packit 13e616
 */
Packit 13e616
Packit 13e616
#include <stdio.h>
Packit 13e616
#include <errno.h>
Packit 13e616
#include <string.h>
Packit 13e616
#include <stdlib.h>
Packit 13e616
#include <stdarg.h>
Packit 13e616
#include <sys/types.h>
Packit 13e616
#include <sys/stat.h>
Packit 13e616
#include <unistd.h>
Packit 13e616
#include <ctype.h>
Packit 13e616
#include <config.h>
Packit 13e616
#include <errno.h>
Packit 13e616
Packit 13e616
#include <complib/cl_nodenamemap.h>
Packit 13e616
#include <complib/cl_math.h>
Packit 13e616
Packit 13e616
#define PARSE_NODE_MAP_BUFLEN  256
Packit 13e616
Packit 13e616
static int parse_node_map_wrap(const char *file_name,
Packit 13e616
			       int (*create)(void *, uint64_t, char *),
Packit 13e616
			       void *cxt,
Packit 13e616
			       char *linebuf,
Packit 13e616
			       unsigned int linebuflen);
Packit 13e616
Packit 13e616
static int map_name(void *cxt, uint64_t guid, char *p)
Packit 13e616
{
Packit 13e616
	cl_qmap_t *map = cxt;
Packit 13e616
	name_map_item_t *item;
Packit 13e616
Packit 13e616
	p = strtok(p, "\"#");
Packit 13e616
	if (!p)
Packit 13e616
		return 0;
Packit 13e616
Packit 13e616
	item = malloc(sizeof(*item));
Packit 13e616
	if (!item)
Packit 13e616
		return -1;
Packit 13e616
	item->guid = guid;
Packit 13e616
	item->name = strdup(p);
Packit 13e616
	cl_qmap_insert(map, item->guid, (cl_map_item_t *) item);
Packit 13e616
	return 0;
Packit 13e616
}
Packit 13e616
Packit 13e616
nn_map_t *open_node_name_map(const char *node_name_map)
Packit 13e616
{
Packit 13e616
	nn_map_t *map;
Packit 13e616
	char linebuf[PARSE_NODE_MAP_BUFLEN + 1];
Packit 13e616
Packit 13e616
	if (!node_name_map) {
Packit 13e616
#ifdef HAVE_DEFAULT_NODENAME_MAP
Packit 13e616
		struct stat buf;
Packit 13e616
		node_name_map = HAVE_DEFAULT_NODENAME_MAP;
Packit 13e616
		if (stat(node_name_map, &buf))
Packit 13e616
			return NULL;
Packit 13e616
#else
Packit 13e616
		return NULL;
Packit 13e616
#endif				/* HAVE_DEFAULT_NODENAME_MAP */
Packit 13e616
	}
Packit 13e616
Packit 13e616
	map = malloc(sizeof(*map));
Packit 13e616
	if (!map)
Packit 13e616
		return NULL;
Packit 13e616
	cl_qmap_init(map);
Packit 13e616
Packit 13e616
	memset(linebuf, '\0', PARSE_NODE_MAP_BUFLEN + 1);
Packit 13e616
	if (parse_node_map_wrap(node_name_map, map_name, map,
Packit 13e616
				linebuf, PARSE_NODE_MAP_BUFLEN)) {
Packit 13e616
		if (errno == EIO) {
Packit 13e616
			fprintf(stderr,
Packit 13e616
				"WARNING failed to parse node name map "
Packit 13e616
				"\"%s\"\n",
Packit 13e616
				node_name_map);
Packit 13e616
			fprintf(stderr,
Packit 13e616
				"WARNING failed line: \"%s\"\n",
Packit 13e616
				linebuf);
Packit 13e616
		}
Packit 13e616
		else
Packit 13e616
			fprintf(stderr,
Packit 13e616
				"WARNING failed to open node name map "
Packit 13e616
				"\"%s\" (%s)\n",
Packit 13e616
				node_name_map, strerror(errno));
Packit 13e616
		close_node_name_map(map);
Packit 13e616
		return NULL;
Packit 13e616
	}
Packit 13e616
Packit 13e616
	return map;
Packit 13e616
}
Packit 13e616
Packit 13e616
void close_node_name_map(nn_map_t * map)
Packit 13e616
{
Packit 13e616
	name_map_item_t *item = NULL;
Packit 13e616
Packit 13e616
	if (!map)
Packit 13e616
		return;
Packit 13e616
Packit 13e616
	item = (name_map_item_t *) cl_qmap_head(map);
Packit 13e616
	while (item != (name_map_item_t *) cl_qmap_end(map)) {
Packit 13e616
		item = (name_map_item_t *) cl_qmap_remove(map, item->guid);
Packit 13e616
		free(item->name);
Packit 13e616
		free(item);
Packit 13e616
		item = (name_map_item_t *) cl_qmap_head(map);
Packit 13e616
	}
Packit 13e616
	free(map);
Packit 13e616
}
Packit 13e616
Packit 13e616
char *remap_node_name(nn_map_t * map, uint64_t target_guid, char *nodedesc)
Packit 13e616
{
Packit 13e616
	char *rc = NULL;
Packit 13e616
	name_map_item_t *item = NULL;
Packit 13e616
Packit 13e616
	if (!map)
Packit 13e616
		goto done;
Packit 13e616
Packit 13e616
	item = (name_map_item_t *) cl_qmap_get(map, target_guid);
Packit 13e616
	if (item != (name_map_item_t *) cl_qmap_end(map))
Packit 13e616
		rc = strdup(item->name);
Packit 13e616
Packit 13e616
done:
Packit 13e616
	if (rc == NULL)
Packit 13e616
		rc = strdup(clean_nodedesc(nodedesc));
Packit 13e616
	return (rc);
Packit 13e616
}
Packit 13e616
Packit 13e616
char *clean_nodedesc(char *nodedesc)
Packit 13e616
{
Packit 13e616
	int i = 0;
Packit 13e616
Packit 13e616
	nodedesc[63] = '\0';
Packit 13e616
	while (nodedesc[i]) {
Packit 13e616
		if (!isprint(nodedesc[i]))
Packit 13e616
			nodedesc[i] = ' ';
Packit 13e616
		i++;
Packit 13e616
	}
Packit 13e616
Packit 13e616
	return (nodedesc);
Packit 13e616
}
Packit 13e616
Packit 13e616
static int parse_node_map_wrap(const char *file_name,
Packit 13e616
			       int (*create) (void *, uint64_t, char *),
Packit 13e616
			       void *cxt,
Packit 13e616
			       char *linebuf,
Packit 13e616
			       unsigned int linebuflen)
Packit 13e616
{
Packit 13e616
	char line[PARSE_NODE_MAP_BUFLEN];
Packit 13e616
	FILE *f;
Packit 13e616
Packit 13e616
	if (!(f = fopen(file_name, "r")))
Packit 13e616
		return -1;
Packit 13e616
Packit 13e616
	while (fgets(line, sizeof(line), f)) {
Packit 13e616
		uint64_t guid;
Packit 13e616
		char *p, *e;
Packit 13e616
Packit 13e616
		p = line;
Packit 13e616
		while (isspace(*p))
Packit 13e616
			p++;
Packit 13e616
		if (*p == '\0' || *p == '\n' || *p == '#')
Packit 13e616
			continue;
Packit 13e616
Packit 13e616
		guid = strtoull(p, &e, 0);
Packit 13e616
		if (e == p || (!isspace(*e) && *e != '#' && *e != '\0')) {
Packit 13e616
			fclose(f);
Packit 13e616
			errno = EIO;
Packit 13e616
			if (linebuf) {
Packit 13e616
				memcpy(linebuf, line,
Packit 13e616
				       MIN(PARSE_NODE_MAP_BUFLEN, linebuflen));
Packit 13e616
				e = strpbrk(linebuf, "\n");
Packit 13e616
				if (e)
Packit 13e616
					*e = '\0';
Packit 13e616
			}
Packit 13e616
			return -1;
Packit 13e616
		}
Packit 13e616
Packit 13e616
		p = e;
Packit 13e616
		while (isspace(*p))
Packit 13e616
			p++;
Packit 13e616
Packit 13e616
		e = strpbrk(p, "\n");
Packit 13e616
		if (e)
Packit 13e616
			*e = '\0';
Packit 13e616
Packit 13e616
		if (create(cxt, guid, p)) {
Packit 13e616
			fclose(f);
Packit 13e616
			return -1;
Packit 13e616
		}
Packit 13e616
	}
Packit 13e616
Packit 13e616
	fclose(f);
Packit 13e616
	return 0;
Packit 13e616
}
Packit 13e616
Packit 13e616
int parse_node_map(const char *file_name,
Packit 13e616
		   int (*create) (void *, uint64_t, char *), void *cxt)
Packit 13e616
{
Packit 13e616
	return parse_node_map_wrap(file_name, create, cxt, NULL, 0);
Packit 13e616
}