446b332e41354599a18b2967f85a33456b9b0faf
[lttng-tools.git] / src / lib / lttng-ctl / rotate.c
1 /*
2 * Copyright (C) 2017 - Julien Desfossez <jdesfossez@efficios.com>
3 *
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.
7 *
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
11 * for more details.
12 *
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
16 */
17
18 #define _LGPL_SOURCE
19 #include <assert.h>
20 #include <string.h>
21
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>
28
29 #include "lttng-ctl-helper.h"
30
31 static
32 enum lttng_rotation_status ask_rotation_info(
33 struct lttng_rotation_handle *rotation_handle,
34 struct lttng_rotation_get_info_return **info)
35 {
36 /* lsm.get_rotation_state.rotation_id */
37 struct lttcomm_session_msg lsm;
38 enum lttng_rotation_status status = LTTNG_ROTATION_STATUS_OK;
39 int ret;
40
41 if (!rotation_handle || !info) {
42 status = LTTNG_ROTATION_STATUS_INVALID;
43 goto end;
44 }
45
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;
49
50 ret = lttng_strncpy(lsm.session.name, rotation_handle->session_name,
51 sizeof(lsm.session.name));
52 if (ret) {
53 status = LTTNG_ROTATION_STATUS_INVALID;
54 goto end;
55 }
56
57 ret = lttng_ctl_ask_sessiond(&lsm, (void **) info);
58 if (ret < 0) {
59 status = LTTNG_ROTATION_STATUS_ERROR;
60 goto end;
61 }
62 end:
63 return status;
64
65 }
66
67 static
68 struct lttng_trace_archive_location *
69 create_trace_archive_location_from_get_info(
70 const struct lttng_rotation_get_info_return *info)
71 {
72 struct lttng_trace_archive_location *location;
73
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);
78 break;
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);
86 break;
87 default:
88 location = NULL;
89 break;
90 }
91 return location;
92 }
93
94 enum lttng_rotation_status lttng_rotation_handle_get_state(
95 struct lttng_rotation_handle *rotation_handle,
96 enum lttng_rotation_state *state)
97 {
98 enum lttng_rotation_status status = LTTNG_ROTATION_STATUS_OK;
99 struct lttng_rotation_get_info_return *info = NULL;
100
101 if (!rotation_handle || !state) {
102 status = LTTNG_ROTATION_STATUS_INVALID;
103 goto end;
104 }
105
106 status = ask_rotation_info(rotation_handle, &info);
107 if (status != LTTNG_ROTATION_STATUS_OK) {
108 goto end;
109 }
110
111 *state = (enum lttng_rotation_state) info->status;
112 if (rotation_handle->archive_location ||
113 *state != LTTNG_ROTATION_STATE_COMPLETED) {
114 /*
115 * The path is only provided by the sessiond once
116 * the session rotation is completed, but not expired.
117 */
118 goto end;
119 }
120
121 /*
122 * Cache the location since the rotation may expire before the user
123 * has a chance to query it.
124 */
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;
129 goto end;
130 }
131 end:
132 free(info);
133 return status;
134 }
135
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)
139 {
140 enum lttng_rotation_status status = LTTNG_ROTATION_STATUS_OK;
141 struct lttng_rotation_get_info_return *info = NULL;
142
143 if (!rotation_handle || !location) {
144 status = LTTNG_ROTATION_STATUS_INVALID;
145 goto end;
146 }
147
148 /* Use the cached location we got from a previous query. */
149 if (rotation_handle->archive_location) {
150 *location = rotation_handle->archive_location;
151 goto end;
152 }
153
154 status = ask_rotation_info(rotation_handle, &info);
155 if (status != LTTNG_ROTATION_STATUS_OK) {
156 goto end;
157 }
158
159 if ((enum lttng_rotation_state) info->status !=
160 LTTNG_ROTATION_STATE_COMPLETED) {
161 status = LTTNG_ROTATION_STATUS_UNAVAILABLE;
162 goto end;
163 }
164
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;
169 goto end;
170 }
171 end:
172 free(info);
173 return status;
174 }
175
176 void lttng_rotation_handle_destroy(
177 struct lttng_rotation_handle *rotation_handle)
178 {
179 if (!rotation_handle) {
180 return;
181 }
182 lttng_trace_archive_location_destroy(rotation_handle->archive_location);
183 free(rotation_handle);
184 }
185
186 static
187 int init_rotation_handle(struct lttng_rotation_handle *rotation_handle,
188 const char *session_name,
189 struct lttng_rotate_session_return *rotate_return)
190 {
191 int ret;
192
193 ret = lttng_strncpy(rotation_handle->session_name, session_name,
194 sizeof(rotation_handle->session_name));
195 if (ret) {
196 goto end;
197 }
198
199 rotation_handle->rotation_id = rotate_return->rotation_id;
200 end:
201 return ret;
202 }
203
204 /*
205 * Rotate the output folder of the session.
206 *
207 * Return 0 on success else a negative LTTng error code.
208 */
209 int lttng_rotate_session(const char *session_name,
210 struct lttng_rotation_immediate_descriptor *descriptor,
211 struct lttng_rotation_handle **rotation_handle)
212 {
213 struct lttcomm_session_msg lsm;
214 struct lttng_rotate_session_return *rotate_return = NULL;
215 int ret;
216 size_t session_name_len;
217
218 if (!session_name) {
219 ret = -LTTNG_ERR_INVALID;
220 goto end;
221 }
222
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;
227 goto end;
228 }
229
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));
234
235 ret = lttng_ctl_ask_sessiond(&lsm, (void **) &rotate_return);
236 if (ret <= 0) {
237 *rotation_handle = NULL;
238 goto end;
239 }
240
241 *rotation_handle = zmalloc(sizeof(struct lttng_rotation_handle));
242 if (!*rotation_handle) {
243 ret = -LTTNG_ERR_NOMEM;
244 goto end;
245 }
246
247 init_rotation_handle(*rotation_handle, session_name, rotate_return);
248
249 ret = 0;
250
251 end:
252 free(rotate_return);
253 return ret;
254 }
255
256 /*
257 * Update the automatic rotation parameters.
258 * 'add' as true enables the provided schedule, false removes the shedule.
259 *
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).
263 *
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.
268 *
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.
272 */
273 static
274 enum lttng_rotation_status lttng_rotation_update_schedule(
275 const char *session_name,
276 const struct lttng_rotation_schedule *schedule,
277 bool add)
278 {
279 struct lttcomm_session_msg lsm;
280 enum lttng_rotation_status status = LTTNG_ROTATION_STATUS_OK;
281 int ret;
282
283 if (!session_name || !schedule) {
284 status = LTTNG_ROTATION_STATUS_INVALID;
285 goto end;
286 }
287
288 if (strlen(session_name) >= sizeof(lsm.session.name)) {
289 status = LTTNG_ROTATION_STATUS_INVALID;
290 goto end;
291 }
292
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));
297
298 lsm.u.rotation_set_schedule.type = (uint32_t) schedule->type;
299 switch (schedule->type) {
300 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
301 {
302 status = lttng_rotation_schedule_size_threshold_get_threshold(
303 schedule, &lsm.u.rotation_set_schedule.value);
304 if (status != LTTNG_ROTATION_STATUS_OK) {
305 if (status == LTTNG_ROTATION_STATUS_UNAVAILABLE) {
306 status = LTTNG_ROTATION_STATUS_INVALID;
307 }
308 goto end;
309 }
310
311 lsm.u.rotation_set_schedule.set = !!add;
312 break;
313 }
314 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
315 {
316 status = lttng_rotation_schedule_periodic_get_period(
317 schedule, &lsm.u.rotation_set_schedule.value);
318 if (status != LTTNG_ROTATION_STATUS_OK) {
319 if (status == LTTNG_ROTATION_STATUS_UNAVAILABLE) {
320 status = LTTNG_ROTATION_STATUS_INVALID;
321 }
322 goto end;
323 }
324
325 lsm.u.rotation_set_schedule.set = !!add;
326 break;
327 }
328 default:
329 status = LTTNG_ROTATION_STATUS_INVALID;
330 goto end;
331 }
332
333 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
334 if (ret >= 0) {
335 goto end;
336 }
337
338 switch (-ret) {
339 case LTTNG_ERR_ROTATION_SCHEDULE_SET:
340 status = LTTNG_ROTATION_STATUS_SCHEDULE_ALREADY_SET;
341 break;
342 case LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET:
343 status = LTTNG_ROTATION_STATUS_INVALID;
344 break;
345 default:
346 status = LTTNG_ROTATION_STATUS_ERROR;
347 }
348 end:
349 return status;
350 }
351
352 static
353 struct lttng_rotation_schedules *lttng_rotation_schedules_create(void)
354 {
355 return zmalloc(sizeof(struct lttng_rotation_schedules));
356 }
357
358 static
359 void lttng_schedules_add(struct lttng_rotation_schedules *schedules,
360 struct lttng_rotation_schedule *schedule)
361 {
362 schedules->schedules[schedules->count++] = schedule;
363 }
364
365 static
366 int get_schedules(const char *session_name,
367 struct lttng_rotation_schedules **_schedules)
368 {
369 int ret;
370 struct lttcomm_session_msg lsm;
371 struct lttng_session_list_schedules_return *schedules_comm;
372 struct lttng_rotation_schedules *schedules = NULL;
373 struct lttng_rotation_schedule *periodic = NULL, *size = NULL;
374
375 memset(&lsm, 0, sizeof(lsm));
376 lsm.cmd_type = LTTNG_SESSION_LIST_ROTATION_SCHEDULES;
377 lttng_ctl_copy_string(lsm.session.name, session_name,
378 sizeof(lsm.session.name));
379
380 ret = lttng_ctl_ask_sessiond(&lsm, (void **) &schedules_comm);
381 if (ret < 0) {
382 goto end;
383 }
384
385 schedules = lttng_rotation_schedules_create();
386 if (!schedules) {
387 ret = -LTTNG_ERR_NOMEM;
388 goto end;
389 }
390
391 if (schedules_comm->periodic.set == 1) {
392 enum lttng_rotation_status status;
393
394 periodic = lttng_rotation_schedule_periodic_create();
395 if (!periodic) {
396 ret = -LTTNG_ERR_NOMEM;
397 goto end;
398 }
399
400 status = lttng_rotation_schedule_periodic_set_period(
401 periodic, schedules_comm->periodic.value);
402 if (status != LTTNG_ROTATION_STATUS_OK) {
403 /*
404 * This would imply that the session daemon returned
405 * an invalid periodic rotation schedule value.
406 */
407 ret = -LTTNG_ERR_UNK;
408 goto end;
409 }
410
411 lttng_schedules_add(schedules, periodic);
412 periodic = NULL;
413 }
414
415 if (schedules_comm->size.set == 1) {
416 enum lttng_rotation_status status;
417
418 size = lttng_rotation_schedule_size_threshold_create();
419 if (!size) {
420 ret = -LTTNG_ERR_NOMEM;
421 goto end;
422 }
423
424 status = lttng_rotation_schedule_size_threshold_set_threshold(
425 size, schedules_comm->size.value);
426 if (status != LTTNG_ROTATION_STATUS_OK) {
427 /*
428 * This would imply that the session daemon returned
429 * an invalid size threshold schedule value.
430 */
431 ret = -LTTNG_ERR_UNK;
432 goto end;
433 }
434
435 lttng_schedules_add(schedules, size);
436 size = NULL;
437 }
438
439 ret = LTTNG_OK;
440 end:
441 free(schedules_comm);
442 free(periodic);
443 free(size);
444 *_schedules = schedules;
445 return ret;
446 }
447
448 enum lttng_rotation_schedule_type lttng_rotation_schedule_get_type(
449 const struct lttng_rotation_schedule *schedule)
450 {
451 return schedule ? schedule->type : LTTNG_ROTATION_SCHEDULE_TYPE_UNKNOWN;
452 }
453
454 struct lttng_rotation_schedule *
455 lttng_rotation_schedule_size_threshold_create(void)
456 {
457 struct lttng_rotation_schedule_size_threshold *schedule;
458
459 schedule = zmalloc(sizeof(*schedule));
460 if (!schedule) {
461 goto end;
462 }
463
464 schedule->parent.type = LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD;
465 end:
466 return &schedule->parent;
467 }
468
469 enum lttng_rotation_status
470 lttng_rotation_schedule_size_threshold_get_threshold(
471 const struct lttng_rotation_schedule *schedule,
472 uint64_t *size_threshold_bytes)
473 {
474 enum lttng_rotation_status status = LTTNG_ROTATION_STATUS_OK;
475 struct lttng_rotation_schedule_size_threshold *size_schedule;
476
477 if (!schedule || !size_threshold_bytes ||
478 schedule->type != LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD) {
479 status = LTTNG_ROTATION_STATUS_INVALID;
480 goto end;
481 }
482
483 size_schedule = container_of(schedule,
484 struct lttng_rotation_schedule_size_threshold,
485 parent);
486 if (size_schedule->size.set) {
487 *size_threshold_bytes = size_schedule->size.bytes;
488 } else {
489 status = LTTNG_ROTATION_STATUS_UNAVAILABLE;
490 goto end;
491 }
492 end:
493 return status;
494 }
495
496 enum lttng_rotation_status
497 lttng_rotation_schedule_size_threshold_set_threshold(
498 struct lttng_rotation_schedule *schedule,
499 uint64_t size_threshold_bytes)
500 {
501 enum lttng_rotation_status status = LTTNG_ROTATION_STATUS_OK;
502 struct lttng_rotation_schedule_size_threshold *size_schedule;
503
504 if (!schedule || size_threshold_bytes == 0 ||
505 size_threshold_bytes == -1ULL ||
506 schedule->type != LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD) {
507 status = LTTNG_ROTATION_STATUS_INVALID;
508 goto end;
509 }
510
511 size_schedule = container_of(schedule,
512 struct lttng_rotation_schedule_size_threshold,
513 parent);
514 size_schedule->size.bytes = size_threshold_bytes;
515 size_schedule->size.set = true;
516 end:
517 return status;
518 }
519
520 struct lttng_rotation_schedule *
521 lttng_rotation_schedule_periodic_create(void)
522 {
523 struct lttng_rotation_schedule_periodic *schedule;
524
525 schedule = zmalloc(sizeof(*schedule));
526 if (!schedule) {
527 goto end;
528 }
529
530 schedule->parent.type = LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC;
531 end:
532 return &schedule->parent;
533 }
534
535 enum lttng_rotation_status
536 lttng_rotation_schedule_periodic_get_period(
537 const struct lttng_rotation_schedule *schedule,
538 uint64_t *period_us)
539 {
540 enum lttng_rotation_status status = LTTNG_ROTATION_STATUS_OK;
541 struct lttng_rotation_schedule_periodic *periodic_schedule;
542
543 if (!schedule || !period_us ||
544 schedule->type != LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC) {
545 status = LTTNG_ROTATION_STATUS_INVALID;
546 goto end;
547 }
548
549 periodic_schedule = container_of(schedule,
550 struct lttng_rotation_schedule_periodic,
551 parent);
552 if (periodic_schedule->period.set) {
553 *period_us = periodic_schedule->period.us;
554 } else {
555 status = LTTNG_ROTATION_STATUS_UNAVAILABLE;
556 goto end;
557 }
558 end:
559 return status;
560 }
561
562 enum lttng_rotation_status
563 lttng_rotation_schedule_periodic_set_period(
564 struct lttng_rotation_schedule *schedule,
565 uint64_t period_us)
566 {
567 enum lttng_rotation_status status = LTTNG_ROTATION_STATUS_OK;
568 struct lttng_rotation_schedule_periodic *periodic_schedule;
569
570 if (!schedule || period_us == 0 || period_us == -1ULL ||
571 schedule->type != LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC) {
572 status = LTTNG_ROTATION_STATUS_INVALID;
573 goto end;
574 }
575
576 periodic_schedule = container_of(schedule,
577 struct lttng_rotation_schedule_periodic,
578 parent);
579 periodic_schedule->period.us = period_us;
580 periodic_schedule->period.set = true;
581 end:
582 return status;
583 }
584
585 void lttng_rotation_schedule_destroy(struct lttng_rotation_schedule *schedule)
586 {
587 if (!schedule) {
588 return;
589 }
590 free(schedule);
591 }
592
593 void lttng_rotation_schedules_destroy(
594 struct lttng_rotation_schedules *schedules)
595 {
596 unsigned int i;
597
598 if (!schedules) {
599 return;
600 }
601
602 for (i = 0; i < schedules->count; i++) {
603 lttng_rotation_schedule_destroy(schedules->schedules[i]);
604 }
605 free(schedules);
606 }
607
608
609 enum lttng_rotation_status lttng_rotation_schedules_get_count(
610 const struct lttng_rotation_schedules *schedules,
611 unsigned int *count)
612 {
613 enum lttng_rotation_status status = LTTNG_ROTATION_STATUS_OK;
614
615 if (!schedules || !count) {
616 status = LTTNG_ROTATION_STATUS_INVALID;
617 goto end;
618 }
619
620 *count = schedules->count;
621 end:
622 return status;
623 }
624
625 const struct lttng_rotation_schedule *lttng_rotation_schedules_get_at_index(
626 const struct lttng_rotation_schedules *schedules,
627 unsigned int index)
628 {
629 const struct lttng_rotation_schedule *schedule = NULL;
630
631 if (!schedules || index >= schedules->count) {
632 goto end;
633 }
634
635 schedule = schedules->schedules[index];
636 end:
637 return schedule;
638 }
639
640 enum lttng_rotation_status lttng_session_add_rotation_schedule(
641 const char *session_name,
642 const struct lttng_rotation_schedule *schedule)
643 {
644 return lttng_rotation_update_schedule(session_name, schedule, true);
645 }
646
647 enum lttng_rotation_status lttng_session_remove_rotation_schedule(
648 const char *session_name,
649 const struct lttng_rotation_schedule *schedule)
650 {
651 return lttng_rotation_update_schedule(session_name, schedule, false);
652 }
653
654 int lttng_session_list_rotation_schedules(
655 const char *session_name,
656 struct lttng_rotation_schedules **schedules)
657 {
658 return get_schedules(session_name, schedules);
659 }
This page took 0.040865 seconds and 3 git commands to generate.