| 1 | /* |
| 2 | * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: LGPL-2.1-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <assert.h> |
| 9 | #include <common/credentials.h> |
| 10 | #include <common/dynamic-array.h> |
| 11 | #include <common/error.h> |
| 12 | #include <common/optional.h> |
| 13 | #include <common/payload-view.h> |
| 14 | #include <common/payload.h> |
| 15 | #include <inttypes.h> |
| 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/event-rule-matches-internal.h> |
| 20 | #include <lttng/condition/event-rule-matches.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> |
| 26 | |
| 27 | LTTNG_HIDDEN |
| 28 | bool lttng_trigger_validate(const struct lttng_trigger *trigger) |
| 29 | { |
| 30 | bool valid; |
| 31 | |
| 32 | if (!trigger) { |
| 33 | valid = false; |
| 34 | goto end; |
| 35 | } |
| 36 | |
| 37 | if (!trigger->creds.uid.is_set) { |
| 38 | valid = false; |
| 39 | goto end; |
| 40 | } |
| 41 | |
| 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 | |
| 63 | urcu_ref_init(&trigger->ref); |
| 64 | |
| 65 | lttng_condition_get(condition); |
| 66 | trigger->condition = condition; |
| 67 | |
| 68 | lttng_action_get(action); |
| 69 | trigger->action = action; |
| 70 | |
| 71 | pthread_mutex_init(&trigger->lock, NULL); |
| 72 | trigger->registered = false; |
| 73 | |
| 74 | end: |
| 75 | return trigger; |
| 76 | } |
| 77 | |
| 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 | */ |
| 83 | struct lttng_condition *lttng_trigger_get_condition( |
| 84 | struct lttng_trigger *trigger) |
| 85 | { |
| 86 | return trigger ? trigger->condition : NULL; |
| 87 | } |
| 88 | |
| 89 | const struct lttng_condition *lttng_trigger_get_const_condition( |
| 90 | const struct lttng_trigger *trigger) |
| 91 | { |
| 92 | return trigger ? trigger->condition : NULL; |
| 93 | } |
| 94 | |
| 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 | */ |
| 100 | struct lttng_action *lttng_trigger_get_action( |
| 101 | struct lttng_trigger *trigger) |
| 102 | { |
| 103 | return trigger ? trigger->action : NULL; |
| 104 | } |
| 105 | |
| 106 | const struct lttng_action *lttng_trigger_get_const_action( |
| 107 | const struct lttng_trigger *trigger) |
| 108 | { |
| 109 | return trigger ? trigger->action : NULL; |
| 110 | } |
| 111 | |
| 112 | static void trigger_destroy_ref(struct urcu_ref *ref) |
| 113 | { |
| 114 | struct lttng_trigger *trigger = |
| 115 | container_of(ref, struct lttng_trigger, ref); |
| 116 | struct lttng_action *action = lttng_trigger_get_action(trigger); |
| 117 | struct lttng_condition *condition = |
| 118 | lttng_trigger_get_condition(trigger); |
| 119 | |
| 120 | assert(action); |
| 121 | assert(condition); |
| 122 | |
| 123 | /* Release ownership. */ |
| 124 | lttng_action_put(action); |
| 125 | lttng_condition_put(condition); |
| 126 | |
| 127 | pthread_mutex_destroy(&trigger->lock); |
| 128 | |
| 129 | free(trigger->name); |
| 130 | free(trigger); |
| 131 | } |
| 132 | |
| 133 | void lttng_trigger_destroy(struct lttng_trigger *trigger) |
| 134 | { |
| 135 | lttng_trigger_put(trigger); |
| 136 | } |
| 137 | |
| 138 | LTTNG_HIDDEN |
| 139 | ssize_t lttng_trigger_create_from_payload( |
| 140 | struct lttng_payload_view *src_view, |
| 141 | struct lttng_trigger **_trigger) |
| 142 | { |
| 143 | ssize_t ret, offset = 0, condition_size, action_size, name_size = 0; |
| 144 | struct lttng_condition *condition = NULL; |
| 145 | struct lttng_action *action = NULL; |
| 146 | const struct lttng_trigger_comm *trigger_comm; |
| 147 | const char *name = NULL; |
| 148 | struct lttng_credentials creds = { |
| 149 | .uid = LTTNG_OPTIONAL_INIT_UNSET, |
| 150 | .gid = LTTNG_OPTIONAL_INIT_UNSET, |
| 151 | }; |
| 152 | struct lttng_trigger *trigger = NULL; |
| 153 | const struct lttng_payload_view trigger_comm_view = |
| 154 | lttng_payload_view_from_view( |
| 155 | src_view, 0, sizeof(*trigger_comm)); |
| 156 | |
| 157 | if (!src_view || !_trigger) { |
| 158 | ret = -1; |
| 159 | goto end; |
| 160 | } |
| 161 | |
| 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 | |
| 168 | /* lttng_trigger_comm header */ |
| 169 | trigger_comm = (typeof(trigger_comm)) trigger_comm_view.buffer.data; |
| 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 | |
| 180 | offset += sizeof(*trigger_comm); |
| 181 | |
| 182 | if (trigger_comm->name_length != 0) { |
| 183 | /* Name. */ |
| 184 | const struct lttng_payload_view name_view = |
| 185 | lttng_payload_view_from_view( |
| 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 | } |
| 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 | |
| 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 | } |
| 214 | |
| 215 | if (condition_size < 0) { |
| 216 | ret = condition_size; |
| 217 | goto end; |
| 218 | } |
| 219 | |
| 220 | offset += condition_size; |
| 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 | } |
| 229 | |
| 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. */ |
| 237 | if ((ssize_t) trigger_comm->length != condition_size + action_size + name_size) { |
| 238 | ret = -1; |
| 239 | goto error; |
| 240 | } |
| 241 | |
| 242 | trigger = lttng_trigger_create(condition, action); |
| 243 | if (!trigger) { |
| 244 | ret = -1; |
| 245 | goto error; |
| 246 | } |
| 247 | |
| 248 | lttng_trigger_set_credentials(trigger, &creds); |
| 249 | |
| 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 | |
| 260 | if (name) { |
| 261 | const enum lttng_trigger_status status = |
| 262 | lttng_trigger_set_name(trigger, name); |
| 263 | |
| 264 | if (status != LTTNG_TRIGGER_STATUS_OK) { |
| 265 | ret = -1; |
| 266 | goto end; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | ret = offset; |
| 271 | |
| 272 | error: |
| 273 | lttng_condition_put(condition); |
| 274 | lttng_action_put(action); |
| 275 | end: |
| 276 | if (ret >= 0) { |
| 277 | *_trigger = trigger; |
| 278 | } else { |
| 279 | lttng_trigger_put(trigger); |
| 280 | } |
| 281 | |
| 282 | return ret; |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | * Both elements are stored contiguously, see their "*_comm" structure |
| 287 | * for the detailed format. |
| 288 | */ |
| 289 | LTTNG_HIDDEN |
| 290 | int lttng_trigger_serialize(const struct lttng_trigger *trigger, |
| 291 | struct lttng_payload *payload) |
| 292 | { |
| 293 | int ret; |
| 294 | size_t header_offset, size_before_payload, size_name; |
| 295 | struct lttng_trigger_comm trigger_comm = {}; |
| 296 | struct lttng_trigger_comm *header; |
| 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); |
| 303 | |
| 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 | |
| 312 | header_offset = payload->buffer.size; |
| 313 | ret = lttng_dynamic_buffer_append(&payload->buffer, &trigger_comm, |
| 314 | sizeof(trigger_comm)); |
| 315 | if (ret) { |
| 316 | goto end; |
| 317 | } |
| 318 | |
| 319 | size_before_payload = payload->buffer.size; |
| 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 | |
| 328 | ret = lttng_condition_serialize(trigger->condition, payload); |
| 329 | if (ret) { |
| 330 | goto end; |
| 331 | } |
| 332 | |
| 333 | ret = lttng_action_serialize(trigger->action, payload); |
| 334 | if (ret) { |
| 335 | goto end; |
| 336 | } |
| 337 | |
| 338 | /* Update payload size. */ |
| 339 | header = (typeof(header)) (payload->buffer.data + header_offset); |
| 340 | header->length = payload->buffer.size - size_before_payload; |
| 341 | end: |
| 342 | return ret; |
| 343 | } |
| 344 | |
| 345 | LTTNG_HIDDEN |
| 346 | bool lttng_trigger_is_equal( |
| 347 | const struct lttng_trigger *a, const struct lttng_trigger *b) |
| 348 | { |
| 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) { |
| 355 | return false; |
| 356 | } |
| 357 | |
| 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 | |
| 374 | enum lttng_trigger_status lttng_trigger_set_name(struct lttng_trigger *trigger, |
| 375 | const char* name) |
| 376 | { |
| 377 | char *name_copy = NULL; |
| 378 | enum lttng_trigger_status status = LTTNG_TRIGGER_STATUS_OK; |
| 379 | |
| 380 | if (!trigger || !name || |
| 381 | strlen(name) == 0) { |
| 382 | status = LTTNG_TRIGGER_STATUS_INVALID; |
| 383 | goto end; |
| 384 | } |
| 385 | |
| 386 | name_copy = strdup(name); |
| 387 | if (!name_copy) { |
| 388 | status = LTTNG_TRIGGER_STATUS_ERROR; |
| 389 | goto end; |
| 390 | } |
| 391 | |
| 392 | free(trigger->name); |
| 393 | |
| 394 | trigger->name = name_copy; |
| 395 | name_copy = NULL; |
| 396 | end: |
| 397 | return status; |
| 398 | } |
| 399 | |
| 400 | enum lttng_trigger_status lttng_trigger_get_name( |
| 401 | const struct lttng_trigger *trigger, const char **name) |
| 402 | { |
| 403 | enum lttng_trigger_status status = LTTNG_TRIGGER_STATUS_OK; |
| 404 | |
| 405 | if (!trigger || !name) { |
| 406 | status = LTTNG_TRIGGER_STATUS_INVALID; |
| 407 | goto end; |
| 408 | } |
| 409 | |
| 410 | if (!trigger->name) { |
| 411 | status = LTTNG_TRIGGER_STATUS_UNSET; |
| 412 | } |
| 413 | |
| 414 | *name = trigger->name; |
| 415 | end: |
| 416 | return status; |
| 417 | } |
| 418 | |
| 419 | LTTNG_HIDDEN |
| 420 | int lttng_trigger_assign_name(struct lttng_trigger *dst, |
| 421 | const struct lttng_trigger *src) |
| 422 | { |
| 423 | int ret = 0; |
| 424 | enum lttng_trigger_status status; |
| 425 | |
| 426 | status = lttng_trigger_set_name(dst, src->name); |
| 427 | if (status != LTTNG_TRIGGER_STATUS_OK) { |
| 428 | ret = -1; |
| 429 | ERR("Failed to set name for trigger"); |
| 430 | goto end; |
| 431 | } |
| 432 | end: |
| 433 | return ret; |
| 434 | } |
| 435 | |
| 436 | LTTNG_HIDDEN |
| 437 | void lttng_trigger_set_tracer_token(struct lttng_trigger *trigger, |
| 438 | uint64_t token) |
| 439 | { |
| 440 | assert(trigger); |
| 441 | LTTNG_OPTIONAL_SET(&trigger->tracer_token, token); |
| 442 | } |
| 443 | |
| 444 | LTTNG_HIDDEN |
| 445 | uint64_t lttng_trigger_get_tracer_token(const struct lttng_trigger *trigger) |
| 446 | { |
| 447 | assert(trigger); |
| 448 | |
| 449 | return LTTNG_OPTIONAL_GET(trigger->tracer_token); |
| 450 | } |
| 451 | |
| 452 | LTTNG_HIDDEN |
| 453 | int lttng_trigger_generate_name(struct lttng_trigger *trigger, |
| 454 | uint64_t unique_id) |
| 455 | { |
| 456 | int ret = 0; |
| 457 | char *generated_name = NULL; |
| 458 | |
| 459 | ret = asprintf(&generated_name, "T%" PRIu64 "", unique_id); |
| 460 | if (ret < 0) { |
| 461 | ERR("Failed to generate trigger name"); |
| 462 | ret = -1; |
| 463 | goto end; |
| 464 | } |
| 465 | |
| 466 | ret = 0; |
| 467 | free(trigger->name); |
| 468 | trigger->name = generated_name; |
| 469 | end: |
| 470 | return ret; |
| 471 | } |
| 472 | |
| 473 | LTTNG_HIDDEN |
| 474 | void lttng_trigger_get(struct lttng_trigger *trigger) |
| 475 | { |
| 476 | urcu_ref_get(&trigger->ref); |
| 477 | } |
| 478 | |
| 479 | LTTNG_HIDDEN |
| 480 | void lttng_trigger_put(struct lttng_trigger *trigger) |
| 481 | { |
| 482 | if (!trigger) { |
| 483 | return; |
| 484 | } |
| 485 | |
| 486 | urcu_ref_put(&trigger->ref , trigger_destroy_ref); |
| 487 | } |
| 488 | |
| 489 | static void delete_trigger_array_element(void *ptr) |
| 490 | { |
| 491 | struct lttng_trigger *trigger = ptr; |
| 492 | |
| 493 | lttng_trigger_put(trigger); |
| 494 | } |
| 495 | |
| 496 | LTTNG_HIDDEN |
| 497 | struct lttng_triggers *lttng_triggers_create(void) |
| 498 | { |
| 499 | struct lttng_triggers *triggers = NULL; |
| 500 | |
| 501 | triggers = zmalloc(sizeof(*triggers)); |
| 502 | if (!triggers) { |
| 503 | goto end; |
| 504 | } |
| 505 | |
| 506 | lttng_dynamic_pointer_array_init(&triggers->array, delete_trigger_array_element); |
| 507 | |
| 508 | end: |
| 509 | return triggers; |
| 510 | } |
| 511 | |
| 512 | LTTNG_HIDDEN |
| 513 | struct lttng_trigger *lttng_triggers_borrow_mutable_at_index( |
| 514 | const struct lttng_triggers *triggers, unsigned int index) |
| 515 | { |
| 516 | struct lttng_trigger *trigger = NULL; |
| 517 | |
| 518 | assert(triggers); |
| 519 | if (index >= lttng_dynamic_pointer_array_get_count(&triggers->array)) { |
| 520 | goto end; |
| 521 | } |
| 522 | |
| 523 | trigger = (struct lttng_trigger *) |
| 524 | lttng_dynamic_pointer_array_get_pointer( |
| 525 | &triggers->array, index); |
| 526 | end: |
| 527 | return trigger; |
| 528 | } |
| 529 | |
| 530 | LTTNG_HIDDEN |
| 531 | int lttng_triggers_add( |
| 532 | struct lttng_triggers *triggers, struct lttng_trigger *trigger) |
| 533 | { |
| 534 | int ret; |
| 535 | |
| 536 | assert(triggers); |
| 537 | assert(trigger); |
| 538 | |
| 539 | lttng_trigger_get(trigger); |
| 540 | |
| 541 | ret = lttng_dynamic_pointer_array_add_pointer(&triggers->array, trigger); |
| 542 | if (ret) { |
| 543 | lttng_trigger_put(trigger); |
| 544 | } |
| 545 | |
| 546 | return ret; |
| 547 | } |
| 548 | |
| 549 | const struct lttng_trigger *lttng_triggers_get_at_index( |
| 550 | const struct lttng_triggers *triggers, unsigned int index) |
| 551 | { |
| 552 | return lttng_triggers_borrow_mutable_at_index(triggers, index); |
| 553 | } |
| 554 | |
| 555 | enum lttng_trigger_status lttng_triggers_get_count(const struct lttng_triggers *triggers, unsigned int *count) |
| 556 | { |
| 557 | enum lttng_trigger_status status = LTTNG_TRIGGER_STATUS_OK; |
| 558 | |
| 559 | if (!triggers || !count) { |
| 560 | status = LTTNG_TRIGGER_STATUS_INVALID; |
| 561 | goto end; |
| 562 | } |
| 563 | |
| 564 | *count = lttng_dynamic_pointer_array_get_count(&triggers->array); |
| 565 | end: |
| 566 | return status; |
| 567 | } |
| 568 | |
| 569 | void lttng_triggers_destroy(struct lttng_triggers *triggers) |
| 570 | { |
| 571 | if (!triggers) { |
| 572 | return; |
| 573 | } |
| 574 | |
| 575 | lttng_dynamic_pointer_array_reset(&triggers->array); |
| 576 | free(triggers); |
| 577 | } |
| 578 | |
| 579 | int lttng_triggers_serialize(const struct lttng_triggers *triggers, |
| 580 | struct lttng_payload *payload) |
| 581 | { |
| 582 | int ret; |
| 583 | unsigned int i, count; |
| 584 | size_t size_before_payload; |
| 585 | struct lttng_triggers_comm triggers_comm = {}; |
| 586 | struct lttng_triggers_comm *header; |
| 587 | enum lttng_trigger_status status; |
| 588 | const size_t header_offset = payload->buffer.size; |
| 589 | |
| 590 | status = lttng_triggers_get_count(triggers, &count); |
| 591 | if (status != LTTNG_TRIGGER_STATUS_OK) { |
| 592 | ret = LTTNG_ERR_INVALID; |
| 593 | goto end; |
| 594 | } |
| 595 | |
| 596 | triggers_comm.count = count; |
| 597 | |
| 598 | /* Placeholder header; updated at the end. */ |
| 599 | ret = lttng_dynamic_buffer_append(&payload->buffer, &triggers_comm, |
| 600 | sizeof(triggers_comm)); |
| 601 | if (ret) { |
| 602 | goto end; |
| 603 | } |
| 604 | |
| 605 | size_before_payload = payload->buffer.size; |
| 606 | |
| 607 | for (i = 0; i < count; i++) { |
| 608 | const struct lttng_trigger *trigger = |
| 609 | lttng_triggers_get_at_index(triggers, i); |
| 610 | |
| 611 | assert(trigger); |
| 612 | |
| 613 | ret = lttng_trigger_serialize(trigger, payload); |
| 614 | if (ret) { |
| 615 | goto end; |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | /* Update payload size. */ |
| 620 | header = (struct lttng_triggers_comm *) ((char *) payload->buffer.data + header_offset); |
| 621 | header->length = payload->buffer.size - size_before_payload; |
| 622 | end: |
| 623 | return ret; |
| 624 | } |
| 625 | |
| 626 | LTTNG_HIDDEN |
| 627 | ssize_t lttng_triggers_create_from_payload( |
| 628 | struct lttng_payload_view *src_view, |
| 629 | struct lttng_triggers **triggers) |
| 630 | { |
| 631 | ssize_t ret, offset = 0, triggers_size = 0; |
| 632 | unsigned int i; |
| 633 | const struct lttng_triggers_comm *triggers_comm; |
| 634 | struct lttng_triggers *local_triggers = NULL; |
| 635 | |
| 636 | if (!src_view || !triggers) { |
| 637 | ret = -1; |
| 638 | goto error; |
| 639 | } |
| 640 | |
| 641 | /* lttng_trigger_comms header */ |
| 642 | triggers_comm = (const struct lttng_triggers_comm *) src_view->buffer.data; |
| 643 | offset += sizeof(*triggers_comm); |
| 644 | |
| 645 | local_triggers = lttng_triggers_create(); |
| 646 | if (!local_triggers) { |
| 647 | ret = -1; |
| 648 | goto error; |
| 649 | } |
| 650 | |
| 651 | for (i = 0; i < triggers_comm->count; i++) { |
| 652 | struct lttng_trigger *trigger = NULL; |
| 653 | struct lttng_payload_view trigger_view = |
| 654 | lttng_payload_view_from_view(src_view, offset, -1); |
| 655 | ssize_t trigger_size; |
| 656 | |
| 657 | trigger_size = lttng_trigger_create_from_payload( |
| 658 | &trigger_view, &trigger); |
| 659 | if (trigger_size < 0) { |
| 660 | ret = trigger_size; |
| 661 | goto error; |
| 662 | } |
| 663 | |
| 664 | /* Transfer ownership of the trigger to the collection. */ |
| 665 | ret = lttng_triggers_add(local_triggers, trigger); |
| 666 | lttng_trigger_put(trigger); |
| 667 | if (ret < 0) { |
| 668 | ret = -1; |
| 669 | goto error; |
| 670 | } |
| 671 | |
| 672 | offset += trigger_size; |
| 673 | triggers_size += trigger_size; |
| 674 | } |
| 675 | |
| 676 | /* Unexpected size of inner-elements; the buffer is corrupted. */ |
| 677 | if ((ssize_t) triggers_comm->length != triggers_size) { |
| 678 | ret = -1; |
| 679 | goto error; |
| 680 | } |
| 681 | |
| 682 | /* Pass ownership to caller. */ |
| 683 | *triggers = local_triggers; |
| 684 | local_triggers = NULL; |
| 685 | |
| 686 | ret = offset; |
| 687 | error: |
| 688 | |
| 689 | lttng_triggers_destroy(local_triggers); |
| 690 | return ret; |
| 691 | } |
| 692 | |
| 693 | LTTNG_HIDDEN |
| 694 | const struct lttng_credentials *lttng_trigger_get_credentials( |
| 695 | const struct lttng_trigger *trigger) |
| 696 | { |
| 697 | return &trigger->creds; |
| 698 | } |
| 699 | |
| 700 | LTTNG_HIDDEN |
| 701 | void lttng_trigger_set_credentials(struct lttng_trigger *trigger, |
| 702 | const struct lttng_credentials *creds) |
| 703 | { |
| 704 | assert(creds); |
| 705 | trigger->creds = *creds; |
| 706 | } |
| 707 | |
| 708 | enum lttng_trigger_status lttng_trigger_set_owner_uid( |
| 709 | struct lttng_trigger *trigger, uid_t uid) |
| 710 | { |
| 711 | enum lttng_trigger_status ret = LTTNG_TRIGGER_STATUS_OK; |
| 712 | const uid_t euid = geteuid(); |
| 713 | const struct lttng_credentials creds = { |
| 714 | .uid = LTTNG_OPTIONAL_INIT_VALUE(uid), |
| 715 | .gid = LTTNG_OPTIONAL_INIT_UNSET, |
| 716 | }; |
| 717 | |
| 718 | if (!trigger) { |
| 719 | ret = LTTNG_TRIGGER_STATUS_INVALID; |
| 720 | goto end; |
| 721 | } |
| 722 | |
| 723 | /* Client-side validation only to report a clearer error. */ |
| 724 | if (euid != 0 && euid != uid) { |
| 725 | ret = LTTNG_TRIGGER_STATUS_PERMISSION_DENIED; |
| 726 | goto end; |
| 727 | } |
| 728 | |
| 729 | lttng_trigger_set_credentials(trigger, &creds); |
| 730 | |
| 731 | end: |
| 732 | return ret; |
| 733 | } |
| 734 | |
| 735 | enum lttng_trigger_status lttng_trigger_get_owner_uid( |
| 736 | const struct lttng_trigger *trigger, uid_t *uid) |
| 737 | { |
| 738 | enum lttng_trigger_status ret = LTTNG_TRIGGER_STATUS_OK; |
| 739 | const struct lttng_credentials *creds = NULL; |
| 740 | |
| 741 | if (!trigger || !uid ) { |
| 742 | ret = LTTNG_TRIGGER_STATUS_INVALID; |
| 743 | goto end; |
| 744 | } |
| 745 | |
| 746 | if (!trigger->creds.uid.is_set ) { |
| 747 | ret = LTTNG_TRIGGER_STATUS_UNSET; |
| 748 | goto end; |
| 749 | } |
| 750 | |
| 751 | creds = lttng_trigger_get_credentials(trigger); |
| 752 | *uid = lttng_credentials_get_uid(creds); |
| 753 | |
| 754 | end: |
| 755 | return ret; |
| 756 | } |
| 757 | |
| 758 | LTTNG_HIDDEN |
| 759 | enum lttng_domain_type lttng_trigger_get_underlying_domain_type_restriction( |
| 760 | const struct lttng_trigger *trigger) |
| 761 | { |
| 762 | enum lttng_domain_type type = LTTNG_DOMAIN_NONE; |
| 763 | const struct lttng_event_rule *event_rule; |
| 764 | enum lttng_condition_status c_status; |
| 765 | enum lttng_condition_type c_type; |
| 766 | |
| 767 | assert(trigger); |
| 768 | assert(trigger->condition); |
| 769 | |
| 770 | c_type = lttng_condition_get_type(trigger->condition); |
| 771 | assert (c_type != LTTNG_CONDITION_TYPE_UNKNOWN); |
| 772 | |
| 773 | switch (c_type) { |
| 774 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: |
| 775 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: |
| 776 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: |
| 777 | /* Apply to any domain. */ |
| 778 | type = LTTNG_DOMAIN_NONE; |
| 779 | break; |
| 780 | case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES: |
| 781 | /* Return the domain of the event rule. */ |
| 782 | c_status = lttng_condition_event_rule_matches_get_rule( |
| 783 | trigger->condition, &event_rule); |
| 784 | assert(c_status == LTTNG_CONDITION_STATUS_OK); |
| 785 | type = lttng_event_rule_get_domain_type(event_rule); |
| 786 | break; |
| 787 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: |
| 788 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: |
| 789 | /* Return the domain of the channel being monitored. */ |
| 790 | c_status = lttng_condition_buffer_usage_get_domain_type( |
| 791 | trigger->condition, &type); |
| 792 | assert(c_status == LTTNG_CONDITION_STATUS_OK); |
| 793 | break; |
| 794 | default: |
| 795 | abort(); |
| 796 | } |
| 797 | |
| 798 | return type; |
| 799 | } |
| 800 | |
| 801 | /* |
| 802 | * Generate bytecode related to the trigger. |
| 803 | * On success LTTNG_OK. On error, returns lttng_error code. |
| 804 | */ |
| 805 | LTTNG_HIDDEN |
| 806 | enum lttng_error_code lttng_trigger_generate_bytecode( |
| 807 | struct lttng_trigger *trigger, |
| 808 | const struct lttng_credentials *creds) |
| 809 | { |
| 810 | enum lttng_error_code ret; |
| 811 | struct lttng_condition *condition = NULL; |
| 812 | |
| 813 | condition = lttng_trigger_get_condition(trigger); |
| 814 | if (!condition) { |
| 815 | ret = LTTNG_ERR_INVALID_TRIGGER; |
| 816 | goto end; |
| 817 | } |
| 818 | |
| 819 | switch (lttng_condition_get_type(condition)) { |
| 820 | case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES: |
| 821 | { |
| 822 | struct lttng_event_rule *event_rule; |
| 823 | const enum lttng_condition_status condition_status = |
| 824 | lttng_condition_event_rule_matches_borrow_rule_mutable( |
| 825 | condition, &event_rule); |
| 826 | |
| 827 | assert(condition_status == LTTNG_CONDITION_STATUS_OK); |
| 828 | |
| 829 | /* Generate the filter bytecode. */ |
| 830 | ret = lttng_event_rule_generate_filter_bytecode( |
| 831 | event_rule, creds); |
| 832 | if (ret != LTTNG_OK) { |
| 833 | goto end; |
| 834 | } |
| 835 | |
| 836 | /* Generate the capture bytecode. */ |
| 837 | ret = lttng_condition_event_rule_matches_generate_capture_descriptor_bytecode( |
| 838 | condition); |
| 839 | if (ret != LTTNG_OK) { |
| 840 | goto end; |
| 841 | } |
| 842 | |
| 843 | ret = LTTNG_OK; |
| 844 | break; |
| 845 | } |
| 846 | default: |
| 847 | ret = LTTNG_OK; |
| 848 | break; |
| 849 | } |
| 850 | end: |
| 851 | return ret; |
| 852 | } |
| 853 | |
| 854 | LTTNG_HIDDEN |
| 855 | struct lttng_trigger *lttng_trigger_copy(const struct lttng_trigger *trigger) |
| 856 | { |
| 857 | int ret; |
| 858 | struct lttng_payload copy_buffer; |
| 859 | struct lttng_condition *condition_copy = NULL; |
| 860 | struct lttng_action *action_copy = NULL; |
| 861 | struct lttng_trigger *copy = NULL; |
| 862 | enum lttng_trigger_status trigger_status; |
| 863 | const char *trigger_name; |
| 864 | uid_t trigger_owner_uid; |
| 865 | |
| 866 | lttng_payload_init(©_buffer); |
| 867 | |
| 868 | ret = lttng_condition_serialize(trigger->condition, ©_buffer); |
| 869 | if (ret < 0) { |
| 870 | goto end; |
| 871 | } |
| 872 | |
| 873 | { |
| 874 | struct lttng_payload_view view = |
| 875 | lttng_payload_view_from_payload( |
| 876 | ©_buffer, 0, -1); |
| 877 | |
| 878 | ret = lttng_condition_create_from_payload( |
| 879 | &view, &condition_copy); |
| 880 | if (ret < 0) { |
| 881 | goto end; |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | lttng_payload_clear(©_buffer); |
| 886 | |
| 887 | ret = lttng_action_serialize(trigger->action, ©_buffer); |
| 888 | if (ret < 0) { |
| 889 | goto end; |
| 890 | } |
| 891 | |
| 892 | { |
| 893 | struct lttng_payload_view view = |
| 894 | lttng_payload_view_from_payload( |
| 895 | ©_buffer, 0, -1); |
| 896 | |
| 897 | ret = lttng_action_create_from_payload( |
| 898 | &view, &action_copy); |
| 899 | if (ret < 0) { |
| 900 | goto end; |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | copy = lttng_trigger_create(condition_copy, action_copy); |
| 905 | if (!copy) { |
| 906 | ERR("Failed to allocate trigger during trigger copy"); |
| 907 | goto end; |
| 908 | } |
| 909 | |
| 910 | trigger_status = lttng_trigger_get_name(trigger, &trigger_name); |
| 911 | switch (trigger_status) { |
| 912 | case LTTNG_TRIGGER_STATUS_OK: |
| 913 | trigger_status = lttng_trigger_set_name(copy, trigger_name); |
| 914 | if (trigger_status != LTTNG_TRIGGER_STATUS_OK) { |
| 915 | ERR("Failed to set name of new trigger during copy"); |
| 916 | goto error_cleanup_trigger; |
| 917 | } |
| 918 | break; |
| 919 | case LTTNG_TRIGGER_STATUS_UNSET: |
| 920 | break; |
| 921 | default: |
| 922 | ERR("Failed to get name of original trigger during copy"); |
| 923 | goto error_cleanup_trigger; |
| 924 | } |
| 925 | |
| 926 | trigger_status = lttng_trigger_get_owner_uid( |
| 927 | trigger, &trigger_owner_uid); |
| 928 | switch (trigger_status) { |
| 929 | case LTTNG_TRIGGER_STATUS_OK: |
| 930 | LTTNG_OPTIONAL_SET(©->creds.uid, trigger_owner_uid); |
| 931 | break; |
| 932 | case LTTNG_TRIGGER_STATUS_UNSET: |
| 933 | break; |
| 934 | default: |
| 935 | ERR("Failed to get owner uid of original trigger during copy"); |
| 936 | goto error_cleanup_trigger; |
| 937 | } |
| 938 | |
| 939 | copy->tracer_token = trigger->tracer_token; |
| 940 | copy->registered = trigger->registered; |
| 941 | goto end; |
| 942 | |
| 943 | error_cleanup_trigger: |
| 944 | lttng_trigger_destroy(copy); |
| 945 | copy = NULL; |
| 946 | end: |
| 947 | lttng_condition_put(condition_copy); |
| 948 | lttng_action_put(action_copy); |
| 949 | lttng_payload_reset(©_buffer); |
| 950 | return copy; |
| 951 | } |
| 952 | |
| 953 | LTTNG_HIDDEN |
| 954 | bool lttng_trigger_needs_tracer_notifier(const struct lttng_trigger *trigger) |
| 955 | { |
| 956 | bool needs_tracer_notifier = false; |
| 957 | const struct lttng_condition *condition = |
| 958 | lttng_trigger_get_const_condition(trigger); |
| 959 | |
| 960 | switch (lttng_condition_get_type(condition)) { |
| 961 | case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES: |
| 962 | needs_tracer_notifier = true; |
| 963 | goto end; |
| 964 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: |
| 965 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: |
| 966 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: |
| 967 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: |
| 968 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: |
| 969 | goto end; |
| 970 | case LTTNG_CONDITION_TYPE_UNKNOWN: |
| 971 | default: |
| 972 | abort(); |
| 973 | } |
| 974 | end: |
| 975 | return needs_tracer_notifier; |
| 976 | } |
| 977 | |
| 978 | LTTNG_HIDDEN |
| 979 | void lttng_trigger_set_as_registered(struct lttng_trigger *trigger) |
| 980 | { |
| 981 | pthread_mutex_lock(&trigger->lock); |
| 982 | trigger->registered = true; |
| 983 | pthread_mutex_unlock(&trigger->lock); |
| 984 | } |
| 985 | |
| 986 | LTTNG_HIDDEN |
| 987 | void lttng_trigger_set_as_unregistered(struct lttng_trigger *trigger) |
| 988 | { |
| 989 | pthread_mutex_lock(&trigger->lock); |
| 990 | trigger->registered = false; |
| 991 | pthread_mutex_unlock(&trigger->lock); |
| 992 | } |
| 993 | |
| 994 | /* |
| 995 | * The trigger must be locked before calling lttng_trigger_registered. |
| 996 | * The lock is necessary since a trigger can be unregistered at anytime. |
| 997 | * Manipulations requiring that the trigger be registered must always acquire |
| 998 | * the trigger lock for the duration of the manipulation using |
| 999 | * `lttng_trigger_lock` and `lttng_trigger_unlock`. |
| 1000 | */ |
| 1001 | LTTNG_HIDDEN |
| 1002 | bool lttng_trigger_is_registered(struct lttng_trigger *trigger) |
| 1003 | { |
| 1004 | ASSERT_LOCKED(trigger->lock); |
| 1005 | return trigger->registered; |
| 1006 | } |
| 1007 | |
| 1008 | LTTNG_HIDDEN |
| 1009 | void lttng_trigger_lock(struct lttng_trigger *trigger) |
| 1010 | { |
| 1011 | pthread_mutex_lock(&trigger->lock); |
| 1012 | } |
| 1013 | |
| 1014 | LTTNG_HIDDEN |
| 1015 | void lttng_trigger_unlock(struct lttng_trigger *trigger) |
| 1016 | { |
| 1017 | pthread_mutex_unlock(&trigger->lock); |
| 1018 | } |