fix: relayd: unaligned access in trace_chunk_registry_ht_key_hash
[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 #include "../exception.hpp"
11
12 #include <common/exception.hpp>
13 #include <common/make-unique-wrapper.hpp>
14 #include <common/mi-lttng.hpp>
15 #include <common/scope-exit.hpp>
16 #include <common/sessiond-comm/sessiond-comm.hpp>
17 #include <common/utils.hpp>
18
19 #include <lttng/lttng.h>
20
21 #include <popt.h>
22 #include <stdbool.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29
30 enum {
31 OPT_HELP = 1,
32 OPT_LIST_OPTIONS,
33 OPT_ALL,
34 OPT_ENABLE_GLOB,
35 };
36
37 namespace {
38 #ifdef LTTNG_EMBED_HELP
39 const char help_msg[] =
40 #include <lttng-destroy.1.h>
41 ;
42 #endif
43
44 int opt_no_wait;
45
46 /* Mi writer */
47 struct mi_writer *writer;
48
49 struct poptOption long_options[] = {
50 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
51 { "help", 'h', POPT_ARG_NONE, nullptr, OPT_HELP, nullptr, nullptr },
52 { "all", 'a', POPT_ARG_NONE, nullptr, OPT_ALL, nullptr, nullptr },
53 { "glob", 'g', POPT_ARG_NONE, nullptr, OPT_ENABLE_GLOB, nullptr, nullptr },
54 { "list-options", 0, POPT_ARG_NONE, nullptr, OPT_LIST_OPTIONS, nullptr, nullptr },
55 { "no-wait", 'n', POPT_ARG_VAL, &opt_no_wait, 1, nullptr, nullptr },
56 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
57 };
58
59 /*
60 * destroy_session
61 *
62 * Unregister the provided session to the session daemon. On success, removes
63 * the default configuration.
64 */
65 cmd_error_code destroy_session(const lttng_session& session)
66 {
67 int ret;
68 bool newline_needed = false, printed_destroy_msg = false;
69
70 const auto print_trailing_new_line = lttng::make_scope_exit([&newline_needed]() noexcept {
71 if (newline_needed) {
72 MSG("");
73 }
74 });
75
76 ret = lttng_stop_tracing_no_wait(session.name);
77 if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
78 LTTNG_THROW_CTL(lttng::format("Failed to stop session `{}`", session.name),
79 static_cast<lttng_error_code>(-ret));
80 }
81
82 const auto session_was_already_stopped = ret == -LTTNG_ERR_TRACE_ALREADY_STOPPED;
83 if (!opt_no_wait) {
84 do {
85 ret = lttng_data_pending(session.name);
86 if (ret < 0) {
87 /* Return the data available call error. */
88 ERR_FMT("Failed to check pending data for session `{}` ({})",
89 session.name,
90 lttng_strerror(ret));
91 return CMD_ERROR;
92 }
93
94 /*
95 * Data sleep time before retrying (in usec). Don't
96 * sleep if the call returned value indicates
97 * availability.
98 */
99 if (ret) {
100 if (!printed_destroy_msg) {
101 _MSG("Destroying session `%s`", session.name);
102 newline_needed = true;
103 printed_destroy_msg = true;
104 fflush(stdout);
105 }
106
107 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US);
108 _MSG(".");
109 fflush(stdout);
110 }
111 } while (ret != 0);
112 }
113
114 std::unique_ptr<char,
115 lttng::memory::create_deleter_class<char, lttng::memory::free>::deleter>
116 stats_str;
117 if (!session_was_already_stopped) {
118 char *raw_stats_str = nullptr;
119
120 /*
121 * Don't print the event and packet loss warnings since the user
122 * already saw them when stopping the trace.
123 */
124 ret = get_session_stats_str(session.name, &raw_stats_str);
125 if (ret < 0) {
126 return CMD_ERROR;
127 }
128
129 /* May still be null if there are no stats to print. */
130 stats_str.reset(raw_stats_str);
131 }
132
133 const auto destruction_handle = [&session]() {
134 struct lttng_destruction_handle *raw_destruction_handle = nullptr;
135
136 auto ctl_ret_code =
137 lttng_destroy_session_ext(session.name, &raw_destruction_handle);
138 if (ctl_ret_code != LTTNG_OK) {
139 LTTNG_THROW_CTL(lttng::format("Failed to destroy session `{}`",
140 session.name),
141 ctl_ret_code);
142 }
143
144 return lttng::make_unique_wrapper<lttng_destruction_handle,
145 lttng_destruction_handle_destroy>(
146 raw_destruction_handle);
147 }();
148
149 if (!opt_no_wait) {
150 enum lttng_destruction_handle_status status;
151
152 do {
153 status = lttng_destruction_handle_wait_for_completion(
154 destruction_handle.get(),
155 DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US / USEC_PER_MSEC);
156 switch (status) {
157 case LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT:
158 if (!printed_destroy_msg) {
159 _MSG("Destroying session `%s`", session.name);
160 newline_needed = true;
161 printed_destroy_msg = true;
162 }
163 _MSG(".");
164 fflush(stdout);
165 break;
166 case LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED:
167 break;
168 default:
169 ERR_FMT("{}An error occurred during the destruction of session `{}`",
170 newline_needed ? "\n" : "",
171 session.name);
172 newline_needed = false;
173 return CMD_ERROR;
174 }
175 } while (status == LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT);
176
177 enum lttng_error_code ctl_ret_code;
178 status = lttng_destruction_handle_get_result(destruction_handle.get(),
179 &ctl_ret_code);
180 if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_OK) {
181 ERR_FMT("{}Failed to query the result of the destruction of session `{}`",
182 newline_needed ? "\n" : "",
183 session.name);
184
185 newline_needed = false;
186 return CMD_ERROR;
187 }
188
189 if (ctl_ret_code != LTTNG_OK) {
190 LTTNG_THROW_CTL(lttng::format("Failed to destroy session `{}`",
191 session.name),
192 ctl_ret_code);
193 }
194
195 enum lttng_rotation_state rotation_state;
196 status = lttng_destruction_handle_get_rotation_state(destruction_handle.get(),
197 &rotation_state);
198 if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_OK) {
199 ERR_FMT("{}Failed to query the rotation state from the destruction handle of session `{}`",
200 newline_needed ? "\n" : "",
201 session.name);
202 newline_needed = false;
203 } else {
204 switch (rotation_state) {
205 case LTTNG_ROTATION_STATE_NO_ROTATION:
206 break;
207 case LTTNG_ROTATION_STATE_COMPLETED:
208 {
209 const struct lttng_trace_archive_location *location;
210
211 status = lttng_destruction_handle_get_archive_location(
212 destruction_handle.get(), &location);
213 if (status == LTTNG_DESTRUCTION_HANDLE_STATUS_OK) {
214 ret = print_trace_archive_location(location, session.name);
215 if (ret) {
216 ERR_FMT("{}Failed to print the location of the latest trace archive of session `{}`",
217 newline_needed ? "\n" : "",
218 session.name);
219 newline_needed = false;
220 }
221
222 break;
223 }
224 }
225 /* fall-through. */
226 default:
227 ERR_FMT("{}Failed to get the location of the rotation performed during the destruction of `{}`",
228 newline_needed ? "\n" : "",
229 session.name);
230 newline_needed = false;
231 break;
232 }
233 }
234 }
235
236 MSG("%sSession `%s` destroyed", newline_needed ? "\n" : "", session.name);
237 newline_needed = false;
238 if (stats_str) {
239 MSG("%s", stats_str.get());
240 }
241
242 /*
243 * If the session being destroy is the "default" session as defined in the .lttngrc file,
244 * destroy the file.
245 */
246 const auto session_name =
247 lttng::make_unique_wrapper<char, lttng::memory::free>(get_session_name_quiet());
248 if (session_name && !strncmp(session.name, session_name.get(), NAME_MAX)) {
249 config_destroy_default();
250 }
251
252 if (lttng_opt_mi) {
253 ret = mi_lttng_session(writer, &session, 0);
254 if (ret) {
255 return CMD_ERROR;
256 }
257 }
258
259 return CMD_SUCCESS;
260 }
261
262 cmd_error_code destroy_sessions(const lttng::cli::session_spec& spec)
263 {
264 bool had_warning = false;
265 bool had_error = false;
266 bool listing_failed = false;
267
268 const auto sessions = [&listing_failed, &spec]() -> lttng::cli::session_list {
269 try {
270 return list_sessions(spec);
271 } catch (const lttng::ctl::error& ctl_exception) {
272 ERR_FMT("Failed to list sessions ({})",
273 lttng_strerror(-ctl_exception.code()));
274 listing_failed = true;
275 return {};
276 } catch (const lttng::cli::no_default_session_error& cli_exception) {
277 /*
278 * The retrieval of the default session name already logs
279 * an error when it fails. There is no value in printing
280 * anything about this exception.
281 */
282 listing_failed = true;
283 return {};
284 }
285 }();
286
287 if (!listing_failed && sessions.size() == 0 &&
288 spec.type_ == lttng::cli::session_spec::type::NAME) {
289 ERR_FMT("Session `{}` not found", spec.value);
290 return CMD_ERROR;
291 }
292
293 if (listing_failed) {
294 return CMD_FATAL;
295 }
296
297 for (const auto& session : sessions) {
298 cmd_error_code sub_ret;
299
300 try {
301 sub_ret = destroy_session(session);
302 } catch (const lttng::ctl::error& ctl_exception) {
303 switch (ctl_exception.code()) {
304 case LTTNG_ERR_NO_SESSION:
305 if (spec.type_ != lttng::cli::session_spec::type::NAME) {
306 /* Session destroyed during command, ignore and carry-on. */
307 sub_ret = CMD_SUCCESS;
308 break;
309 } else {
310 sub_ret = CMD_ERROR;
311 break;
312 }
313 case LTTNG_ERR_NO_SESSIOND:
314 /* Don't keep going on a fatal error. */
315 return CMD_FATAL;
316 default:
317 /* Generic error. */
318 sub_ret = CMD_ERROR;
319 ERR_FMT("Failed to destroy session `{}` ({})",
320 session.name,
321 lttng_strerror(-ctl_exception.code()));
322 break;
323 }
324 }
325
326 /* Keep going, but report the most serious state. */
327 had_warning |= sub_ret == CMD_WARNING;
328 had_error |= sub_ret == CMD_ERROR;
329 }
330
331 if (had_error) {
332 return CMD_ERROR;
333 } else if (had_warning) {
334 return CMD_WARNING;
335 } else {
336 return CMD_SUCCESS;
337 }
338 }
339 } /* namespace */
340
341 /*
342 * The 'destroy <options>' first level command
343 */
344 int cmd_destroy(int argc, const char **argv)
345 {
346 int opt;
347 cmd_error_code command_ret = CMD_SUCCESS;
348 bool success;
349 static poptContext pc;
350 const char *leftover = nullptr;
351 lttng::cli::session_spec spec(lttng::cli::session_spec::type::NAME);
352 lttng::cli::session_list const sessions;
353
354 pc = poptGetContext(nullptr, argc, argv, long_options, 0);
355 poptReadDefaultConfig(pc, 0);
356
357 while ((opt = poptGetNextOpt(pc)) != -1) {
358 switch (opt) {
359 case OPT_HELP:
360 {
361 int ret;
362
363 SHOW_HELP();
364 command_ret = static_cast<cmd_error_code>(ret);
365 goto end;
366 }
367 case OPT_LIST_OPTIONS:
368 list_cmd_options(stdout, long_options);
369 goto end;
370 case OPT_ALL:
371 spec.type_ = lttng::cli::session_spec::type::ALL;
372 break;
373 case OPT_ENABLE_GLOB:
374 spec.type_ = lttng::cli::session_spec::type::GLOB_PATTERN;
375 break;
376 default:
377 command_ret = CMD_UNDEFINED;
378 goto end;
379 }
380 }
381
382 /* Mi preparation */
383 if (lttng_opt_mi) {
384 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
385 if (!writer) {
386 command_ret = CMD_ERROR;
387 goto end;
388 }
389
390 /* Open command element */
391 if (mi_lttng_writer_command_open(writer, mi_lttng_element_command_destroy)) {
392 command_ret = CMD_ERROR;
393 goto end;
394 }
395
396 /* Open output element */
397 if (mi_lttng_writer_open_element(writer, mi_lttng_element_command_output)) {
398 command_ret = CMD_ERROR;
399 goto end;
400 }
401
402 /* For validation and semantic purpose we open a sessions element */
403 if (mi_lttng_sessions_open(writer)) {
404 command_ret = CMD_ERROR;
405 goto end;
406 }
407 }
408
409 spec.value = poptGetArg(pc);
410
411 command_ret = destroy_sessions(spec);
412
413 success = command_ret == CMD_SUCCESS;
414
415 leftover = poptGetArg(pc);
416 if (leftover) {
417 ERR("Unknown argument: %s", leftover);
418 command_ret = CMD_ERROR;
419 success = false;
420 }
421
422 /* Mi closing */
423 if (lttng_opt_mi) {
424 /* Close sessions and output element element */
425 if (mi_lttng_close_multi_element(writer, 2)) {
426 command_ret = CMD_ERROR;
427 goto end;
428 }
429
430 /* Success ? */
431 if (mi_lttng_writer_write_element_bool(
432 writer, mi_lttng_element_command_success, success)) {
433 command_ret = CMD_ERROR;
434 goto end;
435 }
436
437 /* Command element close */
438 if (mi_lttng_writer_command_close(writer)) {
439 command_ret = CMD_ERROR;
440 goto end;
441 }
442 }
443 end:
444 /* Mi clean-up */
445 if (writer && mi_lttng_writer_destroy(writer)) {
446 command_ret = CMD_ERROR;
447 }
448
449 poptFreeContext(pc);
450 return command_ret;
451 }
This page took 0.037343 seconds and 4 git commands to generate.