Rename lttng_event_rule_userspace_probe to lttng_event_rule_kernel_uprobe
[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/event-rule-matches-internal.h>
20 #include <lttng/condition/event-rule-matches.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 (!!a->name != !!b->name) {
350 /* Both must be either anonymous or named. */
351 return false;
352 }
353
354 if (a->name && strcmp(a->name, b->name) != 0) {
355 return false;
356 }
357
358 if (!lttng_condition_is_equal(a->condition, b->condition)) {
359 return false;
360 }
361
362 if (!lttng_action_is_equal(a->action, b->action)) {
363 return false;
364 }
365
366 if (!lttng_credentials_is_equal(lttng_trigger_get_credentials(a),
367 lttng_trigger_get_credentials(b))) {
368 return false;
369 }
370
371 return true;
372 }
373
374 LTTNG_HIDDEN
375 enum lttng_trigger_status lttng_trigger_set_name(struct lttng_trigger *trigger,
376 const char* name)
377 {
378 char *name_copy = NULL;
379 enum lttng_trigger_status status = LTTNG_TRIGGER_STATUS_OK;
380
381 if (!trigger) {
382 status = LTTNG_TRIGGER_STATUS_INVALID;
383 goto end;
384 }
385
386 if (name) {
387 name_copy = strdup(name);
388 if (!name_copy) {
389 status = LTTNG_TRIGGER_STATUS_ERROR;
390 goto end;
391 }
392 }
393
394 free(trigger->name);
395
396 trigger->name = name_copy;
397 name_copy = NULL;
398 end:
399 return status;
400 }
401
402 enum lttng_trigger_status lttng_trigger_get_name(
403 const struct lttng_trigger *trigger, const char **name)
404 {
405 enum lttng_trigger_status status = LTTNG_TRIGGER_STATUS_OK;
406
407 if (!trigger || !name) {
408 status = LTTNG_TRIGGER_STATUS_INVALID;
409 goto end;
410 }
411
412 if (!trigger->name) {
413 status = LTTNG_TRIGGER_STATUS_UNSET;
414 }
415
416 *name = trigger->name;
417 end:
418 return status;
419 }
420
421 LTTNG_HIDDEN
422 int lttng_trigger_assign_name(struct lttng_trigger *dst,
423 const struct lttng_trigger *src)
424 {
425 int ret = 0;
426 enum lttng_trigger_status status;
427
428 status = lttng_trigger_set_name(dst, src->name);
429 if (status != LTTNG_TRIGGER_STATUS_OK) {
430 ret = -1;
431 ERR("Failed to set name for trigger");
432 goto end;
433 }
434 end:
435 return ret;
436 }
437
438 LTTNG_HIDDEN
439 void lttng_trigger_set_tracer_token(struct lttng_trigger *trigger,
440 uint64_t token)
441 {
442 assert(trigger);
443 LTTNG_OPTIONAL_SET(&trigger->tracer_token, token);
444 }
445
446 LTTNG_HIDDEN
447 uint64_t lttng_trigger_get_tracer_token(const struct lttng_trigger *trigger)
448 {
449 assert(trigger);
450
451 return LTTNG_OPTIONAL_GET(trigger->tracer_token);
452 }
453
454 LTTNG_HIDDEN
455 int lttng_trigger_generate_name(struct lttng_trigger *trigger,
456 uint64_t unique_id)
457 {
458 int ret = 0;
459 char *generated_name = NULL;
460
461 ret = asprintf(&generated_name, "T%" PRIu64 "", unique_id);
462 if (ret < 0) {
463 ERR("Failed to generate trigger name");
464 ret = -1;
465 goto end;
466 }
467
468 ret = 0;
469 free(trigger->name);
470 trigger->name = generated_name;
471 end:
472 return ret;
473 }
474
475 LTTNG_HIDDEN
476 void lttng_trigger_get(struct lttng_trigger *trigger)
477 {
478 urcu_ref_get(&trigger->ref);
479 }
480
481 LTTNG_HIDDEN
482 void lttng_trigger_put(struct lttng_trigger *trigger)
483 {
484 if (!trigger) {
485 return;
486 }
487
488 urcu_ref_put(&trigger->ref , trigger_destroy_ref);
489 }
490
491 static void delete_trigger_array_element(void *ptr)
492 {
493 struct lttng_trigger *trigger = ptr;
494
495 lttng_trigger_put(trigger);
496 }
497
498 LTTNG_HIDDEN
499 struct lttng_triggers *lttng_triggers_create(void)
500 {
501 struct lttng_triggers *triggers = NULL;
502
503 triggers = zmalloc(sizeof(*triggers));
504 if (!triggers) {
505 goto end;
506 }
507
508 lttng_dynamic_pointer_array_init(&triggers->array, delete_trigger_array_element);
509
510 end:
511 return triggers;
512 }
513
514 LTTNG_HIDDEN
515 struct lttng_trigger *lttng_triggers_borrow_mutable_at_index(
516 const struct lttng_triggers *triggers, unsigned int index)
517 {
518 struct lttng_trigger *trigger = NULL;
519
520 assert(triggers);
521 if (index >= lttng_dynamic_pointer_array_get_count(&triggers->array)) {
522 goto end;
523 }
524
525 trigger = (struct lttng_trigger *)
526 lttng_dynamic_pointer_array_get_pointer(
527 &triggers->array, index);
528 end:
529 return trigger;
530 }
531
532 LTTNG_HIDDEN
533 int lttng_triggers_add(
534 struct lttng_triggers *triggers, struct lttng_trigger *trigger)
535 {
536 int ret;
537
538 assert(triggers);
539 assert(trigger);
540
541 lttng_trigger_get(trigger);
542
543 ret = lttng_dynamic_pointer_array_add_pointer(&triggers->array, trigger);
544 if (ret) {
545 lttng_trigger_put(trigger);
546 }
547
548 return ret;
549 }
550
551 const struct lttng_trigger *lttng_triggers_get_at_index(
552 const struct lttng_triggers *triggers, unsigned int index)
553 {
554 return lttng_triggers_borrow_mutable_at_index(triggers, index);
555 }
556
557 enum lttng_trigger_status lttng_triggers_get_count(const struct lttng_triggers *triggers, unsigned int *count)
558 {
559 enum lttng_trigger_status status = LTTNG_TRIGGER_STATUS_OK;
560
561 if (!triggers || !count) {
562 status = LTTNG_TRIGGER_STATUS_INVALID;
563 goto end;
564 }
565
566 *count = lttng_dynamic_pointer_array_get_count(&triggers->array);
567 end:
568 return status;
569 }
570
571 void lttng_triggers_destroy(struct lttng_triggers *triggers)
572 {
573 if (!triggers) {
574 return;
575 }
576
577 lttng_dynamic_pointer_array_reset(&triggers->array);
578 free(triggers);
579 }
580
581 int lttng_triggers_serialize(const struct lttng_triggers *triggers,
582 struct lttng_payload *payload)
583 {
584 int ret;
585 unsigned int i, count;
586 size_t size_before_payload;
587 struct lttng_triggers_comm triggers_comm = {};
588 struct lttng_triggers_comm *header;
589 enum lttng_trigger_status status;
590 const size_t header_offset = payload->buffer.size;
591
592 status = lttng_triggers_get_count(triggers, &count);
593 if (status != LTTNG_TRIGGER_STATUS_OK) {
594 ret = LTTNG_ERR_INVALID;
595 goto end;
596 }
597
598 triggers_comm.count = count;
599
600 /* Placeholder header; updated at the end. */
601 ret = lttng_dynamic_buffer_append(&payload->buffer, &triggers_comm,
602 sizeof(triggers_comm));
603 if (ret) {
604 goto end;
605 }
606
607 size_before_payload = payload->buffer.size;
608
609 for (i = 0; i < count; i++) {
610 const struct lttng_trigger *trigger =
611 lttng_triggers_get_at_index(triggers, i);
612
613 assert(trigger);
614
615 ret = lttng_trigger_serialize(trigger, payload);
616 if (ret) {
617 goto end;
618 }
619 }
620
621 /* Update payload size. */
622 header = (struct lttng_triggers_comm *) ((char *) payload->buffer.data + header_offset);
623 header->length = payload->buffer.size - size_before_payload;
624 end:
625 return ret;
626 }
627
628 LTTNG_HIDDEN
629 ssize_t lttng_triggers_create_from_payload(
630 struct lttng_payload_view *src_view,
631 struct lttng_triggers **triggers)
632 {
633 ssize_t ret, offset = 0, triggers_size = 0;
634 unsigned int i;
635 const struct lttng_triggers_comm *triggers_comm;
636 struct lttng_triggers *local_triggers = NULL;
637
638 if (!src_view || !triggers) {
639 ret = -1;
640 goto error;
641 }
642
643 /* lttng_trigger_comms header */
644 triggers_comm = (const struct lttng_triggers_comm *) src_view->buffer.data;
645 offset += sizeof(*triggers_comm);
646
647 local_triggers = lttng_triggers_create();
648 if (!local_triggers) {
649 ret = -1;
650 goto error;
651 }
652
653 for (i = 0; i < triggers_comm->count; i++) {
654 struct lttng_trigger *trigger = NULL;
655 struct lttng_payload_view trigger_view =
656 lttng_payload_view_from_view(src_view, offset, -1);
657 ssize_t trigger_size;
658
659 trigger_size = lttng_trigger_create_from_payload(
660 &trigger_view, &trigger);
661 if (trigger_size < 0) {
662 ret = trigger_size;
663 goto error;
664 }
665
666 /* Transfer ownership of the trigger to the collection. */
667 ret = lttng_triggers_add(local_triggers, trigger);
668 lttng_trigger_put(trigger);
669 if (ret < 0) {
670 ret = -1;
671 goto error;
672 }
673
674 offset += trigger_size;
675 triggers_size += trigger_size;
676 }
677
678 /* Unexpected size of inner-elements; the buffer is corrupted. */
679 if ((ssize_t) triggers_comm->length != triggers_size) {
680 ret = -1;
681 goto error;
682 }
683
684 /* Pass ownership to caller. */
685 *triggers = local_triggers;
686 local_triggers = NULL;
687
688 ret = offset;
689 error:
690
691 lttng_triggers_destroy(local_triggers);
692 return ret;
693 }
694
695 LTTNG_HIDDEN
696 const struct lttng_credentials *lttng_trigger_get_credentials(
697 const struct lttng_trigger *trigger)
698 {
699 return &trigger->creds;
700 }
701
702 LTTNG_HIDDEN
703 void lttng_trigger_set_credentials(struct lttng_trigger *trigger,
704 const struct lttng_credentials *creds)
705 {
706 assert(creds);
707 trigger->creds = *creds;
708 }
709
710 enum lttng_trigger_status lttng_trigger_set_owner_uid(
711 struct lttng_trigger *trigger, uid_t uid)
712 {
713 enum lttng_trigger_status ret = LTTNG_TRIGGER_STATUS_OK;
714 const uid_t euid = geteuid();
715 const struct lttng_credentials creds = {
716 .uid = LTTNG_OPTIONAL_INIT_VALUE(uid),
717 .gid = LTTNG_OPTIONAL_INIT_UNSET,
718 };
719
720 if (!trigger) {
721 ret = LTTNG_TRIGGER_STATUS_INVALID;
722 goto end;
723 }
724
725 /* Client-side validation only to report a clearer error. */
726 if (euid != 0 && euid != uid) {
727 ret = LTTNG_TRIGGER_STATUS_PERMISSION_DENIED;
728 goto end;
729 }
730
731 lttng_trigger_set_credentials(trigger, &creds);
732
733 end:
734 return ret;
735 }
736
737 enum lttng_trigger_status lttng_trigger_get_owner_uid(
738 const struct lttng_trigger *trigger, uid_t *uid)
739 {
740 enum lttng_trigger_status ret = LTTNG_TRIGGER_STATUS_OK;
741 const struct lttng_credentials *creds = NULL;
742
743 if (!trigger || !uid ) {
744 ret = LTTNG_TRIGGER_STATUS_INVALID;
745 goto end;
746 }
747
748 if (!trigger->creds.uid.is_set ) {
749 ret = LTTNG_TRIGGER_STATUS_UNSET;
750 goto end;
751 }
752
753 creds = lttng_trigger_get_credentials(trigger);
754 *uid = lttng_credentials_get_uid(creds);
755
756 end:
757 return ret;
758 }
759
760 LTTNG_HIDDEN
761 enum lttng_domain_type lttng_trigger_get_underlying_domain_type_restriction(
762 const struct lttng_trigger *trigger)
763 {
764 enum lttng_domain_type type = LTTNG_DOMAIN_NONE;
765 const struct lttng_event_rule *event_rule;
766 enum lttng_condition_status c_status;
767 enum lttng_condition_type c_type;
768
769 assert(trigger);
770 assert(trigger->condition);
771
772 c_type = lttng_condition_get_type(trigger->condition);
773 assert (c_type != LTTNG_CONDITION_TYPE_UNKNOWN);
774
775 switch (c_type) {
776 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
777 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
778 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
779 /* Apply to any domain. */
780 type = LTTNG_DOMAIN_NONE;
781 break;
782 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES:
783 /* Return the domain of the event rule. */
784 c_status = lttng_condition_event_rule_matches_get_rule(
785 trigger->condition, &event_rule);
786 assert(c_status == LTTNG_CONDITION_STATUS_OK);
787 type = lttng_event_rule_get_domain_type(event_rule);
788 break;
789 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
790 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
791 /* Return the domain of the channel being monitored. */
792 c_status = lttng_condition_buffer_usage_get_domain_type(
793 trigger->condition, &type);
794 assert(c_status == LTTNG_CONDITION_STATUS_OK);
795 break;
796 default:
797 abort();
798 }
799
800 return type;
801 }
802
803 /*
804 * Generate bytecode related to the trigger.
805 * On success LTTNG_OK. On error, returns lttng_error code.
806 */
807 LTTNG_HIDDEN
808 enum lttng_error_code lttng_trigger_generate_bytecode(
809 struct lttng_trigger *trigger,
810 const struct lttng_credentials *creds)
811 {
812 enum lttng_error_code ret;
813 struct lttng_condition *condition = NULL;
814
815 condition = lttng_trigger_get_condition(trigger);
816 if (!condition) {
817 ret = LTTNG_ERR_INVALID_TRIGGER;
818 goto end;
819 }
820
821 switch (lttng_condition_get_type(condition)) {
822 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES:
823 {
824 struct lttng_event_rule *event_rule;
825 const enum lttng_condition_status condition_status =
826 lttng_condition_event_rule_matches_borrow_rule_mutable(
827 condition, &event_rule);
828
829 assert(condition_status == LTTNG_CONDITION_STATUS_OK);
830
831 /* Generate the filter bytecode. */
832 ret = lttng_event_rule_generate_filter_bytecode(
833 event_rule, creds);
834 if (ret != LTTNG_OK) {
835 goto end;
836 }
837
838 /* Generate the capture bytecode. */
839 ret = lttng_condition_event_rule_matches_generate_capture_descriptor_bytecode(
840 condition);
841 if (ret != LTTNG_OK) {
842 goto end;
843 }
844
845 ret = LTTNG_OK;
846 break;
847 }
848 default:
849 ret = LTTNG_OK;
850 break;
851 }
852 end:
853 return ret;
854 }
855
856 LTTNG_HIDDEN
857 struct lttng_trigger *lttng_trigger_copy(const struct lttng_trigger *trigger)
858 {
859 int ret;
860 struct lttng_payload copy_buffer;
861 struct lttng_condition *condition_copy = NULL;
862 struct lttng_action *action_copy = NULL;
863 struct lttng_trigger *copy = NULL;
864 enum lttng_trigger_status trigger_status;
865 const char *trigger_name;
866 uid_t trigger_owner_uid;
867
868 lttng_payload_init(&copy_buffer);
869
870 ret = lttng_condition_serialize(trigger->condition, &copy_buffer);
871 if (ret < 0) {
872 goto end;
873 }
874
875 {
876 struct lttng_payload_view view =
877 lttng_payload_view_from_payload(
878 &copy_buffer, 0, -1);
879
880 ret = lttng_condition_create_from_payload(
881 &view, &condition_copy);
882 if (ret < 0) {
883 goto end;
884 }
885 }
886
887 lttng_payload_clear(&copy_buffer);
888
889 ret = lttng_action_serialize(trigger->action, &copy_buffer);
890 if (ret < 0) {
891 goto end;
892 }
893
894 {
895 struct lttng_payload_view view =
896 lttng_payload_view_from_payload(
897 &copy_buffer, 0, -1);
898
899 ret = lttng_action_create_from_payload(
900 &view, &action_copy);
901 if (ret < 0) {
902 goto end;
903 }
904 }
905
906 copy = lttng_trigger_create(condition_copy, action_copy);
907 if (!copy) {
908 ERR("Failed to allocate trigger during trigger copy");
909 goto end;
910 }
911
912 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
913 switch (trigger_status) {
914 case LTTNG_TRIGGER_STATUS_OK:
915 trigger_status = lttng_trigger_set_name(copy, trigger_name);
916 if (trigger_status != LTTNG_TRIGGER_STATUS_OK) {
917 ERR("Failed to set name of new trigger during copy");
918 goto error_cleanup_trigger;
919 }
920 break;
921 case LTTNG_TRIGGER_STATUS_UNSET:
922 break;
923 default:
924 ERR("Failed to get name of original trigger during copy");
925 goto error_cleanup_trigger;
926 }
927
928 trigger_status = lttng_trigger_get_owner_uid(
929 trigger, &trigger_owner_uid);
930 switch (trigger_status) {
931 case LTTNG_TRIGGER_STATUS_OK:
932 LTTNG_OPTIONAL_SET(&copy->creds.uid, trigger_owner_uid);
933 break;
934 case LTTNG_TRIGGER_STATUS_UNSET:
935 break;
936 default:
937 ERR("Failed to get owner uid of original trigger during copy");
938 goto error_cleanup_trigger;
939 }
940
941 copy->tracer_token = trigger->tracer_token;
942 copy->registered = trigger->registered;
943 goto end;
944
945 error_cleanup_trigger:
946 lttng_trigger_destroy(copy);
947 copy = NULL;
948 end:
949 lttng_condition_put(condition_copy);
950 lttng_action_put(action_copy);
951 lttng_payload_reset(&copy_buffer);
952 return copy;
953 }
954
955 LTTNG_HIDDEN
956 bool lttng_trigger_needs_tracer_notifier(const struct lttng_trigger *trigger)
957 {
958 bool needs_tracer_notifier = false;
959 const struct lttng_condition *condition =
960 lttng_trigger_get_const_condition(trigger);
961
962 switch (lttng_condition_get_type(condition)) {
963 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES:
964 needs_tracer_notifier = true;
965 goto end;
966 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
967 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
968 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
969 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
970 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
971 goto end;
972 case LTTNG_CONDITION_TYPE_UNKNOWN:
973 default:
974 abort();
975 }
976 end:
977 return needs_tracer_notifier;
978 }
979
980 LTTNG_HIDDEN
981 void lttng_trigger_set_as_registered(struct lttng_trigger *trigger)
982 {
983 pthread_mutex_lock(&trigger->lock);
984 trigger->registered = true;
985 pthread_mutex_unlock(&trigger->lock);
986 }
987
988 LTTNG_HIDDEN
989 void lttng_trigger_set_as_unregistered(struct lttng_trigger *trigger)
990 {
991 pthread_mutex_lock(&trigger->lock);
992 trigger->registered = false;
993 pthread_mutex_unlock(&trigger->lock);
994 }
995
996 /*
997 * The trigger must be locked before calling lttng_trigger_registered.
998 * The lock is necessary since a trigger can be unregistered at anytime.
999 * Manipulations requiring that the trigger be registered must always acquire
1000 * the trigger lock for the duration of the manipulation using
1001 * `lttng_trigger_lock` and `lttng_trigger_unlock`.
1002 */
1003 LTTNG_HIDDEN
1004 bool lttng_trigger_is_registered(struct lttng_trigger *trigger)
1005 {
1006 ASSERT_LOCKED(trigger->lock);
1007 return trigger->registered;
1008 }
1009
1010 LTTNG_HIDDEN
1011 void lttng_trigger_lock(struct lttng_trigger *trigger)
1012 {
1013 pthread_mutex_lock(&trigger->lock);
1014 }
1015
1016 LTTNG_HIDDEN
1017 void lttng_trigger_unlock(struct lttng_trigger *trigger)
1018 {
1019 pthread_mutex_unlock(&trigger->lock);
1020 }
This page took 0.05546 seconds and 4 git commands to generate.