Add a basic .clang-tidy file and fix typedef warnings
[lttng-tools.git] / src / bin / lttng-sessiond / notification-thread-internal.hpp
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
c9e313bc
SM
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>
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>
c9e313bc 21#include "notification-thread.hpp"
f2b3ef9f
JG
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
319dcddc
JG
31struct 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
8abe313a
JG
43struct session_info {
44 struct lttng_ref ref;
139a8d25 45 uint64_t id;
8abe313a
JG
46 char *name;
47 uid_t uid;
48 gid_t gid;
49 /*
c7283958 50 * Hashtable containing back-refs (weak) to all channels in this session.
8abe313a
JG
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;
ea9a44f0 55 struct lttng_session_trigger_list *trigger_list;
8abe313a
JG
56 /* Node in the notification thread state's sessions_ht. */
57 struct cds_lfht_node sessions_ht_node;
1eee26c5
JG
58 /*
59 * Weak reference to the thread state's sessions_ht. Used for removal on
60 * destruction.
61 */
62 struct cds_lfht *sessions_ht;
319dcddc
JG
63 /* Session's state as of the latest update. */
64 struct session_state_sample last_state_sample;
83b934ad
MD
65 /* call_rcu delayed reclaim. */
66 struct rcu_head rcu_node;
8abe313a
JG
67};
68
69struct 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;
83b934ad
MD
82 /* call_rcu delayed reclaim. */
83 struct rcu_head rcu_node;
8abe313a
JG
84};
85
b9a8d78f
FD
86/*
87 * Facilities to carry the different notifications type in the action
88 * processing code path.
89 */
90struct lttng_event_notifier_notification {
91 uint64_t tracer_token;
92 enum lttng_domain_type type;
82b3cbf4
JR
93 size_t capture_buf_size;
94 char *capture_buffer;
b9a8d78f
FD
95};
96
f2b3ef9f
JG
97struct 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 */
121struct notification_client_list {
122 pthread_mutex_t lock;
123 struct urcu_ref ref;
091fa780 124 struct lttng_condition *condition;
57644a7f 125 /* List of triggers that have an identical condition than `condition`. */
091fa780
FD
126 struct cds_list_head triggers_list;
127 struct cds_list_head clients_list;
f2b3ef9f
JG
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
135struct notification_client {
6c24d3fd
JG
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 */
f2b3ef9f
JG
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;
f2bda80e 153 bool is_sessiond;
f2b3ef9f
JG
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;
9016dbfc 176 int current_poll_events;
f2b3ef9f
JG
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 */
882093ee 183 struct lttng_payload payload;
f2b3ef9f
JG
184 /* Bytes left to receive for the current message. */
185 size_t bytes_to_receive;
882093ee
JR
186 /* FDs left to receive for the current message. */
187 int fds_to_receive;
f2b3ef9f
JG
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;
882093ee 221 struct lttng_payload payload;
f2b3ef9f
JG
222 } outbound;
223 } communication;
224 /* call_rcu delayed reclaim. */
225 struct rcu_head rcu_node;
226};
227
dd877ea6
JG
228enum 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};
f2b3ef9f 236
f2b3ef9f
JG
237bool notification_client_list_get(struct notification_client_list *list);
238
f2b3ef9f
JG
239void notification_client_list_put(struct notification_client_list *list);
240
6c24d3fd 241/* Only returns a non-zero value if a fatal error occurred. */
e665dfbc
JG
242using report_client_transmission_result_cb = int (*)(struct notification_client *,
243 enum client_transmission_status,
244 void *);
f2b3ef9f 245
f2b3ef9f
JG
246int notification_client_list_send_evaluation(
247 struct notification_client_list *list,
52d55cf9 248 const struct lttng_trigger *trigger,
f2b3ef9f 249 const struct lttng_evaluation *evaluation,
f2b3ef9f
JG
250 const struct lttng_credentials *source_object_creds,
251 report_client_transmission_result_cb client_report,
252 void *user_data);
253
f2b3ef9f
JG
254int notification_thread_client_communication_update(
255 struct notification_thread_handle *handle,
256 notification_client_id id,
257 enum client_transmission_status transmission_status);
258
82b3cbf4
JR
259/*
260 * Takes ownership of the payload if present.
261 */
b9a8d78f
FD
262struct lttng_event_notifier_notification *lttng_event_notifier_notification_create(
263 uint64_t tracer_token,
82b3cbf4
JR
264 enum lttng_domain_type domain,
265 char *payload,
266 size_t payload_size);
b9a8d78f 267
b9a8d78f
FD
268void lttng_event_notifier_notification_destroy(
269 struct lttng_event_notifier_notification *event_notifier_notification);
270
8abe313a 271#endif /* NOTIFICATION_THREAD_INTERNAL_H */
This page took 0.064736 seconds and 4 git commands to generate.