Fix: lttng-clear: invalid free of session name
[lttng-tools.git] / src / bin / lttng / commands / clear.c
CommitLineData
511ed4e2
JR
1/*
2 * Copyright (C) 2019 - Jonathan Rajotte-Julien <jonathan.rajotte-julien@efficios.com>
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#include <stdbool.h>
27#include <lttng/clear.h>
28#include <lttng/clear-handle.h>
29
30#include "../command.h"
31
32#include <common/mi-lttng.h>
33#include <common/sessiond-comm/sessiond-comm.h>
34#include <common/utils.h>
35
36static int opt_clear_all;
37
38#ifdef LTTNG_EMBED_HELP
39static const char help_msg[] =
40#include <lttng-clear.1.h>
41;
42#endif
43
44/* Mi writer */
45static struct mi_writer *writer;
46
47enum {
48 OPT_HELP = 1,
49 OPT_LIST_OPTIONS,
50};
51
52static struct poptOption long_options[] = {
53 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
54 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
55 {"all", 'a', POPT_ARG_VAL, &opt_clear_all, 1, 0, 0},
56 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
57 {0, 0, 0, 0, 0, 0, 0}
58};
59
60/*
61 * clear session
62 */
63static int clear_session(struct lttng_session *session)
64{
65 enum lttng_clear_handle_status status =
66 LTTNG_CLEAR_HANDLE_STATUS_OK;
67 struct lttng_clear_handle *handle = NULL;
68 enum lttng_error_code ret_code;
69 bool printed_wait_msg = false;
70 char *session_name = NULL;
71 int ret;
72
73 ret = lttng_clear_session(session->name, &handle);
74 if (ret < 0) {
75 ERR("%s", lttng_strerror(ret));
76 goto error;
77 }
78
79 do {
80 status = lttng_clear_handle_wait_for_completion(handle,
81 DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US / USEC_PER_MSEC);
82 switch (status) {
83 case LTTNG_CLEAR_HANDLE_STATUS_TIMEOUT:
84 if (!printed_wait_msg) {
85 _MSG("Waiting for clear of session \"%s\"",
86 session->name);
87 printed_wait_msg = true;
88 }
89 _MSG(".");
90 fflush(stdout);
91 break;
92 case LTTNG_CLEAR_HANDLE_STATUS_COMPLETED:
93 break;
94 default:
95 ERR("Failed to wait for the completion of clear for session \"%s\"",
96 session->name);
97 ret = -1;
98 goto error;
99 }
100 } while (status == LTTNG_CLEAR_HANDLE_STATUS_TIMEOUT);
101
102 status = lttng_clear_handle_get_result(handle, &ret_code);
103 if (status != LTTNG_CLEAR_HANDLE_STATUS_OK) {
104 ERR("Failed to get the result of session clear");
105 ret = -1;
106 goto error;
107 }
108 if (ret_code != LTTNG_OK) {
109 ret = -ret_code;
110 goto error;
111 }
112
113 MSG("%sSession \"%s\" cleared", printed_wait_msg ? "\n" : "",
114 session->name);
115 printed_wait_msg = false;
116
117 if (lttng_opt_mi) {
118 ret = mi_lttng_session(writer, session, 0);
119 if (ret) {
120 ret = CMD_ERROR;
121 goto error;
122 }
123 }
124
125 ret = CMD_SUCCESS;
126error:
127 if (printed_wait_msg) {
128 MSG("");
129 }
130 lttng_clear_handle_destroy(handle);
131 free(session_name);
132 return ret;
133}
134
135/*
136 * clear all sessions
137 *
138 * Call clear_session for each registered sessions
139 */
140static int clear_all_sessions(struct lttng_session *sessions, int count)
141{
142 int i, ret = CMD_SUCCESS;
143
144 if (count == 0) {
145 MSG("No session found, nothing to do.");
146 } else if (count < 0) {
147 ERR("%s", lttng_strerror(ret));
148 goto error;
149 }
150
151 for (i = 0; i < count; i++) {
152 ret = clear_session(&sessions[i]);
153 if (ret < 0) {
154 goto error;
155 }
156 }
157error:
158 return ret;
159}
160
161/*
162 * The 'clear <options>' first level command
163 */
164int cmd_clear(int argc, const char **argv)
165{
166 int opt;
167 int ret = CMD_SUCCESS , i, command_ret = CMD_SUCCESS, success = 1;
168 static poptContext pc;
169 char *session_name = NULL;
170 const char *leftover = NULL;
85391256 171 bool free_session_name = false;
511ed4e2
JR
172 struct lttng_session *sessions = NULL;
173 int count;
174 int found;
175
176 pc = poptGetContext(NULL, argc, argv, long_options, 0);
177 poptReadDefaultConfig(pc, 0);
178
179 while ((opt = poptGetNextOpt(pc)) != -1) {
180 switch (opt) {
181 case OPT_HELP:
182 SHOW_HELP();
183 break;
184 case OPT_LIST_OPTIONS:
185 list_cmd_options(stdout, long_options);
186 break;
187 default:
188 ret = CMD_UNDEFINED;
189 break;
190 }
191 goto end;
192 }
193
194 /* Mi preparation */
195 if (lttng_opt_mi) {
196 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
197 if (!writer) {
198 ret = -LTTNG_ERR_NOMEM;
199 goto end;
200 }
201
202 /* Open command element */
203 ret = mi_lttng_writer_command_open(writer,
204 mi_lttng_element_command_clear);
205 if (ret) {
206 ret = CMD_ERROR;
207 goto end;
208 }
209
210 /* Open output element */
211 ret = mi_lttng_writer_open_element(writer,
212 mi_lttng_element_command_output);
213 if (ret) {
214 ret = CMD_ERROR;
215 goto end;
216 }
217
218 /* For validation and semantic purpose we open a sessions element */
219 ret = mi_lttng_sessions_open(writer);
220 if (ret) {
221 ret = CMD_ERROR;
222 goto end;
223 }
224 }
225
226 if (!opt_clear_all) {
511ed4e2 227 session_name = (char *) poptGetArg(pc);
511ed4e2
JR
228 if (!session_name) {
229 /* No session name specified, lookup default */
230 session_name = get_session_name();
231 if (session_name == NULL) {
232 command_ret = CMD_ERROR;
233 success = 0;
234 goto mi_closing;
235 }
85391256 236 free_session_name = true;
511ed4e2
JR
237 }
238 } else {
239 session_name = NULL;
240 }
241
242 leftover = poptGetArg(pc);
243 if (leftover) {
244 ERR("Unknown argument: %s", leftover);
245 ret = CMD_ERROR;
246 success = 0;
247 goto mi_closing;
248 }
249
250 /* Recuperate all sessions for further operation */
251 count = lttng_list_sessions(&sessions);
252 if (count < 0) {
253 ERR("%s", lttng_strerror(count));
254 command_ret = CMD_ERROR;
255 success = 0;
256 goto mi_closing;
257 }
258
259 /* Ignore session name in case all sessions are to be cleaned */
260 if (opt_clear_all) {
261 command_ret = clear_all_sessions(sessions, count);
262 if (command_ret) {
263 ERR("%s", lttng_strerror(command_ret));
264 success = 0;
265 }
266 } else {
267 /* Find the corresponding lttng_session struct */
268 found = 0;
269 for (i = 0; i < count; i++) {
270 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
271 found = 1;
272 command_ret = clear_session(&sessions[i]);
273 if (command_ret) {
274 ERR("%s", lttng_strerror(command_ret));
275 success = 0;
276 }
277 }
278 }
279
280 if (!found) {
281 ERR("Session name %s not found", session_name);
282 command_ret = LTTNG_ERR_SESS_NOT_FOUND;
283 success = 0;
284 goto mi_closing;
285 }
286 }
287
288mi_closing:
289 /* Mi closing */
290 if (lttng_opt_mi) {
291 /* Close sessions and output element element */
292 ret = mi_lttng_close_multi_element(writer, 2);
293 if (ret) {
294 ret = CMD_ERROR;
295 goto end;
296 }
297
298 /* Success ? */
299 ret = mi_lttng_writer_write_element_bool(writer,
300 mi_lttng_element_command_success, success);
301 if (ret) {
302 ret = CMD_ERROR;
303 goto end;
304 }
305
306 /* Command element close */
307 ret = mi_lttng_writer_command_close(writer);
308 if (ret) {
309 ret = CMD_ERROR;
310 goto end;
311 }
312 }
313end:
314 /* Mi clean-up */
315 if (writer && mi_lttng_writer_destroy(writer)) {
316 /* Preserve original error code */
317 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
318 }
319
320 free(sessions);
85391256
JG
321 if (free_session_name) {
322 free(session_name);
323 }
511ed4e2
JR
324
325 /* Overwrite ret if an error occurred during clear_session/all */
326 ret = command_ret ? command_ret : ret;
327
328 poptFreeContext(pc);
329 return ret;
330}
This page took 0.036342 seconds and 5 git commands to generate.