Command metadata regenerate
[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 poptOption long_options[] = {
43 /* { longName, shortName, argInfo, argPtr, value, descrip, argDesc, } */
44 { "help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0, },
45 { "session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
46 { "list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, 0, 0, },
47 { "list-commands", 0, POPT_ARG_NONE, NULL, OPT_LIST_COMMANDS},
48 { 0, 0, 0, 0, 0, 0, 0, },
49 };
50
51 static struct cmd_struct actions[] = {
52 { "regenerate", metadata_regenerate },
53 { NULL, NULL } /* Array closure */
54 };
55
56 /*
57 * usage
58 */
59 static void usage(FILE *ofp)
60 {
61 fprintf(ofp, "usage: lttng metadata [OPTION] ACTION\n");
62 fprintf(ofp, "\n");
63 fprintf(ofp, "Actions:\n");
64 fprintf(ofp, " regenerate\n");
65 fprintf(ofp, " Regenerate and overwrite the metadata of the session.\n");
66 fprintf(ofp, "Options:\n");
67 fprintf(ofp, " -h, --help Show this help.\n");
68 fprintf(ofp, " --list-options Simple listing of options.\n");
69 fprintf(ofp, " -s, --session NAME Apply to session name.\n");
70 fprintf(ofp, "\n");
71 }
72
73 /*
74 * Count and return the number of arguments in argv.
75 */
76 static int count_arguments(const char **argv)
77 {
78 int i = 0;
79
80 assert(argv);
81
82 while (argv[i] != NULL) {
83 i++;
84 }
85
86 return i;
87 }
88
89 static int metadata_regenerate(int argc, const char **argv)
90 {
91 int ret;
92
93 ret = lttng_metadata_regenerate(session_name);
94 if (ret == 0) {
95 MSG("Metadata successfully regenerated for session %s", session_name);
96 }
97 return ret;
98 }
99
100 static int handle_command(const char **argv)
101 {
102 struct cmd_struct *cmd;
103 int ret = CMD_SUCCESS, i = 0, argc, command_ret = CMD_SUCCESS;
104
105 if (argv == NULL) {
106 usage(stderr);
107 command_ret = CMD_ERROR;
108 goto end;
109 }
110
111 argc = count_arguments(argv);
112
113 cmd = &actions[i];
114 while (cmd->func != NULL) {
115 /* Find command */
116 if (strcmp(argv[0], cmd->name) == 0) {
117 command_ret = cmd->func(argc, argv);
118 goto end;
119 }
120
121 cmd = &actions[i++];
122 }
123
124 ret = CMD_UNDEFINED;
125
126 end:
127 /* Overwrite ret if an error occurred in cmd->func() */
128 ret = command_ret ? command_ret : ret;
129 return ret;
130 }
131
132 /*
133 * Metadata command handling.
134 */
135 int cmd_metadata(int argc, const char **argv)
136 {
137 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS;
138 static poptContext pc;
139
140 if (argc < 1) {
141 usage(stderr);
142 ret = CMD_ERROR;
143 goto end;
144 }
145
146 pc = poptGetContext(NULL, argc, argv, long_options, 0);
147 poptReadDefaultConfig(pc, 0);
148
149 while ((opt = poptGetNextOpt(pc)) != -1) {
150 switch (opt) {
151 case OPT_HELP:
152 usage(stdout);
153 goto end;
154 case OPT_LIST_OPTIONS:
155 list_cmd_options(stdout, long_options);
156 goto end;
157 case OPT_LIST_COMMANDS:
158 list_commands(actions, stdout);
159 goto end;
160 default:
161 usage(stderr);
162 ret = CMD_UNDEFINED;
163 goto end;
164 }
165 }
166
167 if (!opt_session_name) {
168 session_name = get_session_name();
169 if (session_name == NULL) {
170 ret = CMD_ERROR;
171 goto end;
172 }
173 } else {
174 session_name = opt_session_name;
175 }
176
177 command_ret = handle_command(poptGetArgs(pc));
178 if (command_ret) {
179 switch (-command_ret) {
180 default:
181 ERR("%s", lttng_strerror(command_ret));
182 break;
183 }
184 }
185
186 end:
187 if (!opt_session_name) {
188 free(session_name);
189 }
190
191 /* Overwrite ret if an error occurred during handle_command() */
192 ret = command_ret ? command_ret : ret;
193
194 poptFreeContext(pc);
195 return ret;
196 }
This page took 0.033572 seconds and 5 git commands to generate.