summaryrefslogtreecommitdiff
path: root/src/library/loader.c
blob: 1f8ce20ab6713445a71d711066236ce058902d64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
    Copyright (C) 2010 Andreas Baumann <abaumann@yahoo.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "library/loader.h"
#include "port/sys_internal.h"
#include "port/unused.h"

#include "port/stdlib.h"		/* for malloc, free */

#ifdef HAVE_DLFCN
#include <dlfcn.h>                      /* for dlopen. dlclose functions */
#endif

struct wolf_library_t {
	void *handle;			/**< the OS handle for the library */
};

wolf_library_p wolf_library_load( const char *name, wolf_error_t *error ) {
	wolf_library_p l;
	int flags = 0;

	l = (struct wolf_library_t *)malloc( sizeof ( struct wolf_library_t ) );
	if( l == NULL ) {
		*error = WOLF_ERR_OUT_OF_MEMORY;
		return NULL;
	}

#if defined HAVE_DLFCN
	/* TODO: Apache has a flags variable and a direct parameter version, find out why..
	 * also make up our bind how many flags we should "leak" to the application layer
	 */
	flags = RTLD_NOW | RTLD_LOCAL;

	l->handle = dlopen( name, flags );
	if( l->handle == NULL ) {
		*error = WOLF_ERR_INTERNAL;
		return NULL;
	}
#else
#error Not using DLFCN as shared loader. Port first!
#endif

	*error = WOLF_OK;
	return l;
}

wolf_error_t wolf_library_unload( wolf_library_p l ) {
	int res = 0;

	if( l == NULL || l->handle == NULL ) {
		return WOLF_ERR_INVALID_STATE;
	}

	res = dlclose( l->handle );
	if( res != 0 ) {
		return WOLF_ERR_INVALID_STATE;
	}

	free( l );
	l = NULL;

	return WOLF_OK;
}