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 | ||
f3ed775e | 55 | /* |
b09ee5ba FG |
56 | * destroy_session |
57 | * | |
58 | * Unregister the provided session to the session daemon. On success, removes | |
59 | * the default configuration. | |
f3ed775e | 60 | */ |
65f25c66 | 61 | static int destroy_session(struct lttng_session *session) |
f3ed775e DG |
62 | { |
63 | int ret; | |
1dac0189 | 64 | char *session_name = NULL; |
20fb9e02 | 65 | bool session_was_stopped; |
f3ed775e | 66 | |
e20ee7c2 JD |
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 | } | |
20fb9e02 | 71 | session_was_stopped = ret == -LTTNG_ERR_TRACE_ALREADY_STOPPED; |
e20ee7c2 JD |
72 | if (!opt_no_wait) { |
73 | _MSG("Waiting for data availability"); | |
74 | fflush(stdout); | |
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 | usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME); | |
88 | _MSG("."); | |
89 | fflush(stdout); | |
90 | } | |
91 | } while (ret != 0); | |
92 | MSG(""); | |
93 | } | |
20fb9e02 JD |
94 | if (!session_was_stopped) { |
95 | /* | |
96 | * Don't print the event and packet loss warnings since the user | |
97 | * already saw them when stopping the trace. | |
98 | */ | |
99 | print_session_stats(session->name); | |
100 | } | |
e20ee7c2 JD |
101 | |
102 | ret = lttng_destroy_session_no_wait(session->name); | |
f3ed775e | 103 | if (ret < 0) { |
b09ee5ba | 104 | goto error; |
f3ed775e DG |
105 | } |
106 | ||
65f25c66 | 107 | MSG("Session %s destroyed", session->name); |
1dac0189 PPM |
108 | |
109 | session_name = get_session_name_quiet(); | |
110 | if (session_name && !strncmp(session->name, session_name, NAME_MAX)) { | |
111 | config_destroy_default(); | |
112 | } | |
65f25c66 JRJ |
113 | |
114 | if (lttng_opt_mi) { | |
115 | ret = mi_lttng_session(writer, session, 0); | |
116 | if (ret) { | |
117 | ret = CMD_ERROR; | |
118 | goto error; | |
119 | } | |
120 | } | |
121 | ||
f3ed775e | 122 | ret = CMD_SUCCESS; |
b09ee5ba | 123 | error: |
1dac0189 | 124 | free(session_name); |
b09ee5ba FG |
125 | return ret; |
126 | } | |
f3ed775e | 127 | |
b09ee5ba FG |
128 | /* |
129 | * destroy_all_sessions | |
130 | * | |
131 | * Call destroy_sessions for each registered sessions | |
132 | */ | |
65f25c66 | 133 | static int destroy_all_sessions(struct lttng_session *sessions, int count) |
b09ee5ba | 134 | { |
65f25c66 | 135 | int i, ret = CMD_SUCCESS; |
b09ee5ba | 136 | |
b09ee5ba FG |
137 | if (count == 0) { |
138 | MSG("No session found, nothing to do."); | |
60e835ca DG |
139 | } else if (count < 0) { |
140 | ERR("%s", lttng_strerror(ret)); | |
141 | goto error; | |
b09ee5ba | 142 | } |
60e835ca | 143 | |
b09ee5ba | 144 | for (i = 0; i < count; i++) { |
65f25c66 | 145 | ret = destroy_session(&sessions[i]); |
b09ee5ba FG |
146 | if (ret < 0) { |
147 | goto error; | |
148 | } | |
b73d0b29 | 149 | } |
f3ed775e DG |
150 | error: |
151 | return ret; | |
152 | } | |
153 | ||
154 | /* | |
843f5df9 | 155 | * The 'destroy <options>' first level command |
f3ed775e DG |
156 | */ |
157 | int cmd_destroy(int argc, const char **argv) | |
158 | { | |
b09ee5ba | 159 | int opt; |
65f25c66 | 160 | int ret = CMD_SUCCESS , i, command_ret = CMD_SUCCESS, success = 1; |
f3ed775e | 161 | static poptContext pc; |
b09ee5ba | 162 | char *session_name = NULL; |
f3ed775e | 163 | |
65f25c66 JRJ |
164 | struct lttng_session *sessions; |
165 | int count; | |
166 | int found; | |
167 | ||
f3ed775e DG |
168 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
169 | poptReadDefaultConfig(pc, 0); | |
170 | ||
171 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
172 | switch (opt) { | |
173 | case OPT_HELP: | |
4ba92f18 | 174 | SHOW_HELP(); |
b09ee5ba | 175 | break; |
679b4943 SM |
176 | case OPT_LIST_OPTIONS: |
177 | list_cmd_options(stdout, long_options); | |
b09ee5ba | 178 | break; |
f3ed775e | 179 | default: |
f3ed775e | 180 | ret = CMD_UNDEFINED; |
b09ee5ba | 181 | break; |
f3ed775e | 182 | } |
b09ee5ba | 183 | goto end; |
f3ed775e DG |
184 | } |
185 | ||
65f25c66 | 186 | /* Mi preparation */ |
c7e35b03 | 187 | if (lttng_opt_mi) { |
65f25c66 JRJ |
188 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); |
189 | if (!writer) { | |
190 | ret = -LTTNG_ERR_NOMEM; | |
191 | goto end; | |
192 | } | |
193 | ||
194 | /* Open command element */ | |
195 | ret = mi_lttng_writer_command_open(writer, | |
196 | mi_lttng_element_command_destroy); | |
197 | if (ret) { | |
198 | ret = CMD_ERROR; | |
199 | goto end; | |
200 | } | |
201 | ||
202 | /* Open output element */ | |
203 | ret = mi_lttng_writer_open_element(writer, | |
204 | mi_lttng_element_command_output); | |
205 | if (ret) { | |
206 | ret = CMD_ERROR; | |
207 | goto end; | |
208 | } | |
209 | ||
210 | /* For validation and semantic purpose we open a sessions element */ | |
211 | ret = mi_lttng_sessions_open(writer); | |
212 | if (ret) { | |
213 | ret = CMD_ERROR; | |
214 | goto end; | |
215 | } | |
216 | } | |
217 | ||
218 | /* Recuperate all sessions for further operation */ | |
219 | count = lttng_list_sessions(&sessions); | |
220 | if (count < 0) { | |
221 | command_ret = count; | |
222 | success = 0; | |
223 | goto mi_closing; | |
c7e35b03 JR |
224 | } |
225 | ||
fd076c09 | 226 | /* Ignore session name in case all sessions are to be destroyed */ |
b09ee5ba | 227 | if (opt_destroy_all) { |
65f25c66 JRJ |
228 | command_ret = destroy_all_sessions(sessions, count); |
229 | if (command_ret) { | |
230 | success = 0; | |
231 | } | |
232 | } else { | |
233 | opt_session_name = (char *) poptGetArg(pc); | |
234 | ||
1dac0189 | 235 | if (!opt_session_name) { |
65f25c66 JRJ |
236 | /* No session name specified, lookup default */ |
237 | session_name = get_session_name(); | |
238 | if (session_name == NULL) { | |
239 | command_ret = CMD_ERROR; | |
240 | success = 0; | |
241 | goto mi_closing; | |
242 | } | |
243 | } else { | |
244 | session_name = opt_session_name; | |
245 | } | |
fd076c09 | 246 | |
65f25c66 JRJ |
247 | /* Find the corresponding lttng_session struct */ |
248 | found = 0; | |
249 | for (i = 0; i < count; i++) { | |
250 | if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) { | |
251 | found = 1; | |
252 | command_ret = destroy_session(&sessions[i]); | |
253 | if (command_ret) { | |
254 | success = 0; | |
255 | } | |
fd076c09 | 256 | |
65f25c66 JRJ |
257 | } |
258 | } | |
259 | ||
260 | if (!found) { | |
261 | ERR("Session name %s not found", session_name); | |
262 | command_ret = LTTNG_ERR_SESS_NOT_FOUND; | |
263 | success = 0; | |
264 | goto mi_closing; | |
265 | } | |
266 | } | |
267 | ||
268 | mi_closing: | |
269 | /* Mi closing */ | |
270 | if (lttng_opt_mi) { | |
271 | /* Close sessions and output element element */ | |
272 | ret = mi_lttng_close_multi_element(writer, 2); | |
273 | if (ret) { | |
fd076c09 | 274 | ret = CMD_ERROR; |
b09ee5ba FG |
275 | goto end; |
276 | } | |
fd076c09 | 277 | |
65f25c66 JRJ |
278 | /* Success ? */ |
279 | ret = mi_lttng_writer_write_element_bool(writer, | |
280 | mi_lttng_element_command_success, success); | |
281 | if (ret) { | |
282 | ret = CMD_ERROR; | |
283 | goto end; | |
284 | } | |
f3ed775e | 285 | |
65f25c66 JRJ |
286 | /* Command element close */ |
287 | ret = mi_lttng_writer_command_close(writer); | |
288 | if (ret) { | |
289 | ret = CMD_ERROR; | |
290 | goto end; | |
291 | } | |
292 | } | |
f3ed775e | 293 | end: |
65f25c66 JRJ |
294 | /* Mi clean-up */ |
295 | if (writer && mi_lttng_writer_destroy(writer)) { | |
296 | /* Preserve original error code */ | |
297 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; | |
298 | } | |
299 | ||
fd076c09 | 300 | if (opt_session_name == NULL) { |
b09ee5ba FG |
301 | free(session_name); |
302 | } | |
fd076c09 | 303 | |
65f25c66 JRJ |
304 | /* Overwrite ret if an error occurred during destroy_session/all */ |
305 | ret = command_ret ? command_ret : ret; | |
306 | ||
fd076c09 | 307 | poptFreeContext(pc); |
f3ed775e DG |
308 | return ret; |
309 | } |