From 8a6171fce0abdc84fc1488bb78b40a5046fdc4ef Mon Sep 17 00:00:00 2001
From: Agustin Carranza <agustin.carranza@windriver.com>
Date: Wed, 5 Apr 2023 17:23:15 -0300
Subject: [PATCH] Refresh DB session after swact in fm-subagent

After running a swact command, the snmp pod crashed and generated a
coredump. That behavior was due to an invalid reference for the DB
handler.
This change adds a new function to renew the session after the
first failure (this happens after swact) in the connection.
During the retries, the session in renewed and the reference is
restored. So the coredump is now avoided.

Test plan
PASS: * Deploy a multinode configuration (e.g. AIO-DX).
      * Install the SNMP application.
      * Perform a snmpget/snmpwalk/snmpbulk operation related to FM,
        to the floating IP, e.g. WRS-ALARM-MIB::wrsAlarmActiveAlarmId.1
      * Perform a swact operation.
      * Repeat the snmp operation.
      * Both operations succeed and no coredumps are found in the
        filesystem of all controllers.

Closes-bug: 2015408

Signed-off-by: Agustin Carranza <agustin.carranza@windriver.com>
Change-Id: Idf5e8717c4f9605a3a7c1eaee1eb87e38bc305e4
---
 .../docker/stx-fm-subagent/src/snmpActiveAlarms.c     |  3 ++-
 .../docker/stx-fm-subagent/src/snmpAgentPlugin.c      | 11 ++++++++++-
 .../docker/stx-fm-subagent/src/snmpAgentPlugin.h      |  3 ++-
 .../docker/stx-fm-subagent/src/snmpEventLogs.c        |  3 ++-
 4 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/stx-snmp-helm/docker/stx-fm-subagent/src/snmpActiveAlarms.c b/stx-snmp-helm/docker/stx-fm-subagent/src/snmpActiveAlarms.c
index 2b584ee..d202aaf 100644
--- a/stx-snmp-helm/docker/stx-fm-subagent/src/snmpActiveAlarms.c
+++ b/stx-snmp-helm/docker/stx-fm-subagent/src/snmpActiveAlarms.c
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2020-2021 Wind River Systems, Inc.
+* Copyright (c) 2020-2023 Wind River Systems, Inc.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
@@ -75,6 +75,7 @@ Alarm_Scan_Init()
             DEBUGMSG(("cgtsAgentPlugin", 
             "get_all_alarms returns false (%zu/%d). Try again\n",
              (i+1), retryCount));
+            renewAlarmSession();
         }
     }
     if (!isAlarmObtained) {
diff --git a/stx-snmp-helm/docker/stx-fm-subagent/src/snmpAgentPlugin.c b/stx-snmp-helm/docker/stx-fm-subagent/src/snmpAgentPlugin.c
index e20c38a..a71c1d1 100644
--- a/stx-snmp-helm/docker/stx-fm-subagent/src/snmpAgentPlugin.c
+++ b/stx-snmp-helm/docker/stx-fm-subagent/src/snmpAgentPlugin.c
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2020 Wind River Systems, Inc.
+* Copyright (c) 2020-2023 Wind River Systems, Inc.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
@@ -123,6 +123,15 @@ TFmAlarmSessionT getAlarmSession()
     return alm_handle;
 }
 
+void
+renewAlarmSession(void)
+{
+    fm_snmp_util_destroy_session(alm_handle);
+    if (fm_snmp_util_create_session(&alm_handle, NULL) != true){
+        snmp_log(LOG_ERR,"failed to renew alarm database handler\r\n");
+    }
+}
+
 /*
  * our initialization routine, automatically called by the agent
   * (to get called, the function name must match init_FILENAME())
diff --git a/stx-snmp-helm/docker/stx-fm-subagent/src/snmpAgentPlugin.h b/stx-snmp-helm/docker/stx-fm-subagent/src/snmpAgentPlugin.h
index 240393f..787e683 100644
--- a/stx-snmp-helm/docker/stx-fm-subagent/src/snmpAgentPlugin.h
+++ b/stx-snmp-helm/docker/stx-fm-subagent/src/snmpAgentPlugin.h
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2020 Wind River Systems, Inc.
+* Copyright (c) 2020-2023 Wind River Systems, Inc.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
@@ -62,6 +62,7 @@ extern "C" {
 void  init_snmpAgentPlugin(void);
 void  deinit_snmpAgentPlugin(void);
 TFmAlarmSessionT getAlarmSession();
+void  renewAlarmSession(void);
 #ifdef __cplusplus
 }
 #endif
diff --git a/stx-snmp-helm/docker/stx-fm-subagent/src/snmpEventLogs.c b/stx-snmp-helm/docker/stx-fm-subagent/src/snmpEventLogs.c
index eb33ede..2886402 100644
--- a/stx-snmp-helm/docker/stx-fm-subagent/src/snmpEventLogs.c
+++ b/stx-snmp-helm/docker/stx-fm-subagent/src/snmpEventLogs.c
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2020-2021 Wind River Systems, Inc.
+* Copyright (c) 2020-2023 Wind River Systems, Inc.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
@@ -75,6 +75,7 @@ Event_Log_Scan_Init()
             DEBUGMSG(("cgtsAgentPlugin", 
             "get_all_event_logs returns false (%zu/%d). Try again\n",
              (i+1), retryCount));
+            renewAlarmSession();
         }
     }
     if (!isEventObtained) {