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