Fix: consumer: double unlock of rcu read lock on error
[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>
bbbfd849 27#include <lttng/lttng.h>
f3ed775e 28
c399183f 29#include "../command.h"
f3ed775e 30
65f25c66 31#include <common/mi-lttng.h>
42224349 32#include <common/sessiond-comm/sessiond-comm.h>
1dac0189 33#include <common/utils.h>
42224349 34
fd076c09 35static char *opt_session_name;
b09ee5ba 36static int opt_destroy_all;
e20ee7c2 37static int opt_no_wait;
f3ed775e 38
4fc83d94
PP
39#ifdef LTTNG_EMBED_HELP
40static const char help_msg[] =
41#include <lttng-destroy.1.h>
42;
43#endif
44
65f25c66
JRJ
45/* Mi writer */
46static struct mi_writer *writer;
47
f3ed775e
DG
48enum {
49 OPT_HELP = 1,
679b4943 50 OPT_LIST_OPTIONS,
f3ed775e
DG
51};
52
53static struct poptOption long_options[] = {
54 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
55 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
b09ee5ba 56 {"all", 'a', POPT_ARG_VAL, &opt_destroy_all, 1, 0, 0},
679b4943 57 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
e20ee7c2 58 {"no-wait", 'n', POPT_ARG_VAL, &opt_no_wait, 1, 0, 0},
f3ed775e
DG
59 {0, 0, 0, 0, 0, 0, 0}
60};
61
f3ed775e 62/*
b09ee5ba
FG
63 * destroy_session
64 *
65 * Unregister the provided session to the session daemon. On success, removes
66 * the default configuration.
f3ed775e 67 */
65f25c66 68static int destroy_session(struct lttng_session *session)
f3ed775e
DG
69{
70 int ret;
1dac0189 71 char *session_name = NULL;
20fb9e02 72 bool session_was_stopped;
bbbfd849
JG
73 enum lttng_error_code ret_code;
74 struct lttng_destruction_handle *handle = NULL;
75 enum lttng_destruction_handle_status status;
76 bool printed_wait_msg = false;
77 enum lttng_rotation_state rotation_state;
f3ed775e 78
e20ee7c2
JD
79 ret = lttng_stop_tracing_no_wait(session->name);
80 if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
81 ERR("%s", lttng_strerror(ret));
82 }
20fb9e02 83 session_was_stopped = ret == -LTTNG_ERR_TRACE_ALREADY_STOPPED;
e20ee7c2 84 if (!opt_no_wait) {
e20ee7c2
JD
85 do {
86 ret = lttng_data_pending(session->name);
87 if (ret < 0) {
88 /* Return the data available call error. */
89 goto error;
90 }
91
92 /*
93 * Data sleep time before retrying (in usec). Don't sleep if the call
94 * returned value indicates availability.
95 */
96 if (ret) {
01f55be4 97 if (!printed_wait_msg) {
bbbfd849
JG
98 _MSG("Waiting for destruction of session \"%s\"",
99 session->name);
100 printed_wait_msg = true;
01f55be4
JG
101 fflush(stdout);
102 }
103
c8f61fd4 104 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US);
e20ee7c2
JD
105 _MSG(".");
106 fflush(stdout);
107 }
108 } while (ret != 0);
e20ee7c2 109 }
20fb9e02
JD
110 if (!session_was_stopped) {
111 /*
112 * Don't print the event and packet loss warnings since the user
113 * already saw them when stopping the trace.
114 */
115 print_session_stats(session->name);
116 }
e20ee7c2 117
bbbfd849
JG
118 ret_code = lttng_destroy_session_ext(session->name, &handle);
119 if (ret_code != LTTNG_OK) {
120 ret = -ret_code;
b09ee5ba 121 goto error;
f3ed775e
DG
122 }
123
bbbfd849
JG
124 if (opt_no_wait) {
125 goto skip_wait_rotation;
126 }
127
128 do {
129 status = lttng_destruction_handle_wait_for_completion(handle,
c8f61fd4 130 DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US / USEC_PER_MSEC);
bbbfd849
JG
131 switch (status) {
132 case LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT:
133 if (!printed_wait_msg) {
134 _MSG("Waiting for destruction of session \"%s\"",
135 session->name);
136 printed_wait_msg = true;
137 }
138 _MSG(".");
139 fflush(stdout);
140 break;
141 case LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED:
142 break;
143 default:
144 ERR("Failed to wait for the completion of the destruction of session \"%s\"",
145 session->name);
146 ret = -1;
147 goto error;
148 }
149 } while (status == LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT);
150
151 status = lttng_destruction_handle_get_result(handle, &ret_code);
152 if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_OK) {
153 ERR("Failed to get the result of session destruction");
154 ret = -1;
155 goto error;
156 }
157 if (ret_code != LTTNG_OK) {
158 ret = -LTTNG_OK;
159 goto error;
160 }
161
162 status = lttng_destruction_handle_get_rotation_state(handle,
163 &rotation_state);
7d81aa9f
JG
164 if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_OK) {
165 ERR("Failed to get rotation state from destruction handle");
166 goto skip_wait_rotation;
167 }
bbbfd849
JG
168 switch (rotation_state) {
169 case LTTNG_ROTATION_STATE_NO_ROTATION:
170 break;
171 case LTTNG_ROTATION_STATE_COMPLETED:
172 {
173 const struct lttng_trace_archive_location *location;
174
175 status = lttng_destruction_handle_get_archive_location(handle,
176 &location);
177 if (status == LTTNG_DESTRUCTION_HANDLE_STATUS_OK) {
178 if (printed_wait_msg) {
179 MSG("");
180 printed_wait_msg = false;
181 }
182 ret = print_trace_archive_location(location,
183 session->name);
184 if (ret) {
185 ERR("Failed to print the location of trace archive");
186 goto skip_wait_rotation;
187 }
188 break;
189 }
190 /* fall-through. */
191 }
192 default:
193 ERR("Failed to get the location of the rotation performed during the session's destruction");
194 goto skip_wait_rotation;
195 }
196skip_wait_rotation:
197 MSG("%sSession \"%s\" destroyed", printed_wait_msg ? "\n" : "",
198 session->name);
f16edb77 199 printed_wait_msg = false;
1dac0189
PPM
200
201 session_name = get_session_name_quiet();
202 if (session_name && !strncmp(session->name, session_name, NAME_MAX)) {
203 config_destroy_default();
204 }
65f25c66
JRJ
205
206 if (lttng_opt_mi) {
207 ret = mi_lttng_session(writer, session, 0);
208 if (ret) {
209 ret = CMD_ERROR;
210 goto error;
211 }
212 }
213
f3ed775e 214 ret = CMD_SUCCESS;
b09ee5ba 215error:
f16edb77
JG
216 if (printed_wait_msg) {
217 MSG("");
218 }
bbbfd849 219 lttng_destruction_handle_destroy(handle);
1dac0189 220 free(session_name);
b09ee5ba
FG
221 return ret;
222}
f3ed775e 223
b09ee5ba
FG
224/*
225 * destroy_all_sessions
226 *
227 * Call destroy_sessions for each registered sessions
228 */
65f25c66 229static int destroy_all_sessions(struct lttng_session *sessions, int count)
b09ee5ba 230{
65f25c66 231 int i, ret = CMD_SUCCESS;
b09ee5ba 232
b09ee5ba
FG
233 if (count == 0) {
234 MSG("No session found, nothing to do.");
60e835ca
DG
235 } else if (count < 0) {
236 ERR("%s", lttng_strerror(ret));
237 goto error;
b09ee5ba 238 }
60e835ca 239
b09ee5ba 240 for (i = 0; i < count; i++) {
65f25c66 241 ret = destroy_session(&sessions[i]);
b09ee5ba
FG
242 if (ret < 0) {
243 goto error;
244 }
b73d0b29 245 }
f3ed775e
DG
246error:
247 return ret;
248}
249
250/*
843f5df9 251 * The 'destroy <options>' first level command
f3ed775e
DG
252 */
253int cmd_destroy(int argc, const char **argv)
254{
b09ee5ba 255 int opt;
65f25c66 256 int ret = CMD_SUCCESS , i, command_ret = CMD_SUCCESS, success = 1;
f3ed775e 257 static poptContext pc;
b09ee5ba 258 char *session_name = NULL;
68c7f6e5 259 const char *leftover = NULL;
f3ed775e 260
65f25c66
JRJ
261 struct lttng_session *sessions;
262 int count;
263 int found;
264
f3ed775e
DG
265 pc = poptGetContext(NULL, argc, argv, long_options, 0);
266 poptReadDefaultConfig(pc, 0);
267
268 while ((opt = poptGetNextOpt(pc)) != -1) {
269 switch (opt) {
270 case OPT_HELP:
4ba92f18 271 SHOW_HELP();
b09ee5ba 272 break;
679b4943
SM
273 case OPT_LIST_OPTIONS:
274 list_cmd_options(stdout, long_options);
b09ee5ba 275 break;
f3ed775e 276 default:
f3ed775e 277 ret = CMD_UNDEFINED;
b09ee5ba 278 break;
f3ed775e 279 }
b09ee5ba 280 goto end;
f3ed775e
DG
281 }
282
65f25c66 283 /* Mi preparation */
c7e35b03 284 if (lttng_opt_mi) {
65f25c66
JRJ
285 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
286 if (!writer) {
287 ret = -LTTNG_ERR_NOMEM;
288 goto end;
289 }
290
291 /* Open command element */
292 ret = mi_lttng_writer_command_open(writer,
293 mi_lttng_element_command_destroy);
294 if (ret) {
295 ret = CMD_ERROR;
296 goto end;
297 }
298
299 /* Open output element */
300 ret = mi_lttng_writer_open_element(writer,
301 mi_lttng_element_command_output);
302 if (ret) {
303 ret = CMD_ERROR;
304 goto end;
305 }
306
307 /* For validation and semantic purpose we open a sessions element */
308 ret = mi_lttng_sessions_open(writer);
309 if (ret) {
310 ret = CMD_ERROR;
311 goto end;
312 }
313 }
314
315 /* Recuperate all sessions for further operation */
316 count = lttng_list_sessions(&sessions);
317 if (count < 0) {
56c3ec9e
JR
318 ERR("%s", lttng_strerror(count));
319 command_ret = CMD_ERROR;
65f25c66
JRJ
320 success = 0;
321 goto mi_closing;
c7e35b03
JR
322 }
323
fd076c09 324 /* Ignore session name in case all sessions are to be destroyed */
b09ee5ba 325 if (opt_destroy_all) {
65f25c66
JRJ
326 command_ret = destroy_all_sessions(sessions, count);
327 if (command_ret) {
328 success = 0;
329 }
330 } else {
331 opt_session_name = (char *) poptGetArg(pc);
332
1dac0189 333 if (!opt_session_name) {
65f25c66
JRJ
334 /* No session name specified, lookup default */
335 session_name = get_session_name();
336 if (session_name == NULL) {
337 command_ret = CMD_ERROR;
338 success = 0;
339 goto mi_closing;
340 }
341 } else {
342 session_name = opt_session_name;
343 }
fd076c09 344
65f25c66
JRJ
345 /* Find the corresponding lttng_session struct */
346 found = 0;
347 for (i = 0; i < count; i++) {
348 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
349 found = 1;
350 command_ret = destroy_session(&sessions[i]);
351 if (command_ret) {
352 success = 0;
353 }
fd076c09 354
65f25c66
JRJ
355 }
356 }
357
358 if (!found) {
359 ERR("Session name %s not found", session_name);
360 command_ret = LTTNG_ERR_SESS_NOT_FOUND;
361 success = 0;
362 goto mi_closing;
363 }
364 }
365
68c7f6e5
JD
366 leftover = poptGetArg(pc);
367 if (leftover) {
368 ERR("Unknown argument: %s", leftover);
369 ret = CMD_ERROR;
370 success = 0;
371 goto mi_closing;
372 }
373
65f25c66
JRJ
374mi_closing:
375 /* Mi closing */
376 if (lttng_opt_mi) {
377 /* Close sessions and output element element */
378 ret = mi_lttng_close_multi_element(writer, 2);
379 if (ret) {
fd076c09 380 ret = CMD_ERROR;
b09ee5ba
FG
381 goto end;
382 }
fd076c09 383
65f25c66
JRJ
384 /* Success ? */
385 ret = mi_lttng_writer_write_element_bool(writer,
386 mi_lttng_element_command_success, success);
387 if (ret) {
388 ret = CMD_ERROR;
389 goto end;
390 }
f3ed775e 391
65f25c66
JRJ
392 /* Command element close */
393 ret = mi_lttng_writer_command_close(writer);
394 if (ret) {
395 ret = CMD_ERROR;
396 goto end;
397 }
398 }
f3ed775e 399end:
65f25c66
JRJ
400 /* Mi clean-up */
401 if (writer && mi_lttng_writer_destroy(writer)) {
402 /* Preserve original error code */
403 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
404 }
405
fd076c09 406 if (opt_session_name == NULL) {
b09ee5ba
FG
407 free(session_name);
408 }
fd076c09 409
65f25c66
JRJ
410 /* Overwrite ret if an error occurred during destroy_session/all */
411 ret = command_ret ? command_ret : ret;
412
fd076c09 413 poptFreeContext(pc);
f3ed775e
DG
414 return ret;
415}
This page took 0.110017 seconds and 4 git commands to generate.