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
|
/*
* Copyright (C) 2007 Tommi Maekitalo
*
* This library 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 2.1 of the License, or (at your option) any later version.
*
* As a special exception, you may use this file as part of a free
* software library without restriction. Specifically, if other files
* instantiate templates or use macros or inline functions from this
* file, or you compile this file and link it with other files to
* produce an executable, this file does not by itself cause the
* resulting executable to be covered by the GNU General Public
* License. This exception does not however invalidate any other
* reasons why the executable file might be covered by the GNU Library
* General Public License.
*
* This library 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 this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "tnt/process.h"
#include "tnt/tntnet.h"
#include "tnt/cmd.h"
#include "tnt/tntconfig.h"
#include <cxxtools/log.h>
#include <cxxtools/arg.h>
#include <cxxtools/xml/xmldeserializer.h>
#include <cxxtools/jsondeserializer.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <set>
#include <glob.h>
#include <unistd.h>
#include <sys/types.h>
#include <sstream>
#include "config.h"
#ifndef TNTNET_CONF
# define TNTNET_CONF "/etc/tntnet.xml"
#endif
#ifndef TNTNET_PID
# define TNTNET_PID "/var/run/tntnet.pid"
#endif
log_define("tntnet.main")
namespace tnt
{
namespace
{
class Glob
{
private:
glob_t _gl;
unsigned _n;
// non-copyable
// TODO [tntnet-3.0]: use C++11 " = delete" syntax
Glob(const Glob&);
Glob& operator= (const Glob&);
public:
explicit Glob(const std::string& pattern, int flags = 0);
~Glob();
const char* current() const
{ return _gl.gl_pathv ? _gl.gl_pathv[_n] : 0; }
const char* next()
{
if (_gl.gl_pathv && _gl.gl_pathv[_n])
++_n;
return current();
}
};
Glob::Glob(const std::string& pattern, int flags)
: _n(0)
{
int ret = ::glob(pattern.c_str(), flags, 0, &_gl);
if (ret == GLOB_NOMATCH)
{
_gl.gl_pathv = 0;
}
else if (ret != 0)
{
std::ostringstream msg;
msg << "failed to process glob pattern <" << pattern << "> errorcode " << ret;
throw std::runtime_error(msg.str());
}
}
Glob::~Glob()
{ globfree(&_gl); }
template <typename Deserializer>
void processConfigFile(const std::string& configFile, std::set<std::string>& filesProcessed)
{
TntConfig& config = TntConfig::it();
std::ifstream in(configFile.c_str());
if (!in)
throw std::runtime_error("failed to open configuration file \"" + configFile + '"');
Deserializer deserializer(in);
deserializer.deserialize(config);
in.close();
filesProcessed.insert(configFile);
for (std::vector<std::string>::size_type n = 0; n < config.includes.size(); ++n)
{
for (Glob glob(config.includes[n]); glob.current(); glob.next())
{
std::string configFile = glob.current();
if (filesProcessed.find(configFile) == filesProcessed.end())
processConfigFile<Deserializer>(glob.current(), filesProcessed);
}
}
}
#ifdef CXXTOOLS_XMLDESERIALISER_ATTR
class XmlDeserializer : public cxxtools::xml::XmlDeserializer
{
public:
XmlDeserializer(std::istream& is)
: cxxtools::xml::XmlDeserializer(is, true)
{ }
};
#endif
} // namespace
class TntnetProcess : public Process
{
private:
tnt::Tntnet _tntnet;
bool _logall;
void initializeLogging();
protected:
virtual void onInit();
virtual void doWork();
virtual void doShutdown();
public:
TntnetProcess(int& argc, char* argv[]);
};
TntnetProcess::TntnetProcess(int& argc, char* argv[])
: _logall(cxxtools::Arg<bool>(argc, argv, "--logall"))
{
std::string configFile;
cxxtools::Arg<bool> jsonConfig(argc, argv, 'j');
// check for argument -c
cxxtools::Arg<const char*> conf(argc, argv, 'c');
if (conf.isSet())
configFile = conf;
else
{
// read 1st parameter from argument-list
cxxtools::Arg<const char*> conf(argc, argv);
if (conf.isSet())
configFile = conf;
else
{
// check environment-variable TNTNET_CONF
const char* tntnetConf = ::getenv("TNTNET_CONF");
if (tntnetConf)
configFile = tntnetConf;
else if (getuid() != 0)
configFile = "tntnet.xml";
else
configFile = TNTNET_CONF; // take default
}
}
std::set<std::string> filesProcessed;
if (jsonConfig)
processConfigFile<cxxtools::JsonDeserializer>(configFile, filesProcessed);
else
#ifdef CXXTOOLS_XMLDESERIALISER_ATTR
processConfigFile<XmlDeserializer>(configFile, filesProcessed);
#else
processConfigFile<cxxtools::xml::XmlDeserializer>(configFile, filesProcessed);
#endif
if (_logall)
initializeLogging();
}
void TntnetProcess::initializeLogging()
{
log_init(TntConfig::it().logConfiguration);
}
void TntnetProcess::onInit()
{ _tntnet.init(TntConfig::it()); }
void TntnetProcess::doWork()
{ _tntnet.run(); }
void TntnetProcess::doShutdown()
{ tnt::Tntnet::shutdown(); }
}
int main(int argc, char* argv[])
{
std::ios::sync_with_stdio(false);
try
{
cxxtools::Arg<bool> version(argc, argv, 'V');
cxxtools::Arg<bool> versionLong(argc, argv, "--version");
if (version || versionLong)
{
std::cout << PACKAGE_STRING << std::endl;
return 0;
}
cxxtools::Arg<bool> help(argc, argv, 'h');
cxxtools::Arg<bool> helpLong(argc, argv, "--help");
if (help || helpLong)
{
// TODO: Add short explanation of available options
std::cout << "usage: " << argv[0] << " [options] [config-file (default: " TNTNET_CONF ")]\n"
"more info with \"man 8 tntnet\"" << std::endl;
return 0;
}
cxxtools::Arg<bool> cmd(argc, argv, 'C');
if (cmd)
{
log_init("tntnet.xml");
tnt::Cmd cmdapp(std::cout);
tnt::QueryParams queryParams;
while (true)
{
cxxtools::Arg<std::string> param(argc, argv, 'q');
if (!param.isSet())
break;
queryParams.parse_url(param);
}
for (int a = 1; a < argc; ++a)
{
log_info("calling component <" << argv[a] << '>');
cmdapp.call(tnt::Compident(argv[a]), queryParams);
}
}
else
{
tnt::TntnetProcess app(argc, argv);
app.run();
}
}
catch(const std::exception& e)
{
log_fatal(e.what());
std::cerr << e.what() << std::endl;
return -1;
}
}
|