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