Commit | Line | Data |
---|---|---|
3087ef18 | 1 | /* |
21cf9b6b | 2 | * Copyright (C) 2011 EfficiOS Inc. |
3087ef18 | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
3087ef18 | 5 | * |
3087ef18 DG |
6 | */ |
7 | ||
6c1c0768 | 8 | #define _LGPL_SOURCE |
28ab034a JG |
9 | #include "../command.hpp" |
10 | ||
11 | #include <common/mi-lttng.hpp> | |
12 | ||
3087ef18 DG |
13 | #include <popt.h> |
14 | #include <stdio.h> | |
15 | #include <stdlib.h> | |
16 | #include <string.h> | |
17 | #include <sys/stat.h> | |
18 | #include <sys/types.h> | |
19 | #include <unistd.h> | |
ce91cd0b | 20 | |
4fc83d94 PP |
21 | #ifdef LTTNG_EMBED_HELP |
22 | static const char help_msg[] = | |
23 | #include <lttng-set-session.1.h> | |
28ab034a | 24 | ; |
4fc83d94 PP |
25 | #endif |
26 | ||
3087ef18 DG |
27 | enum { |
28 | OPT_HELP = 1, | |
679b4943 | 29 | OPT_LIST_OPTIONS, |
3087ef18 DG |
30 | }; |
31 | ||
ce91cd0b JRJ |
32 | static struct mi_writer *writer; |
33 | ||
3087ef18 DG |
34 | static struct poptOption long_options[] = { |
35 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
cd9adb8b JG |
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 } | |
3087ef18 DG |
39 | }; |
40 | ||
ce91cd0b JRJ |
41 | /* |
42 | * Print the necessary mi for a session and name. | |
43 | */ | |
5b915816 | 44 | static int mi_print(const char *session_name) |
ce91cd0b JRJ |
45 | { |
46 | int ret; | |
47 | ||
a0377dfe FD |
48 | LTTNG_ASSERT(writer); |
49 | LTTNG_ASSERT(session_name); | |
ce91cd0b JRJ |
50 | |
51 | /* | |
52 | * Open a sessions element | |
53 | * This is purely for validation purpose | |
54 | */ | |
55 | ret = mi_lttng_sessions_open(writer); | |
56 | if (ret) { | |
57 | goto end; | |
58 | } | |
59 | ||
60 | /* Open a session element */ | |
61 | ret = mi_lttng_writer_open_element(writer, config_element_session); | |
62 | if (ret) { | |
63 | goto end; | |
64 | } | |
65 | ||
66 | /* Session name */ | |
28ab034a | 67 | ret = mi_lttng_writer_write_element_string(writer, config_element_name, session_name); |
ce91cd0b JRJ |
68 | if (ret) { |
69 | goto end; | |
70 | } | |
71 | ||
72 | /* Close session and sessions element */ | |
73 | ret = mi_lttng_close_multi_element(writer, 2); | |
74 | if (ret) { | |
75 | goto end; | |
76 | } | |
77 | end: | |
78 | return ret; | |
79 | } | |
80 | ||
3087ef18 DG |
81 | /* |
82 | * set_session | |
83 | */ | |
5b915816 | 84 | static int set_session(const char *session_name) |
3087ef18 DG |
85 | { |
86 | int ret = CMD_SUCCESS; | |
7f0c090d PPM |
87 | int count, i; |
88 | unsigned int session_found = 0; | |
89 | struct lttng_session *sessions; | |
3087ef18 | 90 | |
5b915816 | 91 | if (session_name && strlen(session_name) > NAME_MAX) { |
28ab034a | 92 | ERR("Session name too long. Length must be lower or equal to %d", NAME_MAX); |
487b253b | 93 | ret = CMD_ERROR; |
7f0c090d PPM |
94 | goto end; |
95 | } | |
96 | ||
97 | count = lttng_list_sessions(&sessions); | |
98 | if (count < 0) { | |
99 | ret = CMD_ERROR; | |
100 | ERR("%s", lttng_strerror(count)); | |
101 | goto end; | |
102 | } | |
103 | ||
104 | for (i = 0; i < count; i++) { | |
5b915816 | 105 | if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) { |
7f0c090d PPM |
106 | session_found = 1; |
107 | break; | |
108 | } | |
109 | } | |
110 | ||
111 | if (!session_found) { | |
5b915816 | 112 | ERR("Session '%s' not found", session_name); |
7f0c090d | 113 | ret = CMD_ERROR; |
487b253b DG |
114 | goto error; |
115 | } | |
116 | ||
5b915816 | 117 | ret = config_init(session_name); |
3087ef18 | 118 | if (ret < 0) { |
fffd0547 | 119 | ERR("Unable to set session name"); |
3087ef18 DG |
120 | ret = CMD_ERROR; |
121 | goto error; | |
122 | } | |
123 | ||
5b915816 | 124 | MSG("Session set to %s", session_name); |
ce91cd0b | 125 | if (lttng_opt_mi) { |
5b915816 | 126 | ret = mi_print(session_name); |
ce91cd0b JRJ |
127 | if (ret) { |
128 | ret = CMD_ERROR; | |
129 | goto error; | |
130 | } | |
131 | } | |
132 | ||
3087ef18 DG |
133 | ret = CMD_SUCCESS; |
134 | ||
135 | error: | |
7f0c090d PPM |
136 | free(sessions); |
137 | end: | |
3087ef18 DG |
138 | return ret; |
139 | } | |
140 | ||
141 | /* | |
142 | * cmd_set_session | |
143 | */ | |
144 | int cmd_set_session(int argc, const char **argv) | |
145 | { | |
ce91cd0b | 146 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
3087ef18 | 147 | static poptContext pc; |
cd9adb8b | 148 | const char *arg_session_name = nullptr; |
3087ef18 | 149 | |
cd9adb8b | 150 | pc = poptGetContext(nullptr, argc, argv, long_options, 0); |
3087ef18 DG |
151 | poptReadDefaultConfig(pc, 0); |
152 | ||
153 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
154 | switch (opt) { | |
155 | case OPT_HELP: | |
4ba92f18 | 156 | SHOW_HELP(); |
3087ef18 | 157 | goto end; |
679b4943 SM |
158 | case OPT_LIST_OPTIONS: |
159 | list_cmd_options(stdout, long_options); | |
679b4943 | 160 | goto end; |
3087ef18 | 161 | default: |
3087ef18 DG |
162 | ret = CMD_UNDEFINED; |
163 | goto end; | |
164 | } | |
165 | } | |
166 | ||
5b915816 | 167 | arg_session_name = poptGetArg(pc); |
cd9adb8b | 168 | if (arg_session_name == nullptr) { |
3087ef18 | 169 | ERR("Missing session name"); |
ca1c3607 | 170 | ret = CMD_ERROR; |
3087ef18 DG |
171 | goto end; |
172 | } | |
173 | ||
ce91cd0b JRJ |
174 | /* Mi check */ |
175 | if (lttng_opt_mi) { | |
176 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); | |
177 | if (!writer) { | |
178 | ret = -LTTNG_ERR_NOMEM; | |
179 | goto end; | |
180 | } | |
181 | ||
182 | /* Open command element */ | |
28ab034a | 183 | ret = mi_lttng_writer_command_open(writer, mi_lttng_element_command_set_session); |
ce91cd0b JRJ |
184 | if (ret) { |
185 | ret = CMD_ERROR; | |
186 | goto end; | |
187 | } | |
188 | ||
189 | /* Open output element */ | |
28ab034a | 190 | ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command_output); |
ce91cd0b JRJ |
191 | if (ret) { |
192 | ret = CMD_ERROR; | |
193 | goto end; | |
194 | } | |
195 | } | |
196 | ||
5b915816 | 197 | command_ret = set_session(arg_session_name); |
ce91cd0b JRJ |
198 | if (command_ret) { |
199 | success = 0; | |
200 | } | |
201 | ||
202 | /* Mi closing */ | |
203 | if (lttng_opt_mi) { | |
204 | /* Close output element */ | |
205 | ret = mi_lttng_writer_close_element(writer); | |
206 | if (ret) { | |
207 | ret = CMD_ERROR; | |
208 | goto end; | |
209 | } | |
210 | ||
211 | /* Success ? */ | |
28ab034a JG |
212 | ret = mi_lttng_writer_write_element_bool( |
213 | writer, mi_lttng_element_command_success, success); | |
ce91cd0b JRJ |
214 | if (ret) { |
215 | ret = CMD_ERROR; | |
216 | goto end; | |
217 | } | |
218 | ||
219 | /* Command element close */ | |
220 | ret = mi_lttng_writer_command_close(writer); | |
221 | if (ret) { | |
222 | ret = CMD_ERROR; | |
223 | goto end; | |
224 | } | |
225 | } | |
3087ef18 DG |
226 | |
227 | end: | |
ce91cd0b JRJ |
228 | /* Mi clean-up */ |
229 | if (writer && mi_lttng_writer_destroy(writer)) { | |
230 | /* Preserve original error code */ | |
231 | ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL; | |
232 | } | |
233 | ||
83f4233d | 234 | /* Overwrite ret if an error occurred during set_session() */ |
ce91cd0b JRJ |
235 | ret = command_ret ? command_ret : ret; |
236 | ||
ca1c3607 | 237 | poptFreeContext(pc); |
3087ef18 DG |
238 | return ret; |
239 | } |