passing the launch working dir to the configuration handler

as many configuration paths can be relative to the execution working
directory, this directory is recorded at launch time, and the set_params
function does a chdir(<start work dir>); <load conf>; chdir("/") when
it's called.
This commit is contained in:
kakwa 2016-09-01 07:59:02 +02:00
parent 93ead7ee99
commit 6e687f2fe2
5 changed files with 23 additions and 12 deletions

View File

@ -1,4 +1,4 @@
#include <stdbool.h>
#include "utils.h"
int http_server_start(char *conffile, bool stdout_dbg);
int http_server_start(char *conffile, char *conf_wd,bool stdout_dbg);

View File

@ -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;

View File

@ -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;
}

View File

@ -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);
}

View File

@ -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;
}