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