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