2 * Copyright (C) 2017 - Julien Desfossez <jdesfossez@efficios.com>
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License, version 2.1 only,
6 * as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <lttng/lttng-error.h>
23 #include <lttng/rotation.h>
24 #include <lttng/location-internal.h>
25 #include <lttng/rotate-internal.h>
26 #include <common/sessiond-comm/sessiond-comm.h>
27 #include <common/macros.h>
29 #include "lttng-ctl-helper.h"
32 enum lttng_rotation_status
ask_rotation_info(
33 struct lttng_rotation_handle
*rotation_handle
,
34 struct lttng_rotation_get_info_return
**info
)
36 /* lsm.get_rotation_state.rotation_id */
37 struct lttcomm_session_msg lsm
;
38 enum lttng_rotation_status status
= LTTNG_ROTATION_STATUS_OK
;
41 if (!rotation_handle
|| !info
) {
42 status
= LTTNG_ROTATION_STATUS_INVALID
;
46 memset(&lsm
, 0, sizeof(lsm
));
47 lsm
.cmd_type
= LTTNG_ROTATION_GET_INFO
;
48 lsm
.u
.get_rotation_info
.rotation_id
= rotation_handle
->rotation_id
;
50 ret
= lttng_strncpy(lsm
.session
.name
, rotation_handle
->session_name
,
51 sizeof(lsm
.session
.name
));
53 status
= LTTNG_ROTATION_STATUS_INVALID
;
57 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) info
);
59 status
= LTTNG_ROTATION_STATUS_ERROR
;
68 struct lttng_trace_archive_location
*
69 create_trace_archive_location_from_get_info(
70 const struct lttng_rotation_get_info_return
*info
)
72 struct lttng_trace_archive_location
*location
;
74 switch (info
->location_type
) {
75 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
:
76 location
= lttng_trace_archive_location_local_create(
77 info
->location
.local
.absolute_path
);
79 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
:
80 location
= lttng_trace_archive_location_relay_create(
81 info
->location
.relay
.host
,
82 info
->location
.relay
.protocol
,
83 info
->location
.relay
.ports
.control
,
84 info
->location
.relay
.ports
.data
,
85 info
->location
.relay
.relative_path
);
94 enum lttng_rotation_status
lttng_rotation_handle_get_state(
95 struct lttng_rotation_handle
*rotation_handle
,
96 enum lttng_rotation_state
*state
)
98 enum lttng_rotation_status status
= LTTNG_ROTATION_STATUS_OK
;
99 struct lttng_rotation_get_info_return
*info
= NULL
;
101 if (!rotation_handle
|| !state
) {
102 status
= LTTNG_ROTATION_STATUS_INVALID
;
106 status
= ask_rotation_info(rotation_handle
, &info
);
107 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
111 *state
= (enum lttng_rotation_state
) info
->status
;
112 if (rotation_handle
->archive_location
||
113 *state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
115 * The path is only provided by the sessiond once
116 * the session rotation is completed, but not expired.
122 * Cache the location since the rotation may expire before the user
123 * has a chance to query it.
125 rotation_handle
->archive_location
=
126 create_trace_archive_location_from_get_info(info
);
127 if (!rotation_handle
->archive_location
) {
128 status
= LTTNG_ROTATION_STATUS_ERROR
;
136 enum lttng_rotation_status
lttng_rotation_handle_get_archive_location(
137 struct lttng_rotation_handle
*rotation_handle
,
138 const struct lttng_trace_archive_location
**location
)
140 enum lttng_rotation_status status
= LTTNG_ROTATION_STATUS_OK
;
141 struct lttng_rotation_get_info_return
*info
= NULL
;
143 if (!rotation_handle
|| !location
) {
144 status
= LTTNG_ROTATION_STATUS_INVALID
;
148 /* Use the cached location we got from a previous query. */
149 if (rotation_handle
->archive_location
) {
150 *location
= rotation_handle
->archive_location
;
154 status
= ask_rotation_info(rotation_handle
, &info
);
155 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
159 if ((enum lttng_rotation_state
) info
->status
!=
160 LTTNG_ROTATION_STATE_COMPLETED
) {
161 status
= LTTNG_ROTATION_STATUS_UNAVAILABLE
;
165 rotation_handle
->archive_location
=
166 create_trace_archive_location_from_get_info(info
);
167 if (!rotation_handle
->archive_location
) {
168 status
= LTTNG_ROTATION_STATUS_ERROR
;
176 void lttng_rotation_handle_destroy(
177 struct lttng_rotation_handle
*rotation_handle
)
179 if (!rotation_handle
) {
182 lttng_trace_archive_location_destroy(rotation_handle
->archive_location
);
183 free(rotation_handle
);
187 int init_rotation_handle(struct lttng_rotation_handle
*rotation_handle
,
188 const char *session_name
,
189 struct lttng_rotate_session_return
*rotate_return
)
193 ret
= lttng_strncpy(rotation_handle
->session_name
, session_name
,
194 sizeof(rotation_handle
->session_name
));
199 rotation_handle
->rotation_id
= rotate_return
->rotation_id
;
205 * Rotate the output folder of the session.
207 * Return 0 on success else a negative LTTng error code.
209 int lttng_rotate_session(const char *session_name
,
210 struct lttng_rotation_immediate_descriptor
*descriptor
,
211 struct lttng_rotation_handle
**rotation_handle
)
213 struct lttcomm_session_msg lsm
;
214 struct lttng_rotate_session_return
*rotate_return
= NULL
;
216 size_t session_name_len
;
219 ret
= -LTTNG_ERR_INVALID
;
223 session_name_len
= strlen(session_name
);
224 if (session_name_len
>= sizeof(lsm
.session
.name
) ||
225 session_name_len
>= member_sizeof(struct lttng_rotation_handle
, session_name
)) {
226 ret
= -LTTNG_ERR_INVALID
;
230 memset(&lsm
, 0, sizeof(lsm
));
231 lsm
.cmd_type
= LTTNG_ROTATE_SESSION
;
232 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
233 sizeof(lsm
.session
.name
));
235 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &rotate_return
);
237 *rotation_handle
= NULL
;
241 *rotation_handle
= zmalloc(sizeof(struct lttng_rotation_handle
));
242 if (!*rotation_handle
) {
243 ret
= -LTTNG_ERR_NOMEM
;
247 init_rotation_handle(*rotation_handle
, session_name
, rotate_return
);
257 * Update the automatic rotation parameters.
258 * 'add' as true enables the provided schedule, false removes the shedule.
260 * The external API makes it appear as though arbitrary schedules can
261 * be added or removed at will. However, the session daemon is
262 * currently limited to one schedule per type (per session).
264 * The additional flexibility of the public API is offered for future
265 * rotation schedules that could indicate more precise criteria than
266 * size and time (e.g. a domain) where it could make sense to add
267 * multiple schedules of a given type to a session.
269 * Hence, the exact schedule that the user wishes to remove (and not
270 * just its type) must be passed so that the session daemon can
271 * validate that is exists before clearing it.
274 enum lttng_rotation_status
lttng_rotation_update_schedule(
275 const char *session_name
,
276 const struct lttng_rotation_schedule
*schedule
,
279 struct lttcomm_session_msg lsm
;
280 enum lttng_rotation_status status
= LTTNG_ROTATION_STATUS_OK
;
283 if (!session_name
|| !schedule
) {
284 status
= LTTNG_ROTATION_STATUS_INVALID
;
288 if (strlen(session_name
) >= sizeof(lsm
.session
.name
)) {
289 status
= LTTNG_ROTATION_STATUS_INVALID
;
293 memset(&lsm
, 0, sizeof(lsm
));
294 lsm
.cmd_type
= LTTNG_ROTATION_SET_SCHEDULE
;
295 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
296 sizeof(lsm
.session
.name
));
298 lsm
.u
.rotation_set_schedule
.type
= (uint32_t) schedule
->type
;
299 switch (schedule
->type
) {
300 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
304 status
= lttng_rotation_schedule_size_threshold_get_threshold(
305 schedule
, &threshold
);
306 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
307 if (status
== LTTNG_ROTATION_STATUS_UNAVAILABLE
) {
308 status
= LTTNG_ROTATION_STATUS_INVALID
;
312 lsm
.u
.rotation_set_schedule
.value
= threshold
;
313 lsm
.u
.rotation_set_schedule
.set
= !!add
;
316 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
320 status
= lttng_rotation_schedule_periodic_get_period(
322 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
323 if (status
== LTTNG_ROTATION_STATUS_UNAVAILABLE
) {
324 status
= LTTNG_ROTATION_STATUS_INVALID
;
328 lsm
.u
.rotation_set_schedule
.value
= period
;
329 lsm
.u
.rotation_set_schedule
.set
= !!add
;
333 status
= LTTNG_ROTATION_STATUS_INVALID
;
337 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
343 case LTTNG_ERR_ROTATION_SCHEDULE_SET
:
344 status
= LTTNG_ROTATION_STATUS_SCHEDULE_ALREADY_SET
;
346 case LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
:
347 status
= LTTNG_ROTATION_STATUS_INVALID
;
350 status
= LTTNG_ROTATION_STATUS_ERROR
;
357 struct lttng_rotation_schedules
*lttng_rotation_schedules_create(void)
359 return zmalloc(sizeof(struct lttng_rotation_schedules
));
363 void lttng_schedules_add(struct lttng_rotation_schedules
*schedules
,
364 struct lttng_rotation_schedule
*schedule
)
366 schedules
->schedules
[schedules
->count
++] = schedule
;
370 int get_schedules(const char *session_name
,
371 struct lttng_rotation_schedules
**_schedules
)
374 struct lttcomm_session_msg lsm
;
375 struct lttng_session_list_schedules_return
*schedules_comm
;
376 struct lttng_rotation_schedules
*schedules
= NULL
;
377 struct lttng_rotation_schedule
*periodic
= NULL
, *size
= NULL
;
379 memset(&lsm
, 0, sizeof(lsm
));
380 lsm
.cmd_type
= LTTNG_SESSION_LIST_ROTATION_SCHEDULES
;
381 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
382 sizeof(lsm
.session
.name
));
384 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &schedules_comm
);
389 schedules
= lttng_rotation_schedules_create();
391 ret
= -LTTNG_ERR_NOMEM
;
395 if (schedules_comm
->periodic
.set
== 1) {
396 enum lttng_rotation_status status
;
398 periodic
= lttng_rotation_schedule_periodic_create();
400 ret
= -LTTNG_ERR_NOMEM
;
404 status
= lttng_rotation_schedule_periodic_set_period(
405 periodic
, schedules_comm
->periodic
.value
);
406 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
408 * This would imply that the session daemon returned
409 * an invalid periodic rotation schedule value.
411 ret
= -LTTNG_ERR_UNK
;
415 lttng_schedules_add(schedules
, periodic
);
419 if (schedules_comm
->size
.set
== 1) {
420 enum lttng_rotation_status status
;
422 size
= lttng_rotation_schedule_size_threshold_create();
424 ret
= -LTTNG_ERR_NOMEM
;
428 status
= lttng_rotation_schedule_size_threshold_set_threshold(
429 size
, schedules_comm
->size
.value
);
430 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
432 * This would imply that the session daemon returned
433 * an invalid size threshold schedule value.
435 ret
= -LTTNG_ERR_UNK
;
439 lttng_schedules_add(schedules
, size
);
445 free(schedules_comm
);
448 *_schedules
= schedules
;
452 enum lttng_rotation_schedule_type
lttng_rotation_schedule_get_type(
453 const struct lttng_rotation_schedule
*schedule
)
455 return schedule
? schedule
->type
: LTTNG_ROTATION_SCHEDULE_TYPE_UNKNOWN
;
458 struct lttng_rotation_schedule
*
459 lttng_rotation_schedule_size_threshold_create(void)
461 struct lttng_rotation_schedule_size_threshold
*schedule
;
463 schedule
= zmalloc(sizeof(*schedule
));
468 schedule
->parent
.type
= LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
;
470 return &schedule
->parent
;
473 enum lttng_rotation_status
474 lttng_rotation_schedule_size_threshold_get_threshold(
475 const struct lttng_rotation_schedule
*schedule
,
476 uint64_t *size_threshold_bytes
)
478 enum lttng_rotation_status status
= LTTNG_ROTATION_STATUS_OK
;
479 struct lttng_rotation_schedule_size_threshold
*size_schedule
;
481 if (!schedule
|| !size_threshold_bytes
||
482 schedule
->type
!= LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
) {
483 status
= LTTNG_ROTATION_STATUS_INVALID
;
487 size_schedule
= container_of(schedule
,
488 struct lttng_rotation_schedule_size_threshold
,
490 if (size_schedule
->size
.set
) {
491 *size_threshold_bytes
= size_schedule
->size
.bytes
;
493 status
= LTTNG_ROTATION_STATUS_UNAVAILABLE
;
500 enum lttng_rotation_status
501 lttng_rotation_schedule_size_threshold_set_threshold(
502 struct lttng_rotation_schedule
*schedule
,
503 uint64_t size_threshold_bytes
)
505 enum lttng_rotation_status status
= LTTNG_ROTATION_STATUS_OK
;
506 struct lttng_rotation_schedule_size_threshold
*size_schedule
;
508 if (!schedule
|| size_threshold_bytes
== 0 ||
509 size_threshold_bytes
== -1ULL ||
510 schedule
->type
!= LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
) {
511 status
= LTTNG_ROTATION_STATUS_INVALID
;
515 size_schedule
= container_of(schedule
,
516 struct lttng_rotation_schedule_size_threshold
,
518 size_schedule
->size
.bytes
= size_threshold_bytes
;
519 size_schedule
->size
.set
= true;
524 struct lttng_rotation_schedule
*
525 lttng_rotation_schedule_periodic_create(void)
527 struct lttng_rotation_schedule_periodic
*schedule
;
529 schedule
= zmalloc(sizeof(*schedule
));
534 schedule
->parent
.type
= LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
;
536 return &schedule
->parent
;
539 enum lttng_rotation_status
540 lttng_rotation_schedule_periodic_get_period(
541 const struct lttng_rotation_schedule
*schedule
,
544 enum lttng_rotation_status status
= LTTNG_ROTATION_STATUS_OK
;
545 struct lttng_rotation_schedule_periodic
*periodic_schedule
;
547 if (!schedule
|| !period_us
||
548 schedule
->type
!= LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
) {
549 status
= LTTNG_ROTATION_STATUS_INVALID
;
553 periodic_schedule
= container_of(schedule
,
554 struct lttng_rotation_schedule_periodic
,
556 if (periodic_schedule
->period
.set
) {
557 *period_us
= periodic_schedule
->period
.us
;
559 status
= LTTNG_ROTATION_STATUS_UNAVAILABLE
;
566 enum lttng_rotation_status
567 lttng_rotation_schedule_periodic_set_period(
568 struct lttng_rotation_schedule
*schedule
,
571 enum lttng_rotation_status status
= LTTNG_ROTATION_STATUS_OK
;
572 struct lttng_rotation_schedule_periodic
*periodic_schedule
;
574 if (!schedule
|| period_us
== 0 || period_us
== -1ULL ||
575 schedule
->type
!= LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
) {
576 status
= LTTNG_ROTATION_STATUS_INVALID
;
580 periodic_schedule
= container_of(schedule
,
581 struct lttng_rotation_schedule_periodic
,
583 periodic_schedule
->period
.us
= period_us
;
584 periodic_schedule
->period
.set
= true;
589 void lttng_rotation_schedule_destroy(struct lttng_rotation_schedule
*schedule
)
597 void lttng_rotation_schedules_destroy(
598 struct lttng_rotation_schedules
*schedules
)
606 for (i
= 0; i
< schedules
->count
; i
++) {
607 lttng_rotation_schedule_destroy(schedules
->schedules
[i
]);
613 enum lttng_rotation_status
lttng_rotation_schedules_get_count(
614 const struct lttng_rotation_schedules
*schedules
,
617 enum lttng_rotation_status status
= LTTNG_ROTATION_STATUS_OK
;
619 if (!schedules
|| !count
) {
620 status
= LTTNG_ROTATION_STATUS_INVALID
;
624 *count
= schedules
->count
;
629 const struct lttng_rotation_schedule
*lttng_rotation_schedules_get_at_index(
630 const struct lttng_rotation_schedules
*schedules
,
633 const struct lttng_rotation_schedule
*schedule
= NULL
;
635 if (!schedules
|| index
>= schedules
->count
) {
639 schedule
= schedules
->schedules
[index
];
644 enum lttng_rotation_status
lttng_session_add_rotation_schedule(
645 const char *session_name
,
646 const struct lttng_rotation_schedule
*schedule
)
648 return lttng_rotation_update_schedule(session_name
, schedule
, true);
651 enum lttng_rotation_status
lttng_session_remove_rotation_schedule(
652 const char *session_name
,
653 const struct lttng_rotation_schedule
*schedule
)
655 return lttng_rotation_update_schedule(session_name
, schedule
, false);
658 int lttng_session_list_rotation_schedules(
659 const char *session_name
,
660 struct lttng_rotation_schedules
**schedules
)
662 return get_schedules(session_name
, schedules
);