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