Fix: sessiond: size-based rotations never trigger
[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/mi-lttng.h>
13 #include <common/optional.h>
14 #include <common/payload-view.h>
15 #include <common/payload.h>
16 #include <inttypes.h>
17 #include <lttng/action/action-internal.h>
18 #include <lttng/condition/buffer-usage.h>
19 #include <lttng/condition/condition-internal.h>
20 #include <lttng/condition/event-rule-matches-internal.h>
21 #include <lttng/condition/event-rule-matches.h>
22 #include <lttng/domain.h>
23 #include <lttng/error-query-internal.h>
24 #include <lttng/event-expr-internal.h>
25 #include <lttng/event-rule/event-rule-internal.h>
26 #include <lttng/trigger/trigger-internal.h>
27 #include <pthread.h>
28
29 LTTNG_HIDDEN
30 bool lttng_trigger_validate(const struct lttng_trigger *trigger)
31 {
32 bool valid;
33
34 if (!trigger) {
35 valid = false;
36 goto end;
37 }
38
39 if (!trigger->creds.uid.is_set) {
40 valid = false;
41 goto end;
42 }
43
44 valid = lttng_condition_validate(trigger->condition) &&
45 lttng_action_validate(trigger->action);
46 end:
47 return valid;
48 }
49
50 struct lttng_trigger *lttng_trigger_create(
51 struct lttng_condition *condition,
52 struct lttng_action *action)
53 {
54 struct lttng_trigger *trigger = NULL;
55
56 if (!condition || !action) {
57 goto end;
58 }
59
60 trigger = zmalloc(sizeof(struct lttng_trigger));
61 if (!trigger) {
62 goto end;
63 }
64
65 urcu_ref_init(&trigger->ref);
66
67 lttng_condition_get(condition);
68 trigger->condition = condition;
69
70 lttng_action_get(action);
71 trigger->action = action;
72
73 pthread_mutex_init(&trigger->lock, NULL);
74 trigger->registered = false;
75
76 end:
77 return trigger;
78 }
79
80 /*
81 * Note: the lack of reference counting 'get' on the condition object is normal.
82 * This API was exposed as such in 2.11. The client is not expected to call
83 * lttng_condition_destroy on the returned object.
84 */
85 struct lttng_condition *lttng_trigger_get_condition(
86 struct lttng_trigger *trigger)
87 {
88 return trigger ? trigger->condition : NULL;
89 }
90
91 const struct lttng_condition *lttng_trigger_get_const_condition(
92 const struct lttng_trigger *trigger)
93 {
94 return trigger ? trigger->condition : NULL;
95 }
96
97 /*
98 * Note: the lack of reference counting 'get' on the action object is normal.
99 * This API was exposed as such in 2.11. The client is not expected to call
100 * lttng_action_destroy on the returned object.
101 */
102 struct lttng_action *lttng_trigger_get_action(
103 struct lttng_trigger *trigger)
104 {
105 return trigger ? trigger->action : NULL;
106 }
107
108 const struct lttng_action *lttng_trigger_get_const_action(
109 const struct lttng_trigger *trigger)
110 {
111 return trigger ? trigger->action : NULL;
112 }
113
114 static void trigger_destroy_ref(struct urcu_ref *ref)
115 {
116 struct lttng_trigger *trigger =
117 container_of(ref, struct lttng_trigger, ref);
118 struct lttng_action *action = lttng_trigger_get_action(trigger);
119 struct lttng_condition *condition =
120 lttng_trigger_get_condition(trigger);
121
122 assert(action);
123 assert(condition);
124
125 /* Release ownership. */
126 lttng_action_put(action);
127 lttng_condition_put(condition);
128
129 pthread_mutex_destroy(&trigger->lock);
130
131 free(trigger->name);
132 free(trigger);
133 }
134
135 void lttng_trigger_destroy(struct lttng_trigger *trigger)
136 {
137 lttng_trigger_put(trigger);
138 }
139
140 LTTNG_HIDDEN
141 ssize_t lttng_trigger_create_from_payload(
142 struct lttng_payload_view *src_view,
143 struct lttng_trigger **_trigger)
144 {
145 ssize_t ret, offset = 0, condition_size, action_size, name_size = 0;
146 struct lttng_condition *condition = NULL;
147 struct lttng_action *action = NULL;
148 const struct lttng_trigger_comm *trigger_comm;
149 const char *name = NULL;
150 struct lttng_credentials creds = {
151 .uid = LTTNG_OPTIONAL_INIT_UNSET,
152 .gid = LTTNG_OPTIONAL_INIT_UNSET,
153 };
154 struct lttng_trigger *trigger = NULL;
155 const struct lttng_payload_view trigger_comm_view =
156 lttng_payload_view_from_view(
157 src_view, 0, sizeof(*trigger_comm));
158
159 if (!src_view || !_trigger) {
160 ret = -1;
161 goto end;
162 }
163
164 if (!lttng_payload_view_is_valid(&trigger_comm_view)) {
165 /* Payload not large enough to contain the header. */
166 ret = -1;
167 goto end;
168 }
169
170 /* lttng_trigger_comm header */
171 trigger_comm = (typeof(trigger_comm)) trigger_comm_view.buffer.data;
172
173 /* Set the trigger's creds. */
174 if (trigger_comm->uid > (uint64_t) ((uid_t) -1)) {
175 /* UID out of range for this platform. */
176 ret = -1;
177 goto end;
178 }
179
180 LTTNG_OPTIONAL_SET(&creds.uid, trigger_comm->uid);
181
182 offset += sizeof(*trigger_comm);
183
184 if (trigger_comm->name_length != 0) {
185 /* Name. */
186 const struct lttng_payload_view name_view =
187 lttng_payload_view_from_view(
188 src_view, offset,
189 trigger_comm->name_length);
190
191 if (!lttng_payload_view_is_valid(&name_view)) {
192 ret = -1;
193 goto end;
194 }
195
196 name = name_view.buffer.data;
197 if (!lttng_buffer_view_contains_string(&name_view.buffer, name,
198 trigger_comm->name_length)) {
199 ret = -1;
200 goto end;
201 }
202
203 offset += trigger_comm->name_length;
204 name_size = trigger_comm->name_length;
205 }
206
207 {
208 /* struct lttng_condition */
209 struct lttng_payload_view condition_view =
210 lttng_payload_view_from_view(
211 src_view, offset, -1);
212
213 condition_size = lttng_condition_create_from_payload(&condition_view,
214 &condition);
215 }
216
217 if (condition_size < 0) {
218 ret = condition_size;
219 goto end;
220 }
221
222 offset += condition_size;
223 {
224 /* struct lttng_action */
225 struct lttng_payload_view action_view =
226 lttng_payload_view_from_view(
227 src_view, offset, -1);
228
229 action_size = lttng_action_create_from_payload(&action_view, &action);
230 }
231
232 if (action_size < 0) {
233 ret = action_size;
234 goto end;
235 }
236 offset += action_size;
237
238 /* Unexpected size of inner-elements; the buffer is corrupted. */
239 if ((ssize_t) trigger_comm->length != condition_size + action_size + name_size) {
240 ret = -1;
241 goto error;
242 }
243
244 trigger = lttng_trigger_create(condition, action);
245 if (!trigger) {
246 ret = -1;
247 goto error;
248 }
249
250 lttng_trigger_set_credentials(trigger, &creds);
251
252 /*
253 * The trigger object owns references to the action and condition
254 * objects.
255 */
256 lttng_condition_put(condition);
257 condition = NULL;
258
259 lttng_action_put(action);
260 action = NULL;
261
262 if (name) {
263 const enum lttng_trigger_status status =
264 lttng_trigger_set_name(trigger, name);
265
266 if (status != LTTNG_TRIGGER_STATUS_OK) {
267 ret = -1;
268 goto end;
269 }
270 }
271
272 if (trigger_comm->is_hidden) {
273 lttng_trigger_set_hidden(trigger);
274 }
275
276 ret = offset;
277
278 error:
279 lttng_condition_put(condition);
280 lttng_action_put(action);
281 end:
282 if (ret >= 0) {
283 *_trigger = trigger;
284 } else {
285 lttng_trigger_put(trigger);
286 }
287
288 return ret;
289 }
290
291 /*
292 * Both elements are stored contiguously, see their "*_comm" structure
293 * for the detailed format.
294 */
295 LTTNG_HIDDEN
296 int lttng_trigger_serialize(const struct lttng_trigger *trigger,
297 struct lttng_payload *payload)
298 {
299 int ret;
300 size_t header_offset, size_before_payload, size_name;
301 struct lttng_trigger_comm trigger_comm = {};
302 struct lttng_trigger_comm *header;
303 const struct lttng_credentials *creds = NULL;
304
305 creds = lttng_trigger_get_credentials(trigger);
306 assert(creds);
307
308 trigger_comm.uid = LTTNG_OPTIONAL_GET(creds->uid);
309
310 if (trigger->name != NULL) {
311 size_name = strlen(trigger->name) + 1;
312 } else {
313 size_name = 0;
314 }
315
316 trigger_comm.name_length = size_name;
317
318 trigger_comm.is_hidden = lttng_trigger_is_hidden(trigger);
319
320 header_offset = payload->buffer.size;
321 ret = lttng_dynamic_buffer_append(&payload->buffer, &trigger_comm,
322 sizeof(trigger_comm));
323 if (ret) {
324 goto end;
325 }
326
327 size_before_payload = payload->buffer.size;
328
329 /* Trigger name. */
330 ret = lttng_dynamic_buffer_append(
331 &payload->buffer, trigger->name, size_name);
332 if (ret) {
333 goto end;
334 }
335
336 ret = lttng_condition_serialize(trigger->condition, payload);
337 if (ret) {
338 goto end;
339 }
340
341 ret = lttng_action_serialize(trigger->action, payload);
342 if (ret) {
343 goto end;
344 }
345
346 /* Update payload size. */
347 header = (typeof(header)) (payload->buffer.data + header_offset);
348 header->length = payload->buffer.size - size_before_payload;
349 end:
350 return ret;
351 }
352
353 LTTNG_HIDDEN
354 bool lttng_trigger_is_equal(
355 const struct lttng_trigger *a, const struct lttng_trigger *b)
356 {
357 if (!!a->name != !!b->name) {
358 /* Both must be either anonymous or named. */
359 return false;
360 }
361
362 if (a->name && strcmp(a->name, b->name) != 0) {
363 return false;
364 }
365
366 if (!lttng_condition_is_equal(a->condition, b->condition)) {
367 return false;
368 }
369
370 if (!lttng_action_is_equal(a->action, b->action)) {
371 return false;
372 }
373
374 if (!lttng_credentials_is_equal(lttng_trigger_get_credentials(a),
375 lttng_trigger_get_credentials(b))) {
376 return false;
377 }
378
379 if (a->is_hidden != b->is_hidden) {
380 return false;
381 }
382
383 return true;
384 }
385
386 LTTNG_HIDDEN
387 bool lttng_trigger_is_hidden(const struct lttng_trigger *trigger)
388 {
389 return trigger->is_hidden;
390 }
391
392 LTTNG_HIDDEN
393 void lttng_trigger_set_hidden(struct lttng_trigger *trigger)
394 {
395 assert(!trigger->is_hidden);
396 trigger->is_hidden = true;
397 }
398
399 LTTNG_HIDDEN
400 enum lttng_trigger_status lttng_trigger_set_name(struct lttng_trigger *trigger,
401 const char* name)
402 {
403 char *name_copy = NULL;
404 enum lttng_trigger_status status = LTTNG_TRIGGER_STATUS_OK;
405
406 if (!trigger) {
407 status = LTTNG_TRIGGER_STATUS_INVALID;
408 goto end;
409 }
410
411 if (name) {
412 name_copy = strdup(name);
413 if (!name_copy) {
414 status = LTTNG_TRIGGER_STATUS_ERROR;
415 goto end;
416 }
417 }
418
419 free(trigger->name);
420
421 trigger->name = name_copy;
422 name_copy = NULL;
423 end:
424 return status;
425 }
426
427 enum lttng_trigger_status lttng_trigger_get_name(
428 const struct lttng_trigger *trigger, const char **name)
429 {
430 enum lttng_trigger_status status = LTTNG_TRIGGER_STATUS_OK;
431
432 if (!trigger || !name) {
433 status = LTTNG_TRIGGER_STATUS_INVALID;
434 goto end;
435 }
436
437 if (!trigger->name) {
438 status = LTTNG_TRIGGER_STATUS_UNSET;
439 }
440
441 *name = trigger->name;
442 end:
443 return status;
444 }
445
446 LTTNG_HIDDEN
447 int lttng_trigger_assign_name(struct lttng_trigger *dst,
448 const struct lttng_trigger *src)
449 {
450 int ret = 0;
451 enum lttng_trigger_status status;
452
453 status = lttng_trigger_set_name(dst, src->name);
454 if (status != LTTNG_TRIGGER_STATUS_OK) {
455 ret = -1;
456 ERR("Failed to set name for trigger");
457 goto end;
458 }
459 end:
460 return ret;
461 }
462
463 LTTNG_HIDDEN
464 void lttng_trigger_set_tracer_token(struct lttng_trigger *trigger,
465 uint64_t token)
466 {
467 assert(trigger);
468 LTTNG_OPTIONAL_SET(&trigger->tracer_token, token);
469 }
470
471 LTTNG_HIDDEN
472 uint64_t lttng_trigger_get_tracer_token(const struct lttng_trigger *trigger)
473 {
474 assert(trigger);
475
476 return LTTNG_OPTIONAL_GET(trigger->tracer_token);
477 }
478
479 LTTNG_HIDDEN
480 int lttng_trigger_generate_name(struct lttng_trigger *trigger,
481 uint64_t unique_id)
482 {
483 int ret = 0;
484 char *generated_name = NULL;
485
486 ret = asprintf(&generated_name, "trigger%" PRIu64 "", unique_id);
487 if (ret < 0) {
488 ERR("Failed to generate trigger name");
489 ret = -1;
490 goto end;
491 }
492
493 ret = 0;
494 free(trigger->name);
495 trigger->name = generated_name;
496 end:
497 return ret;
498 }
499
500 LTTNG_HIDDEN
501 void lttng_trigger_get(struct lttng_trigger *trigger)
502 {
503 urcu_ref_get(&trigger->ref);
504 }
505
506 LTTNG_HIDDEN
507 void lttng_trigger_put(struct lttng_trigger *trigger)
508 {
509 if (!trigger) {
510 return;
511 }
512
513 urcu_ref_put(&trigger->ref , trigger_destroy_ref);
514 }
515
516 static void delete_trigger_array_element(void *ptr)
517 {
518 struct lttng_trigger *trigger = ptr;
519
520 lttng_trigger_put(trigger);
521 }
522
523 LTTNG_HIDDEN
524 struct lttng_triggers *lttng_triggers_create(void)
525 {
526 struct lttng_triggers *triggers = NULL;
527
528 triggers = zmalloc(sizeof(*triggers));
529 if (!triggers) {
530 goto end;
531 }
532
533 lttng_dynamic_pointer_array_init(&triggers->array, delete_trigger_array_element);
534
535 end:
536 return triggers;
537 }
538
539 LTTNG_HIDDEN
540 struct lttng_trigger *lttng_triggers_borrow_mutable_at_index(
541 const struct lttng_triggers *triggers, unsigned int index)
542 {
543 struct lttng_trigger *trigger = NULL;
544
545 assert(triggers);
546 if (index >= lttng_dynamic_pointer_array_get_count(&triggers->array)) {
547 goto end;
548 }
549
550 trigger = (struct lttng_trigger *)
551 lttng_dynamic_pointer_array_get_pointer(
552 &triggers->array, index);
553 end:
554 return trigger;
555 }
556
557 LTTNG_HIDDEN
558 int lttng_triggers_add(
559 struct lttng_triggers *triggers, struct lttng_trigger *trigger)
560 {
561 int ret;
562
563 assert(triggers);
564 assert(trigger);
565
566 lttng_trigger_get(trigger);
567
568 ret = lttng_dynamic_pointer_array_add_pointer(&triggers->array, trigger);
569 if (ret) {
570 lttng_trigger_put(trigger);
571 }
572
573 return ret;
574 }
575
576 LTTNG_HIDDEN
577 int lttng_triggers_remove_hidden_triggers(struct lttng_triggers *triggers)
578 {
579 int ret;
580 unsigned int trigger_count, i = 0;
581 enum lttng_trigger_status trigger_status;
582
583 assert(triggers);
584
585 trigger_status = lttng_triggers_get_count(triggers, &trigger_count);
586 assert(trigger_status == LTTNG_TRIGGER_STATUS_OK);
587
588 while (i < trigger_count) {
589 const struct lttng_trigger *trigger =
590 lttng_triggers_get_at_index(triggers, i);
591
592 if (lttng_trigger_is_hidden(trigger)) {
593 ret = lttng_dynamic_pointer_array_remove_pointer(
594 &triggers->array, i);
595 if (ret) {
596 goto end;
597 }
598
599 trigger_count--;
600 } else {
601 i++;
602 }
603 }
604
605 ret = 0;
606 end:
607 return ret;
608 }
609
610 const struct lttng_trigger *lttng_triggers_get_at_index(
611 const struct lttng_triggers *triggers, unsigned int index)
612 {
613 return lttng_triggers_borrow_mutable_at_index(triggers, index);
614 }
615
616 enum lttng_trigger_status lttng_triggers_get_count(const struct lttng_triggers *triggers, unsigned int *count)
617 {
618 enum lttng_trigger_status status = LTTNG_TRIGGER_STATUS_OK;
619
620 if (!triggers || !count) {
621 status = LTTNG_TRIGGER_STATUS_INVALID;
622 goto end;
623 }
624
625 *count = lttng_dynamic_pointer_array_get_count(&triggers->array);
626 end:
627 return status;
628 }
629
630 void lttng_triggers_destroy(struct lttng_triggers *triggers)
631 {
632 if (!triggers) {
633 return;
634 }
635
636 lttng_dynamic_pointer_array_reset(&triggers->array);
637 free(triggers);
638 }
639
640 int lttng_triggers_serialize(const struct lttng_triggers *triggers,
641 struct lttng_payload *payload)
642 {
643 int ret;
644 unsigned int i, count;
645 size_t size_before_payload;
646 struct lttng_triggers_comm triggers_comm = {};
647 struct lttng_triggers_comm *header;
648 enum lttng_trigger_status status;
649 const size_t header_offset = payload->buffer.size;
650
651 status = lttng_triggers_get_count(triggers, &count);
652 if (status != LTTNG_TRIGGER_STATUS_OK) {
653 ret = LTTNG_ERR_INVALID;
654 goto end;
655 }
656
657 triggers_comm.count = count;
658
659 /* Placeholder header; updated at the end. */
660 ret = lttng_dynamic_buffer_append(&payload->buffer, &triggers_comm,
661 sizeof(triggers_comm));
662 if (ret) {
663 goto end;
664 }
665
666 size_before_payload = payload->buffer.size;
667
668 for (i = 0; i < count; i++) {
669 const struct lttng_trigger *trigger =
670 lttng_triggers_get_at_index(triggers, i);
671
672 assert(trigger);
673
674 ret = lttng_trigger_serialize(trigger, payload);
675 if (ret) {
676 goto end;
677 }
678 }
679
680 /* Update payload size. */
681 header = (struct lttng_triggers_comm *) ((char *) payload->buffer.data + header_offset);
682 header->length = payload->buffer.size - size_before_payload;
683 end:
684 return ret;
685 }
686
687 LTTNG_HIDDEN
688 ssize_t lttng_triggers_create_from_payload(
689 struct lttng_payload_view *src_view,
690 struct lttng_triggers **triggers)
691 {
692 ssize_t ret, offset = 0, triggers_size = 0;
693 unsigned int i;
694 const struct lttng_triggers_comm *triggers_comm;
695 struct lttng_triggers *local_triggers = NULL;
696
697 if (!src_view || !triggers) {
698 ret = -1;
699 goto error;
700 }
701
702 /* lttng_trigger_comms header */
703 triggers_comm = (const struct lttng_triggers_comm *) src_view->buffer.data;
704 offset += sizeof(*triggers_comm);
705
706 local_triggers = lttng_triggers_create();
707 if (!local_triggers) {
708 ret = -1;
709 goto error;
710 }
711
712 for (i = 0; i < triggers_comm->count; i++) {
713 struct lttng_trigger *trigger = NULL;
714 struct lttng_payload_view trigger_view =
715 lttng_payload_view_from_view(src_view, offset, -1);
716 ssize_t trigger_size;
717
718 trigger_size = lttng_trigger_create_from_payload(
719 &trigger_view, &trigger);
720 if (trigger_size < 0) {
721 ret = trigger_size;
722 goto error;
723 }
724
725 /* Transfer ownership of the trigger to the collection. */
726 ret = lttng_triggers_add(local_triggers, trigger);
727 lttng_trigger_put(trigger);
728 if (ret < 0) {
729 ret = -1;
730 goto error;
731 }
732
733 offset += trigger_size;
734 triggers_size += trigger_size;
735 }
736
737 /* Unexpected size of inner-elements; the buffer is corrupted. */
738 if ((ssize_t) triggers_comm->length != triggers_size) {
739 ret = -1;
740 goto error;
741 }
742
743 /* Pass ownership to caller. */
744 *triggers = local_triggers;
745 local_triggers = NULL;
746
747 ret = offset;
748 error:
749
750 lttng_triggers_destroy(local_triggers);
751 return ret;
752 }
753
754 LTTNG_HIDDEN
755 const struct lttng_credentials *lttng_trigger_get_credentials(
756 const struct lttng_trigger *trigger)
757 {
758 return &trigger->creds;
759 }
760
761 LTTNG_HIDDEN
762 void lttng_trigger_set_credentials(struct lttng_trigger *trigger,
763 const struct lttng_credentials *creds)
764 {
765 /* Triggers do not use the group id to authenticate the user. */
766 assert(creds);
767 LTTNG_OPTIONAL_SET(&trigger->creds.uid, LTTNG_OPTIONAL_GET(creds->uid));
768 LTTNG_OPTIONAL_UNSET(&trigger->creds.gid);
769 }
770
771 enum lttng_trigger_status lttng_trigger_set_owner_uid(
772 struct lttng_trigger *trigger, uid_t uid)
773 {
774 enum lttng_trigger_status ret = LTTNG_TRIGGER_STATUS_OK;
775 const uid_t euid = geteuid();
776 const struct lttng_credentials creds = {
777 .uid = LTTNG_OPTIONAL_INIT_VALUE(uid),
778 .gid = LTTNG_OPTIONAL_INIT_UNSET,
779 };
780
781 if (!trigger) {
782 ret = LTTNG_TRIGGER_STATUS_INVALID;
783 goto end;
784 }
785
786 /* Client-side validation only to report a clearer error. */
787 if (euid != 0 && euid != uid) {
788 ret = LTTNG_TRIGGER_STATUS_PERMISSION_DENIED;
789 goto end;
790 }
791
792 lttng_trigger_set_credentials(trigger, &creds);
793
794 end:
795 return ret;
796 }
797
798 enum lttng_trigger_status lttng_trigger_get_owner_uid(
799 const struct lttng_trigger *trigger, uid_t *uid)
800 {
801 enum lttng_trigger_status ret = LTTNG_TRIGGER_STATUS_OK;
802 const struct lttng_credentials *creds = NULL;
803
804 if (!trigger || !uid ) {
805 ret = LTTNG_TRIGGER_STATUS_INVALID;
806 goto end;
807 }
808
809 if (!trigger->creds.uid.is_set ) {
810 ret = LTTNG_TRIGGER_STATUS_UNSET;
811 goto end;
812 }
813
814 creds = lttng_trigger_get_credentials(trigger);
815 *uid = lttng_credentials_get_uid(creds);
816
817 end:
818 return ret;
819 }
820
821 LTTNG_HIDDEN
822 enum lttng_domain_type lttng_trigger_get_underlying_domain_type_restriction(
823 const struct lttng_trigger *trigger)
824 {
825 enum lttng_domain_type type = LTTNG_DOMAIN_NONE;
826 const struct lttng_event_rule *event_rule;
827 enum lttng_condition_status c_status;
828 enum lttng_condition_type c_type;
829
830 assert(trigger);
831 assert(trigger->condition);
832
833 c_type = lttng_condition_get_type(trigger->condition);
834 assert (c_type != LTTNG_CONDITION_TYPE_UNKNOWN);
835
836 switch (c_type) {
837 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
838 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
839 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
840 /* Apply to any domain. */
841 type = LTTNG_DOMAIN_NONE;
842 break;
843 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES:
844 /* Return the domain of the event rule. */
845 c_status = lttng_condition_event_rule_matches_get_rule(
846 trigger->condition, &event_rule);
847 assert(c_status == LTTNG_CONDITION_STATUS_OK);
848 type = lttng_event_rule_get_domain_type(event_rule);
849 break;
850 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
851 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
852 /* Return the domain of the channel being monitored. */
853 c_status = lttng_condition_buffer_usage_get_domain_type(
854 trigger->condition, &type);
855 assert(c_status == LTTNG_CONDITION_STATUS_OK);
856 break;
857 default:
858 abort();
859 }
860
861 return type;
862 }
863
864 /*
865 * Generate bytecode related to the trigger.
866 * On success LTTNG_OK. On error, returns lttng_error code.
867 */
868 LTTNG_HIDDEN
869 enum lttng_error_code lttng_trigger_generate_bytecode(
870 struct lttng_trigger *trigger,
871 const struct lttng_credentials *creds)
872 {
873 enum lttng_error_code ret;
874 struct lttng_condition *condition = NULL;
875
876 condition = lttng_trigger_get_condition(trigger);
877 if (!condition) {
878 ret = LTTNG_ERR_INVALID_TRIGGER;
879 goto end;
880 }
881
882 switch (lttng_condition_get_type(condition)) {
883 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES:
884 {
885 struct lttng_event_rule *event_rule;
886 const enum lttng_condition_status condition_status =
887 lttng_condition_event_rule_matches_borrow_rule_mutable(
888 condition, &event_rule);
889
890 assert(condition_status == LTTNG_CONDITION_STATUS_OK);
891
892 /* Generate the filter bytecode. */
893 ret = lttng_event_rule_generate_filter_bytecode(
894 event_rule, creds);
895 if (ret != LTTNG_OK) {
896 goto end;
897 }
898
899 /* Generate the capture bytecode. */
900 ret = lttng_condition_event_rule_matches_generate_capture_descriptor_bytecode(
901 condition);
902 if (ret != LTTNG_OK) {
903 goto end;
904 }
905
906 ret = LTTNG_OK;
907 break;
908 }
909 default:
910 ret = LTTNG_OK;
911 break;
912 }
913 end:
914 return ret;
915 }
916
917 LTTNG_HIDDEN
918 struct lttng_trigger *lttng_trigger_copy(const struct lttng_trigger *trigger)
919 {
920 int ret;
921 struct lttng_payload copy_buffer;
922 struct lttng_condition *condition_copy = NULL;
923 struct lttng_action *action_copy = NULL;
924 struct lttng_trigger *copy = NULL;
925 enum lttng_trigger_status trigger_status;
926 const char *trigger_name;
927 uid_t trigger_owner_uid;
928
929 lttng_payload_init(&copy_buffer);
930
931 ret = lttng_condition_serialize(trigger->condition, &copy_buffer);
932 if (ret < 0) {
933 goto end;
934 }
935
936 {
937 struct lttng_payload_view view =
938 lttng_payload_view_from_payload(
939 &copy_buffer, 0, -1);
940
941 ret = lttng_condition_create_from_payload(
942 &view, &condition_copy);
943 if (ret < 0) {
944 goto end;
945 }
946 }
947
948 lttng_payload_clear(&copy_buffer);
949
950 ret = lttng_action_serialize(trigger->action, &copy_buffer);
951 if (ret < 0) {
952 goto end;
953 }
954
955 {
956 struct lttng_payload_view view =
957 lttng_payload_view_from_payload(
958 &copy_buffer, 0, -1);
959
960 ret = lttng_action_create_from_payload(
961 &view, &action_copy);
962 if (ret < 0) {
963 goto end;
964 }
965 }
966
967 copy = lttng_trigger_create(condition_copy, action_copy);
968 if (!copy) {
969 ERR("Failed to allocate trigger during trigger copy");
970 goto end;
971 }
972
973 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
974 switch (trigger_status) {
975 case LTTNG_TRIGGER_STATUS_OK:
976 trigger_status = lttng_trigger_set_name(copy, trigger_name);
977 if (trigger_status != LTTNG_TRIGGER_STATUS_OK) {
978 ERR("Failed to set name of new trigger during copy");
979 goto error_cleanup_trigger;
980 }
981 break;
982 case LTTNG_TRIGGER_STATUS_UNSET:
983 break;
984 default:
985 ERR("Failed to get name of original trigger during copy");
986 goto error_cleanup_trigger;
987 }
988
989 trigger_status = lttng_trigger_get_owner_uid(
990 trigger, &trigger_owner_uid);
991 switch (trigger_status) {
992 case LTTNG_TRIGGER_STATUS_OK:
993 LTTNG_OPTIONAL_SET(&copy->creds.uid, trigger_owner_uid);
994 break;
995 case LTTNG_TRIGGER_STATUS_UNSET:
996 break;
997 default:
998 ERR("Failed to get owner uid of original trigger during copy");
999 goto error_cleanup_trigger;
1000 }
1001
1002 copy->tracer_token = trigger->tracer_token;
1003 copy->registered = trigger->registered;
1004 copy->is_hidden = trigger->is_hidden;
1005 goto end;
1006
1007 error_cleanup_trigger:
1008 lttng_trigger_destroy(copy);
1009 copy = NULL;
1010 end:
1011 lttng_condition_put(condition_copy);
1012 lttng_action_put(action_copy);
1013 lttng_payload_reset(&copy_buffer);
1014 return copy;
1015 }
1016
1017 LTTNG_HIDDEN
1018 bool lttng_trigger_needs_tracer_notifier(const struct lttng_trigger *trigger)
1019 {
1020 bool needs_tracer_notifier = false;
1021 const struct lttng_condition *condition =
1022 lttng_trigger_get_const_condition(trigger);
1023
1024 switch (lttng_condition_get_type(condition)) {
1025 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES:
1026 needs_tracer_notifier = true;
1027 goto end;
1028 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
1029 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
1030 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
1031 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
1032 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
1033 goto end;
1034 case LTTNG_CONDITION_TYPE_UNKNOWN:
1035 default:
1036 abort();
1037 }
1038 end:
1039 return needs_tracer_notifier;
1040 }
1041
1042 LTTNG_HIDDEN
1043 void lttng_trigger_set_as_registered(struct lttng_trigger *trigger)
1044 {
1045 pthread_mutex_lock(&trigger->lock);
1046 trigger->registered = true;
1047 pthread_mutex_unlock(&trigger->lock);
1048 }
1049
1050 LTTNG_HIDDEN
1051 void lttng_trigger_set_as_unregistered(struct lttng_trigger *trigger)
1052 {
1053 pthread_mutex_lock(&trigger->lock);
1054 trigger->registered = false;
1055 pthread_mutex_unlock(&trigger->lock);
1056 }
1057
1058 /*
1059 * The trigger must be locked before calling lttng_trigger_registered.
1060 * The lock is necessary since a trigger can be unregistered at anytime.
1061 * Manipulations requiring that the trigger be registered must always acquire
1062 * the trigger lock for the duration of the manipulation using
1063 * `lttng_trigger_lock` and `lttng_trigger_unlock`.
1064 */
1065 LTTNG_HIDDEN
1066 bool lttng_trigger_is_registered(struct lttng_trigger *trigger)
1067 {
1068 ASSERT_LOCKED(trigger->lock);
1069 return trigger->registered;
1070 }
1071
1072 LTTNG_HIDDEN
1073 void lttng_trigger_lock(struct lttng_trigger *trigger)
1074 {
1075 pthread_mutex_lock(&trigger->lock);
1076 }
1077
1078 LTTNG_HIDDEN
1079 void lttng_trigger_unlock(struct lttng_trigger *trigger)
1080 {
1081 pthread_mutex_unlock(&trigger->lock);
1082 }
1083
1084 LTTNG_HIDDEN
1085 enum lttng_error_code lttng_trigger_mi_serialize(const struct lttng_trigger *trigger,
1086 struct mi_writer *writer,
1087 const struct mi_lttng_error_query_callbacks
1088 *error_query_callbacks)
1089 {
1090 int ret;
1091 enum lttng_error_code ret_code;
1092 enum lttng_trigger_status trigger_status;
1093 const struct lttng_condition *condition = NULL;
1094 const struct lttng_action *action = NULL;
1095 struct lttng_dynamic_array action_path_indexes;
1096 uid_t owner_uid;
1097
1098 assert(trigger);
1099 assert(writer);
1100
1101 lttng_dynamic_array_init(&action_path_indexes, sizeof(uint64_t), NULL);
1102
1103 /* Open trigger element. */
1104 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_trigger);
1105 if (ret) {
1106 goto mi_error;
1107 }
1108
1109 trigger_status = lttng_trigger_get_owner_uid(trigger, &owner_uid);
1110 assert(trigger_status == LTTNG_TRIGGER_STATUS_OK);
1111
1112 /* Name. */
1113 ret = mi_lttng_writer_write_element_string(
1114 writer, config_element_name, trigger->name);
1115 if (ret) {
1116 goto mi_error;
1117 }
1118
1119 /* Owner uid. */
1120 ret = mi_lttng_writer_write_element_signed_int(writer,
1121 mi_lttng_element_trigger_owner_uid,
1122 (int64_t) owner_uid);
1123 if (ret) {
1124 goto mi_error;
1125 }
1126
1127 /* Condition. */
1128 condition = lttng_trigger_get_const_condition(trigger);
1129 assert(condition);
1130 ret_code = lttng_condition_mi_serialize(
1131 trigger, condition, writer, error_query_callbacks);
1132 if (ret_code != LTTNG_OK) {
1133 goto end;
1134 }
1135
1136 /* Action. */
1137 action = lttng_trigger_get_const_action(trigger);
1138 assert(action);
1139 ret_code = lttng_action_mi_serialize(trigger, action, writer,
1140 error_query_callbacks, &action_path_indexes);
1141 if (ret_code != LTTNG_OK) {
1142 goto end;
1143 }
1144
1145 if (error_query_callbacks && error_query_callbacks->trigger_cb) {
1146 struct lttng_error_query_results *results = NULL;
1147
1148 ret_code = error_query_callbacks->trigger_cb(trigger, &results);
1149 if (ret_code != LTTNG_OK) {
1150 goto end;
1151 }
1152
1153 ret_code = lttng_error_query_results_mi_serialize(
1154 results, writer);
1155 lttng_error_query_results_destroy(results);
1156 if (ret_code != LTTNG_OK) {
1157 goto end;
1158 }
1159 }
1160
1161 /* Close trigger element. */
1162 ret = mi_lttng_writer_close_element(writer);
1163 if (ret) {
1164 goto mi_error;
1165 }
1166
1167 ret_code = LTTNG_OK;
1168 goto end;
1169
1170 mi_error:
1171 ret_code = LTTNG_ERR_MI_IO_FAIL;
1172 end:
1173 lttng_dynamic_array_reset(&action_path_indexes);
1174 return ret_code;
1175 }
1176
1177 /* Used by qsort, which expects the semantics of strcmp(). */
1178 static int compare_triggers_by_name(const void *a, const void *b)
1179 {
1180 const struct lttng_trigger *trigger_a =
1181 *((const struct lttng_trigger **) a);
1182 const struct lttng_trigger *trigger_b =
1183 *((const struct lttng_trigger **) b);
1184 const char *name_a, *name_b;
1185 enum lttng_trigger_status trigger_status;
1186
1187 /* Anonymous triggers are not reachable here. */
1188 trigger_status = lttng_trigger_get_name(trigger_a, &name_a);
1189 assert(trigger_status == LTTNG_TRIGGER_STATUS_OK);
1190
1191 trigger_status = lttng_trigger_get_name(trigger_b, &name_b);
1192 assert(trigger_status == LTTNG_TRIGGER_STATUS_OK);
1193
1194 return strcmp(name_a, name_b);
1195 }
1196
1197 LTTNG_HIDDEN
1198 enum lttng_error_code lttng_triggers_mi_serialize(const struct lttng_triggers *triggers,
1199 struct mi_writer *writer,
1200 const struct mi_lttng_error_query_callbacks
1201 *error_query_callbacks)
1202 {
1203 int ret;
1204 enum lttng_error_code ret_code;
1205 enum lttng_trigger_status status;
1206 unsigned int count, i;
1207 struct lttng_dynamic_pointer_array sorted_triggers;
1208
1209 assert(triggers);
1210 assert(writer);
1211
1212 /*
1213 * Sort trigger by name to ensure an order at the MI level and ignore
1214 * any anonymous trigger present.
1215 */
1216 lttng_dynamic_pointer_array_init(&sorted_triggers, NULL);
1217
1218 status = lttng_triggers_get_count(triggers, &count);
1219 assert(status == LTTNG_TRIGGER_STATUS_OK);
1220
1221 for (i = 0; i < count; i++) {
1222 int add_ret;
1223 const char *unused_name;
1224 const struct lttng_trigger *trigger =
1225 lttng_triggers_get_at_index(triggers, i);
1226
1227 status = lttng_trigger_get_name(trigger, &unused_name);
1228 switch (status) {
1229 case LTTNG_TRIGGER_STATUS_OK:
1230 break;
1231 case LTTNG_TRIGGER_STATUS_UNSET:
1232 /* Don't list anonymous triggers. */
1233 continue;
1234 default:
1235 abort();
1236 }
1237
1238 add_ret = lttng_dynamic_pointer_array_add_pointer(
1239 &sorted_triggers, (void *) trigger);
1240
1241 if (add_ret) {
1242 ERR("Failed to lttng_trigger to sorting array.");
1243 ret_code = LTTNG_ERR_NOMEM;
1244 goto error;
1245 }
1246 }
1247
1248 qsort(sorted_triggers.array.buffer.data, count,
1249 sizeof(struct lttng_trigger *),
1250 compare_triggers_by_name);
1251
1252 /* Open triggers element. */
1253 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_triggers);
1254 if (ret) {
1255 ret_code = LTTNG_ERR_MI_IO_FAIL;
1256 goto error;
1257 }
1258
1259 for (i = 0; i < lttng_dynamic_pointer_array_get_count(&sorted_triggers); i++) {
1260 const struct lttng_trigger *trigger =
1261 (const struct lttng_trigger *)
1262 lttng_dynamic_pointer_array_get_pointer(
1263 &sorted_triggers, i);
1264
1265 lttng_trigger_mi_serialize(trigger, writer, error_query_callbacks);
1266 }
1267
1268 /* Close triggers element. */
1269 ret = mi_lttng_writer_close_element(writer);
1270 if (ret) {
1271 ret_code = LTTNG_ERR_MI_IO_FAIL;
1272 goto error;
1273 }
1274
1275 ret_code = LTTNG_OK;
1276
1277 error:
1278 lttng_dynamic_pointer_array_reset(&sorted_triggers);
1279 return ret_code;
1280 }
This page took 0.083126 seconds and 4 git commands to generate.