2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
9 #include <common/error.h>
10 #include <common/macros.h>
11 #include <common/mi-lttng.h>
12 #include <lttng/condition/condition-internal.h>
13 #include <lttng/condition/session-rotation-internal.h>
14 #include <lttng/location-internal.h>
18 bool lttng_condition_session_rotation_validate(
19 const struct lttng_condition
*condition
);
21 int lttng_condition_session_rotation_serialize(
22 const struct lttng_condition
*condition
,
23 struct lttng_payload
*payload
);
25 bool lttng_condition_session_rotation_is_equal(const struct lttng_condition
*_a
,
26 const struct lttng_condition
*_b
);
28 void lttng_condition_session_rotation_destroy(
29 struct lttng_condition
*condition
);
32 enum lttng_error_code
lttng_condition_session_rotation_mi_serialize(
33 const struct lttng_condition
*condition
,
34 struct mi_writer
*writer
);
37 struct lttng_condition rotation_condition_template
= {
38 /* .type omitted; shall be set on creation. */
39 .validate
= lttng_condition_session_rotation_validate
,
40 .serialize
= lttng_condition_session_rotation_serialize
,
41 .equal
= lttng_condition_session_rotation_is_equal
,
42 .destroy
= lttng_condition_session_rotation_destroy
,
43 .mi_serialize
= lttng_condition_session_rotation_mi_serialize
,
47 int lttng_evaluation_session_rotation_serialize(
48 const struct lttng_evaluation
*evaluation
,
49 struct lttng_payload
*payload
);
51 void lttng_evaluation_session_rotation_destroy(
52 struct lttng_evaluation
*evaluation
);
55 struct lttng_evaluation rotation_evaluation_template
= {
56 /* .type omitted; shall be set on creation. */
57 .serialize
= lttng_evaluation_session_rotation_serialize
,
58 .destroy
= lttng_evaluation_session_rotation_destroy
,
62 bool is_rotation_condition(const struct lttng_condition
*condition
)
64 enum lttng_condition_type type
= lttng_condition_get_type(condition
);
66 return type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
||
67 type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
;
71 bool is_rotation_evaluation(const struct lttng_evaluation
*evaluation
)
73 enum lttng_condition_type type
= lttng_evaluation_get_type(evaluation
);
75 return type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
||
76 type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
;
80 bool lttng_condition_session_rotation_validate(
81 const struct lttng_condition
*condition
)
84 struct lttng_condition_session_rotation
*rotation
;
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.");
103 int lttng_condition_session_rotation_serialize(
104 const struct lttng_condition
*condition
,
105 struct lttng_payload
*payload
)
108 size_t session_name_len
;
109 struct lttng_condition_session_rotation
*rotation
;
110 struct lttng_condition_session_rotation_comm rotation_comm
;
112 if (!condition
|| !is_rotation_condition(condition
)) {
117 DBG("Serializing session rotation condition");
118 rotation
= container_of(condition
, struct lttng_condition_session_rotation
,
121 session_name_len
= strlen(rotation
->session_name
) + 1;
122 if (session_name_len
> LTTNG_NAME_MAX
) {
127 rotation_comm
.session_name_len
= session_name_len
;
128 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &rotation_comm
,
129 sizeof(rotation_comm
));
133 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
134 rotation
->session_name
, session_name_len
);
143 bool lttng_condition_session_rotation_is_equal(const struct lttng_condition
*_a
,
144 const struct lttng_condition
*_b
)
146 bool is_equal
= false;
147 struct lttng_condition_session_rotation
*a
, *b
;
149 a
= container_of(_a
, struct lttng_condition_session_rotation
, parent
);
150 b
= container_of(_b
, struct lttng_condition_session_rotation
, parent
);
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.");
159 if (a
->session_name
&& b
->session_name
&&
160 strcmp(a
->session_name
, b
->session_name
)) {
170 void lttng_condition_session_rotation_destroy(
171 struct lttng_condition
*condition
)
173 struct lttng_condition_session_rotation
*rotation
;
175 rotation
= container_of(condition
,
176 struct lttng_condition_session_rotation
, parent
);
178 free(rotation
->session_name
);
183 struct lttng_condition
*lttng_condition_session_rotation_create(
184 enum lttng_condition_type type
)
186 struct lttng_condition_session_rotation
*condition
;
188 condition
= zmalloc(sizeof(struct lttng_condition_session_rotation
));
193 memcpy(&condition
->parent
, &rotation_condition_template
,
194 sizeof(condition
->parent
));
195 lttng_condition_init(&condition
->parent
, type
);
196 return &condition
->parent
;
199 struct lttng_condition
*lttng_condition_session_rotation_ongoing_create(void)
201 return lttng_condition_session_rotation_create(
202 LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
);
205 struct lttng_condition
*lttng_condition_session_rotation_completed_create(void)
207 return lttng_condition_session_rotation_create(
208 LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
);
212 ssize_t
init_condition_from_payload(struct lttng_condition
*condition
,
213 struct lttng_payload_view
*src_view
)
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
));
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");
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
);
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");
240 if (condition_comm
->session_name_len
> LTTNG_NAME_MAX
) {
241 ERR("Failed to initialize from malformed condition buffer: name exceeds LTTNG_MAX_NAME");
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");
253 status
= lttng_condition_session_rotation_set_session_name(condition
,
255 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
256 ERR("Failed to set buffer consumed session name");
261 if (!lttng_condition_validate(condition
)) {
266 condition_size
= sizeof(*condition_comm
) +
267 (ssize_t
) condition_comm
->session_name_len
;
268 ret
= condition_size
;
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
)
280 struct lttng_condition
*condition
= NULL
;
283 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
284 condition
= lttng_condition_session_rotation_ongoing_create();
286 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
287 condition
= lttng_condition_session_rotation_completed_create();
294 if (!_condition
|| !condition
) {
299 ret
= init_condition_from_payload(condition
, view
);
304 *_condition
= condition
;
307 lttng_condition_destroy(condition
);
312 ssize_t
lttng_condition_session_rotation_ongoing_create_from_payload(
313 struct lttng_payload_view
*view
,
314 struct lttng_condition
**condition
)
316 return lttng_condition_session_rotation_create_from_payload(view
,
318 LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
);
322 ssize_t
lttng_condition_session_rotation_completed_create_from_payload(
323 struct lttng_payload_view
*view
,
324 struct lttng_condition
**condition
)
326 return lttng_condition_session_rotation_create_from_payload(view
,
328 LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
);
332 struct lttng_evaluation
*lttng_evaluation_session_rotation_create(
333 enum lttng_condition_type type
, uint64_t id
,
334 struct lttng_trace_archive_location
*location
)
336 struct lttng_evaluation_session_rotation
*evaluation
;
338 evaluation
= zmalloc(sizeof(struct lttng_evaluation_session_rotation
));
343 memcpy(&evaluation
->parent
, &rotation_evaluation_template
,
344 sizeof(evaluation
->parent
));
345 lttng_evaluation_init(&evaluation
->parent
, type
);
347 evaluation
->location
= location
;
348 return &evaluation
->parent
;
352 ssize_t
create_evaluation_from_payload(
353 enum lttng_condition_type type
,
354 struct lttng_payload_view
*view
,
355 struct lttng_evaluation
**_evaluation
)
358 struct lttng_evaluation
*evaluation
= NULL
;
359 struct lttng_trace_archive_location
*location
= NULL
;
360 const struct lttng_evaluation_session_rotation_comm
*comm
;
361 struct lttng_payload_view comm_view
= lttng_payload_view_from_view(
362 view
, 0, sizeof(*comm
));
364 if (!lttng_payload_view_is_valid(&comm_view
)) {
368 comm
= (typeof(comm
)) comm_view
.buffer
.data
;
369 size
= sizeof(*comm
);
370 if (comm
->has_location
) {
371 const struct lttng_buffer_view location_view
=
372 lttng_buffer_view_from_view(
373 &view
->buffer
, sizeof(*comm
), -1);
375 if (!lttng_buffer_view_is_valid(&location_view
)) {
379 ret
= lttng_trace_archive_location_create_from_buffer(
380 &location_view
, &location
);
387 evaluation
= lttng_evaluation_session_rotation_create(type
, comm
->id
,
394 *_evaluation
= evaluation
;
397 lttng_trace_archive_location_destroy(location
);
403 ssize_t
lttng_evaluation_session_rotation_create_from_payload(
404 enum lttng_condition_type type
,
405 struct lttng_payload_view
*view
,
406 struct lttng_evaluation
**_evaluation
)
409 struct lttng_evaluation
*evaluation
= NULL
;
416 ret
= create_evaluation_from_payload(type
, view
, &evaluation
);
421 *_evaluation
= evaluation
;
424 lttng_evaluation_destroy(evaluation
);
429 ssize_t
lttng_evaluation_session_rotation_ongoing_create_from_payload(
430 struct lttng_payload_view
*view
,
431 struct lttng_evaluation
**evaluation
)
433 return lttng_evaluation_session_rotation_create_from_payload(
434 LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
,
439 ssize_t
lttng_evaluation_session_rotation_completed_create_from_payload(
440 struct lttng_payload_view
*view
,
441 struct lttng_evaluation
**evaluation
)
443 return lttng_evaluation_session_rotation_create_from_payload(
444 LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
,
449 struct lttng_evaluation
*lttng_evaluation_session_rotation_ongoing_create(
452 return lttng_evaluation_session_rotation_create(
453 LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
, id
,
458 struct lttng_evaluation
*lttng_evaluation_session_rotation_completed_create(
459 uint64_t id
, struct lttng_trace_archive_location
*location
)
461 return lttng_evaluation_session_rotation_create(
462 LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
, id
,
466 enum lttng_condition_status
467 lttng_condition_session_rotation_get_session_name(
468 const struct lttng_condition
*condition
,
469 const char **session_name
)
471 struct lttng_condition_session_rotation
*rotation
;
472 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
474 if (!condition
|| !is_rotation_condition(condition
) || !session_name
) {
475 status
= LTTNG_CONDITION_STATUS_INVALID
;
479 rotation
= container_of(condition
, struct lttng_condition_session_rotation
,
481 if (!rotation
->session_name
) {
482 status
= LTTNG_CONDITION_STATUS_UNSET
;
485 *session_name
= rotation
->session_name
;
490 enum lttng_condition_status
491 lttng_condition_session_rotation_set_session_name(
492 struct lttng_condition
*condition
, const char *session_name
)
494 char *session_name_copy
;
495 struct lttng_condition_session_rotation
*rotation
;
496 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
498 if (!condition
|| !is_rotation_condition(condition
) ||
499 !session_name
|| strlen(session_name
) == 0) {
500 status
= LTTNG_CONDITION_STATUS_INVALID
;
504 rotation
= container_of(condition
,
505 struct lttng_condition_session_rotation
, parent
);
506 session_name_copy
= strdup(session_name
);
507 if (!session_name_copy
) {
508 status
= LTTNG_CONDITION_STATUS_ERROR
;
512 free(rotation
->session_name
);
513 rotation
->session_name
= session_name_copy
;
519 int lttng_evaluation_session_rotation_serialize(
520 const struct lttng_evaluation
*evaluation
,
521 struct lttng_payload
*payload
)
524 struct lttng_evaluation_session_rotation
*rotation
;
525 struct lttng_evaluation_session_rotation_comm comm
= { 0 };
527 rotation
= container_of(evaluation
,
528 struct lttng_evaluation_session_rotation
, parent
);
529 comm
.id
= rotation
->id
;
530 comm
.has_location
= !!rotation
->location
;
531 ret
= lttng_dynamic_buffer_append(
532 &payload
->buffer
, &comm
, sizeof(comm
));
536 if (!rotation
->location
) {
539 ret
= lttng_trace_archive_location_serialize(rotation
->location
,
546 void lttng_evaluation_session_rotation_destroy(
547 struct lttng_evaluation
*evaluation
)
549 struct lttng_evaluation_session_rotation
*rotation
;
551 rotation
= container_of(evaluation
,
552 struct lttng_evaluation_session_rotation
, parent
);
553 lttng_trace_archive_location_destroy(rotation
->location
);
557 enum lttng_evaluation_status
558 lttng_evaluation_session_rotation_get_id(
559 const struct lttng_evaluation
*evaluation
, uint64_t *id
)
561 const struct lttng_evaluation_session_rotation
*rotation
;
562 enum lttng_evaluation_status status
= LTTNG_EVALUATION_STATUS_OK
;
564 if (!evaluation
|| !id
|| !is_rotation_evaluation(evaluation
)) {
565 status
= LTTNG_EVALUATION_STATUS_INVALID
;
569 rotation
= container_of(evaluation
,
570 struct lttng_evaluation_session_rotation
, parent
);
576 enum lttng_evaluation_status
577 lttng_evaluation_session_rotation_completed_get_location(
578 const struct lttng_evaluation
*evaluation
,
579 const struct lttng_trace_archive_location
**location
)
581 const struct lttng_evaluation_session_rotation
*rotation
;
582 enum lttng_evaluation_status status
= LTTNG_EVALUATION_STATUS_OK
;
584 if (!evaluation
|| !location
||
585 evaluation
->type
!= LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
) {
586 status
= LTTNG_EVALUATION_STATUS_INVALID
;
590 rotation
= container_of(evaluation
,
591 struct lttng_evaluation_session_rotation
, parent
);
592 *location
= rotation
->location
;
598 enum lttng_error_code
lttng_condition_session_rotation_mi_serialize(
599 const struct lttng_condition
*condition
,
600 struct mi_writer
*writer
)
603 enum lttng_error_code ret_code
;
604 enum lttng_condition_status status
;
605 const char *session_name
= NULL
;
606 const char *type_element_str
= NULL
;
610 assert(is_rotation_condition(condition
));
612 switch (lttng_condition_get_type(condition
)) {
613 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
615 mi_lttng_element_condition_session_rotation_completed
;
617 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
619 mi_lttng_element_condition_session_rotation_ongoing
;
626 status
= lttng_condition_session_rotation_get_session_name(
627 condition
, &session_name
);
628 assert(status
== LTTNG_CONDITION_STATUS_OK
);
629 assert(session_name
);
631 /* Open condition session rotation_* element. */
632 ret
= mi_lttng_writer_open_element(writer
, type_element_str
);
638 ret
= mi_lttng_writer_write_element_string(
639 writer
, mi_lttng_element_session_name
, session_name
);
644 /* Close condition session rotation element. */
645 ret
= mi_lttng_writer_close_element(writer
);
654 ret_code
= LTTNG_ERR_MI_IO_FAIL
;