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