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