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 void lttng_rotation_immediate_attr_destroy(
36 struct lttng_rotation_immediate_attr
*attr
)
41 enum lttng_rotation_status
lttng_rotation_immediate_attr_set_session_name(
42 struct lttng_rotation_immediate_attr
*attr
,
43 const char *session_name
)
45 enum lttng_rotation_status status
= LTTNG_ROTATION_STATUS_OK
;
48 if (!attr
|| !session_name
) {
49 status
= LTTNG_ROTATION_STATUS_INVALID
;
53 ret
= lttng_strncpy(attr
->session_name
, session_name
,
54 sizeof(attr
->session_name
));
56 status
= LTTNG_ROTATION_STATUS_INVALID
;
65 enum lttng_rotation_status
ask_rotation_info(
66 struct lttng_rotation_handle
*rotation_handle
,
67 struct lttng_rotation_get_info_return
**info
)
69 /* lsm.get_rotation_state.rotation_id */
70 struct lttcomm_session_msg lsm
;
71 enum lttng_rotation_status status
= LTTNG_ROTATION_STATUS_OK
;
74 if (!rotation_handle
|| !info
) {
75 status
= LTTNG_ROTATION_STATUS_INVALID
;
79 memset(&lsm
, 0, sizeof(lsm
));
80 lsm
.cmd_type
= LTTNG_ROTATION_GET_INFO
;
81 lsm
.u
.get_rotation_info
.rotation_id
= rotation_handle
->rotation_id
;
83 ret
= lttng_strncpy(lsm
.session
.name
, rotation_handle
->session_name
,
84 sizeof(lsm
.session
.name
));
86 status
= LTTNG_ROTATION_STATUS_INVALID
;
90 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) info
);
92 status
= LTTNG_ROTATION_STATUS_ERROR
;
100 enum lttng_rotation_status
lttng_rotation_handle_get_state(
101 struct lttng_rotation_handle
*rotation_handle
,
102 enum lttng_rotation_state
*state
)
104 enum lttng_rotation_status status
= LTTNG_ROTATION_STATUS_OK
;
105 struct lttng_rotation_get_info_return
*info
= NULL
;
108 if (!rotation_handle
|| !state
) {
109 status
= LTTNG_ROTATION_STATUS_INVALID
;
113 status
= ask_rotation_info(rotation_handle
, &info
);
114 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
118 *state
= (enum lttng_rotation_state
) info
->status
;
119 if (rotation_handle
->archive_location
.is_set
||
120 *state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
122 * The path is only provided by the sessiond once
123 * the session rotation is completed, but not expired.
129 * Cache the location since the rotation may expire before the user
130 * has a chance to query it.
132 ret
= lttng_strncpy(rotation_handle
->archive_location
.path
,
134 sizeof(rotation_handle
->archive_location
.path
));
136 status
= LTTNG_ROTATION_STATUS_ERROR
;
139 rotation_handle
->archive_location
.is_set
= true;
145 enum lttng_rotation_status
lttng_rotation_handle_get_completed_archive_location(
146 struct lttng_rotation_handle
*rotation_handle
,
150 enum lttng_rotation_status status
= LTTNG_ROTATION_STATUS_OK
;
151 struct lttng_rotation_get_info_return
*info
= NULL
;
153 if (!rotation_handle
|| !path
) {
154 status
= LTTNG_ROTATION_STATUS_INVALID
;
158 /* Use the cached location we got from a previous query. */
159 if (rotation_handle
->archive_location
.is_set
) {
160 *path
= rotation_handle
->archive_location
.path
;
164 status
= ask_rotation_info(rotation_handle
, &info
);
165 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
169 if ((enum lttng_rotation_state
) info
->status
!=
170 LTTNG_ROTATION_STATE_COMPLETED
) {
171 status
= LTTNG_ROTATION_STATUS_UNAVAILABLE
;
175 ret
= lttng_strncpy(rotation_handle
->archive_location
.path
,
177 sizeof(rotation_handle
->archive_location
.path
));
179 status
= LTTNG_ROTATION_STATUS_ERROR
;
182 rotation_handle
->archive_location
.is_set
= true;
188 void lttng_rotation_handle_destroy(
189 struct lttng_rotation_handle
*rotation_handle
)
191 free(rotation_handle
);
195 int init_rotation_handle(struct lttng_rotation_handle
*rotation_handle
,
196 struct lttng_rotate_session_return
*rotate_return
,
197 struct lttng_rotation_immediate_attr
*attr
)
201 ret
= lttng_strncpy(rotation_handle
->session_name
, attr
->session_name
,
202 sizeof(rotation_handle
->session_name
));
207 rotation_handle
->rotation_id
= rotate_return
->rotation_id
;
213 * Rotate the output folder of the session.
215 * Return 0 on success else a negative LTTng error code.
217 int lttng_rotate_session(struct lttng_rotation_immediate_attr
*attr
,
218 struct lttng_rotation_handle
**rotation_handle
)
220 struct lttcomm_session_msg lsm
;
221 struct lttng_rotate_session_return
*rotate_return
= NULL
;
225 ret
= -LTTNG_ERR_INVALID
;
229 memset(&lsm
, 0, sizeof(lsm
));
230 lsm
.cmd_type
= LTTNG_ROTATE_SESSION
;
231 lttng_ctl_copy_string(lsm
.session
.name
, attr
->session_name
,
232 sizeof(lsm
.session
.name
));
234 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &rotate_return
);
236 *rotation_handle
= NULL
;
240 *rotation_handle
= zmalloc(sizeof(struct lttng_rotation_handle
));
241 if (!*rotation_handle
) {
242 ret
= -LTTNG_ERR_NOMEM
;
246 init_rotation_handle(*rotation_handle
, rotate_return
, attr
);