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