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.
25 #include <sys/types.h>
29 #include <common/mi-lttng.h>
31 #include "../command.h"
33 static char *opt_session_name
;
40 static struct mi_writer
*writer
;
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
},
52 static void usage(FILE *ofp
)
54 fprintf(ofp
, "usage: lttng set-session NAME [OPTIONS]\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");
63 * Print the necessary mi for a session and name.
65 static int mi_print(char *session_name
)
73 * Open a sessions element
74 * This is purely for validation purpose
76 ret
= mi_lttng_sessions_open(writer
);
81 /* Open a session element */
82 ret
= mi_lttng_writer_open_element(writer
, config_element_session
);
88 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
94 /* Close session and sessions element */
95 ret
= mi_lttng_close_multi_element(writer
, 2);
106 static int set_session(void)
108 int ret
= CMD_SUCCESS
;
110 unsigned int session_found
= 0;
111 struct lttng_session
*sessions
;
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",
120 count
= lttng_list_sessions(&sessions
);
123 ERR("%s", lttng_strerror(count
));
127 for (i
= 0; i
< count
; i
++) {
128 if (strncmp(sessions
[i
].name
, opt_session_name
, NAME_MAX
) == 0) {
134 if (!session_found
) {
135 ERR("Session '%s' not found", opt_session_name
);
140 ret
= config_init(opt_session_name
);
142 ERR("Unable to set session name");
147 MSG("Session set to %s", opt_session_name
);
149 ret
= mi_print(opt_session_name
);
167 int cmd_set_session(int argc
, const char **argv
)
169 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
170 static poptContext pc
;
172 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
173 poptReadDefaultConfig(pc
, 0);
175 while ((opt
= poptGetNextOpt(pc
)) != -1) {
180 case OPT_LIST_OPTIONS
:
181 list_cmd_options(stdout
, long_options
);
190 opt_session_name
= (char *) poptGetArg(pc
);
191 if (opt_session_name
== NULL
) {
192 ERR("Missing session name");
200 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
202 ret
= -LTTNG_ERR_NOMEM
;
206 /* Open command element */
207 ret
= mi_lttng_writer_command_open(writer
,
208 mi_lttng_element_command_set_session
);
214 /* Open output element */
215 ret
= mi_lttng_writer_open_element(writer
,
216 mi_lttng_element_command_output
);
223 command_ret
= set_session();
230 /* Close output element */
231 ret
= mi_lttng_writer_close_element(writer
);
238 ret
= mi_lttng_writer_write_element_bool(writer
,
239 mi_lttng_element_command_success
, success
);
245 /* Command element close */
246 ret
= mi_lttng_writer_command_close(writer
);
255 if (writer
&& mi_lttng_writer_destroy(writer
)) {
256 /* Preserve original error code */
257 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
260 /* Overwrite ret if an error occured during set_session() */
261 ret
= command_ret
? command_ret
: ret
;