Revert "Fix: lttng-destroy: string formating error when default session is unset"
[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>
b50fbe86 12#include <common/make-unique-wrapper.hpp>
28ab034a 13#include <common/mi-lttng.hpp>
b50fbe86 14#include <common/scope-exit.hpp>
28ab034a
JG
15#include <common/sessiond-comm/sessiond-comm.hpp>
16#include <common/utils.hpp>
17
18#include <lttng/lttng.h>
19
f3ed775e 20#include <popt.h>
28ab034a 21#include <stdbool.h>
f3ed775e
DG
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27#include <unistd.h>
42224349 28
544349a3
JG
29enum {
30 OPT_HELP = 1,
31 OPT_LIST_OPTIONS,
32 OPT_ALL,
33 OPT_ENABLE_GLOB,
34};
f3ed775e 35
544349a3 36namespace {
4fc83d94 37#ifdef LTTNG_EMBED_HELP
544349a3 38const char help_msg[] =
4fc83d94 39#include <lttng-destroy.1.h>
28ab034a 40 ;
4fc83d94
PP
41#endif
42
544349a3 43int opt_no_wait;
65f25c66 44
544349a3
JG
45/* Mi writer */
46struct mi_writer *writer;
f3ed775e 47
544349a3 48struct poptOption long_options[] = {
f3ed775e 49 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
cd9adb8b 50 { "help", 'h', POPT_ARG_NONE, nullptr, OPT_HELP, nullptr, nullptr },
aabf6773
OD
51 { "all", 'a', POPT_ARG_NONE, nullptr, OPT_ALL, nullptr, nullptr },
52 { "glob", 'g', POPT_ARG_NONE, nullptr, OPT_ENABLE_GLOB, nullptr, nullptr },
cd9adb8b
JG
53 { "list-options", 0, POPT_ARG_NONE, nullptr, OPT_LIST_OPTIONS, nullptr, nullptr },
54 { "no-wait", 'n', POPT_ARG_VAL, &opt_no_wait, 1, nullptr, nullptr },
55 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
f3ed775e
DG
56};
57
f3ed775e 58/*
b09ee5ba
FG
59 * destroy_session
60 *
61 * Unregister the provided session to the session daemon. On success, removes
62 * the default configuration.
f3ed775e 63 */
b50fbe86 64cmd_error_code destroy_session(const lttng_session& session)
f3ed775e
DG
65{
66 int ret;
58f237ca 67 bool newline_needed = false, printed_destroy_msg = false;
b50fbe86
JG
68
69 const auto print_trailing_new_line = lttng::make_scope_exit([&newline_needed]() noexcept {
70 if (newline_needed) {
71 MSG("");
72 }
73 });
f3ed775e 74
aabf6773 75 ret = lttng_stop_tracing_no_wait(session.name);
e20ee7c2 76 if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
f9a41357 77 LTTNG_THROW_CTL(lttng::format("Failed to stop session `{}`", session.name),
b50fbe86 78 static_cast<lttng_error_code>(-ret));
e20ee7c2 79 }
58f237ca 80
b50fbe86 81 const auto session_was_already_stopped = ret == -LTTNG_ERR_TRACE_ALREADY_STOPPED;
e20ee7c2 82 if (!opt_no_wait) {
e20ee7c2 83 do {
aabf6773 84 ret = lttng_data_pending(session.name);
e20ee7c2
JD
85 if (ret < 0) {
86 /* Return the data available call error. */
b50fbe86
JG
87 ERR_FMT("Failed to check pending data for session `{}` ({})",
88 session.name,
89 lttng_strerror(ret));
90 return CMD_ERROR;
e20ee7c2
JD
91 }
92
93 /*
58f237ca
JG
94 * Data sleep time before retrying (in usec). Don't
95 * sleep if the call returned value indicates
96 * availability.
e20ee7c2
JD
97 */
98 if (ret) {
dec2c8e1 99 if (!printed_destroy_msg) {
b50fbe86 100 _MSG("Destroying session `%s`", session.name);
dec2c8e1
JG
101 newline_needed = true;
102 printed_destroy_msg = true;
103 fflush(stdout);
104 }
01f55be4 105
c8f61fd4 106 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US);
e20ee7c2
JD
107 _MSG(".");
108 fflush(stdout);
109 }
110 } while (ret != 0);
e20ee7c2 111 }
58f237ca 112
303ac4ed
JG
113 std::unique_ptr<char,
114 lttng::memory::create_deleter_class<char, lttng::memory::free>::deleter>
b50fbe86 115 stats_str;
58f237ca 116 if (!session_was_already_stopped) {
b50fbe86
JG
117 char *raw_stats_str = nullptr;
118
20fb9e02
JD
119 /*
120 * Don't print the event and packet loss warnings since the user
121 * already saw them when stopping the trace.
122 */
b50fbe86 123 ret = get_session_stats_str(session.name, &raw_stats_str);
58f237ca 124 if (ret < 0) {
b50fbe86 125 return CMD_ERROR;
58f237ca 126 }
e20ee7c2 127
b50fbe86
JG
128 /* May still be null if there are no stats to print. */
129 stats_str.reset(raw_stats_str);
f3ed775e
DG
130 }
131
b50fbe86
JG
132 const auto destruction_handle = [&session]() {
133 struct lttng_destruction_handle *raw_destruction_handle = nullptr;
134
135 auto ctl_ret_code =
136 lttng_destroy_session_ext(session.name, &raw_destruction_handle);
137 if (ctl_ret_code != LTTNG_OK) {
f9a41357
JG
138 LTTNG_THROW_CTL(lttng::format("Failed to destroy session `{}`",
139 session.name),
b50fbe86
JG
140 ctl_ret_code);
141 }
bbbfd849 142
b50fbe86
JG
143 return lttng::make_unique_wrapper<lttng_destruction_handle,
144 lttng_destruction_handle_destroy>(
145 raw_destruction_handle);
146 }();
147
148 if (!opt_no_wait) {
149 enum lttng_destruction_handle_status status;
150
151 do {
152 status = lttng_destruction_handle_wait_for_completion(
153 destruction_handle.get(),
154 DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US / USEC_PER_MSEC);
155 switch (status) {
156 case LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT:
157 if (!printed_destroy_msg) {
158 _MSG("Destroying session `%s`", session.name);
159 newline_needed = true;
160 printed_destroy_msg = true;
161 }
162 _MSG(".");
163 fflush(stdout);
164 break;
165 case LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED:
166 break;
167 default:
168 ERR_FMT("{}An error occurred during the destruction of session `{}`",
169 newline_needed ? "\n" : "",
170 session.name);
171 newline_needed = false;
172 return CMD_ERROR;
bbbfd849 173 }
b50fbe86
JG
174 } while (status == LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT);
175
176 enum lttng_error_code ctl_ret_code;
177 status = lttng_destruction_handle_get_result(destruction_handle.get(),
178 &ctl_ret_code);
179 if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_OK) {
180 ERR_FMT("{}Failed to query the result of the destruction of session `{}`",
181 newline_needed ? "\n" : "",
182 session.name);
183
58f237ca 184 newline_needed = false;
b50fbe86 185 return CMD_ERROR;
bbbfd849 186 }
bbbfd849 187
b50fbe86 188 if (ctl_ret_code != LTTNG_OK) {
f9a41357
JG
189 LTTNG_THROW_CTL(lttng::format("Failed to destroy session `{}`",
190 session.name),
b50fbe86
JG
191 ctl_ret_code);
192 }
bbbfd849 193
b50fbe86
JG
194 enum lttng_rotation_state rotation_state;
195 status = lttng_destruction_handle_get_rotation_state(destruction_handle.get(),
196 &rotation_state);
197 if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_OK) {
198 ERR_FMT("{}Failed to query the rotation state from the destruction handle of session `{}`",
199 newline_needed ? "\n" : "",
200 session.name);
201 newline_needed = false;
202 } else {
203 switch (rotation_state) {
204 case LTTNG_ROTATION_STATE_NO_ROTATION:
205 break;
206 case LTTNG_ROTATION_STATE_COMPLETED:
207 {
208 const struct lttng_trace_archive_location *location;
209
210 status = lttng_destruction_handle_get_archive_location(
211 destruction_handle.get(), &location);
212 if (status == LTTNG_DESTRUCTION_HANDLE_STATUS_OK) {
213 ret = print_trace_archive_location(location, session.name);
214 if (ret) {
215 ERR_FMT("{}Failed to print the location of the latest trace archive of session `{}`",
216 newline_needed ? "\n" : "",
217 session.name);
218 newline_needed = false;
219 }
220
221 break;
222 }
223 }
224 /* fall-through. */
225 default:
226 ERR_FMT("{}Failed to get the location of the rotation performed during the destruction of `{}`",
227 newline_needed ? "\n" : "",
228 session.name);
58f237ca 229 newline_needed = false;
b50fbe86 230 break;
bbbfd849 231 }
bbbfd849 232 }
58f237ca 233 }
b50fbe86
JG
234
235 MSG("%sSession `%s` destroyed", newline_needed ? "\n" : "", session.name);
58f237ca
JG
236 newline_needed = false;
237 if (stats_str) {
b50fbe86 238 MSG("%s", stats_str.get());
58f237ca 239 }
1dac0189 240
b50fbe86
JG
241 /*
242 * If the session being destroy is the "default" session as defined in the .lttngrc file,
243 * destroy the file.
244 */
245 const auto session_name =
303ac4ed 246 lttng::make_unique_wrapper<char, lttng::memory::free>(get_session_name_quiet());
b50fbe86 247 if (session_name && !strncmp(session.name, session_name.get(), NAME_MAX)) {
1dac0189
PPM
248 config_destroy_default();
249 }
65f25c66
JRJ
250
251 if (lttng_opt_mi) {
aabf6773 252 ret = mi_lttng_session(writer, &session, 0);
65f25c66 253 if (ret) {
b50fbe86 254 return CMD_ERROR;
65f25c66
JRJ
255 }
256 }
257
b50fbe86 258 return CMD_SUCCESS;
b09ee5ba 259}
f3ed775e 260
6e11909e 261cmd_error_code destroy_sessions(const lttng::cli::session_spec& spec)
b09ee5ba 262{
b50fbe86
JG
263 bool had_warning = false;
264 bool had_error = false;
544349a3
JG
265 bool listing_failed = false;
266
6e11909e 267 const auto sessions = [&listing_failed, &spec]() -> lttng::cli::session_list {
544349a3
JG
268 try {
269 return list_sessions(spec);
270 } catch (const lttng::ctl::error& ctl_exception) {
271 ERR_FMT("Failed to list sessions ({})",
272 lttng_strerror(-ctl_exception.code()));
273 listing_failed = true;
274 return {};
aabf6773 275 }
544349a3
JG
276 }();
277
6e11909e
JG
278 if (!listing_failed && sessions.size() == 0 &&
279 spec.type_ == lttng::cli::session_spec::type::NAME) {
b50fbe86
JG
280 ERR_FMT("Session `{}` not found", spec.value);
281 return CMD_ERROR;
282 }
283
284 if (listing_failed) {
285 return CMD_FATAL;
544349a3 286 }
aabf6773 287
544349a3 288 for (const auto& session : sessions) {
b50fbe86 289 cmd_error_code sub_ret;
aabf6773 290
b50fbe86
JG
291 try {
292 sub_ret = destroy_session(session);
293 } catch (const lttng::ctl::error& ctl_exception) {
294 switch (ctl_exception.code()) {
295 case LTTNG_ERR_NO_SESSION:
6e11909e 296 if (spec.type_ != lttng::cli::session_spec::type::NAME) {
b50fbe86
JG
297 /* Session destroyed during command, ignore and carry-on. */
298 sub_ret = CMD_SUCCESS;
299 break;
300 } else {
301 sub_ret = CMD_ERROR;
302 break;
303 }
304 case LTTNG_ERR_NO_SESSIOND:
305 /* Don't keep going on a fatal error. */
306 return CMD_FATAL;
307 default:
308 /* Generic error. */
309 sub_ret = CMD_ERROR;
310 ERR_FMT("Failed to destroy session `{}` ({})",
311 session.name,
312 lttng_strerror(-ctl_exception.code()));
313 break;
314 }
544349a3 315 }
b50fbe86
JG
316
317 /* Keep going, but report the most serious state. */
318 had_warning |= sub_ret == CMD_WARNING;
319 had_error |= sub_ret == CMD_ERROR;
b73d0b29 320 }
3285a971 321
b50fbe86
JG
322 if (had_error) {
323 return CMD_ERROR;
324 } else if (had_warning) {
325 return CMD_WARNING;
326 } else {
327 return CMD_SUCCESS;
328 }
f3ed775e 329}
544349a3 330} /* namespace */
f3ed775e
DG
331
332/*
843f5df9 333 * The 'destroy <options>' first level command
f3ed775e
DG
334 */
335int cmd_destroy(int argc, const char **argv)
336{
b09ee5ba 337 int opt;
544349a3 338 cmd_error_code command_ret = CMD_SUCCESS;
aabf6773 339 bool success;
f3ed775e 340 static poptContext pc;
cd9adb8b 341 const char *leftover = nullptr;
6e11909e
JG
342 lttng::cli::session_spec spec(lttng::cli::session_spec::type::NAME);
343 lttng::cli::session_list const sessions;
65f25c66 344
cd9adb8b 345 pc = poptGetContext(nullptr, argc, argv, long_options, 0);
f3ed775e
DG
346 poptReadDefaultConfig(pc, 0);
347
348 while ((opt = poptGetNextOpt(pc)) != -1) {
349 switch (opt) {
350 case OPT_HELP:
544349a3
JG
351 {
352 int ret;
353
4ba92f18 354 SHOW_HELP();
544349a3 355 command_ret = static_cast<cmd_error_code>(ret);
aabf6773 356 goto end;
544349a3 357 }
679b4943
SM
358 case OPT_LIST_OPTIONS:
359 list_cmd_options(stdout, long_options);
aabf6773
OD
360 goto end;
361 case OPT_ALL:
6e11909e 362 spec.type_ = lttng::cli::session_spec::type::ALL;
aabf6773
OD
363 break;
364 case OPT_ENABLE_GLOB:
6e11909e 365 spec.type_ = lttng::cli::session_spec::type::GLOB_PATTERN;
b09ee5ba 366 break;
f3ed775e 367 default:
544349a3 368 command_ret = CMD_UNDEFINED;
aabf6773 369 goto end;
f3ed775e
DG
370 }
371 }
372
65f25c66 373 /* Mi preparation */
c7e35b03 374 if (lttng_opt_mi) {
65f25c66
JRJ
375 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
376 if (!writer) {
544349a3 377 command_ret = CMD_ERROR;
65f25c66
JRJ
378 goto end;
379 }
380
381 /* Open command element */
544349a3
JG
382 if (mi_lttng_writer_command_open(writer, mi_lttng_element_command_destroy)) {
383 command_ret = CMD_ERROR;
65f25c66
JRJ
384 goto end;
385 }
386
387 /* Open output element */
544349a3
JG
388 if (mi_lttng_writer_open_element(writer, mi_lttng_element_command_output)) {
389 command_ret = CMD_ERROR;
65f25c66
JRJ
390 goto end;
391 }
392
393 /* For validation and semantic purpose we open a sessions element */
544349a3
JG
394 if (mi_lttng_sessions_open(writer)) {
395 command_ret = CMD_ERROR;
65f25c66
JRJ
396 goto end;
397 }
398 }
399
aabf6773 400 spec.value = poptGetArg(pc);
fd076c09 401
aabf6773 402 command_ret = destroy_sessions(spec);
65f25c66 403
aabf6773 404 success = command_ret == CMD_SUCCESS;
65f25c66 405
68c7f6e5
JD
406 leftover = poptGetArg(pc);
407 if (leftover) {
408 ERR("Unknown argument: %s", leftover);
544349a3 409 command_ret = CMD_ERROR;
aabf6773 410 success = false;
68c7f6e5
JD
411 }
412
65f25c66
JRJ
413 /* Mi closing */
414 if (lttng_opt_mi) {
415 /* Close sessions and output element element */
544349a3 416 if (mi_lttng_close_multi_element(writer, 2)) {
b50fbe86 417 command_ret = CMD_ERROR;
b09ee5ba
FG
418 goto end;
419 }
fd076c09 420
65f25c66 421 /* Success ? */
544349a3 422 if (mi_lttng_writer_write_element_bool(
b50fbe86
JG
423 writer, mi_lttng_element_command_success, success)) {
424 command_ret = CMD_ERROR;
65f25c66
JRJ
425 goto end;
426 }
f3ed775e 427
65f25c66 428 /* Command element close */
544349a3 429 if (mi_lttng_writer_command_close(writer)) {
b50fbe86 430 command_ret = CMD_ERROR;
65f25c66
JRJ
431 goto end;
432 }
433 }
f3ed775e 434end:
65f25c66
JRJ
435 /* Mi clean-up */
436 if (writer && mi_lttng_writer_destroy(writer)) {
544349a3 437 command_ret = CMD_ERROR;
65f25c66
JRJ
438 }
439
fd076c09 440 poptFreeContext(pc);
544349a3 441 return command_ret;
f3ed775e 442}
This page took 0.099187 seconds and 4 git commands to generate.