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/start-session-internal.h>
15 #include <lttng/action/start-session.h>
17 #define IS_START_SESSION_ACTION(action) \
18 (lttng_action_get_type(action) == LTTNG_ACTION_TYPE_START_SESSION)
20 struct lttng_action_start_session
{
21 struct lttng_action parent
;
25 struct lttng_rate_policy
*policy
;
28 struct lttng_action_start_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_start_session_internal_get_rate_policy(
43 const struct lttng_action
*action
);
45 static struct lttng_action_start_session
*action_start_session_from_action(
46 struct lttng_action
*action
)
50 return container_of(action
, struct lttng_action_start_session
, parent
);
53 static const struct lttng_action_start_session
*
54 action_start_session_from_action_const(const struct lttng_action
*action
)
58 return container_of(action
, struct lttng_action_start_session
, parent
);
61 static bool lttng_action_start_session_validate(struct lttng_action
*action
)
64 struct lttng_action_start_session
*action_start_session
;
71 action_start_session
= action_start_session_from_action(action
);
73 /* A non-empty session name is mandatory. */
74 if (!action_start_session
->session_name
||
75 strlen(action_start_session
->session_name
) == 0) {
85 static bool lttng_action_start_session_is_equal(
86 const struct lttng_action
*_a
, const struct lttng_action
*_b
)
88 bool is_equal
= false;
89 struct lttng_action_start_session
*a
, *b
;
91 a
= container_of(_a
, struct lttng_action_start_session
, parent
);
92 b
= container_of(_b
, struct lttng_action_start_session
, parent
);
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
);
106 static int lttng_action_start_session_serialize(
107 struct lttng_action
*action
, struct lttng_payload
*payload
)
109 struct lttng_action_start_session
*action_start_session
;
110 struct lttng_action_start_session_comm comm
;
111 size_t session_name_len
;
117 action_start_session
= action_start_session_from_action(action
);
119 assert(action_start_session
->session_name
);
121 DBG("Serializing start session action: session-name: %s",
122 action_start_session
->session_name
);
124 session_name_len
= strlen(action_start_session
->session_name
) + 1;
125 comm
.session_name_len
= session_name_len
;
127 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &comm
, sizeof(comm
));
133 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
134 action_start_session
->session_name
, session_name_len
);
140 ret
= lttng_rate_policy_serialize(
141 action_start_session
->policy
, payload
);
152 static void lttng_action_start_session_destroy(struct lttng_action
*action
)
154 struct lttng_action_start_session
*action_start_session
;
160 action_start_session
= action_start_session_from_action(action
);
162 lttng_rate_policy_destroy(action_start_session
->policy
);
163 free(action_start_session
->session_name
);
164 free(action_start_session
);
170 ssize_t
lttng_action_start_session_create_from_payload(
171 struct lttng_payload_view
*view
,
172 struct lttng_action
**p_action
)
174 ssize_t consumed_len
, ret
;
175 const struct lttng_action_start_session_comm
*comm
;
176 const char *session_name
;
177 struct lttng_action
*action
= NULL
;
178 enum lttng_action_status status
;
179 struct lttng_rate_policy
*policy
= NULL
;
181 comm
= (typeof(comm
)) view
->buffer
.data
;
182 session_name
= (const char *) &comm
->data
;
185 if (!lttng_buffer_view_contains_string(&view
->buffer
, session_name
,
186 comm
->session_name_len
)) {
190 consumed_len
= sizeof(*comm
) + comm
->session_name_len
;
194 struct lttng_payload_view policy_view
=
195 lttng_payload_view_from_view(
196 view
, consumed_len
, -1);
197 ret
= lttng_rate_policy_create_from_payload(
198 &policy_view
, &policy
);
206 action
= lttng_action_start_session_create();
212 status
= lttng_action_start_session_set_session_name(
213 action
, session_name
);
214 if (status
!= LTTNG_ACTION_STATUS_OK
) {
220 status
= lttng_action_start_session_set_rate_policy(action
, policy
);
221 if (status
!= LTTNG_ACTION_STATUS_OK
) {
230 lttng_rate_policy_destroy(policy
);
231 lttng_action_start_session_destroy(action
);
236 struct lttng_action
*lttng_action_start_session_create(void)
238 struct lttng_action
*action
= NULL
;
239 struct lttng_rate_policy
*policy
= NULL
;
240 enum lttng_action_status status
;
242 /* Create a every N = 1 rate policy. */
243 policy
= lttng_rate_policy_every_n_create(1);
248 action
= zmalloc(sizeof(struct lttng_action_start_session
));
253 lttng_action_init(action
, LTTNG_ACTION_TYPE_START_SESSION
,
254 lttng_action_start_session_validate
,
255 lttng_action_start_session_serialize
,
256 lttng_action_start_session_is_equal
,
257 lttng_action_start_session_destroy
,
258 lttng_action_start_session_internal_get_rate_policy
,
259 lttng_action_generic_add_error_query_results
);
261 status
= lttng_action_start_session_set_rate_policy(action
, policy
);
262 if (status
!= LTTNG_ACTION_STATUS_OK
) {
269 lttng_rate_policy_destroy(policy
);
273 enum lttng_action_status
lttng_action_start_session_set_session_name(
274 struct lttng_action
*action
, const char *session_name
)
276 struct lttng_action_start_session
*action_start_session
;
277 enum lttng_action_status status
;
279 if (!action
|| !IS_START_SESSION_ACTION(action
) || !session_name
||
280 strlen(session_name
) == 0) {
281 status
= LTTNG_ACTION_STATUS_INVALID
;
285 action_start_session
= action_start_session_from_action(action
);
287 free(action_start_session
->session_name
);
289 action_start_session
->session_name
= strdup(session_name
);
290 if (!action_start_session
->session_name
) {
291 status
= LTTNG_ACTION_STATUS_ERROR
;
295 status
= LTTNG_ACTION_STATUS_OK
;
300 enum lttng_action_status
lttng_action_start_session_get_session_name(
301 const struct lttng_action
*action
, const char **session_name
)
303 const struct lttng_action_start_session
*action_start_session
;
304 enum lttng_action_status status
;
306 if (!action
|| !IS_START_SESSION_ACTION(action
) || !session_name
) {
307 status
= LTTNG_ACTION_STATUS_INVALID
;
311 action_start_session
= action_start_session_from_action_const(action
);
313 *session_name
= action_start_session
->session_name
;
315 status
= LTTNG_ACTION_STATUS_OK
;
320 enum lttng_action_status
lttng_action_start_session_set_rate_policy(
321 struct lttng_action
*action
,
322 const struct lttng_rate_policy
*policy
)
324 enum lttng_action_status status
;
325 struct lttng_action_start_session
*start_session_action
;
326 struct lttng_rate_policy
*copy
= NULL
;
328 if (!action
|| !policy
|| !IS_START_SESSION_ACTION(action
)) {
329 status
= LTTNG_ACTION_STATUS_INVALID
;
333 copy
= lttng_rate_policy_copy(policy
);
335 status
= LTTNG_ACTION_STATUS_ERROR
;
339 start_session_action
= action_start_session_from_action(action
);
341 /* Release the previous rate policy .*/
342 lttng_rate_policy_destroy(start_session_action
->policy
);
344 /* Assign the policy. */
345 start_session_action
->policy
= copy
;
346 status
= LTTNG_ACTION_STATUS_OK
;
350 lttng_rate_policy_destroy(copy
);
354 enum lttng_action_status
lttng_action_start_session_get_rate_policy(
355 const struct lttng_action
*action
,
356 const struct lttng_rate_policy
**policy
)
358 enum lttng_action_status status
;
359 const struct lttng_action_start_session
*start_session_action
;
361 if (!action
|| !policy
|| !IS_START_SESSION_ACTION(action
)) {
362 status
= LTTNG_ACTION_STATUS_INVALID
;
366 start_session_action
= action_start_session_from_action_const(action
);
368 *policy
= start_session_action
->policy
;
369 status
= LTTNG_ACTION_STATUS_OK
;
374 static const struct lttng_rate_policy
*
375 lttng_action_start_session_internal_get_rate_policy(
376 const struct lttng_action
*action
)
378 const struct lttng_action_start_session
*_action
;
379 _action
= action_start_session_from_action_const(action
);
381 return _action
->policy
;