XRootD
Loading...
Searching...
No Matches
XrdAppsCconfig.cc
Go to the documentation of this file.
1/******************************************************************************/
2/* */
3/* X r d A p p s C c o n f i g . c c */
4/* */
5/* (c) 2010 by the Board of Trustees of the Leland Stanford, Jr., University */
6/* All Rights Reserved */
7/* Produced by Andrew Hanushevsky for Stanford University under contract */
8/* DE-AC02-76-SFO0515 with the Department of Energy */
9/* */
10/* This file is part of the XRootD software suite. */
11/* */
12/* XRootD is free software: you can redistribute it and/or modify it under */
13/* the terms of the GNU Lesser General Public License as published by the */
14/* Free Software Foundation, either version 3 of the License, or (at your */
15/* option) any later version. */
16/* */
17/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
18/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
19/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
20/* License for more details. */
21/* */
22/* You should have received a copy of the GNU Lesser General Public License */
23/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
24/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
25/* */
26/* The copyright holder's institutional names and contributor's names may not */
27/* be used to endorse or promote products derived from this software without */
28/* specific prior written permission of the institution or contributor. */
29/******************************************************************************/
30
31#include <fcntl.h>
32#include <cstdio>
33#include <unistd.h>
34#include <sys/types.h>
35#include <sys/stat.h>
36#include <sys/uio.h>
37
38#include "XrdNet/XrdNetAddr.hh"
39#include "XrdOuc/XrdOucEnv.hh"
40#include "XrdOuc/XrdOucNList.hh"
42#include "XrdOuc/XrdOucUtils.hh"
43#include "XrdSys/XrdSysE2T.hh"
44#include "XrdSys/XrdSysError.hh"
48
49/******************************************************************************/
50/* i n L i s t */
51/******************************************************************************/
52
53int inList(const char *var, const char **Vec)
54{
55 int i = 0;
56 while(Vec[i] && strcmp(Vec[i],var)) i++;
57 return (Vec[i] != 0);
58}
59
60/******************************************************************************/
61/* U s a g e */
62/******************************************************************************/
63
64void Usage(int rc)
65{
66 std::cerr <<"\n Usage: cconfig -c <cfn> [-h <host>] [-n <name>] [-x <prog>] [<args>]"
67 "\n<args>: [[pfx]*]<directive> | <directive>[*[sfx]] [<args>]" <<std::endl;
68 exit(rc);
69}
70
71/******************************************************************************/
72/* m a i n */
73/******************************************************************************/
74
75int main(int argc, char *argv[])
76{
77 static const char *Pgm = "cconfig: ";
78 extern char *optarg;
79 extern int opterr, optind, optopt;
80
82 XrdSysError Say(&Logger, "cconfig");
84 XrdOucEnv myEnv, *oldEnv = 0;
85 XrdOucStream *Config;
86 XrdNetAddr theAddr(0);
87
88 const char *Cfn = 0, *Host = 0, *Name = 0, *Xeq = "xrootd";
89 const char *noSub[] = {"cms.prepmsg", "ofs.notifymsg", "oss.stagemsg",
90 "frm.xfr.copycmd", 0};
91 const char *ifChk[] = {"xrd.port", "all.role", "all.manager", 0};
92 const char *slChk[] = {"frm.xfr.copycmd", 0};
93
94 char buff[4096], *var, c;
95 int i, retc = 0, cfgFD, chkQ = 0;
96
97// Process the options
98//
99 opterr = 0;
100 if (argc > 1 && '-' == *argv[1])
101 while ((c = getopt(argc,argv,":c:h:n:x:")) && ((unsigned char)c != 0xff))
102 { switch(c)
103 {
104 case 'c': Cfn = optarg;
105 break;
106 case 'h': Host= optarg;
107 break;
108 case 'n': Name= optarg;
109 break;
110 case 'x': Xeq = optarg;
111 break;
112 default: sprintf(buff,"'%c'", optopt);
113 if (c == ':') Say.Say(Pgm, buff, " value not specified.");
114 else Say.Say(Pgm, buff, " option is invalid.");
115 Usage(1);
116 }
117 }
118
119// Make sure config file has been specified
120//
121 if (!Cfn) {Say.Say(Pgm, "Config file not specified."); Usage(1);}
122
123// Get full host name
124//
125 if (!Host) Host = theAddr.Name();
126 else if (!theAddr.Set(Host,0)) Host = theAddr.Name();
127 if (!Host) {Say.Say(Pgm, "Unable to determine host name."); exit(3);}
128
129// Prepare all selector arguments
130//
131 for (i = optind; i < argc; i++) DirQ.Replace(argv[i],0);
132 chkQ = (DirQ.First() != 0);
133
134// Open the config file
135//
136 if ( (cfgFD = open(Cfn, O_RDONLY, 0)) < 0)
137 {Say.Say(Pgm, XrdSysE2T(errno), " opening config file ", Cfn);
138 exit(4);
139 }
140
141// Construct instance name and stream
142//
143 Name = XrdOucUtils::InstName(Name);
144 sprintf(buff,"%s %s@%s", Xeq, Name, Host);
145 Config = new XrdOucStream(&Say, strdup(buff), &myEnv, "");
146 Config->Attach(cfgFD);
147
148// Now start reading records until eof.
149//
150 while((var = Config->GetMyFirstWord()))
151 {if (chkQ && !DirQ.Find(var)) {Config->noEcho(); continue;}
152 if (inList(var, noSub))
153 {if (inList(var, slChk))
154 while((var = Config->GetWord()) && *var != '/') {}
155 oldEnv = Config->SetEnv(0);
156 if (var) Config->GetRest(buff, sizeof(buff));
157 Config->SetEnv(oldEnv);
158 }
159 else if (inList(var, ifChk))
160 {while((var = Config->GetWord()) && strcmp(var, "if")) {}
161 if (var && !XrdOucUtils::doIf(&Say, *Config, "directive",
162 Host, Name, Xeq))
163 {Config->noEcho(); continue;}
164 }
165 else Config->GetRest(buff, sizeof(buff));
166 Config->Echo();
167 }
168
169// Now check if any errors occurred during file i/o
170//
171 if ((retc = Config->LastError()))
172 {Say.Say(Pgm, XrdSysE2T(retc), " reading config file ", Cfn); retc = 8;}
173 Config->Close();
174
175// Should never get here
176//
177 exit(retc);
178}
int main(int argc, char *argv[])
int inList(const char *var, const char **Vec)
void Usage(int rc)
XrdOucPup XrdCmsParser::Pup & Say
static XrdSysLogger Logger
int optopt
int optind
#define open
Definition XrdPosix.hh:71
const char * XrdSysE2T(int errcode)
Definition XrdSysE2T.cc:99
const char * Name(const char *eName=0, const char **eText=0)
const char * Set(const char *hSpec, int pNum=PortInSpec)
XrdOucNList * First()
void Replace(const char *name, int nval)
XrdOucNList * Find(const char *name)
static const char * InstName(int TranOpt=0)
static int doIf(XrdSysError *eDest, XrdOucStream &Config, const char *what, const char *hname, const char *nname, const char *pname)