Print the location of trace chunk produced at session destruction
[lttng-tools.git] / src / bin / lttng / commands / rotate.c
1 /*
2 * Copyright (C) 2017 - Julien Desfossez <jdesfossez@efficios.com>
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 <inttypes.h>
27 #include <ctype.h>
28 #include <assert.h>
29
30 #include <common/sessiond-comm/sessiond-comm.h>
31 #include <common/mi-lttng.h>
32
33 #include "../command.h"
34 #include <lttng/rotation.h>
35 #include <lttng/location.h>
36
37 static char *opt_session_name;
38 static int opt_no_wait;
39 static struct mi_writer *writer;
40
41 #ifdef LTTNG_EMBED_HELP
42 static const char help_msg[] =
43 #include <lttng-rotate.1.h>
44 ;
45 #endif
46
47 enum {
48 OPT_HELP = 1,
49 OPT_LIST_OPTIONS,
50 };
51
52 static struct poptOption long_options[] = {
53 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
54 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
55 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
56 {"no-wait", 'n', POPT_ARG_VAL, &opt_no_wait, 1, 0, 0},
57 {0, 0, 0, 0, 0, 0, 0}
58 };
59
60 static int rotate_tracing(char *session_name)
61 {
62 int ret;
63 enum cmd_error_code cmd_ret = CMD_SUCCESS;
64 struct lttng_rotation_handle *handle = NULL;
65 enum lttng_rotation_status rotation_status;
66 enum lttng_rotation_state rotation_state = LTTNG_ROTATION_STATE_ONGOING;
67 const struct lttng_trace_archive_location *location = NULL;
68 bool print_location = true;
69
70 DBG("Rotating the output files of session %s", session_name);
71
72 ret = lttng_rotate_session(session_name, NULL, &handle);
73 if (ret < 0) {
74 switch (-ret) {
75 case LTTNG_ERR_SESSION_NOT_STARTED:
76 WARN("Tracing session %s not started yet", session_name);
77 cmd_ret = CMD_WARNING;
78 goto end;
79 default:
80 ERR("%s", lttng_strerror(ret));
81 goto error;
82 }
83 }
84
85 if (opt_no_wait) {
86 rotation_state = LTTNG_ROTATION_STATE_ONGOING;
87 goto skip_wait;
88 }
89
90 _MSG("Waiting for rotation to complete");
91 ret = fflush(stdout);
92 if (ret) {
93 PERROR("fflush");
94 goto error;
95 }
96
97 do {
98 rotation_status = lttng_rotation_handle_get_state(handle,
99 &rotation_state);
100 if (rotation_status != LTTNG_ROTATION_STATUS_OK) {
101 MSG("");
102 ERR("Failed to query the state of the rotation.");
103 goto error;
104 }
105
106 if (rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
107 ret = usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME);
108 if (ret) {
109 PERROR("\nusleep");
110 goto error;
111 }
112 _MSG(".");
113
114 ret = fflush(stdout);
115 if (ret) {
116 PERROR("\nfflush");
117 goto error;
118 }
119 }
120 } while (rotation_state == LTTNG_ROTATION_STATE_ONGOING);
121 MSG("");
122
123 skip_wait:
124 switch (rotation_state) {
125 case LTTNG_ROTATION_STATE_COMPLETED:
126 rotation_status = lttng_rotation_handle_get_archive_location(
127 handle, &location);
128 if (rotation_status != LTTNG_ROTATION_STATUS_OK) {
129 ERR("Failed to retrieve the rotation's completed chunk archive location.");
130 cmd_ret = CMD_ERROR;
131 }
132 break;
133 case LTTNG_ROTATION_STATE_EXPIRED:
134 break;
135 case LTTNG_ROTATION_STATE_ERROR:
136 ERR("Failed to retrieve rotation state.");
137 goto error;
138 case LTTNG_ROTATION_STATE_ONGOING:
139 MSG("Rotation ongoing for session %s", session_name);
140 print_location = false;
141 break;
142 default:
143 ERR("Unexpected rotation state encountered.");
144 goto error;
145 }
146
147 if (!lttng_opt_mi && print_location) {
148 ret = print_trace_archive_location(location,
149 session_name);
150 } else if (lttng_opt_mi) {
151 ret = mi_lttng_rotate(writer, session_name, rotation_state,
152 location);
153 }
154
155 if (ret < 0) {
156 cmd_ret = CMD_ERROR;
157 }
158
159 end:
160 lttng_rotation_handle_destroy(handle);
161 return cmd_ret;
162 error:
163 cmd_ret = CMD_ERROR;
164 goto end;
165 }
166
167 /*
168 * cmd_rotate
169 *
170 * The 'rotate <options>' first level command
171 */
172 int cmd_rotate(int argc, const char **argv)
173 {
174 int opt, ret;
175 enum cmd_error_code cmd_ret = CMD_SUCCESS;
176 int popt_ret;
177 static poptContext pc;
178 char *session_name = NULL;
179 bool free_session_name = false;
180
181 pc = poptGetContext(NULL, argc, argv, long_options, 0);
182 popt_ret = poptReadDefaultConfig(pc, 0);
183 if (popt_ret) {
184 ERR("poptReadDefaultConfig");
185 goto error;
186 }
187
188 while ((opt = poptGetNextOpt(pc)) != -1) {
189 switch (opt) {
190 case OPT_HELP:
191 SHOW_HELP();
192 goto end;
193 case OPT_LIST_OPTIONS:
194 list_cmd_options(stdout, long_options);
195 goto end;
196 default:
197 cmd_ret = CMD_UNDEFINED;
198 goto end;
199 }
200 }
201
202 opt_session_name = (char*) poptGetArg(pc);
203
204 if (!opt_session_name) {
205 session_name = get_session_name();
206 if (!session_name) {
207 goto error;
208 }
209 free_session_name = true;
210 } else {
211 session_name = opt_session_name;
212 }
213
214 /* Mi check */
215 if (lttng_opt_mi) {
216 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
217 if (!writer) {
218 goto error;
219 }
220
221 /* Open rotate command */
222 ret = mi_lttng_writer_command_open(writer,
223 mi_lttng_element_command_rotate);
224 if (ret) {
225 goto error;
226 }
227
228 /* Open output element */
229 ret = mi_lttng_writer_open_element(writer,
230 mi_lttng_element_command_output);
231 if (ret) {
232 goto error;
233 }
234 }
235
236 cmd_ret = rotate_tracing(session_name);
237
238 /* Mi closing */
239 if (lttng_opt_mi) {
240 /* Close output element */
241 ret = mi_lttng_writer_close_element(writer);
242 if (ret) {
243 goto error;
244 }
245 /* Success ? */
246 ret = mi_lttng_writer_write_element_bool(writer,
247 mi_lttng_element_command_success,
248 cmd_ret == CMD_SUCCESS);
249 if (ret) {
250 goto error;
251 }
252
253 /* Command element close */
254 ret = mi_lttng_writer_command_close(writer);
255 if (ret) {
256 goto error;
257 }
258 }
259
260 /* Mi clean-up */
261 if (writer && mi_lttng_writer_destroy(writer)) {
262 goto error;
263 }
264 end:
265 poptFreeContext(pc);
266 if (free_session_name) {
267 free(session_name);
268 }
269
270 return cmd_ret;
271 error:
272 cmd_ret = CMD_ERROR;
273 goto end;
274 }
This page took 0.035285 seconds and 5 git commands to generate.