From 03951374770da5be620e48768588928fbd809c83 Mon Sep 17 00:00:00 2001 From: David Lamparter Date: Sun, 6 Aug 2017 08:28:16 +0200 Subject: [PATCH] *: centralize some exit cleanup into libfrr Start creating a counterpart to frr_init and frr_late_init. Unfortunately, some daemons don't do any exit handling, this doesn't change that just yet. Signed-off-by: David Lamparter --- bgpd/bgp_main.c | 12 ++++-------- ldpd/ldpd.c | 8 +++----- lib/libfrr.c | 21 +++++++++++++++++++++ lib/libfrr.h | 8 ++++++++ nhrpd/nhrp_main.c | 9 ++------- ospf6d/ospf6_main.c | 10 +++------- zebra/main.c | 8 +++----- 7 files changed, 44 insertions(+), 32 deletions(-) diff --git a/bgpd/bgp_main.c b/bgpd/bgp_main.c index 25a562ed6..a80dff271 100644 --- a/bgpd/bgp_main.c +++ b/bgpd/bgp_main.c @@ -172,6 +172,8 @@ static __attribute__((__noreturn__)) void bgp_exit(int status) /* it only makes sense for this to be called on a clean exit */ assert(status == 0); + frr_early_fini(); + bfd_gbl_exit(); bgp_close(); @@ -214,22 +216,16 @@ static __attribute__((__noreturn__)) void bgp_exit(int status) community_list_terminate(bgp_clist); bgp_vrf_terminate(); - cmd_terminate(); - vty_terminate(); #if ENABLE_BGP_VNC vnc_zebra_destroy(); #endif bgp_zebra_destroy(); - /* reverse bgp_master_init */ - if (bm->master) - thread_master_free(bm->master); - - closezlog(); - list_delete(bm->bgp); memset(bm, 0, sizeof(*bm)); + frr_fini(); + if (bgp_debug_count()) log_memstats_stderr("bgpd"); exit(status); diff --git a/ldpd/ldpd.c b/ldpd/ldpd.c index 80af2b14e..0a586ec1c 100644 --- a/ldpd/ldpd.c +++ b/ldpd/ldpd.c @@ -392,6 +392,8 @@ ldpd_shutdown(void) pid_t pid; int status; + frr_early_fini(); + /* close pipes */ msgbuf_clear(&iev_ldpe->ibuf.w); close(iev_ldpe->ibuf.fd); @@ -423,13 +425,9 @@ ldpd_shutdown(void) vrf_terminate(); access_list_reset(); - cmd_terminate(); - vty_terminate(); ldp_zebra_destroy(); - zprivs_terminate(&ldpd_privs); - thread_master_free(master); - closezlog(); + frr_fini(); exit(0); } diff --git a/lib/libfrr.c b/lib/libfrr.c index a5c87e6ed..255f91ec7 100644 --- a/lib/libfrr.c +++ b/lib/libfrr.c @@ -37,6 +37,8 @@ #include "network.h" DEFINE_HOOK(frr_late_init, (struct thread_master * tm), (tm)) +DEFINE_KOOH(frr_early_fini, (), ()) +DEFINE_KOOH(frr_fini, (), ()) const char frr_sysconfdir[] = SYSCONFDIR; const char frr_vtydir[] = DAEMON_VTY_DIR; @@ -831,3 +833,22 @@ void frr_run(struct thread_master *master) while (thread_fetch(master, &thread)) thread_call(&thread); } + +void frr_early_fini(void) +{ + hook_call(frr_early_fini); +} + +void frr_fini(void) +{ + hook_call(frr_fini); + + /* memory_init -> nothing needed */ + vty_terminate(); + cmd_terminate(); + zprivs_terminate(di->privs); + /* signal_init -> nothing needed */ + thread_master_free(master); + closezlog(); + /* frrmod_init -> nothing needed / hooks */ +} diff --git a/lib/libfrr.h b/lib/libfrr.h index 1710fc9a8..8a15d168a 100644 --- a/lib/libfrr.h +++ b/lib/libfrr.h @@ -104,6 +104,14 @@ extern void frr_run(struct thread_master *master); extern bool frr_zclient_addr(struct sockaddr_storage *sa, socklen_t *sa_len, const char *path); +/* these two are before the protocol daemon does its own shutdown + * it's named this way being the counterpart to frr_late_init */ +DECLARE_KOOH(frr_early_fini, (), ()) +extern void frr_early_fini(void); +/* and these two are after the daemon did its own cleanup */ +DECLARE_KOOH(frr_fini, (), ()) +extern void frr_fini(void); + extern char config_default[256]; extern char frr_zclientpath[256]; extern const char frr_sysconfdir[]; diff --git a/nhrpd/nhrp_main.c b/nhrpd/nhrp_main.c index 012d5cd87..3a7186c1d 100644 --- a/nhrpd/nhrp_main.c +++ b/nhrpd/nhrp_main.c @@ -81,6 +81,7 @@ static void nhrp_sigusr1(void) static void nhrp_request_stop(void) { debugf(NHRP_DEBUG_COMMON, "Exiting..."); + frr_early_fini(); nhrp_shortcut_terminate(); nhrp_nhs_terminate(); @@ -89,15 +90,9 @@ static void nhrp_request_stop(void) evmgr_terminate(); nhrp_vc_terminate(); vrf_terminate(); - /* memory_terminate(); */ - /* vty_terminate(); */ - cmd_terminate(); - /* signal_terminate(); */ - zprivs_terminate(&nhrpd_privs); debugf(NHRP_DEBUG_COMMON, "Done."); - - closezlog(); + frr_fini(); exit(0); } diff --git a/ospf6d/ospf6_main.c b/ospf6d/ospf6_main.c index 28bb956c4..e582737f9 100644 --- a/ospf6d/ospf6_main.c +++ b/ospf6d/ospf6_main.c @@ -82,6 +82,8 @@ static void __attribute__((noreturn)) ospf6_exit(int status) struct listnode *node; struct interface *ifp; + frr_early_fini(); + if (ospf6) ospf6_delete(ospf6); @@ -96,19 +98,13 @@ static void __attribute__((noreturn)) ospf6_exit(int status) ospf6_lsa_terminate(); vrf_terminate(); - vty_terminate(); - cmd_terminate(); if (zclient) { zclient_stop(zclient); zclient_free(zclient); } - if (master) - thread_master_free(master); - - closezlog(); - + frr_fini(); exit(status); } diff --git a/zebra/main.c b/zebra/main.c index 538c2f066..c3a7d3635 100644 --- a/zebra/main.c +++ b/zebra/main.c @@ -125,6 +125,7 @@ static void sigint(void) zlog_notice("Terminating on signal"); + frr_early_fini(); #ifdef HAVE_IRDP irdp_finish(); #endif @@ -147,17 +148,14 @@ static void sigint(void) access_list_reset(); prefix_list_reset(); route_map_finish(); - cmd_terminate(); - vty_terminate(); - zprivs_terminate(&zserv_privs); + list_delete(zebrad.client_list); work_queue_free(zebrad.ribq); if (zebrad.lsp_process_q) work_queue_free(zebrad.lsp_process_q); meta_queue_free(zebrad.mq); - thread_master_free(zebrad.master); - closezlog(); + frr_fini(); exit(0); } -- 2.39.5