lttng: Add --glob option to lttng-destroy
[lttng-tools.git] / src / bin / lttng / commands / destroy.cpp
1 /*
2 * Copyright (C) 2011 EfficiOS Inc.
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #define _LGPL_SOURCE
9 #include "../command.hpp"
10
11 #include <common/exception.hpp>
12 #include <common/mi-lttng.hpp>
13 #include <common/sessiond-comm/sessiond-comm.hpp>
14 #include <common/utils.hpp>
15
16 #include <lttng/lttng.h>
17
18 #include <popt.h>
19 #include <stdbool.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
27 static int opt_no_wait;
28
29 #ifdef LTTNG_EMBED_HELP
30 static const char help_msg[] =
31 #include <lttng-destroy.1.h>
32 ;
33 #endif
34
35 /* Mi writer */
36 static struct mi_writer *writer;
37
38 enum {
39 OPT_HELP = 1,
40 OPT_LIST_OPTIONS,
41 OPT_ALL,
42 OPT_ENABLE_GLOB,
43 };
44
45 static struct poptOption long_options[] = {
46 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
47 { "help", 'h', POPT_ARG_NONE, nullptr, OPT_HELP, nullptr, nullptr },
48 { "all", 'a', POPT_ARG_NONE, nullptr, OPT_ALL, nullptr, nullptr },
49 { "glob", 'g', POPT_ARG_NONE, nullptr, OPT_ENABLE_GLOB, nullptr, nullptr },
50 { "list-options", 0, POPT_ARG_NONE, nullptr, OPT_LIST_OPTIONS, nullptr, nullptr },
51 { "no-wait", 'n', POPT_ARG_VAL, &opt_no_wait, 1, nullptr, nullptr },
52 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
53 };
54
55 /*
56 * destroy_session
57 *
58 * Unregister the provided session to the session daemon. On success, removes
59 * the default configuration.
60 */
61 static int destroy_session(const struct lttng_session& session)
62 {
63 int ret;
64 char *session_name = nullptr;
65 bool session_was_already_stopped;
66 enum lttng_error_code ret_code;
67 struct lttng_destruction_handle *handle = nullptr;
68 enum lttng_destruction_handle_status status;
69 bool newline_needed = false, printed_destroy_msg = false;
70 enum lttng_rotation_state rotation_state;
71 char *stats_str = nullptr;
72
73 ret = lttng_stop_tracing_no_wait(session.name);
74 if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
75 ERR("%s", lttng_strerror(ret));
76 }
77
78 session_was_already_stopped = ret == -LTTNG_ERR_TRACE_ALREADY_STOPPED;
79 if (!opt_no_wait) {
80 do {
81 ret = lttng_data_pending(session.name);
82 if (ret < 0) {
83 /* Return the data available call error. */
84 goto error;
85 }
86
87 /*
88 * Data sleep time before retrying (in usec). Don't
89 * sleep if the call returned value indicates
90 * availability.
91 */
92 if (ret) {
93 if (!printed_destroy_msg) {
94 _MSG("Destroying session %s", session.name);
95 newline_needed = true;
96 printed_destroy_msg = true;
97 fflush(stdout);
98 }
99
100 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US);
101 _MSG(".");
102 fflush(stdout);
103 }
104 } while (ret != 0);
105 }
106
107 if (!session_was_already_stopped) {
108 /*
109 * Don't print the event and packet loss warnings since the user
110 * already saw them when stopping the trace.
111 */
112 ret = get_session_stats_str(session.name, &stats_str);
113 if (ret < 0) {
114 goto error;
115 }
116 }
117
118 ret_code = lttng_destroy_session_ext(session.name, &handle);
119 if (ret_code != LTTNG_OK) {
120 ret = -ret_code;
121 goto error;
122 }
123
124 if (opt_no_wait) {
125 goto skip_wait_rotation;
126 }
127
128 do {
129 status = lttng_destruction_handle_wait_for_completion(
130 handle, DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US / USEC_PER_MSEC);
131 switch (status) {
132 case LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT:
133 if (!printed_destroy_msg) {
134 _MSG("Destroying session %s", session.name);
135 newline_needed = true;
136 printed_destroy_msg = true;
137 }
138 _MSG(".");
139 fflush(stdout);
140 break;
141 case LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED:
142 break;
143 default:
144 ERR("%sFailed to wait for the completion of the destruction of session \"%s\"",
145 newline_needed ? "\n" : "",
146 session.name);
147 newline_needed = false;
148 ret = -1;
149 goto error;
150 }
151 } while (status == LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT);
152
153 status = lttng_destruction_handle_get_result(handle, &ret_code);
154 if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_OK) {
155 ERR("%sFailed to get the result of session destruction",
156 newline_needed ? "\n" : "");
157 ret = -1;
158 newline_needed = false;
159 goto error;
160 }
161 if (ret_code != LTTNG_OK) {
162 ret = -ret_code;
163 goto error;
164 }
165
166 status = lttng_destruction_handle_get_rotation_state(handle, &rotation_state);
167 if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_OK) {
168 ERR("%sFailed to get rotation state from destruction handle",
169 newline_needed ? "\n" : "");
170 newline_needed = false;
171 goto skip_wait_rotation;
172 }
173
174 switch (rotation_state) {
175 case LTTNG_ROTATION_STATE_NO_ROTATION:
176 break;
177 case LTTNG_ROTATION_STATE_COMPLETED:
178 {
179 const struct lttng_trace_archive_location *location;
180
181 status = lttng_destruction_handle_get_archive_location(handle, &location);
182 if (status == LTTNG_DESTRUCTION_HANDLE_STATUS_OK) {
183 ret = print_trace_archive_location(location, session.name);
184 if (ret) {
185 ERR("%sFailed to print the location of trace archive",
186 newline_needed ? "\n" : "");
187 newline_needed = false;
188 goto skip_wait_rotation;
189 }
190 break;
191 }
192 }
193 /* fall-through. */
194 default:
195 ERR("%sFailed to get the location of the rotation performed during the session's destruction",
196 newline_needed ? "\n" : "");
197 newline_needed = false;
198 goto skip_wait_rotation;
199 }
200 skip_wait_rotation:
201 MSG("%sSession %s destroyed", newline_needed ? "\n" : "", session.name);
202 newline_needed = false;
203 if (stats_str) {
204 MSG("%s", stats_str);
205 }
206
207 session_name = get_session_name_quiet();
208 if (session_name && !strncmp(session.name, session_name, NAME_MAX)) {
209 config_destroy_default();
210 }
211
212 if (lttng_opt_mi) {
213 ret = mi_lttng_session(writer, &session, 0);
214 if (ret) {
215 ret = CMD_ERROR;
216 goto error;
217 }
218 }
219
220 ret = CMD_SUCCESS;
221 error:
222 if (newline_needed) {
223 MSG("");
224 }
225 lttng_destruction_handle_destroy(handle);
226 free(session_name);
227 free(stats_str);
228 return ret;
229 }
230
231 static int destroy_sessions(const struct session_spec& spec)
232 {
233 try {
234 auto sessions = list_sessions(spec);
235 int ret = CMD_SUCCESS;
236
237 if (sessions.size() == 0) {
238 switch (spec.type) {
239 case session_spec::ALL: /* fall throught */
240 case session_spec::GLOB_PATTERN:
241 MSG("No session found, nothing to do.");
242 break;
243 case session_spec::NAME:
244 ERR("Session name %s not found", spec.value);
245 ret = LTTNG_ERR_SESS_NOT_FOUND;
246 break;
247 }
248
249 return ret;
250 }
251
252 for (const auto& session : sessions) {
253 int const sub_ret = destroy_session(session);
254
255 if (sub_ret != CMD_SUCCESS) {
256 ERR("%s during the destruction of session \"%s\"",
257 lttng_strerror(sub_ret),
258 session.name);
259 ret = CMD_ERROR;
260 }
261 }
262
263 return ret;
264
265 } catch (lttng::ctl::error& error) {
266 ERR("%s", lttng_strerror(error.code()));
267 }
268
269 return CMD_ERROR;
270 }
271
272 /*
273 * The 'destroy <options>' first level command
274 */
275 int cmd_destroy(int argc, const char **argv)
276 {
277 int opt;
278 int ret = CMD_SUCCESS, command_ret = CMD_SUCCESS;
279 bool success;
280 static poptContext pc;
281 const char *leftover = nullptr;
282 struct session_spec spec = {
283 .type = session_spec::NAME,
284 .value = nullptr,
285 };
286 session_list const sessions;
287
288 pc = poptGetContext(nullptr, argc, argv, long_options, 0);
289 poptReadDefaultConfig(pc, 0);
290
291 while ((opt = poptGetNextOpt(pc)) != -1) {
292 switch (opt) {
293 case OPT_HELP:
294 SHOW_HELP();
295 goto end;
296 case OPT_LIST_OPTIONS:
297 list_cmd_options(stdout, long_options);
298 goto end;
299 case OPT_ALL:
300 spec.type = session_spec::ALL;
301 break;
302 case OPT_ENABLE_GLOB:
303 spec.type = session_spec::GLOB_PATTERN;
304 break;
305 default:
306 ret = CMD_UNDEFINED;
307 goto end;
308 }
309 }
310
311 /* Mi preparation */
312 if (lttng_opt_mi) {
313 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
314 if (!writer) {
315 ret = -LTTNG_ERR_NOMEM;
316 goto end;
317 }
318
319 /* Open command element */
320 ret = mi_lttng_writer_command_open(writer, mi_lttng_element_command_destroy);
321 if (ret) {
322 ret = CMD_ERROR;
323 goto end;
324 }
325
326 /* Open output element */
327 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command_output);
328 if (ret) {
329 ret = CMD_ERROR;
330 goto end;
331 }
332
333 /* For validation and semantic purpose we open a sessions element */
334 ret = mi_lttng_sessions_open(writer);
335 if (ret) {
336 ret = CMD_ERROR;
337 goto end;
338 }
339 }
340
341 spec.value = poptGetArg(pc);
342
343 command_ret = destroy_sessions(spec);
344
345 success = command_ret == CMD_SUCCESS;
346
347 leftover = poptGetArg(pc);
348 if (leftover) {
349 ERR("Unknown argument: %s", leftover);
350 ret = CMD_ERROR;
351 success = false;
352 }
353
354 /* Mi closing */
355 if (lttng_opt_mi) {
356 /* Close sessions and output element element */
357 ret = mi_lttng_close_multi_element(writer, 2);
358 if (ret) {
359 ret = CMD_ERROR;
360 goto end;
361 }
362
363 /* Success ? */
364 ret = mi_lttng_writer_write_element_bool(
365 writer, mi_lttng_element_command_success, success);
366 if (ret) {
367 ret = CMD_ERROR;
368 goto end;
369 }
370
371 /* Command element close */
372 ret = mi_lttng_writer_command_close(writer);
373 if (ret) {
374 ret = CMD_ERROR;
375 goto end;
376 }
377 }
378 end:
379 /* Mi clean-up */
380 if (writer && mi_lttng_writer_destroy(writer)) {
381 /* Preserve original error code */
382 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
383 }
384
385 /* Overwrite ret if an error occurred during destroy_session/all */
386 ret = command_ret ? command_ret : ret;
387
388 poptFreeContext(pc);
389 return ret;
390 }
This page took 0.036355 seconds and 4 git commands to generate.