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 <errno.h>
34#include <unistd.h>
35#include <sys/types.h>
36#include <sys/stat.h>
37#include <sys/uio.h>
38
39#include "XrdNet/XrdNetAddr.hh"
40#include "XrdOuc/XrdOucEnv.hh"
41#include "XrdOuc/XrdOucNList.hh"
44#include "XrdOuc/XrdOucUtils.hh"
45#include "XrdSys/XrdSysE2T.hh"
46#include "XrdSys/XrdSysError.hh"
50
51/******************************************************************************/
52/* G l o b a l O b j e c t s */
53/******************************************************************************/
54
55namespace
56{
57 const char *Pgm = "cconfig: ";
58
60 XrdSysError Say(&Logger, "cconfig");
61}
62
63/******************************************************************************/
64/* i n L i s t */
65/******************************************************************************/
66
67int inList(const char *var, const char **Vec)
68{
69 int i = 0;
70 while(Vec[i] && strcmp(Vec[i],var)) i++;
71 return (Vec[i] != 0);
72}
73
74/******************************************************************************/
75/* c f O u t */
76/******************************************************************************/
77
78int cfOut(const char* outFN, XrdOucString& cFile)
79{
80
81// Open the output file
82//
83 int mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
84 int fd = open(outFN, O_CREAT | O_TRUNC | O_WRONLY, mode);
85 if (fd < 0)
86 {Say.Say(Pgm, XrdSysE2T(errno), " opening output file ", outFN);
87 return 8;
88 }
89
90// Write out the config file
91//
92 if (write(fd, cFile.c_str(), cFile.length()) != cFile.length())
93 {Say.Say(Pgm, XrdSysE2T(errno), " writing output file ", outFN);
94 return 1;
95 }
96
97// All done
98//
99 close(fd);
100 return 0;
101}
102
103/******************************************************************************/
104/* U s a g e */
105/******************************************************************************/
106
107void Usage(int rc)
108{
109 std::cerr <<"Usage: cconfig -c <cfn> [<opts>] [<args>]\n"
110 "<opts>: [-h <host>] [-n <name>] [-o <file>] [-x <prog>]\n"
111 "<args>: [[pfx]*]<directive> | <directive>[*[sfx]] [<args>]"
112 <<std::endl;
113 exit(rc);
114}
115
116/******************************************************************************/
117/* m a i n */
118/******************************************************************************/
119
120int main(int argc, char *argv[])
121{
122 extern char *optarg;
123 extern int opterr, optind, optopt;
124
126 XrdOucEnv myEnv, *oldEnv = 0;
127 XrdOucStream *Config;
128 XrdNetAddr theAddr(0);
129
130 const char *Cfn = 0, *Host = 0, *Name = 0, *Xeq = "xrootd", *oFile=0;
131 const char *noSub[] = {"cms.prepmsg", "ofs.notifymsg", "oss.stagemsg",
132 "frm.xfr.copycmd", 0};
133 const char *ifChk[] = {"xrd.port", "all.role", "all.manager", 0};
134 const char *slChk[] = {"frm.xfr.copycmd", 0};
135
136 char buff[4096], *var, c;
137 int i, retc = 0, cfgFD, chkQ = 0;
138
139// Process the options
140//
141 opterr = 0;
142 if (argc > 1 && '-' == *argv[1])
143 while ((c = getopt(argc,argv,":c:h:n:o:x:")) && ((unsigned char)c != 0xff))
144 { switch(c)
145 {
146 case 'c': Cfn = optarg;
147 break;
148 case 'h': Host= optarg;
149 break;
150 case 'n': Name= optarg;
151 break;
152 case 'o': oFile=optarg;
153 break;
154 case 'x': Xeq = optarg;
155 break;
156 default: sprintf(buff,"'%c'", optopt);
157 if (c == ':') Say.Say(Pgm, buff, " value not specified.");
158 else Say.Say(Pgm, buff, " option is invalid.");
159 Usage(1);
160 }
161 }
162
163// Make sure config file has been specified
164//
165 if (!Cfn) {Say.Say(Pgm, "Config file not specified."); Usage(1);}
166
167// Get full host name
168//
169 if (!Host) Host = theAddr.Name();
170 else if (!theAddr.Set(Host,0)) Host = theAddr.Name();
171 if (!Host) {Say.Say(Pgm, "Unable to determine host name."); exit(3);}
172
173// Prepare all selector arguments
174//
175 for (i = optind; i < argc; i++) DirQ.Replace(argv[i],0);
176 chkQ = (DirQ.First() != 0);
177
178// Open the config file
179//
180 if ( (cfgFD = open(Cfn, O_RDONLY, 0)) < 0)
181 {Say.Say(Pgm, XrdSysE2T(errno), " opening config file ", Cfn);
182 exit(4);
183 }
184
185// Construct instance name and stream
186//
187 Name = XrdOucUtils::InstName(Name);
188 sprintf(buff,"%s %s@%s", Xeq, Name, Host);
189 Config = new XrdOucStream(&Say, buff, &myEnv, "");
190 Config->Attach(cfgFD);
191
192// We will capture the resulting config file for later
193//
194 XrdOucString theConfig;
195 Config->Capture(&theConfig);
196
197// Now start reading records until eof.
198//
199 while((var = Config->GetMyFirstWord()))
200 {if (chkQ && !DirQ.Find(var)) {Config->noEcho(); continue;}
201 if (inList(var, noSub))
202 {if (inList(var, slChk))
203 while((var = Config->GetWord()) && *var != '/') {}
204 oldEnv = Config->SetEnv(0);
205 if (var) Config->GetRest(buff, sizeof(buff));
206 Config->SetEnv(oldEnv);
207 }
208 else if (inList(var, ifChk))
209 {while((var = Config->GetWord()) && strcmp(var, "if")) {}
210 if (var && !XrdOucUtils::doIf(&Say, *Config, "directive",
211 Host, Name, Xeq))
212 {Config->noEcho(); continue;}
213 }
214 else Config->GetRest(buff, sizeof(buff));
215 Config->Echo();
216 }
217
218// Now check if any errors occurred during file i/o
219//
220 if ((retc = Config->LastError()))
221 {Say.Say(Pgm, XrdSysE2T(retc), " reading config file ", Cfn); retc = 8;}
222 Config->Close();
223
224// Output the config file upon success if so wanted
225//
226 if (oFile && !retc) retc = cfOut(oFile, theConfig);
227
228 exit(retc);
229}
int main(int argc, char *argv[])
int inList(const char *var, const char **Vec)
void Usage(int rc)
int cfOut(const char *outFN, XrdOucString &cFile)
XrdOucPup XrdCmsParser::Pup & Say
static XrdSysLogger Logger
int optopt
int optind
#define close(a)
Definition XrdPosix.hh:48
#define write(a, b, c)
Definition XrdPosix.hh:115
#define open
Definition XrdPosix.hh:76
const char * XrdSysE2T(int errcode)
Definition XrdSysE2T.cc:104
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)
int length() const
const char * c_str() const
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)