| 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/mi-lttng.hpp> |
| 12 | #include <common/sessiond-comm/sessiond-comm.hpp> |
| 13 | #include <common/utils.hpp> |
| 14 | |
| 15 | #include <lttng/lttng.h> |
| 16 | |
| 17 | #include <popt.h> |
| 18 | #include <stdbool.h> |
| 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> |
| 25 | |
| 26 | static int opt_destroy_all; |
| 27 | static int opt_no_wait; |
| 28 | |
| 29 | #ifdef LTTNG_EMBED_HELP |
| 30 | static const char help_msg[] = |
| 31 | #include <lttng-destroy.1.h> |
| 32 | ; |
| 33 | #endif |
| 34 | |
| 35 | /* Mi writer */ |
| 36 | static struct mi_writer *writer; |
| 37 | |
| 38 | enum { |
| 39 | OPT_HELP = 1, |
| 40 | OPT_LIST_OPTIONS, |
| 41 | }; |
| 42 | |
| 43 | static struct poptOption long_options[] = { |
| 44 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 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 } |
| 50 | }; |
| 51 | |
| 52 | /* |
| 53 | * destroy_session |
| 54 | * |
| 55 | * Unregister the provided session to the session daemon. On success, removes |
| 56 | * the default configuration. |
| 57 | */ |
| 58 | static int destroy_session(struct lttng_session *session) |
| 59 | { |
| 60 | int ret; |
| 61 | char *session_name = nullptr; |
| 62 | bool session_was_already_stopped; |
| 63 | enum lttng_error_code ret_code; |
| 64 | struct lttng_destruction_handle *handle = nullptr; |
| 65 | enum lttng_destruction_handle_status status; |
| 66 | bool newline_needed = false, printed_destroy_msg = false; |
| 67 | enum lttng_rotation_state rotation_state; |
| 68 | char *stats_str = nullptr; |
| 69 | |
| 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 | } |
| 74 | |
| 75 | session_was_already_stopped = ret == -LTTNG_ERR_TRACE_ALREADY_STOPPED; |
| 76 | if (!opt_no_wait) { |
| 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 | /* |
| 85 | * Data sleep time before retrying (in usec). Don't |
| 86 | * sleep if the call returned value indicates |
| 87 | * availability. |
| 88 | */ |
| 89 | if (ret) { |
| 90 | if (!printed_destroy_msg) { |
| 91 | _MSG("Destroying session %s", session->name); |
| 92 | newline_needed = true; |
| 93 | printed_destroy_msg = true; |
| 94 | fflush(stdout); |
| 95 | } |
| 96 | |
| 97 | usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US); |
| 98 | _MSG("."); |
| 99 | fflush(stdout); |
| 100 | } |
| 101 | } while (ret != 0); |
| 102 | } |
| 103 | |
| 104 | if (!session_was_already_stopped) { |
| 105 | /* |
| 106 | * Don't print the event and packet loss warnings since the user |
| 107 | * already saw them when stopping the trace. |
| 108 | */ |
| 109 | ret = get_session_stats_str(session->name, &stats_str); |
| 110 | if (ret < 0) { |
| 111 | goto error; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | ret_code = lttng_destroy_session_ext(session->name, &handle); |
| 116 | if (ret_code != LTTNG_OK) { |
| 117 | ret = -ret_code; |
| 118 | goto error; |
| 119 | } |
| 120 | |
| 121 | if (opt_no_wait) { |
| 122 | goto skip_wait_rotation; |
| 123 | } |
| 124 | |
| 125 | do { |
| 126 | status = lttng_destruction_handle_wait_for_completion( |
| 127 | handle, DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US / USEC_PER_MSEC); |
| 128 | switch (status) { |
| 129 | case LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT: |
| 130 | if (!printed_destroy_msg) { |
| 131 | _MSG("Destroying session %s", session->name); |
| 132 | newline_needed = true; |
| 133 | printed_destroy_msg = true; |
| 134 | } |
| 135 | _MSG("."); |
| 136 | fflush(stdout); |
| 137 | break; |
| 138 | case LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED: |
| 139 | break; |
| 140 | default: |
| 141 | ERR("%sFailed to wait for the completion of the destruction of session \"%s\"", |
| 142 | newline_needed ? "\n" : "", |
| 143 | session->name); |
| 144 | newline_needed = false; |
| 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) { |
| 152 | ERR("%sFailed to get the result of session destruction", |
| 153 | newline_needed ? "\n" : ""); |
| 154 | ret = -1; |
| 155 | newline_needed = false; |
| 156 | goto error; |
| 157 | } |
| 158 | if (ret_code != LTTNG_OK) { |
| 159 | ret = -ret_code; |
| 160 | goto error; |
| 161 | } |
| 162 | |
| 163 | status = lttng_destruction_handle_get_rotation_state(handle, &rotation_state); |
| 164 | if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_OK) { |
| 165 | ERR("%sFailed to get rotation state from destruction handle", |
| 166 | newline_needed ? "\n" : ""); |
| 167 | newline_needed = false; |
| 168 | goto skip_wait_rotation; |
| 169 | } |
| 170 | |
| 171 | switch (rotation_state) { |
| 172 | case LTTNG_ROTATION_STATE_NO_ROTATION: |
| 173 | break; |
| 174 | case LTTNG_ROTATION_STATE_COMPLETED: |
| 175 | { |
| 176 | const struct lttng_trace_archive_location *location; |
| 177 | |
| 178 | status = lttng_destruction_handle_get_archive_location(handle, &location); |
| 179 | if (status == LTTNG_DESTRUCTION_HANDLE_STATUS_OK) { |
| 180 | ret = print_trace_archive_location(location, session->name); |
| 181 | if (ret) { |
| 182 | ERR("%sFailed to print the location of trace archive", |
| 183 | newline_needed ? "\n" : ""); |
| 184 | newline_needed = false; |
| 185 | goto skip_wait_rotation; |
| 186 | } |
| 187 | break; |
| 188 | } |
| 189 | } |
| 190 | /* fall-through. */ |
| 191 | default: |
| 192 | ERR("%sFailed to get the location of the rotation performed during the session's destruction", |
| 193 | newline_needed ? "\n" : ""); |
| 194 | newline_needed = false; |
| 195 | goto skip_wait_rotation; |
| 196 | } |
| 197 | skip_wait_rotation: |
| 198 | MSG("%sSession %s destroyed", newline_needed ? "\n" : "", session->name); |
| 199 | newline_needed = false; |
| 200 | if (stats_str) { |
| 201 | MSG("%s", stats_str); |
| 202 | } |
| 203 | |
| 204 | session_name = get_session_name_quiet(); |
| 205 | if (session_name && !strncmp(session->name, session_name, NAME_MAX)) { |
| 206 | config_destroy_default(); |
| 207 | } |
| 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 | |
| 217 | ret = CMD_SUCCESS; |
| 218 | error: |
| 219 | if (newline_needed) { |
| 220 | MSG(""); |
| 221 | } |
| 222 | lttng_destruction_handle_destroy(handle); |
| 223 | free(session_name); |
| 224 | free(stats_str); |
| 225 | return ret; |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * destroy_all_sessions |
| 230 | * |
| 231 | * Call destroy_sessions for each registered sessions |
| 232 | */ |
| 233 | static int destroy_all_sessions(struct lttng_session *sessions, int count) |
| 234 | { |
| 235 | int i; |
| 236 | bool error_occurred = false; |
| 237 | |
| 238 | LTTNG_ASSERT(count >= 0); |
| 239 | if (count == 0) { |
| 240 | MSG("No session found, nothing to do."); |
| 241 | } |
| 242 | |
| 243 | for (i = 0; i < count; i++) { |
| 244 | int ret = destroy_session(&sessions[i]); |
| 245 | |
| 246 | if (ret < 0) { |
| 247 | ERR("%s during the destruction of session \"%s\"", |
| 248 | lttng_strerror(ret), |
| 249 | sessions[i].name); |
| 250 | /* Continue to next session. */ |
| 251 | error_occurred = true; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | return error_occurred ? CMD_ERROR : CMD_SUCCESS; |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * The 'destroy <options>' first level command |
| 260 | */ |
| 261 | int cmd_destroy(int argc, const char **argv) |
| 262 | { |
| 263 | int opt; |
| 264 | int ret = CMD_SUCCESS, i, command_ret = CMD_SUCCESS, success = 1; |
| 265 | static poptContext pc; |
| 266 | char *session_name = nullptr; |
| 267 | const char *arg_session_name = nullptr; |
| 268 | const char *leftover = nullptr; |
| 269 | |
| 270 | struct lttng_session *sessions = nullptr; |
| 271 | int count; |
| 272 | int found; |
| 273 | |
| 274 | pc = poptGetContext(nullptr, argc, argv, long_options, 0); |
| 275 | poptReadDefaultConfig(pc, 0); |
| 276 | |
| 277 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 278 | switch (opt) { |
| 279 | case OPT_HELP: |
| 280 | SHOW_HELP(); |
| 281 | break; |
| 282 | case OPT_LIST_OPTIONS: |
| 283 | list_cmd_options(stdout, long_options); |
| 284 | break; |
| 285 | default: |
| 286 | ret = CMD_UNDEFINED; |
| 287 | break; |
| 288 | } |
| 289 | goto end; |
| 290 | } |
| 291 | |
| 292 | /* Mi preparation */ |
| 293 | if (lttng_opt_mi) { |
| 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 */ |
| 301 | ret = mi_lttng_writer_command_open(writer, mi_lttng_element_command_destroy); |
| 302 | if (ret) { |
| 303 | ret = CMD_ERROR; |
| 304 | goto end; |
| 305 | } |
| 306 | |
| 307 | /* Open output element */ |
| 308 | ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command_output); |
| 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) { |
| 325 | ERR("%s", lttng_strerror(count)); |
| 326 | command_ret = CMD_ERROR; |
| 327 | success = 0; |
| 328 | goto mi_closing; |
| 329 | } |
| 330 | |
| 331 | /* Ignore session name in case all sessions are to be destroyed */ |
| 332 | if (opt_destroy_all) { |
| 333 | command_ret = destroy_all_sessions(sessions, count); |
| 334 | if (command_ret) { |
| 335 | success = 0; |
| 336 | } |
| 337 | } else { |
| 338 | arg_session_name = poptGetArg(pc); |
| 339 | |
| 340 | if (!arg_session_name) { |
| 341 | /* No session name specified, lookup default */ |
| 342 | session_name = get_session_name(); |
| 343 | } else { |
| 344 | session_name = strdup(arg_session_name); |
| 345 | if (session_name == nullptr) { |
| 346 | PERROR("Failed to copy session name"); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | if (session_name == nullptr) { |
| 351 | command_ret = CMD_ERROR; |
| 352 | success = 0; |
| 353 | goto mi_closing; |
| 354 | } |
| 355 | |
| 356 | /* Find the corresponding lttng_session struct */ |
| 357 | found = 0; |
| 358 | for (i = 0; i < count; i++) { |
| 359 | if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) { |
| 360 | found = 1; |
| 361 | command_ret = destroy_session(&sessions[i]); |
| 362 | if (command_ret) { |
| 363 | success = 0; |
| 364 | ERR("%s during the destruction of session \"%s\"", |
| 365 | lttng_strerror(command_ret), |
| 366 | sessions[i].name); |
| 367 | } |
| 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 | |
| 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 | |
| 387 | mi_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) { |
| 393 | ret = CMD_ERROR; |
| 394 | goto end; |
| 395 | } |
| 396 | |
| 397 | /* Success ? */ |
| 398 | ret = mi_lttng_writer_write_element_bool( |
| 399 | writer, mi_lttng_element_command_success, success); |
| 400 | if (ret) { |
| 401 | ret = CMD_ERROR; |
| 402 | goto end; |
| 403 | } |
| 404 | |
| 405 | /* Command element close */ |
| 406 | ret = mi_lttng_writer_command_close(writer); |
| 407 | if (ret) { |
| 408 | ret = CMD_ERROR; |
| 409 | goto end; |
| 410 | } |
| 411 | } |
| 412 | end: |
| 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 | |
| 419 | free(session_name); |
| 420 | free(sessions); |
| 421 | |
| 422 | /* Overwrite ret if an error occurred during destroy_session/all */ |
| 423 | ret = command_ret ? command_ret : ret; |
| 424 | |
| 425 | poptFreeContext(pc); |
| 426 | return ret; |
| 427 | } |