Blame src/dlmisc.c

Packit 4a16fb
/**
Packit 4a16fb
 * \file dlmisc.c
Packit 4a16fb
 * \brief dynamic loader helpers
Packit 4a16fb
 * \author Jaroslav Kysela <perex@perex.cz>
Packit 4a16fb
 * \date 2001
Packit 4a16fb
 *
Packit 4a16fb
 * Dynamic loader helpers
Packit 4a16fb
 */
Packit 4a16fb
/*
Packit 4a16fb
 *  Dynamic loader helpers
Packit 4a16fb
 *  Copyright (c) 2000 by Jaroslav Kysela <perex@perex.cz>
Packit 4a16fb
 *
Packit 4a16fb
 *
Packit 4a16fb
 *   This library is free software; you can redistribute it and/or modify
Packit 4a16fb
 *   it under the terms of the GNU Lesser General Public License as
Packit 4a16fb
 *   published by the Free Software Foundation; either version 2.1 of
Packit 4a16fb
 *   the License, or (at your option) any later version.
Packit 4a16fb
 *
Packit 4a16fb
 *   This program is distributed in the hope that it will be useful,
Packit 4a16fb
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 4a16fb
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 4a16fb
 *   GNU Lesser General Public License for more details.
Packit 4a16fb
 *
Packit 4a16fb
 *   You should have received a copy of the GNU Lesser General Public
Packit 4a16fb
 *   License along with this library; if not, write to the Free Software
Packit 4a16fb
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit 4a16fb
 *
Packit 4a16fb
 */
Packit 4a16fb
Packit 4a16fb
#include "list.h"
Packit 4a16fb
#include "local.h"
Packit 4a16fb
#ifdef HAVE_LIBPTHREAD
Packit 4a16fb
#include <pthread.h>
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
#ifndef DOC_HIDDEN
Packit 4a16fb
#ifndef PIC
Packit 4a16fb
struct snd_dlsym_link *snd_dlsym_start = NULL;
Packit 4a16fb
#endif
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Opens a dynamic library - ALSA wrapper for \c dlopen.
Packit 4a16fb
 * \param name name of the library, similar to \c dlopen.
Packit 4a16fb
 * \param mode mode flags, similar to \c dlopen.
Packit 4a16fb
 * \param errbuf a string buffer for the error message \c dlerror.
Packit 4a16fb
 * \param errbuflen a length of the string buffer for the error message.
Packit 4a16fb
 * \return Library handle if successful, otherwise \c NULL.
Packit 4a16fb
 *
Packit 4a16fb
 * This function can emulate dynamic linking for the static build of
Packit 4a16fb
 * the alsa-lib library. In that case, \p name is set to \c NULL.
Packit 4a16fb
 */
Packit 4a16fb
#ifndef DOXYGEN
Packit 4a16fb
EXPORT_SYMBOL void *INTERNAL(snd_dlopen)(const char *name, int mode, char *errbuf, size_t errbuflen)
Packit 4a16fb
#else
Packit 4a16fb
void *snd_dlopen(const char *name, int mode, char *errbuf, size_t errbuflen)
Packit 4a16fb
#endif
Packit 4a16fb
{
Packit 4a16fb
#ifndef PIC
Packit 4a16fb
	if (name == NULL)
Packit 4a16fb
		return &snd_dlsym_start;
Packit 4a16fb
#else
Packit 4a16fb
#ifdef HAVE_LIBDL
Packit 4a16fb
	if (name == NULL) {
Packit 4a16fb
		static const char * self = NULL;
Packit 4a16fb
		if (self == NULL) {
Packit 4a16fb
			Dl_info dlinfo;
Packit 4a16fb
			if (dladdr(snd_dlopen, &dlinfo) > 0)
Packit 4a16fb
				self = dlinfo.dli_fname;
Packit 4a16fb
		}
Packit 4a16fb
		name = self;
Packit 4a16fb
	}
Packit 4a16fb
#endif
Packit 4a16fb
#endif
Packit 4a16fb
#ifdef HAVE_LIBDL
Packit 4a16fb
	/*
Packit 4a16fb
	 * Handle the plugin dir not being on the default dlopen search
Packit 4a16fb
	 * path, without resorting to polluting the entire system namespace
Packit 4a16fb
	 * via ld.so.conf.
Packit 4a16fb
	 */
Packit 4a16fb
	void *handle = NULL;
Packit 4a16fb
	char *filename = NULL;
Packit 4a16fb
Packit 4a16fb
	if (name && name[0] != '/') {
Packit 4a16fb
		filename = alloca(sizeof(ALSA_PLUGIN_DIR) + 1 + strlen(name) + 1);
Packit 4a16fb
		if (filename) {
Packit 4a16fb
			strcpy(filename, ALSA_PLUGIN_DIR);
Packit 4a16fb
			strcat(filename, "/");
Packit 4a16fb
			strcat(filename, name);
Packit 4a16fb
			handle = dlopen(filename, mode);
Packit 4a16fb
			if (!handle) {
Packit 4a16fb
				/* if the filename exists and cannot be opened */
Packit 4a16fb
				/* return immediately */
Packit 4a16fb
				if (access(filename, X_OK) == 0)
Packit 4a16fb
					goto errpath;
Packit 4a16fb
			}
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	if (!handle) {
Packit 4a16fb
		handle = dlopen(name, mode);
Packit 4a16fb
		if (!handle)
Packit 4a16fb
			goto errpath;
Packit 4a16fb
	}
Packit 4a16fb
	return handle;
Packit 4a16fb
errpath:
Packit 4a16fb
	if (errbuf)
Packit 4a16fb
		snprintf(errbuf, errbuflen, "%s: %s", filename, dlerror());
Packit 4a16fb
#endif
Packit 4a16fb
	return NULL;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
#ifndef DOXYGEN
Packit 4a16fb
void *INTERNAL(snd_dlopen_old)(const char *name, int mode)
Packit 4a16fb
{
Packit 4a16fb
  return INTERNAL(snd_dlopen)(name, mode, NULL, 0);
Packit 4a16fb
}
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
use_symbol_version(__snd_dlopen_old, snd_dlopen, ALSA_0.9);
Packit 4a16fb
use_default_symbol_version(__snd_dlopen, snd_dlopen, ALSA_1.1.6);
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Closes a dynamic library - ALSA wrapper for \c dlclose.
Packit 4a16fb
 * \param handle Library handle, similar to \c dlclose.
Packit 4a16fb
 * \return Zero if successful, otherwise an error code.
Packit 4a16fb
 *
Packit 4a16fb
 * This function can emulate dynamic linking for the static build of
Packit 4a16fb
 * the alsa-lib library.
Packit 4a16fb
 */
Packit 4a16fb
int snd_dlclose(void *handle)
Packit 4a16fb
{
Packit 4a16fb
#ifndef PIC
Packit 4a16fb
	if (handle == &snd_dlsym_start)
Packit 4a16fb
		return 0;
Packit 4a16fb
#endif
Packit 4a16fb
#ifdef HAVE_LIBDL
Packit 4a16fb
	return dlclose(handle);
Packit 4a16fb
#else
Packit 4a16fb
	return 0;
Packit 4a16fb
#endif
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Verifies a dynamically loaded symbol.
Packit 4a16fb
 * \param handle Library handle, similar to \c dlsym.
Packit 4a16fb
 * \param name Symbol name.
Packit 4a16fb
 * \param version Version of the symbol.
Packit 4a16fb
 * \return Zero is successful, otherwise a negative error code.
Packit 4a16fb
 *
Packit 4a16fb
 * This function checks that the symbol with the version appended to its name
Packit 4a16fb
 * does exist in the library.
Packit 4a16fb
 */
Packit 4a16fb
static int snd_dlsym_verify(void *handle, const char *name, const char *version)
Packit 4a16fb
{
Packit 4a16fb
#ifdef HAVE_LIBDL
Packit 4a16fb
	int res;
Packit 4a16fb
	char *vname;
Packit 4a16fb
	
Packit 4a16fb
	if (handle == NULL)
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	vname = alloca(1 + strlen(name) + strlen(version) + 1);
Packit 4a16fb
	if (vname == NULL)
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	vname[0] = '_';
Packit 4a16fb
	strcpy(vname + 1, name);
Packit 4a16fb
	strcat(vname, version);
Packit 4a16fb
	res = dlsym(handle, vname) == NULL ? -ENOENT : 0;
Packit 4a16fb
	// printf("dlsym verify: %i, vname = '%s'\n", res, vname);
Packit 4a16fb
	if (res < 0)
Packit 4a16fb
		SNDERR("unable to verify version for symbol %s", name);
Packit 4a16fb
	return res;
Packit 4a16fb
#else
Packit 4a16fb
	return 0;
Packit 4a16fb
#endif
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Resolves a symbol from a dynamic library - ALSA wrapper for \c dlsym.
Packit 4a16fb
 * \param handle Library handle, similar to \c dlsym.
Packit 4a16fb
 * \param name Symbol name.
Packit 4a16fb
 * \param version Version of the symbol.
Packit 4a16fb
 *
Packit 4a16fb
 * This function can emulate dynamic linking for the static build of
Packit 4a16fb
 * the alsa-lib library.
Packit 4a16fb
 *
Packit 4a16fb
 * This special version of the \c dlsym function checks also the version
Packit 4a16fb
 * of the symbol. A versioned symbol should be defined using the
Packit 4a16fb
 * #SND_DLSYM_BUILD_VERSION macro.
Packit 4a16fb
 */
Packit 4a16fb
void *snd_dlsym(void *handle, const char *name, const char *version)
Packit 4a16fb
{
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
#ifndef PIC
Packit 4a16fb
	if (handle == &snd_dlsym_start) {
Packit 4a16fb
		/* it's the funny part: */
Packit 4a16fb
		/* we are looking for a symbol in a static library */
Packit 4a16fb
		struct snd_dlsym_link *link = snd_dlsym_start;
Packit 4a16fb
		while (link) {
Packit 4a16fb
			if (!strcmp(name, link->dlsym_name))
Packit 4a16fb
				return (void *)link->dlsym_ptr;
Packit 4a16fb
			link = link->next;
Packit 4a16fb
		}
Packit 4a16fb
		return NULL;
Packit 4a16fb
	}
Packit 4a16fb
#endif
Packit 4a16fb
#ifdef HAVE_LIBDL
Packit 4a16fb
#ifdef VERSIONED_SYMBOLS
Packit 4a16fb
	if (version) {
Packit 4a16fb
		err = snd_dlsym_verify(handle, name, version);
Packit 4a16fb
		if (err < 0)
Packit 4a16fb
			return NULL;
Packit 4a16fb
	}
Packit 4a16fb
#endif
Packit 4a16fb
	return dlsym(handle, name);
Packit 4a16fb
#else
Packit 4a16fb
	return NULL;
Packit 4a16fb
#endif
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * dlobj cache
Packit 4a16fb
 */
Packit 4a16fb
Packit 4a16fb
#ifndef DOC_HIDDEN
Packit 4a16fb
struct dlobj_cache {
Packit 4a16fb
	const char *lib;
Packit 4a16fb
	const char *name;
Packit 4a16fb
	void *dlobj;
Packit 4a16fb
	void *func;
Packit 4a16fb
	unsigned int refcnt;
Packit 4a16fb
	struct list_head list;
Packit 4a16fb
};
Packit 4a16fb
Packit 4a16fb
#ifdef HAVE_LIBPTHREAD
Packit 4a16fb
static pthread_mutex_t snd_dlobj_mutex = PTHREAD_MUTEX_INITIALIZER;
Packit 4a16fb
Packit 4a16fb
static inline void snd_dlobj_lock(void)
Packit 4a16fb
{
Packit 4a16fb
	pthread_mutex_lock(&snd_dlobj_mutex);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static inline void snd_dlobj_unlock(void)
Packit 4a16fb
{
Packit 4a16fb
	pthread_mutex_unlock(&snd_dlobj_mutex);
Packit 4a16fb
}
Packit 4a16fb
#else
Packit 4a16fb
static inline void snd_dlobj_lock(void) {}
Packit 4a16fb
static inline void snd_dlobj_unlock(void) {}
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
static LIST_HEAD(pcm_dlobj_list);
Packit 4a16fb
Packit 4a16fb
static struct dlobj_cache *
Packit 4a16fb
snd_dlobj_cache_get0(const char *lib, const char *name,
Packit 4a16fb
		     const char *version, int verbose)
Packit 4a16fb
{
Packit 4a16fb
	struct list_head *p;
Packit 4a16fb
	struct dlobj_cache *c;
Packit 4a16fb
	void *func, *dlobj;
Packit 4a16fb
	char errbuf[256];
Packit 4a16fb
Packit 4a16fb
	list_for_each(p, &pcm_dlobj_list) {
Packit 4a16fb
		c = list_entry(p, struct dlobj_cache, list);
Packit 4a16fb
		if (c->lib && lib && strcmp(c->lib, lib) != 0)
Packit 4a16fb
			continue;
Packit 4a16fb
		if (!c->lib && lib)
Packit 4a16fb
			continue;
Packit 4a16fb
		if (!lib && c->lib)
Packit 4a16fb
			continue;
Packit 4a16fb
		if (strcmp(c->name, name) == 0) {
Packit 4a16fb
			c->refcnt++;
Packit 4a16fb
			return c;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	errbuf[0] = '\0';
Packit 4a16fb
	dlobj = INTERNAL(snd_dlopen)(lib, RTLD_NOW,
Packit 4a16fb
	                   verbose ? errbuf : 0,
Packit 4a16fb
	                   verbose ? sizeof(errbuf) : 0);
Packit 4a16fb
	if (dlobj == NULL) {
Packit 4a16fb
		if (verbose)
Packit 4a16fb
			SNDERR("Cannot open shared library %s (%s)",
Packit 4a16fb
						lib ? lib : "[builtin]",
Packit 4a16fb
						errbuf);
Packit 4a16fb
		return NULL;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	func = snd_dlsym(dlobj, name, version);
Packit 4a16fb
	if (func == NULL) {
Packit 4a16fb
		if (verbose)
Packit 4a16fb
			SNDERR("symbol %s is not defined inside %s",
Packit 4a16fb
					name, lib ? lib : "[builtin]");
Packit 4a16fb
		goto __err;
Packit 4a16fb
	}
Packit 4a16fb
	c = malloc(sizeof(*c));
Packit 4a16fb
	if (! c)
Packit 4a16fb
		goto __err;
Packit 4a16fb
	c->refcnt = 1;
Packit 4a16fb
	c->lib = lib ? strdup(lib) : NULL;
Packit 4a16fb
	c->name = strdup(name);
Packit 4a16fb
	if ((lib && ! c->lib) || ! c->name) {
Packit 4a16fb
		free((void *)c->name);
Packit 4a16fb
		free((void *)c->lib);
Packit 4a16fb
		free(c);
Packit 4a16fb
	      __err:
Packit 4a16fb
		snd_dlclose(dlobj);
Packit 4a16fb
		snd_dlobj_unlock();
Packit 4a16fb
		return NULL;
Packit 4a16fb
	}
Packit 4a16fb
	c->dlobj = dlobj;
Packit 4a16fb
	c->func = func;
Packit 4a16fb
	list_add_tail(&c->list, &pcm_dlobj_list);
Packit 4a16fb
	return c;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
void *snd_dlobj_cache_get(const char *lib, const char *name,
Packit 4a16fb
			  const char *version, int verbose)
Packit 4a16fb
{
Packit 4a16fb
	struct dlobj_cache *c;
Packit 4a16fb
	void *func = NULL;
Packit 4a16fb
Packit 4a16fb
	snd_dlobj_lock();
Packit 4a16fb
	c = snd_dlobj_cache_get0(lib, name, version, verbose);
Packit 4a16fb
	if (c)
Packit 4a16fb
		func = c->func;
Packit 4a16fb
	snd_dlobj_unlock();
Packit 4a16fb
	return func;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
void *snd_dlobj_cache_get2(const char *lib, const char *name,
Packit 4a16fb
			   const char *version, int verbose)
Packit 4a16fb
{
Packit 4a16fb
	struct dlobj_cache *c;
Packit 4a16fb
	void *func = NULL;
Packit 4a16fb
Packit 4a16fb
	snd_dlobj_lock();
Packit 4a16fb
	c = snd_dlobj_cache_get0(lib, name, version, verbose);
Packit 4a16fb
	if (c) {
Packit 4a16fb
		func = c->func;
Packit 4a16fb
		/* double reference */
Packit 4a16fb
		c->refcnt++;
Packit 4a16fb
	}
Packit 4a16fb
	snd_dlobj_unlock();
Packit 4a16fb
	return func;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
int snd_dlobj_cache_put(void *func)
Packit 4a16fb
{
Packit 4a16fb
	struct list_head *p;
Packit 4a16fb
	struct dlobj_cache *c;
Packit 4a16fb
	unsigned int refcnt;
Packit 4a16fb
Packit 4a16fb
	if (!func)
Packit 4a16fb
		return -ENOENT;
Packit 4a16fb
Packit 4a16fb
	snd_dlobj_lock();
Packit 4a16fb
	list_for_each(p, &pcm_dlobj_list) {
Packit 4a16fb
		c = list_entry(p, struct dlobj_cache, list);
Packit 4a16fb
		if (c->func == func) {
Packit 4a16fb
			refcnt = c->refcnt;
Packit 4a16fb
			if (c->refcnt > 0)
Packit 4a16fb
				c->refcnt--;
Packit 4a16fb
			snd_dlobj_unlock();
Packit 4a16fb
			return refcnt == 1 ? 0 : -EINVAL;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	snd_dlobj_unlock();
Packit 4a16fb
	return -ENOENT;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
void snd_dlobj_cache_cleanup(void)
Packit 4a16fb
{
Packit 4a16fb
	struct list_head *p, *npos;
Packit 4a16fb
	struct dlobj_cache *c;
Packit 4a16fb
Packit 4a16fb
	snd_dlobj_lock();
Packit 4a16fb
	list_for_each_safe(p, npos, &pcm_dlobj_list) {
Packit 4a16fb
		c = list_entry(p, struct dlobj_cache, list);
Packit 4a16fb
		if (c->refcnt)
Packit 4a16fb
			continue;
Packit 4a16fb
		list_del(p);
Packit 4a16fb
		snd_dlclose(c->dlobj);
Packit 4a16fb
		free((void *)c->name); /* shut up gcc warning */
Packit 4a16fb
		free((void *)c->lib); /* shut up gcc warning */
Packit 4a16fb
		free(c);
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	snd_dlobj_unlock();
Packit 4a16fb
}
Packit 4a16fb
#endif