xrootd
XrdClUtils.hh
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 #ifndef __XRD_CL_UTILS_HH__
20 #define __XRD_CL_UTILS_HH__
21 
22 #include <string>
23 #include <vector>
24 #include "XrdCl/XrdClStatus.hh"
25 #include "XrdCl/XrdClLog.hh"
26 #include "XrdCl/XrdClURL.hh"
29 #include "XrdCl/XrdClDefaultEnv.hh"
30 #include "XrdCl/XrdClConstants.hh"
31 #include "XrdNet/XrdNetUtils.hh"
32 
33 #include <sys/time.h>
34 
35 #ifdef __linux__
36 #include <sys/fsuid.h>
37 #endif
38 
39 namespace XrdCl
40 {
41  //----------------------------------------------------------------------------
43  //----------------------------------------------------------------------------
44  class Utils
45  {
46  public:
47  //------------------------------------------------------------------------
49  //------------------------------------------------------------------------
50  template<class Container>
51  static void splitString( Container &result,
52  const std::string &input,
53  const std::string &delimiter )
54  {
55  size_t start = 0;
56  size_t end = 0;
57  size_t length = 0;
58 
59  do
60  {
61  end = input.find( delimiter, start );
62 
63  if( end != std::string::npos )
64  length = end - start;
65  else
66  length = input.length() - start;
67 
68  if( length )
69  result.push_back( input.substr( start, length ) );
70 
71  start = end + delimiter.size();
72  }
73  while( end != std::string::npos );
74  }
75 
76  //------------------------------------------------------------------------
78  //------------------------------------------------------------------------
79  static int GetIntParameter( const URL &url,
80  const std::string &name,
81  int defaultVal );
82 
83  //------------------------------------------------------------------------
85  //------------------------------------------------------------------------
86  static std::string GetStringParameter( const URL &url,
87  const std::string &name,
88  const std::string &defaultVal );
89 
90  //------------------------------------------------------------------------
92  //------------------------------------------------------------------------
94  {
95  IPAuto = 0,
96  IPAll = 1,
97  IPv6 = 2,
98  IPv4 = 3,
100  };
101 
102  //------------------------------------------------------------------------
104  //------------------------------------------------------------------------
105  static AddressType String2AddressType( const std::string &addressType );
106 
107  //------------------------------------------------------------------------
109  //------------------------------------------------------------------------
110  static Status GetHostAddresses( std::vector<XrdNetAddr> &addresses,
111  const URL &url,
112  AddressType type );
113 
114  //------------------------------------------------------------------------
116  //------------------------------------------------------------------------
117  static void LogHostAddresses( Log *log,
118  uint64_t type,
119  const std::string &hostId,
120  std::vector<XrdNetAddr> &addresses );
121 
122  //------------------------------------------------------------------------
124  //------------------------------------------------------------------------
125  static std::string TimeToString( time_t timestamp );
126 
127  //------------------------------------------------------------------------
129  //------------------------------------------------------------------------
130  static uint64_t GetElapsedMicroSecs( timeval start, timeval end );
131 
132  //------------------------------------------------------------------------
134  //------------------------------------------------------------------------
135  static XRootDStatus GetRemoteCheckSum( std::string &checkSum,
136  const std::string &checkSumType,
137  const std::string &server,
138  const std::string &path );
139 
140  //------------------------------------------------------------------------
142  //------------------------------------------------------------------------
143  static XRootDStatus GetLocalCheckSum( std::string &checkSum,
144  const std::string &checkSumType,
145  const std::string &path );
146 
147  //------------------------------------------------------------------------
149  //------------------------------------------------------------------------
150  static std::string BytesToString( uint64_t bytes );
151 
152  //------------------------------------------------------------------------
154  //------------------------------------------------------------------------
155  static XRootDStatus CheckTPC( const std::string &server,
156  uint16_t timeout = 0 );
157 
158  //------------------------------------------------------------------------
163  //------------------------------------------------------------------------
164  static XRootDStatus CheckTPCLite( const std::string &server,
165  uint16_t timeout = 0 );
166 
167  //------------------------------------------------------------------------
169  //------------------------------------------------------------------------
170  static std::string FQDNToCC( const std::string &fqdn );
171 
172  //------------------------------------------------------------------------
174  //------------------------------------------------------------------------
175  static Status GetDirectoryEntries( std::vector<std::string> &entries,
176  const std::string &path );
177 
178  //------------------------------------------------------------------------
180  //------------------------------------------------------------------------
181  static Status ProcessConfig( std::map<std::string, std::string> &config,
182  const std::string &file );
183 
184  //------------------------------------------------------------------------
186  //------------------------------------------------------------------------
187  static void Trim( std::string &str );
188 
189  //------------------------------------------------------------------------
191  //------------------------------------------------------------------------
192  static void LogPropertyList( Log *log,
193  uint64_t topic,
194  const char *format,
195  const PropertyList &list );
196 
197  //------------------------------------------------------------------------
199  //------------------------------------------------------------------------
200  static std::string Char2Hex( uint8_t *array, uint16_t size );
201 
202  //------------------------------------------------------------------------
204  //------------------------------------------------------------------------
205  static std::string NormalizeChecksum( const std::string &name,
206  const std::string &checksum );
207  };
208 
209  //----------------------------------------------------------------------------
211  //----------------------------------------------------------------------------
213  {
214  public:
215  //------------------------------------------------------------------------
217  //------------------------------------------------------------------------
218  ScopedDescriptor( int descriptor ): pDescriptor( descriptor ) {}
219 
220  //------------------------------------------------------------------------
222  //------------------------------------------------------------------------
224 
225  //------------------------------------------------------------------------
227  //------------------------------------------------------------------------
228  int Release()
229  {
230  int desc = pDescriptor;
231  pDescriptor = -1;
232  return desc;
233  }
234 
235  //------------------------------------------------------------------------
237  //------------------------------------------------------------------------
239  {
240  return pDescriptor;
241  }
242 
243  private:
245  };
246 
247 #ifdef __linux__
248  //----------------------------------------------------------------------------
250  //----------------------------------------------------------------------------
251  class ScopedFsUidSetter
252  {
253  public:
254  //------------------------------------------------------------------------
256  //------------------------------------------------------------------------
257  ScopedFsUidSetter(uid_t fsuid, gid_t fsgid, const std::string &streamName)
258  : pFsUid(fsuid), pFsGid(fsgid), pStreamName(streamName)
259  {
260  pOk = true;
261  pPrevFsUid = -1;
262  pPrevFsGid = -1;
263 
264  //----------------------------------------------------------------------
266  //----------------------------------------------------------------------
267  if(pFsUid >= 0) {
268  pPrevFsUid = setfsuid(pFsUid);
269 
270  if(setfsuid(pFsUid) != pFsUid) {
271  pOk = false;
272  return;
273  }
274  }
275 
276  //----------------------------------------------------------------------
278  //----------------------------------------------------------------------
279  if(pFsGid >= 0) {
280  pPrevFsGid = setfsgid(pFsGid);
281 
282  if(setfsgid(pFsGid) != pFsGid) {
283  pOk = false;
284  return;
285  }
286  }
287  }
288 
289  //------------------------------------------------------------------------
291  //------------------------------------------------------------------------
292  ~ScopedFsUidSetter() {
293  Log *log = DefaultEnv::GetLog();
294 
295  if(pPrevFsUid >= 0) {
296  int retcode = setfsuid(pPrevFsUid);
297  log->Dump(XRootDTransportMsg, "[%s] Restored fsuid from %d to %d", pStreamName.c_str(), retcode, pPrevFsUid);
298  }
299 
300  if(pPrevFsGid >= 0) {
301  int retcode = setfsgid(pPrevFsGid);
302  log->Dump(XRootDTransportMsg, "[%s] Restored fsgid from %d to %d", pStreamName.c_str(), retcode, pPrevFsGid);
303  }
304  }
305 
306  bool IsOk() const {
307  return pOk;
308  }
309 
310  private:
311  int pFsUid;
312  int pFsGid;
313 
314  const std::string &pStreamName;
315 
316  int pPrevFsUid;
317  int pPrevFsGid;
318 
319  bool pOk;
320  };
321 #endif
322 
323 }
324 
325 #endif // __XRD_CL_UTILS_HH__
XrdClConstants.hh
XrdCl::XRootDTransportMsg
const uint64_t XRootDTransportMsg
Definition: XrdClConstants.hh:34
XrdCl::Utils::splitString
static void splitString(Container &result, const std::string &input, const std::string &delimiter)
Split a string.
Definition: XrdClUtils.hh:51
XrdClXRootDResponses.hh
XrdCl::ScopedDescriptor::pDescriptor
int pDescriptor
Definition: XrdClUtils.hh:244
XrdCl::ScopedDescriptor
Smart descriptor - closes the descriptor on destruction.
Definition: XrdClUtils.hh:212
XrdCl::Utils::LogHostAddresses
static void LogHostAddresses(Log *log, uint64_t type, const std::string &hostId, std::vector< XrdNetAddr > &addresses)
Log all the addresses on the list.
XrdCl::ScopedDescriptor::Release
int Release()
Release the descriptor being held.
Definition: XrdClUtils.hh:228
XrdCl::Utils::IPv6
@ IPv6
Definition: XrdClUtils.hh:97
XrdCl::PropertyList
A key-value pair map storing both keys and values as strings.
Definition: XrdClPropertyList.hh:40
XrdCl::Utils::GetHostAddresses
static Status GetHostAddresses(std::vector< XrdNetAddr > &addresses, const URL &url, AddressType type)
Resolve IP addresses.
XrdCl::ScopedDescriptor::GetDescriptor
int GetDescriptor()
Get the descriptor.
Definition: XrdClUtils.hh:238
XrdCl::Utils::GetDirectoryEntries
static Status GetDirectoryEntries(std::vector< std::string > &entries, const std::string &path)
Get directory entries.
XrdCl::Log
Handle diagnostics.
Definition: XrdClLog.hh:101
XrdCl::Utils::CheckTPC
static XRootDStatus CheckTPC(const std::string &server, uint16_t timeout=0)
Check if peer supports tpc.
XrdCl::DefaultEnv::GetLog
static Log * GetLog()
Get default log.
XrdCl::Utils::CheckTPCLite
static XRootDStatus CheckTPCLite(const std::string &server, uint16_t timeout=0)
XrdCl::Utils
Random utilities.
Definition: XrdClUtils.hh:44
XrdClLog.hh
XrdCl::Utils::LogPropertyList
static void LogPropertyList(Log *log, uint64_t topic, const char *format, const PropertyList &list)
Log property list.
XrdCl::Utils::IPAuto
@ IPAuto
Definition: XrdClUtils.hh:95
XrdCl::XRootDStatus
Request status.
Definition: XrdClXRootDResponses.hh:212
XrdCl::ScopedDescriptor::ScopedDescriptor
ScopedDescriptor(int descriptor)
Constructor.
Definition: XrdClUtils.hh:218
XrdCl::Utils::TimeToString
static std::string TimeToString(time_t timestamp)
Convert timestamp to a string.
XrdNetUtils.hh
XrdCl::Utils::ProcessConfig
static Status ProcessConfig(std::map< std::string, std::string > &config, const std::string &file)
Process a config file and return key-value pairs.
XrdCl::Utils::GetRemoteCheckSum
static XRootDStatus GetRemoteCheckSum(std::string &checkSum, const std::string &checkSumType, const std::string &server, const std::string &path)
Get a checksum from a remote xrootd server.
XrdCl::Utils::GetStringParameter
static std::string GetStringParameter(const URL &url, const std::string &name, const std::string &defaultVal)
Get a parameter either from the environment or URL.
close
#define close(a)
Definition: XrdPosix.hh:43
XrdCl::Utils::String2AddressType
static AddressType String2AddressType(const std::string &addressType)
Interpret a string as address type, default to IPAll.
XrdClPropertyList.hh
XrdCl
Definition: XrdClAnyObject.hh:25
XrdCl::ScopedDescriptor::~ScopedDescriptor
~ScopedDescriptor()
Destructor.
Definition: XrdClUtils.hh:223
XrdCl::Utils::GetIntParameter
static int GetIntParameter(const URL &url, const std::string &name, int defaultVal)
Get a parameter either from the environment or URL.
XrdClDefaultEnv.hh
XrdCl::Utils::NormalizeChecksum
static std::string NormalizeChecksum(const std::string &name, const std::string &checksum)
Normalize checksum.
XrdCl::Utils::GetElapsedMicroSecs
static uint64_t GetElapsedMicroSecs(timeval start, timeval end)
Get the elapsed microseconds between two timevals.
XrdCl::Utils::FQDNToCC
static std::string FQDNToCC(const std::string &fqdn)
Convert the fully qualified host name to country code.
XrdCl::Utils::BytesToString
static std::string BytesToString(uint64_t bytes)
Convert bytes to a human readable string.
XrdClStatus.hh
XrdCl::Utils::Char2Hex
static std::string Char2Hex(uint8_t *array, uint16_t size)
Print a char array as hex.
XrdClURL.hh
XrdCl::Utils::IPv4Mapped6
@ IPv4Mapped6
Definition: XrdClUtils.hh:99
XrdCl::Status
Procedure execution status.
Definition: XrdClStatus.hh:109
XrdCl::URL
URL representation.
Definition: XrdClURL.hh:30
XrdCl::Utils::Trim
static void Trim(std::string &str)
Trim a string.
XrdCl::Utils::IPv4
@ IPv4
Definition: XrdClUtils.hh:98
XrdCl::Utils::IPAll
@ IPAll
Definition: XrdClUtils.hh:96
XrdCl::Utils::AddressType
AddressType
Address type.
Definition: XrdClUtils.hh:93
XrdCl::Utils::GetLocalCheckSum
static XRootDStatus GetLocalCheckSum(std::string &checkSum, const std::string &checkSumType, const std::string &path)
Get a checksum from local file.