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