Commit | Line | Data |
---|---|---|
259c2674 JD |
1 | /* |
2 | * Copyright (C) 2017 - Julien Desfossez <jdesfossez@efficios.com> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License, version 2 only, | |
6 | * as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along | |
14 | * with this program; if not, write to the Free Software Foundation, Inc., | |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
16 | */ | |
17 | ||
18 | #define _LGPL_SOURCE | |
19 | #include <popt.h> | |
20 | #include <stdio.h> | |
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
23 | #include <sys/stat.h> | |
24 | #include <sys/types.h> | |
25 | #include <unistd.h> | |
26 | #include <inttypes.h> | |
27 | #include <ctype.h> | |
28 | ||
29 | #include <common/sessiond-comm/sessiond-comm.h> | |
30 | #include <common/mi-lttng.h> | |
31 | #include <common/utils.h> | |
32 | ||
33 | #include "../command.h" | |
34 | #include <lttng/rotation.h> | |
35 | ||
36 | static char *opt_session_name; | |
37 | static struct mi_writer *writer; | |
38 | ||
3ae5c539 JD |
39 | #ifdef LTTNG_EMBED_HELP |
40 | static const char help_msg[] = | |
41 | #include <lttng-enable-rotation.1.h> | |
42 | ; | |
43 | #endif | |
44 | ||
259c2674 JD |
45 | enum { |
46 | OPT_HELP = 1, | |
47 | OPT_LIST_OPTIONS, | |
48 | OPT_TIMER, | |
90936dcf | 49 | OPT_SIZE, |
259c2674 JD |
50 | }; |
51 | ||
52 | static struct poptOption long_options[] = { | |
53 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
54 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, | |
55 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, | |
56 | {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0}, | |
57 | {"timer", 0, POPT_ARG_INT, 0, OPT_TIMER, 0, 0}, | |
90936dcf | 58 | {"size", 0, POPT_ARG_INT, 0, OPT_SIZE, 0, 0}, |
259c2674 JD |
59 | {0, 0, 0, 0, 0, 0, 0} |
60 | }; | |
61 | ||
66ea93b1 JG |
62 | static const char *schedule_type_str[] = { |
63 | [LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC] = "periodic", | |
64 | [LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD] = "size-based", | |
65 | }; | |
259c2674 | 66 | |
66ea93b1 JG |
67 | static enum cmd_error_code add_schedule(const char *session_name, |
68 | enum lttng_rotation_schedule_type schedule_type, uint64_t value) | |
69 | { | |
70 | enum cmd_error_code ret = CMD_SUCCESS; | |
71 | struct lttng_rotation_schedule *schedule = NULL; | |
72 | enum lttng_rotation_status status; | |
73 | const char *schedule_type_name; | |
259c2674 | 74 | |
66ea93b1 JG |
75 | switch (schedule_type) { |
76 | case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC: | |
77 | schedule = lttng_rotation_schedule_periodic_create(); | |
78 | if (!schedule) { | |
79 | ret = CMD_ERROR; | |
80 | goto end; | |
259c2674 | 81 | } |
66ea93b1 JG |
82 | status = lttng_rotation_schedule_periodic_set_period(schedule, |
83 | value); | |
84 | break; | |
85 | case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD: | |
86 | schedule = lttng_rotation_schedule_size_threshold_create(); | |
87 | if (!schedule) { | |
88 | ret = CMD_ERROR; | |
89 | goto end; | |
259c2674 | 90 | } |
66ea93b1 JG |
91 | status = lttng_rotation_schedule_size_threshold_set_threshold( |
92 | schedule, value); | |
93 | break; | |
94 | default: | |
95 | ERR("Unknown schedule type"); | |
96 | abort(); | |
259c2674 JD |
97 | } |
98 | ||
66ea93b1 JG |
99 | schedule_type_name = schedule_type_str[schedule_type]; |
100 | ||
101 | switch (status) { | |
102 | case LTTNG_ROTATION_STATUS_OK: | |
103 | break; | |
104 | case LTTNG_ROTATION_STATUS_INVALID: | |
105 | ERR("Invalid value for %s option", schedule_type_name); | |
106 | ret = CMD_ERROR; | |
107 | goto end; | |
108 | default: | |
e6a80fab | 109 | ERR("Unknown error occurred setting %s rotation schedule", |
66ea93b1 JG |
110 | schedule_type_name); |
111 | ret = CMD_ERROR; | |
112 | goto end; | |
90936dcf | 113 | } |
259c2674 | 114 | |
66ea93b1 JG |
115 | status = lttng_session_add_rotation_schedule(session_name, schedule); |
116 | switch (status) { | |
117 | case LTTNG_ROTATION_STATUS_OK: | |
118 | ret = CMD_SUCCESS; | |
119 | switch (schedule_type) { | |
120 | case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC: | |
121 | MSG("Enabled %s rotations every %" PRIu64 " µs on session %s", | |
122 | schedule_type_name, value, session_name); | |
123 | break; | |
124 | case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD: | |
125 | MSG("Enabled %s rotations every %" PRIu64 " bytes written on session %s", | |
126 | schedule_type_name, value, session_name); | |
127 | break; | |
128 | default: | |
129 | abort(); | |
259c2674 | 130 | } |
66ea93b1 JG |
131 | break; |
132 | case LTTNG_ROTATION_STATUS_INVALID: | |
133 | ERR("Invalid parameter for %s rotation schedule", | |
134 | schedule_type_name); | |
135 | ret = CMD_ERROR; | |
136 | break; | |
137 | case LTTNG_ROTATION_STATUS_SCHEDULE_ALREADY_SET: | |
138 | ERR("A %s rotation schedule is already set on session %s", | |
139 | schedule_type_name, | |
140 | session_name); | |
141 | ret = CMD_ERROR; | |
142 | break; | |
143 | case LTTNG_ROTATION_STATUS_ERROR: | |
144 | default: | |
145 | ERR("Failed to enable %s rotation schedule on session %s", | |
146 | schedule_type_name, | |
147 | session_name); | |
148 | ret = CMD_ERROR; | |
149 | break; | |
259c2674 JD |
150 | } |
151 | ||
152 | if (lttng_opt_mi) { | |
66ea93b1 | 153 | int mi_ret; |
259c2674 | 154 | |
66ea93b1 JG |
155 | mi_ret = mi_lttng_rotation_schedule_result(writer, |
156 | schedule, ret == CMD_SUCCESS); | |
157 | if (mi_ret < 0) { | |
158 | ret = CMD_ERROR; | |
259c2674 JD |
159 | goto end; |
160 | } | |
161 | } | |
162 | ||
259c2674 | 163 | end: |
66ea93b1 | 164 | lttng_rotation_schedule_destroy(schedule); |
259c2674 JD |
165 | return ret; |
166 | } | |
167 | ||
168 | /* | |
169 | * cmd_enable_rotation | |
170 | * | |
171 | * The 'enable-rotation <options>' first level command | |
172 | */ | |
173 | int cmd_enable_rotation(int argc, const char **argv) | |
174 | { | |
66ea93b1 JG |
175 | int popt_ret, opt, ret = 0; |
176 | enum cmd_error_code cmd_ret = CMD_SUCCESS; | |
259c2674 JD |
177 | static poptContext pc; |
178 | char *session_name = NULL; | |
179 | char *opt_arg = NULL; | |
259c2674 | 180 | bool free_session_name = false; |
66ea93b1 JG |
181 | uint64_t timer_us = 0, size_bytes = 0; |
182 | bool periodic_rotation = false, size_rotation = false; | |
259c2674 JD |
183 | |
184 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
185 | popt_ret = poptReadDefaultConfig(pc, 0); | |
186 | if (popt_ret) { | |
259c2674 | 187 | ERR("poptReadDefaultConfig"); |
66ea93b1 | 188 | goto error; |
259c2674 JD |
189 | } |
190 | ||
191 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
192 | switch (opt) { | |
193 | case OPT_HELP: | |
194 | SHOW_HELP(); | |
195 | goto end; | |
196 | case OPT_LIST_OPTIONS: | |
197 | list_cmd_options(stdout, long_options); | |
198 | goto end; | |
199 | case OPT_TIMER: | |
200 | errno = 0; | |
201 | opt_arg = poptGetOptArg(pc); | |
202 | if (errno != 0 || !isdigit(opt_arg[0])) { | |
66ea93b1 JG |
203 | ERR("Invalid value for --timer option: %s", opt_arg); |
204 | goto error; | |
205 | } | |
206 | if (utils_parse_time_suffix(opt_arg, &timer_us) < 0) { | |
207 | ERR("Invalid value for --timer option: %s", opt_arg); | |
208 | goto error; | |
259c2674 | 209 | } |
66ea93b1 JG |
210 | if (periodic_rotation) { |
211 | ERR("Only one periodic rotation schedule may be set on a session."); | |
212 | goto error; | |
259c2674 | 213 | } |
66ea93b1 | 214 | periodic_rotation = true; |
259c2674 | 215 | break; |
90936dcf JD |
216 | case OPT_SIZE: |
217 | errno = 0; | |
218 | opt_arg = poptGetOptArg(pc); | |
66ea93b1 JG |
219 | if (utils_parse_size_suffix(opt_arg, &size_bytes) < 0) { |
220 | ERR("Invalid value for --size option: %s", opt_arg); | |
221 | goto error; | |
90936dcf | 222 | } |
66ea93b1 JG |
223 | if (size_rotation) { |
224 | ERR("Only one size-based rotation schedule may be set on a session."); | |
225 | goto error; | |
226 | } | |
227 | size_rotation = true; | |
90936dcf | 228 | break; |
259c2674 | 229 | default: |
66ea93b1 | 230 | cmd_ret = CMD_UNDEFINED; |
259c2674 JD |
231 | goto end; |
232 | } | |
233 | } | |
234 | ||
235 | if (opt_session_name == NULL) { | |
236 | session_name = get_session_name(); | |
237 | if (session_name == NULL) { | |
66ea93b1 | 238 | goto error; |
259c2674 JD |
239 | } |
240 | free_session_name = true; | |
241 | } else { | |
242 | session_name = opt_session_name; | |
243 | } | |
244 | ||
245 | /* Mi check */ | |
246 | if (lttng_opt_mi) { | |
247 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); | |
248 | if (!writer) { | |
66ea93b1 | 249 | goto error; |
259c2674 JD |
250 | } |
251 | ||
252 | /* Open command element */ | |
253 | ret = mi_lttng_writer_command_open(writer, | |
254 | mi_lttng_element_command_enable_rotation); | |
255 | if (ret) { | |
66ea93b1 | 256 | goto error; |
259c2674 JD |
257 | } |
258 | ||
259 | /* Open output element */ | |
260 | ret = mi_lttng_writer_open_element(writer, | |
261 | mi_lttng_element_command_output); | |
262 | if (ret) { | |
66ea93b1 | 263 | goto error; |
259c2674 JD |
264 | } |
265 | } | |
266 | ||
66ea93b1 JG |
267 | if (!periodic_rotation && !size_rotation) { |
268 | ERR("No session rotation schedule parameter provided."); | |
269 | cmd_ret = CMD_ERROR; | |
270 | goto close_command; | |
259c2674 JD |
271 | } |
272 | ||
66ea93b1 JG |
273 | if (lttng_opt_mi) { |
274 | ret = mi_lttng_writer_open_element(writer, | |
275 | mi_lttng_element_rotation_schedule_results); | |
276 | if (ret) { | |
277 | goto error; | |
278 | } | |
279 | ||
280 | ret = mi_lttng_writer_write_element_string(writer, | |
281 | mi_lttng_element_session_name, | |
282 | session_name); | |
283 | if (ret) { | |
284 | goto error; | |
285 | } | |
259c2674 JD |
286 | } |
287 | ||
66ea93b1 JG |
288 | if (periodic_rotation) { |
289 | /* | |
290 | * Continue processing even on error as multiple schedules can | |
291 | * be specified at once. | |
292 | */ | |
293 | cmd_ret = add_schedule(session_name, | |
294 | LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC, | |
295 | timer_us); | |
296 | } | |
297 | ||
298 | if (size_rotation) { | |
a14a81cb | 299 | enum cmd_error_code tmp_ret; |
66ea93b1 JG |
300 | |
301 | /* Don't overwrite cmd_ret if it already indicates an error. */ | |
302 | tmp_ret = add_schedule(session_name, | |
303 | LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD, | |
304 | size_bytes); | |
305 | cmd_ret = cmd_ret ? cmd_ret : tmp_ret; | |
306 | } | |
307 | ||
308 | if (lttng_opt_mi) { | |
309 | /* Close rotation schedule results element */ | |
310 | ret = mi_lttng_writer_close_element(writer); | |
311 | if (ret) { | |
312 | goto error; | |
313 | } | |
314 | } | |
315 | ||
316 | close_command: | |
259c2674 JD |
317 | /* Mi closing */ |
318 | if (lttng_opt_mi) { | |
66ea93b1 | 319 | /* Close output element */ |
259c2674 JD |
320 | ret = mi_lttng_writer_close_element(writer); |
321 | if (ret) { | |
66ea93b1 | 322 | goto error; |
259c2674 | 323 | } |
66ea93b1 | 324 | |
259c2674 JD |
325 | /* Success ? */ |
326 | ret = mi_lttng_writer_write_element_bool(writer, | |
66ea93b1 JG |
327 | mi_lttng_element_command_success, |
328 | cmd_ret == CMD_SUCCESS); | |
259c2674 | 329 | if (ret) { |
66ea93b1 | 330 | goto error; |
259c2674 JD |
331 | } |
332 | ||
333 | /* Command element close */ | |
334 | ret = mi_lttng_writer_command_close(writer); | |
335 | if (ret) { | |
66ea93b1 | 336 | goto error; |
259c2674 JD |
337 | } |
338 | } | |
339 | ||
340 | end: | |
66ea93b1 | 341 | (void) mi_lttng_writer_destroy(writer); |
259c2674 JD |
342 | poptFreeContext(pc); |
343 | if (free_session_name) { | |
344 | free(session_name); | |
345 | } | |
66ea93b1 JG |
346 | return cmd_ret; |
347 | ||
348 | error: | |
349 | cmd_ret = CMD_ERROR; | |
350 | goto end; | |
259c2674 | 351 | } |