Blame opensm/osm_event_plugin.c

Packit 13e616
/*
Packit 13e616
 * Copyright (c) 2008-2009 Voltaire, Inc. All rights reserved.
Packit 13e616
 * Copyright (c) 2007 The Regents of the University of California.
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
/****h* OpenSM Event plugin interface
Packit 13e616
* DESCRIPTION
Packit 13e616
*       Database interface to record subnet events
Packit 13e616
*
Packit 13e616
*       Implementations of this object _MUST_ be thread safe.
Packit 13e616
*
Packit 13e616
* AUTHOR
Packit 13e616
*	Ira Weiny, LLNL
Packit 13e616
*
Packit 13e616
*********/
Packit 13e616
Packit 13e616
#if HAVE_CONFIG_H
Packit 13e616
#  include <config.h>
Packit 13e616
#endif				/* HAVE_CONFIG_H */
Packit 13e616
Packit 13e616
#include <stdlib.h>
Packit 13e616
#include <dlfcn.h>
Packit 13e616
#include <opensm/osm_file_ids.h>
Packit 13e616
#define FILE_ID OSM_FILE_EVENT_PLUGIN_C
Packit 13e616
#include <opensm/osm_event_plugin.h>
Packit 13e616
#include <opensm/osm_opensm.h>
Packit 13e616
Packit 13e616
#if defined(PATH_MAX)
Packit 13e616
#define OSM_PATH_MAX	(PATH_MAX + 1)
Packit 13e616
#elif defined (_POSIX_PATH_MAX)
Packit 13e616
#define OSM_PATH_MAX	(_POSIX_PATH_MAX + 1)
Packit 13e616
#else
Packit 13e616
#define OSM_PATH_MAX	256
Packit 13e616
#endif
Packit 13e616
Packit 13e616
/**
Packit 13e616
 * functions
Packit 13e616
 */
Packit 13e616
osm_epi_plugin_t *osm_epi_construct(osm_opensm_t *osm, char *plugin_name)
Packit 13e616
{
Packit 13e616
	char lib_name[OSM_PATH_MAX];
Packit 13e616
	struct old_if { unsigned ver; } *old_impl;
Packit 13e616
	osm_epi_plugin_t *rc = NULL;
Packit 13e616
Packit 13e616
	if (!plugin_name || !*plugin_name)
Packit 13e616
		return NULL;
Packit 13e616
Packit 13e616
	/* find the plugin */
Packit 13e616
	snprintf(lib_name, sizeof(lib_name), "lib%s.so", plugin_name);
Packit 13e616
Packit 13e616
	rc = malloc(sizeof(*rc));
Packit 13e616
	if (!rc)
Packit 13e616
		return NULL;
Packit 13e616
Packit 13e616
	rc->handle = dlopen(lib_name, RTLD_LAZY);
Packit 13e616
	if (!rc->handle) {
Packit 13e616
		OSM_LOG(&osm->log, OSM_LOG_ERROR,
Packit 13e616
			"Failed to open event plugin \"%s\" : \"%s\"\n",
Packit 13e616
			lib_name, dlerror());
Packit 13e616
		goto DLOPENFAIL;
Packit 13e616
	}
Packit 13e616
Packit 13e616
	rc->impl =
Packit 13e616
	    (osm_event_plugin_t *) dlsym(rc->handle,
Packit 13e616
					 OSM_EVENT_PLUGIN_IMPL_NAME);
Packit 13e616
	if (!rc->impl) {
Packit 13e616
		OSM_LOG(&osm->log, OSM_LOG_ERROR,
Packit 13e616
			"Failed to find \"%s\" symbol in \"%s\" : \"%s\"\n",
Packit 13e616
			OSM_EVENT_PLUGIN_IMPL_NAME, lib_name, dlerror());
Packit 13e616
		goto Exit;
Packit 13e616
	}
Packit 13e616
Packit 13e616
	/* check for old interface */
Packit 13e616
	old_impl = (struct old_if *) rc->impl;
Packit 13e616
	if (old_impl->ver == OSM_ORIG_EVENT_PLUGIN_INTERFACE_VER) {
Packit 13e616
		OSM_LOG(&osm->log, OSM_LOG_ERROR, "Error loading plugin: "
Packit 13e616
			"\'%s\' contains a deprecated interface version %d\n"
Packit 13e616
			"   Please recompile with the new interface.\n",
Packit 13e616
			plugin_name, old_impl->ver);
Packit 13e616
		goto Exit;
Packit 13e616
	}
Packit 13e616
Packit 13e616
	/* Check the version to make sure this module will work with us */
Packit 13e616
	if (strcmp(rc->impl->osm_version, osm->osm_version)) {
Packit 13e616
		OSM_LOG(&osm->log, OSM_LOG_ERROR, "Error loading plugin"
Packit 13e616
			" \'%s\': OpenSM version mismatch - plugin was built"
Packit 13e616
			" against %s version of OpenSM. Skip loading.\n",
Packit 13e616
			plugin_name, rc->impl->osm_version);
Packit 13e616
		goto Exit;
Packit 13e616
	}
Packit 13e616
Packit 13e616
	if (!rc->impl->create) {
Packit 13e616
		OSM_LOG(&osm->log, OSM_LOG_ERROR,
Packit 13e616
			"Error loading plugin \'%s\': no create() method.\n",
Packit 13e616
			plugin_name);
Packit 13e616
		goto Exit;
Packit 13e616
	}
Packit 13e616
Packit 13e616
	rc->plugin_data = rc->impl->create(osm);
Packit 13e616
Packit 13e616
	if (!rc->plugin_data)
Packit 13e616
		goto Exit;
Packit 13e616
Packit 13e616
	rc->plugin_name = strdup(plugin_name);
Packit 13e616
	return rc;
Packit 13e616
Packit 13e616
Exit:
Packit 13e616
	dlclose(rc->handle);
Packit 13e616
DLOPENFAIL:
Packit 13e616
	free(rc);
Packit 13e616
	return NULL;
Packit 13e616
}
Packit 13e616
Packit 13e616
void osm_epi_destroy(osm_epi_plugin_t * plugin)
Packit 13e616
{
Packit 13e616
	if (plugin) {
Packit 13e616
		if (plugin->impl->delete)
Packit 13e616
			plugin->impl->delete(plugin->plugin_data);
Packit 13e616
		dlclose(plugin->handle);
Packit 13e616
		free(plugin->plugin_name);
Packit 13e616
		free(plugin);
Packit 13e616
	}
Packit 13e616
}