00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00042 #include <stdlib.h>
00043 #include <string.h>
00044 
00045 #include "_lcmaps_pluginmanager.h"
00046 #include "lcmaps_log.h"
00047 #include "evaluationmanager.h"
00048 
00057 static lcmaps_db_entry_t* global_plugin_list = NULL;
00058 
00059 int free_lcmaps_db_entry();
00060 
00061 
00072 int startEvaluationManager(const char* name, int argc, char*argv[])
00073 {
00074   if (pdl_init(name) < 0) {
00075     stopEvaluationManager();
00076     return -1;
00077   }
00078 
00079   SetSetOfRules(argc, argv);
00080 
00081   
00082   yyparse();
00083 
00084   
00085 
00086 
00087 
00088   if (yyparse_errors()) {
00089     stopEvaluationManager();
00090     return -1;
00091   }
00092 
00093   
00094 
00095 
00096 
00097 
00098   cleanup_policies();
00099 
00100   
00101 
00102 
00103 
00104   if (check_policies_for_recursion())
00105     return -1;
00106 
00107 
00108   
00109 
00110 
00111 
00112   reduce_policies();
00113 
00114   return 0;
00115 }
00116 
00117 
00129 int getPluginNameAndArgs(lcmaps_db_entry_t** plugins)
00130 {
00131   const plugin_t* p_list, *tmp_p_list;
00132   lcmaps_db_entry_t* p=0;
00133   int path_length;
00134   char* path;
00135   BOOL string_too_long;
00136 
00137   string_too_long = FALSE;
00138 
00139   
00140   if (global_plugin_list) {
00141     *plugins = global_plugin_list;
00142     return 0;
00143   }
00144 
00145   
00146   *plugins = 0;
00147 
00148   if (!pdl_path()) {
00149     lcmaps_log(1, "Initialization of the EvaluationManager either failed or was not done.\n");
00150     return -1; 
00151   }
00152 
00153   path = strdup(pdl_path());
00154   path_length = strlen(path);
00155 
00156   if (path[path_length-1] != '/') {
00157     path = (char *)realloc(path, path_length+2);
00158     path[path_length] = '/';
00159     path[path_length+1] = '\0';
00160 
00161     path_length = strlen(path);
00162   }
00163 
00164   p_list = get_plugins();
00165 
00166   while (p_list) {
00167     if (!*plugins) {
00168       *plugins = (lcmaps_db_entry_t*)malloc(sizeof(lcmaps_db_entry_t));
00169       p = *plugins;
00170     } else {
00171       p->next = (lcmaps_db_entry_t*)malloc(sizeof(lcmaps_db_entry_t));
00172       p = p->next;
00173     }
00174 
00175     
00176     strncpy(p->pluginname, path, LCMAPS_MAXPATHLEN);
00177     strncpy(p->pluginname+path_length, p_list->name, LCMAPS_MAXPATHLEN-path_length);
00178 
00179     if ((strlen(path) + strlen(p_list->name))>=LCMAPS_MAXPATHLEN) {
00180       lcmaps_log(1, "String too long to copy. Max length = %d\n", LCMAPS_MAXPATHLEN);
00181       string_too_long = TRUE;
00182     }
00183 
00184     if (p_list->args) {
00185       strncpy(p->pluginargs, p_list->args, LCMAPS_MAXARGSTRING);
00186       if (strlen(p_list->args)>LCMAPS_MAXARGSTRING) {
00187         lcmaps_log(1, "String too long to copy. Max length = %d\n", LCMAPS_MAXARGSTRING);
00188         string_too_long = TRUE;
00189       }
00190     }
00191     else
00192       *p->pluginargs = '\0';
00193     p->next = 0;
00194 
00195     tmp_p_list = p_list->next;
00196 
00197 
00198 
00199 
00200 
00201     p_list = tmp_p_list;
00202 
00203     
00204     lcmaps_log_debug(1, "%s\n", p->pluginname);
00205     lcmaps_log_debug(1, "%s\n", p->pluginargs);
00206   }
00207 
00208   free(path);
00209 
00210   global_plugin_list = *plugins;
00211 
00212   return string_too_long ? -1 : 0;
00213 }
00214 
00215 
00224 int runEvaluationManager(int argc, char *argv[])
00225 {
00226   const char* plugin_name;
00227   plugin_status_t result;
00228   const policy_t* policy;
00229   int policy_seen;
00230 
00231   policy_seen = 0;
00232 
00233   result = EVALUATION_START;
00234   while ((plugin_name=pdl_next_plugin(result))) {
00235     
00236 
00237 
00238 
00239     if (argc) {
00240       int i;
00241       int found = 0;
00242       policy = get_current_policy();
00243       
00244 
00245 
00246 
00247       for (i=0; policy && (i<argc); ++i) {
00248         if (strcmp(policy->name, argv[i])==0) {
00249           found = 1;
00250           break;
00251         }
00252       }
00253 
00254       if (found) {
00255         
00256 
00257 
00258 
00259 
00260 
00261         policy_seen = 1;
00262       } else {
00263         if (plugin_name) free((char *)plugin_name);
00264 
00265         
00266 
00267 
00268 
00269 
00270 
00271 
00272 
00273 
00274 
00275 
00276 
00277         if (policy_seen)
00278           break;
00279         else {
00280           
00281 
00282 
00283 
00284           result = EVALUATION_FAILURE;
00285           continue;
00286         }
00287       }
00288     }
00289 
00290     result = (runPlugin(plugin_name) ? EVALUATION_FAILURE : EVALUATION_SUCCESS);
00291     
00292     lcmaps_log_debug(1, "runEvaluationManager: running plugin: %s.\n", plugin_name);
00293     lcmaps_log_debug(1, "                    : result %s.\n", (result==EVALUATION_SUCCESS) ? "true" : "false");
00294     
00295     if (plugin_name) free((char *)plugin_name);
00296   }
00297 
00298   if (result==EVALUATION_START)
00299     lcmaps_log(1, "Initialization of the EvaluationManager either failed or was not done.\n");
00300 
00301   return result==EVALUATION_SUCCESS ? 0 : 1;
00302 }
00303 
00304 
00315 int stopEvaluationManager(void)
00316 {
00317   lcmaps_log_debug(1, "stopEvaluationManager: cleaning up!\n");
00318 
00319   free_resources();
00320 
00321   free_lcmaps_db_entry();
00322 
00323   return 0;
00324 }
00325 
00326 
00336 int free_lcmaps_db_entry()
00337 {
00338   lcmaps_db_entry_t* plugin = global_plugin_list;
00339 
00340   while (plugin) {
00341     lcmaps_db_entry_t* tmp = plugin->next;
00342     free(plugin);
00343     plugin = tmp;
00344   }
00345 
00346   global_plugin_list = NULL;
00347 
00348   return 0;
00349 }