Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
XrdProofdConfig.cxx
Go to the documentation of this file.
1 // @(#)root/proofd:$Id$
2 // Author: G. Ganis Jan 2008
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 //////////////////////////////////////////////////////////////////////////
13 // //
14 // XrdProofdConfig //
15 // //
16 // Author: G. Ganis, CERN, 2008 //
17 // //
18 // Implementation of the XrdProofdManager operations related to //
19 // configuration. //
20 // //
21 //////////////////////////////////////////////////////////////////////////
22 #include "XrdProofdPlatform.h"
23 
24 #include "XpdSysError.h"
25 #include "XpdSysLogger.h"
26 #include "XpdSysDNS.h"
27 
28 #include "XrdOuc/XrdOucEnv.hh"
29 #include "XrdOuc/XrdOucStream.hh"
30 #include "XrdOuc/XrdOucString.hh"
31 
32 #include "XrdProofdConfig.h"
33 
34 // Tracing utilities
35 #include "XrdProofdTrace.h"
36 
37 XrdOucString XrdProofdConfig::fgHost;
38 
39 ////////////////////////////////////////////////////////////////////////////////
40 /// Main constructor
41 
42 XrdProofdConfig::XrdProofdConfig(const char *fn, XrdSysError *edest)
43  : fCfgFile(fn), fEDest(edest)
44 {
45  SetCfgEDest(fn, edest);
46 }
47 
48 ////////////////////////////////////////////////////////////////////////////////
49 /// Set config file and error handler
50 
51 void XrdProofdConfig::SetCfgEDest(const char *fn, XrdSysError *edest)
52 {
53  fEDest = edest;
54  if (fn && fCfgFile.fName != fn) {
55  fCfgFile.fName = fn;
56  XrdProofdAux::Expand(fCfgFile.fName);
57  }
58  fCfgFile.fMtime = 0;
59 }
60 
61 ////////////////////////////////////////////////////////////////////////////////
62 /// Return true if the file has never been read or did change since last
63 /// reading, false otherwise.
64 /// If update is true, the modification time is updated, so next call will
65 /// return 0.
66 
67 bool XrdProofdConfig::ReadFile(bool update)
68 {
69  XPDLOC(ALL, "Config::ReadFile")
70 
71  // If we have a file, record the time of last change
72  if (fCfgFile.fName.length() > 0) {
73 
74  // Get the modification time
75  struct stat st;
76  if (stat(fCfgFile.fName.c_str(), &st) != 0)
77  return -1;
78  TRACE(DBG, "file: " << fCfgFile.fName);
79  TRACE(DBG, "time of last modification: " << st.st_mtime);
80 
81  // File should be loaded only once
82  if (st.st_mtime <= fCfgFile.fMtime)
83  return 0;
84 
85  // Save the modification time, if requested
86  if (update) fCfgFile.fMtime = st.st_mtime;
87 
88  // Never read or changed: read it again
89  return 1;
90  } else {
91 
92  // Nothing to process
93  return 0;
94  }
95 }
96 
97 ////////////////////////////////////////////////////////////////////////////////
98 /// Parse config file for the registered directives. The flag 'rcf' is 0
99 /// on the first call, 1 on successive calls.
100 /// Returns 0 on success, -1 otherwise
101 
102 int XrdProofdConfig::ParseFile(bool rcf)
103 {
104  XPDLOC(ALL, "Config::ParseFile")
105 
106  XrdOucString mp;
107 
108  // Check if the config file changed since last read, if any
109  if (!ReadFile()) {
110  TRACE(DBG, "config file already parsed ");
111  return 0;
112  }
113 
114  // Local FQDN
115  if (fgHost.length() <= 0) {
116  char *host = XrdSysDNS::getHostName();
117  fgHost = host ? host : "";
118  SafeFree(host);
119  }
120 
121  // Communicate the host name to the config directives, so that the (deprecated)
122  // old style 'if' condition can be handled
123  fDirectives.Apply(SetHostInDirectives, (void *)fgHost.c_str());
124 
125  // Open the config file
126  int cfgFD;
127  const char *cfn = fCfgFile.fName.c_str();
128  if ((cfgFD = open(cfn, O_RDONLY, 0)) < 0) {
129  TRACE(XERR, "unable to open : " << cfn);
130  return -1;
131  }
132 
133  // Create the stream and attach to the file
134  XrdOucEnv myEnv;
135  XrdOucStream cfg(fEDest, getenv("XRDINSTANCE"), &myEnv);
136  cfg.Attach(cfgFD);
137 
138  // Process items
139  char *var = 0, *val = 0;
140  while ((var = cfg.GetMyFirstWord())) {
141  if (!(strncmp("xpd.", var, 4)) && var[4]) {
142  // xpd directive: process it
143  var += 4;
144  // Get the directive
145  XrdProofdDirective *d = fDirectives.Find(var);
146  if (d) {
147  // Process it
148  val = cfg.GetWord();
149  d->DoDirective(val, &cfg, rcf);
150  }
151  } else if (var[0]) {
152  // Check if we are interested in this non-xpd directive
153  XrdProofdDirective *d = fDirectives.Find(var);
154  if (d) {
155  // Process it
156  val = cfg.GetWord();
157  d->DoDirective(val, &cfg, rcf);
158  }
159  }
160  }
161  close(cfgFD);
162 
163  // Done
164  return 0;
165 }