| 1 | /* |
| 2 | * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include "lttng/action/action.h" |
| 9 | #include "lttng/trigger/trigger-internal.h" |
| 10 | #define _LGPL_SOURCE |
| 11 | #include <urcu.h> |
| 12 | #include <urcu/rculfhash.h> |
| 13 | |
| 14 | #include <common/defaults.h> |
| 15 | #include <common/error.h> |
| 16 | #include <common/futex.h> |
| 17 | #include <common/unix.h> |
| 18 | #include <common/dynamic-buffer.h> |
| 19 | #include <common/hashtable/utils.h> |
| 20 | #include <common/sessiond-comm/sessiond-comm.h> |
| 21 | #include <common/macros.h> |
| 22 | #include <lttng/condition/condition.h> |
| 23 | #include <lttng/action/action-internal.h> |
| 24 | #include <lttng/action/group-internal.h> |
| 25 | #include <lttng/domain-internal.h> |
| 26 | #include <lttng/notification/notification-internal.h> |
| 27 | #include <lttng/condition/condition-internal.h> |
| 28 | #include <lttng/condition/buffer-usage-internal.h> |
| 29 | #include <lttng/condition/session-consumed-size-internal.h> |
| 30 | #include <lttng/condition/session-rotation-internal.h> |
| 31 | #include <lttng/condition/on-event-internal.h> |
| 32 | #include <lttng/domain-internal.h> |
| 33 | #include <lttng/notification/channel-internal.h> |
| 34 | #include <lttng/trigger/trigger-internal.h> |
| 35 | #include <lttng/event-rule/event-rule-internal.h> |
| 36 | |
| 37 | #include <time.h> |
| 38 | #include <unistd.h> |
| 39 | #include <assert.h> |
| 40 | #include <inttypes.h> |
| 41 | #include <fcntl.h> |
| 42 | |
| 43 | #include "condition-internal.h" |
| 44 | #include "event-notifier-error-accounting.h" |
| 45 | #include "notification-thread.h" |
| 46 | #include "notification-thread-events.h" |
| 47 | #include "notification-thread-commands.h" |
| 48 | #include "lttng-sessiond.h" |
| 49 | #include "kernel.h" |
| 50 | |
| 51 | #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP) |
| 52 | #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT) |
| 53 | |
| 54 | /* The tracers currently limit the capture size to PIPE_BUF (4kb on linux). */ |
| 55 | #define MAX_CAPTURE_SIZE (PIPE_BUF) |
| 56 | |
| 57 | enum lttng_object_type { |
| 58 | LTTNG_OBJECT_TYPE_UNKNOWN, |
| 59 | LTTNG_OBJECT_TYPE_NONE, |
| 60 | LTTNG_OBJECT_TYPE_CHANNEL, |
| 61 | LTTNG_OBJECT_TYPE_SESSION, |
| 62 | }; |
| 63 | |
| 64 | struct lttng_trigger_list_element { |
| 65 | /* No ownership of the trigger object is assumed. */ |
| 66 | struct lttng_trigger *trigger; |
| 67 | struct cds_list_head node; |
| 68 | }; |
| 69 | |
| 70 | struct lttng_channel_trigger_list { |
| 71 | struct channel_key channel_key; |
| 72 | /* List of struct lttng_trigger_list_element. */ |
| 73 | struct cds_list_head list; |
| 74 | /* Node in the channel_triggers_ht */ |
| 75 | struct cds_lfht_node channel_triggers_ht_node; |
| 76 | /* call_rcu delayed reclaim. */ |
| 77 | struct rcu_head rcu_node; |
| 78 | }; |
| 79 | |
| 80 | /* |
| 81 | * List of triggers applying to a given session. |
| 82 | * |
| 83 | * See: |
| 84 | * - lttng_session_trigger_list_create() |
| 85 | * - lttng_session_trigger_list_build() |
| 86 | * - lttng_session_trigger_list_destroy() |
| 87 | * - lttng_session_trigger_list_add() |
| 88 | */ |
| 89 | struct lttng_session_trigger_list { |
| 90 | /* |
| 91 | * Not owned by this; points to the session_info structure's |
| 92 | * session name. |
| 93 | */ |
| 94 | const char *session_name; |
| 95 | /* List of struct lttng_trigger_list_element. */ |
| 96 | struct cds_list_head list; |
| 97 | /* Node in the session_triggers_ht */ |
| 98 | struct cds_lfht_node session_triggers_ht_node; |
| 99 | /* |
| 100 | * Weak reference to the notification system's session triggers |
| 101 | * hashtable. |
| 102 | * |
| 103 | * The session trigger list structure structure is owned by |
| 104 | * the session's session_info. |
| 105 | * |
| 106 | * The session_info is kept alive the the channel_infos holding a |
| 107 | * reference to it (reference counting). When those channels are |
| 108 | * destroyed (at runtime or on teardown), the reference they hold |
| 109 | * to the session_info are released. On destruction of session_info, |
| 110 | * session_info_destroy() will remove the list of triggers applying |
| 111 | * to this session from the notification system's state. |
| 112 | * |
| 113 | * This implies that the session_triggers_ht must be destroyed |
| 114 | * after the channels. |
| 115 | */ |
| 116 | struct cds_lfht *session_triggers_ht; |
| 117 | /* Used for delayed RCU reclaim. */ |
| 118 | struct rcu_head rcu_node; |
| 119 | }; |
| 120 | |
| 121 | struct lttng_trigger_ht_element { |
| 122 | struct lttng_trigger *trigger; |
| 123 | struct cds_lfht_node node; |
| 124 | struct cds_lfht_node node_by_name_uid; |
| 125 | /* call_rcu delayed reclaim. */ |
| 126 | struct rcu_head rcu_node; |
| 127 | }; |
| 128 | |
| 129 | struct lttng_condition_list_element { |
| 130 | struct lttng_condition *condition; |
| 131 | struct cds_list_head node; |
| 132 | }; |
| 133 | |
| 134 | struct channel_state_sample { |
| 135 | struct channel_key key; |
| 136 | struct cds_lfht_node channel_state_ht_node; |
| 137 | uint64_t highest_usage; |
| 138 | uint64_t lowest_usage; |
| 139 | uint64_t channel_total_consumed; |
| 140 | /* call_rcu delayed reclaim. */ |
| 141 | struct rcu_head rcu_node; |
| 142 | }; |
| 143 | |
| 144 | static unsigned long hash_channel_key(struct channel_key *key); |
| 145 | static int evaluate_buffer_condition(const struct lttng_condition *condition, |
| 146 | struct lttng_evaluation **evaluation, |
| 147 | const struct notification_thread_state *state, |
| 148 | const struct channel_state_sample *previous_sample, |
| 149 | const struct channel_state_sample *latest_sample, |
| 150 | uint64_t previous_session_consumed_total, |
| 151 | uint64_t latest_session_consumed_total, |
| 152 | struct channel_info *channel_info); |
| 153 | static |
| 154 | int send_evaluation_to_clients(const struct lttng_trigger *trigger, |
| 155 | const struct lttng_evaluation *evaluation, |
| 156 | struct notification_client_list *client_list, |
| 157 | struct notification_thread_state *state, |
| 158 | uid_t channel_uid, gid_t channel_gid); |
| 159 | |
| 160 | |
| 161 | /* session_info API */ |
| 162 | static |
| 163 | void session_info_destroy(void *_data); |
| 164 | static |
| 165 | void session_info_get(struct session_info *session_info); |
| 166 | static |
| 167 | void session_info_put(struct session_info *session_info); |
| 168 | static |
| 169 | struct session_info *session_info_create(const char *name, |
| 170 | uid_t uid, gid_t gid, |
| 171 | struct lttng_session_trigger_list *trigger_list, |
| 172 | struct cds_lfht *sessions_ht); |
| 173 | static |
| 174 | void session_info_add_channel(struct session_info *session_info, |
| 175 | struct channel_info *channel_info); |
| 176 | static |
| 177 | void session_info_remove_channel(struct session_info *session_info, |
| 178 | struct channel_info *channel_info); |
| 179 | |
| 180 | /* lttng_session_trigger_list API */ |
| 181 | static |
| 182 | struct lttng_session_trigger_list *lttng_session_trigger_list_create( |
| 183 | const char *session_name, |
| 184 | struct cds_lfht *session_triggers_ht); |
| 185 | static |
| 186 | struct lttng_session_trigger_list *lttng_session_trigger_list_build( |
| 187 | const struct notification_thread_state *state, |
| 188 | const char *session_name); |
| 189 | static |
| 190 | void lttng_session_trigger_list_destroy( |
| 191 | struct lttng_session_trigger_list *list); |
| 192 | static |
| 193 | int lttng_session_trigger_list_add(struct lttng_session_trigger_list *list, |
| 194 | struct lttng_trigger *trigger); |
| 195 | |
| 196 | static |
| 197 | int client_handle_transmission_status( |
| 198 | struct notification_client *client, |
| 199 | enum client_transmission_status transmission_status, |
| 200 | struct notification_thread_state *state); |
| 201 | |
| 202 | static |
| 203 | int handle_one_event_notifier_notification( |
| 204 | struct notification_thread_state *state, |
| 205 | int pipe, enum lttng_domain_type domain); |
| 206 | |
| 207 | static |
| 208 | void free_lttng_trigger_ht_element_rcu(struct rcu_head *node); |
| 209 | |
| 210 | static |
| 211 | int match_client_socket(struct cds_lfht_node *node, const void *key) |
| 212 | { |
| 213 | /* This double-cast is intended to supress pointer-to-cast warning. */ |
| 214 | const int socket = (int) (intptr_t) key; |
| 215 | const struct notification_client *client = caa_container_of(node, |
| 216 | struct notification_client, client_socket_ht_node); |
| 217 | |
| 218 | return client->socket == socket; |
| 219 | } |
| 220 | |
| 221 | static |
| 222 | int match_client_id(struct cds_lfht_node *node, const void *key) |
| 223 | { |
| 224 | /* This double-cast is intended to supress pointer-to-cast warning. */ |
| 225 | const notification_client_id id = *((notification_client_id *) key); |
| 226 | const struct notification_client *client = caa_container_of( |
| 227 | node, struct notification_client, client_id_ht_node); |
| 228 | |
| 229 | return client->id == id; |
| 230 | } |
| 231 | |
| 232 | static |
| 233 | int match_channel_trigger_list(struct cds_lfht_node *node, const void *key) |
| 234 | { |
| 235 | struct channel_key *channel_key = (struct channel_key *) key; |
| 236 | struct lttng_channel_trigger_list *trigger_list; |
| 237 | |
| 238 | trigger_list = caa_container_of(node, struct lttng_channel_trigger_list, |
| 239 | channel_triggers_ht_node); |
| 240 | |
| 241 | return !!((channel_key->key == trigger_list->channel_key.key) && |
| 242 | (channel_key->domain == trigger_list->channel_key.domain)); |
| 243 | } |
| 244 | |
| 245 | static |
| 246 | int match_session_trigger_list(struct cds_lfht_node *node, const void *key) |
| 247 | { |
| 248 | const char *session_name = (const char *) key; |
| 249 | struct lttng_session_trigger_list *trigger_list; |
| 250 | |
| 251 | trigger_list = caa_container_of(node, struct lttng_session_trigger_list, |
| 252 | session_triggers_ht_node); |
| 253 | |
| 254 | return !!(strcmp(trigger_list->session_name, session_name) == 0); |
| 255 | } |
| 256 | |
| 257 | static |
| 258 | int match_channel_state_sample(struct cds_lfht_node *node, const void *key) |
| 259 | { |
| 260 | struct channel_key *channel_key = (struct channel_key *) key; |
| 261 | struct channel_state_sample *sample; |
| 262 | |
| 263 | sample = caa_container_of(node, struct channel_state_sample, |
| 264 | channel_state_ht_node); |
| 265 | |
| 266 | return !!((channel_key->key == sample->key.key) && |
| 267 | (channel_key->domain == sample->key.domain)); |
| 268 | } |
| 269 | |
| 270 | static |
| 271 | int match_channel_info(struct cds_lfht_node *node, const void *key) |
| 272 | { |
| 273 | struct channel_key *channel_key = (struct channel_key *) key; |
| 274 | struct channel_info *channel_info; |
| 275 | |
| 276 | channel_info = caa_container_of(node, struct channel_info, |
| 277 | channels_ht_node); |
| 278 | |
| 279 | return !!((channel_key->key == channel_info->key.key) && |
| 280 | (channel_key->domain == channel_info->key.domain)); |
| 281 | } |
| 282 | |
| 283 | static |
| 284 | int match_trigger(struct cds_lfht_node *node, const void *key) |
| 285 | { |
| 286 | struct lttng_trigger *trigger_key = (struct lttng_trigger *) key; |
| 287 | struct lttng_trigger_ht_element *trigger_ht_element; |
| 288 | |
| 289 | trigger_ht_element = caa_container_of(node, struct lttng_trigger_ht_element, |
| 290 | node); |
| 291 | |
| 292 | return !!lttng_trigger_is_equal(trigger_key, trigger_ht_element->trigger); |
| 293 | } |
| 294 | |
| 295 | static |
| 296 | int match_trigger_token(struct cds_lfht_node *node, const void *key) |
| 297 | { |
| 298 | const uint64_t *_key = key; |
| 299 | struct notification_trigger_tokens_ht_element *element; |
| 300 | |
| 301 | element = caa_container_of(node, |
| 302 | struct notification_trigger_tokens_ht_element, node); |
| 303 | return *_key == element->token; |
| 304 | } |
| 305 | |
| 306 | static |
| 307 | int match_client_list_condition(struct cds_lfht_node *node, const void *key) |
| 308 | { |
| 309 | struct lttng_condition *condition_key = (struct lttng_condition *) key; |
| 310 | struct notification_client_list *client_list; |
| 311 | const struct lttng_condition *condition; |
| 312 | |
| 313 | assert(condition_key); |
| 314 | |
| 315 | client_list = caa_container_of(node, struct notification_client_list, |
| 316 | notification_trigger_clients_ht_node); |
| 317 | condition = lttng_trigger_get_const_condition(client_list->trigger); |
| 318 | |
| 319 | return !!lttng_condition_is_equal(condition_key, condition); |
| 320 | } |
| 321 | |
| 322 | static |
| 323 | int match_session(struct cds_lfht_node *node, const void *key) |
| 324 | { |
| 325 | const char *name = key; |
| 326 | struct session_info *session_info = caa_container_of( |
| 327 | node, struct session_info, sessions_ht_node); |
| 328 | |
| 329 | return !strcmp(session_info->name, name); |
| 330 | } |
| 331 | |
| 332 | static |
| 333 | const char *notification_command_type_str( |
| 334 | enum notification_thread_command_type type) |
| 335 | { |
| 336 | switch (type) { |
| 337 | case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER: |
| 338 | return "REGISTER_TRIGGER"; |
| 339 | case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER: |
| 340 | return "UNREGISTER_TRIGGER"; |
| 341 | case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL: |
| 342 | return "ADD_CHANNEL"; |
| 343 | case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL: |
| 344 | return "REMOVE_CHANNEL"; |
| 345 | case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING: |
| 346 | return "SESSION_ROTATION_ONGOING"; |
| 347 | case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED: |
| 348 | return "SESSION_ROTATION_COMPLETED"; |
| 349 | case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE: |
| 350 | return "ADD_TRACER_EVENT_SOURCE"; |
| 351 | case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE: |
| 352 | return "REMOVE_TRACER_EVENT_SOURCE"; |
| 353 | case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS: |
| 354 | return "LIST_TRIGGERS"; |
| 355 | case NOTIFICATION_COMMAND_TYPE_QUIT: |
| 356 | return "QUIT"; |
| 357 | case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE: |
| 358 | return "CLIENT_COMMUNICATION_UPDATE"; |
| 359 | default: |
| 360 | abort(); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | /* |
| 365 | * Match trigger based on name and credentials only. |
| 366 | * Name duplication is NOT allowed for the same uid. |
| 367 | */ |
| 368 | static |
| 369 | int match_trigger_by_name_uid(struct cds_lfht_node *node, |
| 370 | const void *key) |
| 371 | { |
| 372 | bool match = false; |
| 373 | const char *name; |
| 374 | const char *key_name; |
| 375 | enum lttng_trigger_status status; |
| 376 | const struct lttng_credentials *key_creds; |
| 377 | const struct lttng_credentials *node_creds; |
| 378 | const struct lttng_trigger *trigger_key = |
| 379 | (const struct lttng_trigger *) key; |
| 380 | const struct lttng_trigger_ht_element *trigger_ht_element = |
| 381 | caa_container_of(node, |
| 382 | struct lttng_trigger_ht_element, |
| 383 | node_by_name_uid); |
| 384 | |
| 385 | status = lttng_trigger_get_name(trigger_ht_element->trigger, &name); |
| 386 | assert(status == LTTNG_TRIGGER_STATUS_OK); |
| 387 | |
| 388 | status = lttng_trigger_get_name(trigger_key, &key_name); |
| 389 | assert(status == LTTNG_TRIGGER_STATUS_OK); |
| 390 | |
| 391 | /* Compare the names. */ |
| 392 | if (strcmp(name, key_name) != 0) { |
| 393 | goto end; |
| 394 | } |
| 395 | |
| 396 | /* Compare the owners' UIDs. */ |
| 397 | key_creds = lttng_trigger_get_credentials(trigger_key); |
| 398 | node_creds = lttng_trigger_get_credentials(trigger_ht_element->trigger); |
| 399 | |
| 400 | match = lttng_credentials_is_equal_uid(key_creds, node_creds); |
| 401 | |
| 402 | end: |
| 403 | return match; |
| 404 | } |
| 405 | |
| 406 | /* |
| 407 | * Hash trigger based on name and credentials only. |
| 408 | */ |
| 409 | static |
| 410 | unsigned long hash_trigger_by_name_uid(const struct lttng_trigger *trigger) |
| 411 | { |
| 412 | unsigned long hash = 0; |
| 413 | const struct lttng_credentials *trigger_creds; |
| 414 | const char *trigger_name; |
| 415 | enum lttng_trigger_status status; |
| 416 | |
| 417 | status = lttng_trigger_get_name(trigger, &trigger_name); |
| 418 | if (status == LTTNG_TRIGGER_STATUS_OK) { |
| 419 | hash = hash_key_str(trigger_name, lttng_ht_seed); |
| 420 | } |
| 421 | |
| 422 | trigger_creds = lttng_trigger_get_credentials(trigger); |
| 423 | hash ^= hash_key_ulong((void *) (unsigned long) LTTNG_OPTIONAL_GET(trigger_creds->uid), |
| 424 | lttng_ht_seed); |
| 425 | |
| 426 | return hash; |
| 427 | } |
| 428 | |
| 429 | static |
| 430 | unsigned long hash_channel_key(struct channel_key *key) |
| 431 | { |
| 432 | unsigned long key_hash = hash_key_u64(&key->key, lttng_ht_seed); |
| 433 | unsigned long domain_hash = hash_key_ulong( |
| 434 | (void *) (unsigned long) key->domain, lttng_ht_seed); |
| 435 | |
| 436 | return key_hash ^ domain_hash; |
| 437 | } |
| 438 | |
| 439 | static |
| 440 | unsigned long hash_client_socket(int socket) |
| 441 | { |
| 442 | return hash_key_ulong((void *) (unsigned long) socket, lttng_ht_seed); |
| 443 | } |
| 444 | |
| 445 | static |
| 446 | unsigned long hash_client_id(notification_client_id id) |
| 447 | { |
| 448 | return hash_key_u64(&id, lttng_ht_seed); |
| 449 | } |
| 450 | |
| 451 | /* |
| 452 | * Get the type of object to which a given condition applies. Bindings let |
| 453 | * the notification system evaluate a trigger's condition when a given |
| 454 | * object's state is updated. |
| 455 | * |
| 456 | * For instance, a condition bound to a channel will be evaluated everytime |
| 457 | * the channel's state is changed by a channel monitoring sample. |
| 458 | */ |
| 459 | static |
| 460 | enum lttng_object_type get_condition_binding_object( |
| 461 | const struct lttng_condition *condition) |
| 462 | { |
| 463 | switch (lttng_condition_get_type(condition)) { |
| 464 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: |
| 465 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: |
| 466 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: |
| 467 | return LTTNG_OBJECT_TYPE_CHANNEL; |
| 468 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: |
| 469 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: |
| 470 | return LTTNG_OBJECT_TYPE_SESSION; |
| 471 | case LTTNG_CONDITION_TYPE_ON_EVENT: |
| 472 | return LTTNG_OBJECT_TYPE_NONE; |
| 473 | default: |
| 474 | return LTTNG_OBJECT_TYPE_UNKNOWN; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | static |
| 479 | void free_channel_info_rcu(struct rcu_head *node) |
| 480 | { |
| 481 | free(caa_container_of(node, struct channel_info, rcu_node)); |
| 482 | } |
| 483 | |
| 484 | static |
| 485 | void channel_info_destroy(struct channel_info *channel_info) |
| 486 | { |
| 487 | if (!channel_info) { |
| 488 | return; |
| 489 | } |
| 490 | |
| 491 | if (channel_info->session_info) { |
| 492 | session_info_remove_channel(channel_info->session_info, |
| 493 | channel_info); |
| 494 | session_info_put(channel_info->session_info); |
| 495 | } |
| 496 | if (channel_info->name) { |
| 497 | free(channel_info->name); |
| 498 | } |
| 499 | call_rcu(&channel_info->rcu_node, free_channel_info_rcu); |
| 500 | } |
| 501 | |
| 502 | static |
| 503 | void free_session_info_rcu(struct rcu_head *node) |
| 504 | { |
| 505 | free(caa_container_of(node, struct session_info, rcu_node)); |
| 506 | } |
| 507 | |
| 508 | /* Don't call directly, use the ref-counting mechanism. */ |
| 509 | static |
| 510 | void session_info_destroy(void *_data) |
| 511 | { |
| 512 | struct session_info *session_info = _data; |
| 513 | int ret; |
| 514 | |
| 515 | assert(session_info); |
| 516 | if (session_info->channel_infos_ht) { |
| 517 | ret = cds_lfht_destroy(session_info->channel_infos_ht, NULL); |
| 518 | if (ret) { |
| 519 | ERR("[notification-thread] Failed to destroy channel information hash table"); |
| 520 | } |
| 521 | } |
| 522 | lttng_session_trigger_list_destroy(session_info->trigger_list); |
| 523 | |
| 524 | rcu_read_lock(); |
| 525 | cds_lfht_del(session_info->sessions_ht, |
| 526 | &session_info->sessions_ht_node); |
| 527 | rcu_read_unlock(); |
| 528 | free(session_info->name); |
| 529 | call_rcu(&session_info->rcu_node, free_session_info_rcu); |
| 530 | } |
| 531 | |
| 532 | static |
| 533 | void session_info_get(struct session_info *session_info) |
| 534 | { |
| 535 | if (!session_info) { |
| 536 | return; |
| 537 | } |
| 538 | lttng_ref_get(&session_info->ref); |
| 539 | } |
| 540 | |
| 541 | static |
| 542 | void session_info_put(struct session_info *session_info) |
| 543 | { |
| 544 | if (!session_info) { |
| 545 | return; |
| 546 | } |
| 547 | lttng_ref_put(&session_info->ref); |
| 548 | } |
| 549 | |
| 550 | static |
| 551 | struct session_info *session_info_create(const char *name, uid_t uid, gid_t gid, |
| 552 | struct lttng_session_trigger_list *trigger_list, |
| 553 | struct cds_lfht *sessions_ht) |
| 554 | { |
| 555 | struct session_info *session_info; |
| 556 | |
| 557 | assert(name); |
| 558 | |
| 559 | session_info = zmalloc(sizeof(*session_info)); |
| 560 | if (!session_info) { |
| 561 | goto end; |
| 562 | } |
| 563 | lttng_ref_init(&session_info->ref, session_info_destroy); |
| 564 | |
| 565 | session_info->channel_infos_ht = cds_lfht_new(DEFAULT_HT_SIZE, |
| 566 | 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL); |
| 567 | if (!session_info->channel_infos_ht) { |
| 568 | goto error; |
| 569 | } |
| 570 | |
| 571 | cds_lfht_node_init(&session_info->sessions_ht_node); |
| 572 | session_info->name = strdup(name); |
| 573 | if (!session_info->name) { |
| 574 | goto error; |
| 575 | } |
| 576 | session_info->uid = uid; |
| 577 | session_info->gid = gid; |
| 578 | session_info->trigger_list = trigger_list; |
| 579 | session_info->sessions_ht = sessions_ht; |
| 580 | end: |
| 581 | return session_info; |
| 582 | error: |
| 583 | session_info_put(session_info); |
| 584 | return NULL; |
| 585 | } |
| 586 | |
| 587 | static |
| 588 | void session_info_add_channel(struct session_info *session_info, |
| 589 | struct channel_info *channel_info) |
| 590 | { |
| 591 | rcu_read_lock(); |
| 592 | cds_lfht_add(session_info->channel_infos_ht, |
| 593 | hash_channel_key(&channel_info->key), |
| 594 | &channel_info->session_info_channels_ht_node); |
| 595 | rcu_read_unlock(); |
| 596 | } |
| 597 | |
| 598 | static |
| 599 | void session_info_remove_channel(struct session_info *session_info, |
| 600 | struct channel_info *channel_info) |
| 601 | { |
| 602 | rcu_read_lock(); |
| 603 | cds_lfht_del(session_info->channel_infos_ht, |
| 604 | &channel_info->session_info_channels_ht_node); |
| 605 | rcu_read_unlock(); |
| 606 | } |
| 607 | |
| 608 | static |
| 609 | struct channel_info *channel_info_create(const char *channel_name, |
| 610 | struct channel_key *channel_key, uint64_t channel_capacity, |
| 611 | struct session_info *session_info) |
| 612 | { |
| 613 | struct channel_info *channel_info = zmalloc(sizeof(*channel_info)); |
| 614 | |
| 615 | if (!channel_info) { |
| 616 | goto end; |
| 617 | } |
| 618 | |
| 619 | cds_lfht_node_init(&channel_info->channels_ht_node); |
| 620 | cds_lfht_node_init(&channel_info->session_info_channels_ht_node); |
| 621 | memcpy(&channel_info->key, channel_key, sizeof(*channel_key)); |
| 622 | channel_info->capacity = channel_capacity; |
| 623 | |
| 624 | channel_info->name = strdup(channel_name); |
| 625 | if (!channel_info->name) { |
| 626 | goto error; |
| 627 | } |
| 628 | |
| 629 | /* |
| 630 | * Set the references between session and channel infos: |
| 631 | * - channel_info holds a strong reference to session_info |
| 632 | * - session_info holds a weak reference to channel_info |
| 633 | */ |
| 634 | session_info_get(session_info); |
| 635 | session_info_add_channel(session_info, channel_info); |
| 636 | channel_info->session_info = session_info; |
| 637 | end: |
| 638 | return channel_info; |
| 639 | error: |
| 640 | channel_info_destroy(channel_info); |
| 641 | return NULL; |
| 642 | } |
| 643 | |
| 644 | LTTNG_HIDDEN |
| 645 | bool notification_client_list_get(struct notification_client_list *list) |
| 646 | { |
| 647 | return urcu_ref_get_unless_zero(&list->ref); |
| 648 | } |
| 649 | |
| 650 | static |
| 651 | void free_notification_client_list_rcu(struct rcu_head *node) |
| 652 | { |
| 653 | free(caa_container_of(node, struct notification_client_list, |
| 654 | rcu_node)); |
| 655 | } |
| 656 | |
| 657 | static |
| 658 | void notification_client_list_release(struct urcu_ref *list_ref) |
| 659 | { |
| 660 | struct notification_client_list *list = |
| 661 | container_of(list_ref, typeof(*list), ref); |
| 662 | struct notification_client_list_element *client_list_element, *tmp; |
| 663 | |
| 664 | if (list->notification_trigger_clients_ht) { |
| 665 | rcu_read_lock(); |
| 666 | cds_lfht_del(list->notification_trigger_clients_ht, |
| 667 | &list->notification_trigger_clients_ht_node); |
| 668 | rcu_read_unlock(); |
| 669 | list->notification_trigger_clients_ht = NULL; |
| 670 | } |
| 671 | cds_list_for_each_entry_safe(client_list_element, tmp, |
| 672 | &list->list, node) { |
| 673 | free(client_list_element); |
| 674 | } |
| 675 | pthread_mutex_destroy(&list->lock); |
| 676 | call_rcu(&list->rcu_node, free_notification_client_list_rcu); |
| 677 | } |
| 678 | |
| 679 | static |
| 680 | struct notification_client_list *notification_client_list_create( |
| 681 | const struct lttng_trigger *trigger) |
| 682 | { |
| 683 | struct notification_client_list *client_list = |
| 684 | zmalloc(sizeof(*client_list)); |
| 685 | |
| 686 | if (!client_list) { |
| 687 | goto error; |
| 688 | } |
| 689 | pthread_mutex_init(&client_list->lock, NULL); |
| 690 | urcu_ref_init(&client_list->ref); |
| 691 | cds_lfht_node_init(&client_list->notification_trigger_clients_ht_node); |
| 692 | CDS_INIT_LIST_HEAD(&client_list->list); |
| 693 | client_list->trigger = trigger; |
| 694 | error: |
| 695 | return client_list; |
| 696 | } |
| 697 | |
| 698 | static |
| 699 | void publish_notification_client_list( |
| 700 | struct notification_thread_state *state, |
| 701 | struct notification_client_list *list) |
| 702 | { |
| 703 | const struct lttng_condition *condition = |
| 704 | lttng_trigger_get_const_condition(list->trigger); |
| 705 | |
| 706 | assert(!list->notification_trigger_clients_ht); |
| 707 | notification_client_list_get(list); |
| 708 | |
| 709 | list->notification_trigger_clients_ht = |
| 710 | state->notification_trigger_clients_ht; |
| 711 | |
| 712 | rcu_read_lock(); |
| 713 | cds_lfht_add(state->notification_trigger_clients_ht, |
| 714 | lttng_condition_hash(condition), |
| 715 | &list->notification_trigger_clients_ht_node); |
| 716 | rcu_read_unlock(); |
| 717 | } |
| 718 | |
| 719 | LTTNG_HIDDEN |
| 720 | void notification_client_list_put(struct notification_client_list *list) |
| 721 | { |
| 722 | if (!list) { |
| 723 | return; |
| 724 | } |
| 725 | return urcu_ref_put(&list->ref, notification_client_list_release); |
| 726 | } |
| 727 | |
| 728 | /* Provides a reference to the returned list. */ |
| 729 | static |
| 730 | struct notification_client_list *get_client_list_from_condition( |
| 731 | struct notification_thread_state *state, |
| 732 | const struct lttng_condition *condition) |
| 733 | { |
| 734 | struct cds_lfht_node *node; |
| 735 | struct cds_lfht_iter iter; |
| 736 | struct notification_client_list *list = NULL; |
| 737 | |
| 738 | rcu_read_lock(); |
| 739 | cds_lfht_lookup(state->notification_trigger_clients_ht, |
| 740 | lttng_condition_hash(condition), |
| 741 | match_client_list_condition, |
| 742 | condition, |
| 743 | &iter); |
| 744 | node = cds_lfht_iter_get_node(&iter); |
| 745 | if (node) { |
| 746 | list = container_of(node, struct notification_client_list, |
| 747 | notification_trigger_clients_ht_node); |
| 748 | list = notification_client_list_get(list) ? list : NULL; |
| 749 | } |
| 750 | |
| 751 | rcu_read_unlock(); |
| 752 | return list; |
| 753 | } |
| 754 | |
| 755 | static |
| 756 | int evaluate_channel_condition_for_client( |
| 757 | const struct lttng_condition *condition, |
| 758 | struct notification_thread_state *state, |
| 759 | struct lttng_evaluation **evaluation, |
| 760 | uid_t *session_uid, gid_t *session_gid) |
| 761 | { |
| 762 | int ret; |
| 763 | struct cds_lfht_iter iter; |
| 764 | struct cds_lfht_node *node; |
| 765 | struct channel_info *channel_info = NULL; |
| 766 | struct channel_key *channel_key = NULL; |
| 767 | struct channel_state_sample *last_sample = NULL; |
| 768 | struct lttng_channel_trigger_list *channel_trigger_list = NULL; |
| 769 | |
| 770 | rcu_read_lock(); |
| 771 | |
| 772 | /* Find the channel associated with the condition. */ |
| 773 | cds_lfht_for_each_entry(state->channel_triggers_ht, &iter, |
| 774 | channel_trigger_list, channel_triggers_ht_node) { |
| 775 | struct lttng_trigger_list_element *element; |
| 776 | |
| 777 | cds_list_for_each_entry(element, &channel_trigger_list->list, node) { |
| 778 | const struct lttng_condition *current_condition = |
| 779 | lttng_trigger_get_const_condition( |
| 780 | element->trigger); |
| 781 | |
| 782 | assert(current_condition); |
| 783 | if (!lttng_condition_is_equal(condition, |
| 784 | current_condition)) { |
| 785 | continue; |
| 786 | } |
| 787 | |
| 788 | /* Found the trigger, save the channel key. */ |
| 789 | channel_key = &channel_trigger_list->channel_key; |
| 790 | break; |
| 791 | } |
| 792 | if (channel_key) { |
| 793 | /* The channel key was found stop iteration. */ |
| 794 | break; |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | if (!channel_key){ |
| 799 | /* No channel found; normal exit. */ |
| 800 | DBG("[notification-thread] No known channel associated with newly subscribed-to condition"); |
| 801 | ret = 0; |
| 802 | goto end; |
| 803 | } |
| 804 | |
| 805 | /* Fetch channel info for the matching channel. */ |
| 806 | cds_lfht_lookup(state->channels_ht, |
| 807 | hash_channel_key(channel_key), |
| 808 | match_channel_info, |
| 809 | channel_key, |
| 810 | &iter); |
| 811 | node = cds_lfht_iter_get_node(&iter); |
| 812 | assert(node); |
| 813 | channel_info = caa_container_of(node, struct channel_info, |
| 814 | channels_ht_node); |
| 815 | |
| 816 | /* Retrieve the channel's last sample, if it exists. */ |
| 817 | cds_lfht_lookup(state->channel_state_ht, |
| 818 | hash_channel_key(channel_key), |
| 819 | match_channel_state_sample, |
| 820 | channel_key, |
| 821 | &iter); |
| 822 | node = cds_lfht_iter_get_node(&iter); |
| 823 | if (node) { |
| 824 | last_sample = caa_container_of(node, |
| 825 | struct channel_state_sample, |
| 826 | channel_state_ht_node); |
| 827 | } else { |
| 828 | /* Nothing to evaluate, no sample was ever taken. Normal exit */ |
| 829 | DBG("[notification-thread] No channel sample associated with newly subscribed-to condition"); |
| 830 | ret = 0; |
| 831 | goto end; |
| 832 | } |
| 833 | |
| 834 | ret = evaluate_buffer_condition(condition, evaluation, state, |
| 835 | NULL, last_sample, |
| 836 | 0, channel_info->session_info->consumed_data_size, |
| 837 | channel_info); |
| 838 | if (ret) { |
| 839 | WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition"); |
| 840 | goto end; |
| 841 | } |
| 842 | |
| 843 | *session_uid = channel_info->session_info->uid; |
| 844 | *session_gid = channel_info->session_info->gid; |
| 845 | end: |
| 846 | rcu_read_unlock(); |
| 847 | return ret; |
| 848 | } |
| 849 | |
| 850 | static |
| 851 | const char *get_condition_session_name(const struct lttng_condition *condition) |
| 852 | { |
| 853 | const char *session_name = NULL; |
| 854 | enum lttng_condition_status status; |
| 855 | |
| 856 | switch (lttng_condition_get_type(condition)) { |
| 857 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: |
| 858 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: |
| 859 | status = lttng_condition_buffer_usage_get_session_name( |
| 860 | condition, &session_name); |
| 861 | break; |
| 862 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: |
| 863 | status = lttng_condition_session_consumed_size_get_session_name( |
| 864 | condition, &session_name); |
| 865 | break; |
| 866 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: |
| 867 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: |
| 868 | status = lttng_condition_session_rotation_get_session_name( |
| 869 | condition, &session_name); |
| 870 | break; |
| 871 | default: |
| 872 | abort(); |
| 873 | } |
| 874 | if (status != LTTNG_CONDITION_STATUS_OK) { |
| 875 | ERR("[notification-thread] Failed to retrieve session rotation condition's session name"); |
| 876 | goto end; |
| 877 | } |
| 878 | end: |
| 879 | return session_name; |
| 880 | } |
| 881 | |
| 882 | static |
| 883 | int evaluate_session_condition_for_client( |
| 884 | const struct lttng_condition *condition, |
| 885 | struct notification_thread_state *state, |
| 886 | struct lttng_evaluation **evaluation, |
| 887 | uid_t *session_uid, gid_t *session_gid) |
| 888 | { |
| 889 | int ret; |
| 890 | struct cds_lfht_iter iter; |
| 891 | struct cds_lfht_node *node; |
| 892 | const char *session_name; |
| 893 | struct session_info *session_info = NULL; |
| 894 | |
| 895 | rcu_read_lock(); |
| 896 | session_name = get_condition_session_name(condition); |
| 897 | |
| 898 | /* Find the session associated with the trigger. */ |
| 899 | cds_lfht_lookup(state->sessions_ht, |
| 900 | hash_key_str(session_name, lttng_ht_seed), |
| 901 | match_session, |
| 902 | session_name, |
| 903 | &iter); |
| 904 | node = cds_lfht_iter_get_node(&iter); |
| 905 | if (!node) { |
| 906 | DBG("[notification-thread] No known session matching name \"%s\"", |
| 907 | session_name); |
| 908 | ret = 0; |
| 909 | goto end; |
| 910 | } |
| 911 | |
| 912 | session_info = caa_container_of(node, struct session_info, |
| 913 | sessions_ht_node); |
| 914 | session_info_get(session_info); |
| 915 | |
| 916 | /* |
| 917 | * Evaluation is performed in-line here since only one type of |
| 918 | * session-bound condition is handled for the moment. |
| 919 | */ |
| 920 | switch (lttng_condition_get_type(condition)) { |
| 921 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: |
| 922 | if (!session_info->rotation.ongoing) { |
| 923 | ret = 0; |
| 924 | goto end_session_put; |
| 925 | } |
| 926 | |
| 927 | *evaluation = lttng_evaluation_session_rotation_ongoing_create( |
| 928 | session_info->rotation.id); |
| 929 | if (!*evaluation) { |
| 930 | /* Fatal error. */ |
| 931 | ERR("[notification-thread] Failed to create session rotation ongoing evaluation for session \"%s\"", |
| 932 | session_info->name); |
| 933 | ret = -1; |
| 934 | goto end_session_put; |
| 935 | } |
| 936 | ret = 0; |
| 937 | break; |
| 938 | default: |
| 939 | ret = 0; |
| 940 | goto end_session_put; |
| 941 | } |
| 942 | |
| 943 | *session_uid = session_info->uid; |
| 944 | *session_gid = session_info->gid; |
| 945 | |
| 946 | end_session_put: |
| 947 | session_info_put(session_info); |
| 948 | end: |
| 949 | rcu_read_unlock(); |
| 950 | return ret; |
| 951 | } |
| 952 | |
| 953 | static |
| 954 | int evaluate_condition_for_client(const struct lttng_trigger *trigger, |
| 955 | const struct lttng_condition *condition, |
| 956 | struct notification_client *client, |
| 957 | struct notification_thread_state *state) |
| 958 | { |
| 959 | int ret; |
| 960 | struct lttng_evaluation *evaluation = NULL; |
| 961 | struct notification_client_list client_list = { |
| 962 | .lock = PTHREAD_MUTEX_INITIALIZER, |
| 963 | }; |
| 964 | struct notification_client_list_element client_list_element = { 0 }; |
| 965 | uid_t object_uid = 0; |
| 966 | gid_t object_gid = 0; |
| 967 | |
| 968 | assert(trigger); |
| 969 | assert(condition); |
| 970 | assert(client); |
| 971 | assert(state); |
| 972 | |
| 973 | switch (get_condition_binding_object(condition)) { |
| 974 | case LTTNG_OBJECT_TYPE_SESSION: |
| 975 | ret = evaluate_session_condition_for_client(condition, state, |
| 976 | &evaluation, &object_uid, &object_gid); |
| 977 | break; |
| 978 | case LTTNG_OBJECT_TYPE_CHANNEL: |
| 979 | ret = evaluate_channel_condition_for_client(condition, state, |
| 980 | &evaluation, &object_uid, &object_gid); |
| 981 | break; |
| 982 | case LTTNG_OBJECT_TYPE_NONE: |
| 983 | DBG("[notification-thread] Newly subscribed-to condition not bound to object, nothing to evaluate"); |
| 984 | ret = 0; |
| 985 | goto end; |
| 986 | case LTTNG_OBJECT_TYPE_UNKNOWN: |
| 987 | default: |
| 988 | ret = -1; |
| 989 | goto end; |
| 990 | } |
| 991 | if (ret) { |
| 992 | /* Fatal error. */ |
| 993 | goto end; |
| 994 | } |
| 995 | if (!evaluation) { |
| 996 | /* Evaluation yielded nothing. Normal exit. */ |
| 997 | DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client"); |
| 998 | ret = 0; |
| 999 | goto end; |
| 1000 | } |
| 1001 | |
| 1002 | /* |
| 1003 | * Create a temporary client list with the client currently |
| 1004 | * subscribing. |
| 1005 | */ |
| 1006 | cds_lfht_node_init(&client_list.notification_trigger_clients_ht_node); |
| 1007 | CDS_INIT_LIST_HEAD(&client_list.list); |
| 1008 | client_list.trigger = trigger; |
| 1009 | |
| 1010 | CDS_INIT_LIST_HEAD(&client_list_element.node); |
| 1011 | client_list_element.client = client; |
| 1012 | cds_list_add(&client_list_element.node, &client_list.list); |
| 1013 | |
| 1014 | /* Send evaluation result to the newly-subscribed client. */ |
| 1015 | DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client"); |
| 1016 | ret = send_evaluation_to_clients(trigger, evaluation, &client_list, |
| 1017 | state, object_uid, object_gid); |
| 1018 | |
| 1019 | end: |
| 1020 | return ret; |
| 1021 | } |
| 1022 | |
| 1023 | static |
| 1024 | int notification_thread_client_subscribe(struct notification_client *client, |
| 1025 | struct lttng_condition *condition, |
| 1026 | struct notification_thread_state *state, |
| 1027 | enum lttng_notification_channel_status *_status) |
| 1028 | { |
| 1029 | int ret = 0; |
| 1030 | struct notification_client_list *client_list = NULL; |
| 1031 | struct lttng_condition_list_element *condition_list_element = NULL; |
| 1032 | struct notification_client_list_element *client_list_element = NULL; |
| 1033 | enum lttng_notification_channel_status status = |
| 1034 | LTTNG_NOTIFICATION_CHANNEL_STATUS_OK; |
| 1035 | |
| 1036 | /* |
| 1037 | * Ensure that the client has not already subscribed to this condition |
| 1038 | * before. |
| 1039 | */ |
| 1040 | cds_list_for_each_entry(condition_list_element, &client->condition_list, node) { |
| 1041 | if (lttng_condition_is_equal(condition_list_element->condition, |
| 1042 | condition)) { |
| 1043 | status = LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED; |
| 1044 | goto end; |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | condition_list_element = zmalloc(sizeof(*condition_list_element)); |
| 1049 | if (!condition_list_element) { |
| 1050 | ret = -1; |
| 1051 | goto error; |
| 1052 | } |
| 1053 | client_list_element = zmalloc(sizeof(*client_list_element)); |
| 1054 | if (!client_list_element) { |
| 1055 | ret = -1; |
| 1056 | goto error; |
| 1057 | } |
| 1058 | |
| 1059 | /* |
| 1060 | * Add the newly-subscribed condition to the client's subscription list. |
| 1061 | */ |
| 1062 | CDS_INIT_LIST_HEAD(&condition_list_element->node); |
| 1063 | condition_list_element->condition = condition; |
| 1064 | cds_list_add(&condition_list_element->node, &client->condition_list); |
| 1065 | |
| 1066 | client_list = get_client_list_from_condition(state, condition); |
| 1067 | if (!client_list) { |
| 1068 | /* |
| 1069 | * No notification-emiting trigger registered with this |
| 1070 | * condition. We don't evaluate the condition right away |
| 1071 | * since this trigger is not registered yet. |
| 1072 | */ |
| 1073 | free(client_list_element); |
| 1074 | goto end; |
| 1075 | } |
| 1076 | |
| 1077 | /* |
| 1078 | * The condition to which the client just subscribed is evaluated |
| 1079 | * at this point so that conditions that are already TRUE result |
| 1080 | * in a notification being sent out. |
| 1081 | * |
| 1082 | * The client_list's trigger is used without locking the list itself. |
| 1083 | * This is correct since the list doesn't own the trigger and the |
| 1084 | * object is immutable. |
| 1085 | */ |
| 1086 | if (evaluate_condition_for_client(client_list->trigger, condition, |
| 1087 | client, state)) { |
| 1088 | WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting."); |
| 1089 | ret = -1; |
| 1090 | free(client_list_element); |
| 1091 | goto end; |
| 1092 | } |
| 1093 | |
| 1094 | /* |
| 1095 | * Add the client to the list of clients interested in a given trigger |
| 1096 | * if a "notification" trigger with a corresponding condition was |
| 1097 | * added prior. |
| 1098 | */ |
| 1099 | client_list_element->client = client; |
| 1100 | CDS_INIT_LIST_HEAD(&client_list_element->node); |
| 1101 | |
| 1102 | pthread_mutex_lock(&client_list->lock); |
| 1103 | cds_list_add(&client_list_element->node, &client_list->list); |
| 1104 | pthread_mutex_unlock(&client_list->lock); |
| 1105 | end: |
| 1106 | if (_status) { |
| 1107 | *_status = status; |
| 1108 | } |
| 1109 | if (client_list) { |
| 1110 | notification_client_list_put(client_list); |
| 1111 | } |
| 1112 | return ret; |
| 1113 | error: |
| 1114 | free(condition_list_element); |
| 1115 | free(client_list_element); |
| 1116 | return ret; |
| 1117 | } |
| 1118 | |
| 1119 | static |
| 1120 | int notification_thread_client_unsubscribe( |
| 1121 | struct notification_client *client, |
| 1122 | struct lttng_condition *condition, |
| 1123 | struct notification_thread_state *state, |
| 1124 | enum lttng_notification_channel_status *_status) |
| 1125 | { |
| 1126 | struct notification_client_list *client_list; |
| 1127 | struct lttng_condition_list_element *condition_list_element, |
| 1128 | *condition_tmp; |
| 1129 | struct notification_client_list_element *client_list_element, |
| 1130 | *client_tmp; |
| 1131 | bool condition_found = false; |
| 1132 | enum lttng_notification_channel_status status = |
| 1133 | LTTNG_NOTIFICATION_CHANNEL_STATUS_OK; |
| 1134 | |
| 1135 | /* Remove the condition from the client's condition list. */ |
| 1136 | cds_list_for_each_entry_safe(condition_list_element, condition_tmp, |
| 1137 | &client->condition_list, node) { |
| 1138 | if (!lttng_condition_is_equal(condition_list_element->condition, |
| 1139 | condition)) { |
| 1140 | continue; |
| 1141 | } |
| 1142 | |
| 1143 | cds_list_del(&condition_list_element->node); |
| 1144 | /* |
| 1145 | * The caller may be iterating on the client's conditions to |
| 1146 | * tear down a client's connection. In this case, the condition |
| 1147 | * will be destroyed at the end. |
| 1148 | */ |
| 1149 | if (condition != condition_list_element->condition) { |
| 1150 | lttng_condition_destroy( |
| 1151 | condition_list_element->condition); |
| 1152 | } |
| 1153 | free(condition_list_element); |
| 1154 | condition_found = true; |
| 1155 | break; |
| 1156 | } |
| 1157 | |
| 1158 | if (!condition_found) { |
| 1159 | status = LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION; |
| 1160 | goto end; |
| 1161 | } |
| 1162 | |
| 1163 | /* |
| 1164 | * Remove the client from the list of clients interested the trigger |
| 1165 | * matching the condition. |
| 1166 | */ |
| 1167 | client_list = get_client_list_from_condition(state, condition); |
| 1168 | if (!client_list) { |
| 1169 | goto end; |
| 1170 | } |
| 1171 | |
| 1172 | pthread_mutex_lock(&client_list->lock); |
| 1173 | cds_list_for_each_entry_safe(client_list_element, client_tmp, |
| 1174 | &client_list->list, node) { |
| 1175 | if (client_list_element->client->id != client->id) { |
| 1176 | continue; |
| 1177 | } |
| 1178 | cds_list_del(&client_list_element->node); |
| 1179 | free(client_list_element); |
| 1180 | break; |
| 1181 | } |
| 1182 | pthread_mutex_unlock(&client_list->lock); |
| 1183 | notification_client_list_put(client_list); |
| 1184 | client_list = NULL; |
| 1185 | end: |
| 1186 | lttng_condition_destroy(condition); |
| 1187 | if (_status) { |
| 1188 | *_status = status; |
| 1189 | } |
| 1190 | return 0; |
| 1191 | } |
| 1192 | |
| 1193 | static |
| 1194 | void free_notification_client_rcu(struct rcu_head *node) |
| 1195 | { |
| 1196 | free(caa_container_of(node, struct notification_client, rcu_node)); |
| 1197 | } |
| 1198 | |
| 1199 | static |
| 1200 | void notification_client_destroy(struct notification_client *client, |
| 1201 | struct notification_thread_state *state) |
| 1202 | { |
| 1203 | if (!client) { |
| 1204 | return; |
| 1205 | } |
| 1206 | |
| 1207 | /* |
| 1208 | * The client object is not reachable by other threads, no need to lock |
| 1209 | * the client here. |
| 1210 | */ |
| 1211 | if (client->socket >= 0) { |
| 1212 | (void) lttcomm_close_unix_sock(client->socket); |
| 1213 | client->socket = -1; |
| 1214 | } |
| 1215 | client->communication.active = false; |
| 1216 | lttng_payload_reset(&client->communication.inbound.payload); |
| 1217 | lttng_payload_reset(&client->communication.outbound.payload); |
| 1218 | pthread_mutex_destroy(&client->lock); |
| 1219 | call_rcu(&client->rcu_node, free_notification_client_rcu); |
| 1220 | } |
| 1221 | |
| 1222 | /* |
| 1223 | * Call with rcu_read_lock held (and hold for the lifetime of the returned |
| 1224 | * client pointer). |
| 1225 | */ |
| 1226 | static |
| 1227 | struct notification_client *get_client_from_socket(int socket, |
| 1228 | struct notification_thread_state *state) |
| 1229 | { |
| 1230 | struct cds_lfht_iter iter; |
| 1231 | struct cds_lfht_node *node; |
| 1232 | struct notification_client *client = NULL; |
| 1233 | |
| 1234 | cds_lfht_lookup(state->client_socket_ht, |
| 1235 | hash_client_socket(socket), |
| 1236 | match_client_socket, |
| 1237 | (void *) (unsigned long) socket, |
| 1238 | &iter); |
| 1239 | node = cds_lfht_iter_get_node(&iter); |
| 1240 | if (!node) { |
| 1241 | goto end; |
| 1242 | } |
| 1243 | |
| 1244 | client = caa_container_of(node, struct notification_client, |
| 1245 | client_socket_ht_node); |
| 1246 | end: |
| 1247 | return client; |
| 1248 | } |
| 1249 | |
| 1250 | /* |
| 1251 | * Call with rcu_read_lock held (and hold for the lifetime of the returned |
| 1252 | * client pointer). |
| 1253 | */ |
| 1254 | static |
| 1255 | struct notification_client *get_client_from_id(notification_client_id id, |
| 1256 | struct notification_thread_state *state) |
| 1257 | { |
| 1258 | struct cds_lfht_iter iter; |
| 1259 | struct cds_lfht_node *node; |
| 1260 | struct notification_client *client = NULL; |
| 1261 | |
| 1262 | cds_lfht_lookup(state->client_id_ht, |
| 1263 | hash_client_id(id), |
| 1264 | match_client_id, |
| 1265 | &id, |
| 1266 | &iter); |
| 1267 | node = cds_lfht_iter_get_node(&iter); |
| 1268 | if (!node) { |
| 1269 | goto end; |
| 1270 | } |
| 1271 | |
| 1272 | client = caa_container_of(node, struct notification_client, |
| 1273 | client_id_ht_node); |
| 1274 | end: |
| 1275 | return client; |
| 1276 | } |
| 1277 | |
| 1278 | static |
| 1279 | bool buffer_usage_condition_applies_to_channel( |
| 1280 | const struct lttng_condition *condition, |
| 1281 | const struct channel_info *channel_info) |
| 1282 | { |
| 1283 | enum lttng_condition_status status; |
| 1284 | enum lttng_domain_type condition_domain; |
| 1285 | const char *condition_session_name = NULL; |
| 1286 | const char *condition_channel_name = NULL; |
| 1287 | |
| 1288 | status = lttng_condition_buffer_usage_get_domain_type(condition, |
| 1289 | &condition_domain); |
| 1290 | assert(status == LTTNG_CONDITION_STATUS_OK); |
| 1291 | if (channel_info->key.domain != condition_domain) { |
| 1292 | goto fail; |
| 1293 | } |
| 1294 | |
| 1295 | status = lttng_condition_buffer_usage_get_session_name( |
| 1296 | condition, &condition_session_name); |
| 1297 | assert((status == LTTNG_CONDITION_STATUS_OK) && condition_session_name); |
| 1298 | |
| 1299 | status = lttng_condition_buffer_usage_get_channel_name( |
| 1300 | condition, &condition_channel_name); |
| 1301 | assert((status == LTTNG_CONDITION_STATUS_OK) && condition_channel_name); |
| 1302 | |
| 1303 | if (strcmp(channel_info->session_info->name, condition_session_name)) { |
| 1304 | goto fail; |
| 1305 | } |
| 1306 | if (strcmp(channel_info->name, condition_channel_name)) { |
| 1307 | goto fail; |
| 1308 | } |
| 1309 | |
| 1310 | return true; |
| 1311 | fail: |
| 1312 | return false; |
| 1313 | } |
| 1314 | |
| 1315 | static |
| 1316 | bool session_consumed_size_condition_applies_to_channel( |
| 1317 | const struct lttng_condition *condition, |
| 1318 | const struct channel_info *channel_info) |
| 1319 | { |
| 1320 | enum lttng_condition_status status; |
| 1321 | const char *condition_session_name = NULL; |
| 1322 | |
| 1323 | status = lttng_condition_session_consumed_size_get_session_name( |
| 1324 | condition, &condition_session_name); |
| 1325 | assert((status == LTTNG_CONDITION_STATUS_OK) && condition_session_name); |
| 1326 | |
| 1327 | if (strcmp(channel_info->session_info->name, condition_session_name)) { |
| 1328 | goto fail; |
| 1329 | } |
| 1330 | |
| 1331 | return true; |
| 1332 | fail: |
| 1333 | return false; |
| 1334 | } |
| 1335 | |
| 1336 | static |
| 1337 | bool trigger_applies_to_channel(const struct lttng_trigger *trigger, |
| 1338 | const struct channel_info *channel_info) |
| 1339 | { |
| 1340 | const struct lttng_condition *condition; |
| 1341 | bool trigger_applies; |
| 1342 | |
| 1343 | condition = lttng_trigger_get_const_condition(trigger); |
| 1344 | if (!condition) { |
| 1345 | goto fail; |
| 1346 | } |
| 1347 | |
| 1348 | switch (lttng_condition_get_type(condition)) { |
| 1349 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: |
| 1350 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: |
| 1351 | trigger_applies = buffer_usage_condition_applies_to_channel( |
| 1352 | condition, channel_info); |
| 1353 | break; |
| 1354 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: |
| 1355 | trigger_applies = session_consumed_size_condition_applies_to_channel( |
| 1356 | condition, channel_info); |
| 1357 | break; |
| 1358 | default: |
| 1359 | goto fail; |
| 1360 | } |
| 1361 | |
| 1362 | return trigger_applies; |
| 1363 | fail: |
| 1364 | return false; |
| 1365 | } |
| 1366 | |
| 1367 | static |
| 1368 | bool trigger_applies_to_client(struct lttng_trigger *trigger, |
| 1369 | struct notification_client *client) |
| 1370 | { |
| 1371 | bool applies = false; |
| 1372 | struct lttng_condition_list_element *condition_list_element; |
| 1373 | |
| 1374 | cds_list_for_each_entry(condition_list_element, &client->condition_list, |
| 1375 | node) { |
| 1376 | applies = lttng_condition_is_equal( |
| 1377 | condition_list_element->condition, |
| 1378 | lttng_trigger_get_condition(trigger)); |
| 1379 | if (applies) { |
| 1380 | break; |
| 1381 | } |
| 1382 | } |
| 1383 | return applies; |
| 1384 | } |
| 1385 | |
| 1386 | /* Must be called with RCU read lock held. */ |
| 1387 | static |
| 1388 | struct lttng_session_trigger_list *get_session_trigger_list( |
| 1389 | struct notification_thread_state *state, |
| 1390 | const char *session_name) |
| 1391 | { |
| 1392 | struct lttng_session_trigger_list *list = NULL; |
| 1393 | struct cds_lfht_node *node; |
| 1394 | struct cds_lfht_iter iter; |
| 1395 | |
| 1396 | cds_lfht_lookup(state->session_triggers_ht, |
| 1397 | hash_key_str(session_name, lttng_ht_seed), |
| 1398 | match_session_trigger_list, |
| 1399 | session_name, |
| 1400 | &iter); |
| 1401 | node = cds_lfht_iter_get_node(&iter); |
| 1402 | if (!node) { |
| 1403 | /* |
| 1404 | * Not an error, the list of triggers applying to that session |
| 1405 | * will be initialized when the session is created. |
| 1406 | */ |
| 1407 | DBG("[notification-thread] No trigger list found for session \"%s\" as it is not yet known to the notification system", |
| 1408 | session_name); |
| 1409 | goto end; |
| 1410 | } |
| 1411 | |
| 1412 | list = caa_container_of(node, |
| 1413 | struct lttng_session_trigger_list, |
| 1414 | session_triggers_ht_node); |
| 1415 | end: |
| 1416 | return list; |
| 1417 | } |
| 1418 | |
| 1419 | /* |
| 1420 | * Allocate an empty lttng_session_trigger_list for the session named |
| 1421 | * 'session_name'. |
| 1422 | * |
| 1423 | * No ownership of 'session_name' is assumed by the session trigger list. |
| 1424 | * It is the caller's responsability to ensure the session name is alive |
| 1425 | * for as long as this list is. |
| 1426 | */ |
| 1427 | static |
| 1428 | struct lttng_session_trigger_list *lttng_session_trigger_list_create( |
| 1429 | const char *session_name, |
| 1430 | struct cds_lfht *session_triggers_ht) |
| 1431 | { |
| 1432 | struct lttng_session_trigger_list *list; |
| 1433 | |
| 1434 | list = zmalloc(sizeof(*list)); |
| 1435 | if (!list) { |
| 1436 | goto end; |
| 1437 | } |
| 1438 | list->session_name = session_name; |
| 1439 | CDS_INIT_LIST_HEAD(&list->list); |
| 1440 | cds_lfht_node_init(&list->session_triggers_ht_node); |
| 1441 | list->session_triggers_ht = session_triggers_ht; |
| 1442 | |
| 1443 | rcu_read_lock(); |
| 1444 | /* Publish the list through the session_triggers_ht. */ |
| 1445 | cds_lfht_add(session_triggers_ht, |
| 1446 | hash_key_str(session_name, lttng_ht_seed), |
| 1447 | &list->session_triggers_ht_node); |
| 1448 | rcu_read_unlock(); |
| 1449 | end: |
| 1450 | return list; |
| 1451 | } |
| 1452 | |
| 1453 | static |
| 1454 | void free_session_trigger_list_rcu(struct rcu_head *node) |
| 1455 | { |
| 1456 | free(caa_container_of(node, struct lttng_session_trigger_list, |
| 1457 | rcu_node)); |
| 1458 | } |
| 1459 | |
| 1460 | static |
| 1461 | void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list *list) |
| 1462 | { |
| 1463 | struct lttng_trigger_list_element *trigger_list_element, *tmp; |
| 1464 | |
| 1465 | /* Empty the list element by element, and then free the list itself. */ |
| 1466 | cds_list_for_each_entry_safe(trigger_list_element, tmp, |
| 1467 | &list->list, node) { |
| 1468 | cds_list_del(&trigger_list_element->node); |
| 1469 | free(trigger_list_element); |
| 1470 | } |
| 1471 | rcu_read_lock(); |
| 1472 | /* Unpublish the list from the session_triggers_ht. */ |
| 1473 | cds_lfht_del(list->session_triggers_ht, |
| 1474 | &list->session_triggers_ht_node); |
| 1475 | rcu_read_unlock(); |
| 1476 | call_rcu(&list->rcu_node, free_session_trigger_list_rcu); |
| 1477 | } |
| 1478 | |
| 1479 | static |
| 1480 | int lttng_session_trigger_list_add(struct lttng_session_trigger_list *list, |
| 1481 | struct lttng_trigger *trigger) |
| 1482 | { |
| 1483 | int ret = 0; |
| 1484 | struct lttng_trigger_list_element *new_element = |
| 1485 | zmalloc(sizeof(*new_element)); |
| 1486 | |
| 1487 | if (!new_element) { |
| 1488 | ret = -1; |
| 1489 | goto end; |
| 1490 | } |
| 1491 | CDS_INIT_LIST_HEAD(&new_element->node); |
| 1492 | new_element->trigger = trigger; |
| 1493 | cds_list_add(&new_element->node, &list->list); |
| 1494 | end: |
| 1495 | return ret; |
| 1496 | } |
| 1497 | |
| 1498 | static |
| 1499 | bool trigger_applies_to_session(const struct lttng_trigger *trigger, |
| 1500 | const char *session_name) |
| 1501 | { |
| 1502 | bool applies = false; |
| 1503 | const struct lttng_condition *condition; |
| 1504 | |
| 1505 | condition = lttng_trigger_get_const_condition(trigger); |
| 1506 | switch (lttng_condition_get_type(condition)) { |
| 1507 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: |
| 1508 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: |
| 1509 | { |
| 1510 | enum lttng_condition_status condition_status; |
| 1511 | const char *condition_session_name; |
| 1512 | |
| 1513 | condition_status = lttng_condition_session_rotation_get_session_name( |
| 1514 | condition, &condition_session_name); |
| 1515 | if (condition_status != LTTNG_CONDITION_STATUS_OK) { |
| 1516 | ERR("[notification-thread] Failed to retrieve session rotation condition's session name"); |
| 1517 | goto end; |
| 1518 | } |
| 1519 | |
| 1520 | assert(condition_session_name); |
| 1521 | applies = !strcmp(condition_session_name, session_name); |
| 1522 | break; |
| 1523 | } |
| 1524 | default: |
| 1525 | goto end; |
| 1526 | } |
| 1527 | end: |
| 1528 | return applies; |
| 1529 | } |
| 1530 | |
| 1531 | /* |
| 1532 | * Allocate and initialize an lttng_session_trigger_list which contains |
| 1533 | * all triggers that apply to the session named 'session_name'. |
| 1534 | * |
| 1535 | * No ownership of 'session_name' is assumed by the session trigger list. |
| 1536 | * It is the caller's responsability to ensure the session name is alive |
| 1537 | * for as long as this list is. |
| 1538 | */ |
| 1539 | static |
| 1540 | struct lttng_session_trigger_list *lttng_session_trigger_list_build( |
| 1541 | const struct notification_thread_state *state, |
| 1542 | const char *session_name) |
| 1543 | { |
| 1544 | int trigger_count = 0; |
| 1545 | struct lttng_session_trigger_list *session_trigger_list = NULL; |
| 1546 | struct lttng_trigger_ht_element *trigger_ht_element = NULL; |
| 1547 | struct cds_lfht_iter iter; |
| 1548 | |
| 1549 | session_trigger_list = lttng_session_trigger_list_create(session_name, |
| 1550 | state->session_triggers_ht); |
| 1551 | |
| 1552 | /* Add all triggers applying to the session named 'session_name'. */ |
| 1553 | cds_lfht_for_each_entry(state->triggers_ht, &iter, trigger_ht_element, |
| 1554 | node) { |
| 1555 | int ret; |
| 1556 | |
| 1557 | if (!trigger_applies_to_session(trigger_ht_element->trigger, |
| 1558 | session_name)) { |
| 1559 | continue; |
| 1560 | } |
| 1561 | |
| 1562 | ret = lttng_session_trigger_list_add(session_trigger_list, |
| 1563 | trigger_ht_element->trigger); |
| 1564 | if (ret) { |
| 1565 | goto error; |
| 1566 | } |
| 1567 | |
| 1568 | trigger_count++; |
| 1569 | } |
| 1570 | |
| 1571 | DBG("[notification-thread] Found %i triggers that apply to newly created session", |
| 1572 | trigger_count); |
| 1573 | return session_trigger_list; |
| 1574 | error: |
| 1575 | lttng_session_trigger_list_destroy(session_trigger_list); |
| 1576 | return NULL; |
| 1577 | } |
| 1578 | |
| 1579 | static |
| 1580 | struct session_info *find_or_create_session_info( |
| 1581 | struct notification_thread_state *state, |
| 1582 | const char *name, uid_t uid, gid_t gid) |
| 1583 | { |
| 1584 | struct session_info *session = NULL; |
| 1585 | struct cds_lfht_node *node; |
| 1586 | struct cds_lfht_iter iter; |
| 1587 | struct lttng_session_trigger_list *trigger_list; |
| 1588 | |
| 1589 | rcu_read_lock(); |
| 1590 | cds_lfht_lookup(state->sessions_ht, |
| 1591 | hash_key_str(name, lttng_ht_seed), |
| 1592 | match_session, |
| 1593 | name, |
| 1594 | &iter); |
| 1595 | node = cds_lfht_iter_get_node(&iter); |
| 1596 | if (node) { |
| 1597 | DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)", |
| 1598 | name, uid, gid); |
| 1599 | session = caa_container_of(node, struct session_info, |
| 1600 | sessions_ht_node); |
| 1601 | assert(session->uid == uid); |
| 1602 | assert(session->gid == gid); |
| 1603 | session_info_get(session); |
| 1604 | goto end; |
| 1605 | } |
| 1606 | |
| 1607 | trigger_list = lttng_session_trigger_list_build(state, name); |
| 1608 | if (!trigger_list) { |
| 1609 | goto error; |
| 1610 | } |
| 1611 | |
| 1612 | session = session_info_create(name, uid, gid, trigger_list, |
| 1613 | state->sessions_ht); |
| 1614 | if (!session) { |
| 1615 | ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)", |
| 1616 | name, uid, gid); |
| 1617 | lttng_session_trigger_list_destroy(trigger_list); |
| 1618 | goto error; |
| 1619 | } |
| 1620 | trigger_list = NULL; |
| 1621 | |
| 1622 | cds_lfht_add(state->sessions_ht, hash_key_str(name, lttng_ht_seed), |
| 1623 | &session->sessions_ht_node); |
| 1624 | end: |
| 1625 | rcu_read_unlock(); |
| 1626 | return session; |
| 1627 | error: |
| 1628 | rcu_read_unlock(); |
| 1629 | session_info_put(session); |
| 1630 | return NULL; |
| 1631 | } |
| 1632 | |
| 1633 | static |
| 1634 | int handle_notification_thread_command_add_channel( |
| 1635 | struct notification_thread_state *state, |
| 1636 | const char *session_name, uid_t session_uid, gid_t session_gid, |
| 1637 | const char *channel_name, enum lttng_domain_type channel_domain, |
| 1638 | uint64_t channel_key_int, uint64_t channel_capacity, |
| 1639 | enum lttng_error_code *cmd_result) |
| 1640 | { |
| 1641 | struct cds_list_head trigger_list; |
| 1642 | struct channel_info *new_channel_info = NULL; |
| 1643 | struct channel_key channel_key = { |
| 1644 | .key = channel_key_int, |
| 1645 | .domain = channel_domain, |
| 1646 | }; |
| 1647 | struct lttng_channel_trigger_list *channel_trigger_list = NULL; |
| 1648 | struct lttng_trigger_ht_element *trigger_ht_element = NULL; |
| 1649 | int trigger_count = 0; |
| 1650 | struct cds_lfht_iter iter; |
| 1651 | struct session_info *session_info = NULL; |
| 1652 | |
| 1653 | DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64 " in %s domain", |
| 1654 | channel_name, session_name, channel_key_int, |
| 1655 | lttng_domain_type_str(channel_domain)); |
| 1656 | |
| 1657 | CDS_INIT_LIST_HEAD(&trigger_list); |
| 1658 | |
| 1659 | session_info = find_or_create_session_info(state, session_name, |
| 1660 | session_uid, session_gid); |
| 1661 | if (!session_info) { |
| 1662 | /* Allocation error or an internal error occurred. */ |
| 1663 | goto error; |
| 1664 | } |
| 1665 | |
| 1666 | new_channel_info = channel_info_create(channel_name, &channel_key, |
| 1667 | channel_capacity, session_info); |
| 1668 | if (!new_channel_info) { |
| 1669 | goto error; |
| 1670 | } |
| 1671 | |
| 1672 | rcu_read_lock(); |
| 1673 | /* Build a list of all triggers applying to the new channel. */ |
| 1674 | cds_lfht_for_each_entry(state->triggers_ht, &iter, trigger_ht_element, |
| 1675 | node) { |
| 1676 | struct lttng_trigger_list_element *new_element; |
| 1677 | |
| 1678 | if (!trigger_applies_to_channel(trigger_ht_element->trigger, |
| 1679 | new_channel_info)) { |
| 1680 | continue; |
| 1681 | } |
| 1682 | |
| 1683 | new_element = zmalloc(sizeof(*new_element)); |
| 1684 | if (!new_element) { |
| 1685 | rcu_read_unlock(); |
| 1686 | goto error; |
| 1687 | } |
| 1688 | CDS_INIT_LIST_HEAD(&new_element->node); |
| 1689 | new_element->trigger = trigger_ht_element->trigger; |
| 1690 | cds_list_add(&new_element->node, &trigger_list); |
| 1691 | trigger_count++; |
| 1692 | } |
| 1693 | rcu_read_unlock(); |
| 1694 | |
| 1695 | DBG("[notification-thread] Found %i triggers that apply to newly added channel", |
| 1696 | trigger_count); |
| 1697 | channel_trigger_list = zmalloc(sizeof(*channel_trigger_list)); |
| 1698 | if (!channel_trigger_list) { |
| 1699 | goto error; |
| 1700 | } |
| 1701 | channel_trigger_list->channel_key = new_channel_info->key; |
| 1702 | CDS_INIT_LIST_HEAD(&channel_trigger_list->list); |
| 1703 | cds_lfht_node_init(&channel_trigger_list->channel_triggers_ht_node); |
| 1704 | cds_list_splice(&trigger_list, &channel_trigger_list->list); |
| 1705 | |
| 1706 | rcu_read_lock(); |
| 1707 | /* Add channel to the channel_ht which owns the channel_infos. */ |
| 1708 | cds_lfht_add(state->channels_ht, |
| 1709 | hash_channel_key(&new_channel_info->key), |
| 1710 | &new_channel_info->channels_ht_node); |
| 1711 | /* |
| 1712 | * Add the list of triggers associated with this channel to the |
| 1713 | * channel_triggers_ht. |
| 1714 | */ |
| 1715 | cds_lfht_add(state->channel_triggers_ht, |
| 1716 | hash_channel_key(&new_channel_info->key), |
| 1717 | &channel_trigger_list->channel_triggers_ht_node); |
| 1718 | rcu_read_unlock(); |
| 1719 | session_info_put(session_info); |
| 1720 | *cmd_result = LTTNG_OK; |
| 1721 | return 0; |
| 1722 | error: |
| 1723 | channel_info_destroy(new_channel_info); |
| 1724 | session_info_put(session_info); |
| 1725 | return 1; |
| 1726 | } |
| 1727 | |
| 1728 | static |
| 1729 | void free_channel_trigger_list_rcu(struct rcu_head *node) |
| 1730 | { |
| 1731 | free(caa_container_of(node, struct lttng_channel_trigger_list, |
| 1732 | rcu_node)); |
| 1733 | } |
| 1734 | |
| 1735 | static |
| 1736 | void free_channel_state_sample_rcu(struct rcu_head *node) |
| 1737 | { |
| 1738 | free(caa_container_of(node, struct channel_state_sample, |
| 1739 | rcu_node)); |
| 1740 | } |
| 1741 | |
| 1742 | static |
| 1743 | int handle_notification_thread_command_remove_channel( |
| 1744 | struct notification_thread_state *state, |
| 1745 | uint64_t channel_key, enum lttng_domain_type domain, |
| 1746 | enum lttng_error_code *cmd_result) |
| 1747 | { |
| 1748 | struct cds_lfht_node *node; |
| 1749 | struct cds_lfht_iter iter; |
| 1750 | struct lttng_channel_trigger_list *trigger_list; |
| 1751 | struct lttng_trigger_list_element *trigger_list_element, *tmp; |
| 1752 | struct channel_key key = { .key = channel_key, .domain = domain }; |
| 1753 | struct channel_info *channel_info; |
| 1754 | |
| 1755 | DBG("[notification-thread] Removing channel key = %" PRIu64 " in %s domain", |
| 1756 | channel_key, lttng_domain_type_str(domain)); |
| 1757 | |
| 1758 | rcu_read_lock(); |
| 1759 | |
| 1760 | cds_lfht_lookup(state->channel_triggers_ht, |
| 1761 | hash_channel_key(&key), |
| 1762 | match_channel_trigger_list, |
| 1763 | &key, |
| 1764 | &iter); |
| 1765 | node = cds_lfht_iter_get_node(&iter); |
| 1766 | /* |
| 1767 | * There is a severe internal error if we are being asked to remove a |
| 1768 | * channel that doesn't exist. |
| 1769 | */ |
| 1770 | if (!node) { |
| 1771 | ERR("[notification-thread] Channel being removed is unknown to the notification thread"); |
| 1772 | goto end; |
| 1773 | } |
| 1774 | |
| 1775 | /* Free the list of triggers associated with this channel. */ |
| 1776 | trigger_list = caa_container_of(node, struct lttng_channel_trigger_list, |
| 1777 | channel_triggers_ht_node); |
| 1778 | cds_list_for_each_entry_safe(trigger_list_element, tmp, |
| 1779 | &trigger_list->list, node) { |
| 1780 | cds_list_del(&trigger_list_element->node); |
| 1781 | free(trigger_list_element); |
| 1782 | } |
| 1783 | cds_lfht_del(state->channel_triggers_ht, node); |
| 1784 | call_rcu(&trigger_list->rcu_node, free_channel_trigger_list_rcu); |
| 1785 | |
| 1786 | /* Free sampled channel state. */ |
| 1787 | cds_lfht_lookup(state->channel_state_ht, |
| 1788 | hash_channel_key(&key), |
| 1789 | match_channel_state_sample, |
| 1790 | &key, |
| 1791 | &iter); |
| 1792 | node = cds_lfht_iter_get_node(&iter); |
| 1793 | /* |
| 1794 | * This is expected to be NULL if the channel is destroyed before we |
| 1795 | * received a sample. |
| 1796 | */ |
| 1797 | if (node) { |
| 1798 | struct channel_state_sample *sample = caa_container_of(node, |
| 1799 | struct channel_state_sample, |
| 1800 | channel_state_ht_node); |
| 1801 | |
| 1802 | cds_lfht_del(state->channel_state_ht, node); |
| 1803 | call_rcu(&sample->rcu_node, free_channel_state_sample_rcu); |
| 1804 | } |
| 1805 | |
| 1806 | /* Remove the channel from the channels_ht and free it. */ |
| 1807 | cds_lfht_lookup(state->channels_ht, |
| 1808 | hash_channel_key(&key), |
| 1809 | match_channel_info, |
| 1810 | &key, |
| 1811 | &iter); |
| 1812 | node = cds_lfht_iter_get_node(&iter); |
| 1813 | assert(node); |
| 1814 | channel_info = caa_container_of(node, struct channel_info, |
| 1815 | channels_ht_node); |
| 1816 | cds_lfht_del(state->channels_ht, node); |
| 1817 | channel_info_destroy(channel_info); |
| 1818 | end: |
| 1819 | rcu_read_unlock(); |
| 1820 | *cmd_result = LTTNG_OK; |
| 1821 | return 0; |
| 1822 | } |
| 1823 | |
| 1824 | static |
| 1825 | int handle_notification_thread_command_session_rotation( |
| 1826 | struct notification_thread_state *state, |
| 1827 | enum notification_thread_command_type cmd_type, |
| 1828 | const char *session_name, uid_t session_uid, gid_t session_gid, |
| 1829 | uint64_t trace_archive_chunk_id, |
| 1830 | struct lttng_trace_archive_location *location, |
| 1831 | enum lttng_error_code *_cmd_result) |
| 1832 | { |
| 1833 | int ret = 0; |
| 1834 | enum lttng_error_code cmd_result = LTTNG_OK; |
| 1835 | struct lttng_session_trigger_list *trigger_list; |
| 1836 | struct lttng_trigger_list_element *trigger_list_element; |
| 1837 | struct session_info *session_info; |
| 1838 | const struct lttng_credentials session_creds = { |
| 1839 | .uid = LTTNG_OPTIONAL_INIT_VALUE(session_uid), |
| 1840 | .gid = LTTNG_OPTIONAL_INIT_VALUE(session_gid), |
| 1841 | }; |
| 1842 | |
| 1843 | rcu_read_lock(); |
| 1844 | |
| 1845 | session_info = find_or_create_session_info(state, session_name, |
| 1846 | session_uid, session_gid); |
| 1847 | if (!session_info) { |
| 1848 | /* Allocation error or an internal error occurred. */ |
| 1849 | ret = -1; |
| 1850 | cmd_result = LTTNG_ERR_NOMEM; |
| 1851 | goto end; |
| 1852 | } |
| 1853 | |
| 1854 | session_info->rotation.ongoing = |
| 1855 | cmd_type == NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING; |
| 1856 | session_info->rotation.id = trace_archive_chunk_id; |
| 1857 | trigger_list = get_session_trigger_list(state, session_name); |
| 1858 | if (!trigger_list) { |
| 1859 | DBG("[notification-thread] No triggers applying to session \"%s\" found", |
| 1860 | session_name); |
| 1861 | goto end; |
| 1862 | } |
| 1863 | |
| 1864 | cds_list_for_each_entry(trigger_list_element, &trigger_list->list, |
| 1865 | node) { |
| 1866 | const struct lttng_condition *condition; |
| 1867 | struct lttng_trigger *trigger; |
| 1868 | struct notification_client_list *client_list; |
| 1869 | struct lttng_evaluation *evaluation = NULL; |
| 1870 | enum lttng_condition_type condition_type; |
| 1871 | enum action_executor_status executor_status; |
| 1872 | |
| 1873 | trigger = trigger_list_element->trigger; |
| 1874 | condition = lttng_trigger_get_const_condition(trigger); |
| 1875 | assert(condition); |
| 1876 | condition_type = lttng_condition_get_type(condition); |
| 1877 | |
| 1878 | if (condition_type == LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING && |
| 1879 | cmd_type != NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING) { |
| 1880 | continue; |
| 1881 | } else if (condition_type == LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED && |
| 1882 | cmd_type != NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED) { |
| 1883 | continue; |
| 1884 | } |
| 1885 | |
| 1886 | client_list = get_client_list_from_condition(state, condition); |
| 1887 | if (cmd_type == NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING) { |
| 1888 | evaluation = lttng_evaluation_session_rotation_ongoing_create( |
| 1889 | trace_archive_chunk_id); |
| 1890 | } else { |
| 1891 | evaluation = lttng_evaluation_session_rotation_completed_create( |
| 1892 | trace_archive_chunk_id, location); |
| 1893 | } |
| 1894 | |
| 1895 | if (!evaluation) { |
| 1896 | /* Internal error */ |
| 1897 | ret = -1; |
| 1898 | cmd_result = LTTNG_ERR_UNK; |
| 1899 | goto put_list; |
| 1900 | } |
| 1901 | |
| 1902 | /* |
| 1903 | * Ownership of `evaluation` transferred to the action executor |
| 1904 | * no matter the result. |
| 1905 | */ |
| 1906 | executor_status = action_executor_enqueue(state->executor, |
| 1907 | trigger, evaluation, &session_creds, |
| 1908 | client_list); |
| 1909 | evaluation = NULL; |
| 1910 | switch (executor_status) { |
| 1911 | case ACTION_EXECUTOR_STATUS_OK: |
| 1912 | break; |
| 1913 | case ACTION_EXECUTOR_STATUS_ERROR: |
| 1914 | case ACTION_EXECUTOR_STATUS_INVALID: |
| 1915 | /* |
| 1916 | * TODO Add trigger identification (name/id) when |
| 1917 | * it is added to the API. |
| 1918 | */ |
| 1919 | ERR("Fatal error occurred while enqueuing action associated with session rotation trigger"); |
| 1920 | ret = -1; |
| 1921 | goto put_list; |
| 1922 | case ACTION_EXECUTOR_STATUS_OVERFLOW: |
| 1923 | /* |
| 1924 | * TODO Add trigger identification (name/id) when |
| 1925 | * it is added to the API. |
| 1926 | * |
| 1927 | * Not a fatal error. |
| 1928 | */ |
| 1929 | WARN("No space left when enqueuing action associated with session rotation trigger"); |
| 1930 | ret = 0; |
| 1931 | goto put_list; |
| 1932 | default: |
| 1933 | abort(); |
| 1934 | } |
| 1935 | |
| 1936 | put_list: |
| 1937 | notification_client_list_put(client_list); |
| 1938 | if (caa_unlikely(ret)) { |
| 1939 | break; |
| 1940 | } |
| 1941 | } |
| 1942 | end: |
| 1943 | session_info_put(session_info); |
| 1944 | *_cmd_result = cmd_result; |
| 1945 | rcu_read_unlock(); |
| 1946 | return ret; |
| 1947 | } |
| 1948 | |
| 1949 | static |
| 1950 | int handle_notification_thread_command_add_tracer_event_source( |
| 1951 | struct notification_thread_state *state, |
| 1952 | int tracer_event_source_fd, |
| 1953 | enum lttng_domain_type domain_type, |
| 1954 | enum lttng_error_code *_cmd_result) |
| 1955 | { |
| 1956 | int ret = 0; |
| 1957 | enum lttng_error_code cmd_result = LTTNG_OK; |
| 1958 | struct notification_event_tracer_event_source_element *element = NULL; |
| 1959 | |
| 1960 | element = zmalloc(sizeof(*element)); |
| 1961 | if (!element) { |
| 1962 | cmd_result = LTTNG_ERR_NOMEM; |
| 1963 | ret = -1; |
| 1964 | goto end; |
| 1965 | } |
| 1966 | |
| 1967 | element->fd = tracer_event_source_fd; |
| 1968 | element->domain = domain_type; |
| 1969 | |
| 1970 | cds_list_add(&element->node, &state->tracer_event_sources_list); |
| 1971 | |
| 1972 | DBG3("[notification-thread] Adding tracer event source fd to poll set: tracer_event_source_fd = %d, domain = '%s'", |
| 1973 | tracer_event_source_fd, |
| 1974 | lttng_domain_type_str(domain_type)); |
| 1975 | |
| 1976 | /* Adding the read side pipe to the event poll. */ |
| 1977 | ret = lttng_poll_add(&state->events, tracer_event_source_fd, LPOLLIN | LPOLLERR); |
| 1978 | if (ret < 0) { |
| 1979 | ERR("[notification-thread] Failed to add tracer event source to poll set: tracer_event_source_fd = %d, domain = '%s'", |
| 1980 | tracer_event_source_fd, |
| 1981 | lttng_domain_type_str(element->domain)); |
| 1982 | cds_list_del(&element->node); |
| 1983 | free(element); |
| 1984 | goto end; |
| 1985 | } |
| 1986 | |
| 1987 | element->is_fd_in_poll_set = true; |
| 1988 | |
| 1989 | end: |
| 1990 | *_cmd_result = cmd_result; |
| 1991 | return ret; |
| 1992 | } |
| 1993 | |
| 1994 | static |
| 1995 | int drain_event_notifier_notification_pipe( |
| 1996 | struct notification_thread_state *state, |
| 1997 | int pipe, enum lttng_domain_type domain) |
| 1998 | { |
| 1999 | struct lttng_poll_event events = {0}; |
| 2000 | int ret; |
| 2001 | |
| 2002 | ret = lttng_poll_create(&events, 1, LTTNG_CLOEXEC); |
| 2003 | if (ret < 0) { |
| 2004 | ERR("[notification-thread] Error creating lttng_poll_event"); |
| 2005 | goto end; |
| 2006 | } |
| 2007 | |
| 2008 | ret = lttng_poll_add(&events, pipe, LPOLLIN); |
| 2009 | if (ret < 0) { |
| 2010 | ERR("[notification-thread] Error adding fd event notifier notification pipe to lttng_poll_event: fd = %d", |
| 2011 | pipe); |
| 2012 | goto end; |
| 2013 | } |
| 2014 | |
| 2015 | while (true) { |
| 2016 | /* |
| 2017 | * Continue to consume notifications as long as there are new |
| 2018 | * ones coming in. The tracer has been asked to stop producing |
| 2019 | * them. |
| 2020 | * |
| 2021 | * LPOLLIN is explicitly checked since LPOLLHUP is implicitly |
| 2022 | * monitored (on Linux, at least) and will be returned when |
| 2023 | * the pipe is closed but empty. |
| 2024 | */ |
| 2025 | ret = lttng_poll_wait_interruptible(&events, 0); |
| 2026 | if (ret == 0 || (LTTNG_POLL_GETEV(&events, 0) & LPOLLIN) == 0) { |
| 2027 | /* No more notification to be read on this pipe. */ |
| 2028 | ret = 0; |
| 2029 | goto end; |
| 2030 | } else if (ret < 0) { |
| 2031 | PERROR("Failed on lttng_poll_wait_interruptible() call"); |
| 2032 | ret = -1; |
| 2033 | goto end; |
| 2034 | } |
| 2035 | |
| 2036 | ret = handle_one_event_notifier_notification(state, pipe, domain); |
| 2037 | if (ret) { |
| 2038 | ERR("[notification-thread] Error consuming an event notifier notification from pipe: fd = %d", |
| 2039 | pipe); |
| 2040 | } |
| 2041 | } |
| 2042 | end: |
| 2043 | lttng_poll_clean(&events); |
| 2044 | return ret; |
| 2045 | } |
| 2046 | |
| 2047 | static |
| 2048 | int handle_notification_thread_command_remove_tracer_event_source( |
| 2049 | struct notification_thread_state *state, |
| 2050 | int tracer_event_source_fd, |
| 2051 | enum lttng_error_code *_cmd_result) |
| 2052 | { |
| 2053 | int ret = 0; |
| 2054 | bool found = false; |
| 2055 | enum lttng_error_code cmd_result = LTTNG_OK; |
| 2056 | struct notification_event_tracer_event_source_element *source_element = NULL, *tmp; |
| 2057 | |
| 2058 | cds_list_for_each_entry_safe(source_element, tmp, |
| 2059 | &state->tracer_event_sources_list, node) { |
| 2060 | if (source_element->fd != tracer_event_source_fd) { |
| 2061 | continue; |
| 2062 | } |
| 2063 | |
| 2064 | DBG("[notification-thread] Removed tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'", |
| 2065 | tracer_event_source_fd, |
| 2066 | lttng_domain_type_str(source_element->domain)); |
| 2067 | cds_list_del(&source_element->node); |
| 2068 | found = true; |
| 2069 | break; |
| 2070 | } |
| 2071 | |
| 2072 | if (!found) { |
| 2073 | /* |
| 2074 | * This is temporarily allowed since the poll activity set is |
| 2075 | * not properly cleaned-up for the moment. This is adressed in |
| 2076 | * an upcoming fix. |
| 2077 | */ |
| 2078 | source_element = NULL; |
| 2079 | goto end; |
| 2080 | } |
| 2081 | |
| 2082 | if (!source_element->is_fd_in_poll_set) { |
| 2083 | /* Skip the poll set removal. */ |
| 2084 | goto end; |
| 2085 | } |
| 2086 | |
| 2087 | DBG3("[notification-thread] Removing tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'", |
| 2088 | tracer_event_source_fd, |
| 2089 | lttng_domain_type_str(source_element->domain)); |
| 2090 | |
| 2091 | /* Removing the fd from the event poll set. */ |
| 2092 | ret = lttng_poll_del(&state->events, tracer_event_source_fd); |
| 2093 | if (ret < 0) { |
| 2094 | ERR("[notification-thread] Failed to remove tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'", |
| 2095 | tracer_event_source_fd, |
| 2096 | lttng_domain_type_str(source_element->domain)); |
| 2097 | cmd_result = LTTNG_ERR_FATAL; |
| 2098 | goto end; |
| 2099 | } |
| 2100 | |
| 2101 | source_element->is_fd_in_poll_set = false; |
| 2102 | |
| 2103 | ret = drain_event_notifier_notification_pipe(state, tracer_event_source_fd, |
| 2104 | source_element->domain); |
| 2105 | if (ret) { |
| 2106 | ERR("[notification-thread] Error draining event notifier notification: tracer_event_source_fd = %d, domain = %s", |
| 2107 | tracer_event_source_fd, |
| 2108 | lttng_domain_type_str(source_element->domain)); |
| 2109 | cmd_result = LTTNG_ERR_FATAL; |
| 2110 | goto end; |
| 2111 | } |
| 2112 | |
| 2113 | /* |
| 2114 | * The drain_event_notifier_notification_pipe() call might have read |
| 2115 | * data from an fd that we received in event in the latest _poll_wait() |
| 2116 | * call. Make sure the thread call poll_wait() again to ensure we have |
| 2117 | * a clean state. |
| 2118 | */ |
| 2119 | state->restart_poll = true; |
| 2120 | |
| 2121 | end: |
| 2122 | free(source_element); |
| 2123 | *_cmd_result = cmd_result; |
| 2124 | return ret; |
| 2125 | } |
| 2126 | |
| 2127 | static |
| 2128 | int condition_on_event_update_error_count(struct lttng_trigger *trigger) |
| 2129 | { |
| 2130 | int ret = 0; |
| 2131 | uint64_t error_count = 0; |
| 2132 | struct lttng_condition *condition; |
| 2133 | enum event_notifier_error_accounting_status status; |
| 2134 | |
| 2135 | condition = lttng_trigger_get_condition(trigger); |
| 2136 | assert(lttng_condition_get_type(condition) == |
| 2137 | LTTNG_CONDITION_TYPE_ON_EVENT); |
| 2138 | |
| 2139 | status = event_notifier_error_accounting_get_count(trigger, &error_count); |
| 2140 | if (status != EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK) { |
| 2141 | uid_t trigger_owner_uid; |
| 2142 | const char *trigger_name; |
| 2143 | const enum lttng_trigger_status trigger_status = |
| 2144 | lttng_trigger_get_owner_uid( |
| 2145 | trigger, &trigger_owner_uid); |
| 2146 | |
| 2147 | assert(trigger_status == LTTNG_TRIGGER_STATUS_OK); |
| 2148 | if (lttng_trigger_get_name(trigger, &trigger_name) != LTTNG_TRIGGER_STATUS_OK) { |
| 2149 | trigger_name = "(unnamed)"; |
| 2150 | } |
| 2151 | |
| 2152 | ERR("Failed to get event notifier error count of trigger for update: trigger owner = %d, trigger name = '%s'", |
| 2153 | trigger_owner_uid, trigger_name); |
| 2154 | ret = -1; |
| 2155 | } |
| 2156 | |
| 2157 | lttng_condition_on_event_set_error_count(condition, error_count); |
| 2158 | return ret; |
| 2159 | } |
| 2160 | |
| 2161 | int handle_notification_thread_remove_tracer_event_source_no_result( |
| 2162 | struct notification_thread_state *state, |
| 2163 | int tracer_event_source_fd) |
| 2164 | { |
| 2165 | int ret; |
| 2166 | enum lttng_error_code cmd_result; |
| 2167 | |
| 2168 | ret = handle_notification_thread_command_remove_tracer_event_source( |
| 2169 | state, tracer_event_source_fd, &cmd_result); |
| 2170 | (void) cmd_result; |
| 2171 | return ret; |
| 2172 | } |
| 2173 | |
| 2174 | static |
| 2175 | bool action_type_needs_tracer_notifier(enum lttng_action_type action_type) |
| 2176 | { |
| 2177 | switch (action_type) { |
| 2178 | case LTTNG_ACTION_TYPE_NOTIFY: |
| 2179 | case LTTNG_ACTION_TYPE_START_SESSION: |
| 2180 | case LTTNG_ACTION_TYPE_STOP_SESSION: |
| 2181 | case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION: |
| 2182 | case LTTNG_ACTION_TYPE_ROTATE_SESSION: |
| 2183 | return true; |
| 2184 | case LTTNG_ACTION_TYPE_GROUP: |
| 2185 | case LTTNG_ACTION_TYPE_UNKNOWN: |
| 2186 | default: |
| 2187 | abort(); |
| 2188 | } |
| 2189 | } |
| 2190 | |
| 2191 | static |
| 2192 | bool action_needs_tracer_notifier(const struct lttng_action *action) |
| 2193 | { |
| 2194 | bool needs_tracer_notifier = false; |
| 2195 | unsigned int i, count; |
| 2196 | enum lttng_action_status action_status; |
| 2197 | enum lttng_action_type action_type; |
| 2198 | |
| 2199 | assert(action); |
| 2200 | /* If there is only one action. Check if it needs a tracer notifier. */ |
| 2201 | action_type = lttng_action_get_type(action); |
| 2202 | if (action_type != LTTNG_ACTION_TYPE_GROUP) { |
| 2203 | needs_tracer_notifier = action_type_needs_tracer_notifier( |
| 2204 | action_type); |
| 2205 | goto end; |
| 2206 | } |
| 2207 | |
| 2208 | /* |
| 2209 | * Iterate over all the actions of the action group and check if any of |
| 2210 | * them needs a tracer notifier. |
| 2211 | */ |
| 2212 | action_status = lttng_action_group_get_count(action, &count); |
| 2213 | assert(action_status == LTTNG_ACTION_STATUS_OK); |
| 2214 | for (i = 0; i < count; i++) { |
| 2215 | const struct lttng_action *inner_action = |
| 2216 | lttng_action_group_get_at_index(action, i); |
| 2217 | |
| 2218 | action_type = lttng_action_get_type(inner_action); |
| 2219 | if (action_type_needs_tracer_notifier(action_type)) { |
| 2220 | needs_tracer_notifier = true; |
| 2221 | goto end; |
| 2222 | } |
| 2223 | } |
| 2224 | |
| 2225 | end: |
| 2226 | return needs_tracer_notifier; |
| 2227 | } |
| 2228 | |
| 2229 | /* |
| 2230 | * A given trigger needs a tracer notifier if |
| 2231 | * it has an event-rule condition, |
| 2232 | * AND |
| 2233 | * it has one or more sessiond-execution action. |
| 2234 | */ |
| 2235 | static |
| 2236 | bool trigger_needs_tracer_notifier(const struct lttng_trigger *trigger) |
| 2237 | { |
| 2238 | bool needs_tracer_notifier = false; |
| 2239 | const struct lttng_condition *condition = |
| 2240 | lttng_trigger_get_const_condition(trigger); |
| 2241 | const struct lttng_action *action = |
| 2242 | lttng_trigger_get_const_action(trigger); |
| 2243 | |
| 2244 | switch (lttng_condition_get_type(condition)) { |
| 2245 | case LTTNG_CONDITION_TYPE_ON_EVENT: |
| 2246 | needs_tracer_notifier = action_needs_tracer_notifier(action); |
| 2247 | goto end; |
| 2248 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: |
| 2249 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: |
| 2250 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: |
| 2251 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: |
| 2252 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: |
| 2253 | goto end; |
| 2254 | case LTTNG_CONDITION_TYPE_UNKNOWN: |
| 2255 | default: |
| 2256 | abort(); |
| 2257 | } |
| 2258 | end: |
| 2259 | return needs_tracer_notifier; |
| 2260 | } |
| 2261 | |
| 2262 | static int handle_notification_thread_command_list_triggers( |
| 2263 | struct notification_thread_handle *handle, |
| 2264 | struct notification_thread_state *state, |
| 2265 | uid_t client_uid, |
| 2266 | struct lttng_triggers **triggers, |
| 2267 | enum lttng_error_code *_cmd_result) |
| 2268 | { |
| 2269 | int ret = 0; |
| 2270 | enum lttng_error_code cmd_result = LTTNG_OK; |
| 2271 | struct cds_lfht_iter iter; |
| 2272 | struct lttng_trigger_ht_element *trigger_ht_element; |
| 2273 | struct lttng_triggers *local_triggers = NULL; |
| 2274 | const struct lttng_credentials *creds; |
| 2275 | |
| 2276 | rcu_read_lock(); |
| 2277 | |
| 2278 | local_triggers = lttng_triggers_create(); |
| 2279 | if (!local_triggers) { |
| 2280 | /* Not a fatal error. */ |
| 2281 | cmd_result = LTTNG_ERR_NOMEM; |
| 2282 | goto end; |
| 2283 | } |
| 2284 | |
| 2285 | cds_lfht_for_each_entry(state->triggers_ht, &iter, |
| 2286 | trigger_ht_element, node) { |
| 2287 | /* |
| 2288 | * Only return the triggers to which the client has access. |
| 2289 | * The root user has visibility over all triggers. |
| 2290 | */ |
| 2291 | creds = lttng_trigger_get_credentials(trigger_ht_element->trigger); |
| 2292 | if (client_uid != lttng_credentials_get_uid(creds) && client_uid != 0) { |
| 2293 | continue; |
| 2294 | } |
| 2295 | |
| 2296 | if (trigger_needs_tracer_notifier(trigger_ht_element->trigger)) { |
| 2297 | ret = condition_on_event_update_error_count( |
| 2298 | trigger_ht_element->trigger); |
| 2299 | assert(!ret); |
| 2300 | } |
| 2301 | |
| 2302 | ret = lttng_triggers_add(local_triggers, |
| 2303 | trigger_ht_element->trigger); |
| 2304 | if (ret < 0) { |
| 2305 | /* Not a fatal error. */ |
| 2306 | ret = 0; |
| 2307 | cmd_result = LTTNG_ERR_NOMEM; |
| 2308 | goto end; |
| 2309 | } |
| 2310 | } |
| 2311 | |
| 2312 | /* Transferring ownership to the caller. */ |
| 2313 | *triggers = local_triggers; |
| 2314 | local_triggers = NULL; |
| 2315 | |
| 2316 | end: |
| 2317 | rcu_read_unlock(); |
| 2318 | lttng_triggers_destroy(local_triggers); |
| 2319 | *_cmd_result = cmd_result; |
| 2320 | return ret; |
| 2321 | } |
| 2322 | |
| 2323 | static |
| 2324 | bool condition_is_supported(struct lttng_condition *condition) |
| 2325 | { |
| 2326 | bool is_supported; |
| 2327 | |
| 2328 | switch (lttng_condition_get_type(condition)) { |
| 2329 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: |
| 2330 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: |
| 2331 | { |
| 2332 | int ret; |
| 2333 | enum lttng_domain_type domain; |
| 2334 | |
| 2335 | ret = lttng_condition_buffer_usage_get_domain_type(condition, |
| 2336 | &domain); |
| 2337 | assert(ret == 0); |
| 2338 | |
| 2339 | if (domain != LTTNG_DOMAIN_KERNEL) { |
| 2340 | is_supported = true; |
| 2341 | goto end; |
| 2342 | } |
| 2343 | |
| 2344 | /* |
| 2345 | * Older kernel tracers don't expose the API to monitor their |
| 2346 | * buffers. Therefore, we reject triggers that require that |
| 2347 | * mechanism to be available to be evaluated. |
| 2348 | * |
| 2349 | * Assume unsupported on error. |
| 2350 | */ |
| 2351 | is_supported = kernel_supports_ring_buffer_snapshot_sample_positions() == 1; |
| 2352 | break; |
| 2353 | } |
| 2354 | case LTTNG_CONDITION_TYPE_ON_EVENT: |
| 2355 | { |
| 2356 | const struct lttng_event_rule *event_rule; |
| 2357 | enum lttng_domain_type domain; |
| 2358 | const enum lttng_condition_status status = |
| 2359 | lttng_condition_on_event_get_rule( |
| 2360 | condition, &event_rule); |
| 2361 | |
| 2362 | assert(status == LTTNG_CONDITION_STATUS_OK); |
| 2363 | |
| 2364 | domain = lttng_event_rule_get_domain_type(event_rule); |
| 2365 | if (domain != LTTNG_DOMAIN_KERNEL) { |
| 2366 | is_supported = true; |
| 2367 | goto end; |
| 2368 | } |
| 2369 | |
| 2370 | /* |
| 2371 | * Older kernel tracers can't emit notification. Therefore, we |
| 2372 | * reject triggers that require that mechanism to be available |
| 2373 | * to be evaluated. |
| 2374 | * |
| 2375 | * Assume unsupported on error. |
| 2376 | */ |
| 2377 | is_supported = kernel_supports_event_notifiers() == 1; |
| 2378 | break; |
| 2379 | } |
| 2380 | default: |
| 2381 | is_supported = true; |
| 2382 | } |
| 2383 | end: |
| 2384 | return is_supported; |
| 2385 | } |
| 2386 | |
| 2387 | /* Must be called with RCU read lock held. */ |
| 2388 | static |
| 2389 | int bind_trigger_to_matching_session(struct lttng_trigger *trigger, |
| 2390 | struct notification_thread_state *state) |
| 2391 | { |
| 2392 | int ret = 0; |
| 2393 | const struct lttng_condition *condition; |
| 2394 | const char *session_name; |
| 2395 | struct lttng_session_trigger_list *trigger_list; |
| 2396 | |
| 2397 | condition = lttng_trigger_get_const_condition(trigger); |
| 2398 | switch (lttng_condition_get_type(condition)) { |
| 2399 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: |
| 2400 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: |
| 2401 | { |
| 2402 | enum lttng_condition_status status; |
| 2403 | |
| 2404 | status = lttng_condition_session_rotation_get_session_name( |
| 2405 | condition, &session_name); |
| 2406 | if (status != LTTNG_CONDITION_STATUS_OK) { |
| 2407 | ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name"); |
| 2408 | ret = -1; |
| 2409 | goto end; |
| 2410 | } |
| 2411 | break; |
| 2412 | } |
| 2413 | default: |
| 2414 | ret = -1; |
| 2415 | goto end; |
| 2416 | } |
| 2417 | |
| 2418 | trigger_list = get_session_trigger_list(state, session_name); |
| 2419 | if (!trigger_list) { |
| 2420 | DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system", |
| 2421 | session_name); |
| 2422 | goto end; |
| 2423 | |
| 2424 | } |
| 2425 | |
| 2426 | DBG("[notification-thread] Newly registered trigger bound to session \"%s\"", |
| 2427 | session_name); |
| 2428 | ret = lttng_session_trigger_list_add(trigger_list, trigger); |
| 2429 | end: |
| 2430 | return ret; |
| 2431 | } |
| 2432 | |
| 2433 | /* Must be called with RCU read lock held. */ |
| 2434 | static |
| 2435 | int bind_trigger_to_matching_channels(struct lttng_trigger *trigger, |
| 2436 | struct notification_thread_state *state) |
| 2437 | { |
| 2438 | int ret = 0; |
| 2439 | struct cds_lfht_node *node; |
| 2440 | struct cds_lfht_iter iter; |
| 2441 | struct channel_info *channel; |
| 2442 | |
| 2443 | cds_lfht_for_each_entry(state->channels_ht, &iter, channel, |
| 2444 | channels_ht_node) { |
| 2445 | struct lttng_trigger_list_element *trigger_list_element; |
| 2446 | struct lttng_channel_trigger_list *trigger_list; |
| 2447 | struct cds_lfht_iter lookup_iter; |
| 2448 | |
| 2449 | if (!trigger_applies_to_channel(trigger, channel)) { |
| 2450 | continue; |
| 2451 | } |
| 2452 | |
| 2453 | cds_lfht_lookup(state->channel_triggers_ht, |
| 2454 | hash_channel_key(&channel->key), |
| 2455 | match_channel_trigger_list, |
| 2456 | &channel->key, |
| 2457 | &lookup_iter); |
| 2458 | node = cds_lfht_iter_get_node(&lookup_iter); |
| 2459 | assert(node); |
| 2460 | trigger_list = caa_container_of(node, |
| 2461 | struct lttng_channel_trigger_list, |
| 2462 | channel_triggers_ht_node); |
| 2463 | |
| 2464 | trigger_list_element = zmalloc(sizeof(*trigger_list_element)); |
| 2465 | if (!trigger_list_element) { |
| 2466 | ret = -1; |
| 2467 | goto end; |
| 2468 | } |
| 2469 | CDS_INIT_LIST_HEAD(&trigger_list_element->node); |
| 2470 | trigger_list_element->trigger = trigger; |
| 2471 | cds_list_add(&trigger_list_element->node, &trigger_list->list); |
| 2472 | DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"", |
| 2473 | channel->name); |
| 2474 | } |
| 2475 | end: |
| 2476 | return ret; |
| 2477 | } |
| 2478 | |
| 2479 | static |
| 2480 | bool is_trigger_action_notify(const struct lttng_trigger *trigger) |
| 2481 | { |
| 2482 | bool is_notify = false; |
| 2483 | unsigned int i, count; |
| 2484 | enum lttng_action_status action_status; |
| 2485 | const struct lttng_action *action = |
| 2486 | lttng_trigger_get_const_action(trigger); |
| 2487 | enum lttng_action_type action_type; |
| 2488 | |
| 2489 | assert(action); |
| 2490 | action_type = lttng_action_get_type(action); |
| 2491 | if (action_type == LTTNG_ACTION_TYPE_NOTIFY) { |
| 2492 | is_notify = true; |
| 2493 | goto end; |
| 2494 | } else if (action_type != LTTNG_ACTION_TYPE_GROUP) { |
| 2495 | goto end; |
| 2496 | } |
| 2497 | |
| 2498 | action_status = lttng_action_group_get_count(action, &count); |
| 2499 | assert(action_status == LTTNG_ACTION_STATUS_OK); |
| 2500 | |
| 2501 | for (i = 0; i < count; i++) { |
| 2502 | const struct lttng_action *inner_action = |
| 2503 | lttng_action_group_get_at_index( |
| 2504 | action, i); |
| 2505 | |
| 2506 | action_type = lttng_action_get_type(inner_action); |
| 2507 | if (action_type == LTTNG_ACTION_TYPE_NOTIFY) { |
| 2508 | is_notify = true; |
| 2509 | goto end; |
| 2510 | } |
| 2511 | } |
| 2512 | |
| 2513 | end: |
| 2514 | return is_notify; |
| 2515 | } |
| 2516 | |
| 2517 | static bool trigger_name_taken(struct notification_thread_state *state, |
| 2518 | const struct lttng_trigger *trigger) |
| 2519 | { |
| 2520 | struct cds_lfht_iter iter; |
| 2521 | |
| 2522 | /* |
| 2523 | * No duplicata is allowed in the triggers_by_name_uid_ht. |
| 2524 | * The match is done against the trigger name and uid. |
| 2525 | */ |
| 2526 | cds_lfht_lookup(state->triggers_by_name_uid_ht, |
| 2527 | hash_trigger_by_name_uid(trigger), |
| 2528 | match_trigger_by_name_uid, |
| 2529 | trigger, |
| 2530 | &iter); |
| 2531 | return !!cds_lfht_iter_get_node(&iter); |
| 2532 | } |
| 2533 | |
| 2534 | static |
| 2535 | enum lttng_error_code generate_trigger_name( |
| 2536 | struct notification_thread_state *state, |
| 2537 | struct lttng_trigger *trigger, const char **name) |
| 2538 | { |
| 2539 | enum lttng_error_code ret_code = LTTNG_OK; |
| 2540 | bool taken = false; |
| 2541 | enum lttng_trigger_status status; |
| 2542 | |
| 2543 | do { |
| 2544 | const int ret = lttng_trigger_generate_name(trigger, |
| 2545 | state->trigger_id.name_offset++); |
| 2546 | if (ret) { |
| 2547 | /* The only reason this can fail right now. */ |
| 2548 | ret_code = LTTNG_ERR_NOMEM; |
| 2549 | break; |
| 2550 | } |
| 2551 | |
| 2552 | status = lttng_trigger_get_name(trigger, name); |
| 2553 | assert(status == LTTNG_TRIGGER_STATUS_OK); |
| 2554 | |
| 2555 | taken = trigger_name_taken(state, trigger); |
| 2556 | } while (taken || state->trigger_id.name_offset == UINT64_MAX); |
| 2557 | |
| 2558 | return ret_code; |
| 2559 | } |
| 2560 | |
| 2561 | /* |
| 2562 | * FIXME A client's credentials are not checked when registering a trigger. |
| 2563 | * |
| 2564 | * The effects of this are benign since: |
| 2565 | * - The client will succeed in registering the trigger, as it is valid, |
| 2566 | * - The trigger will, internally, be bound to the channel/session, |
| 2567 | * - The notifications will not be sent since the client's credentials |
| 2568 | * are checked against the channel at that moment. |
| 2569 | * |
| 2570 | * If this function returns a non-zero value, it means something is |
| 2571 | * fundamentally broken and the whole subsystem/thread will be torn down. |
| 2572 | * |
| 2573 | * If a non-fatal error occurs, just set the cmd_result to the appropriate |
| 2574 | * error code. |
| 2575 | */ |
| 2576 | static |
| 2577 | int handle_notification_thread_command_register_trigger( |
| 2578 | struct notification_thread_state *state, |
| 2579 | struct lttng_trigger *trigger, |
| 2580 | enum lttng_error_code *cmd_result) |
| 2581 | { |
| 2582 | int ret = 0; |
| 2583 | struct lttng_condition *condition; |
| 2584 | struct notification_client *client; |
| 2585 | struct notification_client_list *client_list = NULL; |
| 2586 | struct lttng_trigger_ht_element *trigger_ht_element = NULL; |
| 2587 | struct notification_client_list_element *client_list_element; |
| 2588 | struct notification_trigger_tokens_ht_element *trigger_tokens_ht_element = NULL; |
| 2589 | struct cds_lfht_node *node; |
| 2590 | struct cds_lfht_iter iter; |
| 2591 | const char* trigger_name; |
| 2592 | bool free_trigger = true; |
| 2593 | struct lttng_evaluation *evaluation = NULL; |
| 2594 | struct lttng_credentials object_creds; |
| 2595 | uid_t object_uid; |
| 2596 | gid_t object_gid; |
| 2597 | enum action_executor_status executor_status; |
| 2598 | const uint64_t trigger_tracer_token = |
| 2599 | state->trigger_id.next_tracer_token++; |
| 2600 | |
| 2601 | rcu_read_lock(); |
| 2602 | |
| 2603 | /* Set the trigger's tracer token. */ |
| 2604 | lttng_trigger_set_tracer_token(trigger, trigger_tracer_token); |
| 2605 | |
| 2606 | if (lttng_trigger_get_name(trigger, &trigger_name) == |
| 2607 | LTTNG_TRIGGER_STATUS_UNSET) { |
| 2608 | const enum lttng_error_code ret_code = generate_trigger_name( |
| 2609 | state, trigger, &trigger_name); |
| 2610 | |
| 2611 | if (ret_code != LTTNG_OK) { |
| 2612 | /* Fatal error. */ |
| 2613 | ret = -1; |
| 2614 | *cmd_result = ret_code; |
| 2615 | goto error; |
| 2616 | } |
| 2617 | } else if (trigger_name_taken(state, trigger)) { |
| 2618 | /* Not a fatal error. */ |
| 2619 | *cmd_result = LTTNG_ERR_TRIGGER_EXISTS; |
| 2620 | ret = 0; |
| 2621 | goto error; |
| 2622 | } |
| 2623 | |
| 2624 | condition = lttng_trigger_get_condition(trigger); |
| 2625 | assert(condition); |
| 2626 | |
| 2627 | /* Some conditions require tracers to implement a minimal ABI version. */ |
| 2628 | if (!condition_is_supported(condition)) { |
| 2629 | *cmd_result = LTTNG_ERR_NOT_SUPPORTED; |
| 2630 | goto error; |
| 2631 | } |
| 2632 | |
| 2633 | trigger_ht_element = zmalloc(sizeof(*trigger_ht_element)); |
| 2634 | if (!trigger_ht_element) { |
| 2635 | ret = -1; |
| 2636 | goto error; |
| 2637 | } |
| 2638 | |
| 2639 | /* Add trigger to the trigger_ht. */ |
| 2640 | cds_lfht_node_init(&trigger_ht_element->node); |
| 2641 | cds_lfht_node_init(&trigger_ht_element->node_by_name_uid); |
| 2642 | trigger_ht_element->trigger = trigger; |
| 2643 | |
| 2644 | node = cds_lfht_add_unique(state->triggers_ht, |
| 2645 | lttng_condition_hash(condition), |
| 2646 | match_trigger, |
| 2647 | trigger, |
| 2648 | &trigger_ht_element->node); |
| 2649 | if (node != &trigger_ht_element->node) { |
| 2650 | /* Not a fatal error, simply report it to the client. */ |
| 2651 | *cmd_result = LTTNG_ERR_TRIGGER_EXISTS; |
| 2652 | goto error_free_ht_element; |
| 2653 | } |
| 2654 | |
| 2655 | node = cds_lfht_add_unique(state->triggers_by_name_uid_ht, |
| 2656 | hash_trigger_by_name_uid(trigger), |
| 2657 | match_trigger_by_name_uid, |
| 2658 | trigger, |
| 2659 | &trigger_ht_element->node_by_name_uid); |
| 2660 | if (node != &trigger_ht_element->node_by_name_uid) { |
| 2661 | /* Not a fatal error, simply report it to the client. */ |
| 2662 | cds_lfht_del(state->triggers_ht, &trigger_ht_element->node); |
| 2663 | *cmd_result = LTTNG_ERR_TRIGGER_EXISTS; |
| 2664 | goto error_free_ht_element; |
| 2665 | } |
| 2666 | |
| 2667 | if (lttng_condition_get_type(condition) == LTTNG_CONDITION_TYPE_ON_EVENT) { |
| 2668 | trigger_tokens_ht_element = zmalloc(sizeof(*trigger_tokens_ht_element)); |
| 2669 | if (!trigger_tokens_ht_element) { |
| 2670 | /* Fatal error. */ |
| 2671 | ret = -1; |
| 2672 | cds_lfht_del(state->triggers_ht, |
| 2673 | &trigger_ht_element->node); |
| 2674 | cds_lfht_del(state->triggers_by_name_uid_ht, |
| 2675 | &trigger_ht_element->node_by_name_uid); |
| 2676 | goto error_free_ht_element; |
| 2677 | } |
| 2678 | |
| 2679 | /* Add trigger token to the trigger_tokens_ht. */ |
| 2680 | cds_lfht_node_init(&trigger_tokens_ht_element->node); |
| 2681 | trigger_tokens_ht_element->token = |
| 2682 | LTTNG_OPTIONAL_GET(trigger->tracer_token); |
| 2683 | trigger_tokens_ht_element->trigger = trigger; |
| 2684 | |
| 2685 | node = cds_lfht_add_unique(state->trigger_tokens_ht, |
| 2686 | hash_key_u64(&trigger_tokens_ht_element->token, |
| 2687 | lttng_ht_seed), |
| 2688 | match_trigger_token, |
| 2689 | &trigger_tokens_ht_element->token, |
| 2690 | &trigger_tokens_ht_element->node); |
| 2691 | if (node != &trigger_tokens_ht_element->node) { |
| 2692 | /* Internal corruption, fatal error. */ |
| 2693 | ret = -1; |
| 2694 | *cmd_result = LTTNG_ERR_TRIGGER_EXISTS; |
| 2695 | cds_lfht_del(state->triggers_ht, |
| 2696 | &trigger_ht_element->node); |
| 2697 | cds_lfht_del(state->triggers_by_name_uid_ht, |
| 2698 | &trigger_ht_element->node_by_name_uid); |
| 2699 | goto error_free_ht_element; |
| 2700 | } |
| 2701 | |
| 2702 | if (trigger_needs_tracer_notifier(trigger)) { |
| 2703 | uint64_t error_counter_index = 0; |
| 2704 | enum event_notifier_error_accounting_status error_accounting_status; |
| 2705 | |
| 2706 | error_accounting_status = event_notifier_error_accounting_register_event_notifier( |
| 2707 | trigger, &error_counter_index); |
| 2708 | if (error_accounting_status != EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK) { |
| 2709 | if (error_accounting_status == EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NO_INDEX_AVAILABLE) { |
| 2710 | DBG("Event notifier group error accounting map is full"); |
| 2711 | *cmd_result = LTTNG_ERR_EVENT_NOTIFIER_ERROR_ACCOUNTING_FULL; |
| 2712 | } else { |
| 2713 | ERR("Failed to register event notifier for error accounting"); |
| 2714 | *cmd_result = LTTNG_ERR_EVENT_NOTIFIER_REGISTRATION; |
| 2715 | } |
| 2716 | |
| 2717 | cds_lfht_del(state->triggers_ht, &trigger_ht_element->node); |
| 2718 | cds_lfht_del(state->triggers_by_name_uid_ht, &trigger_ht_element->node_by_name_uid); |
| 2719 | cds_lfht_del(state->trigger_tokens_ht, &trigger_tokens_ht_element->node); |
| 2720 | goto error_free_ht_element; |
| 2721 | } |
| 2722 | |
| 2723 | lttng_condition_on_event_set_error_counter_index( |
| 2724 | condition, error_counter_index); |
| 2725 | } |
| 2726 | |
| 2727 | } |
| 2728 | |
| 2729 | /* |
| 2730 | * Ownership of the trigger and of its wrapper was transfered to |
| 2731 | * the triggers_ht. Same for token ht element if necessary. |
| 2732 | */ |
| 2733 | trigger_tokens_ht_element = NULL; |
| 2734 | trigger_ht_element = NULL; |
| 2735 | free_trigger = false; |
| 2736 | |
| 2737 | /* |
| 2738 | * The rest only applies to triggers that have a "notify" action. |
| 2739 | * It is not skipped as this is the only action type currently |
| 2740 | * supported. |
| 2741 | */ |
| 2742 | if (is_trigger_action_notify(trigger)) { |
| 2743 | client_list = notification_client_list_create(trigger); |
| 2744 | if (!client_list) { |
| 2745 | ret = -1; |
| 2746 | goto error_free_ht_element; |
| 2747 | } |
| 2748 | |
| 2749 | /* Build a list of clients to which this new trigger applies. */ |
| 2750 | cds_lfht_for_each_entry (state->client_socket_ht, &iter, client, |
| 2751 | client_socket_ht_node) { |
| 2752 | if (!trigger_applies_to_client(trigger, client)) { |
| 2753 | continue; |
| 2754 | } |
| 2755 | |
| 2756 | client_list_element = |
| 2757 | zmalloc(sizeof(*client_list_element)); |
| 2758 | if (!client_list_element) { |
| 2759 | ret = -1; |
| 2760 | goto error_put_client_list; |
| 2761 | } |
| 2762 | |
| 2763 | CDS_INIT_LIST_HEAD(&client_list_element->node); |
| 2764 | client_list_element->client = client; |
| 2765 | cds_list_add(&client_list_element->node, |
| 2766 | &client_list->list); |
| 2767 | } |
| 2768 | |
| 2769 | /* |
| 2770 | * Client list ownership transferred to the |
| 2771 | * notification_trigger_clients_ht. |
| 2772 | */ |
| 2773 | publish_notification_client_list(state, client_list); |
| 2774 | } |
| 2775 | |
| 2776 | switch (get_condition_binding_object(condition)) { |
| 2777 | case LTTNG_OBJECT_TYPE_SESSION: |
| 2778 | /* Add the trigger to the list if it matches a known session. */ |
| 2779 | ret = bind_trigger_to_matching_session(trigger, state); |
| 2780 | if (ret) { |
| 2781 | goto error_put_client_list; |
| 2782 | } |
| 2783 | break; |
| 2784 | case LTTNG_OBJECT_TYPE_CHANNEL: |
| 2785 | /* |
| 2786 | * Add the trigger to list of triggers bound to the channels |
| 2787 | * currently known. |
| 2788 | */ |
| 2789 | ret = bind_trigger_to_matching_channels(trigger, state); |
| 2790 | if (ret) { |
| 2791 | goto error_put_client_list; |
| 2792 | } |
| 2793 | break; |
| 2794 | case LTTNG_OBJECT_TYPE_NONE: |
| 2795 | break; |
| 2796 | default: |
| 2797 | ERR("Unknown object type on which to bind a newly registered trigger was encountered"); |
| 2798 | ret = -1; |
| 2799 | goto error_put_client_list; |
| 2800 | } |
| 2801 | |
| 2802 | /* |
| 2803 | * The new trigger's condition must be evaluated against the current |
| 2804 | * state. |
| 2805 | * |
| 2806 | * In the case of `notify` action, nothing preventing clients from |
| 2807 | * subscribing to a condition before the corresponding trigger is |
| 2808 | * registered, we have to evaluate this new condition right away. |
| 2809 | * |
| 2810 | * At some point, we were waiting for the next "evaluation" (e.g. on |
| 2811 | * reception of a channel sample) to evaluate this new condition, but |
| 2812 | * that was broken. |
| 2813 | * |
| 2814 | * The reason it was broken is that waiting for the next sample |
| 2815 | * does not allow us to properly handle transitions for edge-triggered |
| 2816 | * conditions. |
| 2817 | * |
| 2818 | * Consider this example: when we handle a new channel sample, we |
| 2819 | * evaluate each conditions twice: once with the previous state, and |
| 2820 | * again with the newest state. We then use those two results to |
| 2821 | * determine whether a state change happened: a condition was false and |
| 2822 | * became true. If a state change happened, we have to notify clients. |
| 2823 | * |
| 2824 | * Now, if a client subscribes to a given notification and registers |
| 2825 | * a trigger *after* that subscription, we have to make sure the |
| 2826 | * condition is evaluated at this point while considering only the |
| 2827 | * current state. Otherwise, the next evaluation cycle may only see |
| 2828 | * that the evaluations remain the same (true for samples n-1 and n) and |
| 2829 | * the client will never know that the condition has been met. |
| 2830 | */ |
| 2831 | switch (get_condition_binding_object(condition)) { |
| 2832 | case LTTNG_OBJECT_TYPE_SESSION: |
| 2833 | ret = evaluate_session_condition_for_client(condition, state, |
| 2834 | &evaluation, &object_uid, |
| 2835 | &object_gid); |
| 2836 | LTTNG_OPTIONAL_SET(&object_creds.uid, object_uid); |
| 2837 | LTTNG_OPTIONAL_SET(&object_creds.gid, object_gid); |
| 2838 | break; |
| 2839 | case LTTNG_OBJECT_TYPE_CHANNEL: |
| 2840 | ret = evaluate_channel_condition_for_client(condition, state, |
| 2841 | &evaluation, &object_uid, |
| 2842 | &object_gid); |
| 2843 | LTTNG_OPTIONAL_SET(&object_creds.uid, object_uid); |
| 2844 | LTTNG_OPTIONAL_SET(&object_creds.gid, object_gid); |
| 2845 | break; |
| 2846 | case LTTNG_OBJECT_TYPE_NONE: |
| 2847 | ret = 0; |
| 2848 | break; |
| 2849 | case LTTNG_OBJECT_TYPE_UNKNOWN: |
| 2850 | default: |
| 2851 | ret = -1; |
| 2852 | break; |
| 2853 | } |
| 2854 | |
| 2855 | if (ret) { |
| 2856 | /* Fatal error. */ |
| 2857 | goto error_put_client_list; |
| 2858 | } |
| 2859 | |
| 2860 | DBG("Newly registered trigger's condition evaluated to %s", |
| 2861 | evaluation ? "true" : "false"); |
| 2862 | if (!evaluation) { |
| 2863 | /* Evaluation yielded nothing. Normal exit. */ |
| 2864 | ret = 0; |
| 2865 | goto end; |
| 2866 | } |
| 2867 | |
| 2868 | /* |
| 2869 | * Ownership of `evaluation` transferred to the action executor |
| 2870 | * no matter the result. |
| 2871 | */ |
| 2872 | executor_status = action_executor_enqueue(state->executor, trigger, |
| 2873 | evaluation, &object_creds, client_list); |
| 2874 | evaluation = NULL; |
| 2875 | switch (executor_status) { |
| 2876 | case ACTION_EXECUTOR_STATUS_OK: |
| 2877 | break; |
| 2878 | case ACTION_EXECUTOR_STATUS_ERROR: |
| 2879 | case ACTION_EXECUTOR_STATUS_INVALID: |
| 2880 | /* |
| 2881 | * TODO Add trigger identification (name/id) when |
| 2882 | * it is added to the API. |
| 2883 | */ |
| 2884 | ERR("Fatal error occurred while enqueuing action associated to newly registered trigger"); |
| 2885 | ret = -1; |
| 2886 | goto error_put_client_list; |
| 2887 | case ACTION_EXECUTOR_STATUS_OVERFLOW: |
| 2888 | /* |
| 2889 | * TODO Add trigger identification (name/id) when |
| 2890 | * it is added to the API. |
| 2891 | * |
| 2892 | * Not a fatal error. |
| 2893 | */ |
| 2894 | WARN("No space left when enqueuing action associated to newly registered trigger"); |
| 2895 | ret = 0; |
| 2896 | goto end; |
| 2897 | default: |
| 2898 | abort(); |
| 2899 | } |
| 2900 | |
| 2901 | end: |
| 2902 | *cmd_result = LTTNG_OK; |
| 2903 | DBG("Registered trigger: name = `%s`, tracer token = %" PRIu64, |
| 2904 | trigger_name, trigger_tracer_token); |
| 2905 | |
| 2906 | error_put_client_list: |
| 2907 | notification_client_list_put(client_list); |
| 2908 | |
| 2909 | error_free_ht_element: |
| 2910 | if (trigger_ht_element) { |
| 2911 | /* Delayed removal due to RCU constraint on delete. */ |
| 2912 | call_rcu(&trigger_ht_element->rcu_node, |
| 2913 | free_lttng_trigger_ht_element_rcu); |
| 2914 | } |
| 2915 | |
| 2916 | free(trigger_tokens_ht_element); |
| 2917 | error: |
| 2918 | if (free_trigger) { |
| 2919 | lttng_trigger_destroy(trigger); |
| 2920 | } |
| 2921 | rcu_read_unlock(); |
| 2922 | return ret; |
| 2923 | } |
| 2924 | |
| 2925 | static |
| 2926 | void free_lttng_trigger_ht_element_rcu(struct rcu_head *node) |
| 2927 | { |
| 2928 | free(caa_container_of(node, struct lttng_trigger_ht_element, |
| 2929 | rcu_node)); |
| 2930 | } |
| 2931 | |
| 2932 | static |
| 2933 | void free_notification_trigger_tokens_ht_element_rcu(struct rcu_head *node) |
| 2934 | { |
| 2935 | free(caa_container_of(node, struct notification_trigger_tokens_ht_element, |
| 2936 | rcu_node)); |
| 2937 | } |
| 2938 | |
| 2939 | static |
| 2940 | int handle_notification_thread_command_unregister_trigger( |
| 2941 | struct notification_thread_state *state, |
| 2942 | const struct lttng_trigger *trigger, |
| 2943 | enum lttng_error_code *_cmd_reply) |
| 2944 | { |
| 2945 | struct cds_lfht_iter iter; |
| 2946 | struct cds_lfht_node *triggers_ht_node; |
| 2947 | struct lttng_channel_trigger_list *trigger_list; |
| 2948 | struct notification_client_list *client_list; |
| 2949 | struct lttng_trigger_ht_element *trigger_ht_element = NULL; |
| 2950 | const struct lttng_condition *condition = lttng_trigger_get_const_condition( |
| 2951 | trigger); |
| 2952 | enum lttng_error_code cmd_reply; |
| 2953 | |
| 2954 | rcu_read_lock(); |
| 2955 | |
| 2956 | cds_lfht_lookup(state->triggers_ht, |
| 2957 | lttng_condition_hash(condition), |
| 2958 | match_trigger, |
| 2959 | trigger, |
| 2960 | &iter); |
| 2961 | triggers_ht_node = cds_lfht_iter_get_node(&iter); |
| 2962 | if (!triggers_ht_node) { |
| 2963 | cmd_reply = LTTNG_ERR_TRIGGER_NOT_FOUND; |
| 2964 | goto end; |
| 2965 | } else { |
| 2966 | cmd_reply = LTTNG_OK; |
| 2967 | } |
| 2968 | |
| 2969 | /* Remove trigger from channel_triggers_ht. */ |
| 2970 | cds_lfht_for_each_entry(state->channel_triggers_ht, &iter, trigger_list, |
| 2971 | channel_triggers_ht_node) { |
| 2972 | struct lttng_trigger_list_element *trigger_element, *tmp; |
| 2973 | |
| 2974 | cds_list_for_each_entry_safe(trigger_element, tmp, |
| 2975 | &trigger_list->list, node) { |
| 2976 | if (!lttng_trigger_is_equal(trigger, trigger_element->trigger)) { |
| 2977 | continue; |
| 2978 | } |
| 2979 | |
| 2980 | DBG("[notification-thread] Removed trigger from channel_triggers_ht"); |
| 2981 | cds_list_del(&trigger_element->node); |
| 2982 | /* A trigger can only appear once per channel */ |
| 2983 | break; |
| 2984 | } |
| 2985 | } |
| 2986 | |
| 2987 | if (lttng_condition_get_type(condition) == |
| 2988 | LTTNG_CONDITION_TYPE_ON_EVENT) { |
| 2989 | struct notification_trigger_tokens_ht_element |
| 2990 | *trigger_tokens_ht_element; |
| 2991 | |
| 2992 | cds_lfht_for_each_entry (state->trigger_tokens_ht, &iter, |
| 2993 | trigger_tokens_ht_element, node) { |
| 2994 | if (!lttng_trigger_is_equal(trigger, |
| 2995 | trigger_tokens_ht_element->trigger)) { |
| 2996 | continue; |
| 2997 | } |
| 2998 | |
| 2999 | if (trigger_needs_tracer_notifier(trigger_tokens_ht_element->trigger)) { |
| 3000 | event_notifier_error_accounting_unregister_event_notifier( |
| 3001 | trigger_tokens_ht_element->trigger); |
| 3002 | } |
| 3003 | |
| 3004 | DBG("[notification-thread] Removed trigger from tokens_ht"); |
| 3005 | cds_lfht_del(state->trigger_tokens_ht, |
| 3006 | &trigger_tokens_ht_element->node); |
| 3007 | call_rcu(&trigger_tokens_ht_element->rcu_node, |
| 3008 | free_notification_trigger_tokens_ht_element_rcu); |
| 3009 | |
| 3010 | break; |
| 3011 | } |
| 3012 | } |
| 3013 | |
| 3014 | if (is_trigger_action_notify(trigger)) { |
| 3015 | /* |
| 3016 | * Remove and release the client list from |
| 3017 | * notification_trigger_clients_ht. |
| 3018 | */ |
| 3019 | client_list = get_client_list_from_condition(state, condition); |
| 3020 | assert(client_list); |
| 3021 | |
| 3022 | /* Put new reference and the hashtable's reference. */ |
| 3023 | notification_client_list_put(client_list); |
| 3024 | notification_client_list_put(client_list); |
| 3025 | client_list = NULL; |
| 3026 | } |
| 3027 | |
| 3028 | /* Remove trigger from triggers_ht. */ |
| 3029 | trigger_ht_element = caa_container_of(triggers_ht_node, |
| 3030 | struct lttng_trigger_ht_element, node); |
| 3031 | cds_lfht_del(state->triggers_by_name_uid_ht, &trigger_ht_element->node_by_name_uid); |
| 3032 | cds_lfht_del(state->triggers_ht, triggers_ht_node); |
| 3033 | |
| 3034 | /* Release the ownership of the trigger. */ |
| 3035 | lttng_trigger_destroy(trigger_ht_element->trigger); |
| 3036 | call_rcu(&trigger_ht_element->rcu_node, free_lttng_trigger_ht_element_rcu); |
| 3037 | end: |
| 3038 | rcu_read_unlock(); |
| 3039 | if (_cmd_reply) { |
| 3040 | *_cmd_reply = cmd_reply; |
| 3041 | } |
| 3042 | return 0; |
| 3043 | } |
| 3044 | |
| 3045 | /* Returns 0 on success, 1 on exit requested, negative value on error. */ |
| 3046 | int handle_notification_thread_command( |
| 3047 | struct notification_thread_handle *handle, |
| 3048 | struct notification_thread_state *state) |
| 3049 | { |
| 3050 | int ret; |
| 3051 | uint64_t counter; |
| 3052 | struct notification_thread_command *cmd; |
| 3053 | |
| 3054 | /* Read the event pipe to put it back into a quiescent state. */ |
| 3055 | ret = lttng_read(lttng_pipe_get_readfd(handle->cmd_queue.event_pipe), &counter, |
| 3056 | sizeof(counter)); |
| 3057 | if (ret != sizeof(counter)) { |
| 3058 | goto error; |
| 3059 | } |
| 3060 | |
| 3061 | pthread_mutex_lock(&handle->cmd_queue.lock); |
| 3062 | cmd = cds_list_first_entry(&handle->cmd_queue.list, |
| 3063 | struct notification_thread_command, cmd_list_node); |
| 3064 | cds_list_del(&cmd->cmd_list_node); |
| 3065 | pthread_mutex_unlock(&handle->cmd_queue.lock); |
| 3066 | |
| 3067 | DBG("[notification-thread] Received `%s` command", |
| 3068 | notification_command_type_str(cmd->type)); |
| 3069 | switch (cmd->type) { |
| 3070 | case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER: |
| 3071 | ret = handle_notification_thread_command_register_trigger(state, |
| 3072 | cmd->parameters.register_trigger.trigger, |
| 3073 | &cmd->reply_code); |
| 3074 | break; |
| 3075 | case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER: |
| 3076 | ret = handle_notification_thread_command_unregister_trigger( |
| 3077 | state, |
| 3078 | cmd->parameters.unregister_trigger.trigger, |
| 3079 | &cmd->reply_code); |
| 3080 | break; |
| 3081 | case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL: |
| 3082 | ret = handle_notification_thread_command_add_channel( |
| 3083 | state, |
| 3084 | cmd->parameters.add_channel.session.name, |
| 3085 | cmd->parameters.add_channel.session.uid, |
| 3086 | cmd->parameters.add_channel.session.gid, |
| 3087 | cmd->parameters.add_channel.channel.name, |
| 3088 | cmd->parameters.add_channel.channel.domain, |
| 3089 | cmd->parameters.add_channel.channel.key, |
| 3090 | cmd->parameters.add_channel.channel.capacity, |
| 3091 | &cmd->reply_code); |
| 3092 | break; |
| 3093 | case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL: |
| 3094 | ret = handle_notification_thread_command_remove_channel( |
| 3095 | state, cmd->parameters.remove_channel.key, |
| 3096 | cmd->parameters.remove_channel.domain, |
| 3097 | &cmd->reply_code); |
| 3098 | break; |
| 3099 | case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING: |
| 3100 | case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED: |
| 3101 | ret = handle_notification_thread_command_session_rotation( |
| 3102 | state, |
| 3103 | cmd->type, |
| 3104 | cmd->parameters.session_rotation.session_name, |
| 3105 | cmd->parameters.session_rotation.uid, |
| 3106 | cmd->parameters.session_rotation.gid, |
| 3107 | cmd->parameters.session_rotation.trace_archive_chunk_id, |
| 3108 | cmd->parameters.session_rotation.location, |
| 3109 | &cmd->reply_code); |
| 3110 | break; |
| 3111 | case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE: |
| 3112 | ret = handle_notification_thread_command_add_tracer_event_source( |
| 3113 | state, |
| 3114 | cmd->parameters.tracer_event_source.tracer_event_source_fd, |
| 3115 | cmd->parameters.tracer_event_source.domain, |
| 3116 | &cmd->reply_code); |
| 3117 | break; |
| 3118 | case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE: |
| 3119 | ret = handle_notification_thread_command_remove_tracer_event_source( |
| 3120 | state, |
| 3121 | cmd->parameters.tracer_event_source.tracer_event_source_fd, |
| 3122 | &cmd->reply_code); |
| 3123 | break; |
| 3124 | case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS: |
| 3125 | { |
| 3126 | struct lttng_triggers *triggers = NULL; |
| 3127 | |
| 3128 | ret = handle_notification_thread_command_list_triggers( |
| 3129 | handle, |
| 3130 | state, |
| 3131 | cmd->parameters.list_triggers.uid, |
| 3132 | &triggers, |
| 3133 | &cmd->reply_code); |
| 3134 | cmd->reply.list_triggers.triggers = triggers; |
| 3135 | ret = 0; |
| 3136 | break; |
| 3137 | } |
| 3138 | case NOTIFICATION_COMMAND_TYPE_QUIT: |
| 3139 | cmd->reply_code = LTTNG_OK; |
| 3140 | ret = 1; |
| 3141 | goto end; |
| 3142 | case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE: |
| 3143 | { |
| 3144 | const enum client_transmission_status client_status = |
| 3145 | cmd->parameters.client_communication_update |
| 3146 | .status; |
| 3147 | const notification_client_id client_id = |
| 3148 | cmd->parameters.client_communication_update.id; |
| 3149 | struct notification_client *client; |
| 3150 | |
| 3151 | rcu_read_lock(); |
| 3152 | client = get_client_from_id(client_id, state); |
| 3153 | |
| 3154 | if (!client) { |
| 3155 | /* |
| 3156 | * Client error was probably already picked-up by the |
| 3157 | * notification thread or it has disconnected |
| 3158 | * gracefully while this command was queued. |
| 3159 | */ |
| 3160 | DBG("Failed to find notification client to update communication status, client id = %" PRIu64, |
| 3161 | client_id); |
| 3162 | ret = 0; |
| 3163 | } else { |
| 3164 | ret = client_handle_transmission_status( |
| 3165 | client, client_status, state); |
| 3166 | } |
| 3167 | rcu_read_unlock(); |
| 3168 | break; |
| 3169 | } |
| 3170 | default: |
| 3171 | ERR("[notification-thread] Unknown internal command received"); |
| 3172 | goto error_unlock; |
| 3173 | } |
| 3174 | |
| 3175 | if (ret) { |
| 3176 | goto error_unlock; |
| 3177 | } |
| 3178 | end: |
| 3179 | if (cmd->is_async) { |
| 3180 | free(cmd); |
| 3181 | cmd = NULL; |
| 3182 | } else { |
| 3183 | lttng_waiter_wake_up(&cmd->reply_waiter); |
| 3184 | } |
| 3185 | return ret; |
| 3186 | error_unlock: |
| 3187 | /* Wake-up and return a fatal error to the calling thread. */ |
| 3188 | lttng_waiter_wake_up(&cmd->reply_waiter); |
| 3189 | cmd->reply_code = LTTNG_ERR_FATAL; |
| 3190 | error: |
| 3191 | /* Indicate a fatal error to the caller. */ |
| 3192 | return -1; |
| 3193 | } |
| 3194 | |
| 3195 | static |
| 3196 | int socket_set_non_blocking(int socket) |
| 3197 | { |
| 3198 | int ret, flags; |
| 3199 | |
| 3200 | /* Set the pipe as non-blocking. */ |
| 3201 | ret = fcntl(socket, F_GETFL, 0); |
| 3202 | if (ret == -1) { |
| 3203 | PERROR("fcntl get socket flags"); |
| 3204 | goto end; |
| 3205 | } |
| 3206 | flags = ret; |
| 3207 | |
| 3208 | ret = fcntl(socket, F_SETFL, flags | O_NONBLOCK); |
| 3209 | if (ret == -1) { |
| 3210 | PERROR("fcntl set O_NONBLOCK socket flag"); |
| 3211 | goto end; |
| 3212 | } |
| 3213 | DBG("Client socket (fd = %i) set as non-blocking", socket); |
| 3214 | end: |
| 3215 | return ret; |
| 3216 | } |
| 3217 | |
| 3218 | static |
| 3219 | int client_reset_inbound_state(struct notification_client *client) |
| 3220 | { |
| 3221 | int ret; |
| 3222 | |
| 3223 | |
| 3224 | lttng_payload_clear(&client->communication.inbound.payload); |
| 3225 | |
| 3226 | client->communication.inbound.bytes_to_receive = |
| 3227 | sizeof(struct lttng_notification_channel_message); |
| 3228 | client->communication.inbound.msg_type = |
| 3229 | LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN; |
| 3230 | LTTNG_SOCK_SET_UID_CRED(&client->communication.inbound.creds, -1); |
| 3231 | LTTNG_SOCK_SET_GID_CRED(&client->communication.inbound.creds, -1); |
| 3232 | ret = lttng_dynamic_buffer_set_size( |
| 3233 | &client->communication.inbound.payload.buffer, |
| 3234 | client->communication.inbound.bytes_to_receive); |
| 3235 | |
| 3236 | return ret; |
| 3237 | } |
| 3238 | |
| 3239 | int handle_notification_thread_client_connect( |
| 3240 | struct notification_thread_state *state) |
| 3241 | { |
| 3242 | int ret; |
| 3243 | struct notification_client *client; |
| 3244 | |
| 3245 | DBG("[notification-thread] Handling new notification channel client connection"); |
| 3246 | |
| 3247 | client = zmalloc(sizeof(*client)); |
| 3248 | if (!client) { |
| 3249 | /* Fatal error. */ |
| 3250 | ret = -1; |
| 3251 | goto error; |
| 3252 | } |
| 3253 | |
| 3254 | pthread_mutex_init(&client->lock, NULL); |
| 3255 | client->id = state->next_notification_client_id++; |
| 3256 | CDS_INIT_LIST_HEAD(&client->condition_list); |
| 3257 | lttng_payload_init(&client->communication.inbound.payload); |
| 3258 | lttng_payload_init(&client->communication.outbound.payload); |
| 3259 | client->communication.inbound.expect_creds = true; |
| 3260 | |
| 3261 | ret = client_reset_inbound_state(client); |
| 3262 | if (ret) { |
| 3263 | ERR("[notification-thread] Failed to reset client communication's inbound state"); |
| 3264 | ret = 0; |
| 3265 | goto error; |
| 3266 | } |
| 3267 | |
| 3268 | ret = lttcomm_accept_unix_sock(state->notification_channel_socket); |
| 3269 | if (ret < 0) { |
| 3270 | ERR("[notification-thread] Failed to accept new notification channel client connection"); |
| 3271 | ret = 0; |
| 3272 | goto error; |
| 3273 | } |
| 3274 | |
| 3275 | client->socket = ret; |
| 3276 | |
| 3277 | ret = socket_set_non_blocking(client->socket); |
| 3278 | if (ret) { |
| 3279 | ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking"); |
| 3280 | goto error; |
| 3281 | } |
| 3282 | |
| 3283 | ret = lttcomm_setsockopt_creds_unix_sock(client->socket); |
| 3284 | if (ret < 0) { |
| 3285 | ERR("[notification-thread] Failed to set socket options on new notification channel client socket"); |
| 3286 | ret = 0; |
| 3287 | goto error; |
| 3288 | } |
| 3289 | |
| 3290 | ret = lttng_poll_add(&state->events, client->socket, |
| 3291 | LPOLLIN | LPOLLERR | |
| 3292 | LPOLLHUP | LPOLLRDHUP); |
| 3293 | if (ret < 0) { |
| 3294 | ERR("[notification-thread] Failed to add notification channel client socket to poll set"); |
| 3295 | ret = 0; |
| 3296 | goto error; |
| 3297 | } |
| 3298 | DBG("[notification-thread] Added new notification channel client socket (%i) to poll set", |
| 3299 | client->socket); |
| 3300 | |
| 3301 | rcu_read_lock(); |
| 3302 | cds_lfht_add(state->client_socket_ht, |
| 3303 | hash_client_socket(client->socket), |
| 3304 | &client->client_socket_ht_node); |
| 3305 | cds_lfht_add(state->client_id_ht, |
| 3306 | hash_client_id(client->id), |
| 3307 | &client->client_id_ht_node); |
| 3308 | rcu_read_unlock(); |
| 3309 | |
| 3310 | return ret; |
| 3311 | |
| 3312 | error: |
| 3313 | notification_client_destroy(client, state); |
| 3314 | return ret; |
| 3315 | } |
| 3316 | |
| 3317 | /* |
| 3318 | * RCU read-lock must be held by the caller. |
| 3319 | * Client lock must _not_ be held by the caller. |
| 3320 | */ |
| 3321 | static |
| 3322 | int notification_thread_client_disconnect( |
| 3323 | struct notification_client *client, |
| 3324 | struct notification_thread_state *state) |
| 3325 | { |
| 3326 | int ret; |
| 3327 | struct lttng_condition_list_element *condition_list_element, *tmp; |
| 3328 | |
| 3329 | /* Acquire the client lock to disable its communication atomically. */ |
| 3330 | pthread_mutex_lock(&client->lock); |
| 3331 | client->communication.active = false; |
| 3332 | cds_lfht_del(state->client_socket_ht, &client->client_socket_ht_node); |
| 3333 | cds_lfht_del(state->client_id_ht, &client->client_id_ht_node); |
| 3334 | pthread_mutex_unlock(&client->lock); |
| 3335 | |
| 3336 | ret = lttng_poll_del(&state->events, client->socket); |
| 3337 | if (ret) { |
| 3338 | ERR("[notification-thread] Failed to remove client socket %d from poll set", |
| 3339 | client->socket); |
| 3340 | } |
| 3341 | |
| 3342 | /* Release all conditions to which the client was subscribed. */ |
| 3343 | cds_list_for_each_entry_safe(condition_list_element, tmp, |
| 3344 | &client->condition_list, node) { |
| 3345 | (void) notification_thread_client_unsubscribe(client, |
| 3346 | condition_list_element->condition, state, NULL); |
| 3347 | } |
| 3348 | |
| 3349 | /* |
| 3350 | * Client no longer accessible to other threads (through the |
| 3351 | * client lists). |
| 3352 | */ |
| 3353 | notification_client_destroy(client, state); |
| 3354 | return ret; |
| 3355 | } |
| 3356 | |
| 3357 | int handle_notification_thread_client_disconnect( |
| 3358 | int client_socket, struct notification_thread_state *state) |
| 3359 | { |
| 3360 | int ret = 0; |
| 3361 | struct notification_client *client; |
| 3362 | |
| 3363 | rcu_read_lock(); |
| 3364 | DBG("[notification-thread] Closing client connection (socket fd = %i)", |
| 3365 | client_socket); |
| 3366 | client = get_client_from_socket(client_socket, state); |
| 3367 | if (!client) { |
| 3368 | /* Internal state corruption, fatal error. */ |
| 3369 | ERR("[notification-thread] Unable to find client (socket fd = %i)", |
| 3370 | client_socket); |
| 3371 | ret = -1; |
| 3372 | goto end; |
| 3373 | } |
| 3374 | |
| 3375 | ret = notification_thread_client_disconnect(client, state); |
| 3376 | end: |
| 3377 | rcu_read_unlock(); |
| 3378 | return ret; |
| 3379 | } |
| 3380 | |
| 3381 | int handle_notification_thread_client_disconnect_all( |
| 3382 | struct notification_thread_state *state) |
| 3383 | { |
| 3384 | struct cds_lfht_iter iter; |
| 3385 | struct notification_client *client; |
| 3386 | bool error_encoutered = false; |
| 3387 | |
| 3388 | rcu_read_lock(); |
| 3389 | DBG("[notification-thread] Closing all client connections"); |
| 3390 | cds_lfht_for_each_entry(state->client_socket_ht, &iter, client, |
| 3391 | client_socket_ht_node) { |
| 3392 | int ret; |
| 3393 | |
| 3394 | ret = notification_thread_client_disconnect( |
| 3395 | client, state); |
| 3396 | if (ret) { |
| 3397 | error_encoutered = true; |
| 3398 | } |
| 3399 | } |
| 3400 | rcu_read_unlock(); |
| 3401 | return error_encoutered ? 1 : 0; |
| 3402 | } |
| 3403 | |
| 3404 | int handle_notification_thread_trigger_unregister_all( |
| 3405 | struct notification_thread_state *state) |
| 3406 | { |
| 3407 | bool error_occurred = false; |
| 3408 | struct cds_lfht_iter iter; |
| 3409 | struct lttng_trigger_ht_element *trigger_ht_element; |
| 3410 | |
| 3411 | rcu_read_lock(); |
| 3412 | cds_lfht_for_each_entry(state->triggers_ht, &iter, trigger_ht_element, |
| 3413 | node) { |
| 3414 | int ret = handle_notification_thread_command_unregister_trigger( |
| 3415 | state, trigger_ht_element->trigger, NULL); |
| 3416 | if (ret) { |
| 3417 | error_occurred = true; |
| 3418 | } |
| 3419 | } |
| 3420 | rcu_read_unlock(); |
| 3421 | return error_occurred ? -1 : 0; |
| 3422 | } |
| 3423 | |
| 3424 | static |
| 3425 | int client_handle_transmission_status( |
| 3426 | struct notification_client *client, |
| 3427 | enum client_transmission_status transmission_status, |
| 3428 | struct notification_thread_state *state) |
| 3429 | { |
| 3430 | int ret = 0; |
| 3431 | |
| 3432 | switch (transmission_status) { |
| 3433 | case CLIENT_TRANSMISSION_STATUS_COMPLETE: |
| 3434 | ret = lttng_poll_mod(&state->events, client->socket, |
| 3435 | CLIENT_POLL_MASK_IN); |
| 3436 | if (ret) { |
| 3437 | goto end; |
| 3438 | } |
| 3439 | |
| 3440 | break; |
| 3441 | case CLIENT_TRANSMISSION_STATUS_QUEUED: |
| 3442 | /* |
| 3443 | * We want to be notified whenever there is buffer space |
| 3444 | * available to send the rest of the payload. |
| 3445 | */ |
| 3446 | ret = lttng_poll_mod(&state->events, client->socket, |
| 3447 | CLIENT_POLL_MASK_IN_OUT); |
| 3448 | if (ret) { |
| 3449 | goto end; |
| 3450 | } |
| 3451 | break; |
| 3452 | case CLIENT_TRANSMISSION_STATUS_FAIL: |
| 3453 | ret = notification_thread_client_disconnect(client, state); |
| 3454 | if (ret) { |
| 3455 | goto end; |
| 3456 | } |
| 3457 | break; |
| 3458 | case CLIENT_TRANSMISSION_STATUS_ERROR: |
| 3459 | ret = -1; |
| 3460 | goto end; |
| 3461 | default: |
| 3462 | abort(); |
| 3463 | } |
| 3464 | end: |
| 3465 | return ret; |
| 3466 | } |
| 3467 | |
| 3468 | /* Client lock must be acquired by caller. */ |
| 3469 | static |
| 3470 | enum client_transmission_status client_flush_outgoing_queue( |
| 3471 | struct notification_client *client) |
| 3472 | { |
| 3473 | ssize_t ret; |
| 3474 | size_t to_send_count; |
| 3475 | enum client_transmission_status status; |
| 3476 | struct lttng_payload_view pv = lttng_payload_view_from_payload( |
| 3477 | &client->communication.outbound.payload, 0, -1); |
| 3478 | const int fds_to_send_count = |
| 3479 | lttng_payload_view_get_fd_handle_count(&pv); |
| 3480 | |
| 3481 | ASSERT_LOCKED(client->lock); |
| 3482 | |
| 3483 | if (!client->communication.active) { |
| 3484 | status = CLIENT_TRANSMISSION_STATUS_FAIL; |
| 3485 | goto end; |
| 3486 | } |
| 3487 | |
| 3488 | if (pv.buffer.size == 0) { |
| 3489 | /* |
| 3490 | * If both data and fds are equal to zero, we are in an invalid |
| 3491 | * state. |
| 3492 | */ |
| 3493 | assert(fds_to_send_count != 0); |
| 3494 | goto send_fds; |
| 3495 | } |
| 3496 | |
| 3497 | /* Send data. */ |
| 3498 | to_send_count = pv.buffer.size; |
| 3499 | DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue", |
| 3500 | client->socket); |
| 3501 | |
| 3502 | ret = lttcomm_send_unix_sock_non_block(client->socket, |
| 3503 | pv.buffer.data, |
| 3504 | to_send_count); |
| 3505 | if ((ret >= 0 && ret < to_send_count)) { |
| 3506 | DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed", |
| 3507 | client->socket); |
| 3508 | to_send_count -= max(ret, 0); |
| 3509 | |
| 3510 | memmove(client->communication.outbound.payload.buffer.data, |
| 3511 | pv.buffer.data + |
| 3512 | pv.buffer.size - to_send_count, |
| 3513 | to_send_count); |
| 3514 | ret = lttng_dynamic_buffer_set_size( |
| 3515 | &client->communication.outbound.payload.buffer, |
| 3516 | to_send_count); |
| 3517 | if (ret) { |
| 3518 | goto error; |
| 3519 | } |
| 3520 | |
| 3521 | status = CLIENT_TRANSMISSION_STATUS_QUEUED; |
| 3522 | goto end; |
| 3523 | } else if (ret < 0) { |
| 3524 | /* Generic error, disable the client's communication. */ |
| 3525 | ERR("[notification-thread] Failed to flush outgoing queue, disconnecting client (socket fd = %i)", |
| 3526 | client->socket); |
| 3527 | client->communication.active = false; |
| 3528 | status = CLIENT_TRANSMISSION_STATUS_FAIL; |
| 3529 | goto end; |
| 3530 | } else { |
| 3531 | /* |
| 3532 | * No error and flushed the queue completely. |
| 3533 | * |
| 3534 | * The payload buffer size is used later to |
| 3535 | * check if there is notifications queued. So albeit that the |
| 3536 | * direct caller knows that the transmission is complete, we |
| 3537 | * need to set the buffer size to zero. |
| 3538 | */ |
| 3539 | ret = lttng_dynamic_buffer_set_size( |
| 3540 | &client->communication.outbound.payload.buffer, 0); |
| 3541 | if (ret) { |
| 3542 | goto error; |
| 3543 | } |
| 3544 | } |
| 3545 | |
| 3546 | send_fds: |
| 3547 | /* No fds to send, transmission is complete. */ |
| 3548 | if (fds_to_send_count == 0) { |
| 3549 | status = CLIENT_TRANSMISSION_STATUS_COMPLETE; |
| 3550 | goto end; |
| 3551 | } |
| 3552 | |
| 3553 | ret = lttcomm_send_payload_view_fds_unix_sock_non_block( |
| 3554 | client->socket, &pv); |
| 3555 | if (ret < 0) { |
| 3556 | /* Generic error, disable the client's communication. */ |
| 3557 | ERR("[notification-thread] Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)", |
| 3558 | client->socket); |
| 3559 | client->communication.active = false; |
| 3560 | status = CLIENT_TRANSMISSION_STATUS_FAIL; |
| 3561 | goto end; |
| 3562 | } else if (ret == 0) { |
| 3563 | /* Nothing could be sent. */ |
| 3564 | status = CLIENT_TRANSMISSION_STATUS_QUEUED; |
| 3565 | } else { |
| 3566 | /* Fd passing is an all or nothing kind of thing. */ |
| 3567 | status = CLIENT_TRANSMISSION_STATUS_COMPLETE; |
| 3568 | /* |
| 3569 | * The payload _fd_array count is used later to |
| 3570 | * check if there is notifications queued. So although the |
| 3571 | * direct caller knows that the transmission is complete, we |
| 3572 | * need to clear the _fd_array for the queuing check. |
| 3573 | */ |
| 3574 | lttng_dynamic_pointer_array_clear( |
| 3575 | &client->communication.outbound.payload |
| 3576 | ._fd_handles); |
| 3577 | } |
| 3578 | |
| 3579 | end: |
| 3580 | if (status == CLIENT_TRANSMISSION_STATUS_COMPLETE) { |
| 3581 | client->communication.outbound.queued_command_reply = false; |
| 3582 | client->communication.outbound.dropped_notification = false; |
| 3583 | lttng_payload_clear(&client->communication.outbound.payload); |
| 3584 | } |
| 3585 | |
| 3586 | return status; |
| 3587 | error: |
| 3588 | return CLIENT_TRANSMISSION_STATUS_ERROR; |
| 3589 | } |
| 3590 | |
| 3591 | static |
| 3592 | bool client_has_outbound_data_left( |
| 3593 | const struct notification_client *client) |
| 3594 | { |
| 3595 | const struct lttng_payload_view pv = lttng_payload_view_from_payload( |
| 3596 | &client->communication.outbound.payload, 0, -1); |
| 3597 | const bool has_data = pv.buffer.size != 0; |
| 3598 | const bool has_fds = lttng_payload_view_get_fd_handle_count(&pv); |
| 3599 | |
| 3600 | return has_data || has_fds; |
| 3601 | } |
| 3602 | |
| 3603 | /* Client lock must _not_ be held by the caller. */ |
| 3604 | static |
| 3605 | int client_send_command_reply(struct notification_client *client, |
| 3606 | struct notification_thread_state *state, |
| 3607 | enum lttng_notification_channel_status status) |
| 3608 | { |
| 3609 | int ret; |
| 3610 | struct lttng_notification_channel_command_reply reply = { |
| 3611 | .status = (int8_t) status, |
| 3612 | }; |
| 3613 | struct lttng_notification_channel_message msg = { |
| 3614 | .type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY, |
| 3615 | .size = sizeof(reply), |
| 3616 | }; |
| 3617 | char buffer[sizeof(msg) + sizeof(reply)]; |
| 3618 | enum client_transmission_status transmission_status; |
| 3619 | |
| 3620 | memcpy(buffer, &msg, sizeof(msg)); |
| 3621 | memcpy(buffer + sizeof(msg), &reply, sizeof(reply)); |
| 3622 | DBG("[notification-thread] Send command reply (%i)", (int) status); |
| 3623 | |
| 3624 | pthread_mutex_lock(&client->lock); |
| 3625 | if (client->communication.outbound.queued_command_reply) { |
| 3626 | /* Protocol error. */ |
| 3627 | goto error_unlock; |
| 3628 | } |
| 3629 | |
| 3630 | /* Enqueue buffer to outgoing queue and flush it. */ |
| 3631 | ret = lttng_dynamic_buffer_append( |
| 3632 | &client->communication.outbound.payload.buffer, |
| 3633 | buffer, sizeof(buffer)); |
| 3634 | if (ret) { |
| 3635 | goto error_unlock; |
| 3636 | } |
| 3637 | |
| 3638 | transmission_status = client_flush_outgoing_queue(client); |
| 3639 | |
| 3640 | if (client_has_outbound_data_left(client)) { |
| 3641 | /* Queue could not be emptied. */ |
| 3642 | client->communication.outbound.queued_command_reply = true; |
| 3643 | } |
| 3644 | |
| 3645 | pthread_mutex_unlock(&client->lock); |
| 3646 | ret = client_handle_transmission_status( |
| 3647 | client, transmission_status, state); |
| 3648 | if (ret) { |
| 3649 | goto error; |
| 3650 | } |
| 3651 | |
| 3652 | return 0; |
| 3653 | error_unlock: |
| 3654 | pthread_mutex_unlock(&client->lock); |
| 3655 | error: |
| 3656 | return -1; |
| 3657 | } |
| 3658 | |
| 3659 | static |
| 3660 | int client_handle_message_unknown(struct notification_client *client, |
| 3661 | struct notification_thread_state *state) |
| 3662 | { |
| 3663 | int ret; |
| 3664 | /* |
| 3665 | * Receiving message header. The function will be called again |
| 3666 | * once the rest of the message as been received and can be |
| 3667 | * interpreted. |
| 3668 | */ |
| 3669 | const struct lttng_notification_channel_message *msg; |
| 3670 | |
| 3671 | assert(sizeof(*msg) == client->communication.inbound.payload.buffer.size); |
| 3672 | msg = (const struct lttng_notification_channel_message *) |
| 3673 | client->communication.inbound.payload.buffer.data; |
| 3674 | |
| 3675 | if (msg->size == 0 || |
| 3676 | msg->size > DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE) { |
| 3677 | ERR("[notification-thread] Invalid notification channel message: length = %u", |
| 3678 | msg->size); |
| 3679 | ret = -1; |
| 3680 | goto end; |
| 3681 | } |
| 3682 | |
| 3683 | switch (msg->type) { |
| 3684 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE: |
| 3685 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE: |
| 3686 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE: |
| 3687 | break; |
| 3688 | default: |
| 3689 | ret = -1; |
| 3690 | ERR("[notification-thread] Invalid notification channel message: unexpected message type"); |
| 3691 | goto end; |
| 3692 | } |
| 3693 | |
| 3694 | client->communication.inbound.bytes_to_receive = msg->size; |
| 3695 | client->communication.inbound.fds_to_receive = msg->fds; |
| 3696 | client->communication.inbound.msg_type = |
| 3697 | (enum lttng_notification_channel_message_type) msg->type; |
| 3698 | ret = lttng_dynamic_buffer_set_size( |
| 3699 | &client->communication.inbound.payload.buffer, msg->size); |
| 3700 | |
| 3701 | /* msg is not valid anymore due to lttng_dynamic_buffer_set_size. */ |
| 3702 | msg = NULL; |
| 3703 | end: |
| 3704 | return ret; |
| 3705 | } |
| 3706 | |
| 3707 | static |
| 3708 | int client_handle_message_handshake(struct notification_client *client, |
| 3709 | struct notification_thread_state *state) |
| 3710 | { |
| 3711 | int ret; |
| 3712 | struct lttng_notification_channel_command_handshake *handshake_client; |
| 3713 | const struct lttng_notification_channel_command_handshake handshake_reply = { |
| 3714 | .major = LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR, |
| 3715 | .minor = LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR, |
| 3716 | }; |
| 3717 | const struct lttng_notification_channel_message msg_header = { |
| 3718 | .type = LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE, |
| 3719 | .size = sizeof(handshake_reply), |
| 3720 | }; |
| 3721 | enum lttng_notification_channel_status status = |
| 3722 | LTTNG_NOTIFICATION_CHANNEL_STATUS_OK; |
| 3723 | char send_buffer[sizeof(msg_header) + sizeof(handshake_reply)]; |
| 3724 | |
| 3725 | memcpy(send_buffer, &msg_header, sizeof(msg_header)); |
| 3726 | memcpy(send_buffer + sizeof(msg_header), &handshake_reply, |
| 3727 | sizeof(handshake_reply)); |
| 3728 | |
| 3729 | handshake_client = |
| 3730 | (struct lttng_notification_channel_command_handshake *) |
| 3731 | client->communication.inbound.payload.buffer |
| 3732 | .data; |
| 3733 | client->major = handshake_client->major; |
| 3734 | client->minor = handshake_client->minor; |
| 3735 | if (!client->communication.inbound.creds_received) { |
| 3736 | ERR("[notification-thread] No credentials received from client"); |
| 3737 | ret = -1; |
| 3738 | goto end; |
| 3739 | } |
| 3740 | |
| 3741 | client->uid = LTTNG_SOCK_GET_UID_CRED( |
| 3742 | &client->communication.inbound.creds); |
| 3743 | client->gid = LTTNG_SOCK_GET_GID_CRED( |
| 3744 | &client->communication.inbound.creds); |
| 3745 | DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i", |
| 3746 | client->uid, client->gid, (int) client->major, |
| 3747 | (int) client->minor); |
| 3748 | |
| 3749 | if (handshake_client->major != |
| 3750 | LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR) { |
| 3751 | status = LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION; |
| 3752 | } |
| 3753 | |
| 3754 | pthread_mutex_lock(&client->lock); |
| 3755 | /* Outgoing queue will be flushed when the command reply is sent. */ |
| 3756 | ret = lttng_dynamic_buffer_append( |
| 3757 | &client->communication.outbound.payload.buffer, send_buffer, |
| 3758 | sizeof(send_buffer)); |
| 3759 | if (ret) { |
| 3760 | ERR("[notification-thread] Failed to send protocol version to notification channel client"); |
| 3761 | goto end_unlock; |
| 3762 | } |
| 3763 | |
| 3764 | client->validated = true; |
| 3765 | client->communication.active = true; |
| 3766 | pthread_mutex_unlock(&client->lock); |
| 3767 | |
| 3768 | /* Set reception state to receive the next message header. */ |
| 3769 | ret = client_reset_inbound_state(client); |
| 3770 | if (ret) { |
| 3771 | ERR("[notification-thread] Failed to reset client communication's inbound state"); |
| 3772 | goto end; |
| 3773 | } |
| 3774 | |
| 3775 | /* Flushes the outgoing queue. */ |
| 3776 | ret = client_send_command_reply(client, state, status); |
| 3777 | if (ret) { |
| 3778 | ERR("[notification-thread] Failed to send reply to notification channel client"); |
| 3779 | goto end; |
| 3780 | } |
| 3781 | |
| 3782 | goto end; |
| 3783 | end_unlock: |
| 3784 | pthread_mutex_unlock(&client->lock); |
| 3785 | end: |
| 3786 | return ret; |
| 3787 | } |
| 3788 | |
| 3789 | static |
| 3790 | int client_handle_message_subscription( |
| 3791 | struct notification_client *client, |
| 3792 | enum lttng_notification_channel_message_type msg_type, |
| 3793 | struct notification_thread_state *state) |
| 3794 | { |
| 3795 | int ret; |
| 3796 | struct lttng_condition *condition; |
| 3797 | enum lttng_notification_channel_status status = |
| 3798 | LTTNG_NOTIFICATION_CHANNEL_STATUS_OK; |
| 3799 | struct lttng_payload_view condition_view = |
| 3800 | lttng_payload_view_from_payload( |
| 3801 | &client->communication.inbound.payload, |
| 3802 | 0, -1); |
| 3803 | size_t expected_condition_size; |
| 3804 | |
| 3805 | /* |
| 3806 | * No need to lock client to sample the inbound state as the only |
| 3807 | * other thread accessing clients (action executor) only uses the |
| 3808 | * outbound state. |
| 3809 | */ |
| 3810 | expected_condition_size = client->communication.inbound.payload.buffer.size; |
| 3811 | ret = lttng_condition_create_from_payload(&condition_view, &condition); |
| 3812 | if (ret != expected_condition_size) { |
| 3813 | ERR("[notification-thread] Malformed condition received from client"); |
| 3814 | goto end; |
| 3815 | } |
| 3816 | |
| 3817 | if (msg_type == LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE) { |
| 3818 | ret = notification_thread_client_subscribe( |
| 3819 | client, condition, state, &status); |
| 3820 | } else { |
| 3821 | ret = notification_thread_client_unsubscribe( |
| 3822 | client, condition, state, &status); |
| 3823 | } |
| 3824 | |
| 3825 | if (ret) { |
| 3826 | goto end; |
| 3827 | } |
| 3828 | |
| 3829 | /* Set reception state to receive the next message header. */ |
| 3830 | ret = client_reset_inbound_state(client); |
| 3831 | if (ret) { |
| 3832 | ERR("[notification-thread] Failed to reset client communication's inbound state"); |
| 3833 | goto end; |
| 3834 | } |
| 3835 | |
| 3836 | ret = client_send_command_reply(client, state, status); |
| 3837 | if (ret) { |
| 3838 | ERR("[notification-thread] Failed to send reply to notification channel client"); |
| 3839 | goto end; |
| 3840 | } |
| 3841 | |
| 3842 | end: |
| 3843 | return ret; |
| 3844 | } |
| 3845 | |
| 3846 | static |
| 3847 | int client_dispatch_message(struct notification_client *client, |
| 3848 | struct notification_thread_state *state) |
| 3849 | { |
| 3850 | int ret = 0; |
| 3851 | |
| 3852 | if (client->communication.inbound.msg_type != |
| 3853 | LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE && |
| 3854 | client->communication.inbound.msg_type != |
| 3855 | LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN && |
| 3856 | !client->validated) { |
| 3857 | WARN("[notification-thread] client attempted a command before handshake"); |
| 3858 | ret = -1; |
| 3859 | goto end; |
| 3860 | } |
| 3861 | |
| 3862 | switch (client->communication.inbound.msg_type) { |
| 3863 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN: |
| 3864 | { |
| 3865 | ret = client_handle_message_unknown(client, state); |
| 3866 | break; |
| 3867 | } |
| 3868 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE: |
| 3869 | { |
| 3870 | ret = client_handle_message_handshake(client, state); |
| 3871 | break; |
| 3872 | } |
| 3873 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE: |
| 3874 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE: |
| 3875 | { |
| 3876 | ret = client_handle_message_subscription(client, |
| 3877 | client->communication.inbound.msg_type, state); |
| 3878 | break; |
| 3879 | } |
| 3880 | default: |
| 3881 | abort(); |
| 3882 | } |
| 3883 | end: |
| 3884 | return ret; |
| 3885 | } |
| 3886 | |
| 3887 | /* Incoming data from client. */ |
| 3888 | int handle_notification_thread_client_in( |
| 3889 | struct notification_thread_state *state, int socket) |
| 3890 | { |
| 3891 | int ret = 0; |
| 3892 | struct notification_client *client; |
| 3893 | ssize_t recv_ret; |
| 3894 | size_t offset; |
| 3895 | |
| 3896 | rcu_read_lock(); |
| 3897 | client = get_client_from_socket(socket, state); |
| 3898 | if (!client) { |
| 3899 | /* Internal error, abort. */ |
| 3900 | ret = -1; |
| 3901 | goto end; |
| 3902 | } |
| 3903 | |
| 3904 | offset = client->communication.inbound.payload.buffer.size - |
| 3905 | client->communication.inbound.bytes_to_receive; |
| 3906 | if (client->communication.inbound.expect_creds) { |
| 3907 | recv_ret = lttcomm_recv_creds_unix_sock(socket, |
| 3908 | client->communication.inbound.payload.buffer.data + offset, |
| 3909 | client->communication.inbound.bytes_to_receive, |
| 3910 | &client->communication.inbound.creds); |
| 3911 | if (recv_ret > 0) { |
| 3912 | client->communication.inbound.expect_creds = false; |
| 3913 | client->communication.inbound.creds_received = true; |
| 3914 | } |
| 3915 | } else { |
| 3916 | recv_ret = lttcomm_recv_unix_sock_non_block(socket, |
| 3917 | client->communication.inbound.payload.buffer.data + offset, |
| 3918 | client->communication.inbound.bytes_to_receive); |
| 3919 | } |
| 3920 | if (recv_ret >= 0) { |
| 3921 | client->communication.inbound.bytes_to_receive -= recv_ret; |
| 3922 | } else { |
| 3923 | goto error_disconnect_client; |
| 3924 | } |
| 3925 | |
| 3926 | if (client->communication.inbound.bytes_to_receive != 0) { |
| 3927 | /* Message incomplete wait for more data. */ |
| 3928 | ret = 0; |
| 3929 | goto end; |
| 3930 | } |
| 3931 | |
| 3932 | assert(client->communication.inbound.bytes_to_receive == 0); |
| 3933 | |
| 3934 | /* Receive fds. */ |
| 3935 | if (client->communication.inbound.fds_to_receive != 0) { |
| 3936 | ret = lttcomm_recv_payload_fds_unix_sock_non_block( |
| 3937 | client->socket, |
| 3938 | client->communication.inbound.fds_to_receive, |
| 3939 | &client->communication.inbound.payload); |
| 3940 | if (ret > 0) { |
| 3941 | /* |
| 3942 | * Fds received. non blocking fds passing is all |
| 3943 | * or nothing. |
| 3944 | */ |
| 3945 | ssize_t expected_size; |
| 3946 | |
| 3947 | expected_size = sizeof(int) * |
| 3948 | client->communication.inbound |
| 3949 | .fds_to_receive; |
| 3950 | assert(ret == expected_size); |
| 3951 | client->communication.inbound.fds_to_receive = 0; |
| 3952 | } else if (ret == 0) { |
| 3953 | /* Received nothing. */ |
| 3954 | ret = 0; |
| 3955 | goto end; |
| 3956 | } else { |
| 3957 | goto error_disconnect_client; |
| 3958 | } |
| 3959 | } |
| 3960 | |
| 3961 | /* At this point the message is complete.*/ |
| 3962 | assert(client->communication.inbound.bytes_to_receive == 0 && |
| 3963 | client->communication.inbound.fds_to_receive == 0); |
| 3964 | ret = client_dispatch_message(client, state); |
| 3965 | if (ret) { |
| 3966 | /* |
| 3967 | * Only returns an error if this client must be |
| 3968 | * disconnected. |
| 3969 | */ |
| 3970 | goto error_disconnect_client; |
| 3971 | } |
| 3972 | |
| 3973 | end: |
| 3974 | rcu_read_unlock(); |
| 3975 | return ret; |
| 3976 | |
| 3977 | error_disconnect_client: |
| 3978 | ret = notification_thread_client_disconnect(client, state); |
| 3979 | goto end; |
| 3980 | } |
| 3981 | |
| 3982 | /* Client ready to receive outgoing data. */ |
| 3983 | int handle_notification_thread_client_out( |
| 3984 | struct notification_thread_state *state, int socket) |
| 3985 | { |
| 3986 | int ret; |
| 3987 | struct notification_client *client; |
| 3988 | enum client_transmission_status transmission_status; |
| 3989 | |
| 3990 | rcu_read_lock(); |
| 3991 | client = get_client_from_socket(socket, state); |
| 3992 | if (!client) { |
| 3993 | /* Internal error, abort. */ |
| 3994 | ret = -1; |
| 3995 | goto end; |
| 3996 | } |
| 3997 | |
| 3998 | pthread_mutex_lock(&client->lock); |
| 3999 | transmission_status = client_flush_outgoing_queue(client); |
| 4000 | pthread_mutex_unlock(&client->lock); |
| 4001 | |
| 4002 | ret = client_handle_transmission_status( |
| 4003 | client, transmission_status, state); |
| 4004 | if (ret) { |
| 4005 | goto end; |
| 4006 | } |
| 4007 | end: |
| 4008 | rcu_read_unlock(); |
| 4009 | return ret; |
| 4010 | } |
| 4011 | |
| 4012 | static |
| 4013 | bool evaluate_buffer_usage_condition(const struct lttng_condition *condition, |
| 4014 | const struct channel_state_sample *sample, |
| 4015 | uint64_t buffer_capacity) |
| 4016 | { |
| 4017 | bool result = false; |
| 4018 | uint64_t threshold; |
| 4019 | enum lttng_condition_type condition_type; |
| 4020 | const struct lttng_condition_buffer_usage *use_condition = container_of( |
| 4021 | condition, struct lttng_condition_buffer_usage, |
| 4022 | parent); |
| 4023 | |
| 4024 | if (use_condition->threshold_bytes.set) { |
| 4025 | threshold = use_condition->threshold_bytes.value; |
| 4026 | } else { |
| 4027 | /* |
| 4028 | * Threshold was expressed as a ratio. |
| 4029 | * |
| 4030 | * TODO the threshold (in bytes) of conditions expressed |
| 4031 | * as a ratio of total buffer size could be cached to |
| 4032 | * forego this double-multiplication or it could be performed |
| 4033 | * as fixed-point math. |
| 4034 | * |
| 4035 | * Note that caching should accommodates the case where the |
| 4036 | * condition applies to multiple channels (i.e. don't assume |
| 4037 | * that all channels matching my_chann* have the same size...) |
| 4038 | */ |
| 4039 | threshold = (uint64_t) (use_condition->threshold_ratio.value * |
| 4040 | (double) buffer_capacity); |
| 4041 | } |
| 4042 | |
| 4043 | condition_type = lttng_condition_get_type(condition); |
| 4044 | if (condition_type == LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW) { |
| 4045 | DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64 ", highest usage = %" PRIu64, |
| 4046 | threshold, sample->highest_usage); |
| 4047 | |
| 4048 | /* |
| 4049 | * The low condition should only be triggered once _all_ of the |
| 4050 | * streams in a channel have gone below the "low" threshold. |
| 4051 | */ |
| 4052 | if (sample->highest_usage <= threshold) { |
| 4053 | result = true; |
| 4054 | } |
| 4055 | } else { |
| 4056 | DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64 ", highest usage = %" PRIu64, |
| 4057 | threshold, sample->highest_usage); |
| 4058 | |
| 4059 | /* |
| 4060 | * For high buffer usage scenarios, we want to trigger whenever |
| 4061 | * _any_ of the streams has reached the "high" threshold. |
| 4062 | */ |
| 4063 | if (sample->highest_usage >= threshold) { |
| 4064 | result = true; |
| 4065 | } |
| 4066 | } |
| 4067 | |
| 4068 | return result; |
| 4069 | } |
| 4070 | |
| 4071 | static |
| 4072 | bool evaluate_session_consumed_size_condition( |
| 4073 | const struct lttng_condition *condition, |
| 4074 | uint64_t session_consumed_size) |
| 4075 | { |
| 4076 | uint64_t threshold; |
| 4077 | const struct lttng_condition_session_consumed_size *size_condition = |
| 4078 | container_of(condition, |
| 4079 | struct lttng_condition_session_consumed_size, |
| 4080 | parent); |
| 4081 | |
| 4082 | threshold = size_condition->consumed_threshold_bytes.value; |
| 4083 | DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64 ", current size = %" PRIu64, |
| 4084 | threshold, session_consumed_size); |
| 4085 | return session_consumed_size >= threshold; |
| 4086 | } |
| 4087 | |
| 4088 | static |
| 4089 | int evaluate_buffer_condition(const struct lttng_condition *condition, |
| 4090 | struct lttng_evaluation **evaluation, |
| 4091 | const struct notification_thread_state *state, |
| 4092 | const struct channel_state_sample *previous_sample, |
| 4093 | const struct channel_state_sample *latest_sample, |
| 4094 | uint64_t previous_session_consumed_total, |
| 4095 | uint64_t latest_session_consumed_total, |
| 4096 | struct channel_info *channel_info) |
| 4097 | { |
| 4098 | int ret = 0; |
| 4099 | enum lttng_condition_type condition_type; |
| 4100 | const bool previous_sample_available = !!previous_sample; |
| 4101 | bool previous_sample_result = false; |
| 4102 | bool latest_sample_result; |
| 4103 | |
| 4104 | condition_type = lttng_condition_get_type(condition); |
| 4105 | |
| 4106 | switch (condition_type) { |
| 4107 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: |
| 4108 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: |
| 4109 | if (caa_likely(previous_sample_available)) { |
| 4110 | previous_sample_result = |
| 4111 | evaluate_buffer_usage_condition(condition, |
| 4112 | previous_sample, channel_info->capacity); |
| 4113 | } |
| 4114 | latest_sample_result = evaluate_buffer_usage_condition( |
| 4115 | condition, latest_sample, |
| 4116 | channel_info->capacity); |
| 4117 | break; |
| 4118 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: |
| 4119 | if (caa_likely(previous_sample_available)) { |
| 4120 | previous_sample_result = |
| 4121 | evaluate_session_consumed_size_condition( |
| 4122 | condition, |
| 4123 | previous_session_consumed_total); |
| 4124 | } |
| 4125 | latest_sample_result = |
| 4126 | evaluate_session_consumed_size_condition( |
| 4127 | condition, |
| 4128 | latest_session_consumed_total); |
| 4129 | break; |
| 4130 | default: |
| 4131 | /* Unknown condition type; internal error. */ |
| 4132 | abort(); |
| 4133 | } |
| 4134 | |
| 4135 | if (!latest_sample_result || |
| 4136 | (previous_sample_result == latest_sample_result)) { |
| 4137 | /* |
| 4138 | * Only trigger on a condition evaluation transition. |
| 4139 | * |
| 4140 | * NOTE: This edge-triggered logic may not be appropriate for |
| 4141 | * future condition types. |
| 4142 | */ |
| 4143 | goto end; |
| 4144 | } |
| 4145 | |
| 4146 | if (!evaluation || !latest_sample_result) { |
| 4147 | goto end; |
| 4148 | } |
| 4149 | |
| 4150 | switch (condition_type) { |
| 4151 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: |
| 4152 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: |
| 4153 | *evaluation = lttng_evaluation_buffer_usage_create( |
| 4154 | condition_type, |
| 4155 | latest_sample->highest_usage, |
| 4156 | channel_info->capacity); |
| 4157 | break; |
| 4158 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: |
| 4159 | *evaluation = lttng_evaluation_session_consumed_size_create( |
| 4160 | latest_session_consumed_total); |
| 4161 | break; |
| 4162 | default: |
| 4163 | abort(); |
| 4164 | } |
| 4165 | |
| 4166 | if (!*evaluation) { |
| 4167 | ret = -1; |
| 4168 | goto end; |
| 4169 | } |
| 4170 | end: |
| 4171 | return ret; |
| 4172 | } |
| 4173 | |
| 4174 | static |
| 4175 | int client_notification_overflow(struct notification_client *client) |
| 4176 | { |
| 4177 | int ret = 0; |
| 4178 | const struct lttng_notification_channel_message msg = { |
| 4179 | .type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED, |
| 4180 | }; |
| 4181 | |
| 4182 | ASSERT_LOCKED(client->lock); |
| 4183 | |
| 4184 | DBG("Dropping notification addressed to client (socket fd = %i)", |
| 4185 | client->socket); |
| 4186 | if (client->communication.outbound.dropped_notification) { |
| 4187 | /* |
| 4188 | * The client already has a "notification dropped" message |
| 4189 | * in its outgoing queue. Nothing to do since all |
| 4190 | * of those messages are coalesced. |
| 4191 | */ |
| 4192 | goto end; |
| 4193 | } |
| 4194 | |
| 4195 | client->communication.outbound.dropped_notification = true; |
| 4196 | ret = lttng_dynamic_buffer_append( |
| 4197 | &client->communication.outbound.payload.buffer, &msg, |
| 4198 | sizeof(msg)); |
| 4199 | if (ret) { |
| 4200 | PERROR("Failed to enqueue \"dropped notification\" message in client's (socket fd = %i) outgoing queue", |
| 4201 | client->socket); |
| 4202 | } |
| 4203 | end: |
| 4204 | return ret; |
| 4205 | } |
| 4206 | |
| 4207 | static int client_handle_transmission_status_wrapper( |
| 4208 | struct notification_client *client, |
| 4209 | enum client_transmission_status status, |
| 4210 | void *user_data) |
| 4211 | { |
| 4212 | return client_handle_transmission_status(client, status, |
| 4213 | (struct notification_thread_state *) user_data); |
| 4214 | } |
| 4215 | |
| 4216 | static |
| 4217 | int send_evaluation_to_clients(const struct lttng_trigger *trigger, |
| 4218 | const struct lttng_evaluation *evaluation, |
| 4219 | struct notification_client_list* client_list, |
| 4220 | struct notification_thread_state *state, |
| 4221 | uid_t object_uid, gid_t object_gid) |
| 4222 | { |
| 4223 | const struct lttng_credentials creds = { |
| 4224 | .uid = LTTNG_OPTIONAL_INIT_VALUE(object_uid), |
| 4225 | .gid = LTTNG_OPTIONAL_INIT_VALUE(object_gid), |
| 4226 | }; |
| 4227 | |
| 4228 | return notification_client_list_send_evaluation(client_list, |
| 4229 | lttng_trigger_get_const_condition(trigger), evaluation, |
| 4230 | lttng_trigger_get_credentials(trigger), |
| 4231 | &creds, |
| 4232 | client_handle_transmission_status_wrapper, state); |
| 4233 | } |
| 4234 | |
| 4235 | /* |
| 4236 | * Permission checks relative to notification channel clients are performed |
| 4237 | * here. Notice how object, client, and trigger credentials are involved in |
| 4238 | * this check. |
| 4239 | * |
| 4240 | * The `object` credentials are the credentials associated with the "subject" |
| 4241 | * of a condition. For instance, a `rotation completed` condition applies |
| 4242 | * to a session. When that condition is met, it will produce an evaluation |
| 4243 | * against a session. Hence, in this case, the `object` credentials are the |
| 4244 | * credentials of the "subject" session. |
| 4245 | * |
| 4246 | * The `trigger` credentials are the credentials of the user that registered the |
| 4247 | * trigger. |
| 4248 | * |
| 4249 | * The `client` credentials are the credentials of the user that created a given |
| 4250 | * notification channel. |
| 4251 | * |
| 4252 | * In terms of visibility, it is expected that non-privilieged users can only |
| 4253 | * register triggers against "their" objects (their own sessions and |
| 4254 | * applications they are allowed to interact with). They can then open a |
| 4255 | * notification channel and subscribe to notifications associated with those |
| 4256 | * triggers. |
| 4257 | * |
| 4258 | * As for privilieged users, they can register triggers against the objects of |
| 4259 | * other users. They can then subscribe to the notifications associated to their |
| 4260 | * triggers. Privilieged users _can't_ subscribe to the notifications of |
| 4261 | * triggers owned by other users; they must create their own triggers. |
| 4262 | * |
| 4263 | * This is more a concern of usability than security. It would be difficult for |
| 4264 | * a root user reliably subscribe to a specific set of conditions without |
| 4265 | * interference from external users (those could, for instance, unregister |
| 4266 | * their triggers). |
| 4267 | */ |
| 4268 | LTTNG_HIDDEN |
| 4269 | int notification_client_list_send_evaluation( |
| 4270 | struct notification_client_list *client_list, |
| 4271 | const struct lttng_condition *condition, |
| 4272 | const struct lttng_evaluation *evaluation, |
| 4273 | const struct lttng_credentials *trigger_creds, |
| 4274 | const struct lttng_credentials *source_object_creds, |
| 4275 | report_client_transmission_result_cb client_report, |
| 4276 | void *user_data) |
| 4277 | { |
| 4278 | int ret = 0; |
| 4279 | struct lttng_payload msg_payload; |
| 4280 | struct notification_client_list_element *client_list_element, *tmp; |
| 4281 | const struct lttng_notification notification = { |
| 4282 | .condition = (struct lttng_condition *) condition, |
| 4283 | .evaluation = (struct lttng_evaluation *) evaluation, |
| 4284 | }; |
| 4285 | struct lttng_notification_channel_message msg_header = { |
| 4286 | .type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION, |
| 4287 | }; |
| 4288 | |
| 4289 | lttng_payload_init(&msg_payload); |
| 4290 | |
| 4291 | ret = lttng_dynamic_buffer_append(&msg_payload.buffer, &msg_header, |
| 4292 | sizeof(msg_header)); |
| 4293 | if (ret) { |
| 4294 | goto end; |
| 4295 | } |
| 4296 | |
| 4297 | ret = lttng_notification_serialize(¬ification, &msg_payload); |
| 4298 | if (ret) { |
| 4299 | ERR("[notification-thread] Failed to serialize notification"); |
| 4300 | ret = -1; |
| 4301 | goto end; |
| 4302 | } |
| 4303 | |
| 4304 | /* Update payload size. */ |
| 4305 | ((struct lttng_notification_channel_message *) msg_payload.buffer.data) |
| 4306 | ->size = (uint32_t)( |
| 4307 | msg_payload.buffer.size - sizeof(msg_header)); |
| 4308 | |
| 4309 | /* Update the payload number of fds. */ |
| 4310 | { |
| 4311 | const struct lttng_payload_view pv = lttng_payload_view_from_payload( |
| 4312 | &msg_payload, 0, -1); |
| 4313 | |
| 4314 | ((struct lttng_notification_channel_message *) |
| 4315 | msg_payload.buffer.data)->fds = (uint32_t) |
| 4316 | lttng_payload_view_get_fd_handle_count(&pv); |
| 4317 | } |
| 4318 | |
| 4319 | pthread_mutex_lock(&client_list->lock); |
| 4320 | cds_list_for_each_entry_safe(client_list_element, tmp, |
| 4321 | &client_list->list, node) { |
| 4322 | enum client_transmission_status transmission_status; |
| 4323 | struct notification_client *client = |
| 4324 | client_list_element->client; |
| 4325 | |
| 4326 | ret = 0; |
| 4327 | pthread_mutex_lock(&client->lock); |
| 4328 | if (!client->communication.active) { |
| 4329 | /* |
| 4330 | * Skip inactive client (protocol error or |
| 4331 | * disconnecting). |
| 4332 | */ |
| 4333 | DBG("Skipping client at it is marked as inactive"); |
| 4334 | goto skip_client; |
| 4335 | } |
| 4336 | |
| 4337 | if (source_object_creds) { |
| 4338 | if (client->uid != lttng_credentials_get_uid(source_object_creds) && |
| 4339 | client->gid != lttng_credentials_get_gid(source_object_creds) && |
| 4340 | client->uid != 0) { |
| 4341 | /* |
| 4342 | * Client is not allowed to monitor this |
| 4343 | * object. |
| 4344 | */ |
| 4345 | DBG("[notification-thread] Skipping client at it does not have the object permission to receive notification for this trigger"); |
| 4346 | goto skip_client; |
| 4347 | } |
| 4348 | } |
| 4349 | |
| 4350 | if (client->uid != lttng_credentials_get_uid(trigger_creds) && client->gid != lttng_credentials_get_gid(trigger_creds)) { |
| 4351 | DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this trigger"); |
| 4352 | goto skip_client; |
| 4353 | } |
| 4354 | |
| 4355 | DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)", |
| 4356 | client->socket, msg_payload.buffer.size); |
| 4357 | |
| 4358 | if (client_has_outbound_data_left(client)) { |
| 4359 | /* |
| 4360 | * Outgoing data is already buffered for this client; |
| 4361 | * drop the notification and enqueue a "dropped |
| 4362 | * notification" message if this is the first dropped |
| 4363 | * notification since the socket spilled-over to the |
| 4364 | * queue. |
| 4365 | */ |
| 4366 | ret = client_notification_overflow(client); |
| 4367 | if (ret) { |
| 4368 | /* Fatal error. */ |
| 4369 | goto skip_client; |
| 4370 | } |
| 4371 | } |
| 4372 | |
| 4373 | ret = lttng_payload_copy(&msg_payload, &client->communication.outbound.payload); |
| 4374 | if (ret) { |
| 4375 | /* Fatal error. */ |
| 4376 | goto skip_client; |
| 4377 | } |
| 4378 | |
| 4379 | transmission_status = client_flush_outgoing_queue(client); |
| 4380 | pthread_mutex_unlock(&client->lock); |
| 4381 | ret = client_report(client, transmission_status, user_data); |
| 4382 | if (ret) { |
| 4383 | /* Fatal error. */ |
| 4384 | goto end_unlock_list; |
| 4385 | } |
| 4386 | |
| 4387 | continue; |
| 4388 | |
| 4389 | skip_client: |
| 4390 | pthread_mutex_unlock(&client->lock); |
| 4391 | if (ret) { |
| 4392 | /* Fatal error. */ |
| 4393 | goto end_unlock_list; |
| 4394 | } |
| 4395 | } |
| 4396 | ret = 0; |
| 4397 | |
| 4398 | end_unlock_list: |
| 4399 | pthread_mutex_unlock(&client_list->lock); |
| 4400 | end: |
| 4401 | lttng_payload_reset(&msg_payload); |
| 4402 | return ret; |
| 4403 | } |
| 4404 | |
| 4405 | static |
| 4406 | struct lttng_event_notifier_notification *recv_one_event_notifier_notification( |
| 4407 | int notification_pipe_read_fd, enum lttng_domain_type domain) |
| 4408 | { |
| 4409 | int ret; |
| 4410 | uint64_t token; |
| 4411 | struct lttng_event_notifier_notification *notification = NULL; |
| 4412 | char *capture_buffer = NULL; |
| 4413 | size_t capture_buffer_size; |
| 4414 | void *reception_buffer; |
| 4415 | size_t reception_size; |
| 4416 | |
| 4417 | struct lttng_ust_abi_event_notifier_notification ust_notification; |
| 4418 | struct lttng_kernel_event_notifier_notification kernel_notification; |
| 4419 | |
| 4420 | /* Init lttng_event_notifier_notification */ |
| 4421 | switch(domain) { |
| 4422 | case LTTNG_DOMAIN_UST: |
| 4423 | reception_buffer = (void *) &ust_notification; |
| 4424 | reception_size = sizeof(ust_notification); |
| 4425 | break; |
| 4426 | case LTTNG_DOMAIN_KERNEL: |
| 4427 | reception_buffer = (void *) &kernel_notification; |
| 4428 | reception_size = sizeof(kernel_notification); |
| 4429 | break; |
| 4430 | default: |
| 4431 | abort(); |
| 4432 | } |
| 4433 | |
| 4434 | /* |
| 4435 | * The monitoring pipe only holds messages smaller than PIPE_BUF, |
| 4436 | * ensuring that read/write of tracer notifications are atomic. |
| 4437 | */ |
| 4438 | ret = lttng_read(notification_pipe_read_fd, reception_buffer, |
| 4439 | reception_size); |
| 4440 | if (ret != reception_size) { |
| 4441 | PERROR("Failed to read from event source notification pipe: fd = %d, size to read = %zu, ret = %d", |
| 4442 | notification_pipe_read_fd, reception_size, ret); |
| 4443 | ret = -1; |
| 4444 | goto end; |
| 4445 | } |
| 4446 | |
| 4447 | switch(domain) { |
| 4448 | case LTTNG_DOMAIN_UST: |
| 4449 | token = ust_notification.token; |
| 4450 | capture_buffer_size = ust_notification.capture_buf_size; |
| 4451 | break; |
| 4452 | case LTTNG_DOMAIN_KERNEL: |
| 4453 | token = kernel_notification.token; |
| 4454 | capture_buffer_size = kernel_notification.capture_buf_size; |
| 4455 | break; |
| 4456 | default: |
| 4457 | abort(); |
| 4458 | } |
| 4459 | |
| 4460 | if (capture_buffer_size == 0) { |
| 4461 | capture_buffer = NULL; |
| 4462 | goto skip_capture; |
| 4463 | } |
| 4464 | |
| 4465 | if (capture_buffer_size > MAX_CAPTURE_SIZE) { |
| 4466 | ERR("[notification-thread] Event notifier has a capture payload size which exceeds the maximum allowed size: capture_payload_size = %zu bytes, max allowed size = %d bytes", |
| 4467 | capture_buffer_size, MAX_CAPTURE_SIZE); |
| 4468 | goto end; |
| 4469 | } |
| 4470 | |
| 4471 | capture_buffer = zmalloc(capture_buffer_size); |
| 4472 | if (!capture_buffer) { |
| 4473 | ERR("[notification-thread] Failed to allocate capture buffer"); |
| 4474 | goto end; |
| 4475 | } |
| 4476 | |
| 4477 | /* Fetch additional payload (capture). */ |
| 4478 | ret = lttng_read(notification_pipe_read_fd, capture_buffer, capture_buffer_size); |
| 4479 | if (ret != capture_buffer_size) { |
| 4480 | ERR("[notification-thread] Failed to read from event source pipe (fd = %i)", |
| 4481 | notification_pipe_read_fd); |
| 4482 | goto end; |
| 4483 | } |
| 4484 | |
| 4485 | skip_capture: |
| 4486 | notification = lttng_event_notifier_notification_create(token, domain, |
| 4487 | capture_buffer, capture_buffer_size); |
| 4488 | if (notification == NULL) { |
| 4489 | goto end; |
| 4490 | } |
| 4491 | |
| 4492 | /* |
| 4493 | * Ownership transfered to the lttng_event_notifier_notification object. |
| 4494 | */ |
| 4495 | capture_buffer = NULL; |
| 4496 | |
| 4497 | end: |
| 4498 | free(capture_buffer); |
| 4499 | return notification; |
| 4500 | } |
| 4501 | |
| 4502 | static |
| 4503 | int dispatch_one_event_notifier_notification(struct notification_thread_state *state, |
| 4504 | struct lttng_event_notifier_notification *notification) |
| 4505 | { |
| 4506 | struct cds_lfht_node *node; |
| 4507 | struct cds_lfht_iter iter; |
| 4508 | struct notification_trigger_tokens_ht_element *element; |
| 4509 | enum lttng_trigger_status trigger_status; |
| 4510 | struct lttng_evaluation *evaluation = NULL; |
| 4511 | enum action_executor_status executor_status; |
| 4512 | struct notification_client_list *client_list = NULL; |
| 4513 | const char *trigger_name; |
| 4514 | int ret; |
| 4515 | unsigned int capture_count = 0; |
| 4516 | |
| 4517 | /* Find triggers associated with this token. */ |
| 4518 | rcu_read_lock(); |
| 4519 | cds_lfht_lookup(state->trigger_tokens_ht, |
| 4520 | hash_key_u64(¬ification->tracer_token, lttng_ht_seed), |
| 4521 | match_trigger_token, ¬ification->tracer_token, &iter); |
| 4522 | node = cds_lfht_iter_get_node(&iter); |
| 4523 | if (caa_unlikely(!node)) { |
| 4524 | /* |
| 4525 | * This is not an error, slow consumption of the tracer |
| 4526 | * notifications can lead to situations where a trigger is |
| 4527 | * removed but we still get tracer notifications matching a |
| 4528 | * trigger that no longer exists. |
| 4529 | */ |
| 4530 | ret = 0; |
| 4531 | goto end_unlock; |
| 4532 | } |
| 4533 | |
| 4534 | element = caa_container_of(node, |
| 4535 | struct notification_trigger_tokens_ht_element, |
| 4536 | node); |
| 4537 | |
| 4538 | if (!lttng_trigger_should_fire(element->trigger)) { |
| 4539 | ret = 0; |
| 4540 | goto end_unlock; |
| 4541 | } |
| 4542 | |
| 4543 | lttng_trigger_fire(element->trigger); |
| 4544 | |
| 4545 | trigger_status = lttng_trigger_get_name(element->trigger, &trigger_name); |
| 4546 | assert(trigger_status == LTTNG_TRIGGER_STATUS_OK); |
| 4547 | |
| 4548 | if (lttng_condition_on_event_get_capture_descriptor_count( |
| 4549 | lttng_trigger_get_const_condition(element->trigger), |
| 4550 | &capture_count) != LTTNG_CONDITION_STATUS_OK) { |
| 4551 | ERR("Failed to get capture count"); |
| 4552 | ret = -1; |
| 4553 | goto end; |
| 4554 | } |
| 4555 | |
| 4556 | if (!notification->capture_buffer && capture_count != 0) { |
| 4557 | ERR("Expected capture but capture buffer is null"); |
| 4558 | ret = -1; |
| 4559 | goto end; |
| 4560 | } |
| 4561 | |
| 4562 | evaluation = lttng_evaluation_on_event_create( |
| 4563 | container_of(lttng_trigger_get_const_condition( |
| 4564 | element->trigger), |
| 4565 | struct lttng_condition_on_event, |
| 4566 | parent), |
| 4567 | trigger_name, |
| 4568 | notification->capture_buffer, |
| 4569 | notification->capture_buf_size, false); |
| 4570 | |
| 4571 | if (evaluation == NULL) { |
| 4572 | ERR("[notification-thread] Failed to create event rule hit evaluation while creating and enqueuing action executor job"); |
| 4573 | ret = -1; |
| 4574 | goto end_unlock; |
| 4575 | } |
| 4576 | client_list = get_client_list_from_condition(state, |
| 4577 | lttng_trigger_get_const_condition(element->trigger)); |
| 4578 | executor_status = action_executor_enqueue(state->executor, |
| 4579 | element->trigger, evaluation, NULL, client_list); |
| 4580 | switch (executor_status) { |
| 4581 | case ACTION_EXECUTOR_STATUS_OK: |
| 4582 | ret = 0; |
| 4583 | break; |
| 4584 | case ACTION_EXECUTOR_STATUS_OVERFLOW: |
| 4585 | { |
| 4586 | struct notification_client_list_element *client_list_element, |
| 4587 | *tmp; |
| 4588 | |
| 4589 | /* |
| 4590 | * Not a fatal error; this is expected and simply means the |
| 4591 | * executor has too much work queued already. |
| 4592 | */ |
| 4593 | ret = 0; |
| 4594 | |
| 4595 | /* No clients subscribed to notifications for this trigger. */ |
| 4596 | if (!client_list) { |
| 4597 | break; |
| 4598 | } |
| 4599 | |
| 4600 | /* Warn clients that a notification (or more) was dropped. */ |
| 4601 | pthread_mutex_lock(&client_list->lock); |
| 4602 | cds_list_for_each_entry_safe(client_list_element, tmp, |
| 4603 | &client_list->list, node) { |
| 4604 | enum client_transmission_status transmission_status; |
| 4605 | struct notification_client *client = |
| 4606 | client_list_element->client; |
| 4607 | |
| 4608 | pthread_mutex_lock(&client->lock); |
| 4609 | ret = client_notification_overflow(client); |
| 4610 | if (ret) { |
| 4611 | /* Fatal error. */ |
| 4612 | goto next_client; |
| 4613 | } |
| 4614 | |
| 4615 | transmission_status = |
| 4616 | client_flush_outgoing_queue(client); |
| 4617 | ret = client_handle_transmission_status( |
| 4618 | client, transmission_status, state); |
| 4619 | if (ret) { |
| 4620 | /* Fatal error. */ |
| 4621 | goto next_client; |
| 4622 | } |
| 4623 | next_client: |
| 4624 | pthread_mutex_unlock(&client->lock); |
| 4625 | if (ret) { |
| 4626 | break; |
| 4627 | } |
| 4628 | } |
| 4629 | |
| 4630 | pthread_mutex_unlock(&client_list->lock); |
| 4631 | break; |
| 4632 | } |
| 4633 | case ACTION_EXECUTOR_STATUS_INVALID: |
| 4634 | case ACTION_EXECUTOR_STATUS_ERROR: |
| 4635 | /* Fatal error, shut down everything. */ |
| 4636 | ERR("Fatal error encoutered while enqueuing action to the action executor"); |
| 4637 | ret = -1; |
| 4638 | goto end_unlock; |
| 4639 | default: |
| 4640 | /* Unhandled error. */ |
| 4641 | abort(); |
| 4642 | } |
| 4643 | |
| 4644 | end_unlock: |
| 4645 | notification_client_list_put(client_list); |
| 4646 | rcu_read_unlock(); |
| 4647 | end: |
| 4648 | return ret; |
| 4649 | } |
| 4650 | |
| 4651 | static |
| 4652 | int handle_one_event_notifier_notification( |
| 4653 | struct notification_thread_state *state, |
| 4654 | int pipe, enum lttng_domain_type domain) |
| 4655 | { |
| 4656 | int ret = 0; |
| 4657 | struct lttng_event_notifier_notification *notification = NULL; |
| 4658 | |
| 4659 | notification = recv_one_event_notifier_notification(pipe, domain); |
| 4660 | if (notification == NULL) { |
| 4661 | /* Reception failed, don't consider it fatal. */ |
| 4662 | ERR("[notification-thread] Error receiving an event notifier notification from tracer: fd = %i, domain = %s", |
| 4663 | pipe, lttng_domain_type_str(domain)); |
| 4664 | goto end; |
| 4665 | } |
| 4666 | |
| 4667 | ret = dispatch_one_event_notifier_notification(state, notification); |
| 4668 | if (ret) { |
| 4669 | ERR("[notification-thread] Error dispatching an event notifier notification from tracer: fd = %i, domain = %s", |
| 4670 | pipe, lttng_domain_type_str(domain)); |
| 4671 | goto end; |
| 4672 | } |
| 4673 | |
| 4674 | end: |
| 4675 | lttng_event_notifier_notification_destroy(notification); |
| 4676 | return ret; |
| 4677 | } |
| 4678 | |
| 4679 | int handle_notification_thread_event_notification(struct notification_thread_state *state, |
| 4680 | int pipe, enum lttng_domain_type domain) |
| 4681 | { |
| 4682 | return handle_one_event_notifier_notification(state, pipe, domain); |
| 4683 | } |
| 4684 | |
| 4685 | int handle_notification_thread_channel_sample( |
| 4686 | struct notification_thread_state *state, int pipe, |
| 4687 | enum lttng_domain_type domain) |
| 4688 | { |
| 4689 | int ret = 0; |
| 4690 | struct lttcomm_consumer_channel_monitor_msg sample_msg; |
| 4691 | struct channel_info *channel_info; |
| 4692 | struct cds_lfht_node *node; |
| 4693 | struct cds_lfht_iter iter; |
| 4694 | struct lttng_channel_trigger_list *trigger_list; |
| 4695 | struct lttng_trigger_list_element *trigger_list_element; |
| 4696 | bool previous_sample_available = false; |
| 4697 | struct channel_state_sample previous_sample, latest_sample; |
| 4698 | uint64_t previous_session_consumed_total, latest_session_consumed_total; |
| 4699 | struct lttng_credentials channel_creds; |
| 4700 | |
| 4701 | /* |
| 4702 | * The monitoring pipe only holds messages smaller than PIPE_BUF, |
| 4703 | * ensuring that read/write of sampling messages are atomic. |
| 4704 | */ |
| 4705 | ret = lttng_read(pipe, &sample_msg, sizeof(sample_msg)); |
| 4706 | if (ret != sizeof(sample_msg)) { |
| 4707 | ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)", |
| 4708 | pipe); |
| 4709 | ret = -1; |
| 4710 | goto end; |
| 4711 | } |
| 4712 | |
| 4713 | ret = 0; |
| 4714 | latest_sample.key.key = sample_msg.key; |
| 4715 | latest_sample.key.domain = domain; |
| 4716 | latest_sample.highest_usage = sample_msg.highest; |
| 4717 | latest_sample.lowest_usage = sample_msg.lowest; |
| 4718 | latest_sample.channel_total_consumed = sample_msg.total_consumed; |
| 4719 | |
| 4720 | rcu_read_lock(); |
| 4721 | |
| 4722 | /* Retrieve the channel's informations */ |
| 4723 | cds_lfht_lookup(state->channels_ht, |
| 4724 | hash_channel_key(&latest_sample.key), |
| 4725 | match_channel_info, |
| 4726 | &latest_sample.key, |
| 4727 | &iter); |
| 4728 | node = cds_lfht_iter_get_node(&iter); |
| 4729 | if (caa_unlikely(!node)) { |
| 4730 | /* |
| 4731 | * Not an error since the consumer can push a sample to the pipe |
| 4732 | * and the rest of the session daemon could notify us of the |
| 4733 | * channel's destruction before we get a chance to process that |
| 4734 | * sample. |
| 4735 | */ |
| 4736 | DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64 " in %s domain", |
| 4737 | latest_sample.key.key, |
| 4738 | lttng_domain_type_str(domain)); |
| 4739 | goto end_unlock; |
| 4740 | } |
| 4741 | channel_info = caa_container_of(node, struct channel_info, |
| 4742 | channels_ht_node); |
| 4743 | DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64 ") in session %s (highest usage = %" PRIu64 ", lowest usage = %" PRIu64", total consumed = %" PRIu64")", |
| 4744 | channel_info->name, |
| 4745 | latest_sample.key.key, |
| 4746 | channel_info->session_info->name, |
| 4747 | latest_sample.highest_usage, |
| 4748 | latest_sample.lowest_usage, |
| 4749 | latest_sample.channel_total_consumed); |
| 4750 | |
| 4751 | previous_session_consumed_total = |
| 4752 | channel_info->session_info->consumed_data_size; |
| 4753 | |
| 4754 | /* Retrieve the channel's last sample, if it exists, and update it. */ |
| 4755 | cds_lfht_lookup(state->channel_state_ht, |
| 4756 | hash_channel_key(&latest_sample.key), |
| 4757 | match_channel_state_sample, |
| 4758 | &latest_sample.key, |
| 4759 | &iter); |
| 4760 | node = cds_lfht_iter_get_node(&iter); |
| 4761 | if (caa_likely(node)) { |
| 4762 | struct channel_state_sample *stored_sample; |
| 4763 | |
| 4764 | /* Update the sample stored. */ |
| 4765 | stored_sample = caa_container_of(node, |
| 4766 | struct channel_state_sample, |
| 4767 | channel_state_ht_node); |
| 4768 | |
| 4769 | memcpy(&previous_sample, stored_sample, |
| 4770 | sizeof(previous_sample)); |
| 4771 | stored_sample->highest_usage = latest_sample.highest_usage; |
| 4772 | stored_sample->lowest_usage = latest_sample.lowest_usage; |
| 4773 | stored_sample->channel_total_consumed = latest_sample.channel_total_consumed; |
| 4774 | previous_sample_available = true; |
| 4775 | |
| 4776 | latest_session_consumed_total = |
| 4777 | previous_session_consumed_total + |
| 4778 | (latest_sample.channel_total_consumed - previous_sample.channel_total_consumed); |
| 4779 | } else { |
| 4780 | /* |
| 4781 | * This is the channel's first sample, allocate space for and |
| 4782 | * store the new sample. |
| 4783 | */ |
| 4784 | struct channel_state_sample *stored_sample; |
| 4785 | |
| 4786 | stored_sample = zmalloc(sizeof(*stored_sample)); |
| 4787 | if (!stored_sample) { |
| 4788 | ret = -1; |
| 4789 | goto end_unlock; |
| 4790 | } |
| 4791 | |
| 4792 | memcpy(stored_sample, &latest_sample, sizeof(*stored_sample)); |
| 4793 | cds_lfht_node_init(&stored_sample->channel_state_ht_node); |
| 4794 | cds_lfht_add(state->channel_state_ht, |
| 4795 | hash_channel_key(&stored_sample->key), |
| 4796 | &stored_sample->channel_state_ht_node); |
| 4797 | |
| 4798 | latest_session_consumed_total = |
| 4799 | previous_session_consumed_total + |
| 4800 | latest_sample.channel_total_consumed; |
| 4801 | } |
| 4802 | |
| 4803 | channel_info->session_info->consumed_data_size = |
| 4804 | latest_session_consumed_total; |
| 4805 | |
| 4806 | /* Find triggers associated with this channel. */ |
| 4807 | cds_lfht_lookup(state->channel_triggers_ht, |
| 4808 | hash_channel_key(&latest_sample.key), |
| 4809 | match_channel_trigger_list, |
| 4810 | &latest_sample.key, |
| 4811 | &iter); |
| 4812 | node = cds_lfht_iter_get_node(&iter); |
| 4813 | if (caa_likely(!node)) { |
| 4814 | goto end_unlock; |
| 4815 | } |
| 4816 | |
| 4817 | channel_creds = (typeof(channel_creds)) { |
| 4818 | .uid = LTTNG_OPTIONAL_INIT_VALUE(channel_info->session_info->uid), |
| 4819 | .gid = LTTNG_OPTIONAL_INIT_VALUE(channel_info->session_info->gid), |
| 4820 | }; |
| 4821 | |
| 4822 | trigger_list = caa_container_of(node, struct lttng_channel_trigger_list, |
| 4823 | channel_triggers_ht_node); |
| 4824 | cds_list_for_each_entry(trigger_list_element, &trigger_list->list, |
| 4825 | node) { |
| 4826 | const struct lttng_condition *condition; |
| 4827 | struct lttng_trigger *trigger; |
| 4828 | struct notification_client_list *client_list = NULL; |
| 4829 | struct lttng_evaluation *evaluation = NULL; |
| 4830 | enum action_executor_status executor_status; |
| 4831 | |
| 4832 | ret = 0; |
| 4833 | trigger = trigger_list_element->trigger; |
| 4834 | condition = lttng_trigger_get_const_condition(trigger); |
| 4835 | assert(condition); |
| 4836 | |
| 4837 | /* |
| 4838 | * Check if any client is subscribed to the result of this |
| 4839 | * evaluation. |
| 4840 | */ |
| 4841 | client_list = get_client_list_from_condition(state, condition); |
| 4842 | |
| 4843 | ret = evaluate_buffer_condition(condition, &evaluation, state, |
| 4844 | previous_sample_available ? &previous_sample : NULL, |
| 4845 | &latest_sample, |
| 4846 | previous_session_consumed_total, |
| 4847 | latest_session_consumed_total, |
| 4848 | channel_info); |
| 4849 | if (caa_unlikely(ret)) { |
| 4850 | goto put_list; |
| 4851 | } |
| 4852 | |
| 4853 | if (caa_likely(!evaluation)) { |
| 4854 | goto put_list; |
| 4855 | } |
| 4856 | |
| 4857 | if (!lttng_trigger_should_fire(trigger)) { |
| 4858 | goto put_list; |
| 4859 | } |
| 4860 | |
| 4861 | lttng_trigger_fire(trigger); |
| 4862 | |
| 4863 | /* |
| 4864 | * Ownership of `evaluation` transferred to the action executor |
| 4865 | * no matter the result. |
| 4866 | */ |
| 4867 | executor_status = action_executor_enqueue(state->executor, |
| 4868 | trigger, evaluation, &channel_creds, |
| 4869 | client_list); |
| 4870 | evaluation = NULL; |
| 4871 | switch (executor_status) { |
| 4872 | case ACTION_EXECUTOR_STATUS_OK: |
| 4873 | break; |
| 4874 | case ACTION_EXECUTOR_STATUS_ERROR: |
| 4875 | case ACTION_EXECUTOR_STATUS_INVALID: |
| 4876 | /* |
| 4877 | * TODO Add trigger identification (name/id) when |
| 4878 | * it is added to the API. |
| 4879 | */ |
| 4880 | ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger"); |
| 4881 | ret = -1; |
| 4882 | goto put_list; |
| 4883 | case ACTION_EXECUTOR_STATUS_OVERFLOW: |
| 4884 | /* |
| 4885 | * TODO Add trigger identification (name/id) when |
| 4886 | * it is added to the API. |
| 4887 | * |
| 4888 | * Not a fatal error. |
| 4889 | */ |
| 4890 | WARN("No space left when enqueuing action associated with buffer-condition trigger"); |
| 4891 | ret = 0; |
| 4892 | goto put_list; |
| 4893 | default: |
| 4894 | abort(); |
| 4895 | } |
| 4896 | |
| 4897 | put_list: |
| 4898 | notification_client_list_put(client_list); |
| 4899 | if (caa_unlikely(ret)) { |
| 4900 | break; |
| 4901 | } |
| 4902 | } |
| 4903 | end_unlock: |
| 4904 | rcu_read_unlock(); |
| 4905 | end: |
| 4906 | return ret; |
| 4907 | } |