xref: /netbsd-src/external/gpl2/lvm2/dist/lib/misc/sharedlib.c (revision bec4d750d436214708904d6a2c66f8d979761c4b)
1*bec4d750Shaad /*	$NetBSD: sharedlib.c,v 1.1.1.2 2009/02/18 11:17:18 haad Exp $	*/
256a34939Shaad 
356a34939Shaad /*
456a34939Shaad  * Copyright (C) 2002-2004 Sistina Software, Inc. All rights reserved.
556a34939Shaad  * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
656a34939Shaad  *
756a34939Shaad  * This file is part of LVM2.
856a34939Shaad  *
956a34939Shaad  * This copyrighted material is made available to anyone wishing to use,
1056a34939Shaad  * modify, copy, or redistribute it subject to the terms and conditions
1156a34939Shaad  * of the GNU Lesser General Public License v.2.1.
1256a34939Shaad  *
1356a34939Shaad  * You should have received a copy of the GNU Lesser General Public License
1456a34939Shaad  * along with this program; if not, write to the Free Software Foundation,
1556a34939Shaad  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1656a34939Shaad  */
1756a34939Shaad 
1856a34939Shaad #include "lib.h"
1956a34939Shaad #include "config.h"
2056a34939Shaad #include "lvm-string.h"
2156a34939Shaad #include "sharedlib.h"
2256a34939Shaad #include "toolcontext.h"
2356a34939Shaad 
2456a34939Shaad #include <limits.h>
2556a34939Shaad #include <sys/stat.h>
2656a34939Shaad #include <dlfcn.h>
2756a34939Shaad 
get_shared_library_path(struct cmd_context * cmd,const char * libname,char * path,size_t path_len)2856a34939Shaad void get_shared_library_path(struct cmd_context *cmd, const char *libname,
2956a34939Shaad 			     char *path, size_t path_len)
3056a34939Shaad {
3156a34939Shaad 	struct stat info;
3256a34939Shaad 	const char *lib_dir;
3356a34939Shaad 
3456a34939Shaad 	/* If libname doesn't begin with '/' then use lib_dir/libname,
3556a34939Shaad 	 * if present */
3656a34939Shaad 	if (libname[0] == '/' ||
3756a34939Shaad 	    !(lib_dir = find_config_tree_str(cmd, "global/library_dir", 0)) ||
3856a34939Shaad 	    (dm_snprintf(path, path_len, "%s/%s", lib_dir,
3956a34939Shaad 			  libname) == -1) || stat(path, &info) == -1)
4056a34939Shaad 		strncpy(path, libname, path_len);
4156a34939Shaad }
4256a34939Shaad 
load_shared_library(struct cmd_context * cmd,const char * libname,const char * desc,int silent)4356a34939Shaad void *load_shared_library(struct cmd_context *cmd, const char *libname,
4456a34939Shaad 			  const char *desc, int silent)
4556a34939Shaad {
4656a34939Shaad 	char path[PATH_MAX];
4756a34939Shaad 	void *library;
4856a34939Shaad 
49*bec4d750Shaad 	if (is_static()) {
5056a34939Shaad 		log_error("Not loading shared %s library %s in static mode.",
5156a34939Shaad 			  desc, libname);
5256a34939Shaad 		return NULL;
5356a34939Shaad 	}
5456a34939Shaad 
5556a34939Shaad 	get_shared_library_path(cmd, libname, path, sizeof(path));
5656a34939Shaad 
5756a34939Shaad 	log_very_verbose("Opening shared %s library %s", desc, path);
5856a34939Shaad 
5956a34939Shaad 	if (!(library = dlopen(path, RTLD_LAZY | RTLD_GLOBAL))) {
6056a34939Shaad 		if (silent && ignorelockingfailure())
6156a34939Shaad 			log_verbose("Unable to open external %s library %s: %s",
6256a34939Shaad 				    desc, path, dlerror());
6356a34939Shaad 		else
6456a34939Shaad 			log_error("Unable to open external %s library %s: %s",
6556a34939Shaad 				  desc, path, dlerror());
6656a34939Shaad 	}
6756a34939Shaad 
6856a34939Shaad 	return library;
6956a34939Shaad }
70