actions: introduce rotate session action
[lttng-tools.git] / src / common / actions / rotate-session.c
1 /*
2 * Copyright (C) 2019 Simon Marchi <simon.marchi@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #include <assert.h>
9 #include <common/error.h>
10 #include <common/macros.h>
11 #include <lttng/action/action-internal.h>
12 #include <lttng/action/rotate-session-internal.h>
13 #include <lttng/action/rotate-session.h>
14
15 #define IS_ROTATE_SESSION_ACTION(action) \
16 (lttng_action_get_type_const(action) == LTTNG_ACTION_TYPE_ROTATE_SESSION)
17
18 struct lttng_action_rotate_session {
19 struct lttng_action parent;
20
21 /* Owned by this. */
22 char *session_name;
23 };
24
25 struct lttng_action_rotate_session_comm {
26 /* Includes the trailing \0. */
27 uint32_t session_name_len;
28
29 /*
30 * Variable data:
31 *
32 * - session name (null terminated)
33 */
34 char data[];
35 } LTTNG_PACKED;
36
37 static struct lttng_action_rotate_session *action_rotate_session_from_action(
38 struct lttng_action *action)
39 {
40 assert(action);
41
42 return container_of(action, struct lttng_action_rotate_session, parent);
43 }
44
45 static const struct lttng_action_rotate_session *
46 action_rotate_session_from_action_const(const struct lttng_action *action)
47 {
48 assert(action);
49
50 return container_of(action, struct lttng_action_rotate_session, parent);
51 }
52
53 static bool lttng_action_rotate_session_validate(struct lttng_action *action)
54 {
55 bool valid;
56 struct lttng_action_rotate_session *action_rotate_session;
57
58 if (!action) {
59 valid = false;
60 goto end;
61 }
62
63 action_rotate_session = action_rotate_session_from_action(action);
64
65 /* A non-empty session name is mandatory. */
66 if (!action_rotate_session->session_name ||
67 strlen(action_rotate_session->session_name) == 0) {
68 valid = false;
69 goto end;
70 }
71
72 valid = true;
73 end:
74 return valid;
75 }
76
77 static bool lttng_action_rotate_session_is_equal(
78 const struct lttng_action *_a, const struct lttng_action *_b)
79 {
80 bool is_equal = false;
81 const struct lttng_action_rotate_session *a, *b;
82
83 a = action_rotate_session_from_action_const(_a);
84 b = action_rotate_session_from_action_const(_b);
85
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)) {
90 goto end;
91 }
92
93 is_equal = true;
94 end:
95 return is_equal;
96 }
97 static int lttng_action_rotate_session_serialize(
98 struct lttng_action *action, struct lttng_dynamic_buffer *buf)
99 {
100 struct lttng_action_rotate_session *action_rotate_session;
101 struct lttng_action_rotate_session_comm comm;
102 size_t session_name_len;
103 int ret;
104
105 assert(action);
106 assert(buf);
107
108 action_rotate_session = action_rotate_session_from_action(action);
109
110 assert(action_rotate_session->session_name);
111
112 DBG("Serializing rotate session action: session-name: %s",
113 action_rotate_session->session_name);
114
115 session_name_len = strlen(action_rotate_session->session_name) + 1;
116 comm.session_name_len = session_name_len;
117
118 ret = lttng_dynamic_buffer_append(buf, &comm, sizeof(comm));
119 if (ret) {
120 ret = -1;
121 goto end;
122 }
123
124 ret = lttng_dynamic_buffer_append(buf,
125 action_rotate_session->session_name, session_name_len);
126 if (ret) {
127 ret = -1;
128 goto end;
129 }
130
131 ret = 0;
132 end:
133 return ret;
134 }
135
136 static void lttng_action_rotate_session_destroy(struct lttng_action *action)
137 {
138 struct lttng_action_rotate_session *action_rotate_session;
139
140 if (!action) {
141 goto end;
142 }
143
144 action_rotate_session = action_rotate_session_from_action(action);
145
146 free(action_rotate_session->session_name);
147 free(action_rotate_session);
148
149 end:
150 return;
151 }
152
153 ssize_t lttng_action_rotate_session_create_from_buffer(
154 const struct lttng_buffer_view *view,
155 struct lttng_action **p_action)
156 {
157 ssize_t consumed_len;
158 const struct lttng_action_rotate_session_comm *comm;
159 const char *session_name;
160 struct lttng_action *action;
161 enum lttng_action_status status;
162
163 action = lttng_action_rotate_session_create();
164 if (!action) {
165 consumed_len = -1;
166 goto end;
167 }
168
169 comm = (const struct lttng_action_rotate_session_comm *) view->data;
170 session_name = (const char *) &comm->data;
171
172 if (!lttng_buffer_view_contains_string(
173 view, session_name, comm->session_name_len)) {
174 consumed_len = -1;
175 goto end;
176 }
177
178 status = lttng_action_rotate_session_set_session_name(
179 action, session_name);
180 if (status != LTTNG_ACTION_STATUS_OK) {
181 consumed_len = -1;
182 goto end;
183 }
184
185 consumed_len = sizeof(struct lttng_action_rotate_session_comm) +
186 comm->session_name_len;
187 *p_action = action;
188 action = NULL;
189
190 end:
191 lttng_action_rotate_session_destroy(action);
192
193 return consumed_len;
194 }
195
196 struct lttng_action *lttng_action_rotate_session_create(void)
197 {
198 struct lttng_action *action;
199
200 action = zmalloc(sizeof(struct lttng_action_rotate_session));
201 if (!action) {
202 goto end;
203 }
204
205 lttng_action_init(action, LTTNG_ACTION_TYPE_ROTATE_SESSION,
206 lttng_action_rotate_session_validate,
207 lttng_action_rotate_session_serialize,
208 lttng_action_rotate_session_is_equal,
209 lttng_action_rotate_session_destroy);
210
211 end:
212 return action;
213 }
214
215 enum lttng_action_status lttng_action_rotate_session_set_session_name(
216 struct lttng_action *action, const char *session_name)
217 {
218 struct lttng_action_rotate_session *action_rotate_session;
219 enum lttng_action_status status;
220
221 if (!action || !IS_ROTATE_SESSION_ACTION(action) || !session_name ||
222 strlen(session_name) == 0) {
223 status = LTTNG_ACTION_STATUS_INVALID;
224 goto end;
225 }
226
227 action_rotate_session = action_rotate_session_from_action(action);
228
229 free(action_rotate_session->session_name);
230
231 action_rotate_session->session_name = strdup(session_name);
232 if (!action_rotate_session->session_name) {
233 status = LTTNG_ACTION_STATUS_ERROR;
234 goto end;
235 }
236
237 status = LTTNG_ACTION_STATUS_OK;
238 end:
239 return status;
240 }
241
242 enum lttng_action_status lttng_action_rotate_session_get_session_name(
243 const struct lttng_action *action, const char **session_name)
244 {
245 const struct lttng_action_rotate_session *action_rotate_session;
246 enum lttng_action_status status;
247
248 if (!action || !IS_ROTATE_SESSION_ACTION(action) || !session_name) {
249 status = LTTNG_ACTION_STATUS_INVALID;
250 goto end;
251 }
252
253 action_rotate_session = action_rotate_session_from_action_const(action);
254
255 *session_name = action_rotate_session->session_name;
256
257 status = LTTNG_ACTION_STATUS_OK;
258 end:
259 return status;
260 }
This page took 0.049078 seconds and 4 git commands to generate.