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