XRootD
Loading...
Searching...
No Matches
XrdOucUtils Class Reference

#include <XrdOucUtils.hh>

Collaboration diagram for XrdOucUtils:

Public Member Functions

 XrdOucUtils ()
 ~XrdOucUtils ()

Static Public Member Functions

static int argList (char *args, char **argV, int argC)
static char * bin2hex (char *inbuff, int dlen, char *buff, int blen, bool sep=true)
static int doIf (XrdSysError *eDest, XrdOucStream &Config, const char *what, const char *hname, const char *nname, const char *pname)
static bool endsWith (const char *text, const char *ending, int endlen)
static char * eText (int rc, char *eBuff, int eBlen)
static bool findPgm (const char *pgm, XrdOucString &path)
static int fmtBytes (long long val, char *buff, int bsz)
static std::string genHumanSize (size_t size, uint64_t base)
static int genPath (char *buff, int blen, const char *path, const char *psfx=0)
static char * genPath (const char *path, const char *inst, const char *psfx=0)
static char * getFile (const char *path, int &rc, int maxsz=10240, bool notempty=true)
static bool getGID (const char *gName, gid_t &gID)
static int getModificationTime (const char *path, time_t &modificationTime)
static bool getUID (const char *uName, uid_t &uID, gid_t *gID=0)
static int GidName (gid_t gID, char *gName, int gNsz, time_t keepT=0)
static int GroupName (gid_t gID, char *gName, int gNsz)
static int hex2bin (const char *hex, char *bin, int size)
static const char * HSize (size_t bytes, char *buff, int bsz)
static const char * i2bstr (char *buff, int blen, int val, bool pad=false)
static char * Ident (long long &mySID, char *iBuff, int iBlen, const char *iHost, const char *iProg, const char *iName, int Port)
static const char * InstName (const char *name, int Fillit=1)
static const char * InstName (int TranOpt=0)
static int is1of (char *val, const char **clist)
static int isFWD (const char *path, int *port=0, char *hBuff=0, int hBLen=0, bool pTrim=false)
static int Log10 (unsigned long long n)
static int Log2 (unsigned long long n)
static void makeHome (XrdSysError &eDest, const char *inst)
static bool makeHome (XrdSysError &eDest, const char *inst, const char *path, mode_t mode)
static int makePath (char *path, mode_t mode, bool reset=false)
static bool mode2mask (const char *mode, mode_t &mask)
static char * parseHome (XrdSysError &eDest, XrdOucStream &Config, int &mode)
static bool parseLib (XrdSysError &eDest, XrdOucStream &Config, const char *libName, char *&path, char **libparm)
static bool PidFile (XrdSysError &eDest, const char *path)
static int ReLink (const char *path, const char *target, mode_t mode=0)
static void Sanitize (char *instr, char subc='_')
static char * subLogfn (XrdSysError &eDest, const char *inst, char *logfn)
static int Token (const char **str, char delim, char *buff, int bsz)
static void toLower (char *str)
static uint8_t touint8_t (const std::string_view sv)
static void trim (std::string &str)
static void trim (std::string_view &sv)
static int UidName (uid_t uID, char *uName, int uNsz, time_t keepT=0)
static void Undercover (XrdSysError &eDest, int noLog, int *pipeFD=0)
static std::string UrlDecode (const std::string &input)
static std::string UrlEncode (const std::string &input)
static int UserName (uid_t uID, char *uName, int uNsz)
static const char * ValPath (const char *path, mode_t allow, bool isdir)

Static Public Attributes

static const mode_t pathMode = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH

Detailed Description

Definition at line 44 of file XrdOucUtils.hh.

Constructor & Destructor Documentation

◆ XrdOucUtils()

XrdOucUtils::XrdOucUtils ( )
inline

Definition at line 160 of file XrdOucUtils.hh.

160{}

◆ ~XrdOucUtils()

XrdOucUtils::~XrdOucUtils ( )
inline

Definition at line 161 of file XrdOucUtils.hh.

161{}

Member Function Documentation

◆ argList()

int XrdOucUtils::argList ( char * args,
char ** argV,
int argC )
static

Definition at line 127 of file XrdOucUtils.cc.

128{
129 char *aP = args;
130 int j;
131
132// Construct the argv array based on passed command line.
133//
134for (j = 0; j < argC; j++)
135 {while(*aP == ' ') aP++;
136 if (!(*aP)) break;
137
138 if (*aP == '"' || *aP == '\'')
139 {argV[j] = aP+1;
140 aP = index(aP+1, *aP);
141 if (!aP || (*(aP+1) != ' ' && *(aP+1)))
142 {if (!j) argV[0] = 0; return -EINVAL;}
143 *aP++ = '\0';
144 } else {
145 argV[j] = aP;
146 if ((aP = index(aP+1, ' '))) *aP++ = '\0';
147 else {j++; break;}
148 }
149
150 }
151
152// Make sure we did not overflow the vector
153//
154 if (j > argC-1) return -E2BIG;
155
156// End list with a null pointer and return the actual number of arguments
157//
158 argV[j] = 0;
159 return j;
160}

Referenced by XrdOucProg::Setup().

Here is the caller graph for this function:

◆ bin2hex()

char * XrdOucUtils::bin2hex ( char * inbuff,
int dlen,
char * buff,
int blen,
bool sep = true )
static

Definition at line 166 of file XrdOucUtils.cc.

168{
169 static const char hv[] = "0123456789abcdef";
170 char *outbuff = buff;
171 for (int i = 0; i < dlen && blen > 2; i++) {
172 *outbuff++ = hv[(inbuff[i] >> 4) & 0x0f];
173 *outbuff++ = hv[ inbuff[i] & 0x0f];
174 blen -= 2;
175 if (sep && blen > 1 && ((i & 0x03) == 0x03 || i+1 == dlen))
176 {*outbuff++ = ' '; blen--;}
177 }
178 *outbuff = '\0';
179 return buff;
180}

Referenced by XrdOssMio::Map().

Here is the caller graph for this function:

◆ doIf()

int XrdOucUtils::doIf ( XrdSysError * eDest,
XrdOucStream & Config,
const char * what,
const char * hname,
const char * nname,
const char * pname )
static

Definition at line 279 of file XrdOucUtils.cc.

282{
283 static const char *brk[] = {"defined", "exec", "named", 0};
284 XrdOucEnv *theEnv = 0;
285 char *val;
286 int hostok, isDef;
287
288// Make sure that at least one thing appears after the if
289//
290 if (!(val = Config.GetWord()))
291 {if (eDest) eDest->Emsg("Config","Host name missing after 'if' in", what);
292 return -1;
293 }
294
295// Check if we are one of the listed hosts
296//
297 if (!is1of(val, brk))
298 {do {hostok = XrdNetUtils::Match(hname, val);
299 val = Config.GetWord();
300 } while(!hostok && val && !is1of(val, brk));
301 if (hostok)
302 { while(val && !is1of(val, brk)) val = Config.GetWord();
303 // No more directives
304 if (!val) return 1;
305 } else return 0;
306 }
307
308// Check if this is a defined test
309//
310 while(!strcmp(val, "defined"))
311 {if (!(val = Config.GetWord()) || *val != '?')
312 {if (eDest)
313 {eDest->Emsg("Config","'?var' missing after 'defined' in",what);}
314 return -1;
315 }
316 // Get environment if we have none
317 //
318 if (!theEnv && (theEnv = Config.SetEnv(0))) Config.SetEnv(theEnv);
319 if (!theEnv && *(val+1) != '~') return 0;
320
321 // Check if any listed variable is defined.
322 //
323 isDef = 0;
324 while(val && *val == '?')
325 {if (*(val+1) == '~' ? getenv(val+2) : theEnv->Get(val+1)) isDef=1;
326 val = Config.GetWord();
327 }
328 if (!val || !isDef) return isDef;
329 if (strcmp(val, "&&"))
330 {if (eDest)
331 {eDest->Emsg("Config",val,"is invalid for defined test in",what);}
332 return -1;
333 } else {
334 if (!(val = Config.GetWord()))
335 {if (eDest)
336 {eDest->Emsg("Config","missing keyword after '&&' in",what);}
337 return -1;
338 }
339 }
340 if (!is1of(val, brk))
341 {if (eDest)
342 {eDest->Emsg("Config",val,"is invalid after '&&' in",what);}
343 return -1;
344 }
345 }
346
347// Check if we need to compare program names (we are here only if we either
348// passed the hostlist test or there was no hostlist present)
349//
350 if (!strcmp(val, "exec"))
351 {if (!(val = Config.GetWord()) || !strcmp(val, "&&"))
352 {if (eDest)
353 {eDest->Emsg("Config","Program name missing after 'if exec' in",what);}
354 return -1;
355 }
356
357 // Check if we are one of the programs.
358 //
359 if (!pname) return 0;
360 while(val && strcmp(val, pname))
361 if (!strcmp(val, "&&")) return 0;
362 else val = Config.GetWord();
363 if (!val) return 0;
364 while(val && strcmp(val, "&&")) val = Config.GetWord();
365 if (!val) return 1;
366
367 if (!(val = Config.GetWord()))
368 {if (eDest)
369 {eDest->Emsg("Config","Keyword missing after '&&' in",what);}
370 return -1;
371 }
372 if (strcmp(val, "named"))
373 {if (eDest)
374 {eDest->Emsg("Config",val,"is invalid after '&&' in",what);}
375 return -1;
376 }
377 }
378
379// Check if we need to compare net names (we are here only if we either
380// passed the hostlist test or there was no hostlist present)
381//
382 if (!(val = Config.GetWord()))
383 {if (eDest)
384 {eDest->Emsg("Config","Instance name missing after 'if named' in", what);}
385 return -1;
386 }
387
388// Check if we are one of the names
389//
390 if (!nname) return 0;
391 while(val && strcmp(val, nname)) val = Config.GetWord();
392
393// All done
394//
395 return (val != 0);
396}
static XrdSysError eDest(0,"crypto_")
static bool Match(const char *hName, const char *pattern)
char * Get(const char *varname)
Definition XrdOucEnv.hh:69
static int is1of(char *val, const char **clist)
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
XrdCmsConfig Config
XrdOucEnv theEnv

References eDest, XrdOucEnv::Get(), is1of(), and XrdNetUtils::Match().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ endsWith()

bool XrdOucUtils::endsWith ( const char * text,
const char * ending,
int endlen )
static

Definition at line 231 of file XrdOucUtils.cc.

232{
233 int tlen = strlen(text);
234
235 return (tlen >= endlen && !strcmp(text+(tlen-endlen), ending));
236}

Referenced by XrdBwmFile::open().

Here is the caller graph for this function:

◆ eText()

char * XrdOucUtils::eText ( int rc,
char * eBuff,
int eBlen )
static

Definition at line 245 of file XrdOucUtils.cc.

246{
247 const char *etP;
248
249// Get error text
250//
251 etP = XrdSysE2T(rc);
252
253// Copy the text and lower case the first letter
254//
255 strlcpy(eBuff, etP, eBlen);
256
257// All done
258//
259 return eBuff;
260}
const char * XrdSysE2T(int errcode)
Definition XrdSysE2T.cc:104
size_t strlcpy(char *dst, const char *src, size_t sz)

References strlcpy(), and XrdSysE2T().

Here is the call graph for this function:

◆ findPgm()

bool XrdOucUtils::findPgm ( const char * pgm,
XrdOucString & path )
static

Definition at line 402 of file XrdOucUtils.cc.

403{
404 struct stat Stat;
405
406// Check if only executable bit needs to be checked
407//
408 if (*pgm == '/')
409 {if (stat(pgm, &Stat) || !(Stat.st_mode & S_IXOTH)) return false;
410 path = pgm;
411 return true;
412 }
413
414// Make sure we have the paths to check
415//
416 const char *pEnv = getenv("PATH");
417 if (!pEnv) return false;
418
419// Setup to find th executable
420//
421 XrdOucString prog, pList(pEnv);
422 int from = 0;;
423 prog += '/'; prog += pgm;
424
425// Find it!
426//
427 while((from = pList.tokenize(path, from, ':')) != -1)
428 {path += prog;
429 if (!stat(path.c_str(), &Stat) && Stat.st_mode & S_IXOTH) return true;
430 }
431 return false;
432}
struct stat Stat
Definition XrdCks.cc:49
#define stat(a, b)
Definition XrdPosix.hh:101
const char * c_str() const

References XrdOucString::c_str(), Stat, stat, and XrdOucString::tokenize().

Referenced by XrdNetPMarkCfg::Parse().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fmtBytes()

int XrdOucUtils::fmtBytes ( long long val,
char * buff,
int bsz )
static

Definition at line 438 of file XrdOucUtils.cc.

439{
440 static const long long Kval = 1024LL;
441 static const long long Mval = 1024LL*1024LL;
442 static const long long Gval = 1024LL*1024LL*1024LL;
443 static const long long Tval = 1024LL*1024LL*1024LL*1024LL;
444 char sName = ' ';
445 int resid;
446
447// Get correct scaling
448//
449 if (val < 1024) return snprintf(buff, bsz, "%lld", val);
450 if (val < Mval) {val = val*10/Kval; sName = 'K';}
451 else if (val < Gval) {val = val*10/Mval; sName = 'M';}
452 else if (val < Tval) {val = val*10/Gval; sName = 'G';}
453 else {val = val*10/Tval; sName = 'T';}
454 resid = val%10LL; val = val/10LL;
455
456// Format it
457//
458 return snprintf(buff, bsz, "%lld.%d%c", val, resid, sName);
459}

Referenced by XrdFrmPurge::Display().

Here is the caller graph for this function:

◆ genHumanSize()

std::string XrdOucUtils::genHumanSize ( size_t size,
uint64_t base )
static

Generates Human-readable size string (1.1K, 1.0M, ...)

Parameters
sizethe size of the file
baseeither 1024 or 1000
Returns
the human-readable size string of the size passed in parameter depending on the base

Definition at line 461 of file XrdOucUtils.cc.

461 {
462 static const char* units[] = {"", "K", "M", "G", "T", "P", "E"};
463 const int maxUnit = 6;
464
465 double value = static_cast<double>(size);
466 int unitIndex = 0;
467
468 while (value >= static_cast<double>(base) && unitIndex < maxUnit) {
469 value /= static_cast<double>(base);
470 ++unitIndex;
471 }
472
473 auto ceil_to = [](double x, int decimals) {
474 if (x == 0.0) return 0.0; // avoid creating a -0 in the output
475 const double p = std::pow(10.0, decimals);
476 // small epsilon to avoid 1.0 becoming 1.1 due to floating noise
477 return std::ceil(x * p - 1e-12) / p;
478 };
479
480 auto decimals_for = [](double x, int u) {
481 return (u > 0 && x < 10.0) ? 1 : 0; // e.g: 1.0G for exact powers
482 };
483
484 int prec = decimals_for(value, unitIndex);
485 double shown = ceil_to(value, prec);
486
487 // If rounding pushed us to the next unit (e.g. 1024.0K), promote
488 if (shown >= static_cast<double>(base) && unitIndex < maxUnit) {
489 shown /= static_cast<double>(base);
490 ++unitIndex;
491
492 prec = decimals_for(shown, unitIndex);
493 shown = ceil_to(shown, prec);
494 }
495
496 std::ostringstream out;
497 out << std::fixed << std::setprecision(prec) << shown << units[unitIndex];
498 return out.str();
499}

Referenced by getSizeStr().

Here is the caller graph for this function:

◆ genPath() [1/2]

int XrdOucUtils::genPath ( char * buff,
int blen,
const char * path,
const char * psfx = 0 )
static

Definition at line 523 of file XrdOucUtils.cc.

524{
525 int i, j;
526
527 i = strlen(path);
528 j = (psfx ? strlen(psfx) : 0);
529 if (i+j+3 > blen) return -ENAMETOOLONG;
530
531 strcpy(buff, path);
532 if (psfx)
533 {if (buff[i-1] != '/') buff[i++] = '/';
534 strcpy(&buff[i], psfx);
535 if (psfx[j-1] != '/') strcat(buff, "/");
536 }
537 return 0;
538}

◆ genPath() [2/2]

char * XrdOucUtils::genPath ( const char * path,
const char * inst,
const char * psfx = 0 )
static

Definition at line 505 of file XrdOucUtils.cc.

507{
508 char buff[2048];
509 int i = strlcpy(buff, p_path, sizeof(buff));
510
511 if (buff[i-1] != '/') {buff[i++] = '/'; buff[i] = '\0';}
512 if (inst) {strcpy(buff+i, inst); strcat(buff, "/");}
513 if (s_path) strcat(buff, s_path);
514
515 i = strlen(buff);
516 if (buff[i-1] != '/') {buff[i++] = '/'; buff[i] = '\0';}
517
518 return strdup(buff);
519}

References strlcpy().

Referenced by XrdNetCmsNotify::XrdNetCmsNotify(), XrdCmsClientConfig::Configure(), XrdXrootdProtocol::Configure(), XrdCmsConfig::Configure0(), XrdCmsConfig::Configure2(), XrdOfsConfigCP::Init(), and XrdFrcUtils::makePath().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getFile()

char * XrdOucUtils::getFile ( const char * path,
int & rc,
int maxsz = 10240,
bool notempty = true )
static

Definition at line 544 of file XrdOucUtils.cc.

545{
546 struct stat Stat;
547 struct fdHelper
548 {int fd = -1;
549 fdHelper() {}
550 ~fdHelper() {if (fd >= 0) close(fd);}
551 } file;
552 char *buff;
553 int flen;
554
555// Preset RC
556//
557 rc = 0;
558
559// Open the file in read mode
560//
561 if ((file.fd = open(path, O_RDONLY)) < 0) {rc = errno; return 0;}
562
563// Get the size of the file
564//
565 if (fstat(file.fd, &Stat)) {rc = errno; return 0;}
566
567// Check if the size exceeds the maximum allowed
568//
569 if (Stat.st_size > maxsz) {rc = EFBIG; return 0;}
570
571// Make sure the file is not empty if empty files are disallowed
572//
573 if (Stat.st_size == 0 && notempty) {rc = ENODATA; return 0;}
574
575// Allocate a buffer
576//
577 if ((buff = (char *)malloc(Stat.st_size+1)) == 0)
578 {rc = errno; return 0;}
579
580// Read the contents of the file into the buffer
581//
582 if (Stat.st_size)
583 {if ((flen = read(file.fd, buff, Stat.st_size)) < 0)
584 {rc = errno; free(buff); return 0;}
585 } else flen = 0;
586
587// Add null byte. recall the buffer is bigger by one byte
588//
589 buff[flen] = 0;
590
591// Return the size aand the buffer
592//
593 rc = flen;
594 return buff;
595}
#define ENODATA
#define close(a)
Definition XrdPosix.hh:48
#define fstat(a, b)
Definition XrdPosix.hh:62
#define open
Definition XrdPosix.hh:76
#define read(a, b, c)
Definition XrdPosix.hh:82

References close, ENODATA, fstat, open, read, Stat, and stat.

◆ getGID()

bool XrdOucUtils::getGID ( const char * gName,
gid_t & gID )
static

Definition at line 601 of file XrdOucUtils.cc.

602{
603 struct group Grp, *result;
604 char buff[65536];
605
606 getgrnam_r(gName, &Grp, buff, sizeof(buff), &result);
607 if (!result) return false;
608
609 gID = Grp.gr_gid;
610 return true;
611}

Referenced by XrdSecProtocolsss::Authenticate().

Here is the caller graph for this function:

◆ getModificationTime()

int XrdOucUtils::getModificationTime ( const char * path,
time_t & modificationTime )
static

Definition at line 1520 of file XrdOucUtils.cc.

1520 {
1521 struct stat buf;
1522 int statRet = ::stat(path,&buf);
1523 if(!statRet) {
1524 modificationTime = buf.st_mtime;
1525 }
1526 return statRet;
1527}

References stat.

Referenced by XrdTlsContext::XrdTlsContext(), and XrdTlsContext::newHostCertificateDetected().

Here is the caller graph for this function:

◆ getUID()

bool XrdOucUtils::getUID ( const char * uName,
uid_t & uID,
gid_t * gID = 0 )
static

Definition at line 617 of file XrdOucUtils.cc.

618{
619 struct passwd pwd, *result;
620 char buff[16384];
621
622 getpwnam_r(uName, &pwd, buff, sizeof(buff), &result);
623 if (!result) return false;
624
625 uID = pwd.pw_uid;
626 if (gID) *gID = pwd.pw_gid;
627
628 return true;
629}

Referenced by XrdSecProtocolsss::Authenticate().

Here is the caller graph for this function:

◆ GidName()

int XrdOucUtils::GidName ( gid_t gID,
char * gName,
int gNsz,
time_t keepT = 0 )
static

Definition at line 635 of file XrdOucUtils.cc.

636{
637 static const int maxgBsz = 256*1024;
638 static const int addGsz = 4096;
639 struct group *gEnt, gStruct;
640 char gBuff[1024], *gBp = gBuff;
641 int glen = 0, gBsz = sizeof(gBuff), aOK = 1;
642 int n, retVal = 0;
643
644// Get ID from cache, if allowed
645//
646 if (keepT)
647 {int n = LookUp(gidMap, static_cast<unsigned int>(gID),gName,gNsz);
648 if (n > 0) return (n < gNsz ? n : 0);
649 }
650
651// Get the the group struct. If we don't have a large enough buffer, get a
652// larger one and try again up to the maximum buffer we will tolerate.
653//
654 while(( retVal = getgrgid_r(gID, &gStruct, gBp, gBsz, &gEnt) ) == ERANGE)
655 {if (gBsz >= maxgBsz) {aOK = 0; break;}
656 if (gBsz > addGsz) free(gBp);
657 gBsz += addGsz;
658 if (!(gBp = (char *)malloc(gBsz))) {aOK = 0; break;}
659 }
660
661// Return a group name if all went well
662//
663 if (aOK && retVal == 0 && gEnt != NULL)
664 {if (keepT)
665 AddID(gidMap, static_cast<unsigned int>(gID), gEnt->gr_name, keepT);
666 glen = strlen(gEnt->gr_name);
667 if (glen >= gNsz) glen = 0;
668 else strcpy(gName, gEnt->gr_name);
669 } else {
670 n = snprintf(gName, gNsz, "%ud", static_cast<unsigned int>(gID));
671 if (n >= gNsz) glen = 0;
672 }
673
674// Free any allocated buffer and return result
675//
676 if (gBsz > addGsz && gBp) free(gBp);
677 return glen;
678}

◆ GroupName()

int XrdOucUtils::GroupName ( gid_t gID,
char * gName,
int gNsz )
static

Definition at line 684 of file XrdOucUtils.cc.

685{
686 static const int maxgBsz = 256*1024;
687 static const int addGsz = 4096;
688 struct group *gEnt, gStruct;
689 char gBuff[1024], *gBp = gBuff;
690 int glen, gBsz = sizeof(gBuff), aOK = 1;
691 int retVal = 0;
692
693// Get the the group struct. If we don't have a large enough buffer, get a
694// larger one and try again up to the maximum buffer we will tolerate.
695//
696 while(( retVal = getgrgid_r(gID, &gStruct, gBp, gBsz, &gEnt) ) == ERANGE)
697 {if (gBsz >= maxgBsz) {aOK = 0; break;}
698 if (gBsz > addGsz) free(gBp);
699 gBsz += addGsz;
700 if (!(gBp = (char *)malloc(gBsz))) {aOK = 0; break;}
701 }
702
703// Return a group name if all went well
704//
705 if (aOK && retVal == 0 && gEnt != NULL)
706 {glen = strlen(gEnt->gr_name);
707 if (glen >= gNsz) glen = 0;
708 else strcpy(gName, gEnt->gr_name);
709 } else glen = 0;
710
711// Free any allocated buffer and return result
712//
713 if (gBsz > addGsz && gBp) free(gBp);
714 return glen;
715}

Referenced by XrdSecProtocolunix::getCredentials().

Here is the caller graph for this function:

◆ hex2bin()

int XrdOucUtils::hex2bin ( const char * hex,
char * bin,
int size )
static

Definition at line 186 of file XrdOucUtils.cc.

187{
188 int len = 0, lo = -1, hi = -1;
189
190 while (int c = *hex++) {
191 if (c == ' ' || c == '\n')
192 break;
193
194 switch (c) {
195 case '0' ... '9':
196 c -= '0';
197 break;
198 case 'a' ... 'f':
199 c -= 'a' - 10;
200 break;
201 case 'A' ... 'F':
202 c -= 'A' - 10;
203 break;
204 default:
205 return -EINVAL;
206 }
207
208 if (hi == -1)
209 hi = c;
210 else
211 lo = c;
212
213 if (lo == -1)
214 continue;
215
216 if (len >= size)
217 return -EOVERFLOW;
218
219 bin[len++] = (hi << 4) | lo;
220 hi = lo = -1;
221 }
222 if (hi >= 0 || lo >= 0 || len == 0)
223 return -EINVAL;
224 return len;
225}

◆ HSize()

const char * XrdOucUtils::HSize ( size_t bytes,
char * buff,
int bsz )
static

Definition at line 721 of file XrdOucUtils.cc.

722{
723
724// Do fast conversion of the quantity is less than 1K
725//
726 if (bytes < 1024)
727 {snprintf(buff, bsz, "%zu", bytes);
728 return buff;
729 }
730
731// Scale this down
732//
733 const char *suffix = " KMGTPEYZ";
734 double dBytes = static_cast<double>(bytes);
735
736do{dBytes /= 1024.0; suffix++;
737 } while(dBytes >= 1024.0 && *(suffix+1));
738
739
740// Format and return result. Include fractions only if they meaningfully exist.
741//
742 double whole, frac = modf(dBytes, &whole);
743 if (frac >= .005) snprintf(buff, bsz, "%.02lf%c", dBytes, *suffix);
744 else snprintf(buff, bsz, "%g%c", whole, *suffix);
745 return buff;
746}

◆ i2bstr()

const char * XrdOucUtils::i2bstr ( char * buff,
int blen,
int val,
bool pad = false )
static

Definition at line 752 of file XrdOucUtils.cc.

753{
754 char zo[2] = {'0', '1'};
755
756 if (blen < 2) return "";
757
758 buff[--blen] = 0;
759 if (!val) buff[blen--] = '0';
760 else while(val && blen >= 0)
761 {buff[blen--] = zo[val & 0x01];
762 val >>= 1;
763 }
764
765 if (blen >= 0 && pad) while(blen >= 0) buff[blen--] = '0';
766
767 return &buff[blen+1];
768}

◆ Ident()

char * XrdOucUtils::Ident ( long long & mySID,
char * iBuff,
int iBlen,
const char * iHost,
const char * iProg,
const char * iName,
int Port )
static

Definition at line 821 of file XrdOucUtils.cc.

824{
825 static char *theSIN;
826 static long long theSID = genSID(theSIN, iHost, iPort, iName, iProg);
827 const char *sP = getenv("XRDSITE");
828 char uName[256];
829 int myPid = static_cast<int>(getpid());
830
831// Get our username
832//
833 if (UserName(getuid(), uName, sizeof(uName)))
834 sprintf(uName, "%d", static_cast<int>(getuid()));
835
836// Create identification record
837//
838 snprintf(iBuff,iBlen,"%s.%d:%s@%s\n&site=%s&port=%d&inst=%s&pgm=%s",
839 uName, myPid, theSIN, iHost, (sP ? sP : ""), iPort, iName, iProg);
840
841// Return a copy of the sid key
842//
843 h2nll(theSID, mySID);
844 return strdup(theSIN);
845}
static int UserName(uid_t uID, char *uName, int uNsz)

References UserName().

Referenced by XrdFrmMonitor::Init(), and XrdXrootdMonitor::Init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ InstName() [1/2]

const char * XrdOucUtils::InstName ( const char * name,
int Fillit = 1 )
static

Definition at line 868 of file XrdOucUtils.cc.

869{ return (Fillit ? name && *name ? name : "anon"
870 : name && strcmp(name,"anon") && *name ? name : 0);
871}

◆ InstName() [2/2]

const char * XrdOucUtils::InstName ( int TranOpt = 0)
static

Definition at line 851 of file XrdOucUtils.cc.

852{
853 const char *iName = getenv("XRDNAME");
854
855// If tran is zero, return what we have
856//
857 if (!TranOpt) return iName;
858
859// If trans is positive then make sure iName has a value. Otherwise, make sure
860// iName has no value if it's actually "anon".
861//
862 if (TranOpt > 0) {if (!iName || !*iName) iName = "anon";}
863 else if (iName && !strcmp(iName, "anon")) iName = 0;
864 return iName;
865}

Referenced by XrdFrcProxy::XrdFrcProxy(), XrdFrmConfig::XrdFrmConfig(), XrdNetCmsNotify::XrdNetCmsNotify(), XrdSsiSfsConfig::XrdSsiSfsConfig(), XrdOssSys::ConfigStage(), XrdCmsClientConfig::Configure(), XrdConfig::Configure(), XrdFrmConfig::Configure(), XrdCmsConfig::Configure0(), XrdCmsConfig::Configure1(), XrdOfsConfigCP::Init(), XrdOssSpace::Init(), main(), and XrdFrcReqAgent::Start().

Here is the caller graph for this function:

◆ is1of()

int XrdOucUtils::is1of ( char * val,
const char ** clist )
static

Definition at line 877 of file XrdOucUtils.cc.

878{
879 int i = 0;
880 while(clist[i]) if (!strcmp(val, clist[i])) return 1;
881 else i++;
882 return 0;
883}

Referenced by doIf().

Here is the caller graph for this function:

◆ isFWD()

int XrdOucUtils::isFWD ( const char * path,
int * port = 0,
char * hBuff = 0,
int hBLen = 0,
bool pTrim = false )
static

Definition at line 889 of file XrdOucUtils.cc.

891{
892 const char *hName, *hNend, *hPort, *hPend, *hP = path;
893 char *eP;
894 int n;
895
896 if (*path == '/') hP++; // Note: It's assumed an objectid if no slash
897 if (*hP == 'x') hP++;
898 if (strncmp("root:/", hP, 6)) return 0;
899 if (hBuff == 0 || hBLen <= 0) return (hP - path) + 6;
900 hP += 6;
901
902 if (!XrdNetUtils::Parse(hP, &hName, &hNend, &hPort, &hPend)) return 0;
903 if (*hNend == ']') hNend++;
904 else {if (!(*hNend) && !(hNend = index(hName, '/'))) return 0;
905 if (!(*hPend)) hPend = hNend;
906 }
907
908 if (pTrim || !(*hPort)) n = hNend - hP;
909 else n = hPend - hP;
910 if (n >= hBLen) return 0;
911 strncpy(hBuff, hP, n);
912 hBuff[n] = 0;
913
914 if (port)
915 {if (*hNend != ':') *port = 0;
916 else {*port = strtol(hPort, &eP, 10);
917 if (*port < 0 || *port > 65535 || eP != hPend) return 0;
918 }
919 }
920
921 return hPend-path;
922}
static bool Parse(const char *hSpec, const char **hName, const char **hNend, const char **hPort, const char **hPend)

References XrdNetUtils::Parse().

Here is the call graph for this function:

◆ Log10()

int XrdOucUtils::Log10 ( unsigned long long n)
static

Definition at line 952 of file XrdOucUtils.cc.

953{
954 int i = 0;
955
956 #define SHFT(k, m) if (n >= m) { i += k; n /= m; }
957
958 SHFT(16,10000000000000000ULL); SHFT(8,100000000ULL);
959 SHFT(4,10000ULL); SHFT(2,100ULL); SHFT(1,10ULL);
960 return i;
961
962 #undef SHFT
963}
#define SHFT(k)

References SHFT.

◆ Log2()

int XrdOucUtils::Log2 ( unsigned long long n)
static

Definition at line 937 of file XrdOucUtils.cc.

938{
939 int i = 0;
940
941 #define SHFT(k) if (n >= (1ULL << k)) { i += k; n >>= k; }
942
943 SHFT(32); SHFT(16); SHFT(8); SHFT(4); SHFT(2); SHFT(1); return i;
944
945 #undef SHFT
946}

References SHFT.

Referenced by XrdBuffXL::Init(), XrdBuffManager::Obtain(), XrdBuffXL::Obtain(), XrdBuffManager::Recalc(), and XrdBuffXL::Recalc().

Here is the caller graph for this function:

◆ makeHome() [1/2]

void XrdOucUtils::makeHome ( XrdSysError & eDest,
const char * inst )
static

Definition at line 969 of file XrdOucUtils.cc.

970{
971 char buff[2048];
972
973 if (!inst || !getcwd(buff, sizeof(buff))) return;
974
975 strcat(buff, "/"); strcat(buff, inst);
976 if (MAKEDIR(buff, pathMode) && errno != EEXIST)
977 {eDest.Emsg("Config", errno, "create home directory", buff);
978 return;
979 }
980
981 if (chdir(buff) < 0)
982 eDest.Emsg("Config", errno, "chdir to home directory", buff);
983}
#define chdir(a)
Definition XrdPosix.hh:46
#define MAKEDIR(path, mode)
static const mode_t pathMode

References chdir, eDest, MAKEDIR, and pathMode.

Referenced by XrdConfig::Configure(), XrdFrmConfig::Configure(), and makeHome().

Here is the caller graph for this function:

◆ makeHome() [2/2]

bool XrdOucUtils::makeHome ( XrdSysError & eDest,
const char * inst,
const char * path,
mode_t mode )
static

Definition at line 987 of file XrdOucUtils.cc.

989{
990 char cwDir[2048];
991 const char *slash = "", *slash2 = "";
992 int n, rc;
993
994// Provide backward compatibility for instance name qualification
995//
996
997 if (!path || !(n = strlen(path)))
998 {if (inst) makeHome(eDest, inst);
999 return true;
1000 }
1001
1002// Augment the path with instance name, if need be
1003//
1004 if (path[n-1] != '/') slash = "/";
1005 if (!inst || !(n = strlen(inst))) inst = "";
1006 else slash2 = "/";
1007 n = snprintf(cwDir, sizeof(cwDir), "%s%s%s%s", path, slash, inst, slash2);
1008 if (n >= (int)sizeof(cwDir))
1009 {eDest.Emsg("Config", ENAMETOOLONG, "create home directory", cwDir);
1010 return false;
1011 }
1012
1013// Create the path if it doesn't exist
1014//
1015 if ((rc = makePath(cwDir, mode, true)))
1016 {eDest.Emsg("Config", rc, "create home directory", cwDir);
1017 return false;
1018 }
1019
1020// Switch to this directory
1021//
1022 if (chdir(cwDir) < 0)
1023 {eDest.Emsg("Config", errno, "chdir to home directory", cwDir);
1024 return false;
1025 }
1026
1027// All done
1028//
1029 return true;
1030}
static int makePath(char *path, mode_t mode, bool reset=false)
static void makeHome(XrdSysError &eDest, const char *inst)

References chdir, eDest, makeHome(), and makePath().

Here is the call graph for this function:

◆ makePath()

int XrdOucUtils::makePath ( char * path,
mode_t mode,
bool reset = false )
static

Definition at line 1036 of file XrdOucUtils.cc.

1037{
1038 char *next_path = path+1;
1039 struct stat buf;
1040 bool dochmod = false; // The 1st component stays as is
1041
1042// Typically, the path exists. So, do a quick check before launching into it
1043//
1044 if (!reset && !stat(path, &buf)) return 0;
1045
1046// Start creating directories starting with the root
1047//
1048 while((next_path = index(next_path, int('/'))))
1049 {*next_path = '\0';
1050 if (MAKEDIR(path, mode))
1051 if (errno != EEXIST) return -errno;
1052 if (dochmod) CHMOD(path, mode);
1053 dochmod = reset;
1054 *next_path = '/';
1055 next_path = next_path+1;
1056 }
1057
1058// All done
1059//
1060 return 0;
1061}
#define CHMOD(path, mode)

References CHMOD, MAKEDIR, and stat.

Referenced by XrdOssArcBackup::XrdOssArcBackup(), XrdOssArcBackupTask::BkpXeq(), XrdOssArcConfig::BuildPath(), XrdOssArcConfig::Configure(), XrdOssSys::Create(), XrdOfsConfigCP::Init(), makeHome(), XrdFrcUtils::makePath(), XrdFrcUtils::makeQDir(), ReLink(), XrdOssSys::Reloc(), XrdOssSys::Rename(), XrdSecsssKT::Rewrite(), XrdNetSocket::socketPath(), and subLogfn().

Here is the caller graph for this function:

◆ mode2mask()

bool XrdOucUtils::mode2mask ( const char * mode,
mode_t & mask )
static

Definition at line 1067 of file XrdOucUtils.cc.

1068{
1069 mode_t mval[3] = {0}, mbit[3] = {0x04, 0x02, 0x01};
1070 const char *mok = "rwx";
1071 char mlet;
1072
1073// Accept octal mode
1074//
1075 if (isdigit(*mode))
1076 {char *eP;
1077 mask = strtol(mode, &eP, 8);
1078 return *eP == 0;
1079 }
1080
1081// Make sure we have the correct number of characters
1082//
1083 int n = strlen(mode);
1084 if (!n || n > 9 || n/3*3 != n) return false;
1085
1086// Convert groups of three
1087//
1088 int k = 0;
1089 do {for (int i = 0; i < 3; i++)
1090 {mlet = *mode++;
1091 if (mlet != '-')
1092 {if (mlet != mok[i]) return false;
1093 mval[k] |= mbit[i];
1094 }
1095 }
1096 } while(++k < 3 && *mode);
1097
1098// Combine the modes and return success
1099//
1100 mask = mval[0]<<6 | mval[1]<<3 | mval[2];
1101 return true;
1102}

◆ parseHome()

char * XrdOucUtils::parseHome ( XrdSysError & eDest,
XrdOucStream & Config,
int & mode )
static

Definition at line 1158 of file XrdOucUtils.cc.

1159{
1160 char *pval, *val, *HomePath = 0;
1161
1162// Get the path
1163//
1164 pval = Config.GetWord();
1165 if (!pval || !pval[0])
1166 {eDest.Emsg("Config", "home path not specified"); return 0;}
1167
1168// Make sure it's an absolute path
1169//
1170 if (*pval != '/')
1171 {eDest.Emsg("Config", "home path not absolute"); return 0;}
1172
1173// Record the path
1174//
1175 HomePath = strdup(pval);
1176
1177// Get the optional access rights
1178//
1179 mode = S_IRWXU;
1180 if ((val = Config.GetWord()) && val[0])
1181 {if (!strcmp("group", val)) mode |= (S_IRGRP | S_IXGRP);
1182 else {eDest.Emsg("Config", "invalid home path modifier -", val);
1183 free(HomePath);
1184 return 0;
1185 }
1186 }
1187 return HomePath;
1188}

References eDest.

◆ parseLib()

bool XrdOucUtils::parseLib ( XrdSysError & eDest,
XrdOucStream & Config,
const char * libName,
char *& path,
char ** libparm )
static

Definition at line 1108 of file XrdOucUtils.cc.

1110{
1111 char *val, parms[2048];
1112
1113// Get the next token
1114//
1115 val = Config.GetWord();
1116
1117// We do not support stacking as the caller does not support stacking
1118//
1119 if (val && !strcmp("++", val))
1120 {eDest.Say("Config warning: stacked plugins are not supported in "
1121 "this context; directive ignored!");
1122 return true;
1123 }
1124
1125// Now skip over any options
1126//
1127 while(val && *val && *val == '+') val = Config.GetWord();
1128
1129// Check if we actually have a path
1130//
1131 if (!val || !val[0])
1132 {eDest.Emsg("Config", libName, "not specified"); return false;}
1133
1134// Record the path
1135//
1136 if (libPath) free(libPath);
1137 libPath = strdup(val);
1138
1139// Handle optional parameter
1140//
1141 if (!libParm) return true;
1142 if (*libParm) free(*libParm);
1143 *libParm = 0;
1144
1145// Record any parms
1146//
1147 *parms = 0;
1148 if (!Config.GetRest(parms, sizeof(parms)))
1149 {eDest.Emsg("Config", libName, "parameters too long"); return false;}
1150 if (*parms) *libParm = strdup(parms);
1151 return true;
1152}
void Say(const char *text1, const char *text2=0, const char *txt3=0, const char *text4=0, const char *text5=0, const char *txt6=0)

References eDest.

◆ PidFile()

bool XrdOucUtils::PidFile ( XrdSysError & eDest,
const char * path )
static

Definition at line 1495 of file XrdOucUtils.cc.

1496{
1497 char buff[32];
1498 int fd;
1499
1500 if( (fd = open( path, O_WRONLY|O_CREAT|O_TRUNC, 0644 )) < 0 )
1501 {
1502 eDest.Emsg( "Config", errno, "create pidfile" );
1503 return false;
1504 }
1505
1506 if( write( fd, buff, snprintf( buff, sizeof(buff), "%d",
1507 static_cast<int>(getpid()) ) ) < 0 )
1508 {
1509 eDest.Emsg( "Config", errno, "write to pidfile" );
1510 close(fd);
1511 return false;
1512 }
1513
1514 close(fd);
1515 return true;
1516}
#define write(a, b, c)
Definition XrdPosix.hh:115

References close, eDest, open, and write.

Referenced by XrdFrmConfig::Configure().

Here is the caller graph for this function:

◆ ReLink()

int XrdOucUtils::ReLink ( const char * path,
const char * target,
mode_t mode = 0 )
static

Definition at line 1194 of file XrdOucUtils.cc.

1195{
1196 const mode_t AMode = S_IRWXU; // Only us as a default
1197 char pbuff[MAXPATHLEN+64];
1198 int n;
1199
1200// Copy the path
1201//
1202 n = strlen(path);
1203 if (n >= (int)sizeof(pbuff)) return ENAMETOOLONG;
1204 strcpy(pbuff, path);
1205
1206// Unlink the target, make the path, and create the symlink
1207//
1208 unlink(path);
1209 makePath(pbuff, (mode ? mode : AMode));
1210 if (symlink(target, path)) return errno;
1211 return 0;
1212}
#define unlink(a)
Definition XrdPosix.hh:113

References makePath(), and unlink.

Here is the call graph for this function:

◆ Sanitize()

void XrdOucUtils::Sanitize ( char * instr,
char subc = '_' )
static

Definition at line 1218 of file XrdOucUtils.cc.

1219{
1220
1221// Sanitize string according to POSIX.1-2008 stanadard using only the
1222// Portable Filename Character Set: a-z A-Z 0-9 ._- with 1st char not being -
1223//
1224 if (*str)
1225 {if (*str == '-') *str = subc;
1226 else if (*str == ' ') *str = subc;
1227 char *blank = rindex(str, ' ');
1228 if (blank) while(*blank == ' ') *blank-- = 0;
1229 while(*str)
1230 {if (!isalnum(*str) && index("_-.", *str) == 0) *str = subc;
1231 str++;
1232 }
1233 }
1234}

◆ subLogfn()

char * XrdOucUtils::subLogfn ( XrdSysError & eDest,
const char * inst,
char * logfn )
static

Definition at line 1240 of file XrdOucUtils.cc.

1241{
1242 const mode_t lfm = S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH;
1243 char buff[2048], *sp;
1244 int rc;
1245
1246 if (!inst || !*inst) return logfn;
1247 if (!(sp = rindex(logfn, '/'))) strcpy(buff, "./");
1248 else {*sp = '\0'; strcpy(buff, logfn); strcat(buff, "/");}
1249
1250 strcat(buff, inst); strcat(buff, "/");
1251
1252 if ((rc = XrdOucUtils::makePath(buff, lfm)))
1253 {eDest.Emsg("Config", rc, "create log file path", buff);
1254 return 0;
1255 }
1256
1257 if (sp) {*sp = '/'; strcat(buff, sp+1);}
1258 else strcat(buff, logfn);
1259
1260 free(logfn);
1261 return strdup(buff);
1262}

References eDest, and makePath().

Referenced by XrdOucLogging::configLog(), and XrdFrmConfig::Configure().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Token()

int XrdOucUtils::Token ( const char ** str,
char delim,
char * buff,
int bsz )
static

Definition at line 1281 of file XrdOucUtils.cc.

1282{
1283 const char *eP, *bP = *str;
1284 int aLen, mLen;
1285
1286// Trim off the delimeters. Return zero if nothing left.
1287//
1288 while(*bP && *bP == delim) bP++;
1289 if (*bP == 0) {*buff = 0; return 0;}
1290
1291// Find the next delimiter
1292//
1293 eP = bP;
1294 while(*eP && *eP != delim) eP++;
1295
1296// If we ended at a null, make sure next call will return zero
1297//
1298 if (*eP == 0) *str = eP;
1299 else *str = eP+1;
1300
1301// Calculate length and make sure we don't overrun the buffer
1302//
1303 aLen = eP-bP;
1304 if (aLen >= bsz) mLen = bsz-1;
1305 else mLen = aLen;
1306
1307// Copy token into buffer and end with null byte
1308//
1309 strncpy(buff, bP, mLen);
1310 buff[mLen] = 0;
1311
1312// Return actual length
1313//
1314 return aLen;
1315}

◆ toLower()

void XrdOucUtils::toLower ( char * str)
static

Definition at line 1268 of file XrdOucUtils.cc.

1269{
1270 unsigned char* ustr = (unsigned char*)str; // Avoid undefined behaviour
1271
1272// Change each character to lower case
1273//
1274 while(*ustr) {*ustr = tolower(*ustr); ustr++;}
1275}

Referenced by XrdCksManager::Config(), XrdOfsConfigPI::DefaultCS(), getMyFQN(), XrdNetUtils::NetConfig(), and XrdCksConfig::ParseLib().

Here is the caller graph for this function:

◆ touint8_t()

uint8_t XrdOucUtils::touint8_t ( const std::string_view sv)
static

Definition at line 1550 of file XrdOucUtils.cc.

1550 {
1551 unsigned int temp; // wider type for parsing
1552 auto [ptr, ec] = std::from_chars(sv.data(), sv.data() + sv.size(), temp);
1553
1554 if (ec == std::errc::invalid_argument) {
1555 throw std::invalid_argument("Invalid number format");
1556 }
1557 if (ec == std::errc::result_out_of_range || temp > std::numeric_limits<uint8_t>::max()) {
1558 throw std::out_of_range("Value out of range for unsigned short");
1559 }
1560
1561 return static_cast<unsigned short>(temp);
1562}

References ec.

Referenced by XrdHttpHeaderUtils::parseWantReprDigest().

Here is the caller graph for this function:

◆ trim() [1/2]

void XrdOucUtils::trim ( std::string & str)
static

Definition at line 1529 of file XrdOucUtils.cc.

1529 {
1530 // Trim leading non-letters
1531 while( str.size() && !isgraph(str[0]) ) str.erase(str.begin());
1532
1533 // Trim trailing non-letters
1534
1535 while( str.size() && !isgraph(str[str.size()-1]) )
1536 str.resize (str.size () - 1);
1537}

Referenced by XrdHttpCorsHandler::addAllowedOrigin(), XrdHttpReadRangeHandler::Configure(), XrdHttpHeaderUtils::parseReprDigest(), XrdHttpHeaderUtils::parseWantReprDigest(), and trim().

Here is the caller graph for this function:

◆ trim() [2/2]

void XrdOucUtils::trim ( std::string_view & sv)
static

Definition at line 1539 of file XrdOucUtils.cc.

1539 {
1540 const auto toTrim = [](char c) { return !isgraph(c); };
1541 size_t start = 0;
1542 size_t end = sv.size();
1543
1544 while (start < end && toTrim(sv[start])) ++start;
1545 while (end > start && toTrim(sv[end - 1])) --end;
1546
1547 sv = sv.substr(start, end - start);
1548}

◆ UidName()

int XrdOucUtils::UidName ( uid_t uID,
char * uName,
int uNsz,
time_t keepT = 0 )
static

Definition at line 1399 of file XrdOucUtils.cc.

1400{
1401 struct passwd *pEnt, pStruct;
1402 char pBuff[1024];
1403 int n, rc;
1404
1405// Get ID from cache, if allowed
1406//
1407 if (keepT)
1408 {int n = LookUp(uidMap, static_cast<unsigned int>(uID),uName,uNsz);
1409 if (n > 0) return (n < uNsz ? n : 0);
1410 }
1411
1412// Try to obtain the username. We use this form to make sure we are using
1413// the standards conforming version (compilation error otherwise).
1414//
1415 rc = getpwuid_r(uID, &pStruct, pBuff, sizeof(pBuff), &pEnt);
1416 if (rc || !pEnt)
1417 {n = snprintf(uName, uNsz, "%ud", static_cast<unsigned int>(uID));
1418 return (n >= uNsz ? 0 : n);
1419 }
1420
1421// Add entry to the cache if need be
1422//
1423 if (keepT)
1424 AddID(uidMap, static_cast<unsigned int>(uID), pEnt->pw_name, keepT);
1425
1426// Return length of username or zero if it is too big
1427//
1428 n = strlen(pEnt->pw_name);
1429 if (uNsz <= (int)strlen(pEnt->pw_name)) return 0;
1430 strcpy(uName, pEnt->pw_name);
1431 return n;
1432}

◆ Undercover()

void XrdOucUtils::Undercover ( XrdSysError & eDest,
int noLog,
int * pipeFD = 0 )
static

Definition at line 1325 of file XrdOucUtils.cc.

1326{
1327 static const int maxFiles = 256;
1328 pid_t mypid;
1329 int myfd, logFD = eDest.baseFD();
1330
1331// Issue warning if there is no logfile attached
1332//
1333 if (noLog) eDest.Emsg("Config", "Warning! No log file specified; "
1334 "backgrounding disables all logging!");
1335
1336// Fork so that we are not tied to a shell
1337//
1338 if ((mypid = fork()) < 0)
1339 {eDest.Emsg("Config", errno, "fork process 1 for backgrounding");
1340 return;
1341 }
1342 else if (mypid)
1343 {
1344 // we have been given a pair of pipe descriptors to be able to read the
1345 // status of the child process
1346 if( pipeFD )
1347 {
1348 int status = 1;
1349 close( pipeFD[1] );
1350 // read will wait untill the status is communicated by the
1351 // child process, if the child process dies before being able
1352 // to comunicate the status then read will see EOF
1353 if( read( pipeFD[0], &status, sizeof(status) ) != sizeof(status) )
1354 _exit(1);
1355 _exit(status);
1356 }
1357 // no pipes given, return success
1358 else _exit(0);
1359 }
1360
1361 if( pipeFD )
1362 close( pipeFD[0] );
1363
1364// Become the process group leader
1365//
1366 if (setsid() < 0)
1367 {eDest.Emsg("Config", errno, "doing setsid() for backgrounding");
1368 return;
1369 }
1370
1371// Fork to that we are cannot get a controlling terminal
1372//
1373 if ((mypid = fork()) < 0)
1374 {eDest.Emsg("Config", errno, "fork process 2 for backgrounding");
1375 return;
1376 }
1377 else if (mypid) _exit(0);
1378
1379// Switch stdin, stdout, and stderr to /dev/null (we can't use /dev/console
1380// unless we are root which is unlikely).
1381//
1382 if ((myfd = open("/dev/null", O_RDWR)) < 0)
1383 {eDest.Emsg("Config", errno, "open /dev/null for backgrounding");
1384 return;
1385 }
1386 dup2(myfd, 0); dup2(myfd, 1); dup2(myfd, 2); dup2(myfd, logFD);
1387
1388// Close any open file descriptors left open by the parent process
1389// but the communication pipe and the logger's shadow file descriptor.
1390//
1391 for (myfd = 3; myfd < maxFiles; myfd++)
1392 if( (!pipeFD || myfd != pipeFD[1]) && myfd != logFD ) close(myfd);
1393}

References close, eDest, open, and read.

Referenced by XrdConfig::Configure(), and XrdFrmConfig::Configure().

Here is the caller graph for this function:

◆ UrlDecode()

std::string XrdOucUtils::UrlDecode ( const std::string & input)
static

This function takes a string and returns the URL decoded string (rfc3986), also decoding + as ' ' (common in HTML form encoding).

Parameters
inputthe URL encoded string
Returns
the string URL decoded

Definition at line 1669 of file XrdOucUtils.cc.

1670{
1671 std::string out;
1672 out.reserve(input.size());
1673
1674 for (size_t i = 0; i < input.size(); ++i) {
1675 if (input[i] == '%' && i + 2 < input.size() &&
1676 std::isxdigit(input[i + 1]) &&
1677 std::isxdigit(input[i + 2])) {
1678 const int hi = from_hex(input[i + 1]);
1679 const int lo = from_hex(input[i + 2]);
1680 out.push_back(static_cast<char>((hi << 4) | lo));
1681 i += 2;
1682 } else if (input[i] == '+') {
1683 out.push_back(' ');
1684 } else {
1685 out.push_back(input[i]);
1686 }
1687 }
1688 return out;
1689}
static int from_hex(char c)

References from_hex().

Referenced by XrdOssSys::Create().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UrlEncode()

std::string XrdOucUtils::UrlEncode ( const std::string & input)
static

This function takes a string and returns the URL encoded string (rfc3986).

Parameters
inputthe string to be encoded
Returns
the string URL encoded

Definition at line 1635 of file XrdOucUtils.cc.

1636{
1637 static const char hex[] = "0123456789ABCDEF";
1638
1639 std::string out;
1640 out.reserve(input.size() * 3);
1641
1642 for (unsigned char c: input) {
1643 if (is_rfc3986_unreserved(c)) {
1644 out.push_back(c);
1645 } else {
1646 out.push_back('%');
1647 out.push_back(hex[c >> 4]);
1648 out.push_back(hex[c & 0x0f]);
1649 }
1650 }
1651 return out;
1652}
static bool is_rfc3986_unreserved(unsigned char c)

References is_rfc3986_unreserved().

Here is the call graph for this function:

◆ UserName()

int XrdOucUtils::UserName ( uid_t uID,
char * uName,
int uNsz )
static

Definition at line 1438 of file XrdOucUtils.cc.

1439{
1440 struct passwd *pEnt, pStruct;
1441 char pBuff[1024];
1442 int rc;
1443
1444// Try to obtain the username. We use this form to make sure we are using
1445// the standards conforming version (compilation error otherwise).
1446//
1447 rc = getpwuid_r(uID, &pStruct, pBuff, sizeof(pBuff), &pEnt);
1448 if (rc) return rc;
1449 if (!pEnt) return ESRCH;
1450
1451// Return length of username or zero if it is too big
1452//
1453 if (uNsz <= (int)strlen(pEnt->pw_name)) return ENAMETOOLONG;
1454 strcpy(uName, pEnt->pw_name);
1455 return 0;
1456}

Referenced by XrdPfc::Cache::Config(), XrdSecProtocolunix::getCredentials(), and Ident().

Here is the caller graph for this function:

◆ ValPath()

const char * XrdOucUtils::ValPath ( const char * path,
mode_t allow,
bool isdir )
static

Definition at line 1462 of file XrdOucUtils.cc.

1463{
1464 static const mode_t mMask = S_IRWXU | S_IRWXG | S_IRWXO;
1465 struct stat buf;
1466
1467// Check if this really exists
1468//
1469 if (stat(path, &buf))
1470 {if (errno == ENOENT) return "does not exist.";
1471 return XrdSysE2T(errno);
1472 }
1473
1474// Verify that this is the correct type of file
1475//
1476 if (isdir)
1477 {if (!S_ISDIR(buf.st_mode)) return "is not a directory.";
1478 } else {
1479 if (!S_ISREG(buf.st_mode)) return "is not a file.";
1480 }
1481
1482// Verify that the does not have excessive privileges
1483//
1484 if ((buf.st_mode & mMask) & ~allow) return "has excessive access rights.";
1485
1486// All went well
1487//
1488 return 0;
1489}

References stat, and XrdSysE2T().

Referenced by XrdCl::InitTLS().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ pathMode

const mode_t XrdOucUtils::pathMode = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH
static

Definition at line 48 of file XrdOucUtils.hh.

Referenced by makeHome().


The documentation for this class was generated from the following files: