xref: /netbsd-src/external/gpl2/lvm2/dist/lib/locking/external_locking.c (revision 7c604eea85b4f330dc75ffe65e947f4d73758aa0)
1*7c604eeaShaad /*	$NetBSD: external_locking.c,v 1.1.1.2 2009/12/02 00:26:24 haad Exp $	*/
256a34939Shaad 
356a34939Shaad /*
456a34939Shaad  * Copyright (C) 2002-2004 Sistina Software, Inc. All rights reserved.
556a34939Shaad  * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
656a34939Shaad  *
756a34939Shaad  * This file is part of LVM2.
856a34939Shaad  *
956a34939Shaad  * This copyrighted material is made available to anyone wishing to use,
1056a34939Shaad  * modify, copy, or redistribute it subject to the terms and conditions
1156a34939Shaad  * of the GNU Lesser General Public License v.2.1.
1256a34939Shaad  *
1356a34939Shaad  * You should have received a copy of the GNU Lesser General Public License
1456a34939Shaad  * along with this program; if not, write to the Free Software Foundation,
1556a34939Shaad  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1656a34939Shaad  */
1756a34939Shaad 
1856a34939Shaad #include "lib.h"
1956a34939Shaad #include "locking_types.h"
2056a34939Shaad #include "defaults.h"
2156a34939Shaad #include "sharedlib.h"
2256a34939Shaad #include "toolcontext.h"
2356a34939Shaad 
2456a34939Shaad static void *_locking_lib = NULL;
2556a34939Shaad static void (*_reset_fn) (void) = NULL;
2656a34939Shaad static void (*_end_fn) (void) = NULL;
2756a34939Shaad static int (*_lock_fn) (struct cmd_context * cmd, const char *resource,
2856a34939Shaad 			uint32_t flags) = NULL;
2956a34939Shaad static int (*_init_fn) (int type, struct config_tree * cft,
3056a34939Shaad 			uint32_t *flags) = NULL;
31*7c604eeaShaad static int (*_lock_query_fn) (const char *resource, int *mode) = NULL;
3256a34939Shaad 
_lock_resource(struct cmd_context * cmd,const char * resource,uint32_t flags)3356a34939Shaad static int _lock_resource(struct cmd_context *cmd, const char *resource,
3456a34939Shaad 			  uint32_t flags)
3556a34939Shaad {
3656a34939Shaad 	if (_lock_fn)
3756a34939Shaad 		return _lock_fn(cmd, resource, flags);
3856a34939Shaad 	else
3956a34939Shaad 		return 0;
4056a34939Shaad }
4156a34939Shaad 
_fin_external_locking(void)4256a34939Shaad static void _fin_external_locking(void)
4356a34939Shaad {
4456a34939Shaad 	if (_end_fn)
4556a34939Shaad 		_end_fn();
4656a34939Shaad 
4756a34939Shaad 	dlclose(_locking_lib);
4856a34939Shaad 
4956a34939Shaad 	_locking_lib = NULL;
5056a34939Shaad 	_init_fn = NULL;
5156a34939Shaad 	_end_fn = NULL;
5256a34939Shaad 	_lock_fn = NULL;
5356a34939Shaad 	_reset_fn = NULL;
5456a34939Shaad }
5556a34939Shaad 
_reset_external_locking(void)5656a34939Shaad static void _reset_external_locking(void)
5756a34939Shaad {
5856a34939Shaad 	if (_reset_fn)
5956a34939Shaad 		_reset_fn();
6056a34939Shaad }
6156a34939Shaad 
init_external_locking(struct locking_type * locking,struct cmd_context * cmd)6256a34939Shaad int init_external_locking(struct locking_type *locking, struct cmd_context *cmd)
6356a34939Shaad {
6456a34939Shaad 	const char *libname;
6556a34939Shaad 
6656a34939Shaad 	if (_locking_lib) {
6756a34939Shaad 		log_error("External locking already initialised");
6856a34939Shaad 		return 1;
6956a34939Shaad 	}
7056a34939Shaad 
7156a34939Shaad 	locking->lock_resource = _lock_resource;
7256a34939Shaad 	locking->fin_locking = _fin_external_locking;
7356a34939Shaad 	locking->reset_locking = _reset_external_locking;
7456a34939Shaad 	locking->flags = 0;
7556a34939Shaad 
7656a34939Shaad 	libname = find_config_tree_str(cmd, "global/locking_library",
7756a34939Shaad 				       DEFAULT_LOCKING_LIB);
7856a34939Shaad 
7956a34939Shaad 	if (!(_locking_lib = load_shared_library(cmd, libname, "locking", 1)))
8056a34939Shaad 		return_0;
8156a34939Shaad 
8256a34939Shaad 	/* Get the functions we need */
8356a34939Shaad 	if (!(_init_fn = dlsym(_locking_lib, "locking_init")) ||
8456a34939Shaad 	    !(_lock_fn = dlsym(_locking_lib, "lock_resource")) ||
8556a34939Shaad 	    !(_reset_fn = dlsym(_locking_lib, "reset_locking")) ||
8656a34939Shaad 	    !(_end_fn = dlsym(_locking_lib, "locking_end"))) {
8756a34939Shaad 		log_error("Shared library %s does not contain locking "
8856a34939Shaad 			  "functions", libname);
8956a34939Shaad 		dlclose(_locking_lib);
9056a34939Shaad 		_locking_lib = NULL;
9156a34939Shaad 		return 0;
9256a34939Shaad 	}
9356a34939Shaad 
94*7c604eeaShaad 	if (!(_lock_query_fn = dlsym(_locking_lib, "query_resource")))
95*7c604eeaShaad 		log_warn("WARNING: %s: _query_resource() missing: "
96*7c604eeaShaad 			 "Using inferior activation method.", libname);
97*7c604eeaShaad 
9856a34939Shaad 	log_verbose("Loaded external locking library %s", libname);
9956a34939Shaad 	return _init_fn(2, cmd->cft, &locking->flags);
10056a34939Shaad }
101