sessiond: add support for anonymous triggers
[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
9c374932 8#include <assert.h>
3da864a9 9#include <common/credentials.h>
a02903c0 10#include <common/dynamic-array.h>
9c374932 11#include <common/error.h>
3da864a9 12#include <common/optional.h>
9c374932
JR
13#include <common/payload-view.h>
14#include <common/payload.h>
242388e4 15#include <inttypes.h>
9c374932
JR
16#include <lttng/action/action-internal.h>
17#include <lttng/condition/buffer-usage.h>
18#include <lttng/condition/condition-internal.h>
670a26e4
JR
19#include <lttng/condition/event-rule-matches-internal.h>
20#include <lttng/condition/event-rule-matches.h>
9c374932
JR
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>
a58c490f
JG
26
27LTTNG_HIDDEN
b61776fb 28bool lttng_trigger_validate(const struct lttng_trigger *trigger)
a58c490f
JG
29{
30 bool valid;
31
32 if (!trigger) {
33 valid = false;
34 goto end;
35 }
36
64eafdf6
JR
37 if (!trigger->creds.uid.is_set) {
38 valid = false;
39 goto end;
40 }
41
a58c490f
JG
42 valid = lttng_condition_validate(trigger->condition) &&
43 lttng_action_validate(trigger->action);
44end:
45 return valid;
46}
47
48struct 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
f01d28b4
JR
63 urcu_ref_init(&trigger->ref);
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
9c374932
JR
71 pthread_mutex_init(&trigger->lock, NULL);
72 trigger->registered = false;
73
a58c490f
JG
74end:
75 return trigger;
76}
77
7ca172c1
JR
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 */
a58c490f
JG
83struct lttng_condition *lttng_trigger_get_condition(
84 struct lttng_trigger *trigger)
85{
86 return trigger ? trigger->condition : NULL;
87}
88
9b63a4aa
JG
89const struct lttng_condition *lttng_trigger_get_const_condition(
90 const struct lttng_trigger *trigger)
91{
0de2479d 92 return trigger ? trigger->condition : NULL;
9b63a4aa
JG
93}
94
7ca172c1
JR
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 */
e2ba1c78 100struct lttng_action *lttng_trigger_get_action(
a58c490f
JG
101 struct lttng_trigger *trigger)
102{
103 return trigger ? trigger->action : NULL;
104}
105
9b63a4aa
JG
106const struct lttng_action *lttng_trigger_get_const_action(
107 const struct lttng_trigger *trigger)
108{
0de2479d 109 return trigger ? trigger->action : NULL;
9b63a4aa
JG
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
9c374932
JR
127 pthread_mutex_destroy(&trigger->lock);
128
242388e4 129 free(trigger->name);
a58c490f
JG
130 free(trigger);
131}
132
f01d28b4
JR
133void lttng_trigger_destroy(struct lttng_trigger *trigger)
134{
135 lttng_trigger_put(trigger);
136}
137
a58c490f 138LTTNG_HIDDEN
c0a66c84
JG
139ssize_t lttng_trigger_create_from_payload(
140 struct lttng_payload_view *src_view,
6808ef55 141 struct lttng_trigger **_trigger)
a58c490f 142{
242388e4 143 ssize_t ret, offset = 0, condition_size, action_size, name_size = 0;
a58c490f
JG
144 struct lttng_condition *condition = NULL;
145 struct lttng_action *action = NULL;
146 const struct lttng_trigger_comm *trigger_comm;
242388e4 147 const char *name = NULL;
64eafdf6
JR
148 struct lttng_credentials creds = {
149 .uid = LTTNG_OPTIONAL_INIT_UNSET,
150 .gid = LTTNG_OPTIONAL_INIT_UNSET,
151 };
6808ef55 152 struct lttng_trigger *trigger = NULL;
3e6e0df2
JG
153 const struct lttng_payload_view trigger_comm_view =
154 lttng_payload_view_from_view(
155 src_view, 0, sizeof(*trigger_comm));
a58c490f 156
6808ef55 157 if (!src_view || !_trigger) {
a58c490f
JG
158 ret = -1;
159 goto end;
160 }
161
3e6e0df2
JG
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
a58c490f 168 /* lttng_trigger_comm header */
3e6e0df2 169 trigger_comm = (typeof(trigger_comm)) trigger_comm_view.buffer.data;
64eafdf6
JR
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
a58c490f 180 offset += sizeof(*trigger_comm);
242388e4
JR
181
182 if (trigger_comm->name_length != 0) {
183 /* Name. */
184 const struct lttng_payload_view name_view =
185 lttng_payload_view_from_view(
3e6e0df2
JG
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 }
242388e4
JR
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
c0a66c84
JG
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 }
a58c490f 214
a58c490f
JG
215 if (condition_size < 0) {
216 ret = condition_size;
217 goto end;
218 }
c0a66c84 219
a58c490f 220 offset += condition_size;
c0a66c84
JG
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 }
a58c490f 229
a58c490f
JG
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. */
242388e4 237 if ((ssize_t) trigger_comm->length != condition_size + action_size + name_size) {
a58c490f
JG
238 ret = -1;
239 goto error;
240 }
241
6808ef55
JG
242 trigger = lttng_trigger_create(condition, action);
243 if (!trigger) {
a58c490f
JG
244 ret = -1;
245 goto error;
246 }
c0a66c84 247
6808ef55 248 lttng_trigger_set_credentials(trigger, &creds);
64eafdf6 249
7ca172c1
JR
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
242388e4
JR
260 if (name) {
261 const enum lttng_trigger_status status =
6808ef55 262 lttng_trigger_set_name(trigger, name);
242388e4
JR
263
264 if (status != LTTNG_TRIGGER_STATUS_OK) {
265 ret = -1;
266 goto end;
267 }
268 }
269
a58c490f 270 ret = offset;
7ca172c1 271
a58c490f 272error:
1065801b
JG
273 lttng_condition_put(condition);
274 lttng_action_put(action);
7ca172c1 275end:
f5d98ed9 276 if (ret >= 0) {
6808ef55
JG
277 *_trigger = trigger;
278 } else {
279 lttng_trigger_put(trigger);
280 }
281
a58c490f
JG
282 return ret;
283}
284
285/*
a58c490f
JG
286 * Both elements are stored contiguously, see their "*_comm" structure
287 * for the detailed format.
288 */
289LTTNG_HIDDEN
a02903c0 290int lttng_trigger_serialize(const struct lttng_trigger *trigger,
c0a66c84 291 struct lttng_payload *payload)
a58c490f 292{
3647288f 293 int ret;
242388e4 294 size_t header_offset, size_before_payload, size_name;
c0a66c84 295 struct lttng_trigger_comm trigger_comm = {};
3647288f 296 struct lttng_trigger_comm *header;
64eafdf6
JR
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);
a58c490f 303
242388e4
JR
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
c0a66c84
JG
312 header_offset = payload->buffer.size;
313 ret = lttng_dynamic_buffer_append(&payload->buffer, &trigger_comm,
3647288f
JG
314 sizeof(trigger_comm));
315 if (ret) {
a58c490f
JG
316 goto end;
317 }
318
c0a66c84 319 size_before_payload = payload->buffer.size;
242388e4
JR
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
c0a66c84 328 ret = lttng_condition_serialize(trigger->condition, payload);
3647288f 329 if (ret) {
a58c490f
JG
330 goto end;
331 }
a58c490f 332
c0a66c84 333 ret = lttng_action_serialize(trigger->action, payload);
3647288f 334 if (ret) {
a58c490f
JG
335 goto end;
336 }
a58c490f 337
3647288f 338 /* Update payload size. */
c0a66c84
JG
339 header = (typeof(header)) (payload->buffer.data + header_offset);
340 header->length = payload->buffer.size - size_before_payload;
a58c490f
JG
341end:
342 return ret;
343}
3da864a9 344
85c06c44
JR
345LTTNG_HIDDEN
346bool lttng_trigger_is_equal(
347 const struct lttng_trigger *a, const struct lttng_trigger *b)
348{
0efb2ad7
JG
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) {
1ca78886
FD
355 return false;
356 }
357
85c06c44
JR
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
242388e4
JR
374enum lttng_trigger_status lttng_trigger_set_name(struct lttng_trigger *trigger,
375 const char* name)
376{
377 char *name_copy = NULL;
378 enum lttng_trigger_status status = LTTNG_TRIGGER_STATUS_OK;
379
380 if (!trigger || !name ||
381 strlen(name) == 0) {
382 status = LTTNG_TRIGGER_STATUS_INVALID;
383 goto end;
384 }
385
386 name_copy = strdup(name);
387 if (!name_copy) {
388 status = LTTNG_TRIGGER_STATUS_ERROR;
389 goto end;
390 }
391
392 free(trigger->name);
393
394 trigger->name = name_copy;
395 name_copy = NULL;
396end:
397 return status;
398}
399
400enum lttng_trigger_status lttng_trigger_get_name(
401 const struct lttng_trigger *trigger, const char **name)
402{
403 enum lttng_trigger_status status = LTTNG_TRIGGER_STATUS_OK;
404
405 if (!trigger || !name) {
406 status = LTTNG_TRIGGER_STATUS_INVALID;
407 goto end;
408 }
409
410 if (!trigger->name) {
411 status = LTTNG_TRIGGER_STATUS_UNSET;
412 }
413
414 *name = trigger->name;
415end:
416 return status;
417}
418
419LTTNG_HIDDEN
420int lttng_trigger_assign_name(struct lttng_trigger *dst,
421 const struct lttng_trigger *src)
422{
423 int ret = 0;
424 enum lttng_trigger_status status;
425
426 status = lttng_trigger_set_name(dst, src->name);
427 if (status != LTTNG_TRIGGER_STATUS_OK) {
428 ret = -1;
429 ERR("Failed to set name for trigger");
430 goto end;
431 }
432end:
433 return ret;
434}
435
e6887944
JR
436LTTNG_HIDDEN
437void lttng_trigger_set_tracer_token(struct lttng_trigger *trigger,
438 uint64_t token)
439{
440 assert(trigger);
441 LTTNG_OPTIONAL_SET(&trigger->tracer_token, token);
442}
443
444LTTNG_HIDDEN
445uint64_t lttng_trigger_get_tracer_token(const struct lttng_trigger *trigger)
446{
447 assert(trigger);
448
449 return LTTNG_OPTIONAL_GET(trigger->tracer_token);
450}
451
242388e4
JR
452LTTNG_HIDDEN
453int lttng_trigger_generate_name(struct lttng_trigger *trigger,
454 uint64_t unique_id)
455{
456 int ret = 0;
457 char *generated_name = NULL;
458
459 ret = asprintf(&generated_name, "T%" PRIu64 "", unique_id);
460 if (ret < 0) {
461 ERR("Failed to generate trigger name");
462 ret = -1;
463 goto end;
464 }
465
466 ret = 0;
467 free(trigger->name);
468 trigger->name = generated_name;
469end:
470 return ret;
471}
472
f01d28b4
JR
473LTTNG_HIDDEN
474void lttng_trigger_get(struct lttng_trigger *trigger)
475{
476 urcu_ref_get(&trigger->ref);
477}
478
479LTTNG_HIDDEN
480void lttng_trigger_put(struct lttng_trigger *trigger)
481{
482 if (!trigger) {
483 return;
484 }
485
486 urcu_ref_put(&trigger->ref , trigger_destroy_ref);
487}
488
a02903c0
JR
489static void delete_trigger_array_element(void *ptr)
490{
491 struct lttng_trigger *trigger = ptr;
492
493 lttng_trigger_put(trigger);
494}
495
496LTTNG_HIDDEN
497struct lttng_triggers *lttng_triggers_create(void)
498{
499 struct lttng_triggers *triggers = NULL;
500
501 triggers = zmalloc(sizeof(*triggers));
502 if (!triggers) {
503 goto end;
504 }
505
506 lttng_dynamic_pointer_array_init(&triggers->array, delete_trigger_array_element);
507
508end:
509 return triggers;
510}
511
512LTTNG_HIDDEN
513struct lttng_trigger *lttng_triggers_borrow_mutable_at_index(
514 const struct lttng_triggers *triggers, unsigned int index)
515{
516 struct lttng_trigger *trigger = NULL;
517
518 assert(triggers);
519 if (index >= lttng_dynamic_pointer_array_get_count(&triggers->array)) {
520 goto end;
521 }
522
523 trigger = (struct lttng_trigger *)
524 lttng_dynamic_pointer_array_get_pointer(
525 &triggers->array, index);
526end:
527 return trigger;
528}
529
530LTTNG_HIDDEN
531int lttng_triggers_add(
532 struct lttng_triggers *triggers, struct lttng_trigger *trigger)
533{
534 int ret;
535
536 assert(triggers);
537 assert(trigger);
538
539 lttng_trigger_get(trigger);
540
541 ret = lttng_dynamic_pointer_array_add_pointer(&triggers->array, trigger);
542 if (ret) {
543 lttng_trigger_put(trigger);
544 }
545
546 return ret;
547}
548
549const struct lttng_trigger *lttng_triggers_get_at_index(
550 const struct lttng_triggers *triggers, unsigned int index)
551{
552 return lttng_triggers_borrow_mutable_at_index(triggers, index);
553}
554
555enum lttng_trigger_status lttng_triggers_get_count(const struct lttng_triggers *triggers, unsigned int *count)
556{
557 enum lttng_trigger_status status = LTTNG_TRIGGER_STATUS_OK;
558
559 if (!triggers || !count) {
560 status = LTTNG_TRIGGER_STATUS_INVALID;
561 goto end;
562 }
563
564 *count = lttng_dynamic_pointer_array_get_count(&triggers->array);
565end:
566 return status;
567}
568
569void lttng_triggers_destroy(struct lttng_triggers *triggers)
570{
571 if (!triggers) {
572 return;
573 }
574
575 lttng_dynamic_pointer_array_reset(&triggers->array);
576 free(triggers);
577}
578
579int lttng_triggers_serialize(const struct lttng_triggers *triggers,
580 struct lttng_payload *payload)
581{
582 int ret;
583 unsigned int i, count;
584 size_t size_before_payload;
585 struct lttng_triggers_comm triggers_comm = {};
586 struct lttng_triggers_comm *header;
587 enum lttng_trigger_status status;
588 const size_t header_offset = payload->buffer.size;
589
590 status = lttng_triggers_get_count(triggers, &count);
591 if (status != LTTNG_TRIGGER_STATUS_OK) {
592 ret = LTTNG_ERR_INVALID;
593 goto end;
594 }
595
596 triggers_comm.count = count;
597
598 /* Placeholder header; updated at the end. */
599 ret = lttng_dynamic_buffer_append(&payload->buffer, &triggers_comm,
600 sizeof(triggers_comm));
601 if (ret) {
602 goto end;
603 }
604
605 size_before_payload = payload->buffer.size;
606
607 for (i = 0; i < count; i++) {
608 const struct lttng_trigger *trigger =
609 lttng_triggers_get_at_index(triggers, i);
610
611 assert(trigger);
612
613 ret = lttng_trigger_serialize(trigger, payload);
614 if (ret) {
615 goto end;
616 }
617 }
618
619 /* Update payload size. */
620 header = (struct lttng_triggers_comm *) ((char *) payload->buffer.data + header_offset);
621 header->length = payload->buffer.size - size_before_payload;
622end:
623 return ret;
624}
625
626LTTNG_HIDDEN
627ssize_t lttng_triggers_create_from_payload(
628 struct lttng_payload_view *src_view,
629 struct lttng_triggers **triggers)
630{
631 ssize_t ret, offset = 0, triggers_size = 0;
632 unsigned int i;
633 const struct lttng_triggers_comm *triggers_comm;
634 struct lttng_triggers *local_triggers = NULL;
635
636 if (!src_view || !triggers) {
637 ret = -1;
638 goto error;
639 }
640
641 /* lttng_trigger_comms header */
642 triggers_comm = (const struct lttng_triggers_comm *) src_view->buffer.data;
643 offset += sizeof(*triggers_comm);
644
645 local_triggers = lttng_triggers_create();
646 if (!local_triggers) {
647 ret = -1;
648 goto error;
649 }
650
651 for (i = 0; i < triggers_comm->count; i++) {
652 struct lttng_trigger *trigger = NULL;
653 struct lttng_payload_view trigger_view =
654 lttng_payload_view_from_view(src_view, offset, -1);
655 ssize_t trigger_size;
656
657 trigger_size = lttng_trigger_create_from_payload(
658 &trigger_view, &trigger);
659 if (trigger_size < 0) {
660 ret = trigger_size;
661 goto error;
662 }
663
664 /* Transfer ownership of the trigger to the collection. */
665 ret = lttng_triggers_add(local_triggers, trigger);
666 lttng_trigger_put(trigger);
667 if (ret < 0) {
668 ret = -1;
669 goto error;
670 }
671
672 offset += trigger_size;
673 triggers_size += trigger_size;
674 }
675
676 /* Unexpected size of inner-elements; the buffer is corrupted. */
677 if ((ssize_t) triggers_comm->length != triggers_size) {
678 ret = -1;
679 goto error;
680 }
681
682 /* Pass ownership to caller. */
683 *triggers = local_triggers;
684 local_triggers = NULL;
685
686 ret = offset;
687error:
688
689 lttng_triggers_destroy(local_triggers);
690 return ret;
691}
692
3da864a9
JR
693LTTNG_HIDDEN
694const struct lttng_credentials *lttng_trigger_get_credentials(
695 const struct lttng_trigger *trigger)
696{
64eafdf6 697 return &trigger->creds;
3da864a9
JR
698}
699
700LTTNG_HIDDEN
64eafdf6 701void lttng_trigger_set_credentials(struct lttng_trigger *trigger,
3da864a9
JR
702 const struct lttng_credentials *creds)
703{
704 assert(creds);
64eafdf6
JR
705 trigger->creds = *creds;
706}
707
708enum lttng_trigger_status lttng_trigger_set_owner_uid(
709 struct lttng_trigger *trigger, uid_t uid)
710{
711 enum lttng_trigger_status ret = LTTNG_TRIGGER_STATUS_OK;
d84c8ae3 712 const uid_t euid = geteuid();
64eafdf6
JR
713 const struct lttng_credentials creds = {
714 .uid = LTTNG_OPTIONAL_INIT_VALUE(uid),
715 .gid = LTTNG_OPTIONAL_INIT_UNSET,
716 };
717
718 if (!trigger) {
719 ret = LTTNG_TRIGGER_STATUS_INVALID;
720 goto end;
721 }
722
723 /* Client-side validation only to report a clearer error. */
d84c8ae3 724 if (euid != 0 && euid != uid) {
64eafdf6
JR
725 ret = LTTNG_TRIGGER_STATUS_PERMISSION_DENIED;
726 goto end;
727 }
728
729 lttng_trigger_set_credentials(trigger, &creds);
730
731end:
732 return ret;
733}
734
735enum lttng_trigger_status lttng_trigger_get_owner_uid(
736 const struct lttng_trigger *trigger, uid_t *uid)
737{
738 enum lttng_trigger_status ret = LTTNG_TRIGGER_STATUS_OK;
739 const struct lttng_credentials *creds = NULL;
740
741 if (!trigger || !uid ) {
742 ret = LTTNG_TRIGGER_STATUS_INVALID;
743 goto end;
744 }
745
746 if (!trigger->creds.uid.is_set ) {
747 ret = LTTNG_TRIGGER_STATUS_UNSET;
748 goto end;
749 }
750
751 creds = lttng_trigger_get_credentials(trigger);
752 *uid = lttng_credentials_get_uid(creds);
753
754end:
755 return ret;
3da864a9 756}
5c504c41 757
91c96f62
JR
758LTTNG_HIDDEN
759enum lttng_domain_type lttng_trigger_get_underlying_domain_type_restriction(
760 const struct lttng_trigger *trigger)
761{
762 enum lttng_domain_type type = LTTNG_DOMAIN_NONE;
763 const struct lttng_event_rule *event_rule;
764 enum lttng_condition_status c_status;
765 enum lttng_condition_type c_type;
766
767 assert(trigger);
768 assert(trigger->condition);
769
770 c_type = lttng_condition_get_type(trigger->condition);
771 assert (c_type != LTTNG_CONDITION_TYPE_UNKNOWN);
772
773 switch (c_type) {
774 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
775 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
776 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
777 /* Apply to any domain. */
778 type = LTTNG_DOMAIN_NONE;
779 break;
8dbb86b8 780 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES:
91c96f62 781 /* Return the domain of the event rule. */
8dbb86b8 782 c_status = lttng_condition_event_rule_matches_get_rule(
91c96f62
JR
783 trigger->condition, &event_rule);
784 assert(c_status == LTTNG_CONDITION_STATUS_OK);
785 type = lttng_event_rule_get_domain_type(event_rule);
786 break;
787 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
788 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
789 /* Return the domain of the channel being monitored. */
790 c_status = lttng_condition_buffer_usage_get_domain_type(
791 trigger->condition, &type);
792 assert(c_status == LTTNG_CONDITION_STATUS_OK);
793 break;
794 default:
795 abort();
796 }
797
798 return type;
799}
58daac01
JR
800
801/*
802 * Generate bytecode related to the trigger.
803 * On success LTTNG_OK. On error, returns lttng_error code.
804 */
805LTTNG_HIDDEN
806enum lttng_error_code lttng_trigger_generate_bytecode(
807 struct lttng_trigger *trigger,
808 const struct lttng_credentials *creds)
809{
810 enum lttng_error_code ret;
811 struct lttng_condition *condition = NULL;
812
813 condition = lttng_trigger_get_condition(trigger);
814 if (!condition) {
815 ret = LTTNG_ERR_INVALID_TRIGGER;
816 goto end;
817 }
818
819 switch (lttng_condition_get_type(condition)) {
8dbb86b8 820 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES:
58daac01
JR
821 {
822 struct lttng_event_rule *event_rule;
823 const enum lttng_condition_status condition_status =
8dbb86b8
JR
824 lttng_condition_event_rule_matches_borrow_rule_mutable(
825 condition, &event_rule);
58daac01
JR
826
827 assert(condition_status == LTTNG_CONDITION_STATUS_OK);
f2e97f59
JR
828
829 /* Generate the filter bytecode. */
58daac01
JR
830 ret = lttng_event_rule_generate_filter_bytecode(
831 event_rule, creds);
832 if (ret != LTTNG_OK) {
833 goto end;
834 }
835
f2e97f59 836 /* Generate the capture bytecode. */
8dbb86b8 837 ret = lttng_condition_event_rule_matches_generate_capture_descriptor_bytecode(
f2e97f59
JR
838 condition);
839 if (ret != LTTNG_OK) {
840 goto end;
841 }
842
58daac01
JR
843 ret = LTTNG_OK;
844 break;
845 }
846 default:
847 ret = LTTNG_OK;
848 break;
849 }
850end:
851 return ret;
852}
b61776fb
SM
853
854LTTNG_HIDDEN
855struct lttng_trigger *lttng_trigger_copy(const struct lttng_trigger *trigger)
856{
857 int ret;
858 struct lttng_payload copy_buffer;
94dbd8e4
JG
859 struct lttng_condition *condition_copy = NULL;
860 struct lttng_action *action_copy = NULL;
b61776fb 861 struct lttng_trigger *copy = NULL;
94dbd8e4
JG
862 enum lttng_trigger_status trigger_status;
863 const char *trigger_name;
864 uid_t trigger_owner_uid;
b61776fb
SM
865
866 lttng_payload_init(&copy_buffer);
867
94dbd8e4 868 ret = lttng_condition_serialize(trigger->condition, &copy_buffer);
b61776fb
SM
869 if (ret < 0) {
870 goto end;
871 }
872
873 {
874 struct lttng_payload_view view =
875 lttng_payload_view_from_payload(
876 &copy_buffer, 0, -1);
94dbd8e4
JG
877
878 ret = lttng_condition_create_from_payload(
879 &view, &condition_copy);
880 if (ret < 0) {
881 goto end;
882 }
883 }
884
885 lttng_payload_clear(&copy_buffer);
886
887 ret = lttng_action_serialize(trigger->action, &copy_buffer);
888 if (ret < 0) {
889 goto end;
890 }
891
892 {
893 struct lttng_payload_view view =
894 lttng_payload_view_from_payload(
895 &copy_buffer, 0, -1);
896
897 ret = lttng_action_create_from_payload(
898 &view, &action_copy);
b61776fb 899 if (ret < 0) {
b61776fb
SM
900 goto end;
901 }
902 }
903
94dbd8e4
JG
904 copy = lttng_trigger_create(condition_copy, action_copy);
905 if (!copy) {
906 ERR("Failed to allocate trigger during trigger copy");
907 goto end;
908 }
909
910 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
911 switch (trigger_status) {
912 case LTTNG_TRIGGER_STATUS_OK:
913 trigger_status = lttng_trigger_set_name(copy, trigger_name);
914 if (trigger_status != LTTNG_TRIGGER_STATUS_OK) {
915 ERR("Failed to set name of new trigger during copy");
916 goto error_cleanup_trigger;
917 }
918 break;
919 case LTTNG_TRIGGER_STATUS_UNSET:
920 break;
921 default:
922 ERR("Failed to get name of original trigger during copy");
923 goto error_cleanup_trigger;
924 }
925
926 trigger_status = lttng_trigger_get_owner_uid(
927 trigger, &trigger_owner_uid);
928 switch (trigger_status) {
929 case LTTNG_TRIGGER_STATUS_OK:
930 LTTNG_OPTIONAL_SET(&copy->creds.uid, trigger_owner_uid);
931 break;
932 case LTTNG_TRIGGER_STATUS_UNSET:
933 break;
934 default:
935 ERR("Failed to get owner uid of original trigger during copy");
936 goto error_cleanup_trigger;
937 }
938
939 copy->tracer_token = trigger->tracer_token;
940 copy->registered = trigger->registered;
941 goto end;
942
943error_cleanup_trigger:
944 lttng_trigger_destroy(copy);
945 copy = NULL;
b61776fb 946end:
94dbd8e4
JG
947 lttng_condition_put(condition_copy);
948 lttng_action_put(action_copy);
b61776fb
SM
949 lttng_payload_reset(&copy_buffer);
950 return copy;
951}
c738df17
FD
952
953LTTNG_HIDDEN
954bool lttng_trigger_needs_tracer_notifier(const struct lttng_trigger *trigger)
955{
956 bool needs_tracer_notifier = false;
957 const struct lttng_condition *condition =
958 lttng_trigger_get_const_condition(trigger);
959
960 switch (lttng_condition_get_type(condition)) {
8dbb86b8 961 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES:
c738df17
FD
962 needs_tracer_notifier = true;
963 goto end;
964 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
965 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
966 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
967 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
968 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
969 goto end;
970 case LTTNG_CONDITION_TYPE_UNKNOWN:
971 default:
972 abort();
973 }
974end:
975 return needs_tracer_notifier;
976}
9c374932
JR
977
978LTTNG_HIDDEN
979void lttng_trigger_set_as_registered(struct lttng_trigger *trigger)
980{
981 pthread_mutex_lock(&trigger->lock);
982 trigger->registered = true;
983 pthread_mutex_unlock(&trigger->lock);
984}
985
986LTTNG_HIDDEN
987void lttng_trigger_set_as_unregistered(struct lttng_trigger *trigger)
988{
989 pthread_mutex_lock(&trigger->lock);
990 trigger->registered = false;
991 pthread_mutex_unlock(&trigger->lock);
992}
993
994/*
995 * The trigger must be locked before calling lttng_trigger_registered.
996 * The lock is necessary since a trigger can be unregistered at anytime.
997 * Manipulations requiring that the trigger be registered must always acquire
998 * the trigger lock for the duration of the manipulation using
999 * `lttng_trigger_lock` and `lttng_trigger_unlock`.
1000 */
1001LTTNG_HIDDEN
1002bool lttng_trigger_is_registered(struct lttng_trigger *trigger)
1003{
1004 ASSERT_LOCKED(trigger->lock);
1005 return trigger->registered;
1006}
1007
1008LTTNG_HIDDEN
1009void lttng_trigger_lock(struct lttng_trigger *trigger)
1010{
1011 pthread_mutex_lock(&trigger->lock);
1012}
1013
1014LTTNG_HIDDEN
1015void lttng_trigger_unlock(struct lttng_trigger *trigger)
1016{
1017 pthread_mutex_unlock(&trigger->lock);
1018}
This page took 0.080252 seconds and 4 git commands to generate.