trigger: implement listing of registered trigger
[lttng-tools.git] / src / common / condition.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/condition-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 <common/dynamic-buffer.h>
15#include <common/buffer-view.h>
16#include <stdbool.h>
17#include <assert.h>
18
19enum lttng_condition_type lttng_condition_get_type(
20 const struct lttng_condition *condition)
21{
22 return condition ? condition->type : LTTNG_CONDITION_TYPE_UNKNOWN;
23}
24
25void lttng_condition_destroy(struct lttng_condition *condition)
4655b864
JR
26{
27 lttng_condition_put(condition);
28}
29
30static void condition_destroy_ref(struct urcu_ref *ref)
31{
32 struct lttng_condition *condition =
33 container_of(ref, struct lttng_condition, ref);
34
35 condition->destroy(condition);
36}
37
38LTTNG_HIDDEN
39void lttng_condition_get(struct lttng_condition *condition)
40{
41 urcu_ref_get(&condition->ref);
42}
43
44LTTNG_HIDDEN
45void lttng_condition_put(struct lttng_condition *condition)
a58c490f
JG
46{
47 if (!condition) {
48 return;
49 }
50
51 assert(condition->destroy);
4655b864 52 urcu_ref_put(&condition->ref, condition_destroy_ref);
a58c490f
JG
53}
54
4655b864 55
a58c490f
JG
56LTTNG_HIDDEN
57bool lttng_condition_validate(const struct lttng_condition *condition)
58{
59 bool valid;
60
61 if (!condition) {
62 valid = false;
63 goto end;
64 }
65
66 if (!condition->validate) {
67 /* Sub-class guarantees that it can never be invalid. */
68 valid = true;
69 goto end;
70 }
71
72 valid = condition->validate(condition);
73end:
74 return valid;
75}
76
77LTTNG_HIDDEN
3647288f 78int lttng_condition_serialize(const struct lttng_condition *condition,
c0a66c84 79 struct lttng_payload *payload)
a58c490f 80{
3647288f 81 int ret;
c0a66c84 82 struct lttng_condition_comm condition_comm = {};
a58c490f
JG
83
84 if (!condition) {
85 ret = -1;
86 goto end;
87 }
88
3647288f
JG
89 condition_comm.condition_type = (int8_t) condition->type;
90
c0a66c84 91 ret = lttng_dynamic_buffer_append(&payload->buffer, &condition_comm,
3647288f
JG
92 sizeof(condition_comm));
93 if (ret) {
94 goto end;
a58c490f
JG
95 }
96
c0a66c84 97 ret = condition->serialize(condition, payload);
3647288f 98 if (ret) {
a58c490f
JG
99 goto end;
100 }
a58c490f
JG
101end:
102 return ret;
103}
104
105LTTNG_HIDDEN
106bool lttng_condition_is_equal(const struct lttng_condition *a,
107 const struct lttng_condition *b)
108{
109 bool is_equal = false;
110
111 if (!a || !b) {
112 goto end;
113 }
114
115 if (a->type != b->type) {
116 goto end;
117 }
118
6c2fe319
JG
119 if (a == b) {
120 is_equal = true;
121 goto end;
122 }
123
a58c490f
JG
124 is_equal = a->equal ? a->equal(a, b) : true;
125end:
126 return is_equal;
127}
128
129LTTNG_HIDDEN
c0a66c84
JG
130ssize_t lttng_condition_create_from_payload(
131 struct lttng_payload_view *view,
a58c490f
JG
132 struct lttng_condition **condition)
133{
134 ssize_t ret, condition_size = 0;
c0a66c84 135 condition_create_from_payload_cb create_from_payload = NULL;
3e6e0df2
JG
136 const struct lttng_condition_comm *condition_comm;
137 const struct lttng_payload_view condition_comm_view =
138 lttng_payload_view_from_view(
139 view, 0, sizeof(*condition_comm));
a58c490f 140
c0a66c84 141 if (!view || !condition) {
a58c490f
JG
142 ret = -1;
143 goto end;
144 }
145
3e6e0df2
JG
146 if (!lttng_payload_view_is_valid(&condition_comm_view)) {
147 /* Payload not large enough to contain the header. */
148 ret = -1;
149 goto end;
150 }
151
a58c490f 152 DBG("Deserializing condition from buffer");
3e6e0df2 153 condition_comm = (typeof(condition_comm)) condition_comm_view.buffer.data;
a58c490f
JG
154 condition_size += sizeof(*condition_comm);
155
156 switch ((enum lttng_condition_type) condition_comm->condition_type) {
157 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
c0a66c84 158 create_from_payload = lttng_condition_buffer_usage_low_create_from_payload;
a58c490f
JG
159 break;
160 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
c0a66c84 161 create_from_payload = lttng_condition_buffer_usage_high_create_from_payload;
a58c490f 162 break;
e8360425 163 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
c0a66c84 164 create_from_payload = lttng_condition_session_consumed_size_create_from_payload;
e8360425 165 break;
c19092cd 166 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
c0a66c84 167 create_from_payload = lttng_condition_session_rotation_ongoing_create_from_payload;
c19092cd
JG
168 break;
169 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
c0a66c84 170 create_from_payload = lttng_condition_session_rotation_completed_create_from_payload;
c19092cd 171 break;
a58c490f
JG
172 default:
173 ERR("Attempted to create condition of unknown type (%i)",
174 (int) condition_comm->condition_type);
175 ret = -1;
176 goto end;
177 }
178
c0a66c84
JG
179 if (create_from_payload) {
180 struct lttng_payload_view condition_view =
181 lttng_payload_view_from_view(view,
a58c490f
JG
182 sizeof(*condition_comm), -1);
183
c0a66c84 184 ret = create_from_payload(&condition_view, condition);
a58c490f
JG
185 if (ret < 0) {
186 goto end;
187 }
188 condition_size += ret;
189
190 } else {
191 abort();
192 }
193
194 ret = condition_size;
195end:
196 return ret;
197}
198
199LTTNG_HIDDEN
200void lttng_condition_init(struct lttng_condition *condition,
201 enum lttng_condition_type type)
202{
203 condition->type = type;
4655b864 204 urcu_ref_init(&condition->ref);
a58c490f 205}
This page took 0.063257 seconds and 4 git commands to generate.