Change "event rule hit" to "event-rule matches" in user interface strings
[lttng-tools.git] / src / bin / lttng / commands / enable_rotation.c
1 /*
2 * Copyright (C) 2017 Julien Desfossez <jdesfossez@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
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
19 #include <common/sessiond-comm/sessiond-comm.h>
20 #include <common/mi-lttng.h>
21 #include <common/utils.h>
22
23 #include "../command.h"
24 #include <lttng/lttng.h>
25
26 static char *opt_session_name;
27 static struct mi_writer *writer;
28
29 #ifdef LTTNG_EMBED_HELP
30 static const char help_msg[] =
31 #include <lttng-enable-rotation.1.h>
32 ;
33 #endif
34
35 enum {
36 OPT_HELP = 1,
37 OPT_LIST_OPTIONS,
38 OPT_TIMER,
39 OPT_SIZE,
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},
48 {"size", 0, POPT_ARG_INT, 0, OPT_SIZE, 0, 0},
49 {0, 0, 0, 0, 0, 0, 0}
50 };
51
52 static const char *schedule_type_str[] = {
53 [LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC] = "periodic",
54 [LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD] = "size-based",
55 };
56
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;
64
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;
71 }
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;
80 }
81 status = lttng_rotation_schedule_size_threshold_set_threshold(
82 schedule, value);
83 break;
84 default:
85 ERR("Unknown schedule type");
86 abort();
87 }
88
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:
99 ERR("Unknown error occurred setting %s rotation schedule",
100 schedule_type_name);
101 ret = CMD_ERROR;
102 goto end;
103 }
104
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:
111 MSG("Enabled %s rotations every %" PRIu64 " %s on session %s",
112 schedule_type_name, value, USEC_UNIT, session_name);
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();
120 }
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;
140 }
141
142 if (lttng_opt_mi) {
143 int mi_ret;
144
145 mi_ret = mi_lttng_rotation_schedule_result(writer,
146 schedule, ret == CMD_SUCCESS);
147 if (mi_ret < 0) {
148 ret = CMD_ERROR;
149 goto end;
150 }
151 }
152
153 end:
154 lttng_rotation_schedule_destroy(schedule);
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 {
165 int popt_ret, opt, ret = 0;
166 enum cmd_error_code cmd_ret = CMD_SUCCESS;
167 static poptContext pc;
168 char *session_name = NULL;
169 char *opt_arg = NULL;
170 bool free_session_name = false;
171 uint64_t timer_us = 0, size_bytes = 0;
172 bool periodic_rotation = false, size_rotation = false;
173
174 pc = poptGetContext(NULL, argc, argv, long_options, 0);
175 popt_ret = poptReadDefaultConfig(pc, 0);
176 if (popt_ret) {
177 ERR("poptReadDefaultConfig");
178 goto error;
179 }
180
181 while ((opt = poptGetNextOpt(pc)) != -1) {
182 switch (opt) {
183 case OPT_HELP:
184 SHOW_HELP();
185 goto end;
186 case OPT_LIST_OPTIONS:
187 list_cmd_options(stdout, long_options);
188 goto end;
189 case OPT_TIMER:
190 errno = 0;
191 opt_arg = poptGetOptArg(pc);
192 if (errno != 0 || !isdigit(opt_arg[0])) {
193 ERR("Invalid value for --timer option: %s", opt_arg);
194 goto error;
195 }
196 if (utils_parse_time_suffix(opt_arg, &timer_us) < 0) {
197 ERR("Invalid value for --timer option: %s", opt_arg);
198 goto error;
199 }
200 if (periodic_rotation) {
201 ERR("Only one periodic rotation schedule may be set on a session.");
202 goto error;
203 }
204 periodic_rotation = true;
205 break;
206 case OPT_SIZE:
207 errno = 0;
208 opt_arg = poptGetOptArg(pc);
209 if (utils_parse_size_suffix(opt_arg, &size_bytes) < 0) {
210 ERR("Invalid value for --size option: %s", opt_arg);
211 goto error;
212 }
213 if (size_rotation) {
214 ERR("Only one size-based rotation schedule may be set on a session.");
215 goto error;
216 }
217 size_rotation = true;
218 break;
219 default:
220 cmd_ret = CMD_UNDEFINED;
221 goto end;
222 }
223 }
224
225 if (opt_session_name == NULL) {
226 session_name = get_session_name();
227 if (session_name == NULL) {
228 goto error;
229 }
230 free_session_name = true;
231 } else {
232 session_name = opt_session_name;
233 }
234
235 /* Mi check */
236 if (lttng_opt_mi) {
237 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
238 if (!writer) {
239 goto error;
240 }
241
242 /* Open command element */
243 ret = mi_lttng_writer_command_open(writer,
244 mi_lttng_element_command_enable_rotation);
245 if (ret) {
246 goto error;
247 }
248
249 /* Open output element */
250 ret = mi_lttng_writer_open_element(writer,
251 mi_lttng_element_command_output);
252 if (ret) {
253 goto error;
254 }
255 }
256
257 if (!periodic_rotation && !size_rotation) {
258 ERR("No session rotation schedule parameter provided.");
259 cmd_ret = CMD_ERROR;
260 goto close_command;
261 }
262
263 if (lttng_opt_mi) {
264 ret = mi_lttng_writer_open_element(writer,
265 mi_lttng_element_rotation_schedule_results);
266 if (ret) {
267 goto error;
268 }
269
270 ret = mi_lttng_writer_write_element_string(writer,
271 mi_lttng_element_session_name,
272 session_name);
273 if (ret) {
274 goto error;
275 }
276 }
277
278 if (periodic_rotation) {
279 /*
280 * Continue processing even on error as multiple schedules can
281 * be specified at once.
282 */
283 cmd_ret = add_schedule(session_name,
284 LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC,
285 timer_us);
286 }
287
288 if (size_rotation) {
289 enum cmd_error_code tmp_ret;
290
291 /* Don't overwrite cmd_ret if it already indicates an error. */
292 tmp_ret = add_schedule(session_name,
293 LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD,
294 size_bytes);
295 cmd_ret = cmd_ret ? cmd_ret : tmp_ret;
296 }
297
298 if (lttng_opt_mi) {
299 /* Close rotation schedule results element */
300 ret = mi_lttng_writer_close_element(writer);
301 if (ret) {
302 goto error;
303 }
304 }
305
306 close_command:
307 /* Mi closing */
308 if (lttng_opt_mi) {
309 /* Close output element */
310 ret = mi_lttng_writer_close_element(writer);
311 if (ret) {
312 goto error;
313 }
314
315 /* Success ? */
316 ret = mi_lttng_writer_write_element_bool(writer,
317 mi_lttng_element_command_success,
318 cmd_ret == CMD_SUCCESS);
319 if (ret) {
320 goto error;
321 }
322
323 /* Command element close */
324 ret = mi_lttng_writer_command_close(writer);
325 if (ret) {
326 goto error;
327 }
328 }
329
330 end:
331 (void) mi_lttng_writer_destroy(writer);
332 poptFreeContext(pc);
333 if (free_session_name) {
334 free(session_name);
335 }
336 return cmd_ret;
337
338 error:
339 cmd_ret = CMD_ERROR;
340 goto end;
341 }
This page took 0.036561 seconds and 4 git commands to generate.