Fix: lttng-destroy: missing newline on session destruction message
[lttng-tools.git] / src / bin / lttng / commands / destroy.c
1 /*
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
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.h"
20
21 #include <common/mi-lttng.h>
22 #include <common/sessiond-comm/sessiond-comm.h>
23 #include <common/utils.h>
24
25 static char *opt_session_name;
26 static int opt_destroy_all;
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 };
42
43 static 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 */
58 static 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 _MSG("Destroying session %s", session->name);
91 newline_needed = true;
92 printed_destroy_msg = true;
93 fflush(stdout);
94
95 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US);
96 _MSG(".");
97 fflush(stdout);
98 }
99 } while (ret != 0);
100 }
101
102 if (!session_was_already_stopped) {
103 /*
104 * Don't print the event and packet loss warnings since the user
105 * already saw them when stopping the trace.
106 */
107 ret = get_session_stats_str(session->name, &stats_str);
108 if (ret < 0) {
109 goto error;
110 }
111 }
112
113 ret_code = lttng_destroy_session_ext(session->name, &handle);
114 if (ret_code != LTTNG_OK) {
115 ret = -ret_code;
116 goto error;
117 }
118
119 if (opt_no_wait) {
120 goto skip_wait_rotation;
121 }
122
123 do {
124 status = lttng_destruction_handle_wait_for_completion(
125 handle, DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US /
126 USEC_PER_MSEC);
127 switch (status) {
128 case LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT:
129 if (!printed_destroy_msg) {
130 _MSG("Destroying session %s", session->name);
131 newline_needed = true;
132 printed_destroy_msg = true;
133 }
134 _MSG(".");
135 fflush(stdout);
136 break;
137 case LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED:
138 break;
139 default:
140 ERR("%sFailed to wait for the completion of the destruction of session \"%s\"",
141 newline_needed ? "\n" : "",
142 session->name);
143 newline_needed = false;
144 ret = -1;
145 goto error;
146 }
147 } while (status == LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT);
148
149 status = lttng_destruction_handle_get_result(handle, &ret_code);
150 if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_OK) {
151 ERR("%sFailed to get the result of session destruction",
152 newline_needed ? "\n" : "");
153 ret = -1;
154 newline_needed = false;
155 goto error;
156 }
157 if (ret_code != LTTNG_OK) {
158 ret = -ret_code;
159 goto error;
160 }
161
162 status = lttng_destruction_handle_get_rotation_state(
163 handle, &rotation_state);
164 if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_OK) {
165 ERR("%sFailed to get rotation state from destruction handle",
166 newline_needed ? "\n" : "");
167 newline_needed = false;
168 goto skip_wait_rotation;
169 }
170
171 switch (rotation_state) {
172 case LTTNG_ROTATION_STATE_NO_ROTATION:
173 break;
174 case LTTNG_ROTATION_STATE_COMPLETED:
175 {
176 const struct lttng_trace_archive_location *location;
177
178 status = lttng_destruction_handle_get_archive_location(
179 handle, &location);
180 if (status == LTTNG_DESTRUCTION_HANDLE_STATUS_OK) {
181 ret = print_trace_archive_location(
182 location, session->name);
183 if (ret) {
184 ERR("%sFailed to print the location of trace archive",
185 newline_needed ? "\n" : "");
186 newline_needed = false;
187 goto skip_wait_rotation;
188 }
189 break;
190 }
191 /* fall-through. */
192 }
193 default:
194 ERR("%sFailed to get the location of the rotation performed during the session's destruction",
195 newline_needed ? "\n" : "");
196 newline_needed = false;
197 goto skip_wait_rotation;
198 }
199 skip_wait_rotation:
200 MSG("%sSession %s destroyed", newline_needed ? "\n" : "",
201 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 /*
232 * destroy_all_sessions
233 *
234 * Call destroy_sessions for each registered sessions
235 */
236 static int destroy_all_sessions(struct lttng_session *sessions, int count)
237 {
238 int i;
239 bool error_occurred = false;
240
241 assert(count >= 0);
242 if (count == 0) {
243 MSG("No session found, nothing to do.");
244 }
245
246 for (i = 0; i < count; i++) {
247 int ret = destroy_session(&sessions[i]);
248
249 if (ret < 0) {
250 ERR("%s during the destruction of session \"%s\"",
251 lttng_strerror(ret),
252 sessions[i].name);
253 /* Continue to next session. */
254 error_occurred = true;
255 }
256 }
257
258 return error_occurred ? CMD_ERROR : CMD_SUCCESS;
259 }
260
261 /*
262 * The 'destroy <options>' first level command
263 */
264 int cmd_destroy(int argc, const char **argv)
265 {
266 int opt;
267 int ret = CMD_SUCCESS , i, command_ret = CMD_SUCCESS, success = 1;
268 static poptContext pc;
269 char *session_name = NULL;
270 const char *leftover = NULL;
271
272 struct lttng_session *sessions;
273 int count;
274 int found;
275
276 pc = poptGetContext(NULL, argc, argv, long_options, 0);
277 poptReadDefaultConfig(pc, 0);
278
279 while ((opt = poptGetNextOpt(pc)) != -1) {
280 switch (opt) {
281 case OPT_HELP:
282 SHOW_HELP();
283 break;
284 case OPT_LIST_OPTIONS:
285 list_cmd_options(stdout, long_options);
286 break;
287 default:
288 ret = CMD_UNDEFINED;
289 break;
290 }
291 goto end;
292 }
293
294 /* Mi preparation */
295 if (lttng_opt_mi) {
296 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
297 if (!writer) {
298 ret = -LTTNG_ERR_NOMEM;
299 goto end;
300 }
301
302 /* Open command element */
303 ret = mi_lttng_writer_command_open(writer,
304 mi_lttng_element_command_destroy);
305 if (ret) {
306 ret = CMD_ERROR;
307 goto end;
308 }
309
310 /* Open output element */
311 ret = mi_lttng_writer_open_element(writer,
312 mi_lttng_element_command_output);
313 if (ret) {
314 ret = CMD_ERROR;
315 goto end;
316 }
317
318 /* For validation and semantic purpose we open a sessions element */
319 ret = mi_lttng_sessions_open(writer);
320 if (ret) {
321 ret = CMD_ERROR;
322 goto end;
323 }
324 }
325
326 /* Recuperate all sessions for further operation */
327 count = lttng_list_sessions(&sessions);
328 if (count < 0) {
329 ERR("%s", lttng_strerror(count));
330 command_ret = CMD_ERROR;
331 success = 0;
332 goto mi_closing;
333 }
334
335 /* Ignore session name in case all sessions are to be destroyed */
336 if (opt_destroy_all) {
337 command_ret = destroy_all_sessions(sessions, count);
338 if (command_ret) {
339 success = 0;
340 }
341 } else {
342 opt_session_name = (char *) poptGetArg(pc);
343
344 if (!opt_session_name) {
345 /* No session name specified, lookup default */
346 session_name = get_session_name();
347 if (session_name == NULL) {
348 command_ret = CMD_ERROR;
349 success = 0;
350 goto mi_closing;
351 }
352 } else {
353 session_name = opt_session_name;
354 }
355
356 /* Find the corresponding lttng_session struct */
357 found = 0;
358 for (i = 0; i < count; i++) {
359 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
360 found = 1;
361 command_ret = destroy_session(&sessions[i]);
362 if (command_ret) {
363 success = 0;
364 ERR("%s during the destruction of session \"%s\"",
365 lttng_strerror(command_ret),
366 sessions[i].name);
367 }
368 }
369 }
370
371 if (!found) {
372 ERR("Session name %s not found", session_name);
373 command_ret = LTTNG_ERR_SESS_NOT_FOUND;
374 success = 0;
375 goto mi_closing;
376 }
377 }
378
379 leftover = poptGetArg(pc);
380 if (leftover) {
381 ERR("Unknown argument: %s", leftover);
382 ret = CMD_ERROR;
383 success = 0;
384 goto mi_closing;
385 }
386
387 mi_closing:
388 /* Mi closing */
389 if (lttng_opt_mi) {
390 /* Close sessions and output element element */
391 ret = mi_lttng_close_multi_element(writer, 2);
392 if (ret) {
393 ret = CMD_ERROR;
394 goto end;
395 }
396
397 /* Success ? */
398 ret = mi_lttng_writer_write_element_bool(writer,
399 mi_lttng_element_command_success, success);
400 if (ret) {
401 ret = CMD_ERROR;
402 goto end;
403 }
404
405 /* Command element close */
406 ret = mi_lttng_writer_command_close(writer);
407 if (ret) {
408 ret = CMD_ERROR;
409 goto end;
410 }
411 }
412 end:
413 /* Mi clean-up */
414 if (writer && mi_lttng_writer_destroy(writer)) {
415 /* Preserve original error code */
416 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
417 }
418
419 if (opt_session_name == NULL) {
420 free(session_name);
421 }
422
423 /* Overwrite ret if an error occurred during destroy_session/all */
424 ret = command_ret ? command_ret : ret;
425
426 poptFreeContext(pc);
427 return ret;
428 }
This page took 0.037902 seconds and 5 git commands to generate.