/*************************************************************************** * Copyright (C) 03/2009 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. * ***************************************************************************/ #include #include #include #include #include #include #include "configfile.h" #include "event.h" #include "logging.h" #include "pidfile.h" #include "signals.h" #include "usvdevice.h" #include "usvstate.h" #define DEFAULT_CONFIG "alix-usvd.conf" #define DEFAULT_LOGFILE "alix-usvd.log" #define DEFAULT_PIDFILE "alix-usvd.pid" static struct option opts[] = { {"nofork", 0, 0, 'd'}, {"config", 1, 0, 'c'}, {"help", 0, 0, 'h'}, {0, 0, 0, 0} }; int main(int argc, char *argv[]) { char *config = DEFAULT_CONFIG; int debug = 0; int code, arg = 0; do { code = getopt_long(argc, argv, "dc:h", opts, &arg); switch (code) { case 'd': /* debug */ debug = 1; break; case 'c': /* configfile path */ config = optarg; break; case 'h': /* help */ printf( "Usage: alix-usvd [-d] [-c config]\n" "Options: \n" " --debug -d do not fork and log to stderr\n" " --config -c configfile use this configfile\n" " --help -h this help\n" "\n"); exit(0); break; case '?': /* error */ exit(1); break; default: /* unknown / all options parsed */ break; } } while (code != -1); /* parse config file */ if (config_parse(config) < 0) exit(1); if (!debug) { /* check pidfile */ const char *pidfile = config_get_string("global", "pidfile", DEFAULT_PIDFILE); if (pidfile_check(pidfile, 1) != 0) { log_print(LOG_ERROR, "alix-usvd already running"); exit(1); } /* start logging */ const char *logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE); if (log_init(logfile) < 0) exit(1); /* zum daemon mutieren */ daemon(-1, 0); /* create pidfile */ if (pidfile_create(pidfile) < 0) { log_print(LOG_ERROR, "failed to create pidfile %s", pidfile); exit(1); } } log_print(LOG_INFO, "alix-usvd started (pid: %d)", getpid()); signal_init(); signal_set_callback(SIGHUP, event_loop_break); while (1) { if (usvdev_init() < 0) break; if (usvstate_init() < 0) break; event_loop(); usvstate_close(); log_close(); config_free(); if (config_parse(config) < 0) break; const char *logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE); if (!debug && log_init(logfile) < 0) break; log_print(LOG_INFO, "alix-usvd restarted (pid: %d)", getpid()); } return 0; }