| 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 | |
| 29 | #include "../command.h" |
| 30 | |
| 31 | static char *opt_input_path; |
| 32 | static int opt_force; |
| 33 | static int opt_load_all; |
| 34 | |
| 35 | static const char *session_name; |
| 36 | |
| 37 | enum { |
| 38 | OPT_HELP = 1, |
| 39 | OPT_ALL, |
| 40 | OPT_FORCE, |
| 41 | OPT_LIST_OPTIONS, |
| 42 | }; |
| 43 | |
| 44 | static struct mi_writer *writer; |
| 45 | |
| 46 | static struct poptOption load_opts[] = { |
| 47 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 48 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, |
| 49 | {"all", 'a', POPT_ARG_NONE, 0, OPT_ALL, 0, 0}, |
| 50 | {"input-path", 'i', POPT_ARG_STRING, &opt_input_path, 0, 0, 0}, |
| 51 | {"force", 'f', POPT_ARG_NONE, 0, OPT_FORCE, 0, 0}, |
| 52 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
| 53 | {0, 0, 0, 0, 0, 0, 0} |
| 54 | }; |
| 55 | |
| 56 | static int mi_partial_session(const char *session_name) |
| 57 | { |
| 58 | int ret; |
| 59 | assert(writer); |
| 60 | assert(session_name); |
| 61 | |
| 62 | /* Open session element */ |
| 63 | ret = mi_lttng_writer_open_element(writer, config_element_session); |
| 64 | if (ret) { |
| 65 | goto end; |
| 66 | } |
| 67 | |
| 68 | ret = mi_lttng_writer_write_element_string(writer, config_element_name, |
| 69 | session_name); |
| 70 | if (ret) { |
| 71 | goto end; |
| 72 | } |
| 73 | |
| 74 | /* Closing session element */ |
| 75 | ret = mi_lttng_writer_close_element(writer); |
| 76 | end: |
| 77 | return ret; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Mi print of load command |
| 82 | */ |
| 83 | static int mi_load_print(const char *session_name) |
| 84 | { |
| 85 | int ret; |
| 86 | assert(writer); |
| 87 | |
| 88 | if (opt_load_all) { |
| 89 | /* We use a wildcard to represent all sessions */ |
| 90 | session_name = "*"; |
| 91 | } |
| 92 | |
| 93 | /* Print load element */ |
| 94 | ret = mi_lttng_writer_open_element(writer, mi_lttng_element_load); |
| 95 | if (ret) { |
| 96 | goto end; |
| 97 | } |
| 98 | |
| 99 | /* Print session element */ |
| 100 | ret = mi_partial_session(session_name); |
| 101 | if (ret) { |
| 102 | goto end; |
| 103 | } |
| 104 | |
| 105 | /* Path element */ |
| 106 | if (opt_input_path) { |
| 107 | ret = mi_lttng_writer_write_element_string(writer, config_element_path, |
| 108 | opt_input_path); |
| 109 | if (ret) { |
| 110 | goto end; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /* Close load element */ |
| 115 | ret = mi_lttng_writer_close_element(writer); |
| 116 | |
| 117 | end: |
| 118 | return ret; |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * The 'load <options>' first level command |
| 123 | */ |
| 124 | int cmd_load(int argc, const char **argv) |
| 125 | { |
| 126 | int ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success; |
| 127 | int opt; |
| 128 | poptContext pc; |
| 129 | |
| 130 | pc = poptGetContext(NULL, argc, argv, load_opts, 0); |
| 131 | poptReadDefaultConfig(pc, 0); |
| 132 | |
| 133 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 134 | switch (opt) { |
| 135 | case OPT_HELP: |
| 136 | SHOW_HELP(); |
| 137 | goto end; |
| 138 | case OPT_ALL: |
| 139 | opt_load_all = 1; |
| 140 | break; |
| 141 | case OPT_LIST_OPTIONS: |
| 142 | list_cmd_options(stdout, load_opts); |
| 143 | goto end; |
| 144 | case OPT_FORCE: |
| 145 | opt_force = 1; |
| 146 | break; |
| 147 | default: |
| 148 | ret = CMD_UNDEFINED; |
| 149 | goto end; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | if (!opt_load_all) { |
| 154 | session_name = poptGetArg(pc); |
| 155 | if (session_name) { |
| 156 | DBG2("Loading session name: %s", session_name); |
| 157 | } else { |
| 158 | /* Default to load_all */ |
| 159 | opt_load_all = 1; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /* Mi check */ |
| 164 | if (lttng_opt_mi) { |
| 165 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); |
| 166 | if (!writer) { |
| 167 | ret = -LTTNG_ERR_NOMEM; |
| 168 | goto end; |
| 169 | } |
| 170 | |
| 171 | /* Open command element */ |
| 172 | ret = mi_lttng_writer_command_open(writer, |
| 173 | mi_lttng_element_command_load); |
| 174 | if (ret) { |
| 175 | ret = CMD_ERROR; |
| 176 | goto end; |
| 177 | } |
| 178 | |
| 179 | /* Open output element */ |
| 180 | ret = mi_lttng_writer_open_element(writer, |
| 181 | mi_lttng_element_command_output); |
| 182 | if (ret) { |
| 183 | ret = CMD_ERROR; |
| 184 | goto end; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | command_ret = config_load_session(opt_input_path, session_name, opt_force, 0); |
| 189 | if (command_ret) { |
| 190 | ERR("%s", lttng_strerror(command_ret)); |
| 191 | success = 0; |
| 192 | } else { |
| 193 | if (opt_load_all) { |
| 194 | MSG("All sessions have been loaded successfully"); |
| 195 | } else if (session_name) { |
| 196 | ret = config_init((char *)session_name); |
| 197 | if (ret < 0) { |
| 198 | ret = CMD_WARNING; |
| 199 | } |
| 200 | MSG("Session %s has been loaded successfully", session_name); |
| 201 | } else { |
| 202 | MSG("Session has been loaded successfully"); |
| 203 | } |
| 204 | success = 1; |
| 205 | } |
| 206 | |
| 207 | /* Mi Printing and closing */ |
| 208 | if (lttng_opt_mi) { |
| 209 | /* Mi print */ |
| 210 | ret = mi_load_print(session_name); |
| 211 | if (ret) { |
| 212 | ret = CMD_ERROR; |
| 213 | goto end; |
| 214 | } |
| 215 | |
| 216 | /* Close output element */ |
| 217 | ret = mi_lttng_writer_close_element(writer); |
| 218 | if (ret) { |
| 219 | ret = CMD_ERROR; |
| 220 | goto end; |
| 221 | } |
| 222 | |
| 223 | /* Success ? */ |
| 224 | ret = mi_lttng_writer_write_element_bool(writer, |
| 225 | mi_lttng_element_command_success, success); |
| 226 | if (ret) { |
| 227 | ret = CMD_ERROR; |
| 228 | goto end; |
| 229 | } |
| 230 | |
| 231 | /* Command element close */ |
| 232 | ret = mi_lttng_writer_command_close(writer); |
| 233 | if (ret) { |
| 234 | ret = CMD_ERROR; |
| 235 | goto end; |
| 236 | } |
| 237 | } |
| 238 | end: |
| 239 | if (writer && mi_lttng_writer_destroy(writer)) { |
| 240 | /* Preserve original error code */ |
| 241 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; |
| 242 | } |
| 243 | |
| 244 | /* Overwrite ret if the was an error with the load command */ |
| 245 | ret = command_ret ? -command_ret : ret; |
| 246 | |
| 247 | poptFreeContext(pc); |
| 248 | return ret; |
| 249 | } |