Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2009 Philippe Proulx, École Polytechnique de Montréal |
3 |
* Michael Sills-Lavoie, École Polytechnique de Montréal |
4 |
* and others. All rights reserved. |
5 |
* This program and the accompanying materials |
6 |
* are made available under the terms of the Eclipse Public License v1.0 |
7 |
* and Eclipse Distribution License v1.0 which accompany this distribution. |
8 |
* The Eclipse Public License is available at |
9 |
* http://www.eclipse.org/legal/epl-v10.html |
10 |
* and the Eclipse Distribution License is available at |
11 |
* http://www.eclipse.org/org/documents/edl-v10.php. |
12 |
* |
13 |
* Contributors: |
14 |
* Philippe Proulx - initial plugins system |
15 |
*******************************************************************************/ |
16 |
|
17 |
/* |
18 |
* Expression evaluation service. |
19 |
*/ |
20 |
|
21 |
#include "config.h" |
22 |
|
23 |
#if EN_Plugins |
24 |
|
25 |
#ifndef _GNU_SOURCE |
26 |
#define _GNU_SOURCE |
27 |
#endif /* _GNU_SOURCE */ |
28 |
|
29 |
#include <stdio.h> |
30 |
#include <stdlib.h> |
31 |
#include <string.h> |
32 |
#include <sys/types.h> |
33 |
#include <dirent.h> |
34 |
#include <dlfcn.h> |
35 |
|
36 |
#include "trace.h" |
37 |
#include "plugins.h" |
38 |
|
39 |
static void ** plugins_handles = NULL; |
40 |
static size_t plugins_count = 0; |
41 |
|
42 |
static inline int plugins_ext_is(const char * ext, const char * filename) { |
43 |
int ret = 0; |
44 |
const char* real_ext = strrchr(filename, '.'); |
45 |
|
46 |
if (real_ext != NULL) { |
47 |
ret = !strcmp(real_ext + 1, ext); |
48 |
} |
49 |
|
50 |
return ret; |
51 |
} |
52 |
|
53 |
int plugins_load(Protocol * proto, TCFBroadcastGroup * bcg, TCFSuspendGroup * spg) { |
54 |
int ret = 0; |
55 |
char* cur_plugin_path; |
56 |
struct dirent* dirent; |
57 |
DIR* dir; |
58 |
|
59 |
dir = opendir(QUOTE(PATH_Plugins)); |
60 |
if (!dir) { |
61 |
trace(LOG_ALWAYS, "plugins error: failed opening plugins directory \"" QUOTE(PATH_Plugins) "\""); |
62 |
return -1; |
63 |
} |
64 |
while (dirent = readdir(dir)) { |
65 |
if (!strcmp(dirent->d_name, ".") || !strcmp(dirent->d_name, "..")) { |
66 |
continue; |
67 |
} |
68 |
if (!plugins_ext_is(PLUGINS_DEF_EXT, dirent->d_name) || dirent->d_type == DT_DIR) { |
69 |
continue; |
70 |
} |
71 |
if (asprintf(&cur_plugin_path, QUOTE(PATH_Plugins) "/%s", dirent->d_name) == -1) { |
72 |
trace(LOG_ALWAYS, "plugins error: `asprintf' failed for plugin \"%s\"", dirent->d_name); |
73 |
return -1; |
74 |
} |
75 |
if (plugin_init(cur_plugin_path, proto, bcg, spg)) { |
76 |
trace(LOG_ALWAYS, "plugins error: unable to start plugin \"%s\"", cur_plugin_path); |
77 |
ret = -1; |
78 |
goto error_free; |
79 |
} |
80 |
} |
81 |
error_free: |
82 |
free(cur_plugin_path); |
83 |
|
84 |
return ret; |
85 |
} |
86 |
|
87 |
int plugin_init(const char * name, Protocol * proto, TCFBroadcastGroup * bcg, TCFSuspendGroup * spg) { |
88 |
void* handle; |
89 |
void (* init)(Protocol *, TCFBroadcastGroup *, TCFSuspendGroup *); |
90 |
char* error; |
91 |
|
92 |
/* Plugin loading: */ |
93 |
trace(LOG_ALWAYS, "loading plugin \"%s\"", name); |
94 |
handle = dlopen(name, RTLD_LAZY); |
95 |
if (!handle) { |
96 |
trace(LOG_ALWAYS, "plugins error: \"%s\"", dlerror()); |
97 |
return -1; |
98 |
} |
99 |
|
100 |
/* Plugin initialization: */ |
101 |
init = dlsym(handle, "tcf_init_plugin"); |
102 |
if ((error = dlerror()) != NULL) { |
103 |
trace(LOG_ALWAYS, "plugins error: \"%s\"", error); |
104 |
return -1; |
105 |
} |
106 |
trace(LOG_ALWAYS, "initializing plugin \"%s\"", name); |
107 |
init(proto, bcg, spg); |
108 |
|
109 |
/* Handles table update: */ |
110 |
plugins_handles = (void **) realloc(plugins_handles, ++plugins_count * sizeof(void *)); |
111 |
plugins_handles[plugins_count - 1] = handle; |
112 |
|
113 |
return 0; |
114 |
} |
115 |
|
116 |
int plugins_destroy(void) { |
117 |
if (plugins_handles == NULL) { |
118 |
return 0; |
119 |
} |
120 |
size_t i; |
121 |
|
122 |
for (i = 0; i < plugins_count; ++i) { |
123 |
if (dlclose(plugins_handles[i])) { |
124 |
trace(LOG_ALWAYS, "plugins error: \"%s\"", dlerror()); |
125 |
} |
126 |
} |
127 |
free(plugins_handles); |
128 |
|
129 |
return 0; |
130 |
} |
131 |
|
132 |
#endif /* if EN_Plugins */ |
133 |
|