XRootD
XrdClDefaultEnv.cc
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN)
3 // Author: Lukasz Janyst <ljanyst@cern.ch>
4 //------------------------------------------------------------------------------
5 // XRootD is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // XRootD is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with XRootD. If not, see <http://www.gnu.org/licenses/>.
17 //------------------------------------------------------------------------------
18 
19 #include "XrdCl/XrdClDefaultEnv.hh"
20 #include "XrdCl/XrdClConstants.hh"
21 #include "XrdCl/XrdClPostMaster.hh"
22 #include "XrdCl/XrdClLog.hh"
24 #include "XrdCl/XrdClFileTimer.hh"
25 #include "XrdCl/XrdClUtils.hh"
26 #include "XrdCl/XrdClMonitor.hh"
30 #include "XrdCl/XrdClOptimizers.hh"
31 #include "XrdOuc/XrdOucPreload.hh"
32 #include "XrdSys/XrdSysAtomics.hh"
33 #include "XrdSys/XrdSysUtils.hh"
34 #include "XrdSys/XrdSysPwd.hh"
35 #include "XrdVersion.hh"
36 
37 #include <libgen.h>
38 #include <cstring>
39 #include <map>
40 #include <vector>
41 #include <algorithm>
42 #include <cctype>
43 #include <string>
44 #include <pthread.h>
45 #include <sys/types.h>
46 #include <unistd.h>
47 
48 XrdVERSIONINFO( XrdCl, client );
49 
50 //------------------------------------------------------------------------------
51 // Forking functions
52 //------------------------------------------------------------------------------
53 extern "C"
54 {
55  //----------------------------------------------------------------------------
56  // Prepare for the forking
57  //----------------------------------------------------------------------------
58  static void prepare()
59  {
60  using namespace XrdCl;
61  Log *log = DefaultEnv::GetLog();
62  Env *env = DefaultEnv::GetEnv();
63  ForkHandler *forkHandler = DefaultEnv::GetForkHandler();
64 
65  log->Debug( UtilityMsg, "In the prepare fork handler for process %d",
66  getpid() );
67 
68  //--------------------------------------------------------------------------
69  // Run the fork handler if it's enabled
70  //--------------------------------------------------------------------------
71  int runForkHandler = DefaultRunForkHandler;
72  env->GetInt( "RunForkHandler", runForkHandler );
73  if( runForkHandler )
74  forkHandler->Prepare();
75  }
76 
77  //----------------------------------------------------------------------------
78  // Parent handler
79  //----------------------------------------------------------------------------
80  static void parent()
81  {
82  using namespace XrdCl;
83  Log *log = DefaultEnv::GetLog();
84  Env *env = DefaultEnv::GetEnv();
85  ForkHandler *forkHandler = DefaultEnv::GetForkHandler();
86 
87  pid_t pid = getpid();
88  log->Debug( UtilityMsg, "In the parent fork handler for process %d", pid );
89 
90  //--------------------------------------------------------------------------
91  // Run the fork handler if it's enabled
92  //--------------------------------------------------------------------------
93  int runForkHandler = DefaultRunForkHandler;
94  env->GetInt( "RunForkHandler", runForkHandler );
95  if( runForkHandler )
96  {
97  log->SetPid(pid);
98  forkHandler->Parent();
99  }
100  }
101 
102  //----------------------------------------------------------------------------
103  // Child handler
104  //----------------------------------------------------------------------------
105  static void child()
106  {
107  using namespace XrdCl;
109  Log *log = DefaultEnv::GetLog();
110  Env *env = DefaultEnv::GetEnv();
111  ForkHandler *forkHandler = DefaultEnv::GetForkHandler();
112  env->RecreateLock();
113 
114  pid_t pid = getpid();
115  log->Debug( UtilityMsg, "In the child fork handler for process %d", pid );
116 
117  //--------------------------------------------------------------------------
118  // Run the fork handler if it's enabled
119  //--------------------------------------------------------------------------
120  int runForkHandler = DefaultRunForkHandler;
121  env->GetInt( "RunForkHandler", runForkHandler );
122  if( runForkHandler )
123  {
124  log->SetPid(pid);
125  forkHandler->Child();
126  }
127  }
128 }
129 
130 namespace
131 {
132  //----------------------------------------------------------------------------
133  // Translate a string into a topic mask
134  //----------------------------------------------------------------------------
135  struct MaskTranslator
136  {
137  //--------------------------------------------------------------------------
138  // Initialize the translation array
139  //--------------------------------------------------------------------------
140  MaskTranslator()
141  {
142  masks["AppMsg"] = XrdCl::AppMsg;
143  masks["UtilityMsg"] = XrdCl::UtilityMsg;
144  masks["FileMsg"] = XrdCl::FileMsg;
145  masks["PollerMsg"] = XrdCl::PollerMsg;
146  masks["PostMasterMsg"] = XrdCl::PostMasterMsg;
147  masks["XRootDTransportMsg"] = XrdCl::XRootDTransportMsg;
148  masks["TaskMgrMsg"] = XrdCl::TaskMgrMsg;
149  masks["XRootDMsg"] = XrdCl::XRootDMsg;
150  masks["FileSystemMsg"] = XrdCl::FileSystemMsg;
151  masks["AsyncSockMsg"] = XrdCl::AsyncSockMsg;
152  masks["JobMgrMsg"] = XrdCl::JobMgrMsg;
153  masks["PlugInMgrMsg"] = XrdCl::PlugInMgrMsg;
154  masks["ExDbgMsg"] = XrdCl::ExDbgMsg;
155  masks["TlsMsg"] = XrdCl::TlsMsg;
156  }
157 
158  //--------------------------------------------------------------------------
159  // Translate the mask
160  //--------------------------------------------------------------------------
161  uint64_t translateMask( const std::string mask )
162  {
163  if( mask == "" )
164  return 0xffffffffffffffffULL;
165 
166  std::vector<std::string> topics;
167  std::vector<std::string>::iterator it;
168  XrdCl::Utils::splitString( topics, mask, "|" );
169 
170  uint64_t resultMask = 0;
171  std::map<std::string, uint64_t>::iterator maskIt;
172  for( it = topics.begin(); it != topics.end(); ++it )
173  {
174  //----------------------------------------------------------------------
175  // Check for resetting pseudo topics
176  //----------------------------------------------------------------------
177  if( *it == "All" )
178  {
179  resultMask = 0xffffffffffffffffULL;
180  continue;
181  }
182 
183  if( *it == "None" )
184  {
185  resultMask = 0ULL;
186  continue;
187  }
188 
189  //----------------------------------------------------------------------
190  // Check whether given topic should be disabled or enabled
191  //----------------------------------------------------------------------
192  std::string topic = *it;
193  bool disable = false;
194  if( !topic.empty() && topic[0] == '^' )
195  {
196  disable = true;
197  topic = topic.substr( 1, topic.length()-1 );
198  }
199 
200  maskIt = masks.find( topic );
201  if( maskIt == masks.end() )
202  continue;
203 
204  if( disable )
205  resultMask &= (0xffffffffffffffffULL ^ maskIt->second);
206  else
207  resultMask |= maskIt->second;
208  }
209 
210  return resultMask;
211  }
212 
213  std::map<std::string, uint64_t> masks;
214  };
215 
216  //----------------------------------------------------------------------------
217  // Helper for handling environment variables
218  //----------------------------------------------------------------------------
219  template<typename Item>
220  struct EnvVarHolder
221  {
222  EnvVarHolder( const std::string &name_, const Item &def_ ):
223  name( name_ ), def( def_ ) {}
224  std::string name;
225  Item def;
226  };
227 }
228 
229 #define REGISTER_VAR_INT( array, name, def ) \
230  array.push_back( EnvVarHolder<int>( name, def ) )
231 
232 #define REGISTER_VAR_STR( array, name, def ) \
233  array.push_back( EnvVarHolder<std::string>( name, def ) )
234 
235 namespace XrdCl
236 {
237  //----------------------------------------------------------------------------
238  // Statics
239  //----------------------------------------------------------------------------
240  XrdSysMutex DefaultEnv::sInitMutex;
241  Env *DefaultEnv::sEnv = 0;
242  PostMaster *DefaultEnv::sPostMaster = 0;
243  Log *DefaultEnv::sLog = 0;
244  ForkHandler *DefaultEnv::sForkHandler = 0;
245  FileTimer *DefaultEnv::sFileTimer = 0;
246  Monitor *DefaultEnv::sMonitor = 0;
247  XrdOucPinLoader *DefaultEnv::sMonitorLibHandle = 0;
248  bool DefaultEnv::sMonitorInitialized = false;
249  CheckSumManager *DefaultEnv::sCheckSumManager = 0;
250  TransportManager *DefaultEnv::sTransportManager = 0;
251  PlugInManager *DefaultEnv::sPlugInManager = 0;
252 
253  //----------------------------------------------------------------------------
254  // Constructor
255  //----------------------------------------------------------------------------
256  DefaultEnv::DefaultEnv()
257  {
258  Log *log = GetLog();
259  log->Debug( UtilityMsg, "Initializing xrootd client version: %s", XrdVERSION );
260 
261  //--------------------------------------------------------------------------
262  // Declate the variables to be processed
263  //--------------------------------------------------------------------------
264  std::vector<EnvVarHolder<int> > varsInt;
265  std::vector<EnvVarHolder<std::string> > varsStr;
266  REGISTER_VAR_INT( varsInt, "ConnectionWindow", DefaultConnectionWindow );
267  REGISTER_VAR_INT( varsInt, "ConnectionRetry", DefaultConnectionRetry );
268  REGISTER_VAR_INT( varsInt, "RequestTimeout", DefaultRequestTimeout );
269  REGISTER_VAR_INT( varsInt, "StreamTimeout", DefaultStreamTimeout );
270  REGISTER_VAR_INT( varsInt, "SubStreamsPerChannel", DefaultSubStreamsPerChannel );
271  REGISTER_VAR_INT( varsInt, "TimeoutResolution", DefaultTimeoutResolution );
272  REGISTER_VAR_INT( varsInt, "StreamErrorWindow", DefaultStreamErrorWindow );
273  REGISTER_VAR_INT( varsInt, "RunForkHandler", DefaultRunForkHandler );
274  REGISTER_VAR_INT( varsInt, "RedirectLimit", DefaultRedirectLimit );
275  REGISTER_VAR_INT( varsInt, "WorkerThreads", DefaultWorkerThreads );
276  REGISTER_VAR_INT( varsInt, "CPChunkSize", DefaultCPChunkSize );
277  REGISTER_VAR_INT( varsInt, "CPParallelChunks", DefaultCPParallelChunks );
278  REGISTER_VAR_INT( varsInt, "DataServerTTL", DefaultDataServerTTL );
279  REGISTER_VAR_INT( varsInt, "LoadBalancerTTL", DefaultLoadBalancerTTL );
280  REGISTER_VAR_INT( varsInt, "CPInitTimeout", DefaultCPInitTimeout );
281  REGISTER_VAR_INT( varsInt, "CPTPCTimeout", DefaultCPTPCTimeout );
282  REGISTER_VAR_INT( varsInt, "CPTimeout", DefaultCPTimeout );
283  REGISTER_VAR_INT( varsInt, "TCPKeepAlive", DefaultTCPKeepAlive );
284  REGISTER_VAR_INT( varsInt, "TCPKeepAliveTime", DefaultTCPKeepAliveTime );
285  REGISTER_VAR_INT( varsInt, "TCPKeepAliveInterval", DefaultTCPKeepAliveInterval );
286  REGISTER_VAR_INT( varsInt, "TCPKeepProbes", DefaultTCPKeepAliveProbes );
287  REGISTER_VAR_INT( varsInt, "MultiProtocol", DefaultMultiProtocol );
288  REGISTER_VAR_INT( varsInt, "ParallelEvtLoop", DefaultParallelEvtLoop );
289  REGISTER_VAR_INT( varsInt, "MetalinkProcessing", DefaultMetalinkProcessing );
290  REGISTER_VAR_INT( varsInt, "LocalMetalinkFile", DefaultLocalMetalinkFile );
291  REGISTER_VAR_INT( varsInt, "XCpBlockSize", DefaultXCpBlockSize );
292  REGISTER_VAR_INT( varsInt, "NoDelay", DefaultNoDelay );
293  REGISTER_VAR_INT( varsInt, "AioSignal", DefaultAioSignal );
294  REGISTER_VAR_INT( varsInt, "PreferIPv4", DefaultPreferIPv4 );
295  REGISTER_VAR_INT( varsInt, "MaxMetalinkWait", DefaultMaxMetalinkWait );
296  REGISTER_VAR_INT( varsInt, "PreserveLocateTried", DefaultPreserveLocateTried );
297  REGISTER_VAR_INT( varsInt, "NotAuthorizedRetryLimit", DefaultNotAuthorizedRetryLimit );
298  REGISTER_VAR_INT( varsInt, "PreserveXAttrs", DefaultPreserveXAttrs );
299  REGISTER_VAR_INT( varsInt, "NoTlsOK", DefaultNoTlsOK );
300  REGISTER_VAR_INT( varsInt, "TlsNoData", DefaultTlsNoData );
301  REGISTER_VAR_INT( varsInt, "TlsMetalink", DefaultTlsMetalink );
302  REGISTER_VAR_INT( varsInt, "ZipMtlnCksum", DefaultZipMtlnCksum );
303  REGISTER_VAR_INT( varsInt, "IPNoShuffle", DefaultIPNoShuffle );
304  REGISTER_VAR_INT( varsInt, "WantTlsOnNoPgrw", DefaultWantTlsOnNoPgrw );
305  REGISTER_VAR_INT( varsInt, "RetryWrtAtLBLimit", DefaultRetryWrtAtLBLimit );
306  REGISTER_VAR_INT( varsInt, "XRateThreshold", DefaultXRateThreshold );
307  REGISTER_VAR_INT( varsInt, "CpRetry", DefaultCpRetry );
308  REGISTER_VAR_INT( varsInt, "CpUsePgWrtRd", DefaultCpUsePgWrtRd );
309 
310  REGISTER_VAR_STR( varsStr, "ClientMonitor", DefaultClientMonitor );
311  REGISTER_VAR_STR( varsStr, "ClientMonitorParam", DefaultClientMonitorParam );
312  REGISTER_VAR_STR( varsStr, "NetworkStack", DefaultNetworkStack );
313  REGISTER_VAR_STR( varsStr, "PlugIn", DefaultPlugIn );
314  REGISTER_VAR_STR( varsStr, "PlugInConfDir", DefaultPlugInConfDir );
315  REGISTER_VAR_STR( varsStr, "ReadRecovery", DefaultReadRecovery );
316  REGISTER_VAR_STR( varsStr, "WriteRecovery", DefaultWriteRecovery );
317  REGISTER_VAR_STR( varsStr, "OpenRecovery", DefaultOpenRecovery );
318  REGISTER_VAR_STR( varsStr, "GlfnRedirector", DefaultGlfnRedirector );
319  REGISTER_VAR_STR( varsStr, "TlsDbgLvl", DefaultTlsDbgLvl );
320  REGISTER_VAR_STR( varsStr, "CpTarget", DefaultCpTarget );
321  REGISTER_VAR_STR( varsStr, "CpRetryPolicy", DefaultCpRetryPolicy );
322 
323  //--------------------------------------------------------------------------
324  // Process the configuration files
325  //--------------------------------------------------------------------------
326  std::map<std::string, std::string> config, userConfig;
327  Status st = Utils::ProcessConfig( config, "/etc/xrootd/client.conf" );
328 
329  if( !st.IsOK() )
330  log->Warning( UtilityMsg, "Unable to process global config file: %s",
331  st.ToString().c_str() );
332 
333  XrdSysPwd pwdHandler;
334  passwd *pwd = pwdHandler.Get( getuid() );
335  if( pwd )
336  {
337  std::string userConfigFile = pwd->pw_dir;
338  userConfigFile += "/.xrootd/client.conf";
339 
340  st = Utils::ProcessConfig( userConfig, userConfigFile );
341 
342  if( !st.IsOK() )
343  log->Debug( UtilityMsg, "Unable to process user config file: %s",
344  st.ToString().c_str() );
345  }
346  else
347  log->Debug( UtilityMsg, "Unable to find user home directory." );
348 
349  char *conffile = getenv( "XRD_CLCONFFILE" );
350  if( conffile )
351  {
352  st = Utils::ProcessConfig( userConfig, conffile );
353  if( !st.IsOK() )
354  log->Debug( UtilityMsg, "Unable to process %s file: %s",
355  conffile, st.ToString().c_str() );
356  }
357 
358  char *confdir = getenv( "XRD_CLCONFDIR" );
359  if( confdir )
360  {
361  st = Utils::ProcessConfigDir( userConfig, confdir );
362  if( !st.IsOK() )
363  log->Debug( UtilityMsg, "Unable to process %s file: %s",
364  confdir, st.ToString().c_str() );
365  }
366 
367  std::map<std::string, std::string>::iterator it;
368 
369  for( it = config.begin(); it != config.end(); ++it )
370  log->Dump( UtilityMsg, "[Global config] \"%s\" = \"%s\"",
371  it->first.c_str(), it->second.c_str() );
372 
373  for( it = userConfig.begin(); it != userConfig.end(); ++it )
374  {
375  config[it->first] = it->second;
376  log->Dump( UtilityMsg, "[User config] \"%s\" = \"%s\"",
377  it->first.c_str(), it->second.c_str() );
378  }
379 
380  for( it = config.begin(); it != config.end(); ++it )
381  log->Debug( UtilityMsg, "[Effective config] \"%s\" = \"%s\"",
382  it->first.c_str(), it->second.c_str() );
383 
384  //--------------------------------------------------------------------------
385  // Monitoring settings
386  //--------------------------------------------------------------------------
387  char *tmp = strdup( XrdSysUtils::ExecName() );
388  char *appName = basename( tmp );
389  PutString( "AppName", appName );
390  free( tmp );
391  ImportString( "AppName", "XRD_APPNAME" );
392  PutString( "MonInfo", "" );
393  ImportString( "MonInfo", "XRD_MONINFO" );
394 
395  //--------------------------------------------------------------------------
396  // Process ints
397  //--------------------------------------------------------------------------
398  for( size_t i = 0; i < varsInt.size(); ++i )
399  {
400  PutInt( varsInt[i].name, varsInt[i].def );
401 
402  it = config.find( varsInt[i].name );
403  if( it != config.end() )
404  {
405  char *endPtr = 0;
406  int value = (int)strtol( it->second.c_str(), &endPtr, 0 );
407  if( *endPtr )
408  log->Warning( UtilityMsg, "Unable to set %s to %s: not a proper "
409  "integer", varsInt[i].name.c_str(),
410  it->second.c_str() );
411  else
412  PutInt( varsInt[i].name, value );
413  }
414 
415  std::string name = "XRD_" + varsInt[i].name;
416  std::transform( name.begin(), name.end(), name.begin(), ::toupper );
417  ImportInt( varsInt[i].name, name );
418  }
419 
420  //--------------------------------------------------------------------------
421  // Process strings
422  //--------------------------------------------------------------------------
423  for( size_t i = 0; i < varsStr.size(); ++i )
424  {
425  PutString( varsStr[i].name, varsStr[i].def );
426 
427  it = config.find( varsStr[i].name );
428  if( it != config.end() )
429  PutString( varsStr[i].name, it->second );
430 
431  std::string name = "XRD_" + varsStr[i].name;
432  std::transform( name.begin(), name.end(), name.begin(), ::toupper );
433  ImportString( varsStr[i].name, name );
434  }
435 
436  //--------------------------------------------------------------------------
437  // Register fork handlers
438  //--------------------------------------------------------------------------
439  pthread_atfork( prepare, parent, child );
440  }
441 
442  //----------------------------------------------------------------------------
443  // Get default client environment
444  //----------------------------------------------------------------------------
446  {
447  return sEnv;
448  }
449 
450  //------------------------------------------------------------------------
452  //------------------------------------------------------------------------
454  {
455  return XrdVERSION;
456  }
457 
458  //----------------------------------------------------------------------------
459  // Get default post master
460  //----------------------------------------------------------------------------
462  {
463  PostMaster* postMaster = AtomicGet(sPostMaster);
464 
465  if( unlikely( !postMaster ) )
466  {
467  XrdSysMutexHelper scopedLock( sInitMutex );
468  postMaster = AtomicGet(sPostMaster);
469 
470  if( postMaster )
471  return postMaster;
472 
473  postMaster = new PostMaster();
474 
475  if( !postMaster->Initialize() )
476  {
477  delete postMaster;
478  postMaster = 0;
479  return 0;
480  }
481 
482  if( !postMaster->Start() )
483  {
484  postMaster->Finalize();
485  delete postMaster;
486  postMaster = 0;
487  return 0;
488  }
489 
490  sForkHandler->RegisterPostMaster( postMaster );
491  postMaster->GetTaskManager()->RegisterTask( sFileTimer, time(0), false );
492  AtomicCAS(sPostMaster, sPostMaster, postMaster);
493  }
494 
495  return postMaster;
496  }
497 
498  //----------------------------------------------------------------------------
499  // Get log
500  //----------------------------------------------------------------------------
502  {
503  return sLog;
504  }
505 
506  //----------------------------------------------------------------------------
507  // Set log level
508  //----------------------------------------------------------------------------
509  void DefaultEnv::SetLogLevel( const std::string &level )
510  {
511  Log *log = GetLog();
512  log->SetLevel( level );
513  }
514 
515  //----------------------------------------------------------------------------
516  // Set log file
517  //----------------------------------------------------------------------------
518  bool DefaultEnv::SetLogFile( const std::string &filepath )
519  {
520  Log *log = GetLog();
521  LogOutFile *out = new LogOutFile();
522 
523  if( out->Open( filepath ) )
524  {
525  log->SetOutput( out );
526  return true;
527  }
528 
529  delete out;
530  return false;
531  }
532 
533  //----------------------------------------------------------------------------
535  //------------------------------------------------------------------------
536  void DefaultEnv::SetLogMask( const std::string &level,
537  const std::string &mask )
538  {
539  Log *log = GetLog();
540  MaskTranslator translator;
541  uint64_t topicMask = translator.translateMask( mask );
542 
543  if( level == "All" )
544  {
545  log->SetMask( Log::ErrorMsg, topicMask );
546  log->SetMask( Log::WarningMsg, topicMask );
547  log->SetMask( Log::InfoMsg, topicMask );
548  log->SetMask( Log::DebugMsg, topicMask );
549  log->SetMask( Log::DumpMsg, topicMask );
550  return;
551  }
552 
553  log->SetMask( level, topicMask );
554  }
555 
556  //----------------------------------------------------------------------------
557  // Get fork handler
558  //----------------------------------------------------------------------------
560  {
561  return sForkHandler;
562  }
563 
564  //----------------------------------------------------------------------------
565  // Get fork handler
566  //----------------------------------------------------------------------------
568  {
569  return sFileTimer;
570  }
571 
572  //----------------------------------------------------------------------------
573  // Get the monitor object
574  //----------------------------------------------------------------------------
576  {
577  if( unlikely( !sMonitorInitialized ) )
578  {
579  XrdSysMutexHelper scopedLock( sInitMutex );
580  if( !sMonitorInitialized )
581  {
582  //----------------------------------------------------------------------
583  // Check the environment settings
584  //----------------------------------------------------------------------
585  Env *env = GetEnv();
586  Log *log = GetLog();
587  sMonitorInitialized = true;
588  std::string monitorLib = DefaultClientMonitor;
589  env->GetString( "ClientMonitor", monitorLib );
590  if( monitorLib.empty() )
591  {
592  log->Debug( UtilityMsg, "Monitor library name not set. No "
593  "monitoring" );
594  return 0;
595  }
596 
597  std::string monitorParam = DefaultClientMonitorParam;
598  env->GetString( "ClientMonitorParam", monitorParam );
599 
600  log->Debug( UtilityMsg, "Initializing monitoring, lib: %s, param: %s",
601  monitorLib.c_str(), monitorParam.c_str() );
602 
603  //----------------------------------------------------------------------
604  // Loading the plugin
605  //----------------------------------------------------------------------
606  char *errBuffer = new char[4000];
607  sMonitorLibHandle = new XrdOucPinLoader(
608  errBuffer, 4000, &XrdVERSIONINFOVAR( XrdCl ),
609  "monitor", monitorLib.c_str() );
610 
611  typedef XrdCl::Monitor *(*MonLoader)(const char *, const char *);
612  MonLoader loader;
613  loader = (MonLoader)sMonitorLibHandle->Resolve( "XrdClGetMonitor", -1 );
614  if( !loader )
615  {
616  log->Error( UtilityMsg, "Unable to initialize user monitoring: %s",
617  errBuffer );
618  delete [] errBuffer;
619  sMonitorLibHandle->Unload();
620  delete sMonitorLibHandle; sMonitorLibHandle = 0;
621  return 0;
622  }
623 
624  //----------------------------------------------------------------------
625  // Instantiating the monitor object
626  //----------------------------------------------------------------------
627  const char *param = monitorParam.empty() ? 0 : monitorParam.c_str();
628  sMonitor = (*loader)( XrdSysUtils::ExecName(), param );
629 
630  if( !sMonitor )
631  {
632  log->Error( UtilityMsg, "Unable to initialize user monitoring: %s",
633  errBuffer );
634  delete [] errBuffer;
635  sMonitorLibHandle->Unload();
636  delete sMonitorLibHandle; sMonitorLibHandle = 0;
637  return 0;
638  }
639  log->Debug( UtilityMsg, "Successfully initialized monitoring from: %s",
640  monitorLib.c_str() );
641  delete [] errBuffer;
642  }
643  }
644  return sMonitor;
645  }
646 
647  //----------------------------------------------------------------------------
648  // Get checksum manager
649  //----------------------------------------------------------------------------
651  {
652  if( unlikely( !sCheckSumManager ) )
653  {
654  XrdSysMutexHelper scopedLock( sInitMutex );
655  if( !sCheckSumManager )
656  sCheckSumManager = new CheckSumManager();
657  }
658  return sCheckSumManager;
659  }
660 
661  //----------------------------------------------------------------------------
662  // Get transport manager
663  //----------------------------------------------------------------------------
665  {
666  if( unlikely( !sTransportManager ) )
667  {
668  XrdSysMutexHelper scopedLock( sInitMutex );
669  if( !sTransportManager )
670  sTransportManager = new TransportManager();
671  }
672  return sTransportManager;
673  }
674 
675  //----------------------------------------------------------------------------
676  // Get plug-in manager
677  //----------------------------------------------------------------------------
679  {
680  return sPlugInManager;
681  }
682 
683  //----------------------------------------------------------------------------
684  // Retrieve the plug-in factory for the given URL
685  //----------------------------------------------------------------------------
686  PlugInFactory *DefaultEnv::GetPlugInFactory( const std::string url )
687  {
688  return sPlugInManager->GetFactory( url );
689  }
690 
691  //----------------------------------------------------------------------------
692  // Initialize the environment
693  //----------------------------------------------------------------------------
694  void DefaultEnv::Initialize()
695  {
696  sLog = new Log();
697  SetUpLog();
698 
699  sEnv = new DefaultEnv();
700  sForkHandler = new ForkHandler();
701  sFileTimer = new FileTimer();
702  sPlugInManager = new PlugInManager();
703 
704  sPlugInManager->ProcessEnvironmentSettings();
705  sForkHandler->RegisterFileTimer( sFileTimer );
706 
707  //--------------------------------------------------------------------------
708  // MacOSX library loading is completely moronic. We cannot dlopen a library
709  // from a thread other than a main thread, so we-pre dlopen all the
710  // libraries that we may potentially want.
711  //--------------------------------------------------------------------------
712 #ifdef __APPLE__
713  char *errBuff = new char[1024];
714 
715  const char *libs[] =
716  {
717  "libXrdSeckrb5.so",
718  "libXrdSecgsi.so",
719  "libXrdSecgsiAuthzVO.so",
720  "libXrdSecgsiGMAPDN.so",
721  "libXrdSecpwd.so",
722  "libXrdSecsss.so",
723  "libXrdSecunix.so",
724  0
725  };
726 
727  for( int i = 0; libs[i]; ++i )
728  {
729  sLog->Debug( UtilityMsg, "Attempting to pre-load: %s", libs[i] );
730  bool ok = XrdOucPreload( libs[i], errBuff, 1024 );
731  if( !ok )
732  sLog->Error( UtilityMsg, "Unable to pre-load %s: %s", libs[i], errBuff );
733  }
734  delete [] errBuff;
735 #endif
736  }
737 
738  //----------------------------------------------------------------------------
739  // Finalize the environment
740  //----------------------------------------------------------------------------
741  void DefaultEnv::Finalize()
742  {
743  if( sPostMaster )
744  {
745  sPostMaster->Stop();
746  sPostMaster->Finalize();
747  delete sPostMaster;
748  sPostMaster = 0;
749  }
750 
751  delete sTransportManager;
752  sTransportManager = 0;
753 
754  delete sCheckSumManager;
755  sCheckSumManager = 0;
756 
757  delete sMonitor;
758  sMonitor = 0;
759 
760  if( sMonitorLibHandle )
761  sMonitorLibHandle->Unload();
762 
763  delete sMonitorLibHandle;
764  sMonitorLibHandle = 0;
765 
766  delete sForkHandler;
767  sForkHandler = 0;
768 
769  delete sFileTimer;
770  sFileTimer = 0;
771 
772  delete sPlugInManager;
773  sPlugInManager = 0;
774 
775  delete sEnv;
776  sEnv = 0;
777 
778  delete sLog;
779  sLog = 0;
780  }
781 
782  //----------------------------------------------------------------------------
783  // Re-initialize the logging
784  //----------------------------------------------------------------------------
786  {
787  delete sLog;
788  sLog = new Log();
789  SetUpLog();
790  }
791 
792  //----------------------------------------------------------------------------
793  // Set up the log
794  //----------------------------------------------------------------------------
795  void DefaultEnv::SetUpLog()
796  {
797  Log *log = GetLog();
798 
799  //--------------------------------------------------------------------------
800  // Check if the log level has been defined in the environment
801  //--------------------------------------------------------------------------
802  char *level = getenv( "XRD_LOGLEVEL" );
803  if( level )
804  log->SetLevel( level );
805 
806  //--------------------------------------------------------------------------
807  // Check if we need to log to a file
808  //--------------------------------------------------------------------------
809  char *file = getenv( "XRD_LOGFILE" );
810  if( file )
811  {
812  LogOutFile *out = new LogOutFile();
813  if( out->Open( file ) )
814  log->SetOutput( out );
815  else
816  delete out;
817  }
818 
819  //--------------------------------------------------------------------------
820  // Log mask defaults
821  //--------------------------------------------------------------------------
822  MaskTranslator translator;
823  log->SetMask( Log::DumpMsg, translator.translateMask( "All|^PollerMsg" ) );
824 
825  //--------------------------------------------------------------------------
826  // Initialize the topic mask
827  //--------------------------------------------------------------------------
828  char *logMask = getenv( "XRD_LOGMASK" );
829  if( logMask )
830  {
831  uint64_t mask = translator.translateMask( logMask );
832  log->SetMask( Log::ErrorMsg, mask );
833  log->SetMask( Log::WarningMsg, mask );
834  log->SetMask( Log::InfoMsg, mask );
835  log->SetMask( Log::DebugMsg, mask );
836  log->SetMask( Log::DumpMsg, mask );
837  }
838 
839  logMask = getenv( "XRD_LOGMASK_ERROR" );
840  if( logMask ) log->SetMask( Log::ErrorMsg, translator.translateMask( logMask ) );
841 
842  logMask = getenv( "XRD_LOGMASK_WARNING" );
843  if( logMask ) log->SetMask( Log::WarningMsg, translator.translateMask( logMask ) );
844 
845  logMask = getenv( "XRD_LOGMASK_INFO" );
846  if( logMask ) log->SetMask( Log::InfoMsg, translator.translateMask( logMask ) );
847 
848  logMask = getenv( "XRD_LOGMASK_DEBUG" );
849  if( logMask ) log->SetMask( Log::DebugMsg, translator.translateMask( logMask ) );
850 
851  logMask = getenv( "XRD_LOGMASK_DUMP" );
852  if( logMask ) log->SetMask( Log::DumpMsg, translator.translateMask( logMask ) );
853 
854  //--------------------------------------------------------------------------
855  // Set up the topic strings
856  //--------------------------------------------------------------------------
857  log->SetTopicName( AppMsg, "App" );
858  log->SetTopicName( UtilityMsg, "Utility" );
859  log->SetTopicName( FileMsg, "File" );
860  log->SetTopicName( PollerMsg, "Poller" );
861  log->SetTopicName( PostMasterMsg, "PostMaster" );
862  log->SetTopicName( XRootDTransportMsg, "XRootDTransport" );
863  log->SetTopicName( TaskMgrMsg, "TaskMgr" );
864  log->SetTopicName( XRootDMsg, "XRootD" );
865  log->SetTopicName( FileSystemMsg, "FileSystem" );
866  log->SetTopicName( AsyncSockMsg, "AsyncSock" );
867  log->SetTopicName( JobMgrMsg, "JobMgr" );
868  log->SetTopicName( PlugInMgrMsg, "PlugInMgr" );
869  log->SetTopicName( ExDbgMsg, "ExDbgMsg" );
870  log->SetTopicName( TlsMsg, "TlsMsg" );
871  log->SetTopicName( ZipMsg, "ZipMsg" );
872  }
873 
874 }
875 
876 //------------------------------------------------------------------------------
877 // Static initialization and finalization
878 //------------------------------------------------------------------------------
880 
881 //------------------------------------------------------------------------------
882 // The constructor will be invoked in every translation unit
883 // that includes XrdClDefaultEnv.hh, but the DefaultEnv will
884 // be initialized only in the first one
885 //------------------------------------------------------------------------------
887 {
888  if( counter++ == 0 ) XrdCl::DefaultEnv::Initialize();
889 }
890 
891 //------------------------------------------------------------------------------
892 // The destructor will be invoked in every translation unit
893 // that includes XrdClDefaultEnv.hh, but the DefaultEnv will
894 // be finalized only once in the last one
895 //------------------------------------------------------------------------------
897 {
898  if( --counter == 0 ) XrdCl::DefaultEnv::Finalize();
899 }
900 
static void child()
XrdVERSIONINFO(XrdCl, client)
#define REGISTER_VAR_INT(array, name, def)
#define REGISTER_VAR_STR(array, name, def)
static void prepare()
static void parent()
#define unlikely(x)
bool XrdOucPreload(const char *plib, char *eBuff, int eBlen, bool retry)
This include file define a utility function that pre-loads a plugin.
#define AtomicCAS(x, y, z)
#define AtomicGet(x)
Manage the checksum calc objects.
static PlugInManager * GetPlugInManager()
Get plug-in manager.
static bool SetLogFile(const std::string &filepath)
static std::string GetVersion()
Get client version.
static CheckSumManager * GetCheckSumManager()
Get checksum manager.
static Monitor * GetMonitor()
Get the monitor object.
static TransportManager * GetTransportManager()
Get transport manager.
static PlugInFactory * GetPlugInFactory(const std::string url)
static Log * GetLog()
Get default log.
static PostMaster * GetPostMaster()
Get default post master.
static void SetLogLevel(const std::string &level)
static void SetLogMask(const std::string &level, const std::string &mask)
Set log mask.
static FileTimer * GetFileTimer()
Get file timer task.
static ForkHandler * GetForkHandler()
Get the fork handler.
static Env * GetEnv()
Get default client environment.
static void ReInitializeLogging()
Re-initialize the logging.
void RecreateLock()
Definition: XrdClEnv.hh:171
bool PutInt(const std::string &key, int value)
Definition: XrdClEnv.cc:136
bool PutString(const std::string &key, const std::string &value)
Definition: XrdClEnv.cc:52
bool ImportString(const std::string &key, const std::string &shellKey)
Definition: XrdClEnv.cc:266
bool ImportInt(const std::string &key, const std::string &shellKey)
Definition: XrdClEnv.cc:237
bool GetString(const std::string &key, std::string &value)
Definition: XrdClEnv.cc:31
bool GetInt(const std::string &key, int &value)
Definition: XrdClEnv.cc:115
Task generating timeout events for FileStateHandlers in recovery mode.
void Parent()
Handle the parent post-fork.
void Prepare()
Handle the preparation part of the forking process.
void Child()
Handler the child post-fork.
void RegisterFileTimer(FileTimer *fileTimer)
void RegisterPostMaster(PostMaster *postMaster)
Register a post master object.
Write log messages to a file.
Definition: XrdClLog.hh:65
bool Open(const std::string &fileName)
Open the log file.
Definition: XrdClLog.cc:39
Handle diagnostics.
Definition: XrdClLog.hh:101
@ InfoMsg
print info
Definition: XrdClLog.hh:111
@ WarningMsg
report warnings
Definition: XrdClLog.hh:110
@ DebugMsg
print debug info
Definition: XrdClLog.hh:112
@ ErrorMsg
report errors
Definition: XrdClLog.hh:109
@ DumpMsg
print details of the request and responses
Definition: XrdClLog.hh:113
void SetMask(LogLevel level, uint64_t mask)
Sets the mask for the topics of messages that should be printed.
Definition: XrdClLog.hh:224
void SetTopicName(uint64_t topic, std::string name)
Map a topic number to a string.
Definition: XrdClLog.cc:163
void SetLevel(LogLevel level)
Set the level of the messages that should be sent to the destination.
Definition: XrdClLog.hh:193
void Error(uint64_t topic, const char *format,...)
Report an error.
Definition: XrdClLog.cc:231
void Warning(uint64_t topic, const char *format,...)
Report a warning.
Definition: XrdClLog.cc:248
void Dump(uint64_t topic, const char *format,...)
Print a dump message.
Definition: XrdClLog.cc:299
void Debug(uint64_t topic, const char *format,...)
Print a debug message.
Definition: XrdClLog.cc:282
void SetOutput(LogOut *output)
Set the output that should be used.
Definition: XrdClLog.hh:215
void SetPid(pid_t pid)
Set pid.
Definition: XrdClLog.hh:267
An abstract class to describe the client-side monitoring plugin interface.
Definition: XrdClMonitor.hh:56
Manage client-side plug-ins and match them agains URLs.
PlugInFactory * GetFactory(const std::string url)
A hub for dispatching and receiving messages.
bool Start()
Start the post master.
bool Finalize()
Finalizer.
bool Stop()
Stop the postmaster.
TaskManager * GetTaskManager()
Get the task manager object user by the post master.
bool Initialize()
Initializer.
void RegisterTask(Task *task, time_t time, bool own=true)
Manage transport handler objects.
static Status ProcessConfig(std::map< std::string, std::string > &config, const std::string &file)
Process a config file and return key-value pairs.
Definition: XrdClUtils.cc:534
static Status ProcessConfigDir(std::map< std::string, std::string > &config, const std::string &dir)
Process a config directory and return key-value pairs.
Definition: XrdClUtils.cc:566
static void splitString(Container &result, const std::string &input, const std::string &delimiter)
Split a string.
Definition: XrdClUtils.hh:56
void * Resolve(const char *symbl, int mcnt=1)
void Unload(bool dodel=false)
struct passwd * Get(const char *Usr)
Definition: XrdSysPwd.hh:42
static const char * ExecName()
Definition: XrdSysUtils.cc:60
const int DefaultPreserveLocateTried
const int DefaultRunForkHandler
const int DefaultRedirectLimit
const int DefaultCPInitTimeout
const int DefaultXRateThreshold
const int DefaultLoadBalancerTTL
const uint64_t XRootDTransportMsg
const int DefaultMaxMetalinkWait
const char *const DefaultCpTarget
const int DefaultTCPKeepAliveProbes
const int DefaultCPChunkSize
const char *const DefaultClientMonitorParam
const uint64_t AppMsg
const uint64_t PollerMsg
const int DefaultRetryWrtAtLBLimit
const uint64_t PostMasterMsg
const int DefaultWantTlsOnNoPgrw
const char *const DefaultOpenRecovery
const uint64_t ZipMsg
const int DefaultStreamErrorWindow
const char *const DefaultPlugIn
const uint64_t XRootDMsg
const int DefaultMultiProtocol
const int DefaultConnectionRetry
const int DefaultIPNoShuffle
const int DefaultSubStreamsPerChannel
const int DefaultConnectionWindow
const uint64_t FileMsg
const int DefaultDataServerTTL
const uint64_t ExDbgMsg
const int DefaultCPParallelChunks
const int DefaultTlsMetalink
const int DefaultTimeoutResolution
const char *const DefaultNetworkStack
const int DefaultStreamTimeout
const int DefaultLocalMetalinkFile
const int DefaultCpUsePgWrtRd
const int DefaultMetalinkProcessing
const uint64_t AsyncSockMsg
const char *const DefaultPlugInConfDir
const int DefaultTCPKeepAliveInterval
const int DefaultXCpBlockSize
const uint64_t PlugInMgrMsg
const uint64_t UtilityMsg
const char *const DefaultReadRecovery
const int DefaultCPTimeout
const uint64_t TlsMsg
const int DefaultTCPKeepAlive
const int DefaultCpRetry
const int DefaultRequestTimeout
const int DefaultTCPKeepAliveTime
const int DefaultPreserveXAttrs
const int DefaultPreferIPv4
const char *const DefaultWriteRecovery
const char *const DefaultGlfnRedirector
const uint64_t TaskMgrMsg
const char *const DefaultCpRetryPolicy
const int DefaultNotAuthorizedRetryLimit
const int DefaultWorkerThreads
const uint64_t FileSystemMsg
const int DefaultAioSignal
const int DefaultCPTPCTimeout
const int DefaultNoDelay
const int DefaultTlsNoData
const char *const DefaultTlsDbgLvl
const int DefaultNoTlsOK
const uint64_t JobMgrMsg
const char *const DefaultClientMonitor
const int DefaultParallelEvtLoop
const int DefaultZipMtlnCksum
XrdSysError Log
Definition: XrdConfig.cc:113
static int counter
Procedure execution status.
Definition: XrdClStatus.hh:115
bool IsOK() const
We're fine.
Definition: XrdClStatus.hh:124
std::string ToString() const
Create a string representation.
Definition: XrdClStatus.cc:97