Files
@ a48153883675
Branch filter:
Location: libtransport.git/spectrum/src/main.cpp
a48153883675
5.7 KiB
text/x-c++hdr
Config file tweaks
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 | #include "transport/config.h"
#include "transport/transport.h"
#include "transport/usermanager.h"
#include "transport/logger.h"
#include "transport/sqlite3backend.h"
#include "transport/mysqlbackend.h"
#include "transport/userregistration.h"
#include "transport/networkpluginserver.h"
#include "transport/admininterface.h"
#include "Swiften/EventLoop/SimpleEventLoop.h"
#ifndef WIN32
#include "sys/signal.h"
#else
#include <Windows.h>
#include <tchar.h>
#endif
#include "log4cxx/logger.h"
#include "log4cxx/patternlayout.h"
#include "log4cxx/propertyconfigurator.h"
#include "log4cxx/consoleappender.h"
using namespace log4cxx;
using namespace Transport;
Swift::SimpleEventLoop *eventLoop_ = NULL;
static void spectrum_sigint_handler(int sig) {
eventLoop_->stop();
}
static void spectrum_sigterm_handler(int sig) {
eventLoop_->stop();
}
#ifndef WIN32
static void daemonize(const char *cwd, const char *lock_file) {
pid_t pid, sid;
FILE* lock_file_f;
char process_pid[20];
/* already a daemon */
if ( getppid() == 1 ) return;
/* Fork off the parent process */
pid = fork();
if (pid < 0) {
exit(1);
}
/* If we got a good PID, then we can exit the parent process. */
if (pid > 0) {
exit(0);
}
/* At this point we are executing as the child process */
/* Change the file mode mask */
umask(0);
/* Create a new SID for the child process */
sid = setsid();
if (sid < 0) {
exit(1);
}
/* Change the current working directory. This prevents the current
directory from being locked; hence not being able to remove it. */
if ((chdir(cwd)) < 0) {
exit(1);
}
if (lock_file) {
/* write our pid into it & close the file. */
lock_file_f = fopen(lock_file, "w+");
if (lock_file_f == NULL) {
std::cout << "EE cannot create lock file " << lock_file << ". Exiting\n";
exit(1);
}
sprintf(process_pid,"%d\n",getpid());
if (fwrite(process_pid,1,strlen(process_pid),lock_file_f) < strlen(process_pid)) {
std::cout << "EE cannot write to lock file " << lock_file << ". Exiting\n";
exit(1);
}
fclose(lock_file_f);
}
if (freopen( "/dev/null", "r", stdin) == NULL) {
std::cout << "EE cannot open /dev/null. Exiting\n";
exit(1);
}
}
#endif
int main(int argc, char **argv)
{
Config config;
boost::program_options::variables_map vm;
bool no_daemon = false;
std::string config_file;
#ifndef WIN32
if (signal(SIGINT, spectrum_sigint_handler) == SIG_ERR) {
std::cout << "SIGINT handler can't be set\n";
return -1;
}
if (signal(SIGTERM, spectrum_sigterm_handler) == SIG_ERR) {
std::cout << "SIGTERM handler can't be set\n";
return -1;
}
#endif
boost::program_options::options_description desc("Usage: spectrum [OPTIONS] <config_file.cfg>\nAllowed options");
desc.add_options()
("help,h", "help")
("no-daemonize,n", "Do not run spectrum as daemon")
("config", boost::program_options::value<std::string>(&config_file)->default_value(""), "Config file")
;
try
{
boost::program_options::positional_options_description p;
p.add("config", -1);
boost::program_options::store(boost::program_options::command_line_parser(argc, argv).
options(desc).positional(p).run(), vm);
boost::program_options::notify(vm);
if(vm.count("help"))
{
std::cout << desc << "\n";
return 1;
}
if(vm.count("config") == 0) {
std::cout << desc << "\n";
return 1;
}
if(vm.count("no-daemonize")) {
no_daemon = true;
}
}
catch (std::runtime_error& e)
{
std::cout << desc << "\n";
return 1;
}
catch (...)
{
std::cout << desc << "\n";
return 1;
}
if (!config.load(vm["config"].as<std::string>())) {
std::cerr << "Can't load configuration file.\n";
return 1;
}
#ifndef WIN32
if (!no_daemon) {
daemonize("/", NULL);
}
#endif
if (CONFIG_STRING(&config, "logging.config").empty()) {
LoggerPtr root = log4cxx::Logger::getRootLogger();
#ifdef WIN32
root->addAppender(new ConsoleAppender(new PatternLayout(L"%d %-5p %c: %m%n")));
#else
root->addAppender(new ConsoleAppender(new PatternLayout("%d %-5p %c: %m%n")));
#endif
}
else {
log4cxx::PropertyConfigurator::configure(CONFIG_STRING(&config, "logging.config"));
}
Swift::SimpleEventLoop eventLoop;
Swift::BoostNetworkFactories *factories = new Swift::BoostNetworkFactories(&eventLoop);
UserRegistry userRegistry(&config, factories);
Component transport(&eventLoop, factories, &config, NULL, &userRegistry);
// Logger logger(&transport);
StorageBackend *storageBackend = NULL;
#ifdef WITH_SQLITE
if (CONFIG_STRING(&config, "database.type") == "sqlite3") {
storageBackend = new SQLite3Backend(&config);
if (!storageBackend->connect()) {
std::cerr << "Can't connect to database.\n";
return -1;
}
}
#endif
#ifdef WITH_MYSQL
if (CONFIG_STRING(&config, "database.type") == "mysql") {
storageBackend = new MySQLBackend(&config);
if (!storageBackend->connect()) {
std::cerr << "Can't connect to database.\n";
return -1;
}
}
#endif
UserManager userManager(&transport, &userRegistry, storageBackend);
UserRegistration *userRegistration = NULL;
if (storageBackend) {
userRegistration = new UserRegistration(&transport, &userManager, storageBackend);
userRegistration->start();
// logger.setUserRegistration(&userRegistration);
}
// logger.setUserManager(&userManager);
NetworkPluginServer plugin(&transport, &config, &userManager);
AdminInterface adminInterface(&transport, &userManager, &plugin, storageBackend);
eventLoop_ = &eventLoop;
eventLoop.run();
if (userRegistration) {
userRegistration->stop();
delete userRegistration;
}
delete storageBackend;
delete factories;
}
|