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