xref: /netbsd-src/external/gpl2/lvm2/dist/lib/locking/external_locking.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: external_locking.c,v 1.1.1.1 2008/12/22 00:18:04 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 
32 static int _lock_resource(struct cmd_context *cmd, const char *resource,
33 			  uint32_t flags)
34 {
35 	if (_lock_fn)
36 		return _lock_fn(cmd, resource, flags);
37 	else
38 		return 0;
39 }
40 
41 static void _fin_external_locking(void)
42 {
43 	if (_end_fn)
44 		_end_fn();
45 
46 	dlclose(_locking_lib);
47 
48 	_locking_lib = NULL;
49 	_init_fn = NULL;
50 	_end_fn = NULL;
51 	_lock_fn = NULL;
52 	_reset_fn = NULL;
53 }
54 
55 static void _reset_external_locking(void)
56 {
57 	if (_reset_fn)
58 		_reset_fn();
59 }
60 
61 int init_external_locking(struct locking_type *locking, struct cmd_context *cmd)
62 {
63 	const char *libname;
64 
65 	if (_locking_lib) {
66 		log_error("External locking already initialised");
67 		return 1;
68 	}
69 
70 	locking->lock_resource = _lock_resource;
71 	locking->fin_locking = _fin_external_locking;
72 	locking->reset_locking = _reset_external_locking;
73 	locking->flags = 0;
74 
75 	libname = find_config_tree_str(cmd, "global/locking_library",
76 				       DEFAULT_LOCKING_LIB);
77 
78 	if (!(_locking_lib = load_shared_library(cmd, libname, "locking", 1)))
79 		return_0;
80 
81 	/* Get the functions we need */
82 	if (!(_init_fn = dlsym(_locking_lib, "locking_init")) ||
83 	    !(_lock_fn = dlsym(_locking_lib, "lock_resource")) ||
84 	    !(_reset_fn = dlsym(_locking_lib, "reset_locking")) ||
85 	    !(_end_fn = dlsym(_locking_lib, "locking_end"))) {
86 		log_error("Shared library %s does not contain locking "
87 			  "functions", libname);
88 		dlclose(_locking_lib);
89 		_locking_lib = NULL;
90 		return 0;
91 	}
92 
93 	log_verbose("Loaded external locking library %s", libname);
94 	return _init_fn(2, cmd->cft, &locking->flags);
95 }
96