Fix: Exclusion check iterates multiple times on same array
[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
DG
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:
4ba92f18 182 SHOW_HELP();
f3ed775e 183 goto end;
679b4943
SM
184 case OPT_LIST_OPTIONS:
185 list_cmd_options(stdout, long_options);
679b4943 186 goto end;
f3ed775e 187 default:
f3ed775e
DG
188 ret = CMD_UNDEFINED;
189 goto end;
190 }
191 }
192
e5b83100
JRJ
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
f3ed775e
DG
229 opt_session_name = (char*) poptGetArg(pc);
230
e5b83100
JRJ
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 }
f3ed775e
DG
260
261end:
e5b83100
JRJ
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
ca1c3607 271 poptFreeContext(pc);
f3ed775e
DG
272 return ret;
273}
This page took 0.068595 seconds and 4 git commands to generate.