XrdProofdConfig.cxx

Go to the documentation of this file.
00001 // @(#)root/proofd:$Id: XrdProofdConfig.cxx 36520 2010-11-05 16:08:23Z ganis $
00002 // Author: G. Ganis Jan 2008
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //////////////////////////////////////////////////////////////////////////
00013 //                                                                      //
00014 // XrdProofdConfig                                                      //
00015 //                                                                      //
00016 // Author: G. Ganis, CERN, 2008                                         //
00017 //                                                                      //
00018 // Implementation of the XrdProofdManager operations related to         //
00019 // configuration.                                                       //
00020 //                                                                      //
00021 //////////////////////////////////////////////////////////////////////////
00022 #include "XrdProofdPlatform.h"
00023 
00024 #ifdef OLDXRDOUC
00025 #  include "XrdOuc/XrdOucError.hh"
00026 #  include "XrdOuc/XrdOucLogger.hh"
00027 #else
00028 #  include "XrdSys/XrdSysError.hh"
00029 #  include "XrdSys/XrdSysLogger.hh"
00030 #endif
00031 #include "XrdNet/XrdNetDNS.hh"
00032 #include "XrdOuc/XrdOucEnv.hh"
00033 #include "XrdOuc/XrdOucStream.hh"
00034 #include "XrdOuc/XrdOucString.hh"
00035 
00036 #include "XrdProofdConfig.h"
00037 
00038 // Tracing utilities
00039 #include "XrdProofdTrace.h"
00040 
00041 XrdOucString XrdProofdConfig::fgHost;
00042 
00043 //__________________________________________________________________________
00044 XrdProofdConfig::XrdProofdConfig(const char *fn, XrdSysError *edest)
00045                 : fCfgFile(fn), fEDest(edest)
00046 {
00047    // Main constructor
00048 
00049    SetCfgEDest(fn, edest);
00050 }
00051 
00052 //__________________________________________________________________________
00053 void XrdProofdConfig::SetCfgEDest(const char *fn, XrdSysError *edest)
00054 {
00055    // Set config file and error handler
00056 
00057    fEDest = edest;
00058    if (fn && fCfgFile.fName != fn) {
00059       fCfgFile.fName = fn;
00060       XrdProofdAux::Expand(fCfgFile.fName);
00061    }
00062    fCfgFile.fMtime = 0;
00063 }
00064 
00065 //__________________________________________________________________________
00066 bool XrdProofdConfig::ReadFile(bool update)
00067 {
00068    // Return true if the file has never been read or did change since last
00069    // reading, false otherwise.
00070    // If update is true, the modification time is updated, so next call will
00071    // return 0.
00072    XPDLOC(ALL, "Config::ReadFile")
00073 
00074    // If we have a file, record the time of last change
00075    if (fCfgFile.fName.length() > 0) {
00076 
00077       // Get the modification time
00078       struct stat st;
00079       if (stat(fCfgFile.fName.c_str(), &st) != 0)
00080          return -1;
00081       TRACE(DBG, "file: " << fCfgFile.fName);
00082       TRACE(DBG, "time of last modification: " << st.st_mtime);
00083 
00084       // File should be loaded only once
00085       if (st.st_mtime <= fCfgFile.fMtime)
00086          return 0;
00087 
00088       // Save the modification time, if requested
00089       if (update) fCfgFile.fMtime = st.st_mtime;
00090 
00091       // Never read or changed: read it again
00092       return 1;
00093    } else {
00094 
00095       // Nothing to process
00096       return 0;
00097    }
00098 }
00099 
00100 //__________________________________________________________________________
00101 int XrdProofdConfig::ParseFile(bool rcf)
00102 {
00103    // Parse config file for the registered directives. The flag 'rcf' is 0
00104    // on the first call, 1 on successive calls.
00105    // Returns 0 on success, -1 otherwise
00106    XPDLOC(ALL, "Config::ParseFile")
00107 
00108    XrdOucString mp;
00109 
00110    // Check if the config file changed since last read, if any
00111    if (!ReadFile()) {
00112       TRACE(DBG, "config file already parsed ");
00113       return 0;
00114    }
00115 
00116    // Local FQDN
00117    if (fgHost.length() <= 0) {
00118       char *host = XrdNetDNS::getHostName();
00119       fgHost = host ? host : "";
00120       SafeFree(host);
00121    }
00122 
00123    // Communicate the host name to the config directives, so that the (deprecated)
00124    // old style 'if' condition can be handled
00125    fDirectives.Apply(SetHostInDirectives, (void *)fgHost.c_str());
00126 
00127    // Open the config file
00128    int cfgFD;
00129    const char *cfn = fCfgFile.fName.c_str();
00130    if ((cfgFD = open(cfn, O_RDONLY, 0)) < 0) {
00131       TRACE(XERR, "unable to open : " << cfn);
00132       return -1;
00133    }
00134 
00135    // Create the stream and attach to the file
00136    XrdOucEnv myEnv;
00137    XrdOucStream cfg(fEDest, getenv("XRDINSTANCE"),  &myEnv);
00138    cfg.Attach(cfgFD);
00139 
00140    // Process items
00141    char *var = 0, *val = 0;
00142    while ((var = cfg.GetMyFirstWord())) {
00143       if (!(strncmp("xpd.", var, 4)) && var[4]) {
00144          // xpd directive: process it
00145          var += 4;
00146          // Get the directive
00147          XrdProofdDirective *d = fDirectives.Find(var);
00148          if (d) {
00149             // Process it
00150             val = cfg.GetWord();
00151             d->DoDirective(val, &cfg, rcf);
00152          }
00153       } else if (var[0]) {
00154          // Check if we are interested in this non-xpd directive
00155          XrdProofdDirective *d = fDirectives.Find(var);
00156          if (d) {
00157             // Process it
00158             val = cfg.GetWord();
00159             d->DoDirective(val, &cfg, rcf);
00160          }
00161       }
00162    }
00163 
00164    // Done
00165    return 0;
00166 }

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