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