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