From e54948a5a18f33b7b99574a72c6b5de8a49c377c Mon Sep 17 00:00:00 2001 From: Sabareesh-Kumar-Anandan <59681634+Sabareesh-Kumar-Anandan@users.noreply.github.com> Date: Fri, 4 Sep 2020 08:49:08 +0530 Subject: [PATCH] [teamsyncd][teammgrd] Graceful exit after receiving SIGTERM (#1407) * [teamsyncd][teammgrd] Graceful exit after receiving SIGTERM Signed-off-by: Sabareesh Kumar Anandan * Update teammgrd.cpp * Update teamsyncd.cpp Co-authored-by: pavel-shirshov --- cfgmgr/teammgrd.cpp | 12 ++++------- teamsyncd/teamsyncd.cpp | 44 +++++++++++++++++------------------------ 2 files changed, 22 insertions(+), 34 deletions(-) diff --git a/cfgmgr/teammgrd.cpp b/cfgmgr/teammgrd.cpp index 60828e038d6f..9a97a3d50250 100644 --- a/cfgmgr/teammgrd.cpp +++ b/cfgmgr/teammgrd.cpp @@ -65,14 +65,8 @@ int main(int argc, char **argv) s.addSelectables(o->getSelectables()); } - while (true) - { - if(received_sigterm) - { - teammgr.cleanTeamProcesses(SIGTERM); - received_sigterm = false; - } - + while (!received_sigterm) + { Selectable *sel; int ret; @@ -91,6 +85,8 @@ int main(int argc, char **argv) auto *c = (Executor *)sel; c->execute(); } + teammgr.cleanTeamProcesses(SIGTERM); + SWSS_LOG_NOTICE("Exiting"); } catch (const exception &e) { diff --git a/teamsyncd/teamsyncd.cpp b/teamsyncd/teamsyncd.cpp index 92f59553a90a..6adedab81d6d 100644 --- a/teamsyncd/teamsyncd.cpp +++ b/teamsyncd/teamsyncd.cpp @@ -32,36 +32,28 @@ int main(int argc, char **argv) /* Register the signal handler for SIGTERM */ signal(SIGTERM, sig_handler); - while (1) + try { - try - { - NetLink netlink; - - netlink.registerGroup(RTNLGRP_LINK); - cout << "Listens to teamd events..." << endl; - netlink.dumpRequest(RTM_GETLINK); - - s.addSelectable(&netlink); - while (true) - { - if(received_sigterm) - { - sync.cleanTeamSync(); - received_sigterm = false; - } + NetLink netlink; + netlink.registerGroup(RTNLGRP_LINK); + cout << "Listens to teamd events..." << endl; + netlink.dumpRequest(RTM_GETLINK); - Selectable *temps; - s.select(&temps, 1000); // block for a second - sync.periodic(); - } - } - catch (const std::exception& e) + s.addSelectable(&netlink); + while (!received_sigterm) { - cout << "Exception \"" << e.what() << "\" had been thrown in deamon" << endl; - return 0; + Selectable *temps; + s.select(&temps, 1000); // block for a second + sync.periodic(); } + sync.cleanTeamSync(); + SWSS_LOG_NOTICE("Received SIGTERM Exiting"); + } + catch (const std::exception& e) + { + cout << "Exception \"" << e.what() << "\" had been thrown in deamon" << endl; + return 0; } - return 1; + return 0; }