trigger: lttng_triggers: implement a container for multiple triggers
authorJonathan Rajotte <jonathan.rajotte-julien@efficios.com>
Tue, 21 Jan 2020 19:22:37 +0000 (14:22 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Tue, 17 Nov 2020 20:24:05 +0000 (15:24 -0500)
This container is exposed for the listing of triggers.

We also plan on using it internally in the sessiond for inter-thread
communication.

The current implementation is backed by a lttng_dynamic_pointer_array.

Caller of lttng_triggers_add is responsible for managing ownership via
ref-counting of the lttng_trigger object.

Signed-off-by: Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
Change-Id: Ib541027a6d7d856daa746de5aa49f0002bbe036f

include/lttng/trigger/trigger-internal.h
include/lttng/trigger/trigger.h
src/common/trigger.c

index df69f3d110a2c49fe970f80883f74f018cea0c37..6942ece78d7a88d9b15a2585fdb200e43f0261f8 100644 (file)
@@ -10,6 +10,7 @@
 
 #include <lttng/trigger/trigger.h>
 #include <common/credentials.h>
+#include <common/dynamic-array.h>
 #include <common/macros.h>
 #include <common/optional.h>
 #include <stdint.h>
@@ -37,6 +38,10 @@ struct lttng_trigger {
        LTTNG_OPTIONAL(uint64_t) tracer_token;
 };
 
+struct lttng_triggers {
+       struct lttng_dynamic_pointer_array array;
+};
+
 struct lttng_trigger_comm {
        /*
         * Credentials, only the uid portion is used for now.
@@ -54,12 +59,19 @@ struct lttng_trigger_comm {
        char payload[];
 } LTTNG_PACKED;
 
+struct lttng_triggers_comm {
+       uint32_t count;
+       uint32_t length;
+       /* Count * lttng_trigger_comm structure */
+       char payload[];
+};
+
 LTTNG_HIDDEN
 ssize_t lttng_trigger_create_from_payload(struct lttng_payload_view *view,
                struct lttng_trigger **trigger);
 
 LTTNG_HIDDEN
-int lttng_trigger_serialize(struct lttng_trigger *trigger,
+int lttng_trigger_serialize(const struct lttng_trigger *trigger,
                struct lttng_payload *payload);
 
 LTTNG_HIDDEN
@@ -98,6 +110,49 @@ void lttng_trigger_get(struct lttng_trigger *trigger);
 LTTNG_HIDDEN
 void lttng_trigger_put(struct lttng_trigger *trigger);
 
+/*
+ * Allocate a new set of triggers.
+ * The returned object must be freed via lttng_triggers_destroy.
+ */
+LTTNG_HIDDEN
+struct lttng_triggers *lttng_triggers_create(void);
+
+/*
+ * Return the a pointer to a mutable element at index "index" of an
+ * lttng_triggers set.
+ *
+ * This differs from the public `lttng_triggers_get_at_index` in that
+ * the returned pointer to a mutable trigger.
+ *
+ * The ownership of the trigger set element is NOT transfered.
+ * The returned object can NOT be freed via lttng_trigger_destroy.
+ */
+LTTNG_HIDDEN
+struct lttng_trigger *lttng_triggers_borrow_mutable_at_index(
+               const struct lttng_triggers *triggers, unsigned int index);
+
+/*
+ * Add a trigger to the triggers set.
+ *
+ * A reference to the added trigger is acquired on behalf of the trigger set
+ * on success.
+ */
+LTTNG_HIDDEN
+int lttng_triggers_add(
+               struct lttng_triggers *triggers, struct lttng_trigger *trigger);
+
+/*
+ * Serialize a trigger set to an lttng_payload object.
+ * Return LTTNG_OK on success, negative lttng error code on error.
+ */
+LTTNG_HIDDEN
+int lttng_triggers_serialize(const struct lttng_triggers *triggers,
+               struct lttng_payload *payload);
+
+LTTNG_HIDDEN
+ssize_t lttng_triggers_create_from_payload(struct lttng_payload_view *view,
+               struct lttng_triggers **triggers);
+
 LTTNG_HIDDEN
 const struct lttng_credentials *lttng_trigger_get_credentials(
                const struct lttng_trigger *trigger);
index 96e018f248d536eecc6c08e3c0d06f2f5a212dbc..b8e6080d9519a7edaaab43ca291c7c45f56e685d 100644 (file)
@@ -13,6 +13,8 @@
 struct lttng_action;
 struct lttng_condition;
 struct lttng_trigger;
+/* A set of triggers. */
+struct lttng_triggers;
 
 #ifdef __cplusplus
 extern "C" {
@@ -148,6 +150,33 @@ extern int lttng_register_trigger(struct lttng_trigger *trigger);
  */
 extern int lttng_unregister_trigger(struct lttng_trigger *trigger);
 
+/*
+ * Get a trigger from the set at a given index.
+ *
+ * Note that the trigger set maintains the ownership of the returned trigger.
+ * It must not be destroyed by the user, nor should a reference to it be held
+ * beyond the lifetime of the trigger set.
+ *
+ * Returns a trigger, or NULL on error.
+ */
+extern const struct lttng_trigger *lttng_triggers_get_at_index(
+               const struct lttng_triggers *triggers, unsigned int index);
+
+/*
+ * Get the number of triggers in a trigger set.
+ *
+ * Return LTTNG_TRIGGER_STATUS_OK on success,
+ * LTTNG_TRIGGER_STATUS_INVALID when invalid parameters are passed.
+ */
+extern enum lttng_trigger_status lttng_triggers_get_count(
+               const struct lttng_triggers *triggers, unsigned int *count);
+
+/*
+ * Destroy a trigger set.
+ */
+extern void lttng_triggers_destroy(struct lttng_triggers *triggers);
+
+
 #ifdef __cplusplus
 }
 #endif
index f77a321295d4c10a9c7d67ce51071bc649854a19..26997fd1f21da68796f6a45e8d25633c8ed6d398 100644 (file)
@@ -12,6 +12,7 @@
 #include <common/payload.h>
 #include <common/payload-view.h>
 #include <common/error.h>
+#include <common/dynamic-array.h>
 #include <common/optional.h>
 #include <assert.h>
 #include <inttypes.h>
@@ -255,7 +256,7 @@ end:
  * for the detailed format.
  */
 LTTNG_HIDDEN
-int lttng_trigger_serialize(struct lttng_trigger *trigger,
+int lttng_trigger_serialize(const struct lttng_trigger *trigger,
                struct lttng_payload *payload)
 {
        int ret;
@@ -448,6 +449,210 @@ void lttng_trigger_put(struct lttng_trigger *trigger)
        urcu_ref_put(&trigger->ref , trigger_destroy_ref);
 }
 
+static void delete_trigger_array_element(void *ptr)
+{
+       struct lttng_trigger *trigger = ptr;
+
+       lttng_trigger_put(trigger);
+}
+
+LTTNG_HIDDEN
+struct lttng_triggers *lttng_triggers_create(void)
+{
+       struct lttng_triggers *triggers = NULL;
+
+       triggers = zmalloc(sizeof(*triggers));
+       if (!triggers) {
+               goto end;
+       }
+
+       lttng_dynamic_pointer_array_init(&triggers->array, delete_trigger_array_element);
+
+end:
+       return triggers;
+}
+
+LTTNG_HIDDEN
+struct lttng_trigger *lttng_triggers_borrow_mutable_at_index(
+               const struct lttng_triggers *triggers, unsigned int index)
+{
+       struct lttng_trigger *trigger = NULL;
+
+       assert(triggers);
+       if (index >= lttng_dynamic_pointer_array_get_count(&triggers->array)) {
+               goto end;
+       }
+
+       trigger = (struct lttng_trigger *)
+                       lttng_dynamic_pointer_array_get_pointer(
+                                       &triggers->array, index);
+end:
+       return trigger;
+}
+
+LTTNG_HIDDEN
+int lttng_triggers_add(
+               struct lttng_triggers *triggers, struct lttng_trigger *trigger)
+{
+       int ret;
+
+       assert(triggers);
+       assert(trigger);
+
+       lttng_trigger_get(trigger);
+
+       ret = lttng_dynamic_pointer_array_add_pointer(&triggers->array, trigger);
+       if (ret) {
+               lttng_trigger_put(trigger);
+       }
+
+       return ret;
+}
+
+const struct lttng_trigger *lttng_triggers_get_at_index(
+               const struct lttng_triggers *triggers, unsigned int index)
+{
+       return lttng_triggers_borrow_mutable_at_index(triggers, index);
+}
+
+enum lttng_trigger_status lttng_triggers_get_count(const struct lttng_triggers *triggers, unsigned int *count)
+{
+       enum lttng_trigger_status status = LTTNG_TRIGGER_STATUS_OK;
+
+       if (!triggers || !count) {
+               status = LTTNG_TRIGGER_STATUS_INVALID;
+               goto end;
+       }
+
+       *count = lttng_dynamic_pointer_array_get_count(&triggers->array);
+end:
+       return status;
+}
+
+void lttng_triggers_destroy(struct lttng_triggers *triggers)
+{
+       if (!triggers) {
+               return;
+       }
+
+       lttng_dynamic_pointer_array_reset(&triggers->array);
+       free(triggers);
+}
+
+int lttng_triggers_serialize(const struct lttng_triggers *triggers,
+               struct lttng_payload *payload)
+{
+       int ret;
+       unsigned int i, count;
+       size_t size_before_payload;
+       struct lttng_triggers_comm triggers_comm = {};
+       struct lttng_triggers_comm *header;
+       enum lttng_trigger_status status;
+       const size_t header_offset = payload->buffer.size;
+
+       status = lttng_triggers_get_count(triggers, &count);
+       if (status != LTTNG_TRIGGER_STATUS_OK) {
+               ret = LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       triggers_comm.count = count;
+
+       /* Placeholder header; updated at the end. */
+       ret = lttng_dynamic_buffer_append(&payload->buffer, &triggers_comm,
+                       sizeof(triggers_comm));
+       if (ret) {
+               goto end;
+       }
+
+       size_before_payload = payload->buffer.size;
+
+       for (i = 0; i < count; i++) {
+               const struct lttng_trigger *trigger =
+                               lttng_triggers_get_at_index(triggers, i);
+
+               assert(trigger);
+
+               ret = lttng_trigger_serialize(trigger, payload);
+               if (ret) {
+                       goto end;
+               }
+       }
+
+       /* Update payload size. */
+       header = (struct lttng_triggers_comm *) ((char *) payload->buffer.data + header_offset);
+       header->length = payload->buffer.size - size_before_payload;
+end:
+       return ret;
+}
+
+LTTNG_HIDDEN
+ssize_t lttng_triggers_create_from_payload(
+               struct lttng_payload_view *src_view,
+               struct lttng_triggers **triggers)
+{
+       ssize_t ret, offset = 0, triggers_size = 0;
+       unsigned int i;
+       const struct lttng_triggers_comm *triggers_comm;
+       struct lttng_triggers *local_triggers = NULL;
+
+       if (!src_view || !triggers) {
+               ret = -1;
+               goto error;
+       }
+
+       /* lttng_trigger_comms header */
+       triggers_comm = (const struct lttng_triggers_comm *) src_view->buffer.data;
+       offset += sizeof(*triggers_comm);
+
+       local_triggers = lttng_triggers_create();
+       if (!local_triggers) {
+               ret = -1;
+               goto error;
+       }
+
+       for (i = 0; i < triggers_comm->count; i++) {
+               struct lttng_trigger *trigger = NULL;
+               struct lttng_payload_view trigger_view =
+                               lttng_payload_view_from_view(src_view, offset, -1);
+               ssize_t trigger_size;
+
+               trigger_size = lttng_trigger_create_from_payload(
+                               &trigger_view, &trigger);
+               if (trigger_size < 0) {
+                       ret = trigger_size;
+                       goto error;
+               }
+
+               /* Transfer ownership of the trigger to the collection. */
+               ret = lttng_triggers_add(local_triggers, trigger);
+               lttng_trigger_put(trigger);
+               if (ret < 0) {
+                       ret = -1;
+                       goto error;
+               }
+
+               offset += trigger_size;
+               triggers_size += trigger_size;
+       }
+
+       /* Unexpected size of inner-elements; the buffer is corrupted. */
+       if ((ssize_t) triggers_comm->length != triggers_size) {
+               ret = -1;
+               goto error;
+       }
+
+       /* Pass ownership to caller. */
+       *triggers = local_triggers;
+       local_triggers = NULL;
+
+       ret = offset;
+error:
+
+       lttng_triggers_destroy(local_triggers);
+       return ret;
+}
+
 LTTNG_HIDDEN
 const struct lttng_credentials *lttng_trigger_get_credentials(
                const struct lttng_trigger *trigger)
This page took 0.029776 seconds and 4 git commands to generate.