File 0003-fence_sbd-Check-if-the-sbd-daemon-is-running-before-.patch of Package fence-agents.37578

From b0700d8f43fe64750591da888da68e0f84767d13 Mon Sep 17 00:00:00 2001
From: xin liang <xliang@suse.com>
Date: Tue, 10 Dec 2024 10:00:00 +0800
Subject: [PATCH 3/3] fence_sbd: Check if the sbd daemon is running before
 using SBD_DEVICE enviroment variable

And add @SBDPID_PATH@ for the sbd daemon pid file path
---
 agents/sbd/fence_sbd.py | 31 ++++++++++++++++++++++++++++++-
 configure.ac            |  2 ++
 make/fencebuild.mk      |  1 +
 3 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/agents/sbd/fence_sbd.py b/agents/sbd/fence_sbd.py
index a0084ac2..e4221531 100644
--- a/agents/sbd/fence_sbd.py
+++ b/agents/sbd/fence_sbd.py
@@ -14,6 +14,7 @@ DEVICE_INIT = 1
 DEVICE_NOT_INIT = -3
 PATH_NOT_EXISTS = -1
 PATH_NOT_BLOCK = -2
+SBD_PID_FILE = "@SBDPID_PATH@"
 
 def is_block_device(filename):
     """Checks if a given path is a valid block device
@@ -356,6 +357,34 @@ Comma separated list of sbd devices",
         "order": 200
         }
 
+
+def sbd_daemon_is_running():
+    """Check if the sbd daemon is running
+    """
+    if not os.path.exists(SBD_PID_FILE):
+        logging.info("SBD PID file %s does not exist", SBD_PID_FILE)
+        return False
+
+    try:
+        with open(SBD_PID_FILE, "r") as pid_file:
+            pid = int(pid_file.read().strip())
+    except Exception as e:
+        logging.error("Failed to read PID file %s: %s", SBD_PID_FILE, e)
+        return False
+
+    try:
+        # send signal 0 to check if the process is running
+        os.kill(pid, 0)
+    except ProcessLookupError:
+        logging.info("SBD daemon is not running")
+        return False
+    except Exception as e:
+        logging.error("Failed to send signal 0 to PID %d: %s", pid, e)
+        return False
+
+    return True
+
+
 def main():
     """Main function
     """
@@ -385,7 +414,7 @@ which can be used in environments where sbd can be used (shared storage)."
     # If not specified then read SBD_DEVICE from environment
     if "--devices" not in options:
         dev_list = os.getenv("SBD_DEVICE")
-        if dev_list:
+        if dev_list and sbd_daemon_is_running():
             options["--devices"] = ",".join(dev_list.split(";"))
         else:
             fail_usage("No SBD devices specified. \
diff --git a/configure.ac b/configure.ac
index 13615113..fd412347 100644
--- a/configure.ac
+++ b/configure.ac
@@ -145,6 +145,8 @@ eval FENCETMPDIR="`eval echo ${FENCETMPDIR}`"
 AC_DEFINE_UNQUOTED(FENCETMPDIR,"$FENCETMPDIR", Where Fence agents keep state files)
 AC_SUBST(FENCETMPDIR)
 
+SBDPID_PATH=${localstatedir}/run/sbd.pid
+AC_SUBST(SBDPID_PATH)
 
 if test "x$AGENTS_LIST" = x; then
 	AC_ERROR([No agents selected])
diff --git a/make/fencebuild.mk b/make/fencebuild.mk
index 9a3c6d6d..bc925919 100644
--- a/make/fencebuild.mk
+++ b/make/fencebuild.mk
@@ -9,6 +9,7 @@ define gen_agent_from_py
 		-e 's#@''SBINDIR@#${sbindir}#g' \
 		-e 's#@''LIBEXECDIR@#${libexecdir}#g' \
 		-e 's#@''FENCETMPDIR@#${FENCETMPDIR}#g' \
+		-e 's#@''SBDPID_PATH@#${SBDPID_PATH}#g' \
 		-e 's#@''IPMITOOL_PATH@#${IPMITOOL_PATH}#g' \
 		-e 's#@''OPENSTACK_PATH@#${OPENSTACK_PATH}#g' \
 		-e 's#@''AMTTOOL_PATH@#${AMTTOOL_PATH}#g' \
-- 
2.45.2

openSUSE Build Service is sponsored by