SoW-2019-0002: Dynamic Snapshot
[lttng-tools.git] / src / common / actions / rotate-session.c
1 /*
2 * Copyright (C) 2019 EfficiOS, Inc.
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 #include <assert.h>
19 #include <common/error.h>
20 #include <common/macros.h>
21 #include <lttng/action/action-internal.h>
22 #include <lttng/action/rotate-session-internal.h>
23 #include <lttng/action/rotate-session.h>
24
25 struct lttng_action_rotate_session {
26 struct lttng_action parent;
27
28 /* Owned by this. */
29 char *session_name;
30 };
31
32 struct lttng_action_rotate_session_comm {
33 /* Includes the trailing \0. */
34 uint32_t session_name_len;
35
36 /*
37 * Variable data:
38 *
39 * - session name (null terminated)
40 */
41 char data[];
42 } LTTNG_PACKED;
43
44 static struct lttng_action_rotate_session *action_rotate_session_from_action(
45 struct lttng_action *action)
46 {
47 assert(action);
48
49 return container_of(action, struct lttng_action_rotate_session, parent);
50 }
51
52 static const struct lttng_action_rotate_session *action_rotate_session_from_action_const(
53 const struct lttng_action *action)
54 {
55 assert(action);
56
57 return container_of(action, struct lttng_action_rotate_session, parent);
58 }
59
60 static bool lttng_action_rotate_session_validate(struct lttng_action *action)
61 {
62 bool valid;
63 struct lttng_action_rotate_session *action_rotate_session;
64
65 if (!action) {
66 valid = false;
67 goto end;
68 }
69
70 action_rotate_session = action_rotate_session_from_action(action);
71
72 /* A non-empty session name is mandatory. */
73 if (!action_rotate_session->session_name ||
74 strlen(action_rotate_session->session_name) == 0) {
75 valid = false;
76 goto end;
77 }
78
79 valid = true;
80 end:
81 return valid;
82 }
83
84 static bool lttng_action_rotate_session_is_equal(const struct lttng_action *_a, const struct lttng_action *_b)
85 {
86 bool is_equal = false;
87 const struct lttng_action_rotate_session *a, *b;
88
89 a = action_rotate_session_from_action_const(_a);
90 b = action_rotate_session_from_action_const(_b);
91
92 /* Action is not valid if this is not true. */
93 assert(a->session_name);
94 assert(b->session_name);
95 if (strcmp(a->session_name, b->session_name)) {
96 goto end;
97 }
98
99 is_equal = true;
100 end:
101 return is_equal;
102 }
103 static int lttng_action_rotate_session_serialize(
104 struct lttng_action *action, struct lttng_dynamic_buffer *buf)
105 {
106 struct lttng_action_rotate_session *action_rotate_session;
107 struct lttng_action_rotate_session_comm comm;
108 size_t session_name_len;
109 int ret;
110
111 assert(action);
112 assert(buf);
113
114 action_rotate_session = action_rotate_session_from_action(action);
115
116 assert(action_rotate_session->session_name);
117
118 DBG("Serializing rotate session action: session-name: %s",
119 action_rotate_session->session_name);
120
121 session_name_len = strlen(action_rotate_session->session_name) + 1;
122 comm.session_name_len = session_name_len;
123
124 ret = lttng_dynamic_buffer_append(buf, &comm, sizeof(comm));
125 if (ret) {
126 ret = -1;
127 goto end;
128 }
129
130 ret = lttng_dynamic_buffer_append(buf,
131 action_rotate_session->session_name, session_name_len);
132 if (ret) {
133 ret = -1;
134 goto end;
135 }
136
137 ret = 0;
138 end:
139 return ret;
140 }
141
142 static void lttng_action_rotate_session_destroy(struct lttng_action *action)
143 {
144 struct lttng_action_rotate_session *action_rotate_session;
145
146 if (!action) {
147 goto end;
148 }
149
150 action_rotate_session = action_rotate_session_from_action(action);
151
152 free(action_rotate_session->session_name);
153 free(action_rotate_session);
154
155 end:
156 return;
157 }
158
159 ssize_t lttng_action_rotate_session_create_from_buffer(
160 const struct lttng_buffer_view *view,
161 struct lttng_action **p_action)
162 {
163 ssize_t consumed_len;
164 struct lttng_action_rotate_session_comm *comm;
165 const char *session_name;
166 struct lttng_action *action;
167 enum lttng_action_status status;
168
169 action = lttng_action_rotate_session_create();
170 if (!action) {
171 consumed_len = -1;
172 goto end;
173 }
174
175 comm = (struct lttng_action_rotate_session_comm *) view->data;
176 session_name = (const char *) &comm->data;
177
178 if (!lttng_buffer_view_validate_string(
179 view, session_name, comm->session_name_len)) {
180 consumed_len = -1;
181 goto end;
182 }
183
184 status = lttng_action_rotate_session_set_session_name(
185 action, session_name);
186 if (status != LTTNG_ACTION_STATUS_OK) {
187 consumed_len = -1;
188 goto end;
189 }
190
191 consumed_len = sizeof(struct lttng_action_rotate_session_comm) +
192 comm->session_name_len;
193 *p_action = action;
194 action = NULL;
195
196 end:
197 lttng_action_rotate_session_destroy(action);
198
199 return consumed_len;
200 }
201
202 struct lttng_action *lttng_action_rotate_session_create(void)
203 {
204 struct lttng_action *action;
205
206 action = zmalloc(sizeof(struct lttng_action_rotate_session));
207 if (!action) {
208 goto end;
209 }
210
211 lttng_action_init(action, LTTNG_ACTION_TYPE_ROTATE_SESSION,
212 lttng_action_rotate_session_validate,
213 lttng_action_rotate_session_serialize,
214 lttng_action_rotate_session_is_equal,
215 lttng_action_rotate_session_destroy);
216
217 end:
218 return action;
219 }
220
221 enum lttng_action_status lttng_action_rotate_session_set_session_name(
222 struct lttng_action *action, const char *session_name)
223 {
224 struct lttng_action_rotate_session *action_rotate_session;
225 enum lttng_action_status status;
226
227 if (!action || !session_name || strlen(session_name) == 0) {
228 status = LTTNG_ACTION_STATUS_INVALID;
229 goto end;
230 }
231
232 action_rotate_session = action_rotate_session_from_action(action);
233
234 free(action_rotate_session->session_name);
235
236 action_rotate_session->session_name = strdup(session_name);
237 if (!action_rotate_session->session_name) {
238 status = LTTNG_ACTION_STATUS_ERROR;
239 goto end;
240 }
241
242 status = LTTNG_ACTION_STATUS_OK;
243 end:
244 return status;
245 }
246
247 enum lttng_action_status lttng_action_rotate_session_get_session_name(
248 const struct lttng_action *action, const char **session_name)
249 {
250 const struct lttng_action_rotate_session *action_rotate_session;
251 enum lttng_action_status status;
252
253 if (!action || !session_name) {
254 status = LTTNG_ACTION_STATUS_INVALID;
255 goto end;
256 }
257
258 action_rotate_session = action_rotate_session_from_action_const(action);
259
260 *session_name = action_rotate_session->session_name;
261
262 status = LTTNG_ACTION_STATUS_OK;
263 end:
264 return status;
265 }
This page took 0.052279 seconds and 5 git commands to generate.