Clean-up: lttng: only include lttng.h
[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 char *opt_session_name;
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 char *session_name = NULL;
168 bool free_session_name = false;
169
170 pc = poptGetContext(NULL, argc, argv, long_options, 0);
171 popt_ret = poptReadDefaultConfig(pc, 0);
172 if (popt_ret) {
173 ERR("poptReadDefaultConfig");
174 goto error;
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 opt_session_name = (char*) poptGetArg(pc);
192
193 if (!opt_session_name) {
194 session_name = get_session_name();
195 if (!session_name) {
196 goto error;
197 }
198 free_session_name = true;
199 } else {
200 session_name = opt_session_name;
201 }
202
203 /* Mi check */
204 if (lttng_opt_mi) {
205 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
206 if (!writer) {
207 goto error;
208 }
209
210 /* Open rotate command */
211 ret = mi_lttng_writer_command_open(writer,
212 mi_lttng_element_command_rotate);
213 if (ret) {
214 goto error;
215 }
216
217 /* Open output element */
218 ret = mi_lttng_writer_open_element(writer,
219 mi_lttng_element_command_output);
220 if (ret) {
221 goto error;
222 }
223 }
224
225 cmd_ret = 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 goto error;
233 }
234 /* Success ? */
235 ret = mi_lttng_writer_write_element_bool(writer,
236 mi_lttng_element_command_success,
237 cmd_ret == CMD_SUCCESS);
238 if (ret) {
239 goto error;
240 }
241
242 /* Command element close */
243 ret = mi_lttng_writer_command_close(writer);
244 if (ret) {
245 goto error;
246 }
247 }
248
249 /* Mi clean-up */
250 if (writer && mi_lttng_writer_destroy(writer)) {
251 goto error;
252 }
253 end:
254 poptFreeContext(pc);
255 if (free_session_name) {
256 free(session_name);
257 }
258
259 return cmd_ret;
260 error:
261 cmd_ret = CMD_ERROR;
262 goto end;
263 }
This page took 0.033474 seconds and 4 git commands to generate.