14fe9afd8362ee20eaaf2bc5618460af7217d953
[lttng-tools.git] / src / bin / lttng / commands / set_session.c
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 #define _LGPL_SOURCE
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>
27 #include <assert.h>
28
29 #include <common/mi-lttng.h>
30
31 #include "../command.h"
32
33 static char *opt_session_name;
34
35 enum {
36 OPT_HELP = 1,
37 OPT_LIST_OPTIONS,
38 };
39
40 static struct mi_writer *writer;
41
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},
45 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
46 {0, 0, 0, 0, 0, 0, 0}
47 };
48
49 /*
50 * usage
51 */
52 static void usage(FILE *ofp)
53 {
54 fprintf(ofp, "usage: lttng set-session NAME [OPTIONS]\n");
55 fprintf(ofp, "\n");
56 fprintf(ofp, "Options:\n");
57 fprintf(ofp, " -h, --help Show this help\n");
58 fprintf(ofp, " --list-options Simple listing of options\n");
59 fprintf(ofp, "\n");
60 }
61
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
103 /*
104 * set_session
105 */
106 static int set_session(void)
107 {
108 int ret = CMD_SUCCESS;
109 int count, i;
110 unsigned int session_found = 0;
111 struct lttng_session *sessions;
112
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;
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;
137 goto error;
138 }
139
140 ret = config_init(opt_session_name);
141 if (ret < 0) {
142 ERR("Unable to set session name");
143 ret = CMD_ERROR;
144 goto error;
145 }
146
147 MSG("Session set to %s", opt_session_name);
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
156 ret = CMD_SUCCESS;
157
158 error:
159 free(sessions);
160 end:
161 return ret;
162 }
163
164 /*
165 * cmd_set_session
166 */
167 int cmd_set_session(int argc, const char **argv)
168 {
169 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
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:
178 usage(stdout);
179 goto end;
180 case OPT_LIST_OPTIONS:
181 list_cmd_options(stdout, long_options);
182 goto end;
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);
194 ret = CMD_ERROR;
195 goto end;
196 }
197
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 }
252
253 end:
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
263 poptFreeContext(pc);
264 return ret;
265 }
This page took 0.033319 seconds and 3 git commands to generate.