Commit | Line | Data |
---|---|---|
bfb2ec6a SM |
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> | |
7f4d5b07 JR |
12 | #include <lttng/action/rate-policy-internal.h> |
13 | #include <lttng/action/rate-policy.h> | |
bfb2ec6a SM |
14 | #include <lttng/action/rotate-session-internal.h> |
15 | #include <lttng/action/rotate-session.h> | |
16 | ||
17 | #define IS_ROTATE_SESSION_ACTION(action) \ | |
17182cfd | 18 | (lttng_action_get_type(action) == LTTNG_ACTION_TYPE_ROTATE_SESSION) |
bfb2ec6a SM |
19 | |
20 | struct lttng_action_rotate_session { | |
21 | struct lttng_action parent; | |
22 | ||
23 | /* Owned by this. */ | |
24 | char *session_name; | |
7f4d5b07 | 25 | struct lttng_rate_policy *policy; |
bfb2ec6a SM |
26 | }; |
27 | ||
28 | struct lttng_action_rotate_session_comm { | |
29 | /* Includes the trailing \0. */ | |
30 | uint32_t session_name_len; | |
31 | ||
32 | /* | |
33 | * Variable data: | |
34 | * | |
35 | * - session name (null terminated) | |
c2c58434 | 36 | * - policy |
bfb2ec6a SM |
37 | */ |
38 | char data[]; | |
39 | } LTTNG_PACKED; | |
40 | ||
7f4d5b07 JR |
41 | static const struct lttng_rate_policy * |
42 | lttng_action_rotate_session_internal_get_rate_policy( | |
2d57482c JR |
43 | const struct lttng_action *action); |
44 | ||
bfb2ec6a SM |
45 | static struct lttng_action_rotate_session *action_rotate_session_from_action( |
46 | struct lttng_action *action) | |
47 | { | |
48 | assert(action); | |
49 | ||
50 | return container_of(action, struct lttng_action_rotate_session, parent); | |
51 | } | |
52 | ||
53 | static const struct lttng_action_rotate_session * | |
54 | action_rotate_session_from_action_const(const struct lttng_action *action) | |
55 | { | |
56 | assert(action); | |
57 | ||
58 | return container_of(action, struct lttng_action_rotate_session, parent); | |
59 | } | |
60 | ||
61 | static bool lttng_action_rotate_session_validate(struct lttng_action *action) | |
62 | { | |
63 | bool valid; | |
64 | struct lttng_action_rotate_session *action_rotate_session; | |
65 | ||
66 | if (!action) { | |
67 | valid = false; | |
68 | goto end; | |
69 | } | |
70 | ||
71 | action_rotate_session = action_rotate_session_from_action(action); | |
72 | ||
73 | /* A non-empty session name is mandatory. */ | |
74 | if (!action_rotate_session->session_name || | |
75 | strlen(action_rotate_session->session_name) == 0) { | |
76 | valid = false; | |
77 | goto end; | |
78 | } | |
79 | ||
80 | valid = true; | |
81 | end: | |
82 | return valid; | |
83 | } | |
84 | ||
85 | static bool lttng_action_rotate_session_is_equal( | |
86 | const struct lttng_action *_a, const struct lttng_action *_b) | |
87 | { | |
88 | bool is_equal = false; | |
89 | const struct lttng_action_rotate_session *a, *b; | |
90 | ||
91 | a = action_rotate_session_from_action_const(_a); | |
92 | b = action_rotate_session_from_action_const(_b); | |
93 | ||
94 | /* Action is not valid if this is not true. */ | |
95 | assert(a->session_name); | |
96 | assert(b->session_name); | |
97 | if (strcmp(a->session_name, b->session_name)) { | |
98 | goto end; | |
99 | } | |
100 | ||
7f4d5b07 | 101 | is_equal = lttng_rate_policy_is_equal(a->policy, b->policy); |
bfb2ec6a SM |
102 | end: |
103 | return is_equal; | |
104 | } | |
105 | static int lttng_action_rotate_session_serialize( | |
c0a66c84 | 106 | struct lttng_action *action, struct lttng_payload *payload) |
bfb2ec6a SM |
107 | { |
108 | struct lttng_action_rotate_session *action_rotate_session; | |
109 | struct lttng_action_rotate_session_comm comm; | |
110 | size_t session_name_len; | |
111 | int ret; | |
112 | ||
113 | assert(action); | |
c0a66c84 | 114 | assert(payload); |
bfb2ec6a SM |
115 | |
116 | action_rotate_session = action_rotate_session_from_action(action); | |
117 | ||
118 | assert(action_rotate_session->session_name); | |
119 | ||
120 | DBG("Serializing rotate session action: session-name: %s", | |
121 | action_rotate_session->session_name); | |
122 | ||
123 | session_name_len = strlen(action_rotate_session->session_name) + 1; | |
124 | comm.session_name_len = session_name_len; | |
125 | ||
c0a66c84 JG |
126 | ret = lttng_dynamic_buffer_append( |
127 | &payload->buffer, &comm, sizeof(comm)); | |
bfb2ec6a SM |
128 | if (ret) { |
129 | ret = -1; | |
130 | goto end; | |
131 | } | |
132 | ||
c0a66c84 | 133 | ret = lttng_dynamic_buffer_append(&payload->buffer, |
bfb2ec6a SM |
134 | action_rotate_session->session_name, session_name_len); |
135 | if (ret) { | |
136 | ret = -1; | |
137 | goto end; | |
138 | } | |
139 | ||
7f4d5b07 | 140 | ret = lttng_rate_policy_serialize( |
c2c58434 JR |
141 | action_rotate_session->policy, payload); |
142 | if (ret) { | |
143 | ret = -1; | |
144 | goto end; | |
145 | } | |
bfb2ec6a SM |
146 | end: |
147 | return ret; | |
148 | } | |
149 | ||
150 | static void lttng_action_rotate_session_destroy(struct lttng_action *action) | |
151 | { | |
152 | struct lttng_action_rotate_session *action_rotate_session; | |
153 | ||
154 | if (!action) { | |
155 | goto end; | |
156 | } | |
157 | ||
158 | action_rotate_session = action_rotate_session_from_action(action); | |
159 | ||
7f4d5b07 | 160 | lttng_rate_policy_destroy(action_rotate_session->policy); |
bfb2ec6a SM |
161 | free(action_rotate_session->session_name); |
162 | free(action_rotate_session); | |
163 | ||
164 | end: | |
165 | return; | |
166 | } | |
167 | ||
c0a66c84 JG |
168 | ssize_t lttng_action_rotate_session_create_from_payload( |
169 | struct lttng_payload_view *view, | |
bfb2ec6a SM |
170 | struct lttng_action **p_action) |
171 | { | |
c2c58434 | 172 | ssize_t consumed_len, ret; |
bfb2ec6a SM |
173 | const struct lttng_action_rotate_session_comm *comm; |
174 | const char *session_name; | |
175 | struct lttng_action *action; | |
176 | enum lttng_action_status status; | |
7f4d5b07 | 177 | struct lttng_rate_policy *policy = NULL; |
bfb2ec6a SM |
178 | |
179 | action = lttng_action_rotate_session_create(); | |
180 | if (!action) { | |
181 | consumed_len = -1; | |
182 | goto end; | |
183 | } | |
184 | ||
c0a66c84 | 185 | comm = (typeof(comm)) view->buffer.data; |
bfb2ec6a SM |
186 | session_name = (const char *) &comm->data; |
187 | ||
188 | if (!lttng_buffer_view_contains_string( | |
c0a66c84 | 189 | &view->buffer, session_name, comm->session_name_len)) { |
bfb2ec6a SM |
190 | consumed_len = -1; |
191 | goto end; | |
192 | } | |
c2c58434 JR |
193 | consumed_len = sizeof(*comm) + comm->session_name_len; |
194 | ||
7f4d5b07 | 195 | /* Rate policy. */ |
c2c58434 JR |
196 | { |
197 | struct lttng_payload_view policy_view = | |
198 | lttng_payload_view_from_view( | |
199 | view, consumed_len, -1); | |
7f4d5b07 | 200 | ret = lttng_rate_policy_create_from_payload( |
c2c58434 JR |
201 | &policy_view, &policy); |
202 | if (ret < 0) { | |
203 | consumed_len = -1; | |
204 | goto end; | |
205 | } | |
206 | consumed_len += ret; | |
207 | } | |
bfb2ec6a SM |
208 | |
209 | status = lttng_action_rotate_session_set_session_name( | |
210 | action, session_name); | |
211 | if (status != LTTNG_ACTION_STATUS_OK) { | |
212 | consumed_len = -1; | |
213 | goto end; | |
214 | } | |
215 | ||
c2c58434 | 216 | assert(policy); |
7f4d5b07 | 217 | status = lttng_action_rotate_session_set_rate_policy(action, policy); |
c2c58434 JR |
218 | if (status != LTTNG_ACTION_STATUS_OK) { |
219 | consumed_len = -1; | |
220 | goto end; | |
221 | } | |
222 | ||
bfb2ec6a SM |
223 | *p_action = action; |
224 | action = NULL; | |
225 | ||
226 | end: | |
7f4d5b07 | 227 | lttng_rate_policy_destroy(policy); |
bfb2ec6a SM |
228 | lttng_action_rotate_session_destroy(action); |
229 | ||
230 | return consumed_len; | |
231 | } | |
232 | ||
233 | struct lttng_action *lttng_action_rotate_session_create(void) | |
234 | { | |
c2c58434 | 235 | struct lttng_action *action = NULL; |
7f4d5b07 | 236 | struct lttng_rate_policy *policy = NULL; |
c2c58434 JR |
237 | enum lttng_action_status status; |
238 | ||
7f4d5b07 JR |
239 | /* Create a every N = 1 rate policy. */ |
240 | policy = lttng_rate_policy_every_n_create(1); | |
c2c58434 JR |
241 | if (!policy) { |
242 | goto end; | |
243 | } | |
bfb2ec6a SM |
244 | |
245 | action = zmalloc(sizeof(struct lttng_action_rotate_session)); | |
246 | if (!action) { | |
247 | goto end; | |
248 | } | |
249 | ||
250 | lttng_action_init(action, LTTNG_ACTION_TYPE_ROTATE_SESSION, | |
251 | lttng_action_rotate_session_validate, | |
252 | lttng_action_rotate_session_serialize, | |
253 | lttng_action_rotate_session_is_equal, | |
2d57482c | 254 | lttng_action_rotate_session_destroy, |
588c4b0d JG |
255 | lttng_action_rotate_session_internal_get_rate_policy, |
256 | lttng_action_generic_add_error_query_results); | |
bfb2ec6a | 257 | |
7f4d5b07 | 258 | status = lttng_action_rotate_session_set_rate_policy(action, policy); |
c2c58434 JR |
259 | if (status != LTTNG_ACTION_STATUS_OK) { |
260 | free(action); | |
261 | action = NULL; | |
262 | goto end; | |
263 | } | |
264 | ||
bfb2ec6a | 265 | end: |
7f4d5b07 | 266 | lttng_rate_policy_destroy(policy); |
bfb2ec6a SM |
267 | return action; |
268 | } | |
269 | ||
270 | enum lttng_action_status lttng_action_rotate_session_set_session_name( | |
271 | struct lttng_action *action, const char *session_name) | |
272 | { | |
273 | struct lttng_action_rotate_session *action_rotate_session; | |
274 | enum lttng_action_status status; | |
275 | ||
276 | if (!action || !IS_ROTATE_SESSION_ACTION(action) || !session_name || | |
277 | strlen(session_name) == 0) { | |
278 | status = LTTNG_ACTION_STATUS_INVALID; | |
279 | goto end; | |
280 | } | |
281 | ||
282 | action_rotate_session = action_rotate_session_from_action(action); | |
283 | ||
284 | free(action_rotate_session->session_name); | |
285 | ||
286 | action_rotate_session->session_name = strdup(session_name); | |
287 | if (!action_rotate_session->session_name) { | |
288 | status = LTTNG_ACTION_STATUS_ERROR; | |
289 | goto end; | |
290 | } | |
291 | ||
292 | status = LTTNG_ACTION_STATUS_OK; | |
293 | end: | |
294 | return status; | |
295 | } | |
296 | ||
297 | enum lttng_action_status lttng_action_rotate_session_get_session_name( | |
298 | const struct lttng_action *action, const char **session_name) | |
299 | { | |
300 | const struct lttng_action_rotate_session *action_rotate_session; | |
301 | enum lttng_action_status status; | |
302 | ||
303 | if (!action || !IS_ROTATE_SESSION_ACTION(action) || !session_name) { | |
304 | status = LTTNG_ACTION_STATUS_INVALID; | |
305 | goto end; | |
306 | } | |
307 | ||
308 | action_rotate_session = action_rotate_session_from_action_const(action); | |
309 | ||
310 | *session_name = action_rotate_session->session_name; | |
311 | ||
312 | status = LTTNG_ACTION_STATUS_OK; | |
313 | end: | |
314 | return status; | |
315 | } | |
c2c58434 | 316 | |
7f4d5b07 | 317 | enum lttng_action_status lttng_action_rotate_session_set_rate_policy( |
c2c58434 | 318 | struct lttng_action *action, |
7f4d5b07 | 319 | const struct lttng_rate_policy *policy) |
c2c58434 JR |
320 | { |
321 | enum lttng_action_status status; | |
322 | struct lttng_action_rotate_session *rotate_session_action; | |
7f4d5b07 | 323 | struct lttng_rate_policy *copy = NULL; |
c2c58434 JR |
324 | |
325 | if (!action || !policy || !IS_ROTATE_SESSION_ACTION(action)) { | |
326 | status = LTTNG_ACTION_STATUS_INVALID; | |
327 | goto end; | |
328 | } | |
329 | ||
7f4d5b07 | 330 | copy = lttng_rate_policy_copy(policy); |
c2c58434 JR |
331 | if (!copy) { |
332 | status = LTTNG_ACTION_STATUS_ERROR; | |
333 | goto end; | |
334 | } | |
335 | ||
336 | rotate_session_action = action_rotate_session_from_action(action); | |
337 | ||
7f4d5b07 JR |
338 | /* Free the previous rate policy .*/ |
339 | lttng_rate_policy_destroy(rotate_session_action->policy); | |
c2c58434 JR |
340 | |
341 | /* Assign the policy. */ | |
342 | rotate_session_action->policy = copy; | |
343 | status = LTTNG_ACTION_STATUS_OK; | |
344 | copy = NULL; | |
345 | ||
346 | end: | |
7f4d5b07 | 347 | lttng_rate_policy_destroy(copy); |
c2c58434 JR |
348 | return status; |
349 | } | |
350 | ||
7f4d5b07 | 351 | enum lttng_action_status lttng_action_rotate_session_get_rate_policy( |
c2c58434 | 352 | const struct lttng_action *action, |
7f4d5b07 | 353 | const struct lttng_rate_policy **policy) |
c2c58434 JR |
354 | { |
355 | enum lttng_action_status status; | |
356 | const struct lttng_action_rotate_session *rotate_session_action; | |
357 | ||
358 | if (!action || !policy || !IS_ROTATE_SESSION_ACTION(action)) { | |
359 | status = LTTNG_ACTION_STATUS_INVALID; | |
360 | goto end; | |
361 | } | |
362 | ||
363 | rotate_session_action = action_rotate_session_from_action_const(action); | |
364 | ||
365 | *policy = rotate_session_action->policy; | |
366 | status = LTTNG_ACTION_STATUS_OK; | |
367 | end: | |
368 | return status; | |
369 | } | |
2d57482c | 370 | |
7f4d5b07 JR |
371 | static const struct lttng_rate_policy * |
372 | lttng_action_rotate_session_internal_get_rate_policy( | |
2d57482c JR |
373 | const struct lttng_action *action) |
374 | { | |
375 | const struct lttng_action_rotate_session *_action; | |
376 | _action = action_rotate_session_from_action_const(action); | |
377 | ||
378 | return _action->policy; | |
379 | } |