Cleanup: always use sysconf to get the page size
[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
19 #include <common/sessiond-comm/sessiond-comm.h>
20 #include <common/mi-lttng.h>
21
22 #include "../command.h"
23 #include <lttng/lttng.h>
24
25 static char *opt_session_name;
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 char *session_name = NULL;
167 bool free_session_name = false;
168
169 pc = poptGetContext(NULL, argc, argv, long_options, 0);
170 popt_ret = poptReadDefaultConfig(pc, 0);
171 if (popt_ret) {
172 ERR("poptReadDefaultConfig");
173 goto error;
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 opt_session_name = (char*) poptGetArg(pc);
191
192 if (!opt_session_name) {
193 session_name = get_session_name();
194 if (!session_name) {
195 goto error;
196 }
197 free_session_name = true;
198 } else {
199 session_name = opt_session_name;
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 goto error;
207 }
208
209 /* Open rotate command */
210 ret = mi_lttng_writer_command_open(writer,
211 mi_lttng_element_command_rotate);
212 if (ret) {
213 goto error;
214 }
215
216 /* Open output element */
217 ret = mi_lttng_writer_open_element(writer,
218 mi_lttng_element_command_output);
219 if (ret) {
220 goto error;
221 }
222 }
223
224 cmd_ret = rotate_tracing(session_name);
225
226 /* Mi closing */
227 if (lttng_opt_mi) {
228 /* Close output element */
229 ret = mi_lttng_writer_close_element(writer);
230 if (ret) {
231 goto error;
232 }
233 /* Success ? */
234 ret = mi_lttng_writer_write_element_bool(writer,
235 mi_lttng_element_command_success,
236 cmd_ret == CMD_SUCCESS);
237 if (ret) {
238 goto error;
239 }
240
241 /* Command element close */
242 ret = mi_lttng_writer_command_close(writer);
243 if (ret) {
244 goto error;
245 }
246 }
247
248 /* Mi clean-up */
249 if (writer && mi_lttng_writer_destroy(writer)) {
250 goto error;
251 }
252 end:
253 poptFreeContext(pc);
254 if (free_session_name) {
255 free(session_name);
256 }
257
258 return cmd_ret;
259 error:
260 cmd_ret = CMD_ERROR;
261 goto end;
262 }
This page took 0.033949 seconds and 4 git commands to generate.