Build fix: missing stdio.h include in signal-helper.hpp
[lttng-tools.git] / src / bin / lttng / commands / clear.cpp
CommitLineData
511ed4e2 1/*
4942c256 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>
050dd639 17#include <lttng/lttng.h>
511ed4e2 18
c9e313bc 19#include "../command.hpp"
511ed4e2 20
c9e313bc
SM
21#include <common/mi-lttng.hpp>
22#include <common/sessiond-comm/sessiond-comm.hpp>
23#include <common/utils.hpp>
511ed4e2
JR
24
25static int opt_clear_all;
26
27#ifdef LTTNG_EMBED_HELP
28static const char help_msg[] =
29#include <lttng-clear.1.h>
30;
31#endif
32
33/* Mi writer */
34static struct mi_writer *writer;
35
36enum {
37 OPT_HELP = 1,
38 OPT_LIST_OPTIONS,
39};
40
41static 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 */
52static 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;
511ed4e2
JR
59 int ret;
60
61 ret = lttng_clear_session(session->name, &handle);
62 if (ret < 0) {
63 ERR("%s", lttng_strerror(ret));
64 goto error;
65 }
66
67 do {
68 status = lttng_clear_handle_wait_for_completion(handle,
69 DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US / USEC_PER_MSEC);
70 switch (status) {
71 case LTTNG_CLEAR_HANDLE_STATUS_TIMEOUT:
72 if (!printed_wait_msg) {
73 _MSG("Waiting for clear of session \"%s\"",
74 session->name);
75 printed_wait_msg = true;
76 }
77 _MSG(".");
78 fflush(stdout);
79 break;
80 case LTTNG_CLEAR_HANDLE_STATUS_COMPLETED:
81 break;
82 default:
83 ERR("Failed to wait for the completion of clear for session \"%s\"",
84 session->name);
85 ret = -1;
86 goto error;
87 }
88 } while (status == LTTNG_CLEAR_HANDLE_STATUS_TIMEOUT);
89
90 status = lttng_clear_handle_get_result(handle, &ret_code);
91 if (status != LTTNG_CLEAR_HANDLE_STATUS_OK) {
92 ERR("Failed to get the result of session clear");
93 ret = -1;
94 goto error;
95 }
96 if (ret_code != LTTNG_OK) {
97 ret = -ret_code;
98 goto error;
99 }
100
101 MSG("%sSession \"%s\" cleared", printed_wait_msg ? "\n" : "",
102 session->name);
103 printed_wait_msg = false;
104
105 if (lttng_opt_mi) {
106 ret = mi_lttng_session(writer, session, 0);
107 if (ret) {
108 ret = CMD_ERROR;
109 goto error;
110 }
111 }
112
113 ret = CMD_SUCCESS;
114error:
115 if (printed_wait_msg) {
116 MSG("");
117 }
118 lttng_clear_handle_destroy(handle);
511ed4e2
JR
119 return ret;
120}
121
122/*
123 * clear all sessions
124 *
125 * Call clear_session for each registered sessions
126 */
127static int clear_all_sessions(struct lttng_session *sessions, int count)
128{
129 int i, ret = CMD_SUCCESS;
130
131 if (count == 0) {
132 MSG("No session found, nothing to do.");
133 } else if (count < 0) {
134 ERR("%s", lttng_strerror(ret));
135 goto error;
136 }
137
138 for (i = 0; i < count; i++) {
139 ret = clear_session(&sessions[i]);
140 if (ret < 0) {
141 goto error;
142 }
143 }
144error:
145 return ret;
146}
147
148/*
149 * The 'clear <options>' first level command
150 */
151int cmd_clear(int argc, const char **argv)
152{
153 int opt;
154 int ret = CMD_SUCCESS , i, command_ret = CMD_SUCCESS, success = 1;
155 static poptContext pc;
156 char *session_name = NULL;
5b915816 157 const char *arg_session_name = NULL;
511ed4e2 158 const char *leftover = NULL;
511ed4e2
JR
159 struct lttng_session *sessions = NULL;
160 int count;
161 int found;
162
163 pc = poptGetContext(NULL, argc, argv, long_options, 0);
164 poptReadDefaultConfig(pc, 0);
165
166 while ((opt = poptGetNextOpt(pc)) != -1) {
167 switch (opt) {
168 case OPT_HELP:
169 SHOW_HELP();
170 break;
171 case OPT_LIST_OPTIONS:
172 list_cmd_options(stdout, long_options);
173 break;
174 default:
175 ret = CMD_UNDEFINED;
176 break;
177 }
178 goto end;
179 }
180
181 /* Mi preparation */
182 if (lttng_opt_mi) {
183 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
184 if (!writer) {
185 ret = -LTTNG_ERR_NOMEM;
186 goto end;
187 }
188
189 /* Open command element */
190 ret = mi_lttng_writer_command_open(writer,
191 mi_lttng_element_command_clear);
192 if (ret) {
193 ret = CMD_ERROR;
194 goto end;
195 }
196
197 /* Open output element */
198 ret = mi_lttng_writer_open_element(writer,
199 mi_lttng_element_command_output);
200 if (ret) {
201 ret = CMD_ERROR;
202 goto end;
203 }
204
205 /* For validation and semantic purpose we open a sessions element */
206 ret = mi_lttng_sessions_open(writer);
207 if (ret) {
208 ret = CMD_ERROR;
209 goto end;
210 }
211 }
212
213 if (!opt_clear_all) {
5b915816
MJ
214 arg_session_name = poptGetArg(pc);
215 if (!arg_session_name) {
511ed4e2
JR
216 /* No session name specified, lookup default */
217 session_name = get_session_name();
5b915816
MJ
218 } else {
219 session_name = strdup(arg_session_name);
511ed4e2 220 if (session_name == NULL) {
5b915816 221 PERROR("Failed to copy session name");
511ed4e2
JR
222 }
223 }
5b915816
MJ
224
225 if (session_name == NULL) {
226 command_ret = CMD_ERROR;
227 success = 0;
228 goto mi_closing;
229 }
511ed4e2
JR
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
278mi_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 }
303end:
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);
5b915816 311 free(session_name);
511ed4e2
JR
312
313 /* Overwrite ret if an error occurred during clear_session/all */
314 ret = command_ret ? command_ret : ret;
315
316 poptFreeContext(pc);
317 return ret;
318}
This page took 0.057659 seconds and 4 git commands to generate.