| 1 | /* |
| 2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
| 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 <assert.h> |
| 27 | |
| 28 | #include <common/mi-lttng.h> |
| 29 | |
| 30 | #include "../command.h" |
| 31 | |
| 32 | static char *opt_session_name; |
| 33 | |
| 34 | enum { |
| 35 | OPT_HELP = 1, |
| 36 | OPT_LIST_OPTIONS, |
| 37 | }; |
| 38 | |
| 39 | static struct mi_writer *writer; |
| 40 | |
| 41 | static struct poptOption long_options[] = { |
| 42 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 43 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, |
| 44 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
| 45 | {0, 0, 0, 0, 0, 0, 0} |
| 46 | }; |
| 47 | |
| 48 | /* |
| 49 | * Print the necessary mi for a session and name. |
| 50 | */ |
| 51 | static int mi_print(char *session_name) |
| 52 | { |
| 53 | int ret; |
| 54 | |
| 55 | assert(writer); |
| 56 | assert(session_name); |
| 57 | |
| 58 | /* |
| 59 | * Open a sessions element |
| 60 | * This is purely for validation purpose |
| 61 | */ |
| 62 | ret = mi_lttng_sessions_open(writer); |
| 63 | if (ret) { |
| 64 | goto end; |
| 65 | } |
| 66 | |
| 67 | /* Open a session element */ |
| 68 | ret = mi_lttng_writer_open_element(writer, config_element_session); |
| 69 | if (ret) { |
| 70 | goto end; |
| 71 | } |
| 72 | |
| 73 | /* Session name */ |
| 74 | ret = mi_lttng_writer_write_element_string(writer , config_element_name, |
| 75 | session_name); |
| 76 | if (ret) { |
| 77 | goto end; |
| 78 | } |
| 79 | |
| 80 | /* Close session and sessions element */ |
| 81 | ret = mi_lttng_close_multi_element(writer, 2); |
| 82 | if (ret) { |
| 83 | goto end; |
| 84 | } |
| 85 | end: |
| 86 | return ret; |
| 87 | } |
| 88 | |
| 89 | /* |
| 90 | * set_session |
| 91 | */ |
| 92 | static int set_session(void) |
| 93 | { |
| 94 | int ret = CMD_SUCCESS; |
| 95 | int count, i; |
| 96 | unsigned int session_found = 0; |
| 97 | struct lttng_session *sessions; |
| 98 | |
| 99 | if (opt_session_name && strlen(opt_session_name) > NAME_MAX) { |
| 100 | ERR("Session name too long. Length must be lower or equal to %d", |
| 101 | NAME_MAX); |
| 102 | ret = CMD_ERROR; |
| 103 | goto end; |
| 104 | } |
| 105 | |
| 106 | count = lttng_list_sessions(&sessions); |
| 107 | if (count < 0) { |
| 108 | ret = CMD_ERROR; |
| 109 | ERR("%s", lttng_strerror(count)); |
| 110 | goto end; |
| 111 | } |
| 112 | |
| 113 | for (i = 0; i < count; i++) { |
| 114 | if (strncmp(sessions[i].name, opt_session_name, NAME_MAX) == 0) { |
| 115 | session_found = 1; |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | if (!session_found) { |
| 121 | ERR("Session '%s' not found", opt_session_name); |
| 122 | ret = CMD_ERROR; |
| 123 | goto error; |
| 124 | } |
| 125 | |
| 126 | ret = config_init(opt_session_name); |
| 127 | if (ret < 0) { |
| 128 | ERR("Unable to set session name"); |
| 129 | ret = CMD_ERROR; |
| 130 | goto error; |
| 131 | } |
| 132 | |
| 133 | MSG("Session set to %s", opt_session_name); |
| 134 | if (lttng_opt_mi) { |
| 135 | ret = mi_print(opt_session_name); |
| 136 | if (ret) { |
| 137 | ret = CMD_ERROR; |
| 138 | goto error; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | ret = CMD_SUCCESS; |
| 143 | |
| 144 | error: |
| 145 | free(sessions); |
| 146 | end: |
| 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * cmd_set_session |
| 152 | */ |
| 153 | int cmd_set_session(int argc, const char **argv) |
| 154 | { |
| 155 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
| 156 | static poptContext pc; |
| 157 | |
| 158 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
| 159 | poptReadDefaultConfig(pc, 0); |
| 160 | |
| 161 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 162 | switch (opt) { |
| 163 | case OPT_HELP: |
| 164 | SHOW_HELP(); |
| 165 | goto end; |
| 166 | case OPT_LIST_OPTIONS: |
| 167 | list_cmd_options(stdout, long_options); |
| 168 | goto end; |
| 169 | default: |
| 170 | ret = CMD_UNDEFINED; |
| 171 | goto end; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | opt_session_name = (char *) poptGetArg(pc); |
| 176 | if (opt_session_name == NULL) { |
| 177 | ERR("Missing session name"); |
| 178 | ret = CMD_ERROR; |
| 179 | goto end; |
| 180 | } |
| 181 | |
| 182 | /* Mi check */ |
| 183 | if (lttng_opt_mi) { |
| 184 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); |
| 185 | if (!writer) { |
| 186 | ret = -LTTNG_ERR_NOMEM; |
| 187 | goto end; |
| 188 | } |
| 189 | |
| 190 | /* Open command element */ |
| 191 | ret = mi_lttng_writer_command_open(writer, |
| 192 | mi_lttng_element_command_set_session); |
| 193 | if (ret) { |
| 194 | ret = CMD_ERROR; |
| 195 | goto end; |
| 196 | } |
| 197 | |
| 198 | /* Open output element */ |
| 199 | ret = mi_lttng_writer_open_element(writer, |
| 200 | mi_lttng_element_command_output); |
| 201 | if (ret) { |
| 202 | ret = CMD_ERROR; |
| 203 | goto end; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | command_ret = set_session(); |
| 208 | if (command_ret) { |
| 209 | success = 0; |
| 210 | } |
| 211 | |
| 212 | /* Mi closing */ |
| 213 | if (lttng_opt_mi) { |
| 214 | /* Close output element */ |
| 215 | ret = mi_lttng_writer_close_element(writer); |
| 216 | if (ret) { |
| 217 | ret = CMD_ERROR; |
| 218 | goto end; |
| 219 | } |
| 220 | |
| 221 | /* Success ? */ |
| 222 | ret = mi_lttng_writer_write_element_bool(writer, |
| 223 | mi_lttng_element_command_success, success); |
| 224 | if (ret) { |
| 225 | ret = CMD_ERROR; |
| 226 | goto end; |
| 227 | } |
| 228 | |
| 229 | /* Command element close */ |
| 230 | ret = mi_lttng_writer_command_close(writer); |
| 231 | if (ret) { |
| 232 | ret = CMD_ERROR; |
| 233 | goto end; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | end: |
| 238 | /* Mi clean-up */ |
| 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 an error occurred during set_session() */ |
| 245 | ret = command_ret ? command_ret : ret; |
| 246 | |
| 247 | poptFreeContext(pc); |
| 248 | return ret; |
| 249 | } |