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