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