Fix: lttng-snapshot: use after free of max size argument
[lttng-tools.git] / src / bin / lttng / commands / stop.cpp
CommitLineData
f3ed775e 1/*
21cf9b6b 2 * Copyright (C) 2011 EfficiOS Inc.
f3ed775e 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
f3ed775e 5 *
f3ed775e
DG
6 */
7
6c1c0768 8#define _LGPL_SOURCE
f3ed775e
DG
9#include <popt.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <sys/stat.h>
14#include <sys/types.h>
15#include <unistd.h>
f3ed775e 16
c9e313bc
SM
17#include <common/sessiond-comm/sessiond-comm.hpp>
18#include <common/mi-lttng.hpp>
e5b83100 19
c9e313bc 20#include "../command.hpp"
42224349 21
f3ed775e 22static char *opt_session_name;
38ee087f 23static int opt_no_wait;
e5b83100 24static struct mi_writer *writer;
f3ed775e 25
4fc83d94
PP
26#ifdef LTTNG_EMBED_HELP
27static const char help_msg[] =
28#include <lttng-stop.1.h>
29;
30#endif
31
f3ed775e
DG
32enum {
33 OPT_HELP = 1,
679b4943 34 OPT_LIST_OPTIONS,
f3ed775e
DG
35};
36
37static struct poptOption long_options[] = {
38 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
39 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
679b4943 40 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
38ee087f 41 {"no-wait", 'n', POPT_ARG_VAL, &opt_no_wait, 1, 0, 0},
f3ed775e
DG
42 {0, 0, 0, 0, 0, 0, 0}
43};
44
e5b83100
JRJ
45/*
46 * Mi print of partial session
47 */
48static int mi_print_session(char *session_name, int enabled)
49{
50 int ret;
a0377dfe
FD
51 LTTNG_ASSERT(writer);
52 LTTNG_ASSERT(session_name);
e5b83100
JRJ
53
54 /* Open session element */
55 ret = mi_lttng_writer_open_element(writer, config_element_session);
56 if (ret) {
57 goto end;
58 }
59
60 /* Print session name element */
61 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
62 session_name);
63 if (ret) {
64 goto end;
65 }
66
67 /* Is enabled ? */
68 ret = mi_lttng_writer_write_element_bool(writer, config_element_enabled,
69 enabled);
70 if (ret) {
71 goto end;
72 }
73
74 /* Close session element */
75 ret = mi_lttng_writer_close_element(writer);
76
77end:
78 return ret;
79}
f3ed775e
DG
80
81/*
cd80958d 82 * Start tracing for all trace of the session.
f3ed775e
DG
83 */
84static int stop_tracing(void)
85{
ae856491 86 int ret;
f3ed775e
DG
87 char *session_name;
88
89 if (opt_session_name == NULL) {
90 session_name = get_session_name();
91 if (session_name == NULL) {
92 ret = CMD_ERROR;
93 goto error;
94 }
95 } else {
96 session_name = opt_session_name;
97 }
98
8eb7a5e2 99 ret = lttng_stop_tracing_no_wait(session_name);
f3ed775e 100 if (ret < 0) {
42224349 101 switch (-ret) {
f73fabfd 102 case LTTNG_ERR_TRACE_ALREADY_STOPPED:
42224349
DG
103 WARN("Tracing already stopped for session %s", session_name);
104 break;
105 default:
106 ERR("%s", lttng_strerror(ret));
107 break;
108 }
f3ed775e
DG
109 goto free_name;
110 }
111
8eb7a5e2
JG
112 if (!opt_no_wait) {
113 _MSG("Waiting for data availability");
114 fflush(stdout);
115 do {
116 ret = lttng_data_pending(session_name);
117 if (ret < 0) {
118 /* Return the data available call error. */
e5b83100 119 goto free_name;
8eb7a5e2
JG
120 }
121
122 /*
123 * Data sleep time before retrying (in usec). Don't sleep if the call
124 * returned value indicates availability.
125 */
126 if (ret) {
c8f61fd4 127 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US);
8eb7a5e2
JG
128 _MSG(".");
129 fflush(stdout);
130 }
131 } while (ret != 0);
132 MSG("");
133 }
134
ae856491
DG
135 ret = CMD_SUCCESS;
136
20fb9e02 137 print_session_stats(session_name);
f3ed775e 138 MSG("Tracing stopped for session %s", session_name);
e5b83100
JRJ
139 if (lttng_opt_mi) {
140 ret = mi_print_session(session_name, 0);
141 if (ret) {
142 goto free_name;
143 }
144 }
f3ed775e
DG
145
146free_name:
b73d0b29
MD
147 if (opt_session_name == NULL) {
148 free(session_name);
149 }
cd80958d 150
f3ed775e
DG
151error:
152 return ret;
153}
154
155/*
156 * cmd_stop
157 *
158 * The 'stop <options>' first level command
159 */
160int cmd_stop(int argc, const char **argv)
161{
e5b83100 162 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
f3ed775e 163 static poptContext pc;
68c7f6e5 164 const char *leftover = NULL;
f3ed775e
DG
165
166 pc = poptGetContext(NULL, argc, argv, long_options, 0);
167 poptReadDefaultConfig(pc, 0);
168
169 while ((opt = poptGetNextOpt(pc)) != -1) {
170 switch (opt) {
171 case OPT_HELP:
4ba92f18 172 SHOW_HELP();
f3ed775e 173 goto end;
679b4943
SM
174 case OPT_LIST_OPTIONS:
175 list_cmd_options(stdout, long_options);
679b4943 176 goto end;
f3ed775e 177 default:
f3ed775e
DG
178 ret = CMD_UNDEFINED;
179 goto end;
180 }
181 }
182
e5b83100
JRJ
183 /* Mi check */
184 if (lttng_opt_mi) {
185 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
186 if (!writer) {
187 ret = -LTTNG_ERR_NOMEM;
188 goto end;
189 }
190
191 /* Open command element */
192 ret = mi_lttng_writer_command_open(writer,
193 mi_lttng_element_command_stop);
194 if (ret) {
195 ret = CMD_ERROR;
196 goto end;
197 }
198
199 /* Open output element */
200 ret = mi_lttng_writer_open_element(writer,
201 mi_lttng_element_command_output);
202 if (ret) {
203 ret = CMD_ERROR;
204 goto end;
205 }
206
207 /*
208 * Open sessions element
209 * For validation
210 */
211 ret = mi_lttng_writer_open_element(writer,
212 config_element_sessions);
213 if (ret) {
214 ret = CMD_ERROR;
215 goto end;
216 }
217 }
218
f3ed775e
DG
219 opt_session_name = (char*) poptGetArg(pc);
220
68c7f6e5
JD
221 leftover = poptGetArg(pc);
222 if (leftover) {
223 ERR("Unknown argument: %s", leftover);
224 ret = CMD_ERROR;
225 goto end;
226 }
227
e5b83100
JRJ
228 command_ret = stop_tracing();
229 if (command_ret) {
230 success = 0;
231 }
232
233 /* Mi closing */
234 if (lttng_opt_mi) {
235 /* Close sessions and output element */
236 ret = mi_lttng_close_multi_element(writer, 2);
237 if (ret) {
238 ret = CMD_ERROR;
239 goto end;
240 }
241
242 /* Success ? */
243 ret = mi_lttng_writer_write_element_bool(writer,
244 mi_lttng_element_command_success, success);
245 if (ret) {
246 ret = CMD_ERROR;
247 goto end;
248 }
249
250 /* Command element close */
251 ret = mi_lttng_writer_command_close(writer);
252 if (ret) {
253 ret = CMD_ERROR;
254 goto end;
255 }
256 }
f3ed775e
DG
257
258end:
e5b83100
JRJ
259 /* Mi clean-up */
260 if (writer && mi_lttng_writer_destroy(writer)) {
261 /* Preserve original error code */
262 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
263 }
264
265 /* Overwrite ret if an error occurred in stop_tracing() */
266 ret = command_ret ? command_ret : ret;
267
ca1c3607 268 poptFreeContext(pc);
f3ed775e
DG
269 return ret;
270}
This page took 0.074567 seconds and 4 git commands to generate.