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