| 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 | #include <assert.h> |
| 29 | |
| 30 | #include <common/sessiond-comm/sessiond-comm.h> |
| 31 | #include <common/mi-lttng.h> |
| 32 | |
| 33 | #include "../command.h" |
| 34 | #include <lttng/rotation.h> |
| 35 | |
| 36 | static char *opt_session_name; |
| 37 | static int opt_no_wait; |
| 38 | static struct mi_writer *writer; |
| 39 | |
| 40 | #ifdef LTTNG_EMBED_HELP |
| 41 | static const char help_msg[] = |
| 42 | #include <lttng-rotate.1.h> |
| 43 | ; |
| 44 | #endif |
| 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 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
| 55 | {"no-wait", 'n', POPT_ARG_VAL, &opt_no_wait, 1, 0, 0}, |
| 56 | {0, 0, 0, 0, 0, 0, 0} |
| 57 | }; |
| 58 | |
| 59 | static int mi_output_rotate(const char *status, const char *path, |
| 60 | const char *session_name) |
| 61 | { |
| 62 | int ret; |
| 63 | |
| 64 | if (!lttng_opt_mi) { |
| 65 | ret = 0; |
| 66 | goto end; |
| 67 | } |
| 68 | |
| 69 | ret = mi_lttng_writer_open_element(writer, |
| 70 | mi_lttng_element_rotation); |
| 71 | if (ret) { |
| 72 | goto end; |
| 73 | } |
| 74 | |
| 75 | ret = mi_lttng_writer_write_element_string(writer, |
| 76 | mi_lttng_element_session_name, session_name); |
| 77 | if (ret) { |
| 78 | goto end; |
| 79 | } |
| 80 | |
| 81 | ret = mi_lttng_writer_write_element_string(writer, |
| 82 | mi_lttng_element_rotate_status, status); |
| 83 | if (ret) { |
| 84 | goto end; |
| 85 | } |
| 86 | if (path) { |
| 87 | ret = mi_lttng_writer_write_element_string(writer, |
| 88 | config_element_path, path); |
| 89 | if (ret) { |
| 90 | goto end; |
| 91 | } |
| 92 | } |
| 93 | /* Close rotation element */ |
| 94 | ret = mi_lttng_writer_close_element(writer); |
| 95 | if (ret) { |
| 96 | goto end; |
| 97 | } |
| 98 | |
| 99 | end: |
| 100 | return ret; |
| 101 | } |
| 102 | |
| 103 | static int rotate_tracing(char *session_name) |
| 104 | { |
| 105 | int ret; |
| 106 | struct lttng_rotation_immediate_attr *attr = NULL; |
| 107 | struct lttng_rotation_handle *handle = NULL; |
| 108 | enum lttng_rotation_status rotation_status; |
| 109 | enum lttng_rotation_state rotation_state = LTTNG_ROTATION_STATE_ONGOING; |
| 110 | |
| 111 | DBG("Rotating the output files of session %s", session_name); |
| 112 | |
| 113 | attr = lttng_rotation_immediate_attr_create(); |
| 114 | if (!attr) { |
| 115 | goto error; |
| 116 | } |
| 117 | |
| 118 | ret = lttng_rotation_immediate_attr_set_session_name(attr, session_name); |
| 119 | if (ret < 0) { |
| 120 | ERR("Session name exceeds the maximal allowed length"); |
| 121 | goto error; |
| 122 | } |
| 123 | |
| 124 | ret = lttng_rotate_session(attr, &handle); |
| 125 | if (ret < 0) { |
| 126 | switch (-ret) { |
| 127 | case LTTNG_ERR_SESSION_NOT_STARTED: |
| 128 | WARN("Tracing session %s not started yet", session_name); |
| 129 | break; |
| 130 | default: |
| 131 | ERR("%s", lttng_strerror(ret)); |
| 132 | break; |
| 133 | } |
| 134 | goto error; |
| 135 | } |
| 136 | |
| 137 | if (!opt_no_wait) { |
| 138 | _MSG("Waiting for rotation to complete"); |
| 139 | ret = fflush(stdout); |
| 140 | if (ret) { |
| 141 | PERROR("fflush"); |
| 142 | goto error; |
| 143 | } |
| 144 | |
| 145 | do { |
| 146 | rotation_status = lttng_rotation_handle_get_state(handle, |
| 147 | &rotation_state); |
| 148 | if (rotation_status != LTTNG_ROTATION_STATUS_OK) { |
| 149 | ERR("Failed to query the state of the rotation"); |
| 150 | goto error; |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * Data sleep time before retrying (in usec). Don't |
| 155 | * sleep if the call returned value indicates |
| 156 | * availability. |
| 157 | */ |
| 158 | if (rotation_state == LTTNG_ROTATION_STATE_ONGOING) { |
| 159 | ret = usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME); |
| 160 | if (ret) { |
| 161 | PERROR("usleep"); |
| 162 | goto error; |
| 163 | } |
| 164 | _MSG("."); |
| 165 | |
| 166 | ret = fflush(stdout); |
| 167 | if (ret) { |
| 168 | PERROR("fflush"); |
| 169 | goto error; |
| 170 | } |
| 171 | } |
| 172 | } while (rotation_state == LTTNG_ROTATION_STATE_ONGOING); |
| 173 | MSG(""); |
| 174 | } |
| 175 | |
| 176 | switch (rotation_state) { |
| 177 | case LTTNG_ROTATION_STATE_COMPLETED: |
| 178 | { |
| 179 | const char *path; |
| 180 | |
| 181 | rotation_status = lttng_rotation_handle_get_completed_archive_location( |
| 182 | handle, &path); |
| 183 | if (rotation_status != LTTNG_ROTATION_STATUS_OK) { |
| 184 | ERR("Failed to retrieve the rotation's completed chunk archive location"); |
| 185 | goto error; |
| 186 | } |
| 187 | MSG("Trace chunk archive for session %s is now readable at %s", |
| 188 | session_name, path); |
| 189 | ret = mi_output_rotate("completed", path, session_name); |
| 190 | if (ret) { |
| 191 | goto error; |
| 192 | } |
| 193 | ret = CMD_SUCCESS; |
| 194 | goto end; |
| 195 | } |
| 196 | case LTTNG_ROTATION_STATE_EXPIRED: |
| 197 | MSG("Session %s rotated, but handle expired", session_name); |
| 198 | ret = mi_output_rotate("expired", NULL, session_name); |
| 199 | if (ret) { |
| 200 | goto error; |
| 201 | } |
| 202 | ret = CMD_SUCCESS; |
| 203 | goto end; |
| 204 | default: |
| 205 | ERR("Unexpected rotation state received, aborting..."); |
| 206 | goto error; |
| 207 | } |
| 208 | |
| 209 | error: |
| 210 | ret = CMD_ERROR; |
| 211 | end: |
| 212 | lttng_rotation_handle_destroy(handle); |
| 213 | lttng_rotation_immediate_attr_destroy(attr); |
| 214 | return ret; |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * cmd_rotate |
| 219 | * |
| 220 | * The 'rotate <options>' first level command |
| 221 | */ |
| 222 | int cmd_rotate(int argc, const char **argv) |
| 223 | { |
| 224 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
| 225 | int popt_ret; |
| 226 | static poptContext pc; |
| 227 | char *session_name = NULL; |
| 228 | bool free_session_name = false; |
| 229 | |
| 230 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
| 231 | popt_ret = poptReadDefaultConfig(pc, 0); |
| 232 | if (popt_ret) { |
| 233 | ret = CMD_ERROR; |
| 234 | ERR("poptReadDefaultConfig"); |
| 235 | goto end; |
| 236 | } |
| 237 | |
| 238 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 239 | switch (opt) { |
| 240 | case OPT_HELP: |
| 241 | SHOW_HELP(); |
| 242 | goto end; |
| 243 | case OPT_LIST_OPTIONS: |
| 244 | list_cmd_options(stdout, long_options); |
| 245 | goto end; |
| 246 | default: |
| 247 | ret = CMD_UNDEFINED; |
| 248 | goto end; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | opt_session_name = (char*) poptGetArg(pc); |
| 253 | |
| 254 | if (!opt_session_name) { |
| 255 | session_name = get_session_name(); |
| 256 | if (!session_name) { |
| 257 | goto end; |
| 258 | } |
| 259 | free_session_name = true; |
| 260 | } else { |
| 261 | session_name = opt_session_name; |
| 262 | } |
| 263 | |
| 264 | /* Mi check */ |
| 265 | if (lttng_opt_mi) { |
| 266 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); |
| 267 | if (!writer) { |
| 268 | ret = -LTTNG_ERR_NOMEM; |
| 269 | goto end; |
| 270 | } |
| 271 | |
| 272 | /* Open rotate command */ |
| 273 | ret = mi_lttng_writer_command_open(writer, |
| 274 | mi_lttng_element_command_rotate); |
| 275 | if (ret) { |
| 276 | ret = CMD_ERROR; |
| 277 | goto end; |
| 278 | } |
| 279 | |
| 280 | /* Open output element */ |
| 281 | ret = mi_lttng_writer_open_element(writer, |
| 282 | mi_lttng_element_command_output); |
| 283 | if (ret) { |
| 284 | goto end; |
| 285 | } |
| 286 | |
| 287 | /* Open rotations element */ |
| 288 | ret = mi_lttng_writer_open_element(writer, |
| 289 | mi_lttng_element_rotations); |
| 290 | if (ret) { |
| 291 | goto end; |
| 292 | } |
| 293 | |
| 294 | } |
| 295 | |
| 296 | command_ret = rotate_tracing(session_name); |
| 297 | if (command_ret) { |
| 298 | success = 0; |
| 299 | } |
| 300 | |
| 301 | /* Mi closing */ |
| 302 | if (lttng_opt_mi) { |
| 303 | /* Close rotations element */ |
| 304 | ret = mi_lttng_writer_close_element(writer); |
| 305 | if (ret) { |
| 306 | goto end; |
| 307 | } |
| 308 | /* Close output element */ |
| 309 | ret = mi_lttng_writer_close_element(writer); |
| 310 | if (ret) { |
| 311 | goto end; |
| 312 | } |
| 313 | /* Success ? */ |
| 314 | ret = mi_lttng_writer_write_element_bool(writer, |
| 315 | mi_lttng_element_command_success, success); |
| 316 | if (ret) { |
| 317 | ret = CMD_ERROR; |
| 318 | goto end; |
| 319 | } |
| 320 | |
| 321 | /* Command element close */ |
| 322 | ret = mi_lttng_writer_command_close(writer); |
| 323 | if (ret) { |
| 324 | ret = CMD_ERROR; |
| 325 | goto end; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | end: |
| 330 | /* Mi clean-up */ |
| 331 | if (writer && mi_lttng_writer_destroy(writer)) { |
| 332 | /* Preserve original error code */ |
| 333 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; |
| 334 | } |
| 335 | |
| 336 | /* Overwrite ret if an error occurred with start_tracing */ |
| 337 | ret = command_ret ? command_ret : ret; |
| 338 | poptFreeContext(pc); |
| 339 | if (free_session_name) { |
| 340 | free(session_name); |
| 341 | } |
| 342 | return ret; |
| 343 | } |