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