Fix: assume that conditions are valid before being compared
[lttng-tools.git] / src / common / session-consumed-size.c
1 /*
2 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@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 #include <lttng/condition/condition-internal.h>
19 #include <lttng/condition/session-consumed-size-internal.h>
20 #include <lttng/constant.h>
21 #include <common/macros.h>
22 #include <common/error.h>
23 #include <assert.h>
24 #include <math.h>
25 #include <float.h>
26 #include <time.h>
27
28 #define IS_CONSUMED_SIZE_CONDITION(condition) ( \
29 lttng_condition_get_type(condition) == LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE \
30 )
31
32 #define IS_CONSUMED_SIZE_EVALUATION(evaluation) ( \
33 lttng_evaluation_get_type(evaluation) == LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE \
34 )
35
36 static
37 void lttng_condition_session_consumed_size_destroy(struct lttng_condition *condition)
38 {
39 struct lttng_condition_session_consumed_size *consumed_size;
40
41 consumed_size = container_of(condition,
42 struct lttng_condition_session_consumed_size, parent);
43
44 free(consumed_size->session_name);
45 free(consumed_size);
46 }
47
48 static
49 bool lttng_condition_session_consumed_size_validate(
50 const struct lttng_condition *condition)
51 {
52 bool valid = false;
53 struct lttng_condition_session_consumed_size *consumed;
54
55 if (!condition) {
56 goto end;
57 }
58
59 consumed = container_of(condition, struct lttng_condition_session_consumed_size,
60 parent);
61 if (!consumed->session_name) {
62 ERR("Invalid buffer condition: a target session name must be set.");
63 goto end;
64 }
65 if (!consumed->consumed_threshold_bytes.set) {
66 ERR("Invalid session condition: a threshold must be set.");
67 goto end;
68 }
69
70 valid = true;
71 end:
72 return valid;
73 }
74
75 static
76 int lttng_condition_session_consumed_size_serialize(
77 const struct lttng_condition *condition,
78 struct lttng_dynamic_buffer *buf)
79 {
80 int ret;
81 size_t session_name_len;
82 struct lttng_condition_session_consumed_size *consumed;
83 struct lttng_condition_session_consumed_size_comm consumed_comm;
84
85 if (!condition || !IS_CONSUMED_SIZE_CONDITION(condition)) {
86 ret = -1;
87 goto end;
88 }
89
90 DBG("Serializing session consumed size condition");
91 consumed = container_of(condition,
92 struct lttng_condition_session_consumed_size,
93 parent);
94
95 session_name_len = strlen(consumed->session_name) + 1;
96 if (session_name_len > LTTNG_NAME_MAX) {
97 ret = -1;
98 goto end;
99 }
100
101 consumed_comm.consumed_threshold_bytes =
102 consumed->consumed_threshold_bytes.value;
103 consumed_comm.session_name_len = (uint32_t) session_name_len;
104
105 ret = lttng_dynamic_buffer_append(buf, &consumed_comm,
106 sizeof(consumed_comm));
107 if (ret) {
108 goto end;
109 }
110 ret = lttng_dynamic_buffer_append(buf, consumed->session_name,
111 session_name_len);
112 if (ret) {
113 goto end;
114 }
115 end:
116 return ret;
117 }
118
119 static
120 bool lttng_condition_session_consumed_size_is_equal(const struct lttng_condition *_a,
121 const struct lttng_condition *_b)
122 {
123 bool is_equal = false;
124 struct lttng_condition_session_consumed_size *a, *b;
125
126 a = container_of(_a, struct lttng_condition_session_consumed_size, parent);
127 b = container_of(_b, struct lttng_condition_session_consumed_size, parent);
128
129 if (a->consumed_threshold_bytes.set && b->consumed_threshold_bytes.set) {
130 uint64_t a_value, b_value;
131
132 a_value = a->consumed_threshold_bytes.value;
133 b_value = b->consumed_threshold_bytes.value;
134 if (a_value != b_value) {
135 goto end;
136 }
137 }
138
139 assert(a->session_name);
140 assert(b->session_name);
141 if (strcmp(a->session_name, b->session_name)) {
142 goto end;
143 }
144
145 is_equal = true;
146 end:
147 return is_equal;
148 }
149
150 struct lttng_condition *lttng_condition_session_consumed_size_create(void)
151 {
152 struct lttng_condition_session_consumed_size *condition;
153
154 condition = zmalloc(sizeof(struct lttng_condition_session_consumed_size));
155 if (!condition) {
156 return NULL;
157 }
158
159 lttng_condition_init(&condition->parent, LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE);
160 condition->parent.validate = lttng_condition_session_consumed_size_validate;
161 condition->parent.serialize = lttng_condition_session_consumed_size_serialize;
162 condition->parent.equal = lttng_condition_session_consumed_size_is_equal;
163 condition->parent.destroy = lttng_condition_session_consumed_size_destroy;
164 return &condition->parent;
165 }
166
167 static
168 ssize_t init_condition_from_buffer(struct lttng_condition *condition,
169 const struct lttng_buffer_view *src_view)
170 {
171 ssize_t ret, condition_size;
172 enum lttng_condition_status status;
173 const struct lttng_condition_session_consumed_size_comm *condition_comm;
174 const char *session_name;
175 struct lttng_buffer_view names_view;
176
177 if (src_view->size < sizeof(*condition_comm)) {
178 ERR("Failed to initialize from malformed condition buffer: buffer too short to contain header");
179 ret = -1;
180 goto end;
181 }
182
183 condition_comm = (const struct lttng_condition_session_consumed_size_comm *) src_view->data;
184 names_view = lttng_buffer_view_from_view(src_view,
185 sizeof(*condition_comm), -1);
186
187 if (condition_comm->session_name_len > LTTNG_NAME_MAX) {
188 ERR("Failed to initialize from malformed condition buffer: name exceeds LTTNG_MAX_NAME");
189 ret = -1;
190 goto end;
191 }
192
193 if (names_view.size < condition_comm->session_name_len) {
194 ERR("Failed to initialize from malformed condition buffer: buffer too short to contain element names");
195 ret = -1;
196 goto end;
197 }
198
199 status = lttng_condition_session_consumed_size_set_threshold(condition,
200 condition_comm->consumed_threshold_bytes);
201 if (status != LTTNG_CONDITION_STATUS_OK) {
202 ERR("Failed to initialize session consumed condition threshold");
203 ret = -1;
204 goto end;
205 }
206
207 session_name = names_view.data;
208 if (*(session_name + condition_comm->session_name_len - 1) != '\0') {
209 ERR("Malformed session name encountered in condition buffer");
210 ret = -1;
211 goto end;
212 }
213
214 status = lttng_condition_session_consumed_size_set_session_name(condition,
215 session_name);
216 if (status != LTTNG_CONDITION_STATUS_OK) {
217 ERR("Failed to set buffer consumed session name");
218 ret = -1;
219 goto end;
220 }
221
222 if (!lttng_condition_validate(condition)) {
223 ret = -1;
224 goto end;
225 }
226
227 condition_size = sizeof(*condition_comm) +
228 (ssize_t) condition_comm->session_name_len;
229 ret = condition_size;
230 end:
231 return ret;
232 }
233
234 LTTNG_HIDDEN
235 ssize_t lttng_condition_session_consumed_size_create_from_buffer(
236 const struct lttng_buffer_view *view,
237 struct lttng_condition **_condition)
238 {
239 ssize_t ret;
240 struct lttng_condition *condition =
241 lttng_condition_session_consumed_size_create();
242
243 if (!_condition || !condition) {
244 ret = -1;
245 goto error;
246 }
247
248 ret = init_condition_from_buffer(condition, view);
249 if (ret < 0) {
250 goto error;
251 }
252
253 *_condition = condition;
254 return ret;
255 error:
256 lttng_condition_destroy(condition);
257 return ret;
258 }
259
260 static
261 struct lttng_evaluation *create_evaluation_from_buffer(
262 const struct lttng_buffer_view *view)
263 {
264 const struct lttng_evaluation_session_consumed_size_comm *comm =
265 (const struct lttng_evaluation_session_consumed_size_comm *) view->data;
266 struct lttng_evaluation *evaluation = NULL;
267
268 if (view->size < sizeof(*comm)) {
269 goto end;
270 }
271
272 evaluation = lttng_evaluation_session_consumed_size_create(
273 comm->session_consumed);
274 end:
275 return evaluation;
276 }
277
278 LTTNG_HIDDEN
279 ssize_t lttng_evaluation_session_consumed_size_create_from_buffer(
280 const struct lttng_buffer_view *view,
281 struct lttng_evaluation **_evaluation)
282 {
283 ssize_t ret;
284 struct lttng_evaluation *evaluation = NULL;
285
286 if (!_evaluation) {
287 ret = -1;
288 goto error;
289 }
290
291 evaluation = create_evaluation_from_buffer(view);
292 if (!evaluation) {
293 ret = -1;
294 goto error;
295 }
296
297 *_evaluation = evaluation;
298 ret = sizeof(struct lttng_evaluation_session_consumed_size_comm);
299 return ret;
300 error:
301 lttng_evaluation_destroy(evaluation);
302 return ret;
303 }
304
305 enum lttng_condition_status
306 lttng_condition_session_consumed_size_get_threshold(
307 const struct lttng_condition *condition,
308 uint64_t *consumed_threshold_bytes)
309 {
310 struct lttng_condition_session_consumed_size *consumed;
311 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
312
313 if (!condition || !IS_CONSUMED_SIZE_CONDITION(condition) || !consumed_threshold_bytes) {
314 status = LTTNG_CONDITION_STATUS_INVALID;
315 goto end;
316 }
317
318 consumed = container_of(condition, struct lttng_condition_session_consumed_size,
319 parent);
320 if (!consumed->consumed_threshold_bytes.set) {
321 status = LTTNG_CONDITION_STATUS_UNSET;
322 goto end;
323 }
324 *consumed_threshold_bytes = consumed->consumed_threshold_bytes.value;
325 end:
326 return status;
327 }
328
329 enum lttng_condition_status
330 lttng_condition_session_consumed_size_set_threshold(
331 struct lttng_condition *condition, uint64_t consumed_threshold_bytes)
332 {
333 struct lttng_condition_session_consumed_size *consumed;
334 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
335
336 if (!condition || !IS_CONSUMED_SIZE_CONDITION(condition)) {
337 status = LTTNG_CONDITION_STATUS_INVALID;
338 goto end;
339 }
340
341 consumed = container_of(condition, struct lttng_condition_session_consumed_size,
342 parent);
343 consumed->consumed_threshold_bytes.set = true;
344 consumed->consumed_threshold_bytes.value = consumed_threshold_bytes;
345 end:
346 return status;
347 }
348
349 enum lttng_condition_status
350 lttng_condition_session_consumed_size_get_session_name(
351 const struct lttng_condition *condition,
352 const char **session_name)
353 {
354 struct lttng_condition_session_consumed_size *consumed;
355 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
356
357 if (!condition || !IS_CONSUMED_SIZE_CONDITION(condition) || !session_name) {
358 status = LTTNG_CONDITION_STATUS_INVALID;
359 goto end;
360 }
361
362 consumed = container_of(condition, struct lttng_condition_session_consumed_size,
363 parent);
364 if (!consumed->session_name) {
365 status = LTTNG_CONDITION_STATUS_UNSET;
366 goto end;
367 }
368 *session_name = consumed->session_name;
369 end:
370 return status;
371 }
372
373 enum lttng_condition_status
374 lttng_condition_session_consumed_size_set_session_name(
375 struct lttng_condition *condition, const char *session_name)
376 {
377 char *session_name_copy;
378 struct lttng_condition_session_consumed_size *consumed;
379 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
380
381 if (!condition || !IS_CONSUMED_SIZE_CONDITION(condition) ||
382 !session_name || strlen(session_name) == 0) {
383 status = LTTNG_CONDITION_STATUS_INVALID;
384 goto end;
385 }
386
387 consumed = container_of(condition, struct lttng_condition_session_consumed_size,
388 parent);
389 session_name_copy = strdup(session_name);
390 if (!session_name_copy) {
391 status = LTTNG_CONDITION_STATUS_ERROR;
392 goto end;
393 }
394
395 if (consumed->session_name) {
396 free(consumed->session_name);
397 }
398 consumed->session_name = session_name_copy;
399 end:
400 return status;
401 }
402
403 static
404 int lttng_evaluation_session_consumed_size_serialize(
405 struct lttng_evaluation *evaluation,
406 struct lttng_dynamic_buffer *buf)
407 {
408 struct lttng_evaluation_session_consumed_size *consumed;
409 struct lttng_evaluation_session_consumed_size_comm comm;
410
411 consumed = container_of(evaluation, struct lttng_evaluation_session_consumed_size,
412 parent);
413 comm.session_consumed = consumed->session_consumed;
414 return lttng_dynamic_buffer_append(buf, &comm, sizeof(comm));
415 }
416
417 static
418 void lttng_evaluation_session_consumed_size_destroy(
419 struct lttng_evaluation *evaluation)
420 {
421 struct lttng_evaluation_session_consumed_size *consumed;
422
423 consumed = container_of(evaluation, struct lttng_evaluation_session_consumed_size,
424 parent);
425 free(consumed);
426 }
427
428 LTTNG_HIDDEN
429 struct lttng_evaluation *lttng_evaluation_session_consumed_size_create(
430 uint64_t consumed)
431 {
432 struct lttng_evaluation_session_consumed_size *consumed_eval;
433
434 consumed_eval = zmalloc(sizeof(struct lttng_evaluation_session_consumed_size));
435 if (!consumed_eval) {
436 goto end;
437 }
438
439 consumed_eval->parent.type = LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE;
440 consumed_eval->session_consumed = consumed;
441 consumed_eval->parent.serialize = lttng_evaluation_session_consumed_size_serialize;
442 consumed_eval->parent.destroy = lttng_evaluation_session_consumed_size_destroy;
443 end:
444 return &consumed_eval->parent;
445 }
446
447 enum lttng_evaluation_status
448 lttng_evaluation_session_consumed_size_get_consumed_size(
449 const struct lttng_evaluation *evaluation,
450 uint64_t *session_consumed)
451 {
452 struct lttng_evaluation_session_consumed_size *consumed;
453 enum lttng_evaluation_status status = LTTNG_EVALUATION_STATUS_OK;
454
455 if (!evaluation || !IS_CONSUMED_SIZE_EVALUATION(evaluation) ||
456 !session_consumed) {
457 status = LTTNG_EVALUATION_STATUS_INVALID;
458 goto end;
459 }
460
461 consumed = container_of(evaluation, struct lttng_evaluation_session_consumed_size,
462 parent);
463 *session_consumed = consumed->session_consumed;
464 end:
465 return status;
466 }
This page took 0.063029 seconds and 4 git commands to generate.