torrent-stats/torrent-stats.c

144 lines
3.8 KiB
C
Raw Permalink Normal View History

2007-07-14 15:31:27 +02:00
/***************************************************************************
* Copyright (C) 07/2007 by Olaf Rempel *
* razzor@kopf-tisch.de *
* *
* 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; version 2 of the License *
* *
* This program 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 General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
2007-05-15 17:39:34 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
2008-11-02 13:14:53 +01:00
#include <getopt.h>
2009-02-25 14:17:23 +01:00
#include <signal.h>
2008-11-02 13:14:53 +01:00
2007-05-15 17:39:34 +02:00
#include "configfile.h"
2007-06-18 15:44:05 +02:00
#include "connection.h"
2007-05-15 17:39:34 +02:00
#include "event.h"
#include "logging.h"
2009-05-03 14:20:03 +02:00
#include "pidfile.h"
#include "process.h"
#include "signals.h"
2009-05-04 14:42:29 +02:00
#include "torrentfile.h"
2007-05-15 17:39:34 +02:00
2008-11-02 13:14:53 +01:00
#define DEFAULT_CONFIG "torrent-stats.conf"
2009-02-24 14:55:09 +01:00
#define DEFAULT_LOGFILE "torrent-stats.log"
2009-05-03 14:20:03 +02:00
#define DEFAULT_PIDFILE "torrent-stats.pid"
2008-11-02 13:14:53 +01:00
static struct option opts[] = {
{"config", 1, 0, 'c'},
2009-02-24 14:55:09 +01:00
{"debug", 0, 0, 'd'},
2008-11-02 13:14:53 +01:00
{"help", 0, 0, 'h'},
{0, 0, 0, 0}
};
2007-05-15 17:39:34 +02:00
int main(int argc, char *argv[])
{
2008-11-02 13:14:53 +01:00
char *config = DEFAULT_CONFIG;
2009-02-24 14:55:09 +01:00
int debug = 0;
2008-11-02 13:14:53 +01:00
int code, arg = 0;
do {
2009-02-24 14:55:09 +01:00
code = getopt_long(argc, argv, "c:u:dh", opts, &arg);
2008-11-02 13:14:53 +01:00
switch (code) {
case 'c': /* config */
config = optarg;
break;
2009-02-24 14:55:09 +01:00
case 'd': /* debug */
debug = 1;
break;
2008-11-02 13:14:53 +01:00
case 'h': /* help */
printf("Usage: torrent-stats [options]\n"
"Options: \n"
" --config -c configfile use this configfile\n"
2009-02-24 14:55:09 +01:00
" --debug -d do not fork and log to stderr\n"
2008-11-02 13:14:53 +01:00
" --help -h this help\n"
"\n");
exit(0);
break;
case '?': /* error */
exit(1);
break;
default: /* unknown / all options parsed */
break;
}
} while (code != -1);
2009-02-24 14:55:09 +01:00
/* parse config file */
2008-11-02 13:14:53 +01:00
if (config_parse(config) < 0)
exit(1);
2007-05-15 17:39:34 +02:00
2009-02-24 14:55:09 +01:00
/* check logfile */
const char *logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE);
if (!debug) {
2009-05-03 14:20:03 +02:00
/* check pidfile */
const char *pidfile = config_get_string("global", "pidfile", DEFAULT_PIDFILE);
if (pidfile_check(pidfile, 1) != 0) {
log_print(LOG_ERROR, "torrent-stats already running");
exit(1);
}
2009-02-24 14:55:09 +01:00
/* start logging */
if (log_init(logfile) < 0)
2009-02-25 14:17:23 +01:00
exit(1);
2009-02-24 14:55:09 +01:00
/* zum daemon mutieren */
daemon(-1, 0);
2009-05-03 14:20:03 +02:00
/* create pidfile */
if (pidfile_create(pidfile) < 0) {
log_print(LOG_ERROR, "failed to create pidfile %s", pidfile);
exit(1);
}
2009-02-24 14:55:09 +01:00
}
2009-05-03 14:20:03 +02:00
log_print(LOG_INFO, "torrent-stats started (pid: %d)", getpid());
2009-02-24 14:55:09 +01:00
2009-05-03 14:20:03 +02:00
signal_init();
signal_set_callback(SIGCHLD, childproc_cleanup);
signal_set_callback(SIGHUP, event_loop_break);
2007-06-18 15:44:05 +02:00
2009-05-03 14:20:03 +02:00
while (1) {
2009-05-04 14:42:29 +02:00
if (torrentfile_init() < 0)
break;
2009-05-03 14:20:03 +02:00
if (ctcs_init() < 0)
break;
2007-05-15 17:39:34 +02:00
2009-05-03 14:20:03 +02:00
event_loop();
2009-02-24 12:49:40 +01:00
2009-05-03 14:20:03 +02:00
ctcs_free();
2007-06-19 15:44:57 +02:00
2009-05-03 14:20:03 +02:00
log_close();
2009-02-25 14:17:23 +01:00
2009-05-03 14:20:03 +02:00
config_free();
2009-02-25 14:17:23 +01:00
2009-05-03 14:20:03 +02:00
if (config_parse(config) < 0)
break;
logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE);
if (!debug && log_init(logfile) < 0)
break;
log_print(LOG_INFO, "torrent-stats restarted (pid: %d)", getpid());
}
2009-02-25 14:17:23 +01:00
2007-05-15 17:39:34 +02:00
return 0;
}