argpar: sync with upstream - adjust to iterator API
[lttng-tools.git] / src / common / conditions / session-rotation.cpp
1 /*
2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #include <common/error.h>
9 #include <common/macros.h>
10 #include <common/mi-lttng.h>
11 #include <lttng/condition/condition-internal.h>
12 #include <lttng/condition/session-rotation-internal.h>
13 #include <lttng/location-internal.h>
14 #include <stdbool.h>
15
16 static
17 bool lttng_condition_session_rotation_validate(
18 const struct lttng_condition *condition);
19 static
20 int lttng_condition_session_rotation_serialize(
21 const struct lttng_condition *condition,
22 struct lttng_payload *payload);
23 static
24 bool lttng_condition_session_rotation_is_equal(const struct lttng_condition *_a,
25 const struct lttng_condition *_b);
26 static
27 void lttng_condition_session_rotation_destroy(
28 struct lttng_condition *condition);
29
30 static
31 enum lttng_error_code lttng_condition_session_rotation_mi_serialize(
32 const struct lttng_condition *condition,
33 struct mi_writer *writer);
34
35 static const
36 struct lttng_condition rotation_condition_template = {
37 {},
38 LTTNG_CONDITION_TYPE_UNKNOWN, /* type unset, shall be set on creation. */
39 lttng_condition_session_rotation_validate,
40 lttng_condition_session_rotation_serialize,
41 lttng_condition_session_rotation_is_equal,
42 lttng_condition_session_rotation_destroy,
43 lttng_condition_session_rotation_mi_serialize,
44 };
45
46 static
47 int lttng_evaluation_session_rotation_serialize(
48 const struct lttng_evaluation *evaluation,
49 struct lttng_payload *payload);
50 static
51 void lttng_evaluation_session_rotation_destroy(
52 struct lttng_evaluation *evaluation);
53
54 static const
55 struct lttng_evaluation rotation_evaluation_template = {
56 LTTNG_CONDITION_TYPE_UNKNOWN, /* type unset, shall be set on creation. */
57 lttng_evaluation_session_rotation_serialize,
58 lttng_evaluation_session_rotation_destroy,
59 };
60
61 static
62 bool is_rotation_condition(const struct lttng_condition *condition)
63 {
64 enum lttng_condition_type type = lttng_condition_get_type(condition);
65
66 return type == LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING ||
67 type == LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED;
68 }
69
70 static
71 bool is_rotation_evaluation(const struct lttng_evaluation *evaluation)
72 {
73 enum lttng_condition_type type = lttng_evaluation_get_type(evaluation);
74
75 return type == LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING ||
76 type == LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED;
77 }
78
79 static
80 bool lttng_condition_session_rotation_validate(
81 const struct lttng_condition *condition)
82 {
83 bool valid = false;
84 struct lttng_condition_session_rotation *rotation;
85
86 if (!condition) {
87 goto end;
88 }
89
90 rotation = container_of(condition,
91 struct lttng_condition_session_rotation, parent);
92 if (!rotation->session_name) {
93 ERR("Invalid session rotation condition: a target session name must be set.");
94 goto end;
95 }
96
97 valid = true;
98 end:
99 return valid;
100 }
101
102 static
103 int lttng_condition_session_rotation_serialize(
104 const struct lttng_condition *condition,
105 struct lttng_payload *payload)
106 {
107 int ret;
108 size_t session_name_len;
109 struct lttng_condition_session_rotation *rotation;
110 struct lttng_condition_session_rotation_comm rotation_comm;
111
112 if (!condition || !is_rotation_condition(condition)) {
113 ret = -1;
114 goto end;
115 }
116
117 DBG("Serializing session rotation condition");
118 rotation = container_of(condition, struct lttng_condition_session_rotation,
119 parent);
120
121 session_name_len = strlen(rotation->session_name) + 1;
122 if (session_name_len > LTTNG_NAME_MAX) {
123 ret = -1;
124 goto end;
125 }
126
127 rotation_comm.session_name_len = session_name_len;
128 ret = lttng_dynamic_buffer_append(&payload->buffer, &rotation_comm,
129 sizeof(rotation_comm));
130 if (ret) {
131 goto end;
132 }
133 ret = lttng_dynamic_buffer_append(&payload->buffer,
134 rotation->session_name, session_name_len);
135 if (ret) {
136 goto end;
137 }
138 end:
139 return ret;
140 }
141
142 static
143 bool lttng_condition_session_rotation_is_equal(const struct lttng_condition *_a,
144 const struct lttng_condition *_b)
145 {
146 bool is_equal = false;
147 struct lttng_condition_session_rotation *a, *b;
148
149 a = container_of(_a, struct lttng_condition_session_rotation, parent);
150 b = container_of(_b, struct lttng_condition_session_rotation, parent);
151
152 /* Both session names must be set or both must be unset. */
153 if ((a->session_name && !b->session_name) ||
154 (!a->session_name && b->session_name)) {
155 WARN("Comparing session rotation conditions with uninitialized session names.");
156 goto end;
157 }
158
159 if (a->session_name && b->session_name &&
160 strcmp(a->session_name, b->session_name)) {
161 goto end;
162 }
163
164 is_equal = true;
165 end:
166 return is_equal;
167 }
168
169 static
170 void lttng_condition_session_rotation_destroy(
171 struct lttng_condition *condition)
172 {
173 struct lttng_condition_session_rotation *rotation;
174
175 rotation = container_of(condition,
176 struct lttng_condition_session_rotation, parent);
177
178 free(rotation->session_name);
179 free(rotation);
180 }
181
182 static
183 struct lttng_condition *lttng_condition_session_rotation_create(
184 enum lttng_condition_type type)
185 {
186 struct lttng_condition_session_rotation *condition;
187
188 condition = (lttng_condition_session_rotation *) zmalloc(sizeof(struct lttng_condition_session_rotation));
189 if (!condition) {
190 return NULL;
191 }
192
193 memcpy(&condition->parent, &rotation_condition_template,
194 sizeof(condition->parent));
195 lttng_condition_init(&condition->parent, type);
196 return &condition->parent;
197 }
198
199 struct lttng_condition *lttng_condition_session_rotation_ongoing_create(void)
200 {
201 return lttng_condition_session_rotation_create(
202 LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING);
203 }
204
205 struct lttng_condition *lttng_condition_session_rotation_completed_create(void)
206 {
207 return lttng_condition_session_rotation_create(
208 LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED);
209 }
210
211 static
212 ssize_t init_condition_from_payload(struct lttng_condition *condition,
213 struct lttng_payload_view *src_view)
214 {
215 ssize_t ret, condition_size;
216 enum lttng_condition_status status;
217 const char *session_name;
218 struct lttng_buffer_view name_view;
219 const struct lttng_condition_session_rotation_comm *condition_comm;
220 struct lttng_payload_view condition_comm_view =
221 lttng_payload_view_from_view(
222 src_view, 0, sizeof(*condition_comm));
223
224 if (!lttng_payload_view_is_valid(&condition_comm_view)) {
225 ERR("Failed to initialize from malformed condition buffer: buffer too short to contain header");
226 ret = -1;
227 goto end;
228 }
229
230 condition_comm = (typeof(condition_comm)) src_view->buffer.data;
231 name_view = lttng_buffer_view_from_view(&src_view->buffer,
232 sizeof(*condition_comm), condition_comm->session_name_len);
233
234 if (!lttng_buffer_view_is_valid(&name_view)) {
235 ERR("Failed to initialize from malformed condition buffer: buffer too short to contain session name");
236 ret = -1;
237 goto end;
238 }
239
240 if (condition_comm->session_name_len > LTTNG_NAME_MAX) {
241 ERR("Failed to initialize from malformed condition buffer: name exceeds LTTNG_MAX_NAME");
242 ret = -1;
243 goto end;
244 }
245
246 session_name = name_view.data;
247 if (*(session_name + condition_comm->session_name_len - 1) != '\0') {
248 ERR("Malformed session name encountered in condition buffer");
249 ret = -1;
250 goto end;
251 }
252
253 status = lttng_condition_session_rotation_set_session_name(condition,
254 session_name);
255 if (status != LTTNG_CONDITION_STATUS_OK) {
256 ERR("Failed to set buffer consumed session name");
257 ret = -1;
258 goto end;
259 }
260
261 if (!lttng_condition_validate(condition)) {
262 ret = -1;
263 goto end;
264 }
265
266 condition_size = sizeof(*condition_comm) +
267 (ssize_t) condition_comm->session_name_len;
268 ret = condition_size;
269 end:
270 return ret;
271 }
272
273 static
274 ssize_t lttng_condition_session_rotation_create_from_payload(
275 struct lttng_payload_view *view,
276 struct lttng_condition **_condition,
277 enum lttng_condition_type type)
278 {
279 ssize_t ret;
280 struct lttng_condition *condition = NULL;
281
282 switch (type) {
283 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
284 condition = lttng_condition_session_rotation_ongoing_create();
285 break;
286 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
287 condition = lttng_condition_session_rotation_completed_create();
288 break;
289 default:
290 ret = -1;
291 goto error;
292 }
293
294 if (!_condition || !condition) {
295 ret = -1;
296 goto error;
297 }
298
299 ret = init_condition_from_payload(condition, view);
300 if (ret < 0) {
301 goto error;
302 }
303
304 *_condition = condition;
305 return ret;
306 error:
307 lttng_condition_destroy(condition);
308 return ret;
309 }
310
311 ssize_t lttng_condition_session_rotation_ongoing_create_from_payload(
312 struct lttng_payload_view *view,
313 struct lttng_condition **condition)
314 {
315 return lttng_condition_session_rotation_create_from_payload(view,
316 condition,
317 LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING);
318 }
319
320 ssize_t lttng_condition_session_rotation_completed_create_from_payload(
321 struct lttng_payload_view *view,
322 struct lttng_condition **condition)
323 {
324 return lttng_condition_session_rotation_create_from_payload(view,
325 condition,
326 LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED);
327 }
328
329 static
330 struct lttng_evaluation *lttng_evaluation_session_rotation_create(
331 enum lttng_condition_type type, uint64_t id,
332 struct lttng_trace_archive_location *location)
333 {
334 struct lttng_evaluation_session_rotation *evaluation;
335
336 evaluation = (lttng_evaluation_session_rotation *) zmalloc(sizeof(struct lttng_evaluation_session_rotation));
337 if (!evaluation) {
338 return NULL;
339 }
340
341 memcpy(&evaluation->parent, &rotation_evaluation_template,
342 sizeof(evaluation->parent));
343 lttng_evaluation_init(&evaluation->parent, type);
344 evaluation->id = id;
345 if (location) {
346 lttng_trace_archive_location_get(location);
347 }
348 evaluation->location = location;
349 return &evaluation->parent;
350 }
351
352 static
353 ssize_t create_evaluation_from_payload(
354 enum lttng_condition_type type,
355 struct lttng_payload_view *view,
356 struct lttng_evaluation **_evaluation)
357 {
358 ssize_t ret, size;
359 struct lttng_evaluation *evaluation = NULL;
360 struct lttng_trace_archive_location *location = NULL;
361 const struct lttng_evaluation_session_rotation_comm *comm;
362 struct lttng_payload_view comm_view = lttng_payload_view_from_view(
363 view, 0, sizeof(*comm));
364
365 if (!lttng_payload_view_is_valid(&comm_view)) {
366 goto error;
367 }
368
369 comm = (typeof(comm)) comm_view.buffer.data;
370 size = sizeof(*comm);
371 if (comm->has_location) {
372 const struct lttng_buffer_view location_view =
373 lttng_buffer_view_from_view(
374 &view->buffer, sizeof(*comm), -1);
375
376 if (!lttng_buffer_view_is_valid(&location_view)) {
377 goto error;
378 }
379
380 ret = lttng_trace_archive_location_create_from_buffer(
381 &location_view, &location);
382 if (ret < 0) {
383 goto error;
384 }
385 size += ret;
386 }
387
388 evaluation = lttng_evaluation_session_rotation_create(type, comm->id,
389 location);
390 if (!evaluation) {
391 goto error;
392 }
393
394 lttng_trace_archive_location_put(location);
395 ret = size;
396 *_evaluation = evaluation;
397 return ret;
398 error:
399 lttng_trace_archive_location_put(location);
400 evaluation = NULL;
401 return -1;
402 }
403
404 static
405 ssize_t lttng_evaluation_session_rotation_create_from_payload(
406 enum lttng_condition_type type,
407 struct lttng_payload_view *view,
408 struct lttng_evaluation **_evaluation)
409 {
410 ssize_t ret;
411 struct lttng_evaluation *evaluation = NULL;
412
413 if (!_evaluation) {
414 ret = -1;
415 goto error;
416 }
417
418 ret = create_evaluation_from_payload(type, view, &evaluation);
419 if (ret < 0) {
420 goto error;
421 }
422
423 *_evaluation = evaluation;
424 return ret;
425 error:
426 lttng_evaluation_destroy(evaluation);
427 return ret;
428 }
429
430 ssize_t lttng_evaluation_session_rotation_ongoing_create_from_payload(
431 struct lttng_payload_view *view,
432 struct lttng_evaluation **evaluation)
433 {
434 return lttng_evaluation_session_rotation_create_from_payload(
435 LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING,
436 view, evaluation);
437 }
438
439 ssize_t lttng_evaluation_session_rotation_completed_create_from_payload(
440 struct lttng_payload_view *view,
441 struct lttng_evaluation **evaluation)
442 {
443 return lttng_evaluation_session_rotation_create_from_payload(
444 LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED,
445 view, evaluation);
446 }
447
448 struct lttng_evaluation *lttng_evaluation_session_rotation_ongoing_create(
449 uint64_t id)
450 {
451 return lttng_evaluation_session_rotation_create(
452 LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING, id,
453 NULL);
454 }
455
456 struct lttng_evaluation *lttng_evaluation_session_rotation_completed_create(
457 uint64_t id, struct lttng_trace_archive_location *location)
458 {
459 return lttng_evaluation_session_rotation_create(
460 LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED, id,
461 location);
462 }
463
464 enum lttng_condition_status
465 lttng_condition_session_rotation_get_session_name(
466 const struct lttng_condition *condition,
467 const char **session_name)
468 {
469 struct lttng_condition_session_rotation *rotation;
470 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
471
472 if (!condition || !is_rotation_condition(condition) || !session_name) {
473 status = LTTNG_CONDITION_STATUS_INVALID;
474 goto end;
475 }
476
477 rotation = container_of(condition, struct lttng_condition_session_rotation,
478 parent);
479 if (!rotation->session_name) {
480 status = LTTNG_CONDITION_STATUS_UNSET;
481 goto end;
482 }
483 *session_name = rotation->session_name;
484 end:
485 return status;
486 }
487
488 enum lttng_condition_status
489 lttng_condition_session_rotation_set_session_name(
490 struct lttng_condition *condition, const char *session_name)
491 {
492 char *session_name_copy;
493 struct lttng_condition_session_rotation *rotation;
494 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
495
496 if (!condition || !is_rotation_condition(condition) ||
497 !session_name || strlen(session_name) == 0) {
498 status = LTTNG_CONDITION_STATUS_INVALID;
499 goto end;
500 }
501
502 rotation = container_of(condition,
503 struct lttng_condition_session_rotation, parent);
504 session_name_copy = strdup(session_name);
505 if (!session_name_copy) {
506 status = LTTNG_CONDITION_STATUS_ERROR;
507 goto end;
508 }
509
510 free(rotation->session_name);
511 rotation->session_name = session_name_copy;
512 end:
513 return status;
514 }
515
516 static
517 int lttng_evaluation_session_rotation_serialize(
518 const struct lttng_evaluation *evaluation,
519 struct lttng_payload *payload)
520 {
521 int ret;
522 struct lttng_evaluation_session_rotation *rotation;
523 struct lttng_evaluation_session_rotation_comm comm = { 0 };
524
525 rotation = container_of(evaluation,
526 struct lttng_evaluation_session_rotation, parent);
527 comm.id = rotation->id;
528 comm.has_location = !!rotation->location;
529 ret = lttng_dynamic_buffer_append(
530 &payload->buffer, &comm, sizeof(comm));
531 if (ret) {
532 goto end;
533 }
534 if (!rotation->location) {
535 goto end;
536 }
537 ret = lttng_trace_archive_location_serialize(rotation->location,
538 &payload->buffer);
539 end:
540 return ret;
541 }
542
543 static
544 void lttng_evaluation_session_rotation_destroy(
545 struct lttng_evaluation *evaluation)
546 {
547 struct lttng_evaluation_session_rotation *rotation;
548
549 rotation = container_of(evaluation,
550 struct lttng_evaluation_session_rotation, parent);
551 lttng_trace_archive_location_put(rotation->location);
552 free(rotation);
553 }
554
555 enum lttng_evaluation_status
556 lttng_evaluation_session_rotation_get_id(
557 const struct lttng_evaluation *evaluation, uint64_t *id)
558 {
559 const struct lttng_evaluation_session_rotation *rotation;
560 enum lttng_evaluation_status status = LTTNG_EVALUATION_STATUS_OK;
561
562 if (!evaluation || !id || !is_rotation_evaluation(evaluation)) {
563 status = LTTNG_EVALUATION_STATUS_INVALID;
564 goto end;
565 }
566
567 rotation = container_of(evaluation,
568 struct lttng_evaluation_session_rotation, parent);
569 *id = rotation->id;
570 end:
571 return status;
572 }
573
574 /*
575 * The public API assumes that trace archive locations are always provided as
576 * "constant". This means that the user of liblttng-ctl never has to destroy a
577 * trace archive location. Hence, users of liblttng-ctl have no visibility of
578 * the reference counting of archive locations.
579 */
580 enum lttng_evaluation_status
581 lttng_evaluation_session_rotation_completed_get_location(
582 const struct lttng_evaluation *evaluation,
583 const struct lttng_trace_archive_location **location)
584 {
585 const struct lttng_evaluation_session_rotation *rotation;
586 enum lttng_evaluation_status status = LTTNG_EVALUATION_STATUS_OK;
587
588 if (!evaluation || !location ||
589 evaluation->type != LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED) {
590 status = LTTNG_EVALUATION_STATUS_INVALID;
591 goto end;
592 }
593
594 rotation = container_of(evaluation,
595 struct lttng_evaluation_session_rotation, parent);
596 *location = rotation->location;
597 end:
598 return status;
599 }
600
601 static
602 enum lttng_error_code lttng_condition_session_rotation_mi_serialize(
603 const struct lttng_condition *condition,
604 struct mi_writer *writer)
605 {
606 int ret;
607 enum lttng_error_code ret_code;
608 enum lttng_condition_status status;
609 const char *session_name = NULL;
610 const char *type_element_str = NULL;
611
612 LTTNG_ASSERT(condition);
613 LTTNG_ASSERT(writer);
614 LTTNG_ASSERT(is_rotation_condition(condition));
615
616 switch (lttng_condition_get_type(condition)) {
617 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
618 type_element_str =
619 mi_lttng_element_condition_session_rotation_completed;
620 break;
621 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
622 type_element_str =
623 mi_lttng_element_condition_session_rotation_ongoing;
624 break;
625 default:
626 abort();
627 break;
628 }
629
630 status = lttng_condition_session_rotation_get_session_name(
631 condition, &session_name);
632 LTTNG_ASSERT(status == LTTNG_CONDITION_STATUS_OK);
633 LTTNG_ASSERT(session_name);
634
635 /* Open condition session rotation_* element. */
636 ret = mi_lttng_writer_open_element(writer, type_element_str);
637 if (ret) {
638 goto mi_error;
639 }
640
641 /* Session name. */
642 ret = mi_lttng_writer_write_element_string(
643 writer, mi_lttng_element_session_name, session_name);
644 if (ret) {
645 goto mi_error;
646 }
647
648 /* Close condition session rotation element. */
649 ret = mi_lttng_writer_close_element(writer);
650 if (ret) {
651 goto mi_error;
652 }
653
654 ret_code = LTTNG_OK;
655 goto end;
656
657 mi_error:
658 ret_code = LTTNG_ERR_MI_IO_FAIL;
659 end:
660 return ret_code;
661 }
This page took 0.04333 seconds and 4 git commands to generate.