2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include <lttng/condition/condition-internal.h>
9 #include <lttng/condition/session-rotation-internal.h>
10 #include <lttng/location-internal.h>
11 #include <common/macros.h>
12 #include <common/error.h>
17 bool lttng_condition_session_rotation_validate(
18 const struct lttng_condition
*condition
);
20 int lttng_condition_session_rotation_serialize(
21 const struct lttng_condition
*condition
,
22 struct lttng_dynamic_buffer
*buf
);
24 bool lttng_condition_session_rotation_is_equal(const struct lttng_condition
*_a
,
25 const struct lttng_condition
*_b
);
27 void lttng_condition_session_rotation_destroy(
28 struct lttng_condition
*condition
);
31 struct lttng_condition rotation_condition_template
= {
32 /* .type omitted; shall be set on creation. */
33 .validate
= lttng_condition_session_rotation_validate
,
34 .serialize
= lttng_condition_session_rotation_serialize
,
35 .equal
= lttng_condition_session_rotation_is_equal
,
36 .destroy
= lttng_condition_session_rotation_destroy
,
40 int lttng_evaluation_session_rotation_serialize(
41 const struct lttng_evaluation
*evaluation
,
42 struct lttng_dynamic_buffer
*buf
);
44 void lttng_evaluation_session_rotation_destroy(
45 struct lttng_evaluation
*evaluation
);
48 struct lttng_evaluation rotation_evaluation_template
= {
49 /* .type omitted; shall be set on creation. */
50 .serialize
= lttng_evaluation_session_rotation_serialize
,
51 .destroy
= lttng_evaluation_session_rotation_destroy
,
55 bool is_rotation_condition(const struct lttng_condition
*condition
)
57 enum lttng_condition_type type
= lttng_condition_get_type(condition
);
59 return type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
||
60 type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
;
64 bool is_rotation_evaluation(const struct lttng_evaluation
*evaluation
)
66 enum lttng_condition_type type
= lttng_evaluation_get_type(evaluation
);
68 return type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
||
69 type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
;
73 bool lttng_condition_session_rotation_validate(
74 const struct lttng_condition
*condition
)
77 struct lttng_condition_session_rotation
*rotation
;
83 rotation
= container_of(condition
,
84 struct lttng_condition_session_rotation
, parent
);
85 if (!rotation
->session_name
) {
86 ERR("Invalid session rotation condition: a target session name must be set.");
96 int lttng_condition_session_rotation_serialize(
97 const struct lttng_condition
*condition
,
98 struct lttng_dynamic_buffer
*buf
)
101 size_t session_name_len
;
102 struct lttng_condition_session_rotation
*rotation
;
103 struct lttng_condition_session_rotation_comm rotation_comm
;
105 if (!condition
|| !is_rotation_condition(condition
)) {
110 DBG("Serializing session rotation condition");
111 rotation
= container_of(condition
, struct lttng_condition_session_rotation
,
114 session_name_len
= strlen(rotation
->session_name
) + 1;
115 if (session_name_len
> LTTNG_NAME_MAX
) {
120 rotation_comm
.session_name_len
= session_name_len
;
121 ret
= lttng_dynamic_buffer_append(buf
, &rotation_comm
,
122 sizeof(rotation_comm
));
126 ret
= lttng_dynamic_buffer_append(buf
, rotation
->session_name
,
136 bool lttng_condition_session_rotation_is_equal(const struct lttng_condition
*_a
,
137 const struct lttng_condition
*_b
)
139 bool is_equal
= false;
140 struct lttng_condition_session_rotation
*a
, *b
;
142 a
= container_of(_a
, struct lttng_condition_session_rotation
, parent
);
143 b
= container_of(_b
, struct lttng_condition_session_rotation
, parent
);
145 /* Both session names must be set or both must be unset. */
146 if ((a
->session_name
&& !b
->session_name
) ||
147 (!a
->session_name
&& b
->session_name
)) {
148 WARN("Comparing session rotation conditions with uninitialized session names.");
152 if (a
->session_name
&& b
->session_name
&&
153 strcmp(a
->session_name
, b
->session_name
)) {
163 void lttng_condition_session_rotation_destroy(
164 struct lttng_condition
*condition
)
166 struct lttng_condition_session_rotation
*rotation
;
168 rotation
= container_of(condition
,
169 struct lttng_condition_session_rotation
, parent
);
171 free(rotation
->session_name
);
176 struct lttng_condition
*lttng_condition_session_rotation_create(
177 enum lttng_condition_type type
)
179 struct lttng_condition_session_rotation
*condition
;
181 condition
= zmalloc(sizeof(struct lttng_condition_session_rotation
));
186 memcpy(&condition
->parent
, &rotation_condition_template
,
187 sizeof(condition
->parent
));
188 lttng_condition_init(&condition
->parent
, type
);
189 return &condition
->parent
;
192 struct lttng_condition
*lttng_condition_session_rotation_ongoing_create(void)
194 return lttng_condition_session_rotation_create(
195 LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
);
198 struct lttng_condition
*lttng_condition_session_rotation_completed_create(void)
200 return lttng_condition_session_rotation_create(
201 LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
);
205 ssize_t
init_condition_from_buffer(struct lttng_condition
*condition
,
206 const struct lttng_buffer_view
*src_view
)
208 ssize_t ret
, condition_size
;
209 enum lttng_condition_status status
;
210 const struct lttng_condition_session_rotation_comm
*condition_comm
;
211 const char *session_name
;
212 struct lttng_buffer_view name_view
;
214 if (src_view
->size
< sizeof(*condition_comm
)) {
215 ERR("Failed to initialize from malformed condition buffer: buffer too short to contain header");
220 condition_comm
= (const struct lttng_condition_session_rotation_comm
*) src_view
->data
;
221 name_view
= lttng_buffer_view_from_view(src_view
,
222 sizeof(*condition_comm
), -1);
224 if (condition_comm
->session_name_len
> LTTNG_NAME_MAX
) {
225 ERR("Failed to initialize from malformed condition buffer: name exceeds LTTNG_MAX_NAME");
230 if (name_view
.size
< condition_comm
->session_name_len
) {
231 ERR("Failed to initialize from malformed condition buffer: buffer too short to contain session name");
236 session_name
= name_view
.data
;
237 if (*(session_name
+ condition_comm
->session_name_len
- 1) != '\0') {
238 ERR("Malformed session name encountered in condition buffer");
243 status
= lttng_condition_session_rotation_set_session_name(condition
,
245 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
246 ERR("Failed to set buffer consumed session name");
251 if (!lttng_condition_validate(condition
)) {
256 condition_size
= sizeof(*condition_comm
) +
257 (ssize_t
) condition_comm
->session_name_len
;
258 ret
= condition_size
;
264 ssize_t
lttng_condition_session_rotation_create_from_buffer(
265 const struct lttng_buffer_view
*view
,
266 struct lttng_condition
**_condition
,
267 enum lttng_condition_type type
)
270 struct lttng_condition
*condition
= NULL
;
273 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
274 condition
= lttng_condition_session_rotation_ongoing_create();
276 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
277 condition
= lttng_condition_session_rotation_completed_create();
284 if (!_condition
|| !condition
) {
289 ret
= init_condition_from_buffer(condition
, view
);
294 *_condition
= condition
;
297 lttng_condition_destroy(condition
);
302 ssize_t
lttng_condition_session_rotation_ongoing_create_from_buffer(
303 const struct lttng_buffer_view
*view
,
304 struct lttng_condition
**condition
)
306 return lttng_condition_session_rotation_create_from_buffer(view
,
308 LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
);
312 ssize_t
lttng_condition_session_rotation_completed_create_from_buffer(
313 const struct lttng_buffer_view
*view
,
314 struct lttng_condition
**condition
)
316 return lttng_condition_session_rotation_create_from_buffer(view
,
318 LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
);
322 struct lttng_evaluation
*lttng_evaluation_session_rotation_create(
323 enum lttng_condition_type type
, uint64_t id
,
324 struct lttng_trace_archive_location
*location
)
326 struct lttng_evaluation_session_rotation
*evaluation
;
328 evaluation
= zmalloc(sizeof(struct lttng_evaluation_session_rotation
));
333 memcpy(&evaluation
->parent
, &rotation_evaluation_template
,
334 sizeof(evaluation
->parent
));
335 lttng_evaluation_init(&evaluation
->parent
, type
);
337 evaluation
->location
= location
;
338 return &evaluation
->parent
;
342 ssize_t
create_evaluation_from_buffer(
343 enum lttng_condition_type type
,
344 const struct lttng_buffer_view
*view
,
345 struct lttng_evaluation
**_evaluation
)
348 struct lttng_evaluation
*evaluation
= NULL
;
349 struct lttng_trace_archive_location
*location
= NULL
;
350 const struct lttng_evaluation_session_rotation_comm
*comm
=
351 (const struct lttng_evaluation_session_rotation_comm
*) view
->data
;
352 struct lttng_buffer_view location_view
;
354 if (view
->size
< sizeof(*comm
)) {
358 size
= sizeof(*comm
);
359 if (comm
->has_location
) {
360 location_view
= lttng_buffer_view_from_view(view
, sizeof(*comm
),
362 if (!location_view
.data
) {
366 ret
= lttng_trace_archive_location_create_from_buffer(
367 &location_view
, &location
);
374 evaluation
= lttng_evaluation_session_rotation_create(type
, comm
->id
,
381 *_evaluation
= evaluation
;
384 lttng_trace_archive_location_destroy(location
);
390 ssize_t
lttng_evaluation_session_rotation_create_from_buffer(
391 enum lttng_condition_type type
,
392 const struct lttng_buffer_view
*view
,
393 struct lttng_evaluation
**_evaluation
)
396 struct lttng_evaluation
*evaluation
= NULL
;
403 ret
= create_evaluation_from_buffer(type
, view
, &evaluation
);
408 *_evaluation
= evaluation
;
411 lttng_evaluation_destroy(evaluation
);
416 ssize_t
lttng_evaluation_session_rotation_ongoing_create_from_buffer(
417 const struct lttng_buffer_view
*view
,
418 struct lttng_evaluation
**evaluation
)
420 return lttng_evaluation_session_rotation_create_from_buffer(
421 LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
,
426 ssize_t
lttng_evaluation_session_rotation_completed_create_from_buffer(
427 const struct lttng_buffer_view
*view
,
428 struct lttng_evaluation
**evaluation
)
430 return lttng_evaluation_session_rotation_create_from_buffer(
431 LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
,
436 struct lttng_evaluation
*lttng_evaluation_session_rotation_ongoing_create(
439 return lttng_evaluation_session_rotation_create(
440 LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
, id
,
445 struct lttng_evaluation
*lttng_evaluation_session_rotation_completed_create(
446 uint64_t id
, struct lttng_trace_archive_location
*location
)
448 return lttng_evaluation_session_rotation_create(
449 LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
, id
,
453 enum lttng_condition_status
454 lttng_condition_session_rotation_get_session_name(
455 const struct lttng_condition
*condition
,
456 const char **session_name
)
458 struct lttng_condition_session_rotation
*rotation
;
459 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
461 if (!condition
|| !is_rotation_condition(condition
) || !session_name
) {
462 status
= LTTNG_CONDITION_STATUS_INVALID
;
466 rotation
= container_of(condition
, struct lttng_condition_session_rotation
,
468 if (!rotation
->session_name
) {
469 status
= LTTNG_CONDITION_STATUS_UNSET
;
472 *session_name
= rotation
->session_name
;
477 enum lttng_condition_status
478 lttng_condition_session_rotation_set_session_name(
479 struct lttng_condition
*condition
, const char *session_name
)
481 char *session_name_copy
;
482 struct lttng_condition_session_rotation
*rotation
;
483 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
485 if (!condition
|| !is_rotation_condition(condition
) ||
486 !session_name
|| strlen(session_name
) == 0) {
487 status
= LTTNG_CONDITION_STATUS_INVALID
;
491 rotation
= container_of(condition
,
492 struct lttng_condition_session_rotation
, parent
);
493 session_name_copy
= strdup(session_name
);
494 if (!session_name_copy
) {
495 status
= LTTNG_CONDITION_STATUS_ERROR
;
499 free(rotation
->session_name
);
500 rotation
->session_name
= session_name_copy
;
506 int lttng_evaluation_session_rotation_serialize(
507 const struct lttng_evaluation
*evaluation
,
508 struct lttng_dynamic_buffer
*buf
)
511 struct lttng_evaluation_session_rotation
*rotation
;
512 struct lttng_evaluation_session_rotation_comm comm
= { 0 };
514 rotation
= container_of(evaluation
,
515 struct lttng_evaluation_session_rotation
, parent
);
516 comm
.id
= rotation
->id
;
517 comm
.has_location
= !!rotation
->location
;
518 ret
= lttng_dynamic_buffer_append(buf
, &comm
, sizeof(comm
));
522 if (!rotation
->location
) {
525 ret
= lttng_trace_archive_location_serialize(rotation
->location
,
532 void lttng_evaluation_session_rotation_destroy(
533 struct lttng_evaluation
*evaluation
)
535 struct lttng_evaluation_session_rotation
*rotation
;
537 rotation
= container_of(evaluation
,
538 struct lttng_evaluation_session_rotation
, parent
);
539 lttng_trace_archive_location_destroy(rotation
->location
);
543 enum lttng_evaluation_status
544 lttng_evaluation_session_rotation_get_id(
545 const struct lttng_evaluation
*evaluation
, uint64_t *id
)
547 const struct lttng_evaluation_session_rotation
*rotation
;
548 enum lttng_evaluation_status status
= LTTNG_EVALUATION_STATUS_OK
;
550 if (!evaluation
|| !id
|| !is_rotation_evaluation(evaluation
)) {
551 status
= LTTNG_EVALUATION_STATUS_INVALID
;
555 rotation
= container_of(evaluation
,
556 struct lttng_evaluation_session_rotation
, parent
);
562 enum lttng_evaluation_status
563 lttng_evaluation_session_rotation_completed_get_location(
564 const struct lttng_evaluation
*evaluation
,
565 const struct lttng_trace_archive_location
**location
)
567 const struct lttng_evaluation_session_rotation
*rotation
;
568 enum lttng_evaluation_status status
= LTTNG_EVALUATION_STATUS_OK
;
570 if (!evaluation
|| !location
||
571 evaluation
->type
!= LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
) {
572 status
= LTTNG_EVALUATION_STATUS_INVALID
;
576 rotation
= container_of(evaluation
,
577 struct lttng_evaluation_session_rotation
, parent
);
578 *location
= rotation
->location
;