Build: bump autoconf version requirement to 2.64
[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 _GNU_SOURCE
19 #define _LGPL_SOURCE
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
28 #include "../command.h"
29
30 #include <common/mi-lttng.h>
31 #include <common/sessiond-comm/sessiond-comm.h>
32
33 static char *opt_session_name;
34 static int opt_destroy_all;
35
36 /* Mi writer */
37 static struct mi_writer *writer;
38
39 enum {
40 OPT_HELP = 1,
41 OPT_LIST_OPTIONS,
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},
47 {"all", 'a', POPT_ARG_VAL, &opt_destroy_all, 1, 0, 0},
48 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
49 {0, 0, 0, 0, 0, 0, 0}
50 };
51
52 /*
53 * usage
54 */
55 static void usage(FILE *ofp)
56 {
57 fprintf(ofp, "usage: lttng destroy [NAME] [OPTIONS]\n");
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");
62 fprintf(ofp, "Options:\n");
63 fprintf(ofp, " -h, --help Show this help\n");
64 fprintf(ofp, " -a, --all Destroy all sessions\n");
65 fprintf(ofp, " --list-options Simple listing of options\n");
66 fprintf(ofp, "\n");
67 }
68
69 /*
70 * destroy_session
71 *
72 * Unregister the provided session to the session daemon. On success, removes
73 * the default configuration.
74 */
75 static int destroy_session(struct lttng_session *session)
76 {
77 int ret;
78
79 ret = lttng_destroy_session(session->name);
80 if (ret < 0) {
81 switch (-ret) {
82 case LTTNG_ERR_SESS_NOT_FOUND:
83 WARN("Session name %s not found", session->name);
84 break;
85 default:
86 ERR("%s", lttng_strerror(ret));
87 break;
88 }
89 goto error;
90 }
91
92 MSG("Session %s destroyed", session->name);
93 config_destroy_default();
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
103 ret = CMD_SUCCESS;
104 error:
105 return ret;
106 }
107
108 /*
109 * destroy_all_sessions
110 *
111 * Call destroy_sessions for each registered sessions
112 */
113 static int destroy_all_sessions(struct lttng_session *sessions, int count)
114 {
115 int i, ret = CMD_SUCCESS;
116
117 if (count == 0) {
118 MSG("No session found, nothing to do.");
119 } else if (count < 0) {
120 ERR("%s", lttng_strerror(ret));
121 goto error;
122 }
123
124 for (i = 0; i < count; i++) {
125 ret = destroy_session(&sessions[i]);
126 if (ret < 0) {
127 goto error;
128 }
129 }
130 error:
131 return ret;
132 }
133
134 /*
135 * The 'destroy <options>' first level command
136 */
137 int cmd_destroy(int argc, const char **argv)
138 {
139 int opt;
140 int ret = CMD_SUCCESS , i, command_ret = CMD_SUCCESS, success = 1;
141 static poptContext pc;
142 char *session_name = NULL;
143
144 struct lttng_session *sessions;
145 int count;
146 int found;
147
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:
154 usage(stdout);
155 break;
156 case OPT_LIST_OPTIONS:
157 list_cmd_options(stdout, long_options);
158 break;
159 default:
160 usage(stderr);
161 ret = CMD_UNDEFINED;
162 break;
163 }
164 goto end;
165 }
166
167 /* Mi preparation */
168 if (lttng_opt_mi) {
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;
205 }
206
207 /* Ignore session name in case all sessions are to be destroyed */
208 if (opt_destroy_all) {
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 }
227
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 }
237
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) {
255 ret = CMD_ERROR;
256 goto end;
257 }
258
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 }
266
267 /* Command element close */
268 ret = mi_lttng_writer_command_close(writer);
269 if (ret) {
270 ret = CMD_ERROR;
271 goto end;
272 }
273 }
274 end:
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
281 if (opt_session_name == NULL) {
282 free(session_name);
283 }
284
285 /* Overwrite ret if an error occurred during destroy_session/all */
286 ret = command_ret ? command_ret : ret;
287
288 poptFreeContext(pc);
289 return ret;
290 }
This page took 0.035443 seconds and 5 git commands to generate.