Fix: error out on leftover arguments
[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 const char *leftover = NULL;
170
171 pc = poptGetContext(NULL, argc, argv, long_options, 0);
172 poptReadDefaultConfig(pc, 0);
173
174 while ((opt = poptGetNextOpt(pc)) != -1) {
175 switch (opt) {
176 case OPT_HELP:
177 SHOW_HELP();
178 goto end;
179 case OPT_LIST_OPTIONS:
180 list_cmd_options(stdout, long_options);
181 goto end;
182 default:
183 ret = CMD_UNDEFINED;
184 goto end;
185 }
186 }
187
188 /* Mi check */
189 if (lttng_opt_mi) {
190 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
191 if (!writer) {
192 ret = -LTTNG_ERR_NOMEM;
193 goto end;
194 }
195
196 /* Open command element */
197 ret = mi_lttng_writer_command_open(writer,
198 mi_lttng_element_command_stop);
199 if (ret) {
200 ret = CMD_ERROR;
201 goto end;
202 }
203
204 /* Open output element */
205 ret = mi_lttng_writer_open_element(writer,
206 mi_lttng_element_command_output);
207 if (ret) {
208 ret = CMD_ERROR;
209 goto end;
210 }
211
212 /*
213 * Open sessions element
214 * For validation
215 */
216 ret = mi_lttng_writer_open_element(writer,
217 config_element_sessions);
218 if (ret) {
219 ret = CMD_ERROR;
220 goto end;
221 }
222 }
223
224 opt_session_name = (char*) poptGetArg(pc);
225
226 leftover = poptGetArg(pc);
227 if (leftover) {
228 ERR("Unknown argument: %s", leftover);
229 ret = CMD_ERROR;
230 goto end;
231 }
232
233 command_ret = stop_tracing();
234 if (command_ret) {
235 success = 0;
236 }
237
238 /* Mi closing */
239 if (lttng_opt_mi) {
240 /* Close sessions and output element */
241 ret = mi_lttng_close_multi_element(writer, 2);
242 if (ret) {
243 ret = CMD_ERROR;
244 goto end;
245 }
246
247 /* Success ? */
248 ret = mi_lttng_writer_write_element_bool(writer,
249 mi_lttng_element_command_success, success);
250 if (ret) {
251 ret = CMD_ERROR;
252 goto end;
253 }
254
255 /* Command element close */
256 ret = mi_lttng_writer_command_close(writer);
257 if (ret) {
258 ret = CMD_ERROR;
259 goto end;
260 }
261 }
262
263 end:
264 /* Mi clean-up */
265 if (writer && mi_lttng_writer_destroy(writer)) {
266 /* Preserve original error code */
267 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
268 }
269
270 /* Overwrite ret if an error occurred in stop_tracing() */
271 ret = command_ret ? command_ret : ret;
272
273 poptFreeContext(pc);
274 return ret;
275 }
This page took 0.036371 seconds and 5 git commands to generate.