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 | ||
93ec662e JD |
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 | ||
fb288d1e | 78 | if (argc > 1) { |
85f8aaa1 | 79 | ret = CMD_UNDEFINED; |
fb288d1e JD |
80 | goto end; |
81 | } | |
eded6438 | 82 | ret = lttng_regenerate_metadata(session_name); |
93ec662e JD |
83 | if (ret == 0) { |
84 | MSG("Metadata successfully regenerated for session %s", session_name); | |
85f8aaa1 JG |
85 | } else { |
86 | ERR("%s", lttng_strerror(ret)); | |
93ec662e | 87 | } |
fb288d1e JD |
88 | |
89 | end: | |
93ec662e JD |
90 | return ret; |
91 | } | |
92 | ||
93 | static int handle_command(const char **argv) | |
94 | { | |
95 | struct cmd_struct *cmd; | |
96 | int ret = CMD_SUCCESS, i = 0, argc, command_ret = CMD_SUCCESS; | |
97 | ||
98 | if (argv == NULL) { | |
85f8aaa1 JG |
99 | ERR("No action specified for metadata command."); |
100 | ret = CMD_ERROR; | |
93ec662e JD |
101 | goto end; |
102 | } | |
103 | ||
104 | argc = count_arguments(argv); | |
105 | ||
106 | cmd = &actions[i]; | |
107 | while (cmd->func != NULL) { | |
108 | /* Find command */ | |
109 | if (strcmp(argv[0], cmd->name) == 0) { | |
54897b57 JD |
110 | if (lttng_opt_mi) { |
111 | /* Action element */ | |
112 | ret = mi_lttng_writer_open_element(writer, | |
113 | mi_lttng_element_command_metadata_action); | |
114 | if (ret) { | |
115 | ret = CMD_ERROR; | |
116 | goto end; | |
117 | } | |
118 | ||
119 | /* Name of the action */ | |
120 | ret = mi_lttng_writer_write_element_string(writer, | |
121 | config_element_name, argv[0]); | |
122 | if (ret) { | |
123 | ret = CMD_ERROR; | |
124 | goto end; | |
125 | } | |
126 | } | |
93ec662e | 127 | command_ret = cmd->func(argc, argv); |
54897b57 JD |
128 | if (lttng_opt_mi) { |
129 | /* Close output and action element */ | |
130 | ret = mi_lttng_writer_close_element(writer); | |
131 | if (ret) { | |
132 | ret = CMD_ERROR; | |
133 | goto end; | |
134 | } | |
135 | } | |
93ec662e JD |
136 | goto end; |
137 | } | |
138 | ||
139 | cmd = &actions[i++]; | |
140 | } | |
141 | ||
142 | ret = CMD_UNDEFINED; | |
143 | ||
144 | end: | |
145 | /* Overwrite ret if an error occurred in cmd->func() */ | |
146 | ret = command_ret ? command_ret : ret; | |
147 | return ret; | |
148 | } | |
149 | ||
150 | /* | |
151 | * Metadata command handling. | |
152 | */ | |
153 | int cmd_metadata(int argc, const char **argv) | |
154 | { | |
54897b57 | 155 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
93ec662e JD |
156 | static poptContext pc; |
157 | ||
158 | if (argc < 1) { | |
3b9c7d3f | 159 | SHOW_HELP(); |
93ec662e JD |
160 | ret = CMD_ERROR; |
161 | goto end; | |
162 | } | |
163 | ||
164 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
165 | poptReadDefaultConfig(pc, 0); | |
166 | ||
54897b57 JD |
167 | if (lttng_opt_mi) { |
168 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); | |
169 | if (!writer) { | |
170 | ret = -LTTNG_ERR_NOMEM; | |
171 | goto end; | |
172 | } | |
173 | /* Open command element */ | |
174 | ret = mi_lttng_writer_command_open(writer, | |
175 | mi_lttng_element_command_metadata); | |
176 | if (ret) { | |
177 | ret = CMD_ERROR; | |
178 | goto end; | |
179 | } | |
180 | ||
181 | /* Open output element */ | |
182 | ret = mi_lttng_writer_open_element(writer, | |
183 | mi_lttng_element_command_output); | |
184 | if (ret) { | |
185 | ret = CMD_ERROR; | |
186 | goto end; | |
187 | } | |
188 | } | |
189 | ||
93ec662e JD |
190 | while ((opt = poptGetNextOpt(pc)) != -1) { |
191 | switch (opt) { | |
192 | case OPT_HELP: | |
3b9c7d3f | 193 | SHOW_HELP(); |
93ec662e JD |
194 | goto end; |
195 | case OPT_LIST_OPTIONS: | |
196 | list_cmd_options(stdout, long_options); | |
197 | goto end; | |
198 | case OPT_LIST_COMMANDS: | |
199 | list_commands(actions, stdout); | |
200 | goto end; | |
201 | default: | |
3b9c7d3f | 202 | SHOW_HELP(); |
93ec662e JD |
203 | ret = CMD_UNDEFINED; |
204 | goto end; | |
205 | } | |
206 | } | |
207 | ||
208 | if (!opt_session_name) { | |
209 | session_name = get_session_name(); | |
210 | if (session_name == NULL) { | |
211 | ret = CMD_ERROR; | |
212 | goto end; | |
213 | } | |
214 | } else { | |
215 | session_name = opt_session_name; | |
216 | } | |
217 | ||
218 | command_ret = handle_command(poptGetArgs(pc)); | |
219 | if (command_ret) { | |
85f8aaa1 | 220 | success = 0; |
93ec662e JD |
221 | } |
222 | ||
54897b57 JD |
223 | if (lttng_opt_mi) { |
224 | /* Close output element */ | |
225 | ret = mi_lttng_writer_close_element(writer); | |
226 | if (ret) { | |
227 | ret = CMD_ERROR; | |
228 | goto end; | |
229 | } | |
230 | ||
231 | /* Success ? */ | |
232 | ret = mi_lttng_writer_write_element_bool(writer, | |
233 | mi_lttng_element_command_success, success); | |
234 | if (ret) { | |
235 | ret = CMD_ERROR; | |
236 | goto end; | |
237 | } | |
238 | ||
239 | /* Command element close */ | |
240 | ret = mi_lttng_writer_command_close(writer); | |
241 | if (ret) { | |
242 | ret = CMD_ERROR; | |
243 | goto end; | |
244 | } | |
245 | } | |
246 | ||
93ec662e | 247 | end: |
54897b57 JD |
248 | /* Mi clean-up */ |
249 | if (writer && mi_lttng_writer_destroy(writer)) { | |
250 | /* Preserve original error code */ | |
251 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; | |
252 | } | |
253 | ||
93ec662e JD |
254 | if (!opt_session_name) { |
255 | free(session_name); | |
256 | } | |
257 | ||
258 | /* Overwrite ret if an error occurred during handle_command() */ | |
259 | ret = command_ret ? command_ret : ret; | |
260 | ||
261 | poptFreeContext(pc); | |
262 | return ret; | |
263 | } |