a0f5e1d65befd4f5990def4bd91acd948c6dd3f5
[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 int opt_force;
34 static int opt_load_all;
35
36 static const char *session_name;
37
38 enum {
39 OPT_HELP = 1,
40 OPT_ALL,
41 OPT_FORCE,
42 OPT_LIST_OPTIONS,
43 };
44
45 static struct mi_writer *writer;
46
47 static struct poptOption load_opts[] = {
48 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
49 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
50 {"all", 'a', POPT_ARG_NONE, 0, OPT_ALL, 0, 0},
51 {"input-path", 'i', POPT_ARG_STRING, &opt_input_path, 0, 0, 0},
52 {"force", 'f', POPT_ARG_NONE, 0, OPT_FORCE, 0, 0},
53 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
54 {0, 0, 0, 0, 0, 0, 0}
55 };
56
57 static int mi_partial_session(const char *session_name)
58 {
59 int ret;
60 assert(writer);
61 assert(session_name);
62
63 /* Open session element */
64 ret = mi_lttng_writer_open_element(writer, config_element_session);
65 if (ret) {
66 goto end;
67 }
68
69 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
70 session_name);
71 if (ret) {
72 goto end;
73 }
74
75 /* Closing session element */
76 ret = mi_lttng_writer_close_element(writer);
77 end:
78 return ret;
79 }
80
81 /*
82 * Mi print of load command
83 */
84 static int mi_load_print(const char *session_name)
85 {
86 int ret;
87 assert(writer);
88
89 if (opt_load_all) {
90 /* We use a wildcard to represent all sessions */
91 session_name = "*";
92 }
93
94 /* Print load element */
95 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_load);
96 if (ret) {
97 goto end;
98 }
99
100 /* Print session element */
101 ret = mi_partial_session(session_name);
102 if (ret) {
103 goto end;
104 }
105
106 /* Path element */
107 if (opt_input_path) {
108 ret = mi_lttng_writer_write_element_string(writer, config_element_path,
109 opt_input_path);
110 if (ret) {
111 goto end;
112 }
113 }
114
115 /* Close load element */
116 ret = mi_lttng_writer_close_element(writer);
117
118 end:
119 return ret;
120 }
121
122 /*
123 * The 'load <options>' first level command
124 */
125 int cmd_load(int argc, const char **argv)
126 {
127 int ret, success;
128 int opt;
129 poptContext pc;
130 struct lttng_load_session_attr *session_attr = NULL;
131 char *input_path = NULL;
132
133 pc = poptGetContext(NULL, argc, argv, load_opts, 0);
134 poptReadDefaultConfig(pc, 0);
135
136 while ((opt = poptGetNextOpt(pc)) != -1) {
137 switch (opt) {
138 case OPT_HELP:
139 SHOW_HELP();
140 ret = CMD_SUCCESS;
141 goto end;
142 case OPT_ALL:
143 opt_load_all = 1;
144 break;
145 case OPT_LIST_OPTIONS:
146 list_cmd_options(stdout, load_opts);
147 ret = CMD_SUCCESS;
148 goto end;
149 case OPT_FORCE:
150 opt_force = 1;
151 break;
152 default:
153 ret = CMD_UNDEFINED;
154 goto end;
155 }
156 }
157
158 ret = lttng_session_daemon_alive();
159 if (!ret) {
160 ERR("No session daemon is available");
161 ret = CMD_ERROR;
162 goto end;
163 }
164
165 if (!opt_load_all) {
166 session_name = poptGetArg(pc);
167 if (session_name) {
168 DBG2("Loading session name: %s", session_name);
169 } else {
170 /* Default to load_all */
171 opt_load_all = 1;
172 }
173 }
174
175 /* Mi check */
176 if (lttng_opt_mi) {
177 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
178 if (!writer) {
179 ret = CMD_ERROR;
180 goto end;
181 }
182
183 /* Open command element */
184 ret = mi_lttng_writer_command_open(writer,
185 mi_lttng_element_command_load);
186 if (ret) {
187 ret = CMD_ERROR;
188 goto end;
189 }
190
191 /* Open output element */
192 ret = mi_lttng_writer_open_element(writer,
193 mi_lttng_element_command_output);
194 if (ret) {
195 ret = CMD_ERROR;
196 goto end;
197 }
198 }
199
200 /* Prepare load attributes */
201 session_attr = lttng_load_session_attr_create();
202 if (!session_attr) {
203 ERR("Failed to create load session attributes");
204 ret = CMD_ERROR;
205 goto end;
206 }
207
208 /*
209 * Set the input url
210 * lttng_load_session_attr_set_input_url only suppports absolute path.
211 * Use realpath to resolve any relative path.
212 * */
213 if (opt_input_path) {
214 input_path = realpath(opt_input_path, NULL);
215 if (!input_path) {
216 PERROR("Invalid input path");
217 ret = CMD_ERROR;
218 goto end;
219 }
220 } else {
221 input_path = NULL;
222 }
223
224 ret = lttng_load_session_attr_set_input_url(session_attr,
225 input_path);
226 if (ret) {
227 ERR("Invalid input path");
228 ret = CMD_ERROR;
229 goto end;
230 }
231
232 /* Set the session name. NULL means all sessions should be loaded */
233 ret = lttng_load_session_attr_set_session_name(session_attr,
234 session_name);
235 if (ret) {
236 ERR("Invalid session name");
237 ret = CMD_ERROR;
238 goto end;
239 }
240
241 /* Set the overwrite attribute */
242 ret = lttng_load_session_attr_set_overwrite(session_attr, opt_force);
243 if (ret) {
244 ERR("Force argument could not be applied");
245 ret = CMD_ERROR;
246 goto end;
247 }
248
249 ret = lttng_load_session(session_attr);
250 if (ret) {
251 ERR("%s", lttng_strerror(ret));
252 success = 0;
253 ret = CMD_ERROR;
254 } else {
255 if (opt_load_all) {
256 MSG("All sessions have been loaded successfully");
257 } else if (session_name) {
258 ret = config_init((char *) session_name);
259 if (ret < 0) {
260 ret = CMD_WARNING;
261 }
262 MSG("Session %s has been loaded successfully", session_name);
263 } else {
264 MSG("Session has been loaded successfully");
265 }
266 success = 1;
267 ret = CMD_SUCCESS;
268 }
269
270 /* Mi Printing and closing */
271 if (lttng_opt_mi) {
272 /* Mi print */
273 ret = mi_load_print(session_name);
274 if (ret) {
275 ret = CMD_ERROR;
276 goto end;
277 }
278
279 /* Close output element */
280 ret = mi_lttng_writer_close_element(writer);
281 if (ret) {
282 ret = CMD_ERROR;
283 goto end;
284 }
285
286 /* Success ? */
287 ret = mi_lttng_writer_write_element_bool(writer,
288 mi_lttng_element_command_success, success);
289 if (ret) {
290 ret = CMD_ERROR;
291 goto end;
292 }
293
294 /* Command element close */
295 ret = mi_lttng_writer_command_close(writer);
296 if (ret) {
297 ret = CMD_ERROR;
298 goto end;
299 }
300 }
301 end:
302 if (writer && mi_lttng_writer_destroy(writer)) {
303 ERR("Failed to destroy mi lttng writer");
304 }
305
306 lttng_load_session_attr_destroy(session_attr);
307 free(input_path);
308 poptFreeContext(pc);
309 return ret;
310 }
This page took 0.082021 seconds and 3 git commands to generate.