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