2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
14 #include <sys/types.h>
17 #include <common/mi-lttng.hpp>
19 #include "../command.hpp"
21 static char *opt_session_name
;
23 #ifdef LTTNG_EMBED_HELP
24 static const char help_msg
[] =
25 #include <lttng-set-session.1.h>
34 static struct mi_writer
*writer
;
36 static struct poptOption long_options
[] = {
37 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
38 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
39 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
44 * Print the necessary mi for a session and name.
46 static int mi_print(char *session_name
)
51 LTTNG_ASSERT(session_name
);
54 * Open a sessions element
55 * This is purely for validation purpose
57 ret
= mi_lttng_sessions_open(writer
);
62 /* Open a session element */
63 ret
= mi_lttng_writer_open_element(writer
, config_element_session
);
69 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
75 /* Close session and sessions element */
76 ret
= mi_lttng_close_multi_element(writer
, 2);
87 static int set_session(void)
89 int ret
= CMD_SUCCESS
;
91 unsigned int session_found
= 0;
92 struct lttng_session
*sessions
;
94 if (opt_session_name
&& strlen(opt_session_name
) > NAME_MAX
) {
95 ERR("Session name too long. Length must be lower or equal to %d",
101 count
= lttng_list_sessions(&sessions
);
104 ERR("%s", lttng_strerror(count
));
108 for (i
= 0; i
< count
; i
++) {
109 if (strncmp(sessions
[i
].name
, opt_session_name
, NAME_MAX
) == 0) {
115 if (!session_found
) {
116 ERR("Session '%s' not found", opt_session_name
);
121 ret
= config_init(opt_session_name
);
123 ERR("Unable to set session name");
128 MSG("Session set to %s", opt_session_name
);
130 ret
= mi_print(opt_session_name
);
148 int cmd_set_session(int argc
, const char **argv
)
150 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
151 static poptContext pc
;
153 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
154 poptReadDefaultConfig(pc
, 0);
156 while ((opt
= poptGetNextOpt(pc
)) != -1) {
161 case OPT_LIST_OPTIONS
:
162 list_cmd_options(stdout
, long_options
);
170 opt_session_name
= (char *) poptGetArg(pc
);
171 if (opt_session_name
== NULL
) {
172 ERR("Missing session name");
179 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
181 ret
= -LTTNG_ERR_NOMEM
;
185 /* Open command element */
186 ret
= mi_lttng_writer_command_open(writer
,
187 mi_lttng_element_command_set_session
);
193 /* Open output element */
194 ret
= mi_lttng_writer_open_element(writer
,
195 mi_lttng_element_command_output
);
202 command_ret
= set_session();
209 /* Close output element */
210 ret
= mi_lttng_writer_close_element(writer
);
217 ret
= mi_lttng_writer_write_element_bool(writer
,
218 mi_lttng_element_command_success
, success
);
224 /* Command element close */
225 ret
= mi_lttng_writer_command_close(writer
);
234 if (writer
&& mi_lttng_writer_destroy(writer
)) {
235 /* Preserve original error code */
236 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
239 /* Overwrite ret if an error occurred during set_session() */
240 ret
= command_ret
? command_ret
: ret
;