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