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
);
338 lttng_trace_archive_location_get(location
);
340 evaluation
->location
= location
;
341 return &evaluation
->parent
;
345 ssize_t
create_evaluation_from_buffer(
346 enum lttng_condition_type type
,
347 const struct lttng_buffer_view
*view
,
348 struct lttng_evaluation
**_evaluation
)
351 struct lttng_evaluation
*evaluation
= NULL
;
352 struct lttng_trace_archive_location
*location
= NULL
;
353 const struct lttng_evaluation_session_rotation_comm
*comm
=
354 (const struct lttng_evaluation_session_rotation_comm
*) view
->data
;
355 struct lttng_buffer_view location_view
;
357 if (view
->size
< sizeof(*comm
)) {
361 size
= sizeof(*comm
);
362 if (comm
->has_location
) {
363 location_view
= lttng_buffer_view_from_view(view
, sizeof(*comm
),
365 if (!location_view
.data
) {
369 ret
= lttng_trace_archive_location_create_from_buffer(
370 &location_view
, &location
);
377 evaluation
= lttng_evaluation_session_rotation_create(type
, comm
->id
,
383 lttng_trace_archive_location_put(location
);
385 *_evaluation
= evaluation
;
388 lttng_trace_archive_location_put(location
);
394 ssize_t
lttng_evaluation_session_rotation_create_from_buffer(
395 enum lttng_condition_type type
,
396 const struct lttng_buffer_view
*view
,
397 struct lttng_evaluation
**_evaluation
)
400 struct lttng_evaluation
*evaluation
= NULL
;
407 ret
= create_evaluation_from_buffer(type
, view
, &evaluation
);
412 *_evaluation
= evaluation
;
415 lttng_evaluation_destroy(evaluation
);
420 ssize_t
lttng_evaluation_session_rotation_ongoing_create_from_buffer(
421 const struct lttng_buffer_view
*view
,
422 struct lttng_evaluation
**evaluation
)
424 return lttng_evaluation_session_rotation_create_from_buffer(
425 LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
,
430 ssize_t
lttng_evaluation_session_rotation_completed_create_from_buffer(
431 const struct lttng_buffer_view
*view
,
432 struct lttng_evaluation
**evaluation
)
434 return lttng_evaluation_session_rotation_create_from_buffer(
435 LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
,
440 struct lttng_evaluation
*lttng_evaluation_session_rotation_ongoing_create(
443 return lttng_evaluation_session_rotation_create(
444 LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
, id
,
449 struct lttng_evaluation
*lttng_evaluation_session_rotation_completed_create(
450 uint64_t id
, struct lttng_trace_archive_location
*location
)
452 return lttng_evaluation_session_rotation_create(
453 LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
, id
,
457 enum lttng_condition_status
458 lttng_condition_session_rotation_get_session_name(
459 const struct lttng_condition
*condition
,
460 const char **session_name
)
462 struct lttng_condition_session_rotation
*rotation
;
463 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
465 if (!condition
|| !is_rotation_condition(condition
) || !session_name
) {
466 status
= LTTNG_CONDITION_STATUS_INVALID
;
470 rotation
= container_of(condition
, struct lttng_condition_session_rotation
,
472 if (!rotation
->session_name
) {
473 status
= LTTNG_CONDITION_STATUS_UNSET
;
476 *session_name
= rotation
->session_name
;
481 enum lttng_condition_status
482 lttng_condition_session_rotation_set_session_name(
483 struct lttng_condition
*condition
, const char *session_name
)
485 char *session_name_copy
;
486 struct lttng_condition_session_rotation
*rotation
;
487 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
489 if (!condition
|| !is_rotation_condition(condition
) ||
490 !session_name
|| strlen(session_name
) == 0) {
491 status
= LTTNG_CONDITION_STATUS_INVALID
;
495 rotation
= container_of(condition
,
496 struct lttng_condition_session_rotation
, parent
);
497 session_name_copy
= strdup(session_name
);
498 if (!session_name_copy
) {
499 status
= LTTNG_CONDITION_STATUS_ERROR
;
503 free(rotation
->session_name
);
504 rotation
->session_name
= session_name_copy
;
510 int lttng_evaluation_session_rotation_serialize(
511 const struct lttng_evaluation
*evaluation
,
512 struct lttng_dynamic_buffer
*buf
)
515 struct lttng_evaluation_session_rotation
*rotation
;
516 struct lttng_evaluation_session_rotation_comm comm
= { 0 };
518 rotation
= container_of(evaluation
,
519 struct lttng_evaluation_session_rotation
, parent
);
520 comm
.id
= rotation
->id
;
521 comm
.has_location
= !!rotation
->location
;
522 ret
= lttng_dynamic_buffer_append(buf
, &comm
, sizeof(comm
));
526 if (!rotation
->location
) {
529 ret
= lttng_trace_archive_location_serialize(rotation
->location
,
536 void lttng_evaluation_session_rotation_destroy(
537 struct lttng_evaluation
*evaluation
)
539 struct lttng_evaluation_session_rotation
*rotation
;
541 rotation
= container_of(evaluation
,
542 struct lttng_evaluation_session_rotation
, parent
);
543 lttng_trace_archive_location_put(rotation
->location
);
547 enum lttng_evaluation_status
548 lttng_evaluation_session_rotation_get_id(
549 const struct lttng_evaluation
*evaluation
, uint64_t *id
)
551 const struct lttng_evaluation_session_rotation
*rotation
;
552 enum lttng_evaluation_status status
= LTTNG_EVALUATION_STATUS_OK
;
554 if (!evaluation
|| !id
|| !is_rotation_evaluation(evaluation
)) {
555 status
= LTTNG_EVALUATION_STATUS_INVALID
;
559 rotation
= container_of(evaluation
,
560 struct lttng_evaluation_session_rotation
, parent
);
567 * The public API assumes that trace archive locations are always provided as
568 * "constant". This means that the user of liblttng-ctl never has to destroy a
569 * trace archive location. Hence, users of liblttng-ctl have no visibility of
570 * the reference counting of archive locations.
572 enum lttng_evaluation_status
573 lttng_evaluation_session_rotation_completed_get_location(
574 const struct lttng_evaluation
*evaluation
,
575 const struct lttng_trace_archive_location
**location
)
577 const struct lttng_evaluation_session_rotation
*rotation
;
578 enum lttng_evaluation_status status
= LTTNG_EVALUATION_STATUS_OK
;
580 if (!evaluation
|| !location
||
581 evaluation
->type
!= LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
) {
582 status
= LTTNG_EVALUATION_STATUS_INVALID
;
586 rotation
= container_of(evaluation
,
587 struct lttng_evaluation_session_rotation
, parent
);
588 *location
= rotation
->location
;