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