Main Page   Modules   Data Structures   File List   Data Fields   Globals   Related Pages  

evaluationmanager.c

Go to the documentation of this file.
00001 /*
00002  *   Copyright (c) 2003 EU DataGrid        http://www.eu-datagrid.org/
00003  *
00004  *   $Id: evaluationmanager.c,v 1.23 2004/10/01 15:17:31 maart Exp $
00005  *
00006  *   Copyright (c) 2003 by
00007  *      G.M. Venekamp <venekamp@nikhef.nl>
00008  *      NIKHEF Amsterdam, the Netherlands
00009  *
00010  *   This software is distributed under a BSD-style open source
00011  *   licence. For a complete description of the licence take a look
00012  *   at: http://eu-datagrid.web.cern.ch/eu-datagrid/license.html
00013  *
00014  */
00015 
00016 
00035 #include <stdlib.h>
00036 #include <string.h>
00037 
00038 #include "_lcmaps_pluginmanager.h"
00039 #include "lcmaps_log.h"
00040 #include "evaluationmanager.h"
00041 
00050 static lcmaps_db_entry_t* global_plugin_list = NULL;
00051 
00052 int free_lcmaps_db_entry();
00053 
00054 
00063 int startEvaluationManager(const char* name)
00064 {
00065   if (pdl_init(name) < 0) {
00066     stopEvaluationManager();
00067     return -1;
00068   }
00069 
00070   /*  Let's see if the config file makes sence.  */
00071   yyparse();
00072 
00073   /*
00074    *  Check if there were any errors. Ifo so, there is not much point
00075    *  on continuing.
00076    */
00077   if (yyparse_errors()) {
00078     stopEvaluationManager();
00079     return -1;
00080   }
00081 
00082   /*
00083    *  Check to see if there are any recustions in the config file.
00084    *  If so, continuing might/will cause an infinite loop.
00085    */
00086   if (check_policies_for_recursion())
00087     return -1;
00088 
00089   /*
00090    *  We need to replace the variables found in the policy rules by
00091    *  their respective values.
00092    */
00093   reduce_policies();
00094 
00095   return 0;
00096 }
00097 
00098 
00110 int getPluginNameAndArgs(lcmaps_db_entry_t** plugins)
00111 {
00112   const plugin_t* p_list, *tmp_p_list;
00113   lcmaps_db_entry_t* p=0;
00114   int path_length;
00115   char* path;
00116   BOOL string_too_long;
00117 
00118   string_too_long = FALSE;
00119 
00120   /*  Check if the plugins have been requested before.  */
00121   if (global_plugin_list) {
00122     *plugins = global_plugin_list;
00123     return 0;
00124   }
00125 
00126   /*  Set to a safe default value.  */
00127   *plugins = 0;
00128 
00129   if (!pdl_path()) {
00130     lcmaps_log(1, "Initialization of the EvaluationManager either failed or was not done.\n");
00131     return -1; 
00132   }
00133 
00134   path = strdup(pdl_path());
00135   path_length = strlen(path);
00136 
00137   if (path[path_length-1] != '/') {
00138     path = (char *)realloc(path, path_length+2);
00139     path[path_length] = '/';
00140     path[path_length+1] = '\0';
00141 
00142     path_length = strlen(path);
00143   }
00144 
00145   p_list = get_plugins();
00146 
00147   while (p_list) {
00148     if (!*plugins) {
00149       *plugins = (lcmaps_db_entry_t*)malloc(sizeof(lcmaps_db_entry_t));
00150       p = *plugins;
00151     } else {
00152       p->next = (lcmaps_db_entry_t*)malloc(sizeof(lcmaps_db_entry_t));
00153       p = p->next;
00154     }
00155 
00156     /*  Copy the name and arguments while respecting max. lengths.  */
00157     strncpy(p->pluginname, path, LCMAPS_MAXPATHLEN);
00158     strncpy(p->pluginname+path_length, p_list->name, LCMAPS_MAXPATHLEN-path_length);
00159 
00160     if ((strlen(path) + strlen(p_list->name))>=LCMAPS_MAXPATHLEN) {
00161       lcmaps_log(1, "String too long to copy. Max length = %d\n", LCMAPS_MAXPATHLEN);
00162       string_too_long = TRUE;
00163     }
00164 
00165     if (p_list->args) {
00166       strncpy(p->pluginargs, p_list->args, LCMAPS_MAXARGSTRING);
00167       if (strlen(p_list->args)>LCMAPS_MAXARGSTRING) {
00168         lcmaps_log(1, "String too long to copy. Max length = %d\n", LCMAPS_MAXARGSTRING);
00169         string_too_long = TRUE;
00170       }
00171     }
00172     else
00173       *p->pluginargs = '\0';
00174     p->next = 0;
00175 
00176     tmp_p_list = p_list->next;
00177 
00178     if (p_list->name)  free(p_list->name);
00179     if (p_list->args)  free(p_list->args);
00180     free((plugin_t*)p_list);
00181 
00182     p_list = tmp_p_list;
00183 
00184     /*  Output some debug info.  */
00185     lcmaps_log_debug(1, "%s\n", p->pluginname);
00186     lcmaps_log_debug(1, "%s\n", p->pluginargs);
00187   }
00188 
00189   free(path);
00190 
00191   global_plugin_list = *plugins;
00192 
00193   return string_too_long ? -1 : 0;
00194 }
00195 
00196 
00205 int runEvaluationManager(int argc, char *argv[])
00206 {
00207   const char* plugin_name;
00208   plugin_status_t result;
00209   policy_t* policy;
00210   int policy_seen;
00211 
00212   lcmaps_log_debug(1, "runEvaluationManager called\n");
00213 
00214   policy_seen = 0;
00215 
00216   result = EVALUATION_START;
00217   while ((plugin_name=pdl_next_plugin(result))) {
00218     /*
00219      *  First check if we have to look for one specific policy rule.
00220      *  If it is not the case, the old behaviour is unaffected.
00221      */
00222     if (argc) {
00223       int i;
00224       int found = 0;
00225       policy = get_current_policy();
00226       /*
00227        *  Let us find out whether we have found the requested policy.
00228        */
00229 
00230       for (i=0; policy, i<argc; ++i) {
00231         if (strcmp(policy->name, argv[i])==0) {
00232           found = 1;
00233           break;
00234         }
00235       }
00236 
00237       if (found) {
00238         /*
00239          *  Target policy has been found. Set a flag to indicate when
00240          *  the current policy is left, the search for next policy rules
00241          *  will be aborted.
00242          *
00243          */
00244         policy_seen = 1;
00245       } else {
00246         if (plugin_name) free((char *)plugin_name);
00247 
00248         /*
00249          *  We are looking for a specific policy rule and the current one
00250          *  is not the onw we are looking for. There two situations in
00251          *  which we are here:
00252          *    - current policy is not the one we are looking for, and
00253          *      we have not yet seen it;
00254          *    - current policy is not the one we are looking for, and
00255          *      we have seen the one we wanted before.
00256          *  If we have seen the policy rule we are looking for, we are
00257          *  done. If not, we need to continue to look for it.
00258          *
00259          */
00260         if (policy_seen)
00261           break;
00262         else {
00263           /*
00264            *  Set the result to EVALUATION_FAILURE in order to fall through
00265            *  the next policy rule. This might take a few steps though!
00266            */
00267           result = EVALUATION_FAILURE;
00268           continue;
00269         }
00270       }
00271     }
00272 
00273     result = (runPlugin(plugin_name) ? EVALUATION_FAILURE : EVALUATION_SUCCESS);
00274     
00275     lcmaps_log_debug(1, "runEvaluationManager: running plugin: %s.\n", plugin_name);
00276     lcmaps_log_debug(1, "                    : result %s.\n", (result==EVALUATION_SUCCESS) ? "true" : "false");
00277     
00278     if (plugin_name) free((char *)plugin_name);
00279   }
00280 
00281   if (result==EVALUATION_START)
00282     lcmaps_log(1, "Initialization of the EvaluationManager either failed or was not done.\n");
00283 
00284   return result==EVALUATION_SUCCESS ? 0 : 1;
00285 }
00286 
00287 
00298 int stopEvaluationManager(void)
00299 {
00300   lcmaps_log_debug(1, "stopEvaluationManager: cleaning up!\n");
00301 
00302   free_resources();
00303 
00304   free_lcmaps_db_entry();
00305 
00306   return 0;
00307 }
00308 
00309 
00319 int free_lcmaps_db_entry()
00320 {
00321   lcmaps_db_entry_t* plugin = global_plugin_list;
00322 
00323   while (plugin) {
00324     lcmaps_db_entry_t* tmp = plugin->next;
00325     free(plugin);
00326     plugin = tmp;
00327   }
00328 
00329   global_plugin_list = NULL;
00330 
00331   return 0;
00332 }

Generated on Sat Oct 2 02:18:26 2004 for edg-lcmaps by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002