| 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 | #ifndef NOTIFICATION_THREAD_INTERNAL_H |
| 9 | #define NOTIFICATION_THREAD_INTERNAL_H |
| 10 | |
| 11 | #include <common/credentials.h> |
| 12 | #include <lttng/notification/channel-internal.h> |
| 13 | #include <lttng/ref-internal.h> |
| 14 | #include <stdbool.h> |
| 15 | #include <unistd.h> |
| 16 | #include <urcu/rculfhash.h> |
| 17 | #include <urcu/ref.h> |
| 18 | |
| 19 | struct channel_key { |
| 20 | uint64_t key; |
| 21 | enum lttng_domain_type domain; |
| 22 | }; |
| 23 | |
| 24 | struct session_info { |
| 25 | struct lttng_ref ref; |
| 26 | char *name; |
| 27 | uid_t uid; |
| 28 | gid_t gid; |
| 29 | /* |
| 30 | * Hashtable containing back-refs (weak) to all channels in this session. |
| 31 | * The hashtable's key is a hash of (struct channel_key) and |
| 32 | * the value is of type (struct channel_info *). |
| 33 | */ |
| 34 | struct cds_lfht *channel_infos_ht; |
| 35 | struct lttng_session_trigger_list *trigger_list; |
| 36 | /* Node in the notification thread state's sessions_ht. */ |
| 37 | struct cds_lfht_node sessions_ht_node; |
| 38 | /* |
| 39 | * Weak reference to the thread state's sessions_ht. Used for removal on |
| 40 | * destruction. |
| 41 | */ |
| 42 | struct cds_lfht *sessions_ht; |
| 43 | uint64_t consumed_data_size; |
| 44 | struct { |
| 45 | /* Whether a rotation is ongoing for this session. */ |
| 46 | bool ongoing; |
| 47 | /* Identifier of the currently ongoing rotation. */ |
| 48 | uint64_t id; |
| 49 | } rotation; |
| 50 | /* call_rcu delayed reclaim. */ |
| 51 | struct rcu_head rcu_node; |
| 52 | }; |
| 53 | |
| 54 | struct channel_info { |
| 55 | struct channel_key key; |
| 56 | char *name; |
| 57 | uint64_t capacity; |
| 58 | /* |
| 59 | * A channel info holds a reference (lttng_ref) on session_info. |
| 60 | * session_info, in return, holds a weak reference to the channel. |
| 61 | */ |
| 62 | struct session_info *session_info; |
| 63 | /* Node in the notification thread state's channels_ht. */ |
| 64 | struct cds_lfht_node channels_ht_node; |
| 65 | /* Node in the session_info's channels_ht. */ |
| 66 | struct cds_lfht_node session_info_channels_ht_node; |
| 67 | /* call_rcu delayed reclaim. */ |
| 68 | struct rcu_head rcu_node; |
| 69 | }; |
| 70 | |
| 71 | struct notification_client_list_element { |
| 72 | struct notification_client *client; |
| 73 | struct cds_list_head node; |
| 74 | }; |
| 75 | |
| 76 | /* |
| 77 | * Thread safety of notification_client and notification_client_list. |
| 78 | * |
| 79 | * The notification thread (main thread) and the action executor |
| 80 | * interact through client lists. Hence, when the action executor |
| 81 | * thread looks-up the list of clients subscribed to a given |
| 82 | * condition, it will acquire a reference to the list and lock it |
| 83 | * while attempting to communicate with the various clients. |
| 84 | * |
| 85 | * It is not necessary to reference-count clients as they are guaranteed |
| 86 | * to be 'alive' if they are present in a list and that list is locked. Indeed, |
| 87 | * removing references to the client from those subscription lists is part of |
| 88 | * the work performed on destruction of a client. |
| 89 | * |
| 90 | * No provision for other access scenarios are taken into account; |
| 91 | * this is the bare minimum to make these accesses safe and the |
| 92 | * notification thread's state is _not_ "thread-safe" in any general |
| 93 | * sense. |
| 94 | */ |
| 95 | struct notification_client_list { |
| 96 | pthread_mutex_t lock; |
| 97 | struct urcu_ref ref; |
| 98 | const struct lttng_trigger *trigger; |
| 99 | struct cds_list_head list; |
| 100 | /* Weak reference to container. */ |
| 101 | struct cds_lfht *notification_trigger_clients_ht; |
| 102 | struct cds_lfht_node notification_trigger_clients_ht_node; |
| 103 | /* call_rcu delayed reclaim. */ |
| 104 | struct rcu_head rcu_node; |
| 105 | }; |
| 106 | |
| 107 | struct notification_client { |
| 108 | /* Nests within the notification_client_list lock. */ |
| 109 | pthread_mutex_t lock; |
| 110 | notification_client_id id; |
| 111 | int socket; |
| 112 | /* Client protocol version. */ |
| 113 | uint8_t major, minor; |
| 114 | uid_t uid; |
| 115 | gid_t gid; |
| 116 | /* |
| 117 | * Indicates if the credentials and versions of the client have been |
| 118 | * checked. |
| 119 | */ |
| 120 | bool validated; |
| 121 | /* |
| 122 | * Conditions to which the client's notification channel is subscribed. |
| 123 | * List of struct lttng_condition_list_node. The condition member is |
| 124 | * owned by the client. |
| 125 | */ |
| 126 | struct cds_list_head condition_list; |
| 127 | struct cds_lfht_node client_socket_ht_node; |
| 128 | struct cds_lfht_node client_id_ht_node; |
| 129 | struct { |
| 130 | /* |
| 131 | * If a client's communication is inactive, it means that a |
| 132 | * fatal error has occurred (could be either a protocol error or |
| 133 | * the socket API returned a fatal error). No further |
| 134 | * communication should be attempted; the client is queued for |
| 135 | * clean-up. |
| 136 | */ |
| 137 | bool active; |
| 138 | struct { |
| 139 | /* |
| 140 | * During the reception of a message, the reception |
| 141 | * buffers' "size" is set to contain the current |
| 142 | * message's complete payload. |
| 143 | */ |
| 144 | struct lttng_dynamic_buffer buffer; |
| 145 | /* Bytes left to receive for the current message. */ |
| 146 | size_t bytes_to_receive; |
| 147 | /* Type of the message being received. */ |
| 148 | enum lttng_notification_channel_message_type msg_type; |
| 149 | /* |
| 150 | * Indicates whether or not credentials are expected |
| 151 | * from the client. |
| 152 | */ |
| 153 | bool expect_creds; |
| 154 | /* |
| 155 | * Indicates whether or not credentials were received |
| 156 | * from the client. |
| 157 | */ |
| 158 | bool creds_received; |
| 159 | /* Only used during credentials reception. */ |
| 160 | lttng_sock_cred creds; |
| 161 | } inbound; |
| 162 | struct { |
| 163 | /* |
| 164 | * Indicates whether or not a notification addressed to |
| 165 | * this client was dropped because a command reply was |
| 166 | * already buffered. |
| 167 | * |
| 168 | * A notification is dropped whenever the buffer is not |
| 169 | * empty. |
| 170 | */ |
| 171 | bool dropped_notification; |
| 172 | /* |
| 173 | * Indicates whether or not a command reply is already |
| 174 | * buffered. In this case, it means that the client is |
| 175 | * not consuming command replies before emitting a new |
| 176 | * one. This could be caused by a protocol error or a |
| 177 | * misbehaving/malicious client. |
| 178 | */ |
| 179 | bool queued_command_reply; |
| 180 | struct lttng_dynamic_buffer buffer; |
| 181 | } outbound; |
| 182 | } communication; |
| 183 | /* call_rcu delayed reclaim. */ |
| 184 | struct rcu_head rcu_node; |
| 185 | }; |
| 186 | |
| 187 | enum client_transmission_status { |
| 188 | CLIENT_TRANSMISSION_STATUS_COMPLETE, |
| 189 | CLIENT_TRANSMISSION_STATUS_QUEUED, |
| 190 | /* Communication failure. */ |
| 191 | CLIENT_TRANSMISSION_STATUS_FAIL, |
| 192 | /* Fatal error. */ |
| 193 | CLIENT_TRANSMISSION_STATUS_ERROR, |
| 194 | }; |
| 195 | |
| 196 | LTTNG_HIDDEN |
| 197 | bool notification_client_list_get(struct notification_client_list *list); |
| 198 | |
| 199 | LTTNG_HIDDEN |
| 200 | void notification_client_list_put(struct notification_client_list *list); |
| 201 | |
| 202 | typedef int (*report_client_transmission_result_cb)( |
| 203 | struct notification_client *client, |
| 204 | enum client_transmission_status status, |
| 205 | void *user_data); |
| 206 | |
| 207 | LTTNG_HIDDEN |
| 208 | int notification_client_list_send_evaluation( |
| 209 | struct notification_client_list *list, |
| 210 | const struct lttng_condition *condition, |
| 211 | const struct lttng_evaluation *evaluation, |
| 212 | const struct lttng_credentials *trigger_creds, |
| 213 | const struct lttng_credentials *source_object_creds, |
| 214 | report_client_transmission_result_cb client_report, |
| 215 | void *user_data); |
| 216 | |
| 217 | #endif /* NOTIFICATION_THREAD_INTERNAL_H */ |