7682fe4581078897ea516836c978c4f8cb8f1b9e
[deliverable/lttng-tools.git] / src / lib / lttng-ctl / rotate.c
1 /*
2 * Copyright (C) 2017 - Julien Desfossez <jdesfossez@efficios.com>
3 *
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.
7 *
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
11 * for more details.
12 *
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
16 */
17
18 #define _LGPL_SOURCE
19 #include <assert.h>
20 #include <string.h>
21
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>
26
27 #include "lttng-ctl-helper.h"
28
29 struct lttng_rotate_session_attr *lttng_rotate_session_attr_create(void)
30 {
31 return zmalloc(sizeof(struct lttng_rotate_session_attr));
32 }
33
34 void lttng_rotate_session_attr_destroy(struct lttng_rotate_session_attr *attr)
35 {
36 if (attr) {
37 free(attr);
38 attr = NULL;
39 }
40 }
41
42 int lttng_rotate_session_attr_set_session_name(
43 struct lttng_rotate_session_attr *attr,
44 const char *session_name)
45 {
46 int ret = 0;
47 size_t len;
48
49 if (!attr || !session_name) {
50 ret = -LTTNG_ERR_INVALID;
51 goto error;
52 }
53
54 len = strlen(session_name);
55 if (len >= LTTNG_NAME_MAX) {
56 ret = -LTTNG_ERR_INVALID;
57 goto error;
58 }
59
60 strncpy(attr->session_name, session_name, len);
61
62 error:
63 return ret;
64 }
65
66 void lttng_rotate_session_attr_set_timer(
67 struct lttng_rotate_session_attr *attr,
68 uint64_t timer)
69 {
70 attr->timer_us = timer;
71 }
72
73 void lttng_rotate_session_attr_set_size(
74 struct lttng_rotate_session_attr *attr,
75 uint64_t size)
76 {
77 attr->size = size;
78 }
79
80 enum lttng_rotate_status lttng_rotate_session_get_status(
81 struct lttng_rotate_session_handle *rotate_handle)
82 {
83 if (!rotate_handle) {
84 return LTTNG_ROTATE_ERROR;
85 }
86 return rotate_handle->status;
87 }
88
89 int lttng_rotate_session_get_output_path(
90 struct lttng_rotate_session_handle *rotate_handle,
91 char **path)
92 {
93 int ret;
94
95 *path = zmalloc(PATH_MAX);
96 if (!*path) {
97 ret = -1;
98 goto end;
99 }
100
101 if (rotate_handle->status == LTTNG_ROTATE_COMPLETED) {
102 snprintf(*path, PATH_MAX, "%s", rotate_handle->output_path);
103 ret = 0;
104 } else {
105 ret = -1;
106 }
107
108 end:
109 return ret;
110 }
111
112 void lttng_rotate_session_handle_destroy(
113 struct lttng_rotate_session_handle *rotate_handle)
114 {
115 if (!rotate_handle) {
116 return;
117 }
118 free(rotate_handle);
119 rotate_handle = NULL;
120 }
121
122 static
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)
126 {
127 snprintf(rotate_handle->session_name, LTTNG_NAME_MAX, "%s",
128 attr->session_name);
129 rotate_handle->rotate_id = rotate_return->rotate_id;
130 rotate_handle->status = rotate_return->status;
131 }
132
133 /*
134 * Rotate the output folder of the session.
135 *
136 * Return 0 on success else a negative LTTng error code.
137 */
138 int lttng_rotate_session(struct lttng_rotate_session_attr *attr,
139 struct lttng_rotate_session_handle **rotate_handle)
140 {
141 struct lttcomm_session_msg lsm;
142 struct lttng_rotate_session_return *rotate_return = NULL;
143 int ret;
144
145 if (!attr) {
146 ret = -LTTNG_ERR_INVALID;
147 goto end;
148 }
149
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));
154
155 ret = lttng_ctl_ask_sessiond(&lsm, (void **) &rotate_return);
156 fprintf(stderr, "RET: %d\n", ret);
157 if (ret < 0) {
158 *rotate_handle = NULL;
159 goto end;
160 }
161
162 *rotate_handle = zmalloc(sizeof(struct lttng_rotate_session_handle));
163 if (!*rotate_handle) {
164 ret = -LTTNG_ERR_NOMEM;
165 goto end;
166 }
167
168 init_rotate_handle(*rotate_handle, rotate_return, attr);
169
170 end:
171 free(rotate_return);
172 return ret;
173 }
174
175 /*
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.
179 */
180 int lttng_rotate_session_pending(
181 struct lttng_rotate_session_handle *rotate_handle)
182 {
183 /* lsm.rotate_pending.rotate_id */
184 struct lttcomm_session_msg lsm;
185 struct lttng_rotate_session_attr attr;
186 int ret;
187 struct lttng_rotate_pending_return *pending_return = NULL;
188
189 snprintf(attr.session_name, LTTNG_NAME_MAX, "%s",
190 rotate_handle->session_name);
191
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));
197
198 ret = lttng_ctl_ask_sessiond(&lsm, (void **) &pending_return);
199 if (ret < 0) {
200 rotate_handle->status = LTTNG_ROTATE_ERROR;
201 goto end;
202 }
203
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:
212 ret = 0;
213 break;
214 /* Still pending */
215 case LTTNG_ROTATE_STARTED:
216 ret = 1;
217 break;
218 /* Error */
219 default:
220 ret = -1;
221 break;
222 }
223
224 end:
225 free(pending_return);
226 return ret;
227 }
228
229 /*
230 * Configure the automatic rotate parameters.
231 *
232 * Return 0 on success else a negative LTTng error code.
233 */
234 int lttng_rotate_setup(struct lttng_rotate_session_attr *attr)
235 {
236 struct lttcomm_session_msg lsm;
237 int ret;
238
239 if (!attr) {
240 ret = -LTTNG_ERR_INVALID;
241 goto end;
242 }
243
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;
250
251 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
252 fprintf(stderr, "SETUP RET: %d\n", ret);
253
254 end:
255 return ret;
256 }
257
This page took 0.035797 seconds and 4 git commands to generate.