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