Port: Remove _GNU_SOURCE, defined in config.h
[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
27 #include "../command.h"
28
29 #include <common/mi-lttng.h>
30 #include <common/sessiond-comm/sessiond-comm.h>
31 #include <common/utils.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 char *session_name = NULL;
79
80 ret = lttng_destroy_session(session->name);
81 if (ret < 0) {
82 switch (-ret) {
83 case LTTNG_ERR_SESS_NOT_FOUND:
84 WARN("Session name %s not found", session->name);
85 break;
86 default:
87 ERR("%s", lttng_strerror(ret));
88 break;
89 }
90 goto error;
91 }
92
93 MSG("Session %s destroyed", session->name);
94
95 session_name = get_session_name_quiet();
96 if (session_name && !strncmp(session->name, session_name, NAME_MAX)) {
97 config_destroy_default();
98 }
99
100 if (lttng_opt_mi) {
101 ret = mi_lttng_session(writer, session, 0);
102 if (ret) {
103 ret = CMD_ERROR;
104 goto error;
105 }
106 }
107
108 ret = CMD_SUCCESS;
109 error:
110 free(session_name);
111 return ret;
112 }
113
114 /*
115 * destroy_all_sessions
116 *
117 * Call destroy_sessions for each registered sessions
118 */
119 static int destroy_all_sessions(struct lttng_session *sessions, int count)
120 {
121 int i, ret = CMD_SUCCESS;
122
123 if (count == 0) {
124 MSG("No session found, nothing to do.");
125 } else if (count < 0) {
126 ERR("%s", lttng_strerror(ret));
127 goto error;
128 }
129
130 for (i = 0; i < count; i++) {
131 ret = destroy_session(&sessions[i]);
132 if (ret < 0) {
133 goto error;
134 }
135 }
136 error:
137 return ret;
138 }
139
140 /*
141 * The 'destroy <options>' first level command
142 */
143 int cmd_destroy(int argc, const char **argv)
144 {
145 int opt;
146 int ret = CMD_SUCCESS , i, command_ret = CMD_SUCCESS, success = 1;
147 static poptContext pc;
148 char *session_name = NULL;
149
150 struct lttng_session *sessions;
151 int count;
152 int found;
153
154 pc = poptGetContext(NULL, argc, argv, long_options, 0);
155 poptReadDefaultConfig(pc, 0);
156
157 while ((opt = poptGetNextOpt(pc)) != -1) {
158 switch (opt) {
159 case OPT_HELP:
160 usage(stdout);
161 break;
162 case OPT_LIST_OPTIONS:
163 list_cmd_options(stdout, long_options);
164 break;
165 default:
166 usage(stderr);
167 ret = CMD_UNDEFINED;
168 break;
169 }
170 goto end;
171 }
172
173 /* Mi preparation */
174 if (lttng_opt_mi) {
175 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
176 if (!writer) {
177 ret = -LTTNG_ERR_NOMEM;
178 goto end;
179 }
180
181 /* Open command element */
182 ret = mi_lttng_writer_command_open(writer,
183 mi_lttng_element_command_destroy);
184 if (ret) {
185 ret = CMD_ERROR;
186 goto end;
187 }
188
189 /* Open output element */
190 ret = mi_lttng_writer_open_element(writer,
191 mi_lttng_element_command_output);
192 if (ret) {
193 ret = CMD_ERROR;
194 goto end;
195 }
196
197 /* For validation and semantic purpose we open a sessions element */
198 ret = mi_lttng_sessions_open(writer);
199 if (ret) {
200 ret = CMD_ERROR;
201 goto end;
202 }
203 }
204
205 /* Recuperate all sessions for further operation */
206 count = lttng_list_sessions(&sessions);
207 if (count < 0) {
208 command_ret = count;
209 success = 0;
210 goto mi_closing;
211 }
212
213 /* Ignore session name in case all sessions are to be destroyed */
214 if (opt_destroy_all) {
215 command_ret = destroy_all_sessions(sessions, count);
216 if (command_ret) {
217 success = 0;
218 }
219 } else {
220 opt_session_name = (char *) poptGetArg(pc);
221
222 if (!opt_session_name) {
223 /* No session name specified, lookup default */
224 session_name = get_session_name();
225 if (session_name == NULL) {
226 command_ret = CMD_ERROR;
227 success = 0;
228 goto mi_closing;
229 }
230 } else {
231 session_name = opt_session_name;
232 }
233
234 /* Find the corresponding lttng_session struct */
235 found = 0;
236 for (i = 0; i < count; i++) {
237 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
238 found = 1;
239 command_ret = destroy_session(&sessions[i]);
240 if (command_ret) {
241 success = 0;
242 }
243
244 }
245 }
246
247 if (!found) {
248 ERR("Session name %s not found", session_name);
249 command_ret = LTTNG_ERR_SESS_NOT_FOUND;
250 success = 0;
251 goto mi_closing;
252 }
253 }
254
255 mi_closing:
256 /* Mi closing */
257 if (lttng_opt_mi) {
258 /* Close sessions and output element element */
259 ret = mi_lttng_close_multi_element(writer, 2);
260 if (ret) {
261 ret = CMD_ERROR;
262 goto end;
263 }
264
265 /* Success ? */
266 ret = mi_lttng_writer_write_element_bool(writer,
267 mi_lttng_element_command_success, success);
268 if (ret) {
269 ret = CMD_ERROR;
270 goto end;
271 }
272
273 /* Command element close */
274 ret = mi_lttng_writer_command_close(writer);
275 if (ret) {
276 ret = CMD_ERROR;
277 goto end;
278 }
279 }
280 end:
281 /* Mi clean-up */
282 if (writer && mi_lttng_writer_destroy(writer)) {
283 /* Preserve original error code */
284 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
285 }
286
287 if (opt_session_name == NULL) {
288 free(session_name);
289 }
290
291 /* Overwrite ret if an error occurred during destroy_session/all */
292 ret = command_ret ? command_ret : ret;
293
294 poptFreeContext(pc);
295 return ret;
296 }
This page took 0.036894 seconds and 5 git commands to generate.