Blob Blame History Raw
From 8a0e19a7702f61622d06b1c473fb3d9a5924c8f4 Mon Sep 17 00:00:00 2001
From: Ken Gaillot <kgaillot@redhat.com>
Date: Tue, 14 Jan 2020 18:07:18 -0600
Subject: [PATCH 05/18] Refactor: liblrmd: new convenience function for
 allocating lrmd_event_data_t

---
 daemons/controld/controld_execd.c     |  7 +------
 include/crm/lrmd.h                    |  2 ++
 lib/lrmd/lrmd_client.c                | 34 +++++++++++++++++++++++++++++++++-
 lib/pacemaker/pcmk_sched_transition.c |  7 +------
 lib/pacemaker/pcmk_trans_unpack.c     |  9 +++------
 5 files changed, 40 insertions(+), 19 deletions(-)

diff --git a/daemons/controld/controld_execd.c b/daemons/controld/controld_execd.c
index 82f2bf1..17cc8d6 100644
--- a/daemons/controld/controld_execd.c
+++ b/daemons/controld/controld_execd.c
@@ -1878,15 +1878,10 @@ construct_op(lrm_state_t * lrm_state, xmlNode * rsc_op, const char *rsc_id, cons
 
     CRM_ASSERT(rsc_id && operation);
 
-    op = calloc(1, sizeof(lrmd_event_data_t));
-    CRM_ASSERT(op != NULL);
-
+    op = lrmd_new_event(rsc_id, operation, 0);
     op->type = lrmd_event_exec_complete;
-    op->op_type = strdup(operation);
     op->op_status = PCMK_LRM_OP_PENDING;
     op->rc = -1;
-    op->rsc_id = strdup(rsc_id);
-    op->interval_ms = 0;
     op->timeout = 0;
     op->start_delay = 0;
 
diff --git a/include/crm/lrmd.h b/include/crm/lrmd.h
index cfa2925..3ad1f05 100644
--- a/include/crm/lrmd.h
+++ b/include/crm/lrmd.h
@@ -248,6 +248,8 @@ typedef struct lrmd_event_data_s {
     const char *exit_reason;
 } lrmd_event_data_t;
 
+lrmd_event_data_t *lrmd_new_event(const char *rsc_id, const char *task,
+                                  guint interval_ms);
 lrmd_event_data_t *lrmd_copy_event(lrmd_event_data_t * event);
 void lrmd_free_event(lrmd_event_data_t * event);
 
diff --git a/lib/lrmd/lrmd_client.c b/lib/lrmd/lrmd_client.c
index 2469c52..d16743d 100644
--- a/lib/lrmd/lrmd_client.c
+++ b/lib/lrmd/lrmd_client.c
@@ -1,5 +1,7 @@
 /*
- * Copyright 2012-2018 David Vossel <davidvossel@gmail.com>
+ * Copyright 2012-2020 the Pacemaker project contributors
+ *
+ * The version control history for this file may have further details.
  *
  * This source code is licensed under the GNU Lesser General Public License
  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
@@ -175,6 +177,36 @@ lrmd_key_value_freeall(lrmd_key_value_t * head)
     }
 }
 
+/*!
+ * Create a new lrmd_event_data_t object
+ *
+ * \param[in] rsc_id       ID of resource involved in event
+ * \param[in] task         Action name
+ * \param[in] interval_ms  Action interval
+ *
+ * \return Newly allocated and initialized lrmd_event_data_t
+ * \note This functions asserts on memory errors, so the return value is
+ *       guaranteed to be non-NULL. The caller is responsible for freeing the
+ *       result with lrmd_free_event().
+ */
+lrmd_event_data_t *
+lrmd_new_event(const char *rsc_id, const char *task, guint interval_ms)
+{
+    lrmd_event_data_t *event = calloc(1, sizeof(lrmd_event_data_t));
+
+    CRM_ASSERT(event != NULL);
+    if (rsc_id != NULL) {
+        event->rsc_id = strdup(rsc_id);
+        CRM_ASSERT(event->rsc_id != NULL);
+    }
+    if (task != NULL) {
+        event->op_type = strdup(task);
+        CRM_ASSERT(event->op_type != NULL);
+    }
+    event->interval_ms = interval_ms;
+    return event;
+}
+
 lrmd_event_data_t *
 lrmd_copy_event(lrmd_event_data_t * event)
 {
diff --git a/lib/pacemaker/pcmk_sched_transition.c b/lib/pacemaker/pcmk_sched_transition.c
index c415b75..1698c85 100644
--- a/lib/pacemaker/pcmk_sched_transition.c
+++ b/lib/pacemaker/pcmk_sched_transition.c
@@ -131,12 +131,7 @@ create_op(xmlNode *cib_resource, const char *task, guint interval_ms,
     lrmd_event_data_t *op = NULL;
     xmlNode *xop = NULL;
 
-    op = calloc(1, sizeof(lrmd_event_data_t));
-
-    op->rsc_id = strdup(ID(cib_resource));
-    op->interval_ms = interval_ms;
-    op->op_type = strdup(task);
-
+    op = lrmd_new_event(ID(cib_resource), task, interval_ms);
     op->rc = outcome;
     op->op_status = 0;
     op->params = NULL;          /* TODO: Fill me in */
diff --git a/lib/pacemaker/pcmk_trans_unpack.c b/lib/pacemaker/pcmk_trans_unpack.c
index e57f386..3e53289 100644
--- a/lib/pacemaker/pcmk_trans_unpack.c
+++ b/lib/pacemaker/pcmk_trans_unpack.c
@@ -298,12 +298,9 @@ convert_graph_action(xmlNode * resource, crm_action_t * action, int status, int
     CRM_CHECK(action_resource != NULL, crm_log_xml_warn(action->xml, "Bad");
               return NULL);
 
-    op = calloc(1, sizeof(lrmd_event_data_t));
-
-    op->rsc_id = strdup(ID(action_resource));
-    op->interval_ms = action->interval_ms;
-    op->op_type = strdup(crm_element_value(action->xml, XML_LRM_ATTR_TASK));
-
+    op = lrmd_new_event(ID(action_resource),
+                        crm_element_value(action->xml, XML_LRM_ATTR_TASK),
+                        action->interval_ms);
     op->rc = rc;
     op->op_status = status;
     op->t_run = time(NULL);
-- 
1.8.3.1