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