| 1 | /* |
| 2 | * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com> |
| 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_clear_all; |
| 27 | |
| 28 | #ifdef LTTNG_EMBED_HELP |
| 29 | static const char help_msg[] = |
| 30 | #include <lttng-clear.1.h> |
| 31 | ; |
| 32 | #endif |
| 33 | |
| 34 | /* Mi writer */ |
| 35 | static struct mi_writer *writer; |
| 36 | |
| 37 | enum { |
| 38 | OPT_HELP = 1, |
| 39 | OPT_LIST_OPTIONS, |
| 40 | }; |
| 41 | |
| 42 | static struct poptOption long_options[] = { |
| 43 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 44 | { "help", 'h', POPT_ARG_NONE, nullptr, OPT_HELP, nullptr, nullptr }, |
| 45 | { "all", 'a', POPT_ARG_VAL, &opt_clear_all, 1, nullptr, nullptr }, |
| 46 | { "list-options", 0, POPT_ARG_NONE, nullptr, OPT_LIST_OPTIONS, nullptr, nullptr }, |
| 47 | { nullptr, 0, 0, nullptr, 0, nullptr, nullptr } |
| 48 | }; |
| 49 | |
| 50 | /* |
| 51 | * clear session |
| 52 | */ |
| 53 | static int clear_session(struct lttng_session *session) |
| 54 | { |
| 55 | enum lttng_clear_handle_status status = LTTNG_CLEAR_HANDLE_STATUS_OK; |
| 56 | struct lttng_clear_handle *handle = nullptr; |
| 57 | enum lttng_error_code ret_code; |
| 58 | bool printed_wait_msg = false; |
| 59 | int ret; |
| 60 | |
| 61 | ret = lttng_clear_session(session->name, &handle); |
| 62 | if (ret < 0) { |
| 63 | ERR("%s", lttng_strerror(ret)); |
| 64 | goto error; |
| 65 | } |
| 66 | |
| 67 | do { |
| 68 | status = lttng_clear_handle_wait_for_completion( |
| 69 | handle, DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US / USEC_PER_MSEC); |
| 70 | switch (status) { |
| 71 | case LTTNG_CLEAR_HANDLE_STATUS_TIMEOUT: |
| 72 | if (!printed_wait_msg) { |
| 73 | _MSG("Waiting for clear of session \"%s\"", session->name); |
| 74 | printed_wait_msg = true; |
| 75 | } |
| 76 | _MSG("."); |
| 77 | fflush(stdout); |
| 78 | break; |
| 79 | case LTTNG_CLEAR_HANDLE_STATUS_COMPLETED: |
| 80 | break; |
| 81 | default: |
| 82 | ERR("Failed to wait for the completion of clear for session \"%s\"", |
| 83 | session->name); |
| 84 | ret = -1; |
| 85 | goto error; |
| 86 | } |
| 87 | } while (status == LTTNG_CLEAR_HANDLE_STATUS_TIMEOUT); |
| 88 | |
| 89 | status = lttng_clear_handle_get_result(handle, &ret_code); |
| 90 | if (status != LTTNG_CLEAR_HANDLE_STATUS_OK) { |
| 91 | ERR("Failed to get the result of session clear"); |
| 92 | ret = -1; |
| 93 | goto error; |
| 94 | } |
| 95 | if (ret_code != LTTNG_OK) { |
| 96 | ret = -ret_code; |
| 97 | goto error; |
| 98 | } |
| 99 | |
| 100 | MSG("%sSession \"%s\" cleared", printed_wait_msg ? "\n" : "", session->name); |
| 101 | printed_wait_msg = false; |
| 102 | |
| 103 | if (lttng_opt_mi) { |
| 104 | ret = mi_lttng_session(writer, session, 0); |
| 105 | if (ret) { |
| 106 | ret = CMD_ERROR; |
| 107 | goto error; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | ret = CMD_SUCCESS; |
| 112 | error: |
| 113 | if (printed_wait_msg) { |
| 114 | MSG(""); |
| 115 | } |
| 116 | lttng_clear_handle_destroy(handle); |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * clear all sessions |
| 122 | * |
| 123 | * Call clear_session for each registered sessions |
| 124 | */ |
| 125 | static int clear_all_sessions(struct lttng_session *sessions, int count) |
| 126 | { |
| 127 | int i, ret = CMD_SUCCESS; |
| 128 | |
| 129 | if (count == 0) { |
| 130 | MSG("No session found, nothing to do."); |
| 131 | } else if (count < 0) { |
| 132 | ERR("%s", lttng_strerror(ret)); |
| 133 | goto error; |
| 134 | } |
| 135 | |
| 136 | for (i = 0; i < count; i++) { |
| 137 | ret = clear_session(&sessions[i]); |
| 138 | if (ret < 0) { |
| 139 | goto error; |
| 140 | } |
| 141 | } |
| 142 | error: |
| 143 | return ret; |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * The 'clear <options>' first level command |
| 148 | */ |
| 149 | int cmd_clear(int argc, const char **argv) |
| 150 | { |
| 151 | int opt; |
| 152 | int ret = CMD_SUCCESS, i, command_ret = CMD_SUCCESS, success = 1; |
| 153 | static poptContext pc; |
| 154 | char *session_name = nullptr; |
| 155 | const char *arg_session_name = nullptr; |
| 156 | const char *leftover = nullptr; |
| 157 | struct lttng_session *sessions = nullptr; |
| 158 | int count; |
| 159 | bool found; |
| 160 | |
| 161 | pc = poptGetContext(nullptr, argc, argv, long_options, 0); |
| 162 | poptReadDefaultConfig(pc, 0); |
| 163 | |
| 164 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 165 | switch (opt) { |
| 166 | case OPT_HELP: |
| 167 | SHOW_HELP(); |
| 168 | break; |
| 169 | case OPT_LIST_OPTIONS: |
| 170 | list_cmd_options(stdout, long_options); |
| 171 | break; |
| 172 | default: |
| 173 | ret = CMD_UNDEFINED; |
| 174 | break; |
| 175 | } |
| 176 | goto end; |
| 177 | } |
| 178 | |
| 179 | /* Mi preparation */ |
| 180 | if (lttng_opt_mi) { |
| 181 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); |
| 182 | if (!writer) { |
| 183 | ret = -LTTNG_ERR_NOMEM; |
| 184 | goto end; |
| 185 | } |
| 186 | |
| 187 | /* Open command element */ |
| 188 | ret = mi_lttng_writer_command_open(writer, mi_lttng_element_command_clear); |
| 189 | if (ret) { |
| 190 | ret = CMD_ERROR; |
| 191 | goto end; |
| 192 | } |
| 193 | |
| 194 | /* Open output element */ |
| 195 | ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command_output); |
| 196 | if (ret) { |
| 197 | ret = CMD_ERROR; |
| 198 | goto end; |
| 199 | } |
| 200 | |
| 201 | /* For validation and semantic purpose we open a sessions element */ |
| 202 | ret = mi_lttng_sessions_open(writer); |
| 203 | if (ret) { |
| 204 | ret = CMD_ERROR; |
| 205 | goto end; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | if (!opt_clear_all) { |
| 210 | arg_session_name = poptGetArg(pc); |
| 211 | if (!arg_session_name) { |
| 212 | /* No session name specified, lookup default */ |
| 213 | session_name = get_session_name(); |
| 214 | } else { |
| 215 | session_name = strdup(arg_session_name); |
| 216 | if (session_name == nullptr) { |
| 217 | PERROR("Failed to copy session name"); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | if (session_name == nullptr) { |
| 222 | command_ret = CMD_ERROR; |
| 223 | success = 0; |
| 224 | goto mi_closing; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | leftover = poptGetArg(pc); |
| 229 | if (leftover) { |
| 230 | ERR("Unknown argument: %s", leftover); |
| 231 | ret = CMD_ERROR; |
| 232 | success = 0; |
| 233 | goto mi_closing; |
| 234 | } |
| 235 | |
| 236 | /* Recuperate all sessions for further operation */ |
| 237 | count = lttng_list_sessions(&sessions); |
| 238 | if (count < 0) { |
| 239 | ERR("%s", lttng_strerror(count)); |
| 240 | command_ret = CMD_ERROR; |
| 241 | success = 0; |
| 242 | goto mi_closing; |
| 243 | } |
| 244 | |
| 245 | /* Ignore session name in case all sessions are to be cleaned */ |
| 246 | if (opt_clear_all) { |
| 247 | command_ret = clear_all_sessions(sessions, count); |
| 248 | if (command_ret) { |
| 249 | ERR("%s", lttng_strerror(command_ret)); |
| 250 | success = 0; |
| 251 | } |
| 252 | } else { |
| 253 | /* Find the corresponding lttng_session struct */ |
| 254 | found = false; |
| 255 | for (i = 0; i < count; i++) { |
| 256 | if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) { |
| 257 | found = true; |
| 258 | command_ret = clear_session(&sessions[i]); |
| 259 | if (command_ret) { |
| 260 | ERR("%s", lttng_strerror(command_ret)); |
| 261 | success = 0; |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | if (!found) { |
| 267 | ERR("Session name %s not found", session_name); |
| 268 | command_ret = LTTNG_ERR_SESS_NOT_FOUND; |
| 269 | success = 0; |
| 270 | goto mi_closing; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | mi_closing: |
| 275 | /* Mi closing */ |
| 276 | if (lttng_opt_mi) { |
| 277 | /* Close sessions and output element element */ |
| 278 | ret = mi_lttng_close_multi_element(writer, 2); |
| 279 | if (ret) { |
| 280 | ret = CMD_ERROR; |
| 281 | goto end; |
| 282 | } |
| 283 | |
| 284 | /* Success ? */ |
| 285 | ret = mi_lttng_writer_write_element_bool( |
| 286 | writer, mi_lttng_element_command_success, success); |
| 287 | if (ret) { |
| 288 | ret = CMD_ERROR; |
| 289 | goto end; |
| 290 | } |
| 291 | |
| 292 | /* Command element close */ |
| 293 | ret = mi_lttng_writer_command_close(writer); |
| 294 | if (ret) { |
| 295 | ret = CMD_ERROR; |
| 296 | goto end; |
| 297 | } |
| 298 | } |
| 299 | end: |
| 300 | /* Mi clean-up */ |
| 301 | if (writer && mi_lttng_writer_destroy(writer)) { |
| 302 | /* Preserve original error code */ |
| 303 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; |
| 304 | } |
| 305 | |
| 306 | free(sessions); |
| 307 | free(session_name); |
| 308 | |
| 309 | /* Overwrite ret if an error occurred during clear_session/all */ |
| 310 | ret = command_ret ? command_ret : ret; |
| 311 | |
| 312 | poptFreeContext(pc); |
| 313 | return ret; |
| 314 | } |