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> | |
670a26e4 JR |
19 | #include <lttng/condition/event-rule-matches-internal.h> |
20 | #include <lttng/condition/event-rule-matches.h> | |
9c374932 JR |
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 | { | |
0efb2ad7 JG |
349 | if (!!a->name != !!b->name) { |
350 | /* Both must be either anonymous or named. */ | |
351 | return false; | |
352 | } | |
353 | ||
354 | if (a->name && strcmp(a->name, b->name) != 0) { | |
1ca78886 FD |
355 | return false; |
356 | } | |
357 | ||
85c06c44 JR |
358 | if (!lttng_condition_is_equal(a->condition, b->condition)) { |
359 | return false; | |
360 | } | |
361 | ||
362 | if (!lttng_action_is_equal(a->action, b->action)) { | |
363 | return false; | |
364 | } | |
365 | ||
366 | if (!lttng_credentials_is_equal(lttng_trigger_get_credentials(a), | |
367 | lttng_trigger_get_credentials(b))) { | |
368 | return false; | |
369 | } | |
370 | ||
371 | return true; | |
372 | } | |
373 | ||
a5c2d2a7 | 374 | LTTNG_HIDDEN |
242388e4 JR |
375 | enum lttng_trigger_status lttng_trigger_set_name(struct lttng_trigger *trigger, |
376 | const char* name) | |
377 | { | |
378 | char *name_copy = NULL; | |
379 | enum lttng_trigger_status status = LTTNG_TRIGGER_STATUS_OK; | |
380 | ||
a5c2d2a7 | 381 | if (!trigger) { |
242388e4 JR |
382 | status = LTTNG_TRIGGER_STATUS_INVALID; |
383 | goto end; | |
384 | } | |
385 | ||
a5c2d2a7 JG |
386 | if (name) { |
387 | name_copy = strdup(name); | |
388 | if (!name_copy) { | |
389 | status = LTTNG_TRIGGER_STATUS_ERROR; | |
390 | goto end; | |
391 | } | |
242388e4 JR |
392 | } |
393 | ||
394 | free(trigger->name); | |
395 | ||
396 | trigger->name = name_copy; | |
397 | name_copy = NULL; | |
398 | end: | |
399 | return status; | |
400 | } | |
401 | ||
402 | enum lttng_trigger_status lttng_trigger_get_name( | |
403 | const struct lttng_trigger *trigger, const char **name) | |
404 | { | |
405 | enum lttng_trigger_status status = LTTNG_TRIGGER_STATUS_OK; | |
406 | ||
407 | if (!trigger || !name) { | |
408 | status = LTTNG_TRIGGER_STATUS_INVALID; | |
409 | goto end; | |
410 | } | |
411 | ||
412 | if (!trigger->name) { | |
413 | status = LTTNG_TRIGGER_STATUS_UNSET; | |
414 | } | |
415 | ||
416 | *name = trigger->name; | |
417 | end: | |
418 | return status; | |
419 | } | |
420 | ||
421 | LTTNG_HIDDEN | |
422 | int lttng_trigger_assign_name(struct lttng_trigger *dst, | |
423 | const struct lttng_trigger *src) | |
424 | { | |
425 | int ret = 0; | |
426 | enum lttng_trigger_status status; | |
427 | ||
428 | status = lttng_trigger_set_name(dst, src->name); | |
429 | if (status != LTTNG_TRIGGER_STATUS_OK) { | |
430 | ret = -1; | |
431 | ERR("Failed to set name for trigger"); | |
432 | goto end; | |
433 | } | |
434 | end: | |
435 | return ret; | |
436 | } | |
437 | ||
e6887944 JR |
438 | LTTNG_HIDDEN |
439 | void lttng_trigger_set_tracer_token(struct lttng_trigger *trigger, | |
440 | uint64_t token) | |
441 | { | |
442 | assert(trigger); | |
443 | LTTNG_OPTIONAL_SET(&trigger->tracer_token, token); | |
444 | } | |
445 | ||
446 | LTTNG_HIDDEN | |
447 | uint64_t lttng_trigger_get_tracer_token(const struct lttng_trigger *trigger) | |
448 | { | |
449 | assert(trigger); | |
450 | ||
451 | return LTTNG_OPTIONAL_GET(trigger->tracer_token); | |
452 | } | |
453 | ||
242388e4 JR |
454 | LTTNG_HIDDEN |
455 | int lttng_trigger_generate_name(struct lttng_trigger *trigger, | |
456 | uint64_t unique_id) | |
457 | { | |
458 | int ret = 0; | |
459 | char *generated_name = NULL; | |
460 | ||
461 | ret = asprintf(&generated_name, "T%" PRIu64 "", unique_id); | |
462 | if (ret < 0) { | |
463 | ERR("Failed to generate trigger name"); | |
464 | ret = -1; | |
465 | goto end; | |
466 | } | |
467 | ||
468 | ret = 0; | |
469 | free(trigger->name); | |
470 | trigger->name = generated_name; | |
471 | end: | |
472 | return ret; | |
473 | } | |
474 | ||
f01d28b4 JR |
475 | LTTNG_HIDDEN |
476 | void lttng_trigger_get(struct lttng_trigger *trigger) | |
477 | { | |
478 | urcu_ref_get(&trigger->ref); | |
479 | } | |
480 | ||
481 | LTTNG_HIDDEN | |
482 | void lttng_trigger_put(struct lttng_trigger *trigger) | |
483 | { | |
484 | if (!trigger) { | |
485 | return; | |
486 | } | |
487 | ||
488 | urcu_ref_put(&trigger->ref , trigger_destroy_ref); | |
489 | } | |
490 | ||
a02903c0 JR |
491 | static void delete_trigger_array_element(void *ptr) |
492 | { | |
493 | struct lttng_trigger *trigger = ptr; | |
494 | ||
495 | lttng_trigger_put(trigger); | |
496 | } | |
497 | ||
498 | LTTNG_HIDDEN | |
499 | struct lttng_triggers *lttng_triggers_create(void) | |
500 | { | |
501 | struct lttng_triggers *triggers = NULL; | |
502 | ||
503 | triggers = zmalloc(sizeof(*triggers)); | |
504 | if (!triggers) { | |
505 | goto end; | |
506 | } | |
507 | ||
508 | lttng_dynamic_pointer_array_init(&triggers->array, delete_trigger_array_element); | |
509 | ||
510 | end: | |
511 | return triggers; | |
512 | } | |
513 | ||
514 | LTTNG_HIDDEN | |
515 | struct lttng_trigger *lttng_triggers_borrow_mutable_at_index( | |
516 | const struct lttng_triggers *triggers, unsigned int index) | |
517 | { | |
518 | struct lttng_trigger *trigger = NULL; | |
519 | ||
520 | assert(triggers); | |
521 | if (index >= lttng_dynamic_pointer_array_get_count(&triggers->array)) { | |
522 | goto end; | |
523 | } | |
524 | ||
525 | trigger = (struct lttng_trigger *) | |
526 | lttng_dynamic_pointer_array_get_pointer( | |
527 | &triggers->array, index); | |
528 | end: | |
529 | return trigger; | |
530 | } | |
531 | ||
532 | LTTNG_HIDDEN | |
533 | int lttng_triggers_add( | |
534 | struct lttng_triggers *triggers, struct lttng_trigger *trigger) | |
535 | { | |
536 | int ret; | |
537 | ||
538 | assert(triggers); | |
539 | assert(trigger); | |
540 | ||
541 | lttng_trigger_get(trigger); | |
542 | ||
543 | ret = lttng_dynamic_pointer_array_add_pointer(&triggers->array, trigger); | |
544 | if (ret) { | |
545 | lttng_trigger_put(trigger); | |
546 | } | |
547 | ||
548 | return ret; | |
549 | } | |
550 | ||
551 | const struct lttng_trigger *lttng_triggers_get_at_index( | |
552 | const struct lttng_triggers *triggers, unsigned int index) | |
553 | { | |
554 | return lttng_triggers_borrow_mutable_at_index(triggers, index); | |
555 | } | |
556 | ||
557 | enum lttng_trigger_status lttng_triggers_get_count(const struct lttng_triggers *triggers, unsigned int *count) | |
558 | { | |
559 | enum lttng_trigger_status status = LTTNG_TRIGGER_STATUS_OK; | |
560 | ||
561 | if (!triggers || !count) { | |
562 | status = LTTNG_TRIGGER_STATUS_INVALID; | |
563 | goto end; | |
564 | } | |
565 | ||
566 | *count = lttng_dynamic_pointer_array_get_count(&triggers->array); | |
567 | end: | |
568 | return status; | |
569 | } | |
570 | ||
571 | void lttng_triggers_destroy(struct lttng_triggers *triggers) | |
572 | { | |
573 | if (!triggers) { | |
574 | return; | |
575 | } | |
576 | ||
577 | lttng_dynamic_pointer_array_reset(&triggers->array); | |
578 | free(triggers); | |
579 | } | |
580 | ||
581 | int lttng_triggers_serialize(const struct lttng_triggers *triggers, | |
582 | struct lttng_payload *payload) | |
583 | { | |
584 | int ret; | |
585 | unsigned int i, count; | |
586 | size_t size_before_payload; | |
587 | struct lttng_triggers_comm triggers_comm = {}; | |
588 | struct lttng_triggers_comm *header; | |
589 | enum lttng_trigger_status status; | |
590 | const size_t header_offset = payload->buffer.size; | |
591 | ||
592 | status = lttng_triggers_get_count(triggers, &count); | |
593 | if (status != LTTNG_TRIGGER_STATUS_OK) { | |
594 | ret = LTTNG_ERR_INVALID; | |
595 | goto end; | |
596 | } | |
597 | ||
598 | triggers_comm.count = count; | |
599 | ||
600 | /* Placeholder header; updated at the end. */ | |
601 | ret = lttng_dynamic_buffer_append(&payload->buffer, &triggers_comm, | |
602 | sizeof(triggers_comm)); | |
603 | if (ret) { | |
604 | goto end; | |
605 | } | |
606 | ||
607 | size_before_payload = payload->buffer.size; | |
608 | ||
609 | for (i = 0; i < count; i++) { | |
610 | const struct lttng_trigger *trigger = | |
611 | lttng_triggers_get_at_index(triggers, i); | |
612 | ||
613 | assert(trigger); | |
614 | ||
615 | ret = lttng_trigger_serialize(trigger, payload); | |
616 | if (ret) { | |
617 | goto end; | |
618 | } | |
619 | } | |
620 | ||
621 | /* Update payload size. */ | |
622 | header = (struct lttng_triggers_comm *) ((char *) payload->buffer.data + header_offset); | |
623 | header->length = payload->buffer.size - size_before_payload; | |
624 | end: | |
625 | return ret; | |
626 | } | |
627 | ||
628 | LTTNG_HIDDEN | |
629 | ssize_t lttng_triggers_create_from_payload( | |
630 | struct lttng_payload_view *src_view, | |
631 | struct lttng_triggers **triggers) | |
632 | { | |
633 | ssize_t ret, offset = 0, triggers_size = 0; | |
634 | unsigned int i; | |
635 | const struct lttng_triggers_comm *triggers_comm; | |
636 | struct lttng_triggers *local_triggers = NULL; | |
637 | ||
638 | if (!src_view || !triggers) { | |
639 | ret = -1; | |
640 | goto error; | |
641 | } | |
642 | ||
643 | /* lttng_trigger_comms header */ | |
644 | triggers_comm = (const struct lttng_triggers_comm *) src_view->buffer.data; | |
645 | offset += sizeof(*triggers_comm); | |
646 | ||
647 | local_triggers = lttng_triggers_create(); | |
648 | if (!local_triggers) { | |
649 | ret = -1; | |
650 | goto error; | |
651 | } | |
652 | ||
653 | for (i = 0; i < triggers_comm->count; i++) { | |
654 | struct lttng_trigger *trigger = NULL; | |
655 | struct lttng_payload_view trigger_view = | |
656 | lttng_payload_view_from_view(src_view, offset, -1); | |
657 | ssize_t trigger_size; | |
658 | ||
659 | trigger_size = lttng_trigger_create_from_payload( | |
660 | &trigger_view, &trigger); | |
661 | if (trigger_size < 0) { | |
662 | ret = trigger_size; | |
663 | goto error; | |
664 | } | |
665 | ||
666 | /* Transfer ownership of the trigger to the collection. */ | |
667 | ret = lttng_triggers_add(local_triggers, trigger); | |
668 | lttng_trigger_put(trigger); | |
669 | if (ret < 0) { | |
670 | ret = -1; | |
671 | goto error; | |
672 | } | |
673 | ||
674 | offset += trigger_size; | |
675 | triggers_size += trigger_size; | |
676 | } | |
677 | ||
678 | /* Unexpected size of inner-elements; the buffer is corrupted. */ | |
679 | if ((ssize_t) triggers_comm->length != triggers_size) { | |
680 | ret = -1; | |
681 | goto error; | |
682 | } | |
683 | ||
684 | /* Pass ownership to caller. */ | |
685 | *triggers = local_triggers; | |
686 | local_triggers = NULL; | |
687 | ||
688 | ret = offset; | |
689 | error: | |
690 | ||
691 | lttng_triggers_destroy(local_triggers); | |
692 | return ret; | |
693 | } | |
694 | ||
3da864a9 JR |
695 | LTTNG_HIDDEN |
696 | const struct lttng_credentials *lttng_trigger_get_credentials( | |
697 | const struct lttng_trigger *trigger) | |
698 | { | |
64eafdf6 | 699 | return &trigger->creds; |
3da864a9 JR |
700 | } |
701 | ||
702 | LTTNG_HIDDEN | |
64eafdf6 | 703 | void lttng_trigger_set_credentials(struct lttng_trigger *trigger, |
3da864a9 JR |
704 | const struct lttng_credentials *creds) |
705 | { | |
706 | assert(creds); | |
64eafdf6 JR |
707 | trigger->creds = *creds; |
708 | } | |
709 | ||
710 | enum lttng_trigger_status lttng_trigger_set_owner_uid( | |
711 | struct lttng_trigger *trigger, uid_t uid) | |
712 | { | |
713 | enum lttng_trigger_status ret = LTTNG_TRIGGER_STATUS_OK; | |
d84c8ae3 | 714 | const uid_t euid = geteuid(); |
64eafdf6 JR |
715 | const struct lttng_credentials creds = { |
716 | .uid = LTTNG_OPTIONAL_INIT_VALUE(uid), | |
717 | .gid = LTTNG_OPTIONAL_INIT_UNSET, | |
718 | }; | |
719 | ||
720 | if (!trigger) { | |
721 | ret = LTTNG_TRIGGER_STATUS_INVALID; | |
722 | goto end; | |
723 | } | |
724 | ||
725 | /* Client-side validation only to report a clearer error. */ | |
d84c8ae3 | 726 | if (euid != 0 && euid != uid) { |
64eafdf6 JR |
727 | ret = LTTNG_TRIGGER_STATUS_PERMISSION_DENIED; |
728 | goto end; | |
729 | } | |
730 | ||
731 | lttng_trigger_set_credentials(trigger, &creds); | |
732 | ||
733 | end: | |
734 | return ret; | |
735 | } | |
736 | ||
737 | enum lttng_trigger_status lttng_trigger_get_owner_uid( | |
738 | const struct lttng_trigger *trigger, uid_t *uid) | |
739 | { | |
740 | enum lttng_trigger_status ret = LTTNG_TRIGGER_STATUS_OK; | |
741 | const struct lttng_credentials *creds = NULL; | |
742 | ||
743 | if (!trigger || !uid ) { | |
744 | ret = LTTNG_TRIGGER_STATUS_INVALID; | |
745 | goto end; | |
746 | } | |
747 | ||
748 | if (!trigger->creds.uid.is_set ) { | |
749 | ret = LTTNG_TRIGGER_STATUS_UNSET; | |
750 | goto end; | |
751 | } | |
752 | ||
753 | creds = lttng_trigger_get_credentials(trigger); | |
754 | *uid = lttng_credentials_get_uid(creds); | |
755 | ||
756 | end: | |
757 | return ret; | |
3da864a9 | 758 | } |
5c504c41 | 759 | |
91c96f62 JR |
760 | LTTNG_HIDDEN |
761 | enum lttng_domain_type lttng_trigger_get_underlying_domain_type_restriction( | |
762 | const struct lttng_trigger *trigger) | |
763 | { | |
764 | enum lttng_domain_type type = LTTNG_DOMAIN_NONE; | |
765 | const struct lttng_event_rule *event_rule; | |
766 | enum lttng_condition_status c_status; | |
767 | enum lttng_condition_type c_type; | |
768 | ||
769 | assert(trigger); | |
770 | assert(trigger->condition); | |
771 | ||
772 | c_type = lttng_condition_get_type(trigger->condition); | |
773 | assert (c_type != LTTNG_CONDITION_TYPE_UNKNOWN); | |
774 | ||
775 | switch (c_type) { | |
776 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: | |
777 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: | |
778 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
779 | /* Apply to any domain. */ | |
780 | type = LTTNG_DOMAIN_NONE; | |
781 | break; | |
8dbb86b8 | 782 | case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES: |
91c96f62 | 783 | /* Return the domain of the event rule. */ |
8dbb86b8 | 784 | c_status = lttng_condition_event_rule_matches_get_rule( |
91c96f62 JR |
785 | trigger->condition, &event_rule); |
786 | assert(c_status == LTTNG_CONDITION_STATUS_OK); | |
787 | type = lttng_event_rule_get_domain_type(event_rule); | |
788 | break; | |
789 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
790 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
791 | /* Return the domain of the channel being monitored. */ | |
792 | c_status = lttng_condition_buffer_usage_get_domain_type( | |
793 | trigger->condition, &type); | |
794 | assert(c_status == LTTNG_CONDITION_STATUS_OK); | |
795 | break; | |
796 | default: | |
797 | abort(); | |
798 | } | |
799 | ||
800 | return type; | |
801 | } | |
58daac01 JR |
802 | |
803 | /* | |
804 | * Generate bytecode related to the trigger. | |
805 | * On success LTTNG_OK. On error, returns lttng_error code. | |
806 | */ | |
807 | LTTNG_HIDDEN | |
808 | enum lttng_error_code lttng_trigger_generate_bytecode( | |
809 | struct lttng_trigger *trigger, | |
810 | const struct lttng_credentials *creds) | |
811 | { | |
812 | enum lttng_error_code ret; | |
813 | struct lttng_condition *condition = NULL; | |
814 | ||
815 | condition = lttng_trigger_get_condition(trigger); | |
816 | if (!condition) { | |
817 | ret = LTTNG_ERR_INVALID_TRIGGER; | |
818 | goto end; | |
819 | } | |
820 | ||
821 | switch (lttng_condition_get_type(condition)) { | |
8dbb86b8 | 822 | case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES: |
58daac01 JR |
823 | { |
824 | struct lttng_event_rule *event_rule; | |
825 | const enum lttng_condition_status condition_status = | |
8dbb86b8 JR |
826 | lttng_condition_event_rule_matches_borrow_rule_mutable( |
827 | condition, &event_rule); | |
58daac01 JR |
828 | |
829 | assert(condition_status == LTTNG_CONDITION_STATUS_OK); | |
f2e97f59 JR |
830 | |
831 | /* Generate the filter bytecode. */ | |
58daac01 JR |
832 | ret = lttng_event_rule_generate_filter_bytecode( |
833 | event_rule, creds); | |
834 | if (ret != LTTNG_OK) { | |
835 | goto end; | |
836 | } | |
837 | ||
f2e97f59 | 838 | /* Generate the capture bytecode. */ |
8dbb86b8 | 839 | ret = lttng_condition_event_rule_matches_generate_capture_descriptor_bytecode( |
f2e97f59 JR |
840 | condition); |
841 | if (ret != LTTNG_OK) { | |
842 | goto end; | |
843 | } | |
844 | ||
58daac01 JR |
845 | ret = LTTNG_OK; |
846 | break; | |
847 | } | |
848 | default: | |
849 | ret = LTTNG_OK; | |
850 | break; | |
851 | } | |
852 | end: | |
853 | return ret; | |
854 | } | |
b61776fb SM |
855 | |
856 | LTTNG_HIDDEN | |
857 | struct lttng_trigger *lttng_trigger_copy(const struct lttng_trigger *trigger) | |
858 | { | |
859 | int ret; | |
860 | struct lttng_payload copy_buffer; | |
94dbd8e4 JG |
861 | struct lttng_condition *condition_copy = NULL; |
862 | struct lttng_action *action_copy = NULL; | |
b61776fb | 863 | struct lttng_trigger *copy = NULL; |
94dbd8e4 JG |
864 | enum lttng_trigger_status trigger_status; |
865 | const char *trigger_name; | |
866 | uid_t trigger_owner_uid; | |
b61776fb SM |
867 | |
868 | lttng_payload_init(©_buffer); | |
869 | ||
94dbd8e4 | 870 | ret = lttng_condition_serialize(trigger->condition, ©_buffer); |
b61776fb SM |
871 | if (ret < 0) { |
872 | goto end; | |
873 | } | |
874 | ||
875 | { | |
876 | struct lttng_payload_view view = | |
877 | lttng_payload_view_from_payload( | |
878 | ©_buffer, 0, -1); | |
94dbd8e4 JG |
879 | |
880 | ret = lttng_condition_create_from_payload( | |
881 | &view, &condition_copy); | |
882 | if (ret < 0) { | |
883 | goto end; | |
884 | } | |
885 | } | |
886 | ||
887 | lttng_payload_clear(©_buffer); | |
888 | ||
889 | ret = lttng_action_serialize(trigger->action, ©_buffer); | |
890 | if (ret < 0) { | |
891 | goto end; | |
892 | } | |
893 | ||
894 | { | |
895 | struct lttng_payload_view view = | |
896 | lttng_payload_view_from_payload( | |
897 | ©_buffer, 0, -1); | |
898 | ||
899 | ret = lttng_action_create_from_payload( | |
900 | &view, &action_copy); | |
b61776fb | 901 | if (ret < 0) { |
b61776fb SM |
902 | goto end; |
903 | } | |
904 | } | |
905 | ||
94dbd8e4 JG |
906 | copy = lttng_trigger_create(condition_copy, action_copy); |
907 | if (!copy) { | |
908 | ERR("Failed to allocate trigger during trigger copy"); | |
909 | goto end; | |
910 | } | |
911 | ||
912 | trigger_status = lttng_trigger_get_name(trigger, &trigger_name); | |
913 | switch (trigger_status) { | |
914 | case LTTNG_TRIGGER_STATUS_OK: | |
915 | trigger_status = lttng_trigger_set_name(copy, trigger_name); | |
916 | if (trigger_status != LTTNG_TRIGGER_STATUS_OK) { | |
917 | ERR("Failed to set name of new trigger during copy"); | |
918 | goto error_cleanup_trigger; | |
919 | } | |
920 | break; | |
921 | case LTTNG_TRIGGER_STATUS_UNSET: | |
922 | break; | |
923 | default: | |
924 | ERR("Failed to get name of original trigger during copy"); | |
925 | goto error_cleanup_trigger; | |
926 | } | |
927 | ||
928 | trigger_status = lttng_trigger_get_owner_uid( | |
929 | trigger, &trigger_owner_uid); | |
930 | switch (trigger_status) { | |
931 | case LTTNG_TRIGGER_STATUS_OK: | |
932 | LTTNG_OPTIONAL_SET(©->creds.uid, trigger_owner_uid); | |
933 | break; | |
934 | case LTTNG_TRIGGER_STATUS_UNSET: | |
935 | break; | |
936 | default: | |
937 | ERR("Failed to get owner uid of original trigger during copy"); | |
938 | goto error_cleanup_trigger; | |
939 | } | |
940 | ||
941 | copy->tracer_token = trigger->tracer_token; | |
942 | copy->registered = trigger->registered; | |
943 | goto end; | |
944 | ||
945 | error_cleanup_trigger: | |
946 | lttng_trigger_destroy(copy); | |
947 | copy = NULL; | |
b61776fb | 948 | end: |
94dbd8e4 JG |
949 | lttng_condition_put(condition_copy); |
950 | lttng_action_put(action_copy); | |
b61776fb SM |
951 | lttng_payload_reset(©_buffer); |
952 | return copy; | |
953 | } | |
c738df17 FD |
954 | |
955 | LTTNG_HIDDEN | |
956 | bool lttng_trigger_needs_tracer_notifier(const struct lttng_trigger *trigger) | |
957 | { | |
958 | bool needs_tracer_notifier = false; | |
959 | const struct lttng_condition *condition = | |
960 | lttng_trigger_get_const_condition(trigger); | |
961 | ||
962 | switch (lttng_condition_get_type(condition)) { | |
8dbb86b8 | 963 | case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES: |
c738df17 FD |
964 | needs_tracer_notifier = true; |
965 | goto end; | |
966 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: | |
967 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
968 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
969 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: | |
970 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
971 | goto end; | |
972 | case LTTNG_CONDITION_TYPE_UNKNOWN: | |
973 | default: | |
974 | abort(); | |
975 | } | |
976 | end: | |
977 | return needs_tracer_notifier; | |
978 | } | |
9c374932 JR |
979 | |
980 | LTTNG_HIDDEN | |
981 | void lttng_trigger_set_as_registered(struct lttng_trigger *trigger) | |
982 | { | |
983 | pthread_mutex_lock(&trigger->lock); | |
984 | trigger->registered = true; | |
985 | pthread_mutex_unlock(&trigger->lock); | |
986 | } | |
987 | ||
988 | LTTNG_HIDDEN | |
989 | void lttng_trigger_set_as_unregistered(struct lttng_trigger *trigger) | |
990 | { | |
991 | pthread_mutex_lock(&trigger->lock); | |
992 | trigger->registered = false; | |
993 | pthread_mutex_unlock(&trigger->lock); | |
994 | } | |
995 | ||
996 | /* | |
997 | * The trigger must be locked before calling lttng_trigger_registered. | |
998 | * The lock is necessary since a trigger can be unregistered at anytime. | |
999 | * Manipulations requiring that the trigger be registered must always acquire | |
1000 | * the trigger lock for the duration of the manipulation using | |
1001 | * `lttng_trigger_lock` and `lttng_trigger_unlock`. | |
1002 | */ | |
1003 | LTTNG_HIDDEN | |
1004 | bool lttng_trigger_is_registered(struct lttng_trigger *trigger) | |
1005 | { | |
1006 | ASSERT_LOCKED(trigger->lock); | |
1007 | return trigger->registered; | |
1008 | } | |
1009 | ||
1010 | LTTNG_HIDDEN | |
1011 | void lttng_trigger_lock(struct lttng_trigger *trigger) | |
1012 | { | |
1013 | pthread_mutex_lock(&trigger->lock); | |
1014 | } | |
1015 | ||
1016 | LTTNG_HIDDEN | |
1017 | void lttng_trigger_unlock(struct lttng_trigger *trigger) | |
1018 | { | |
1019 | pthread_mutex_unlock(&trigger->lock); | |
1020 | } |