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