Fix: lttng: track: leaked command parameter
[lttng-tools.git] / src / bin / lttng / commands / destroy.cpp
... / ...
CommitLineData
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
25static char *opt_session_name;
26static int opt_destroy_all;
27static int opt_no_wait;
28
29#ifdef LTTNG_EMBED_HELP
30static const char help_msg[] =
31#include <lttng-destroy.1.h>
32;
33#endif
34
35/* Mi writer */
36static struct mi_writer *writer;
37
38enum {
39 OPT_HELP = 1,
40 OPT_LIST_OPTIONS,
41};
42
43static struct poptOption long_options[] = {
44 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
45 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
46 {"all", 'a', POPT_ARG_VAL, &opt_destroy_all, 1, 0, 0},
47 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
48 {"no-wait", 'n', POPT_ARG_VAL, &opt_no_wait, 1, 0, 0},
49 {0, 0, 0, 0, 0, 0, 0}
50};
51
52/*
53 * destroy_session
54 *
55 * Unregister the provided session to the session daemon. On success, removes
56 * the default configuration.
57 */
58static int destroy_session(struct lttng_session *session)
59{
60 int ret;
61 char *session_name = NULL;
62 bool session_was_already_stopped;
63 enum lttng_error_code ret_code;
64 struct lttng_destruction_handle *handle = NULL;
65 enum lttng_destruction_handle_status status;
66 bool newline_needed = false, printed_destroy_msg = false;
67 enum lttng_rotation_state rotation_state;
68 char *stats_str = NULL;
69
70 ret = lttng_stop_tracing_no_wait(session->name);
71 if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
72 ERR("%s", lttng_strerror(ret));
73 }
74
75 session_was_already_stopped = ret == -LTTNG_ERR_TRACE_ALREADY_STOPPED;
76 if (!opt_no_wait) {
77 do {
78 ret = lttng_data_pending(session->name);
79 if (ret < 0) {
80 /* Return the data available call error. */
81 goto error;
82 }
83
84 /*
85 * Data sleep time before retrying (in usec). Don't
86 * sleep if the call returned value indicates
87 * availability.
88 */
89 if (ret) {
90 if (!printed_destroy_msg) {
91 _MSG("Destroying session %s",
92 session->name);
93 newline_needed = true;
94 printed_destroy_msg = true;
95 fflush(stdout);
96 }
97
98 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US);
99 _MSG(".");
100 fflush(stdout);
101 }
102 } while (ret != 0);
103 }
104
105 if (!session_was_already_stopped) {
106 /*
107 * Don't print the event and packet loss warnings since the user
108 * already saw them when stopping the trace.
109 */
110 ret = get_session_stats_str(session->name, &stats_str);
111 if (ret < 0) {
112 goto error;
113 }
114 }
115
116 ret_code = lttng_destroy_session_ext(session->name, &handle);
117 if (ret_code != LTTNG_OK) {
118 ret = -ret_code;
119 goto error;
120 }
121
122 if (opt_no_wait) {
123 goto skip_wait_rotation;
124 }
125
126 do {
127 status = lttng_destruction_handle_wait_for_completion(
128 handle, DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US /
129 USEC_PER_MSEC);
130 switch (status) {
131 case LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT:
132 if (!printed_destroy_msg) {
133 _MSG("Destroying session %s", session->name);
134 newline_needed = true;
135 printed_destroy_msg = true;
136 }
137 _MSG(".");
138 fflush(stdout);
139 break;
140 case LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED:
141 break;
142 default:
143 ERR("%sFailed to wait for the completion of the destruction of session \"%s\"",
144 newline_needed ? "\n" : "",
145 session->name);
146 newline_needed = false;
147 ret = -1;
148 goto error;
149 }
150 } while (status == LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT);
151
152 status = lttng_destruction_handle_get_result(handle, &ret_code);
153 if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_OK) {
154 ERR("%sFailed to get the result of session destruction",
155 newline_needed ? "\n" : "");
156 ret = -1;
157 newline_needed = false;
158 goto error;
159 }
160 if (ret_code != LTTNG_OK) {
161 ret = -ret_code;
162 goto error;
163 }
164
165 status = lttng_destruction_handle_get_rotation_state(
166 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(
182 handle, &location);
183 if (status == LTTNG_DESTRUCTION_HANDLE_STATUS_OK) {
184 ret = print_trace_archive_location(
185 location, session->name);
186 if (ret) {
187 ERR("%sFailed to print the location of trace archive",
188 newline_needed ? "\n" : "");
189 newline_needed = false;
190 goto skip_wait_rotation;
191 }
192 break;
193 }
194 }
195 /* fall-through. */
196 default:
197 ERR("%sFailed to get the location of the rotation performed during the session's destruction",
198 newline_needed ? "\n" : "");
199 newline_needed = false;
200 goto skip_wait_rotation;
201 }
202skip_wait_rotation:
203 MSG("%sSession %s destroyed", newline_needed ? "\n" : "",
204 session->name);
205 newline_needed = false;
206 if (stats_str) {
207 MSG("%s", stats_str);
208 }
209
210 session_name = get_session_name_quiet();
211 if (session_name && !strncmp(session->name, session_name, NAME_MAX)) {
212 config_destroy_default();
213 }
214
215 if (lttng_opt_mi) {
216 ret = mi_lttng_session(writer, session, 0);
217 if (ret) {
218 ret = CMD_ERROR;
219 goto error;
220 }
221 }
222
223 ret = CMD_SUCCESS;
224error:
225 if (newline_needed) {
226 MSG("");
227 }
228 lttng_destruction_handle_destroy(handle);
229 free(session_name);
230 free(stats_str);
231 return ret;
232}
233
234/*
235 * destroy_all_sessions
236 *
237 * Call destroy_sessions for each registered sessions
238 */
239static int destroy_all_sessions(struct lttng_session *sessions, int count)
240{
241 int i;
242 bool error_occurred = false;
243
244 LTTNG_ASSERT(count >= 0);
245 if (count == 0) {
246 MSG("No session found, nothing to do.");
247 }
248
249 for (i = 0; i < count; i++) {
250 int ret = destroy_session(&sessions[i]);
251
252 if (ret < 0) {
253 ERR("%s during the destruction of session \"%s\"",
254 lttng_strerror(ret),
255 sessions[i].name);
256 /* Continue to next session. */
257 error_occurred = true;
258 }
259 }
260
261 return error_occurred ? CMD_ERROR : CMD_SUCCESS;
262}
263
264/*
265 * The 'destroy <options>' first level command
266 */
267int cmd_destroy(int argc, const char **argv)
268{
269 int opt;
270 int ret = CMD_SUCCESS , i, command_ret = CMD_SUCCESS, success = 1;
271 static poptContext pc;
272 char *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 opt_session_name = (char *) poptGetArg(pc);
346
347 if (!opt_session_name) {
348 /* No session name specified, lookup default */
349 session_name = get_session_name();
350 if (session_name == NULL) {
351 command_ret = CMD_ERROR;
352 success = 0;
353 goto mi_closing;
354 }
355 } else {
356 session_name = opt_session_name;
357 }
358
359 /* Find the corresponding lttng_session struct */
360 found = 0;
361 for (i = 0; i < count; i++) {
362 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
363 found = 1;
364 command_ret = destroy_session(&sessions[i]);
365 if (command_ret) {
366 success = 0;
367 ERR("%s during the destruction of session \"%s\"",
368 lttng_strerror(command_ret),
369 sessions[i].name);
370 }
371 }
372 }
373
374 if (!found) {
375 ERR("Session name %s not found", session_name);
376 command_ret = LTTNG_ERR_SESS_NOT_FOUND;
377 success = 0;
378 goto mi_closing;
379 }
380 }
381
382 leftover = poptGetArg(pc);
383 if (leftover) {
384 ERR("Unknown argument: %s", leftover);
385 ret = CMD_ERROR;
386 success = 0;
387 goto mi_closing;
388 }
389
390mi_closing:
391 /* Mi closing */
392 if (lttng_opt_mi) {
393 /* Close sessions and output element element */
394 ret = mi_lttng_close_multi_element(writer, 2);
395 if (ret) {
396 ret = CMD_ERROR;
397 goto end;
398 }
399
400 /* Success ? */
401 ret = mi_lttng_writer_write_element_bool(writer,
402 mi_lttng_element_command_success, success);
403 if (ret) {
404 ret = CMD_ERROR;
405 goto end;
406 }
407
408 /* Command element close */
409 ret = mi_lttng_writer_command_close(writer);
410 if (ret) {
411 ret = CMD_ERROR;
412 goto end;
413 }
414 }
415end:
416 /* Mi clean-up */
417 if (writer && mi_lttng_writer_destroy(writer)) {
418 /* Preserve original error code */
419 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
420 }
421
422 if (opt_session_name == NULL) {
423 free(session_name);
424 }
425
426 free(sessions);
427
428 /* Overwrite ret if an error occurred during destroy_session/all */
429 ret = command_ret ? command_ret : ret;
430
431 poptFreeContext(pc);
432 return ret;
433}
This page took 0.025048 seconds and 4 git commands to generate.