Move firing policy from lttng_trigger to lttng_action
[lttng-tools.git] / src / common / trigger.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/trigger/trigger-internal.h>
9#include <lttng/condition/condition-internal.h>
e393070a
JR
10#include <lttng/condition/on-event-internal.h>
11#include <lttng/condition/on-event.h>
12#include <lttng/condition/on-event-internal.h>
91c96f62
JR
13#include <lttng/condition/buffer-usage.h>
14#include <lttng/event-rule/event-rule-internal.h>
f2e97f59 15#include <lttng/event-expr-internal.h>
a58c490f 16#include <lttng/action/action-internal.h>
3da864a9 17#include <common/credentials.h>
9e620ea7
JG
18#include <common/payload.h>
19#include <common/payload-view.h>
91c96f62 20#include <lttng/domain.h>
a58c490f 21#include <common/error.h>
a02903c0 22#include <common/dynamic-array.h>
3da864a9 23#include <common/optional.h>
a58c490f 24#include <assert.h>
242388e4 25#include <inttypes.h>
a58c490f
JG
26
27LTTNG_HIDDEN
b61776fb 28bool lttng_trigger_validate(const struct lttng_trigger *trigger)
a58c490f
JG
29{
30 bool valid;
31
32 if (!trigger) {
33 valid = false;
34 goto end;
35 }
36
64eafdf6
JR
37 if (!trigger->creds.uid.is_set) {
38 valid = false;
39 goto end;
40 }
41
a58c490f
JG
42 valid = lttng_condition_validate(trigger->condition) &&
43 lttng_action_validate(trigger->action);
44end:
45 return valid;
46}
47
48struct lttng_trigger *lttng_trigger_create(
49 struct lttng_condition *condition,
50 struct lttng_action *action)
51{
52 struct lttng_trigger *trigger = NULL;
53
54 if (!condition || !action) {
55 goto end;
56 }
57
58 trigger = zmalloc(sizeof(struct lttng_trigger));
59 if (!trigger) {
60 goto end;
61 }
62
f01d28b4
JR
63 urcu_ref_init(&trigger->ref);
64
7ca172c1 65 lttng_condition_get(condition);
a58c490f 66 trigger->condition = condition;
7ca172c1
JR
67
68 lttng_action_get(action);
a58c490f 69 trigger->action = action;
3da864a9 70
a58c490f
JG
71end:
72 return trigger;
73}
74
7ca172c1
JR
75/*
76 * Note: the lack of reference counting 'get' on the condition object is normal.
77 * This API was exposed as such in 2.11. The client is not expected to call
78 * lttng_condition_destroy on the returned object.
79 */
a58c490f
JG
80struct lttng_condition *lttng_trigger_get_condition(
81 struct lttng_trigger *trigger)
82{
83 return trigger ? trigger->condition : NULL;
84}
85
9b63a4aa
JG
86const struct lttng_condition *lttng_trigger_get_const_condition(
87 const struct lttng_trigger *trigger)
88{
0de2479d 89 return trigger ? trigger->condition : NULL;
9b63a4aa
JG
90}
91
7ca172c1
JR
92/*
93 * Note: the lack of reference counting 'get' on the action object is normal.
94 * This API was exposed as such in 2.11. The client is not expected to call
95 * lttng_action_destroy on the returned object.
96 */
e2ba1c78 97struct lttng_action *lttng_trigger_get_action(
a58c490f
JG
98 struct lttng_trigger *trigger)
99{
100 return trigger ? trigger->action : NULL;
101}
102
9b63a4aa
JG
103const struct lttng_action *lttng_trigger_get_const_action(
104 const struct lttng_trigger *trigger)
105{
0de2479d 106 return trigger ? trigger->action : NULL;
9b63a4aa
JG
107}
108
f01d28b4 109static void trigger_destroy_ref(struct urcu_ref *ref)
a58c490f 110{
f01d28b4
JR
111 struct lttng_trigger *trigger =
112 container_of(ref, struct lttng_trigger, ref);
7ca172c1
JR
113 struct lttng_action *action = lttng_trigger_get_action(trigger);
114 struct lttng_condition *condition =
115 lttng_trigger_get_condition(trigger);
116
7ca172c1
JR
117 assert(action);
118 assert(condition);
119
120 /* Release ownership. */
121 lttng_action_put(action);
122 lttng_condition_put(condition);
123
242388e4 124 free(trigger->name);
a58c490f
JG
125 free(trigger);
126}
127
f01d28b4
JR
128void lttng_trigger_destroy(struct lttng_trigger *trigger)
129{
130 lttng_trigger_put(trigger);
131}
132
a58c490f 133LTTNG_HIDDEN
c0a66c84
JG
134ssize_t lttng_trigger_create_from_payload(
135 struct lttng_payload_view *src_view,
6808ef55 136 struct lttng_trigger **_trigger)
a58c490f 137{
242388e4 138 ssize_t ret, offset = 0, condition_size, action_size, name_size = 0;
a58c490f
JG
139 struct lttng_condition *condition = NULL;
140 struct lttng_action *action = NULL;
141 const struct lttng_trigger_comm *trigger_comm;
242388e4 142 const char *name = NULL;
64eafdf6
JR
143 struct lttng_credentials creds = {
144 .uid = LTTNG_OPTIONAL_INIT_UNSET,
145 .gid = LTTNG_OPTIONAL_INIT_UNSET,
146 };
6808ef55 147 struct lttng_trigger *trigger = NULL;
3e6e0df2
JG
148 const struct lttng_payload_view trigger_comm_view =
149 lttng_payload_view_from_view(
150 src_view, 0, sizeof(*trigger_comm));
a58c490f 151
6808ef55 152 if (!src_view || !_trigger) {
a58c490f
JG
153 ret = -1;
154 goto end;
155 }
156
3e6e0df2
JG
157 if (!lttng_payload_view_is_valid(&trigger_comm_view)) {
158 /* Payload not large enough to contain the header. */
159 ret = -1;
160 goto end;
161 }
162
a58c490f 163 /* lttng_trigger_comm header */
3e6e0df2 164 trigger_comm = (typeof(trigger_comm)) trigger_comm_view.buffer.data;
64eafdf6
JR
165
166 /* Set the trigger's creds. */
167 if (trigger_comm->uid > (uint64_t) ((uid_t) -1)) {
168 /* UID out of range for this platform. */
169 ret = -1;
170 goto end;
171 }
172
173 LTTNG_OPTIONAL_SET(&creds.uid, trigger_comm->uid);
174
a58c490f 175 offset += sizeof(*trigger_comm);
242388e4
JR
176
177 if (trigger_comm->name_length != 0) {
178 /* Name. */
179 const struct lttng_payload_view name_view =
180 lttng_payload_view_from_view(
3e6e0df2
JG
181 src_view, offset,
182 trigger_comm->name_length);
183
184 if (!lttng_payload_view_is_valid(&name_view)) {
185 ret = -1;
186 goto end;
187 }
242388e4
JR
188
189 name = name_view.buffer.data;
190 if (!lttng_buffer_view_contains_string(&name_view.buffer, name,
191 trigger_comm->name_length)) {
192 ret = -1;
193 goto end;
194 }
195
196 offset += trigger_comm->name_length;
197 name_size = trigger_comm->name_length;
198 }
199
c0a66c84
JG
200 {
201 /* struct lttng_condition */
202 struct lttng_payload_view condition_view =
203 lttng_payload_view_from_view(
204 src_view, offset, -1);
205
206 condition_size = lttng_condition_create_from_payload(&condition_view,
207 &condition);
208 }
a58c490f 209
a58c490f
JG
210 if (condition_size < 0) {
211 ret = condition_size;
212 goto end;
213 }
c0a66c84 214
a58c490f 215 offset += condition_size;
c0a66c84
JG
216 {
217 /* struct lttng_action */
218 struct lttng_payload_view action_view =
219 lttng_payload_view_from_view(
220 src_view, offset, -1);
221
222 action_size = lttng_action_create_from_payload(&action_view, &action);
223 }
a58c490f 224
a58c490f
JG
225 if (action_size < 0) {
226 ret = action_size;
227 goto end;
228 }
229 offset += action_size;
230
231 /* Unexpected size of inner-elements; the buffer is corrupted. */
242388e4 232 if ((ssize_t) trigger_comm->length != condition_size + action_size + name_size) {
a58c490f
JG
233 ret = -1;
234 goto error;
235 }
236
6808ef55
JG
237 trigger = lttng_trigger_create(condition, action);
238 if (!trigger) {
a58c490f
JG
239 ret = -1;
240 goto error;
241 }
c0a66c84 242
6808ef55 243 lttng_trigger_set_credentials(trigger, &creds);
64eafdf6 244
7ca172c1
JR
245 /*
246 * The trigger object owns references to the action and condition
247 * objects.
248 */
249 lttng_condition_put(condition);
250 condition = NULL;
251
252 lttng_action_put(action);
253 action = NULL;
254
242388e4
JR
255 if (name) {
256 const enum lttng_trigger_status status =
6808ef55 257 lttng_trigger_set_name(trigger, name);
242388e4
JR
258
259 if (status != LTTNG_TRIGGER_STATUS_OK) {
260 ret = -1;
261 goto end;
262 }
263 }
264
a58c490f 265 ret = offset;
7ca172c1 266
a58c490f 267error:
1065801b
JG
268 lttng_condition_put(condition);
269 lttng_action_put(action);
7ca172c1 270end:
f5d98ed9 271 if (ret >= 0) {
6808ef55
JG
272 *_trigger = trigger;
273 } else {
274 lttng_trigger_put(trigger);
275 }
276
a58c490f
JG
277 return ret;
278}
279
280/*
a58c490f
JG
281 * Both elements are stored contiguously, see their "*_comm" structure
282 * for the detailed format.
283 */
284LTTNG_HIDDEN
a02903c0 285int lttng_trigger_serialize(const struct lttng_trigger *trigger,
c0a66c84 286 struct lttng_payload *payload)
a58c490f 287{
3647288f 288 int ret;
242388e4 289 size_t header_offset, size_before_payload, size_name;
c0a66c84 290 struct lttng_trigger_comm trigger_comm = {};
3647288f 291 struct lttng_trigger_comm *header;
64eafdf6
JR
292 const struct lttng_credentials *creds = NULL;
293
294 creds = lttng_trigger_get_credentials(trigger);
295 assert(creds);
296
297 trigger_comm.uid = LTTNG_OPTIONAL_GET(creds->uid);
a58c490f 298
242388e4
JR
299 if (trigger->name != NULL) {
300 size_name = strlen(trigger->name) + 1;
301 } else {
302 size_name = 0;
303 }
304
305 trigger_comm.name_length = size_name;
306
c0a66c84
JG
307 header_offset = payload->buffer.size;
308 ret = lttng_dynamic_buffer_append(&payload->buffer, &trigger_comm,
3647288f
JG
309 sizeof(trigger_comm));
310 if (ret) {
a58c490f
JG
311 goto end;
312 }
313
c0a66c84 314 size_before_payload = payload->buffer.size;
242388e4
JR
315
316 /* Trigger name. */
317 ret = lttng_dynamic_buffer_append(
318 &payload->buffer, trigger->name, size_name);
319 if (ret) {
320 goto end;
321 }
322
c0a66c84 323 ret = lttng_condition_serialize(trigger->condition, payload);
3647288f 324 if (ret) {
a58c490f
JG
325 goto end;
326 }
a58c490f 327
c0a66c84 328 ret = lttng_action_serialize(trigger->action, payload);
3647288f 329 if (ret) {
a58c490f
JG
330 goto end;
331 }
a58c490f 332
3647288f 333 /* Update payload size. */
c0a66c84
JG
334 header = (typeof(header)) (payload->buffer.data + header_offset);
335 header->length = payload->buffer.size - size_before_payload;
a58c490f
JG
336end:
337 return ret;
338}
3da864a9 339
85c06c44
JR
340LTTNG_HIDDEN
341bool lttng_trigger_is_equal(
342 const struct lttng_trigger *a, const struct lttng_trigger *b)
343{
1ca78886
FD
344 if (strcmp(a->name, b->name) != 0) {
345 return false;
346 }
347
85c06c44
JR
348 if (!lttng_condition_is_equal(a->condition, b->condition)) {
349 return false;
350 }
351
352 if (!lttng_action_is_equal(a->action, b->action)) {
353 return false;
354 }
355
356 if (!lttng_credentials_is_equal(lttng_trigger_get_credentials(a),
357 lttng_trigger_get_credentials(b))) {
358 return false;
359 }
360
361 return true;
362}
363
242388e4
JR
364enum lttng_trigger_status lttng_trigger_set_name(struct lttng_trigger *trigger,
365 const char* name)
366{
367 char *name_copy = NULL;
368 enum lttng_trigger_status status = LTTNG_TRIGGER_STATUS_OK;
369
370 if (!trigger || !name ||
371 strlen(name) == 0) {
372 status = LTTNG_TRIGGER_STATUS_INVALID;
373 goto end;
374 }
375
376 name_copy = strdup(name);
377 if (!name_copy) {
378 status = LTTNG_TRIGGER_STATUS_ERROR;
379 goto end;
380 }
381
382 free(trigger->name);
383
384 trigger->name = name_copy;
385 name_copy = NULL;
386end:
387 return status;
388}
389
390enum lttng_trigger_status lttng_trigger_get_name(
391 const struct lttng_trigger *trigger, const char **name)
392{
393 enum lttng_trigger_status status = LTTNG_TRIGGER_STATUS_OK;
394
395 if (!trigger || !name) {
396 status = LTTNG_TRIGGER_STATUS_INVALID;
397 goto end;
398 }
399
400 if (!trigger->name) {
401 status = LTTNG_TRIGGER_STATUS_UNSET;
402 }
403
404 *name = trigger->name;
405end:
406 return status;
407}
408
409LTTNG_HIDDEN
410int lttng_trigger_assign_name(struct lttng_trigger *dst,
411 const struct lttng_trigger *src)
412{
413 int ret = 0;
414 enum lttng_trigger_status status;
415
416 status = lttng_trigger_set_name(dst, src->name);
417 if (status != LTTNG_TRIGGER_STATUS_OK) {
418 ret = -1;
419 ERR("Failed to set name for trigger");
420 goto end;
421 }
422end:
423 return ret;
424}
425
e6887944
JR
426LTTNG_HIDDEN
427void lttng_trigger_set_tracer_token(struct lttng_trigger *trigger,
428 uint64_t token)
429{
430 assert(trigger);
431 LTTNG_OPTIONAL_SET(&trigger->tracer_token, token);
432}
433
434LTTNG_HIDDEN
435uint64_t lttng_trigger_get_tracer_token(const struct lttng_trigger *trigger)
436{
437 assert(trigger);
438
439 return LTTNG_OPTIONAL_GET(trigger->tracer_token);
440}
441
242388e4
JR
442LTTNG_HIDDEN
443int lttng_trigger_generate_name(struct lttng_trigger *trigger,
444 uint64_t unique_id)
445{
446 int ret = 0;
447 char *generated_name = NULL;
448
449 ret = asprintf(&generated_name, "T%" PRIu64 "", unique_id);
450 if (ret < 0) {
451 ERR("Failed to generate trigger name");
452 ret = -1;
453 goto end;
454 }
455
456 ret = 0;
457 free(trigger->name);
458 trigger->name = generated_name;
459end:
460 return ret;
461}
462
f01d28b4
JR
463LTTNG_HIDDEN
464void lttng_trigger_get(struct lttng_trigger *trigger)
465{
466 urcu_ref_get(&trigger->ref);
467}
468
469LTTNG_HIDDEN
470void lttng_trigger_put(struct lttng_trigger *trigger)
471{
472 if (!trigger) {
473 return;
474 }
475
476 urcu_ref_put(&trigger->ref , trigger_destroy_ref);
477}
478
a02903c0
JR
479static void delete_trigger_array_element(void *ptr)
480{
481 struct lttng_trigger *trigger = ptr;
482
483 lttng_trigger_put(trigger);
484}
485
486LTTNG_HIDDEN
487struct lttng_triggers *lttng_triggers_create(void)
488{
489 struct lttng_triggers *triggers = NULL;
490
491 triggers = zmalloc(sizeof(*triggers));
492 if (!triggers) {
493 goto end;
494 }
495
496 lttng_dynamic_pointer_array_init(&triggers->array, delete_trigger_array_element);
497
498end:
499 return triggers;
500}
501
502LTTNG_HIDDEN
503struct lttng_trigger *lttng_triggers_borrow_mutable_at_index(
504 const struct lttng_triggers *triggers, unsigned int index)
505{
506 struct lttng_trigger *trigger = NULL;
507
508 assert(triggers);
509 if (index >= lttng_dynamic_pointer_array_get_count(&triggers->array)) {
510 goto end;
511 }
512
513 trigger = (struct lttng_trigger *)
514 lttng_dynamic_pointer_array_get_pointer(
515 &triggers->array, index);
516end:
517 return trigger;
518}
519
520LTTNG_HIDDEN
521int lttng_triggers_add(
522 struct lttng_triggers *triggers, struct lttng_trigger *trigger)
523{
524 int ret;
525
526 assert(triggers);
527 assert(trigger);
528
529 lttng_trigger_get(trigger);
530
531 ret = lttng_dynamic_pointer_array_add_pointer(&triggers->array, trigger);
532 if (ret) {
533 lttng_trigger_put(trigger);
534 }
535
536 return ret;
537}
538
539const struct lttng_trigger *lttng_triggers_get_at_index(
540 const struct lttng_triggers *triggers, unsigned int index)
541{
542 return lttng_triggers_borrow_mutable_at_index(triggers, index);
543}
544
545enum lttng_trigger_status lttng_triggers_get_count(const struct lttng_triggers *triggers, unsigned int *count)
546{
547 enum lttng_trigger_status status = LTTNG_TRIGGER_STATUS_OK;
548
549 if (!triggers || !count) {
550 status = LTTNG_TRIGGER_STATUS_INVALID;
551 goto end;
552 }
553
554 *count = lttng_dynamic_pointer_array_get_count(&triggers->array);
555end:
556 return status;
557}
558
559void lttng_triggers_destroy(struct lttng_triggers *triggers)
560{
561 if (!triggers) {
562 return;
563 }
564
565 lttng_dynamic_pointer_array_reset(&triggers->array);
566 free(triggers);
567}
568
569int lttng_triggers_serialize(const struct lttng_triggers *triggers,
570 struct lttng_payload *payload)
571{
572 int ret;
573 unsigned int i, count;
574 size_t size_before_payload;
575 struct lttng_triggers_comm triggers_comm = {};
576 struct lttng_triggers_comm *header;
577 enum lttng_trigger_status status;
578 const size_t header_offset = payload->buffer.size;
579
580 status = lttng_triggers_get_count(triggers, &count);
581 if (status != LTTNG_TRIGGER_STATUS_OK) {
582 ret = LTTNG_ERR_INVALID;
583 goto end;
584 }
585
586 triggers_comm.count = count;
587
588 /* Placeholder header; updated at the end. */
589 ret = lttng_dynamic_buffer_append(&payload->buffer, &triggers_comm,
590 sizeof(triggers_comm));
591 if (ret) {
592 goto end;
593 }
594
595 size_before_payload = payload->buffer.size;
596
597 for (i = 0; i < count; i++) {
598 const struct lttng_trigger *trigger =
599 lttng_triggers_get_at_index(triggers, i);
600
601 assert(trigger);
602
603 ret = lttng_trigger_serialize(trigger, payload);
604 if (ret) {
605 goto end;
606 }
607 }
608
609 /* Update payload size. */
610 header = (struct lttng_triggers_comm *) ((char *) payload->buffer.data + header_offset);
611 header->length = payload->buffer.size - size_before_payload;
612end:
613 return ret;
614}
615
616LTTNG_HIDDEN
617ssize_t lttng_triggers_create_from_payload(
618 struct lttng_payload_view *src_view,
619 struct lttng_triggers **triggers)
620{
621 ssize_t ret, offset = 0, triggers_size = 0;
622 unsigned int i;
623 const struct lttng_triggers_comm *triggers_comm;
624 struct lttng_triggers *local_triggers = NULL;
625
626 if (!src_view || !triggers) {
627 ret = -1;
628 goto error;
629 }
630
631 /* lttng_trigger_comms header */
632 triggers_comm = (const struct lttng_triggers_comm *) src_view->buffer.data;
633 offset += sizeof(*triggers_comm);
634
635 local_triggers = lttng_triggers_create();
636 if (!local_triggers) {
637 ret = -1;
638 goto error;
639 }
640
641 for (i = 0; i < triggers_comm->count; i++) {
642 struct lttng_trigger *trigger = NULL;
643 struct lttng_payload_view trigger_view =
644 lttng_payload_view_from_view(src_view, offset, -1);
645 ssize_t trigger_size;
646
647 trigger_size = lttng_trigger_create_from_payload(
648 &trigger_view, &trigger);
649 if (trigger_size < 0) {
650 ret = trigger_size;
651 goto error;
652 }
653
654 /* Transfer ownership of the trigger to the collection. */
655 ret = lttng_triggers_add(local_triggers, trigger);
656 lttng_trigger_put(trigger);
657 if (ret < 0) {
658 ret = -1;
659 goto error;
660 }
661
662 offset += trigger_size;
663 triggers_size += trigger_size;
664 }
665
666 /* Unexpected size of inner-elements; the buffer is corrupted. */
667 if ((ssize_t) triggers_comm->length != triggers_size) {
668 ret = -1;
669 goto error;
670 }
671
672 /* Pass ownership to caller. */
673 *triggers = local_triggers;
674 local_triggers = NULL;
675
676 ret = offset;
677error:
678
679 lttng_triggers_destroy(local_triggers);
680 return ret;
681}
682
3da864a9
JR
683LTTNG_HIDDEN
684const struct lttng_credentials *lttng_trigger_get_credentials(
685 const struct lttng_trigger *trigger)
686{
64eafdf6 687 return &trigger->creds;
3da864a9
JR
688}
689
690LTTNG_HIDDEN
64eafdf6 691void lttng_trigger_set_credentials(struct lttng_trigger *trigger,
3da864a9
JR
692 const struct lttng_credentials *creds)
693{
694 assert(creds);
64eafdf6
JR
695 trigger->creds = *creds;
696}
697
698enum lttng_trigger_status lttng_trigger_set_owner_uid(
699 struct lttng_trigger *trigger, uid_t uid)
700{
701 enum lttng_trigger_status ret = LTTNG_TRIGGER_STATUS_OK;
702 const struct lttng_credentials creds = {
703 .uid = LTTNG_OPTIONAL_INIT_VALUE(uid),
704 .gid = LTTNG_OPTIONAL_INIT_UNSET,
705 };
706
707 if (!trigger) {
708 ret = LTTNG_TRIGGER_STATUS_INVALID;
709 goto end;
710 }
711
712 /* Client-side validation only to report a clearer error. */
713 if (geteuid() != 0) {
714 ret = LTTNG_TRIGGER_STATUS_PERMISSION_DENIED;
715 goto end;
716 }
717
718 lttng_trigger_set_credentials(trigger, &creds);
719
720end:
721 return ret;
722}
723
724enum lttng_trigger_status lttng_trigger_get_owner_uid(
725 const struct lttng_trigger *trigger, uid_t *uid)
726{
727 enum lttng_trigger_status ret = LTTNG_TRIGGER_STATUS_OK;
728 const struct lttng_credentials *creds = NULL;
729
730 if (!trigger || !uid ) {
731 ret = LTTNG_TRIGGER_STATUS_INVALID;
732 goto end;
733 }
734
735 if (!trigger->creds.uid.is_set ) {
736 ret = LTTNG_TRIGGER_STATUS_UNSET;
737 goto end;
738 }
739
740 creds = lttng_trigger_get_credentials(trigger);
741 *uid = lttng_credentials_get_uid(creds);
742
743end:
744 return ret;
3da864a9 745}
5c504c41 746
91c96f62
JR
747LTTNG_HIDDEN
748enum lttng_domain_type lttng_trigger_get_underlying_domain_type_restriction(
749 const struct lttng_trigger *trigger)
750{
751 enum lttng_domain_type type = LTTNG_DOMAIN_NONE;
752 const struct lttng_event_rule *event_rule;
753 enum lttng_condition_status c_status;
754 enum lttng_condition_type c_type;
755
756 assert(trigger);
757 assert(trigger->condition);
758
759 c_type = lttng_condition_get_type(trigger->condition);
760 assert (c_type != LTTNG_CONDITION_TYPE_UNKNOWN);
761
762 switch (c_type) {
763 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
764 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
765 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
766 /* Apply to any domain. */
767 type = LTTNG_DOMAIN_NONE;
768 break;
d602bd6a 769 case LTTNG_CONDITION_TYPE_ON_EVENT:
91c96f62 770 /* Return the domain of the event rule. */
d602bd6a 771 c_status = lttng_condition_on_event_get_rule(
91c96f62
JR
772 trigger->condition, &event_rule);
773 assert(c_status == LTTNG_CONDITION_STATUS_OK);
774 type = lttng_event_rule_get_domain_type(event_rule);
775 break;
776 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
777 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
778 /* Return the domain of the channel being monitored. */
779 c_status = lttng_condition_buffer_usage_get_domain_type(
780 trigger->condition, &type);
781 assert(c_status == LTTNG_CONDITION_STATUS_OK);
782 break;
783 default:
784 abort();
785 }
786
787 return type;
788}
58daac01
JR
789
790/*
791 * Generate bytecode related to the trigger.
792 * On success LTTNG_OK. On error, returns lttng_error code.
793 */
794LTTNG_HIDDEN
795enum lttng_error_code lttng_trigger_generate_bytecode(
796 struct lttng_trigger *trigger,
797 const struct lttng_credentials *creds)
798{
799 enum lttng_error_code ret;
800 struct lttng_condition *condition = NULL;
801
802 condition = lttng_trigger_get_condition(trigger);
803 if (!condition) {
804 ret = LTTNG_ERR_INVALID_TRIGGER;
805 goto end;
806 }
807
808 switch (lttng_condition_get_type(condition)) {
d602bd6a 809 case LTTNG_CONDITION_TYPE_ON_EVENT:
58daac01
JR
810 {
811 struct lttng_event_rule *event_rule;
812 const enum lttng_condition_status condition_status =
d602bd6a 813 lttng_condition_on_event_borrow_rule_mutable(
58daac01
JR
814 condition, &event_rule);
815
816 assert(condition_status == LTTNG_CONDITION_STATUS_OK);
f2e97f59
JR
817
818 /* Generate the filter bytecode. */
58daac01
JR
819 ret = lttng_event_rule_generate_filter_bytecode(
820 event_rule, creds);
821 if (ret != LTTNG_OK) {
822 goto end;
823 }
824
f2e97f59 825 /* Generate the capture bytecode. */
d602bd6a 826 ret = lttng_condition_on_event_generate_capture_descriptor_bytecode(
f2e97f59
JR
827 condition);
828 if (ret != LTTNG_OK) {
829 goto end;
830 }
831
58daac01
JR
832 ret = LTTNG_OK;
833 break;
834 }
835 default:
836 ret = LTTNG_OK;
837 break;
838 }
839end:
840 return ret;
841}
b61776fb
SM
842
843LTTNG_HIDDEN
844struct lttng_trigger *lttng_trigger_copy(const struct lttng_trigger *trigger)
845{
846 int ret;
847 struct lttng_payload copy_buffer;
848 struct lttng_trigger *copy = NULL;
849
850 lttng_payload_init(&copy_buffer);
851
852 ret = lttng_trigger_serialize(trigger, &copy_buffer);
853 if (ret < 0) {
854 goto end;
855 }
856
857 {
858 struct lttng_payload_view view =
859 lttng_payload_view_from_payload(
860 &copy_buffer, 0, -1);
861 ret = lttng_trigger_create_from_payload(
862 &view, &copy);
863 if (ret < 0) {
864 copy = NULL;
865 goto end;
866 }
867 }
868
869end:
870 lttng_payload_reset(&copy_buffer);
871 return copy;
872}
c738df17
FD
873
874LTTNG_HIDDEN
875bool lttng_trigger_needs_tracer_notifier(const struct lttng_trigger *trigger)
876{
877 bool needs_tracer_notifier = false;
878 const struct lttng_condition *condition =
879 lttng_trigger_get_const_condition(trigger);
880
881 switch (lttng_condition_get_type(condition)) {
882 case LTTNG_CONDITION_TYPE_ON_EVENT:
883 needs_tracer_notifier = true;
884 goto end;
885 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
886 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
887 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
888 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
889 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
890 goto end;
891 case LTTNG_CONDITION_TYPE_UNKNOWN:
892 default:
893 abort();
894 }
895end:
896 return needs_tracer_notifier;
897}
This page took 0.075639 seconds and 4 git commands to generate.