| 1 | /* |
| 2 | * Copyright (C) 2019 - Jonathan Rajotte-Julien <jonathan.rajotte-julien@efficios.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License, version 2 only, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | |
| 18 | #define _LGPL_SOURCE |
| 19 | #include <popt.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 | #include <stdbool.h> |
| 27 | |
| 28 | #include "../command.h" |
| 29 | |
| 30 | #include <common/mi-lttng.h> |
| 31 | #include <common/sessiond-comm/sessiond-comm.h> |
| 32 | #include <common/utils.h> |
| 33 | |
| 34 | static char *opt_session_name; |
| 35 | static int opt_clear_all; |
| 36 | |
| 37 | #ifdef LTTNG_EMBED_HELP |
| 38 | static const char help_msg[] = |
| 39 | #include <lttng-clear.1.h> |
| 40 | ; |
| 41 | #endif |
| 42 | |
| 43 | /* Mi writer */ |
| 44 | static struct mi_writer *writer; |
| 45 | |
| 46 | enum { |
| 47 | OPT_HELP = 1, |
| 48 | OPT_LIST_OPTIONS, |
| 49 | }; |
| 50 | |
| 51 | static struct poptOption long_options[] = { |
| 52 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 53 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, |
| 54 | {"all", 'a', POPT_ARG_VAL, &opt_clear_all, 1, 0, 0}, |
| 55 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
| 56 | {0, 0, 0, 0, 0, 0, 0} |
| 57 | }; |
| 58 | |
| 59 | /* |
| 60 | * clear session |
| 61 | * |
| 62 | */ |
| 63 | static int clear_session(struct lttng_session *session) |
| 64 | { |
| 65 | int ret; |
| 66 | char *session_name = NULL; |
| 67 | |
| 68 | ret = lttng_clear_session(session->name); |
| 69 | if (ret < 0) { |
| 70 | ERR("%s", lttng_strerror(ret)); |
| 71 | goto error; |
| 72 | } |
| 73 | |
| 74 | MSG("Session %s cleared", session->name); |
| 75 | |
| 76 | if (lttng_opt_mi) { |
| 77 | ret = mi_lttng_session(writer, session, 0); |
| 78 | if (ret) { |
| 79 | ret = CMD_ERROR; |
| 80 | goto error; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | ret = CMD_SUCCESS; |
| 85 | error: |
| 86 | free(session_name); |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * clear all sessions |
| 92 | * |
| 93 | * Call clear_session for each registered sessions |
| 94 | */ |
| 95 | static int clear_all_sessions(struct lttng_session *sessions, int count) |
| 96 | { |
| 97 | int i, ret = CMD_SUCCESS; |
| 98 | |
| 99 | if (count == 0) { |
| 100 | MSG("No session found, nothing to do."); |
| 101 | } else if (count < 0) { |
| 102 | ERR("%s", lttng_strerror(ret)); |
| 103 | goto error; |
| 104 | } |
| 105 | |
| 106 | for (i = 0; i < count; i++) { |
| 107 | ret = clear_session(&sessions[i]); |
| 108 | if (ret < 0) { |
| 109 | goto error; |
| 110 | } |
| 111 | } |
| 112 | error: |
| 113 | return ret; |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * The 'clear <options>' first level command |
| 118 | */ |
| 119 | int cmd_clear(int argc, const char **argv) |
| 120 | { |
| 121 | int opt; |
| 122 | int ret = CMD_SUCCESS , i, command_ret = CMD_SUCCESS, success = 1; |
| 123 | static poptContext pc; |
| 124 | char *session_name = NULL; |
| 125 | const char *leftover = NULL; |
| 126 | |
| 127 | struct lttng_session *sessions; |
| 128 | int count; |
| 129 | int found; |
| 130 | |
| 131 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
| 132 | poptReadDefaultConfig(pc, 0); |
| 133 | |
| 134 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 135 | switch (opt) { |
| 136 | case OPT_HELP: |
| 137 | SHOW_HELP(); |
| 138 | break; |
| 139 | case OPT_LIST_OPTIONS: |
| 140 | list_cmd_options(stdout, long_options); |
| 141 | break; |
| 142 | default: |
| 143 | ret = CMD_UNDEFINED; |
| 144 | break; |
| 145 | } |
| 146 | goto end; |
| 147 | } |
| 148 | |
| 149 | /* Mi preparation */ |
| 150 | if (lttng_opt_mi) { |
| 151 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); |
| 152 | if (!writer) { |
| 153 | ret = -LTTNG_ERR_NOMEM; |
| 154 | goto end; |
| 155 | } |
| 156 | |
| 157 | /* Open command element */ |
| 158 | ret = mi_lttng_writer_command_open(writer, |
| 159 | mi_lttng_element_command_clear); |
| 160 | if (ret) { |
| 161 | ret = CMD_ERROR; |
| 162 | goto end; |
| 163 | } |
| 164 | |
| 165 | /* Open output element */ |
| 166 | ret = mi_lttng_writer_open_element(writer, |
| 167 | mi_lttng_element_command_output); |
| 168 | if (ret) { |
| 169 | ret = CMD_ERROR; |
| 170 | goto end; |
| 171 | } |
| 172 | |
| 173 | /* For validation and semantic purpose we open a sessions element */ |
| 174 | ret = mi_lttng_sessions_open(writer); |
| 175 | if (ret) { |
| 176 | ret = CMD_ERROR; |
| 177 | goto end; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | if (!opt_clear_all) { |
| 182 | opt_session_name = (char *) poptGetArg(pc); |
| 183 | |
| 184 | if (!opt_session_name) { |
| 185 | /* No session name specified, lookup default */ |
| 186 | session_name = get_session_name(); |
| 187 | if (session_name == NULL) { |
| 188 | command_ret = CMD_ERROR; |
| 189 | success = 0; |
| 190 | goto mi_closing; |
| 191 | } |
| 192 | } else { |
| 193 | session_name = opt_session_name; |
| 194 | } |
| 195 | } else { |
| 196 | session_name = NULL; |
| 197 | } |
| 198 | |
| 199 | leftover = poptGetArg(pc); |
| 200 | if (leftover) { |
| 201 | ERR("Unknown argument: %s", leftover); |
| 202 | ret = CMD_ERROR; |
| 203 | success = 0; |
| 204 | goto mi_closing; |
| 205 | } |
| 206 | |
| 207 | /* Recuperate all sessions for further operation */ |
| 208 | count = lttng_list_sessions(&sessions); |
| 209 | if (count < 0) { |
| 210 | ERR("%s", lttng_strerror(count)); |
| 211 | command_ret = CMD_ERROR; |
| 212 | success = 0; |
| 213 | goto mi_closing; |
| 214 | } |
| 215 | |
| 216 | /* Ignore session name in case all sessions are to be cleaned */ |
| 217 | if (opt_clear_all) { |
| 218 | command_ret = clear_all_sessions(sessions, count); |
| 219 | if (command_ret) { |
| 220 | success = 0; |
| 221 | } |
| 222 | } else { |
| 223 | /* Find the corresponding lttng_session struct */ |
| 224 | found = 0; |
| 225 | for (i = 0; i < count; i++) { |
| 226 | if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) { |
| 227 | found = 1; |
| 228 | command_ret = clear_session(&sessions[i]); |
| 229 | if (command_ret) { |
| 230 | success = 0; |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | if (!found) { |
| 236 | ERR("Session name %s not found", session_name); |
| 237 | command_ret = LTTNG_ERR_SESS_NOT_FOUND; |
| 238 | success = 0; |
| 239 | goto mi_closing; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | |
| 244 | mi_closing: |
| 245 | /* Mi closing */ |
| 246 | if (lttng_opt_mi) { |
| 247 | /* Close sessions and output element element */ |
| 248 | ret = mi_lttng_close_multi_element(writer, 2); |
| 249 | if (ret) { |
| 250 | ret = CMD_ERROR; |
| 251 | goto end; |
| 252 | } |
| 253 | |
| 254 | /* Success ? */ |
| 255 | ret = mi_lttng_writer_write_element_bool(writer, |
| 256 | mi_lttng_element_command_success, success); |
| 257 | if (ret) { |
| 258 | ret = CMD_ERROR; |
| 259 | goto end; |
| 260 | } |
| 261 | |
| 262 | /* Command element close */ |
| 263 | ret = mi_lttng_writer_command_close(writer); |
| 264 | if (ret) { |
| 265 | ret = CMD_ERROR; |
| 266 | goto end; |
| 267 | } |
| 268 | } |
| 269 | end: |
| 270 | /* Mi clean-up */ |
| 271 | if (writer && mi_lttng_writer_destroy(writer)) { |
| 272 | /* Preserve original error code */ |
| 273 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; |
| 274 | } |
| 275 | |
| 276 | if (opt_session_name == NULL) { |
| 277 | free(session_name); |
| 278 | } |
| 279 | |
| 280 | /* Overwrite ret if an error occurred during clear_session/all */ |
| 281 | ret = command_ret ? command_ret : ret; |
| 282 | |
| 283 | poptFreeContext(pc); |
| 284 | return ret; |
| 285 | } |