Implements `lttng_event_notifier_notification_{create,destroy}()`
[lttng-tools.git] / src / bin / lttng-sessiond / notification-thread-internal.h
CommitLineData
8abe313a 1/*
ab5be9fa 2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8abe313a 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
8abe313a 5 *
8abe313a
JG
6 */
7
8#ifndef NOTIFICATION_THREAD_INTERNAL_H
9#define NOTIFICATION_THREAD_INTERNAL_H
10
f2b3ef9f
JG
11#include <common/compat/socket.h>
12#include <common/credentials.h>
882093ee 13#include <common/payload.h>
f2b3ef9f 14#include <lttng/notification/channel-internal.h>
8abe313a 15#include <lttng/ref-internal.h>
f2b3ef9f 16#include <stdbool.h>
ae0a823f 17#include <unistd.h>
f2b3ef9f
JG
18#include <urcu/rculfhash.h>
19#include <urcu/ref.h>
20#include <urcu/call-rcu.h>
21#include "notification-thread.h"
22
23struct lttng_evaluation;
24struct notification_thread_handle;
8abe313a
JG
25
26struct channel_key {
27 uint64_t key;
28 enum lttng_domain_type domain;
29};
30
31struct session_info {
32 struct lttng_ref ref;
33 char *name;
34 uid_t uid;
35 gid_t gid;
36 /*
c7283958 37 * Hashtable containing back-refs (weak) to all channels in this session.
8abe313a
JG
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;
ea9a44f0 42 struct lttng_session_trigger_list *trigger_list;
8abe313a
JG
43 /* Node in the notification thread state's sessions_ht. */
44 struct cds_lfht_node sessions_ht_node;
1eee26c5
JG
45 /*
46 * Weak reference to the thread state's sessions_ht. Used for removal on
47 * destruction.
48 */
49 struct cds_lfht *sessions_ht;
e8360425 50 uint64_t consumed_data_size;
51eab943
JG
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;
83b934ad
MD
57 /* call_rcu delayed reclaim. */
58 struct rcu_head rcu_node;
8abe313a
JG
59};
60
61struct 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;
83b934ad
MD
74 /* call_rcu delayed reclaim. */
75 struct rcu_head rcu_node;
8abe313a
JG
76};
77
b9a8d78f
FD
78/*
79 * Facilities to carry the different notifications type in the action
80 * processing code path.
81 */
82struct lttng_event_notifier_notification {
83 uint64_t tracer_token;
84 enum lttng_domain_type type;
85};
86
f2b3ef9f
JG
87struct notification_client_list_element {
88 struct notification_client *client;
89 struct cds_list_head node;
90};
91
92/*
93 * Thread safety of notification_client and notification_client_list.
94 *
95 * The notification thread (main thread) and the action executor
96 * interact through client lists. Hence, when the action executor
97 * thread looks-up the list of clients subscribed to a given
98 * condition, it will acquire a reference to the list and lock it
99 * while attempting to communicate with the various clients.
100 *
101 * It is not necessary to reference-count clients as they are guaranteed
102 * to be 'alive' if they are present in a list and that list is locked. Indeed,
103 * removing references to the client from those subscription lists is part of
104 * the work performed on destruction of a client.
105 *
106 * No provision for other access scenarios are taken into account;
107 * this is the bare minimum to make these accesses safe and the
108 * notification thread's state is _not_ "thread-safe" in any general
109 * sense.
110 */
111struct notification_client_list {
112 pthread_mutex_t lock;
113 struct urcu_ref ref;
114 const struct lttng_trigger *trigger;
115 struct cds_list_head list;
116 /* Weak reference to container. */
117 struct cds_lfht *notification_trigger_clients_ht;
118 struct cds_lfht_node notification_trigger_clients_ht_node;
119 /* call_rcu delayed reclaim. */
120 struct rcu_head rcu_node;
121};
122
123struct notification_client {
6c24d3fd
JG
124 /*
125 * Nests within the notification_client_list lock.
126 *
127 * Protects the outbound communication and the active flag which
128 * is used by both the notification and action executor threads.
129 *
130 * The remaining fields of the object can be used without any
131 * synchronization as they are either immutable (id, creds, version) or
132 * only accessed by the notification thread.
133 */
f2b3ef9f
JG
134 pthread_mutex_t lock;
135 notification_client_id id;
136 int socket;
137 /* Client protocol version. */
138 uint8_t major, minor;
139 uid_t uid;
140 gid_t gid;
141 /*
142 * Indicates if the credentials and versions of the client have been
143 * checked.
144 */
145 bool validated;
146 /*
147 * Conditions to which the client's notification channel is subscribed.
148 * List of struct lttng_condition_list_node. The condition member is
149 * owned by the client.
150 */
151 struct cds_list_head condition_list;
152 struct cds_lfht_node client_socket_ht_node;
153 struct cds_lfht_node client_id_ht_node;
154 struct {
155 /*
156 * If a client's communication is inactive, it means that a
157 * fatal error has occurred (could be either a protocol error or
158 * the socket API returned a fatal error). No further
159 * communication should be attempted; the client is queued for
160 * clean-up.
161 */
162 bool active;
163 struct {
164 /*
165 * During the reception of a message, the reception
166 * buffers' "size" is set to contain the current
167 * message's complete payload.
168 */
882093ee 169 struct lttng_payload payload;
f2b3ef9f
JG
170 /* Bytes left to receive for the current message. */
171 size_t bytes_to_receive;
882093ee
JR
172 /* FDs left to receive for the current message. */
173 int fds_to_receive;
f2b3ef9f
JG
174 /* Type of the message being received. */
175 enum lttng_notification_channel_message_type msg_type;
176 /*
177 * Indicates whether or not credentials are expected
178 * from the client.
179 */
180 bool expect_creds;
181 /*
182 * Indicates whether or not credentials were received
183 * from the client.
184 */
185 bool creds_received;
186 /* Only used during credentials reception. */
187 lttng_sock_cred creds;
188 } inbound;
189 struct {
190 /*
191 * Indicates whether or not a notification addressed to
192 * this client was dropped because a command reply was
193 * already buffered.
194 *
195 * A notification is dropped whenever the buffer is not
196 * empty.
197 */
198 bool dropped_notification;
199 /*
200 * Indicates whether or not a command reply is already
201 * buffered. In this case, it means that the client is
202 * not consuming command replies before emitting a new
203 * one. This could be caused by a protocol error or a
204 * misbehaving/malicious client.
205 */
206 bool queued_command_reply;
882093ee 207 struct lttng_payload payload;
f2b3ef9f
JG
208 } outbound;
209 } communication;
210 /* call_rcu delayed reclaim. */
211 struct rcu_head rcu_node;
212};
213
dd877ea6
JG
214enum client_transmission_status {
215 CLIENT_TRANSMISSION_STATUS_COMPLETE,
216 CLIENT_TRANSMISSION_STATUS_QUEUED,
217 /* Communication failure. */
218 CLIENT_TRANSMISSION_STATUS_FAIL,
219 /* Fatal error. */
220 CLIENT_TRANSMISSION_STATUS_ERROR,
221};
f2b3ef9f
JG
222
223LTTNG_HIDDEN
224bool notification_client_list_get(struct notification_client_list *list);
225
226LTTNG_HIDDEN
227void notification_client_list_put(struct notification_client_list *list);
228
6c24d3fd 229/* Only returns a non-zero value if a fatal error occurred. */
f2b3ef9f
JG
230typedef int (*report_client_transmission_result_cb)(
231 struct notification_client *client,
232 enum client_transmission_status status,
233 void *user_data);
234
235LTTNG_HIDDEN
236int notification_client_list_send_evaluation(
237 struct notification_client_list *list,
238 const struct lttng_condition *condition,
239 const struct lttng_evaluation *evaluation,
240 const struct lttng_credentials *trigger_creds,
241 const struct lttng_credentials *source_object_creds,
242 report_client_transmission_result_cb client_report,
243 void *user_data);
244
245LTTNG_HIDDEN
246int notification_thread_client_communication_update(
247 struct notification_thread_handle *handle,
248 notification_client_id id,
249 enum client_transmission_status transmission_status);
250
b9a8d78f
FD
251LTTNG_HIDDEN
252struct lttng_event_notifier_notification *lttng_event_notifier_notification_create(
253 uint64_t tracer_token,
254 enum lttng_domain_type domain);
255
256LTTNG_HIDDEN
257void lttng_event_notifier_notification_destroy(
258 struct lttng_event_notifier_notification *event_notifier_notification);
259
8abe313a 260#endif /* NOTIFICATION_THREAD_INTERNAL_H */
This page took 0.043934 seconds and 4 git commands to generate.