XrdAppsCconfig.cc

Go to the documentation of this file.
00001 /******************************************************************************/
00002 /*                                                                            */
00003 /*                     X r d A p p s C c o n f i g . c c                      */
00004 /*                                                                            */
00005 /* (c) 2010 by the Board of Trustees of the Leland Stanford, Jr., University  */
00006 /*                            All Rights Reserved                             */
00007 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
00008 /*              DE-AC02-76-SFO0515 with the Department of Energy              */
00009 /******************************************************************************/
00010 
00011 #include <fcntl.h>
00012 #include <stdio.h>
00013 #include <unistd.h>
00014 #include <sys/types.h>
00015 #include <sys/socket.h>
00016 #include <sys/stat.h>
00017 #include <sys/uio.h>
00018 
00019 #include "XrdNet/XrdNetDNS.hh"
00020 #include "XrdOuc/XrdOucEnv.hh"
00021 #include "XrdOuc/XrdOucNList.hh"
00022 #include "XrdOuc/XrdOucStream.hh"
00023 #include "XrdOuc/XrdOucUtils.hh"
00024 #include "XrdSys/XrdSysError.hh"
00025 #include "XrdSys/XrdSysLogger.hh"
00026 #include "XrdSys/XrdSysHeaders.hh"
00027 #include "XrdSys/XrdSysPthread.hh"
00028 
00029 /******************************************************************************/
00030 /*                                i n L i s t                                 */
00031 /******************************************************************************/
00032   
00033 int inList(const char *var, const char **Vec)
00034 {
00035    int i = 0;
00036    while(Vec[i] && strcmp(Vec[i],var)) i++;
00037    return (Vec[i] != 0);
00038 }
00039 
00040 /******************************************************************************/
00041 /*                                 U s a g e                                  */
00042 /******************************************************************************/
00043   
00044 void Usage(int rc)
00045 {
00046    cerr <<"\n Usage: cconfig -c <cfn> [-h <host>] [-n <name>] [-x <prog>] [<args>]"
00047           "\n<args>: [[pfx]*]<directive> | <directive>[*[sfx]] [<args>]" <<endl;
00048    exit(rc);
00049 }
00050 
00051 /******************************************************************************/
00052 /*                                  m a i n                                   */
00053 /******************************************************************************/
00054   
00055 int main(int argc, char *argv[])
00056 {
00057    static const char *Pgm = "cconfig: ";
00058    extern char *optarg;
00059    extern int opterr, optind, optopt;
00060 
00061    XrdSysLogger       Logger;
00062    XrdSysError        Say(&Logger, "cconfig");
00063    XrdOucNList_Anchor DirQ;
00064    XrdOucEnv          myEnv, *oldEnv = 0;
00065    XrdOucStream      *Config;
00066 
00067    const char *Cfn = 0, *Host = 0, *Name = 0, *Xeq = "xrootd";
00068    const char *noSub[] = {"cms.prepmsg", "ofs.notifymsg", "oss.stagemsg",
00069                           "frm.xfr.copycmd", 0};
00070    const char *ifChk[] = {"xrd.port", "all.role", "all.manager", 0};
00071    const char *slChk[] = {"frm.xfr.copycmd", 0};
00072 
00073    char buff[4096], *var, c;
00074    int i, retc = 0, cfgFD, chkQ = 0;
00075 
00076 // Process the options
00077 //
00078    opterr = 0;
00079    if (argc > 1 && '-' == *argv[1]) 
00080       while ((c = getopt(argc,argv,":c:h:n:x:")) && ((unsigned char)c != 0xff))
00081      { switch(c)
00082        {
00083        case 'c': Cfn = optarg;
00084                  break;
00085        case 'h': Host= optarg;
00086                  break;
00087        case 'n': Name= optarg;
00088                  break;
00089        case 'x': Xeq = optarg;
00090                  break;
00091        default:  sprintf(buff,"'%c'", optopt);
00092                  if (c == ':') Say.Say(Pgm, buff, " value not specified.");
00093                     else Say.Say(Pgm, buff, " option is invalid.");
00094                  Usage(1);
00095        }
00096      }
00097 
00098 // Make sure config file has been specified
00099 //
00100    if (!Cfn) {Say.Say(Pgm, "Config file not specified."); Usage(1);}
00101 
00102 // Get full host name
00103 //
00104    if (!Host) Host = XrdNetDNS::getHostName();
00105       else {sockaddr IPAddr;
00106             Host = (XrdNetDNS::getHostAddr(Host, &IPAddr)
00107                  ?  XrdNetDNS::getHostName(IPAddr) : 0);
00108            }
00109    if (!Host) {Say.Say(Pgm, "Unable to determine host name."); exit(3);}
00110 
00111 // Prepare all selector arguments
00112 //
00113    for (i = optind; i < argc; i++) DirQ.Replace(argv[i],0);
00114    chkQ = (DirQ.First() != 0);
00115 
00116 // Open the config file
00117 //
00118    if ( (cfgFD = open(Cfn, O_RDONLY, 0)) < 0)
00119       {Say.Say(Pgm, strerror(errno), " opening config file ", Cfn);
00120        exit(4);
00121       }
00122 
00123 // Construct instance name and stream
00124 //
00125    Name = XrdOucUtils::InstName(Name);
00126    sprintf(buff,"%s %s@%s", Xeq, Name, Host);
00127    Config = new XrdOucStream(&Say, strdup(buff), &myEnv, "");
00128    Config->Attach(cfgFD);
00129 
00130 // Now start reading records until eof.
00131 //
00132    while((var = Config->GetMyFirstWord()))
00133         {if (chkQ && !DirQ.Find(var)) {Config->noEcho(); continue;}
00134               if (inList(var, noSub))
00135                  {if (inList(var, slChk))
00136                      while((var = Config->GetWord()) && *var != '/');
00137                   oldEnv = Config->SetEnv(0);
00138                   if (var) Config->GetRest(buff, sizeof(buff));
00139                   Config->SetEnv(oldEnv);
00140                  }
00141          else if (inList(var, ifChk))
00142                  {while((var = Config->GetWord()) && strcmp(var, "if"));
00143                   if (var && !XrdOucUtils::doIf(&Say, *Config, "directive",
00144                                                 Host, Name, Xeq))
00145                      {Config->noEcho(); continue;}
00146                  }
00147          else Config->GetRest(buff, sizeof(buff));
00148          Config->Echo();
00149         }
00150 
00151 // Now check if any errors occured during file i/o
00152 //
00153    if ((retc = Config->LastError()))
00154       {Say.Say(Pgm, strerror(retc), " reading config file ", Cfn); retc = 8;}
00155    Config->Close();
00156 
00157 // Should never get here
00158 //
00159    exit(retc);
00160 }

Generated on Tue Jul 5 14:46:16 2011 for ROOT_528-00b_version by  doxygen 1.5.1