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