Fix: lttng: uninitialized pointer free'd when no sessiond is present
[lttng-tools.git] / src / common / evaluation.c
CommitLineData
a58c490f 1/*
ab5be9fa 2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
a58c490f 3 *
ab5be9fa 4 * SPDX-License-Identifier: LGPL-2.1-only
a58c490f 5 *
a58c490f
JG
6 */
7
8#include <lttng/condition/evaluation-internal.h>
9#include <lttng/condition/buffer-usage-internal.h>
e8360425 10#include <lttng/condition/session-consumed-size-internal.h>
c19092cd 11#include <lttng/condition/session-rotation-internal.h>
a58c490f
JG
12#include <common/macros.h>
13#include <common/error.h>
14#include <stdbool.h>
15#include <assert.h>
16
c19092cd
JG
17LTTNG_HIDDEN
18void lttng_evaluation_init(struct lttng_evaluation *evaluation,
19 enum lttng_condition_type type)
20{
21 evaluation->type = type;
22}
23
a58c490f 24LTTNG_HIDDEN
9b63a4aa 25int lttng_evaluation_serialize(const struct lttng_evaluation *evaluation,
3647288f 26 struct lttng_dynamic_buffer *buf)
a58c490f 27{
3647288f 28 int ret;
db315d75
JG
29 struct lttng_evaluation_comm evaluation_comm = {
30 .type = (int8_t) evaluation->type
31 };
a58c490f 32
3647288f
JG
33 ret = lttng_dynamic_buffer_append(buf, &evaluation_comm,
34 sizeof(evaluation_comm));
35 if (ret) {
36 goto end;
a58c490f 37 }
a58c490f
JG
38
39 if (evaluation->serialize) {
3647288f
JG
40 ret = evaluation->serialize(evaluation, buf);
41 if (ret) {
a58c490f
JG
42 goto end;
43 }
a58c490f 44 }
a58c490f
JG
45end:
46 return ret;
47}
48
49LTTNG_HIDDEN
50ssize_t lttng_evaluation_create_from_buffer(
51 const struct lttng_buffer_view *src_view,
52 struct lttng_evaluation **evaluation)
53{
54 ssize_t ret, evaluation_size = 0;
55 const struct lttng_evaluation_comm *evaluation_comm;
56 const struct lttng_buffer_view evaluation_view =
57 lttng_buffer_view_from_view(src_view,
58 sizeof(*evaluation_comm), -1);
59
60 if (!src_view || !evaluation) {
61 ret = -1;
62 goto end;
63 }
64
65 evaluation_comm = (const struct lttng_evaluation_comm *) src_view->data;
66 evaluation_size += sizeof(*evaluation_comm);
67
68 switch ((enum lttng_condition_type) evaluation_comm->type) {
69 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
70 ret = lttng_evaluation_buffer_usage_low_create_from_buffer(
71 &evaluation_view, evaluation);
72 if (ret < 0) {
73 goto end;
74 }
75 evaluation_size += ret;
76 break;
77 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
78 ret = lttng_evaluation_buffer_usage_high_create_from_buffer(
79 &evaluation_view, evaluation);
80 if (ret < 0) {
81 goto end;
82 }
83 evaluation_size += ret;
84 break;
e8360425
JD
85 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
86 ret = lttng_evaluation_session_consumed_size_create_from_buffer(
87 &evaluation_view, evaluation);
88 if (ret < 0) {
89 goto end;
90 }
91 evaluation_size += ret;
92 break;
c19092cd
JG
93 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
94 ret = lttng_evaluation_session_rotation_ongoing_create_from_buffer(
95 &evaluation_view, evaluation);
96 if (ret < 0) {
97 goto end;
98 }
99 evaluation_size += ret;
100 break;
101 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
102 ret = lttng_evaluation_session_rotation_completed_create_from_buffer(
103 &evaluation_view, evaluation);
104 if (ret < 0) {
105 goto end;
106 }
107 evaluation_size += ret;
108 break;
a58c490f
JG
109 default:
110 ERR("Attempted to create evaluation of unknown type (%i)",
111 (int) evaluation_comm->type);
112 ret = -1;
113 goto end;
114 }
115 ret = evaluation_size;
116end:
117 return ret;
118}
119
120enum lttng_condition_type lttng_evaluation_get_type(
121 const struct lttng_evaluation *evaluation)
122{
123 return evaluation ? evaluation->type : LTTNG_CONDITION_TYPE_UNKNOWN;
124}
125
126void lttng_evaluation_destroy(struct lttng_evaluation *evaluation)
127{
128 if (!evaluation) {
129 return;
130 }
131
132 assert(evaluation->destroy);
133 evaluation->destroy(evaluation);
134}
This page took 0.066835 seconds and 5 git commands to generate.