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/firing-policy-internal.h>
13 #include <lttng/action/firing-policy.h>
14 #include <lttng/action/stop-session-internal.h>
15 #include <lttng/action/stop-session.h>
17 #define IS_STOP_SESSION_ACTION(action) \
18 (lttng_action_get_type(action) == LTTNG_ACTION_TYPE_STOP_SESSION)
20 struct lttng_action_stop_session
{
21 struct lttng_action parent
;
25 struct lttng_firing_policy
*policy
;
28 struct lttng_action_stop_session_comm
{
29 /* Includes the trailing \0. */
30 uint32_t session_name_len
;
35 * - session name (null terminated)
41 static struct lttng_action_stop_session
*action_stop_session_from_action(
42 struct lttng_action
*action
)
46 return container_of(action
, struct lttng_action_stop_session
, parent
);
49 static const struct lttng_action_stop_session
*
50 action_stop_session_from_action_const(const struct lttng_action
*action
)
54 return container_of(action
, struct lttng_action_stop_session
, parent
);
57 static bool lttng_action_stop_session_validate(struct lttng_action
*action
)
60 struct lttng_action_stop_session
*action_stop_session
;
67 action_stop_session
= action_stop_session_from_action(action
);
69 /* A non-empty session name is mandatory. */
70 if (!action_stop_session
->session_name
||
71 strlen(action_stop_session
->session_name
) == 0) {
81 static bool lttng_action_stop_session_is_equal(
82 const struct lttng_action
*_a
, const struct lttng_action
*_b
)
84 bool is_equal
= false;
85 const struct lttng_action_stop_session
*a
, *b
;
87 a
= action_stop_session_from_action_const(_a
);
88 b
= action_stop_session_from_action_const(_b
);
90 /* Action is not valid if this is not true. */
91 assert(a
->session_name
);
92 assert(b
->session_name
);
93 if (strcmp(a
->session_name
, b
->session_name
)) {
97 is_equal
= lttng_firing_policy_is_equal(a
->policy
, b
->policy
);
102 static int lttng_action_stop_session_serialize(
103 struct lttng_action
*action
, struct lttng_payload
*payload
)
105 struct lttng_action_stop_session
*action_stop_session
;
106 struct lttng_action_stop_session_comm comm
;
107 size_t session_name_len
;
113 action_stop_session
= action_stop_session_from_action(action
);
115 assert(action_stop_session
->session_name
);
117 DBG("Serializing stop session action: session-name: %s",
118 action_stop_session
->session_name
);
120 session_name_len
= strlen(action_stop_session
->session_name
) + 1;
121 comm
.session_name_len
= session_name_len
;
123 ret
= lttng_dynamic_buffer_append(
124 &payload
->buffer
, &comm
, sizeof(comm
));
130 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
131 action_stop_session
->session_name
, session_name_len
);
137 ret
= lttng_firing_policy_serialize(
138 action_stop_session
->policy
, payload
);
149 static void lttng_action_stop_session_destroy(struct lttng_action
*action
)
151 struct lttng_action_stop_session
*action_stop_session
;
157 action_stop_session
= action_stop_session_from_action(action
);
159 lttng_firing_policy_destroy(action_stop_session
->policy
);
160 free(action_stop_session
->session_name
);
161 free(action_stop_session
);
167 ssize_t
lttng_action_stop_session_create_from_payload(
168 struct lttng_payload_view
*view
,
169 struct lttng_action
**p_action
)
171 ssize_t consumed_len
, ret
;
172 const struct lttng_action_stop_session_comm
*comm
;
173 const char *session_name
;
174 struct lttng_action
*action
= NULL
;
175 enum lttng_action_status status
;
176 struct lttng_firing_policy
*policy
= NULL
;
178 comm
= (typeof(comm
)) view
->buffer
.data
;
179 session_name
= (const char *) &comm
->data
;
182 if (!lttng_buffer_view_contains_string(
183 &view
->buffer
, session_name
, comm
->session_name_len
)) {
187 consumed_len
= sizeof(*comm
) + comm
->session_name_len
;
191 struct lttng_payload_view policy_view
=
192 lttng_payload_view_from_view(
193 view
, consumed_len
, -1);
194 ret
= lttng_firing_policy_create_from_payload(
195 &policy_view
, &policy
);
203 action
= lttng_action_stop_session_create();
209 status
= lttng_action_stop_session_set_session_name(
210 action
, session_name
);
211 if (status
!= LTTNG_ACTION_STATUS_OK
) {
217 status
= lttng_action_stop_session_set_firing_policy(action
, policy
);
218 if (status
!= LTTNG_ACTION_STATUS_OK
) {
227 lttng_firing_policy_destroy(policy
);
228 lttng_action_stop_session_destroy(action
);
233 struct lttng_action
*lttng_action_stop_session_create(void)
235 struct lttng_action
*action
= NULL
;
236 struct lttng_firing_policy
*policy
= NULL
;
237 enum lttng_action_status status
;
239 /* Create a every N = 1 firing policy. */
240 policy
= lttng_firing_policy_every_n_create(1);
245 action
= zmalloc(sizeof(struct lttng_action_stop_session
));
250 lttng_action_init(action
, LTTNG_ACTION_TYPE_STOP_SESSION
,
251 lttng_action_stop_session_validate
,
252 lttng_action_stop_session_serialize
,
253 lttng_action_stop_session_is_equal
,
254 lttng_action_stop_session_destroy
);
256 status
= lttng_action_stop_session_set_firing_policy(action
, policy
);
257 if (status
!= LTTNG_ACTION_STATUS_OK
) {
264 lttng_firing_policy_destroy(policy
);
268 enum lttng_action_status
lttng_action_stop_session_set_session_name(
269 struct lttng_action
*action
, const char *session_name
)
271 struct lttng_action_stop_session
*action_stop_session
;
272 enum lttng_action_status status
;
274 if (!action
|| !IS_STOP_SESSION_ACTION(action
) || !session_name
||
275 strlen(session_name
) == 0) {
276 status
= LTTNG_ACTION_STATUS_INVALID
;
280 action_stop_session
= action_stop_session_from_action(action
);
282 free(action_stop_session
->session_name
);
284 action_stop_session
->session_name
= strdup(session_name
);
285 if (!action_stop_session
->session_name
) {
286 status
= LTTNG_ACTION_STATUS_ERROR
;
290 status
= LTTNG_ACTION_STATUS_OK
;
295 enum lttng_action_status
lttng_action_stop_session_get_session_name(
296 const struct lttng_action
*action
, const char **session_name
)
298 const struct lttng_action_stop_session
*action_stop_session
;
299 enum lttng_action_status status
;
301 if (!action
|| !IS_STOP_SESSION_ACTION(action
) || !session_name
) {
302 status
= LTTNG_ACTION_STATUS_INVALID
;
306 action_stop_session
= action_stop_session_from_action_const(action
);
308 *session_name
= action_stop_session
->session_name
;
310 status
= LTTNG_ACTION_STATUS_OK
;
315 enum lttng_action_status
lttng_action_stop_session_set_firing_policy(
316 struct lttng_action
*action
,
317 const struct lttng_firing_policy
*policy
)
319 enum lttng_action_status status
;
320 struct lttng_action_stop_session
*stop_session_action
;
321 struct lttng_firing_policy
*copy
= NULL
;
323 if (!action
|| !policy
|| !IS_STOP_SESSION_ACTION(action
)) {
324 status
= LTTNG_ACTION_STATUS_INVALID
;
328 copy
= lttng_firing_policy_copy(policy
);
330 status
= LTTNG_ACTION_STATUS_ERROR
;
333 stop_session_action
= action_stop_session_from_action(action
);
335 /* Free the previous firing policy .*/
336 lttng_firing_policy_destroy(stop_session_action
->policy
);
338 stop_session_action
->policy
= copy
;
339 status
= LTTNG_ACTION_STATUS_OK
;
343 lttng_firing_policy_destroy(copy
);
347 enum lttng_action_status
lttng_action_stop_session_get_firing_policy(
348 const struct lttng_action
*action
,
349 const struct lttng_firing_policy
**policy
)
351 enum lttng_action_status status
;
352 const struct lttng_action_stop_session
*stop_session_action
;
354 if (!action
|| !policy
|| !IS_STOP_SESSION_ACTION(action
)) {
355 status
= LTTNG_ACTION_STATUS_INVALID
;
359 stop_session_action
= action_stop_session_from_action_const(action
);
361 *policy
= stop_session_action
->policy
;
362 status
= LTTNG_ACTION_STATUS_OK
;