From 763227aee4a3d86e718c3665fe5fa13055f67f03 Mon Sep 17 00:00:00 2001 From: Dimitri Staessens Date: Fri, 8 Jul 2022 16:55:51 +0200 Subject: ipcpd: Refactor main functions The structure of main functions of the IPCPs was a bit strange with a ipcp_shutdown() call that combined waiting for a terminating signal with stopping the internal threads. This is now revised into a symmetrical design of ipcp_start(), which now includes the create response towards the IRMd. ipcp_sigwait(), which waits for a shutdown signal ipcp_stop() that then stops the internal threads. Now the main() functions of the IPCPs will make sense without checking what that ipcp_shutdown() functions actually does. Signed-off-by: Dimitri Staessens Signed-off-by: Sander Vrijders --- src/ipcpd/broadcast/main.c | 19 +++++----------- src/ipcpd/eth/eth.c | 21 ++++++----------- src/ipcpd/ipcp.c | 57 ++++++++++++++++++++++++++++++---------------- src/ipcpd/ipcp.h | 6 +++-- src/ipcpd/local/main.c | 22 +++++++----------- src/ipcpd/udp/main.c | 22 +++++++----------- src/ipcpd/unicast/main.c | 23 +++++++------------ 7 files changed, 78 insertions(+), 92 deletions(-) diff --git a/src/ipcpd/broadcast/main.c b/src/ipcpd/broadcast/main.c index fdd3ca99..4dc2f877 100644 --- a/src/ipcpd/broadcast/main.c +++ b/src/ipcpd/broadcast/main.c @@ -295,24 +295,20 @@ int main(int argc, goto fail_enroll_init; } - if (ipcp_boot() < 0) { + if (ipcp_start() < 0) { log_err("Failed to boot IPCP."); - goto fail_boot; - } - - if (ipcp_create_r(0)) { - log_err("Failed to notify IRMd we are initialized."); - ipcp_set_state(IPCP_NULL); - goto fail_create_r; + goto fail_start; } - ipcp_shutdown(); + ipcp_sigwait(); if (ipcp_get_state() == IPCP_SHUTDOWN) { stop_components(); finalize_components(); } + ipcp_stop(); + enroll_fini(); connmgr_fini(); @@ -323,9 +319,7 @@ int main(int argc, exit(EXIT_SUCCESS); - fail_create_r: - ipcp_shutdown(); - fail_boot: + fail_start: enroll_fini(); fail_enroll_init: connmgr_fini(); @@ -334,6 +328,5 @@ int main(int argc, fail_notifier_init: ipcp_fini(); fail_init: - ipcp_create_r(-1); exit(EXIT_FAILURE); } diff --git a/src/ipcpd/eth/eth.c b/src/ipcpd/eth/eth.c index 294373b6..3a27d2c6 100644 --- a/src/ipcpd/eth/eth.c +++ b/src/ipcpd/eth/eth.c @@ -1849,18 +1849,12 @@ int main(int argc, goto fail_data_init; } - if (ipcp_boot() < 0) { - log_err("Failed to boot IPCP."); - goto fail_boot; + if (ipcp_start() < 0) { + log_err("Failed to start IPCP."); + goto fail_start; } - if (ipcp_create_r(0)) { - log_err("Failed to notify IRMd we are initialized."); - ipcp_set_state(IPCP_NULL); - goto fail_create_r; - } - - ipcp_shutdown(); + ipcp_sigwait(); if (ipcp_get_state() == IPCP_SHUTDOWN) { for (i = 0; i < IPCP_ETH_WR_THR; ++i) @@ -1883,19 +1877,18 @@ int main(int argc, #endif } + ipcp_stop(); + ipcp_fini(); eth_data_fini(); exit(EXIT_SUCCESS); - fail_create_r: - ipcp_shutdown(); - fail_boot: + fail_start: eth_data_fini(); fail_data_init: ipcp_fini(); fail_init: - ipcp_create_r(-1); exit(EXIT_FAILURE); } diff --git a/src/ipcpd/ipcp.c b/src/ipcpd/ipcp.c index eefa72af..395e9b78 100644 --- a/src/ipcpd/ipcp.c +++ b/src/ipcpd/ipcp.c @@ -43,6 +43,7 @@ #include #include #include +#include #include #include #include @@ -624,7 +625,7 @@ static void * mainloop(void * o) default: ret_msg.has_result = true; ret_msg.result = -1; - log_err("Don't know that message code"); + log_err("Unknown message code: %d.", msg->code); break; } exit: @@ -764,6 +765,15 @@ int ipcp_init(int argc, goto fail_rib_init; } + if (rib_reg(IPCP_INFO, &r_ops)) + goto fail_rib_reg; + + ipcpi.tpm = tpm_create(IPCP_MIN_THREADS, IPCP_ADD_THREADS, + mainloop, NULL); + if (ipcpi.tpm == NULL) + goto fail_tpm_create; + + list_head_init(&ipcpi.cmds); ipcpi.alloc_id = -1; @@ -772,6 +782,10 @@ int ipcp_init(int argc, return 0; + fail_tpm_create: + rib_unreg(IPCP_INFO); + fail_rib_reg: + rib_fini(); fail_rib_init: pthread_cond_destroy(&ipcpi.cmd_cond); fail_cmd_cond: @@ -794,7 +808,7 @@ int ipcp_init(int argc, return ret; } -int ipcp_boot() +int ipcp_start() { sigset_t sigset; sigemptyset(&sigset); @@ -803,11 +817,6 @@ int ipcp_boot() sigaddset(&sigset, SIGHUP); sigaddset(&sigset, SIGPIPE); - ipcpi.tpm = tpm_create(IPCP_MIN_THREADS, IPCP_ADD_THREADS, - mainloop, NULL); - if (ipcpi.tpm == NULL) - goto fail_tpm_create; - pthread_sigmask(SIG_BLOCK, &sigset, NULL); if (tpm_start(ipcpi.tpm)) @@ -815,29 +824,31 @@ int ipcp_boot() ipcp_set_state(IPCP_INIT); - if (rib_reg(IPCP_INFO, &r_ops)) - goto fail_rib_reg; - if (pthread_create(&ipcpi.acceptor, NULL, acceptloop, NULL)) { log_err("Failed to create acceptor thread."); - ipcp_set_state(IPCP_NULL); goto fail_acceptor; } - return 0; + if (ipcp_create_r(0)) { + log_err("Failed to notify IRMd we are initialized."); + goto fail_create_r; + } + return 0; + fail_create_r: + pthread_cancel(ipcpi.acceptor); + pthread_join(ipcpi.acceptor, NULL); fail_acceptor: - rib_unreg(IPCP_INFO); - fail_rib_reg: + ipcp_set_state(IPCP_NULL); tpm_stop(ipcpi.tpm); fail_tpm_start: tpm_destroy(ipcpi.tpm); - fail_tpm_create: + ipcp_create_r(-1); return -1; } -void ipcp_shutdown() +void ipcp_sigwait() { siginfo_t info; @@ -891,19 +902,25 @@ void ipcp_shutdown() continue; } } +} - pthread_cancel(ipcpi.acceptor); +void ipcp_stop() +{ + log_info("IPCP %d shutting down.", getpid()); + pthread_cancel(ipcpi.acceptor); pthread_join(ipcpi.acceptor, NULL); - tpm_stop(ipcpi.tpm); - tpm_destroy(ipcpi.tpm); - log_info("IPCP %d shutting down.", getpid()); + tpm_stop(ipcpi.tpm); } void ipcp_fini() { + tpm_destroy(ipcpi.tpm); + + rib_unreg(IPCP_INFO); + rib_fini(); close(ipcpi.sockfd); diff --git a/src/ipcpd/ipcp.h b/src/ipcpd/ipcp.h index 79b7b7c0..5d1ae546 100644 --- a/src/ipcpd/ipcp.h +++ b/src/ipcpd/ipcp.h @@ -120,9 +120,11 @@ int ipcp_init(int argc, struct ipcp_ops * ops, enum ipcp_type type); -int ipcp_boot(void); +int ipcp_start(void); -void ipcp_shutdown(void); +void ipcp_sigwait(void); + +void ipcp_stop(void); void ipcp_fini(void); diff --git a/src/ipcpd/local/main.c b/src/ipcpd/local/main.c index 15b6128b..b6aaa81b 100644 --- a/src/ipcpd/local/main.c +++ b/src/ipcpd/local/main.c @@ -360,36 +360,30 @@ int main(int argc, goto fail_data_init; } - if (ipcp_boot() < 0) { - log_err("Failed to boot IPCP."); - goto fail_boot; + if (ipcp_start() < 0) { + log_err("Failed to start IPCP."); + goto fail_start; } - if (ipcp_create_r(0)) { - log_err("Failed to notify IRMd we are initialized."); - goto fail_create_r; - } - - ipcp_shutdown(); + ipcp_sigwait(); if (ipcp_get_state() == IPCP_SHUTDOWN) { pthread_cancel(local_data.packet_loop); pthread_join(local_data.packet_loop, NULL); } + ipcp_stop(); + ipcp_fini(); local_data_fini(); exit(EXIT_SUCCESS); - fail_create_r: - ipcp_set_state(IPCP_NULL); - ipcp_shutdown(); - fail_boot: + + fail_start: local_data_fini(); fail_data_init: ipcp_fini(); fail_init: - ipcp_create_r(-1); exit(EXIT_FAILURE); } diff --git a/src/ipcpd/udp/main.c b/src/ipcpd/udp/main.c index 9960f880..522a2b16 100644 --- a/src/ipcpd/udp/main.c +++ b/src/ipcpd/udp/main.c @@ -1137,17 +1137,12 @@ int main(int argc, goto fail_data_init; } - if (ipcp_boot() < 0) { - log_err("Failed to boot IPCP."); - goto fail_boot; + if (ipcp_start() < 0) { + log_err("Failed to start IPCP."); + goto fail_start; } - if (ipcp_create_r(0)) { - log_err("Failed to notify IRMd we are initialized."); - goto fail_create_r; - } - - ipcp_shutdown(); + ipcp_sigwait(); if (ipcp_get_state() == IPCP_SHUTDOWN) { for (i = 0; i < IPCP_UDP_RD_THR; ++i) @@ -1163,19 +1158,18 @@ int main(int argc, pthread_join(udp_data.mgmt_handler, NULL); } + ipcp_stop(); + ipcp_fini(); udp_data_fini(); exit(EXIT_SUCCESS); - fail_create_r: - ipcp_set_state(IPCP_NULL); - ipcp_shutdown(); - fail_boot: + + fail_start: udp_data_fini(); fail_data_init: ipcp_fini(); fail_init: - ipcp_create_r(-1); exit(EXIT_FAILURE); } diff --git a/src/ipcpd/unicast/main.c b/src/ipcpd/unicast/main.c index b56c218c..e3d1c65c 100644 --- a/src/ipcpd/unicast/main.c +++ b/src/ipcpd/unicast/main.c @@ -327,24 +327,20 @@ int main(int argc, goto fail_enroll_init; } - if (ipcp_boot() < 0) { - log_err("Failed to boot IPCP."); - goto fail_boot; - } - - if (ipcp_create_r(0)) { - log_err("Failed to notify IRMd we are initialized."); - ipcp_set_state(IPCP_NULL); - goto fail_create_r; + if (ipcp_start() < 0) { + log_err("Failed to start IPCP."); + goto fail_start; } - ipcp_shutdown(); + ipcp_sigwait(); if (ipcp_get_state() == IPCP_SHUTDOWN) { stop_components(); finalize_components(); } + ipcp_stop(); + enroll_fini(); connmgr_fini(); @@ -355,17 +351,14 @@ int main(int argc, exit(EXIT_SUCCESS); - fail_create_r: - ipcp_shutdown(); - fail_boot: + fail_start: enroll_fini(); fail_enroll_init: connmgr_fini(); fail_connmgr_init: notifier_fini(); fail_notifier_init: - ipcp_fini(); + ipcp_fini(); fail_init: - ipcp_create_r(-1); exit(EXIT_FAILURE); } -- cgit v1.2.3