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