1 /*	$NetBSD: external_locking.c,v 1.1.1.2 2009/12/02 00:26:24 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 "locking_types.h"
20 #include "defaults.h"
21 #include "sharedlib.h"
22 #include "toolcontext.h"
23 
24 static void *_locking_lib = NULL;
25 static void (*_reset_fn) (void) = NULL;
26 static void (*_end_fn) (void) = NULL;
27 static int (*_lock_fn) (struct cmd_context * cmd, const char *resource,
28 			uint32_t flags) = NULL;
29 static int (*_init_fn) (int type, struct config_tree * cft,
30 			uint32_t *flags) = NULL;
31 static int (*_lock_query_fn) (const char *resource, int *mode) = NULL;
32 
33 static int _lock_resource(struct cmd_context *cmd, const char *resource,
34 			  uint32_t flags)
35 {
36 	if (_lock_fn)
37 		return _lock_fn(cmd, resource, flags);
38 	else
39 		return 0;
40 }
41 
42 static void _fin_external_locking(void)
43 {
44 	if (_end_fn)
45 		_end_fn();
46 
47 	dlclose(_locking_lib);
48 
49 	_locking_lib = NULL;
50 	_init_fn = NULL;
51 	_end_fn = NULL;
52 	_lock_fn = NULL;
53 	_reset_fn = NULL;
54 }
55 
56 static void _reset_external_locking(void)
57 {
58 	if (_reset_fn)
59 		_reset_fn();
60 }
61 
62 int init_external_locking(struct locking_type *locking, struct cmd_context *cmd)
63 {
64 	const char *libname;
65 
66 	if (_locking_lib) {
67 		log_error("External locking already initialised");
68 		return 1;
69 	}
70 
71 	locking->lock_resource = _lock_resource;
72 	locking->fin_locking = _fin_external_locking;
73 	locking->reset_locking = _reset_external_locking;
74 	locking->flags = 0;
75 
76 	libname = find_config_tree_str(cmd, "global/locking_library",
77 				       DEFAULT_LOCKING_LIB);
78 
79 	if (!(_locking_lib = load_shared_library(cmd, libname, "locking", 1)))
80 		return_0;
81 
82 	/* Get the functions we need */
83 	if (!(_init_fn = dlsym(_locking_lib, "locking_init")) ||
84 	    !(_lock_fn = dlsym(_locking_lib, "lock_resource")) ||
85 	    !(_reset_fn = dlsym(_locking_lib, "reset_locking")) ||
86 	    !(_end_fn = dlsym(_locking_lib, "locking_end"))) {
87 		log_error("Shared library %s does not contain locking "
88 			  "functions", libname);
89 		dlclose(_locking_lib);
90 		_locking_lib = NULL;
91 		return 0;
92 	}
93 
94 	if (!(_lock_query_fn = dlsym(_locking_lib, "query_resource")))
95 		log_warn("WARNING: %s: _query_resource() missing: "
96 			 "Using inferior activation method.", libname);
97 
98 	log_verbose("Loaded external locking library %s", libname);
99 	return _init_fn(2, cmd->cft, &locking->flags);
100 }
101