qnapd/qnapd.c

200 lines
4.9 KiB
C
Raw Normal View History

2011-04-17 17:08:46 +02:00
/***************************************************************************
2011-05-21 12:08:16 +02:00
* Copyright (C) 05/2011 by Olaf Rempel *
2011-04-17 17:08:46 +02:00
* 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; either version 2 of the License, or *
* (at your option) any later version. *
* *
* 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. *
***************************************************************************/
2011-04-17 16:59:51 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
2011-04-17 17:08:46 +02:00
#include <getopt.h>
2011-04-17 16:59:51 +02:00
2012-12-08 00:39:04 +01:00
#include <signal.h>
2012-02-14 20:29:05 +01:00
2011-04-17 17:08:46 +02:00
#include "configfile.h"
2012-12-09 01:30:37 +01:00
#include "diskwatch.h"
2011-04-17 16:59:51 +02:00
#include "event.h"
#include "lcd.h"
2012-12-09 01:30:37 +01:00
#include "lcdpage.h"
2011-04-17 16:59:51 +02:00
#include "logging.h"
2012-12-08 00:39:04 +01:00
#include "pic.h"
2011-04-17 17:08:46 +02:00
#include "pidfile.h"
#include "signals.h"
2011-04-17 16:59:51 +02:00
2012-12-08 00:39:04 +01:00
#define DEFAULT_CONFIG "qnapd.conf"
#define DEFAULT_LOGFILE "qnapd.log"
#define DEFAULT_PIDFILE "qnapd.pid"
2011-04-17 17:08:46 +02:00
2012-12-09 01:30:37 +01:00
#define LCD_TIMEOUT 15
2011-04-17 17:08:46 +02:00
static struct option opts[] = {
2012-12-08 00:39:04 +01:00
{ "config", 1, 0, 'c' },
{ "debug", 0, 0, 'd' },
{ "help", 0, 0, 'h' },
{ 0, 0, 0, 0 }
2011-04-17 17:08:46 +02:00
};
static int restart_var;
static void trigger_restart(void *privdata)
{
2012-12-08 00:39:04 +01:00
restart_var = 1;
2011-04-17 17:08:46 +02:00
}
int check_restart(int *maxfd, void *readfds, void *writefds, struct timeval *timeout, void *privdata)
2011-04-17 16:59:51 +02:00
{
2012-12-08 00:39:04 +01:00
if (restart_var == 1) {
restart_var = 0;
return 1;
}
2011-04-17 16:59:51 +02:00
2012-12-08 00:39:04 +01:00
return 0;
2011-04-17 17:08:46 +02:00
}
2011-04-17 16:59:51 +02:00
2011-04-17 17:08:46 +02:00
int main(int argc, char *argv[])
{
2011-04-17 17:37:38 +02:00
char *config = DEFAULT_CONFIG;
int code, arg = 0, debug = 0;
do {
code = getopt_long(argc, argv, "c:dh", opts, &arg);
switch (code) {
case 'c': /* config */
config = optarg;
break;
case 'd': /* debug */
debug = 1;
break;
case 'h': /* help */
2012-12-08 00:39:04 +01:00
printf("Usage: qnapd [options]\n"
2011-04-17 17:37:38 +02:00
"Options: \n"
" --config -c configfile use this configfile\n"
" --debug -d do not fork and log to stderr\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) {
2012-12-08 00:39:04 +01:00
log_print(LOG_ERROR, "qnapd already running");
2011-04-17 17:37:38 +02:00
exit(1);
}
/* start logging */
const char *logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE);
if (log_init(logfile) < 0)
exit(1);
2012-12-08 00:39:04 +01:00
/* mutate to daemon */
2011-04-17 17:37:38 +02:00
if (daemon(-1, 0) < 0) {
log_print(LOG_ERROR, "failed to daemonize");
exit(1);
}
/* create pidfile */
if (pidfile_create(pidfile) < 0) {
log_print(LOG_ERROR, "failed to create pidfile %s", pidfile);
exit(1);
}
}
2011-04-17 17:08:46 +02:00
2011-04-17 17:37:38 +02:00
signal_init();
2012-12-08 00:39:04 +01:00
signal_add_callback(SIGHUP, trigger_restart, NULL);
2011-04-17 17:08:46 +02:00
2012-12-08 00:39:04 +01:00
log_print(LOG_EVERYTIME, "qnapd started (pid:%d)", getpid());
2011-04-17 17:08:46 +02:00
2011-04-17 17:37:38 +02:00
while (1) {
2012-12-08 00:39:04 +01:00
struct lcddev *lcd = NULL;
struct picdev *pic = NULL;
do {
const char *devicename = config_get_string("global", "lcddevice", NULL);
if (devicename != NULL) {
2012-12-09 01:30:37 +01:00
int lcdtimeout;
config_get_int("global", "lcdtimeout", &lcdtimeout, LCD_TIMEOUT);
lcd = lcd_open(devicename, lcdtimeout);
2012-12-08 00:39:04 +01:00
if (lcd == NULL) {
break;
}
if (lcdpage_init(lcd) < 0) {
break;
}
}
devicename = config_get_string("global", "picdevice", NULL);
if (devicename != NULL) {
pic = pic_open(devicename, lcd);
if (pic == NULL) {
break;
}
}
2012-12-09 01:30:37 +01:00
if (diskwatch_init(lcd) < 0)
2011-05-21 12:07:02 +02:00
break;
2012-12-08 00:39:04 +01:00
/* exited on restart / SIGUSR1 */
event_loop(check_restart, NULL, NULL);
} while (0);
2011-04-17 17:08:46 +02:00
2012-12-09 01:30:37 +01:00
diskwatch_exit();
2011-05-21 12:07:02 +02:00
2012-12-08 00:39:04 +01:00
if (pic != NULL) {
pic_close(pic);
}
2012-02-14 20:38:12 +01:00
2012-12-08 00:39:04 +01:00
if (lcd != NULL) {
lcdpage_free();
lcd_close(lcd);
}
2011-04-17 17:08:46 +02:00
2011-04-17 17:37:38 +02:00
config_free();
2011-04-17 17:08:46 +02:00
2011-04-17 17:37:38 +02:00
if (config_parse(config) < 0)
break;
2011-04-17 17:08:46 +02:00
2011-04-17 17:37:38 +02:00
const char *logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE);
if (!debug && log_init(logfile) < 0)
break;
2011-04-17 17:08:46 +02:00
2012-12-08 00:39:04 +01:00
log_print(LOG_EVERYTIME, "qnapd restarted (pid:%d) ", getpid());
2011-04-17 17:37:38 +02:00
}
2011-04-17 17:08:46 +02:00
2011-04-17 17:37:38 +02:00
return 0;
2011-04-17 16:59:51 +02:00
}