/[pdpsoft]/trunk/grid-mw-security/ees/src/plugin_manager/plugin_manager.c
ViewVC logotype

Contents of /trunk/grid-mw-security/ees/src/plugin_manager/plugin_manager.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 872 - (show annotations) (download) (as text)
Tue Oct 6 15:02:24 2009 UTC (12 years, 11 months ago) by aramv
File MIME type: text/x-chdr
File size: 6782 byte(s)
Renamed some methods
1 #include "plugin_manager.h"
2 #include "_plugin_manager.h"
3
4 lcmaps_plugindl_t * get_running_plugin(){
5 return _running_plugin;
6 }
7
8 void set_running_plugin(lcmaps_plugindl_t * plugin){
9 _running_plugin = plugin;
10 }
11
12 EES_RC addPlugin(int argc, char* argv[]){
13 lcmaps_plugindl_t *last_plugin = NULL, *current_plugin = NULL;
14 /* prepare_plugin returns NULL if plugin failed to load. */
15 if((current_plugin = prepare_plugin(argc, argv)) != NULL){
16 if(plugin_list == NULL){
17 plugin_list = current_plugin;
18 } else {
19 last_plugin = plugin_list;
20 while(last_plugin->next != NULL){
21 last_plugin = last_plugin->next;
22 }
23 last_plugin->next = current_plugin;
24 }
25 print_lcmaps_plugin(LOG_INFO, current_plugin);
26 return EES_SUCCESS;
27 }
28 return EES_FAILURE;
29 }
30
31 /* returns 1 on errors, 0 for success */
32 EES_RC startPluginManager(){
33 plugin_list = NULL;
34 _running_plugin = NULL;
35 return EES_SUCCESS;
36 }
37
38 EES_RC clean_plugin_list(lcmaps_plugindl_t * list){
39 lcmaps_plugindl_t * plugin_entry=NULL;
40 lcmaps_plugindl_t * plugin_next=NULL;
41 int i;
42 plugin_entry=list;
43
44 while(plugin_entry!=NULL){
45 if(plugin_entry->handle != NULL){
46 if(!getenv("VALGRIND")){
47 if((dlclose(plugin_entry->handle))){
48 eef_log(0, "ees.clean_plugin_list(): dlclose error %s\n", dlerror());
49 return EES_FAILURE;
50 }
51 } else {
52 eef_log(LOG_DEBUG, "Running in valgrind, not dlclose'ing plugins\n");
53 }
54 }
55 /*free(plugin_entry->pluginname);*/
56 for(i = 0; i < plugin_entry->init_argc; i++){
57 free(plugin_entry->init_argv[i]);
58 }
59 /*free(plugin_entry->init_argv);*/
60 plugin_next = plugin_entry->next;
61 free(plugin_entry);
62 plugin_entry = plugin_next;
63 }
64 list = plugin_entry = plugin_next = NULL;
65 return EES_SUCCESS;
66 }
67
68 /* Wrapper for dlsym with error logging. Adapted from lcmaps_pluginmanager.c get_procsymbol */
69 lcmaps_proc_t get_procsymbol(void* handle, char* symname){
70 lcmaps_proc_t symhandle;
71 char* errstring;
72
73 /* dlsym returns void* whereas we need to cast to a function pointer.
74 This is a known problem. See: http://www.opengroup.org/onlinepubs/009695399/functions/dlsym.html#tag_03_112_08
75 */
76 symhandle = dlsym(handle,symname);
77 if((errstring = dlerror()) != NULL){
78 eef_log(0, "ees.get_procsymbol(): dlsym error %s\n", errstring);
79 return NULL;
80 }
81 return symhandle;
82 }
83
84 EES_RC print_lcmaps_plugin(int debug_lvl, lcmaps_plugindl_t *plugin){
85 int i = 0;
86 /*eef_log(debug_lvl, "plugin name : %s\n", plugin->pluginname);*/
87 /*eef_log(debug_lvl, "plugin argc : %i\n", plugin->init_argc);*/
88 for(i = 0; i < plugin->init_argc; i++){
89 eef_log(debug_lvl, "plugin argv[%i] : %s\n", i, plugin->init_argv[i]);
90 }
91 return EES_SUCCESS;
92 }
93
94 lcmaps_plugindl_t* get_plugin(char* plugin_name){
95 lcmaps_plugindl_t * current_plugin = plugin_list;
96 while(current_plugin != NULL){
97 print_lcmaps_plugin(LOG_INFO, current_plugin);
98 if(strcmp(current_plugin->pluginname, plugin_name) == 0){
99 return current_plugin;
100 }
101 current_plugin = current_plugin->next;
102 }
103 return NULL;
104 }
105
106
107 lcmaps_plugindl_t * prepare_plugin(int argc, char** argv){
108 lcmaps_plugindl_t * current_plugin = NULL;
109 int i = 0;
110
111 /* current plugin struct */
112 if(!(current_plugin = calloc(1,sizeof(lcmaps_plugindl_t)))){
113 return NULL;
114 } else {
115
116 strncpy(current_plugin->pluginname, argv[0], FILENAME_MAX);
117 current_plugin->init_argc = argc;
118 for(i = 0; i < argc; i++){
119 current_plugin->init_argv[i] = (char*) strdup(argv[i]);
120 }
121
122 /* cleanup */
123 for(i = 0; i < argc; i++){
124 free(argv[i]);
125 }
126 free(argv);
127
128 /* print information */
129 print_lcmaps_plugin(LOG_DEBUG, current_plugin);
130
131 /* return plugin struct */
132 return current_plugin;
133 }
134 return NULL;
135 }
136
137 EES_RC initialize_plugin(lcmaps_plugindl_t* plugin){
138 const char* local_module_dir = get_modules_path();
139 size_t max_plugin_name = FILENAME_MAX - strlen(local_module_dir);
140
141 if(strlen(local_module_dir) >= FILENAME_MAX){
142 return EES_FAILURE;
143 }
144 eef_log(LOG_DEBUG, "Using modules path: %s\n", local_module_dir);
145 memset(abs_plugin_path, 0, FILENAME_MAX);
146
147 /*TODO maak safe(r) */
148 if(!(strncat(abs_plugin_path, local_module_dir, strlen(local_module_dir)) && \
149 strncat(abs_plugin_path, plugin->init_argv[0], max_plugin_name)
150 /*&& strcat(abs_plugin_path, ".mod")*/
151 )){
152 return EES_FAILURE;
153 }
154
155 eef_log(LOG_DEBUG, "Loading plugin %s\n", &abs_plugin_path);
156
157 /* load plugin handle */
158 plugin->handle = dlopen(abs_plugin_path, RTLD_NOW);
159
160 eef_log(LOG_DEBUG,"Opening: %s\n", abs_plugin_path);
161
162 bzero(abs_plugin_path, strlen(abs_plugin_path) + 1);
163
164 if(!plugin->handle){
165 eef_log(LOG_ERR, "%s\n", dlerror());
166 return EES_FAILURE;
167 }
168
169 if(dlerror()){
170 eef_log(LOG_ERR, "%s\n", errno);
171 return EES_FAILURE;
172 }
173
174 plugin->procs[INITPROC] = NULL;
175
176 /* link function pointers */
177 plugin->procs[INITPROC] = get_procsymbol(plugin->handle, "plugin_initialize");
178 if(plugin->procs[INITPROC] == NULL){
179 eef_log(LOG_ERR,"ees.mod-PluginInit(): plugin %s not compliant\n", plugin->pluginname);
180 return EES_FAILURE;
181 }
182
183 plugin->procs[RUNPROC] = get_procsymbol(plugin->handle, "plugin_run");
184 if(plugin->procs[RUNPROC] == NULL){
185 eef_log(LOG_ERR, "ees.runPlugin\n");
186 return EES_FAILURE;
187 }
188
189 plugin->procs[TERMPROC] = get_procsymbol(plugin->handle, "plugin_terminate");
190 if(plugin->procs[TERMPROC] == NULL){
191 eef_log(LOG_ERR, "ees.termPlugin\n");
192 return EES_FAILURE;
193 }
194
195 set_running_plugin(plugin);
196
197 init_fnc_ptr = plugin->procs[INITPROC];
198 /* call function pointer */
199 if(init_fnc_ptr(plugin->init_argc, plugin->init_argv) == EES_PL_SUCCESS){
200 return EES_SUCCESS;
201 }
202
203 return EES_FAILURE;
204 }
205
206 EES_RC initializePlugins(void){
207 lcmaps_plugindl_t *node = plugin_list;
208 while(node != NULL){
209 if(initialize_plugin(node) == EES_FAILURE){
210 return EES_FAILURE;
211 }
212 node = node->next;
213 }
214 return EES_SUCCESS;
215 }
216
217 EES_RC runPlugin(char* plugin_name){
218 lcmaps_plugindl_t * current_plugin;
219 current_plugin = get_plugin(plugin_name);
220 run_fnc_ptr = current_plugin->procs[RUNPROC];
221 eef_log(LOG_DEBUG, "Run method for %s linked at %p\n", plugin_name, run_fnc_ptr);
222 set_running_plugin(current_plugin);
223 run_fnc_ptr();
224 return EES_SUCCESS;
225 }
226
227 /* basically hetzelfde als de runPlugin functie */
228 EES_RC termPlugin(char* plugin_name){
229 lcmaps_plugindl_t * current_plugin;
230 current_plugin = get_plugin(plugin_name);
231 term_fnc_ptr = current_plugin->procs[TERMPROC];
232 eef_log(LOG_DEBUG, "Run method for %s linked at %p\n", plugin_name, term_fnc_ptr);
233 set_running_plugin(current_plugin);
234 term_fnc_ptr();
235 return EES_SUCCESS;
236 }
237
238 EES_RC stopPluginManager(){
239 return clean_plugin_list(plugin_list);
240 }

grid.support@nikhef.nl
ViewVC Help
Powered by ViewVC 1.1.28