From 5e50f072478167a7e2027f39d4c8925c9c4fefde Mon Sep 17 00:00:00 2001 From: erkin Date: Tue, 26 Jan 2021 22:29:04 +0300 Subject: op-mode: T3110: Gracefully handle SIGPIPE in show-interfaces --- src/op_mode/show_interfaces.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'src/op_mode/show_interfaces.py') diff --git a/src/op_mode/show_interfaces.py b/src/op_mode/show_interfaces.py index de41274a7..256c86d2a 100755 --- a/src/op_mode/show_interfaces.py +++ b/src/op_mode/show_interfaces.py @@ -35,14 +35,23 @@ glob_ifnames = '/sys/class/net/({})*'.format('|'.join(interfaces)) actions = {} -def register (name): +def register(name): """ - decorator to register a function into actions with a name - it allows to use actions[name] to call the registered function + Decorator to register a function into actions with a name. + `actions[name]' can be used to call the registered functions. + We wrap each function in a SIGPIPE handler as all registered functions + can be subject to a broken pipe if there are a lot of interfaces. """ def _register(function): - actions[name] = function - return function + def handled_function(*args, **kwargs): + try: + function(*args, **kwargs) + except BrokenPipeError: + # Flush output to /dev/null and bail out. + os.dup2(os.open(os.devnull, os.O_WRONLY), sys.stdout.fileno()) + sys.exit(1) + actions[name] = handled_function + return handled_function return _register -- cgit v1.2.3