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