From 8df182e6d1eaa9a0fd6323b9b6c22f118b28acd4 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 5 Feb 2019 03:22:39 +0000 Subject: initial --- main.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 main.c (limited to 'main.c') diff --git a/main.c b/main.c new file mode 100644 index 0000000..6c50983 --- /dev/null +++ b/main.c @@ -0,0 +1,98 @@ +/* + qotdserver +Copyright (C) 2019 Richard Knight + +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 3 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, see . + + */ + +#include +#include +#include +#include +#include "ini.h" +#include "qotdserver.h" +#include "logger.h" + + +void signal_handler(int sig){ + switch(sig){ + case SIGTERM: + clean_db(); + write_log("SIGTERM received"); + close_logger(); + exit(0); + break; + case SIGKILL: + clean_db(); + write_log("SIGKILL received"); + close_logger(); + exit(0); + break; + } +} + +static int config_handler(void *user, const char *section, const char *name, + const char *value) +{ + Config *cnf = (Config*)user; + + #define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0 + + if (MATCH("database", "user")) { + cnf->db_user = strdup(value); + } else if (MATCH("database", "password")) { + cnf->db_pass = strdup(value); + } else if (MATCH("database", "host")) { + cnf->db_host = strdup(value); + } else if (MATCH("database", "name")) { + cnf->db_name = strdup(value); + } else if (MATCH("general", "daily")) { + cnf->daily = atoi(value); + } else { + return 0; + } + return 1; +} + + +int main(int argc, char** argv) +{ + signal(SIGTERM, signal_handler); + signal(SIGKILL, signal_handler); + + char *conf_path = "/etc/qotdserver.conf"; + + + if (ini_parse(conf_path, config_handler, &config) < 0) { + printf("Can't load configuration file %s\n", conf_path); + return (EXIT_FAILURE); + } + + if (!init_logger(&config)) { + printf("Can't open log file for writing\n"); + return (EXIT_FAILURE); + } + + if (!db_connect(&config)) { + return (EXIT_FAILURE); + } + + write_log("Starting server"); + + daemon(0, 0); + serve(); + +} + -- cgit v1.2.3