lttng-ctl rotate: fix: possible unaligned access in packed structure
[lttng-tools.git] / src / lib / lttng-ctl / rotate.c
CommitLineData
d68c9a04
JD
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>
dd73d57b 24#include <lttng/location-internal.h>
d68c9a04
JD
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
d68c9a04
JD
31static
32enum 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 }
62end:
63 return status;
64
65}
66
dd73d57b
JG
67static
68struct lttng_trace_archive_location *
69create_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
d68c9a04
JD
94enum 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;
d68c9a04
JD
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;
dd73d57b 112 if (rotation_handle->archive_location ||
d68c9a04
JD
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 */
dd73d57b
JG
125 rotation_handle->archive_location =
126 create_trace_archive_location_from_get_info(info);
127 if (!rotation_handle->archive_location) {
d68c9a04
JD
128 status = LTTNG_ROTATION_STATUS_ERROR;
129 goto end;
130 }
d68c9a04
JD
131end:
132 free(info);
133 return status;
134}
135
dd73d57b 136enum lttng_rotation_status lttng_rotation_handle_get_archive_location(
d68c9a04 137 struct lttng_rotation_handle *rotation_handle,
dd73d57b 138 const struct lttng_trace_archive_location **location)
d68c9a04 139{
d68c9a04
JD
140 enum lttng_rotation_status status = LTTNG_ROTATION_STATUS_OK;
141 struct lttng_rotation_get_info_return *info = NULL;
142
dd73d57b 143 if (!rotation_handle || !location) {
d68c9a04
JD
144 status = LTTNG_ROTATION_STATUS_INVALID;
145 goto end;
146 }
147
148 /* Use the cached location we got from a previous query. */
dd73d57b
JG
149 if (rotation_handle->archive_location) {
150 *location = rotation_handle->archive_location;
d68c9a04
JD
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
dd73d57b
JG
165 rotation_handle->archive_location =
166 create_trace_archive_location_from_get_info(info);
167 if (!rotation_handle->archive_location) {
d68c9a04
JD
168 status = LTTNG_ROTATION_STATUS_ERROR;
169 goto end;
170 }
d68c9a04
JD
171end:
172 free(info);
173 return status;
174}
175
176void lttng_rotation_handle_destroy(
177 struct lttng_rotation_handle *rotation_handle)
178{
06b180a1
JR
179 if (!rotation_handle) {
180 return;
181 }
dd73d57b 182 lttng_trace_archive_location_destroy(rotation_handle->archive_location);
d68c9a04
JD
183 free(rotation_handle);
184}
185
186static
187int init_rotation_handle(struct lttng_rotation_handle *rotation_handle,
dbd512ea 188 const char *session_name,
66ea93b1 189 struct lttng_rotate_session_return *rotate_return)
d68c9a04
JD
190{
191 int ret;
192
dbd512ea 193 ret = lttng_strncpy(rotation_handle->session_name, session_name,
d68c9a04
JD
194 sizeof(rotation_handle->session_name));
195 if (ret) {
196 goto end;
197 }
198
199 rotation_handle->rotation_id = rotate_return->rotation_id;
200end:
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 */
dbd512ea 209int lttng_rotate_session(const char *session_name,
66ea93b1 210 struct lttng_rotation_immediate_descriptor *descriptor,
d68c9a04
JD
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;
dbd512ea 216 size_t session_name_len;
d68c9a04 217
dbd512ea
JG
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)) {
d68c9a04
JD
226 ret = -LTTNG_ERR_INVALID;
227 goto end;
228 }
229
230 memset(&lsm, 0, sizeof(lsm));
231 lsm.cmd_type = LTTNG_ROTATE_SESSION;
dbd512ea 232 lttng_ctl_copy_string(lsm.session.name, session_name,
d68c9a04
JD
233 sizeof(lsm.session.name));
234
235 ret = lttng_ctl_ask_sessiond(&lsm, (void **) &rotate_return);
1320cab1 236 if (ret <= 0) {
d68c9a04
JD
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
66ea93b1 247 init_rotation_handle(*rotation_handle, session_name, rotate_return);
d68c9a04
JD
248
249 ret = 0;
250
251end:
252 free(rotate_return);
253 return ret;
254}
259c2674
JD
255
256/*
66ea93b1
JG
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.
259c2674 272 */
66ea93b1
JG
273static
274enum lttng_rotation_status lttng_rotation_update_schedule(
275 const char *session_name,
276 const struct lttng_rotation_schedule *schedule,
277 bool add)
259c2674
JD
278{
279 struct lttcomm_session_msg lsm;
66ea93b1 280 enum lttng_rotation_status status = LTTNG_ROTATION_STATUS_OK;
259c2674
JD
281 int ret;
282
66ea93b1
JG
283 if (!session_name || !schedule) {
284 status = LTTNG_ROTATION_STATUS_INVALID;
dbd512ea
JG
285 goto end;
286 }
287
288 if (strlen(session_name) >= sizeof(lsm.session.name)) {
66ea93b1 289 status = LTTNG_ROTATION_STATUS_INVALID;
259c2674
JD
290 goto end;
291 }
292
293 memset(&lsm, 0, sizeof(lsm));
294 lsm.cmd_type = LTTNG_ROTATION_SET_SCHEDULE;
dbd512ea 295 lttng_ctl_copy_string(lsm.session.name, session_name,
259c2674 296 sizeof(lsm.session.name));
66ea93b1
JG
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 {
7370c955
JG
302 uint64_t threshold;
303
66ea93b1 304 status = lttng_rotation_schedule_size_threshold_get_threshold(
7370c955 305 schedule, &threshold);
66ea93b1 306 if (status != LTTNG_ROTATION_STATUS_OK) {
ed9f1fb2
JG
307 if (status == LTTNG_ROTATION_STATUS_UNAVAILABLE) {
308 status = LTTNG_ROTATION_STATUS_INVALID;
309 }
66ea93b1
JG
310 goto end;
311 }
7370c955 312 lsm.u.rotation_set_schedule.value = threshold;
66ea93b1
JG
313 lsm.u.rotation_set_schedule.set = !!add;
314 break;
315 }
316 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
317 {
7370c955
JG
318 uint64_t period;
319
66ea93b1 320 status = lttng_rotation_schedule_periodic_get_period(
7370c955 321 schedule, &period);
66ea93b1 322 if (status != LTTNG_ROTATION_STATUS_OK) {
ed9f1fb2
JG
323 if (status == LTTNG_ROTATION_STATUS_UNAVAILABLE) {
324 status = LTTNG_ROTATION_STATUS_INVALID;
325 }
66ea93b1
JG
326 goto end;
327 }
7370c955 328 lsm.u.rotation_set_schedule.value = period;
66ea93b1
JG
329 lsm.u.rotation_set_schedule.set = !!add;
330 break;
331 }
332 default:
333 status = LTTNG_ROTATION_STATUS_INVALID;
334 goto end;
335 }
259c2674
JD
336
337 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
66ea93b1
JG
338 if (ret >= 0) {
339 goto end;
340 }
341
342 switch (-ret) {
343 case LTTNG_ERR_ROTATION_SCHEDULE_SET:
344 status = LTTNG_ROTATION_STATUS_SCHEDULE_ALREADY_SET;
345 break;
346 case LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET:
347 status = LTTNG_ROTATION_STATUS_INVALID;
348 break;
349 default:
350 status = LTTNG_ROTATION_STATUS_ERROR;
351 }
259c2674 352end:
66ea93b1
JG
353 return status;
354}
355
356static
357struct lttng_rotation_schedules *lttng_rotation_schedules_create(void)
358{
359 return zmalloc(sizeof(struct lttng_rotation_schedules));
259c2674 360}
329f3443 361
66ea93b1
JG
362static
363void lttng_schedules_add(struct lttng_rotation_schedules *schedules,
364 struct lttng_rotation_schedule *schedule)
365{
366 schedules->schedules[schedules->count++] = schedule;
367}
368
369static
370int get_schedules(const char *session_name,
371 struct lttng_rotation_schedules **_schedules)
329f3443 372{
329f3443 373 int ret;
66ea93b1
JG
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;
329f3443
JD
378
379 memset(&lsm, 0, sizeof(lsm));
66ea93b1 380 lsm.cmd_type = LTTNG_SESSION_LIST_ROTATION_SCHEDULES;
329f3443
JD
381 lttng_ctl_copy_string(lsm.session.name, session_name,
382 sizeof(lsm.session.name));
383
66ea93b1 384 ret = lttng_ctl_ask_sessiond(&lsm, (void **) &schedules_comm);
329f3443 385 if (ret < 0) {
329f3443
JD
386 goto end;
387 }
388
66ea93b1
JG
389 schedules = lttng_rotation_schedules_create();
390 if (!schedules) {
391 ret = -LTTNG_ERR_NOMEM;
392 goto end;
393 }
394
395 if (schedules_comm->periodic.set == 1) {
396 enum lttng_rotation_status status;
397
398 periodic = lttng_rotation_schedule_periodic_create();
399 if (!periodic) {
400 ret = -LTTNG_ERR_NOMEM;
401 goto end;
402 }
403
404 status = lttng_rotation_schedule_periodic_set_period(
405 periodic, schedules_comm->periodic.value);
406 if (status != LTTNG_ROTATION_STATUS_OK) {
407 /*
408 * This would imply that the session daemon returned
409 * an invalid periodic rotation schedule value.
410 */
411 ret = -LTTNG_ERR_UNK;
412 goto end;
413 }
414
415 lttng_schedules_add(schedules, periodic);
416 periodic = NULL;
417 }
418
419 if (schedules_comm->size.set == 1) {
420 enum lttng_rotation_status status;
421
422 size = lttng_rotation_schedule_size_threshold_create();
423 if (!size) {
424 ret = -LTTNG_ERR_NOMEM;
425 goto end;
426 }
427
428 status = lttng_rotation_schedule_size_threshold_set_threshold(
429 size, schedules_comm->size.value);
430 if (status != LTTNG_ROTATION_STATUS_OK) {
431 /*
432 * This would imply that the session daemon returned
433 * an invalid size threshold schedule value.
434 */
435 ret = -LTTNG_ERR_UNK;
436 goto end;
437 }
438
439 lttng_schedules_add(schedules, size);
440 size = NULL;
441 }
442
443 ret = LTTNG_OK;
329f3443 444end:
66ea93b1
JG
445 free(schedules_comm);
446 free(periodic);
447 free(size);
448 *_schedules = schedules;
329f3443
JD
449 return ret;
450}
451
66ea93b1
JG
452enum lttng_rotation_schedule_type lttng_rotation_schedule_get_type(
453 const struct lttng_rotation_schedule *schedule)
329f3443 454{
66ea93b1
JG
455 return schedule ? schedule->type : LTTNG_ROTATION_SCHEDULE_TYPE_UNKNOWN;
456}
329f3443 457
66ea93b1
JG
458struct lttng_rotation_schedule *
459lttng_rotation_schedule_size_threshold_create(void)
460{
461 struct lttng_rotation_schedule_size_threshold *schedule;
329f3443 462
66ea93b1
JG
463 schedule = zmalloc(sizeof(*schedule));
464 if (!schedule) {
329f3443
JD
465 goto end;
466 }
467
66ea93b1
JG
468 schedule->parent.type = LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD;
469end:
470 return &schedule->parent;
471}
472
473enum lttng_rotation_status
474lttng_rotation_schedule_size_threshold_get_threshold(
475 const struct lttng_rotation_schedule *schedule,
476 uint64_t *size_threshold_bytes)
477{
478 enum lttng_rotation_status status = LTTNG_ROTATION_STATUS_OK;
479 struct lttng_rotation_schedule_size_threshold *size_schedule;
329f3443 480
ed9f1fb2
JG
481 if (!schedule || !size_threshold_bytes ||
482 schedule->type != LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD) {
66ea93b1
JG
483 status = LTTNG_ROTATION_STATUS_INVALID;
484 goto end;
485 }
486
487 size_schedule = container_of(schedule,
488 struct lttng_rotation_schedule_size_threshold,
489 parent);
490 if (size_schedule->size.set) {
491 *size_threshold_bytes = size_schedule->size.bytes;
492 } else {
493 status = LTTNG_ROTATION_STATUS_UNAVAILABLE;
494 goto end;
495 }
496end:
497 return status;
498}
499
500enum lttng_rotation_status
501lttng_rotation_schedule_size_threshold_set_threshold(
502 struct lttng_rotation_schedule *schedule,
503 uint64_t size_threshold_bytes)
504{
505 enum lttng_rotation_status status = LTTNG_ROTATION_STATUS_OK;
506 struct lttng_rotation_schedule_size_threshold *size_schedule;
507
508 if (!schedule || size_threshold_bytes == 0 ||
ed9f1fb2
JG
509 size_threshold_bytes == -1ULL ||
510 schedule->type != LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD) {
66ea93b1
JG
511 status = LTTNG_ROTATION_STATUS_INVALID;
512 goto end;
513 }
329f3443 514
66ea93b1
JG
515 size_schedule = container_of(schedule,
516 struct lttng_rotation_schedule_size_threshold,
517 parent);
518 size_schedule->size.bytes = size_threshold_bytes;
519 size_schedule->size.set = true;
329f3443 520end:
66ea93b1
JG
521 return status;
522}
523
524struct lttng_rotation_schedule *
525lttng_rotation_schedule_periodic_create(void)
526{
527 struct lttng_rotation_schedule_periodic *schedule;
528
529 schedule = zmalloc(sizeof(*schedule));
530 if (!schedule) {
531 goto end;
532 }
533
534 schedule->parent.type = LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC;
535end:
536 return &schedule->parent;
537}
538
539enum lttng_rotation_status
540lttng_rotation_schedule_periodic_get_period(
541 const struct lttng_rotation_schedule *schedule,
542 uint64_t *period_us)
543{
544 enum lttng_rotation_status status = LTTNG_ROTATION_STATUS_OK;
545 struct lttng_rotation_schedule_periodic *periodic_schedule;
546
ed9f1fb2
JG
547 if (!schedule || !period_us ||
548 schedule->type != LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC) {
66ea93b1
JG
549 status = LTTNG_ROTATION_STATUS_INVALID;
550 goto end;
551 }
552
553 periodic_schedule = container_of(schedule,
554 struct lttng_rotation_schedule_periodic,
555 parent);
556 if (periodic_schedule->period.set) {
557 *period_us = periodic_schedule->period.us;
558 } else {
559 status = LTTNG_ROTATION_STATUS_UNAVAILABLE;
560 goto end;
561 }
562end:
563 return status;
564}
565
566enum lttng_rotation_status
567lttng_rotation_schedule_periodic_set_period(
568 struct lttng_rotation_schedule *schedule,
569 uint64_t period_us)
570{
571 enum lttng_rotation_status status = LTTNG_ROTATION_STATUS_OK;
572 struct lttng_rotation_schedule_periodic *periodic_schedule;
573
ed9f1fb2
JG
574 if (!schedule || period_us == 0 || period_us == -1ULL ||
575 schedule->type != LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC) {
66ea93b1
JG
576 status = LTTNG_ROTATION_STATUS_INVALID;
577 goto end;
578 }
579
580 periodic_schedule = container_of(schedule,
581 struct lttng_rotation_schedule_periodic,
582 parent);
583 periodic_schedule->period.us = period_us;
584 periodic_schedule->period.set = true;
585end:
586 return status;
587}
588
589void lttng_rotation_schedule_destroy(struct lttng_rotation_schedule *schedule)
590{
591 if (!schedule) {
592 return;
593 }
594 free(schedule);
595}
596
597void lttng_rotation_schedules_destroy(
598 struct lttng_rotation_schedules *schedules)
599{
600 unsigned int i;
601
602 if (!schedules) {
603 return;
604 }
605
606 for (i = 0; i < schedules->count; i++) {
607 lttng_rotation_schedule_destroy(schedules->schedules[i]);
608 }
609 free(schedules);
610}
611
612
613enum lttng_rotation_status lttng_rotation_schedules_get_count(
614 const struct lttng_rotation_schedules *schedules,
615 unsigned int *count)
616{
617 enum lttng_rotation_status status = LTTNG_ROTATION_STATUS_OK;
618
619 if (!schedules || !count) {
620 status = LTTNG_ROTATION_STATUS_INVALID;
621 goto end;
622 }
623
624 *count = schedules->count;
625end:
626 return status;
627}
628
629const struct lttng_rotation_schedule *lttng_rotation_schedules_get_at_index(
630 const struct lttng_rotation_schedules *schedules,
631 unsigned int index)
632{
633 const struct lttng_rotation_schedule *schedule = NULL;
634
635 if (!schedules || index >= schedules->count) {
636 goto end;
637 }
638
639 schedule = schedules->schedules[index];
640end:
641 return schedule;
642}
643
644enum lttng_rotation_status lttng_session_add_rotation_schedule(
645 const char *session_name,
646 const struct lttng_rotation_schedule *schedule)
647{
648 return lttng_rotation_update_schedule(session_name, schedule, true);
649}
650
651enum lttng_rotation_status lttng_session_remove_rotation_schedule(
652 const char *session_name,
653 const struct lttng_rotation_schedule *schedule)
654{
655 return lttng_rotation_update_schedule(session_name, schedule, false);
656}
657
658int lttng_session_list_rotation_schedules(
659 const char *session_name,
660 struct lttng_rotation_schedules **schedules)
661{
662 return get_schedules(session_name, schedules);
329f3443 663}
This page took 0.057797 seconds and 5 git commands to generate.