2 * Copyright (C) 2019 Simon Marchi <simon.marchi@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
9 #include <common/error.h>
10 #include <common/macros.h>
11 #include <lttng/action/action-internal.h>
12 #include <lttng/action/rotate-session-internal.h>
13 #include <lttng/action/rotate-session.h>
15 #define IS_ROTATE_SESSION_ACTION(action) \
16 (lttng_action_get_type_const(action) == LTTNG_ACTION_TYPE_ROTATE_SESSION)
18 struct lttng_action_rotate_session
{
19 struct lttng_action parent
;
25 struct lttng_action_rotate_session_comm
{
26 /* Includes the trailing \0. */
27 uint32_t session_name_len
;
32 * - session name (null terminated)
37 static struct lttng_action_rotate_session
*action_rotate_session_from_action(
38 struct lttng_action
*action
)
42 return container_of(action
, struct lttng_action_rotate_session
, parent
);
45 static const struct lttng_action_rotate_session
*
46 action_rotate_session_from_action_const(const struct lttng_action
*action
)
50 return container_of(action
, struct lttng_action_rotate_session
, parent
);
53 static bool lttng_action_rotate_session_validate(struct lttng_action
*action
)
56 struct lttng_action_rotate_session
*action_rotate_session
;
63 action_rotate_session
= action_rotate_session_from_action(action
);
65 /* A non-empty session name is mandatory. */
66 if (!action_rotate_session
->session_name
||
67 strlen(action_rotate_session
->session_name
) == 0) {
77 static bool lttng_action_rotate_session_is_equal(
78 const struct lttng_action
*_a
, const struct lttng_action
*_b
)
80 bool is_equal
= false;
81 const struct lttng_action_rotate_session
*a
, *b
;
83 a
= action_rotate_session_from_action_const(_a
);
84 b
= action_rotate_session_from_action_const(_b
);
86 /* Action is not valid if this is not true. */
87 assert(a
->session_name
);
88 assert(b
->session_name
);
89 if (strcmp(a
->session_name
, b
->session_name
)) {
97 static int lttng_action_rotate_session_serialize(
98 struct lttng_action
*action
, struct lttng_payload
*payload
)
100 struct lttng_action_rotate_session
*action_rotate_session
;
101 struct lttng_action_rotate_session_comm comm
;
102 size_t session_name_len
;
108 action_rotate_session
= action_rotate_session_from_action(action
);
110 assert(action_rotate_session
->session_name
);
112 DBG("Serializing rotate session action: session-name: %s",
113 action_rotate_session
->session_name
);
115 session_name_len
= strlen(action_rotate_session
->session_name
) + 1;
116 comm
.session_name_len
= session_name_len
;
118 ret
= lttng_dynamic_buffer_append(
119 &payload
->buffer
, &comm
, sizeof(comm
));
125 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
126 action_rotate_session
->session_name
, session_name_len
);
137 static void lttng_action_rotate_session_destroy(struct lttng_action
*action
)
139 struct lttng_action_rotate_session
*action_rotate_session
;
145 action_rotate_session
= action_rotate_session_from_action(action
);
147 free(action_rotate_session
->session_name
);
148 free(action_rotate_session
);
154 ssize_t
lttng_action_rotate_session_create_from_payload(
155 struct lttng_payload_view
*view
,
156 struct lttng_action
**p_action
)
158 ssize_t consumed_len
;
159 const struct lttng_action_rotate_session_comm
*comm
;
160 const char *session_name
;
161 struct lttng_action
*action
;
162 enum lttng_action_status status
;
164 action
= lttng_action_rotate_session_create();
170 comm
= (typeof(comm
)) view
->buffer
.data
;
171 session_name
= (const char *) &comm
->data
;
173 if (!lttng_buffer_view_contains_string(
174 &view
->buffer
, session_name
, comm
->session_name_len
)) {
179 status
= lttng_action_rotate_session_set_session_name(
180 action
, session_name
);
181 if (status
!= LTTNG_ACTION_STATUS_OK
) {
186 consumed_len
= sizeof(*comm
) + comm
->session_name_len
;
191 lttng_action_rotate_session_destroy(action
);
196 struct lttng_action
*lttng_action_rotate_session_create(void)
198 struct lttng_action
*action
;
200 action
= zmalloc(sizeof(struct lttng_action_rotate_session
));
205 lttng_action_init(action
, LTTNG_ACTION_TYPE_ROTATE_SESSION
,
206 lttng_action_rotate_session_validate
,
207 lttng_action_rotate_session_serialize
,
208 lttng_action_rotate_session_is_equal
,
209 lttng_action_rotate_session_destroy
);
215 enum lttng_action_status
lttng_action_rotate_session_set_session_name(
216 struct lttng_action
*action
, const char *session_name
)
218 struct lttng_action_rotate_session
*action_rotate_session
;
219 enum lttng_action_status status
;
221 if (!action
|| !IS_ROTATE_SESSION_ACTION(action
) || !session_name
||
222 strlen(session_name
) == 0) {
223 status
= LTTNG_ACTION_STATUS_INVALID
;
227 action_rotate_session
= action_rotate_session_from_action(action
);
229 free(action_rotate_session
->session_name
);
231 action_rotate_session
->session_name
= strdup(session_name
);
232 if (!action_rotate_session
->session_name
) {
233 status
= LTTNG_ACTION_STATUS_ERROR
;
237 status
= LTTNG_ACTION_STATUS_OK
;
242 enum lttng_action_status
lttng_action_rotate_session_get_session_name(
243 const struct lttng_action
*action
, const char **session_name
)
245 const struct lttng_action_rotate_session
*action_rotate_session
;
246 enum lttng_action_status status
;
248 if (!action
|| !IS_ROTATE_SESSION_ACTION(action
) || !session_name
) {
249 status
= LTTNG_ACTION_STATUS_INVALID
;
253 action_rotate_session
= action_rotate_session_from_action_const(action
);
255 *session_name
= action_rotate_session
->session_name
;
257 status
= LTTNG_ACTION_STATUS_OK
;