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