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