Skip to content

Commit aee8250

Browse files
committed
Fixed xcvrd shutdown flow.
Signed-off-by: Nazarii Hnydyn <[email protected]>
1 parent 3c6a57a commit aee8250

3 files changed

Lines changed: 391 additions & 200 deletions

File tree

  • sonic-ledd/scripts
  • sonic-psud/scripts
  • sonic-xcvrd/scripts

sonic-ledd/scripts/ledd

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
"""
44
ledd
5-
65
Front-panel LED control daemon for SONiC
76
"""
87

@@ -15,6 +14,7 @@ try:
1514
import sys
1615
import syslog
1716
from swsscommon import swsscommon
17+
from sonic_daemon_base.daemon_base import Logger
1818
from sonic_daemon_base.daemon_base import DaemonBase
1919
except ImportError, e:
2020
raise ImportError (str(e) + " - required module not found")
@@ -35,15 +35,19 @@ Options:
3535

3636
LED_MODULE_NAME = "led_control"
3737
LED_CLASS_NAME = "LedControl"
38+
3839
SELECT_TIMEOUT = 1000
3940

41+
LEDUTIL_LOAD_ERROR = 1
42+
DAEMON_INIT_ERROR = 2
43+
44+
logger = Logger(SYSLOG_IDENTIFIER)
45+
4046
class DaemonLedd(DaemonBase):
4147
def __init__(self):
4248
DaemonBase.__init__(self)
4349

44-
def __exit__(self):
45-
DaemonBase.__exit__(self)
46-
50+
# Run daemon
4751
def run(self):
4852
# Parse options if provided
4953
if (len(sys.argv) > 1):
@@ -67,11 +71,11 @@ class DaemonLedd(DaemonBase):
6771
# Load platform-specific LedControl module
6872
led_control = self.load_platform_util(LED_MODULE_NAME, LED_CLASS_NAME)
6973
if not led_control:
70-
self.log_error("failed to load ledutil")
71-
sys.exit(1)
74+
logger.log_error("Failed to load ledutil", True)
75+
sys.exit(LEDUTIL_LOAD_ERROR)
7276

7377
# Open a handle to the Application database
74-
appl_db = self.db_connect(swsscommon.APPL_DB)
78+
appl_db = DaemonBase.db_connect(swsscommon.APPL_DB)
7579

7680
# Subscribe to PORT table notifications in the Application DB
7781
sel = swsscommon.Select()
@@ -88,7 +92,7 @@ class DaemonLedd(DaemonBase):
8892
# Do not flood log when select times out
8993
continue
9094
if state != swsscommon.Select.OBJECT:
91-
self.log_warning("sel.select() did not return swsscommon.Select.OBJECT")
95+
logger.log_warning("sel.select() did not return swsscommon.Select.OBJECT")
9296
continue
9397

9498
(key, op, fvp) = sst.pop()
@@ -107,15 +111,15 @@ class DaemonLedd(DaemonBase):
107111

108112
def main():
109113
if not os.geteuid() == 0:
110-
print "Error: Must be root to run this daemon"
114+
logger.log_error("Error: Must be root to run this daemon")
111115
sys.exit(1)
112116

113-
daemon_ledd = DaemonLedd()
114-
if not daemon_ledd:
115-
print "Failed to instantiate LED daemon"
116-
sys.exit(1)
117+
ledd = DaemonLedd()
118+
if not ledd:
119+
logger.log_error("Failed to instantiate LED daemon")
120+
sys.exit(DAEMON_INIT_ERROR)
117121

118-
daemon_ledd.run()
122+
ledd.run()
119123

120124
if __name__ == '__main__':
121125
main()

sonic-psud/scripts/psud

Lines changed: 76 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,66 +12,117 @@
1212
try:
1313
import sys
1414
import time
15+
import signal
16+
import threading
1517
from swsscommon import swsscommon
18+
from sonic_daemon_base.daemon_base import Logger
1619
from sonic_daemon_base.daemon_base import DaemonBase
1720
except ImportError, e:
1821
raise ImportError (str(e) + " - required module not found")
1922

20-
#============================= Constants =============================
23+
#
24+
# Constants ====================================================================
25+
#
26+
27+
SYSLOG_IDENTIFIER = "psud"
2128

2229
PLATFORM_SPECIFIC_MODULE_NAME = "psuutil"
2330
PLATFORM_SPECIFIC_CLASS_NAME = "PsuUtil"
2431

32+
CHASSIS_INFO_NUM_PSUS_FIELD = 'num_psus'
33+
34+
PSU_INFO_PRESENCE_FIELD = 'presence'
35+
PSU_INFO_STATUS_FIELD = 'status'
36+
2537
PSU_INFO_UPDATE_PERIOD_SECS = 3
2638

39+
PSUUTIL_LOAD_ERROR = 1
40+
DAEMON_INIT_ERROR = 2
41+
42+
logger = Logger(SYSLOG_IDENTIFIER)
43+
44+
#
45+
# Helper functions =============================================================
46+
#
47+
48+
def psu_db_update(psuutil, psu_tbl, num_psus):
49+
for psu_index in range(1, num_psus + 1):
50+
fvs = swsscommon.FieldValuePairs([(PSU_INFO_PRESENCE_FIELD,
51+
'true' if psuutil.get_psu_presence(psu_index) else 'false'),
52+
(PSU_INFO_STATUS_FIELD,
53+
'true' if psuutil.get_psu_status(psu_index) else 'false')])
54+
psu_tbl.set("PSU {}".format(psu_index), fvs)
55+
56+
#
57+
# Daemon =======================================================================
58+
#
59+
2760
class DaemonPsud(DaemonBase):
2861
def __init__(self):
2962
DaemonBase.__init__(self)
3063

31-
def __exit__(self):
32-
DaemonBase.__exit__(self)
64+
self.stop = threading.Event()
3365

66+
# Signal handler
67+
def signal_handler(self, sig, frame):
68+
if sig == signal.SIGHUP:
69+
logger.log_info("Caught SIGHUP - ignoring...")
70+
elif sig == signal.SIGINT:
71+
logger.log_info("Caught SIGINT - exiting...")
72+
self.stop.set()
73+
elif sig == signal.SIGTERM:
74+
logger.log_info("Caught SIGTERM - exiting...")
75+
self.stop.set()
76+
else:
77+
logger.log_warning("Caught unhandled signal '" + sig + "'")
78+
79+
# Run daemon
3480
def run(self):
81+
logger.log_info("Starting up...")
82+
3583
# Load platform-specific psuutil class
3684
platform_psuutil = self.load_platform_util(PLATFORM_SPECIFIC_MODULE_NAME, PLATFORM_SPECIFIC_CLASS_NAME)
3785
if not platform_psuutil:
38-
self.log_error("failed to load psuutil")
39-
sys.exit(1)
86+
logger.log_error("Failed to load psuutil", True)
87+
sys.exit(PSUUTIL_LOAD_ERROR)
4088

41-
state_db = self.db_connect(swsscommon.STATE_DB)
89+
# Connect to STATE_DB and create psu/chassis info tables
90+
state_db = DaemonBase.db_connect(swsscommon.STATE_DB)
4291
psu_tbl = swsscommon.Table(state_db, "PSU_INFO")
4392
chassis_tbl = swsscommon.Table(state_db, "CHASSIS_INFO")
93+
94+
# Post psu number info to STATE_DB
4495
num_psus = platform_psuutil.get_num_psus()
45-
fvs = swsscommon.FieldValuePairs([('num_psus', str(num_psus))])
96+
fvs = swsscommon.FieldValuePairs([(CHASSIS_INFO_NUM_PSUS_FIELD, str(num_psus))])
4697
chassis_tbl.set('chassis 1', fvs)
4798

48-
# Start main loop to listen to the PSU change event.
49-
self.log_info("Start main loop")
50-
while True:
99+
# Start main loop
100+
logger.log_info("Start daemon main loop")
101+
102+
while not self.stop.wait(PSU_INFO_UPDATE_PERIOD_SECS):
51103
psu_db_update(platform_psuutil, psu_tbl, num_psus)
52-
time.sleep(PSU_INFO_UPDATE_PERIOD_SECS)
53104

54-
# Clean all the information from DB and then exit
105+
logger.log_info("Stop daemon main loop")
106+
107+
# Delete all the information from DB and then exit
55108
for psu_index in range(1, num_psus + 1):
56109
psu_tbl._del("PSU {}".format(psu_index))
110+
57111
chassis_tbl._del('chassis 1')
58-
return 1
59112

60-
def psu_db_update(psuutil, psu_tbl, num_psus):
61-
for psu_index in range(1, num_psus + 1):
62-
fvs = swsscommon.FieldValuePairs([('presence',
63-
'true' if psuutil.get_psu_presence(psu_index) else 'false'),
64-
('status',
65-
'true' if psuutil.get_psu_status(psu_index) else 'false')])
66-
psu_tbl.set("PSU {}".format(psu_index), fvs)
113+
logger.log_info("Shutting down...")
114+
115+
#
116+
# Main =========================================================================
117+
#
67118

68119
def main():
69-
daemon_psud = DaemonPsud()
70-
if not daemon_psud:
71-
print "Failed to load psu daemon utilities"
72-
sys.exit(1)
120+
psud = DaemonPsud()
121+
if not psud:
122+
logger.log_error("Failed to instantiate PSU daemon")
123+
sys.exit(DAEMON_INIT_ERROR)
73124

74-
daemon_psud.run()
125+
psud.run()
75126

76127
if __name__ == '__main__':
77128
main()

0 commit comments

Comments
 (0)