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