| 1 | /* |
| 2 | * Copyright (C) 2015 - 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 <assert.h> |
| 20 | #include <ctype.h> |
| 21 | #include <popt.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <unistd.h> |
| 26 | |
| 27 | #include <common/mi-lttng.h> |
| 28 | |
| 29 | #include "../command.h" |
| 30 | |
| 31 | static char *opt_session_name; |
| 32 | static char *session_name = NULL; |
| 33 | |
| 34 | static int metadata_regenerate(int argc, const char **argv); |
| 35 | |
| 36 | #ifdef LTTNG_EMBED_HELP |
| 37 | static const char help_msg[] = |
| 38 | #include <lttng-metadata.1.h> |
| 39 | ; |
| 40 | #endif |
| 41 | |
| 42 | enum { |
| 43 | OPT_HELP = 1, |
| 44 | OPT_LIST_OPTIONS, |
| 45 | OPT_LIST_COMMANDS, |
| 46 | }; |
| 47 | |
| 48 | static struct mi_writer *writer; |
| 49 | |
| 50 | static struct poptOption long_options[] = { |
| 51 | /* { longName, shortName, argInfo, argPtr, value, descrip, argDesc, } */ |
| 52 | { "help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0, }, |
| 53 | { "session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0}, |
| 54 | { "list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, 0, 0, }, |
| 55 | { "list-commands", 0, POPT_ARG_NONE, NULL, OPT_LIST_COMMANDS}, |
| 56 | { 0, 0, 0, 0, 0, 0, 0, }, |
| 57 | }; |
| 58 | |
| 59 | static struct cmd_struct actions[] = { |
| 60 | { "regenerate", metadata_regenerate }, |
| 61 | { NULL, NULL } /* Array closure */ |
| 62 | }; |
| 63 | |
| 64 | /* |
| 65 | * Count and return the number of arguments in argv. |
| 66 | */ |
| 67 | static int count_arguments(const char **argv) |
| 68 | { |
| 69 | int i = 0; |
| 70 | |
| 71 | assert(argv); |
| 72 | |
| 73 | while (argv[i] != NULL) { |
| 74 | i++; |
| 75 | } |
| 76 | |
| 77 | return i; |
| 78 | } |
| 79 | |
| 80 | static int metadata_regenerate(int argc, const char **argv) |
| 81 | { |
| 82 | int ret; |
| 83 | |
| 84 | if (argc > 1) { |
| 85 | ret = -LTTNG_ERR_INVALID; |
| 86 | goto end; |
| 87 | } |
| 88 | ret = lttng_regenerate_metadata(session_name); |
| 89 | if (ret == 0) { |
| 90 | MSG("Metadata successfully regenerated for session %s", session_name); |
| 91 | } |
| 92 | |
| 93 | end: |
| 94 | return ret; |
| 95 | } |
| 96 | |
| 97 | static int handle_command(const char **argv) |
| 98 | { |
| 99 | struct cmd_struct *cmd; |
| 100 | int ret = CMD_SUCCESS, i = 0, argc, command_ret = CMD_SUCCESS; |
| 101 | |
| 102 | if (argv == NULL) { |
| 103 | ERR("argv is null"); |
| 104 | command_ret = CMD_ERROR; |
| 105 | goto end; |
| 106 | } |
| 107 | |
| 108 | argc = count_arguments(argv); |
| 109 | |
| 110 | cmd = &actions[i]; |
| 111 | while (cmd->func != NULL) { |
| 112 | /* Find command */ |
| 113 | if (strcmp(argv[0], cmd->name) == 0) { |
| 114 | if (lttng_opt_mi) { |
| 115 | /* Action element */ |
| 116 | ret = mi_lttng_writer_open_element(writer, |
| 117 | mi_lttng_element_command_metadata_action); |
| 118 | if (ret) { |
| 119 | ret = CMD_ERROR; |
| 120 | goto end; |
| 121 | } |
| 122 | |
| 123 | /* Name of the action */ |
| 124 | ret = mi_lttng_writer_write_element_string(writer, |
| 125 | config_element_name, argv[0]); |
| 126 | if (ret) { |
| 127 | ret = CMD_ERROR; |
| 128 | goto end; |
| 129 | } |
| 130 | } |
| 131 | command_ret = cmd->func(argc, argv); |
| 132 | if (lttng_opt_mi) { |
| 133 | /* Close output and action element */ |
| 134 | ret = mi_lttng_writer_close_element(writer); |
| 135 | if (ret) { |
| 136 | ret = CMD_ERROR; |
| 137 | goto end; |
| 138 | } |
| 139 | } |
| 140 | goto end; |
| 141 | } |
| 142 | |
| 143 | cmd = &actions[i++]; |
| 144 | } |
| 145 | |
| 146 | ret = CMD_UNDEFINED; |
| 147 | |
| 148 | end: |
| 149 | /* Overwrite ret if an error occurred in cmd->func() */ |
| 150 | ret = command_ret ? command_ret : ret; |
| 151 | return ret; |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Metadata command handling. |
| 156 | */ |
| 157 | int cmd_metadata(int argc, const char **argv) |
| 158 | { |
| 159 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
| 160 | static poptContext pc; |
| 161 | |
| 162 | if (argc < 1) { |
| 163 | SHOW_HELP(); |
| 164 | ret = CMD_ERROR; |
| 165 | goto end; |
| 166 | } |
| 167 | |
| 168 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
| 169 | poptReadDefaultConfig(pc, 0); |
| 170 | |
| 171 | if (lttng_opt_mi) { |
| 172 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); |
| 173 | if (!writer) { |
| 174 | ret = -LTTNG_ERR_NOMEM; |
| 175 | goto end; |
| 176 | } |
| 177 | /* Open command element */ |
| 178 | ret = mi_lttng_writer_command_open(writer, |
| 179 | mi_lttng_element_command_metadata); |
| 180 | if (ret) { |
| 181 | ret = CMD_ERROR; |
| 182 | goto end; |
| 183 | } |
| 184 | |
| 185 | /* Open output element */ |
| 186 | ret = mi_lttng_writer_open_element(writer, |
| 187 | mi_lttng_element_command_output); |
| 188 | if (ret) { |
| 189 | ret = CMD_ERROR; |
| 190 | goto end; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 195 | switch (opt) { |
| 196 | case OPT_HELP: |
| 197 | SHOW_HELP(); |
| 198 | goto end; |
| 199 | case OPT_LIST_OPTIONS: |
| 200 | list_cmd_options(stdout, long_options); |
| 201 | goto end; |
| 202 | case OPT_LIST_COMMANDS: |
| 203 | list_commands(actions, stdout); |
| 204 | goto end; |
| 205 | default: |
| 206 | SHOW_HELP(); |
| 207 | ret = CMD_UNDEFINED; |
| 208 | goto end; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | if (!opt_session_name) { |
| 213 | session_name = get_session_name(); |
| 214 | if (session_name == NULL) { |
| 215 | ret = CMD_ERROR; |
| 216 | goto end; |
| 217 | } |
| 218 | } else { |
| 219 | session_name = opt_session_name; |
| 220 | } |
| 221 | |
| 222 | command_ret = handle_command(poptGetArgs(pc)); |
| 223 | if (command_ret) { |
| 224 | switch (-command_ret) { |
| 225 | default: |
| 226 | ERR("%s", lttng_strerror(command_ret)); |
| 227 | success = 0; |
| 228 | break; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | if (lttng_opt_mi) { |
| 233 | /* Close output element */ |
| 234 | ret = mi_lttng_writer_close_element(writer); |
| 235 | if (ret) { |
| 236 | ret = CMD_ERROR; |
| 237 | goto end; |
| 238 | } |
| 239 | |
| 240 | /* Success ? */ |
| 241 | ret = mi_lttng_writer_write_element_bool(writer, |
| 242 | mi_lttng_element_command_success, success); |
| 243 | if (ret) { |
| 244 | ret = CMD_ERROR; |
| 245 | goto end; |
| 246 | } |
| 247 | |
| 248 | /* Command element close */ |
| 249 | ret = mi_lttng_writer_command_close(writer); |
| 250 | if (ret) { |
| 251 | ret = CMD_ERROR; |
| 252 | goto end; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | end: |
| 257 | /* Mi clean-up */ |
| 258 | if (writer && mi_lttng_writer_destroy(writer)) { |
| 259 | /* Preserve original error code */ |
| 260 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; |
| 261 | } |
| 262 | |
| 263 | if (!opt_session_name) { |
| 264 | free(session_name); |
| 265 | } |
| 266 | |
| 267 | /* Overwrite ret if an error occurred during handle_command() */ |
| 268 | ret = command_ret ? command_ret : ret; |
| 269 | |
| 270 | poptFreeContext(pc); |
| 271 | return ret; |
| 272 | } |