/[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 589 - (show annotations) (download) (as text)
Wed Jul 22 10:16:59 2009 UTC (12 years, 6 months ago) by aramv
File MIME type: text/x-chdr
File size: 6030 byte(s)
Changed flow of code a bit. Checking return values etc
1 #include "plugin_manager.h"
2 #include "_plugin_manager.h"
3 #include <string.h>
4 #include <stdio.h>
5
6 lcmaps_plugindl_t * get_running_plugin(){
7 return _running_plugin;
8 }
9
10 void set_running_plugin(lcmaps_plugindl_t * plugin){
11 _running_plugin = plugin;
12 }
13
14 NG_RC add_plugin(int argc, char* argv[]){
15 lcmaps_plugindl_t *last_plugin = NULL, *current_plugin = NULL;
16 // PluginInit returns NULL if plugin failed to load.
17 if((current_plugin = PluginInit(argc, argv)) != NULL){
18 if(plugin_list == NULL){
19 plugin_list = current_plugin;
20 } else {
21 last_plugin = plugin_list;
22 while(last_plugin->next != NULL){
23 last_plugin = last_plugin->next;
24 }
25 last_plugin->next = current_plugin;
26 }
27 print_lcmaps_plugin(0, current_plugin);
28 return NG_SUCCESS;
29 }
30 return NG_FAILURE;
31 }
32
33 /* returns 1 on errors, 0 for success */
34 NG_RC startPluginManager(){
35 plugin_list = NULL;
36 _running_plugin = NULL;
37 return NG_SUCCESS;
38 }
39
40 NG_RC clean_plugin_list(lcmaps_plugindl_t * list){
41 lcmaps_plugindl_t * plugin_entry=NULL;
42 lcmaps_plugindl_t * plugin_next=NULL;
43 plugin_entry=list;
44 int i;
45
46 while(plugin_entry!=NULL){
47 if(plugin_entry->handle != NULL){
48 if((dlclose(plugin_entry->handle))){
49 ng_log(0, "ees.clean_plugin_list(): dlclose error %s\n", dlerror());
50 return NG_FAILURE;
51 }
52 }
53 /*free(plugin_entry->pluginname);*/
54 for(i = 0; i < plugin_entry->init_argc; i++){
55 free(plugin_entry->init_argv[i]);
56 }
57 /*free(plugin_entry->init_argv);*/
58 plugin_next = plugin_entry->next;
59 free(plugin_entry);
60 plugin_entry = plugin_next;
61 }
62 list = plugin_entry = plugin_next = NULL;
63 return NG_SUCCESS;
64 }
65
66 /* Wrapper for dlsym with error logging. Adapted from lcmaps_pluginmanager.c get_procsymbol */
67 lcmaps_proc_t get_procsymbol(void* handle, char* symname){
68 lcmaps_proc_t symhandle;
69 symhandle = dlsym(handle,symname);
70 char* errstring;
71 if((errstring = dlerror()) != NULL){
72 ng_log(0, "ees.get_procsymbol(): dlsym error %s\n", errstring);
73 return NULL;
74 }
75 return symhandle;
76 }
77
78 lcmaps_plugindl_t * PluginInit(int argc, char** argv){
79 int i = 0;
80 int max_args = LCMAPS_MAXARGS;
81 memset(abs_plugin_path, 0, LCMAPS_MAXPATHLEN);
82 lcmaps_plugindl_t * current_plugin = NULL;
83
84 /*TODO maak safe(r) */
85 if(!(strcat(abs_plugin_path, MODULE_DIR) && \
86 strcat(abs_plugin_path, argv[0])
87 /*&& strcat(abs_plugin_path, ".mod")*/
88 )){
89 goto fail_startPluginManager;
90 }
91
92 /* current plugin struct */
93 if(!(current_plugin = calloc(1,sizeof(lcmaps_plugindl_t)))){
94 goto fail_startPluginManager;
95 } else {
96
97 ng_log(LOG_ERR, "Loading plugin %s\n", &abs_plugin_path);
98
99 /* load plugin handle */
100 current_plugin->handle = dlopen(abs_plugin_path, RTLD_NOW);
101
102 /* this is a bit silly */
103
104 if(argc < LCMAPS_MAXARGS){
105 max_args = argc;
106 } else {
107 max_args = LCMAPS_MAXARGS;
108 }
109
110 strncpy(current_plugin->pluginname, argv[0], LCMAPS_MAXPATHLEN);
111 /*current_plugin->pluginname = argv[0];*/
112 /**(current_plugin->pluginargs) = *argv[0];*/
113
114 current_plugin->init_argc = max_args;
115 for(i = 0; i < max_args; i++){
116 current_plugin->init_argv[i] = strdup(argv[i]);
117 }
118 for(i = 0; i < argc; i++){
119 free(argv[i]);
120 }
121 free(argv);
122
123 if(!current_plugin->handle){
124 ng_log(LOG_ERR, "%s", dlerror());
125 goto fail_startPluginManager;
126 }
127
128 if(dlerror()){
129 ng_log(LOG_ERR, "%s", errno);
130 goto fail_startPluginManager;
131 }
132
133 current_plugin->procs[INITPROC] = NULL;
134
135 /* link function pointers */
136 current_plugin->procs[INITPROC] = get_procsymbol(current_plugin->handle, "plugin_initialize");
137 if(current_plugin->procs[INITPROC] == NULL){
138 ng_log(LOG_ERR,"ees.mod-PluginInit(): plugin %s not compliant\n", current_plugin->pluginname);
139 goto fail_startPluginManager;
140 }
141
142 current_plugin->procs[RUNPROC] = get_procsymbol(current_plugin->handle, "plugin_run");
143 if(current_plugin->procs[RUNPROC] == NULL){
144 ng_log(LOG_ERR, "ees.runPlugin\n");
145 goto fail_startPluginManager;
146 }
147
148 current_plugin->procs[TERMPROC] = get_procsymbol(current_plugin->handle, "plugin_terminate");
149 if(current_plugin->procs[TERMPROC] == NULL){
150 ng_log(LOG_ERR, "ees.termPlugin\n");
151 goto fail_startPluginManager;
152 }
153
154 set_running_plugin(current_plugin);
155
156 init_fnc_ptr = current_plugin->procs[INITPROC];
157
158 /* call function pointer */
159 if(init_fnc_ptr(current_plugin->init_argc, current_plugin->init_argv) == NG_PL_FAILURE){
160 goto fail_startPluginManager;
161 }
162
163 print_lcmaps_plugin(0, current_plugin);
164
165 /* return plugin struct */
166 return current_plugin;
167 }
168
169 fail_startPluginManager:
170 clean_plugin_list(current_plugin);
171 return NULL;
172 }
173
174 NG_RC print_lcmaps_plugin(int debug_lvl, lcmaps_plugindl_t *plugin){
175 ng_log(debug_lvl, "plugin name : %s\n", plugin->pluginname);
176 return NG_SUCCESS;
177 }
178
179 lcmaps_plugindl_t* get_plugin(char* plugin_name){
180 lcmaps_plugindl_t * current_plugin = plugin_list;
181 while(current_plugin != NULL){
182 print_lcmaps_plugin(LOG_DEBUG, current_plugin);
183 if(strcmp(current_plugin->pluginname, plugin_name) == 0){
184 return current_plugin;
185 }
186 current_plugin = current_plugin->next;
187 }
188 return NULL;
189 }
190
191 NG_RC runPlugin(char* plugin_name){
192 lcmaps_plugindl_t * current_plugin;
193 current_plugin = get_plugin(plugin_name);
194 run_fnc_ptr = current_plugin->procs[RUNPROC];
195 ng_log(LOG_DEBUG, "Run method for %s linked at %p\n", plugin_name, run_fnc_ptr);
196 set_running_plugin(current_plugin);
197 run_fnc_ptr();
198 return NG_SUCCESS;
199 }
200
201 /* basically hetzelfde als de runPlugin functie */
202 NG_RC termPlugin(char* plugin_name){
203 lcmaps_plugindl_t * current_plugin;
204 current_plugin = get_plugin(plugin_name);
205 term_fnc_ptr = current_plugin->procs[TERMPROC];
206 ng_log(LOG_DEBUG, "Run method for %s linked at %p\n", plugin_name, term_fnc_ptr);
207 set_running_plugin(current_plugin);
208 term_fnc_ptr();
209 return NG_SUCCESS;
210 }
211
212 NG_RC stopPluginManager(){
213 return clean_plugin_list(plugin_list);
214 }

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