Commit | Line | Data |
---|---|---|
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 | ||
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 | ||
54897b57 JD |
42 | static struct mi_writer *writer; |
43 | ||
93ec662e JD |
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 | * usage | |
60 | */ | |
61 | static void usage(FILE *ofp) | |
62 | { | |
63 | fprintf(ofp, "usage: lttng metadata [OPTION] ACTION\n"); | |
64 | fprintf(ofp, "\n"); | |
65 | fprintf(ofp, "Actions:\n"); | |
66 | fprintf(ofp, " regenerate\n"); | |
67 | fprintf(ofp, " Regenerate and overwrite the metadata of the session.\n"); | |
68 | fprintf(ofp, "Options:\n"); | |
69 | fprintf(ofp, " -h, --help Show this help.\n"); | |
70 | fprintf(ofp, " --list-options Simple listing of options.\n"); | |
71 | fprintf(ofp, " -s, --session NAME Apply to session name.\n"); | |
72 | fprintf(ofp, "\n"); | |
73 | } | |
74 | ||
75 | /* | |
76 | * Count and return the number of arguments in argv. | |
77 | */ | |
78 | static int count_arguments(const char **argv) | |
79 | { | |
80 | int i = 0; | |
81 | ||
82 | assert(argv); | |
83 | ||
84 | while (argv[i] != NULL) { | |
85 | i++; | |
86 | } | |
87 | ||
88 | return i; | |
89 | } | |
90 | ||
91 | static int metadata_regenerate(int argc, const char **argv) | |
92 | { | |
93 | int ret; | |
94 | ||
95 | ret = lttng_metadata_regenerate(session_name); | |
96 | if (ret == 0) { | |
97 | MSG("Metadata successfully regenerated for session %s", session_name); | |
98 | } | |
99 | return ret; | |
100 | } | |
101 | ||
102 | static int handle_command(const char **argv) | |
103 | { | |
104 | struct cmd_struct *cmd; | |
105 | int ret = CMD_SUCCESS, i = 0, argc, command_ret = CMD_SUCCESS; | |
106 | ||
107 | if (argv == NULL) { | |
108 | usage(stderr); | |
109 | command_ret = CMD_ERROR; | |
110 | goto end; | |
111 | } | |
112 | ||
113 | argc = count_arguments(argv); | |
114 | ||
115 | cmd = &actions[i]; | |
116 | while (cmd->func != NULL) { | |
117 | /* Find command */ | |
118 | if (strcmp(argv[0], cmd->name) == 0) { | |
54897b57 JD |
119 | if (lttng_opt_mi) { |
120 | /* Action element */ | |
121 | ret = mi_lttng_writer_open_element(writer, | |
122 | mi_lttng_element_command_metadata_action); | |
123 | if (ret) { | |
124 | ret = CMD_ERROR; | |
125 | goto end; | |
126 | } | |
127 | ||
128 | /* Name of the action */ | |
129 | ret = mi_lttng_writer_write_element_string(writer, | |
130 | config_element_name, argv[0]); | |
131 | if (ret) { | |
132 | ret = CMD_ERROR; | |
133 | goto end; | |
134 | } | |
135 | } | |
93ec662e | 136 | command_ret = cmd->func(argc, argv); |
54897b57 JD |
137 | if (lttng_opt_mi) { |
138 | /* Close output and action element */ | |
139 | ret = mi_lttng_writer_close_element(writer); | |
140 | if (ret) { | |
141 | ret = CMD_ERROR; | |
142 | goto end; | |
143 | } | |
144 | } | |
93ec662e JD |
145 | goto end; |
146 | } | |
147 | ||
148 | cmd = &actions[i++]; | |
149 | } | |
150 | ||
151 | ret = CMD_UNDEFINED; | |
152 | ||
153 | end: | |
154 | /* Overwrite ret if an error occurred in cmd->func() */ | |
155 | ret = command_ret ? command_ret : ret; | |
156 | return ret; | |
157 | } | |
158 | ||
159 | /* | |
160 | * Metadata command handling. | |
161 | */ | |
162 | int cmd_metadata(int argc, const char **argv) | |
163 | { | |
54897b57 | 164 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
93ec662e JD |
165 | static poptContext pc; |
166 | ||
167 | if (argc < 1) { | |
168 | usage(stderr); | |
169 | ret = CMD_ERROR; | |
170 | goto end; | |
171 | } | |
172 | ||
173 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
174 | poptReadDefaultConfig(pc, 0); | |
175 | ||
54897b57 JD |
176 | if (lttng_opt_mi) { |
177 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); | |
178 | if (!writer) { | |
179 | ret = -LTTNG_ERR_NOMEM; | |
180 | goto end; | |
181 | } | |
182 | /* Open command element */ | |
183 | ret = mi_lttng_writer_command_open(writer, | |
184 | mi_lttng_element_command_metadata); | |
185 | if (ret) { | |
186 | ret = CMD_ERROR; | |
187 | goto end; | |
188 | } | |
189 | ||
190 | /* Open output element */ | |
191 | ret = mi_lttng_writer_open_element(writer, | |
192 | mi_lttng_element_command_output); | |
193 | if (ret) { | |
194 | ret = CMD_ERROR; | |
195 | goto end; | |
196 | } | |
197 | } | |
198 | ||
93ec662e JD |
199 | while ((opt = poptGetNextOpt(pc)) != -1) { |
200 | switch (opt) { | |
201 | case OPT_HELP: | |
202 | usage(stdout); | |
203 | goto end; | |
204 | case OPT_LIST_OPTIONS: | |
205 | list_cmd_options(stdout, long_options); | |
206 | goto end; | |
207 | case OPT_LIST_COMMANDS: | |
208 | list_commands(actions, stdout); | |
209 | goto end; | |
210 | default: | |
211 | usage(stderr); | |
212 | ret = CMD_UNDEFINED; | |
213 | goto end; | |
214 | } | |
215 | } | |
216 | ||
217 | if (!opt_session_name) { | |
218 | session_name = get_session_name(); | |
219 | if (session_name == NULL) { | |
220 | ret = CMD_ERROR; | |
221 | goto end; | |
222 | } | |
223 | } else { | |
224 | session_name = opt_session_name; | |
225 | } | |
226 | ||
227 | command_ret = handle_command(poptGetArgs(pc)); | |
228 | if (command_ret) { | |
229 | switch (-command_ret) { | |
230 | default: | |
231 | ERR("%s", lttng_strerror(command_ret)); | |
54897b57 | 232 | success = 0; |
93ec662e JD |
233 | break; |
234 | } | |
235 | } | |
236 | ||
54897b57 JD |
237 | if (lttng_opt_mi) { |
238 | /* Close output element */ | |
239 | ret = mi_lttng_writer_close_element(writer); | |
240 | if (ret) { | |
241 | ret = CMD_ERROR; | |
242 | goto end; | |
243 | } | |
244 | ||
245 | /* Success ? */ | |
246 | ret = mi_lttng_writer_write_element_bool(writer, | |
247 | mi_lttng_element_command_success, success); | |
248 | if (ret) { | |
249 | ret = CMD_ERROR; | |
250 | goto end; | |
251 | } | |
252 | ||
253 | /* Command element close */ | |
254 | ret = mi_lttng_writer_command_close(writer); | |
255 | if (ret) { | |
256 | ret = CMD_ERROR; | |
257 | goto end; | |
258 | } | |
259 | } | |
260 | ||
93ec662e | 261 | end: |
54897b57 JD |
262 | /* Mi clean-up */ |
263 | if (writer && mi_lttng_writer_destroy(writer)) { | |
264 | /* Preserve original error code */ | |
265 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; | |
266 | } | |
267 | ||
93ec662e JD |
268 | if (!opt_session_name) { |
269 | free(session_name); | |
270 | } | |
271 | ||
272 | /* Overwrite ret if an error occurred during handle_command() */ | |
273 | ret = command_ret ? command_ret : ret; | |
274 | ||
275 | poptFreeContext(pc); | |
276 | return ret; | |
277 | } |