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 enum lttng_rotate_status
lttng_rotate_session_get_status(
67 struct lttng_rotate_session_handle
*rotate_handle
)
70 return LTTNG_ROTATE_ERROR
;
72 return rotate_handle
->status
;
75 int lttng_rotate_session_get_output_path(
76 struct lttng_rotate_session_handle
*rotate_handle
,
81 *path
= zmalloc(PATH_MAX
);
87 if (rotate_handle
->status
== LTTNG_ROTATE_COMPLETED
) {
88 snprintf(*path
, PATH_MAX
, "%s", rotate_handle
->output_path
);
98 void lttng_rotate_session_handle_destroy(
99 struct lttng_rotate_session_handle
*rotate_handle
)
101 if (!rotate_handle
) {
105 rotate_handle
= NULL
;
109 void init_rotate_handle(struct lttng_rotate_session_handle
*rotate_handle
,
110 struct lttng_rotate_session_return
*rotate_return
,
111 struct lttng_rotate_session_attr
*attr
)
113 snprintf(rotate_handle
->session_name
, LTTNG_NAME_MAX
, "%s",
115 rotate_handle
->rotate_id
= rotate_return
->rotate_id
;
116 rotate_handle
->status
= rotate_return
->status
;
120 * Rotate the output folder of the session.
122 * Return 0 on success else a negative LTTng error code.
124 int lttng_rotate_session(struct lttng_rotate_session_attr
*attr
,
125 struct lttng_rotate_session_handle
**rotate_handle
)
127 struct lttcomm_session_msg lsm
;
128 struct lttng_rotate_session_return
*rotate_return
= NULL
;
132 ret
= -LTTNG_ERR_INVALID
;
136 memset(&lsm
, 0, sizeof(lsm
));
137 lsm
.cmd_type
= LTTNG_ROTATE_SESSION
;
138 lttng_ctl_copy_string(lsm
.session
.name
, attr
->session_name
,
139 sizeof(lsm
.session
.name
));
141 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &rotate_return
);
142 fprintf(stderr
, "RET: %d\n", ret
);
144 *rotate_handle
= NULL
;
148 *rotate_handle
= zmalloc(sizeof(struct lttng_rotate_session_handle
));
149 if (!*rotate_handle
) {
150 ret
= -LTTNG_ERR_NOMEM
;
154 init_rotate_handle(*rotate_handle
, rotate_return
, attr
);
162 * Ask the session daemon if the current rotation is complete.
163 * If it is, return 0 and populate the output_path with the path of the
164 * rotated chunk. Return 1 if the rotation is pending.
166 int lttng_rotate_session_pending(
167 struct lttng_rotate_session_handle
*rotate_handle
)
169 /* lsm.rotate_pending.rotate_id */
170 struct lttcomm_session_msg lsm
;
171 struct lttng_rotate_session_attr attr
;
173 struct lttng_rotate_pending_return
*pending_return
= NULL
;
175 snprintf(attr
.session_name
, LTTNG_NAME_MAX
, "%s",
176 rotate_handle
->session_name
);
178 memset(&lsm
, 0, sizeof(lsm
));
179 lsm
.cmd_type
= LTTNG_ROTATE_PENDING
;
180 lsm
.u
.rotate_pending
.rotate_id
= rotate_handle
->rotate_id
;
181 lttng_ctl_copy_string(lsm
.session
.name
, attr
.session_name
,
182 sizeof(lsm
.session
.name
));
184 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending_return
);
186 rotate_handle
->status
= LTTNG_ROTATE_ERROR
;
190 rotate_handle
->status
= pending_return
->status
;
191 if (pending_return
->status
== LTTNG_ROTATE_COMPLETED
) {
192 snprintf(rotate_handle
->output_path
, PATH_MAX
, "%s",
193 pending_return
->output_path
);
195 } else if (pending_return
->status
== LTTNG_ROTATE_STARTED
) {
202 free(pending_return
);