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