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