Fix: error out on leftover arguments
[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
28 #include "../command.h"
29
30 #include <common/mi-lttng.h>
31 #include <common/sessiond-comm/sessiond-comm.h>
32 #include <common/utils.h>
33
34 static char *opt_session_name;
35 static int opt_destroy_all;
36 static int opt_no_wait;
37
38 /* Mi writer */
39 static struct mi_writer *writer;
40
41 enum {
42 OPT_HELP = 1,
43 OPT_LIST_OPTIONS,
44 };
45
46 static struct poptOption long_options[] = {
47 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
48 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
49 {"all", 'a', POPT_ARG_VAL, &opt_destroy_all, 1, 0, 0},
50 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
51 {"no-wait", 'n', POPT_ARG_VAL, &opt_no_wait, 1, 0, 0},
52 {0, 0, 0, 0, 0, 0, 0}
53 };
54
55 /*
56 * destroy_session
57 *
58 * Unregister the provided session to the session daemon. On success, removes
59 * the default configuration.
60 */
61 static int destroy_session(struct lttng_session *session)
62 {
63 int ret;
64 char *session_name = NULL;
65 bool session_was_stopped;
66
67 ret = lttng_stop_tracing_no_wait(session->name);
68 if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
69 ERR("%s", lttng_strerror(ret));
70 }
71 session_was_stopped = ret == -LTTNG_ERR_TRACE_ALREADY_STOPPED;
72 if (!opt_no_wait) {
73 bool printed_wait_msg = false;
74
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 data availability");
89 fflush(stdout);
90 }
91
92 printed_wait_msg = true;
93 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME);
94 _MSG(".");
95 fflush(stdout);
96 }
97 } while (ret != 0);
98 if (printed_wait_msg) {
99 MSG("");
100 }
101 }
102 if (!session_was_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 print_session_stats(session->name);
108 }
109
110 ret = lttng_destroy_session_no_wait(session->name);
111 if (ret < 0) {
112 goto error;
113 }
114
115 MSG("Session %s destroyed", session->name);
116
117 session_name = get_session_name_quiet();
118 if (session_name && !strncmp(session->name, session_name, NAME_MAX)) {
119 config_destroy_default();
120 }
121
122 if (lttng_opt_mi) {
123 ret = mi_lttng_session(writer, session, 0);
124 if (ret) {
125 ret = CMD_ERROR;
126 goto error;
127 }
128 }
129
130 ret = CMD_SUCCESS;
131 error:
132 free(session_name);
133 return ret;
134 }
135
136 /*
137 * destroy_all_sessions
138 *
139 * Call destroy_sessions for each registered sessions
140 */
141 static int destroy_all_sessions(struct lttng_session *sessions, int count)
142 {
143 int i, ret = CMD_SUCCESS;
144
145 if (count == 0) {
146 MSG("No session found, nothing to do.");
147 } else if (count < 0) {
148 ERR("%s", lttng_strerror(ret));
149 goto error;
150 }
151
152 for (i = 0; i < count; i++) {
153 ret = destroy_session(&sessions[i]);
154 if (ret < 0) {
155 goto error;
156 }
157 }
158 error:
159 return ret;
160 }
161
162 /*
163 * The 'destroy <options>' first level command
164 */
165 int cmd_destroy(int argc, const char **argv)
166 {
167 int opt;
168 int ret = CMD_SUCCESS , i, command_ret = CMD_SUCCESS, success = 1;
169 static poptContext pc;
170 char *session_name = NULL;
171 const char *leftover = NULL;
172
173 struct lttng_session *sessions;
174 int count;
175 int found;
176
177 pc = poptGetContext(NULL, argc, argv, long_options, 0);
178 poptReadDefaultConfig(pc, 0);
179
180 while ((opt = poptGetNextOpt(pc)) != -1) {
181 switch (opt) {
182 case OPT_HELP:
183 SHOW_HELP();
184 break;
185 case OPT_LIST_OPTIONS:
186 list_cmd_options(stdout, long_options);
187 break;
188 default:
189 ret = CMD_UNDEFINED;
190 break;
191 }
192 goto end;
193 }
194
195 /* Mi preparation */
196 if (lttng_opt_mi) {
197 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
198 if (!writer) {
199 ret = -LTTNG_ERR_NOMEM;
200 goto end;
201 }
202
203 /* Open command element */
204 ret = mi_lttng_writer_command_open(writer,
205 mi_lttng_element_command_destroy);
206 if (ret) {
207 ret = CMD_ERROR;
208 goto end;
209 }
210
211 /* Open output element */
212 ret = mi_lttng_writer_open_element(writer,
213 mi_lttng_element_command_output);
214 if (ret) {
215 ret = CMD_ERROR;
216 goto end;
217 }
218
219 /* For validation and semantic purpose we open a sessions element */
220 ret = mi_lttng_sessions_open(writer);
221 if (ret) {
222 ret = CMD_ERROR;
223 goto end;
224 }
225 }
226
227 /* Recuperate all sessions for further operation */
228 count = lttng_list_sessions(&sessions);
229 if (count < 0) {
230 ERR("%s", lttng_strerror(count));
231 command_ret = CMD_ERROR;
232 success = 0;
233 goto mi_closing;
234 }
235
236 /* Ignore session name in case all sessions are to be destroyed */
237 if (opt_destroy_all) {
238 command_ret = destroy_all_sessions(sessions, count);
239 if (command_ret) {
240 success = 0;
241 }
242 } else {
243 opt_session_name = (char *) poptGetArg(pc);
244
245 if (!opt_session_name) {
246 /* No session name specified, lookup default */
247 session_name = get_session_name();
248 if (session_name == NULL) {
249 command_ret = CMD_ERROR;
250 success = 0;
251 goto mi_closing;
252 }
253 } else {
254 session_name = opt_session_name;
255 }
256
257 /* Find the corresponding lttng_session struct */
258 found = 0;
259 for (i = 0; i < count; i++) {
260 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
261 found = 1;
262 command_ret = destroy_session(&sessions[i]);
263 if (command_ret) {
264 success = 0;
265 }
266
267 }
268 }
269
270 if (!found) {
271 ERR("Session name %s not found", session_name);
272 command_ret = LTTNG_ERR_SESS_NOT_FOUND;
273 success = 0;
274 goto mi_closing;
275 }
276 }
277
278 leftover = poptGetArg(pc);
279 if (leftover) {
280 ERR("Unknown argument: %s", leftover);
281 ret = CMD_ERROR;
282 success = 0;
283 goto mi_closing;
284 }
285
286 mi_closing:
287 /* Mi closing */
288 if (lttng_opt_mi) {
289 /* Close sessions and output element element */
290 ret = mi_lttng_close_multi_element(writer, 2);
291 if (ret) {
292 ret = CMD_ERROR;
293 goto end;
294 }
295
296 /* Success ? */
297 ret = mi_lttng_writer_write_element_bool(writer,
298 mi_lttng_element_command_success, success);
299 if (ret) {
300 ret = CMD_ERROR;
301 goto end;
302 }
303
304 /* Command element close */
305 ret = mi_lttng_writer_command_close(writer);
306 if (ret) {
307 ret = CMD_ERROR;
308 goto end;
309 }
310 }
311 end:
312 /* Mi clean-up */
313 if (writer && mi_lttng_writer_destroy(writer)) {
314 /* Preserve original error code */
315 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
316 }
317
318 if (opt_session_name == NULL) {
319 free(session_name);
320 }
321
322 /* Overwrite ret if an error occurred during destroy_session/all */
323 ret = command_ret ? command_ret : ret;
324
325 poptFreeContext(pc);
326 return ret;
327 }
This page took 0.036883 seconds and 5 git commands to generate.