7a6dcbd690b32bef4a708cb4b662a8ae80f9e86b
[lttng-tools.git] / src / lib / lttng-ctl / rotate.c
1 /*
2 * Copyright (C) 2017 - Julien Desfossez <jdesfossez@efficios.com>
3 *
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License, version 2.1 only,
6 * as published by the Free Software Foundation.
7 *
8 * This library is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
11 * for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18 #define _LGPL_SOURCE
19 #include <assert.h>
20 #include <string.h>
21
22 #include <lttng/lttng-error.h>
23 #include <lttng/rotate.h>
24 #include <lttng/rotate-internal.h>
25 #include <common/sessiond-comm/sessiond-comm.h>
26
27 #include "lttng-ctl-helper.h"
28
29 struct lttng_rotate_session_attr *lttng_rotate_session_attr_create(void)
30 {
31 return zmalloc(sizeof(struct lttng_rotate_session_attr));
32 }
33
34 void lttng_rotate_session_attr_destroy(struct lttng_rotate_session_attr *attr)
35 {
36 if (attr) {
37 free(attr);
38 attr = NULL;
39 }
40 }
41
42 int lttng_rotate_session_attr_set_session_name(
43 struct lttng_rotate_session_attr *attr,
44 const char *session_name)
45 {
46 int ret = 0;
47 size_t len;
48
49 if (!attr || !session_name) {
50 ret = -LTTNG_ERR_INVALID;
51 goto error;
52 }
53
54 len = strlen(session_name);
55 if (len >= LTTNG_NAME_MAX) {
56 ret = -LTTNG_ERR_INVALID;
57 goto error;
58 }
59
60 strncpy(attr->session_name, session_name, len);
61
62 error:
63 return ret;
64 }
65
66 enum lttng_rotate_status lttng_rotate_session_get_status(
67 struct lttng_rotate_session_handle *rotate_handle)
68 {
69 if (!rotate_handle) {
70 return LTTNG_ROTATE_ERROR;
71 }
72 return rotate_handle->status;
73 }
74
75 int lttng_rotate_session_get_output_path(
76 struct lttng_rotate_session_handle *rotate_handle,
77 char **path)
78 {
79 int ret;
80
81 *path = zmalloc(PATH_MAX);
82 if (!*path) {
83 ret = -1;
84 goto end;
85 }
86
87 if (rotate_handle->status == LTTNG_ROTATE_COMPLETED) {
88 snprintf(*path, PATH_MAX, "%s", rotate_handle->output_path);
89 ret = 0;
90 } else {
91 ret = -1;
92 }
93
94 end:
95 return ret;
96 }
97
98 void lttng_rotate_session_handle_destroy(
99 struct lttng_rotate_session_handle *rotate_handle)
100 {
101 if (!rotate_handle) {
102 return;
103 }
104 free(rotate_handle);
105 rotate_handle = NULL;
106 }
107
108 static
109 void init_rotate_handle(struct lttng_rotate_session_handle *rotate_handle,
110 struct lttng_rotate_session_return *rotate_return,
111 struct lttng_rotate_session_attr *attr)
112 {
113 snprintf(rotate_handle->session_name, LTTNG_NAME_MAX, "%s",
114 attr->session_name);
115 rotate_handle->rotate_id = rotate_return->rotate_id;
116 rotate_handle->status = rotate_return->status;
117 }
118
119 /*
120 * Rotate the output folder of the session.
121 *
122 * Return 0 on success else a negative LTTng error code.
123 */
124 int lttng_rotate_session(struct lttng_rotate_session_attr *attr,
125 struct lttng_rotate_session_handle **rotate_handle)
126 {
127 struct lttcomm_session_msg lsm;
128 struct lttng_rotate_session_return *rotate_return = NULL;
129 int ret;
130
131 if (!attr) {
132 ret = -LTTNG_ERR_INVALID;
133 goto end;
134 }
135
136 memset(&lsm, 0, sizeof(lsm));
137 lsm.cmd_type = LTTNG_ROTATE_SESSION;
138 lttng_ctl_copy_string(lsm.session.name, attr->session_name,
139 sizeof(lsm.session.name));
140
141 ret = lttng_ctl_ask_sessiond(&lsm, (void **) &rotate_return);
142 fprintf(stderr, "RET: %d\n", ret);
143 if (ret < 0) {
144 *rotate_handle = NULL;
145 goto end;
146 }
147
148 *rotate_handle = zmalloc(sizeof(struct lttng_rotate_session_handle));
149 if (!*rotate_handle) {
150 ret = -LTTNG_ERR_NOMEM;
151 goto end;
152 }
153
154 init_rotate_handle(*rotate_handle, rotate_return, attr);
155
156 end:
157 free(rotate_return);
158 return ret;
159 }
160
161 /*
162 * Ask the session daemon if the current rotation is complete.
163 * If it is, return 0 and populate the output_path with the path of the
164 * rotated chunk. Return 1 if the rotation is pending.
165 */
166 int lttng_rotate_session_pending(
167 struct lttng_rotate_session_handle *rotate_handle)
168 {
169 /* lsm.rotate_pending.rotate_id */
170 struct lttcomm_session_msg lsm;
171 struct lttng_rotate_session_attr attr;
172 int ret;
173 struct lttng_rotate_pending_return *pending_return = NULL;
174
175 snprintf(attr.session_name, LTTNG_NAME_MAX, "%s",
176 rotate_handle->session_name);
177
178 memset(&lsm, 0, sizeof(lsm));
179 lsm.cmd_type = LTTNG_ROTATE_PENDING;
180 lsm.u.rotate_pending.rotate_id = rotate_handle->rotate_id;
181 lttng_ctl_copy_string(lsm.session.name, attr.session_name,
182 sizeof(lsm.session.name));
183
184 ret = lttng_ctl_ask_sessiond(&lsm, (void **) &pending_return);
185 if (ret < 0) {
186 rotate_handle->status = LTTNG_ROTATE_ERROR;
187 goto end;
188 }
189
190 rotate_handle->status = pending_return->status;
191 if (pending_return->status == LTTNG_ROTATE_COMPLETED) {
192 snprintf(rotate_handle->output_path, PATH_MAX, "%s",
193 pending_return->output_path);
194 ret = 0;
195 } else if (pending_return->status == LTTNG_ROTATE_STARTED) {
196 ret = 1;
197 } else {
198 ret = -1;
199 }
200
201 end:
202 free(pending_return);
203 return ret;
204 }
This page took 0.039662 seconds and 4 git commands to generate.