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
;
39 static struct mi_writer
*writer
;
41 static struct poptOption long_options
[] = {
42 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
43 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
44 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
49 * Print the necessary mi for a session and name.
51 static int mi_print(char *session_name
)
59 * Open a sessions element
60 * This is purely for validation purpose
62 ret
= mi_lttng_sessions_open(writer
);
67 /* Open a session element */
68 ret
= mi_lttng_writer_open_element(writer
, config_element_session
);
74 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
80 /* Close session and sessions element */
81 ret
= mi_lttng_close_multi_element(writer
, 2);
92 static int set_session(void)
94 int ret
= CMD_SUCCESS
;
96 unsigned int session_found
= 0;
97 struct lttng_session
*sessions
;
99 if (opt_session_name
&& strlen(opt_session_name
) > NAME_MAX
) {
100 ERR("Session name too long. Length must be lower or equal to %d",
106 count
= lttng_list_sessions(&sessions
);
109 ERR("%s", lttng_strerror(count
));
113 for (i
= 0; i
< count
; i
++) {
114 if (strncmp(sessions
[i
].name
, opt_session_name
, NAME_MAX
) == 0) {
120 if (!session_found
) {
121 ERR("Session '%s' not found", opt_session_name
);
126 ret
= config_init(opt_session_name
);
128 ERR("Unable to set session name");
133 MSG("Session set to %s", opt_session_name
);
135 ret
= mi_print(opt_session_name
);
153 int cmd_set_session(int argc
, const char **argv
)
155 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
156 static poptContext pc
;
158 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
159 poptReadDefaultConfig(pc
, 0);
161 while ((opt
= poptGetNextOpt(pc
)) != -1) {
166 case OPT_LIST_OPTIONS
:
167 list_cmd_options(stdout
, long_options
);
175 opt_session_name
= (char *) poptGetArg(pc
);
176 if (opt_session_name
== NULL
) {
177 ERR("Missing session name");
184 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
186 ret
= -LTTNG_ERR_NOMEM
;
190 /* Open command element */
191 ret
= mi_lttng_writer_command_open(writer
,
192 mi_lttng_element_command_set_session
);
198 /* Open output element */
199 ret
= mi_lttng_writer_open_element(writer
,
200 mi_lttng_element_command_output
);
207 command_ret
= set_session();
214 /* Close output element */
215 ret
= mi_lttng_writer_close_element(writer
);
222 ret
= mi_lttng_writer_write_element_bool(writer
,
223 mi_lttng_element_command_success
, success
);
229 /* Command element close */
230 ret
= mi_lttng_writer_command_close(writer
);
239 if (writer
&& mi_lttng_writer_destroy(writer
)) {
240 /* Preserve original error code */
241 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
244 /* Overwrite ret if an error occurred during set_session() */
245 ret
= command_ret
? command_ret
: ret
;