uts-server/src/cmd/uts-server.c

129 lines
3.4 KiB
C
Raw Normal View History

2016-11-02 18:58:14 +01:00
#include "http.h"
#include <argp.h>
2015-12-17 15:49:58 +01:00
#include <ctype.h>
#include <getopt.h>
#include <libgen.h>
#include <limits.h>
2017-01-27 00:28:46 +01:00
#ifdef BSD
#include <sys/syslimits.h>
#else
#include <linux/limits.h>
2017-01-27 00:28:46 +01:00
#endif /* BSD */
2016-11-02 18:58:14 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <sys/syslog.h>
#include <unistd.h>
2015-12-17 15:49:58 +01:00
const char *argp_program_version = UTS_VERSION;
2016-08-23 19:38:07 +02:00
const char *argp_program_bug_address =
"Pierre-Francois Carpentier <carpentier.pf@gmail.com>";
2015-12-17 15:49:58 +01:00
static char doc[] = "\nUTS micro timestamp server (RFC 3161)";
static struct argp_option options[] = {
2016-08-23 19:38:07 +02:00
{"conffile", 'c', "CONFFILE", 0, "Path to configuration file"},
{"daemonize", 'd', 0, 0, "Launch as a daemon"},
{"pidfile", 'p', "PIDFILE", 0, "Path to pid file"},
{"debug", 'D', 0, 0, "STDOUT debugging"},
2016-08-23 19:38:07 +02:00
{0}};
2015-12-17 15:49:58 +01:00
/* A description of the arguments we accept. */
2016-09-06 08:51:48 +02:00
static char args_doc[] = "-c CONFFILE [-d] [-D] [-p <pidfile>]";
2015-12-17 15:49:58 +01:00
2016-08-23 19:38:07 +02:00
struct arguments {
char *args[2]; /* arg1 & arg2 */
int daemonize;
bool stdout_dbg;
2015-12-17 15:49:58 +01:00
char *conffile;
char *pidfile;
2015-12-17 15:49:58 +01:00
};
2016-08-23 19:38:07 +02:00
static error_t parse_opt(int key, char *arg, struct argp_state *state) {
2015-12-17 15:49:58 +01:00
/* Get the input argument from argp_parse, which we
know is a pointer to our arguments structure. */
struct arguments *arguments = (struct arguments *)state->input;
2016-08-23 19:38:07 +02:00
switch (key) {
case 'd':
arguments->daemonize = 1;
break;
case 'D':
arguments->stdout_dbg = 1;
break;
2016-08-23 19:38:07 +02:00
case 'c':
arguments->conffile = arg;
break;
case 'p':
arguments->pidfile = arg;
break;
2016-08-23 19:38:07 +02:00
default:
return ARGP_ERR_UNKNOWN;
2015-12-17 15:49:58 +01:00
}
return 0;
}
/* Our argp parser. */
2016-08-23 19:38:07 +02:00
static struct argp argp = {options, parse_opt, args_doc, doc};
2015-12-17 15:49:58 +01:00
2016-08-23 19:38:07 +02:00
int main(int argc, char **argv) {
2015-12-17 15:49:58 +01:00
struct arguments args;
2016-08-23 19:38:07 +02:00
args.conffile = NULL;
args.pidfile = NULL;
2015-12-17 15:49:58 +01:00
args.daemonize = 0;
2016-08-24 23:41:55 +02:00
args.stdout_dbg = 0;
2016-08-23 19:38:07 +02:00
argp_parse(&argp, argc, argv, 0, 0, &args);
int ret = EXIT_SUCCESS;
2015-12-17 15:49:58 +01:00
// get the full path of the configuration (daemon -> chdir to / concequently
// full path is necessary)
char conf_fp[PATH_MAX];
if (realpath(args.conffile, conf_fp) == NULL) {
syslog(LOG_CRIT, "unable to get the full path of the configuration "
"file, uts-server start failed");
return EXIT_FAILURE;
}
init_pid(args.pidfile);
// get the full path for the pid file
char pid_file[PATH_MAX];
if ((args.pidfile != NULL) && realpath(args.pidfile, pid_file) == NULL) {
syslog(LOG_CRIT, "unable to get the full path of the pid "
"file, uts-server start failed");
2016-09-01 08:29:07 +02:00
return EXIT_FAILURE;
}
// get the directory containing the configuration file
// other uts-server files (ca, certs, etc) can be declared relatively to the
// configuration file
char *tmp_wd = strdup(conf_fp);
char *conf_wd = dirname(tmp_wd);
if (args.daemonize)
skeleton_daemon();
else
set_sig_handler();
2016-09-01 08:29:07 +02:00
syslog(LOG_NOTICE,
"uts-server daemon starting with conf '%s' from working dir '%s'",
conf_fp, conf_wd);
if (args.pidfile != NULL) {
if (write_pid(pid_file) == 0) {
syslog(LOG_CRIT, "failed to write pid file '%s'", pid_file);
return EXIT_FAILURE;
}
}
2016-08-23 19:38:07 +02:00
while (1) {
ret = http_server_start(conf_fp, conf_wd, args.stdout_dbg);
2015-12-17 14:09:44 +01:00
break;
}
syslog(LOG_NOTICE, "uts-server daemon terminated.");
free(tmp_wd);
2015-12-17 14:09:44 +01:00
closelog();
return ret;
2015-12-17 14:09:44 +01:00
}