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