| 1 | /* |
| 2 | * Copyright (C) 2017 - Julien Desfossez <jdesfossez@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 <inttypes.h> |
| 27 | #include <ctype.h> |
| 28 | |
| 29 | #include <common/sessiond-comm/sessiond-comm.h> |
| 30 | #include <common/mi-lttng.h> |
| 31 | |
| 32 | #include "../command.h" |
| 33 | #include <lttng/rotation.h> |
| 34 | |
| 35 | static char *opt_session_name; |
| 36 | static struct mi_writer *writer; |
| 37 | |
| 38 | #ifdef LTTNG_EMBED_HELP |
| 39 | static const char help_msg[] = |
| 40 | #include <lttng-disable-rotation.1.h> |
| 41 | ; |
| 42 | #endif |
| 43 | |
| 44 | enum { |
| 45 | OPT_HELP = 1, |
| 46 | OPT_LIST_OPTIONS, |
| 47 | OPT_TIMER, |
| 48 | OPT_SIZE, |
| 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 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
| 55 | {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0}, |
| 56 | {"timer", 0, POPT_ARG_NONE, 0, OPT_TIMER, 0, 0}, |
| 57 | {"size", 0, POPT_ARG_NONE, 0, OPT_SIZE, 0, 0}, |
| 58 | {0, 0, 0, 0, 0, 0, 0} |
| 59 | }; |
| 60 | |
| 61 | static const char *schedule_type_str[] = { |
| 62 | [LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC] = "periodic", |
| 63 | [LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD] = "size-based", |
| 64 | }; |
| 65 | |
| 66 | static const struct lttng_rotation_schedule *get_schedule( |
| 67 | const char *session_name, |
| 68 | const struct lttng_rotation_schedules *schedules, |
| 69 | enum lttng_rotation_schedule_type schedule_type) |
| 70 | { |
| 71 | unsigned int count, i; |
| 72 | enum lttng_rotation_status status; |
| 73 | const struct lttng_rotation_schedule *ret = NULL; |
| 74 | |
| 75 | status = lttng_rotation_schedules_get_count(schedules, &count); |
| 76 | if (status != LTTNG_ROTATION_STATUS_OK) { |
| 77 | ERR("Unable to determine the number of rotation schedules of session %s", |
| 78 | session_name); |
| 79 | goto end; |
| 80 | } |
| 81 | |
| 82 | for (i = 0; i < count; i++) { |
| 83 | const struct lttng_rotation_schedule *schedule = NULL; |
| 84 | |
| 85 | schedule = lttng_rotation_schedules_get_at_index(schedules, i); |
| 86 | if (!schedule) { |
| 87 | ERR("Unable to retrieve rotation schedule at index %u", |
| 88 | i); |
| 89 | goto end; |
| 90 | } |
| 91 | |
| 92 | if (lttng_rotation_schedule_get_type(schedule) == |
| 93 | schedule_type) { |
| 94 | ret = schedule; |
| 95 | break; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | if (!ret) { |
| 100 | ERR("No %s rotation schedule active on session %s", |
| 101 | schedule_type_str[schedule_type], session_name); |
| 102 | } |
| 103 | end: |
| 104 | return ret; |
| 105 | } |
| 106 | |
| 107 | static struct lttng_rotation_schedule *create_empty_schedule( |
| 108 | enum lttng_rotation_schedule_type type) |
| 109 | { |
| 110 | struct lttng_rotation_schedule *schedule = NULL; |
| 111 | |
| 112 | switch (type) { |
| 113 | case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC: |
| 114 | schedule = lttng_rotation_schedule_periodic_create(); |
| 115 | break; |
| 116 | case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD: |
| 117 | schedule = lttng_rotation_schedule_size_threshold_create(); |
| 118 | break; |
| 119 | default: |
| 120 | abort(); |
| 121 | } |
| 122 | return schedule; |
| 123 | } |
| 124 | |
| 125 | static enum cmd_error_code remove_schedule(const char *session_name, |
| 126 | enum lttng_rotation_schedule_type schedule_type) |
| 127 | { |
| 128 | enum cmd_error_code cmd_ret; |
| 129 | int ret; |
| 130 | const struct lttng_rotation_schedule *schedule = NULL; |
| 131 | struct lttng_rotation_schedules *schedules = NULL; |
| 132 | enum lttng_rotation_status status; |
| 133 | const char *schedule_type_name; |
| 134 | struct lttng_rotation_schedule *empty_schedule = NULL; |
| 135 | |
| 136 | switch (schedule_type) { |
| 137 | case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC: |
| 138 | case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD: |
| 139 | break; |
| 140 | default: |
| 141 | ERR("Unknown schedule type"); |
| 142 | abort(); |
| 143 | } |
| 144 | |
| 145 | schedule_type_name = schedule_type_str[schedule_type]; |
| 146 | |
| 147 | ret = lttng_session_list_rotation_schedules(session_name, &schedules); |
| 148 | if (ret != LTTNG_OK) { |
| 149 | ERR("Failed to list rotation schedules of session %s", |
| 150 | session_name); |
| 151 | goto error; |
| 152 | } |
| 153 | |
| 154 | schedule = get_schedule(session_name, schedules, schedule_type); |
| 155 | if (!schedule) { |
| 156 | cmd_ret = CMD_ERROR; |
| 157 | /* |
| 158 | * get_schedule() logs its own errors. |
| 159 | * A temporaty schedule is created to serialize an MI rotation |
| 160 | * schedule descriptor of the appropriate type that has no |
| 161 | * attributes set. |
| 162 | */ |
| 163 | empty_schedule = create_empty_schedule(schedule_type); |
| 164 | if (!empty_schedule) { |
| 165 | goto error; |
| 166 | } |
| 167 | goto skip_removal; |
| 168 | } |
| 169 | |
| 170 | status = lttng_session_remove_rotation_schedule(session_name, schedule); |
| 171 | switch (status) { |
| 172 | case LTTNG_ROTATION_STATUS_OK: |
| 173 | MSG("Disabled %s rotation on session %s", |
| 174 | schedule_type_name, session_name); |
| 175 | cmd_ret = CMD_SUCCESS; |
| 176 | break; |
| 177 | case LTTNG_ROTATION_STATUS_SCHEDULE_NOT_SET: |
| 178 | ERR("No %s rotation schedule set on session %s", |
| 179 | schedule_type_name, |
| 180 | session_name); |
| 181 | cmd_ret = CMD_ERROR; |
| 182 | break; |
| 183 | case LTTNG_ROTATION_STATUS_ERROR: |
| 184 | case LTTNG_ROTATION_STATUS_INVALID: |
| 185 | default: |
| 186 | ERR("Failed to disable %s rotation schedule on session %s", |
| 187 | schedule_type_name, session_name); |
| 188 | cmd_ret = CMD_ERROR; |
| 189 | break; |
| 190 | } |
| 191 | |
| 192 | skip_removal: |
| 193 | if (lttng_opt_mi) { |
| 194 | ret = mi_lttng_rotation_schedule_result(writer, |
| 195 | schedule ? schedule : empty_schedule, |
| 196 | cmd_ret == CMD_SUCCESS); |
| 197 | if (ret < 0) { |
| 198 | goto error; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | end: |
| 203 | lttng_rotation_schedules_destroy(schedules); |
| 204 | lttng_rotation_schedule_destroy(empty_schedule); |
| 205 | return cmd_ret; |
| 206 | error: |
| 207 | cmd_ret = CMD_ERROR; |
| 208 | goto end; |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * cmd_disable_rotation |
| 213 | * |
| 214 | * The 'disable-rotation <options>' first level command |
| 215 | */ |
| 216 | int cmd_disable_rotation(int argc, const char **argv) |
| 217 | { |
| 218 | int popt_ret, opt, ret = 0; |
| 219 | enum cmd_error_code cmd_ret = CMD_SUCCESS; |
| 220 | static poptContext pc; |
| 221 | char *session_name = NULL; |
| 222 | bool free_session_name = false; |
| 223 | bool periodic_rotation = false, size_rotation = false; |
| 224 | |
| 225 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
| 226 | popt_ret = poptReadDefaultConfig(pc, 0); |
| 227 | if (popt_ret) { |
| 228 | ret = CMD_ERROR; |
| 229 | ERR("poptReadDefaultConfig"); |
| 230 | goto end; |
| 231 | } |
| 232 | |
| 233 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 234 | switch (opt) { |
| 235 | case OPT_HELP: |
| 236 | SHOW_HELP(); |
| 237 | goto end; |
| 238 | case OPT_LIST_OPTIONS: |
| 239 | list_cmd_options(stdout, long_options); |
| 240 | goto end; |
| 241 | case OPT_TIMER: |
| 242 | periodic_rotation = true; |
| 243 | break; |
| 244 | case OPT_SIZE: |
| 245 | size_rotation = true; |
| 246 | break; |
| 247 | default: |
| 248 | ret = CMD_UNDEFINED; |
| 249 | goto end; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | if (opt_session_name == NULL) { |
| 254 | session_name = get_session_name(); |
| 255 | if (session_name == NULL) { |
| 256 | goto error; |
| 257 | } |
| 258 | free_session_name = true; |
| 259 | } else { |
| 260 | session_name = opt_session_name; |
| 261 | } |
| 262 | |
| 263 | /* Mi check */ |
| 264 | if (lttng_opt_mi) { |
| 265 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); |
| 266 | if (!writer) { |
| 267 | goto error; |
| 268 | } |
| 269 | |
| 270 | /* Open command element */ |
| 271 | ret = mi_lttng_writer_command_open(writer, |
| 272 | mi_lttng_element_command_disable_rotation); |
| 273 | if (ret) { |
| 274 | goto error; |
| 275 | } |
| 276 | |
| 277 | /* Open output element */ |
| 278 | ret = mi_lttng_writer_open_element(writer, |
| 279 | mi_lttng_element_command_output); |
| 280 | if (ret) { |
| 281 | goto error; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | if (!periodic_rotation && !size_rotation) { |
| 286 | ERR("No session rotation schedule type provided."); |
| 287 | cmd_ret = CMD_ERROR; |
| 288 | goto close_command; |
| 289 | } |
| 290 | |
| 291 | if (lttng_opt_mi) { |
| 292 | ret = mi_lttng_writer_open_element(writer, |
| 293 | mi_lttng_element_rotation_schedule_results); |
| 294 | if (ret) { |
| 295 | goto error; |
| 296 | } |
| 297 | |
| 298 | ret = mi_lttng_writer_write_element_string(writer, |
| 299 | mi_lttng_element_session_name, |
| 300 | session_name); |
| 301 | if (ret) { |
| 302 | goto error; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | if (periodic_rotation) { |
| 307 | /* |
| 308 | * Continue processing even on error as multiple schedules can |
| 309 | * be specified at once. |
| 310 | */ |
| 311 | cmd_ret = remove_schedule(session_name, |
| 312 | LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC); |
| 313 | } |
| 314 | |
| 315 | if (size_rotation) { |
| 316 | enum cmd_error_code tmp_ret; |
| 317 | |
| 318 | /* Don't overwrite cmd_ret if it already indicates an error. */ |
| 319 | tmp_ret = remove_schedule(session_name, |
| 320 | LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD); |
| 321 | cmd_ret = cmd_ret ? cmd_ret : tmp_ret; |
| 322 | } |
| 323 | |
| 324 | if (lttng_opt_mi) { |
| 325 | /* Close rotation schedule results element */ |
| 326 | ret = mi_lttng_writer_close_element(writer); |
| 327 | if (ret) { |
| 328 | goto error; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | close_command: |
| 333 | /* Mi closing */ |
| 334 | if (lttng_opt_mi) { |
| 335 | /* Close output element */ |
| 336 | ret = mi_lttng_writer_close_element(writer); |
| 337 | if (ret) { |
| 338 | goto error; |
| 339 | } |
| 340 | |
| 341 | /* Success ? */ |
| 342 | ret = mi_lttng_writer_write_element_bool(writer, |
| 343 | mi_lttng_element_command_success, |
| 344 | cmd_ret == CMD_SUCCESS); |
| 345 | if (ret) { |
| 346 | goto error; |
| 347 | } |
| 348 | |
| 349 | /* Command element close */ |
| 350 | ret = mi_lttng_writer_command_close(writer); |
| 351 | if (ret) { |
| 352 | goto error; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | end: |
| 357 | (void) mi_lttng_writer_destroy(writer); |
| 358 | poptFreeContext(pc); |
| 359 | if (free_session_name) { |
| 360 | free(session_name); |
| 361 | } |
| 362 | return cmd_ret; |
| 363 | error: |
| 364 | cmd_ret = CMD_ERROR; |
| 365 | goto end; |
| 366 | } |