actions: introduce rotate session action
authorSimon Marchi <simon.marchi@efficios.com>
Mon, 2 Dec 2019 20:01:50 +0000 (15:01 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Tue, 9 Jun 2020 18:09:59 +0000 (14:09 -0400)
This patch introduces the API for the "rotate session" action.

A rotate session action is created using the
lttng_action_rotate_session_create function.  It is mandatory to set a
session name using lttng_action_rotate_session_set_session_name before
using the action in a trigger.

The patch adds the code for serializing the action and deserializing it
on the sessiond side, but not the code for executing it.

Change-Id: I8ed1a71a00deaa6abafaff703a8980c2c7598bda
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
include/Makefile.am
include/lttng/action/action.h
include/lttng/action/rotate-session-internal.h [new file with mode: 0644]
include/lttng/action/rotate-session.h [new file with mode: 0644]
include/lttng/lttng.h
src/common/Makefile.am
src/common/actions/action.c
src/common/actions/rotate-session.c [new file with mode: 0644]

index b31df9da157cc521c149c4d30fa49266b56d4b37..21efe4076c7a4056f03dac8471739c7a1c6f936a 100644 (file)
@@ -122,6 +122,7 @@ lttnginclude_HEADERS = \
 lttngactioninclude_HEADERS= \
        lttng/action/action.h \
        lttng/action/notify.h \
+       lttng/action/rotate-session.h \
        lttng/action/start-session.h \
        lttng/action/stop-session.h
 
@@ -146,6 +147,7 @@ noinst_HEADERS = \
        lttng/load-internal.h \
        lttng/action/action-internal.h \
        lttng/action/notify-internal.h \
+       lttng/action/rotate-session-internal.h \
        lttng/action/start-session-internal.h \
        lttng/action/stop-session-internal.h \
        lttng/condition/condition-internal.h \
index cc26ed9141e33d142b2488c67052061e45b76047..03f2964f3ae08cef8c2d265a2bfaffade7e78217 100644 (file)
@@ -19,6 +19,7 @@ enum lttng_action_type {
        LTTNG_ACTION_TYPE_NOTIFY = 0,
        LTTNG_ACTION_TYPE_START_SESSION = 1,
        LTTNG_ACTION_TYPE_STOP_SESSION = 2,
+       LTTNG_ACTION_TYPE_ROTATE_SESSION = 3,
 };
 
 enum lttng_action_status {
diff --git a/include/lttng/action/rotate-session-internal.h b/include/lttng/action/rotate-session-internal.h
new file mode 100644 (file)
index 0000000..2cce9fc
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2019 Simon Marchi <simon.marchi@efficios.com>
+ *
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ */
+
+#ifndef LTTNG_ACTION_ROTATE_SESSION_INTERNAL_H
+#define LTTNG_ACTION_ROTATE_SESSION_INTERNAL_H
+
+#include <sys/types.h>
+
+#include <common/macros.h>
+
+struct lttng_action;
+struct lttng_buffer_view;
+
+/*
+ * Create a "rotate session" action from a buffer view.
+ *
+ * On success, return the number of bytes consumed from `view`, and the created
+ * action in `*action`. On failure, return -1.
+ */
+LTTNG_HIDDEN
+extern ssize_t lttng_action_rotate_session_create_from_buffer(
+               const struct lttng_buffer_view *view,
+               struct lttng_action **action);
+
+#endif /* LTTNG_ACTION_ROTATE_SESSION_INTERNAL_H */
diff --git a/include/lttng/action/rotate-session.h b/include/lttng/action/rotate-session.h
new file mode 100644 (file)
index 0000000..750c073
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2019 Simon Marchi <simon.marchi@efficios.com>
+ *
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ */
+
+#ifndef LTTNG_ACTION_ROTATE_SESSION_H
+#define LTTNG_ACTION_ROTATE_SESSION_H
+
+struct lttng_action;
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * Create a newly allocated rotate-session action object.
+ *
+ * A rotate session action object must have a session name set to be considered
+ * valid when used with a trigger object (lttng_trigger). A name can be set
+ * using `lttng_action_rotate_session_set_session_name`.
+ *
+ * Returns a new action on success, NULL on failure. This action must be
+ * destroyed using lttng_action_destroy().
+ */
+extern struct lttng_action *lttng_action_rotate_session_create(void);
+
+/*
+ * Set the session name of an lttng_action object of type
+ * LTTNG_ACTION_TYPE_ROTATE_SESSION.
+ */
+extern enum lttng_action_status lttng_action_rotate_session_set_session_name(
+               struct lttng_action *action, const char *session_name);
+
+/*
+ * Get the session name of an lttng_action object of type
+ * LTTNG_ACTION_TYPE_ROTATE_SESSION.
+ */
+extern enum lttng_action_status lttng_action_rotate_session_get_session_name(
+               const struct lttng_action *action, const char **session_name);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* LTTNG_ACTION_ROTATE_SESSION_H */
index 24638a351cb1248e626d1c20436bd90baaa2b1ed..b3465cb936c087a055794ea097dfb7bce75ae32b 100644 (file)
@@ -18,6 +18,7 @@
 /* Include every LTTng ABI/API available. */
 #include <lttng/action/action.h>
 #include <lttng/action/notify.h>
+#include <lttng/action/rotate-session.h>
 #include <lttng/action/start-session.h>
 #include <lttng/action/stop-session.h>
 #include <lttng/channel.h>
index 5303f26431f3c9e1db9791d314cafc7ffeafbb10..75fe7c9070833a147e7571f2a5b8be7156b85bae 100644 (file)
@@ -29,6 +29,7 @@ EXTRA_DIST = mi-lttng-4.0.xsd
 libcommon_la_SOURCES = \
        actions/action.c \
        actions/notify.c \
+       actions/rotate-session.c \
        actions/start-session.c \
        actions/stop-session.c \
        buffer-usage.c \
index 208858e2671c6d94210b9dd81cccbae57a1dd56a..98e1849dd41950018b2276e98408afc763895591 100644 (file)
@@ -9,6 +9,7 @@
 #include <common/error.h>
 #include <lttng/action/action-internal.h>
 #include <lttng/action/notify-internal.h>
+#include <lttng/action/rotate-session-internal.h>
 #include <lttng/action/start-session-internal.h>
 #include <lttng/action/stop-session-internal.h>
 
@@ -19,6 +20,8 @@ static const char *lttng_action_type_string(enum lttng_action_type action_type)
                return "UNKNOWN";
        case LTTNG_ACTION_TYPE_NOTIFY:
                return "NOTIFY";
+       case LTTNG_ACTION_TYPE_ROTATE_SESSION:
+               return "ROTATE_SESSION";
        case LTTNG_ACTION_TYPE_START_SESSION:
                return "START_SESSION";
        case LTTNG_ACTION_TYPE_STOP_SESSION:
@@ -133,6 +136,10 @@ ssize_t lttng_action_create_from_buffer(const struct lttng_buffer_view *view,
        case LTTNG_ACTION_TYPE_NOTIFY:
                create_from_buffer_cb = lttng_action_notify_create_from_buffer;
                break;
+       case LTTNG_ACTION_TYPE_ROTATE_SESSION:
+               create_from_buffer_cb =
+                               lttng_action_rotate_session_create_from_buffer;
+               break;
        case LTTNG_ACTION_TYPE_START_SESSION:
                create_from_buffer_cb =
                                lttng_action_start_session_create_from_buffer;
diff --git a/src/common/actions/rotate-session.c b/src/common/actions/rotate-session.c
new file mode 100644 (file)
index 0000000..9880623
--- /dev/null
@@ -0,0 +1,260 @@
+/*
+ * Copyright (C) 2019 Simon Marchi <simon.marchi@efficios.com>
+ *
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ */
+
+#include <assert.h>
+#include <common/error.h>
+#include <common/macros.h>
+#include <lttng/action/action-internal.h>
+#include <lttng/action/rotate-session-internal.h>
+#include <lttng/action/rotate-session.h>
+
+#define IS_ROTATE_SESSION_ACTION(action) \
+       (lttng_action_get_type_const(action) == LTTNG_ACTION_TYPE_ROTATE_SESSION)
+
+struct lttng_action_rotate_session {
+       struct lttng_action parent;
+
+       /* Owned by this. */
+       char *session_name;
+};
+
+struct lttng_action_rotate_session_comm {
+       /* Includes the trailing \0. */
+       uint32_t session_name_len;
+
+       /*
+        * Variable data:
+        *
+        *  - session name (null terminated)
+        */
+       char data[];
+} LTTNG_PACKED;
+
+static struct lttng_action_rotate_session *action_rotate_session_from_action(
+               struct lttng_action *action)
+{
+       assert(action);
+
+       return container_of(action, struct lttng_action_rotate_session, parent);
+}
+
+static const struct lttng_action_rotate_session *
+action_rotate_session_from_action_const(const struct lttng_action *action)
+{
+       assert(action);
+
+       return container_of(action, struct lttng_action_rotate_session, parent);
+}
+
+static bool lttng_action_rotate_session_validate(struct lttng_action *action)
+{
+       bool valid;
+       struct lttng_action_rotate_session *action_rotate_session;
+
+       if (!action) {
+               valid = false;
+               goto end;
+       }
+
+       action_rotate_session = action_rotate_session_from_action(action);
+
+       /* A non-empty session name is mandatory. */
+       if (!action_rotate_session->session_name ||
+                       strlen(action_rotate_session->session_name) == 0) {
+               valid = false;
+               goto end;
+       }
+
+       valid = true;
+end:
+       return valid;
+}
+
+static bool lttng_action_rotate_session_is_equal(
+               const struct lttng_action *_a, const struct lttng_action *_b)
+{
+       bool is_equal = false;
+       const struct lttng_action_rotate_session *a, *b;
+
+       a = action_rotate_session_from_action_const(_a);
+       b = action_rotate_session_from_action_const(_b);
+
+       /* Action is not valid if this is not true. */
+       assert(a->session_name);
+       assert(b->session_name);
+       if (strcmp(a->session_name, b->session_name)) {
+               goto end;
+       }
+
+       is_equal = true;
+end:
+       return is_equal;
+}
+static int lttng_action_rotate_session_serialize(
+               struct lttng_action *action, struct lttng_dynamic_buffer *buf)
+{
+       struct lttng_action_rotate_session *action_rotate_session;
+       struct lttng_action_rotate_session_comm comm;
+       size_t session_name_len;
+       int ret;
+
+       assert(action);
+       assert(buf);
+
+       action_rotate_session = action_rotate_session_from_action(action);
+
+       assert(action_rotate_session->session_name);
+
+       DBG("Serializing rotate session action: session-name: %s",
+                       action_rotate_session->session_name);
+
+       session_name_len = strlen(action_rotate_session->session_name) + 1;
+       comm.session_name_len = session_name_len;
+
+       ret = lttng_dynamic_buffer_append(buf, &comm, sizeof(comm));
+       if (ret) {
+               ret = -1;
+               goto end;
+       }
+
+       ret = lttng_dynamic_buffer_append(buf,
+                       action_rotate_session->session_name, session_name_len);
+       if (ret) {
+               ret = -1;
+               goto end;
+       }
+
+       ret = 0;
+end:
+       return ret;
+}
+
+static void lttng_action_rotate_session_destroy(struct lttng_action *action)
+{
+       struct lttng_action_rotate_session *action_rotate_session;
+
+       if (!action) {
+               goto end;
+       }
+
+       action_rotate_session = action_rotate_session_from_action(action);
+
+       free(action_rotate_session->session_name);
+       free(action_rotate_session);
+
+end:
+       return;
+}
+
+ssize_t lttng_action_rotate_session_create_from_buffer(
+               const struct lttng_buffer_view *view,
+               struct lttng_action **p_action)
+{
+       ssize_t consumed_len;
+       const struct lttng_action_rotate_session_comm *comm;
+       const char *session_name;
+       struct lttng_action *action;
+       enum lttng_action_status status;
+
+       action = lttng_action_rotate_session_create();
+       if (!action) {
+               consumed_len = -1;
+               goto end;
+       }
+
+       comm = (const struct lttng_action_rotate_session_comm *) view->data;
+       session_name = (const char *) &comm->data;
+
+       if (!lttng_buffer_view_contains_string(
+                       view, session_name, comm->session_name_len)) {
+               consumed_len = -1;
+               goto end;
+       }
+
+       status = lttng_action_rotate_session_set_session_name(
+                       action, session_name);
+       if (status != LTTNG_ACTION_STATUS_OK) {
+               consumed_len = -1;
+               goto end;
+       }
+
+       consumed_len = sizeof(struct lttng_action_rotate_session_comm) +
+               comm->session_name_len;
+       *p_action = action;
+       action = NULL;
+
+end:
+       lttng_action_rotate_session_destroy(action);
+
+       return consumed_len;
+}
+
+struct lttng_action *lttng_action_rotate_session_create(void)
+{
+       struct lttng_action *action;
+
+       action = zmalloc(sizeof(struct lttng_action_rotate_session));
+       if (!action) {
+               goto end;
+       }
+
+       lttng_action_init(action, LTTNG_ACTION_TYPE_ROTATE_SESSION,
+                       lttng_action_rotate_session_validate,
+                       lttng_action_rotate_session_serialize,
+                       lttng_action_rotate_session_is_equal,
+                       lttng_action_rotate_session_destroy);
+
+end:
+       return action;
+}
+
+enum lttng_action_status lttng_action_rotate_session_set_session_name(
+               struct lttng_action *action, const char *session_name)
+{
+       struct lttng_action_rotate_session *action_rotate_session;
+       enum lttng_action_status status;
+
+       if (!action || !IS_ROTATE_SESSION_ACTION(action) || !session_name ||
+                       strlen(session_name) == 0) {
+               status = LTTNG_ACTION_STATUS_INVALID;
+               goto end;
+       }
+
+       action_rotate_session = action_rotate_session_from_action(action);
+
+       free(action_rotate_session->session_name);
+
+       action_rotate_session->session_name = strdup(session_name);
+       if (!action_rotate_session->session_name) {
+               status = LTTNG_ACTION_STATUS_ERROR;
+               goto end;
+       }
+
+       status = LTTNG_ACTION_STATUS_OK;
+end:
+       return status;
+}
+
+enum lttng_action_status lttng_action_rotate_session_get_session_name(
+               const struct lttng_action *action, const char **session_name)
+{
+       const struct lttng_action_rotate_session *action_rotate_session;
+       enum lttng_action_status status;
+
+       if (!action || !IS_ROTATE_SESSION_ACTION(action) || !session_name) {
+               status = LTTNG_ACTION_STATUS_INVALID;
+               goto end;
+       }
+
+       action_rotate_session = action_rotate_session_from_action_const(action);
+
+       *session_name = action_rotate_session->session_name;
+
+       status = LTTNG_ACTION_STATUS_OK;
+end:
+       return status;
+}
This page took 0.031521 seconds and 4 git commands to generate.