2 * Copyright (C) 2015 Julien Desfossez <jdesfossez@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
9 #include "../command.hpp"
11 #include <common/mi-lttng.hpp>
20 static char *opt_session_name
;
21 static char *session_name
= nullptr;
23 static int metadata_regenerate(int argc
, const char **argv
);
25 #ifdef LTTNG_EMBED_HELP
26 static const char help_msg
[] =
27 #include <lttng-metadata.1.h>
37 static struct mi_writer
*writer
;
39 static struct poptOption long_options
[] = {
40 /* { longName, shortName, argInfo, argPtr, value, descrip, argDesc, } */
50 { "session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, nullptr, nullptr },
60 { "list-commands", 0, POPT_ARG_NONE
, nullptr, OPT_LIST_COMMANDS
, nullptr, nullptr },
72 static struct cmd_struct actions
[] = {
73 { "regenerate", metadata_regenerate
}, { nullptr, nullptr } /* Array closure */
77 * Count and return the number of arguments in argv.
79 static int count_arguments(const char **argv
)
85 while (argv
[i
] != nullptr) {
92 static int metadata_regenerate(int argc
, const char **argv
__attribute__((unused
)))
100 ret
= lttng_regenerate_metadata(session_name
);
102 MSG("Metadata successfully regenerated for session %s", session_name
);
104 ERR("%s", lttng_strerror(ret
));
111 static int handle_command(const char **argv
)
113 struct cmd_struct
*cmd
;
114 int ret
= CMD_SUCCESS
, i
= 0, argc
, command_ret
= CMD_SUCCESS
;
116 if (argv
== nullptr) {
117 ERR("No action specified for metadata command.");
122 argc
= count_arguments(argv
);
123 LTTNG_ASSERT(argc
>= 1);
126 while (cmd
->func
!= nullptr) {
128 if (strcmp(argv
[0], cmd
->name
) == 0) {
131 ret
= mi_lttng_writer_open_element(
132 writer
, mi_lttng_element_command_metadata_action
);
138 /* Name of the action */
139 ret
= mi_lttng_writer_write_element_string(
140 writer
, config_element_name
, argv
[0]);
146 command_ret
= cmd
->func(argc
, argv
);
148 /* Close output and action element */
149 ret
= mi_lttng_writer_close_element(writer
);
164 /* Overwrite ret if an error occurred in cmd->func() */
165 ret
= command_ret
? command_ret
: ret
;
170 * Metadata command handling.
172 int cmd_metadata(int argc
, const char **argv
)
174 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
175 static poptContext pc
;
183 pc
= poptGetContext(nullptr, argc
, argv
, long_options
, 0);
184 poptReadDefaultConfig(pc
, 0);
187 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
189 ret
= -LTTNG_ERR_NOMEM
;
192 /* Open command element */
193 ret
= mi_lttng_writer_command_open(writer
, mi_lttng_element_command_metadata
);
199 /* Open output element */
200 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_command_output
);
207 while ((opt
= poptGetNextOpt(pc
)) != -1) {
212 case OPT_LIST_OPTIONS
:
213 list_cmd_options(stdout
, long_options
);
215 case OPT_LIST_COMMANDS
:
216 list_commands(actions
, stdout
);
225 if (!opt_session_name
) {
226 session_name
= get_session_name();
227 if (session_name
== nullptr) {
232 session_name
= opt_session_name
;
235 command_ret
= handle_command(poptGetArgs(pc
));
241 /* Close output element */
242 ret
= mi_lttng_writer_close_element(writer
);
249 ret
= mi_lttng_writer_write_element_bool(
250 writer
, mi_lttng_element_command_success
, success
);
256 /* Command element close */
257 ret
= mi_lttng_writer_command_close(writer
);
266 if (writer
&& mi_lttng_writer_destroy(writer
)) {
267 /* Preserve original error code */
268 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
271 if (!opt_session_name
) {
275 /* Overwrite ret if an error occurred during handle_command() */
276 ret
= command_ret
? command_ret
: ret
;