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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 589 - (hide annotations) (download) (as text)
Wed Jul 22 10:16:59 2009 UTC (12 years, 9 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 aramv 429 #include "plugin_manager.h"
2 aramv 446 #include "_plugin_manager.h"
3 aramv 429 #include <string.h>
4 aramv 586 #include <stdio.h>
5 aramv 429
6 aramv 502 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 aramv 586 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 aramv 572 return NG_FAILURE;
31 aramv 566 }
32    
33 aramv 502 /* returns 1 on errors, 0 for success */
34     NG_RC startPluginManager(){
35 aramv 445 plugin_list = NULL;
36 aramv 502 _running_plugin = NULL;
37 aramv 586 return NG_SUCCESS;
38 aramv 445 }
39 aramv 429
40 aramv 502 NG_RC clean_plugin_list(lcmaps_plugindl_t * list){
41 aramv 445 lcmaps_plugindl_t * plugin_entry=NULL;
42     lcmaps_plugindl_t * plugin_next=NULL;
43     plugin_entry=list;
44 aramv 586 int i;
45 aramv 445
46 aramv 589 while(plugin_entry!=NULL){
47     if(plugin_entry->handle != NULL){
48     if((dlclose(plugin_entry->handle))){
49 aramv 586 ng_log(0, "ees.clean_plugin_list(): dlclose error %s\n", dlerror());
50     return NG_FAILURE;
51     }
52     }
53 aramv 589 /*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 aramv 445 }
62     list = plugin_entry = plugin_next = NULL;
63 aramv 586 return NG_SUCCESS;
64 aramv 445 }
65    
66     /* Wrapper for dlsym with error logging. Adapted from lcmaps_pluginmanager.c get_procsymbol */
67 aramv 484 lcmaps_proc_t get_procsymbol(void* handle, char* symname){
68 aramv 445 lcmaps_proc_t symhandle;
69     symhandle = dlsym(handle,symname);
70 aramv 502 char* errstring;
71     if((errstring = dlerror()) != NULL){
72     ng_log(0, "ees.get_procsymbol(): dlsym error %s\n", errstring);
73 aramv 445 return NULL;
74     }
75     return symhandle;
76     }
77    
78 aramv 586 lcmaps_plugindl_t * PluginInit(int argc, char** argv){
79     int i = 0;
80     int max_args = LCMAPS_MAXARGS;
81 aramv 445 memset(abs_plugin_path, 0, LCMAPS_MAXPATHLEN);
82 aramv 460 lcmaps_plugindl_t * current_plugin = NULL;
83 aramv 445
84 aramv 460 /*TODO maak safe(r) */
85 aramv 480 if(!(strcat(abs_plugin_path, MODULE_DIR) && \
86 aramv 586 strcat(abs_plugin_path, argv[0])
87     /*&& strcat(abs_plugin_path, ".mod")*/
88     )){
89 aramv 460 goto fail_startPluginManager;
90     }
91 aramv 429
92     /* current plugin struct */
93 aramv 586 if(!(current_plugin = calloc(1,sizeof(lcmaps_plugindl_t)))){
94     goto fail_startPluginManager;
95 aramv 589 } else {
96 aramv 429
97 aramv 589 ng_log(LOG_ERR, "Loading plugin %s\n", &abs_plugin_path);
98 aramv 429
99 aramv 589 /* load plugin handle */
100     current_plugin->handle = dlopen(abs_plugin_path, RTLD_NOW);
101 aramv 429
102 aramv 589 /* this is a bit silly */
103 aramv 429
104 aramv 589 if(argc < LCMAPS_MAXARGS){
105     max_args = argc;
106     } else {
107     max_args = LCMAPS_MAXARGS;
108     }
109 aramv 429
110 aramv 589 strncpy(current_plugin->pluginname, argv[0], LCMAPS_MAXPATHLEN);
111     /*current_plugin->pluginname = argv[0];*/
112     /**(current_plugin->pluginargs) = *argv[0];*/
113 aramv 429
114 aramv 589 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 aramv 586
123 aramv 589 if(!current_plugin->handle){
124     ng_log(LOG_ERR, "%s", dlerror());
125     goto fail_startPluginManager;
126     }
127 aramv 586
128 aramv 589 if(dlerror()){
129     ng_log(LOG_ERR, "%s", errno);
130     goto fail_startPluginManager;
131     }
132 aramv 586
133 aramv 589 current_plugin->procs[INITPROC] = NULL;
134 aramv 429
135 aramv 589 /* 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 aramv 429
142 aramv 589 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 aramv 502
148 aramv 589 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 aramv 502
154 aramv 589 set_running_plugin(current_plugin);
155 aramv 502
156 aramv 589 init_fnc_ptr = current_plugin->procs[INITPROC];
157 aramv 429
158 aramv 589 /* 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 aramv 429
163 aramv 589 print_lcmaps_plugin(0, current_plugin);
164 aramv 460
165 aramv 589 /* return plugin struct */
166     return current_plugin;
167     }
168 aramv 429
169 aramv 445 fail_startPluginManager:
170     clean_plugin_list(current_plugin);
171     return NULL;
172 aramv 429 }
173 aramv 445
174 aramv 502 NG_RC print_lcmaps_plugin(int debug_lvl, lcmaps_plugindl_t *plugin){
175 aramv 460 ng_log(debug_lvl, "plugin name : %s\n", plugin->pluginname);
176 aramv 586 return NG_SUCCESS;
177 aramv 460 }
178    
179 aramv 502 lcmaps_plugindl_t* get_plugin(char* plugin_name){
180 aramv 481 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 aramv 502 return current_plugin;
185 aramv 481 }
186     current_plugin = current_plugin->next;
187     }
188 aramv 502 return NULL;
189 aramv 481 }
190    
191 aramv 502 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 aramv 589 return clean_plugin_list(plugin_list);
214 aramv 445 }

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