2 * Copyright (C) 2019 EfficiOS, Inc.
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License, version 2.1 only,
6 * as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <common/error.h>
20 #include <common/macros.h>
21 #include <lttng/action/action-internal.h>
22 #include <lttng/action/rotate-session-internal.h>
23 #include <lttng/action/rotate-session.h>
25 struct lttng_action_rotate_session
{
26 struct lttng_action parent
;
32 struct lttng_action_rotate_session_comm
{
33 /* Includes the trailing \0. */
34 uint32_t session_name_len
;
39 * - session name (null terminated)
44 static struct lttng_action_rotate_session
*action_rotate_session_from_action(
45 struct lttng_action
*action
)
49 return container_of(action
, struct lttng_action_rotate_session
, parent
);
52 static const struct lttng_action_rotate_session
*action_rotate_session_from_action_const(
53 const struct lttng_action
*action
)
57 return container_of(action
, struct lttng_action_rotate_session
, parent
);
60 static bool lttng_action_rotate_session_validate(struct lttng_action
*action
)
63 struct lttng_action_rotate_session
*action_rotate_session
;
70 action_rotate_session
= action_rotate_session_from_action(action
);
72 /* A non-empty session name is mandatory. */
73 if (!action_rotate_session
->session_name
||
74 strlen(action_rotate_session
->session_name
) == 0) {
84 static bool lttng_action_rotate_session_is_equal(const struct lttng_action
*_a
, const struct lttng_action
*_b
)
86 bool is_equal
= false;
87 const struct lttng_action_rotate_session
*a
, *b
;
89 a
= action_rotate_session_from_action_const(_a
);
90 b
= action_rotate_session_from_action_const(_b
);
92 /* Action is not valid if this is not true. */
93 assert(a
->session_name
);
94 assert(b
->session_name
);
95 if (strcmp(a
->session_name
, b
->session_name
)) {
103 static int lttng_action_rotate_session_serialize(
104 struct lttng_action
*action
, struct lttng_dynamic_buffer
*buf
)
106 struct lttng_action_rotate_session
*action_rotate_session
;
107 struct lttng_action_rotate_session_comm comm
;
108 size_t session_name_len
;
114 action_rotate_session
= action_rotate_session_from_action(action
);
116 assert(action_rotate_session
->session_name
);
118 DBG("Serializing rotate session action: session-name: %s",
119 action_rotate_session
->session_name
);
121 session_name_len
= strlen(action_rotate_session
->session_name
) + 1;
122 comm
.session_name_len
= session_name_len
;
124 ret
= lttng_dynamic_buffer_append(buf
, &comm
, sizeof(comm
));
130 ret
= lttng_dynamic_buffer_append(buf
,
131 action_rotate_session
->session_name
, session_name_len
);
142 static void lttng_action_rotate_session_destroy(struct lttng_action
*action
)
144 struct lttng_action_rotate_session
*action_rotate_session
;
150 action_rotate_session
= action_rotate_session_from_action(action
);
152 free(action_rotate_session
->session_name
);
153 free(action_rotate_session
);
159 ssize_t
lttng_action_rotate_session_create_from_buffer(
160 const struct lttng_buffer_view
*view
,
161 struct lttng_action
**p_action
)
163 ssize_t consumed_len
;
164 struct lttng_action_rotate_session_comm
*comm
;
165 const char *session_name
;
166 struct lttng_action
*action
;
167 enum lttng_action_status status
;
169 action
= lttng_action_rotate_session_create();
175 comm
= (struct lttng_action_rotate_session_comm
*) view
->data
;
176 session_name
= (const char *) &comm
->data
;
178 if (!lttng_buffer_view_validate_string(
179 view
, session_name
, comm
->session_name_len
)) {
184 status
= lttng_action_rotate_session_set_session_name(
185 action
, session_name
);
186 if (status
!= LTTNG_ACTION_STATUS_OK
) {
191 consumed_len
= sizeof(struct lttng_action_rotate_session_comm
) +
192 comm
->session_name_len
;
197 lttng_action_rotate_session_destroy(action
);
202 struct lttng_action
*lttng_action_rotate_session_create(void)
204 struct lttng_action
*action
;
206 action
= zmalloc(sizeof(struct lttng_action_rotate_session
));
211 lttng_action_init(action
, LTTNG_ACTION_TYPE_ROTATE_SESSION
,
212 lttng_action_rotate_session_validate
,
213 lttng_action_rotate_session_serialize
,
214 lttng_action_rotate_session_is_equal
,
215 lttng_action_rotate_session_destroy
);
221 enum lttng_action_status
lttng_action_rotate_session_set_session_name(
222 struct lttng_action
*action
, const char *session_name
)
224 struct lttng_action_rotate_session
*action_rotate_session
;
225 enum lttng_action_status status
;
227 if (!action
|| !session_name
|| strlen(session_name
) == 0) {
228 status
= LTTNG_ACTION_STATUS_INVALID
;
232 action_rotate_session
= action_rotate_session_from_action(action
);
234 free(action_rotate_session
->session_name
);
236 action_rotate_session
->session_name
= strdup(session_name
);
237 if (!action_rotate_session
->session_name
) {
238 status
= LTTNG_ACTION_STATUS_ERROR
;
242 status
= LTTNG_ACTION_STATUS_OK
;
247 enum lttng_action_status
lttng_action_rotate_session_get_session_name(
248 const struct lttng_action
*action
, const char **session_name
)
250 const struct lttng_action_rotate_session
*action_rotate_session
;
251 enum lttng_action_status status
;
253 if (!action
|| !session_name
) {
254 status
= LTTNG_ACTION_STATUS_INVALID
;
258 action_rotate_session
= action_rotate_session_from_action_const(action
);
260 *session_name
= action_rotate_session
->session_name
;
262 status
= LTTNG_ACTION_STATUS_OK
;