summaryrefslogtreecommitdiffstats
path: root/src/lib/plugin.c
blob: 09df8de3f03052b420c63247a9c3a28d723f518d (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
/*
 * (c) Copyright 2016 Olliver Schinagl
 * Author: Olliver Schinagl <oliver@schinagl.nl>
 *
 * SPDX-License-Identifier:	AGPL-3.0+
 */

#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>

#include <engagement/plugin.h>

struct plugin *plugin_load(const char *path)
{
	struct plugin *plugin;
	void *handle;

	handle = dlopen(path, RTLD_LAZY); /* TODO check if RTLD_LAZY is what we want */
	if (!handle) {
		// TODO EINA_LOG
		fprintf(stderr, "dlopen failed: %s\n", dlerror());
		return NULL;
	}

	/* TODO use macro's to allow for static inclusion */
	/* TODO use dlvsym() if available */
	plugin = (struct plugin *)dlsym(handle, PLUGIN_SYMBOL);
	if(!plugin) {
		// TODO EINA_LOG
		fprintf(stderr, "dlsym failed%s\n", dlerror());
		return NULL;
	}

	if (plugin->magic != PLUGIN_MAGIC) {
		// TODO EINA_LOG
		fprintf(stderr, "Failed to init plugin, magic mismatch, plugin corrupt?\n");
		return NULL;
	}
	plugin->_handle = handle;

	if (plugin->load)
		plugin->load();

	return plugin;
}

int plugin_unload(struct plugin *plugin)
{
	int error;

	if (!plugin) {
		// TODO EINA_LOG
		fprintf(stderr, "Cannot unload plugin %p.\n", plugin);
		return -EFAULT;
	}
	
	if (plugin->unload)
		plugin->unload();

	if (!plugin->_handle) {
		// TODO EINA_LOG
		fprintf(stderr, "Error, no handle for plugin %s\n",
			       plugin->info.name);
		return -EFAULT;
	}
	error = dlclose(plugin->_handle);
	if (error)
		// TODO EINA_LOG
		fprintf(stderr, "Failed to close %s\n", dlerror());

	return error;
}