2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
9 #include "../command.hpp"
11 #include <common/mi-lttng.hpp>
18 #include <sys/types.h>
21 #ifdef LTTNG_EMBED_HELP
22 static const char help_msg
[] =
23 #include <lttng-set-session.1.h>
32 static struct mi_writer
*writer
;
34 static struct poptOption long_options
[] = {
35 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
36 { "help", 'h', POPT_ARG_NONE
, nullptr, OPT_HELP
, nullptr, nullptr },
37 { "list-options", 0, POPT_ARG_NONE
, nullptr, OPT_LIST_OPTIONS
, nullptr, nullptr },
38 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
42 * Print the necessary mi for a session and name.
44 static int mi_print(const char *session_name
)
49 LTTNG_ASSERT(session_name
);
52 * Open a sessions element
53 * This is purely for validation purpose
55 ret
= mi_lttng_sessions_open(writer
);
60 /* Open a session element */
61 ret
= mi_lttng_writer_open_element(writer
, config_element_session
);
67 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
, session_name
);
72 /* Close session and sessions element */
73 ret
= mi_lttng_close_multi_element(writer
, 2);
84 static int set_session(const char *session_name
)
86 int ret
= CMD_SUCCESS
;
88 unsigned int session_found
= 0;
89 struct lttng_session
*sessions
;
91 if (session_name
&& strlen(session_name
) > NAME_MAX
) {
92 ERR("Session name too long. Length must be lower or equal to %d", NAME_MAX
);
97 count
= lttng_list_sessions(&sessions
);
100 ERR("%s", lttng_strerror(count
));
104 for (i
= 0; i
< count
; i
++) {
105 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
111 if (!session_found
) {
112 ERR("Session '%s' not found", session_name
);
117 ret
= config_init(session_name
);
119 ERR("Unable to set session name");
124 MSG("Session set to %s", session_name
);
126 ret
= mi_print(session_name
);
144 int cmd_set_session(int argc
, const char **argv
)
146 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
147 static poptContext pc
;
148 const char *arg_session_name
= nullptr;
150 pc
= poptGetContext(nullptr, argc
, argv
, long_options
, 0);
151 poptReadDefaultConfig(pc
, 0);
153 while ((opt
= poptGetNextOpt(pc
)) != -1) {
158 case OPT_LIST_OPTIONS
:
159 list_cmd_options(stdout
, long_options
);
167 arg_session_name
= poptGetArg(pc
);
168 if (arg_session_name
== nullptr) {
169 ERR("Missing session name");
176 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
178 ret
= -LTTNG_ERR_NOMEM
;
182 /* Open command element */
183 ret
= mi_lttng_writer_command_open(writer
, mi_lttng_element_command_set_session
);
189 /* Open output element */
190 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_command_output
);
197 command_ret
= set_session(arg_session_name
);
204 /* Close output element */
205 ret
= mi_lttng_writer_close_element(writer
);
212 ret
= mi_lttng_writer_write_element_bool(
213 writer
, mi_lttng_element_command_success
, success
);
219 /* Command element close */
220 ret
= mi_lttng_writer_command_close(writer
);
229 if (writer
&& mi_lttng_writer_destroy(writer
)) {
230 /* Preserve original error code */
231 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
234 /* Overwrite ret if an error occurred during set_session() */
235 ret
= command_ret
? command_ret
: ret
;