XrdBwmConfig.cc

Go to the documentation of this file.
00001 /******************************************************************************/
00002 /*                                                                            */
00003 /*                       X r d B w m C o n f i g . c c                        */
00004 /*                                                                            */
00005 /* (C) 2010 by the Board of Trustees of the Leland Stanford, Jr., University  */
00006 /*                            All Rights Reserved                             */
00007 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
00008 /*               DE-AC02-76-SFO0515 with the Deprtment of Energy              */
00009 /******************************************************************************/
00010   
00011 #include <unistd.h>
00012 #include <ctype.h>
00013 #include <errno.h>
00014 #include <fcntl.h>
00015 #include <stdlib.h>
00016 #include <strings.h>
00017 #include <stdio.h>
00018 #include <sys/param.h>
00019 
00020 #include "XrdBwm/XrdBwm.hh"
00021 #include "XrdBwm/XrdBwmLogger.hh"
00022 #include "XrdBwm/XrdBwmPolicy.hh"
00023 #include "XrdBwm/XrdBwmPolicy1.hh"
00024 #include "XrdBwm/XrdBwmTrace.hh"
00025 
00026 #include "XrdOuc/XrdOuca2x.hh"
00027 #include "XrdOuc/XrdOucEnv.hh"
00028 #include "XrdSys/XrdSysError.hh"
00029 #include "XrdSys/XrdSysHeaders.hh"
00030 #include "XrdSys/XrdSysPlugin.hh"
00031 #include "XrdOuc/XrdOucStream.hh"
00032 #include "XrdOuc/XrdOucTrace.hh"
00033 
00034 #include "XrdAcc/XrdAccAuthorize.hh"
00035   
00036 /******************************************************************************/
00037 /*                               d e f i n e s                                */
00038 /******************************************************************************/
00039 
00040 #define TS_Xeq(x,m)   if (!strcmp(x,var)) return m(Config,Eroute);
00041 
00042 #define TS_Str(x,m)   if (!strcmp(x,var)) {free(m); m = strdup(val); return 0;}
00043 
00044 #define TS_PList(x,m)  if (!strcmp(x,var)) \
00045                           {m.Insert(new XrdOucPList(val,1)); return 0;}
00046 
00047 #define TS_Chr(x,m)   if (!strcmp(x,var)) {m = val[0]; return 0;}
00048 
00049 #define TS_Bit(x,m,v) if (!strcmp(x,var)) {m |= v; Config.Echo(); return 0;}
00050 
00051 #define Max(x,y) (x > y ? x : y)
00052 
00053 /******************************************************************************/
00054 /*                             C o n f i g u r e                              */
00055 /******************************************************************************/
00056   
00057 int XrdBwm::Configure(XrdSysError &Eroute) {
00058 /*
00059   Function: Establish default values using a configuration file.
00060 
00061   Input:    None.
00062 
00063   Output:   0 upon success or !0 otherwise.
00064 */
00065    char *var;
00066    int  cfgFD, retc, NoGo = 0;
00067    XrdOucEnv myEnv;
00068    XrdOucStream Config(&Eroute, getenv("XRDINSTANCE"), &myEnv, "=====> ");
00069 
00070 // Print warm-up message
00071 //
00072    Eroute.Say("++++++ Bwm initialization started.");
00073 
00074 // Get the debug level from the command line
00075 //
00076    if (getenv("XRDDEBUG")) BwmTrace.What = TRACE_ALL;
00077 
00078 // If there is no config file, return with the defaults sets.
00079 //
00080    if( !ConfigFN || !*ConfigFN)
00081      Eroute.Emsg("Config", "Configuration file not specified.");
00082      else {
00083            // Try to open the configuration file.
00084            //
00085            if ( (cfgFD = open(ConfigFN, O_RDONLY, 0)) < 0)
00086               return Eroute.Emsg("Config", errno, "open config file",
00087                                  ConfigFN);
00088            Config.Attach(cfgFD);
00089 
00090            // Now start reading records until eof.
00091            //
00092            while((var = Config.GetMyFirstWord()))
00093                 {if (!strncmp(var, "bwm.", 4))
00094                     if (ConfigXeq(var+4,Config,Eroute)) {Config.Echo();NoGo=1;}
00095                 }
00096 
00097            // Now check if any errors occured during file i/o
00098            //
00099            if ((retc = Config.LastError()))
00100            NoGo = Eroute.Emsg("Config", -retc, "read config file",
00101                               ConfigFN);
00102            Config.Close();
00103           }
00104 
00105 // Determine whether we should initialize authorization
00106 //
00107    if (Authorize) NoGo |= setupAuth(Eroute);
00108 
00109 // Establish scheduling policy
00110 //
00111    if (PolLib) NoGo |= setupPolicy(Eroute);
00112       else Policy = new XrdBwmPolicy1(PolSlotsIn, PolSlotsOut);
00113 
00114 // Start logger object
00115 //
00116    if (!NoGo && Logger) NoGo = Logger->Start(&Eroute);
00117 
00118 // Inform the handle of the policy and logger
00119 //
00120    if (!NoGo) XrdBwmHandle::setPolicy(Policy, Logger);
00121 
00122 // All done
00123 //
00124    Eroute.Say("------ Bwm initialization ", (NoGo ? "failed." : "completed."));
00125    return NoGo;
00126 }
00127 
00128 /******************************************************************************/
00129 /*                     p r i v a t e   f u n c t i o n s                      */
00130 /******************************************************************************/
00131 /******************************************************************************/
00132 /*                             C o n f i g X e q                              */
00133 /******************************************************************************/
00134   
00135 int XrdBwm::ConfigXeq(char *var, XrdOucStream &Config,
00136                                  XrdSysError &Eroute)
00137 {
00138     TS_Bit("authorize",     Authorize, 1);
00139     TS_Xeq("authlib",       xalib);
00140     TS_Xeq("log",           xlog);
00141     TS_Xeq("policy",        xpol);
00142     TS_Xeq("trace",         xtrace);
00143 
00144     // No match found, complain.
00145     //
00146     Eroute.Say("Config warning: ignoring unknown directive '",var,"'.");
00147     Config.Echo();
00148     return 0;
00149 }
00150 
00151 /******************************************************************************/
00152 /*                                 x a l i b                                  */
00153 /******************************************************************************/
00154   
00155 /* Function: xalib
00156 
00157    Purpose:  To parse the directive: authlib <path> [<parms>]
00158 
00159              <path>    the path of the authorization library to be used.
00160              <parms>   optional parms to be passed
00161 
00162   Output: 0 upon success or !0 upon failure.
00163 */
00164 
00165 int XrdBwm::xalib(XrdOucStream &Config, XrdSysError &Eroute)
00166 {
00167     char *val, parms[1024];
00168 
00169 // Get the path
00170 //
00171    if (!(val = Config.GetWord()) || !val[0])
00172       {Eroute.Emsg("Config", "authlib not specified"); return 1;}
00173 
00174 // Record the path
00175 //
00176    if (AuthLib) free(AuthLib);
00177    AuthLib = strdup(val);
00178 
00179 // Record any parms
00180 //
00181    if (!Config.GetRest(parms, sizeof(parms)))
00182       {Eroute.Emsg("Config", "authlib parameters too long"); return 1;}
00183    if (AuthParm) free(AuthParm);
00184    AuthParm = (*parms ? strdup(parms) : 0);
00185    return 0;
00186 }
00187 
00188 /******************************************************************************/
00189 /*                                  x l o g                                   */
00190 /******************************************************************************/
00191   
00192 /* Function: xlog
00193 
00194    Purpose:  Parse directive: log {* | <|prog> | <>path>}
00195 
00196              <prog>   - is the program to execute and dynamically feed messages
00197                         about the indicated events. Messages are piped to prog.
00198              <path>   - is the udp named socket to receive the message. The
00199                         server creates the path if it's not present. If <path>
00200                         is an asterisk, then messages are written to standard
00201                         log file.
00202 
00203    Output: 0 upon success or !0 upon failure.
00204 */
00205 int XrdBwm::xlog(XrdOucStream &Config, XrdSysError &Eroute)
00206 {
00207     char *val, parms[1024];
00208 
00209     if (!(val = Config.GetWord()))
00210        {Eroute.Emsg("Config", "log parameters not specified"); return 1;}
00211 
00212 // Get the remaining parameters
00213 //
00214    Config.RetToken();
00215    if (!Config.GetRest(parms, sizeof(parms)))
00216       {Eroute.Emsg("Config", "log parameters too long"); return 1;}
00217    val = (*parms == '|' ? parms+1 : parms);
00218 
00219 // Create a log object
00220 //
00221    if (Logger) delete Logger;
00222    Logger = new XrdBwmLogger(val);
00223 
00224 // All done
00225 //
00226    return 0;
00227 }
00228 
00229 /******************************************************************************/
00230 /*                                  x p o l                                   */
00231 /******************************************************************************/
00232   
00233 /* Function: xpol
00234 
00235    Purpose:  To parse the directive: policy args
00236 
00237              Args: {maxslots <innum> <outnum> | lib <path> [<parms>]}
00238 
00239              <num>     maximum number of slots available.
00240              <path>    if preceeded by lib, the path of the policy library to 
00241                        be used; otherwise, the file that describes policy.
00242              <parms>   optional parms to be passed
00243 
00244   Output: 0 upon success or !0 upon failure.
00245 */
00246 
00247 int XrdBwm::xpol(XrdOucStream &Config, XrdSysError &Eroute)
00248 {
00249     char *val, parms[2048];
00250     int pl;
00251 
00252 // Get next token
00253 //
00254    if (!(val = Config.GetWord()) || !val[0])
00255       {Eroute.Emsg("Config", "policy  not specified"); return 1;}
00256 
00257 // Start afresh
00258 //
00259    if (PolLib)  {free(PolLib);  PolLib  = 0;}
00260    if (PolParm) {free(PolParm); PolParm = 0;}
00261    PolSlotsIn = PolSlotsOut = 0;
00262 
00263 // If the word maxslots then this is a simple policy
00264 //
00265    if (!strcmp("maxslots", val))
00266       {if (!(val = Config.GetWord()) || !val[0])
00267           {Eroute.Emsg("Config", "policy in slots not specified"); return 1;}
00268        if (XrdOuca2x::a2i(Eroute,"policy in slots",val,&pl,0,32767)) return 1;
00269        PolSlotsIn = pl;
00270        if (!(val = Config.GetWord()) || !val[0])
00271           {Eroute.Emsg("Config", "policy out slots not specified"); return 1;}
00272        if (XrdOuca2x::a2i(Eroute,"policy out slots",val,&pl,0,32767)) return 1;
00273        PolSlotsOut = pl;
00274        return 0;
00275       }
00276 
00277 // Make sure the word is lib
00278 //
00279    if (strcmp("lib", val))
00280       {Eroute.Emsg("Config", "invalid policy keyword -", val); return 1;}
00281    if (!(val = Config.GetWord()) || !val[0])
00282       {Eroute.Emsg("Config", "policy library not specified"); return 1;}
00283 
00284 // Set the library
00285 //
00286    PolLib = strdup(val);
00287 
00288 // Get any parameters
00289 //
00290    if (!Config.GetRest(parms, sizeof(parms)))
00291       {Eroute.Emsg("Config", "policy lib parameters too long"); return 1;}
00292    PolParm = (*parms ? strdup(parms) : 0);
00293 
00294 // All done
00295 //
00296    return 0;
00297 }
00298 
00299 /******************************************************************************/
00300 /*                                x t r a c e                                 */
00301 /******************************************************************************/
00302 
00303 /* Function: xtrace
00304 
00305    Purpose:  To parse the directive: trace <events>
00306 
00307              <events> the blank separated list of events to trace. Trace
00308                       directives are cummalative.
00309 
00310    Output: 0 upon success or !0 upon failure.
00311 */
00312 
00313 int XrdBwm::xtrace(XrdOucStream &Config, XrdSysError &Eroute)
00314 {
00315     static struct traceopts {const char *opname; int opval;} tropts[] =
00316        {
00317         {"all",      TRACE_ALL},
00318         {"calls",    TRACE_calls},
00319         {"debug",    TRACE_debug},
00320         {"delay",    TRACE_delay},
00321         {"sched",    TRACE_sched},
00322         {"tokens",   TRACE_tokens}
00323        };
00324     int i, neg, trval = 0, numopts = sizeof(tropts)/sizeof(struct traceopts);
00325     char *val;
00326 
00327     if (!(val = Config.GetWord()))
00328        {Eroute.Emsg("Config", "trace option not specified"); return 1;}
00329     while (val)
00330          {if (!strcmp(val, "off")) trval = 0;
00331              else {if ((neg = (val[0] == '-' && val[1]))) val++;
00332                    for (i = 0; i < numopts; i++)
00333                        {if (!strcmp(val, tropts[i].opname))
00334                            {if (neg) trval &= ~tropts[i].opval;
00335                                else  trval |=  tropts[i].opval;
00336                             break;
00337                            }
00338                        }
00339                    if (i >= numopts)
00340                       Eroute.Say("Config warning: ignoring invalid trace option '",val,"'.");
00341                   }
00342           val = Config.GetWord();
00343          }
00344     BwmTrace.What = trval;
00345 
00346 // All done
00347 //
00348    return 0;
00349 }
00350 
00351 /******************************************************************************/
00352 /*                             s e t u p A u t h                              */
00353 /******************************************************************************/
00354 
00355 int XrdBwm::setupAuth(XrdSysError &Eroute)
00356 {
00357    extern XrdAccAuthorize *XrdAccDefaultAuthorizeObject(XrdSysLogger *lp,
00358                                                         const char   *cfn,
00359                                                         const char   *parm);
00360    XrdSysPlugin    *myLib;
00361    XrdAccAuthorize *(*ep)(XrdSysLogger *, const char *, const char *);
00362 
00363 // Authorization comes from the library or we use the default
00364 //
00365    if (!AuthLib) return 0 == (Authorization = XrdAccDefaultAuthorizeObject
00366                               (Eroute.logger(),ConfigFN,AuthParm));
00367 
00368 // Create a pluin object (we will throw this away without deletion because
00369 // the library must stay open but we never want to reference it again).
00370 //
00371    if (!(myLib = new XrdSysPlugin(&Eroute, AuthLib))) return 1;
00372 
00373 // Now get the entry point of the object creator
00374 //
00375    ep = (XrdAccAuthorize *(*)(XrdSysLogger *, const char *, const char *))
00376                              (myLib->getPlugin("XrdAccAuthorizeObject"));
00377    if (!ep) return 1;
00378 
00379 // Get the Object now
00380 //
00381    return 0 == (Authorization = ep(Eroute.logger(), ConfigFN, AuthParm));
00382 }
00383 
00384 /******************************************************************************/
00385 /*                           s e t u p P o l i c y                            */
00386 /******************************************************************************/
00387 
00388 int XrdBwm::setupPolicy(XrdSysError &Eroute)
00389 {
00390    XrdSysPlugin    *myLib;
00391    XrdBwmPolicy    *(*ep)(XrdSysLogger *, const char *, const char *);
00392 
00393 // Create a plugin object (we will throw this away without deletion because
00394 // the library must stay open but we never want to reference it again).
00395 //
00396    if (!(myLib = new XrdSysPlugin(&Eroute, PolLib))) return 1;
00397 
00398 // Now get the entry point of the object creator
00399 //
00400    ep = (XrdBwmPolicy *(*)(XrdSysLogger *, const char *, const char *))
00401                           (myLib->getPlugin("XrdBwmPolicyObject"));
00402    if (!ep) return 1;
00403 
00404 // Get the Object now
00405 //
00406    return 0 == (Policy = ep(Eroute.logger(), ConfigFN, PolParm));
00407 }

Generated on Tue Jul 5 14:46:16 2011 for ROOT_528-00b_version by  doxygen 1.5.1