Commit | Line | Data |
---|---|---|
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 | ||
8 | #include <lttng/trigger/trigger-internal.h> | |
9 | #include <lttng/condition/condition-internal.h> | |
e393070a JR |
10 | #include <lttng/condition/on-event-internal.h> |
11 | #include <lttng/condition/on-event.h> | |
12 | #include <lttng/condition/on-event-internal.h> | |
91c96f62 JR |
13 | #include <lttng/condition/buffer-usage.h> |
14 | #include <lttng/event-rule/event-rule-internal.h> | |
f2e97f59 | 15 | #include <lttng/event-expr-internal.h> |
a58c490f | 16 | #include <lttng/action/action-internal.h> |
3da864a9 | 17 | #include <common/credentials.h> |
9e620ea7 JG |
18 | #include <common/payload.h> |
19 | #include <common/payload-view.h> | |
91c96f62 | 20 | #include <lttng/domain.h> |
a58c490f | 21 | #include <common/error.h> |
a02903c0 | 22 | #include <common/dynamic-array.h> |
3da864a9 | 23 | #include <common/optional.h> |
a58c490f | 24 | #include <assert.h> |
242388e4 | 25 | #include <inttypes.h> |
a58c490f JG |
26 | |
27 | LTTNG_HIDDEN | |
b61776fb | 28 | bool 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); | |
44 | end: | |
45 | return valid; | |
46 | } | |
47 | ||
48 | struct lttng_trigger *lttng_trigger_create( | |
49 | struct lttng_condition *condition, | |
50 | struct lttng_action *action) | |
51 | { | |
52 | struct lttng_trigger *trigger = NULL; | |
53 | ||
54 | if (!condition || !action) { | |
55 | goto end; | |
56 | } | |
57 | ||
58 | trigger = zmalloc(sizeof(struct lttng_trigger)); | |
59 | if (!trigger) { | |
60 | goto end; | |
61 | } | |
62 | ||
f01d28b4 JR |
63 | urcu_ref_init(&trigger->ref); |
64 | ||
5c504c41 JR |
65 | trigger->firing_policy.type = LTTNG_TRIGGER_FIRING_POLICY_EVERY_N; |
66 | trigger->firing_policy.threshold = 1; | |
67 | ||
7ca172c1 | 68 | lttng_condition_get(condition); |
a58c490f | 69 | trigger->condition = condition; |
7ca172c1 JR |
70 | |
71 | lttng_action_get(action); | |
a58c490f | 72 | trigger->action = action; |
3da864a9 | 73 | |
a58c490f JG |
74 | end: |
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 |
83 | struct lttng_condition *lttng_trigger_get_condition( |
84 | struct lttng_trigger *trigger) | |
85 | { | |
86 | return trigger ? trigger->condition : NULL; | |
87 | } | |
88 | ||
9b63a4aa JG |
89 | const 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 | 100 | struct lttng_action *lttng_trigger_get_action( |
a58c490f JG |
101 | struct lttng_trigger *trigger) |
102 | { | |
103 | return trigger ? trigger->action : NULL; | |
104 | } | |
105 | ||
9b63a4aa JG |
106 | const 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 | 112 | static void trigger_destroy_ref(struct urcu_ref *ref) |
a58c490f | 113 | { |
f01d28b4 JR |
114 | struct lttng_trigger *trigger = |
115 | container_of(ref, struct lttng_trigger, ref); | |
7ca172c1 JR |
116 | struct lttng_action *action = lttng_trigger_get_action(trigger); |
117 | struct lttng_condition *condition = | |
118 | lttng_trigger_get_condition(trigger); | |
119 | ||
7ca172c1 JR |
120 | assert(action); |
121 | assert(condition); | |
122 | ||
123 | /* Release ownership. */ | |
124 | lttng_action_put(action); | |
125 | lttng_condition_put(condition); | |
126 | ||
242388e4 | 127 | free(trigger->name); |
a58c490f JG |
128 | free(trigger); |
129 | } | |
130 | ||
f01d28b4 JR |
131 | void lttng_trigger_destroy(struct lttng_trigger *trigger) |
132 | { | |
133 | lttng_trigger_put(trigger); | |
134 | } | |
135 | ||
5c504c41 JR |
136 | static bool is_firing_policy_valid(enum lttng_trigger_firing_policy policy) |
137 | { | |
138 | bool valid = false; | |
139 | ||
140 | switch (policy) { | |
141 | case LTTNG_TRIGGER_FIRING_POLICY_EVERY_N: | |
142 | case LTTNG_TRIGGER_FIRING_POLICY_ONCE_AFTER_N: | |
143 | valid = true; | |
144 | break; | |
145 | default: | |
146 | valid = false; | |
147 | break; | |
148 | } | |
149 | ||
150 | return valid; | |
151 | } | |
152 | ||
a58c490f | 153 | LTTNG_HIDDEN |
c0a66c84 JG |
154 | ssize_t lttng_trigger_create_from_payload( |
155 | struct lttng_payload_view *src_view, | |
6808ef55 | 156 | struct lttng_trigger **_trigger) |
a58c490f | 157 | { |
242388e4 | 158 | ssize_t ret, offset = 0, condition_size, action_size, name_size = 0; |
a58c490f JG |
159 | struct lttng_condition *condition = NULL; |
160 | struct lttng_action *action = NULL; | |
161 | const struct lttng_trigger_comm *trigger_comm; | |
242388e4 | 162 | const char *name = NULL; |
5c504c41 JR |
163 | uint64_t firing_policy_threshold; |
164 | enum lttng_trigger_firing_policy firing_policy; | |
64eafdf6 JR |
165 | struct lttng_credentials creds = { |
166 | .uid = LTTNG_OPTIONAL_INIT_UNSET, | |
167 | .gid = LTTNG_OPTIONAL_INIT_UNSET, | |
168 | }; | |
6808ef55 | 169 | struct lttng_trigger *trigger = NULL; |
3e6e0df2 JG |
170 | const struct lttng_payload_view trigger_comm_view = |
171 | lttng_payload_view_from_view( | |
172 | src_view, 0, sizeof(*trigger_comm)); | |
a58c490f | 173 | |
6808ef55 | 174 | if (!src_view || !_trigger) { |
a58c490f JG |
175 | ret = -1; |
176 | goto end; | |
177 | } | |
178 | ||
3e6e0df2 JG |
179 | if (!lttng_payload_view_is_valid(&trigger_comm_view)) { |
180 | /* Payload not large enough to contain the header. */ | |
181 | ret = -1; | |
182 | goto end; | |
183 | } | |
184 | ||
a58c490f | 185 | /* lttng_trigger_comm header */ |
3e6e0df2 | 186 | trigger_comm = (typeof(trigger_comm)) trigger_comm_view.buffer.data; |
64eafdf6 JR |
187 | |
188 | /* Set the trigger's creds. */ | |
189 | if (trigger_comm->uid > (uint64_t) ((uid_t) -1)) { | |
190 | /* UID out of range for this platform. */ | |
191 | ret = -1; | |
192 | goto end; | |
193 | } | |
194 | ||
195 | LTTNG_OPTIONAL_SET(&creds.uid, trigger_comm->uid); | |
196 | ||
a58c490f | 197 | offset += sizeof(*trigger_comm); |
242388e4 | 198 | |
5c504c41 JR |
199 | firing_policy = trigger_comm->firing_policy_type; |
200 | if (!is_firing_policy_valid(firing_policy)) { | |
201 | ret =-1; | |
202 | goto end; | |
203 | } | |
204 | ||
205 | firing_policy_threshold = trigger_comm->firing_policy_threshold; | |
242388e4 JR |
206 | if (trigger_comm->name_length != 0) { |
207 | /* Name. */ | |
208 | const struct lttng_payload_view name_view = | |
209 | lttng_payload_view_from_view( | |
3e6e0df2 JG |
210 | src_view, offset, |
211 | trigger_comm->name_length); | |
212 | ||
213 | if (!lttng_payload_view_is_valid(&name_view)) { | |
214 | ret = -1; | |
215 | goto end; | |
216 | } | |
242388e4 JR |
217 | |
218 | name = name_view.buffer.data; | |
219 | if (!lttng_buffer_view_contains_string(&name_view.buffer, name, | |
220 | trigger_comm->name_length)) { | |
221 | ret = -1; | |
222 | goto end; | |
223 | } | |
224 | ||
225 | offset += trigger_comm->name_length; | |
226 | name_size = trigger_comm->name_length; | |
227 | } | |
228 | ||
c0a66c84 JG |
229 | { |
230 | /* struct lttng_condition */ | |
231 | struct lttng_payload_view condition_view = | |
232 | lttng_payload_view_from_view( | |
233 | src_view, offset, -1); | |
234 | ||
235 | condition_size = lttng_condition_create_from_payload(&condition_view, | |
236 | &condition); | |
237 | } | |
a58c490f | 238 | |
a58c490f JG |
239 | if (condition_size < 0) { |
240 | ret = condition_size; | |
241 | goto end; | |
242 | } | |
c0a66c84 | 243 | |
a58c490f | 244 | offset += condition_size; |
c0a66c84 JG |
245 | { |
246 | /* struct lttng_action */ | |
247 | struct lttng_payload_view action_view = | |
248 | lttng_payload_view_from_view( | |
249 | src_view, offset, -1); | |
250 | ||
251 | action_size = lttng_action_create_from_payload(&action_view, &action); | |
252 | } | |
a58c490f | 253 | |
a58c490f JG |
254 | if (action_size < 0) { |
255 | ret = action_size; | |
256 | goto end; | |
257 | } | |
258 | offset += action_size; | |
259 | ||
260 | /* Unexpected size of inner-elements; the buffer is corrupted. */ | |
242388e4 | 261 | if ((ssize_t) trigger_comm->length != condition_size + action_size + name_size) { |
a58c490f JG |
262 | ret = -1; |
263 | goto error; | |
264 | } | |
265 | ||
6808ef55 JG |
266 | trigger = lttng_trigger_create(condition, action); |
267 | if (!trigger) { | |
a58c490f JG |
268 | ret = -1; |
269 | goto error; | |
270 | } | |
c0a66c84 | 271 | |
6808ef55 | 272 | lttng_trigger_set_credentials(trigger, &creds); |
64eafdf6 | 273 | |
7ca172c1 JR |
274 | /* |
275 | * The trigger object owns references to the action and condition | |
276 | * objects. | |
277 | */ | |
278 | lttng_condition_put(condition); | |
279 | condition = NULL; | |
280 | ||
281 | lttng_action_put(action); | |
282 | action = NULL; | |
283 | ||
242388e4 JR |
284 | if (name) { |
285 | const enum lttng_trigger_status status = | |
6808ef55 | 286 | lttng_trigger_set_name(trigger, name); |
242388e4 JR |
287 | |
288 | if (status != LTTNG_TRIGGER_STATUS_OK) { | |
289 | ret = -1; | |
290 | goto end; | |
291 | } | |
292 | } | |
293 | ||
5c504c41 JR |
294 | /* Set the policy. */ |
295 | { | |
296 | const enum lttng_trigger_status status = | |
297 | lttng_trigger_set_firing_policy(trigger, | |
298 | firing_policy, | |
299 | firing_policy_threshold); | |
300 | ||
301 | if (status != LTTNG_TRIGGER_STATUS_OK) { | |
302 | ret = -1; | |
303 | goto end; | |
304 | } | |
305 | } | |
306 | ||
a58c490f | 307 | ret = offset; |
7ca172c1 | 308 | |
a58c490f | 309 | error: |
1065801b JG |
310 | lttng_condition_put(condition); |
311 | lttng_action_put(action); | |
7ca172c1 | 312 | end: |
f5d98ed9 | 313 | if (ret >= 0) { |
6808ef55 JG |
314 | *_trigger = trigger; |
315 | } else { | |
316 | lttng_trigger_put(trigger); | |
317 | } | |
318 | ||
a58c490f JG |
319 | return ret; |
320 | } | |
321 | ||
322 | /* | |
a58c490f JG |
323 | * Both elements are stored contiguously, see their "*_comm" structure |
324 | * for the detailed format. | |
325 | */ | |
326 | LTTNG_HIDDEN | |
a02903c0 | 327 | int lttng_trigger_serialize(const struct lttng_trigger *trigger, |
c0a66c84 | 328 | struct lttng_payload *payload) |
a58c490f | 329 | { |
3647288f | 330 | int ret; |
242388e4 | 331 | size_t header_offset, size_before_payload, size_name; |
c0a66c84 | 332 | struct lttng_trigger_comm trigger_comm = {}; |
3647288f | 333 | struct lttng_trigger_comm *header; |
64eafdf6 JR |
334 | const struct lttng_credentials *creds = NULL; |
335 | ||
336 | creds = lttng_trigger_get_credentials(trigger); | |
337 | assert(creds); | |
338 | ||
339 | trigger_comm.uid = LTTNG_OPTIONAL_GET(creds->uid); | |
a58c490f | 340 | |
242388e4 JR |
341 | if (trigger->name != NULL) { |
342 | size_name = strlen(trigger->name) + 1; | |
343 | } else { | |
344 | size_name = 0; | |
345 | } | |
346 | ||
347 | trigger_comm.name_length = size_name; | |
5c504c41 JR |
348 | trigger_comm.firing_policy_type = (uint8_t) trigger->firing_policy.type; |
349 | trigger_comm.firing_policy_threshold = (uint64_t) trigger->firing_policy.threshold; | |
242388e4 | 350 | |
c0a66c84 JG |
351 | header_offset = payload->buffer.size; |
352 | ret = lttng_dynamic_buffer_append(&payload->buffer, &trigger_comm, | |
3647288f JG |
353 | sizeof(trigger_comm)); |
354 | if (ret) { | |
a58c490f JG |
355 | goto end; |
356 | } | |
357 | ||
c0a66c84 | 358 | size_before_payload = payload->buffer.size; |
242388e4 JR |
359 | |
360 | /* Trigger name. */ | |
361 | ret = lttng_dynamic_buffer_append( | |
362 | &payload->buffer, trigger->name, size_name); | |
363 | if (ret) { | |
364 | goto end; | |
365 | } | |
366 | ||
c0a66c84 | 367 | ret = lttng_condition_serialize(trigger->condition, payload); |
3647288f | 368 | if (ret) { |
a58c490f JG |
369 | goto end; |
370 | } | |
a58c490f | 371 | |
c0a66c84 | 372 | ret = lttng_action_serialize(trigger->action, payload); |
3647288f | 373 | if (ret) { |
a58c490f JG |
374 | goto end; |
375 | } | |
a58c490f | 376 | |
3647288f | 377 | /* Update payload size. */ |
c0a66c84 JG |
378 | header = (typeof(header)) (payload->buffer.data + header_offset); |
379 | header->length = payload->buffer.size - size_before_payload; | |
a58c490f JG |
380 | end: |
381 | return ret; | |
382 | } | |
3da864a9 | 383 | |
85c06c44 JR |
384 | LTTNG_HIDDEN |
385 | bool lttng_trigger_is_equal( | |
386 | const struct lttng_trigger *a, const struct lttng_trigger *b) | |
387 | { | |
5c504c41 JR |
388 | if (a->firing_policy.type != b->firing_policy.type) { |
389 | return false; | |
390 | } | |
391 | ||
392 | if (a->firing_policy.threshold != b->firing_policy.threshold) { | |
393 | return false; | |
394 | } | |
395 | ||
85c06c44 JR |
396 | /* |
397 | * Name is not taken into account since it is cosmetic only. | |
398 | */ | |
399 | if (!lttng_condition_is_equal(a->condition, b->condition)) { | |
400 | return false; | |
401 | } | |
402 | ||
403 | if (!lttng_action_is_equal(a->action, b->action)) { | |
404 | return false; | |
405 | } | |
406 | ||
407 | if (!lttng_credentials_is_equal(lttng_trigger_get_credentials(a), | |
408 | lttng_trigger_get_credentials(b))) { | |
409 | return false; | |
410 | } | |
411 | ||
412 | return true; | |
413 | } | |
414 | ||
242388e4 JR |
415 | enum lttng_trigger_status lttng_trigger_set_name(struct lttng_trigger *trigger, |
416 | const char* name) | |
417 | { | |
418 | char *name_copy = NULL; | |
419 | enum lttng_trigger_status status = LTTNG_TRIGGER_STATUS_OK; | |
420 | ||
421 | if (!trigger || !name || | |
422 | strlen(name) == 0) { | |
423 | status = LTTNG_TRIGGER_STATUS_INVALID; | |
424 | goto end; | |
425 | } | |
426 | ||
427 | name_copy = strdup(name); | |
428 | if (!name_copy) { | |
429 | status = LTTNG_TRIGGER_STATUS_ERROR; | |
430 | goto end; | |
431 | } | |
432 | ||
433 | free(trigger->name); | |
434 | ||
435 | trigger->name = name_copy; | |
436 | name_copy = NULL; | |
437 | end: | |
438 | return status; | |
439 | } | |
440 | ||
441 | enum lttng_trigger_status lttng_trigger_get_name( | |
442 | const struct lttng_trigger *trigger, const char **name) | |
443 | { | |
444 | enum lttng_trigger_status status = LTTNG_TRIGGER_STATUS_OK; | |
445 | ||
446 | if (!trigger || !name) { | |
447 | status = LTTNG_TRIGGER_STATUS_INVALID; | |
448 | goto end; | |
449 | } | |
450 | ||
451 | if (!trigger->name) { | |
452 | status = LTTNG_TRIGGER_STATUS_UNSET; | |
453 | } | |
454 | ||
455 | *name = trigger->name; | |
456 | end: | |
457 | return status; | |
458 | } | |
459 | ||
460 | LTTNG_HIDDEN | |
461 | int lttng_trigger_assign_name(struct lttng_trigger *dst, | |
462 | const struct lttng_trigger *src) | |
463 | { | |
464 | int ret = 0; | |
465 | enum lttng_trigger_status status; | |
466 | ||
467 | status = lttng_trigger_set_name(dst, src->name); | |
468 | if (status != LTTNG_TRIGGER_STATUS_OK) { | |
469 | ret = -1; | |
470 | ERR("Failed to set name for trigger"); | |
471 | goto end; | |
472 | } | |
473 | end: | |
474 | return ret; | |
475 | } | |
476 | ||
e6887944 JR |
477 | LTTNG_HIDDEN |
478 | void lttng_trigger_set_tracer_token(struct lttng_trigger *trigger, | |
479 | uint64_t token) | |
480 | { | |
481 | assert(trigger); | |
482 | LTTNG_OPTIONAL_SET(&trigger->tracer_token, token); | |
483 | } | |
484 | ||
485 | LTTNG_HIDDEN | |
486 | uint64_t lttng_trigger_get_tracer_token(const struct lttng_trigger *trigger) | |
487 | { | |
488 | assert(trigger); | |
489 | ||
490 | return LTTNG_OPTIONAL_GET(trigger->tracer_token); | |
491 | } | |
492 | ||
242388e4 JR |
493 | LTTNG_HIDDEN |
494 | int lttng_trigger_generate_name(struct lttng_trigger *trigger, | |
495 | uint64_t unique_id) | |
496 | { | |
497 | int ret = 0; | |
498 | char *generated_name = NULL; | |
499 | ||
500 | ret = asprintf(&generated_name, "T%" PRIu64 "", unique_id); | |
501 | if (ret < 0) { | |
502 | ERR("Failed to generate trigger name"); | |
503 | ret = -1; | |
504 | goto end; | |
505 | } | |
506 | ||
507 | ret = 0; | |
508 | free(trigger->name); | |
509 | trigger->name = generated_name; | |
510 | end: | |
511 | return ret; | |
512 | } | |
513 | ||
f01d28b4 JR |
514 | LTTNG_HIDDEN |
515 | void lttng_trigger_get(struct lttng_trigger *trigger) | |
516 | { | |
517 | urcu_ref_get(&trigger->ref); | |
518 | } | |
519 | ||
520 | LTTNG_HIDDEN | |
521 | void lttng_trigger_put(struct lttng_trigger *trigger) | |
522 | { | |
523 | if (!trigger) { | |
524 | return; | |
525 | } | |
526 | ||
527 | urcu_ref_put(&trigger->ref , trigger_destroy_ref); | |
528 | } | |
529 | ||
a02903c0 JR |
530 | static void delete_trigger_array_element(void *ptr) |
531 | { | |
532 | struct lttng_trigger *trigger = ptr; | |
533 | ||
534 | lttng_trigger_put(trigger); | |
535 | } | |
536 | ||
537 | LTTNG_HIDDEN | |
538 | struct lttng_triggers *lttng_triggers_create(void) | |
539 | { | |
540 | struct lttng_triggers *triggers = NULL; | |
541 | ||
542 | triggers = zmalloc(sizeof(*triggers)); | |
543 | if (!triggers) { | |
544 | goto end; | |
545 | } | |
546 | ||
547 | lttng_dynamic_pointer_array_init(&triggers->array, delete_trigger_array_element); | |
548 | ||
549 | end: | |
550 | return triggers; | |
551 | } | |
552 | ||
553 | LTTNG_HIDDEN | |
554 | struct lttng_trigger *lttng_triggers_borrow_mutable_at_index( | |
555 | const struct lttng_triggers *triggers, unsigned int index) | |
556 | { | |
557 | struct lttng_trigger *trigger = NULL; | |
558 | ||
559 | assert(triggers); | |
560 | if (index >= lttng_dynamic_pointer_array_get_count(&triggers->array)) { | |
561 | goto end; | |
562 | } | |
563 | ||
564 | trigger = (struct lttng_trigger *) | |
565 | lttng_dynamic_pointer_array_get_pointer( | |
566 | &triggers->array, index); | |
567 | end: | |
568 | return trigger; | |
569 | } | |
570 | ||
571 | LTTNG_HIDDEN | |
572 | int lttng_triggers_add( | |
573 | struct lttng_triggers *triggers, struct lttng_trigger *trigger) | |
574 | { | |
575 | int ret; | |
576 | ||
577 | assert(triggers); | |
578 | assert(trigger); | |
579 | ||
580 | lttng_trigger_get(trigger); | |
581 | ||
582 | ret = lttng_dynamic_pointer_array_add_pointer(&triggers->array, trigger); | |
583 | if (ret) { | |
584 | lttng_trigger_put(trigger); | |
585 | } | |
586 | ||
587 | return ret; | |
588 | } | |
589 | ||
590 | const struct lttng_trigger *lttng_triggers_get_at_index( | |
591 | const struct lttng_triggers *triggers, unsigned int index) | |
592 | { | |
593 | return lttng_triggers_borrow_mutable_at_index(triggers, index); | |
594 | } | |
595 | ||
596 | enum lttng_trigger_status lttng_triggers_get_count(const struct lttng_triggers *triggers, unsigned int *count) | |
597 | { | |
598 | enum lttng_trigger_status status = LTTNG_TRIGGER_STATUS_OK; | |
599 | ||
600 | if (!triggers || !count) { | |
601 | status = LTTNG_TRIGGER_STATUS_INVALID; | |
602 | goto end; | |
603 | } | |
604 | ||
605 | *count = lttng_dynamic_pointer_array_get_count(&triggers->array); | |
606 | end: | |
607 | return status; | |
608 | } | |
609 | ||
610 | void lttng_triggers_destroy(struct lttng_triggers *triggers) | |
611 | { | |
612 | if (!triggers) { | |
613 | return; | |
614 | } | |
615 | ||
616 | lttng_dynamic_pointer_array_reset(&triggers->array); | |
617 | free(triggers); | |
618 | } | |
619 | ||
620 | int lttng_triggers_serialize(const struct lttng_triggers *triggers, | |
621 | struct lttng_payload *payload) | |
622 | { | |
623 | int ret; | |
624 | unsigned int i, count; | |
625 | size_t size_before_payload; | |
626 | struct lttng_triggers_comm triggers_comm = {}; | |
627 | struct lttng_triggers_comm *header; | |
628 | enum lttng_trigger_status status; | |
629 | const size_t header_offset = payload->buffer.size; | |
630 | ||
631 | status = lttng_triggers_get_count(triggers, &count); | |
632 | if (status != LTTNG_TRIGGER_STATUS_OK) { | |
633 | ret = LTTNG_ERR_INVALID; | |
634 | goto end; | |
635 | } | |
636 | ||
637 | triggers_comm.count = count; | |
638 | ||
639 | /* Placeholder header; updated at the end. */ | |
640 | ret = lttng_dynamic_buffer_append(&payload->buffer, &triggers_comm, | |
641 | sizeof(triggers_comm)); | |
642 | if (ret) { | |
643 | goto end; | |
644 | } | |
645 | ||
646 | size_before_payload = payload->buffer.size; | |
647 | ||
648 | for (i = 0; i < count; i++) { | |
649 | const struct lttng_trigger *trigger = | |
650 | lttng_triggers_get_at_index(triggers, i); | |
651 | ||
652 | assert(trigger); | |
653 | ||
654 | ret = lttng_trigger_serialize(trigger, payload); | |
655 | if (ret) { | |
656 | goto end; | |
657 | } | |
658 | } | |
659 | ||
660 | /* Update payload size. */ | |
661 | header = (struct lttng_triggers_comm *) ((char *) payload->buffer.data + header_offset); | |
662 | header->length = payload->buffer.size - size_before_payload; | |
663 | end: | |
664 | return ret; | |
665 | } | |
666 | ||
667 | LTTNG_HIDDEN | |
668 | ssize_t lttng_triggers_create_from_payload( | |
669 | struct lttng_payload_view *src_view, | |
670 | struct lttng_triggers **triggers) | |
671 | { | |
672 | ssize_t ret, offset = 0, triggers_size = 0; | |
673 | unsigned int i; | |
674 | const struct lttng_triggers_comm *triggers_comm; | |
675 | struct lttng_triggers *local_triggers = NULL; | |
676 | ||
677 | if (!src_view || !triggers) { | |
678 | ret = -1; | |
679 | goto error; | |
680 | } | |
681 | ||
682 | /* lttng_trigger_comms header */ | |
683 | triggers_comm = (const struct lttng_triggers_comm *) src_view->buffer.data; | |
684 | offset += sizeof(*triggers_comm); | |
685 | ||
686 | local_triggers = lttng_triggers_create(); | |
687 | if (!local_triggers) { | |
688 | ret = -1; | |
689 | goto error; | |
690 | } | |
691 | ||
692 | for (i = 0; i < triggers_comm->count; i++) { | |
693 | struct lttng_trigger *trigger = NULL; | |
694 | struct lttng_payload_view trigger_view = | |
695 | lttng_payload_view_from_view(src_view, offset, -1); | |
696 | ssize_t trigger_size; | |
697 | ||
698 | trigger_size = lttng_trigger_create_from_payload( | |
699 | &trigger_view, &trigger); | |
700 | if (trigger_size < 0) { | |
701 | ret = trigger_size; | |
702 | goto error; | |
703 | } | |
704 | ||
705 | /* Transfer ownership of the trigger to the collection. */ | |
706 | ret = lttng_triggers_add(local_triggers, trigger); | |
707 | lttng_trigger_put(trigger); | |
708 | if (ret < 0) { | |
709 | ret = -1; | |
710 | goto error; | |
711 | } | |
712 | ||
713 | offset += trigger_size; | |
714 | triggers_size += trigger_size; | |
715 | } | |
716 | ||
717 | /* Unexpected size of inner-elements; the buffer is corrupted. */ | |
718 | if ((ssize_t) triggers_comm->length != triggers_size) { | |
719 | ret = -1; | |
720 | goto error; | |
721 | } | |
722 | ||
723 | /* Pass ownership to caller. */ | |
724 | *triggers = local_triggers; | |
725 | local_triggers = NULL; | |
726 | ||
727 | ret = offset; | |
728 | error: | |
729 | ||
730 | lttng_triggers_destroy(local_triggers); | |
731 | return ret; | |
732 | } | |
733 | ||
3da864a9 JR |
734 | LTTNG_HIDDEN |
735 | const struct lttng_credentials *lttng_trigger_get_credentials( | |
736 | const struct lttng_trigger *trigger) | |
737 | { | |
64eafdf6 | 738 | return &trigger->creds; |
3da864a9 JR |
739 | } |
740 | ||
741 | LTTNG_HIDDEN | |
64eafdf6 | 742 | void lttng_trigger_set_credentials(struct lttng_trigger *trigger, |
3da864a9 JR |
743 | const struct lttng_credentials *creds) |
744 | { | |
745 | assert(creds); | |
64eafdf6 JR |
746 | trigger->creds = *creds; |
747 | } | |
748 | ||
749 | enum lttng_trigger_status lttng_trigger_set_owner_uid( | |
750 | struct lttng_trigger *trigger, uid_t uid) | |
751 | { | |
752 | enum lttng_trigger_status ret = LTTNG_TRIGGER_STATUS_OK; | |
753 | const struct lttng_credentials creds = { | |
754 | .uid = LTTNG_OPTIONAL_INIT_VALUE(uid), | |
755 | .gid = LTTNG_OPTIONAL_INIT_UNSET, | |
756 | }; | |
757 | ||
758 | if (!trigger) { | |
759 | ret = LTTNG_TRIGGER_STATUS_INVALID; | |
760 | goto end; | |
761 | } | |
762 | ||
763 | /* Client-side validation only to report a clearer error. */ | |
764 | if (geteuid() != 0) { | |
765 | ret = LTTNG_TRIGGER_STATUS_PERMISSION_DENIED; | |
766 | goto end; | |
767 | } | |
768 | ||
769 | lttng_trigger_set_credentials(trigger, &creds); | |
770 | ||
771 | end: | |
772 | return ret; | |
773 | } | |
774 | ||
775 | enum lttng_trigger_status lttng_trigger_get_owner_uid( | |
776 | const struct lttng_trigger *trigger, uid_t *uid) | |
777 | { | |
778 | enum lttng_trigger_status ret = LTTNG_TRIGGER_STATUS_OK; | |
779 | const struct lttng_credentials *creds = NULL; | |
780 | ||
781 | if (!trigger || !uid ) { | |
782 | ret = LTTNG_TRIGGER_STATUS_INVALID; | |
783 | goto end; | |
784 | } | |
785 | ||
786 | if (!trigger->creds.uid.is_set ) { | |
787 | ret = LTTNG_TRIGGER_STATUS_UNSET; | |
788 | goto end; | |
789 | } | |
790 | ||
791 | creds = lttng_trigger_get_credentials(trigger); | |
792 | *uid = lttng_credentials_get_uid(creds); | |
793 | ||
794 | end: | |
795 | return ret; | |
3da864a9 | 796 | } |
5c504c41 JR |
797 | |
798 | enum lttng_trigger_status lttng_trigger_set_firing_policy( | |
799 | struct lttng_trigger *trigger, | |
800 | enum lttng_trigger_firing_policy policy_type, | |
801 | uint64_t threshold) | |
802 | { | |
803 | enum lttng_trigger_status ret = LTTNG_TRIGGER_STATUS_OK; | |
804 | assert(trigger); | |
805 | ||
806 | if (threshold < 1) { | |
807 | ret = LTTNG_TRIGGER_STATUS_INVALID; | |
808 | goto end; | |
809 | } | |
810 | ||
811 | trigger->firing_policy.type = policy_type; | |
812 | trigger->firing_policy.threshold = threshold; | |
813 | ||
814 | end: | |
815 | return ret; | |
816 | } | |
817 | ||
818 | enum lttng_trigger_status lttng_trigger_get_firing_policy( | |
819 | const struct lttng_trigger *trigger, | |
820 | enum lttng_trigger_firing_policy *policy_type, | |
821 | uint64_t *threshold) | |
822 | { | |
823 | enum lttng_trigger_status status = LTTNG_TRIGGER_STATUS_OK; | |
824 | ||
825 | if (!trigger || !policy_type || !threshold) { | |
826 | status = LTTNG_TRIGGER_STATUS_INVALID; | |
827 | goto end; | |
828 | } | |
829 | ||
830 | *policy_type = trigger->firing_policy.type; | |
831 | *threshold = trigger->firing_policy.threshold; | |
832 | ||
833 | end: | |
834 | return status; | |
835 | } | |
836 | ||
837 | LTTNG_HIDDEN | |
838 | bool lttng_trigger_should_fire(const struct lttng_trigger *trigger) | |
839 | { | |
840 | bool ready_to_fire = false; | |
841 | ||
842 | assert(trigger); | |
843 | ||
844 | switch (trigger->firing_policy.type) { | |
845 | case LTTNG_TRIGGER_FIRING_POLICY_EVERY_N: | |
846 | if (trigger->firing_policy.current_count < trigger->firing_policy.threshold) { | |
847 | ready_to_fire = true; | |
848 | } | |
849 | break; | |
850 | case LTTNG_TRIGGER_FIRING_POLICY_ONCE_AFTER_N: | |
851 | if (trigger->firing_policy.current_count < trigger->firing_policy.threshold) { | |
852 | ready_to_fire = true; | |
853 | } | |
854 | break; | |
855 | default: | |
856 | abort(); | |
857 | }; | |
858 | ||
859 | return ready_to_fire; | |
860 | } | |
861 | ||
862 | LTTNG_HIDDEN | |
863 | void lttng_trigger_fire(struct lttng_trigger *trigger) | |
864 | { | |
865 | assert(trigger); | |
866 | ||
867 | trigger->firing_policy.current_count++; | |
868 | ||
869 | switch (trigger->firing_policy.type) { | |
870 | case LTTNG_TRIGGER_FIRING_POLICY_EVERY_N: | |
871 | if (trigger->firing_policy.current_count == trigger->firing_policy.threshold) { | |
872 | trigger->firing_policy.current_count = 0; | |
873 | } | |
874 | ||
875 | break; | |
876 | case LTTNG_TRIGGER_FIRING_POLICY_ONCE_AFTER_N: | |
877 | /* | |
878 | * TODO: | |
879 | * As an optimisation, deactivate the trigger condition and | |
880 | * remove any checks in the traced application or kernel since | |
881 | * the trigger will never fire again. | |
882 | */ | |
883 | break; | |
884 | default: | |
885 | abort(); | |
886 | }; | |
887 | } | |
91c96f62 JR |
888 | |
889 | LTTNG_HIDDEN | |
890 | enum lttng_domain_type lttng_trigger_get_underlying_domain_type_restriction( | |
891 | const struct lttng_trigger *trigger) | |
892 | { | |
893 | enum lttng_domain_type type = LTTNG_DOMAIN_NONE; | |
894 | const struct lttng_event_rule *event_rule; | |
895 | enum lttng_condition_status c_status; | |
896 | enum lttng_condition_type c_type; | |
897 | ||
898 | assert(trigger); | |
899 | assert(trigger->condition); | |
900 | ||
901 | c_type = lttng_condition_get_type(trigger->condition); | |
902 | assert (c_type != LTTNG_CONDITION_TYPE_UNKNOWN); | |
903 | ||
904 | switch (c_type) { | |
905 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: | |
906 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: | |
907 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
908 | /* Apply to any domain. */ | |
909 | type = LTTNG_DOMAIN_NONE; | |
910 | break; | |
d602bd6a | 911 | case LTTNG_CONDITION_TYPE_ON_EVENT: |
91c96f62 | 912 | /* Return the domain of the event rule. */ |
d602bd6a | 913 | c_status = lttng_condition_on_event_get_rule( |
91c96f62 JR |
914 | trigger->condition, &event_rule); |
915 | assert(c_status == LTTNG_CONDITION_STATUS_OK); | |
916 | type = lttng_event_rule_get_domain_type(event_rule); | |
917 | break; | |
918 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
919 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
920 | /* Return the domain of the channel being monitored. */ | |
921 | c_status = lttng_condition_buffer_usage_get_domain_type( | |
922 | trigger->condition, &type); | |
923 | assert(c_status == LTTNG_CONDITION_STATUS_OK); | |
924 | break; | |
925 | default: | |
926 | abort(); | |
927 | } | |
928 | ||
929 | return type; | |
930 | } | |
58daac01 JR |
931 | |
932 | /* | |
933 | * Generate bytecode related to the trigger. | |
934 | * On success LTTNG_OK. On error, returns lttng_error code. | |
935 | */ | |
936 | LTTNG_HIDDEN | |
937 | enum lttng_error_code lttng_trigger_generate_bytecode( | |
938 | struct lttng_trigger *trigger, | |
939 | const struct lttng_credentials *creds) | |
940 | { | |
941 | enum lttng_error_code ret; | |
942 | struct lttng_condition *condition = NULL; | |
943 | ||
944 | condition = lttng_trigger_get_condition(trigger); | |
945 | if (!condition) { | |
946 | ret = LTTNG_ERR_INVALID_TRIGGER; | |
947 | goto end; | |
948 | } | |
949 | ||
950 | switch (lttng_condition_get_type(condition)) { | |
d602bd6a | 951 | case LTTNG_CONDITION_TYPE_ON_EVENT: |
58daac01 JR |
952 | { |
953 | struct lttng_event_rule *event_rule; | |
954 | const enum lttng_condition_status condition_status = | |
d602bd6a | 955 | lttng_condition_on_event_borrow_rule_mutable( |
58daac01 JR |
956 | condition, &event_rule); |
957 | ||
958 | assert(condition_status == LTTNG_CONDITION_STATUS_OK); | |
f2e97f59 JR |
959 | |
960 | /* Generate the filter bytecode. */ | |
58daac01 JR |
961 | ret = lttng_event_rule_generate_filter_bytecode( |
962 | event_rule, creds); | |
963 | if (ret != LTTNG_OK) { | |
964 | goto end; | |
965 | } | |
966 | ||
f2e97f59 | 967 | /* Generate the capture bytecode. */ |
d602bd6a | 968 | ret = lttng_condition_on_event_generate_capture_descriptor_bytecode( |
f2e97f59 JR |
969 | condition); |
970 | if (ret != LTTNG_OK) { | |
971 | goto end; | |
972 | } | |
973 | ||
58daac01 JR |
974 | ret = LTTNG_OK; |
975 | break; | |
976 | } | |
977 | default: | |
978 | ret = LTTNG_OK; | |
979 | break; | |
980 | } | |
981 | end: | |
982 | return ret; | |
983 | } | |
b61776fb SM |
984 | |
985 | LTTNG_HIDDEN | |
986 | struct lttng_trigger *lttng_trigger_copy(const struct lttng_trigger *trigger) | |
987 | { | |
988 | int ret; | |
989 | struct lttng_payload copy_buffer; | |
990 | struct lttng_trigger *copy = NULL; | |
991 | ||
992 | lttng_payload_init(©_buffer); | |
993 | ||
994 | ret = lttng_trigger_serialize(trigger, ©_buffer); | |
995 | if (ret < 0) { | |
996 | goto end; | |
997 | } | |
998 | ||
999 | { | |
1000 | struct lttng_payload_view view = | |
1001 | lttng_payload_view_from_payload( | |
1002 | ©_buffer, 0, -1); | |
1003 | ret = lttng_trigger_create_from_payload( | |
1004 | &view, ©); | |
1005 | if (ret < 0) { | |
1006 | copy = NULL; | |
1007 | goto end; | |
1008 | } | |
1009 | } | |
1010 | ||
1011 | end: | |
1012 | lttng_payload_reset(©_buffer); | |
1013 | return copy; | |
1014 | } | |
c738df17 FD |
1015 | |
1016 | LTTNG_HIDDEN | |
1017 | bool lttng_trigger_needs_tracer_notifier(const struct lttng_trigger *trigger) | |
1018 | { | |
1019 | bool needs_tracer_notifier = false; | |
1020 | const struct lttng_condition *condition = | |
1021 | lttng_trigger_get_const_condition(trigger); | |
1022 | ||
1023 | switch (lttng_condition_get_type(condition)) { | |
1024 | case LTTNG_CONDITION_TYPE_ON_EVENT: | |
1025 | needs_tracer_notifier = true; | |
1026 | goto end; | |
1027 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: | |
1028 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
1029 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
1030 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: | |
1031 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
1032 | goto end; | |
1033 | case LTTNG_CONDITION_TYPE_UNKNOWN: | |
1034 | default: | |
1035 | abort(); | |
1036 | } | |
1037 | end: | |
1038 | return needs_tracer_notifier; | |
1039 | } |