QNAP-TS419p system daemon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

277 lines
6.5 KiB

/***************************************************************************
* Copyright (C) 05/2011 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; 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. *
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <getopt.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/utsname.h>
#include <time.h>
#include "configfile.h"
#include "disktimeout.h"
#include "event.h"
#include "lcd.h"
#include "logging.h"
#include "pidfile.h"
#include "signals.h"
void pic_init(void);
void pic_exit(void);
#define LCD_DEVICE "/dev/ttyS0"
#define LCD_TIMEOUT 15
#define DEFAULT_CONFIG PROGNAME".conf"
#define DEFAULT_LOGFILE PROGNAME".log"
#define DEFAULT_PIDFILE PROGNAME".pid"
static struct option opts[] = {
{"config", 1, 0, 'c'},
{"debug", 0, 0, 'd'},
{"help", 0, 0, 'h'},
{0, 0, 0, 0}
};
#define PAGE_MAX 7
static int page;
static int button_callback(struct lcddev *dev, int button, void *privdata)
{
if (button == LCD_EVENT_BUTTON2) {
page = (page < PAGE_MAX) ? page +1 : 0;
} else if (button == LCD_EVENT_BUTTON1) {
page = (page > 0) ? page -1 : PAGE_MAX;
} else if (button == LCD_EVENT_BACKLIGHT) {
return 0; /* no update requested */
}
if (button != LCD_EVENT_UPDATE) {
lcd_trigger_backlight(dev, LCD_TIMEOUT);
}
int update = 0;
switch (page) {
case 7:
{
FILE *fp = fopen("/etc/issue.net", "r");
char line1[64];
char line2[64];
memset(line1, 0x00, sizeof(line1));
memset(line2, 0x00, sizeof(line2));
if (fp != NULL) {
fread(line1, 1, sizeof(line1), fp);
fclose(fp);
} else {
strcpy(line1, "<unknown system>");
}
struct utsname utsbuf;
uname(&utsbuf);
snprintf(line2, sizeof(line2), "%s %s", utsbuf.sysname, utsbuf.release);
lcd_setlines(dev, 500, line1, line2);
}
break;
case 1:
lcd_setlines(dev, 0, "Hostname:", "storenix.lan");
break;
case 2:
lcd_setlines(dev, 0, "Address(br0):", "10.10.250.135");
break;
case 3:
lcd_setlines(dev, 0, "Netmask(br0):", "255.255.0.0");
break;
case 4:
lcd_setlines(dev, 0, "Gateway(br0):", "10.10.250.250");
break;
case 5:
lcd_setlines(dev, 0, "LoadAVG:", "x.xx x.xx x.xx");
break;
case 6:
lcd_setlines(dev, 0, "Uptime:", " XXXd XX:XX:XX");
break;
case 0:
{
char line1[20];
char line2[20];
struct tm tmp;
time_t now;
time(&now);
localtime_r(&now, &tmp);
strftime(line1, sizeof(line1), "Time: %H:%M:%S", &tmp);
strftime(line2, sizeof(line2), "Date: %d-%m-%Y", &tmp);
lcd_setlines(dev, 0, line1, line2);
update = 1000;
}
break;
}
return update;
}
static int restart_var;
static void trigger_restart(void *privdata)
{
int *restart = (int *)privdata;
*restart = 1;
}
int check_restart(int *maxfd, void *readfds, void *writefds, struct timeval *timeout, void *privdata)
{
int *restart = (int *)privdata;
if (*restart == 1) {
*restart = 0;
return 1;
}
return 0;
}
int main(int argc, char *argv[])
{
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 */
printf("Usage: "PROGNAME" [options]\n"
"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) {
log_print(LOG_ERROR, PROGNAME" 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 */
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);
}
}
signal_init();
signal_add_callback(SIGHUP, trigger_restart, (void *)&restart_var);
log_print(LOG_EVERYTIME, PROGNAME" started (pid:%d)", getpid());
while (1) {
const char *lcddevice = config_get_string("global", "lcddevice", NULL);
struct lcddev *dev = NULL;
if (lcddevice != NULL) {
dev = lcd_open(lcddevice, &button_callback, NULL);
if (dev == NULL)
break;
}
pic_init();
if (disktimeout_init() < 0)
break;
/* exited on restart / SIGUSR1 */
event_loop(check_restart, NULL, (void *)&restart_var);
disktimeout_exit();
pic_exit();
if (dev != NULL)
lcd_close(dev);
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_EVERYTIME, PROGNAME" restarted (pid:%d) ", getpid());
}
return 0;
}