diff --git a/inc/http.h b/inc/http.h index d29de81..2ead941 100644 --- a/inc/http.h +++ b/inc/http.h @@ -1,4 +1,4 @@ #include #include "utils.h" -int http_server_start(char *conffile, bool stdout_dbg); +int http_server_start(char *conffile, char *conf_wd,bool stdout_dbg); diff --git a/inc/utils.h b/inc/utils.h index ff22742..f978e01 100644 --- a/inc/utils.h +++ b/inc/utils.h @@ -4,6 +4,6 @@ void skeleton_daemon(); void uts_logger(rfc3161_context *ct, int priority, char *fmt, ...); void log_hex(rfc3161_context *ct, int priority, char *id, unsigned char *content, int content_length); -int set_params(rfc3161_context *ct, char *conf_file); +int set_params(rfc3161_context *ct, char *conf_file, char *conf_wd); int g_uts_sig_up; int g_uts_sig; diff --git a/src/cmd/uts-server.c b/src/cmd/uts-server.c index b60277e..af9aff0 100644 --- a/src/cmd/uts-server.c +++ b/src/cmd/uts-server.c @@ -61,19 +61,27 @@ int main(int argc, char **argv) { args.daemonize = 0; args.stdout_dbg = 0; argp_parse(&argp, argc, argv, 0, 0, &args); + int ret = EXIT_SUCCESS; if (args.daemonize) skeleton_daemon(); + // get the current path, the configuration can be relative to this path + char conf_wd[PATH_MAX]; + if (getcwd(conf_wd, sizeof(conf_wd)) == NULL){ + syslog(LOG_CRIT, "unable to get the current, uts-server start failed"); + return EXIT_FAILURE; + } + + syslog(LOG_NOTICE, "uts-server daemon starting with conf '%s' from working dir '%s'", args.conffile, conf_wd); + while (1) { - // TODO: Insert daemon code here. - syslog(LOG_NOTICE, "uts-server daemon starting."); - http_server_start(args.conffile, args.stdout_dbg); + ret = http_server_start(args.conffile, conf_wd, args.stdout_dbg); break; } syslog(LOG_NOTICE, "uts-server daemon terminated."); closelog(); - return EXIT_SUCCESS; + return ret; } diff --git a/src/lib/http.c b/src/lib/http.c index f53f127..654f8f9 100644 --- a/src/lib/http.c +++ b/src/lib/http.c @@ -128,15 +128,15 @@ int rfc3161_handler(struct mg_connection *conn, void *context) { return 1; } -int http_server_start(char *conffile, bool stdout_dbg) { +int http_server_start(char *conffile, char * conf_wd, bool stdout_dbg) { struct mg_context *ctx; struct mg_callbacks callbacks; rfc3161_context *ct = (rfc3161_context *)calloc(1, sizeof(rfc3161_context)); ct->stdout_dbg = stdout_dbg; ct->loglevel = 8; - if (!set_params(ct, conffile)) - return 1; + if (!set_params(ct, conffile, conf_wd)) + return EXIT_FAILURE; // Prepare callbacks structure. We have only one callback, the rest are // NULL. @@ -146,8 +146,7 @@ int http_server_start(char *conffile, bool stdout_dbg) { ctx = mg_start(&callbacks, NULL, ct->http_options); mg_set_request_handler(ctx, "/", rfc3161_handler, (void *)ct); - // Wait until user hits "enter". Server is running in separate thread. - // Navigating to http://localhost:8080 will invoke begin_request_handler(). + // Wait until some signals are received while ( g_uts_sig == 0 ){ sleep(1); } diff --git a/src/lib/utils.c b/src/lib/utils.c index 691fa7f..7f57118 100644 --- a/src/lib/utils.c +++ b/src/lib/utils.c @@ -217,11 +217,13 @@ static CONF *load_config_file(rfc3161_context *ct, const char *filename) { return NULL; } -int set_params(rfc3161_context *ct, char *conf_file) { +int set_params(rfc3161_context *ct, char *conf_file, char *conf_wd) { int ret = 0; CONF *conf = load_config_file(ct, conf_file); ret = 1; int http_counter = 0; + + chdir(conf_wd); // first pass to set the loglevel as soon as possible for (int i = 0; i < RFC3161_OPTIONS_LEN; i++) { int type = rfc3161_options[i].type; @@ -277,8 +279,10 @@ int set_params(rfc3161_context *ct, char *conf_file) { ct->ts_ctx = create_tsctx(ct, conf, NULL, NULL); if (ct->ts_ctx == NULL) ret = 0; + chdir("/"); return ret; end: + chdir("/"); return 0; }