| 1 | /* |
| 2 | * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@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 | |
| 13 | #include <lttng/lttng.h> |
| 14 | |
| 15 | #include <inttypes.h> |
| 16 | #include <popt.h> |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | |
| 21 | static char *opt_output_path; |
| 22 | static bool opt_force; |
| 23 | static bool opt_save_all; |
| 24 | static struct mi_writer *writer; |
| 25 | |
| 26 | #ifdef LTTNG_EMBED_HELP |
| 27 | static const char help_msg[] = |
| 28 | #include <lttng-save.1.h> |
| 29 | ; |
| 30 | #endif |
| 31 | |
| 32 | enum { |
| 33 | OPT_HELP = 1, |
| 34 | OPT_ALL, |
| 35 | OPT_FORCE, |
| 36 | OPT_LIST_OPTIONS, |
| 37 | }; |
| 38 | |
| 39 | static struct poptOption save_opts[] = { |
| 40 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 41 | { "help", 'h', POPT_ARG_NONE, nullptr, OPT_HELP, nullptr, nullptr }, |
| 42 | { "all", 'a', POPT_ARG_NONE, nullptr, OPT_ALL, nullptr, nullptr }, |
| 43 | { "output-path", 'o', POPT_ARG_STRING, &opt_output_path, 0, nullptr, nullptr }, |
| 44 | { "force", 'f', POPT_ARG_NONE, nullptr, OPT_FORCE, nullptr, nullptr }, |
| 45 | { "list-options", 0, POPT_ARG_NONE, nullptr, OPT_LIST_OPTIONS, nullptr, nullptr }, |
| 46 | { nullptr, 0, 0, nullptr, 0, nullptr, nullptr } |
| 47 | }; |
| 48 | |
| 49 | static int mi_partial_session(const char *session_name) |
| 50 | { |
| 51 | int ret; |
| 52 | LTTNG_ASSERT(writer); |
| 53 | LTTNG_ASSERT(session_name); |
| 54 | |
| 55 | /* Open session element */ |
| 56 | ret = mi_lttng_writer_open_element(writer, config_element_session); |
| 57 | if (ret) { |
| 58 | goto end; |
| 59 | } |
| 60 | |
| 61 | ret = mi_lttng_writer_write_element_string(writer, config_element_name, session_name); |
| 62 | if (ret) { |
| 63 | goto end; |
| 64 | } |
| 65 | |
| 66 | /* Closing session element */ |
| 67 | ret = mi_lttng_writer_close_element(writer); |
| 68 | end: |
| 69 | return ret; |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * Mi print of save command |
| 74 | */ |
| 75 | static int mi_save_print(const char *session_name) |
| 76 | { |
| 77 | int ret; |
| 78 | LTTNG_ASSERT(writer); |
| 79 | |
| 80 | if (opt_save_all) { |
| 81 | /* We use a wildcard to represent all sessions */ |
| 82 | session_name = "*"; |
| 83 | } |
| 84 | |
| 85 | /* Print save element */ |
| 86 | ret = mi_lttng_writer_open_element(writer, mi_lttng_element_save); |
| 87 | if (ret) { |
| 88 | goto end; |
| 89 | } |
| 90 | |
| 91 | /* Print session element */ |
| 92 | ret = mi_partial_session(session_name); |
| 93 | if (ret) { |
| 94 | goto end; |
| 95 | } |
| 96 | |
| 97 | /* Path element */ |
| 98 | if (opt_output_path) { |
| 99 | ret = mi_lttng_writer_write_element_string( |
| 100 | writer, config_element_path, opt_output_path); |
| 101 | if (ret) { |
| 102 | goto end; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /* Close save element */ |
| 107 | ret = mi_lttng_writer_close_element(writer); |
| 108 | end: |
| 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * The 'save <options>' first level command |
| 114 | */ |
| 115 | int cmd_save(int argc, const char **argv) |
| 116 | { |
| 117 | int ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success; |
| 118 | int opt; |
| 119 | const char *arg_session_name = nullptr, *leftover = nullptr; |
| 120 | poptContext pc; |
| 121 | struct lttng_save_session_attr *attr; |
| 122 | |
| 123 | pc = poptGetContext(nullptr, argc, argv, save_opts, 0); |
| 124 | poptReadDefaultConfig(pc, 0); |
| 125 | |
| 126 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 127 | switch (opt) { |
| 128 | case OPT_HELP: |
| 129 | SHOW_HELP(); |
| 130 | goto end; |
| 131 | case OPT_ALL: |
| 132 | opt_save_all = true; |
| 133 | break; |
| 134 | case OPT_FORCE: |
| 135 | opt_force = true; |
| 136 | break; |
| 137 | case OPT_LIST_OPTIONS: |
| 138 | list_cmd_options(stdout, save_opts); |
| 139 | goto end; |
| 140 | default: |
| 141 | ret = CMD_UNDEFINED; |
| 142 | goto end; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | if (!opt_save_all) { |
| 147 | arg_session_name = poptGetArg(pc); |
| 148 | if (arg_session_name) { |
| 149 | DBG2("Session name: %s", arg_session_name); |
| 150 | } else { |
| 151 | /* default to opt_save_all */ |
| 152 | opt_save_all = true; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | leftover = poptGetArg(pc); |
| 157 | if (leftover) { |
| 158 | ERR("Unknown argument: %s", leftover); |
| 159 | ret = CMD_ERROR; |
| 160 | goto end; |
| 161 | } |
| 162 | |
| 163 | attr = lttng_save_session_attr_create(); |
| 164 | if (!attr) { |
| 165 | ret = CMD_FATAL; |
| 166 | goto end_destroy; |
| 167 | } |
| 168 | |
| 169 | if (lttng_save_session_attr_set_session_name(attr, arg_session_name)) { |
| 170 | ret = CMD_ERROR; |
| 171 | goto end_destroy; |
| 172 | } |
| 173 | |
| 174 | if (lttng_save_session_attr_set_overwrite(attr, opt_force)) { |
| 175 | ret = CMD_ERROR; |
| 176 | goto end_destroy; |
| 177 | } |
| 178 | |
| 179 | if (lttng_save_session_attr_set_output_url(attr, opt_output_path)) { |
| 180 | ret = CMD_ERROR; |
| 181 | goto end_destroy; |
| 182 | } |
| 183 | |
| 184 | /* Mi check */ |
| 185 | if (lttng_opt_mi) { |
| 186 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); |
| 187 | if (!writer) { |
| 188 | ret = -LTTNG_ERR_NOMEM; |
| 189 | goto end_destroy; |
| 190 | } |
| 191 | |
| 192 | /* Open command element */ |
| 193 | ret = mi_lttng_writer_command_open(writer, mi_lttng_element_command_save); |
| 194 | if (ret) { |
| 195 | ret = CMD_ERROR; |
| 196 | goto end_destroy; |
| 197 | } |
| 198 | |
| 199 | /* Open output element */ |
| 200 | ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command_output); |
| 201 | if (ret) { |
| 202 | ret = CMD_ERROR; |
| 203 | goto end_destroy; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | command_ret = lttng_save_session(attr); |
| 208 | if (command_ret < 0) { |
| 209 | ERR("%s", lttng_strerror(command_ret)); |
| 210 | success = 0; |
| 211 | } else { |
| 212 | /* Inform the user of what just happened on success. */ |
| 213 | if (arg_session_name && opt_output_path) { |
| 214 | MSG("Session %s saved successfully in %s.", |
| 215 | arg_session_name, |
| 216 | opt_output_path); |
| 217 | } else if (arg_session_name && !opt_output_path) { |
| 218 | MSG("Session %s saved successfully.", arg_session_name); |
| 219 | } else if (!arg_session_name && opt_output_path) { |
| 220 | MSG("All sessions have been saved successfully in %s.", opt_output_path); |
| 221 | } else { |
| 222 | MSG("All sessions have been saved successfully."); |
| 223 | } |
| 224 | success = 1; |
| 225 | } |
| 226 | |
| 227 | /* Mi Printing and closing */ |
| 228 | if (lttng_opt_mi) { |
| 229 | /* Mi print */ |
| 230 | ret = mi_save_print(arg_session_name); |
| 231 | if (ret) { |
| 232 | ret = CMD_ERROR; |
| 233 | goto end_destroy; |
| 234 | } |
| 235 | |
| 236 | /* Close output element */ |
| 237 | ret = mi_lttng_writer_close_element(writer); |
| 238 | if (ret) { |
| 239 | ret = CMD_ERROR; |
| 240 | goto end_destroy; |
| 241 | } |
| 242 | |
| 243 | /* Success ? */ |
| 244 | ret = mi_lttng_writer_write_element_bool( |
| 245 | writer, mi_lttng_element_command_success, success); |
| 246 | if (ret) { |
| 247 | ret = CMD_ERROR; |
| 248 | goto end_destroy; |
| 249 | } |
| 250 | |
| 251 | /* Command element close */ |
| 252 | ret = mi_lttng_writer_command_close(writer); |
| 253 | if (ret) { |
| 254 | ret = CMD_ERROR; |
| 255 | goto end_destroy; |
| 256 | } |
| 257 | } |
| 258 | end_destroy: |
| 259 | lttng_save_session_attr_destroy(attr); |
| 260 | end: |
| 261 | /* Mi clean-up */ |
| 262 | if (writer && mi_lttng_writer_destroy(writer)) { |
| 263 | /* Preserve original error code */ |
| 264 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; |
| 265 | } |
| 266 | |
| 267 | /* Overwrite ret if command failed */ |
| 268 | ret = command_ret ? -command_ret : ret; |
| 269 | |
| 270 | poptFreeContext(pc); |
| 271 | return ret; |
| 272 | } |