Commit | Line | Data |
---|---|---|
8c42d845 JG |
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 | ||
6c1c0768 | 18 | #define _LGPL_SOURCE |
8c42d845 JG |
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 | ||
1734c658 | 26 | #include <common/mi-lttng.h> |
f40ef1d5 | 27 | #include <common/config/session-config.h> |
ea6ebdf0 | 28 | #include <lttng/load.h> |
1734c658 | 29 | |
8c42d845 JG |
30 | #include "../command.h" |
31 | ||
32 | static char *opt_input_path; | |
8c42d845 JG |
33 | static int opt_force; |
34 | static int opt_load_all; | |
35 | ||
11143783 DG |
36 | static const char *session_name; |
37 | ||
8c42d845 JG |
38 | enum { |
39 | OPT_HELP = 1, | |
40 | OPT_ALL, | |
41 | OPT_FORCE, | |
13a810d5 | 42 | OPT_LIST_OPTIONS, |
8c42d845 JG |
43 | }; |
44 | ||
1734c658 JRJ |
45 | static struct mi_writer *writer; |
46 | ||
8c42d845 JG |
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}, | |
8c42d845 JG |
51 | {"input-path", 'i', POPT_ARG_STRING, &opt_input_path, 0, 0, 0}, |
52 | {"force", 'f', POPT_ARG_NONE, 0, OPT_FORCE, 0, 0}, | |
13a810d5 | 53 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
8c42d845 JG |
54 | {0, 0, 0, 0, 0, 0, 0} |
55 | }; | |
56 | ||
1734c658 JRJ |
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 | ||
8c42d845 JG |
122 | /* |
123 | * The 'load <options>' first level command | |
124 | */ | |
125 | int cmd_load(int argc, const char **argv) | |
126 | { | |
1734c658 | 127 | int ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success; |
8c42d845 JG |
128 | int opt; |
129 | poptContext pc; | |
ea6ebdf0 JR |
130 | struct lttng_load_session_attr *session_attr = NULL; |
131 | char *input_path = NULL; | |
8c42d845 JG |
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: | |
4ba92f18 | 139 | SHOW_HELP(); |
8c42d845 JG |
140 | goto end; |
141 | case OPT_ALL: | |
142 | opt_load_all = 1; | |
143 | break; | |
13a810d5 DG |
144 | case OPT_LIST_OPTIONS: |
145 | list_cmd_options(stdout, load_opts); | |
146 | goto end; | |
8c42d845 JG |
147 | case OPT_FORCE: |
148 | opt_force = 1; | |
149 | break; | |
150 | default: | |
8c42d845 JG |
151 | ret = CMD_UNDEFINED; |
152 | goto end; | |
153 | } | |
154 | } | |
155 | ||
11143783 DG |
156 | if (!opt_load_all) { |
157 | session_name = poptGetArg(pc); | |
732b768a DG |
158 | if (session_name) { |
159 | DBG2("Loading session name: %s", session_name); | |
1734c658 JRJ |
160 | } else { |
161 | /* Default to load_all */ | |
162 | opt_load_all = 1; | |
11143783 | 163 | } |
8c42d845 JG |
164 | } |
165 | ||
1734c658 JRJ |
166 | /* Mi check */ |
167 | if (lttng_opt_mi) { | |
168 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); | |
169 | if (!writer) { | |
170 | ret = -LTTNG_ERR_NOMEM; | |
171 | goto end; | |
172 | } | |
173 | ||
174 | /* Open command element */ | |
175 | ret = mi_lttng_writer_command_open(writer, | |
176 | mi_lttng_element_command_load); | |
177 | if (ret) { | |
178 | ret = CMD_ERROR; | |
179 | goto end; | |
180 | } | |
181 | ||
182 | /* Open output element */ | |
183 | ret = mi_lttng_writer_open_element(writer, | |
184 | mi_lttng_element_command_output); | |
185 | if (ret) { | |
186 | ret = CMD_ERROR; | |
187 | goto end; | |
188 | } | |
189 | } | |
190 | ||
ea6ebdf0 JR |
191 | /* Prepare load attributes */ |
192 | session_attr = lttng_load_session_attr_create(); | |
193 | if (!session_attr) { | |
194 | ERR("Load session attributes creation failed"); | |
195 | ret = -LTTNG_ERR_NOMEM; | |
196 | goto end; | |
197 | } | |
198 | ||
199 | /* | |
200 | * Set the input url | |
201 | * lttng_load_session_attr_set_input_url support only absolute path. | |
202 | * Use realpath to resolve any relative path. | |
203 | * */ | |
204 | if (opt_input_path) { | |
205 | input_path = realpath(opt_input_path, NULL); | |
206 | if (!input_path) { | |
207 | PERROR("Input path is invalid"); | |
208 | } | |
209 | } else { | |
210 | input_path = NULL; | |
211 | } | |
212 | ||
213 | ret = lttng_load_session_attr_set_input_url(session_attr, | |
214 | input_path); | |
215 | if (ret) { | |
216 | ERR("Input path is invalid"); | |
217 | goto end; | |
218 | } | |
219 | ||
220 | /* Set the session name. NULL means all sessions should be loaded */ | |
221 | ret = lttng_load_session_attr_set_session_name(session_attr, | |
222 | session_name); | |
223 | if (ret) { | |
224 | ERR("Session name is invalid"); | |
225 | goto end; | |
226 | } | |
227 | ||
228 | /* Set the overwrite attribute */ | |
229 | ret = lttng_load_session_attr_set_overwrite(session_attr, opt_force); | |
230 | if (ret) { | |
231 | ERR("Force argument could not be applied"); | |
232 | goto end; | |
233 | } | |
234 | ||
235 | command_ret = lttng_load_session(session_attr); | |
1734c658 JRJ |
236 | if (command_ret) { |
237 | ERR("%s", lttng_strerror(command_ret)); | |
238 | success = 0; | |
279d6193 DG |
239 | } else { |
240 | if (opt_load_all) { | |
241 | MSG("All sessions have been loaded successfully"); | |
732b768a | 242 | } else if (session_name) { |
8e525b95 DG |
243 | ret = config_init((char *)session_name); |
244 | if (ret < 0) { | |
245 | ret = CMD_WARNING; | |
246 | } | |
279d6193 | 247 | MSG("Session %s has been loaded successfully", session_name); |
732b768a DG |
248 | } else { |
249 | MSG("Session has been loaded successfully"); | |
279d6193 | 250 | } |
1734c658 JRJ |
251 | success = 1; |
252 | } | |
253 | ||
254 | /* Mi Printing and closing */ | |
255 | if (lttng_opt_mi) { | |
256 | /* Mi print */ | |
257 | ret = mi_load_print(session_name); | |
258 | if (ret) { | |
259 | ret = CMD_ERROR; | |
260 | goto end; | |
261 | } | |
262 | ||
263 | /* Close output element */ | |
264 | ret = mi_lttng_writer_close_element(writer); | |
265 | if (ret) { | |
266 | ret = CMD_ERROR; | |
267 | goto end; | |
268 | } | |
269 | ||
270 | /* Success ? */ | |
271 | ret = mi_lttng_writer_write_element_bool(writer, | |
272 | mi_lttng_element_command_success, success); | |
273 | if (ret) { | |
274 | ret = CMD_ERROR; | |
275 | goto end; | |
276 | } | |
277 | ||
278 | /* Command element close */ | |
279 | ret = mi_lttng_writer_command_close(writer); | |
280 | if (ret) { | |
281 | ret = CMD_ERROR; | |
282 | goto end; | |
283 | } | |
8c42d845 JG |
284 | } |
285 | end: | |
1734c658 JRJ |
286 | if (writer && mi_lttng_writer_destroy(writer)) { |
287 | /* Preserve original error code */ | |
288 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; | |
289 | } | |
290 | ||
291 | /* Overwrite ret if the was an error with the load command */ | |
292 | ret = command_ret ? -command_ret : ret; | |
293 | ||
ea6ebdf0 JR |
294 | lttng_load_session_attr_destroy(session_attr); |
295 | free(input_path); | |
8c42d845 JG |
296 | poptFreeContext(pc); |
297 | return ret; | |
298 | } |