Sync ax_have_epoll.m4 with autoconf-archive
[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);
9927c13b 111 assert(argc >= 1);
93ec662e
JD
112
113 cmd = &actions[i];
114 while (cmd->func != NULL) {
115 /* Find command */
116 if (strcmp(argv[0], cmd->name) == 0) {
54897b57
JD
117 if (lttng_opt_mi) {
118 /* Action element */
119 ret = mi_lttng_writer_open_element(writer,
120 mi_lttng_element_command_metadata_action);
121 if (ret) {
122 ret = CMD_ERROR;
123 goto end;
124 }
125
126 /* Name of the action */
127 ret = mi_lttng_writer_write_element_string(writer,
128 config_element_name, argv[0]);
129 if (ret) {
130 ret = CMD_ERROR;
131 goto end;
132 }
133 }
93ec662e 134 command_ret = cmd->func(argc, argv);
54897b57
JD
135 if (lttng_opt_mi) {
136 /* Close output and action element */
137 ret = mi_lttng_writer_close_element(writer);
138 if (ret) {
139 ret = CMD_ERROR;
140 goto end;
141 }
142 }
93ec662e
JD
143 goto end;
144 }
145
146 cmd = &actions[i++];
147 }
148
149 ret = CMD_UNDEFINED;
150
151end:
152 /* Overwrite ret if an error occurred in cmd->func() */
153 ret = command_ret ? command_ret : ret;
154 return ret;
155}
156
157/*
158 * Metadata command handling.
159 */
160int cmd_metadata(int argc, const char **argv)
161{
54897b57 162 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
93ec662e
JD
163 static poptContext pc;
164
165 if (argc < 1) {
3b9c7d3f 166 SHOW_HELP();
93ec662e
JD
167 ret = CMD_ERROR;
168 goto end;
169 }
170
171 pc = poptGetContext(NULL, argc, argv, long_options, 0);
172 poptReadDefaultConfig(pc, 0);
173
54897b57
JD
174 if (lttng_opt_mi) {
175 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
176 if (!writer) {
177 ret = -LTTNG_ERR_NOMEM;
178 goto end;
179 }
180 /* Open command element */
181 ret = mi_lttng_writer_command_open(writer,
182 mi_lttng_element_command_metadata);
183 if (ret) {
184 ret = CMD_ERROR;
185 goto end;
186 }
187
188 /* Open output element */
189 ret = mi_lttng_writer_open_element(writer,
190 mi_lttng_element_command_output);
191 if (ret) {
192 ret = CMD_ERROR;
193 goto end;
194 }
195 }
196
93ec662e
JD
197 while ((opt = poptGetNextOpt(pc)) != -1) {
198 switch (opt) {
199 case OPT_HELP:
3b9c7d3f 200 SHOW_HELP();
93ec662e
JD
201 goto end;
202 case OPT_LIST_OPTIONS:
203 list_cmd_options(stdout, long_options);
204 goto end;
205 case OPT_LIST_COMMANDS:
206 list_commands(actions, stdout);
207 goto end;
208 default:
3b9c7d3f 209 SHOW_HELP();
93ec662e
JD
210 ret = CMD_UNDEFINED;
211 goto end;
212 }
213 }
214
215 if (!opt_session_name) {
216 session_name = get_session_name();
217 if (session_name == NULL) {
218 ret = CMD_ERROR;
219 goto end;
220 }
221 } else {
222 session_name = opt_session_name;
223 }
224
225 command_ret = handle_command(poptGetArgs(pc));
226 if (command_ret) {
c86592f2 227 success = 0;
93ec662e
JD
228 }
229
54897b57
JD
230 if (lttng_opt_mi) {
231 /* Close output element */
232 ret = mi_lttng_writer_close_element(writer);
233 if (ret) {
234 ret = CMD_ERROR;
235 goto end;
236 }
237
238 /* Success ? */
239 ret = mi_lttng_writer_write_element_bool(writer,
240 mi_lttng_element_command_success, success);
241 if (ret) {
242 ret = CMD_ERROR;
243 goto end;
244 }
245
246 /* Command element close */
247 ret = mi_lttng_writer_command_close(writer);
248 if (ret) {
249 ret = CMD_ERROR;
250 goto end;
251 }
252 }
253
93ec662e 254end:
54897b57
JD
255 /* Mi clean-up */
256 if (writer && mi_lttng_writer_destroy(writer)) {
257 /* Preserve original error code */
258 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
259 }
260
93ec662e
JD
261 if (!opt_session_name) {
262 free(session_name);
263 }
264
265 /* Overwrite ret if an error occurred during handle_command() */
266 ret = command_ret ? command_ret : ret;
267
268 poptFreeContext(pc);
269 return ret;
270}
This page took 0.043606 seconds and 4 git commands to generate.