In ntfimcn the OM handle shall have a short lifespan. Change from creating a
handle once when ntfimcn process starts to create a handle each time it is
needed and finalize when no longer needed.
---
 src/ntf/ntfd/ntfs_amf.c      |   8 ++--
 src/ntf/ntfd/ntfs_imcnutil.c | 105 +++++++++++++++++--------------------------
 2 files changed, 45 insertions(+), 68 deletions(-)

diff --git a/src/ntf/ntfd/ntfs_amf.c b/src/ntf/ntfd/ntfs_amf.c
index e9c521d4a..cda35d6f9 100644
--- a/src/ntf/ntfd/ntfs_amf.c
+++ b/src/ntf/ntfd/ntfs_amf.c
@@ -263,10 +263,12 @@ response:
                checkNotificationList();
        }
 done:
-       /* Start the osafntfimcn process if becoming active or stop the process
-        * if leaving active state
+       /* Kills the osafntfimcnd process if current state is Active or Standby
+        * and the process is not already running in this state. The process
+        * will be restarted by the process surveillance thread. This function
+        * will not return until the process is terminated.
         */
-       handle_state_ntfimcn(new_haState);
+       handle_state_ntfimcn(ntfs_cb->ha_state);
 
        TRACE_LEAVE();
 }
diff --git a/src/ntf/ntfd/ntfs_imcnutil.c b/src/ntf/ntfd/ntfs_imcnutil.c
index 3add5db5a..dd27a255c 100644
--- a/src/ntf/ntfd/ntfs_imcnutil.c
+++ b/src/ntf/ntfd/ntfs_imcnutil.c
@@ -41,13 +41,13 @@
 #include "osaf/configmake.h"
 
 typedef struct {
+       SaAmfHAStateT ha_state;
        pid_t pid;
        pthread_t thread;
-       bool ntfimcn_on;
 } init_params_t;
 
-static init_params_t ipar = {0, 0, false};
-pthread_mutex_t ntfimcn_mutex = PTHREAD_MUTEX_INITIALIZER;
+static init_params_t ipar;
+pthread_mutex_t ntfimcn_mutex;
 
 /**
  * Kill the osafntfimcn child process using previously saved Pid
@@ -190,14 +190,6 @@ done:
        TRACE_LEAVE();
 }
 
-/**
- * Create a ntfimcnd process
- * Note: The process can run on both active and standby node. It must be given
- *       a start argument with this information.
- *
- * @param ha_state[in] Current HA state
- * @return PID for the created process
- */
 static pid_t create_imcnprocess(SaAmfHAStateT ha_state)
 {
        char *start_args[3];
@@ -234,11 +226,6 @@ static pid_t create_imcnprocess(SaAmfHAStateT ha_state)
  * Start the imcn process and wait for process to exit.
  * If the process exit then restart it.
  *
- * TODO: (Lennart #2506) The ntfimcn process is no longer started on the
- *       standby node. Handling of HA state in the ntfimcn process can 
therefore
- *       be removed. This also means that giving a start argument would no
- *       longer be needed.
- *
  * @param _init_params[in]
  * @return
  */
@@ -253,18 +240,13 @@ static void *cnsurvail_thread(void *_init_params)
 
        while (1) {
                osaf_mutex_lock_ordie(&ntfimcn_mutex);
-               /* Note: Always give HA state SA_AMF_HA_ACTIVE to the imcn
-                * process since ntfimcn is no longer started on the standby
-                * node */
-               pid = create_imcnprocess(SA_AMF_HA_ACTIVE);
+               pid = create_imcnprocess(ipar->ha_state);
                ipar->pid = pid;
                osaf_mutex_unlock_ordie(&ntfimcn_mutex);
 
                /* Wait for child process to exit */
                do {
                        rc = waitpid(ipar->pid, &status, 0);
-                       TRACE("%s: waitpid() released. rc = %d",
-                               __FUNCTION__, rc);
                } while ((rc == -1) && (errno == EINTR));
 
                if ((rc == -1) && (errno == ECHILD)) {
@@ -285,26 +267,29 @@ static void *cnsurvail_thread(void *_init_params)
                TRACE("osafntfimcnd process terminated reason %s (%d)",
                      exit_rc ? "exit" : "other", exit_stat);
        }
-       TRACE_LEAVE();
 }
 
 /**
- * Start the imcn process surveillance thread.
- * The surveillance thread starts the imcn process
- * Note: This shall be done only if we are active
+ * Start the imcn process surveillance thread
+ *
+ * @param ha_state[in]
  */
-static void start_ntfimcn(void)
+static void start_cnprocess(SaAmfHAStateT ha_state)
 {
        int rc;
 
        TRACE_ENTER();
 
-       rc = pthread_create(
-               &ipar.thread, NULL, cnsurvail_thread, (void *)&ipar);
+       rc = pthread_mutex_init(&ntfimcn_mutex, NULL);
        if (rc != 0)
                osaf_abort(rc);
 
-       ipar.ntfimcn_on = true;
+       ipar.ha_state = ha_state;
+
+       rc =
+           pthread_create(&ipar.thread, NULL, cnsurvail_thread, (void *)&ipar);
+       if (rc != 0)
+               osaf_abort(rc);
 
        TRACE_LEAVE();
 }
@@ -312,45 +297,34 @@ static void start_ntfimcn(void)
 /**
  * Initialize the configuration change notifier
  *
- * Start ntfimcn if we are active
- *
  * @param ha_state[in]
  */
-void init_ntfimcn(SaAmfHAStateT ha_state)
-{
-       TRACE_ENTER();
-       if (ha_state == SA_AMF_HA_ACTIVE) {
-               start_ntfimcn();
-       }
-       TRACE_LEAVE();
-}
+void init_ntfimcn(SaAmfHAStateT ha_state) { start_cnprocess(ha_state); }
 
 /**
- * Start or stop ntfimcn when changing HA state
- *
- * The ntfimcn process and the surveillance thread (see cnsurvail_thread())
- * is stopped or started based on given HA state we are changing to
- * ntfimcn shall be started if we are becoming active else it shall be stopped
- * Note1: The ntfimcn process is started by the surveillance thread
- * Note2: When stopping ntfimcn also the surveillance thread has to be
- * terminated or else it will start the ntfimcn process again.
+ * Kill the imcn process. Use the pid saved when the process was started
+ * This will cause the imcn process to be restarted in the given state.
+ * This is done only if the current state is Active or Standby and if
+ * the state has changed
  *
- * @param ha_state[in] The HA state we are changing to
+ * @param ha_state[in]
  */
-void handle_state_ntfimcn(SaAmfHAStateT to_ha_state)
+void handle_state_ntfimcn(SaAmfHAStateT ha_state)
 {
-       TRACE_ENTER2("to_ha_state = %d", to_ha_state);
-       /* Note: ntfimcnd process still needs HA state even if it only is
-        * started on the active node. See TODO in cnsurvail_thread() */
-       if ((to_ha_state == SA_AMF_HA_ACTIVE) &&
-               (ipar.ntfimcn_on == false)) {
-               /* We are becoming active and ntfimcn is not started.
-                * Start ntfimcn */
-               start_ntfimcn();
-       } else if ((ipar.ntfimcn_on == true) &&
-               (to_ha_state != SA_AMF_HA_ACTIVE)) {
-               /* Always stop ntfimcn if we are leaving active state */
-               stop_ntfimcn();
+       TRACE_ENTER();
+       if ((ha_state == SA_AMF_HA_ACTIVE) || (ha_state == SA_AMF_HA_STANDBY)) {
+               osaf_mutex_lock_ordie(&ntfimcn_mutex);
+               if (ha_state != ipar.ha_state) {
+                       ipar.ha_state = ha_state;
+                       TRACE("%s: Terminating osafntfimcnd process",
+                             __FUNCTION__);
+                       timedwait_imcn_exit();
+               } else {
+                       TRACE("%s: osafntfimcnd process not restarted. "
+                             "Already in correct state",
+                             __FUNCTION__);
+               }
+               osaf_mutex_unlock_ordie(&ntfimcn_mutex);
        }
        TRACE_LEAVE();
 }
@@ -368,7 +342,7 @@ int stop_ntfimcn(void)
        int rc = 0;
        TRACE_ENTER();
 
-       if (ipar.ntfimcn_on == true) {
+       if (ipar.ha_state != 0) {
                TRACE("%s: Cancel the imcn surveillance thread", __FUNCTION__);
                rc = pthread_cancel(ipar.thread);
                if (rc != 0)
@@ -376,11 +350,12 @@ int stop_ntfimcn(void)
                rc = pthread_join(ipar.thread, &join_ret);
                if (rc != 0)
                        osaf_abort(rc);
+               rc = pthread_mutex_destroy(&ntfimcn_mutex);
+               if (rc != 0)
+                       osaf_abort(rc);
 
                TRACE("%s: Terminating osafntfimcnd process", __FUNCTION__);
                timedwait_imcn_exit();
-
-               ipar.ntfimcn_on = false;
        }
 
        TRACE_LEAVE();
-- 
2.13.0


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Opensaf-devel mailing list
Opensaf-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/opensaf-devel

Reply via email to