From e02be8a6ae1766c4c61a874ce74a86507bdc2bcc Mon Sep 17 00:00:00 2001 From: Mke Brady Date: Wed, 29 Apr 2015 19:52:56 +0100 Subject: [PATCH] Add atexit functionality, continue to add config file support. --- common.h | 2 +- shairport.c | 45 ++++++++++++++++++++++++++++++--------------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/common.h b/common.h index 4adc6d83..8c50edf3 100644 --- a/common.h +++ b/common.h @@ -41,7 +41,7 @@ enum stuffing_type { typedef struct { - config_t cfg; // if used, all other command-line arguments are ignored + config_t cfg; char *password; char *apname; #ifdef CONFIG_METADATA diff --git a/shairport.c b/shairport.c index bc5a7d5a..141c7d56 100644 --- a/shairport.c +++ b/shairport.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #include "config.h" @@ -61,6 +62,7 @@ static int shutting_down = 0; +static char* appName = NULL; void shairport_shutdown() { if (shutting_down) @@ -191,7 +193,7 @@ void usage(char *progname) { #endif #endif #ifdef SUPPORT_CONFIG_FILES - printf("All other settings are in the configuration file at /etc/%s.conf\n",progname); + printf("\nGeneral options can be configured in /etc/%s.conf.\n",appName); #endif printf("\n"); mdns_ls_backends(); @@ -282,7 +284,7 @@ int parse_options(int argc, char **argv) { #ifdef SUPPORT_CONFIG_FILES char configuration_file_path[4096]; strcpy(configuration_file_path,"/etc/"); - strcat(configuration_file_path,argv[0]); + strcat(configuration_file_path,appName); strcat(configuration_file_path,".conf"); debug(1,"Looking for file \"%s\"",configuration_file_path); config_t cfg; @@ -290,20 +292,20 @@ int parse_options(int argc, char **argv) { const char *str; int value; - config_init(&cfg); + config_init(&config.cfg); debug(1,"config stuff initialised"); /* Read the file. If there is an error, report it and exit. */ - if(config_read_file(&cfg,configuration_file_path)) { + if(config_read_file(&config.cfg,configuration_file_path)) { debug(1,"Configuration file opened."); debug(1,"name"); /* Get the Service Name. */ - if(config_lookup_string(&cfg, "general.name", &str)) - config.apname=strdup(str); + if(config_lookup_string(&config.cfg, "general.name", &str)) + config.apname=str; debug(1,"daemonize"); /* Get the Daemonize setting. */ - if(config_lookup_string(&cfg, "general.daemonize", &str)) { + if(config_lookup_string(&config.cfg, "general.daemonize", &str)) { if (strcasecmp(str,"no")==0) config.daemonise=0; else if (strcasecmp(str,"yes")==0) @@ -314,25 +316,25 @@ int parse_options(int argc, char **argv) { debug(1,"mdns_backend"); /* Get the mdns_backend setting. */ - if(config_lookup_string(&cfg, "general.mdns_backend", &str)) - config.mdns_name=strdup(str); + if(config_lookup_string(&config.cfg, "general.mdns_backend", &str)) + config.mdns_name=str; debug(1,"output_backend"); /* Get the output_backend setting. */ - if(config_lookup_string(&cfg, "general.output_backend", &str)) - config.output_name=strdup(str); + if(config_lookup_string(&config.cfg, "general.output_backend", &str)) + config.output_name=str; debug(1,"port"); /* Get the port setting. */ - if(config_lookup_int(&cfg, "general.port", &value)) + if(config_lookup_int(&config.cfg, "general.port", &value)) config.port=value; debug(1,"done"); } else { - debug(1,"Line %d of the configuration file \"%s\":\n%s",config_error_line(&cfg),config_error_file(&cfg),config_error_text(&cfg)); + debug(1,"Line %d of the configuration file \"%s\":\n%s", + config_error_line(&config.cfg),config_error_file(&config.cfg),config_error_text(&config.cfg)); } -// config_destroy(&cfg); #endif /* Print out options */ @@ -423,11 +425,18 @@ const char *pid_file_proc(void) { } #endif +void exit_function() { + debug(1,"atexit function called..."); +#ifdef SUPPORT_CONFIG_FILES + config_destroy(&config.cfg); +#endif +} + int main(int argc, char **argv) { daemon_set_verbosity(LOG_DEBUG); - memset(&config, 0, sizeof(config)); // also clears all strings, BTW + atexit(exit_function); // set defaults config.statistics_requested - 0; // don't print stats in the log @@ -448,6 +457,12 @@ int main(int argc, char **argv) { snprintf(config.apname, 20 + 100, "Shairport Sync on %s", hostname); set_requested_connection_state_to_output(1); // we expect to be able to connect to the output device + // this is a bit weird, but apparently necessary + char* basec = strdup(argv[0]); + char* bname = basename(basec); + appName = strdup(bname); + free(basec); + /* Check if we are called with -V or --version parameter */ if (argc >= 2 && ((strcmp(argv[1], "-V")==0) || (strcmp(argv[1], "--version")==0))) { print_version();