2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
4 * SPDX-License-Identifier: GPL-2.0-only
14 #include <sys/types.h>
18 #include <common/mi-lttng.h>
20 #include "../command.h"
22 static char *opt_session_name
;
24 #ifdef LTTNG_EMBED_HELP
25 static const char help_msg
[] =
26 #include <lttng-set-session.1.h>
35 static struct mi_writer
*writer
;
37 static struct poptOption long_options
[] = {
38 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
39 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
40 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
45 * Print the necessary mi for a session and name.
47 static int mi_print(char *session_name
)
55 * Open a sessions element
56 * This is purely for validation purpose
58 ret
= mi_lttng_sessions_open(writer
);
63 /* Open a session element */
64 ret
= mi_lttng_writer_open_element(writer
, config_element_session
);
70 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
76 /* Close session and sessions element */
77 ret
= mi_lttng_close_multi_element(writer
, 2);
88 static int set_session(void)
90 int ret
= CMD_SUCCESS
;
92 unsigned int session_found
= 0;
93 struct lttng_session
*sessions
;
95 if (opt_session_name
&& strlen(opt_session_name
) > NAME_MAX
) {
96 ERR("Session name too long. Length must be lower or equal to %d",
102 count
= lttng_list_sessions(&sessions
);
105 ERR("%s", lttng_strerror(count
));
109 for (i
= 0; i
< count
; i
++) {
110 if (strncmp(sessions
[i
].name
, opt_session_name
, NAME_MAX
) == 0) {
116 if (!session_found
) {
117 ERR("Session '%s' not found", opt_session_name
);
122 ret
= config_init(opt_session_name
);
124 ERR("Unable to set session name");
129 MSG("Session set to %s", opt_session_name
);
131 ret
= mi_print(opt_session_name
);
149 int cmd_set_session(int argc
, const char **argv
)
151 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
152 static poptContext pc
;
154 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
155 poptReadDefaultConfig(pc
, 0);
157 while ((opt
= poptGetNextOpt(pc
)) != -1) {
162 case OPT_LIST_OPTIONS
:
163 list_cmd_options(stdout
, long_options
);
171 opt_session_name
= (char *) poptGetArg(pc
);
172 if (opt_session_name
== NULL
) {
173 ERR("Missing session name");
180 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
182 ret
= -LTTNG_ERR_NOMEM
;
186 /* Open command element */
187 ret
= mi_lttng_writer_command_open(writer
,
188 mi_lttng_element_command_set_session
);
194 /* Open output element */
195 ret
= mi_lttng_writer_open_element(writer
,
196 mi_lttng_element_command_output
);
203 command_ret
= set_session();
210 /* Close output element */
211 ret
= mi_lttng_writer_close_element(writer
);
218 ret
= mi_lttng_writer_write_element_bool(writer
,
219 mi_lttng_element_command_success
, success
);
225 /* Command element close */
226 ret
= mi_lttng_writer_command_close(writer
);
235 if (writer
&& mi_lttng_writer_destroy(writer
)) {
236 /* Preserve original error code */
237 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
240 /* Overwrite ret if an error occurred during set_session() */
241 ret
= command_ret
? command_ret
: ret
;