CLI: show event filter string
[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>
26
c399183f 27#include "../command.h"
f3ed775e 28
65f25c66 29#include <common/mi-lttng.h>
42224349 30#include <common/sessiond-comm/sessiond-comm.h>
1dac0189 31#include <common/utils.h>
42224349 32
fd076c09 33static char *opt_session_name;
b09ee5ba 34static int opt_destroy_all;
e20ee7c2 35static int opt_no_wait;
f3ed775e 36
65f25c66
JRJ
37/* Mi writer */
38static struct mi_writer *writer;
39
f3ed775e
DG
40enum {
41 OPT_HELP = 1,
679b4943 42 OPT_LIST_OPTIONS,
f3ed775e
DG
43};
44
45static struct poptOption long_options[] = {
46 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
47 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
b09ee5ba 48 {"all", 'a', POPT_ARG_VAL, &opt_destroy_all, 1, 0, 0},
679b4943 49 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
e20ee7c2 50 {"no-wait", 'n', POPT_ARG_VAL, &opt_no_wait, 1, 0, 0},
f3ed775e
DG
51 {0, 0, 0, 0, 0, 0, 0}
52};
53
54/*
55 * usage
56 */
57static void usage(FILE *ofp)
58{
32a6298d 59 fprintf(ofp, "usage: lttng destroy [NAME] [OPTIONS]\n");
f3ed775e
DG
60 fprintf(ofp, "\n");
61 fprintf(ofp, "Where NAME is an optional session name. If not specified, lttng will\n");
62 fprintf(ofp, "get it from the configuration directory (.lttng).\n");
63 fprintf(ofp, "\n");
32a6298d 64 fprintf(ofp, "Options:\n");
f3ed775e 65 fprintf(ofp, " -h, --help Show this help\n");
b09ee5ba 66 fprintf(ofp, " -a, --all Destroy all sessions\n");
27221701 67 fprintf(ofp, " --list-options Simple listing of options\n");
e20ee7c2 68 fprintf(ofp, " -n, --no-wait Don't wait for data availability\n");
f3ed775e
DG
69 fprintf(ofp, "\n");
70}
71
72/*
b09ee5ba
FG
73 * destroy_session
74 *
75 * Unregister the provided session to the session daemon. On success, removes
76 * the default configuration.
f3ed775e 77 */
65f25c66 78static int destroy_session(struct lttng_session *session)
f3ed775e
DG
79{
80 int ret;
1dac0189 81 char *session_name = NULL;
f3ed775e 82
e20ee7c2
JD
83 ret = lttng_stop_tracing_no_wait(session->name);
84 if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
85 ERR("%s", lttng_strerror(ret));
86 }
87 if (!opt_no_wait) {
88 _MSG("Waiting for data availability");
89 fflush(stdout);
90 do {
91 ret = lttng_data_pending(session->name);
92 if (ret < 0) {
93 /* Return the data available call error. */
94 goto error;
95 }
96
97 /*
98 * Data sleep time before retrying (in usec). Don't sleep if the call
99 * returned value indicates availability.
100 */
101 if (ret) {
102 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME);
103 _MSG(".");
104 fflush(stdout);
105 }
106 } while (ret != 0);
107 MSG("");
108 }
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:
ca1c3607 182 usage(stdout);
b09ee5ba 183 break;
679b4943
SM
184 case OPT_LIST_OPTIONS:
185 list_cmd_options(stdout, long_options);
b09ee5ba 186 break;
f3ed775e
DG
187 default:
188 usage(stderr);
189 ret = CMD_UNDEFINED;
b09ee5ba 190 break;
f3ed775e 191 }
b09ee5ba 192 goto end;
f3ed775e
DG
193 }
194
65f25c66 195 /* Mi preparation */
c7e35b03 196 if (lttng_opt_mi) {
65f25c66
JRJ
197 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
198 if (!writer) {
199 ret = -LTTNG_ERR_NOMEM;
200 goto end;
201 }
202
203 /* Open command element */
204 ret = mi_lttng_writer_command_open(writer,
205 mi_lttng_element_command_destroy);
206 if (ret) {
207 ret = CMD_ERROR;
208 goto end;
209 }
210
211 /* Open output element */
212 ret = mi_lttng_writer_open_element(writer,
213 mi_lttng_element_command_output);
214 if (ret) {
215 ret = CMD_ERROR;
216 goto end;
217 }
218
219 /* For validation and semantic purpose we open a sessions element */
220 ret = mi_lttng_sessions_open(writer);
221 if (ret) {
222 ret = CMD_ERROR;
223 goto end;
224 }
225 }
226
227 /* Recuperate all sessions for further operation */
228 count = lttng_list_sessions(&sessions);
229 if (count < 0) {
230 command_ret = count;
231 success = 0;
232 goto mi_closing;
c7e35b03
JR
233 }
234
fd076c09 235 /* Ignore session name in case all sessions are to be destroyed */
b09ee5ba 236 if (opt_destroy_all) {
65f25c66
JRJ
237 command_ret = destroy_all_sessions(sessions, count);
238 if (command_ret) {
239 success = 0;
240 }
241 } else {
242 opt_session_name = (char *) poptGetArg(pc);
243
1dac0189 244 if (!opt_session_name) {
65f25c66
JRJ
245 /* No session name specified, lookup default */
246 session_name = get_session_name();
247 if (session_name == NULL) {
248 command_ret = CMD_ERROR;
249 success = 0;
250 goto mi_closing;
251 }
252 } else {
253 session_name = opt_session_name;
254 }
fd076c09 255
65f25c66
JRJ
256 /* Find the corresponding lttng_session struct */
257 found = 0;
258 for (i = 0; i < count; i++) {
259 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
260 found = 1;
261 command_ret = destroy_session(&sessions[i]);
262 if (command_ret) {
263 success = 0;
264 }
fd076c09 265
65f25c66
JRJ
266 }
267 }
268
269 if (!found) {
270 ERR("Session name %s not found", session_name);
271 command_ret = LTTNG_ERR_SESS_NOT_FOUND;
272 success = 0;
273 goto mi_closing;
274 }
275 }
276
277mi_closing:
278 /* Mi closing */
279 if (lttng_opt_mi) {
280 /* Close sessions and output element element */
281 ret = mi_lttng_close_multi_element(writer, 2);
282 if (ret) {
fd076c09 283 ret = CMD_ERROR;
b09ee5ba
FG
284 goto end;
285 }
fd076c09 286
65f25c66
JRJ
287 /* Success ? */
288 ret = mi_lttng_writer_write_element_bool(writer,
289 mi_lttng_element_command_success, success);
290 if (ret) {
291 ret = CMD_ERROR;
292 goto end;
293 }
f3ed775e 294
65f25c66
JRJ
295 /* Command element close */
296 ret = mi_lttng_writer_command_close(writer);
297 if (ret) {
298 ret = CMD_ERROR;
299 goto end;
300 }
301 }
f3ed775e 302end:
65f25c66
JRJ
303 /* Mi clean-up */
304 if (writer && mi_lttng_writer_destroy(writer)) {
305 /* Preserve original error code */
306 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
307 }
308
fd076c09 309 if (opt_session_name == NULL) {
b09ee5ba
FG
310 free(session_name);
311 }
fd076c09 312
65f25c66
JRJ
313 /* Overwrite ret if an error occurred during destroy_session/all */
314 ret = command_ret ? command_ret : ret;
315
fd076c09 316 poptFreeContext(pc);
f3ed775e
DG
317 return ret;
318}
This page took 0.053084 seconds and 4 git commands to generate.