Fix: lttng: enable-rotation: leak of command parameter
[lttng-tools.git] / src / bin / lttng / commands / load.cpp
1 /*
2 * Copyright (C) 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #define _LGPL_SOURCE
9 #include <inttypes.h>
10 #include <popt.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 #include <common/mi-lttng.hpp>
16 #include <common/config/session-config.hpp>
17 #include <lttng/lttng.h>
18
19 #include "../command.hpp"
20
21 static char *the_opt_input_path;
22 static char *the_opt_override_url;
23 static char *the_opt_override_session_name;
24 static int the_opt_force;
25 static int the_opt_load_all;
26
27 static const char *the_session_name;
28
29 #ifdef LTTNG_EMBED_HELP
30 static const char help_msg[] =
31 #include <lttng-load.1.h>
32 ;
33 #endif
34
35 enum {
36 OPT_HELP = 1,
37 OPT_ALL,
38 OPT_FORCE,
39 OPT_LIST_OPTIONS,
40 };
41
42 static struct mi_writer *the_writer;
43
44 static struct poptOption the_load_opts[] = {
45 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
46 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
47 {"all", 'a', POPT_ARG_NONE, 0, OPT_ALL, 0, 0},
48 {"input-path", 'i', POPT_ARG_STRING, &the_opt_input_path, 0, 0, 0},
49 {"force", 'f', POPT_ARG_NONE, 0, OPT_FORCE, 0, 0},
50 {"override-url", 0, POPT_ARG_STRING, &the_opt_override_url, 0, 0, 0},
51 {"override-name", 0, POPT_ARG_STRING, &the_opt_override_session_name, 0, 0, 0},
52 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
53 {0, 0, 0, 0, 0, 0, 0}
54 };
55
56 static int mi_partial_session(const char *session_name)
57 {
58 int ret;
59 LTTNG_ASSERT(the_writer);
60 LTTNG_ASSERT(session_name);
61
62 /* Open session element */
63 ret = mi_lttng_writer_open_element(the_writer, config_element_session);
64 if (ret) {
65 goto end;
66 }
67
68 ret = mi_lttng_writer_write_element_string(the_writer, config_element_name,
69 session_name);
70 if (ret) {
71 goto end;
72 }
73
74 /* Closing session element */
75 ret = mi_lttng_writer_close_element(the_writer);
76 end:
77 return ret;
78 }
79
80 /*
81 * Mi print of load command
82 */
83 static int mi_load_print(const char *session_name)
84 {
85 int ret;
86 LTTNG_ASSERT(the_writer);
87
88 if (the_opt_load_all) {
89 /* We use a wildcard to represent all sessions */
90 session_name = "*";
91 }
92
93 /* Print load element */
94 ret = mi_lttng_writer_open_element(the_writer, mi_lttng_element_load);
95 if (ret) {
96 goto end;
97 }
98
99 /* Print session element */
100 ret = mi_partial_session(session_name);
101 if (ret) {
102 goto end;
103 }
104
105 /* Path element */
106 if (the_opt_input_path) {
107 ret = mi_lttng_writer_write_element_string(the_writer, config_element_path,
108 the_opt_input_path);
109 if (ret) {
110 goto end;
111 }
112 }
113
114 /* Print override elements */
115 ret = mi_lttng_writer_open_element(the_writer, mi_lttng_element_load_overrides);
116 if (ret) {
117 goto end;
118 }
119
120 /* Session name override element */
121 if (the_opt_override_session_name) {
122 ret = mi_lttng_writer_write_element_string(the_writer,
123 config_element_name, the_opt_override_session_name);
124 if (ret) {
125 goto end;
126 }
127 }
128
129 /* Session url override element */
130 if (the_opt_override_url) {
131 ret = mi_lttng_writer_write_element_string(the_writer,
132 mi_lttng_element_load_override_url,
133 the_opt_override_url);
134 if (ret) {
135 goto end;
136 }
137 }
138
139 /* Close override and load element */
140 ret = mi_lttng_close_multi_element(the_writer, 2);
141 end:
142 return ret;
143 }
144
145 /*
146 * The 'load <options>' first level command
147 */
148 int cmd_load(int argc, const char **argv)
149 {
150 int ret, success;
151 int opt;
152 poptContext pc;
153 struct lttng_load_session_attr *session_attr = NULL;
154 char *input_path = NULL;
155 const char *leftover = NULL;
156
157 pc = poptGetContext(NULL, argc, argv, the_load_opts, 0);
158 poptReadDefaultConfig(pc, 0);
159
160 while ((opt = poptGetNextOpt(pc)) != -1) {
161 switch (opt) {
162 case OPT_HELP:
163 SHOW_HELP();
164 ret = CMD_SUCCESS;
165 goto end;
166 case OPT_ALL:
167 the_opt_load_all = 1;
168 break;
169 case OPT_LIST_OPTIONS:
170 list_cmd_options(stdout, the_load_opts);
171 ret = CMD_SUCCESS;
172 goto end;
173 case OPT_FORCE:
174 the_opt_force = 1;
175 break;
176 default:
177 ret = CMD_UNDEFINED;
178 goto end;
179 }
180 }
181
182 ret = lttng_session_daemon_alive();
183 if (!ret) {
184 ERR("No session daemon is available");
185 ret = CMD_ERROR;
186 goto end;
187 }
188
189 if (!the_opt_load_all) {
190 the_session_name = poptGetArg(pc);
191 if (the_session_name) {
192 DBG2("Loading session name: %s", the_session_name);
193 } else {
194 /* Default to load_all */
195 the_opt_load_all = 1;
196 }
197 }
198
199 leftover = poptGetArg(pc);
200 if (leftover) {
201 ERR("Unknown argument: %s", leftover);
202 ret = CMD_ERROR;
203 goto end;
204 }
205
206 /* Mi check */
207 if (lttng_opt_mi) {
208 the_writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
209 if (!the_writer) {
210 ret = CMD_ERROR;
211 goto end;
212 }
213
214 /* Open command element */
215 ret = mi_lttng_writer_command_open(the_writer,
216 mi_lttng_element_command_load);
217 if (ret) {
218 ret = CMD_ERROR;
219 goto end;
220 }
221
222 /* Open output element */
223 ret = mi_lttng_writer_open_element(the_writer,
224 mi_lttng_element_command_output);
225 if (ret) {
226 ret = CMD_ERROR;
227 goto end;
228 }
229 }
230
231 /* Prepare load attributes */
232 session_attr = lttng_load_session_attr_create();
233 if (!session_attr) {
234 ERR("Failed to create load session attributes");
235 ret = CMD_ERROR;
236 goto end;
237 }
238
239 /*
240 * Set the input url
241 * lttng_load_session_attr_set_input_url only suppports absolute path.
242 * Use realpath to resolve any relative path.
243 * */
244 if (the_opt_input_path) {
245 input_path = realpath(the_opt_input_path, NULL);
246 if (!input_path) {
247 PERROR("Invalid input path");
248 ret = CMD_ERROR;
249 goto end;
250 }
251 } else {
252 input_path = NULL;
253 }
254
255 ret = lttng_load_session_attr_set_input_url(session_attr,
256 input_path);
257 if (ret) {
258 ERR("Invalid input path");
259 ret = CMD_ERROR;
260 goto end;
261 }
262
263 /* Set the session name. NULL means all sessions should be loaded */
264 ret = lttng_load_session_attr_set_session_name(session_attr,
265 the_session_name);
266 if (ret) {
267 ERR("Invalid session name");
268 ret = CMD_ERROR;
269 goto end;
270 }
271
272 /* Set the overwrite attribute */
273 ret = lttng_load_session_attr_set_overwrite(session_attr, the_opt_force);
274 if (ret) {
275 ERR("Force argument could not be applied");
276 ret = CMD_ERROR;
277 goto end;
278 }
279
280 /* Set the overrides attributes if any */
281 if (the_opt_override_url) {
282 ret = lttng_load_session_attr_set_override_url(session_attr,
283 the_opt_override_url);
284 if (ret) {
285 ERR("Url override is invalid");
286 goto end;
287 }
288 }
289
290 if (the_opt_override_session_name) {
291 if (the_opt_load_all) {
292 ERR("Options --all and --override-name cannot be used simultaneously");
293 ret = CMD_ERROR;
294 goto end;
295 }
296 ret = lttng_load_session_attr_set_override_session_name(session_attr,
297 the_opt_override_session_name);
298 if (ret) {
299 ERR("Failed to set session name override");
300 ret = CMD_ERROR;
301 goto end;
302 }
303 }
304
305 ret = lttng_load_session(session_attr);
306 if (ret) {
307 ERR("%s", lttng_strerror(ret));
308 success = 0;
309 ret = CMD_ERROR;
310 } else {
311 if (the_opt_load_all) {
312 MSG("All sessions have been loaded successfully");
313 } else if (the_session_name) {
314 ret = config_init((char *) the_session_name);
315 if (ret < 0) {
316 WARN("Could not set %s as the default session",
317 the_session_name);
318 }
319 MSG("Session %s has been loaded successfully", the_session_name);
320 } else {
321 MSG("Session has been loaded successfully");
322 }
323
324 if (the_opt_override_session_name) {
325 MSG("Session name overridden with %s",
326 the_opt_override_session_name);
327 }
328
329 if (the_opt_override_url) {
330 MSG("Session output url overridden with %s", the_opt_override_url);
331 }
332 success = 1;
333 ret = CMD_SUCCESS;
334 }
335
336 /* Mi Printing and closing */
337 if (lttng_opt_mi) {
338 /* Mi print */
339 ret = mi_load_print(the_session_name);
340 if (ret) {
341 ret = CMD_ERROR;
342 goto end;
343 }
344
345 /* Close output element */
346 ret = mi_lttng_writer_close_element(the_writer);
347 if (ret) {
348 ret = CMD_ERROR;
349 goto end;
350 }
351
352 /* Success ? */
353 ret = mi_lttng_writer_write_element_bool(the_writer,
354 mi_lttng_element_command_success, success);
355 if (ret) {
356 ret = CMD_ERROR;
357 goto end;
358 }
359
360 /* Command element close */
361 ret = mi_lttng_writer_command_close(the_writer);
362 if (ret) {
363 ret = CMD_ERROR;
364 goto end;
365 }
366 }
367 end:
368 if (the_writer && mi_lttng_writer_destroy(the_writer)) {
369 ERR("Failed to destroy mi lttng writer");
370 }
371
372 lttng_load_session_attr_destroy(session_attr);
373 free(input_path);
374 poptFreeContext(pc);
375 return ret;
376 }
This page took 0.10008 seconds and 4 git commands to generate.