Fix: lttng-snapshot: use after free of max size argument
[lttng-tools.git] / src / bin / lttng / commands / regenerate.cpp
1 /*
2 * Copyright (C) 2015 Julien Desfossez <jdesfossez@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #define _LGPL_SOURCE
9 #include <ctype.h>
10 #include <popt.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <unistd.h>
15
16 #include <common/mi-lttng.hpp>
17
18 #include "../command.hpp"
19
20 static char *opt_session_name;
21 static char *session_name = NULL;
22
23 static int regenerate_metadata(int argc, const char **argv);
24 static int regenerate_statedump(int argc, const char **argv);
25
26 #ifdef LTTNG_EMBED_HELP
27 static const char help_msg[] =
28 #include <lttng-regenerate.1.h>
29 ;
30 #endif
31
32 enum {
33 OPT_HELP = 1,
34 OPT_LIST_OPTIONS,
35 OPT_LIST_COMMANDS,
36 };
37
38 static struct mi_writer *writer;
39
40 static struct poptOption long_options[] = {
41 /* { longName, shortName, argInfo, argPtr, value, descrip, argDesc, } */
42 { "help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0, },
43 { "session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
44 { "list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, 0, 0, },
45 { "list-commands", 0, POPT_ARG_NONE, NULL, OPT_LIST_COMMANDS, NULL, NULL},
46 { 0, 0, 0, 0, 0, 0, 0, },
47 };
48
49 static struct cmd_struct actions[] = {
50 { "metadata", regenerate_metadata },
51 { "statedump", regenerate_statedump },
52 { NULL, NULL } /* Array closure */
53 };
54
55 /*
56 * Count and return the number of arguments in argv.
57 */
58 static int count_arguments(const char **argv)
59 {
60 int i = 0;
61
62 LTTNG_ASSERT(argv);
63
64 while (argv[i] != NULL) {
65 i++;
66 }
67
68 return i;
69 }
70
71 static int regenerate_metadata(int argc,
72 const char **argv __attribute__((unused)))
73 {
74 int ret;
75
76 if (argc > 1) {
77 ret = CMD_UNDEFINED;
78 goto end;
79 }
80 ret = lttng_regenerate_metadata(session_name);
81 if (ret == 0) {
82 MSG("Metadata successfully regenerated for session %s", session_name);
83 } else {
84 ERR("%s", lttng_strerror(ret));
85 }
86
87 end:
88 return ret;
89 }
90
91 static int regenerate_statedump(int argc,
92 const char **argv __attribute__((unused)))
93 {
94 int ret;
95
96 if (argc > 1) {
97 ret = -LTTNG_ERR_INVALID;
98 goto end;
99 }
100 ret = lttng_regenerate_statedump(session_name);
101 if (ret == 0) {
102 MSG("State dump successfully regenerated for session %s", session_name);
103 }
104
105 end:
106 return ret;
107 }
108
109 static int handle_command(const char **argv)
110 {
111 struct cmd_struct *cmd;
112 int ret = CMD_SUCCESS, i = 0, argc, command_ret = CMD_SUCCESS;
113
114 if (argv == NULL) {
115 ERR("No object specified for regenerate command.");
116 command_ret = CMD_ERROR;
117 goto end;
118 }
119
120 argc = count_arguments(argv);
121 LTTNG_ASSERT(argc >= 1);
122
123 cmd = &actions[i];
124 while (cmd->func != NULL) {
125 /* Find command */
126 if (strcmp(argv[0], cmd->name) == 0) {
127 if (lttng_opt_mi) {
128 /* Action element */
129 ret = mi_lttng_writer_open_element(writer,
130 mi_lttng_element_command_regenerate_action);
131 if (ret) {
132 ret = CMD_ERROR;
133 goto end;
134 }
135
136 /* Name of the action */
137 ret = mi_lttng_writer_write_element_string(writer,
138 config_element_name, argv[0]);
139 if (ret) {
140 ret = CMD_ERROR;
141 goto end;
142 }
143 }
144 command_ret = cmd->func(argc, argv);
145 if (lttng_opt_mi) {
146 /* Close output and action element */
147 ret = mi_lttng_writer_close_element(writer);
148 if (ret) {
149 ret = CMD_ERROR;
150 goto end;
151 }
152 }
153 goto end;
154 }
155
156 cmd = &actions[i++];
157 }
158
159 ret = CMD_UNDEFINED;
160
161 end:
162 /* Overwrite ret if an error occurred in cmd->func() */
163 ret = command_ret ? command_ret : ret;
164 return ret;
165 }
166
167 /*
168 * regenerate command handling.
169 */
170 int cmd_regenerate(int argc, const char **argv)
171 {
172 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
173 static poptContext pc;
174
175 if (argc < 1) {
176 SHOW_HELP();
177 ret = CMD_ERROR;
178 goto end;
179 }
180
181 pc = poptGetContext(NULL, argc, argv, long_options, 0);
182 poptReadDefaultConfig(pc, 0);
183
184 if (lttng_opt_mi) {
185 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
186 if (!writer) {
187 ret = -LTTNG_ERR_NOMEM;
188 goto end;
189 }
190 /* Open command element */
191 ret = mi_lttng_writer_command_open(writer,
192 mi_lttng_element_command_regenerate);
193 if (ret) {
194 ret = CMD_ERROR;
195 goto end;
196 }
197
198 /* Open output element */
199 ret = mi_lttng_writer_open_element(writer,
200 mi_lttng_element_command_output);
201 if (ret) {
202 ret = CMD_ERROR;
203 goto end;
204 }
205 }
206
207 while ((opt = poptGetNextOpt(pc)) != -1) {
208 switch (opt) {
209 case OPT_HELP:
210 SHOW_HELP();
211 goto end;
212 case OPT_LIST_OPTIONS:
213 list_cmd_options(stdout, long_options);
214 goto end;
215 case OPT_LIST_COMMANDS:
216 list_commands(actions, stdout);
217 goto end;
218 default:
219 SHOW_HELP();
220 ret = CMD_UNDEFINED;
221 goto end;
222 }
223 }
224
225 if (!opt_session_name) {
226 session_name = get_session_name();
227 if (session_name == NULL) {
228 ret = CMD_ERROR;
229 goto end;
230 }
231 } else {
232 session_name = opt_session_name;
233 }
234
235 command_ret = handle_command(poptGetArgs(pc));
236 if (command_ret) {
237 success = 0;
238 }
239
240 if (lttng_opt_mi) {
241 /* Close output element */
242 ret = mi_lttng_writer_close_element(writer);
243 if (ret) {
244 ret = CMD_ERROR;
245 goto end;
246 }
247
248 /* Success ? */
249 ret = mi_lttng_writer_write_element_bool(writer,
250 mi_lttng_element_command_success, success);
251 if (ret) {
252 ret = CMD_ERROR;
253 goto end;
254 }
255
256 /* Command element close */
257 ret = mi_lttng_writer_command_close(writer);
258 if (ret) {
259 ret = CMD_ERROR;
260 goto end;
261 }
262 }
263
264 end:
265 /* Mi clean-up */
266 if (writer && mi_lttng_writer_destroy(writer)) {
267 /* Preserve original error code */
268 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
269 }
270
271 if (!opt_session_name) {
272 free(session_name);
273 }
274
275 /* Overwrite ret if an error occurred during handle_command() */
276 ret = command_ret ? command_ret : ret;
277
278 poptFreeContext(pc);
279 return ret;
280 }
This page took 0.035765 seconds and 4 git commands to generate.