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