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/stop-session-internal.h>
13 #include <lttng/action/stop-session.h>
15 #define IS_STOP_SESSION_ACTION(action) \
16 (lttng_action_get_type(action) == LTTNG_ACTION_TYPE_STOP_SESSION)
18 struct lttng_action_stop_session
{
19 struct lttng_action parent
;
25 struct lttng_action_stop_session_comm
{
26 /* Includes the trailing \0. */
27 uint32_t session_name_len
;
32 * - session name (null terminated)
37 static struct lttng_action_stop_session
*action_stop_session_from_action(
38 struct lttng_action
*action
)
42 return container_of(action
, struct lttng_action_stop_session
, parent
);
45 static const struct lttng_action_stop_session
*
46 action_stop_session_from_action_const(const struct lttng_action
*action
)
50 return container_of(action
, struct lttng_action_stop_session
, parent
);
53 static bool lttng_action_stop_session_validate(struct lttng_action
*action
)
56 struct lttng_action_stop_session
*action_stop_session
;
63 action_stop_session
= action_stop_session_from_action(action
);
65 /* A non-empty session name is mandatory. */
66 if (!action_stop_session
->session_name
||
67 strlen(action_stop_session
->session_name
) == 0) {
77 static bool lttng_action_stop_session_is_equal(
78 const struct lttng_action
*_a
, const struct lttng_action
*_b
)
80 bool is_equal
= false;
81 const struct lttng_action_stop_session
*a
, *b
;
83 a
= action_stop_session_from_action_const(_a
);
84 b
= action_stop_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
)) {
98 static int lttng_action_stop_session_serialize(
99 struct lttng_action
*action
, struct lttng_payload
*payload
)
101 struct lttng_action_stop_session
*action_stop_session
;
102 struct lttng_action_stop_session_comm comm
;
103 size_t session_name_len
;
109 action_stop_session
= action_stop_session_from_action(action
);
111 assert(action_stop_session
->session_name
);
113 DBG("Serializing stop session action: session-name: %s",
114 action_stop_session
->session_name
);
116 session_name_len
= strlen(action_stop_session
->session_name
) + 1;
117 comm
.session_name_len
= session_name_len
;
119 ret
= lttng_dynamic_buffer_append(
120 &payload
->buffer
, &comm
, sizeof(comm
));
126 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
127 action_stop_session
->session_name
, session_name_len
);
138 static void lttng_action_stop_session_destroy(struct lttng_action
*action
)
140 struct lttng_action_stop_session
*action_stop_session
;
146 action_stop_session
= action_stop_session_from_action(action
);
148 free(action_stop_session
->session_name
);
149 free(action_stop_session
);
155 ssize_t
lttng_action_stop_session_create_from_payload(
156 struct lttng_payload_view
*view
,
157 struct lttng_action
**p_action
)
159 ssize_t consumed_len
;
160 const struct lttng_action_stop_session_comm
*comm
;
161 const char *session_name
;
162 struct lttng_action
*action
;
163 enum lttng_action_status status
;
165 action
= lttng_action_stop_session_create();
171 comm
= (typeof(comm
)) view
->buffer
.data
;
172 session_name
= (const char *) &comm
->data
;
174 if (!lttng_buffer_view_contains_string(
175 &view
->buffer
, session_name
, comm
->session_name_len
)) {
180 status
= lttng_action_stop_session_set_session_name(
181 action
, session_name
);
182 if (status
!= LTTNG_ACTION_STATUS_OK
) {
187 consumed_len
= sizeof(*comm
) + comm
->session_name_len
;
192 lttng_action_stop_session_destroy(action
);
197 struct lttng_action
*lttng_action_stop_session_create(void)
199 struct lttng_action
*action
;
201 action
= zmalloc(sizeof(struct lttng_action_stop_session
));
206 lttng_action_init(action
, LTTNG_ACTION_TYPE_STOP_SESSION
,
207 lttng_action_stop_session_validate
,
208 lttng_action_stop_session_serialize
,
209 lttng_action_stop_session_is_equal
,
210 lttng_action_stop_session_destroy
);
216 enum lttng_action_status
lttng_action_stop_session_set_session_name(
217 struct lttng_action
*action
, const char *session_name
)
219 struct lttng_action_stop_session
*action_stop_session
;
220 enum lttng_action_status status
;
222 if (!action
|| !IS_STOP_SESSION_ACTION(action
) || !session_name
||
223 strlen(session_name
) == 0) {
224 status
= LTTNG_ACTION_STATUS_INVALID
;
228 action_stop_session
= action_stop_session_from_action(action
);
230 free(action_stop_session
->session_name
);
232 action_stop_session
->session_name
= strdup(session_name
);
233 if (!action_stop_session
->session_name
) {
234 status
= LTTNG_ACTION_STATUS_ERROR
;
238 status
= LTTNG_ACTION_STATUS_OK
;
243 enum lttng_action_status
lttng_action_stop_session_get_session_name(
244 const struct lttng_action
*action
, const char **session_name
)
246 const struct lttng_action_stop_session
*action_stop_session
;
247 enum lttng_action_status status
;
249 if (!action
|| !IS_STOP_SESSION_ACTION(action
) || !session_name
) {
250 status
= LTTNG_ACTION_STATUS_INVALID
;
254 action_stop_session
= action_stop_session_from_action_const(action
);
256 *session_name
= action_stop_session
->session_name
;
258 status
= LTTNG_ACTION_STATUS_OK
;