Commit | Line | Data |
---|---|---|
93ec662e | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2015 Julien Desfossez <jdesfossez@efficios.com> |
93ec662e | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
93ec662e | 5 | * |
93ec662e JD |
6 | */ |
7 | ||
8 | #define _LGPL_SOURCE | |
28ab034a JG |
9 | #include "../command.hpp" |
10 | ||
11 | #include <common/mi-lttng.hpp> | |
12 | ||
93ec662e JD |
13 | #include <ctype.h> |
14 | #include <popt.h> | |
15 | #include <stdio.h> | |
16 | #include <stdlib.h> | |
17 | #include <string.h> | |
18 | #include <unistd.h> | |
19 | ||
93ec662e | 20 | static char *opt_session_name; |
cd9adb8b | 21 | static char *session_name = nullptr; |
93ec662e JD |
22 | |
23 | static int metadata_regenerate(int argc, const char **argv); | |
24 | ||
4fc83d94 PP |
25 | #ifdef LTTNG_EMBED_HELP |
26 | static const char help_msg[] = | |
27 | #include <lttng-metadata.1.h> | |
28ab034a | 28 | ; |
4fc83d94 PP |
29 | #endif |
30 | ||
93ec662e JD |
31 | enum { |
32 | OPT_HELP = 1, | |
33 | OPT_LIST_OPTIONS, | |
34 | OPT_LIST_COMMANDS, | |
35 | }; | |
36 | ||
54897b57 JD |
37 | static struct mi_writer *writer; |
38 | ||
93ec662e JD |
39 | static struct poptOption long_options[] = { |
40 | /* { longName, shortName, argInfo, argPtr, value, descrip, argDesc, } */ | |
28ab034a JG |
41 | { |
42 | "help", | |
43 | 'h', | |
44 | POPT_ARG_NONE, | |
cd9adb8b | 45 | nullptr, |
28ab034a | 46 | OPT_HELP, |
cd9adb8b JG |
47 | nullptr, |
48 | nullptr, | |
28ab034a | 49 | }, |
cd9adb8b | 50 | { "session", 's', POPT_ARG_STRING, &opt_session_name, 0, nullptr, nullptr }, |
28ab034a JG |
51 | { |
52 | "list-options", | |
53 | 0, | |
54 | POPT_ARG_NONE, | |
cd9adb8b | 55 | nullptr, |
28ab034a | 56 | OPT_LIST_OPTIONS, |
cd9adb8b JG |
57 | nullptr, |
58 | nullptr, | |
28ab034a | 59 | }, |
cd9adb8b | 60 | { "list-commands", 0, POPT_ARG_NONE, nullptr, OPT_LIST_COMMANDS, nullptr, nullptr }, |
28ab034a | 61 | { |
cd9adb8b | 62 | nullptr, |
28ab034a JG |
63 | 0, |
64 | 0, | |
cd9adb8b | 65 | nullptr, |
28ab034a | 66 | 0, |
cd9adb8b JG |
67 | nullptr, |
68 | nullptr, | |
28ab034a | 69 | }, |
93ec662e JD |
70 | }; |
71 | ||
72 | static struct cmd_struct actions[] = { | |
cd9adb8b | 73 | { "regenerate", metadata_regenerate }, { nullptr, nullptr } /* Array closure */ |
93ec662e JD |
74 | }; |
75 | ||
93ec662e JD |
76 | /* |
77 | * Count and return the number of arguments in argv. | |
78 | */ | |
79 | static int count_arguments(const char **argv) | |
80 | { | |
81 | int i = 0; | |
82 | ||
a0377dfe | 83 | LTTNG_ASSERT(argv); |
93ec662e | 84 | |
cd9adb8b | 85 | while (argv[i] != nullptr) { |
93ec662e JD |
86 | i++; |
87 | } | |
88 | ||
89 | return i; | |
90 | } | |
91 | ||
28ab034a | 92 | static int metadata_regenerate(int argc, const char **argv __attribute__((unused))) |
93ec662e JD |
93 | { |
94 | int ret; | |
95 | ||
fb288d1e | 96 | if (argc > 1) { |
c86592f2 | 97 | ret = CMD_UNDEFINED; |
fb288d1e JD |
98 | goto end; |
99 | } | |
eded6438 | 100 | ret = lttng_regenerate_metadata(session_name); |
93ec662e JD |
101 | if (ret == 0) { |
102 | MSG("Metadata successfully regenerated for session %s", session_name); | |
c86592f2 JG |
103 | } else { |
104 | ERR("%s", lttng_strerror(ret)); | |
93ec662e | 105 | } |
fb288d1e JD |
106 | |
107 | end: | |
93ec662e JD |
108 | return ret; |
109 | } | |
110 | ||
111 | static int handle_command(const char **argv) | |
112 | { | |
113 | struct cmd_struct *cmd; | |
114 | int ret = CMD_SUCCESS, i = 0, argc, command_ret = CMD_SUCCESS; | |
115 | ||
cd9adb8b | 116 | if (argv == nullptr) { |
c86592f2 JG |
117 | ERR("No action specified for metadata command."); |
118 | ret = CMD_ERROR; | |
93ec662e JD |
119 | goto end; |
120 | } | |
121 | ||
122 | argc = count_arguments(argv); | |
a0377dfe | 123 | LTTNG_ASSERT(argc >= 1); |
93ec662e JD |
124 | |
125 | cmd = &actions[i]; | |
cd9adb8b | 126 | while (cmd->func != nullptr) { |
93ec662e JD |
127 | /* Find command */ |
128 | if (strcmp(argv[0], cmd->name) == 0) { | |
54897b57 JD |
129 | if (lttng_opt_mi) { |
130 | /* Action element */ | |
28ab034a JG |
131 | ret = mi_lttng_writer_open_element( |
132 | writer, mi_lttng_element_command_metadata_action); | |
54897b57 JD |
133 | if (ret) { |
134 | ret = CMD_ERROR; | |
135 | goto end; | |
136 | } | |
137 | ||
138 | /* Name of the action */ | |
28ab034a JG |
139 | ret = mi_lttng_writer_write_element_string( |
140 | writer, config_element_name, argv[0]); | |
54897b57 JD |
141 | if (ret) { |
142 | ret = CMD_ERROR; | |
143 | goto end; | |
144 | } | |
145 | } | |
93ec662e | 146 | command_ret = cmd->func(argc, argv); |
54897b57 JD |
147 | if (lttng_opt_mi) { |
148 | /* Close output and action element */ | |
149 | ret = mi_lttng_writer_close_element(writer); | |
150 | if (ret) { | |
151 | ret = CMD_ERROR; | |
152 | goto end; | |
153 | } | |
154 | } | |
93ec662e JD |
155 | goto end; |
156 | } | |
157 | ||
158 | cmd = &actions[i++]; | |
159 | } | |
160 | ||
161 | ret = CMD_UNDEFINED; | |
162 | ||
163 | end: | |
164 | /* Overwrite ret if an error occurred in cmd->func() */ | |
165 | ret = command_ret ? command_ret : ret; | |
166 | return ret; | |
167 | } | |
168 | ||
169 | /* | |
170 | * Metadata command handling. | |
171 | */ | |
172 | int cmd_metadata(int argc, const char **argv) | |
173 | { | |
54897b57 | 174 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
93ec662e JD |
175 | static poptContext pc; |
176 | ||
177 | if (argc < 1) { | |
3b9c7d3f | 178 | SHOW_HELP(); |
93ec662e JD |
179 | ret = CMD_ERROR; |
180 | goto end; | |
181 | } | |
182 | ||
cd9adb8b | 183 | pc = poptGetContext(nullptr, argc, argv, long_options, 0); |
93ec662e JD |
184 | poptReadDefaultConfig(pc, 0); |
185 | ||
54897b57 JD |
186 | if (lttng_opt_mi) { |
187 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); | |
188 | if (!writer) { | |
189 | ret = -LTTNG_ERR_NOMEM; | |
190 | goto end; | |
191 | } | |
192 | /* Open command element */ | |
28ab034a | 193 | ret = mi_lttng_writer_command_open(writer, mi_lttng_element_command_metadata); |
54897b57 JD |
194 | if (ret) { |
195 | ret = CMD_ERROR; | |
196 | goto end; | |
197 | } | |
198 | ||
199 | /* Open output element */ | |
28ab034a | 200 | ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command_output); |
54897b57 JD |
201 | if (ret) { |
202 | ret = CMD_ERROR; | |
203 | goto end; | |
204 | } | |
205 | } | |
206 | ||
93ec662e JD |
207 | while ((opt = poptGetNextOpt(pc)) != -1) { |
208 | switch (opt) { | |
209 | case OPT_HELP: | |
3b9c7d3f | 210 | SHOW_HELP(); |
93ec662e JD |
211 | goto end; |
212 | case OPT_LIST_OPTIONS: | |
213 | list_cmd_options(stdout, long_options); | |
214 | goto end; | |
215 | case OPT_LIST_COMMANDS: | |
216 | list_commands(actions, stdout); | |
217 | goto end; | |
218 | default: | |
3b9c7d3f | 219 | SHOW_HELP(); |
93ec662e JD |
220 | ret = CMD_UNDEFINED; |
221 | goto end; | |
222 | } | |
223 | } | |
224 | ||
225 | if (!opt_session_name) { | |
226 | session_name = get_session_name(); | |
cd9adb8b | 227 | if (session_name == nullptr) { |
93ec662e JD |
228 | ret = CMD_ERROR; |
229 | goto end; | |
230 | } | |
231 | } else { | |
232 | session_name = opt_session_name; | |
233 | } | |
234 | ||
235 | command_ret = handle_command(poptGetArgs(pc)); | |
236 | if (command_ret) { | |
c86592f2 | 237 | success = 0; |
93ec662e JD |
238 | } |
239 | ||
54897b57 JD |
240 | if (lttng_opt_mi) { |
241 | /* Close output element */ | |
242 | ret = mi_lttng_writer_close_element(writer); | |
243 | if (ret) { | |
244 | ret = CMD_ERROR; | |
245 | goto end; | |
246 | } | |
247 | ||
248 | /* Success ? */ | |
28ab034a JG |
249 | ret = mi_lttng_writer_write_element_bool( |
250 | writer, mi_lttng_element_command_success, success); | |
54897b57 JD |
251 | if (ret) { |
252 | ret = CMD_ERROR; | |
253 | goto end; | |
254 | } | |
255 | ||
256 | /* Command element close */ | |
257 | ret = mi_lttng_writer_command_close(writer); | |
258 | if (ret) { | |
259 | ret = CMD_ERROR; | |
260 | goto end; | |
261 | } | |
262 | } | |
263 | ||
93ec662e | 264 | end: |
54897b57 JD |
265 | /* Mi clean-up */ |
266 | if (writer && mi_lttng_writer_destroy(writer)) { | |
267 | /* Preserve original error code */ | |
268 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; | |
269 | } | |
270 | ||
93ec662e JD |
271 | if (!opt_session_name) { |
272 | free(session_name); | |
273 | } | |
274 | ||
275 | /* Overwrite ret if an error occurred during handle_command() */ | |
276 | ret = command_ret ? command_ret : ret; | |
277 | ||
278 | poptFreeContext(pc); | |
279 | return ret; | |
280 | } |