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