Fix: lttng: poptGetArg doesn't provide string ownership
[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/lttng.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, 0, OPT_HELP, 0, 0},
43 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
44 {"no-wait", 'n', POPT_ARG_VAL, &opt_no_wait, 1, 0, 0},
45 {0, 0, 0, 0, 0, 0, 0}
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 = NULL;
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 = NULL;
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, NULL, &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,
87 &rotation_state);
88 if (rotation_status != LTTNG_ROTATION_STATUS_OK) {
89 MSG("");
90 ERR("Failed to query the state of the rotation.");
91 goto error;
92 }
93
94 if (rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
95 ret = usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US);
96 if (ret) {
97 PERROR("\nusleep");
98 goto error;
99 }
100 _MSG(".");
101
102 ret = fflush(stdout);
103 if (ret) {
104 PERROR("\nfflush");
105 goto error;
106 }
107 }
108 } while (rotation_state == LTTNG_ROTATION_STATE_ONGOING);
109 MSG("");
110
111 skip_wait:
112 switch (rotation_state) {
113 case LTTNG_ROTATION_STATE_COMPLETED:
114 rotation_status = lttng_rotation_handle_get_archive_location(
115 handle, &location);
116 if (rotation_status != LTTNG_ROTATION_STATUS_OK) {
117 ERR("Failed to retrieve the rotation's completed chunk archive location.");
118 cmd_ret = CMD_ERROR;
119 }
120 break;
121 case LTTNG_ROTATION_STATE_EXPIRED:
122 break;
123 case LTTNG_ROTATION_STATE_ERROR:
124 ERR("Failed to retrieve rotation state.");
125 goto error;
126 case LTTNG_ROTATION_STATE_ONGOING:
127 MSG("Rotation ongoing for session %s", session_name);
128 print_location = false;
129 break;
130 default:
131 ERR("Unexpected rotation state encountered.");
132 goto error;
133 }
134
135 if (!lttng_opt_mi && print_location) {
136 ret = print_trace_archive_location(location,
137 session_name);
138 } else if (lttng_opt_mi) {
139 ret = mi_lttng_rotate(writer, session_name, rotation_state,
140 location);
141 }
142
143 if (ret < 0) {
144 cmd_ret = CMD_ERROR;
145 }
146
147 end:
148 lttng_rotation_handle_destroy(handle);
149 return cmd_ret;
150 error:
151 cmd_ret = CMD_ERROR;
152 goto end;
153 }
154
155 /*
156 * cmd_rotate
157 *
158 * The 'rotate <options>' first level command
159 */
160 int cmd_rotate(int argc, const char **argv)
161 {
162 int opt, ret;
163 enum cmd_error_code cmd_ret = CMD_SUCCESS;
164 int popt_ret;
165 static poptContext pc;
166 const char *arg_session_name = NULL;
167 char *session_name = NULL;
168
169 pc = poptGetContext(NULL, argc, argv, long_options, 0);
170 popt_ret = poptReadDefaultConfig(pc, 0);
171 if (popt_ret) {
172 ERR("poptReadDefaultConfig");
173 cmd_ret = CMD_ERROR;
174 goto end;
175 }
176
177 while ((opt = poptGetNextOpt(pc)) != -1) {
178 switch (opt) {
179 case OPT_HELP:
180 SHOW_HELP();
181 goto end;
182 case OPT_LIST_OPTIONS:
183 list_cmd_options(stdout, long_options);
184 goto end;
185 default:
186 cmd_ret = CMD_UNDEFINED;
187 goto end;
188 }
189 }
190
191 arg_session_name = poptGetArg(pc);
192 if (arg_session_name == NULL) {
193 session_name = get_session_name();
194 } else {
195 session_name = strdup(arg_session_name);
196 if (session_name == NULL) {
197 PERROR("Failed to copy session name");
198 }
199 }
200
201 if (session_name == NULL) {
202 cmd_ret = CMD_ERROR;
203 goto end;
204 }
205
206 /* Mi check */
207 if (lttng_opt_mi) {
208 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
209 if (!writer) {
210 cmd_ret = CMD_ERROR;
211 goto end;
212 }
213
214 /* Open rotate command */
215 ret = mi_lttng_writer_command_open(writer,
216 mi_lttng_element_command_rotate);
217 if (ret) {
218 cmd_ret = CMD_ERROR;
219 goto end;
220 }
221
222 /* Open output element */
223 ret = mi_lttng_writer_open_element(writer,
224 mi_lttng_element_command_output);
225 if (ret) {
226 cmd_ret = CMD_ERROR;
227 goto end;
228 }
229 }
230
231 cmd_ret = rotate_tracing(session_name);
232
233 /* Mi closing */
234 if (lttng_opt_mi) {
235 /* Close output element */
236 ret = mi_lttng_writer_close_element(writer);
237 if (ret) {
238 cmd_ret = CMD_ERROR;
239 goto end;
240 }
241 /* Success ? */
242 ret = mi_lttng_writer_write_element_bool(writer,
243 mi_lttng_element_command_success,
244 cmd_ret == CMD_SUCCESS);
245 if (ret) {
246 cmd_ret = CMD_ERROR;
247 goto end;
248 }
249
250 /* Command element close */
251 ret = mi_lttng_writer_command_close(writer);
252 if (ret) {
253 cmd_ret = CMD_ERROR;
254 goto end;
255 }
256 }
257
258 /* Mi clean-up */
259 if (writer && mi_lttng_writer_destroy(writer)) {
260 cmd_ret = CMD_ERROR;
261 goto end;
262 }
263 end:
264 free(session_name);
265 poptFreeContext(pc);
266
267 return cmd_ret;
268 }
This page took 0.033687 seconds and 4 git commands to generate.