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 enum lttng_rotation_status
lttng_rotation_handle_get_state(
153 struct lttng_rotation_handle
*rotation_handle
,
154 enum lttng_rotation_state
*state
)
156 enum lttng_rotation_status status
= LTTNG_ROTATION_STATUS_OK
;
157 struct lttng_rotation_get_info_return
*info
= NULL
;
160 if (!rotation_handle
|| !state
) {
161 status
= LTTNG_ROTATION_STATUS_INVALID
;
165 status
= ask_rotation_info(rotation_handle
, &info
);
166 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
170 *state
= (enum lttng_rotation_state
) info
->status
;
171 if (rotation_handle
->archive_location
.is_set
||
172 *state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
174 * The path is only provided by the sessiond once
175 * the session rotation is completed, but not expired.
181 * Cache the location since the rotation may expire before the user
182 * has a chance to query it.
184 ret
= lttng_strncpy(rotation_handle
->archive_location
.path
,
186 sizeof(rotation_handle
->archive_location
.path
));
188 status
= LTTNG_ROTATION_STATUS_ERROR
;
191 rotation_handle
->archive_location
.is_set
= true;
197 enum lttng_rotation_status
lttng_rotation_handle_get_completed_archive_location(
198 struct lttng_rotation_handle
*rotation_handle
,
202 enum lttng_rotation_status status
= LTTNG_ROTATION_STATUS_OK
;
203 struct lttng_rotation_get_info_return
*info
= NULL
;
205 if (!rotation_handle
|| !path
) {
206 status
= LTTNG_ROTATION_STATUS_INVALID
;
210 /* Use the cached location we got from a previous query. */
211 if (rotation_handle
->archive_location
.is_set
) {
212 *path
= rotation_handle
->archive_location
.path
;
216 status
= ask_rotation_info(rotation_handle
, &info
);
217 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
221 if ((enum lttng_rotation_state
) info
->status
!=
222 LTTNG_ROTATION_STATE_COMPLETED
) {
223 status
= LTTNG_ROTATION_STATUS_UNAVAILABLE
;
227 ret
= lttng_strncpy(rotation_handle
->archive_location
.path
,
229 sizeof(rotation_handle
->archive_location
.path
));
231 status
= LTTNG_ROTATION_STATUS_ERROR
;
234 rotation_handle
->archive_location
.is_set
= true;
240 void lttng_rotation_handle_destroy(
241 struct lttng_rotation_handle
*rotation_handle
)
243 free(rotation_handle
);
247 int init_rotation_handle(struct lttng_rotation_handle
*rotation_handle
,
248 struct lttng_rotate_session_return
*rotate_return
,
249 struct lttng_rotation_immediate_attr
*attr
)
253 ret
= lttng_strncpy(rotation_handle
->session_name
, attr
->session_name
,
254 sizeof(rotation_handle
->session_name
));
259 rotation_handle
->rotation_id
= rotate_return
->rotation_id
;
265 * Rotate the output folder of the session.
267 * Return 0 on success else a negative LTTng error code.
269 int lttng_rotate_session(struct lttng_rotation_immediate_attr
*attr
,
270 struct lttng_rotation_handle
**rotation_handle
)
272 struct lttcomm_session_msg lsm
;
273 struct lttng_rotate_session_return
*rotate_return
= NULL
;
277 ret
= -LTTNG_ERR_INVALID
;
281 memset(&lsm
, 0, sizeof(lsm
));
282 lsm
.cmd_type
= LTTNG_ROTATE_SESSION
;
283 lttng_ctl_copy_string(lsm
.session
.name
, attr
->session_name
,
284 sizeof(lsm
.session
.name
));
286 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &rotate_return
);
288 *rotation_handle
= NULL
;
292 *rotation_handle
= zmalloc(sizeof(struct lttng_rotation_handle
));
293 if (!*rotation_handle
) {
294 ret
= -LTTNG_ERR_NOMEM
;
298 init_rotation_handle(*rotation_handle
, rotate_return
, attr
);
308 * Configure the automatic rotate parameters.
310 int lttng_rotation_set_schedule(
311 struct lttng_rotation_schedule_attr
*attr
)
313 struct lttcomm_session_msg lsm
;
317 ret
= -LTTNG_ERR_INVALID
;
321 memset(&lsm
, 0, sizeof(lsm
));
322 lsm
.cmd_type
= LTTNG_ROTATION_SET_SCHEDULE
;
323 lttng_ctl_copy_string(lsm
.session
.name
, attr
->session_name
,
324 sizeof(lsm
.session
.name
));
325 lsm
.u
.rotate_setup
.timer_us
= attr
->timer_us
;
327 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);