Commit | Line | Data |
---|---|---|
f3ed775e DG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * | |
d14d33bf AM |
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. | |
f3ed775e DG |
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 | * | |
d14d33bf AM |
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. | |
f3ed775e DG |
16 | */ |
17 | ||
6c1c0768 | 18 | #define _LGPL_SOURCE |
f3ed775e DG |
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> | |
20fb9e02 | 26 | #include <stdbool.h> |
f3ed775e | 27 | |
c399183f | 28 | #include "../command.h" |
f3ed775e | 29 | |
65f25c66 | 30 | #include <common/mi-lttng.h> |
42224349 | 31 | #include <common/sessiond-comm/sessiond-comm.h> |
1dac0189 | 32 | #include <common/utils.h> |
42224349 | 33 | |
fd076c09 | 34 | static char *opt_session_name; |
b09ee5ba | 35 | static int opt_destroy_all; |
e20ee7c2 | 36 | static int opt_no_wait; |
f3ed775e | 37 | |
65f25c66 JRJ |
38 | /* Mi writer */ |
39 | static struct mi_writer *writer; | |
40 | ||
f3ed775e DG |
41 | enum { |
42 | OPT_HELP = 1, | |
679b4943 | 43 | OPT_LIST_OPTIONS, |
f3ed775e DG |
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}, | |
b09ee5ba | 49 | {"all", 'a', POPT_ARG_VAL, &opt_destroy_all, 1, 0, 0}, |
679b4943 | 50 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
e20ee7c2 | 51 | {"no-wait", 'n', POPT_ARG_VAL, &opt_no_wait, 1, 0, 0}, |
f3ed775e DG |
52 | {0, 0, 0, 0, 0, 0, 0} |
53 | }; | |
54 | ||
55 | /* | |
56 | * usage | |
57 | */ | |
58 | static void usage(FILE *ofp) | |
59 | { | |
32a6298d | 60 | fprintf(ofp, "usage: lttng destroy [NAME] [OPTIONS]\n"); |
f3ed775e DG |
61 | fprintf(ofp, "\n"); |
62 | fprintf(ofp, "Where NAME is an optional session name. If not specified, lttng will\n"); | |
63 | fprintf(ofp, "get it from the configuration directory (.lttng).\n"); | |
64 | fprintf(ofp, "\n"); | |
32a6298d | 65 | fprintf(ofp, "Options:\n"); |
f3ed775e | 66 | fprintf(ofp, " -h, --help Show this help\n"); |
b09ee5ba | 67 | fprintf(ofp, " -a, --all Destroy all sessions\n"); |
27221701 | 68 | fprintf(ofp, " --list-options Simple listing of options\n"); |
e20ee7c2 | 69 | fprintf(ofp, " -n, --no-wait Don't wait for data availability\n"); |
f3ed775e DG |
70 | fprintf(ofp, "\n"); |
71 | } | |
72 | ||
73 | /* | |
b09ee5ba FG |
74 | * destroy_session |
75 | * | |
76 | * Unregister the provided session to the session daemon. On success, removes | |
77 | * the default configuration. | |
f3ed775e | 78 | */ |
65f25c66 | 79 | static int destroy_session(struct lttng_session *session) |
f3ed775e DG |
80 | { |
81 | int ret; | |
1dac0189 | 82 | char *session_name = NULL; |
20fb9e02 | 83 | bool session_was_stopped; |
f3ed775e | 84 | |
e20ee7c2 JD |
85 | ret = lttng_stop_tracing_no_wait(session->name); |
86 | if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) { | |
87 | ERR("%s", lttng_strerror(ret)); | |
88 | } | |
20fb9e02 | 89 | session_was_stopped = ret == -LTTNG_ERR_TRACE_ALREADY_STOPPED; |
e20ee7c2 JD |
90 | if (!opt_no_wait) { |
91 | _MSG("Waiting for data availability"); | |
92 | fflush(stdout); | |
93 | do { | |
94 | ret = lttng_data_pending(session->name); | |
95 | if (ret < 0) { | |
96 | /* Return the data available call error. */ | |
97 | goto error; | |
98 | } | |
99 | ||
100 | /* | |
101 | * Data sleep time before retrying (in usec). Don't sleep if the call | |
102 | * returned value indicates availability. | |
103 | */ | |
104 | if (ret) { | |
105 | usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME); | |
106 | _MSG("."); | |
107 | fflush(stdout); | |
108 | } | |
109 | } while (ret != 0); | |
110 | MSG(""); | |
111 | } | |
20fb9e02 JD |
112 | if (!session_was_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 | print_session_stats(session->name); | |
118 | } | |
e20ee7c2 JD |
119 | |
120 | ret = lttng_destroy_session_no_wait(session->name); | |
f3ed775e | 121 | if (ret < 0) { |
b09ee5ba | 122 | goto error; |
f3ed775e DG |
123 | } |
124 | ||
65f25c66 | 125 | MSG("Session %s destroyed", session->name); |
1dac0189 PPM |
126 | |
127 | session_name = get_session_name_quiet(); | |
128 | if (session_name && !strncmp(session->name, session_name, NAME_MAX)) { | |
129 | config_destroy_default(); | |
130 | } | |
65f25c66 JRJ |
131 | |
132 | if (lttng_opt_mi) { | |
133 | ret = mi_lttng_session(writer, session, 0); | |
134 | if (ret) { | |
135 | ret = CMD_ERROR; | |
136 | goto error; | |
137 | } | |
138 | } | |
139 | ||
f3ed775e | 140 | ret = CMD_SUCCESS; |
b09ee5ba | 141 | error: |
1dac0189 | 142 | free(session_name); |
b09ee5ba FG |
143 | return ret; |
144 | } | |
f3ed775e | 145 | |
b09ee5ba FG |
146 | /* |
147 | * destroy_all_sessions | |
148 | * | |
149 | * Call destroy_sessions for each registered sessions | |
150 | */ | |
65f25c66 | 151 | static int destroy_all_sessions(struct lttng_session *sessions, int count) |
b09ee5ba | 152 | { |
65f25c66 | 153 | int i, ret = CMD_SUCCESS; |
b09ee5ba | 154 | |
b09ee5ba FG |
155 | if (count == 0) { |
156 | MSG("No session found, nothing to do."); | |
60e835ca DG |
157 | } else if (count < 0) { |
158 | ERR("%s", lttng_strerror(ret)); | |
159 | goto error; | |
b09ee5ba | 160 | } |
60e835ca | 161 | |
b09ee5ba | 162 | for (i = 0; i < count; i++) { |
65f25c66 | 163 | ret = destroy_session(&sessions[i]); |
b09ee5ba FG |
164 | if (ret < 0) { |
165 | goto error; | |
166 | } | |
b73d0b29 | 167 | } |
f3ed775e DG |
168 | error: |
169 | return ret; | |
170 | } | |
171 | ||
172 | /* | |
843f5df9 | 173 | * The 'destroy <options>' first level command |
f3ed775e DG |
174 | */ |
175 | int cmd_destroy(int argc, const char **argv) | |
176 | { | |
b09ee5ba | 177 | int opt; |
65f25c66 | 178 | int ret = CMD_SUCCESS , i, command_ret = CMD_SUCCESS, success = 1; |
f3ed775e | 179 | static poptContext pc; |
b09ee5ba | 180 | char *session_name = NULL; |
f3ed775e | 181 | |
65f25c66 JRJ |
182 | struct lttng_session *sessions; |
183 | int count; | |
184 | int found; | |
185 | ||
f3ed775e DG |
186 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
187 | poptReadDefaultConfig(pc, 0); | |
188 | ||
189 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
190 | switch (opt) { | |
191 | case OPT_HELP: | |
ca1c3607 | 192 | usage(stdout); |
b09ee5ba | 193 | break; |
679b4943 SM |
194 | case OPT_LIST_OPTIONS: |
195 | list_cmd_options(stdout, long_options); | |
b09ee5ba | 196 | break; |
f3ed775e DG |
197 | default: |
198 | usage(stderr); | |
199 | ret = CMD_UNDEFINED; | |
b09ee5ba | 200 | break; |
f3ed775e | 201 | } |
b09ee5ba | 202 | goto end; |
f3ed775e DG |
203 | } |
204 | ||
65f25c66 | 205 | /* Mi preparation */ |
c7e35b03 | 206 | if (lttng_opt_mi) { |
65f25c66 JRJ |
207 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); |
208 | if (!writer) { | |
209 | ret = -LTTNG_ERR_NOMEM; | |
210 | goto end; | |
211 | } | |
212 | ||
213 | /* Open command element */ | |
214 | ret = mi_lttng_writer_command_open(writer, | |
215 | mi_lttng_element_command_destroy); | |
216 | if (ret) { | |
217 | ret = CMD_ERROR; | |
218 | goto end; | |
219 | } | |
220 | ||
221 | /* Open output element */ | |
222 | ret = mi_lttng_writer_open_element(writer, | |
223 | mi_lttng_element_command_output); | |
224 | if (ret) { | |
225 | ret = CMD_ERROR; | |
226 | goto end; | |
227 | } | |
228 | ||
229 | /* For validation and semantic purpose we open a sessions element */ | |
230 | ret = mi_lttng_sessions_open(writer); | |
231 | if (ret) { | |
232 | ret = CMD_ERROR; | |
233 | goto end; | |
234 | } | |
235 | } | |
236 | ||
237 | /* Recuperate all sessions for further operation */ | |
238 | count = lttng_list_sessions(&sessions); | |
239 | if (count < 0) { | |
240 | command_ret = count; | |
241 | success = 0; | |
242 | goto mi_closing; | |
c7e35b03 JR |
243 | } |
244 | ||
fd076c09 | 245 | /* Ignore session name in case all sessions are to be destroyed */ |
b09ee5ba | 246 | if (opt_destroy_all) { |
65f25c66 JRJ |
247 | command_ret = destroy_all_sessions(sessions, count); |
248 | if (command_ret) { | |
249 | success = 0; | |
250 | } | |
251 | } else { | |
252 | opt_session_name = (char *) poptGetArg(pc); | |
253 | ||
1dac0189 | 254 | if (!opt_session_name) { |
65f25c66 JRJ |
255 | /* No session name specified, lookup default */ |
256 | session_name = get_session_name(); | |
257 | if (session_name == NULL) { | |
258 | command_ret = CMD_ERROR; | |
259 | success = 0; | |
260 | goto mi_closing; | |
261 | } | |
262 | } else { | |
263 | session_name = opt_session_name; | |
264 | } | |
fd076c09 | 265 | |
65f25c66 JRJ |
266 | /* Find the corresponding lttng_session struct */ |
267 | found = 0; | |
268 | for (i = 0; i < count; i++) { | |
269 | if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) { | |
270 | found = 1; | |
271 | command_ret = destroy_session(&sessions[i]); | |
272 | if (command_ret) { | |
273 | success = 0; | |
274 | } | |
fd076c09 | 275 | |
65f25c66 JRJ |
276 | } |
277 | } | |
278 | ||
279 | if (!found) { | |
280 | ERR("Session name %s not found", session_name); | |
281 | command_ret = LTTNG_ERR_SESS_NOT_FOUND; | |
282 | success = 0; | |
283 | goto mi_closing; | |
284 | } | |
285 | } | |
286 | ||
287 | mi_closing: | |
288 | /* Mi closing */ | |
289 | if (lttng_opt_mi) { | |
290 | /* Close sessions and output element element */ | |
291 | ret = mi_lttng_close_multi_element(writer, 2); | |
292 | if (ret) { | |
fd076c09 | 293 | ret = CMD_ERROR; |
b09ee5ba FG |
294 | goto end; |
295 | } | |
fd076c09 | 296 | |
65f25c66 JRJ |
297 | /* Success ? */ |
298 | ret = mi_lttng_writer_write_element_bool(writer, | |
299 | mi_lttng_element_command_success, success); | |
300 | if (ret) { | |
301 | ret = CMD_ERROR; | |
302 | goto end; | |
303 | } | |
f3ed775e | 304 | |
65f25c66 JRJ |
305 | /* Command element close */ |
306 | ret = mi_lttng_writer_command_close(writer); | |
307 | if (ret) { | |
308 | ret = CMD_ERROR; | |
309 | goto end; | |
310 | } | |
311 | } | |
f3ed775e | 312 | end: |
65f25c66 JRJ |
313 | /* Mi clean-up */ |
314 | if (writer && mi_lttng_writer_destroy(writer)) { | |
315 | /* Preserve original error code */ | |
316 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; | |
317 | } | |
318 | ||
fd076c09 | 319 | if (opt_session_name == NULL) { |
b09ee5ba FG |
320 | free(session_name); |
321 | } | |
fd076c09 | 322 | |
65f25c66 JRJ |
323 | /* Overwrite ret if an error occurred during destroy_session/all */ |
324 | ret = command_ret ? command_ret : ret; | |
325 | ||
fd076c09 | 326 | poptFreeContext(pc); |
f3ed775e DG |
327 | return ret; |
328 | } |