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/start-session-internal.h>
13 #include <lttng/action/start-session.h>
15 #define IS_START_SESSION_ACTION(action) \
16 (lttng_action_get_type(action) == LTTNG_ACTION_TYPE_START_SESSION)
18 struct lttng_action_start_session
{
19 struct lttng_action parent
;
25 struct lttng_action_start_session_comm
{
26 /* Includes the trailing \0. */
27 uint32_t session_name_len
;
32 * - session name (null terminated)
37 static struct lttng_action_start_session
*action_start_session_from_action(
38 struct lttng_action
*action
)
42 return container_of(action
, struct lttng_action_start_session
, parent
);
45 static const struct lttng_action_start_session
*
46 action_start_session_from_action_const(const struct lttng_action
*action
)
50 return container_of(action
, struct lttng_action_start_session
, parent
);
53 static bool lttng_action_start_session_validate(struct lttng_action
*action
)
56 struct lttng_action_start_session
*action_start_session
;
63 action_start_session
= action_start_session_from_action(action
);
65 /* A non-empty session name is mandatory. */
66 if (!action_start_session
->session_name
||
67 strlen(action_start_session
->session_name
) == 0) {
77 static bool lttng_action_start_session_is_equal(
78 const struct lttng_action
*_a
, const struct lttng_action
*_b
)
80 bool is_equal
= false;
81 struct lttng_action_start_session
*a
, *b
;
83 a
= container_of(_a
, struct lttng_action_start_session
, parent
);
84 b
= container_of(_b
, struct lttng_action_start_session
, parent
);
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_start_session_serialize(
99 struct lttng_action
*action
, struct lttng_payload
*payload
)
101 struct lttng_action_start_session
*action_start_session
;
102 struct lttng_action_start_session_comm comm
;
103 size_t session_name_len
;
109 action_start_session
= action_start_session_from_action(action
);
111 assert(action_start_session
->session_name
);
113 DBG("Serializing start session action: session-name: %s",
114 action_start_session
->session_name
);
116 session_name_len
= strlen(action_start_session
->session_name
) + 1;
117 comm
.session_name_len
= session_name_len
;
119 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &comm
, sizeof(comm
));
125 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
126 action_start_session
->session_name
, session_name_len
);
137 static void lttng_action_start_session_destroy(struct lttng_action
*action
)
139 struct lttng_action_start_session
*action_start_session
;
145 action_start_session
= action_start_session_from_action(action
);
147 free(action_start_session
->session_name
);
148 free(action_start_session
);
154 ssize_t
lttng_action_start_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_start_session_comm
*comm
;
160 const char *session_name
;
161 struct lttng_action
*action
;
162 enum lttng_action_status status
;
164 action
= lttng_action_start_session_create();
170 comm
= (typeof(comm
)) view
->buffer
.data
;
171 session_name
= (const char *) &comm
->data
;
173 if (!lttng_buffer_view_contains_string(&view
->buffer
, session_name
,
174 comm
->session_name_len
)) {
179 status
= lttng_action_start_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_start_session_destroy(action
);
196 struct lttng_action
*lttng_action_start_session_create(void)
198 struct lttng_action
*action
;
200 action
= zmalloc(sizeof(struct lttng_action_start_session
));
205 lttng_action_init(action
, LTTNG_ACTION_TYPE_START_SESSION
,
206 lttng_action_start_session_validate
,
207 lttng_action_start_session_serialize
,
208 lttng_action_start_session_is_equal
,
209 lttng_action_start_session_destroy
);
215 enum lttng_action_status
lttng_action_start_session_set_session_name(
216 struct lttng_action
*action
, const char *session_name
)
218 struct lttng_action_start_session
*action_start_session
;
219 enum lttng_action_status status
;
221 if (!action
|| !IS_START_SESSION_ACTION(action
) || !session_name
||
222 strlen(session_name
) == 0) {
223 status
= LTTNG_ACTION_STATUS_INVALID
;
227 action_start_session
= action_start_session_from_action(action
);
229 free(action_start_session
->session_name
);
231 action_start_session
->session_name
= strdup(session_name
);
232 if (!action_start_session
->session_name
) {
233 status
= LTTNG_ACTION_STATUS_ERROR
;
237 status
= LTTNG_ACTION_STATUS_OK
;
242 enum lttng_action_status
lttng_action_start_session_get_session_name(
243 const struct lttng_action
*action
, const char **session_name
)
245 const struct lttng_action_start_session
*action_start_session
;
246 enum lttng_action_status status
;
248 if (!action
|| !IS_START_SESSION_ACTION(action
) || !session_name
) {
249 status
= LTTNG_ACTION_STATUS_INVALID
;
253 action_start_session
= action_start_session_from_action_const(action
);
255 *session_name
= action_start_session
->session_name
;
257 status
= LTTNG_ACTION_STATUS_OK
;