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