Build fix: poll compatibility mode: zmalloc prototype changed
[lttng-tools.git] / src / common / trigger.cpp
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
c9e313bc
SM
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>
242388e4 15#include <inttypes.h>
c9e313bc 16#include <lttng/action/action-internal.hpp>
9c374932 17#include <lttng/condition/buffer-usage.h>
c9e313bc
SM
18#include <lttng/condition/condition-internal.hpp>
19#include <lttng/condition/event-rule-matches-internal.hpp>
670a26e4 20#include <lttng/condition/event-rule-matches.h>
9c374932 21#include <lttng/domain.h>
c9e313bc
SM
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>
9c374932 26#include <pthread.h>
a58c490f 27
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
64803277 58 trigger = zmalloc<lttng_trigger>();
a58c490f
JG
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
a0377dfe
FD
120 LTTNG_ASSERT(action);
121 LTTNG_ASSERT(condition);
7ca172c1
JR
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
c0a66c84
JG
138ssize_t lttng_trigger_create_from_payload(
139 struct lttng_payload_view *src_view,
6808ef55 140 struct lttng_trigger **_trigger)
a58c490f 141{
242388e4 142 ssize_t ret, offset = 0, condition_size, action_size, name_size = 0;
a58c490f
JG
143 struct lttng_condition *condition = NULL;
144 struct lttng_action *action = NULL;
145 const struct lttng_trigger_comm *trigger_comm;
242388e4 146 const char *name = NULL;
64eafdf6
JR
147 struct lttng_credentials creds = {
148 .uid = LTTNG_OPTIONAL_INIT_UNSET,
149 .gid = LTTNG_OPTIONAL_INIT_UNSET,
150 };
6808ef55 151 struct lttng_trigger *trigger = NULL;
3e6e0df2
JG
152 const struct lttng_payload_view trigger_comm_view =
153 lttng_payload_view_from_view(
154 src_view, 0, sizeof(*trigger_comm));
a58c490f 155
6808ef55 156 if (!src_view || !_trigger) {
a58c490f
JG
157 ret = -1;
158 goto end;
159 }
160
3e6e0df2
JG
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
a58c490f 167 /* lttng_trigger_comm header */
3e6e0df2 168 trigger_comm = (typeof(trigger_comm)) trigger_comm_view.buffer.data;
64eafdf6
JR
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
a58c490f 179 offset += sizeof(*trigger_comm);
242388e4
JR
180
181 if (trigger_comm->name_length != 0) {
182 /* Name. */
183 const struct lttng_payload_view name_view =
184 lttng_payload_view_from_view(
3e6e0df2
JG
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 }
242388e4
JR
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
c0a66c84
JG
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 }
a58c490f 213
a58c490f
JG
214 if (condition_size < 0) {
215 ret = condition_size;
216 goto end;
217 }
c0a66c84 218
a58c490f 219 offset += condition_size;
c0a66c84
JG
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 }
a58c490f 228
a58c490f
JG
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. */
242388e4 236 if ((ssize_t) trigger_comm->length != condition_size + action_size + name_size) {
a58c490f
JG
237 ret = -1;
238 goto error;
239 }
240
6808ef55
JG
241 trigger = lttng_trigger_create(condition, action);
242 if (!trigger) {
a58c490f
JG
243 ret = -1;
244 goto error;
245 }
c0a66c84 246
6808ef55 247 lttng_trigger_set_credentials(trigger, &creds);
64eafdf6 248
7ca172c1
JR
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
242388e4
JR
259 if (name) {
260 const enum lttng_trigger_status status =
6808ef55 261 lttng_trigger_set_name(trigger, name);
242388e4
JR
262
263 if (status != LTTNG_TRIGGER_STATUS_OK) {
264 ret = -1;
265 goto end;
266 }
267 }
268
a58c490f 269 ret = offset;
7ca172c1 270
a58c490f 271error:
1065801b
JG
272 lttng_condition_put(condition);
273 lttng_action_put(action);
7ca172c1 274end:
f5d98ed9 275 if (ret >= 0) {
6808ef55
JG
276 *_trigger = trigger;
277 } else {
278 lttng_trigger_put(trigger);
279 }
280
a58c490f
JG
281 return ret;
282}
283
284/*
a58c490f
JG
285 * Both elements are stored contiguously, see their "*_comm" structure
286 * for the detailed format.
287 */
a02903c0 288int lttng_trigger_serialize(const struct lttng_trigger *trigger,
c0a66c84 289 struct lttng_payload *payload)
a58c490f 290{
3647288f 291 int ret;
242388e4 292 size_t header_offset, size_before_payload, size_name;
c0a66c84 293 struct lttng_trigger_comm trigger_comm = {};
3647288f 294 struct lttng_trigger_comm *header;
64eafdf6
JR
295 const struct lttng_credentials *creds = NULL;
296
297 creds = lttng_trigger_get_credentials(trigger);
a0377dfe 298 LTTNG_ASSERT(creds);
64eafdf6
JR
299
300 trigger_comm.uid = LTTNG_OPTIONAL_GET(creds->uid);
a58c490f 301
242388e4
JR
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
c0a66c84
JG
310 header_offset = payload->buffer.size;
311 ret = lttng_dynamic_buffer_append(&payload->buffer, &trigger_comm,
3647288f
JG
312 sizeof(trigger_comm));
313 if (ret) {
a58c490f
JG
314 goto end;
315 }
316
c0a66c84 317 size_before_payload = payload->buffer.size;
242388e4
JR
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
c0a66c84 326 ret = lttng_condition_serialize(trigger->condition, payload);
3647288f 327 if (ret) {
a58c490f
JG
328 goto end;
329 }
a58c490f 330
c0a66c84 331 ret = lttng_action_serialize(trigger->action, payload);
3647288f 332 if (ret) {
a58c490f
JG
333 goto end;
334 }
a58c490f 335
3647288f 336 /* Update payload size. */
c0a66c84
JG
337 header = (typeof(header)) (payload->buffer.data + header_offset);
338 header->length = payload->buffer.size - size_before_payload;
a58c490f
JG
339end:
340 return ret;
341}
3da864a9 342
85c06c44
JR
343bool lttng_trigger_is_equal(
344 const struct lttng_trigger *a, const struct lttng_trigger *b)
345{
0efb2ad7
JG
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) {
1ca78886
FD
352 return false;
353 }
354
85c06c44
JR
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
f2bda80e
JG
368 if (a->is_hidden != b->is_hidden) {
369 return false;
370 }
371
85c06c44
JR
372 return true;
373}
374
f2bda80e
JG
375bool lttng_trigger_is_hidden(const struct lttng_trigger *trigger)
376{
6ce1601f 377 LTTNG_ASSERT(trigger);
f2bda80e
JG
378 return trigger->is_hidden;
379}
380
f2bda80e
JG
381void lttng_trigger_set_hidden(struct lttng_trigger *trigger)
382{
a0377dfe 383 LTTNG_ASSERT(!trigger->is_hidden);
f2bda80e
JG
384 trigger->is_hidden = true;
385}
386
242388e4
JR
387enum 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
a5c2d2a7 393 if (!trigger) {
242388e4
JR
394 status = LTTNG_TRIGGER_STATUS_INVALID;
395 goto end;
396 }
397
a5c2d2a7
JG
398 if (name) {
399 name_copy = strdup(name);
400 if (!name_copy) {
401 status = LTTNG_TRIGGER_STATUS_ERROR;
402 goto end;
403 }
242388e4
JR
404 }
405
406 free(trigger->name);
407
408 trigger->name = name_copy;
409 name_copy = NULL;
410end:
411 return status;
412}
413
414enum 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;
429end:
430 return status;
431}
432
242388e4
JR
433int 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 }
445end:
446 return ret;
447}
448
e6887944
JR
449void lttng_trigger_set_tracer_token(struct lttng_trigger *trigger,
450 uint64_t token)
451{
a0377dfe 452 LTTNG_ASSERT(trigger);
e6887944
JR
453 LTTNG_OPTIONAL_SET(&trigger->tracer_token, token);
454}
455
e6887944
JR
456uint64_t lttng_trigger_get_tracer_token(const struct lttng_trigger *trigger)
457{
a0377dfe 458 LTTNG_ASSERT(trigger);
e6887944
JR
459
460 return LTTNG_OPTIONAL_GET(trigger->tracer_token);
461}
462
242388e4
JR
463int lttng_trigger_generate_name(struct lttng_trigger *trigger,
464 uint64_t unique_id)
465{
466 int ret = 0;
467 char *generated_name = NULL;
468
80ea4bfa 469 ret = asprintf(&generated_name, "trigger%" PRIu64 "", unique_id);
242388e4
JR
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;
479end:
480 return ret;
481}
482
f01d28b4
JR
483void lttng_trigger_get(struct lttng_trigger *trigger)
484{
485 urcu_ref_get(&trigger->ref);
486}
487
f01d28b4
JR
488void 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
a02903c0
JR
497static void delete_trigger_array_element(void *ptr)
498{
a6bc4ca9 499 struct lttng_trigger *trigger = (lttng_trigger *) ptr;
a02903c0
JR
500
501 lttng_trigger_put(trigger);
502}
503
a02903c0
JR
504struct lttng_triggers *lttng_triggers_create(void)
505{
506 struct lttng_triggers *triggers = NULL;
507
64803277 508 triggers = zmalloc<lttng_triggers>();
a02903c0
JR
509 if (!triggers) {
510 goto end;
511 }
512
513 lttng_dynamic_pointer_array_init(&triggers->array, delete_trigger_array_element);
514
515end:
516 return triggers;
517}
518
a02903c0
JR
519struct 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
a0377dfe 524 LTTNG_ASSERT(triggers);
a02903c0
JR
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);
532end:
533 return trigger;
534}
535
a02903c0
JR
536int lttng_triggers_add(
537 struct lttng_triggers *triggers, struct lttng_trigger *trigger)
538{
539 int ret;
540
a0377dfe
FD
541 LTTNG_ASSERT(triggers);
542 LTTNG_ASSERT(trigger);
a02903c0
JR
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
f2bda80e
JG
554int 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
a0377dfe 560 LTTNG_ASSERT(triggers);
f2bda80e
JG
561
562 trigger_status = lttng_triggers_get_count(triggers, &trigger_count);
a0377dfe 563 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
f2bda80e
JG
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;
583end:
584 return ret;
585}
586
a02903c0
JR
587const 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
593enum 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);
603end:
604 return status;
605}
606
607void 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
617int 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
a0377dfe 649 LTTNG_ASSERT(trigger);
a02903c0
JR
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;
660end:
661 return ret;
662}
663
a02903c0
JR
664ssize_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;
724error:
725
726 lttng_triggers_destroy(local_triggers);
727 return ret;
728}
729
3da864a9
JR
730const struct lttng_credentials *lttng_trigger_get_credentials(
731 const struct lttng_trigger *trigger)
732{
64eafdf6 733 return &trigger->creds;
3da864a9
JR
734}
735
64eafdf6 736void lttng_trigger_set_credentials(struct lttng_trigger *trigger,
3da864a9
JR
737 const struct lttng_credentials *creds)
738{
a0377dfe 739 LTTNG_ASSERT(creds);
64eafdf6
JR
740 trigger->creds = *creds;
741}
742
743enum 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;
d84c8ae3 747 const uid_t euid = geteuid();
64eafdf6
JR
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. */
d84c8ae3 759 if (euid != 0 && euid != uid) {
64eafdf6
JR
760 ret = LTTNG_TRIGGER_STATUS_PERMISSION_DENIED;
761 goto end;
762 }
763
764 lttng_trigger_set_credentials(trigger, &creds);
765
766end:
767 return ret;
768}
769
770enum 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
789end:
790 return ret;
3da864a9 791}
5c504c41 792
91c96f62
JR
793enum 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
a0377dfe
FD
801 LTTNG_ASSERT(trigger);
802 LTTNG_ASSERT(trigger->condition);
91c96f62
JR
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;
8dbb86b8 814 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES:
91c96f62 815 /* Return the domain of the event rule. */
8dbb86b8 816 c_status = lttng_condition_event_rule_matches_get_rule(
91c96f62 817 trigger->condition, &event_rule);
a0377dfe 818 LTTNG_ASSERT(c_status == LTTNG_CONDITION_STATUS_OK);
91c96f62
JR
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);
a0377dfe 826 LTTNG_ASSERT(c_status == LTTNG_CONDITION_STATUS_OK);
91c96f62
JR
827 break;
828 default:
829 abort();
830 }
831
832 return type;
833}
58daac01
JR
834
835/*
836 * Generate bytecode related to the trigger.
837 * On success LTTNG_OK. On error, returns lttng_error code.
838 */
58daac01
JR
839enum 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)) {
8dbb86b8 853 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES:
58daac01
JR
854 {
855 struct lttng_event_rule *event_rule;
856 const enum lttng_condition_status condition_status =
8dbb86b8
JR
857 lttng_condition_event_rule_matches_borrow_rule_mutable(
858 condition, &event_rule);
58daac01 859
a0377dfe 860 LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK);
f2e97f59
JR
861
862 /* Generate the filter bytecode. */
58daac01
JR
863 ret = lttng_event_rule_generate_filter_bytecode(
864 event_rule, creds);
865 if (ret != LTTNG_OK) {
866 goto end;
867 }
868
f2e97f59 869 /* Generate the capture bytecode. */
8dbb86b8 870 ret = lttng_condition_event_rule_matches_generate_capture_descriptor_bytecode(
f2e97f59
JR
871 condition);
872 if (ret != LTTNG_OK) {
873 goto end;
874 }
875
58daac01
JR
876 ret = LTTNG_OK;
877 break;
878 }
879 default:
880 ret = LTTNG_OK;
881 break;
882 }
883end:
884 return ret;
885}
b61776fb 886
b61776fb
SM
887struct lttng_trigger *lttng_trigger_copy(const struct lttng_trigger *trigger)
888{
889 int ret;
890 struct lttng_payload copy_buffer;
94dbd8e4
JG
891 struct lttng_condition *condition_copy = NULL;
892 struct lttng_action *action_copy = NULL;
b61776fb 893 struct lttng_trigger *copy = NULL;
94dbd8e4
JG
894 enum lttng_trigger_status trigger_status;
895 const char *trigger_name;
896 uid_t trigger_owner_uid;
b61776fb
SM
897
898 lttng_payload_init(&copy_buffer);
899
94dbd8e4 900 ret = lttng_condition_serialize(trigger->condition, &copy_buffer);
b61776fb
SM
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);
94dbd8e4
JG
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);
b61776fb 931 if (ret < 0) {
b61776fb
SM
932 goto end;
933 }
934 }
935
94dbd8e4
JG
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;
f2bda80e 973 copy->is_hidden = trigger->is_hidden;
94dbd8e4
JG
974 goto end;
975
976error_cleanup_trigger:
977 lttng_trigger_destroy(copy);
978 copy = NULL;
b61776fb 979end:
94dbd8e4
JG
980 lttng_condition_put(condition_copy);
981 lttng_action_put(action_copy);
b61776fb
SM
982 lttng_payload_reset(&copy_buffer);
983 return copy;
984}
c738df17 985
c738df17
FD
986bool 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)) {
8dbb86b8 993 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES:
c738df17
FD
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 }
1006end:
1007 return needs_tracer_notifier;
1008}
9c374932 1009
9c374932
JR
1010void 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
9c374932
JR
1017void 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 */
9c374932
JR
1031bool lttng_trigger_is_registered(struct lttng_trigger *trigger)
1032{
1033 ASSERT_LOCKED(trigger->lock);
1034 return trigger->registered;
1035}
1036
9c374932
JR
1037void lttng_trigger_lock(struct lttng_trigger *trigger)
1038{
1039 pthread_mutex_lock(&trigger->lock);
1040}
1041
9c374932
JR
1042void lttng_trigger_unlock(struct lttng_trigger *trigger)
1043{
1044 pthread_mutex_unlock(&trigger->lock);
1045}
6a751b95 1046
6a751b95
JR
1047enum 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
a0377dfe
FD
1060 LTTNG_ASSERT(trigger);
1061 LTTNG_ASSERT(writer);
6a751b95
JR
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);
a0377dfe 1072 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
6a751b95
JR
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);
a0377dfe 1091 LTTNG_ASSERT(condition);
6a751b95
JR
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);
a0377dfe 1100 LTTNG_ASSERT(action);
6a751b95
JR
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
1132mi_error:
1133 ret_code = LTTNG_ERR_MI_IO_FAIL;
1134end:
1135 lttng_dynamic_array_reset(&action_path_indexes);
1136 return ret_code;
1137}
1138
1139/* Used by qsort, which expects the semantics of strcmp(). */
1140static 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);
a0377dfe 1151 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
6a751b95
JR
1152
1153 trigger_status = lttng_trigger_get_name(trigger_b, &name_b);
a0377dfe 1154 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
6a751b95
JR
1155
1156 return strcmp(name_a, name_b);
1157}
1158
6a751b95
JR
1159enum 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
a0377dfe
FD
1170 LTTNG_ASSERT(triggers);
1171 LTTNG_ASSERT(writer);
6a751b95
JR
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);
a0377dfe 1180 LTTNG_ASSERT(status == LTTNG_TRIGGER_STATUS_OK);
6a751b95
JR
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
1238error:
1239 lttng_dynamic_pointer_array_reset(&sorted_triggers);
1240 return ret_code;
1241}
This page took 0.137646 seconds and 4 git commands to generate.