From a853f3b8f8a924b240407bebc4ec1401f8e4019d Mon Sep 17 00:00:00 2001 From: Taras Keryk Date: Wed, 13 Apr 2022 05:57:32 -0700 Subject: [PATCH 1/3] [BFN] Fixed SONiC fwutil exec time Signed-off-by: Taras Keryk --- .../sonic_platform/component.py | 2 ++ .../sonic_platform/platform_utils.py | 33 ++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/component.py b/platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/component.py index 47a0993bf3e..a7f236cb42a 100644 --- a/platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/component.py +++ b/platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/component.py @@ -6,6 +6,7 @@ import json from collections import OrderedDict from sonic_py_common import device_info + from platform_utils import limit_execution_time except ImportError as e: raise ImportError(str(e) + "- required module not found") @@ -24,6 +25,7 @@ def get_bios_version(): except subprocess.CalledProcessError as e: raise RuntimeError("Failed to get BIOS version") +@limit_execution_time(1) def get_bmc_version(): """ Retrieves the firmware version of the BMC diff --git a/platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/platform_utils.py b/platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/platform_utils.py index f6ceeddcf09..62152ec16c9 100644 --- a/platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/platform_utils.py +++ b/platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/platform_utils.py @@ -10,6 +10,9 @@ raise ImportError(str(e) + "- required module not found") def file_create(path, mode=None): + """ + Checks if a file has been created with the appropriate permissions + """ def run_cmd(cmd): if os.geteuid() != 0: cmd.insert(0, 'sudo') @@ -42,4 +45,32 @@ def handler(sig, frame): finally: signal.signal(signal.SIGTERM, sigterm_handler) return result - return wrapper \ No newline at end of file + return wrapper + +def limit_execution_time(execution_time_secs: int): + """ + Wrapper for a function whose execution time must be limited + Args: + execution_time_secs: maximum execution time in seconds, + after which the function execution will be stopped + """ + def wrapper(func): + @wraps(func) + def execution_func(*args, **kwargs): + def handler(): + raise + + signal.signal(signal.SIGALRM, handler) + signal.alarm(execution_time_secs) + result = None + try: + result = func(*args, **kwargs) + except Exception: + raise + finally: + signal.alarm(0) + + return result + return execution_func + return wrapper + From d9d7778c60dfc52cd2ab493702d4ccdfc1390e59 Mon Sep 17 00:00:00 2001 From: Taras Keryk Date: Wed, 13 Apr 2022 07:17:14 -0700 Subject: [PATCH 2/3] Fixed comments Signed-off-by: Taras Keryk --- .../sonic_platform/platform_utils.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/platform_utils.py b/platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/platform_utils.py index 62152ec16c9..67c3ce3a760 100644 --- a/platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/platform_utils.py +++ b/platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/platform_utils.py @@ -11,7 +11,10 @@ def file_create(path, mode=None): """ - Checks if a file has been created with the appropriate permissions + Ensure that file is created with the appropriate permissions + Args: + path: full path of a file + mode: file permission in octal representation """ def run_cmd(cmd): if os.geteuid() != 0: @@ -23,7 +26,7 @@ def run_cmd(cmd): run_cmd(['mkdir', '-p', file_path]) if not os.path.isfile(path): run_cmd(['touch', path]) - if (mode is not None): + if (mode is not None): run_cmd(['chmod', mode, path]) def cancel_on_sigterm(func): From 6b1ff45c77afaaee312983826f90bb0cc6beacd9 Mon Sep 17 00:00:00 2001 From: Taras Keryk Date: Mon, 9 May 2022 09:38:14 -0700 Subject: [PATCH 3/3] Updated limit_execution_time Signed-off-by: Taras Keryk --- .../sonic_platform/platform_utils.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/platform_utils.py b/platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/platform_utils.py index 67c3ce3a760..2f7b5aecb6d 100644 --- a/platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/platform_utils.py +++ b/platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/platform_utils.py @@ -60,16 +60,17 @@ def limit_execution_time(execution_time_secs: int): def wrapper(func): @wraps(func) def execution_func(*args, **kwargs): - def handler(): - raise + def handler(sig, frame): + if sigalrm_handler: + sigalrm_handler(sig, frame) + raise Exception("Canceling {}() execution...".format(func.__name__)) + sigalrm_handler = signal.getsignal(signal.SIGALRM) signal.signal(signal.SIGALRM, handler) signal.alarm(execution_time_secs) result = None try: result = func(*args, **kwargs) - except Exception: - raise finally: signal.alarm(0)