Fix: destroy schedule attr
[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 const char *leftover = NULL;
167
168 pc = poptGetContext(NULL, argc, argv, load_opts, 0);
169 poptReadDefaultConfig(pc, 0);
170
171 while ((opt = poptGetNextOpt(pc)) != -1) {
172 switch (opt) {
173 case OPT_HELP:
174 SHOW_HELP();
175 ret = CMD_SUCCESS;
176 goto end;
177 case OPT_ALL:
178 opt_load_all = 1;
179 break;
180 case OPT_LIST_OPTIONS:
181 list_cmd_options(stdout, load_opts);
182 ret = CMD_SUCCESS;
183 goto end;
184 case OPT_FORCE:
185 opt_force = 1;
186 break;
187 default:
188 ret = CMD_UNDEFINED;
189 goto end;
190 }
191 }
192
193 ret = lttng_session_daemon_alive();
194 if (!ret) {
195 ERR("No session daemon is available");
196 ret = CMD_ERROR;
197 goto end;
198 }
199
200 if (!opt_load_all) {
201 session_name = poptGetArg(pc);
202 if (session_name) {
203 DBG2("Loading session name: %s", session_name);
204 } else {
205 /* Default to load_all */
206 opt_load_all = 1;
207 }
208 }
209
210 leftover = poptGetArg(pc);
211 if (leftover) {
212 ERR("Unknown argument: %s", leftover);
213 ret = CMD_ERROR;
214 goto end;
215 }
216
217 /* Mi check */
218 if (lttng_opt_mi) {
219 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
220 if (!writer) {
221 ret = CMD_ERROR;
222 goto end;
223 }
224
225 /* Open command element */
226 ret = mi_lttng_writer_command_open(writer,
227 mi_lttng_element_command_load);
228 if (ret) {
229 ret = CMD_ERROR;
230 goto end;
231 }
232
233 /* Open output element */
234 ret = mi_lttng_writer_open_element(writer,
235 mi_lttng_element_command_output);
236 if (ret) {
237 ret = CMD_ERROR;
238 goto end;
239 }
240 }
241
242 /* Prepare load attributes */
243 session_attr = lttng_load_session_attr_create();
244 if (!session_attr) {
245 ERR("Failed to create load session attributes");
246 ret = CMD_ERROR;
247 goto end;
248 }
249
250 /*
251 * Set the input url
252 * lttng_load_session_attr_set_input_url only suppports absolute path.
253 * Use realpath to resolve any relative path.
254 * */
255 if (opt_input_path) {
256 input_path = realpath(opt_input_path, NULL);
257 if (!input_path) {
258 PERROR("Invalid input path");
259 ret = CMD_ERROR;
260 goto end;
261 }
262 } else {
263 input_path = NULL;
264 }
265
266 ret = lttng_load_session_attr_set_input_url(session_attr,
267 input_path);
268 if (ret) {
269 ERR("Invalid input path");
270 ret = CMD_ERROR;
271 goto end;
272 }
273
274 /* Set the session name. NULL means all sessions should be loaded */
275 ret = lttng_load_session_attr_set_session_name(session_attr,
276 session_name);
277 if (ret) {
278 ERR("Invalid session name");
279 ret = CMD_ERROR;
280 goto end;
281 }
282
283 /* Set the overwrite attribute */
284 ret = lttng_load_session_attr_set_overwrite(session_attr, opt_force);
285 if (ret) {
286 ERR("Force argument could not be applied");
287 ret = CMD_ERROR;
288 goto end;
289 }
290
291 /* Set the overrides attributes if any */
292 if (opt_override_url) {
293 ret = lttng_load_session_attr_set_override_url(session_attr,
294 opt_override_url);
295 if (ret) {
296 ERR("Url override is invalid");
297 goto end;
298 }
299 }
300
301 if (opt_override_session_name) {
302 if (opt_load_all) {
303 ERR("Options --all and --override-name cannot be used simultaneously");
304 ret = CMD_ERROR;
305 goto end;
306 }
307 ret = lttng_load_session_attr_set_override_session_name(session_attr,
308 opt_override_session_name);
309 if (ret) {
310 ERR("Failed to set session name override");
311 ret = CMD_ERROR;
312 goto end;
313 }
314 }
315
316 ret = lttng_load_session(session_attr);
317 if (ret) {
318 ERR("%s", lttng_strerror(ret));
319 success = 0;
320 ret = CMD_ERROR;
321 } else {
322 if (opt_load_all) {
323 MSG("All sessions have been loaded successfully");
324 } else if (session_name) {
325 ret = config_init((char *) session_name);
326 if (ret < 0) {
327 WARN("Could not set %s as the default session",
328 session_name);
329 }
330 MSG("Session %s has been loaded successfully", session_name);
331 } else {
332 MSG("Session has been loaded successfully");
333 }
334
335 if (opt_override_session_name) {
336 MSG("Session name overridden with %s",
337 opt_override_session_name);
338 }
339
340 if (opt_override_url) {
341 MSG("Session output url overridden with %s", opt_override_url);
342 }
343 success = 1;
344 ret = CMD_SUCCESS;
345 }
346
347 /* Mi Printing and closing */
348 if (lttng_opt_mi) {
349 /* Mi print */
350 ret = mi_load_print(session_name);
351 if (ret) {
352 ret = CMD_ERROR;
353 goto end;
354 }
355
356 /* Close output element */
357 ret = mi_lttng_writer_close_element(writer);
358 if (ret) {
359 ret = CMD_ERROR;
360 goto end;
361 }
362
363 /* Success ? */
364 ret = mi_lttng_writer_write_element_bool(writer,
365 mi_lttng_element_command_success, success);
366 if (ret) {
367 ret = CMD_ERROR;
368 goto end;
369 }
370
371 /* Command element close */
372 ret = mi_lttng_writer_command_close(writer);
373 if (ret) {
374 ret = CMD_ERROR;
375 goto end;
376 }
377 }
378 end:
379 if (writer && mi_lttng_writer_destroy(writer)) {
380 ERR("Failed to destroy mi lttng writer");
381 }
382
383 lttng_load_session_attr_destroy(session_attr);
384 free(input_path);
385 poptFreeContext(pc);
386 return ret;
387 }
This page took 0.053542 seconds and 4 git commands to generate.