Fix: per-pid ust buffers flush race with application unregister
[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
6c1c0768 18#define _LGPL_SOURCE
f3ed775e
DG
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 30#include <common/sessiond-comm/sessiond-comm.h>
1dac0189 31#include <common/utils.h>
42224349 32
fd076c09 33static char *opt_session_name;
b09ee5ba 34static int opt_destroy_all;
f3ed775e 35
65f25c66
JRJ
36/* Mi writer */
37static struct mi_writer *writer;
38
f3ed775e
DG
39enum {
40 OPT_HELP = 1,
679b4943 41 OPT_LIST_OPTIONS,
f3ed775e
DG
42};
43
44static 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 */
55static 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 75static int destroy_session(struct lttng_session *session)
f3ed775e
DG
76{
77 int ret;
1dac0189 78 char *session_name = NULL;
f3ed775e 79
65f25c66 80 ret = lttng_destroy_session(session->name);
f3ed775e 81 if (ret < 0) {
42224349 82 switch (-ret) {
f73fabfd 83 case LTTNG_ERR_SESS_NOT_FOUND:
65f25c66 84 WARN("Session name %s not found", session->name);
42224349
DG
85 break;
86 default:
60e835ca 87 ERR("%s", lttng_strerror(ret));
42224349
DG
88 break;
89 }
b09ee5ba 90 goto error;
f3ed775e
DG
91 }
92
65f25c66 93 MSG("Session %s destroyed", session->name);
1dac0189
PPM
94
95 session_name = get_session_name_quiet();
96 if (session_name && !strncmp(session->name, session_name, NAME_MAX)) {
97 config_destroy_default();
98 }
65f25c66
JRJ
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
f3ed775e 108 ret = CMD_SUCCESS;
b09ee5ba 109error:
1dac0189 110 free(session_name);
b09ee5ba
FG
111 return ret;
112}
f3ed775e 113
b09ee5ba
FG
114/*
115 * destroy_all_sessions
116 *
117 * Call destroy_sessions for each registered sessions
118 */
65f25c66 119static int destroy_all_sessions(struct lttng_session *sessions, int count)
b09ee5ba 120{
65f25c66 121 int i, ret = CMD_SUCCESS;
b09ee5ba 122
b09ee5ba
FG
123 if (count == 0) {
124 MSG("No session found, nothing to do.");
60e835ca
DG
125 } else if (count < 0) {
126 ERR("%s", lttng_strerror(ret));
127 goto error;
b09ee5ba 128 }
60e835ca 129
b09ee5ba 130 for (i = 0; i < count; i++) {
65f25c66 131 ret = destroy_session(&sessions[i]);
b09ee5ba
FG
132 if (ret < 0) {
133 goto error;
134 }
b73d0b29 135 }
f3ed775e
DG
136error:
137 return ret;
138}
139
140/*
843f5df9 141 * The 'destroy <options>' first level command
f3ed775e
DG
142 */
143int cmd_destroy(int argc, const char **argv)
144{
b09ee5ba 145 int opt;
65f25c66 146 int ret = CMD_SUCCESS , i, command_ret = CMD_SUCCESS, success = 1;
f3ed775e 147 static poptContext pc;
b09ee5ba 148 char *session_name = NULL;
f3ed775e 149
65f25c66
JRJ
150 struct lttng_session *sessions;
151 int count;
152 int found;
153
f3ed775e
DG
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:
ca1c3607 160 usage(stdout);
b09ee5ba 161 break;
679b4943
SM
162 case OPT_LIST_OPTIONS:
163 list_cmd_options(stdout, long_options);
b09ee5ba 164 break;
f3ed775e
DG
165 default:
166 usage(stderr);
167 ret = CMD_UNDEFINED;
b09ee5ba 168 break;
f3ed775e 169 }
b09ee5ba 170 goto end;
f3ed775e
DG
171 }
172
65f25c66 173 /* Mi preparation */
c7e35b03 174 if (lttng_opt_mi) {
65f25c66
JRJ
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;
c7e35b03
JR
211 }
212
fd076c09 213 /* Ignore session name in case all sessions are to be destroyed */
b09ee5ba 214 if (opt_destroy_all) {
65f25c66
JRJ
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
1dac0189 222 if (!opt_session_name) {
65f25c66
JRJ
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 }
fd076c09 233
65f25c66
JRJ
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 }
fd076c09 243
65f25c66
JRJ
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
255mi_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) {
fd076c09 261 ret = CMD_ERROR;
b09ee5ba
FG
262 goto end;
263 }
fd076c09 264
65f25c66
JRJ
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 }
f3ed775e 272
65f25c66
JRJ
273 /* Command element close */
274 ret = mi_lttng_writer_command_close(writer);
275 if (ret) {
276 ret = CMD_ERROR;
277 goto end;
278 }
279 }
f3ed775e 280end:
65f25c66
JRJ
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
fd076c09 287 if (opt_session_name == NULL) {
b09ee5ba
FG
288 free(session_name);
289 }
fd076c09 290
65f25c66
JRJ
291 /* Overwrite ret if an error occurred during destroy_session/all */
292 ret = command_ret ? command_ret : ret;
293
fd076c09 294 poptFreeContext(pc);
f3ed775e
DG
295 return ret;
296}
This page took 0.082156 seconds and 4 git commands to generate.