From 4fb81107bfad2fe4cbea00cb87e1b6fbb92bd869 Mon Sep 17 00:00:00 2001 From: dimitri staessens Date: Sun, 30 Oct 2016 22:33:17 +0100 Subject: ipcpd: Clean shutdown when bootstrap fails Fixes a double free (conf->dif_name) and the handling of non-created threads when the bootstrap of an IPCP fails --- src/ipcpd/shim-eth-llc/main.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'src/ipcpd/shim-eth-llc/main.c') diff --git a/src/ipcpd/shim-eth-llc/main.c b/src/ipcpd/shim-eth-llc/main.c index b7b9f783..6dcd07ef 100644 --- a/src/ipcpd/shim-eth-llc/main.c +++ b/src/ipcpd/shim-eth-llc/main.c @@ -673,7 +673,11 @@ void ipcp_sig_handler(int sig, siginfo_t * info, void * c) pthread_rwlock_wrlock(&ipcpi.state_lock); - ipcp_set_state(IPCP_SHUTDOWN); + if (ipcp_get_state() == IPCP_INIT) + ipcp_set_state(IPCP_NULL); + + if (ipcp_get_state() == IPCP_ENROLLED) + ipcp_set_state(IPCP_SHUTDOWN); pthread_rwlock_unlock(&ipcpi.state_lock); } @@ -702,9 +706,6 @@ static int eth_llc_ipcp_bootstrap(struct dif_config * conf) assert(conf); assert(conf->type == THIS_TYPE); - /* this IPCP doesn't need to maintain its dif_name */ - free(conf->dif_name); - if (conf->if_name == NULL) { LOG_ERR("Interface name is NULL."); return -1; @@ -831,7 +832,6 @@ static int eth_llc_ipcp_bootstrap(struct dif_config * conf) if (ipcp_get_state() != IPCP_INIT) { pthread_rwlock_unlock(&ipcpi.state_lock); LOG_ERR("IPCP in wrong state."); - close(skfd); return -1; } @@ -855,6 +855,9 @@ static int eth_llc_ipcp_bootstrap(struct dif_config * conf) pthread_rwlock_unlock(&ipcpi.state_lock); + /* this IPCP doesn't need to maintain its dif_name */ + free(conf->dif_name); + LOG_DBG("Bootstrapped shim IPCP over Ethernet with LLC with api %d.", getpid()); @@ -1135,11 +1138,12 @@ int main(int argc, char * argv[]) ipcp_fini(); - pthread_cancel(eth_llc_data.sdu_reader); - pthread_cancel(eth_llc_data.sdu_writer); - - pthread_join(eth_llc_data.sdu_writer, NULL); - pthread_join(eth_llc_data.sdu_reader, NULL); + if (ipcp_get_state() == IPCP_SHUTDOWN) { + pthread_cancel(eth_llc_data.sdu_reader); + pthread_cancel(eth_llc_data.sdu_writer); + pthread_join(eth_llc_data.sdu_writer, NULL); + pthread_join(eth_llc_data.sdu_reader, NULL); + } eth_llc_data_fini(); -- cgit v1.2.3