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