Fix: incorrect error message on metadata missing argument
[lttng-tools.git] / src / bin / lttng / commands / stop.c
CommitLineData
f3ed775e
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
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.
f3ed775e
DG
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 *
d14d33bf
AM
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.
f3ed775e
DG
16 */
17
6c1c0768 18#define _LGPL_SOURCE
f3ed775e
DG
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>
e5b83100 26#include <assert.h>
f3ed775e 27
42224349 28#include <common/sessiond-comm/sessiond-comm.h>
e5b83100
JRJ
29#include <common/mi-lttng.h>
30
31#include "../command.h"
42224349 32
f3ed775e 33static char *opt_session_name;
38ee087f 34static int opt_no_wait;
e5b83100 35static struct mi_writer *writer;
f3ed775e 36
4fc83d94
PP
37#ifdef LTTNG_EMBED_HELP
38static const char help_msg[] =
39#include <lttng-stop.1.h>
40;
41#endif
42
f3ed775e
DG
43enum {
44 OPT_HELP = 1,
679b4943 45 OPT_LIST_OPTIONS,
f3ed775e
DG
46};
47
48static struct poptOption long_options[] = {
49 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
50 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
679b4943 51 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
38ee087f 52 {"no-wait", 'n', POPT_ARG_VAL, &opt_no_wait, 1, 0, 0},
f3ed775e
DG
53 {0, 0, 0, 0, 0, 0, 0}
54};
55
e5b83100
JRJ
56/*
57 * Mi print of partial session
58 */
59static 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
88end:
89 return ret;
90}
f3ed775e
DG
91
92/*
cd80958d 93 * Start tracing for all trace of the session.
f3ed775e
DG
94 */
95static int stop_tracing(void)
96{
ae856491 97 int ret;
f3ed775e
DG
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
8eb7a5e2 110 ret = lttng_stop_tracing_no_wait(session_name);
f3ed775e 111 if (ret < 0) {
42224349 112 switch (-ret) {
f73fabfd 113 case LTTNG_ERR_TRACE_ALREADY_STOPPED:
42224349
DG
114 WARN("Tracing already stopped for session %s", session_name);
115 break;
116 default:
117 ERR("%s", lttng_strerror(ret));
118 break;
119 }
f3ed775e
DG
120 goto free_name;
121 }
122
8eb7a5e2
JG
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. */
e5b83100 130 goto free_name;
8eb7a5e2
JG
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
ae856491
DG
146 ret = CMD_SUCCESS;
147
20fb9e02 148 print_session_stats(session_name);
f3ed775e 149 MSG("Tracing stopped for session %s", session_name);
e5b83100
JRJ
150 if (lttng_opt_mi) {
151 ret = mi_print_session(session_name, 0);
152 if (ret) {
153 goto free_name;
154 }
155 }
f3ed775e
DG
156
157free_name:
b73d0b29
MD
158 if (opt_session_name == NULL) {
159 free(session_name);
160 }
cd80958d 161
f3ed775e
DG
162error:
163 return ret;
164}
165
166/*
167 * cmd_stop
168 *
169 * The 'stop <options>' first level command
170 */
171int cmd_stop(int argc, const char **argv)
172{
e5b83100 173 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
f3ed775e 174 static poptContext pc;
68c7f6e5 175 const char *leftover = NULL;
f3ed775e
DG
176
177 pc = poptGetContext(NULL, argc, argv, long_options, 0);
178 poptReadDefaultConfig(pc, 0);
179
180 while ((opt = poptGetNextOpt(pc)) != -1) {
181 switch (opt) {
182 case OPT_HELP:
4ba92f18 183 SHOW_HELP();
f3ed775e 184 goto end;
679b4943
SM
185 case OPT_LIST_OPTIONS:
186 list_cmd_options(stdout, long_options);
679b4943 187 goto end;
f3ed775e 188 default:
f3ed775e
DG
189 ret = CMD_UNDEFINED;
190 goto end;
191 }
192 }
193
e5b83100
JRJ
194 /* Mi check */
195 if (lttng_opt_mi) {
196 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
197 if (!writer) {
198 ret = -LTTNG_ERR_NOMEM;
199 goto end;
200 }
201
202 /* Open command element */
203 ret = mi_lttng_writer_command_open(writer,
204 mi_lttng_element_command_stop);
205 if (ret) {
206 ret = CMD_ERROR;
207 goto end;
208 }
209
210 /* Open output element */
211 ret = mi_lttng_writer_open_element(writer,
212 mi_lttng_element_command_output);
213 if (ret) {
214 ret = CMD_ERROR;
215 goto end;
216 }
217
218 /*
219 * Open sessions element
220 * For validation
221 */
222 ret = mi_lttng_writer_open_element(writer,
223 config_element_sessions);
224 if (ret) {
225 ret = CMD_ERROR;
226 goto end;
227 }
228 }
229
f3ed775e
DG
230 opt_session_name = (char*) poptGetArg(pc);
231
68c7f6e5
JD
232 leftover = poptGetArg(pc);
233 if (leftover) {
234 ERR("Unknown argument: %s", leftover);
235 ret = CMD_ERROR;
236 goto end;
237 }
238
e5b83100
JRJ
239 command_ret = stop_tracing();
240 if (command_ret) {
241 success = 0;
242 }
243
244 /* Mi closing */
245 if (lttng_opt_mi) {
246 /* Close sessions and output element */
247 ret = mi_lttng_close_multi_element(writer, 2);
248 if (ret) {
249 ret = CMD_ERROR;
250 goto end;
251 }
252
253 /* Success ? */
254 ret = mi_lttng_writer_write_element_bool(writer,
255 mi_lttng_element_command_success, success);
256 if (ret) {
257 ret = CMD_ERROR;
258 goto end;
259 }
260
261 /* Command element close */
262 ret = mi_lttng_writer_command_close(writer);
263 if (ret) {
264 ret = CMD_ERROR;
265 goto end;
266 }
267 }
f3ed775e
DG
268
269end:
e5b83100
JRJ
270 /* Mi clean-up */
271 if (writer && mi_lttng_writer_destroy(writer)) {
272 /* Preserve original error code */
273 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
274 }
275
276 /* Overwrite ret if an error occurred in stop_tracing() */
277 ret = command_ret ? command_ret : ret;
278
ca1c3607 279 poptFreeContext(pc);
f3ed775e
DG
280 return ret;
281}
This page took 0.073185 seconds and 4 git commands to generate.