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
6c1c0768 18#define _LGPL_SOURCE
8c42d845
JG
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>
f40ef1d5 27#include <common/config/session-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,
13a810d5 41 OPT_LIST_OPTIONS,
8c42d845
JG
42};
43
1734c658
JRJ
44static struct mi_writer *writer;
45
8c42d845
JG
46static 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},
8c42d845
JG
50 {"input-path", 'i', POPT_ARG_STRING, &opt_input_path, 0, 0, 0},
51 {"force", 'f', POPT_ARG_NONE, 0, OPT_FORCE, 0, 0},
13a810d5 52 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
8c42d845
JG
53 {0, 0, 0, 0, 0, 0, 0}
54};
55
1734c658
JRJ
56static int mi_partial_session(const char *session_name)
57{
58 int ret;
59 assert(writer);
60 assert(session_name);
61
62 /* Open session element */
63 ret = mi_lttng_writer_open_element(writer, config_element_session);
64 if (ret) {
65 goto end;
66 }
67
68 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
69 session_name);
70 if (ret) {
71 goto end;
72 }
73
74 /* Closing session element */
75 ret = mi_lttng_writer_close_element(writer);
76end:
77 return ret;
78}
79
80/*
81 * Mi print of load command
82 */
83static int mi_load_print(const char *session_name)
84{
85 int ret;
86 assert(writer);
87
88 if (opt_load_all) {
89 /* We use a wildcard to represent all sessions */
90 session_name = "*";
91 }
92
93 /* Print load element */
94 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_load);
95 if (ret) {
96 goto end;
97 }
98
99 /* Print session element */
100 ret = mi_partial_session(session_name);
101 if (ret) {
102 goto end;
103 }
104
105 /* Path element */
106 if (opt_input_path) {
107 ret = mi_lttng_writer_write_element_string(writer, config_element_path,
108 opt_input_path);
109 if (ret) {
110 goto end;
111 }
112 }
113
114 /* Close load element */
115 ret = mi_lttng_writer_close_element(writer);
116
117end:
118 return ret;
119}
120
8c42d845
JG
121/*
122 * The 'load <options>' first level command
123 */
124int cmd_load(int argc, const char **argv)
125{
1734c658 126 int ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success;
8c42d845
JG
127 int opt;
128 poptContext pc;
129
130 pc = poptGetContext(NULL, argc, argv, load_opts, 0);
131 poptReadDefaultConfig(pc, 0);
132
133 while ((opt = poptGetNextOpt(pc)) != -1) {
134 switch (opt) {
135 case OPT_HELP:
4ba92f18 136 SHOW_HELP();
8c42d845
JG
137 goto end;
138 case OPT_ALL:
139 opt_load_all = 1;
140 break;
13a810d5
DG
141 case OPT_LIST_OPTIONS:
142 list_cmd_options(stdout, load_opts);
143 goto end;
8c42d845
JG
144 case OPT_FORCE:
145 opt_force = 1;
146 break;
147 default:
8c42d845
JG
148 ret = CMD_UNDEFINED;
149 goto end;
150 }
151 }
152
78987a23
JG
153 ret = lttng_session_daemon_alive();
154 if (!ret) {
155 ERR("No session daemon is available");
156 ret = CMD_ERROR;
157 goto end;
158 }
159 ret = 0;
160
11143783
DG
161 if (!opt_load_all) {
162 session_name = poptGetArg(pc);
732b768a
DG
163 if (session_name) {
164 DBG2("Loading session name: %s", session_name);
1734c658
JRJ
165 } else {
166 /* Default to load_all */
167 opt_load_all = 1;
11143783 168 }
8c42d845
JG
169 }
170
1734c658
JRJ
171 /* Mi check */
172 if (lttng_opt_mi) {
173 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
174 if (!writer) {
175 ret = -LTTNG_ERR_NOMEM;
176 goto end;
177 }
178
179 /* Open command element */
180 ret = mi_lttng_writer_command_open(writer,
181 mi_lttng_element_command_load);
182 if (ret) {
183 ret = CMD_ERROR;
184 goto end;
185 }
186
187 /* Open output element */
188 ret = mi_lttng_writer_open_element(writer,
189 mi_lttng_element_command_output);
190 if (ret) {
191 ret = CMD_ERROR;
192 goto end;
193 }
194 }
195
196 command_ret = config_load_session(opt_input_path, session_name, opt_force, 0);
197 if (command_ret) {
198 ERR("%s", lttng_strerror(command_ret));
199 success = 0;
279d6193
DG
200 } else {
201 if (opt_load_all) {
202 MSG("All sessions have been loaded successfully");
732b768a 203 } else if (session_name) {
8e525b95
DG
204 ret = config_init((char *)session_name);
205 if (ret < 0) {
206 ret = CMD_WARNING;
207 }
279d6193 208 MSG("Session %s has been loaded successfully", session_name);
732b768a
DG
209 } else {
210 MSG("Session has been loaded successfully");
279d6193 211 }
1734c658
JRJ
212 success = 1;
213 }
214
215 /* Mi Printing and closing */
216 if (lttng_opt_mi) {
217 /* Mi print */
218 ret = mi_load_print(session_name);
219 if (ret) {
220 ret = CMD_ERROR;
221 goto end;
222 }
223
224 /* Close output element */
225 ret = mi_lttng_writer_close_element(writer);
226 if (ret) {
227 ret = CMD_ERROR;
228 goto end;
229 }
230
231 /* Success ? */
232 ret = mi_lttng_writer_write_element_bool(writer,
233 mi_lttng_element_command_success, success);
234 if (ret) {
235 ret = CMD_ERROR;
236 goto end;
237 }
238
239 /* Command element close */
240 ret = mi_lttng_writer_command_close(writer);
241 if (ret) {
242 ret = CMD_ERROR;
243 goto end;
244 }
8c42d845
JG
245 }
246end:
1734c658
JRJ
247 if (writer && mi_lttng_writer_destroy(writer)) {
248 /* Preserve original error code */
249 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
250 }
251
252 /* Overwrite ret if the was an error with the load command */
253 ret = command_ret ? -command_ret : ret;
254
8c42d845
JG
255 poptFreeContext(pc);
256 return ret;
257}
This page took 0.041002 seconds and 4 git commands to generate.