318{
319
320
321
322
323
324
325
326 const char *xrdInst="XRDINSTANCE=";
327
328 int retc, NoGo = 0, clPort = -1;
329 const char *temp;
330 char c, buff[512], *dfltProt, *libProt = 0;
331 uid_t myUid = 0;
332 gid_t myGid = 0;
333 extern char *optarg;
334 extern int optind, opterr;
336 int pipeFD[2] = {-1, -1};
337 const char *pidFN = 0;
338 static const int myMaxc = 80;
339 char **urArgv, *myArgv[myMaxc], argBuff[myMaxc*3+8];
340 char *argbP = argBuff, *argbE = argbP+sizeof(argBuff)-4;
341 char *ifList = 0;
342 int myArgc = 1, urArgc = argc, i;
343 bool noV6, ipV4 = false, ipV6 = false, rootChk = true, optbg = false;
344
345
346
348 for (int k = 1; k < argc; k++)
349 {CmdLine += ' '; CmdLine += argv[k];}
350
351
352
353 retc = strlen(argv[0]);
354 while(retc--) if (argv[0][retc] == '/') break;
355 myProg = &argv[0][retc+1];
356
357
358
359
360
361
362 {char *p = dfltProt = strdup(myProg);
363 while(*p && (*p == '.' || *p == '-')) p++;
364 if (*p)
365 {char *dot = index(p, '.'), *dash = index(p, '-');
366 if (dot && (dot < dash || !dash)) p = dot;
367 else if (dash) p = dash;
368 else p = 0;
369 if (p) *p = '\0';
370 if (!strcmp("xrootd", dfltProt)) dfltProt[5] = 0;
371 else if (!strcmp("cmsd", dfltProt)) dfltProt[3] = 0;
372 }
373 }
374 myArgv[0] = argv[0];
375
376
377
378
379 i = 1;
380 while(i < argc)
381 {if (*(argv[i]) == '-' && *(argv[i]+1) == '+')
382 {int n = strlen(argv[i]+2), j = i+1, k = 1;
383 if (urArgc == argc) urArgc = i;
384 if (n) memcpy(buff, argv[i]+2, (n > 256 ? 256 : n));
385 strcpy(&(buff[n]), ".argv**");
386 while(j < argc && (*(argv[j]) != '-' || *(argv[j]+1) != '+')) j++;
387 urArgv = new char*[j-i+1];
388 urArgv[0] = argv[0];
389 i++;
390 while(i < j) urArgv[k++] = argv[i++];
391 urArgv[k] = 0;
393 strcpy(&(buff[n]), ".argc");
395 } else i++;
396 }
398
399
400
401
402 opterr = 0;
403 if (argc > 1 && '-' == *argv[1])
404 while ((c = getopt(urArgc,argv,":a:A:bc:dhHI:k:l:L:n:p:P:R:s:S:vw:W:z"))
405 && ((unsigned char)c != 0xff))
406 { switch(c)
407 {
408 case 'a': if (AdminPath) free(AdminPath);
409 AdminPath = strdup(optarg);
410 AdminMode = ProtInfo.AdmMode = S_IRWXU;
412 break;
413 case 'A': if (AdminPath) free(AdminPath);
414 AdminPath = strdup(optarg);
415 AdminMode = ProtInfo.AdmMode = S_IRWXU | S_IRWXG;
417 break;
418 case 'b': optbg = true;
419 break;
420 case 'c': if (ConfigFN) free(ConfigFN);
421 ConfigFN = strdup(optarg);
422 break;
424 ProtInfo.DebugON = 1;
426 break;
428 break;
430 break;
431 case 'I': if (!strcmp("v4", optarg)) {ipV4 = true; ipV6 = false;}
432 else if (!strcmp("v6", optarg)) {ipV4 = false; ipV6 = true;}
433 else {
Log.
Emsg(
"Config",
"Invalid -I argument -",optarg);
435 }
436 break;
438 {
Log.
Emsg(
"Config",
"Invalid -k argument -",optarg);
440 }
441 break;
442 case 'l': LogInfo.logArg = optarg;
443 break;
444 case 'L': if (!*optarg)
445 {
Log.
Emsg(
"Config",
"Protocol library path not specified.");
447 }
448 if (libProt) free(libProt);
449 libProt = strdup(optarg);
450 break;
451 case 'n': myInsName = (!strcmp(optarg,"anon")||!strcmp(optarg,"default")
452 ? 0 : optarg);
453 break;
455 break;
456 case 'P': if (dfltProt) free(dfltProt);
457 dfltProt = strdup(optarg);
458 break;
459 case 'R':
if (!(getUG(optarg, myUid, myGid)))
Usage(1);
460 rootChk = false;
461 break;
462 case 's': pidFN = optarg;
463 break;
464 case 'S': mySitName = optarg;
465 break;
466 case ':': buff[0] =
'-'; buff[1] =
optopt; buff[2] = 0;
467 Log.
Emsg(
"Config", buff,
"parameter not specified.");
469 break;
470 case 'v': std::cerr <<XrdVSTRING <<std::endl;
471 _exit(0);
472 break;
473 case 'w': if (HomePath) free(HomePath);
474 HomePath = strdup(optarg);
475 HomeMode = S_IRWXU;
476 Specs |= hpSpec;
477 break;
478 case 'W': if (HomePath) free(HomePath);
479 HomePath = strdup(optarg);
480 HomeMode = S_IRWXU | S_IRGRP | S_IXGRP;
481 Specs |= hpSpec;
482 break;
483 case 'z': LogInfo.hiRes = true;
484 break;
485
487 {
Log.
Emsg(
"Config",
"Long options are not supported.");
489 }
490 if (myArgc >= myMaxc || argbP >= argbE)
491 {
Log.
Emsg(
"Config",
"Too many command line arguments.");
493 }
494 myArgv[myArgc++] = argbP;
495 *argbP++ =
'-'; *argbP++ =
optopt; *argbP++ = 0;
496 break;
497 }
498 }
499
500
501
503 {
Log.
Emsg(
"Config",
"Command line adminpath is not absolute.");
504 exit(17);
505 }
506
507
508
509 if (HomePath && *HomePath != '/')
510 {
Log.
Emsg(
"Config",
"Command line home path is not absolute.");
511 exit(17);
512 }
513
514
515
516
517 if (ConfigFN) setCFG(true);
518
519
520
523 else if (ipV6){
if (noV6)
Log.
Say(
"Config warning: ipV6 appears to be broken;"
524 " forced ipV6 mode not advised!");
526 }
527 else if (noV6)
Log.
Say(
"Config warning: ipV6 is misconfigured or "
528 "unavailable; reverting to ipV4.");
529
530
531
533
534
535
536 if (myGid && setegid(myGid))
537 {
Log.
Emsg(
"Config", errno,
"set effective gid"); exit(17);}
538 if (myUid && seteuid(myUid))
539 {
Log.
Emsg(
"Config", errno,
"set effective uid"); exit(17);}
540
541
542
543 if (rootChk && geteuid() == 0)
544 {
Log.
Emsg(
"Config",
"Security reasons prohibit running as "
545 "superuser; program is terminating.");
546 _exit(8);
547 }
548
549
550
551 if (urArgc-
optind+2 >= myMaxc)
552 {
Log.
Emsg(
"Config",
"Too many command line arguments.");
554 }
556
557
558
559 myArgv[myArgc] = 0;
560 ProtInfo.argc = myArgc;
561 ProtInfo.argv = myArgv;
562
563
564
565 if (optbg)
566 {
567#ifdef WIN32
569#else
570 if (pipe( pipeFD ) == -1)
571 {
Log.
Emsg(
"Config", errno,
"create a pipe"); exit(17);}
573#endif
574 }
575
576
577
578
580 if (!(myName = myIPAddr->
Name(0, &temp))) myName =
"";
581
582
583
584 ProtInfo.myName = myName;
585 ProtInfo.myAddr = myIPAddr->
SockAddr();
587 ProtInfo.myProg = myProg;
588
589
590
591
592
593 sprintf(buff,"%s%s %s@%s", xrdInst, myProg, ProtInfo.myInst, myName);
594 myInstance = strdup(buff);
595 putenv(myInstance);
596 myInstance += strlen(xrdInst);
600
601
602
603 if (LogInfo.logArg)
604 {LogInfo.xrdEnv = &
theEnv;
605 LogInfo.iName = myInsName;
606 LogInfo.cfgFn = ConfigFN;
610 }
611
612
613
614
615
616 if (!(*myName))
617 {
Log.
Emsg(
"Config",
"Unable to determine host name; ",
618 (temp ? temp : "reason unknown"),
619 "; execution terminated.");
620 _exit(16);
621 }
622
623
624
626
627
628
629 strcpy(buff, "Starting on ");
630 retc = strlen(buff);
633 Log.
Say(0, CmdLine.c_str());
635
636
637
638
639
641 {
Log.
Emsg(
"Config",myName,
"does not appear to be registered in the DNS.");
642 Log.
Emsg(
"Config",
"Verify that the '/etc/hosts' file is correct and "
643 "this machine is registered in DNS.");
644 Log.
Emsg(
"Config",
"Execution continues but connection failures may occur.");
645 myDomain = 0;
646 } else if (!(myDomain = index(myName, '.')))
647 Log.
Say(
"Config warning: this hostname, ", myName,
648 ", is registered without a domain qualification.");
649
650
651
652 Firstcp = Lastcp =
new XrdConfigProt(strdup(dfltProt), libProt, 0);
653
654
655
656 Log.
Say(
"++++++ ", myInstance,
" initialization started.");
657
658
659
660 devNull = XrdSysFD_Open(
"/dev/null", O_RDONLY);
662 {
Log.
Emsg(
"Config", errno,
"open '/dev/null' which is required!");
663 NoGo = 1;
664 }
665
666
667
668 if (ConfigFN)
669 {
Log.
Say(
"Config using configuration file ", ConfigFN);
670 ProtInfo.ConfigFN = ConfigFN;
671 NoGo = ConfigProc();
672 }
673 if (clPort >= 0) PortTCP = clPort;
674 if (ProtInfo.DebugON)
677 }
678
679
680
681 NoGo |= SetupAPath();
682
683
684
685 if (!NoGo)
686 {if (!tlsCert) ProtInfo.tlsCtx= 0;
687 else {
Log.
Say(
"++++++ ", myInstance,
" TLS initialization started.");
688 if (SetupTLS())
689 {
Log.
Say(
"------ ",myInstance,
" TLS initialization ended.");
692 } else {
693 NoGo = 1;
694 Log.
Say(
"------ ",myInstance,
" TLS initialization failed.");
695 }
696 }
697 }
698
699
700
701
702 if (!NoGo)
704 {
Log.
Say(
"Config TLS port specification ignored; TLS not configured!");
705 PortTLS = -1;
706 } else {
708 ProtInfo.tlsPort = (PortTLS > 0 ? PortTLS : 0);
709 }
710 }
711
712
713
715
716
717
720
721
722
724 {
Log.
Emsg(
"Config",
"Unable to determine interface addresses!");
725 NoGo = 1;
726 }
727
728
729
730 if ((myInsName || HomePath)
732
733
734
735 if (!PidFile(pidFN, optbg)) NoGo = 1;
736
737
738
739 if (!NoGo) Manifest(pidFN);
740
741
742
743 if (!NoGo) NoGo = Setup(dfltProt, libProt);
744
745
746
747 setCFG(false);
748
749
750
751
752 if (tmoInfo && !NoGo)
754 if (!theGS)
Log.
Say(
"Config warning: TCP monitoring not enabled; "
755 "tcpmonlib plugin not loaded!");
759 }
760 }
761
762
763
764#ifndef WIN32
765 if (optbg)
766 {
767 int status = NoGo ? 1 : 0;
768 if(
write( pipeFD[1], &status,
sizeof( status ) )) {};
770 }
771#endif
772
773
774
775 temp = (NoGo ? " initialization failed." : " initialization completed.");
776 sprintf(buff, "%s:%d", myInstance, PortTCP);
777 Log.
Say(
"------ ", buff, temp);
778 if (LogInfo.logArg)
779 {strcat(buff, " running ");
780 retc = strlen(buff);
783 }
784 return NoGo;
785}
void Usage(const char *msg)
const sockaddr * SockAddr()
const char * Name(const char *eName=0, const char **eText=0)
static int GetIF(XrdOucTList **ifList, const char **eText=0)
static void SetMsgs(XrdSysError *erp)
void PutInt(const char *varname, long value)
static int Export(const char *Var, const char *Val)
void * GetPtr(const char *varname)
void PutPtr(const char *varname, void *value)
static bool configLog(XrdSysError &eDest, configLogInfo &logInfo)
T * Load(const char *Symbol)
static const char * Set(const char *name, int maxlen=15)
static const char * InstName(int TranOpt=0)
static void makeHome(XrdSysError &eDest, const char *inst)
static void Undercover(XrdSysError &eDest, int noLog, int *pipeFD=0)
static int a2p(XrdSysError &, const char *ptype, const char *val, bool anyOK=true)
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
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)
XrdSysLogger * logger(XrdSysLogger *lp=0)
void AddMsg(const char *msg)
int ParseKeep(const char *arg)
static void setDebug(XrdSysError *erp)
static int FmtUname(char *buff, int blen)
XrdOucPinKing< XrdTcpMonPin > KingPin