| 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 | enum { |
| 39 | OPT_HELP = 1, |
| 40 | OPT_LIST_OPTIONS, |
| 41 | OPT_TIMER, |
| 42 | }; |
| 43 | |
| 44 | static struct poptOption long_options[] = { |
| 45 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 46 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, |
| 47 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
| 48 | {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0}, |
| 49 | {"timer", 0, POPT_ARG_NONE, 0, OPT_TIMER, 0, 0}, |
| 50 | {0, 0, 0, 0, 0, 0, 0} |
| 51 | }; |
| 52 | |
| 53 | static int setup_rotate(char *session_name, uint64_t timer) |
| 54 | { |
| 55 | int ret = 0; |
| 56 | struct lttng_rotation_schedule_attr *attr = NULL; |
| 57 | |
| 58 | attr = lttng_rotation_schedule_attr_create(); |
| 59 | if (!attr) { |
| 60 | goto error; |
| 61 | } |
| 62 | |
| 63 | if (lttng_opt_mi) { |
| 64 | /* Open rotation_schedule element */ |
| 65 | ret = mi_lttng_writer_open_element(writer, |
| 66 | config_element_rotation_schedule); |
| 67 | if (ret) { |
| 68 | goto error; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | ret = lttng_rotation_schedule_attr_set_session_name(attr, session_name); |
| 73 | if (ret < 0) { |
| 74 | goto error; |
| 75 | } |
| 76 | |
| 77 | if (lttng_opt_mi) { |
| 78 | ret = mi_lttng_writer_write_element_string(writer, |
| 79 | mi_lttng_element_session_name, session_name); |
| 80 | if (ret) { |
| 81 | goto error; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if (timer == -1ULL) { |
| 86 | lttng_rotation_schedule_attr_set_timer_period(attr, timer); |
| 87 | MSG("Disabling rotation timer on session %s", session_name); |
| 88 | } |
| 89 | |
| 90 | ret = lttng_rotation_set_schedule(attr); |
| 91 | if (ret) { |
| 92 | ERR("%s", lttng_strerror(ret)); |
| 93 | if (lttng_opt_mi) { |
| 94 | ret = mi_lttng_writer_write_element_string(writer, |
| 95 | mi_lttng_element_rotate_status, "error"); |
| 96 | if (ret) { |
| 97 | goto end; |
| 98 | } |
| 99 | /* Close rotation_schedule element */ |
| 100 | ret = mi_lttng_writer_close_element(writer); |
| 101 | if (ret) { |
| 102 | goto end; |
| 103 | } |
| 104 | } |
| 105 | goto error; |
| 106 | } |
| 107 | |
| 108 | if (lttng_opt_mi) { |
| 109 | ret = mi_lttng_writer_write_element_string(writer, |
| 110 | mi_lttng_element_rotate_status, "success"); |
| 111 | if (ret) { |
| 112 | goto end; |
| 113 | } |
| 114 | |
| 115 | /* Close rotation_schedule element */ |
| 116 | ret = mi_lttng_writer_close_element(writer); |
| 117 | if (ret) { |
| 118 | goto end; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | ret = 0; |
| 123 | goto end; |
| 124 | |
| 125 | error: |
| 126 | ret = -1; |
| 127 | end: |
| 128 | return ret; |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * cmd_disable_rotation |
| 133 | * |
| 134 | * The 'enable-rotation <options>' first level command |
| 135 | */ |
| 136 | int cmd_disable_rotation(int argc, const char **argv) |
| 137 | { |
| 138 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
| 139 | int popt_ret; |
| 140 | static poptContext pc; |
| 141 | char *session_name = NULL; |
| 142 | bool free_session_name = false; |
| 143 | uint64_t timer = 0; |
| 144 | |
| 145 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
| 146 | popt_ret = poptReadDefaultConfig(pc, 0); |
| 147 | if (popt_ret) { |
| 148 | ret = CMD_ERROR; |
| 149 | ERR("poptReadDefaultConfig"); |
| 150 | goto end; |
| 151 | } |
| 152 | |
| 153 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 154 | switch (opt) { |
| 155 | case OPT_HELP: |
| 156 | SHOW_HELP(); |
| 157 | goto end; |
| 158 | case OPT_LIST_OPTIONS: |
| 159 | list_cmd_options(stdout, long_options); |
| 160 | goto end; |
| 161 | case OPT_TIMER: |
| 162 | timer = -1ULL; |
| 163 | break; |
| 164 | default: |
| 165 | ret = CMD_UNDEFINED; |
| 166 | goto end; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | if (opt_session_name == NULL) { |
| 171 | session_name = get_session_name(); |
| 172 | if (session_name == NULL) { |
| 173 | goto end; |
| 174 | } |
| 175 | free_session_name = true; |
| 176 | } else { |
| 177 | session_name = opt_session_name; |
| 178 | } |
| 179 | |
| 180 | /* Mi check */ |
| 181 | if (lttng_opt_mi) { |
| 182 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); |
| 183 | if (!writer) { |
| 184 | ret = -LTTNG_ERR_NOMEM; |
| 185 | goto end; |
| 186 | } |
| 187 | |
| 188 | /* Open command element */ |
| 189 | ret = mi_lttng_writer_command_open(writer, |
| 190 | mi_lttng_element_command_disable_rotation); |
| 191 | if (ret) { |
| 192 | ret = CMD_ERROR; |
| 193 | goto end; |
| 194 | } |
| 195 | |
| 196 | /* Open output element */ |
| 197 | ret = mi_lttng_writer_open_element(writer, |
| 198 | mi_lttng_element_command_output); |
| 199 | if (ret) { |
| 200 | ret = CMD_ERROR; |
| 201 | goto end; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /* No config options, just rotate the session now */ |
| 206 | if (timer == 0) { |
| 207 | ERR("No timer given"); |
| 208 | success = 0; |
| 209 | command_ret = -1; |
| 210 | } else { |
| 211 | command_ret = setup_rotate(session_name, timer); |
| 212 | } |
| 213 | |
| 214 | if (command_ret) { |
| 215 | ERR("%s", lttng_strerror(command_ret)); |
| 216 | success = 0; |
| 217 | } |
| 218 | |
| 219 | /* Mi closing */ |
| 220 | if (lttng_opt_mi) { |
| 221 | /* Close output element */ |
| 222 | ret = mi_lttng_writer_close_element(writer); |
| 223 | if (ret) { |
| 224 | goto end; |
| 225 | } |
| 226 | |
| 227 | /* Success ? */ |
| 228 | ret = mi_lttng_writer_write_element_bool(writer, |
| 229 | mi_lttng_element_command_success, success); |
| 230 | if (ret) { |
| 231 | ret = CMD_ERROR; |
| 232 | goto end; |
| 233 | } |
| 234 | |
| 235 | /* Command element close */ |
| 236 | ret = mi_lttng_writer_command_close(writer); |
| 237 | if (ret) { |
| 238 | ret = CMD_ERROR; |
| 239 | goto end; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | end: |
| 244 | /* Mi clean-up */ |
| 245 | if (writer && mi_lttng_writer_destroy(writer)) { |
| 246 | /* Preserve original error code */ |
| 247 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; |
| 248 | } |
| 249 | |
| 250 | /* Overwrite ret if an error occurred with start_tracing */ |
| 251 | ret = command_ret ? command_ret : ret; |
| 252 | poptFreeContext(pc); |
| 253 | if (free_session_name) { |
| 254 | free(session_name); |
| 255 | } |
| 256 | return ret; |
| 257 | } |