2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
14 #include <sys/types.h>
18 #include <common/mi-lttng.h>
20 #include "../command.h"
22 #ifdef LTTNG_EMBED_HELP
23 static const char help_msg
[] =
24 #include <lttng-set-session.1.h>
33 static struct mi_writer
*writer
;
35 static struct poptOption long_options
[] = {
36 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
37 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
38 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
43 * Print the necessary mi for a session and name.
45 static int mi_print(const char *session_name
)
53 * Open a sessions element
54 * This is purely for validation purpose
56 ret
= mi_lttng_sessions_open(writer
);
61 /* Open a session element */
62 ret
= mi_lttng_writer_open_element(writer
, config_element_session
);
68 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
74 /* Close session and sessions element */
75 ret
= mi_lttng_close_multi_element(writer
, 2);
86 static int set_session(const char *session_name
)
88 int ret
= CMD_SUCCESS
;
90 unsigned int session_found
= 0;
91 struct lttng_session
*sessions
;
93 if (session_name
&& strlen(session_name
) > NAME_MAX
) {
94 ERR("Session name too long. Length must be lower or equal to %d",
100 count
= lttng_list_sessions(&sessions
);
103 ERR("%s", lttng_strerror(count
));
107 for (i
= 0; i
< count
; i
++) {
108 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
114 if (!session_found
) {
115 ERR("Session '%s' not found", session_name
);
120 ret
= config_init(session_name
);
122 ERR("Unable to set session name");
127 MSG("Session set to %s", session_name
);
129 ret
= mi_print(session_name
);
147 int cmd_set_session(int argc
, const char **argv
)
149 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
150 static poptContext pc
;
151 const char *arg_session_name
= NULL
;
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 arg_session_name
= poptGetArg(pc
);
171 if (arg_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(arg_session_name
);
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
;