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