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