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/rotate-internal.h>
25 #include <common/sessiond-comm/sessiond-comm.h>
26 #include <common/macros.h>
28 #include "lttng-ctl-helper.h"
30 struct lttng_rotation_immediate_attr
*lttng_rotation_immediate_attr_create(void)
32 return zmalloc(sizeof(struct lttng_rotation_immediate_attr
));
35 struct lttng_rotation_schedule_attr
*lttng_rotation_schedule_attr_create(void)
37 return zmalloc(sizeof(struct lttng_rotation_schedule_attr
));
40 void lttng_rotation_immediate_attr_destroy(
41 struct lttng_rotation_immediate_attr
*attr
)
46 void lttng_rotation_schedule_attr_destroy(struct lttng_rotation_schedule_attr
*attr
)
54 enum lttng_rotation_status
lttng_rotation_immediate_attr_set_session_name(
55 struct lttng_rotation_immediate_attr
*attr
,
56 const char *session_name
)
58 enum lttng_rotation_status status
= LTTNG_ROTATION_STATUS_OK
;
61 if (!attr
|| !session_name
) {
62 status
= LTTNG_ROTATION_STATUS_INVALID
;
66 ret
= lttng_strncpy(attr
->session_name
, session_name
,
67 sizeof(attr
->session_name
));
69 status
= LTTNG_ROTATION_STATUS_INVALID
;
78 enum lttng_rotation_status
ask_rotation_info(
79 struct lttng_rotation_handle
*rotation_handle
,
80 struct lttng_rotation_get_info_return
**info
)
82 /* lsm.get_rotation_state.rotation_id */
83 struct lttcomm_session_msg lsm
;
84 enum lttng_rotation_status status
= LTTNG_ROTATION_STATUS_OK
;
87 if (!rotation_handle
|| !info
) {
88 status
= LTTNG_ROTATION_STATUS_INVALID
;
92 memset(&lsm
, 0, sizeof(lsm
));
93 lsm
.cmd_type
= LTTNG_ROTATION_GET_INFO
;
94 lsm
.u
.get_rotation_info
.rotation_id
= rotation_handle
->rotation_id
;
96 ret
= lttng_strncpy(lsm
.session
.name
, rotation_handle
->session_name
,
97 sizeof(lsm
.session
.name
));
99 status
= LTTNG_ROTATION_STATUS_INVALID
;
103 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) info
);
105 status
= LTTNG_ROTATION_STATUS_ERROR
;
113 enum lttng_rotation_status
lttng_rotation_schedule_attr_set_session_name(
114 struct lttng_rotation_schedule_attr
*attr
,
115 const char *session_name
)
117 enum lttng_rotation_status status
= LTTNG_ROTATION_STATUS_OK
;
120 if (!attr
|| !session_name
) {
121 status
= LTTNG_ROTATION_STATUS_INVALID
;
125 ret
= lttng_strncpy(attr
->session_name
, session_name
,
126 sizeof(attr
->session_name
));
128 status
= LTTNG_ROTATION_STATUS_INVALID
;
136 enum lttng_rotation_status
lttng_rotation_schedule_attr_set_timer_period(
137 struct lttng_rotation_schedule_attr
*attr
,
140 enum lttng_rotation_status status
= LTTNG_ROTATION_STATUS_OK
;
143 status
= LTTNG_ROTATION_STATUS_INVALID
;
147 attr
->timer_us
= timer
;
152 void lttng_rotation_schedule_attr_set_size(
153 struct lttng_rotation_schedule_attr
*attr
, uint64_t size
)
158 enum lttng_rotation_status
lttng_rotation_handle_get_state(
159 struct lttng_rotation_handle
*rotation_handle
,
160 enum lttng_rotation_state
*state
)
162 enum lttng_rotation_status status
= LTTNG_ROTATION_STATUS_OK
;
163 struct lttng_rotation_get_info_return
*info
= NULL
;
166 if (!rotation_handle
|| !state
) {
167 status
= LTTNG_ROTATION_STATUS_INVALID
;
171 status
= ask_rotation_info(rotation_handle
, &info
);
172 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
176 *state
= (enum lttng_rotation_state
) info
->status
;
177 if (rotation_handle
->archive_location
.is_set
||
178 *state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
180 * The path is only provided by the sessiond once
181 * the session rotation is completed, but not expired.
187 * Cache the location since the rotation may expire before the user
188 * has a chance to query it.
190 ret
= lttng_strncpy(rotation_handle
->archive_location
.path
,
192 sizeof(rotation_handle
->archive_location
.path
));
194 status
= LTTNG_ROTATION_STATUS_ERROR
;
197 rotation_handle
->archive_location
.is_set
= true;
203 enum lttng_rotation_status
lttng_rotation_handle_get_completed_archive_location(
204 struct lttng_rotation_handle
*rotation_handle
,
208 enum lttng_rotation_status status
= LTTNG_ROTATION_STATUS_OK
;
209 struct lttng_rotation_get_info_return
*info
= NULL
;
211 if (!rotation_handle
|| !path
) {
212 status
= LTTNG_ROTATION_STATUS_INVALID
;
216 /* Use the cached location we got from a previous query. */
217 if (rotation_handle
->archive_location
.is_set
) {
218 *path
= rotation_handle
->archive_location
.path
;
222 status
= ask_rotation_info(rotation_handle
, &info
);
223 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
227 if ((enum lttng_rotation_state
) info
->status
!=
228 LTTNG_ROTATION_STATE_COMPLETED
) {
229 status
= LTTNG_ROTATION_STATUS_UNAVAILABLE
;
233 ret
= lttng_strncpy(rotation_handle
->archive_location
.path
,
235 sizeof(rotation_handle
->archive_location
.path
));
237 status
= LTTNG_ROTATION_STATUS_ERROR
;
240 rotation_handle
->archive_location
.is_set
= true;
246 void lttng_rotation_handle_destroy(
247 struct lttng_rotation_handle
*rotation_handle
)
249 free(rotation_handle
);
253 int init_rotation_handle(struct lttng_rotation_handle
*rotation_handle
,
254 struct lttng_rotate_session_return
*rotate_return
,
255 struct lttng_rotation_immediate_attr
*attr
)
259 ret
= lttng_strncpy(rotation_handle
->session_name
, attr
->session_name
,
260 sizeof(rotation_handle
->session_name
));
265 rotation_handle
->rotation_id
= rotate_return
->rotation_id
;
271 * Rotate the output folder of the session.
273 * Return 0 on success else a negative LTTng error code.
275 int lttng_rotate_session(struct lttng_rotation_immediate_attr
*attr
,
276 struct lttng_rotation_handle
**rotation_handle
)
278 struct lttcomm_session_msg lsm
;
279 struct lttng_rotate_session_return
*rotate_return
= NULL
;
283 ret
= -LTTNG_ERR_INVALID
;
287 memset(&lsm
, 0, sizeof(lsm
));
288 lsm
.cmd_type
= LTTNG_ROTATE_SESSION
;
289 lttng_ctl_copy_string(lsm
.session
.name
, attr
->session_name
,
290 sizeof(lsm
.session
.name
));
292 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &rotate_return
);
294 *rotation_handle
= NULL
;
298 *rotation_handle
= zmalloc(sizeof(struct lttng_rotation_handle
));
299 if (!*rotation_handle
) {
300 ret
= -LTTNG_ERR_NOMEM
;
304 init_rotation_handle(*rotation_handle
, rotate_return
, attr
);
314 * Configure the automatic rotate parameters.
316 int lttng_rotation_set_schedule(
317 struct lttng_rotation_schedule_attr
*attr
)
319 struct lttcomm_session_msg lsm
;
323 ret
= -LTTNG_ERR_INVALID
;
327 memset(&lsm
, 0, sizeof(lsm
));
328 lsm
.cmd_type
= LTTNG_ROTATION_SET_SCHEDULE
;
329 lttng_ctl_copy_string(lsm
.session
.name
, attr
->session_name
,
330 sizeof(lsm
.session
.name
));
331 lsm
.u
.rotate_setup
.timer_us
= attr
->timer_us
;
332 lsm
.u
.rotate_setup
.size
= attr
->size
;
334 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
339 int lttng_rotation_schedule_get_timer_period(const char *session_name
,
340 uint64_t *rotate_timer
)
342 struct lttcomm_session_msg lsm
;
343 struct lttng_rotation_schedule_get_timer_period
*get_timer
= NULL
;
346 memset(&lsm
, 0, sizeof(lsm
));
347 lsm
.cmd_type
= LTTNG_ROTATION_SCHEDULE_GET_TIMER_PERIOD
;
348 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
349 sizeof(lsm
.session
.name
));
351 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &get_timer
);
357 *rotate_timer
= get_timer
->rotate_timer
;
364 int lttng_rotation_schedule_get_size(const char *session_name
,
365 uint64_t *rotate_size
)
367 struct lttcomm_session_msg lsm
;
368 struct lttng_rotation_schedule_get_size
*get_size
= NULL
;
371 memset(&lsm
, 0, sizeof(lsm
));
372 lsm
.cmd_type
= LTTNG_ROTATION_SCHEDULE_GET_SIZE
;
373 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
374 sizeof(lsm
.session
.name
));
376 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &get_size
);
382 *rotate_size
= get_size
->rotate_size
;