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