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