lttng: Add --glob option to lttng-destroy
[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
aabf6773 11#include <common/exception.hpp>
28ab034a
JG
12#include <common/mi-lttng.hpp>
13#include <common/sessiond-comm/sessiond-comm.hpp>
14#include <common/utils.hpp>
15
16#include <lttng/lttng.h>
17
f3ed775e 18#include <popt.h>
28ab034a 19#include <stdbool.h>
f3ed775e
DG
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>
42224349 26
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,
aabf6773
OD
41 OPT_ALL,
42 OPT_ENABLE_GLOB,
f3ed775e
DG
43};
44
45static struct poptOption long_options[] = {
46 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
cd9adb8b 47 { "help", 'h', POPT_ARG_NONE, nullptr, OPT_HELP, nullptr, nullptr },
aabf6773
OD
48 { "all", 'a', POPT_ARG_NONE, nullptr, OPT_ALL, nullptr, nullptr },
49 { "glob", 'g', POPT_ARG_NONE, nullptr, OPT_ENABLE_GLOB, nullptr, nullptr },
cd9adb8b
JG
50 { "list-options", 0, POPT_ARG_NONE, nullptr, OPT_LIST_OPTIONS, nullptr, nullptr },
51 { "no-wait", 'n', POPT_ARG_VAL, &opt_no_wait, 1, nullptr, nullptr },
52 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
f3ed775e
DG
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 */
aabf6773 61static int destroy_session(const struct lttng_session& session)
f3ed775e
DG
62{
63 int ret;
cd9adb8b 64 char *session_name = nullptr;
58f237ca 65 bool session_was_already_stopped;
bbbfd849 66 enum lttng_error_code ret_code;
cd9adb8b 67 struct lttng_destruction_handle *handle = nullptr;
58f237ca
JG
68 enum lttng_destruction_handle_status status;
69 bool newline_needed = false, printed_destroy_msg = false;
bbbfd849 70 enum lttng_rotation_state rotation_state;
cd9adb8b 71 char *stats_str = nullptr;
f3ed775e 72
aabf6773 73 ret = lttng_stop_tracing_no_wait(session.name);
e20ee7c2
JD
74 if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
75 ERR("%s", lttng_strerror(ret));
76 }
58f237ca
JG
77
78 session_was_already_stopped = ret == -LTTNG_ERR_TRACE_ALREADY_STOPPED;
e20ee7c2 79 if (!opt_no_wait) {
e20ee7c2 80 do {
aabf6773 81 ret = lttng_data_pending(session.name);
e20ee7c2
JD
82 if (ret < 0) {
83 /* Return the data available call error. */
84 goto error;
85 }
86
87 /*
58f237ca
JG
88 * Data sleep time before retrying (in usec). Don't
89 * sleep if the call returned value indicates
90 * availability.
e20ee7c2
JD
91 */
92 if (ret) {
dec2c8e1 93 if (!printed_destroy_msg) {
aabf6773 94 _MSG("Destroying session %s", session.name);
dec2c8e1
JG
95 newline_needed = true;
96 printed_destroy_msg = true;
97 fflush(stdout);
98 }
01f55be4 99
c8f61fd4 100 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US);
e20ee7c2
JD
101 _MSG(".");
102 fflush(stdout);
103 }
104 } while (ret != 0);
e20ee7c2 105 }
58f237ca
JG
106
107 if (!session_was_already_stopped) {
20fb9e02
JD
108 /*
109 * Don't print the event and packet loss warnings since the user
110 * already saw them when stopping the trace.
111 */
aabf6773 112 ret = get_session_stats_str(session.name, &stats_str);
58f237ca
JG
113 if (ret < 0) {
114 goto error;
115 }
20fb9e02 116 }
e20ee7c2 117
aabf6773 118 ret_code = lttng_destroy_session_ext(session.name, &handle);
bbbfd849
JG
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 {
58f237ca 129 status = lttng_destruction_handle_wait_for_completion(
28ab034a 130 handle, DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US / USEC_PER_MSEC);
bbbfd849
JG
131 switch (status) {
132 case LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT:
58f237ca 133 if (!printed_destroy_msg) {
aabf6773 134 _MSG("Destroying session %s", session.name);
58f237ca
JG
135 newline_needed = true;
136 printed_destroy_msg = true;
bbbfd849
JG
137 }
138 _MSG(".");
139 fflush(stdout);
140 break;
141 case LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED:
142 break;
143 default:
58f237ca 144 ERR("%sFailed to wait for the completion of the destruction of session \"%s\"",
28ab034a 145 newline_needed ? "\n" : "",
aabf6773 146 session.name);
58f237ca 147 newline_needed = false;
bbbfd849
JG
148 ret = -1;
149 goto error;
150 }
151 } while (status == LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT);
152
153 status = lttng_destruction_handle_get_result(handle, &ret_code);
154 if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_OK) {
58f237ca 155 ERR("%sFailed to get the result of session destruction",
28ab034a 156 newline_needed ? "\n" : "");
bbbfd849 157 ret = -1;
58f237ca 158 newline_needed = false;
bbbfd849
JG
159 goto error;
160 }
161 if (ret_code != LTTNG_OK) {
3285a971 162 ret = -ret_code;
bbbfd849
JG
163 goto error;
164 }
165
28ab034a 166 status = lttng_destruction_handle_get_rotation_state(handle, &rotation_state);
7d81aa9f 167 if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_OK) {
58f237ca 168 ERR("%sFailed to get rotation state from destruction handle",
28ab034a 169 newline_needed ? "\n" : "");
58f237ca 170 newline_needed = false;
7d81aa9f
JG
171 goto skip_wait_rotation;
172 }
58f237ca 173
bbbfd849
JG
174 switch (rotation_state) {
175 case LTTNG_ROTATION_STATE_NO_ROTATION:
176 break;
58f237ca 177 case LTTNG_ROTATION_STATE_COMPLETED:
bbbfd849
JG
178 {
179 const struct lttng_trace_archive_location *location;
180
28ab034a 181 status = lttng_destruction_handle_get_archive_location(handle, &location);
bbbfd849 182 if (status == LTTNG_DESTRUCTION_HANDLE_STATUS_OK) {
aabf6773 183 ret = print_trace_archive_location(location, session.name);
bbbfd849 184 if (ret) {
58f237ca 185 ERR("%sFailed to print the location of trace archive",
28ab034a 186 newline_needed ? "\n" : "");
58f237ca 187 newline_needed = false;
bbbfd849
JG
188 goto skip_wait_rotation;
189 }
190 break;
191 }
58f237ca 192 }
30eb3927 193 /* fall-through. */
58f237ca
JG
194 default:
195 ERR("%sFailed to get the location of the rotation performed during the session's destruction",
28ab034a 196 newline_needed ? "\n" : "");
58f237ca 197 newline_needed = false;
bbbfd849
JG
198 goto skip_wait_rotation;
199 }
200skip_wait_rotation:
aabf6773 201 MSG("%sSession %s destroyed", newline_needed ? "\n" : "", session.name);
58f237ca
JG
202 newline_needed = false;
203 if (stats_str) {
204 MSG("%s", stats_str);
205 }
1dac0189
PPM
206
207 session_name = get_session_name_quiet();
aabf6773 208 if (session_name && !strncmp(session.name, session_name, NAME_MAX)) {
1dac0189
PPM
209 config_destroy_default();
210 }
65f25c66
JRJ
211
212 if (lttng_opt_mi) {
aabf6773 213 ret = mi_lttng_session(writer, &session, 0);
65f25c66
JRJ
214 if (ret) {
215 ret = CMD_ERROR;
216 goto error;
217 }
218 }
219
f3ed775e 220 ret = CMD_SUCCESS;
b09ee5ba 221error:
58f237ca 222 if (newline_needed) {
f16edb77
JG
223 MSG("");
224 }
bbbfd849 225 lttng_destruction_handle_destroy(handle);
1dac0189 226 free(session_name);
58f237ca 227 free(stats_str);
b09ee5ba
FG
228 return ret;
229}
f3ed775e 230
aabf6773 231static int destroy_sessions(const struct session_spec& spec)
b09ee5ba 232{
aabf6773
OD
233 try {
234 auto sessions = list_sessions(spec);
235 int ret = CMD_SUCCESS;
236
237 if (sessions.size() == 0) {
238 switch (spec.type) {
239 case session_spec::ALL: /* fall throught */
240 case session_spec::GLOB_PATTERN:
241 MSG("No session found, nothing to do.");
242 break;
243 case session_spec::NAME:
244 ERR("Session name %s not found", spec.value);
245 ret = LTTNG_ERR_SESS_NOT_FOUND;
246 break;
247 }
b09ee5ba 248
aabf6773
OD
249 return ret;
250 }
60e835ca 251
aabf6773
OD
252 for (const auto& session : sessions) {
253 int const sub_ret = destroy_session(session);
3285a971 254
aabf6773
OD
255 if (sub_ret != CMD_SUCCESS) {
256 ERR("%s during the destruction of session \"%s\"",
257 lttng_strerror(sub_ret),
258 session.name);
259 ret = CMD_ERROR;
260 }
b09ee5ba 261 }
aabf6773
OD
262
263 return ret;
264
265 } catch (lttng::ctl::error& error) {
266 ERR("%s", lttng_strerror(error.code()));
b73d0b29 267 }
3285a971 268
aabf6773 269 return CMD_ERROR;
f3ed775e
DG
270}
271
272/*
843f5df9 273 * The 'destroy <options>' first level command
f3ed775e
DG
274 */
275int cmd_destroy(int argc, const char **argv)
276{
b09ee5ba 277 int opt;
aabf6773
OD
278 int ret = CMD_SUCCESS, command_ret = CMD_SUCCESS;
279 bool success;
f3ed775e 280 static poptContext pc;
cd9adb8b 281 const char *leftover = nullptr;
aabf6773
OD
282 struct session_spec spec = {
283 .type = session_spec::NAME,
284 .value = nullptr,
285 };
286 session_list const sessions;
65f25c66 287
cd9adb8b 288 pc = poptGetContext(nullptr, argc, argv, long_options, 0);
f3ed775e
DG
289 poptReadDefaultConfig(pc, 0);
290
291 while ((opt = poptGetNextOpt(pc)) != -1) {
292 switch (opt) {
293 case OPT_HELP:
4ba92f18 294 SHOW_HELP();
aabf6773 295 goto end;
679b4943
SM
296 case OPT_LIST_OPTIONS:
297 list_cmd_options(stdout, long_options);
aabf6773
OD
298 goto end;
299 case OPT_ALL:
300 spec.type = session_spec::ALL;
301 break;
302 case OPT_ENABLE_GLOB:
303 spec.type = session_spec::GLOB_PATTERN;
b09ee5ba 304 break;
f3ed775e 305 default:
f3ed775e 306 ret = CMD_UNDEFINED;
aabf6773 307 goto end;
f3ed775e
DG
308 }
309 }
310
65f25c66 311 /* Mi preparation */
c7e35b03 312 if (lttng_opt_mi) {
65f25c66
JRJ
313 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
314 if (!writer) {
315 ret = -LTTNG_ERR_NOMEM;
316 goto end;
317 }
318
319 /* Open command element */
28ab034a 320 ret = mi_lttng_writer_command_open(writer, mi_lttng_element_command_destroy);
65f25c66
JRJ
321 if (ret) {
322 ret = CMD_ERROR;
323 goto end;
324 }
325
326 /* Open output element */
28ab034a 327 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command_output);
65f25c66
JRJ
328 if (ret) {
329 ret = CMD_ERROR;
330 goto end;
331 }
332
333 /* For validation and semantic purpose we open a sessions element */
334 ret = mi_lttng_sessions_open(writer);
335 if (ret) {
336 ret = CMD_ERROR;
337 goto end;
338 }
339 }
340
aabf6773 341 spec.value = poptGetArg(pc);
fd076c09 342
aabf6773 343 command_ret = destroy_sessions(spec);
65f25c66 344
aabf6773 345 success = command_ret == CMD_SUCCESS;
65f25c66 346
68c7f6e5
JD
347 leftover = poptGetArg(pc);
348 if (leftover) {
349 ERR("Unknown argument: %s", leftover);
350 ret = CMD_ERROR;
aabf6773 351 success = false;
68c7f6e5
JD
352 }
353
65f25c66
JRJ
354 /* Mi closing */
355 if (lttng_opt_mi) {
356 /* Close sessions and output element element */
357 ret = mi_lttng_close_multi_element(writer, 2);
358 if (ret) {
fd076c09 359 ret = CMD_ERROR;
b09ee5ba
FG
360 goto end;
361 }
fd076c09 362
65f25c66 363 /* Success ? */
28ab034a
JG
364 ret = mi_lttng_writer_write_element_bool(
365 writer, mi_lttng_element_command_success, success);
65f25c66
JRJ
366 if (ret) {
367 ret = CMD_ERROR;
368 goto end;
369 }
f3ed775e 370
65f25c66
JRJ
371 /* Command element close */
372 ret = mi_lttng_writer_command_close(writer);
373 if (ret) {
374 ret = CMD_ERROR;
375 goto end;
376 }
377 }
f3ed775e 378end:
65f25c66
JRJ
379 /* Mi clean-up */
380 if (writer && mi_lttng_writer_destroy(writer)) {
381 /* Preserve original error code */
382 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
383 }
384
65f25c66
JRJ
385 /* Overwrite ret if an error occurred during destroy_session/all */
386 ret = command_ret ? command_ret : ret;
387
fd076c09 388 poptFreeContext(pc);
f3ed775e
DG
389 return ret;
390}
This page took 0.092461 seconds and 4 git commands to generate.