-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdConfig.cc
1872 lines (1602 loc) · 64.6 KB
/
XrdConfig.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************/
/* */
/* X r d C o n f i g . c c */
/* */
/* (c) 2011 by the Board of Trustees of the Leland Stanford, Jr., University */
/* Produced by Andrew Hanushevsky for Stanford University under contract */
/* DE-AC02-76-SFO0515 with the Deprtment of Energy */
/* */
/* This file is part of the XRootD software suite. */
/* */
/* XRootD is free software: you can redistribute it and/or modify it under */
/* the terms of the GNU Lesser General Public License as published by the */
/* Free Software Foundation, either version 3 of the License, or (at your */
/* option) any later version. */
/* */
/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
/* License for more details. */
/* */
/* You should have received a copy of the GNU Lesser General Public License */
/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
/* */
/* The copyright holder's institutional names and contributor's names may not */
/* be used to endorse or promote products derived from this software without */
/* specific prior written permission of the institution or contributor. */
/******************************************************************************/
/*
The default port number comes from:
1) The command line option,
2) The config file,
3) The /etc/services file for service corresponding to the program name.
*/
#include <unistd.h>
#include <ctype.h>
#include <pwd.h>
#include <string.h>
#include <stdio.h>
#include <sys/param.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "XrdVersion.hh"
#include "Xrd/XrdBuffXL.hh"
#include "Xrd/XrdConfig.hh"
#include "Xrd/XrdInfo.hh"
#include "Xrd/XrdLink.hh"
#include "Xrd/XrdPoll.hh"
#include "Xrd/XrdStats.hh"
#include "XrdNet/XrdNetAddr.hh"
#include "XrdNet/XrdNetIF.hh"
#include "XrdNet/XrdNetSecurity.hh"
#include "XrdNet/XrdNetUtils.hh"
#include "XrdOuc/XrdOuca2x.hh"
#include "XrdOuc/XrdOucEnv.hh"
#include "XrdOuc/XrdOucLogging.hh"
#include "XrdOuc/XrdOucSiteName.hh"
#include "XrdOuc/XrdOucStream.hh"
#include "XrdOuc/XrdOucUtils.hh"
#include "XrdSys/XrdSysHeaders.hh"
#include "XrdSys/XrdSysTimer.hh"
#include "XrdSys/XrdSysUtils.hh"
#ifdef __linux__
#include <netinet/tcp.h>
#endif
#ifdef __APPLE__
#include <AvailabilityMacros.h>
#endif
/******************************************************************************/
/* S t a t i c O b j e c t s */
/******************************************************************************/
const char *XrdConfig::TraceID = "Config";
namespace XrdNetSocketCFG
{
extern int ka_Idle;
extern int ka_Itvl;
extern int ka_Icnt;
};
namespace XrdGlobal
{
XrdBuffXL xlBuff;
}
namespace
{
XrdOucEnv theEnv;
};
/******************************************************************************/
/* d e f i n e s */
/******************************************************************************/
#define TS_Xeq(x,m) if (!strcmp(x,var)) return m(eDest, Config);
#ifndef S_IAMB
#define S_IAMB 0x1FF
#endif
/******************************************************************************/
/* L o c a l C l a s s e s */
/******************************************************************************/
/******************************************************************************/
/* X r d C o n f i g P r o t */
/******************************************************************************/
class XrdConfigProt
{
public:
XrdConfigProt *Next;
char *proname;
char *libpath;
char *parms;
int port;
int wanopt;
XrdConfigProt(char *pn, char *ln, char *pp, int np=-1, int wo=0)
{Next = 0; proname = pn; libpath = ln; parms = pp;
port=np; wanopt = wo;
}
~XrdConfigProt()
{free(proname);
if (libpath) free(libpath);
if (parms) free(parms);
}
};
/******************************************************************************/
/* C o n s t r u c t o r */
/******************************************************************************/
XrdConfig::XrdConfig() : Log(&Logger, "Xrd"), Trace(&Log), Sched(&Log, &Trace),
BuffPool(&Log, &Trace)
{
// Preset all variables with common defaults
//
PortTCP = -1;
PortUDP = -1;
PortWAN = 0;
ConfigFN = 0;
myInsName= 0;
mySitName= 0;
AdminPath= strdup("/tmp");
AdminMode= 0700;
Police = 0;
Net_Blen = 0; // Accept OS default (leave Linux autotune in effect)
Net_Opts = XRDNET_KEEPALIVE;
Wan_Blen = 1024*1024; // Default window size 1M
Wan_Opts = XRDNET_KEEPALIVE;
repDest[0] = 0;
repDest[1] = 0;
repInt = 600;
repOpts = 0;
ppNet = 0;
NetTCPlep = -1;
NetADM = 0;
coreV = 1;
memset(NetTCP, 0, sizeof(NetTCP));
Firstcp = Lastcp = 0;
ProtInfo.eDest = &Log; // Stable -> Error Message/Logging Handler
ProtInfo.NetTCP = 0; // Stable -> Network Object
ProtInfo.BPool = &BuffPool; // Stable -> Buffer Pool Manager
ProtInfo.Sched = &Sched; // Stable -> System Scheduler
ProtInfo.ConfigFN= 0; // We will fill this in later
ProtInfo.Stats = 0; // We will fill this in later
ProtInfo.Trace = &Trace; // Stable -> Trace Information
ProtInfo.AdmPath = AdminPath; // Stable -> The admin path
ProtInfo.AdmMode = AdminMode; // Stable -> The admin path mode
ProtInfo.theEnv = &theEnv; // Additional information
ProtInfo.Format = XrdFORMATB;
ProtInfo.WANPort = 0;
ProtInfo.WANWSize = 0;
ProtInfo.WSize = 0;
ProtInfo.ConnMax = -1; // Max connections (fd limit)
ProtInfo.readWait = 3*1000; // Wait time for data before we reschedule
ProtInfo.idleWait = 0; // Seconds connection may remain idle (0=off)
ProtInfo.hailWait =30*1000; // Wait time for data before we drop connection
ProtInfo.DebugON = 0; // 1 if started with -d
ProtInfo.argc = 0;
ProtInfo.argv = 0;
XrdNetAddr::SetCache(3*60*60); // Cache address resolutions for 3 hours
}
/******************************************************************************/
/* C o n f i g u r e */
/******************************************************************************/
int XrdConfig::Configure(int argc, char **argv)
{
/*
Function: Establish configuration at start up time.
Input: None.
Output: 0 upon success or !0 otherwise.
*/
const char *xrdInst="XRDINSTANCE=";
int retc, NoGo = 0, clPort = -1, optbg = 0;
const char *temp;
char c, buff[512], *dfltProt, *libProt = 0;
uid_t myUid = 0;
gid_t myGid = 0;
extern char *optarg;
extern int optind, opterr;
struct XrdOucLogging::configLogInfo LogInfo;
int pipeFD[2] = {-1, -1};
const char *pidFN = 0;
static const int myMaxc = 80;
char **urArgv, *myArgv[myMaxc], argBuff[myMaxc*3+8];
char *argbP = argBuff, *argbE = argbP+sizeof(argBuff)-4;
char *ifList = 0;
int myArgc = 1, urArgc = argc, i;
bool noV6, ipV4 = false, ipV6 = false;
// Obtain the protocol name we will be using
//
retc = strlen(argv[0]);
while(retc--) if (argv[0][retc] == '/') break;
myProg = dfltProt = &argv[0][retc+1];
// Setup the initial required protocol. The program name matches the protocol
// name but may be arbitrarily suffixed. We need to ignore this suffix. So we
// look for it here and it it exists we duplicate argv[0] (yes, loosing some
// bytes - sorry valgrind) without the suffix.
//
{char *p = dfltProt;
while(*p && (*p == '.' || *p == '-')) p++;
if (*p)
{char *dot = index(p, '.'), *dash = index(p, '-'), sepc;
if (dot && (dot < dash || !dash)) p = dot;
else if (dash) p = dash;
else p = 0;
if (p) {sepc = *p; *p = '\0'; dfltProt = strdup(dfltProt); *p = sepc;}
}
}
myArgv[0] = argv[0];
// Prescan the argument list to see if there is a passthrough option. In any
// case, we will set the ephemeral argv/arg in the environment.
//
i = 1;
while(i < argc)
{if (*(argv[i]) == '-' && *(argv[i]+1) == '+')
{int n = strlen(argv[i]+2), j = i+1, k = 1;
if (urArgc == argc) urArgc = i;
if (n) strncpy(buff, argv[i]+2, (n > 256 ? 256 : n));
strcpy(&(buff[n]), ".argv**");
while(j < argc && (*(argv[j]) != '-' || *(argv[j]+1) != '+')) j++;
urArgv = new char*[j-i+1];
urArgv[0] = argv[0];
i++;
while(i < j) urArgv[k++] = argv[i++];
urArgv[k] = 0;
theEnv.PutPtr(buff, urArgv);
strcpy(&(buff[n]), ".argc");
theEnv.PutInt(buff, static_cast<long>(k));
} else i++;
}
theEnv.PutPtr("argv[0]", argv[0]);
// Process the options. Note that we cannot passthrough long options or
// options that take arguments because getopt permutes the arguments.
//
opterr = 0;
if (argc > 1 && '-' == *argv[1])
while ((c = getopt(urArgc,argv,":bc:dhHI:k:l:L:n:p:P:R:s:S:vz"))
&& ((unsigned char)c != 0xff))
{ switch(c)
{
case 'b': optbg = 1;
break;
case 'c': if (ConfigFN) free(ConfigFN);
ConfigFN = strdup(optarg);
break;
case 'd': Trace.What |= TRACE_ALL;
ProtInfo.DebugON = 1;
XrdOucEnv::Export("XRDDEBUG", "1");
break;
case 'h': Usage(0);
break;
case 'H': Usage(-1);
break;
case 'I': if (!strcmp("v4", optarg)) {ipV4 = true; ipV6 = false;}
else if (!strcmp("v6", optarg)) {ipV4 = false; ipV6 = true;}
else {Log.Emsg("Config", "Invalid -I argument -",optarg);
Usage(1);
}
break;
case 'k': if (!(LogInfo.keepV = Log.logger()->ParseKeep(optarg)))
{Log.Emsg("Config","Invalid -k argument -",optarg);
Usage(1);
}
break;
case 'l': LogInfo.logArg = optarg;
break;
case 'L': if (!*optarg)
{Log.Emsg("Config", "Protocol library path not specified.");
Usage(1);
}
if (libProt) free(libProt);
libProt = strdup(optarg);
break;
case 'n': myInsName = (!strcmp(optarg,"anon")||!strcmp(optarg,"default")
? 0 : optarg);
break;
case 'p': if ((clPort = yport(&Log, "tcp", optarg)) < 0) Usage(1);
break;
case 'P': dfltProt = optarg;
break;
case 'R': if (!(getUG(optarg, myUid, myGid))) Usage(1);
break;
case 's': pidFN = optarg;
break;
case 'S': mySitName = optarg;
break;
case ':': buff[0] = '-'; buff[1] = optopt; buff[2] = 0;
Log.Emsg("Config", buff, "parameter not specified.");
Usage(1);
break;
case 'v': cerr <<XrdVSTRING <<endl;
_exit(0);
break;
case 'z': LogInfo.hiRes = true;
break;
default: if (optopt == '-' && *(argv[optind]+1) == '-')
{Log.Emsg("Config", "Long options are not supported.");
Usage(1);
}
if (myArgc >= myMaxc || argbP >= argbE)
{Log.Emsg("Config", "Too many command line arguments.");
Usage(1);
}
myArgv[myArgc++] = argbP;
*argbP++ = '-'; *argbP++ = optopt; *argbP++ = 0;
break;
}
}
// The first thing we must do is to set the correct networking mode
//
noV6 = XrdNetAddr::IPV4Set();
if (ipV4) XrdNetAddr::SetIPV4();
else if (ipV6){if (noV6) Log.Say("Config warning: ipV6 appears to be broken;"
" forced ipV6 mode not advised!");
XrdNetAddr::SetIPV6();
}
else if (noV6) Log.Say("Config warning: ipV6 is misconfigured or "
"unavailable; reverting to ipV4.");
// Set the site name if we have one
//
if (mySitName) mySitName = XrdOucSiteName::Set(mySitName, 63);
// Drop into non-privileged state if so requested
//
if (myGid && setegid(myGid))
{Log.Emsg("Config", errno, "set effective gid"); exit(17);}
if (myUid && seteuid(myUid))
{Log.Emsg("Config", errno, "set effective uid"); exit(17);}
// Pass over any parameters
//
if (urArgc-optind+2 >= myMaxc)
{Log.Emsg("Config", "Too many command line arguments.");
Usage(1);
}
for ( ; optind < urArgc; optind++) myArgv[myArgc++] = argv[optind];
// Record the actual arguments that we will pass on
//
myArgv[myArgc] = 0;
ProtInfo.argc = myArgc;
ProtInfo.argv = myArgv;
// Resolve background/foreground issues
//
if (optbg)
{
#ifdef WIN32
XrdOucUtils::Undercover(&Log, !LogInfo.logArg);
#else
if (pipe( pipeFD ) == -1)
{Log.Emsg("Config", errno, "create a pipe"); exit(17);}
XrdOucUtils::Undercover(Log, !LogInfo.logArg, pipeFD);
#endif
}
// Bind the log file if we have one
//
if (LogInfo.logArg)
{LogInfo.xrdEnv = &theEnv;
LogInfo.iName = myInsName;
LogInfo.cfgFn = ConfigFN;
if (!XrdOucLogging::configLog(Log, LogInfo)) _exit(16);
Log.logger()->AddMsg(XrdBANNER);
}
// Get the full host name. In theory, we should always get some kind of name.
// We must define myIPAddr here because we may need to run in v4 mode and
// that doesn't get set until after the options are scanned.
//
static XrdNetAddr *myIPAddr = new XrdNetAddr((int)0);
if (!(myName = myIPAddr->Name(0, &temp)))
{Log.Emsg("Config", "Unable to determine host name; ",
(temp ? temp : "reason unknown"),
"; execution terminated.");
_exit(16);
}
// Verify that we have a real name. We've had problems with people setting up
// bad /etc/hosts files that can cause connection failures if "allow" is used.
// Otherwise, determine our domain name.
//
if (!myIPAddr->isRegistered())
{Log.Emsg("Config",myName,"does not appear to be registered in the DNS.");
Log.Emsg("Config","Verify that the '/etc/hosts' file is correct and "
"this machine is registered in DNS.");
Log.Emsg("Config", "Execution continues but connection failures may occur.");
myDomain = 0;
} else if (!(myDomain = index(myName, '.')))
Log.Say("Config warning: this hostname, ", myName,
", is registered without a domain qualification.");
// Get our IP address and FQN
//
ProtInfo.myName = myName;
ProtInfo.myAddr = myIPAddr->SockAddr();
ProtInfo.myInst = XrdOucUtils::InstName(myInsName);
ProtInfo.myProg = myProg;
// Set the Environmental variable to hold the instance name
// XRDINSTANCE=<pgm> <instance name>@<host name>
// XrdOucEnv::Export("XRDINSTANCE")
//
sprintf(buff,"%s%s %s@%s", xrdInst, myProg, ProtInfo.myInst, myName);
myInstance = strdup(buff);
putenv(myInstance); // XrdOucEnv::Export("XRDINSTANCE",...)
myInstance += strlen(xrdInst);
XrdOucEnv::Export("XRDHOST", myName);
XrdOucEnv::Export("XRDNAME", ProtInfo.myInst);
XrdOucEnv::Export("XRDPROG", myProg);
XrdNetIF::SetMsgs(&Log);
// Put out the herald
//
strcpy(buff, "Starting on ");
retc = strlen(buff);
XrdSysUtils::FmtUname(buff+retc, sizeof(buff)-retc);
Log.Say(0, buff);
Log.Say(XrdBANNER);
// Setup the initial required protocol.
//
Firstcp = Lastcp = new XrdConfigProt(strdup(dfltProt), libProt, 0);
// Let start it up!
//
Log.Say("++++++ ", myInstance, " initialization started.");
// Process the configuration file, if one is present
//
if (ConfigFN && *ConfigFN)
{Log.Say("Config using configuration file ", ConfigFN);
ProtInfo.ConfigFN = ConfigFN;
setCFG();
NoGo = ConfigProc();
}
if (clPort >= 0) PortTCP = clPort;
if (ProtInfo.DebugON)
{Trace.What = TRACE_ALL;
XrdSysThread::setDebug(&Log);
}
// Put largest buffer size in the env
//
theEnv.PutInt("MaxBuffSize", XrdGlobal::xlBuff.MaxSize());
// Export the network interface list at this point
//
if (ppNet && XrdNetIF::GetIF(ifList, 0, true))
XrdOucEnv::Export("XRDIFADDRS",ifList);
// Configure network routing
//
if (!XrdInet::netIF.SetIF(myIPAddr, ifList))
{Log.Emsg("Config", "Unable to determine interface addresses!");
NoGo = 1;
}
// Now initialize the default protocl
//
if (!NoGo) NoGo = Setup(dfltProt);
// If we hae a net name change the working directory
//
if (myInsName) XrdOucUtils::makeHome(Log, myInsName);
// if we call this it means that the daemon has forked and we are
// in the child process
#ifndef WIN32
if (optbg)
{
if (pidFN && !XrdOucUtils::PidFile(Log, pidFN ) )
NoGo = 1;
int status = NoGo ? 1 : 0;
if(write( pipeFD[1], &status, sizeof( status ) )) {};
close( pipeFD[1]);
}
#endif
// Establish a pid/manifest file for auto-collection
//
if (!NoGo) Manifest(pidFN);
// All done, close the stream and return the return code.
//
temp = (NoGo ? " initialization failed." : " initialization completed.");
sprintf(buff, "%s:%d", myInstance, PortTCP);
Log.Say("------ ", buff, temp);
if (LogInfo.logArg)
{strcat(buff, " running ");
retc = strlen(buff);
XrdSysUtils::FmtUname(buff+retc, sizeof(buff)-retc);
Log.logger()->AddMsg(buff);
}
return NoGo;
}
/******************************************************************************/
/* C o n f i g X e q */
/******************************************************************************/
int XrdConfig::ConfigXeq(char *var, XrdOucStream &Config, XrdSysError *eDest)
{
int dynamic;
// Determine whether is is dynamic or not
//
if (eDest) dynamic = 1;
else {dynamic = 0; eDest = &Log;}
// Process common items
//
TS_Xeq("buffers", xbuf);
TS_Xeq("network", xnet);
TS_Xeq("sched", xsched);
TS_Xeq("trace", xtrace);
// Process items that can only be processed once
//
if (!dynamic)
{
TS_Xeq("adminpath", xapath);
TS_Xeq("allow", xallow);
TS_Xeq("port", xport);
TS_Xeq("protocol", xprot);
TS_Xeq("report", xrep);
TS_Xeq("sitename", xsit);
TS_Xeq("timeout", xtmo);
}
// No match found, complain.
//
eDest->Say("Config warning: ignoring unknown xrd directive '",var,"'.");
Config.Echo();
return 0;
}
/******************************************************************************/
/* P r i v a t e F u n c t i o n s */
/******************************************************************************/
/******************************************************************************/
/* A S o c k e t */
/******************************************************************************/
int XrdConfig::ASocket(const char *path, const char *fname, mode_t mode)
{
char xpath[MAXPATHLEN+8], sokpath[108];
int plen = strlen(path), flen = strlen(fname);
int rc;
// Make sure we can fit everything in our buffer
//
if ((plen + flen + 3) > (int)sizeof(sokpath))
{Log.Emsg("Config", "admin path", path, "too long");
return 1;
}
// Create the directory path
//
strcpy(xpath, path);
if ((rc = XrdOucUtils::makePath(xpath, mode)))
{Log.Emsg("Config", rc, "create admin path", xpath);
return 1;
}
// *!*!* At this point we do not yet support the admin path for xrd.
// sp we comment out all of the following code.
/*
// Construct the actual socket name
//
if (sokpath[plen-1] != '/') sokpath[plen++] = '/';
strcpy(&sokpath[plen], fname);
// Create an admin network
//
NetADM = new XrdInet(&Log);
if (myDomain) NetADM->setDomain(myDomain);
// Bind the netwok to the named socket
//
if (!NetADM->Bind(sokpath)) return 1;
// Set the mode and return
//
chmod(sokpath, mode); // This may fail on some platforms
*/
return 0;
}
/******************************************************************************/
/* C o n f i g P r o c */
/******************************************************************************/
int XrdConfig::ConfigProc()
{
char *var;
int cfgFD, retc, NoGo = 0;
XrdOucEnv myEnv;
XrdOucStream Config(&Log, myInstance, &myEnv, "=====> ");
// Try to open the configuration file.
//
if ( (cfgFD = open(ConfigFN, O_RDONLY, 0)) < 0)
{Log.Emsg("Config", errno, "open config file", ConfigFN);
return 1;
}
Config.Attach(cfgFD);
// Now start reading records until eof.
//
while((var = Config.GetMyFirstWord()))
if (!strncmp(var, "xrd.", 4)
|| !strcmp (var, "all.adminpath")
|| !strcmp (var, "all.sitename" ))
if (ConfigXeq(var+4, Config)) {Config.Echo(); NoGo = 1;}
// Now check if any errors occured during file i/o
//
if ((retc = Config.LastError()))
NoGo = Log.Emsg("Config", retc, "read config file", ConfigFN);
Config.Close();
// Return final return code
//
return NoGo;
}
/******************************************************************************/
/* g e t U G */
/******************************************************************************/
int XrdConfig::getUG(char *parm, uid_t &newUid, gid_t &newGid)
{
struct passwd *pp;
// Get the userid entry
//
if (!(*parm))
{Log.Emsg("Config", "-R user not specified."); return 0;}
if (isdigit(*parm))
{if (!(newUid = atol(parm)))
{Log.Emsg("Config", "-R", parm, "is invalid"); return 0;}
pp = getpwuid(newUid);
}
else pp = getpwnam(parm);
// Make sure it is valid and acceptable
//
if (!pp)
{Log.Emsg("Config", errno, "retrieve -R user password entry");
return 0;
}
if (!(newUid = pp->pw_uid))
{Log.Emsg("Config", "-R", parm, "is still unacceptably a superuser!");
return 0;
}
newGid = pp->pw_gid;
return 1;
}
/******************************************************************************/
/* M a n i f e s t */
/******************************************************************************/
void XrdConfig::Manifest(const char *pidfn)
{
const char *Slash;
char envBuff[8192], pwdBuff[1024], manBuff[1024], *pidP, *sP, *xP;
int envFD, envLen;
// Get the current working directory
//
if (!getcwd(pwdBuff, sizeof(pwdBuff)))
{Log.Emsg("Config", "Unable to get current working directory!");
return;
}
// Prepare for symlinks
//
strcpy(envBuff, ProtInfo.AdmPath);
envLen = strlen(envBuff);
if (envBuff[envLen-1] != '/') {envBuff[envLen] = '/'; envLen++;}
strcpy(envBuff+envLen, ".xrd/");
xP = envBuff+envLen+5;
// Create a symlink to the configuration file
//
if ((sP = getenv("XRDCONFIGFN")))
{sprintf(xP, "=/conf/%s.cf", myProg);
XrdOucUtils::ReLink(envBuff, sP);
}
// Create a symlink to where core files will be found
//
sprintf(xP, "=/core/%s", myProg);
XrdOucUtils::ReLink(envBuff, pwdBuff);
// Create a symlink to where log files will be found
//
if ((sP = getenv("XRDLOGDIR")))
{sprintf(xP, "=/logs/%s", myProg);
XrdOucUtils::ReLink(envBuff, sP);
}
// Create a symlink to out proc information (Linux only)
//
#ifdef __linux__
sprintf(xP, "=/proc/%s", myProg);
sprintf(manBuff, "/proc/%d", getpid());
XrdOucUtils::ReLink(envBuff, manBuff);
#endif
// Create environment string
//
envLen = snprintf(envBuff, sizeof(envBuff), "pid=%d&host=%s&inst=%s&ver=%s"
"&cfgfn=%s&cwd=%s&apath=%s&logfn=%s\n",
static_cast<int>(getpid()), ProtInfo.myName,
ProtInfo.myInst, XrdVSTRING,
(getenv("XRDCONFIGFN") ? getenv("XRDCONFIGFN") : ""),
pwdBuff, ProtInfo.AdmPath, Log.logger()->xlogFN());
// Find out where we should write this
//
if (pidfn && (Slash = rindex(pidfn, '/')))
{strncpy(manBuff, pidfn, Slash-pidfn); pidP = manBuff+(Slash-pidfn);}
else {strcpy(manBuff, "/tmp"); pidP = manBuff+4;}
// Construct the pid file name for ourselves
//
snprintf(pidP, sizeof(manBuff)-(pidP-manBuff), "/%s.%s.env",
ProtInfo.myProg, ProtInfo.myInst);
// Open the file
//
if ((envFD = open(manBuff, O_WRONLY|O_CREAT|O_TRUNC, 0664)) < 0)
{Log.Emsg("Config", errno, "create envfile", manBuff);
return;
}
// Write out environmental information
//
if (write(envFD, envBuff, envLen) < 0)
Log.Emsg("Config", errno, "write to envfile", manBuff);
// All done
//
close(envFD);
}
/******************************************************************************/
/* s e t C F G */
/******************************************************************************/
void XrdConfig::setCFG()
{
char cwdBuff[1024], *cfnP = cwdBuff;
int n;
// If the config file is absolute, export it as is
//
if (*ConfigFN == '/')
{XrdOucEnv::Export("XRDCONFIGFN", ConfigFN);
return;
}
// Prefix current working directory to the config file
//
if (!getcwd(cwdBuff, sizeof(cwdBuff)-strlen(ConfigFN)-2)) cfnP = ConfigFN;
else {n = strlen(cwdBuff);
if (cwdBuff[n-1] != '/') cwdBuff[n++] = '/';
strcpy(cwdBuff+n, ConfigFN);
}
// Export result
//
XrdOucEnv::Export("XRDCONFIGFN", cfnP);
}
/******************************************************************************/
/* s e t F D L */
/******************************************************************************/
int XrdConfig::setFDL()
{
static const int maxFD = 1048576;
struct rlimit rlim;
char buff[100];
// Get the resource limit
//
if (getrlimit(RLIMIT_NOFILE, &rlim) < 0)
return Log.Emsg("Config", errno, "get FD limit");
// Set the limit to the maximum allowed
//
if (rlim.rlim_max == RLIM_INFINITY) rlim.rlim_max = maxFD;
rlim.rlim_cur = rlim.rlim_max;
#if (defined(__APPLE__) && defined(MAC_OS_X_VERSION_10_5))
if (rlim.rlim_cur > OPEN_MAX) rlim.rlim_max = rlim.rlim_cur = OPEN_MAX;
#endif
if (setrlimit(RLIMIT_NOFILE, &rlim) < 0)
return Log.Emsg("Config", errno,"set FD limit");
// Obtain the actual limit now
//
if (getrlimit(RLIMIT_NOFILE, &rlim) < 0)
return Log.Emsg("Config", errno, "get FD limit");
// Establish operating limit
//
ProtInfo.ConnMax = rlim.rlim_cur;
sprintf(buff, "%d", ProtInfo.ConnMax);
Log.Say("Config maximum number of connections restricted to ", buff);
// Set core limit and but Solaris
//
#if !defined( __solaris__ ) && defined(RLIMIT_CORE)
if (coreV >= 0)
{if (getrlimit(RLIMIT_CORE, &rlim) < 0)
Log.Emsg("Config", errno, "get core limit");
else {rlim.rlim_cur = (coreV ? rlim.rlim_max : 0);
if (setrlimit(RLIMIT_CORE, &rlim) < 0)
Log.Emsg("Config", errno,"set core limit");
}
}
#endif
// The scheduler will have already set the thread limit. We just report it
//
#if defined(__linux__) && defined(RLIMIT_NPROC)
// Obtain the actual limit now (Scheduler construction sets this to rlim_max)
//
if (getrlimit(RLIMIT_NPROC, &rlim) < 0)
return Log.Emsg("Config", errno, "get thread limit");
// Establish operating limit
//
int nthr = static_cast<int>(rlim.rlim_cur);
if (nthr < 8192 || ProtInfo.DebugON)
{sprintf(buff, "%d", static_cast<int>(rlim.rlim_cur));
Log.Say("Config maximum number of threads restricted to ", buff);
}
#endif
return 0;
}
/******************************************************************************/
/* S e t u p */
/******************************************************************************/
int XrdConfig::Setup(char *dfltp)
{
XrdInet *NetWAN;
XrdConfigProt *cp;
int i, wsz, arbNet;
// Establish the FD limit
//
if (setFDL()) return 1;
// Special handling for Linux sendfile()
//
#if defined(__linux__) && defined(TCP_CORK)
{ int sokFD, setON = 1;
if ((sokFD = socket(PF_INET, SOCK_STREAM, 0)) >= 0)
{setsockopt(sokFD, XrdNetUtils::ProtoID("tcp"), TCP_NODELAY,
&setON, sizeof(setON));
if (setsockopt(sokFD, SOL_TCP, TCP_CORK, &setON, sizeof(setON)) < 0)
XrdLink::sfOK = 0;
close(sokFD);
}
}
#endif
// Indicate how sendfile is being handled
//
TRACE(NET,"sendfile " <<(XrdLink::sfOK ? "enabled." : "disabled!"));
// Initialize the buffer manager
//
BuffPool.Init();
// Start the scheduler
//
Sched.Start();
// Setup the link and socket polling infrastructure
//
XrdLink::Init(&Log, &Trace, &Sched);
XrdPoll::Init(&Log, &Trace, &Sched);
if (!XrdLink::Setup(ProtInfo.ConnMax, ProtInfo.idleWait)
|| !XrdPoll::Setup(ProtInfo.ConnMax)) return 1;
// Modify the AdminPath to account for any instance name. Note that there is
// a negligible memory leak under ceratin path combinations. Not enough to
// warrant a lot of logic to get around.
//
if (myInsName) ProtInfo.AdmPath = XrdOucUtils::genPath(AdminPath,myInsName);
else ProtInfo.AdmPath = AdminPath;
XrdOucEnv::Export("XRDADMINPATH", ProtInfo.AdmPath);
AdminPath = XrdOucUtils::genPath(AdminPath, myInsName, ".xrd");
// Setup admin connection now
//
if (ASocket(AdminPath, "admin", (mode_t)AdminMode)) return 1;
// Determine the default port number (only for xrootd) if not specified.
//
if (PortTCP < 0)
{if ((PortTCP = XrdNetUtils::ServPort(dfltp))) PortUDP = PortTCP;
else PortTCP = -1;
}
// We now go through all of the protocols and get each respective port number.
//
XrdProtLoad::Init(&Log, &Trace); cp = Firstcp;
while(cp)
{ProtInfo.Port = (cp->port < 0 ? PortTCP : cp->port);
XrdOucEnv::Export("XRDPORT", ProtInfo.Port);
if ((cp->port = XrdProtLoad::Port(cp->libpath, cp->proname,
cp->parms, &ProtInfo)) < 0) return 1;
cp = cp->Next;
}
// Allocate the statistics object. This is akward since we only know part
// of the current configuration. The object will figure this out later.
//
ProtInfo.Stats = new XrdStats(&Log, &Sched, &BuffPool,
ProtInfo.myName, Firstcp->port,
ProtInfo.myInst, ProtInfo.myProg, mySitName);
// Allocate a WAN port number of we need to
//
if (PortWAN && (NetWAN = new XrdInet(&Log, &Trace, Police)))
{if (Wan_Opts || Wan_Blen) NetWAN->setDefaults(Wan_Opts, Wan_Blen);
if (myDomain) NetWAN->setDomain(myDomain);
if (NetWAN->Bind((PortWAN > 0 ? PortWAN : 0), "tcp")) return 1;
PortWAN = NetWAN->Port();
wsz = NetWAN->WSize();
Wan_Blen = (wsz < Wan_Blen || !Wan_Blen ? wsz : Wan_Blen);
TRACE(NET,"WAN port " <<PortWAN <<" wsz=" <<Wan_Blen <<" (" <<wsz <<')');
NetTCP[XrdProtLoad::ProtoMax] = NetWAN;
} else {PortWAN = 0; Wan_Blen = 0;}