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