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/rotate.h>
24 #include <lttng/rotate-internal.h>
25 #include <common/sessiond-comm/sessiond-comm.h>
27 #include "lttng-ctl-helper.h"
29 struct lttng_rotate_session_attr
*lttng_rotate_session_attr_create(void)
31 return zmalloc(sizeof(struct lttng_rotate_session_attr
));
34 void lttng_rotate_session_attr_destroy(struct lttng_rotate_session_attr
*attr
)
42 int lttng_rotate_session_attr_set_session_name(
43 struct lttng_rotate_session_attr
*attr
,
44 const char *session_name
)
49 if (!attr
|| !session_name
) {
50 ret
= -LTTNG_ERR_INVALID
;
54 len
= strlen(session_name
);
55 if (len
>= LTTNG_NAME_MAX
) {
56 ret
= -LTTNG_ERR_INVALID
;
60 strncpy(attr
->session_name
, session_name
, len
);
66 void lttng_rotate_session_attr_set_timer(
67 struct lttng_rotate_session_attr
*attr
,
70 attr
->timer_us
= timer
;
73 void lttng_rotate_session_attr_set_size(
74 struct lttng_rotate_session_attr
*attr
,
80 enum lttng_rotate_status
lttng_rotate_session_get_status(
81 struct lttng_rotate_session_handle
*rotate_handle
)
84 return LTTNG_ROTATE_ERROR
;
86 return rotate_handle
->status
;
89 int lttng_rotate_session_get_output_path(
90 struct lttng_rotate_session_handle
*rotate_handle
,
95 *path
= zmalloc(PATH_MAX
);
101 if (rotate_handle
->status
== LTTNG_ROTATE_COMPLETED
) {
102 snprintf(*path
, PATH_MAX
, "%s", rotate_handle
->output_path
);
112 void lttng_rotate_session_handle_destroy(
113 struct lttng_rotate_session_handle
*rotate_handle
)
115 if (!rotate_handle
) {
119 rotate_handle
= NULL
;
123 void init_rotate_handle(struct lttng_rotate_session_handle
*rotate_handle
,
124 struct lttng_rotate_session_return
*rotate_return
,
125 struct lttng_rotate_session_attr
*attr
)
127 snprintf(rotate_handle
->session_name
, LTTNG_NAME_MAX
, "%s",
129 rotate_handle
->rotate_id
= rotate_return
->rotate_id
;
130 rotate_handle
->status
= rotate_return
->status
;
134 * Rotate the output folder of the session.
136 * Return 0 on success else a negative LTTng error code.
138 int lttng_rotate_session(struct lttng_rotate_session_attr
*attr
,
139 struct lttng_rotate_session_handle
**rotate_handle
)
141 struct lttcomm_session_msg lsm
;
142 struct lttng_rotate_session_return
*rotate_return
= NULL
;
146 ret
= -LTTNG_ERR_INVALID
;
150 memset(&lsm
, 0, sizeof(lsm
));
151 lsm
.cmd_type
= LTTNG_ROTATE_SESSION
;
152 lttng_ctl_copy_string(lsm
.session
.name
, attr
->session_name
,
153 sizeof(lsm
.session
.name
));
155 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &rotate_return
);
156 fprintf(stderr
, "RET: %d\n", ret
);
158 *rotate_handle
= NULL
;
162 *rotate_handle
= zmalloc(sizeof(struct lttng_rotate_session_handle
));
163 if (!*rotate_handle
) {
164 ret
= -LTTNG_ERR_NOMEM
;
168 init_rotate_handle(*rotate_handle
, rotate_return
, attr
);
176 * Ask the session daemon if the current rotation is complete.
177 * If it is, return 0 and populate the output_path with the path of the
178 * rotated chunk. Return 1 if the rotation is pending.
180 int lttng_rotate_session_pending(
181 struct lttng_rotate_session_handle
*rotate_handle
)
183 /* lsm.rotate_pending.rotate_id */
184 struct lttcomm_session_msg lsm
;
185 struct lttng_rotate_session_attr attr
;
187 struct lttng_rotate_pending_return
*pending_return
= NULL
;
189 snprintf(attr
.session_name
, LTTNG_NAME_MAX
, "%s",
190 rotate_handle
->session_name
);
192 memset(&lsm
, 0, sizeof(lsm
));
193 lsm
.cmd_type
= LTTNG_ROTATE_PENDING
;
194 lsm
.u
.rotate_pending
.rotate_id
= rotate_handle
->rotate_id
;
195 lttng_ctl_copy_string(lsm
.session
.name
, attr
.session_name
,
196 sizeof(lsm
.session
.name
));
198 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending_return
);
200 rotate_handle
->status
= LTTNG_ROTATE_ERROR
;
204 rotate_handle
->status
= pending_return
->status
;
205 switch(pending_return
->status
) {
206 /* Not pending anymore */
207 case LTTNG_ROTATE_COMPLETED
:
208 snprintf(rotate_handle
->output_path
, PATH_MAX
, "%s",
209 pending_return
->output_path
);
210 case LTTNG_ROTATE_EXPIRED
:
211 case LTTNG_ROTATE_EMPTY
:
215 case LTTNG_ROTATE_STARTED
:
225 free(pending_return
);
230 * Configure the automatic rotate parameters.
232 * Return 0 on success else a negative LTTng error code.
234 int lttng_rotate_setup(struct lttng_rotate_session_attr
*attr
)
236 struct lttcomm_session_msg lsm
;
240 ret
= -LTTNG_ERR_INVALID
;
244 memset(&lsm
, 0, sizeof(lsm
));
245 lsm
.cmd_type
= LTTNG_ROTATE_SETUP
;
246 lttng_ctl_copy_string(lsm
.session
.name
, attr
->session_name
,
247 sizeof(lsm
.session
.name
));
248 lsm
.u
.rotate_setup
.timer_us
= attr
->timer_us
;
249 lsm
.u
.rotate_setup
.size
= attr
->size
;
251 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
252 fprintf(stderr
, "SETUP RET: %d\n", ret
);