Commit | Line | Data |
---|---|---|
6e0be6cc 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/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 | ||
8cf2762e JD |
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 | ||
6e0be6cc JD |
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); | |
6e0be6cc JD |
156 | if (ret < 0) { |
157 | *rotate_handle = NULL; | |
158 | goto end; | |
159 | } | |
160 | ||
161 | *rotate_handle = zmalloc(sizeof(struct lttng_rotate_session_handle)); | |
162 | if (!*rotate_handle) { | |
163 | ret = -LTTNG_ERR_NOMEM; | |
164 | goto end; | |
165 | } | |
166 | ||
167 | init_rotate_handle(*rotate_handle, rotate_return, attr); | |
168 | ||
169 | end: | |
170 | free(rotate_return); | |
171 | return ret; | |
172 | } | |
173 | ||
174 | /* | |
175 | * Ask the session daemon if the current rotation is complete. | |
176 | * If it is, return 0 and populate the output_path with the path of the | |
177 | * rotated chunk. Return 1 if the rotation is pending. | |
178 | */ | |
179 | int lttng_rotate_session_pending( | |
180 | struct lttng_rotate_session_handle *rotate_handle) | |
181 | { | |
182 | /* lsm.rotate_pending.rotate_id */ | |
183 | struct lttcomm_session_msg lsm; | |
184 | struct lttng_rotate_session_attr attr; | |
185 | int ret; | |
186 | struct lttng_rotate_pending_return *pending_return = NULL; | |
187 | ||
188 | snprintf(attr.session_name, LTTNG_NAME_MAX, "%s", | |
189 | rotate_handle->session_name); | |
190 | ||
191 | memset(&lsm, 0, sizeof(lsm)); | |
192 | lsm.cmd_type = LTTNG_ROTATE_PENDING; | |
193 | lsm.u.rotate_pending.rotate_id = rotate_handle->rotate_id; | |
194 | lttng_ctl_copy_string(lsm.session.name, attr.session_name, | |
195 | sizeof(lsm.session.name)); | |
196 | ||
197 | ret = lttng_ctl_ask_sessiond(&lsm, (void **) &pending_return); | |
198 | if (ret < 0) { | |
199 | rotate_handle->status = LTTNG_ROTATE_ERROR; | |
200 | goto end; | |
201 | } | |
202 | ||
203 | rotate_handle->status = pending_return->status; | |
4a478f45 JD |
204 | switch(pending_return->status) { |
205 | /* Not pending anymore */ | |
206 | case LTTNG_ROTATE_COMPLETED: | |
6e0be6cc JD |
207 | snprintf(rotate_handle->output_path, PATH_MAX, "%s", |
208 | pending_return->output_path); | |
4a478f45 JD |
209 | case LTTNG_ROTATE_EXPIRED: |
210 | case LTTNG_ROTATE_EMPTY: | |
6e0be6cc | 211 | ret = 0; |
4a478f45 JD |
212 | break; |
213 | /* Still pending */ | |
214 | case LTTNG_ROTATE_STARTED: | |
6e0be6cc | 215 | ret = 1; |
4a478f45 JD |
216 | break; |
217 | /* Error */ | |
218 | default: | |
6e0be6cc | 219 | ret = -1; |
4a478f45 | 220 | break; |
6e0be6cc JD |
221 | } |
222 | ||
223 | end: | |
224 | free(pending_return); | |
225 | return ret; | |
226 | } | |
8cf2762e JD |
227 | |
228 | /* | |
229 | * Configure the automatic rotate parameters. | |
230 | * | |
231 | * Return 0 on success else a negative LTTng error code. | |
232 | */ | |
233 | int lttng_rotate_setup(struct lttng_rotate_session_attr *attr) | |
234 | { | |
235 | struct lttcomm_session_msg lsm; | |
236 | int ret; | |
237 | ||
238 | if (!attr) { | |
239 | ret = -LTTNG_ERR_INVALID; | |
240 | goto end; | |
241 | } | |
242 | ||
243 | memset(&lsm, 0, sizeof(lsm)); | |
244 | lsm.cmd_type = LTTNG_ROTATE_SETUP; | |
245 | lttng_ctl_copy_string(lsm.session.name, attr->session_name, | |
246 | sizeof(lsm.session.name)); | |
247 | lsm.u.rotate_setup.timer_us = attr->timer_us; | |
248 | lsm.u.rotate_setup.size = attr->size; | |
249 | ||
250 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); | |
8cf2762e JD |
251 | |
252 | end: | |
253 | return ret; | |
254 | } | |
255 |