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