Expose monitor timer interval to lttngctl and client
[lttng-tools.git] / src / bin / lttng / commands / destroy.c
CommitLineData
f3ed775e
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
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.
f3ed775e
DG
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 *
d14d33bf
AM
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.
f3ed775e
DG
16 */
17
6c1c0768 18#define _LGPL_SOURCE
f3ed775e
DG
19#include <popt.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <unistd.h>
20fb9e02 26#include <stdbool.h>
f3ed775e 27
c399183f 28#include "../command.h"
f3ed775e 29
65f25c66 30#include <common/mi-lttng.h>
42224349 31#include <common/sessiond-comm/sessiond-comm.h>
1dac0189 32#include <common/utils.h>
42224349 33
fd076c09 34static char *opt_session_name;
b09ee5ba 35static int opt_destroy_all;
e20ee7c2 36static int opt_no_wait;
f3ed775e 37
65f25c66
JRJ
38/* Mi writer */
39static struct mi_writer *writer;
40
f3ed775e
DG
41enum {
42 OPT_HELP = 1,
679b4943 43 OPT_LIST_OPTIONS,
f3ed775e
DG
44};
45
46static struct poptOption long_options[] = {
47 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
48 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
b09ee5ba 49 {"all", 'a', POPT_ARG_VAL, &opt_destroy_all, 1, 0, 0},
679b4943 50 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
e20ee7c2 51 {"no-wait", 'n', POPT_ARG_VAL, &opt_no_wait, 1, 0, 0},
f3ed775e
DG
52 {0, 0, 0, 0, 0, 0, 0}
53};
54
f3ed775e 55/*
b09ee5ba
FG
56 * destroy_session
57 *
58 * Unregister the provided session to the session daemon. On success, removes
59 * the default configuration.
f3ed775e 60 */
65f25c66 61static int destroy_session(struct lttng_session *session)
f3ed775e
DG
62{
63 int ret;
1dac0189 64 char *session_name = NULL;
20fb9e02 65 bool session_was_stopped;
f3ed775e 66
e20ee7c2
JD
67 ret = lttng_stop_tracing_no_wait(session->name);
68 if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
69 ERR("%s", lttng_strerror(ret));
70 }
20fb9e02 71 session_was_stopped = ret == -LTTNG_ERR_TRACE_ALREADY_STOPPED;
e20ee7c2 72 if (!opt_no_wait) {
01f55be4
JG
73 bool printed_wait_msg = false;
74
e20ee7c2
JD
75 do {
76 ret = lttng_data_pending(session->name);
77 if (ret < 0) {
78 /* Return the data available call error. */
79 goto error;
80 }
81
82 /*
83 * Data sleep time before retrying (in usec). Don't sleep if the call
84 * returned value indicates availability.
85 */
86 if (ret) {
01f55be4
JG
87 if (!printed_wait_msg) {
88 _MSG("Waiting for data availability");
89 fflush(stdout);
90 }
91
92 printed_wait_msg = true;
e20ee7c2
JD
93 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME);
94 _MSG(".");
95 fflush(stdout);
96 }
97 } while (ret != 0);
01f55be4
JG
98 if (printed_wait_msg) {
99 MSG("");
100 }
e20ee7c2 101 }
20fb9e02
JD
102 if (!session_was_stopped) {
103 /*
104 * Don't print the event and packet loss warnings since the user
105 * already saw them when stopping the trace.
106 */
107 print_session_stats(session->name);
108 }
e20ee7c2
JD
109
110 ret = lttng_destroy_session_no_wait(session->name);
f3ed775e 111 if (ret < 0) {
b09ee5ba 112 goto error;
f3ed775e
DG
113 }
114
65f25c66 115 MSG("Session %s destroyed", session->name);
1dac0189
PPM
116
117 session_name = get_session_name_quiet();
118 if (session_name && !strncmp(session->name, session_name, NAME_MAX)) {
119 config_destroy_default();
120 }
65f25c66
JRJ
121
122 if (lttng_opt_mi) {
123 ret = mi_lttng_session(writer, session, 0);
124 if (ret) {
125 ret = CMD_ERROR;
126 goto error;
127 }
128 }
129
f3ed775e 130 ret = CMD_SUCCESS;
b09ee5ba 131error:
1dac0189 132 free(session_name);
b09ee5ba
FG
133 return ret;
134}
f3ed775e 135
b09ee5ba
FG
136/*
137 * destroy_all_sessions
138 *
139 * Call destroy_sessions for each registered sessions
140 */
65f25c66 141static int destroy_all_sessions(struct lttng_session *sessions, int count)
b09ee5ba 142{
65f25c66 143 int i, ret = CMD_SUCCESS;
b09ee5ba 144
b09ee5ba
FG
145 if (count == 0) {
146 MSG("No session found, nothing to do.");
60e835ca
DG
147 } else if (count < 0) {
148 ERR("%s", lttng_strerror(ret));
149 goto error;
b09ee5ba 150 }
60e835ca 151
b09ee5ba 152 for (i = 0; i < count; i++) {
65f25c66 153 ret = destroy_session(&sessions[i]);
b09ee5ba
FG
154 if (ret < 0) {
155 goto error;
156 }
b73d0b29 157 }
f3ed775e
DG
158error:
159 return ret;
160}
161
162/*
843f5df9 163 * The 'destroy <options>' first level command
f3ed775e
DG
164 */
165int cmd_destroy(int argc, const char **argv)
166{
b09ee5ba 167 int opt;
65f25c66 168 int ret = CMD_SUCCESS , i, command_ret = CMD_SUCCESS, success = 1;
f3ed775e 169 static poptContext pc;
b09ee5ba 170 char *session_name = NULL;
f3ed775e 171
65f25c66
JRJ
172 struct lttng_session *sessions;
173 int count;
174 int found;
175
f3ed775e
DG
176 pc = poptGetContext(NULL, argc, argv, long_options, 0);
177 poptReadDefaultConfig(pc, 0);
178
179 while ((opt = poptGetNextOpt(pc)) != -1) {
180 switch (opt) {
181 case OPT_HELP:
4ba92f18 182 SHOW_HELP();
b09ee5ba 183 break;
679b4943
SM
184 case OPT_LIST_OPTIONS:
185 list_cmd_options(stdout, long_options);
b09ee5ba 186 break;
f3ed775e 187 default:
f3ed775e 188 ret = CMD_UNDEFINED;
b09ee5ba 189 break;
f3ed775e 190 }
b09ee5ba 191 goto end;
f3ed775e
DG
192 }
193
65f25c66 194 /* Mi preparation */
c7e35b03 195 if (lttng_opt_mi) {
65f25c66
JRJ
196 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
197 if (!writer) {
198 ret = -LTTNG_ERR_NOMEM;
199 goto end;
200 }
201
202 /* Open command element */
203 ret = mi_lttng_writer_command_open(writer,
204 mi_lttng_element_command_destroy);
205 if (ret) {
206 ret = CMD_ERROR;
207 goto end;
208 }
209
210 /* Open output element */
211 ret = mi_lttng_writer_open_element(writer,
212 mi_lttng_element_command_output);
213 if (ret) {
214 ret = CMD_ERROR;
215 goto end;
216 }
217
218 /* For validation and semantic purpose we open a sessions element */
219 ret = mi_lttng_sessions_open(writer);
220 if (ret) {
221 ret = CMD_ERROR;
222 goto end;
223 }
224 }
225
226 /* Recuperate all sessions for further operation */
227 count = lttng_list_sessions(&sessions);
228 if (count < 0) {
229 command_ret = count;
230 success = 0;
231 goto mi_closing;
c7e35b03
JR
232 }
233
fd076c09 234 /* Ignore session name in case all sessions are to be destroyed */
b09ee5ba 235 if (opt_destroy_all) {
65f25c66
JRJ
236 command_ret = destroy_all_sessions(sessions, count);
237 if (command_ret) {
238 success = 0;
239 }
240 } else {
241 opt_session_name = (char *) poptGetArg(pc);
242
1dac0189 243 if (!opt_session_name) {
65f25c66
JRJ
244 /* No session name specified, lookup default */
245 session_name = get_session_name();
246 if (session_name == NULL) {
247 command_ret = CMD_ERROR;
248 success = 0;
249 goto mi_closing;
250 }
251 } else {
252 session_name = opt_session_name;
253 }
fd076c09 254
65f25c66
JRJ
255 /* Find the corresponding lttng_session struct */
256 found = 0;
257 for (i = 0; i < count; i++) {
258 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
259 found = 1;
260 command_ret = destroy_session(&sessions[i]);
261 if (command_ret) {
262 success = 0;
263 }
fd076c09 264
65f25c66
JRJ
265 }
266 }
267
268 if (!found) {
269 ERR("Session name %s not found", session_name);
270 command_ret = LTTNG_ERR_SESS_NOT_FOUND;
271 success = 0;
272 goto mi_closing;
273 }
274 }
275
276mi_closing:
277 /* Mi closing */
278 if (lttng_opt_mi) {
279 /* Close sessions and output element element */
280 ret = mi_lttng_close_multi_element(writer, 2);
281 if (ret) {
fd076c09 282 ret = CMD_ERROR;
b09ee5ba
FG
283 goto end;
284 }
fd076c09 285
65f25c66
JRJ
286 /* Success ? */
287 ret = mi_lttng_writer_write_element_bool(writer,
288 mi_lttng_element_command_success, success);
289 if (ret) {
290 ret = CMD_ERROR;
291 goto end;
292 }
f3ed775e 293
65f25c66
JRJ
294 /* Command element close */
295 ret = mi_lttng_writer_command_close(writer);
296 if (ret) {
297 ret = CMD_ERROR;
298 goto end;
299 }
300 }
f3ed775e 301end:
65f25c66
JRJ
302 /* Mi clean-up */
303 if (writer && mi_lttng_writer_destroy(writer)) {
304 /* Preserve original error code */
305 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
306 }
307
fd076c09 308 if (opt_session_name == NULL) {
b09ee5ba
FG
309 free(session_name);
310 }
fd076c09 311
65f25c66
JRJ
312 /* Overwrite ret if an error occurred during destroy_session/all */
313 ret = command_ret ? command_ret : ret;
314
fd076c09 315 poptFreeContext(pc);
f3ed775e
DG
316 return ret;
317}
This page took 0.056908 seconds and 4 git commands to generate.