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