Imported Upstream version 2.02.79
[platform/upstream/device-mapper.git] / lib / locking / external_locking.c
1 /*
2  * Copyright (C) 2002-2004 Sistina Software, Inc. All rights reserved.
3  * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
4  *
5  * This file is part of LVM2.
6  *
7  * This copyrighted material is made available to anyone wishing to use,
8  * modify, copy, or redistribute it subject to the terms and conditions
9  * of the GNU Lesser General Public License v.2.1.
10  *
11  * You should have received a copy of the GNU Lesser General Public License
12  * along with this program; if not, write to the Free Software Foundation,
13  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
14  */
15
16 #include "lib.h"
17 #include "locking_types.h"
18 #include "defaults.h"
19 #include "sharedlib.h"
20 #include "toolcontext.h"
21
22 static void *_locking_lib = NULL;
23 static void (*_reset_fn) (void) = NULL;
24 static void (*_end_fn) (void) = NULL;
25 static int (*_lock_fn) (struct cmd_context * cmd, const char *resource,
26                         uint32_t flags) = NULL;
27 static int (*_init_fn) (int type, struct config_tree * cft,
28                         uint32_t *flags) = NULL;
29 static int (*_lock_query_fn) (const char *resource, int *mode) = NULL;
30
31 static int _lock_resource(struct cmd_context *cmd, const char *resource,
32                           uint32_t flags)
33 {
34         if (_lock_fn)
35                 return _lock_fn(cmd, resource, flags);
36         else
37                 return 0;
38 }
39
40 static void _fin_external_locking(void)
41 {
42         if (_end_fn)
43                 _end_fn();
44
45         dlclose(_locking_lib);
46
47         _locking_lib = NULL;
48         _init_fn = NULL;
49         _end_fn = NULL;
50         _lock_fn = NULL;
51         _reset_fn = NULL;
52 }
53
54 static void _reset_external_locking(void)
55 {
56         if (_reset_fn)
57                 _reset_fn();
58 }
59
60 int init_external_locking(struct locking_type *locking, struct cmd_context *cmd)
61 {
62         const char *libname;
63
64         if (_locking_lib) {
65                 log_error("External locking already initialised");
66                 return 1;
67         }
68
69         locking->lock_resource = _lock_resource;
70         locking->fin_locking = _fin_external_locking;
71         locking->reset_locking = _reset_external_locking;
72         locking->flags = 0;
73
74         libname = find_config_tree_str(cmd, "global/locking_library",
75                                        DEFAULT_LOCKING_LIB);
76
77         if (!(_locking_lib = load_shared_library(cmd, libname, "locking", 1)))
78                 return_0;
79
80         /* Get the functions we need */
81         if (!(_init_fn = dlsym(_locking_lib, "locking_init")) ||
82             !(_lock_fn = dlsym(_locking_lib, "lock_resource")) ||
83             !(_reset_fn = dlsym(_locking_lib, "reset_locking")) ||
84             !(_end_fn = dlsym(_locking_lib, "locking_end"))) {
85                 log_error("Shared library %s does not contain locking "
86                           "functions", libname);
87                 dlclose(_locking_lib);
88                 _locking_lib = NULL;
89                 return 0;
90         }
91
92         if (!(_lock_query_fn = dlsym(_locking_lib, "query_resource")))
93                 log_warn("WARNING: %s: _query_resource() missing: "
94                          "Using inferior activation method.", libname);
95
96         log_verbose("Loaded external locking library %s", libname);
97         return _init_fn(2, cmd->cft, &locking->flags);
98 }