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