Move to kernel style SPDX license identifiers
[lttng-tools.git] / src / bin / lttng / commands / clear.c
1 /*
2 * Copyright (C) 2019 Jonathan Rajotte-Julien <jonathan.rajotte-julien@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
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;
160 const char *leftover = NULL;
161 bool free_session_name = false;
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) {
217 session_name = (char *) poptGetArg(pc);
218 if (!session_name) {
219 /* No session name specified, lookup default */
220 session_name = get_session_name();
221 if (session_name == NULL) {
222 command_ret = CMD_ERROR;
223 success = 0;
224 goto mi_closing;
225 }
226 free_session_name = true;
227 }
228 } else {
229 session_name = NULL;
230 }
231
232 leftover = poptGetArg(pc);
233 if (leftover) {
234 ERR("Unknown argument: %s", leftover);
235 ret = CMD_ERROR;
236 success = 0;
237 goto mi_closing;
238 }
239
240 /* Recuperate all sessions for further operation */
241 count = lttng_list_sessions(&sessions);
242 if (count < 0) {
243 ERR("%s", lttng_strerror(count));
244 command_ret = CMD_ERROR;
245 success = 0;
246 goto mi_closing;
247 }
248
249 /* Ignore session name in case all sessions are to be cleaned */
250 if (opt_clear_all) {
251 command_ret = clear_all_sessions(sessions, count);
252 if (command_ret) {
253 ERR("%s", lttng_strerror(command_ret));
254 success = 0;
255 }
256 } else {
257 /* Find the corresponding lttng_session struct */
258 found = 0;
259 for (i = 0; i < count; i++) {
260 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
261 found = 1;
262 command_ret = clear_session(&sessions[i]);
263 if (command_ret) {
264 ERR("%s", lttng_strerror(command_ret));
265 success = 0;
266 }
267 }
268 }
269
270 if (!found) {
271 ERR("Session name %s not found", session_name);
272 command_ret = LTTNG_ERR_SESS_NOT_FOUND;
273 success = 0;
274 goto mi_closing;
275 }
276 }
277
278 mi_closing:
279 /* Mi closing */
280 if (lttng_opt_mi) {
281 /* Close sessions and output element element */
282 ret = mi_lttng_close_multi_element(writer, 2);
283 if (ret) {
284 ret = CMD_ERROR;
285 goto end;
286 }
287
288 /* Success ? */
289 ret = mi_lttng_writer_write_element_bool(writer,
290 mi_lttng_element_command_success, success);
291 if (ret) {
292 ret = CMD_ERROR;
293 goto end;
294 }
295
296 /* Command element close */
297 ret = mi_lttng_writer_command_close(writer);
298 if (ret) {
299 ret = CMD_ERROR;
300 goto end;
301 }
302 }
303 end:
304 /* Mi clean-up */
305 if (writer && mi_lttng_writer_destroy(writer)) {
306 /* Preserve original error code */
307 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
308 }
309
310 free(sessions);
311 if (free_session_name) {
312 free(session_name);
313 }
314
315 /* Overwrite ret if an error occurred during clear_session/all */
316 ret = command_ret ? command_ret : ret;
317
318 poptFreeContext(pc);
319 return ret;
320 }
This page took 0.036859 seconds and 5 git commands to generate.