| 1 | /* |
| 2 | * Copyright (C) 2014 - Jérémie Galarneau <jeremie.galarneau@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 <inttypes.h> |
| 20 | #include <popt.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <assert.h> |
| 25 | |
| 26 | #include <common/mi-lttng.h> |
| 27 | #include <common/config/session-config.h> |
| 28 | #include <lttng/load.h> |
| 29 | |
| 30 | #include "../command.h" |
| 31 | |
| 32 | static char *opt_input_path; |
| 33 | static int opt_force; |
| 34 | static int opt_load_all; |
| 35 | |
| 36 | static const char *session_name; |
| 37 | |
| 38 | enum { |
| 39 | OPT_HELP = 1, |
| 40 | OPT_ALL, |
| 41 | OPT_FORCE, |
| 42 | OPT_LIST_OPTIONS, |
| 43 | }; |
| 44 | |
| 45 | static struct mi_writer *writer; |
| 46 | |
| 47 | static struct poptOption load_opts[] = { |
| 48 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 49 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, |
| 50 | {"all", 'a', POPT_ARG_NONE, 0, OPT_ALL, 0, 0}, |
| 51 | {"input-path", 'i', POPT_ARG_STRING, &opt_input_path, 0, 0, 0}, |
| 52 | {"force", 'f', POPT_ARG_NONE, 0, OPT_FORCE, 0, 0}, |
| 53 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
| 54 | {0, 0, 0, 0, 0, 0, 0} |
| 55 | }; |
| 56 | |
| 57 | static int mi_partial_session(const char *session_name) |
| 58 | { |
| 59 | int ret; |
| 60 | assert(writer); |
| 61 | assert(session_name); |
| 62 | |
| 63 | /* Open session element */ |
| 64 | ret = mi_lttng_writer_open_element(writer, config_element_session); |
| 65 | if (ret) { |
| 66 | goto end; |
| 67 | } |
| 68 | |
| 69 | ret = mi_lttng_writer_write_element_string(writer, config_element_name, |
| 70 | session_name); |
| 71 | if (ret) { |
| 72 | goto end; |
| 73 | } |
| 74 | |
| 75 | /* Closing session element */ |
| 76 | ret = mi_lttng_writer_close_element(writer); |
| 77 | end: |
| 78 | return ret; |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * Mi print of load command |
| 83 | */ |
| 84 | static int mi_load_print(const char *session_name) |
| 85 | { |
| 86 | int ret; |
| 87 | assert(writer); |
| 88 | |
| 89 | if (opt_load_all) { |
| 90 | /* We use a wildcard to represent all sessions */ |
| 91 | session_name = "*"; |
| 92 | } |
| 93 | |
| 94 | /* Print load element */ |
| 95 | ret = mi_lttng_writer_open_element(writer, mi_lttng_element_load); |
| 96 | if (ret) { |
| 97 | goto end; |
| 98 | } |
| 99 | |
| 100 | /* Print session element */ |
| 101 | ret = mi_partial_session(session_name); |
| 102 | if (ret) { |
| 103 | goto end; |
| 104 | } |
| 105 | |
| 106 | /* Path element */ |
| 107 | if (opt_input_path) { |
| 108 | ret = mi_lttng_writer_write_element_string(writer, config_element_path, |
| 109 | opt_input_path); |
| 110 | if (ret) { |
| 111 | goto end; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /* Close load element */ |
| 116 | ret = mi_lttng_writer_close_element(writer); |
| 117 | |
| 118 | end: |
| 119 | return ret; |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | * The 'load <options>' first level command |
| 124 | */ |
| 125 | int cmd_load(int argc, const char **argv) |
| 126 | { |
| 127 | int ret, success; |
| 128 | int opt; |
| 129 | poptContext pc; |
| 130 | struct lttng_load_session_attr *session_attr = NULL; |
| 131 | char *input_path = NULL; |
| 132 | |
| 133 | pc = poptGetContext(NULL, argc, argv, load_opts, 0); |
| 134 | poptReadDefaultConfig(pc, 0); |
| 135 | |
| 136 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 137 | switch (opt) { |
| 138 | case OPT_HELP: |
| 139 | SHOW_HELP(); |
| 140 | ret = CMD_SUCCESS; |
| 141 | goto end; |
| 142 | case OPT_ALL: |
| 143 | opt_load_all = 1; |
| 144 | break; |
| 145 | case OPT_LIST_OPTIONS: |
| 146 | list_cmd_options(stdout, load_opts); |
| 147 | ret = CMD_SUCCESS; |
| 148 | goto end; |
| 149 | case OPT_FORCE: |
| 150 | opt_force = 1; |
| 151 | break; |
| 152 | default: |
| 153 | ret = CMD_UNDEFINED; |
| 154 | goto end; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | if (!opt_load_all) { |
| 159 | session_name = poptGetArg(pc); |
| 160 | if (session_name) { |
| 161 | DBG2("Loading session name: %s", session_name); |
| 162 | } else { |
| 163 | /* Default to load_all */ |
| 164 | opt_load_all = 1; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /* Mi check */ |
| 169 | if (lttng_opt_mi) { |
| 170 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); |
| 171 | if (!writer) { |
| 172 | ret = CMD_ERROR; |
| 173 | goto end; |
| 174 | } |
| 175 | |
| 176 | /* Open command element */ |
| 177 | ret = mi_lttng_writer_command_open(writer, |
| 178 | mi_lttng_element_command_load); |
| 179 | if (ret) { |
| 180 | ret = CMD_ERROR; |
| 181 | goto end; |
| 182 | } |
| 183 | |
| 184 | /* Open output element */ |
| 185 | ret = mi_lttng_writer_open_element(writer, |
| 186 | mi_lttng_element_command_output); |
| 187 | if (ret) { |
| 188 | ret = CMD_ERROR; |
| 189 | goto end; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /* Prepare load attributes */ |
| 194 | session_attr = lttng_load_session_attr_create(); |
| 195 | if (!session_attr) { |
| 196 | ERR("Failed to create load session attributes"); |
| 197 | ret = CMD_ERROR; |
| 198 | goto end; |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * Set the input url |
| 203 | * lttng_load_session_attr_set_input_url only suppports absolute path. |
| 204 | * Use realpath to resolve any relative path. |
| 205 | * */ |
| 206 | if (opt_input_path) { |
| 207 | input_path = realpath(opt_input_path, NULL); |
| 208 | if (!input_path) { |
| 209 | PERROR("Invalid input path"); |
| 210 | ret = CMD_ERROR; |
| 211 | goto end; |
| 212 | } |
| 213 | } else { |
| 214 | input_path = NULL; |
| 215 | } |
| 216 | |
| 217 | ret = lttng_load_session_attr_set_input_url(session_attr, |
| 218 | input_path); |
| 219 | if (ret) { |
| 220 | ERR("Invalid input path"); |
| 221 | ret = CMD_ERROR; |
| 222 | goto end; |
| 223 | } |
| 224 | |
| 225 | /* Set the session name. NULL means all sessions should be loaded */ |
| 226 | ret = lttng_load_session_attr_set_session_name(session_attr, |
| 227 | session_name); |
| 228 | if (ret) { |
| 229 | ERR("Invalid session name"); |
| 230 | ret = CMD_ERROR; |
| 231 | goto end; |
| 232 | } |
| 233 | |
| 234 | /* Set the overwrite attribute */ |
| 235 | ret = lttng_load_session_attr_set_overwrite(session_attr, opt_force); |
| 236 | if (ret) { |
| 237 | ERR("Force argument could not be applied"); |
| 238 | ret = CMD_ERROR; |
| 239 | goto end; |
| 240 | } |
| 241 | |
| 242 | ret = lttng_load_session(session_attr); |
| 243 | if (ret) { |
| 244 | ERR("%s", lttng_strerror(ret)); |
| 245 | success = 0; |
| 246 | ret = CMD_ERROR; |
| 247 | } else { |
| 248 | if (opt_load_all) { |
| 249 | MSG("All sessions have been loaded successfully"); |
| 250 | } else if (session_name) { |
| 251 | ret = config_init((char *) session_name); |
| 252 | if (ret < 0) { |
| 253 | ret = CMD_WARNING; |
| 254 | } |
| 255 | MSG("Session %s has been loaded successfully", session_name); |
| 256 | } else { |
| 257 | MSG("Session has been loaded successfully"); |
| 258 | } |
| 259 | success = 1; |
| 260 | ret = CMD_SUCCESS; |
| 261 | } |
| 262 | |
| 263 | /* Mi Printing and closing */ |
| 264 | if (lttng_opt_mi) { |
| 265 | /* Mi print */ |
| 266 | ret = mi_load_print(session_name); |
| 267 | if (ret) { |
| 268 | ret = CMD_ERROR; |
| 269 | goto end; |
| 270 | } |
| 271 | |
| 272 | /* Close output element */ |
| 273 | ret = mi_lttng_writer_close_element(writer); |
| 274 | if (ret) { |
| 275 | ret = CMD_ERROR; |
| 276 | goto end; |
| 277 | } |
| 278 | |
| 279 | /* Success ? */ |
| 280 | ret = mi_lttng_writer_write_element_bool(writer, |
| 281 | mi_lttng_element_command_success, success); |
| 282 | if (ret) { |
| 283 | ret = CMD_ERROR; |
| 284 | goto end; |
| 285 | } |
| 286 | |
| 287 | /* Command element close */ |
| 288 | ret = mi_lttng_writer_command_close(writer); |
| 289 | if (ret) { |
| 290 | ret = CMD_ERROR; |
| 291 | goto end; |
| 292 | } |
| 293 | } |
| 294 | end: |
| 295 | if (writer && mi_lttng_writer_destroy(writer)) { |
| 296 | ERR("Failed to destroy mi lttng writer"); |
| 297 | } |
| 298 | |
| 299 | lttng_load_session_attr_destroy(session_attr); |
| 300 | free(input_path); |
| 301 | poptFreeContext(pc); |
| 302 | return ret; |
| 303 | } |