argpar: sync with upstream - adjust to iterator API
[lttng-tools.git] / src / bin / lttng / commands / metadata.cpp
1 /*
2 * Copyright (C) 2015 Julien Desfossez <jdesfossez@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #define _LGPL_SOURCE
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
16 #include <common/mi-lttng.h>
17
18 #include "../command.h"
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
25 #ifdef LTTNG_EMBED_HELP
26 static const char help_msg[] =
27 #include <lttng-metadata.1.h>
28 ;
29 #endif
30
31 enum {
32 OPT_HELP = 1,
33 OPT_LIST_OPTIONS,
34 OPT_LIST_COMMANDS,
35 };
36
37 static struct mi_writer *writer;
38
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, },
44 { "list-commands", 0, POPT_ARG_NONE, NULL, OPT_LIST_COMMANDS},
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
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
60 LTTNG_ASSERT(argv);
61
62 while (argv[i] != NULL) {
63 i++;
64 }
65
66 return i;
67 }
68
69 static int metadata_regenerate(int argc, const char **argv)
70 {
71 int ret;
72
73 if (argc > 1) {
74 ret = CMD_UNDEFINED;
75 goto end;
76 }
77 ret = lttng_regenerate_metadata(session_name);
78 if (ret == 0) {
79 MSG("Metadata successfully regenerated for session %s", session_name);
80 } else {
81 ERR("%s", lttng_strerror(ret));
82 }
83
84 end:
85 return ret;
86 }
87
88 static int handle_command(const char **argv)
89 {
90 struct cmd_struct *cmd;
91 int ret = CMD_SUCCESS, i = 0, argc, command_ret = CMD_SUCCESS;
92
93 if (argv == NULL) {
94 ERR("No action specified for metadata command.");
95 ret = CMD_ERROR;
96 goto end;
97 }
98
99 argc = count_arguments(argv);
100 LTTNG_ASSERT(argc >= 1);
101
102 cmd = &actions[i];
103 while (cmd->func != NULL) {
104 /* Find command */
105 if (strcmp(argv[0], cmd->name) == 0) {
106 if (lttng_opt_mi) {
107 /* Action element */
108 ret = mi_lttng_writer_open_element(writer,
109 mi_lttng_element_command_metadata_action);
110 if (ret) {
111 ret = CMD_ERROR;
112 goto end;
113 }
114
115 /* Name of the action */
116 ret = mi_lttng_writer_write_element_string(writer,
117 config_element_name, argv[0]);
118 if (ret) {
119 ret = CMD_ERROR;
120 goto end;
121 }
122 }
123 command_ret = cmd->func(argc, argv);
124 if (lttng_opt_mi) {
125 /* Close output and action element */
126 ret = mi_lttng_writer_close_element(writer);
127 if (ret) {
128 ret = CMD_ERROR;
129 goto end;
130 }
131 }
132 goto end;
133 }
134
135 cmd = &actions[i++];
136 }
137
138 ret = CMD_UNDEFINED;
139
140 end:
141 /* Overwrite ret if an error occurred in cmd->func() */
142 ret = command_ret ? command_ret : ret;
143 return ret;
144 }
145
146 /*
147 * Metadata command handling.
148 */
149 int cmd_metadata(int argc, const char **argv)
150 {
151 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
152 static poptContext pc;
153
154 if (argc < 1) {
155 SHOW_HELP();
156 ret = CMD_ERROR;
157 goto end;
158 }
159
160 pc = poptGetContext(NULL, argc, argv, long_options, 0);
161 poptReadDefaultConfig(pc, 0);
162
163 if (lttng_opt_mi) {
164 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
165 if (!writer) {
166 ret = -LTTNG_ERR_NOMEM;
167 goto end;
168 }
169 /* Open command element */
170 ret = mi_lttng_writer_command_open(writer,
171 mi_lttng_element_command_metadata);
172 if (ret) {
173 ret = CMD_ERROR;
174 goto end;
175 }
176
177 /* Open output element */
178 ret = mi_lttng_writer_open_element(writer,
179 mi_lttng_element_command_output);
180 if (ret) {
181 ret = CMD_ERROR;
182 goto end;
183 }
184 }
185
186 while ((opt = poptGetNextOpt(pc)) != -1) {
187 switch (opt) {
188 case OPT_HELP:
189 SHOW_HELP();
190 goto end;
191 case OPT_LIST_OPTIONS:
192 list_cmd_options(stdout, long_options);
193 goto end;
194 case OPT_LIST_COMMANDS:
195 list_commands(actions, stdout);
196 goto end;
197 default:
198 SHOW_HELP();
199 ret = CMD_UNDEFINED;
200 goto end;
201 }
202 }
203
204 if (!opt_session_name) {
205 session_name = get_session_name();
206 if (session_name == NULL) {
207 ret = CMD_ERROR;
208 goto end;
209 }
210 } else {
211 session_name = opt_session_name;
212 }
213
214 command_ret = handle_command(poptGetArgs(pc));
215 if (command_ret) {
216 success = 0;
217 }
218
219 if (lttng_opt_mi) {
220 /* Close output element */
221 ret = mi_lttng_writer_close_element(writer);
222 if (ret) {
223 ret = CMD_ERROR;
224 goto end;
225 }
226
227 /* Success ? */
228 ret = mi_lttng_writer_write_element_bool(writer,
229 mi_lttng_element_command_success, success);
230 if (ret) {
231 ret = CMD_ERROR;
232 goto end;
233 }
234
235 /* Command element close */
236 ret = mi_lttng_writer_command_close(writer);
237 if (ret) {
238 ret = CMD_ERROR;
239 goto end;
240 }
241 }
242
243 end:
244 /* Mi clean-up */
245 if (writer && mi_lttng_writer_destroy(writer)) {
246 /* Preserve original error code */
247 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
248 }
249
250 if (!opt_session_name) {
251 free(session_name);
252 }
253
254 /* Overwrite ret if an error occurred during handle_command() */
255 ret = command_ret ? command_ret : ret;
256
257 poptFreeContext(pc);
258 return ret;
259 }
This page took 0.035732 seconds and 4 git commands to generate.