xref: /dflybsd-src/contrib/lvm2/dist/lib/misc/sharedlib.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
1*86d7f5d3SJohn Marino /*	$NetBSD: sharedlib.c,v 1.1.1.2 2009/02/18 11:17:18 haad Exp $	*/
2*86d7f5d3SJohn Marino 
3*86d7f5d3SJohn Marino /*
4*86d7f5d3SJohn Marino  * Copyright (C) 2002-2004 Sistina Software, Inc. All rights reserved.
5*86d7f5d3SJohn Marino  * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
6*86d7f5d3SJohn Marino  *
7*86d7f5d3SJohn Marino  * This file is part of LVM2.
8*86d7f5d3SJohn Marino  *
9*86d7f5d3SJohn Marino  * This copyrighted material is made available to anyone wishing to use,
10*86d7f5d3SJohn Marino  * modify, copy, or redistribute it subject to the terms and conditions
11*86d7f5d3SJohn Marino  * of the GNU Lesser General Public License v.2.1.
12*86d7f5d3SJohn Marino  *
13*86d7f5d3SJohn Marino  * You should have received a copy of the GNU Lesser General Public License
14*86d7f5d3SJohn Marino  * along with this program; if not, write to the Free Software Foundation,
15*86d7f5d3SJohn Marino  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16*86d7f5d3SJohn Marino  */
17*86d7f5d3SJohn Marino 
18*86d7f5d3SJohn Marino #include "lib.h"
19*86d7f5d3SJohn Marino #include "config.h"
20*86d7f5d3SJohn Marino #include "lvm-string.h"
21*86d7f5d3SJohn Marino #include "sharedlib.h"
22*86d7f5d3SJohn Marino #include "toolcontext.h"
23*86d7f5d3SJohn Marino 
24*86d7f5d3SJohn Marino #include <limits.h>
25*86d7f5d3SJohn Marino #include <sys/stat.h>
26*86d7f5d3SJohn Marino #include <dlfcn.h>
27*86d7f5d3SJohn Marino 
get_shared_library_path(struct cmd_context * cmd,const char * libname,char * path,size_t path_len)28*86d7f5d3SJohn Marino void get_shared_library_path(struct cmd_context *cmd, const char *libname,
29*86d7f5d3SJohn Marino 			     char *path, size_t path_len)
30*86d7f5d3SJohn Marino {
31*86d7f5d3SJohn Marino 	struct stat info;
32*86d7f5d3SJohn Marino 	const char *lib_dir;
33*86d7f5d3SJohn Marino 
34*86d7f5d3SJohn Marino 	/* If libname doesn't begin with '/' then use lib_dir/libname,
35*86d7f5d3SJohn Marino 	 * if present */
36*86d7f5d3SJohn Marino 	if (libname[0] == '/' ||
37*86d7f5d3SJohn Marino 	    !(lib_dir = find_config_tree_str(cmd, "global/library_dir", 0)) ||
38*86d7f5d3SJohn Marino 	    (dm_snprintf(path, path_len, "%s/%s", lib_dir,
39*86d7f5d3SJohn Marino 			  libname) == -1) || stat(path, &info) == -1)
40*86d7f5d3SJohn Marino 		strncpy(path, libname, path_len);
41*86d7f5d3SJohn Marino }
42*86d7f5d3SJohn Marino 
load_shared_library(struct cmd_context * cmd,const char * libname,const char * desc,int silent)43*86d7f5d3SJohn Marino void *load_shared_library(struct cmd_context *cmd, const char *libname,
44*86d7f5d3SJohn Marino 			  const char *desc, int silent)
45*86d7f5d3SJohn Marino {
46*86d7f5d3SJohn Marino 	char path[PATH_MAX];
47*86d7f5d3SJohn Marino 	void *library;
48*86d7f5d3SJohn Marino 
49*86d7f5d3SJohn Marino 	if (is_static()) {
50*86d7f5d3SJohn Marino 		log_error("Not loading shared %s library %s in static mode.",
51*86d7f5d3SJohn Marino 			  desc, libname);
52*86d7f5d3SJohn Marino 		return NULL;
53*86d7f5d3SJohn Marino 	}
54*86d7f5d3SJohn Marino 
55*86d7f5d3SJohn Marino 	get_shared_library_path(cmd, libname, path, sizeof(path));
56*86d7f5d3SJohn Marino 
57*86d7f5d3SJohn Marino 	log_very_verbose("Opening shared %s library %s", desc, path);
58*86d7f5d3SJohn Marino 
59*86d7f5d3SJohn Marino 	if (!(library = dlopen(path, RTLD_LAZY | RTLD_GLOBAL))) {
60*86d7f5d3SJohn Marino 		if (silent && ignorelockingfailure())
61*86d7f5d3SJohn Marino 			log_verbose("Unable to open external %s library %s: %s",
62*86d7f5d3SJohn Marino 				    desc, path, dlerror());
63*86d7f5d3SJohn Marino 		else
64*86d7f5d3SJohn Marino 			log_error("Unable to open external %s library %s: %s",
65*86d7f5d3SJohn Marino 				  desc, path, dlerror());
66*86d7f5d3SJohn Marino 	}
67*86d7f5d3SJohn Marino 
68*86d7f5d3SJohn Marino 	return library;
69*86d7f5d3SJohn Marino }
70