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  //----------------------------------------------------------------------------
451  // Get default post master
452  //----------------------------------------------------------------------------
454  {
455  PostMaster* postMaster = AtomicGet(sPostMaster);
456 
457  if( unlikely( !postMaster ) )
458  {
459  XrdSysMutexHelper scopedLock( sInitMutex );
460  postMaster = AtomicGet(sPostMaster);
461 
462  if( postMaster )
463  return postMaster;
464 
465  postMaster = new PostMaster();
466 
467  if( !postMaster->Initialize() )
468  {
469  delete postMaster;
470  postMaster = 0;
471  return 0;
472  }
473 
474  if( !postMaster->Start() )
475  {
476  postMaster->Finalize();
477  delete postMaster;
478  postMaster = 0;
479  return 0;
480  }
481 
482  sForkHandler->RegisterPostMaster( postMaster );
483  postMaster->GetTaskManager()->RegisterTask( sFileTimer, time(0), false );
484  AtomicCAS(sPostMaster, sPostMaster, postMaster);
485  }
486 
487  return postMaster;
488  }
489 
490  //----------------------------------------------------------------------------
491  // Get log
492  //----------------------------------------------------------------------------
494  {
495  return sLog;
496  }
497 
498  //----------------------------------------------------------------------------
499  // Set log level
500  //----------------------------------------------------------------------------
501  void DefaultEnv::SetLogLevel( const std::string &level )
502  {
503  Log *log = GetLog();
504  log->SetLevel( level );
505  }
506 
507  //----------------------------------------------------------------------------
508  // Set log file
509  //----------------------------------------------------------------------------
510  bool DefaultEnv::SetLogFile( const std::string &filepath )
511  {
512  Log *log = GetLog();
513  LogOutFile *out = new LogOutFile();
514 
515  if( out->Open( filepath ) )
516  {
517  log->SetOutput( out );
518  return true;
519  }
520 
521  delete out;
522  return false;
523  }
524 
525  //----------------------------------------------------------------------------
527  //------------------------------------------------------------------------
528  void DefaultEnv::SetLogMask( const std::string &level,
529  const std::string &mask )
530  {
531  Log *log = GetLog();
532  MaskTranslator translator;
533  uint64_t topicMask = translator.translateMask( mask );
534 
535  if( level == "All" )
536  {
537  log->SetMask( Log::ErrorMsg, topicMask );
538  log->SetMask( Log::WarningMsg, topicMask );
539  log->SetMask( Log::InfoMsg, topicMask );
540  log->SetMask( Log::DebugMsg, topicMask );
541  log->SetMask( Log::DumpMsg, topicMask );
542  return;
543  }
544 
545  log->SetMask( level, topicMask );
546  }
547 
548  //----------------------------------------------------------------------------
549  // Get fork handler
550  //----------------------------------------------------------------------------
552  {
553  return sForkHandler;
554  }
555 
556  //----------------------------------------------------------------------------
557  // Get fork handler
558  //----------------------------------------------------------------------------
560  {
561  return sFileTimer;
562  }
563 
564  //----------------------------------------------------------------------------
565  // Get the monitor object
566  //----------------------------------------------------------------------------
568  {
569  if( unlikely( !sMonitorInitialized ) )
570  {
571  XrdSysMutexHelper scopedLock( sInitMutex );
572  if( !sMonitorInitialized )
573  {
574  //----------------------------------------------------------------------
575  // Check the environment settings
576  //----------------------------------------------------------------------
577  Env *env = GetEnv();
578  Log *log = GetLog();
579  sMonitorInitialized = true;
580  std::string monitorLib = DefaultClientMonitor;
581  env->GetString( "ClientMonitor", monitorLib );
582  if( monitorLib.empty() )
583  {
584  log->Debug( UtilityMsg, "Monitor library name not set. No "
585  "monitoring" );
586  return 0;
587  }
588 
589  std::string monitorParam = DefaultClientMonitorParam;
590  env->GetString( "ClientMonitorParam", monitorParam );
591 
592  log->Debug( UtilityMsg, "Initializing monitoring, lib: %s, param: %s",
593  monitorLib.c_str(), monitorParam.c_str() );
594 
595  //----------------------------------------------------------------------
596  // Loading the plugin
597  //----------------------------------------------------------------------
598  char *errBuffer = new char[4000];
599  sMonitorLibHandle = new XrdOucPinLoader(
600  errBuffer, 4000, &XrdVERSIONINFOVAR( XrdCl ),
601  "monitor", monitorLib.c_str() );
602 
603  typedef XrdCl::Monitor *(*MonLoader)(const char *, const char *);
604  MonLoader loader;
605  loader = (MonLoader)sMonitorLibHandle->Resolve( "XrdClGetMonitor", -1 );
606  if( !loader )
607  {
608  log->Error( UtilityMsg, "Unable to initialize user monitoring: %s",
609  errBuffer );
610  delete [] errBuffer;
611  sMonitorLibHandle->Unload();
612  delete sMonitorLibHandle; sMonitorLibHandle = 0;
613  return 0;
614  }
615 
616  //----------------------------------------------------------------------
617  // Instantiating the monitor object
618  //----------------------------------------------------------------------
619  const char *param = monitorParam.empty() ? 0 : monitorParam.c_str();
620  sMonitor = (*loader)( XrdSysUtils::ExecName(), param );
621 
622  if( !sMonitor )
623  {
624  log->Error( UtilityMsg, "Unable to initialize user monitoring: %s",
625  errBuffer );
626  delete [] errBuffer;
627  sMonitorLibHandle->Unload();
628  delete sMonitorLibHandle; sMonitorLibHandle = 0;
629  return 0;
630  }
631  log->Debug( UtilityMsg, "Successfully initialized monitoring from: %s",
632  monitorLib.c_str() );
633  delete [] errBuffer;
634  }
635  }
636  return sMonitor;
637  }
638 
639  //----------------------------------------------------------------------------
640  // Get checksum manager
641  //----------------------------------------------------------------------------
643  {
644  if( unlikely( !sCheckSumManager ) )
645  {
646  XrdSysMutexHelper scopedLock( sInitMutex );
647  if( !sCheckSumManager )
648  sCheckSumManager = new CheckSumManager();
649  }
650  return sCheckSumManager;
651  }
652 
653  //----------------------------------------------------------------------------
654  // Get transport manager
655  //----------------------------------------------------------------------------
657  {
658  if( unlikely( !sTransportManager ) )
659  {
660  XrdSysMutexHelper scopedLock( sInitMutex );
661  if( !sTransportManager )
662  sTransportManager = new TransportManager();
663  }
664  return sTransportManager;
665  }
666 
667  //----------------------------------------------------------------------------
668  // Get plug-in manager
669  //----------------------------------------------------------------------------
671  {
672  return sPlugInManager;
673  }
674 
675  //----------------------------------------------------------------------------
676  // Retrieve the plug-in factory for the given URL
677  //----------------------------------------------------------------------------
678  PlugInFactory *DefaultEnv::GetPlugInFactory( const std::string url )
679  {
680  return sPlugInManager->GetFactory( url );
681  }
682 
683  //----------------------------------------------------------------------------
684  // Initialize the environment
685  //----------------------------------------------------------------------------
686  void DefaultEnv::Initialize()
687  {
688  sLog = new Log();
689  SetUpLog();
690 
691  sEnv = new DefaultEnv();
692  sForkHandler = new ForkHandler();
693  sFileTimer = new FileTimer();
694  sPlugInManager = new PlugInManager();
695 
696  sPlugInManager->ProcessEnvironmentSettings();
697  sForkHandler->RegisterFileTimer( sFileTimer );
698 
699  //--------------------------------------------------------------------------
700  // MacOSX library loading is completely moronic. We cannot dlopen a library
701  // from a thread other than a main thread, so we-pre dlopen all the
702  // libraries that we may potentially want.
703  //--------------------------------------------------------------------------
704 #ifdef __APPLE__
705  char *errBuff = new char[1024];
706 
707  const char *libs[] =
708  {
709  "libXrdSeckrb5.so",
710  "libXrdSecgsi.so",
711  "libXrdSecgsiAuthzVO.so",
712  "libXrdSecgsiGMAPDN.so",
713  "libXrdSecpwd.so",
714  "libXrdSecsss.so",
715  "libXrdSecunix.so",
716  0
717  };
718 
719  for( int i = 0; libs[i]; ++i )
720  {
721  sLog->Debug( UtilityMsg, "Attempting to pre-load: %s", libs[i] );
722  bool ok = XrdOucPreload( libs[i], errBuff, 1024 );
723  if( !ok )
724  sLog->Error( UtilityMsg, "Unable to pre-load %s: %s", libs[i], errBuff );
725  }
726  delete [] errBuff;
727 #endif
728  }
729 
730  //----------------------------------------------------------------------------
731  // Finalize the environment
732  //----------------------------------------------------------------------------
733  void DefaultEnv::Finalize()
734  {
735  if( sPostMaster )
736  {
737  sPostMaster->Stop();
738  sPostMaster->Finalize();
739  delete sPostMaster;
740  sPostMaster = 0;
741  }
742 
743  delete sTransportManager;
744  sTransportManager = 0;
745 
746  delete sCheckSumManager;
747  sCheckSumManager = 0;
748 
749  delete sMonitor;
750  sMonitor = 0;
751 
752  if( sMonitorLibHandle )
753  sMonitorLibHandle->Unload();
754 
755  delete sMonitorLibHandle;
756  sMonitorLibHandle = 0;
757 
758  delete sForkHandler;
759  sForkHandler = 0;
760 
761  delete sFileTimer;
762  sFileTimer = 0;
763 
764  delete sPlugInManager;
765  sPlugInManager = 0;
766 
767  delete sEnv;
768  sEnv = 0;
769 
770  delete sLog;
771  sLog = 0;
772  }
773 
774  //----------------------------------------------------------------------------
775  // Re-initialize the logging
776  //----------------------------------------------------------------------------
778  {
779  delete sLog;
780  sLog = new Log();
781  SetUpLog();
782  }
783 
784  //----------------------------------------------------------------------------
785  // Set up the log
786  //----------------------------------------------------------------------------
787  void DefaultEnv::SetUpLog()
788  {
789  Log *log = GetLog();
790 
791  //--------------------------------------------------------------------------
792  // Check if the log level has been defined in the environment
793  //--------------------------------------------------------------------------
794  char *level = getenv( "XRD_LOGLEVEL" );
795  if( level )
796  log->SetLevel( level );
797 
798  //--------------------------------------------------------------------------
799  // Check if we need to log to a file
800  //--------------------------------------------------------------------------
801  char *file = getenv( "XRD_LOGFILE" );
802  if( file )
803  {
804  LogOutFile *out = new LogOutFile();
805  if( out->Open( file ) )
806  log->SetOutput( out );
807  else
808  delete out;
809  }
810 
811  //--------------------------------------------------------------------------
812  // Log mask defaults
813  //--------------------------------------------------------------------------
814  MaskTranslator translator;
815  log->SetMask( Log::DumpMsg, translator.translateMask( "All|^PollerMsg" ) );
816 
817  //--------------------------------------------------------------------------
818  // Initialize the topic mask
819  //--------------------------------------------------------------------------
820  char *logMask = getenv( "XRD_LOGMASK" );
821  if( logMask )
822  {
823  uint64_t mask = translator.translateMask( logMask );
824  log->SetMask( Log::ErrorMsg, mask );
825  log->SetMask( Log::WarningMsg, mask );
826  log->SetMask( Log::InfoMsg, mask );
827  log->SetMask( Log::DebugMsg, mask );
828  log->SetMask( Log::DumpMsg, mask );
829  }
830 
831  logMask = getenv( "XRD_LOGMASK_ERROR" );
832  if( logMask ) log->SetMask( Log::ErrorMsg, translator.translateMask( logMask ) );
833 
834  logMask = getenv( "XRD_LOGMASK_WARNING" );
835  if( logMask ) log->SetMask( Log::WarningMsg, translator.translateMask( logMask ) );
836 
837  logMask = getenv( "XRD_LOGMASK_INFO" );
838  if( logMask ) log->SetMask( Log::InfoMsg, translator.translateMask( logMask ) );
839 
840  logMask = getenv( "XRD_LOGMASK_DEBUG" );
841  if( logMask ) log->SetMask( Log::DebugMsg, translator.translateMask( logMask ) );
842 
843  logMask = getenv( "XRD_LOGMASK_DUMP" );
844  if( logMask ) log->SetMask( Log::DumpMsg, translator.translateMask( logMask ) );
845 
846  //--------------------------------------------------------------------------
847  // Set up the topic strings
848  //--------------------------------------------------------------------------
849  log->SetTopicName( AppMsg, "App" );
850  log->SetTopicName( UtilityMsg, "Utility" );
851  log->SetTopicName( FileMsg, "File" );
852  log->SetTopicName( PollerMsg, "Poller" );
853  log->SetTopicName( PostMasterMsg, "PostMaster" );
854  log->SetTopicName( XRootDTransportMsg, "XRootDTransport" );
855  log->SetTopicName( TaskMgrMsg, "TaskMgr" );
856  log->SetTopicName( XRootDMsg, "XRootD" );
857  log->SetTopicName( FileSystemMsg, "FileSystem" );
858  log->SetTopicName( AsyncSockMsg, "AsyncSock" );
859  log->SetTopicName( JobMgrMsg, "JobMgr" );
860  log->SetTopicName( PlugInMgrMsg, "PlugInMgr" );
861  log->SetTopicName( ExDbgMsg, "ExDbgMsg" );
862  log->SetTopicName( TlsMsg, "TlsMsg" );
863  log->SetTopicName( ZipMsg, "ZipMsg" );
864  }
865 
866 }
867 
868 //------------------------------------------------------------------------------
869 // Static initialization and finalization
870 //------------------------------------------------------------------------------
872 
873 //------------------------------------------------------------------------------
874 // The constructor will be invoked in every translation unit
875 // that includes XrdClDefaultEnv.hh, but the DefaultEnv will
876 // be initialized only in the first one
877 //------------------------------------------------------------------------------
879 {
880  if( counter++ == 0 ) XrdCl::DefaultEnv::Initialize();
881 }
882 
883 //------------------------------------------------------------------------------
884 // The destructor will be invoked in every translation unit
885 // that includes XrdClDefaultEnv.hh, but the DefaultEnv will
886 // be finalized only once in the last one
887 //------------------------------------------------------------------------------
889 {
890  if( --counter == 0 ) XrdCl::DefaultEnv::Finalize();
891 }
892 
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 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:141
bool PutInt(const std::string &key, int value)
Definition: XrdClEnv.cc:110
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:177
bool ImportInt(const std::string &key, const std::string &shellKey)
Definition: XrdClEnv.cc:148
bool GetString(const std::string &key, std::string &value)
Definition: XrdClEnv.cc:31
bool GetInt(const std::string &key, int &value)
Definition: XrdClEnv.cc:89
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:112
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