| 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 <common/credentials.hpp> |
| 9 | #include <common/dynamic-array.hpp> |
| 10 | #include <common/error.hpp> |
| 11 | #include <common/mi-lttng.hpp> |
| 12 | #include <common/optional.hpp> |
| 13 | #include <common/payload-view.hpp> |
| 14 | #include <common/payload.hpp> |
| 15 | #include <inttypes.h> |
| 16 | #include <lttng/action/action-internal.hpp> |
| 17 | #include <lttng/condition/buffer-usage.h> |
| 18 | #include <lttng/condition/condition-internal.hpp> |
| 19 | #include <lttng/condition/event-rule-matches-internal.hpp> |
| 20 | #include <lttng/condition/event-rule-matches.h> |
| 21 | #include <lttng/domain.h> |
| 22 | #include <lttng/error-query-internal.hpp> |
| 23 | #include <lttng/event-expr-internal.hpp> |
| 24 | #include <lttng/event-rule/event-rule-internal.hpp> |
| 25 | #include <lttng/trigger/trigger-internal.hpp> |
| 26 | #include <pthread.h> |
| 27 | |
| 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<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 | lttng::utils::container_of(ref, <tng_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 | LTTNG_ASSERT(action); |
| 121 | LTTNG_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 | ssize_t lttng_trigger_create_from_payload( |
| 139 | struct lttng_payload_view *src_view, |
| 140 | struct lttng_trigger **_trigger) |
| 141 | { |
| 142 | ssize_t ret, offset = 0, condition_size, action_size, name_size = 0; |
| 143 | struct lttng_condition *condition = NULL; |
| 144 | struct lttng_action *action = NULL; |
| 145 | const struct lttng_trigger_comm *trigger_comm; |
| 146 | const char *name = NULL; |
| 147 | struct lttng_credentials creds = { |
| 148 | .uid = LTTNG_OPTIONAL_INIT_UNSET, |
| 149 | .gid = LTTNG_OPTIONAL_INIT_UNSET, |
| 150 | }; |
| 151 | struct lttng_trigger *trigger = NULL; |
| 152 | const struct lttng_payload_view trigger_comm_view = |
| 153 | lttng_payload_view_from_view( |
| 154 | src_view, 0, sizeof(*trigger_comm)); |
| 155 | |
| 156 | if (!src_view || !_trigger) { |
| 157 | ret = -1; |
| 158 | goto end; |
| 159 | } |
| 160 | |
| 161 | if (!lttng_payload_view_is_valid(&trigger_comm_view)) { |
| 162 | /* Payload not large enough to contain the header. */ |
| 163 | ret = -1; |
| 164 | goto end; |
| 165 | } |
| 166 | |
| 167 | /* lttng_trigger_comm header */ |
| 168 | trigger_comm = (typeof(trigger_comm)) trigger_comm_view.buffer.data; |
| 169 | |
| 170 | /* Set the trigger's creds. */ |
| 171 | if (trigger_comm->uid > (uint64_t) ((uid_t) -1)) { |
| 172 | /* UID out of range for this platform. */ |
| 173 | ret = -1; |
| 174 | goto end; |
| 175 | } |
| 176 | |
| 177 | LTTNG_OPTIONAL_SET(&creds.uid, trigger_comm->uid); |
| 178 | |
| 179 | offset += sizeof(*trigger_comm); |
| 180 | |
| 181 | if (trigger_comm->name_length != 0) { |
| 182 | /* Name. */ |
| 183 | const struct lttng_payload_view name_view = |
| 184 | lttng_payload_view_from_view( |
| 185 | src_view, offset, |
| 186 | trigger_comm->name_length); |
| 187 | |
| 188 | if (!lttng_payload_view_is_valid(&name_view)) { |
| 189 | ret = -1; |
| 190 | goto end; |
| 191 | } |
| 192 | |
| 193 | name = name_view.buffer.data; |
| 194 | if (!lttng_buffer_view_contains_string(&name_view.buffer, name, |
| 195 | trigger_comm->name_length)) { |
| 196 | ret = -1; |
| 197 | goto end; |
| 198 | } |
| 199 | |
| 200 | offset += trigger_comm->name_length; |
| 201 | name_size = trigger_comm->name_length; |
| 202 | } |
| 203 | |
| 204 | { |
| 205 | /* struct lttng_condition */ |
| 206 | struct lttng_payload_view condition_view = |
| 207 | lttng_payload_view_from_view( |
| 208 | src_view, offset, -1); |
| 209 | |
| 210 | condition_size = lttng_condition_create_from_payload(&condition_view, |
| 211 | &condition); |
| 212 | } |
| 213 | |
| 214 | if (condition_size < 0) { |
| 215 | ret = condition_size; |
| 216 | goto end; |
| 217 | } |
| 218 | |
| 219 | offset += condition_size; |
| 220 | { |
| 221 | /* struct lttng_action */ |
| 222 | struct lttng_payload_view action_view = |
| 223 | lttng_payload_view_from_view( |
| 224 | src_view, offset, -1); |
| 225 | |
| 226 | action_size = lttng_action_create_from_payload(&action_view, &action); |
| 227 | } |
| 228 | |
| 229 | if (action_size < 0) { |
| 230 | ret = action_size; |
| 231 | goto end; |
| 232 | } |
| 233 | offset += action_size; |
| 234 | |
| 235 | /* Unexpected size of inner-elements; the buffer is corrupted. */ |
| 236 | if ((ssize_t) trigger_comm->length != condition_size + action_size + name_size) { |
| 237 | ret = -1; |
| 238 | goto error; |
| 239 | } |
| 240 | |
| 241 | trigger = lttng_trigger_create(condition, action); |
| 242 | if (!trigger) { |
| 243 | ret = -1; |
| 244 | goto error; |
| 245 | } |
| 246 | |
| 247 | lttng_trigger_set_credentials(trigger, &creds); |
| 248 | |
| 249 | /* |
| 250 | * The trigger object owns references to the action and condition |
| 251 | * objects. |
| 252 | */ |
| 253 | lttng_condition_put(condition); |
| 254 | condition = NULL; |
| 255 | |
| 256 | lttng_action_put(action); |
| 257 | action = NULL; |
| 258 | |
| 259 | if (name) { |
| 260 | const enum lttng_trigger_status status = |
| 261 | lttng_trigger_set_name(trigger, name); |
| 262 | |
| 263 | if (status != LTTNG_TRIGGER_STATUS_OK) { |
| 264 | ret = -1; |
| 265 | goto end; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | if (trigger_comm->is_hidden) { |
| 270 | lttng_trigger_set_hidden(trigger); |
| 271 | } |
| 272 | |
| 273 | ret = offset; |
| 274 | |
| 275 | error: |
| 276 | lttng_condition_put(condition); |
| 277 | lttng_action_put(action); |
| 278 | end: |
| 279 | if (ret >= 0) { |
| 280 | *_trigger = trigger; |
| 281 | } else { |
| 282 | lttng_trigger_put(trigger); |
| 283 | } |
| 284 | |
| 285 | return ret; |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | * Both elements are stored contiguously, see their "*_comm" structure |
| 290 | * for the detailed format. |
| 291 | */ |
| 292 | int lttng_trigger_serialize(const struct lttng_trigger *trigger, |
| 293 | struct lttng_payload *payload) |
| 294 | { |
| 295 | int ret; |
| 296 | size_t header_offset, size_before_payload, size_name; |
| 297 | struct lttng_trigger_comm trigger_comm = {}; |
| 298 | struct lttng_trigger_comm *header; |
| 299 | const struct lttng_credentials *creds = NULL; |
| 300 | |
| 301 | creds = lttng_trigger_get_credentials(trigger); |
| 302 | LTTNG_ASSERT(creds); |
| 303 | |
| 304 | trigger_comm.uid = LTTNG_OPTIONAL_GET(creds->uid); |
| 305 | |
| 306 | if (trigger->name != NULL) { |
| 307 | size_name = strlen(trigger->name) + 1; |
| 308 | } else { |
| 309 | size_name = 0; |
| 310 | } |
| 311 | |
| 312 | trigger_comm.name_length = size_name; |
| 313 | |
| 314 | trigger_comm.is_hidden = lttng_trigger_is_hidden(trigger); |
| 315 | |
| 316 | header_offset = payload->buffer.size; |
| 317 | ret = lttng_dynamic_buffer_append(&payload->buffer, &trigger_comm, |
| 318 | sizeof(trigger_comm)); |
| 319 | if (ret) { |
| 320 | goto end; |
| 321 | } |
| 322 | |
| 323 | size_before_payload = payload->buffer.size; |
| 324 | |
| 325 | /* Trigger name. */ |
| 326 | ret = lttng_dynamic_buffer_append( |
| 327 | &payload->buffer, trigger->name, size_name); |
| 328 | if (ret) { |
| 329 | goto end; |
| 330 | } |
| 331 | |
| 332 | ret = lttng_condition_serialize(trigger->condition, payload); |
| 333 | if (ret) { |
| 334 | goto end; |
| 335 | } |
| 336 | |
| 337 | ret = lttng_action_serialize(trigger->action, payload); |
| 338 | if (ret) { |
| 339 | goto end; |
| 340 | } |
| 341 | |
| 342 | /* Update payload size. */ |
| 343 | header = (typeof(header)) (payload->buffer.data + header_offset); |
| 344 | header->length = payload->buffer.size - size_before_payload; |
| 345 | end: |
| 346 | return ret; |
| 347 | } |
| 348 | |
| 349 | bool lttng_trigger_is_equal( |
| 350 | const struct lttng_trigger *a, const struct lttng_trigger *b) |
| 351 | { |
| 352 | if (!!a->name != !!b->name) { |
| 353 | /* Both must be either anonymous or named. */ |
| 354 | return false; |
| 355 | } |
| 356 | |
| 357 | if (a->name && strcmp(a->name, b->name) != 0) { |
| 358 | return false; |
| 359 | } |
| 360 | |
| 361 | if (!lttng_condition_is_equal(a->condition, b->condition)) { |
| 362 | return false; |
| 363 | } |
| 364 | |
| 365 | if (!lttng_action_is_equal(a->action, b->action)) { |
| 366 | return false; |
| 367 | } |
| 368 | |
| 369 | if (!lttng_credentials_is_equal(lttng_trigger_get_credentials(a), |
| 370 | lttng_trigger_get_credentials(b))) { |
| 371 | return false; |
| 372 | } |
| 373 | |
| 374 | if (a->is_hidden != b->is_hidden) { |
| 375 | return false; |
| 376 | } |
| 377 | |
| 378 | return true; |
| 379 | } |
| 380 | |
| 381 | bool lttng_trigger_is_hidden(const struct lttng_trigger *trigger) |
| 382 | { |
| 383 | LTTNG_ASSERT(trigger); |
| 384 | return trigger->is_hidden; |
| 385 | } |
| 386 | |
| 387 | void lttng_trigger_set_hidden(struct lttng_trigger *trigger) |
| 388 | { |
| 389 | LTTNG_ASSERT(!trigger->is_hidden); |
| 390 | trigger->is_hidden = true; |
| 391 | } |
| 392 | |
| 393 | enum lttng_trigger_status lttng_trigger_set_name(struct lttng_trigger *trigger, |
| 394 | const char* name) |
| 395 | { |
| 396 | char *name_copy = NULL; |
| 397 | enum lttng_trigger_status status = LTTNG_TRIGGER_STATUS_OK; |
| 398 | |
| 399 | if (!trigger) { |
| 400 | status = LTTNG_TRIGGER_STATUS_INVALID; |
| 401 | goto end; |
| 402 | } |
| 403 | |
| 404 | if (name) { |
| 405 | name_copy = strdup(name); |
| 406 | if (!name_copy) { |
| 407 | status = LTTNG_TRIGGER_STATUS_ERROR; |
| 408 | goto end; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | free(trigger->name); |
| 413 | |
| 414 | trigger->name = name_copy; |
| 415 | name_copy = NULL; |
| 416 | end: |
| 417 | return status; |
| 418 | } |
| 419 | |
| 420 | enum lttng_trigger_status lttng_trigger_get_name( |
| 421 | const struct lttng_trigger *trigger, const char **name) |
| 422 | { |
| 423 | enum lttng_trigger_status status = LTTNG_TRIGGER_STATUS_OK; |
| 424 | |
| 425 | if (!trigger || !name) { |
| 426 | status = LTTNG_TRIGGER_STATUS_INVALID; |
| 427 | goto end; |
| 428 | } |
| 429 | |
| 430 | if (!trigger->name) { |
| 431 | status = LTTNG_TRIGGER_STATUS_UNSET; |
| 432 | } |
| 433 | |
| 434 | *name = trigger->name; |
| 435 | end: |
| 436 | return status; |
| 437 | } |
| 438 | |
| 439 | int lttng_trigger_assign_name(struct lttng_trigger *dst, |
| 440 | const struct lttng_trigger *src) |
| 441 | { |
| 442 | int ret = 0; |
| 443 | enum lttng_trigger_status status; |
| 444 | |
| 445 | status = lttng_trigger_set_name(dst, src->name); |
| 446 | if (status != LTTNG_TRIGGER_STATUS_OK) { |
| 447 | ret = -1; |
| 448 | ERR("Failed to set name for trigger"); |
| 449 | goto end; |
| 450 | } |
| 451 | end: |
| 452 | return ret; |
| 453 | } |
| 454 | |
| 455 | void lttng_trigger_set_tracer_token(struct lttng_trigger *trigger, |
| 456 | uint64_t token) |
| 457 | { |
| 458 | LTTNG_ASSERT(trigger); |
| 459 | LTTNG_OPTIONAL_SET(&trigger->tracer_token, token); |
| 460 | } |
| 461 | |
| 462 | uint64_t lttng_trigger_get_tracer_token(const struct lttng_trigger *trigger) |
| 463 | { |
| 464 | LTTNG_ASSERT(trigger); |
| 465 | |
| 466 | return LTTNG_OPTIONAL_GET(trigger->tracer_token); |
| 467 | } |
| 468 | |
| 469 | int lttng_trigger_generate_name(struct lttng_trigger *trigger, |
| 470 | uint64_t unique_id) |
| 471 | { |
| 472 | int ret = 0; |
| 473 | char *generated_name = NULL; |
| 474 | |
| 475 | ret = asprintf(&generated_name, "trigger%" PRIu64 "", unique_id); |
| 476 | if (ret < 0) { |
| 477 | ERR("Failed to generate trigger name"); |
| 478 | ret = -1; |
| 479 | goto end; |
| 480 | } |
| 481 | |
| 482 | ret = 0; |
| 483 | free(trigger->name); |
| 484 | trigger->name = generated_name; |
| 485 | end: |
| 486 | return ret; |
| 487 | } |
| 488 | |
| 489 | void lttng_trigger_get(struct lttng_trigger *trigger) |
| 490 | { |
| 491 | urcu_ref_get(&trigger->ref); |
| 492 | } |
| 493 | |
| 494 | void lttng_trigger_put(struct lttng_trigger *trigger) |
| 495 | { |
| 496 | if (!trigger) { |
| 497 | return; |
| 498 | } |
| 499 | |
| 500 | urcu_ref_put(&trigger->ref , trigger_destroy_ref); |
| 501 | } |
| 502 | |
| 503 | static void delete_trigger_array_element(void *ptr) |
| 504 | { |
| 505 | struct lttng_trigger *trigger = (lttng_trigger *) ptr; |
| 506 | |
| 507 | lttng_trigger_put(trigger); |
| 508 | } |
| 509 | |
| 510 | struct lttng_triggers *lttng_triggers_create(void) |
| 511 | { |
| 512 | struct lttng_triggers *triggers = NULL; |
| 513 | |
| 514 | triggers = zmalloc<lttng_triggers>(); |
| 515 | if (!triggers) { |
| 516 | goto end; |
| 517 | } |
| 518 | |
| 519 | lttng_dynamic_pointer_array_init(&triggers->array, delete_trigger_array_element); |
| 520 | |
| 521 | end: |
| 522 | return triggers; |
| 523 | } |
| 524 | |
| 525 | struct lttng_trigger *lttng_triggers_borrow_mutable_at_index( |
| 526 | const struct lttng_triggers *triggers, unsigned int index) |
| 527 | { |
| 528 | struct lttng_trigger *trigger = NULL; |
| 529 | |
| 530 | LTTNG_ASSERT(triggers); |
| 531 | if (index >= lttng_dynamic_pointer_array_get_count(&triggers->array)) { |
| 532 | goto end; |
| 533 | } |
| 534 | |
| 535 | trigger = (struct lttng_trigger *) |
| 536 | lttng_dynamic_pointer_array_get_pointer( |
| 537 | &triggers->array, index); |
| 538 | end: |
| 539 | return trigger; |
| 540 | } |
| 541 | |
| 542 | int lttng_triggers_add( |
| 543 | struct lttng_triggers *triggers, struct lttng_trigger *trigger) |
| 544 | { |
| 545 | int ret; |
| 546 | |
| 547 | LTTNG_ASSERT(triggers); |
| 548 | LTTNG_ASSERT(trigger); |
| 549 | |
| 550 | lttng_trigger_get(trigger); |
| 551 | |
| 552 | ret = lttng_dynamic_pointer_array_add_pointer(&triggers->array, trigger); |
| 553 | if (ret) { |
| 554 | lttng_trigger_put(trigger); |
| 555 | } |
| 556 | |
| 557 | return ret; |
| 558 | } |
| 559 | |
| 560 | int lttng_triggers_remove_hidden_triggers(struct lttng_triggers *triggers) |
| 561 | { |
| 562 | int ret; |
| 563 | unsigned int trigger_count, i = 0; |
| 564 | enum lttng_trigger_status trigger_status; |
| 565 | |
| 566 | LTTNG_ASSERT(triggers); |
| 567 | |
| 568 | trigger_status = lttng_triggers_get_count(triggers, &trigger_count); |
| 569 | LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK); |
| 570 | |
| 571 | while (i < trigger_count) { |
| 572 | const struct lttng_trigger *trigger = |
| 573 | lttng_triggers_get_at_index(triggers, i); |
| 574 | |
| 575 | if (lttng_trigger_is_hidden(trigger)) { |
| 576 | ret = lttng_dynamic_pointer_array_remove_pointer( |
| 577 | &triggers->array, i); |
| 578 | if (ret) { |
| 579 | goto end; |
| 580 | } |
| 581 | |
| 582 | trigger_count--; |
| 583 | } else { |
| 584 | i++; |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | ret = 0; |
| 589 | end: |
| 590 | return ret; |
| 591 | } |
| 592 | |
| 593 | const struct lttng_trigger *lttng_triggers_get_at_index( |
| 594 | const struct lttng_triggers *triggers, unsigned int index) |
| 595 | { |
| 596 | return lttng_triggers_borrow_mutable_at_index(triggers, index); |
| 597 | } |
| 598 | |
| 599 | enum lttng_trigger_status lttng_triggers_get_count(const struct lttng_triggers *triggers, unsigned int *count) |
| 600 | { |
| 601 | enum lttng_trigger_status status = LTTNG_TRIGGER_STATUS_OK; |
| 602 | |
| 603 | if (!triggers || !count) { |
| 604 | status = LTTNG_TRIGGER_STATUS_INVALID; |
| 605 | goto end; |
| 606 | } |
| 607 | |
| 608 | *count = lttng_dynamic_pointer_array_get_count(&triggers->array); |
| 609 | end: |
| 610 | return status; |
| 611 | } |
| 612 | |
| 613 | void lttng_triggers_destroy(struct lttng_triggers *triggers) |
| 614 | { |
| 615 | if (!triggers) { |
| 616 | return; |
| 617 | } |
| 618 | |
| 619 | lttng_dynamic_pointer_array_reset(&triggers->array); |
| 620 | free(triggers); |
| 621 | } |
| 622 | |
| 623 | int lttng_triggers_serialize(const struct lttng_triggers *triggers, |
| 624 | struct lttng_payload *payload) |
| 625 | { |
| 626 | int ret; |
| 627 | unsigned int i, count; |
| 628 | size_t size_before_payload; |
| 629 | struct lttng_triggers_comm triggers_comm = {}; |
| 630 | struct lttng_triggers_comm *header; |
| 631 | enum lttng_trigger_status status; |
| 632 | const size_t header_offset = payload->buffer.size; |
| 633 | |
| 634 | status = lttng_triggers_get_count(triggers, &count); |
| 635 | if (status != LTTNG_TRIGGER_STATUS_OK) { |
| 636 | ret = LTTNG_ERR_INVALID; |
| 637 | goto end; |
| 638 | } |
| 639 | |
| 640 | triggers_comm.count = count; |
| 641 | |
| 642 | /* Placeholder header; updated at the end. */ |
| 643 | ret = lttng_dynamic_buffer_append(&payload->buffer, &triggers_comm, |
| 644 | sizeof(triggers_comm)); |
| 645 | if (ret) { |
| 646 | goto end; |
| 647 | } |
| 648 | |
| 649 | size_before_payload = payload->buffer.size; |
| 650 | |
| 651 | for (i = 0; i < count; i++) { |
| 652 | const struct lttng_trigger *trigger = |
| 653 | lttng_triggers_get_at_index(triggers, i); |
| 654 | |
| 655 | LTTNG_ASSERT(trigger); |
| 656 | |
| 657 | ret = lttng_trigger_serialize(trigger, payload); |
| 658 | if (ret) { |
| 659 | goto end; |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | /* Update payload size. */ |
| 664 | header = (struct lttng_triggers_comm *) ((char *) payload->buffer.data + header_offset); |
| 665 | header->length = payload->buffer.size - size_before_payload; |
| 666 | end: |
| 667 | return ret; |
| 668 | } |
| 669 | |
| 670 | ssize_t lttng_triggers_create_from_payload( |
| 671 | struct lttng_payload_view *src_view, |
| 672 | struct lttng_triggers **triggers) |
| 673 | { |
| 674 | ssize_t ret, offset = 0, triggers_size = 0; |
| 675 | unsigned int i; |
| 676 | const struct lttng_triggers_comm *triggers_comm; |
| 677 | struct lttng_triggers *local_triggers = NULL; |
| 678 | |
| 679 | if (!src_view || !triggers) { |
| 680 | ret = -1; |
| 681 | goto error; |
| 682 | } |
| 683 | |
| 684 | /* lttng_trigger_comms header */ |
| 685 | triggers_comm = (const struct lttng_triggers_comm *) src_view->buffer.data; |
| 686 | offset += sizeof(*triggers_comm); |
| 687 | |
| 688 | local_triggers = lttng_triggers_create(); |
| 689 | if (!local_triggers) { |
| 690 | ret = -1; |
| 691 | goto error; |
| 692 | } |
| 693 | |
| 694 | for (i = 0; i < triggers_comm->count; i++) { |
| 695 | struct lttng_trigger *trigger = NULL; |
| 696 | struct lttng_payload_view trigger_view = |
| 697 | lttng_payload_view_from_view(src_view, offset, -1); |
| 698 | ssize_t trigger_size; |
| 699 | |
| 700 | trigger_size = lttng_trigger_create_from_payload( |
| 701 | &trigger_view, &trigger); |
| 702 | if (trigger_size < 0) { |
| 703 | ret = trigger_size; |
| 704 | goto error; |
| 705 | } |
| 706 | |
| 707 | /* Transfer ownership of the trigger to the collection. */ |
| 708 | ret = lttng_triggers_add(local_triggers, trigger); |
| 709 | lttng_trigger_put(trigger); |
| 710 | if (ret < 0) { |
| 711 | ret = -1; |
| 712 | goto error; |
| 713 | } |
| 714 | |
| 715 | offset += trigger_size; |
| 716 | triggers_size += trigger_size; |
| 717 | } |
| 718 | |
| 719 | /* Unexpected size of inner-elements; the buffer is corrupted. */ |
| 720 | if ((ssize_t) triggers_comm->length != triggers_size) { |
| 721 | ret = -1; |
| 722 | goto error; |
| 723 | } |
| 724 | |
| 725 | /* Pass ownership to caller. */ |
| 726 | *triggers = local_triggers; |
| 727 | local_triggers = NULL; |
| 728 | |
| 729 | ret = offset; |
| 730 | error: |
| 731 | |
| 732 | lttng_triggers_destroy(local_triggers); |
| 733 | return ret; |
| 734 | } |
| 735 | |
| 736 | const struct lttng_credentials *lttng_trigger_get_credentials( |
| 737 | const struct lttng_trigger *trigger) |
| 738 | { |
| 739 | return &trigger->creds; |
| 740 | } |
| 741 | |
| 742 | void lttng_trigger_set_credentials(struct lttng_trigger *trigger, |
| 743 | const struct lttng_credentials *creds) |
| 744 | { |
| 745 | /* Triggers do not use the group id to authenticate the user. */ |
| 746 | LTTNG_ASSERT(creds); |
| 747 | LTTNG_OPTIONAL_SET(&trigger->creds.uid, LTTNG_OPTIONAL_GET(creds->uid)); |
| 748 | LTTNG_OPTIONAL_UNSET(&trigger->creds.gid); |
| 749 | } |
| 750 | |
| 751 | enum lttng_trigger_status lttng_trigger_set_owner_uid( |
| 752 | struct lttng_trigger *trigger, uid_t uid) |
| 753 | { |
| 754 | enum lttng_trigger_status ret = LTTNG_TRIGGER_STATUS_OK; |
| 755 | const uid_t euid = geteuid(); |
| 756 | const struct lttng_credentials creds = { |
| 757 | .uid = LTTNG_OPTIONAL_INIT_VALUE(uid), |
| 758 | .gid = LTTNG_OPTIONAL_INIT_UNSET, |
| 759 | }; |
| 760 | |
| 761 | if (!trigger) { |
| 762 | ret = LTTNG_TRIGGER_STATUS_INVALID; |
| 763 | goto end; |
| 764 | } |
| 765 | |
| 766 | /* Client-side validation only to report a clearer error. */ |
| 767 | if (euid != 0 && euid != uid) { |
| 768 | ret = LTTNG_TRIGGER_STATUS_PERMISSION_DENIED; |
| 769 | goto end; |
| 770 | } |
| 771 | |
| 772 | lttng_trigger_set_credentials(trigger, &creds); |
| 773 | |
| 774 | end: |
| 775 | return ret; |
| 776 | } |
| 777 | |
| 778 | enum lttng_trigger_status lttng_trigger_get_owner_uid( |
| 779 | const struct lttng_trigger *trigger, uid_t *uid) |
| 780 | { |
| 781 | enum lttng_trigger_status ret = LTTNG_TRIGGER_STATUS_OK; |
| 782 | const struct lttng_credentials *creds = NULL; |
| 783 | |
| 784 | if (!trigger || !uid ) { |
| 785 | ret = LTTNG_TRIGGER_STATUS_INVALID; |
| 786 | goto end; |
| 787 | } |
| 788 | |
| 789 | if (!trigger->creds.uid.is_set ) { |
| 790 | ret = LTTNG_TRIGGER_STATUS_UNSET; |
| 791 | goto end; |
| 792 | } |
| 793 | |
| 794 | creds = lttng_trigger_get_credentials(trigger); |
| 795 | *uid = lttng_credentials_get_uid(creds); |
| 796 | |
| 797 | end: |
| 798 | return ret; |
| 799 | } |
| 800 | |
| 801 | enum lttng_domain_type lttng_trigger_get_underlying_domain_type_restriction( |
| 802 | const struct lttng_trigger *trigger) |
| 803 | { |
| 804 | enum lttng_domain_type type = LTTNG_DOMAIN_NONE; |
| 805 | const struct lttng_event_rule *event_rule; |
| 806 | enum lttng_condition_status c_status; |
| 807 | enum lttng_condition_type c_type; |
| 808 | |
| 809 | LTTNG_ASSERT(trigger); |
| 810 | LTTNG_ASSERT(trigger->condition); |
| 811 | |
| 812 | c_type = lttng_condition_get_type(trigger->condition); |
| 813 | assert (c_type != LTTNG_CONDITION_TYPE_UNKNOWN); |
| 814 | |
| 815 | switch (c_type) { |
| 816 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: |
| 817 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: |
| 818 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: |
| 819 | /* Apply to any domain. */ |
| 820 | type = LTTNG_DOMAIN_NONE; |
| 821 | break; |
| 822 | case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES: |
| 823 | /* Return the domain of the event rule. */ |
| 824 | c_status = lttng_condition_event_rule_matches_get_rule( |
| 825 | trigger->condition, &event_rule); |
| 826 | LTTNG_ASSERT(c_status == LTTNG_CONDITION_STATUS_OK); |
| 827 | type = lttng_event_rule_get_domain_type(event_rule); |
| 828 | break; |
| 829 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: |
| 830 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: |
| 831 | /* Return the domain of the channel being monitored. */ |
| 832 | c_status = lttng_condition_buffer_usage_get_domain_type( |
| 833 | trigger->condition, &type); |
| 834 | LTTNG_ASSERT(c_status == LTTNG_CONDITION_STATUS_OK); |
| 835 | break; |
| 836 | default: |
| 837 | abort(); |
| 838 | } |
| 839 | |
| 840 | return type; |
| 841 | } |
| 842 | |
| 843 | /* |
| 844 | * Generate bytecode related to the trigger. |
| 845 | * On success LTTNG_OK. On error, returns lttng_error code. |
| 846 | */ |
| 847 | enum lttng_error_code lttng_trigger_generate_bytecode( |
| 848 | struct lttng_trigger *trigger, |
| 849 | const struct lttng_credentials *creds) |
| 850 | { |
| 851 | enum lttng_error_code ret; |
| 852 | struct lttng_condition *condition = NULL; |
| 853 | |
| 854 | condition = lttng_trigger_get_condition(trigger); |
| 855 | if (!condition) { |
| 856 | ret = LTTNG_ERR_INVALID_TRIGGER; |
| 857 | goto end; |
| 858 | } |
| 859 | |
| 860 | switch (lttng_condition_get_type(condition)) { |
| 861 | case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES: |
| 862 | { |
| 863 | struct lttng_event_rule *event_rule; |
| 864 | const enum lttng_condition_status condition_status = |
| 865 | lttng_condition_event_rule_matches_borrow_rule_mutable( |
| 866 | condition, &event_rule); |
| 867 | |
| 868 | LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK); |
| 869 | |
| 870 | /* Generate the filter bytecode. */ |
| 871 | ret = lttng_event_rule_generate_filter_bytecode( |
| 872 | event_rule, creds); |
| 873 | if (ret != LTTNG_OK) { |
| 874 | goto end; |
| 875 | } |
| 876 | |
| 877 | /* Generate the capture bytecode. */ |
| 878 | ret = lttng_condition_event_rule_matches_generate_capture_descriptor_bytecode( |
| 879 | condition); |
| 880 | if (ret != LTTNG_OK) { |
| 881 | goto end; |
| 882 | } |
| 883 | |
| 884 | ret = LTTNG_OK; |
| 885 | break; |
| 886 | } |
| 887 | default: |
| 888 | ret = LTTNG_OK; |
| 889 | break; |
| 890 | } |
| 891 | end: |
| 892 | return ret; |
| 893 | } |
| 894 | |
| 895 | struct lttng_trigger *lttng_trigger_copy(const struct lttng_trigger *trigger) |
| 896 | { |
| 897 | int ret; |
| 898 | struct lttng_payload copy_buffer; |
| 899 | struct lttng_condition *condition_copy = NULL; |
| 900 | struct lttng_action *action_copy = NULL; |
| 901 | struct lttng_trigger *copy = NULL; |
| 902 | enum lttng_trigger_status trigger_status; |
| 903 | const char *trigger_name; |
| 904 | uid_t trigger_owner_uid; |
| 905 | |
| 906 | lttng_payload_init(©_buffer); |
| 907 | |
| 908 | ret = lttng_condition_serialize(trigger->condition, ©_buffer); |
| 909 | if (ret < 0) { |
| 910 | goto end; |
| 911 | } |
| 912 | |
| 913 | { |
| 914 | struct lttng_payload_view view = |
| 915 | lttng_payload_view_from_payload( |
| 916 | ©_buffer, 0, -1); |
| 917 | |
| 918 | ret = lttng_condition_create_from_payload( |
| 919 | &view, &condition_copy); |
| 920 | if (ret < 0) { |
| 921 | goto end; |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | lttng_payload_clear(©_buffer); |
| 926 | |
| 927 | ret = lttng_action_serialize(trigger->action, ©_buffer); |
| 928 | if (ret < 0) { |
| 929 | goto end; |
| 930 | } |
| 931 | |
| 932 | { |
| 933 | struct lttng_payload_view view = |
| 934 | lttng_payload_view_from_payload( |
| 935 | ©_buffer, 0, -1); |
| 936 | |
| 937 | ret = lttng_action_create_from_payload( |
| 938 | &view, &action_copy); |
| 939 | if (ret < 0) { |
| 940 | goto end; |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | copy = lttng_trigger_create(condition_copy, action_copy); |
| 945 | if (!copy) { |
| 946 | ERR("Failed to allocate trigger during trigger copy"); |
| 947 | goto end; |
| 948 | } |
| 949 | |
| 950 | trigger_status = lttng_trigger_get_name(trigger, &trigger_name); |
| 951 | switch (trigger_status) { |
| 952 | case LTTNG_TRIGGER_STATUS_OK: |
| 953 | trigger_status = lttng_trigger_set_name(copy, trigger_name); |
| 954 | if (trigger_status != LTTNG_TRIGGER_STATUS_OK) { |
| 955 | ERR("Failed to set name of new trigger during copy"); |
| 956 | goto error_cleanup_trigger; |
| 957 | } |
| 958 | break; |
| 959 | case LTTNG_TRIGGER_STATUS_UNSET: |
| 960 | break; |
| 961 | default: |
| 962 | ERR("Failed to get name of original trigger during copy"); |
| 963 | goto error_cleanup_trigger; |
| 964 | } |
| 965 | |
| 966 | trigger_status = lttng_trigger_get_owner_uid( |
| 967 | trigger, &trigger_owner_uid); |
| 968 | switch (trigger_status) { |
| 969 | case LTTNG_TRIGGER_STATUS_OK: |
| 970 | LTTNG_OPTIONAL_SET(©->creds.uid, trigger_owner_uid); |
| 971 | break; |
| 972 | case LTTNG_TRIGGER_STATUS_UNSET: |
| 973 | break; |
| 974 | default: |
| 975 | ERR("Failed to get owner uid of original trigger during copy"); |
| 976 | goto error_cleanup_trigger; |
| 977 | } |
| 978 | |
| 979 | copy->tracer_token = trigger->tracer_token; |
| 980 | copy->registered = trigger->registered; |
| 981 | copy->is_hidden = trigger->is_hidden; |
| 982 | goto end; |
| 983 | |
| 984 | error_cleanup_trigger: |
| 985 | lttng_trigger_destroy(copy); |
| 986 | copy = NULL; |
| 987 | end: |
| 988 | lttng_condition_put(condition_copy); |
| 989 | lttng_action_put(action_copy); |
| 990 | lttng_payload_reset(©_buffer); |
| 991 | return copy; |
| 992 | } |
| 993 | |
| 994 | bool lttng_trigger_needs_tracer_notifier(const struct lttng_trigger *trigger) |
| 995 | { |
| 996 | bool needs_tracer_notifier = false; |
| 997 | const struct lttng_condition *condition = |
| 998 | lttng_trigger_get_const_condition(trigger); |
| 999 | |
| 1000 | switch (lttng_condition_get_type(condition)) { |
| 1001 | case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES: |
| 1002 | needs_tracer_notifier = true; |
| 1003 | goto end; |
| 1004 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: |
| 1005 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: |
| 1006 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: |
| 1007 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: |
| 1008 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: |
| 1009 | goto end; |
| 1010 | case LTTNG_CONDITION_TYPE_UNKNOWN: |
| 1011 | default: |
| 1012 | abort(); |
| 1013 | } |
| 1014 | end: |
| 1015 | return needs_tracer_notifier; |
| 1016 | } |
| 1017 | |
| 1018 | void lttng_trigger_set_as_registered(struct lttng_trigger *trigger) |
| 1019 | { |
| 1020 | pthread_mutex_lock(&trigger->lock); |
| 1021 | trigger->registered = true; |
| 1022 | pthread_mutex_unlock(&trigger->lock); |
| 1023 | } |
| 1024 | |
| 1025 | void lttng_trigger_set_as_unregistered(struct lttng_trigger *trigger) |
| 1026 | { |
| 1027 | pthread_mutex_lock(&trigger->lock); |
| 1028 | trigger->registered = false; |
| 1029 | pthread_mutex_unlock(&trigger->lock); |
| 1030 | } |
| 1031 | |
| 1032 | /* |
| 1033 | * The trigger must be locked before calling lttng_trigger_registered. |
| 1034 | * The lock is necessary since a trigger can be unregistered at anytime. |
| 1035 | * Manipulations requiring that the trigger be registered must always acquire |
| 1036 | * the trigger lock for the duration of the manipulation using |
| 1037 | * `lttng_trigger_lock` and `lttng_trigger_unlock`. |
| 1038 | */ |
| 1039 | bool lttng_trigger_is_registered(struct lttng_trigger *trigger) |
| 1040 | { |
| 1041 | ASSERT_LOCKED(trigger->lock); |
| 1042 | return trigger->registered; |
| 1043 | } |
| 1044 | |
| 1045 | void lttng_trigger_lock(struct lttng_trigger *trigger) |
| 1046 | { |
| 1047 | pthread_mutex_lock(&trigger->lock); |
| 1048 | } |
| 1049 | |
| 1050 | void lttng_trigger_unlock(struct lttng_trigger *trigger) |
| 1051 | { |
| 1052 | pthread_mutex_unlock(&trigger->lock); |
| 1053 | } |
| 1054 | |
| 1055 | enum lttng_error_code lttng_trigger_mi_serialize(const struct lttng_trigger *trigger, |
| 1056 | struct mi_writer *writer, |
| 1057 | const struct mi_lttng_error_query_callbacks |
| 1058 | *error_query_callbacks) |
| 1059 | { |
| 1060 | int ret; |
| 1061 | enum lttng_error_code ret_code; |
| 1062 | enum lttng_trigger_status trigger_status; |
| 1063 | const struct lttng_condition *condition = NULL; |
| 1064 | const struct lttng_action *action = NULL; |
| 1065 | struct lttng_dynamic_array action_path_indexes; |
| 1066 | uid_t owner_uid; |
| 1067 | |
| 1068 | LTTNG_ASSERT(trigger); |
| 1069 | LTTNG_ASSERT(writer); |
| 1070 | |
| 1071 | lttng_dynamic_array_init(&action_path_indexes, sizeof(uint64_t), NULL); |
| 1072 | |
| 1073 | /* Open trigger element. */ |
| 1074 | ret = mi_lttng_writer_open_element(writer, mi_lttng_element_trigger); |
| 1075 | if (ret) { |
| 1076 | goto mi_error; |
| 1077 | } |
| 1078 | |
| 1079 | trigger_status = lttng_trigger_get_owner_uid(trigger, &owner_uid); |
| 1080 | LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK); |
| 1081 | |
| 1082 | /* Name. */ |
| 1083 | ret = mi_lttng_writer_write_element_string( |
| 1084 | writer, config_element_name, trigger->name); |
| 1085 | if (ret) { |
| 1086 | goto mi_error; |
| 1087 | } |
| 1088 | |
| 1089 | /* Owner uid. */ |
| 1090 | ret = mi_lttng_writer_write_element_signed_int(writer, |
| 1091 | mi_lttng_element_trigger_owner_uid, |
| 1092 | (int64_t) owner_uid); |
| 1093 | if (ret) { |
| 1094 | goto mi_error; |
| 1095 | } |
| 1096 | |
| 1097 | /* Condition. */ |
| 1098 | condition = lttng_trigger_get_const_condition(trigger); |
| 1099 | LTTNG_ASSERT(condition); |
| 1100 | ret_code = lttng_condition_mi_serialize( |
| 1101 | trigger, condition, writer, error_query_callbacks); |
| 1102 | if (ret_code != LTTNG_OK) { |
| 1103 | goto end; |
| 1104 | } |
| 1105 | |
| 1106 | /* Action. */ |
| 1107 | action = lttng_trigger_get_const_action(trigger); |
| 1108 | LTTNG_ASSERT(action); |
| 1109 | ret_code = lttng_action_mi_serialize(trigger, action, writer, |
| 1110 | error_query_callbacks, &action_path_indexes); |
| 1111 | if (ret_code != LTTNG_OK) { |
| 1112 | goto end; |
| 1113 | } |
| 1114 | |
| 1115 | if (error_query_callbacks && error_query_callbacks->trigger_cb) { |
| 1116 | struct lttng_error_query_results *results = NULL; |
| 1117 | |
| 1118 | ret_code = error_query_callbacks->trigger_cb(trigger, &results); |
| 1119 | if (ret_code != LTTNG_OK) { |
| 1120 | goto end; |
| 1121 | } |
| 1122 | |
| 1123 | ret_code = lttng_error_query_results_mi_serialize( |
| 1124 | results, writer); |
| 1125 | lttng_error_query_results_destroy(results); |
| 1126 | if (ret_code != LTTNG_OK) { |
| 1127 | goto end; |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | /* Close trigger element. */ |
| 1132 | ret = mi_lttng_writer_close_element(writer); |
| 1133 | if (ret) { |
| 1134 | goto mi_error; |
| 1135 | } |
| 1136 | |
| 1137 | ret_code = LTTNG_OK; |
| 1138 | goto end; |
| 1139 | |
| 1140 | mi_error: |
| 1141 | ret_code = LTTNG_ERR_MI_IO_FAIL; |
| 1142 | end: |
| 1143 | lttng_dynamic_array_reset(&action_path_indexes); |
| 1144 | return ret_code; |
| 1145 | } |
| 1146 | |
| 1147 | /* Used by qsort, which expects the semantics of strcmp(). */ |
| 1148 | static int compare_triggers_by_name(const void *a, const void *b) |
| 1149 | { |
| 1150 | const struct lttng_trigger *trigger_a = |
| 1151 | *((const struct lttng_trigger **) a); |
| 1152 | const struct lttng_trigger *trigger_b = |
| 1153 | *((const struct lttng_trigger **) b); |
| 1154 | const char *name_a, *name_b; |
| 1155 | enum lttng_trigger_status trigger_status; |
| 1156 | |
| 1157 | /* Anonymous triggers are not reachable here. */ |
| 1158 | trigger_status = lttng_trigger_get_name(trigger_a, &name_a); |
| 1159 | LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK); |
| 1160 | |
| 1161 | trigger_status = lttng_trigger_get_name(trigger_b, &name_b); |
| 1162 | LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK); |
| 1163 | |
| 1164 | return strcmp(name_a, name_b); |
| 1165 | } |
| 1166 | |
| 1167 | enum lttng_error_code lttng_triggers_mi_serialize(const struct lttng_triggers *triggers, |
| 1168 | struct mi_writer *writer, |
| 1169 | const struct mi_lttng_error_query_callbacks |
| 1170 | *error_query_callbacks) |
| 1171 | { |
| 1172 | int ret; |
| 1173 | enum lttng_error_code ret_code; |
| 1174 | enum lttng_trigger_status status; |
| 1175 | unsigned int count, i; |
| 1176 | struct lttng_dynamic_pointer_array sorted_triggers; |
| 1177 | |
| 1178 | LTTNG_ASSERT(triggers); |
| 1179 | LTTNG_ASSERT(writer); |
| 1180 | |
| 1181 | /* |
| 1182 | * Sort trigger by name to ensure an order at the MI level and ignore |
| 1183 | * any anonymous trigger present. |
| 1184 | */ |
| 1185 | lttng_dynamic_pointer_array_init(&sorted_triggers, NULL); |
| 1186 | |
| 1187 | status = lttng_triggers_get_count(triggers, &count); |
| 1188 | LTTNG_ASSERT(status == LTTNG_TRIGGER_STATUS_OK); |
| 1189 | |
| 1190 | for (i = 0; i < count; i++) { |
| 1191 | int add_ret; |
| 1192 | const char *unused_name; |
| 1193 | const struct lttng_trigger *trigger = |
| 1194 | lttng_triggers_get_at_index(triggers, i); |
| 1195 | |
| 1196 | status = lttng_trigger_get_name(trigger, &unused_name); |
| 1197 | switch (status) { |
| 1198 | case LTTNG_TRIGGER_STATUS_OK: |
| 1199 | break; |
| 1200 | case LTTNG_TRIGGER_STATUS_UNSET: |
| 1201 | /* Don't list anonymous triggers. */ |
| 1202 | continue; |
| 1203 | default: |
| 1204 | abort(); |
| 1205 | } |
| 1206 | |
| 1207 | add_ret = lttng_dynamic_pointer_array_add_pointer( |
| 1208 | &sorted_triggers, (void *) trigger); |
| 1209 | |
| 1210 | if (add_ret) { |
| 1211 | ERR("Failed to lttng_trigger to sorting array."); |
| 1212 | ret_code = LTTNG_ERR_NOMEM; |
| 1213 | goto error; |
| 1214 | } |
| 1215 | } |
| 1216 | |
| 1217 | qsort(sorted_triggers.array.buffer.data, count, |
| 1218 | sizeof(struct lttng_trigger *), |
| 1219 | compare_triggers_by_name); |
| 1220 | |
| 1221 | /* Open triggers element. */ |
| 1222 | ret = mi_lttng_writer_open_element(writer, mi_lttng_element_triggers); |
| 1223 | if (ret) { |
| 1224 | ret_code = LTTNG_ERR_MI_IO_FAIL; |
| 1225 | goto error; |
| 1226 | } |
| 1227 | |
| 1228 | for (i = 0; i < lttng_dynamic_pointer_array_get_count(&sorted_triggers); i++) { |
| 1229 | const struct lttng_trigger *trigger = |
| 1230 | (const struct lttng_trigger *) |
| 1231 | lttng_dynamic_pointer_array_get_pointer( |
| 1232 | &sorted_triggers, i); |
| 1233 | |
| 1234 | lttng_trigger_mi_serialize(trigger, writer, error_query_callbacks); |
| 1235 | } |
| 1236 | |
| 1237 | /* Close triggers element. */ |
| 1238 | ret = mi_lttng_writer_close_element(writer); |
| 1239 | if (ret) { |
| 1240 | ret_code = LTTNG_ERR_MI_IO_FAIL; |
| 1241 | goto error; |
| 1242 | } |
| 1243 | |
| 1244 | ret_code = LTTNG_OK; |
| 1245 | |
| 1246 | error: |
| 1247 | lttng_dynamic_pointer_array_reset(&sorted_triggers); |
| 1248 | return ret_code; |
| 1249 | } |