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