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