Mi save&load command: support and validation
[lttng-tools.git] / src / bin / lttng / commands / load.c
CommitLineData
8c42d845
JG
1/*
2 * Copyright (C) 2014 - Jérémie Galarneau <jeremie.galarneau@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 _GNU_SOURCE
19#include <inttypes.h>
20#include <popt.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <assert.h>
25
1734c658 26#include <common/mi-lttng.h>
8c42d845 27#include <common/config/config.h>
1734c658 28
8c42d845
JG
29#include "../command.h"
30
31static char *opt_input_path;
8c42d845
JG
32static int opt_force;
33static int opt_load_all;
34
11143783
DG
35static const char *session_name;
36
8c42d845
JG
37enum {
38 OPT_HELP = 1,
39 OPT_ALL,
40 OPT_FORCE,
41};
42
1734c658
JRJ
43static struct mi_writer *writer;
44
8c42d845
JG
45static struct poptOption load_opts[] = {
46 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
47 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
48 {"all", 'a', POPT_ARG_NONE, 0, OPT_ALL, 0, 0},
8c42d845
JG
49 {"input-path", 'i', POPT_ARG_STRING, &opt_input_path, 0, 0, 0},
50 {"force", 'f', POPT_ARG_NONE, 0, OPT_FORCE, 0, 0},
51 {0, 0, 0, 0, 0, 0, 0}
52};
53
54/*
55 * usage
56 */
57static void usage(FILE *ofp)
58{
11143783 59 fprintf(ofp, "usage: lttng load [OPTIONS] [SESSION]\n");
8c42d845
JG
60 fprintf(ofp, "\n");
61 fprintf(ofp, "Options:\n");
d7c6b799
DG
62 fprintf(ofp, " -h, --help Show this help\n");
63 fprintf(ofp, " -a, --all Load all sessions (default)\n");
64 fprintf(ofp, " -i, --input-path PATH Input path of the session file(s).\n");
65 fprintf(ofp, " If a directory, load all files in it\n");
66 fprintf(ofp, " else try to load the given file.\n");
67 fprintf(ofp, " -f, --force Override existing session(s).\n");
68 fprintf(ofp, " This will destroy existing session(s)\n");
69 fprintf(ofp, " before creating new one(s).\n");
8c42d845
JG
70}
71
1734c658
JRJ
72static int mi_partial_session(const char *session_name)
73{
74 int ret;
75 assert(writer);
76 assert(session_name);
77
78 /* Open session element */
79 ret = mi_lttng_writer_open_element(writer, config_element_session);
80 if (ret) {
81 goto end;
82 }
83
84 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
85 session_name);
86 if (ret) {
87 goto end;
88 }
89
90 /* Closing session element */
91 ret = mi_lttng_writer_close_element(writer);
92end:
93 return ret;
94}
95
96/*
97 * Mi print of load command
98 */
99static int mi_load_print(const char *session_name)
100{
101 int ret;
102 assert(writer);
103
104 if (opt_load_all) {
105 /* We use a wildcard to represent all sessions */
106 session_name = "*";
107 }
108
109 /* Print load element */
110 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_load);
111 if (ret) {
112 goto end;
113 }
114
115 /* Print session element */
116 ret = mi_partial_session(session_name);
117 if (ret) {
118 goto end;
119 }
120
121 /* Path element */
122 if (opt_input_path) {
123 ret = mi_lttng_writer_write_element_string(writer, config_element_path,
124 opt_input_path);
125 if (ret) {
126 goto end;
127 }
128 }
129
130 /* Close load element */
131 ret = mi_lttng_writer_close_element(writer);
132
133end:
134 return ret;
135}
136
8c42d845
JG
137/*
138 * The 'load <options>' first level command
139 */
140int cmd_load(int argc, const char **argv)
141{
1734c658 142 int ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success;
8c42d845
JG
143 int opt;
144 poptContext pc;
145
146 pc = poptGetContext(NULL, argc, argv, load_opts, 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_ALL:
155 opt_load_all = 1;
156 break;
157 case OPT_FORCE:
158 opt_force = 1;
159 break;
160 default:
161 usage(stderr);
162 ret = CMD_UNDEFINED;
163 goto end;
164 }
165 }
166
11143783
DG
167 if (!opt_load_all) {
168 session_name = poptGetArg(pc);
732b768a
DG
169 if (session_name) {
170 DBG2("Loading session name: %s", session_name);
1734c658
JRJ
171 } else {
172 /* Default to load_all */
173 opt_load_all = 1;
11143783 174 }
8c42d845
JG
175 }
176
1734c658
JRJ
177 /* Mi check */
178 if (lttng_opt_mi) {
179 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
180 if (!writer) {
181 ret = -LTTNG_ERR_NOMEM;
182 goto end;
183 }
184
185 /* Open command element */
186 ret = mi_lttng_writer_command_open(writer,
187 mi_lttng_element_command_load);
188 if (ret) {
189 ret = CMD_ERROR;
190 goto end;
191 }
192
193 /* Open output element */
194 ret = mi_lttng_writer_open_element(writer,
195 mi_lttng_element_command_output);
196 if (ret) {
197 ret = CMD_ERROR;
198 goto end;
199 }
200 }
201
202 command_ret = config_load_session(opt_input_path, session_name, opt_force, 0);
203 if (command_ret) {
204 ERR("%s", lttng_strerror(command_ret));
205 success = 0;
279d6193
DG
206 } else {
207 if (opt_load_all) {
208 MSG("All sessions have been loaded successfully");
732b768a 209 } else if (session_name) {
8e525b95
DG
210 ret = config_init((char *)session_name);
211 if (ret < 0) {
212 ret = CMD_WARNING;
213 }
279d6193 214 MSG("Session %s has been loaded successfully", session_name);
732b768a
DG
215 } else {
216 MSG("Session has been loaded successfully");
279d6193 217 }
1734c658
JRJ
218 success = 1;
219 }
220
221 /* Mi Printing and closing */
222 if (lttng_opt_mi) {
223 /* Mi print */
224 ret = mi_load_print(session_name);
225 if (ret) {
226 ret = CMD_ERROR;
227 goto end;
228 }
229
230 /* Close output element */
231 ret = mi_lttng_writer_close_element(writer);
232 if (ret) {
233 ret = CMD_ERROR;
234 goto end;
235 }
236
237 /* Success ? */
238 ret = mi_lttng_writer_write_element_bool(writer,
239 mi_lttng_element_command_success, success);
240 if (ret) {
241 ret = CMD_ERROR;
242 goto end;
243 }
244
245 /* Command element close */
246 ret = mi_lttng_writer_command_close(writer);
247 if (ret) {
248 ret = CMD_ERROR;
249 goto end;
250 }
8c42d845
JG
251 }
252end:
1734c658
JRJ
253 if (writer && mi_lttng_writer_destroy(writer)) {
254 /* Preserve original error code */
255 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
256 }
257
258 /* Overwrite ret if the was an error with the load command */
259 ret = command_ret ? -command_ret : ret;
260
8c42d845
JG
261 poptFreeContext(pc);
262 return ret;
263}
This page took 0.033012 seconds and 4 git commands to generate.