Commit | Line | Data |
---|---|---|
d68c9a04 JD |
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/rotation.h> | |
dd73d57b | 24 | #include <lttng/location-internal.h> |
d68c9a04 JD |
25 | #include <lttng/rotate-internal.h> |
26 | #include <common/sessiond-comm/sessiond-comm.h> | |
27 | #include <common/macros.h> | |
28 | ||
29 | #include "lttng-ctl-helper.h" | |
30 | ||
259c2674 JD |
31 | struct lttng_rotation_schedule_attr *lttng_rotation_schedule_attr_create(void) |
32 | { | |
33 | return zmalloc(sizeof(struct lttng_rotation_schedule_attr)); | |
34 | } | |
35 | ||
259c2674 JD |
36 | void lttng_rotation_schedule_attr_destroy(struct lttng_rotation_schedule_attr *attr) |
37 | { | |
38 | if (attr) { | |
39 | free(attr); | |
40 | attr = NULL; | |
41 | } | |
42 | } | |
43 | ||
d68c9a04 JD |
44 | static |
45 | enum lttng_rotation_status ask_rotation_info( | |
46 | struct lttng_rotation_handle *rotation_handle, | |
47 | struct lttng_rotation_get_info_return **info) | |
48 | { | |
49 | /* lsm.get_rotation_state.rotation_id */ | |
50 | struct lttcomm_session_msg lsm; | |
51 | enum lttng_rotation_status status = LTTNG_ROTATION_STATUS_OK; | |
52 | int ret; | |
53 | ||
54 | if (!rotation_handle || !info) { | |
55 | status = LTTNG_ROTATION_STATUS_INVALID; | |
56 | goto end; | |
57 | } | |
58 | ||
59 | memset(&lsm, 0, sizeof(lsm)); | |
60 | lsm.cmd_type = LTTNG_ROTATION_GET_INFO; | |
61 | lsm.u.get_rotation_info.rotation_id = rotation_handle->rotation_id; | |
62 | ||
63 | ret = lttng_strncpy(lsm.session.name, rotation_handle->session_name, | |
64 | sizeof(lsm.session.name)); | |
65 | if (ret) { | |
66 | status = LTTNG_ROTATION_STATUS_INVALID; | |
67 | goto end; | |
68 | } | |
69 | ||
70 | ret = lttng_ctl_ask_sessiond(&lsm, (void **) info); | |
71 | if (ret < 0) { | |
72 | status = LTTNG_ROTATION_STATUS_ERROR; | |
73 | goto end; | |
74 | } | |
75 | end: | |
76 | return status; | |
77 | ||
78 | } | |
79 | ||
259c2674 JD |
80 | enum lttng_rotation_status lttng_rotation_schedule_attr_set_timer_period( |
81 | struct lttng_rotation_schedule_attr *attr, | |
82 | uint64_t timer) | |
83 | { | |
84 | enum lttng_rotation_status status = LTTNG_ROTATION_STATUS_OK; | |
85 | ||
86 | if (!attr) { | |
87 | status = LTTNG_ROTATION_STATUS_INVALID; | |
88 | goto end; | |
89 | } | |
90 | ||
91 | attr->timer_us = timer; | |
92 | end: | |
93 | return status; | |
94 | } | |
95 | ||
90936dcf JD |
96 | void lttng_rotation_schedule_attr_set_size( |
97 | struct lttng_rotation_schedule_attr *attr, uint64_t size) | |
98 | { | |
99 | attr->size = size; | |
100 | } | |
101 | ||
dd73d57b JG |
102 | static |
103 | struct lttng_trace_archive_location * | |
104 | create_trace_archive_location_from_get_info( | |
105 | const struct lttng_rotation_get_info_return *info) | |
106 | { | |
107 | struct lttng_trace_archive_location *location; | |
108 | ||
109 | switch (info->location_type) { | |
110 | case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL: | |
111 | location = lttng_trace_archive_location_local_create( | |
112 | info->location.local.absolute_path); | |
113 | break; | |
114 | case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY: | |
115 | location = lttng_trace_archive_location_relay_create( | |
116 | info->location.relay.host, | |
117 | info->location.relay.protocol, | |
118 | info->location.relay.ports.control, | |
119 | info->location.relay.ports.data, | |
120 | info->location.relay.relative_path); | |
121 | break; | |
122 | default: | |
123 | location = NULL; | |
124 | break; | |
125 | } | |
126 | return location; | |
127 | } | |
128 | ||
d68c9a04 JD |
129 | enum lttng_rotation_status lttng_rotation_handle_get_state( |
130 | struct lttng_rotation_handle *rotation_handle, | |
131 | enum lttng_rotation_state *state) | |
132 | { | |
133 | enum lttng_rotation_status status = LTTNG_ROTATION_STATUS_OK; | |
134 | struct lttng_rotation_get_info_return *info = NULL; | |
d68c9a04 JD |
135 | |
136 | if (!rotation_handle || !state) { | |
137 | status = LTTNG_ROTATION_STATUS_INVALID; | |
138 | goto end; | |
139 | } | |
140 | ||
141 | status = ask_rotation_info(rotation_handle, &info); | |
142 | if (status != LTTNG_ROTATION_STATUS_OK) { | |
143 | goto end; | |
144 | } | |
145 | ||
146 | *state = (enum lttng_rotation_state) info->status; | |
dd73d57b | 147 | if (rotation_handle->archive_location || |
d68c9a04 JD |
148 | *state != LTTNG_ROTATION_STATE_COMPLETED) { |
149 | /* | |
150 | * The path is only provided by the sessiond once | |
151 | * the session rotation is completed, but not expired. | |
152 | */ | |
153 | goto end; | |
154 | } | |
155 | ||
156 | /* | |
157 | * Cache the location since the rotation may expire before the user | |
158 | * has a chance to query it. | |
159 | */ | |
dd73d57b JG |
160 | rotation_handle->archive_location = |
161 | create_trace_archive_location_from_get_info(info); | |
162 | if (!rotation_handle->archive_location) { | |
d68c9a04 JD |
163 | status = LTTNG_ROTATION_STATUS_ERROR; |
164 | goto end; | |
165 | } | |
d68c9a04 JD |
166 | end: |
167 | free(info); | |
168 | return status; | |
169 | } | |
170 | ||
dd73d57b | 171 | enum lttng_rotation_status lttng_rotation_handle_get_archive_location( |
d68c9a04 | 172 | struct lttng_rotation_handle *rotation_handle, |
dd73d57b | 173 | const struct lttng_trace_archive_location **location) |
d68c9a04 | 174 | { |
d68c9a04 JD |
175 | enum lttng_rotation_status status = LTTNG_ROTATION_STATUS_OK; |
176 | struct lttng_rotation_get_info_return *info = NULL; | |
177 | ||
dd73d57b | 178 | if (!rotation_handle || !location) { |
d68c9a04 JD |
179 | status = LTTNG_ROTATION_STATUS_INVALID; |
180 | goto end; | |
181 | } | |
182 | ||
183 | /* Use the cached location we got from a previous query. */ | |
dd73d57b JG |
184 | if (rotation_handle->archive_location) { |
185 | *location = rotation_handle->archive_location; | |
d68c9a04 JD |
186 | goto end; |
187 | } | |
188 | ||
189 | status = ask_rotation_info(rotation_handle, &info); | |
190 | if (status != LTTNG_ROTATION_STATUS_OK) { | |
191 | goto end; | |
192 | } | |
193 | ||
194 | if ((enum lttng_rotation_state) info->status != | |
195 | LTTNG_ROTATION_STATE_COMPLETED) { | |
196 | status = LTTNG_ROTATION_STATUS_UNAVAILABLE; | |
197 | goto end; | |
198 | } | |
199 | ||
dd73d57b JG |
200 | rotation_handle->archive_location = |
201 | create_trace_archive_location_from_get_info(info); | |
202 | if (!rotation_handle->archive_location) { | |
d68c9a04 JD |
203 | status = LTTNG_ROTATION_STATUS_ERROR; |
204 | goto end; | |
205 | } | |
d68c9a04 JD |
206 | end: |
207 | free(info); | |
208 | return status; | |
209 | } | |
210 | ||
211 | void lttng_rotation_handle_destroy( | |
212 | struct lttng_rotation_handle *rotation_handle) | |
213 | { | |
06b180a1 JR |
214 | if (!rotation_handle) { |
215 | return; | |
216 | } | |
dd73d57b | 217 | lttng_trace_archive_location_destroy(rotation_handle->archive_location); |
d68c9a04 JD |
218 | free(rotation_handle); |
219 | } | |
220 | ||
221 | static | |
222 | int init_rotation_handle(struct lttng_rotation_handle *rotation_handle, | |
dbd512ea | 223 | const char *session_name, |
d68c9a04 JD |
224 | struct lttng_rotate_session_return *rotate_return, |
225 | struct lttng_rotation_immediate_attr *attr) | |
226 | { | |
227 | int ret; | |
228 | ||
dbd512ea | 229 | ret = lttng_strncpy(rotation_handle->session_name, session_name, |
d68c9a04 JD |
230 | sizeof(rotation_handle->session_name)); |
231 | if (ret) { | |
232 | goto end; | |
233 | } | |
234 | ||
235 | rotation_handle->rotation_id = rotate_return->rotation_id; | |
236 | end: | |
237 | return ret; | |
238 | } | |
239 | ||
240 | /* | |
241 | * Rotate the output folder of the session. | |
242 | * | |
243 | * Return 0 on success else a negative LTTng error code. | |
244 | */ | |
dbd512ea JG |
245 | int lttng_rotate_session(const char *session_name, |
246 | struct lttng_rotation_immediate_attr *attr, | |
d68c9a04 JD |
247 | struct lttng_rotation_handle **rotation_handle) |
248 | { | |
249 | struct lttcomm_session_msg lsm; | |
250 | struct lttng_rotate_session_return *rotate_return = NULL; | |
251 | int ret; | |
dbd512ea | 252 | size_t session_name_len; |
d68c9a04 | 253 | |
dbd512ea JG |
254 | if (!session_name) { |
255 | ret = -LTTNG_ERR_INVALID; | |
256 | goto end; | |
257 | } | |
258 | ||
259 | session_name_len = strlen(session_name); | |
260 | if (session_name_len >= sizeof(lsm.session.name) || | |
261 | session_name_len >= member_sizeof(struct lttng_rotation_handle, session_name)) { | |
d68c9a04 JD |
262 | ret = -LTTNG_ERR_INVALID; |
263 | goto end; | |
264 | } | |
265 | ||
266 | memset(&lsm, 0, sizeof(lsm)); | |
267 | lsm.cmd_type = LTTNG_ROTATE_SESSION; | |
dbd512ea | 268 | lttng_ctl_copy_string(lsm.session.name, session_name, |
d68c9a04 JD |
269 | sizeof(lsm.session.name)); |
270 | ||
271 | ret = lttng_ctl_ask_sessiond(&lsm, (void **) &rotate_return); | |
272 | if (ret < 0) { | |
273 | *rotation_handle = NULL; | |
274 | goto end; | |
275 | } | |
276 | ||
277 | *rotation_handle = zmalloc(sizeof(struct lttng_rotation_handle)); | |
278 | if (!*rotation_handle) { | |
279 | ret = -LTTNG_ERR_NOMEM; | |
280 | goto end; | |
281 | } | |
282 | ||
dbd512ea JG |
283 | init_rotation_handle(*rotation_handle, session_name, rotate_return, |
284 | attr); | |
d68c9a04 JD |
285 | |
286 | ret = 0; | |
287 | ||
288 | end: | |
289 | free(rotate_return); | |
290 | return ret; | |
291 | } | |
259c2674 JD |
292 | |
293 | /* | |
294 | * Configure the automatic rotate parameters. | |
295 | */ | |
dbd512ea | 296 | int lttng_rotation_set_schedule(const char *session_name, |
259c2674 JD |
297 | struct lttng_rotation_schedule_attr *attr) |
298 | { | |
299 | struct lttcomm_session_msg lsm; | |
300 | int ret; | |
301 | ||
dbd512ea JG |
302 | if (!attr || !session_name) { |
303 | ret = -LTTNG_ERR_INVALID; | |
304 | goto end; | |
305 | } | |
306 | ||
307 | if (strlen(session_name) >= sizeof(lsm.session.name)) { | |
259c2674 JD |
308 | ret = -LTTNG_ERR_INVALID; |
309 | goto end; | |
310 | } | |
311 | ||
312 | memset(&lsm, 0, sizeof(lsm)); | |
313 | lsm.cmd_type = LTTNG_ROTATION_SET_SCHEDULE; | |
dbd512ea | 314 | lttng_ctl_copy_string(lsm.session.name, session_name, |
259c2674 JD |
315 | sizeof(lsm.session.name)); |
316 | lsm.u.rotate_setup.timer_us = attr->timer_us; | |
90936dcf | 317 | lsm.u.rotate_setup.size = attr->size; |
259c2674 JD |
318 | |
319 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); | |
259c2674 JD |
320 | end: |
321 | return ret; | |
322 | } | |
329f3443 JD |
323 | |
324 | int lttng_rotation_schedule_get_timer_period(const char *session_name, | |
325 | uint64_t *rotate_timer) | |
326 | { | |
327 | struct lttcomm_session_msg lsm; | |
328 | struct lttng_rotation_schedule_get_timer_period *get_timer = NULL; | |
329 | int ret; | |
330 | ||
331 | memset(&lsm, 0, sizeof(lsm)); | |
332 | lsm.cmd_type = LTTNG_ROTATION_SCHEDULE_GET_TIMER_PERIOD; | |
333 | lttng_ctl_copy_string(lsm.session.name, session_name, | |
334 | sizeof(lsm.session.name)); | |
335 | ||
336 | ret = lttng_ctl_ask_sessiond(&lsm, (void **) &get_timer); | |
337 | if (ret < 0) { | |
338 | ret = -1; | |
339 | goto end; | |
340 | } | |
341 | ||
342 | *rotate_timer = get_timer->rotate_timer; | |
329f3443 | 343 | ret = 0; |
329f3443 JD |
344 | end: |
345 | free(get_timer); | |
346 | return ret; | |
347 | } | |
348 | ||
349 | int lttng_rotation_schedule_get_size(const char *session_name, | |
350 | uint64_t *rotate_size) | |
351 | { | |
352 | struct lttcomm_session_msg lsm; | |
353 | struct lttng_rotation_schedule_get_size *get_size = NULL; | |
354 | int ret; | |
355 | ||
356 | memset(&lsm, 0, sizeof(lsm)); | |
357 | lsm.cmd_type = LTTNG_ROTATION_SCHEDULE_GET_SIZE; | |
358 | lttng_ctl_copy_string(lsm.session.name, session_name, | |
359 | sizeof(lsm.session.name)); | |
360 | ||
361 | ret = lttng_ctl_ask_sessiond(&lsm, (void **) &get_size); | |
362 | if (ret < 0) { | |
363 | ret = -1; | |
364 | goto end; | |
365 | } | |
366 | ||
367 | *rotate_size = get_size->rotate_size; | |
368 | ||
369 | ret = 0; | |
370 | ||
371 | end: | |
372 | free(get_size); | |
373 | return ret; | |
374 | } |