| 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 _GNU_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 | |
| 27 | #include "../command.h" |
| 28 | |
| 29 | static char *opt_session_name; |
| 30 | |
| 31 | enum { |
| 32 | OPT_HELP = 1, |
| 33 | OPT_LIST_OPTIONS, |
| 34 | }; |
| 35 | |
| 36 | static struct poptOption long_options[] = { |
| 37 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 38 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, |
| 39 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
| 40 | {0, 0, 0, 0, 0, 0, 0} |
| 41 | }; |
| 42 | |
| 43 | /* |
| 44 | * usage |
| 45 | */ |
| 46 | static void usage(FILE *ofp) |
| 47 | { |
| 48 | fprintf(ofp, "usage: lttng set-session NAME [OPTIONS]\n"); |
| 49 | fprintf(ofp, "\n"); |
| 50 | fprintf(ofp, "Options:\n"); |
| 51 | fprintf(ofp, " -h, --help Show this help\n"); |
| 52 | fprintf(ofp, " --list-options Simple listing of options\n"); |
| 53 | fprintf(ofp, "\n"); |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * set_session |
| 58 | */ |
| 59 | static int set_session(void) |
| 60 | { |
| 61 | int ret = CMD_SUCCESS; |
| 62 | int count, i; |
| 63 | unsigned int session_found = 0; |
| 64 | struct lttng_session *sessions; |
| 65 | |
| 66 | if (opt_session_name && strlen(opt_session_name) > NAME_MAX) { |
| 67 | ERR("Session name too long. Length must be lower or equal to %d", |
| 68 | NAME_MAX); |
| 69 | ret = CMD_ERROR; |
| 70 | goto end; |
| 71 | } |
| 72 | |
| 73 | count = lttng_list_sessions(&sessions); |
| 74 | if (count < 0) { |
| 75 | ret = CMD_ERROR; |
| 76 | ERR("%s", lttng_strerror(count)); |
| 77 | goto end; |
| 78 | } |
| 79 | |
| 80 | for (i = 0; i < count; i++) { |
| 81 | if (strncmp(sessions[i].name, opt_session_name, NAME_MAX) == 0) { |
| 82 | session_found = 1; |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | if (!session_found) { |
| 88 | ERR("Session '%s' not found", opt_session_name); |
| 89 | ret = CMD_ERROR; |
| 90 | goto error; |
| 91 | } |
| 92 | |
| 93 | ret = config_init(opt_session_name); |
| 94 | if (ret < 0) { |
| 95 | ERR("Unable to set session name"); |
| 96 | ret = CMD_ERROR; |
| 97 | goto error; |
| 98 | } |
| 99 | |
| 100 | MSG("Session set to %s", opt_session_name); |
| 101 | ret = CMD_SUCCESS; |
| 102 | |
| 103 | error: |
| 104 | free(sessions); |
| 105 | end: |
| 106 | return ret; |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * cmd_set_session |
| 111 | */ |
| 112 | int cmd_set_session(int argc, const char **argv) |
| 113 | { |
| 114 | int opt, ret = CMD_SUCCESS; |
| 115 | static poptContext pc; |
| 116 | |
| 117 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
| 118 | poptReadDefaultConfig(pc, 0); |
| 119 | |
| 120 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 121 | switch (opt) { |
| 122 | case OPT_HELP: |
| 123 | usage(stdout); |
| 124 | goto end; |
| 125 | case OPT_LIST_OPTIONS: |
| 126 | list_cmd_options(stdout, long_options); |
| 127 | goto end; |
| 128 | default: |
| 129 | usage(stderr); |
| 130 | ret = CMD_UNDEFINED; |
| 131 | goto end; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | opt_session_name = (char *) poptGetArg(pc); |
| 136 | if (opt_session_name == NULL) { |
| 137 | ERR("Missing session name"); |
| 138 | usage(stderr); |
| 139 | ret = CMD_ERROR; |
| 140 | goto end; |
| 141 | } |
| 142 | |
| 143 | ret = set_session(); |
| 144 | |
| 145 | end: |
| 146 | poptFreeContext(pc); |
| 147 | return ret; |
| 148 | } |