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/rate-policy-internal.h>
13 #include <lttng/action/rate-policy.h>
14 #include <lttng/action/rotate-session-internal.h>
15 #include <lttng/action/rotate-session.h>
17 #define IS_ROTATE_SESSION_ACTION(action) \
18 (lttng_action_get_type(action) == LTTNG_ACTION_TYPE_ROTATE_SESSION)
20 struct lttng_action_rotate_session
{
21 struct lttng_action parent
;
25 struct lttng_rate_policy
*policy
;
28 struct lttng_action_rotate_session_comm
{
29 /* Includes the trailing \0. */
30 uint32_t session_name_len
;
35 * - session name (null terminated)
41 static const struct lttng_rate_policy
*
42 lttng_action_rotate_session_internal_get_rate_policy(
43 const struct lttng_action
*action
);
45 static struct lttng_action_rotate_session
*action_rotate_session_from_action(
46 struct lttng_action
*action
)
50 return container_of(action
, struct lttng_action_rotate_session
, parent
);
53 static const struct lttng_action_rotate_session
*
54 action_rotate_session_from_action_const(const struct lttng_action
*action
)
58 return container_of(action
, struct lttng_action_rotate_session
, parent
);
61 static bool lttng_action_rotate_session_validate(struct lttng_action
*action
)
64 struct lttng_action_rotate_session
*action_rotate_session
;
71 action_rotate_session
= action_rotate_session_from_action(action
);
73 /* A non-empty session name is mandatory. */
74 if (!action_rotate_session
->session_name
||
75 strlen(action_rotate_session
->session_name
) == 0) {
85 static bool lttng_action_rotate_session_is_equal(
86 const struct lttng_action
*_a
, const struct lttng_action
*_b
)
88 bool is_equal
= false;
89 const struct lttng_action_rotate_session
*a
, *b
;
91 a
= action_rotate_session_from_action_const(_a
);
92 b
= action_rotate_session_from_action_const(_b
);
94 /* Action is not valid if this is not true. */
95 assert(a
->session_name
);
96 assert(b
->session_name
);
97 if (strcmp(a
->session_name
, b
->session_name
)) {
101 is_equal
= lttng_rate_policy_is_equal(a
->policy
, b
->policy
);
105 static int lttng_action_rotate_session_serialize(
106 struct lttng_action
*action
, struct lttng_payload
*payload
)
108 struct lttng_action_rotate_session
*action_rotate_session
;
109 struct lttng_action_rotate_session_comm comm
;
110 size_t session_name_len
;
116 action_rotate_session
= action_rotate_session_from_action(action
);
118 assert(action_rotate_session
->session_name
);
120 DBG("Serializing rotate session action: session-name: %s",
121 action_rotate_session
->session_name
);
123 session_name_len
= strlen(action_rotate_session
->session_name
) + 1;
124 comm
.session_name_len
= session_name_len
;
126 ret
= lttng_dynamic_buffer_append(
127 &payload
->buffer
, &comm
, sizeof(comm
));
133 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
134 action_rotate_session
->session_name
, session_name_len
);
140 ret
= lttng_rate_policy_serialize(
141 action_rotate_session
->policy
, payload
);
150 static void lttng_action_rotate_session_destroy(struct lttng_action
*action
)
152 struct lttng_action_rotate_session
*action_rotate_session
;
158 action_rotate_session
= action_rotate_session_from_action(action
);
160 lttng_rate_policy_destroy(action_rotate_session
->policy
);
161 free(action_rotate_session
->session_name
);
162 free(action_rotate_session
);
168 ssize_t
lttng_action_rotate_session_create_from_payload(
169 struct lttng_payload_view
*view
,
170 struct lttng_action
**p_action
)
172 ssize_t consumed_len
, ret
;
173 const struct lttng_action_rotate_session_comm
*comm
;
174 const char *session_name
;
175 struct lttng_action
*action
;
176 enum lttng_action_status status
;
177 struct lttng_rate_policy
*policy
= NULL
;
179 action
= lttng_action_rotate_session_create();
185 comm
= (typeof(comm
)) view
->buffer
.data
;
186 session_name
= (const char *) &comm
->data
;
188 if (!lttng_buffer_view_contains_string(
189 &view
->buffer
, session_name
, comm
->session_name_len
)) {
193 consumed_len
= sizeof(*comm
) + comm
->session_name_len
;
197 struct lttng_payload_view policy_view
=
198 lttng_payload_view_from_view(
199 view
, consumed_len
, -1);
200 ret
= lttng_rate_policy_create_from_payload(
201 &policy_view
, &policy
);
209 status
= lttng_action_rotate_session_set_session_name(
210 action
, session_name
);
211 if (status
!= LTTNG_ACTION_STATUS_OK
) {
217 status
= lttng_action_rotate_session_set_rate_policy(action
, policy
);
218 if (status
!= LTTNG_ACTION_STATUS_OK
) {
227 lttng_rate_policy_destroy(policy
);
228 lttng_action_rotate_session_destroy(action
);
233 struct lttng_action
*lttng_action_rotate_session_create(void)
235 struct lttng_action
*action
= NULL
;
236 struct lttng_rate_policy
*policy
= NULL
;
237 enum lttng_action_status status
;
239 /* Create a every N = 1 rate policy. */
240 policy
= lttng_rate_policy_every_n_create(1);
245 action
= zmalloc(sizeof(struct lttng_action_rotate_session
));
250 lttng_action_init(action
, LTTNG_ACTION_TYPE_ROTATE_SESSION
,
251 lttng_action_rotate_session_validate
,
252 lttng_action_rotate_session_serialize
,
253 lttng_action_rotate_session_is_equal
,
254 lttng_action_rotate_session_destroy
,
255 lttng_action_rotate_session_internal_get_rate_policy
,
256 lttng_action_generic_add_error_query_results
);
258 status
= lttng_action_rotate_session_set_rate_policy(action
, policy
);
259 if (status
!= LTTNG_ACTION_STATUS_OK
) {
266 lttng_rate_policy_destroy(policy
);
270 enum lttng_action_status
lttng_action_rotate_session_set_session_name(
271 struct lttng_action
*action
, const char *session_name
)
273 struct lttng_action_rotate_session
*action_rotate_session
;
274 enum lttng_action_status status
;
276 if (!action
|| !IS_ROTATE_SESSION_ACTION(action
) || !session_name
||
277 strlen(session_name
) == 0) {
278 status
= LTTNG_ACTION_STATUS_INVALID
;
282 action_rotate_session
= action_rotate_session_from_action(action
);
284 free(action_rotate_session
->session_name
);
286 action_rotate_session
->session_name
= strdup(session_name
);
287 if (!action_rotate_session
->session_name
) {
288 status
= LTTNG_ACTION_STATUS_ERROR
;
292 status
= LTTNG_ACTION_STATUS_OK
;
297 enum lttng_action_status
lttng_action_rotate_session_get_session_name(
298 const struct lttng_action
*action
, const char **session_name
)
300 const struct lttng_action_rotate_session
*action_rotate_session
;
301 enum lttng_action_status status
;
303 if (!action
|| !IS_ROTATE_SESSION_ACTION(action
) || !session_name
) {
304 status
= LTTNG_ACTION_STATUS_INVALID
;
308 action_rotate_session
= action_rotate_session_from_action_const(action
);
310 *session_name
= action_rotate_session
->session_name
;
312 status
= LTTNG_ACTION_STATUS_OK
;
317 enum lttng_action_status
lttng_action_rotate_session_set_rate_policy(
318 struct lttng_action
*action
,
319 const struct lttng_rate_policy
*policy
)
321 enum lttng_action_status status
;
322 struct lttng_action_rotate_session
*rotate_session_action
;
323 struct lttng_rate_policy
*copy
= NULL
;
325 if (!action
|| !policy
|| !IS_ROTATE_SESSION_ACTION(action
)) {
326 status
= LTTNG_ACTION_STATUS_INVALID
;
330 copy
= lttng_rate_policy_copy(policy
);
332 status
= LTTNG_ACTION_STATUS_ERROR
;
336 rotate_session_action
= action_rotate_session_from_action(action
);
338 /* Free the previous rate policy .*/
339 lttng_rate_policy_destroy(rotate_session_action
->policy
);
341 /* Assign the policy. */
342 rotate_session_action
->policy
= copy
;
343 status
= LTTNG_ACTION_STATUS_OK
;
347 lttng_rate_policy_destroy(copy
);
351 enum lttng_action_status
lttng_action_rotate_session_get_rate_policy(
352 const struct lttng_action
*action
,
353 const struct lttng_rate_policy
**policy
)
355 enum lttng_action_status status
;
356 const struct lttng_action_rotate_session
*rotate_session_action
;
358 if (!action
|| !policy
|| !IS_ROTATE_SESSION_ACTION(action
)) {
359 status
= LTTNG_ACTION_STATUS_INVALID
;
363 rotate_session_action
= action_rotate_session_from_action_const(action
);
365 *policy
= rotate_session_action
->policy
;
366 status
= LTTNG_ACTION_STATUS_OK
;
371 static const struct lttng_rate_policy
*
372 lttng_action_rotate_session_internal_get_rate_policy(
373 const struct lttng_action
*action
)
375 const struct lttng_action_rotate_session
*_action
;
376 _action
= action_rotate_session_from_action_const(action
);
378 return _action
->policy
;