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 | ||
18 | #define _GNU_SOURCE | |
6c1c0768 | 19 | #define _LGPL_SOURCE |
f3ed775e DG |
20 | #include <popt.h> |
21 | #include <stdio.h> | |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
24 | #include <sys/stat.h> | |
25 | #include <sys/types.h> | |
26 | #include <unistd.h> | |
27 | ||
c399183f | 28 | #include "../command.h" |
f3ed775e | 29 | |
65f25c66 | 30 | #include <common/mi-lttng.h> |
42224349 DG |
31 | #include <common/sessiond-comm/sessiond-comm.h> |
32 | ||
fd076c09 | 33 | static char *opt_session_name; |
b09ee5ba | 34 | static int opt_destroy_all; |
f3ed775e | 35 | |
65f25c66 JRJ |
36 | /* Mi writer */ |
37 | static struct mi_writer *writer; | |
38 | ||
f3ed775e DG |
39 | enum { |
40 | OPT_HELP = 1, | |
679b4943 | 41 | OPT_LIST_OPTIONS, |
f3ed775e DG |
42 | }; |
43 | ||
44 | static struct poptOption long_options[] = { | |
45 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
46 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, | |
b09ee5ba | 47 | {"all", 'a', POPT_ARG_VAL, &opt_destroy_all, 1, 0, 0}, |
679b4943 | 48 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
f3ed775e DG |
49 | {0, 0, 0, 0, 0, 0, 0} |
50 | }; | |
51 | ||
52 | /* | |
53 | * usage | |
54 | */ | |
55 | static void usage(FILE *ofp) | |
56 | { | |
32a6298d | 57 | fprintf(ofp, "usage: lttng destroy [NAME] [OPTIONS]\n"); |
f3ed775e DG |
58 | fprintf(ofp, "\n"); |
59 | fprintf(ofp, "Where NAME is an optional session name. If not specified, lttng will\n"); | |
60 | fprintf(ofp, "get it from the configuration directory (.lttng).\n"); | |
61 | fprintf(ofp, "\n"); | |
32a6298d | 62 | fprintf(ofp, "Options:\n"); |
f3ed775e | 63 | fprintf(ofp, " -h, --help Show this help\n"); |
b09ee5ba | 64 | fprintf(ofp, " -a, --all Destroy all sessions\n"); |
27221701 | 65 | fprintf(ofp, " --list-options Simple listing of options\n"); |
f3ed775e DG |
66 | fprintf(ofp, "\n"); |
67 | } | |
68 | ||
69 | /* | |
b09ee5ba FG |
70 | * destroy_session |
71 | * | |
72 | * Unregister the provided session to the session daemon. On success, removes | |
73 | * the default configuration. | |
f3ed775e | 74 | */ |
65f25c66 | 75 | static int destroy_session(struct lttng_session *session) |
f3ed775e DG |
76 | { |
77 | int ret; | |
f3ed775e | 78 | |
65f25c66 | 79 | ret = lttng_destroy_session(session->name); |
f3ed775e | 80 | if (ret < 0) { |
42224349 | 81 | switch (-ret) { |
f73fabfd | 82 | case LTTNG_ERR_SESS_NOT_FOUND: |
65f25c66 | 83 | WARN("Session name %s not found", session->name); |
42224349 DG |
84 | break; |
85 | default: | |
60e835ca | 86 | ERR("%s", lttng_strerror(ret)); |
42224349 DG |
87 | break; |
88 | } | |
b09ee5ba | 89 | goto error; |
f3ed775e DG |
90 | } |
91 | ||
65f25c66 | 92 | MSG("Session %s destroyed", session->name); |
b09ee5ba | 93 | config_destroy_default(); |
65f25c66 JRJ |
94 | |
95 | if (lttng_opt_mi) { | |
96 | ret = mi_lttng_session(writer, session, 0); | |
97 | if (ret) { | |
98 | ret = CMD_ERROR; | |
99 | goto error; | |
100 | } | |
101 | } | |
102 | ||
f3ed775e | 103 | ret = CMD_SUCCESS; |
b09ee5ba FG |
104 | error: |
105 | return ret; | |
106 | } | |
f3ed775e | 107 | |
b09ee5ba FG |
108 | /* |
109 | * destroy_all_sessions | |
110 | * | |
111 | * Call destroy_sessions for each registered sessions | |
112 | */ | |
65f25c66 | 113 | static int destroy_all_sessions(struct lttng_session *sessions, int count) |
b09ee5ba | 114 | { |
65f25c66 | 115 | int i, ret = CMD_SUCCESS; |
b09ee5ba | 116 | |
b09ee5ba FG |
117 | if (count == 0) { |
118 | MSG("No session found, nothing to do."); | |
60e835ca DG |
119 | } else if (count < 0) { |
120 | ERR("%s", lttng_strerror(ret)); | |
121 | goto error; | |
b09ee5ba | 122 | } |
60e835ca | 123 | |
b09ee5ba | 124 | for (i = 0; i < count; i++) { |
65f25c66 | 125 | ret = destroy_session(&sessions[i]); |
b09ee5ba FG |
126 | if (ret < 0) { |
127 | goto error; | |
128 | } | |
b73d0b29 | 129 | } |
f3ed775e DG |
130 | error: |
131 | return ret; | |
132 | } | |
133 | ||
134 | /* | |
843f5df9 | 135 | * The 'destroy <options>' first level command |
f3ed775e DG |
136 | */ |
137 | int cmd_destroy(int argc, const char **argv) | |
138 | { | |
b09ee5ba | 139 | int opt; |
65f25c66 | 140 | int ret = CMD_SUCCESS , i, command_ret = CMD_SUCCESS, success = 1; |
f3ed775e | 141 | static poptContext pc; |
b09ee5ba | 142 | char *session_name = NULL; |
f3ed775e | 143 | |
65f25c66 JRJ |
144 | struct lttng_session *sessions; |
145 | int count; | |
146 | int found; | |
147 | ||
f3ed775e DG |
148 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
149 | poptReadDefaultConfig(pc, 0); | |
150 | ||
151 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
152 | switch (opt) { | |
153 | case OPT_HELP: | |
ca1c3607 | 154 | usage(stdout); |
b09ee5ba | 155 | break; |
679b4943 SM |
156 | case OPT_LIST_OPTIONS: |
157 | list_cmd_options(stdout, long_options); | |
b09ee5ba | 158 | break; |
f3ed775e DG |
159 | default: |
160 | usage(stderr); | |
161 | ret = CMD_UNDEFINED; | |
b09ee5ba | 162 | break; |
f3ed775e | 163 | } |
b09ee5ba | 164 | goto end; |
f3ed775e DG |
165 | } |
166 | ||
65f25c66 | 167 | /* Mi preparation */ |
c7e35b03 | 168 | if (lttng_opt_mi) { |
65f25c66 JRJ |
169 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); |
170 | if (!writer) { | |
171 | ret = -LTTNG_ERR_NOMEM; | |
172 | goto end; | |
173 | } | |
174 | ||
175 | /* Open command element */ | |
176 | ret = mi_lttng_writer_command_open(writer, | |
177 | mi_lttng_element_command_destroy); | |
178 | if (ret) { | |
179 | ret = CMD_ERROR; | |
180 | goto end; | |
181 | } | |
182 | ||
183 | /* Open output element */ | |
184 | ret = mi_lttng_writer_open_element(writer, | |
185 | mi_lttng_element_command_output); | |
186 | if (ret) { | |
187 | ret = CMD_ERROR; | |
188 | goto end; | |
189 | } | |
190 | ||
191 | /* For validation and semantic purpose we open a sessions element */ | |
192 | ret = mi_lttng_sessions_open(writer); | |
193 | if (ret) { | |
194 | ret = CMD_ERROR; | |
195 | goto end; | |
196 | } | |
197 | } | |
198 | ||
199 | /* Recuperate all sessions for further operation */ | |
200 | count = lttng_list_sessions(&sessions); | |
201 | if (count < 0) { | |
202 | command_ret = count; | |
203 | success = 0; | |
204 | goto mi_closing; | |
c7e35b03 JR |
205 | } |
206 | ||
fd076c09 | 207 | /* Ignore session name in case all sessions are to be destroyed */ |
b09ee5ba | 208 | if (opt_destroy_all) { |
65f25c66 JRJ |
209 | command_ret = destroy_all_sessions(sessions, count); |
210 | if (command_ret) { | |
211 | success = 0; | |
212 | } | |
213 | } else { | |
214 | opt_session_name = (char *) poptGetArg(pc); | |
215 | ||
216 | if (opt_session_name == NULL) { | |
217 | /* No session name specified, lookup default */ | |
218 | session_name = get_session_name(); | |
219 | if (session_name == NULL) { | |
220 | command_ret = CMD_ERROR; | |
221 | success = 0; | |
222 | goto mi_closing; | |
223 | } | |
224 | } else { | |
225 | session_name = opt_session_name; | |
226 | } | |
fd076c09 | 227 | |
65f25c66 JRJ |
228 | /* Find the corresponding lttng_session struct */ |
229 | found = 0; | |
230 | for (i = 0; i < count; i++) { | |
231 | if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) { | |
232 | found = 1; | |
233 | command_ret = destroy_session(&sessions[i]); | |
234 | if (command_ret) { | |
235 | success = 0; | |
236 | } | |
fd076c09 | 237 | |
65f25c66 JRJ |
238 | } |
239 | } | |
240 | ||
241 | if (!found) { | |
242 | ERR("Session name %s not found", session_name); | |
243 | command_ret = LTTNG_ERR_SESS_NOT_FOUND; | |
244 | success = 0; | |
245 | goto mi_closing; | |
246 | } | |
247 | } | |
248 | ||
249 | mi_closing: | |
250 | /* Mi closing */ | |
251 | if (lttng_opt_mi) { | |
252 | /* Close sessions and output element element */ | |
253 | ret = mi_lttng_close_multi_element(writer, 2); | |
254 | if (ret) { | |
fd076c09 | 255 | ret = CMD_ERROR; |
b09ee5ba FG |
256 | goto end; |
257 | } | |
fd076c09 | 258 | |
65f25c66 JRJ |
259 | /* Success ? */ |
260 | ret = mi_lttng_writer_write_element_bool(writer, | |
261 | mi_lttng_element_command_success, success); | |
262 | if (ret) { | |
263 | ret = CMD_ERROR; | |
264 | goto end; | |
265 | } | |
f3ed775e | 266 | |
65f25c66 JRJ |
267 | /* Command element close */ |
268 | ret = mi_lttng_writer_command_close(writer); | |
269 | if (ret) { | |
270 | ret = CMD_ERROR; | |
271 | goto end; | |
272 | } | |
273 | } | |
f3ed775e | 274 | end: |
65f25c66 JRJ |
275 | /* Mi clean-up */ |
276 | if (writer && mi_lttng_writer_destroy(writer)) { | |
277 | /* Preserve original error code */ | |
278 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; | |
279 | } | |
280 | ||
fd076c09 | 281 | if (opt_session_name == NULL) { |
b09ee5ba FG |
282 | free(session_name); |
283 | } | |
fd076c09 | 284 | |
65f25c66 JRJ |
285 | /* Overwrite ret if an error occurred during destroy_session/all */ |
286 | ret = command_ret ? command_ret : ret; | |
287 | ||
fd076c09 | 288 | poptFreeContext(pc); |
f3ed775e DG |
289 | return ret; |
290 | } |