condition: implement event rule based condition
[lttng-tools.git] / src / common / conditions / event-rule.c
CommitLineData
683d081a
JR
1/*
2 * Copyright (C) 2020 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8#include <assert.h>
9#include <common/error.h>
10#include <common/macros.h>
11#include <lttng/condition/condition-internal.h>
12#include <lttng/condition/event-rule-internal.h>
13#include <lttng/condition/event-rule.h>
14#include <lttng/event-rule/event-rule-internal.h>
15#include <stdbool.h>
16
17#define IS_EVENT_RULE_CONDITION(condition) \
18 (lttng_condition_get_type(condition) == \
19 LTTNG_CONDITION_TYPE_EVENT_RULE_HIT)
20
21static bool is_event_rule_evaluation(const struct lttng_evaluation *evaluation)
22{
23 enum lttng_condition_type type = lttng_evaluation_get_type(evaluation);
24
25 return type == LTTNG_CONDITION_TYPE_EVENT_RULE_HIT;
26}
27
28static bool lttng_condition_event_rule_validate(
29 const struct lttng_condition *condition);
30static int lttng_condition_event_rule_serialize(
31 const struct lttng_condition *condition,
32 struct lttng_payload *payload);
33static bool lttng_condition_event_rule_is_equal(
34 const struct lttng_condition *_a,
35 const struct lttng_condition *_b);
36static void lttng_condition_event_rule_destroy(
37 struct lttng_condition *condition);
38
39static bool lttng_condition_event_rule_validate(
40 const struct lttng_condition *condition)
41{
42 bool valid = false;
43 struct lttng_condition_event_rule *event_rule;
44
45 if (!condition) {
46 goto end;
47 }
48
49 event_rule = container_of(
50 condition, struct lttng_condition_event_rule, parent);
51 if (!event_rule->rule) {
52 ERR("Invalid event rule condition: a rule must be set.");
53 goto end;
54 }
55
56 valid = lttng_event_rule_validate(event_rule->rule);
57end:
58 return valid;
59}
60
61static int lttng_condition_event_rule_serialize(
62 const struct lttng_condition *condition,
63 struct lttng_payload *payload)
64{
65 int ret;
66 size_t header_offset, size_before_payload;
67 struct lttng_condition_event_rule *event_rule;
68 struct lttng_condition_event_rule_comm event_rule_comm = {};
69 struct lttng_condition_event_rule_comm *header = NULL;
70
71 if (!condition || !IS_EVENT_RULE_CONDITION(condition)) {
72 ret = -1;
73 goto end;
74 }
75
76 DBG("Serializing event rule condition");
77 event_rule = container_of(
78 condition, struct lttng_condition_event_rule, parent);
79
80 header_offset = payload->buffer.size;
81 ret = lttng_dynamic_buffer_append(&payload->buffer, &event_rule_comm,
82 sizeof(event_rule_comm));
83 if (ret) {
84 goto end;
85 }
86
87 size_before_payload = payload->buffer.size;
88 ret = lttng_event_rule_serialize(event_rule->rule, payload);
89 if (ret) {
90 goto end;
91 }
92
93 /* Update payload size. */
94 header = (struct lttng_condition_event_rule_comm *)
95 ((char *) payload->buffer.data + header_offset);
96 header->event_rule_length = payload->buffer.size - size_before_payload;
97
98end:
99 return ret;
100}
101
102static bool lttng_condition_event_rule_is_equal(
103 const struct lttng_condition *_a,
104 const struct lttng_condition *_b)
105{
106 bool is_equal = false;
107 struct lttng_condition_event_rule *a, *b;
108
109 a = container_of(_a, struct lttng_condition_event_rule, parent);
110 b = container_of(_b, struct lttng_condition_event_rule, parent);
111
112 /* Both event rules must be set or both must be unset. */
113 if ((a->rule && !b->rule) || (!a->rule && b->rule)) {
114 WARN("Comparing event_rule conditions with uninitialized rule");
115 goto end;
116 }
117
118 is_equal = lttng_event_rule_is_equal(a->rule, b->rule);
119end:
120 return is_equal;
121}
122
123static void lttng_condition_event_rule_destroy(
124 struct lttng_condition *condition)
125{
126 struct lttng_condition_event_rule *event_rule;
127
128 event_rule = container_of(
129 condition, struct lttng_condition_event_rule, parent);
130
131 lttng_event_rule_put(event_rule->rule);
132 free(event_rule);
133}
134
135struct lttng_condition *lttng_condition_event_rule_create(
136 struct lttng_event_rule *rule)
137{
138 struct lttng_condition *parent = NULL;
139 struct lttng_condition_event_rule *condition = NULL;
140
141 if (!rule) {
142 goto end;
143 }
144
145 condition = zmalloc(sizeof(struct lttng_condition_event_rule));
146 if (!condition) {
147 return NULL;
148 }
149
150 lttng_condition_init(&condition->parent,
151 LTTNG_CONDITION_TYPE_EVENT_RULE_HIT);
152 condition->parent.validate = lttng_condition_event_rule_validate,
153 condition->parent.serialize = lttng_condition_event_rule_serialize,
154 condition->parent.equal = lttng_condition_event_rule_is_equal,
155 condition->parent.destroy = lttng_condition_event_rule_destroy,
156
157 lttng_event_rule_get(rule);
158 condition->rule = rule;
159 rule = NULL;
160
161 parent = &condition->parent;
162end:
163 return parent;
164}
165
166LTTNG_HIDDEN
167ssize_t lttng_condition_event_rule_create_from_payload(
168 struct lttng_payload_view *view,
169 struct lttng_condition **_condition)
170{
171 ssize_t offset, event_rule_length;
172 struct lttng_condition *condition = NULL;
173 struct lttng_event_rule *event_rule = NULL;
174 const struct lttng_condition_event_rule_comm *header;
175 const struct lttng_payload_view header_view =
176 lttng_payload_view_from_view(
177 view, 0, sizeof(*header));
178
179 if (!view || !_condition) {
180 goto error;
181 }
182
183 if (!lttng_payload_view_is_valid(&header_view)) {
184 ERR("Failed to initialize from malformed event rule condition: buffer too short to contain header");
185 goto error;
186 }
187
188 header = (const struct lttng_condition_event_rule_comm *)
189 header_view.buffer.data;
190 offset = sizeof(*header);
191
192 /* lttng_event_rule payload. */
193 {
194 struct lttng_payload_view event_rule_view =
195 lttng_payload_view_from_view(view, offset, -1);
196
197 event_rule_length = lttng_event_rule_create_from_payload(
198 &event_rule_view, &event_rule);
199 }
200
201 if (event_rule_length < 0 || !event_rule) {
202 goto error;
203 }
204
205 if ((size_t) header->event_rule_length != event_rule_length) {
206 goto error;
207 }
208
209 /* Move to the end of the payload. */
210 offset += header->event_rule_length;
211
212 /* Acquires a reference to the event rule. */
213 condition = lttng_condition_event_rule_create(event_rule);
214 if (!condition) {
215 goto error;
216 }
217
218 *_condition = condition;
219 condition = NULL;
220 goto end;
221
222error:
223 offset = -1;
224
225end:
226 lttng_event_rule_put(event_rule);
227 lttng_condition_put(condition);
228 return offset;
229}
230
231LTTNG_HIDDEN
232enum lttng_condition_status lttng_condition_event_rule_borrow_rule_mutable(
233 const struct lttng_condition *condition,
234 struct lttng_event_rule **rule)
235{
236 struct lttng_condition_event_rule *event_rule;
237 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
238
239 if (!condition || !IS_EVENT_RULE_CONDITION(condition) || !rule) {
240 status = LTTNG_CONDITION_STATUS_INVALID;
241 goto end;
242 }
243
244 event_rule = container_of(
245 condition, struct lttng_condition_event_rule, parent);
246 if (!event_rule->rule) {
247 status = LTTNG_CONDITION_STATUS_UNSET;
248 goto end;
249 }
250
251 *rule = event_rule->rule;
252end:
253 return status;
254}
255
256enum lttng_condition_status lttng_condition_event_rule_get_rule(
257 const struct lttng_condition *condition,
258 const struct lttng_event_rule **rule)
259{
260 struct lttng_event_rule *mutable_rule = NULL;
261 const enum lttng_condition_status status =
262 lttng_condition_event_rule_borrow_rule_mutable(
263 condition, &mutable_rule);
264
265 *rule = mutable_rule;
266 return status;
267}
268
269LTTNG_HIDDEN
270ssize_t lttng_evaluation_event_rule_create_from_payload(
271 struct lttng_payload_view *view,
272 struct lttng_evaluation **_evaluation)
273{
274 ssize_t ret, offset = 0;
275 const char *trigger_name;
276 struct lttng_evaluation *evaluation = NULL;
277 const struct lttng_evaluation_event_rule_comm *header;
278 const struct lttng_payload_view header_view =
279 lttng_payload_view_from_view(
280 view, 0, sizeof(*header));
281
282 if (!_evaluation) {
283 ret = -1;
284 goto error;
285 }
286
287 if (!lttng_payload_view_is_valid(&header_view)) {
288 ERR("Failed to initialize from malformed event rule evaluation: buffer too short to contain header");
289 ret = -1;
290 goto error;
291 }
292
293 header = (typeof(header)) header_view.buffer.data;
294
295 /* Map the originating trigger's name. */
296 offset += sizeof(*header);
297 {
298 struct lttng_payload_view current_view =
299 lttng_payload_view_from_view(view, offset,
300 header->trigger_name_length);
301
302 if (!lttng_payload_view_is_valid(&current_view)) {
303 ERR("Failed to initialize from malformed event rule evaluation: buffer too short to contain trigger name");
304 ret = -1;
305 goto error;
306 }
307
308 trigger_name = current_view.buffer.data;
309 if (!lttng_buffer_view_contains_string(&current_view.buffer,
310 trigger_name, header->trigger_name_length)) {
311 ERR("Failed to initialize from malformed event rule evaluation: invalid trigger name");
312 ret = -1;
313 goto error;
314 }
315 }
316
317 offset += header->trigger_name_length;
318
319 evaluation = lttng_evaluation_event_rule_create(trigger_name);
320 if (!evaluation) {
321 ret = -1;
322 goto error;
323 }
324
325 *_evaluation = evaluation;
326 evaluation = NULL;
327 ret = offset;
328
329error:
330 lttng_evaluation_destroy(evaluation);
331 return ret;
332}
333
334static int lttng_evaluation_event_rule_serialize(
335 const struct lttng_evaluation *evaluation,
336 struct lttng_payload *payload)
337{
338 int ret = 0;
339 struct lttng_evaluation_event_rule *hit;
340 struct lttng_evaluation_event_rule_comm comm;
341
342 hit = container_of(
343 evaluation, struct lttng_evaluation_event_rule, parent);
344
345 assert(hit->name);
346 comm.trigger_name_length = strlen(hit->name) + 1;
347
348 ret = lttng_dynamic_buffer_append(
349 &payload->buffer, &comm, sizeof(comm));
350 if (ret) {
351 goto end;
352 }
353
354 ret = lttng_dynamic_buffer_append(
355 &payload->buffer, hit->name, comm.trigger_name_length);
356end:
357 return ret;
358}
359
360static void lttng_evaluation_event_rule_destroy(
361 struct lttng_evaluation *evaluation)
362{
363 struct lttng_evaluation_event_rule *hit;
364
365 hit = container_of(
366 evaluation, struct lttng_evaluation_event_rule, parent);
367 free(hit->name);
368 free(hit);
369}
370
371LTTNG_HIDDEN
372struct lttng_evaluation *lttng_evaluation_event_rule_create(
373 const char *trigger_name)
374{
375 struct lttng_evaluation_event_rule *hit;
376 struct lttng_evaluation *evaluation = NULL;
377
378 hit = zmalloc(sizeof(struct lttng_evaluation_event_rule));
379 if (!hit) {
380 goto end;
381 }
382
383 hit->name = strdup(trigger_name);
384 if (!hit->name) {
385 goto end;
386 }
387
388 hit->parent.type = LTTNG_CONDITION_TYPE_EVENT_RULE_HIT;
389 hit->parent.serialize = lttng_evaluation_event_rule_serialize;
390 hit->parent.destroy = lttng_evaluation_event_rule_destroy;
391
392 evaluation = &hit->parent;
393 hit = NULL;
394
395end:
396 if (hit) {
397 lttng_evaluation_event_rule_destroy(&hit->parent);
398 }
399
400 return evaluation;
401}
402
403enum lttng_evaluation_status lttng_evaluation_event_rule_get_trigger_name(
404 const struct lttng_evaluation *evaluation, const char **name)
405{
406 struct lttng_evaluation_event_rule *hit;
407 enum lttng_evaluation_status status = LTTNG_EVALUATION_STATUS_OK;
408
409 if (!evaluation || !is_event_rule_evaluation(evaluation) || !name) {
410 status = LTTNG_EVALUATION_STATUS_INVALID;
411 goto end;
412 }
413
414 hit = container_of(
415 evaluation, struct lttng_evaluation_event_rule, parent);
416 *name = hit->name;
417end:
418 return status;
419}
This page took 0.041359 seconds and 4 git commands to generate.