b179e91ce33e8f91e94a3d741d9ef7a0a34e03ab
[lttng-tools.git] / src / bin / lttng / commands / disable_rotation.c
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
32 #include "../command.h"
33 #include <lttng/rotation.h>
34
35 static char *opt_session_name;
36 static struct mi_writer *writer;
37
38 #ifdef LTTNG_EMBED_HELP
39 static const char help_msg[] =
40 #include <lttng-disable-rotation.1.h>
41 ;
42 #endif
43
44 enum {
45 OPT_HELP = 1,
46 OPT_LIST_OPTIONS,
47 OPT_TIMER,
48 OPT_SIZE,
49 };
50
51 static struct poptOption long_options[] = {
52 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
53 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
54 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
55 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
56 {"timer", 0, POPT_ARG_NONE, 0, OPT_TIMER, 0, 0},
57 {"size", 0, POPT_ARG_NONE, 0, OPT_SIZE, 0, 0},
58 {0, 0, 0, 0, 0, 0, 0}
59 };
60
61 static int setup_rotate(char *session_name, uint64_t timer, uint64_t size)
62 {
63 int ret = 0;
64 struct lttng_rotation_schedule_attr *attr = NULL;
65
66 attr = lttng_rotation_schedule_attr_create();
67 if (!attr) {
68 goto error;
69 }
70
71 if (lttng_opt_mi) {
72 /* Open rotation_schedule element */
73 ret = mi_lttng_writer_open_element(writer,
74 config_element_rotation_schedule);
75 if (ret) {
76 goto error;
77 }
78 }
79
80 if (lttng_opt_mi) {
81 ret = mi_lttng_writer_write_element_string(writer,
82 mi_lttng_element_session_name, session_name);
83 if (ret) {
84 goto error;
85 }
86 }
87
88 if (timer == -1ULL) {
89 lttng_rotation_schedule_attr_set_timer_period(attr, timer);
90 MSG("Disabling rotation timer on session %s", session_name);
91 }
92 if (size == -1ULL) {
93 lttng_rotation_schedule_attr_set_size(attr, size);
94 MSG("Disabling rotation based on size on session %s", session_name);
95 }
96
97 ret = lttng_rotation_set_schedule(session_name, attr);
98 if (ret) {
99 ERR("%s", lttng_strerror(ret));
100 if (lttng_opt_mi) {
101 ret = mi_lttng_writer_write_element_string(writer,
102 mi_lttng_element_rotate_status, "error");
103 if (ret) {
104 goto end;
105 }
106 /* Close rotation_schedule element */
107 ret = mi_lttng_writer_close_element(writer);
108 if (ret) {
109 goto end;
110 }
111 }
112 goto error;
113 }
114
115 if (lttng_opt_mi) {
116 ret = mi_lttng_writer_write_element_string(writer,
117 mi_lttng_element_rotate_status, "success");
118 if (ret) {
119 goto end;
120 }
121
122 /* Close rotation_schedule element */
123 ret = mi_lttng_writer_close_element(writer);
124 if (ret) {
125 goto end;
126 }
127 }
128
129 ret = 0;
130 goto end;
131
132 error:
133 ret = -1;
134 end:
135 return ret;
136 }
137
138 /*
139 * cmd_disable_rotation
140 *
141 * The 'enable-rotation <options>' first level command
142 */
143 int cmd_disable_rotation(int argc, const char **argv)
144 {
145 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
146 int popt_ret;
147 static poptContext pc;
148 char *session_name = NULL;
149 bool free_session_name = false;
150 uint64_t timer = 0, size = 0;
151
152 pc = poptGetContext(NULL, argc, argv, long_options, 0);
153 popt_ret = poptReadDefaultConfig(pc, 0);
154 if (popt_ret) {
155 ret = CMD_ERROR;
156 ERR("poptReadDefaultConfig");
157 goto end;
158 }
159
160 while ((opt = poptGetNextOpt(pc)) != -1) {
161 switch (opt) {
162 case OPT_HELP:
163 SHOW_HELP();
164 goto end;
165 case OPT_LIST_OPTIONS:
166 list_cmd_options(stdout, long_options);
167 goto end;
168 case OPT_TIMER:
169 timer = -1ULL;
170 break;
171 case OPT_SIZE:
172 size = -1ULL;
173 break;
174 default:
175 ret = CMD_UNDEFINED;
176 goto end;
177 }
178 }
179
180 if (opt_session_name == NULL) {
181 session_name = get_session_name();
182 if (session_name == NULL) {
183 goto end;
184 }
185 free_session_name = true;
186 } else {
187 session_name = opt_session_name;
188 }
189
190 /* Mi check */
191 if (lttng_opt_mi) {
192 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
193 if (!writer) {
194 ret = -LTTNG_ERR_NOMEM;
195 goto end;
196 }
197
198 /* Open command element */
199 ret = mi_lttng_writer_command_open(writer,
200 mi_lttng_element_command_disable_rotation);
201 if (ret) {
202 ret = CMD_ERROR;
203 goto end;
204 }
205
206 /* Open output element */
207 ret = mi_lttng_writer_open_element(writer,
208 mi_lttng_element_command_output);
209 if (ret) {
210 ret = CMD_ERROR;
211 goto end;
212 }
213 }
214
215 /* No config options, just rotate the session now */
216 if (timer == 0 && size == 0) {
217 ERR("Missing timer period (--timer) or size limit (--size) option");
218 success = 0;
219 command_ret = -1;
220 } else {
221 command_ret = setup_rotate(session_name, timer, size);
222 }
223
224 if (command_ret) {
225 ERR("%s", lttng_strerror(command_ret));
226 success = 0;
227 }
228
229 /* Mi closing */
230 if (lttng_opt_mi) {
231 /* Close output element */
232 ret = mi_lttng_writer_close_element(writer);
233 if (ret) {
234 goto end;
235 }
236
237 /* Success ? */
238 ret = mi_lttng_writer_write_element_bool(writer,
239 mi_lttng_element_command_success, success);
240 if (ret) {
241 ret = CMD_ERROR;
242 goto end;
243 }
244
245 /* Command element close */
246 ret = mi_lttng_writer_command_close(writer);
247 if (ret) {
248 ret = CMD_ERROR;
249 goto end;
250 }
251 }
252
253 end:
254 /* Mi clean-up */
255 if (writer && mi_lttng_writer_destroy(writer)) {
256 /* Preserve original error code */
257 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
258 }
259
260 /* Overwrite ret if an error occurred with start_tracing */
261 ret = command_ret ? command_ret : ret;
262 poptFreeContext(pc);
263 if (free_session_name) {
264 free(session_name);
265 }
266 return ret;
267 }
This page took 0.034671 seconds and 3 git commands to generate.