xref: /dflybsd-src/contrib/lvm2/dist/lib/misc/sharedlib.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
186d7f5d3SJohn Marino /*	$NetBSD: sharedlib.c,v 1.1.1.2 2009/02/18 11:17:18 haad Exp $	*/
286d7f5d3SJohn Marino 
386d7f5d3SJohn Marino /*
486d7f5d3SJohn Marino  * Copyright (C) 2002-2004 Sistina Software, Inc. All rights reserved.
586d7f5d3SJohn Marino  * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
686d7f5d3SJohn Marino  *
786d7f5d3SJohn Marino  * This file is part of LVM2.
886d7f5d3SJohn Marino  *
986d7f5d3SJohn Marino  * This copyrighted material is made available to anyone wishing to use,
1086d7f5d3SJohn Marino  * modify, copy, or redistribute it subject to the terms and conditions
1186d7f5d3SJohn Marino  * of the GNU Lesser General Public License v.2.1.
1286d7f5d3SJohn Marino  *
1386d7f5d3SJohn Marino  * You should have received a copy of the GNU Lesser General Public License
1486d7f5d3SJohn Marino  * along with this program; if not, write to the Free Software Foundation,
1586d7f5d3SJohn Marino  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1686d7f5d3SJohn Marino  */
1786d7f5d3SJohn Marino 
1886d7f5d3SJohn Marino #include "lib.h"
1986d7f5d3SJohn Marino #include "config.h"
2086d7f5d3SJohn Marino #include "lvm-string.h"
2186d7f5d3SJohn Marino #include "sharedlib.h"
2286d7f5d3SJohn Marino #include "toolcontext.h"
2386d7f5d3SJohn Marino 
2486d7f5d3SJohn Marino #include <limits.h>
2586d7f5d3SJohn Marino #include <sys/stat.h>
2686d7f5d3SJohn Marino #include <dlfcn.h>
2786d7f5d3SJohn Marino 
get_shared_library_path(struct cmd_context * cmd,const char * libname,char * path,size_t path_len)2886d7f5d3SJohn Marino void get_shared_library_path(struct cmd_context *cmd, const char *libname,
2986d7f5d3SJohn Marino 			     char *path, size_t path_len)
3086d7f5d3SJohn Marino {
3186d7f5d3SJohn Marino 	struct stat info;
3286d7f5d3SJohn Marino 	const char *lib_dir;
3386d7f5d3SJohn Marino 
3486d7f5d3SJohn Marino 	/* If libname doesn't begin with '/' then use lib_dir/libname,
3586d7f5d3SJohn Marino 	 * if present */
3686d7f5d3SJohn Marino 	if (libname[0] == '/' ||
3786d7f5d3SJohn Marino 	    !(lib_dir = find_config_tree_str(cmd, "global/library_dir", 0)) ||
3886d7f5d3SJohn Marino 	    (dm_snprintf(path, path_len, "%s/%s", lib_dir,
3986d7f5d3SJohn Marino 			  libname) == -1) || stat(path, &info) == -1)
4086d7f5d3SJohn Marino 		strncpy(path, libname, path_len);
4186d7f5d3SJohn Marino }
4286d7f5d3SJohn Marino 
load_shared_library(struct cmd_context * cmd,const char * libname,const char * desc,int silent)4386d7f5d3SJohn Marino void *load_shared_library(struct cmd_context *cmd, const char *libname,
4486d7f5d3SJohn Marino 			  const char *desc, int silent)
4586d7f5d3SJohn Marino {
4686d7f5d3SJohn Marino 	char path[PATH_MAX];
4786d7f5d3SJohn Marino 	void *library;
4886d7f5d3SJohn Marino 
4986d7f5d3SJohn Marino 	if (is_static()) {
5086d7f5d3SJohn Marino 		log_error("Not loading shared %s library %s in static mode.",
5186d7f5d3SJohn Marino 			  desc, libname);
5286d7f5d3SJohn Marino 		return NULL;
5386d7f5d3SJohn Marino 	}
5486d7f5d3SJohn Marino 
5586d7f5d3SJohn Marino 	get_shared_library_path(cmd, libname, path, sizeof(path));
5686d7f5d3SJohn Marino 
5786d7f5d3SJohn Marino 	log_very_verbose("Opening shared %s library %s", desc, path);
5886d7f5d3SJohn Marino 
5986d7f5d3SJohn Marino 	if (!(library = dlopen(path, RTLD_LAZY | RTLD_GLOBAL))) {
6086d7f5d3SJohn Marino 		if (silent && ignorelockingfailure())
6186d7f5d3SJohn Marino 			log_verbose("Unable to open external %s library %s: %s",
6286d7f5d3SJohn Marino 				    desc, path, dlerror());
6386d7f5d3SJohn Marino 		else
6486d7f5d3SJohn Marino 			log_error("Unable to open external %s library %s: %s",
6586d7f5d3SJohn Marino 				  desc, path, dlerror());
6686d7f5d3SJohn Marino 	}
6786d7f5d3SJohn Marino 
6886d7f5d3SJohn Marino 	return library;
6986d7f5d3SJohn Marino }
70