Debian Codegraph @

libmowgli-2/src/libmowgli/module/loader_posix.c

raw
1 /*
2 * libmowgli: A collection of useful routines for programming.
3 * loader_posix.c: Loadable modules for POSIX systems.
4 *
5 * Copyright (c) 2007 William Pitcock <nenolod -at- sacredspiral.co.uk>
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice is present in all copies.
10 *
11 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
12 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
13 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
14 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
15 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
16 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
19 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
20 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21 * POSSIBILITY OF SUCH DAMAGE.
22 */
23
24 #include "mowgli.h"
25
26 #include <dlfcn.h>
27
28 #ifdef __OpenBSD__
29 # define RTLD_NOW RTLD_LAZY
30 #endif
31
32 #ifndef RTLD_LOCAL
33 # define RTLD_LOCAL 0
34 #endif
35
36 mowgli_module_t
37 mowgli_module_open(const char *path)
38 {
39 void *handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
40
41 if (handle == NULL)
42 {
43 mowgli_log("Failed to open %s: %s", path, dlerror());
44 return NULL;
45 }
46
47 return handle;
48 }
49
50 void *
51 mowgli_module_symbol(mowgli_module_t module, const char *symbol)
52 {
53 void *handle;
54
55 return_val_if_fail(module != NULL, NULL);
56
57 handle = dlsym(module, symbol);
58
59 if (handle == NULL)
60 {
61 mowgli_log("Failed to find symbol %s: %s", symbol, dlerror());
62 return NULL;
63 }
64
65 return handle;
66 }
67
68 void
69 mowgli_module_close(mowgli_module_t module)
70 {
71 return_if_fail(module != NULL);
72
73 dlclose(module);
74 }