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