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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
/***************************************************************************
* *
* Copyright (C) 2009-2010 Alexandr Tkachev <tka4ev@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "stdafx.h"
#include "dcpp/Util.h"
#include "dcpp/File.h"
#include "dcpp/Thread.h"
#include "utility.h"
#include "ServerManager.h"
#include "VersionGlobal.h"
#ifndef _WIN32
#include <signal.h>
#ifdef ENABLE_STACKTRACE
#include "extra/stacktrace.h"
#endif
#include <getopt.h>
#include <fstream>
#endif
char pidfile[256+1] = {0};
char config_dir[1024+1] = {0};
char local_dir[1024+1] = {0};
#ifndef _WIN32
static void SigHandler(int sig) {
string str = "Received signal ";
if (sig == SIGINT) {
str += "SIGINT";
} else if (sig == SIGTERM) {
str += "SIGTERM";
} else if (sig == SIGQUIT) {
str += "SIGQUIT";
} else if (sig == SIGHUP) {
ConfigReload();
return;
} else {
str += Util::toString(sig);
}
str += " ending...";
logging(bDaemon, bsyslog, true, str);
bIsClose = true;
ServerStop();
// restore to default...
struct sigaction sigact;
sigact.sa_handler = SIG_DFL;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
sigaction(sig, &sigact, nullptr);
}
// code of this function based on function tr_daemon from transmission daemon
#if defined(_WIN32)
#define USE_NO_DAEMON
#elif !defined(HAVE_DAEMON) || defined(__UCLIBC__)
#define USE_EIDCPP_DAEMON
#else
#define USE_OS_DAEMON
#endif
static int eidcpp_daemon( int nochdir, int noclose ) {
#if defined (USE_OS_DAEMON)
return daemon (nochdir, noclose);
#elif defined (USE_EIDCPP_DAEMON)
switch (fork()) {
case -1: return -1;
case 0: break;
default: _exit(0);
}
if( setsid( ) == -1 )
return -1;
if( !nochdir )
chdir( "/" );
if( !noclose ) {
int fd = open( "/dev/null", O_RDWR, 0 );
dup2( fd, STDIN_FILENO );
dup2( fd, STDOUT_FILENO );
dup2( fd, STDERR_FILENO );
close( fd );
}
return 0;
#else /* USE_NO_DAEMON */
return 0;
#endif
}
#endif
void printHelp() {
printf("Usage:\n"
" eiskaltdcpp-daemon -d [options]\n"
" eiskaltdcpp-daemon <Key>\n"
"EiskaltDC++ is a cross-platform program that uses the Direct Connect and ADC protocols.\n"
"\n"
"Keys:\n"
" -d, --daemon\t Run program as daemon\n"
" -h, --help\t Show this message\n"
" -V, --version\t Show version string\n"
#ifndef _WIN32
" -v, --verbose\t Verbose mode\n"
" -D, --debug\t Debug mode\n"
" -s, --syslog\t Use syslog in daemon mode\n"
" -S <file>, --log=<file>\t Write daemon log to <file> (default: @config_dir@/Logs/daemon.log)\n"
" -P <port>, --port=<port>\t Set port for XMLRPC or JSONRPC (default: 3121)\n"
" -L <ip>, --ip=<ip>\t\t Set IP address for XMLRPC or JSONRPC (default: 127.0.0.1)\n"
" -p <file>, --pidfile=<file>\t Write daemon process ID to <file>\n"
" -c <dir>, --confdir=<dir>\t Store config in <dir>\n"
" -l <dir>, --localdir=<dir>\t Store local data (cache, temp files) in <dir> (defaults is equal confdir)\n"
#ifdef XMLRPC_DAEMON
" -u <file>, --rpclog=<file>\t Write xmlrpc log to <file> (default: /tmp/eiskaltdcpp-daemon.xmlrpc.log)\n"
" -U <uripath>, --uripath=<uripath>\t Set UriPath for xmlrpc abyss server to <uripath> (default: /eiskaltdcpp)\n"
#endif
#endif // _WIN32
);
}
void printVersion() {
printf("%s\n", eiskaltdcppVersionString.c_str());
}
#ifndef _WIN32
static struct option opts[] = {
{ "help", no_argument, nullptr, 'h'},
{ "version", no_argument, nullptr, 'V'},
{ "daemon", no_argument, nullptr, 'd'},
{ "verbose", no_argument, nullptr, 'v'},
{ "debug", no_argument, nullptr, 'D'},
{ "confdir", required_argument, nullptr, 'c'},
{ "localdir",required_argument, nullptr, 'l'},
{ "pidfile", required_argument, nullptr, 'p'},
{ "port", required_argument, nullptr, 'P'},
{ "rpclog", required_argument, nullptr, 'u'},
{ "uripath", required_argument, nullptr, 'U'},
{ "ip", required_argument, nullptr, 'L'},
{ "syslog", required_argument, nullptr, 's'},
{ "log", required_argument, nullptr, 'S'},
{ nullptr, 0, nullptr, 0}
};
void writePidFile(char *path)
{
std::ofstream pidfile(path);
pidfile << getpid();
}
void parseArgs(int argc, char* argv[]) {
int ch;
while((ch = getopt_long(argc, argv, "hVdvDsp:c:l:P:L:S:u:U:", opts, nullptr)) != -1) {
switch (ch) {
case 'P':
lport = (unsigned short int) atoi (optarg);
break;
case 'L':
lip.assign(optarg,50);
break;
case 'S':
LOG_FILE.assign(optarg,1024);
break;
case 'u':
xmlrpcLog.assign(optarg,1024);
break;
case 'U':
xmlrpcUriPath.assign(optarg,1024);
break;
case 'v':
isVerbose = true;
break;
case 'D':
isDebug = true;
break;
case 'd':
bDaemon = true;
break;
case 's':
bsyslog = true;
break;
case 'p':
strncpy(pidfile, optarg, 256);
break;
case 'c':
strncpy(config_dir, optarg, 1024);
break;
case 'l':
strncpy(local_dir, optarg, 1024);
break;
case 'V':
printVersion();
exit(0);
case 'h':
printHelp();
exit(0);
default:
exit(0);
}
}
}
#else // _WIN32
void parseArgs(int argc, char* argv[]) {
for (int i = 0; i < argc; i++){
if (!strcmp(argv[i],"--help") || !strcmp(argv[i],"-h")){
printHelp();
exit(0);
}
else if (!strcmp(argv[i],"--version") || !strcmp(argv[i],"-v")){
printVersion();
exit(0);
}
}
}
#endif // _WIN32
int main(int argc, char* argv[])
{
parseArgs(argc, argv);
sTitle = "eiskaltdcpp-daemon (" + eiskaltdcppVersionString + ")";
#ifdef _DEBUG_DAEMON
sTitle += " [debug]";
#endif
Util::PathsMap override;
if (config_dir[0]) {
string tmp(config_dir);
tmp = tmp.substr(tmp.size()-1, tmp.size()) == PATH_SEPARATOR_STR ? tmp : tmp + PATH_SEPARATOR_STR;
override[Util::PATH_USER_CONFIG] = tmp;
override[Util::PATH_USER_LOCAL] = tmp;
if (!Util::fileExists(string(config_dir))) {
logging(bDaemon, bsyslog, false, string("ERROR: Config directory: No such file or directory (" + string(config_dir) + ")"));
}
}
if (local_dir[0]) {
string tmp(local_dir);
tmp = tmp.substr(tmp.size()-1, tmp.size()) == PATH_SEPARATOR_STR ? tmp : tmp + PATH_SEPARATOR_STR;
override[Util::PATH_USER_LOCAL] = tmp;
if (!Util::fileExists(string(local_dir))) {
logging(bDaemon, bsyslog, false, string("ERROR: Local data directory: No such file or directory (" + string(local_dir) + ")"));
}
}
Util::initialize(override);
if (isDebug) {
printf("\
PATH_GLOBAL_CONFIG: %s\n\
PATH_USER_CONFIG: %s\n\
PATH_USER_LOCAL: %s\n\
PATH_RESOURCES: %s\n\
PATH_LOCALE: %s\n\
PATH_DOWNLOADS: %s\n\
PATH_FILE_LISTS %s\n\
PATH_HUB_LISTS: %s\n\
PATH_NOTEPAD: %s\n",
Util::getPath(Util::PATH_GLOBAL_CONFIG).c_str(),
Util::getPath(Util::PATH_USER_CONFIG).c_str(),
Util::getPath(Util::PATH_USER_LOCAL).c_str(),
Util::getPath(Util::PATH_RESOURCES).c_str(),
Util::getPath(Util::PATH_LOCALE).c_str(),
Util::getPath(Util::PATH_DOWNLOADS).c_str(),
Util::getPath(Util::PATH_FILE_LISTS).c_str(),
Util::getPath(Util::PATH_HUB_LISTS).c_str(),
Util::getPath(Util::PATH_NOTEPAD).c_str()
);
fflush(stdout);
}
PATH = Util::getPath(Util::PATH_USER_CONFIG);
LOCAL_PATH = Util::getPath(Util::PATH_USER_LOCAL);
if (LOG_FILE.empty())
#ifdef _WIN32
LOG_FILE = PATH + "logs\\daemon.log";
#else
LOG_FILE = PATH + "Logs/daemon.log";
#endif
if (bDaemon) {
if (!Util::fileExists(LOG_FILE)) {
logging(false, false, false, string("ERROR: Daemon log: No such file or directory (") + LOG_FILE.c_str()+ string(")"));
}
}
#ifndef _WIN32
if (bDaemon) {
if (eidcpp_daemon(true,false) == -1)
return EXIT_FAILURE;
if (pidfile[0])
writePidFile(pidfile);
}
#endif
logging(bDaemon, bsyslog, true, string("Starting "+sTitle+" using "+PATH+" as config directory and "+LOCAL_PATH+" as local data directory."));
#ifndef _WIN32
sigset_t sst;
sigemptyset(&sst);
sigaddset(&sst, SIGPIPE);
sigaddset(&sst, SIGURG);
sigaddset(&sst, SIGALRM);
if (bDaemon)
sigaddset(&sst, SIGHUP);
pthread_sigmask(SIG_BLOCK, &sst, nullptr);
struct sigaction sigact;
sigact.sa_handler = SigHandler;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
if (sigaction(SIGINT, &sigact, nullptr) == -1) {
logging(bDaemon, bsyslog, false, string("Cannot create sigaction SIGINT!" + string(strerror(errno))));
exit(EXIT_FAILURE);
}
if (sigaction(SIGTERM, &sigact, nullptr) == -1) {
logging(bDaemon, bsyslog, false, string("Cannot create sigaction SIGTERM!"+ string(strerror(errno))));
exit(EXIT_FAILURE);
}
if (sigaction(SIGQUIT, &sigact, nullptr) == -1) {
logging(bDaemon, bsyslog, false, string("Cannot create sigaction SIGQUIT!" + string(strerror(errno))));
exit(EXIT_FAILURE);
}
if (!bDaemon && sigaction(SIGHUP, &sigact, nullptr) == -1) {
logging(bDaemon, bsyslog, false, string("Cannot create sigaction SIGHUP!" + string(strerror(errno))));
exit(EXIT_FAILURE);
}
#endif
ServerInitialize();
if (!ServerStart()) {
logging(bDaemon, bsyslog, false, "Server start failed!");
return EXIT_FAILURE;
} else {
logging(bDaemon, bsyslog, true, sTitle+" running...");
}
#if !defined(_WIN32) && defined(ENABLE_STACKTRACE)
signal(SIGSEGV, printBacktrace);
#endif
while (bServerRunning) {
Thread::sleep(1);
if (bServerTerminated)
ServerStop();
}
return 0;
}
|