2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
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.
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.
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.
24 #include <sys/types.h>
28 #include <common/mi-lttng.h>
30 #include "../command.h"
32 static char *opt_session_name
;
34 #ifdef LTTNG_EMBED_HELP
35 static const char help_msg
[] =
36 #include <lttng-set-session.1.h>
45 static struct mi_writer
*writer
;
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
},
55 * Print the necessary mi for a session and name.
57 static int mi_print(char *session_name
)
65 * Open a sessions element
66 * This is purely for validation purpose
68 ret
= mi_lttng_sessions_open(writer
);
73 /* Open a session element */
74 ret
= mi_lttng_writer_open_element(writer
, config_element_session
);
80 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
86 /* Close session and sessions element */
87 ret
= mi_lttng_close_multi_element(writer
, 2);
98 static int set_session(void)
100 int ret
= CMD_SUCCESS
;
102 unsigned int session_found
= 0;
103 struct lttng_session
*sessions
;
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",
112 count
= lttng_list_sessions(&sessions
);
115 ERR("%s", lttng_strerror(count
));
119 for (i
= 0; i
< count
; i
++) {
120 if (strncmp(sessions
[i
].name
, opt_session_name
, NAME_MAX
) == 0) {
126 if (!session_found
) {
127 ERR("Session '%s' not found", opt_session_name
);
132 ret
= config_init(opt_session_name
);
134 ERR("Unable to set session name");
139 MSG("Session set to %s", opt_session_name
);
141 ret
= mi_print(opt_session_name
);
159 int cmd_set_session(int argc
, const char **argv
)
161 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
162 static poptContext pc
;
164 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
165 poptReadDefaultConfig(pc
, 0);
167 while ((opt
= poptGetNextOpt(pc
)) != -1) {
172 case OPT_LIST_OPTIONS
:
173 list_cmd_options(stdout
, long_options
);
181 opt_session_name
= (char *) poptGetArg(pc
);
182 if (opt_session_name
== NULL
) {
183 ERR("Missing session name");
190 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
192 ret
= -LTTNG_ERR_NOMEM
;
196 /* Open command element */
197 ret
= mi_lttng_writer_command_open(writer
,
198 mi_lttng_element_command_set_session
);
204 /* Open output element */
205 ret
= mi_lttng_writer_open_element(writer
,
206 mi_lttng_element_command_output
);
213 command_ret
= set_session();
220 /* Close output element */
221 ret
= mi_lttng_writer_close_element(writer
);
228 ret
= mi_lttng_writer_write_element_bool(writer
,
229 mi_lttng_element_command_success
, success
);
235 /* Command element close */
236 ret
= mi_lttng_writer_command_close(writer
);
245 if (writer
&& mi_lttng_writer_destroy(writer
)) {
246 /* Preserve original error code */
247 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
250 /* Overwrite ret if an error occurred during set_session() */
251 ret
= command_ret
? command_ret
: ret
;