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