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