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) Members of the EGEE Collaboration. 2004.
00003  * See http://eu-egee.org/partners/ for details on the copyright holders.
00004  * For license conditions see the license file or
00005  * http://eu-egee.org/license.html
00006  */
00007 
00008 /*
00009  *   Copyright (c) 2003 EU DataGrid        http://www.eu-datagrid.org/
00010  *
00011  *   $Id: evaluationmanager.c,v 1.31 2005/03/01 17:05:10 venekamp Exp $
00012  *
00013  *   Copyright (c) 2003 by
00014  *      G.M. Venekamp <venekamp@nikhef.nl>
00015  *      NIKHEF Amsterdam, the Netherlands
00016  *
00017  *   This software is distributed under a BSD-style open source
00018  *   licence. For a complete description of the licence take a look
00019  *   at: http://eu-datagrid.web.cern.ch/eu-datagrid/license.html
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   /*  Let's see if the config file makes sence.  */
00082   yyparse();
00083 
00084   /*
00085    *  Check if there were any errors. Ifo so, there is not much point
00086    *  on continuing.
00087    */
00088   if (yyparse_errors()) {
00089     stopEvaluationManager();
00090     return -1;
00091   }
00092 
00093   /*
00094    *  It might be that we have allocated policy rulez without any rules
00095    *  attachted to it. Before we can continue we must clean up the tree
00096    *  a free the allocated policy rules with an empty rule set.
00097    */
00098   cleanup_policies();
00099 
00100   /*
00101    *  Check to see if there are any recustions in the config file.
00102    *  If so, continuing might/will cause an infinite loop.
00103    */
00104   if (check_policies_for_recursion())
00105     return -1;
00106 
00107 
00108   /*
00109    *  We need to replace the variables found in the policy rules by
00110    *  their respective values.
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   /*  Check if the plugins have been requested before.  */
00140   if (global_plugin_list) {
00141     *plugins = global_plugin_list;
00142     return 0;
00143   }
00144 
00145   /*  Set to a safe default value.  */
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     /*  Copy the name and arguments while respecting max. lengths.  */
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 //    if (p_list->name)  free(p_list->name);
00198 //    if (p_list->args)  free(p_list->args);
00199 //    free((plugin_t*)p_list);
00200 
00201     p_list = tmp_p_list;
00202 
00203     /*  Output some debug info.  */
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      *  First check if we have to look for one specific policy rule.
00237      *  If it is not the case, the old behaviour is unaffected.
00238      */
00239     if (argc) {
00240       int i;
00241       int found = 0;
00242       policy = get_current_policy();
00243       /*
00244        *  Let us find out whether we have found the requested policy.
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          *  Target policy has been found. Set a flag to indicate when
00257          *  the current policy is left, the search for next policy rules
00258          *  will be aborted.
00259          *
00260          */
00261         policy_seen = 1;
00262       } else {
00263         if (plugin_name) free((char *)plugin_name);
00264 
00265         /*
00266          *  We are looking for a specific policy rule and the current one
00267          *  is not the onw we are looking for. There are two situations in
00268          *  which we are here:
00269          *    - current policy is not the one we are looking for, and
00270          *      we have not yet seen it;
00271          *    - current policy is not the one we are looking for, and
00272          *      we have seen the one we wanted before.
00273          *  If we have seen the policy rule we are looking for, we are
00274          *  done. If not, we need to continue to look for it.
00275          *
00276          */
00277         if (policy_seen)
00278           break;
00279         else {
00280           /*
00281            *  Set the result to EVALUATION_FAILURE in order to fall through
00282            *  the next policy rule. This might take a few steps though!
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 }

Generated on Sun May 29 21:22:10 2005 for lcmaps by doxygen 1.3.5