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