Commit | Line | Data |
---|---|---|
511ed4e2 | 1 | /* |
9e3c8d58 | 2 | * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com> |
511ed4e2 | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
511ed4e2 | 5 | * |
511ed4e2 JR |
6 | */ |
7 | ||
8 | #define _LGPL_SOURCE | |
9 | #include <popt.h> | |
10 | #include <stdio.h> | |
11 | #include <stdlib.h> | |
12 | #include <string.h> | |
13 | #include <sys/stat.h> | |
14 | #include <sys/types.h> | |
15 | #include <unistd.h> | |
16 | #include <stdbool.h> | |
17 | #include <lttng/clear.h> | |
18 | #include <lttng/clear-handle.h> | |
19 | ||
20 | #include "../command.h" | |
21 | ||
22 | #include <common/mi-lttng.h> | |
23 | #include <common/sessiond-comm/sessiond-comm.h> | |
24 | #include <common/utils.h> | |
25 | ||
26 | static int opt_clear_all; | |
27 | ||
28 | #ifdef LTTNG_EMBED_HELP | |
29 | static const char help_msg[] = | |
30 | #include <lttng-clear.1.h> | |
31 | ; | |
32 | #endif | |
33 | ||
34 | /* Mi writer */ | |
35 | static struct mi_writer *writer; | |
36 | ||
37 | enum { | |
38 | OPT_HELP = 1, | |
39 | OPT_LIST_OPTIONS, | |
40 | }; | |
41 | ||
42 | static struct poptOption long_options[] = { | |
43 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
44 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, | |
45 | {"all", 'a', POPT_ARG_VAL, &opt_clear_all, 1, 0, 0}, | |
46 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, | |
47 | {0, 0, 0, 0, 0, 0, 0} | |
48 | }; | |
49 | ||
50 | /* | |
51 | * clear session | |
52 | */ | |
53 | static int clear_session(struct lttng_session *session) | |
54 | { | |
55 | enum lttng_clear_handle_status status = | |
56 | LTTNG_CLEAR_HANDLE_STATUS_OK; | |
57 | struct lttng_clear_handle *handle = NULL; | |
58 | enum lttng_error_code ret_code; | |
59 | bool printed_wait_msg = false; | |
60 | char *session_name = NULL; | |
61 | int ret; | |
62 | ||
63 | ret = lttng_clear_session(session->name, &handle); | |
64 | if (ret < 0) { | |
65 | ERR("%s", lttng_strerror(ret)); | |
66 | goto error; | |
67 | } | |
68 | ||
69 | do { | |
70 | status = lttng_clear_handle_wait_for_completion(handle, | |
71 | DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US / USEC_PER_MSEC); | |
72 | switch (status) { | |
73 | case LTTNG_CLEAR_HANDLE_STATUS_TIMEOUT: | |
74 | if (!printed_wait_msg) { | |
75 | _MSG("Waiting for clear of session \"%s\"", | |
76 | session->name); | |
77 | printed_wait_msg = true; | |
78 | } | |
79 | _MSG("."); | |
80 | fflush(stdout); | |
81 | break; | |
82 | case LTTNG_CLEAR_HANDLE_STATUS_COMPLETED: | |
83 | break; | |
84 | default: | |
85 | ERR("Failed to wait for the completion of clear for session \"%s\"", | |
86 | session->name); | |
87 | ret = -1; | |
88 | goto error; | |
89 | } | |
90 | } while (status == LTTNG_CLEAR_HANDLE_STATUS_TIMEOUT); | |
91 | ||
92 | status = lttng_clear_handle_get_result(handle, &ret_code); | |
93 | if (status != LTTNG_CLEAR_HANDLE_STATUS_OK) { | |
94 | ERR("Failed to get the result of session clear"); | |
95 | ret = -1; | |
96 | goto error; | |
97 | } | |
98 | if (ret_code != LTTNG_OK) { | |
99 | ret = -ret_code; | |
100 | goto error; | |
101 | } | |
102 | ||
103 | MSG("%sSession \"%s\" cleared", printed_wait_msg ? "\n" : "", | |
104 | session->name); | |
105 | printed_wait_msg = false; | |
106 | ||
107 | if (lttng_opt_mi) { | |
108 | ret = mi_lttng_session(writer, session, 0); | |
109 | if (ret) { | |
110 | ret = CMD_ERROR; | |
111 | goto error; | |
112 | } | |
113 | } | |
114 | ||
115 | ret = CMD_SUCCESS; | |
116 | error: | |
117 | if (printed_wait_msg) { | |
118 | MSG(""); | |
119 | } | |
120 | lttng_clear_handle_destroy(handle); | |
121 | free(session_name); | |
122 | return ret; | |
123 | } | |
124 | ||
125 | /* | |
126 | * clear all sessions | |
127 | * | |
128 | * Call clear_session for each registered sessions | |
129 | */ | |
130 | static int clear_all_sessions(struct lttng_session *sessions, int count) | |
131 | { | |
132 | int i, ret = CMD_SUCCESS; | |
133 | ||
134 | if (count == 0) { | |
135 | MSG("No session found, nothing to do."); | |
136 | } else if (count < 0) { | |
137 | ERR("%s", lttng_strerror(ret)); | |
138 | goto error; | |
139 | } | |
140 | ||
141 | for (i = 0; i < count; i++) { | |
142 | ret = clear_session(&sessions[i]); | |
143 | if (ret < 0) { | |
144 | goto error; | |
145 | } | |
146 | } | |
147 | error: | |
148 | return ret; | |
149 | } | |
150 | ||
151 | /* | |
152 | * The 'clear <options>' first level command | |
153 | */ | |
154 | int cmd_clear(int argc, const char **argv) | |
155 | { | |
156 | int opt; | |
157 | int ret = CMD_SUCCESS , i, command_ret = CMD_SUCCESS, success = 1; | |
158 | static poptContext pc; | |
159 | char *session_name = NULL; | |
d569ed5f | 160 | const char *arg_session_name = NULL; |
511ed4e2 | 161 | const char *leftover = NULL; |
511ed4e2 JR |
162 | struct lttng_session *sessions = NULL; |
163 | int count; | |
164 | int found; | |
165 | ||
166 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
167 | poptReadDefaultConfig(pc, 0); | |
168 | ||
169 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
170 | switch (opt) { | |
171 | case OPT_HELP: | |
172 | SHOW_HELP(); | |
173 | break; | |
174 | case OPT_LIST_OPTIONS: | |
175 | list_cmd_options(stdout, long_options); | |
176 | break; | |
177 | default: | |
178 | ret = CMD_UNDEFINED; | |
179 | break; | |
180 | } | |
181 | goto end; | |
182 | } | |
183 | ||
184 | /* Mi preparation */ | |
185 | if (lttng_opt_mi) { | |
186 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); | |
187 | if (!writer) { | |
188 | ret = -LTTNG_ERR_NOMEM; | |
189 | goto end; | |
190 | } | |
191 | ||
192 | /* Open command element */ | |
193 | ret = mi_lttng_writer_command_open(writer, | |
194 | mi_lttng_element_command_clear); | |
195 | if (ret) { | |
196 | ret = CMD_ERROR; | |
197 | goto end; | |
198 | } | |
199 | ||
200 | /* Open output element */ | |
201 | ret = mi_lttng_writer_open_element(writer, | |
202 | mi_lttng_element_command_output); | |
203 | if (ret) { | |
204 | ret = CMD_ERROR; | |
205 | goto end; | |
206 | } | |
207 | ||
208 | /* For validation and semantic purpose we open a sessions element */ | |
209 | ret = mi_lttng_sessions_open(writer); | |
210 | if (ret) { | |
211 | ret = CMD_ERROR; | |
212 | goto end; | |
213 | } | |
214 | } | |
215 | ||
216 | if (!opt_clear_all) { | |
d569ed5f MJ |
217 | arg_session_name = poptGetArg(pc); |
218 | if (!arg_session_name) { | |
511ed4e2 JR |
219 | /* No session name specified, lookup default */ |
220 | session_name = get_session_name(); | |
d569ed5f MJ |
221 | } else { |
222 | session_name = strdup(arg_session_name); | |
511ed4e2 | 223 | if (session_name == NULL) { |
d569ed5f | 224 | PERROR("Failed to copy session name"); |
511ed4e2 JR |
225 | } |
226 | } | |
d569ed5f MJ |
227 | |
228 | if (session_name == NULL) { | |
229 | command_ret = CMD_ERROR; | |
230 | success = 0; | |
231 | goto mi_closing; | |
232 | } | |
511ed4e2 JR |
233 | } |
234 | ||
235 | leftover = poptGetArg(pc); | |
236 | if (leftover) { | |
237 | ERR("Unknown argument: %s", leftover); | |
238 | ret = CMD_ERROR; | |
239 | success = 0; | |
240 | goto mi_closing; | |
241 | } | |
242 | ||
243 | /* Recuperate all sessions for further operation */ | |
244 | count = lttng_list_sessions(&sessions); | |
245 | if (count < 0) { | |
246 | ERR("%s", lttng_strerror(count)); | |
247 | command_ret = CMD_ERROR; | |
248 | success = 0; | |
249 | goto mi_closing; | |
250 | } | |
251 | ||
252 | /* Ignore session name in case all sessions are to be cleaned */ | |
253 | if (opt_clear_all) { | |
254 | command_ret = clear_all_sessions(sessions, count); | |
255 | if (command_ret) { | |
256 | ERR("%s", lttng_strerror(command_ret)); | |
257 | success = 0; | |
258 | } | |
259 | } else { | |
260 | /* Find the corresponding lttng_session struct */ | |
261 | found = 0; | |
262 | for (i = 0; i < count; i++) { | |
263 | if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) { | |
264 | found = 1; | |
265 | command_ret = clear_session(&sessions[i]); | |
266 | if (command_ret) { | |
267 | ERR("%s", lttng_strerror(command_ret)); | |
268 | success = 0; | |
269 | } | |
270 | } | |
271 | } | |
272 | ||
273 | if (!found) { | |
274 | ERR("Session name %s not found", session_name); | |
275 | command_ret = LTTNG_ERR_SESS_NOT_FOUND; | |
276 | success = 0; | |
277 | goto mi_closing; | |
278 | } | |
279 | } | |
280 | ||
281 | mi_closing: | |
282 | /* Mi closing */ | |
283 | if (lttng_opt_mi) { | |
284 | /* Close sessions and output element element */ | |
285 | ret = mi_lttng_close_multi_element(writer, 2); | |
286 | if (ret) { | |
287 | ret = CMD_ERROR; | |
288 | goto end; | |
289 | } | |
290 | ||
291 | /* Success ? */ | |
292 | ret = mi_lttng_writer_write_element_bool(writer, | |
293 | mi_lttng_element_command_success, success); | |
294 | if (ret) { | |
295 | ret = CMD_ERROR; | |
296 | goto end; | |
297 | } | |
298 | ||
299 | /* Command element close */ | |
300 | ret = mi_lttng_writer_command_close(writer); | |
301 | if (ret) { | |
302 | ret = CMD_ERROR; | |
303 | goto end; | |
304 | } | |
305 | } | |
306 | end: | |
307 | /* Mi clean-up */ | |
308 | if (writer && mi_lttng_writer_destroy(writer)) { | |
309 | /* Preserve original error code */ | |
310 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; | |
311 | } | |
312 | ||
313 | free(sessions); | |
d569ed5f | 314 | free(session_name); |
511ed4e2 JR |
315 | |
316 | /* Overwrite ret if an error occurred during clear_session/all */ | |
317 | ret = command_ret ? command_ret : ret; | |
318 | ||
319 | poptFreeContext(pc); | |
320 | return ret; | |
321 | } |