From 6f8024ef27933d0123aee3d02002ca3600270e72 Mon Sep 17 00:00:00 2001 From: dimitri staessens Date: Wed, 6 Jul 2016 22:09:15 +0200 Subject: lib:irmd: Fix logs and irm_create The irm_create() function called irmd_destroy before some key values were initialized. Logs cleanup was missing. --- include/ouroboros/logs.h | 3 ++- src/irmd/main.c | 28 +++++++++++++++++----------- src/lib/logs.c | 6 ++++++ 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/include/ouroboros/logs.h b/include/ouroboros/logs.h index 0ffc5699..db3f8519 100644 --- a/include/ouroboros/logs.h +++ b/include/ouroboros/logs.h @@ -30,7 +30,8 @@ #error You must define OUROBOROS_PREFIX before including this file #endif -int set_logfile(char * filename); +int set_logfile(char * filename); +void close_logfile(); #define ANSI_COLOR_RED "\x1b[31m" #define ANSI_COLOR_GREEN "\x1b[32m" diff --git a/src/irmd/main.c b/src/irmd/main.c index 26251858..62673251 100644 --- a/src/irmd/main.c +++ b/src/irmd/main.c @@ -1696,6 +1696,8 @@ static struct irm * irm_create() if (instance == NULL) return NULL; + instance->state = IRMD_NULL; + if (access("/dev/shm/" SHM_DU_MAP_FILENAME, F_OK) != -1) { struct shm_du_map * dum = shm_du_map_open(); @@ -1716,6 +1718,8 @@ static struct irm * irm_create() free(instance); exit(EXIT_SUCCESS); } + + shm_du_map_close(dum); } if (pthread_rwlock_init(&instance->state_lock, NULL)) { @@ -1736,17 +1740,6 @@ static struct irm * irm_create() return NULL; } - instance->threadpool = malloc(sizeof(pthread_t) * IRMD_THREADPOOL_SIZE); - if (instance->threadpool == NULL) { - irm_destroy(); - return NULL; - } - - if ((instance->dum = shm_du_map_create()) == NULL) { - irm_destroy(); - return NULL; - } - INIT_LIST_HEAD(&instance->ipcps); INIT_LIST_HEAD(&instance->spawned_apis); INIT_LIST_HEAD(&instance->registry); @@ -1758,6 +1751,12 @@ static struct irm * irm_create() return NULL; } + instance->threadpool = malloc(sizeof(pthread_t) * IRMD_THREADPOOL_SIZE); + if (instance->threadpool == NULL) { + irm_destroy(); + return NULL; + } + if (stat(SOCK_PATH, &st) == -1) { if (mkdir(SOCK_PATH, 0777)) { LOG_ERR("Failed to create sockets directory."); @@ -1778,6 +1777,11 @@ static struct irm * irm_create() return NULL; } + if ((instance->dum = shm_du_map_create()) == NULL) { + irm_destroy(); + return NULL; + } + instance->state = IRMD_RUNNING; return instance; @@ -1894,5 +1898,7 @@ int main(int argc, char ** argv) irm_destroy(); + close_logfile(); + exit(EXIT_SUCCESS); } diff --git a/src/lib/logs.c b/src/lib/logs.c index d86073e4..eed1ee10 100644 --- a/src/lib/logs.c +++ b/src/lib/logs.c @@ -34,3 +34,9 @@ int set_logfile(char * filename) return 0; } + +void close_logfile() +{ + if (logfile != NULL) + fclose(logfile); +} -- cgit v1.2.3 From 442c2b866e55545a0e21eaaf36b11621c8110411 Mon Sep 17 00:00:00 2001 From: dimitri staessens Date: Thu, 7 Jul 2016 09:31:49 +0200 Subject: irmd, ipcps: Close logfile on exit IRMd and ipcps will now close the logfile upon exit. Also corrects exit(1) to exit(EXIT_FAILURE) and exit(0) to exit(EXIT_SUCCESS). --- src/ipcpd/local/main.c | 15 ++++++++++----- src/ipcpd/shim-eth-llc/main.c | 12 ++++++++---- src/ipcpd/shim-udp/main.c | 15 ++++++++++----- src/irmd/main.c | 8 +++++--- 4 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/ipcpd/local/main.c b/src/ipcpd/local/main.c index ffe69dfb..ae75ae5e 100644 --- a/src/ipcpd/local/main.c +++ b/src/ipcpd/local/main.c @@ -585,11 +585,13 @@ int main(int argc, char * argv[]) if (ipcp_parse_arg(argc, argv)) { LOG_ERR("Failed to parse arguments."); - exit(1); + exit(EXIT_FAILURE); } - if (shim_ap_init() < 0) - exit(1); + if (shim_ap_init() < 0) { + close_logfile(); + exit(EXIT_FAILURE); + } /* store the process id of the irmd */ irmd_api = atoi(argv[1]); @@ -608,8 +610,9 @@ int main(int argc, char * argv[]) _ipcp = ipcp_local_create(); if (_ipcp == NULL) { - LOG_ERR("Won't."); - exit(1); + LOG_ERR("Failed to create IPCP."); + close_logfile(); + exit(EXIT_FAILURE); } pthread_rwlock_wrlock(&_ipcp->state_lock); @@ -630,6 +633,8 @@ int main(int argc, char * argv[]) free(_ipcp->ops); free(_ipcp); + close_logfile(); + exit(EXIT_SUCCESS); } diff --git a/src/ipcpd/shim-eth-llc/main.c b/src/ipcpd/shim-eth-llc/main.c index 5e355bd7..4c260301 100644 --- a/src/ipcpd/shim-eth-llc/main.c +++ b/src/ipcpd/shim-eth-llc/main.c @@ -1247,7 +1247,7 @@ int main(int argc, char * argv[]) if (ipcp_parse_arg(argc, argv)) { LOG_ERR("Failed to parse arguments."); - exit(1); + exit(EXIT_FAILURE); } /* store the process id of the irmd */ @@ -1268,14 +1268,16 @@ int main(int argc, char * argv[]) _ipcp = ipcp_instance_create(); if (_ipcp == NULL) { LOG_ERR("Failed to create instance."); - exit(1); + close_logfile(); + exit(EXIT_FAILURE); } _ipcp->data = (struct ipcp_data *) eth_llc_ipcp_data_create(); if (_ipcp->data == NULL) { LOG_ERR("Failed to create instance data."); free(_ipcp); - exit(1); + close_logfile(); + exit(EXIT_FAILURE); } for (i = 0; i < AP_MAX_FLOWS; i++) { @@ -1304,5 +1306,7 @@ int main(int argc, char * argv[]) free(_ipcp); - exit(0); + close_logfile(); + + exit(EXIT_SUCCESS); } diff --git a/src/ipcpd/shim-udp/main.c b/src/ipcpd/shim-udp/main.c index 92204970..ddf4e80e 100644 --- a/src/ipcpd/shim-udp/main.c +++ b/src/ipcpd/shim-udp/main.c @@ -1569,11 +1569,13 @@ int main(int argc, char * argv[]) if (ipcp_parse_arg(argc, argv)) { LOG_ERR("Failed to parse arguments."); - exit(1); + exit(EXIT_FAILURE); } - if (shim_ap_init() < 0) - exit(1); + if (shim_ap_init() < 0) { + close_logfile(); + exit(EXIT_FAILURE); + } /* store the process id of the irmd */ irmd_api = atoi(argv[1]); @@ -1592,8 +1594,9 @@ int main(int argc, char * argv[]) _ipcp = ipcp_udp_create(); if (_ipcp == NULL) { - LOG_ERR("Won't."); - exit(1); + LOG_ERR("Failed to create IPCP."); + close_logfile(); + exit(EXIT_FAILURE); } pthread_rwlock_wrlock(&_ipcp->state_lock); @@ -1614,6 +1617,8 @@ int main(int argc, char * argv[]) free(_ipcp->ops); free(_ipcp); + close_logfile(); + exit(EXIT_SUCCESS); } diff --git a/src/irmd/main.c b/src/irmd/main.c index 62673251..1f72fa1a 100644 --- a/src/irmd/main.c +++ b/src/irmd/main.c @@ -1704,7 +1704,7 @@ static struct irm * irm_create() if (dum == NULL) { LOG_ERR("Could not examine existing shm file."); free(instance); - exit(EXIT_FAILURE); + return NULL; } if (kill(shm_du_map_owner(dum), 0) < 0) { @@ -1716,7 +1716,7 @@ static struct irm * irm_create() shm_du_map_owner(dum)); shm_du_map_close(dum); free(instance); - exit(EXIT_SUCCESS); + return NULL; } shm_du_map_close(dum); @@ -1879,8 +1879,10 @@ int main(int argc, char ** argv) exit(EXIT_FAILURE); instance = irm_create(); - if (instance == NULL) + if (instance == NULL) { + close_logfile(); exit(EXIT_FAILURE); + } for (t = 0; t < IRMD_THREADPOOL_SIZE; ++t) pthread_create(&instance->threadpool[t], NULL, mainloop, NULL); -- cgit v1.2.3