Commit | Line | Data |
---|---|---|
ab0ee2ca | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
ab0ee2ca | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
ab0ee2ca | 5 | * |
ab0ee2ca JG |
6 | */ |
7 | ||
f2b3ef9f | 8 | #include "lttng/action/action.h" |
c9e313bc | 9 | #include "lttng/trigger/trigger-internal.hpp" |
ab0ee2ca | 10 | #define _LGPL_SOURCE |
28ab034a JG |
11 | #include "condition-internal.hpp" |
12 | #include "event-notifier-error-accounting.hpp" | |
13 | #include "kernel.hpp" | |
14 | #include "lttng-sessiond.hpp" | |
15 | #include "notification-thread-commands.hpp" | |
16 | #include "notification-thread-events.hpp" | |
17 | #include "notification-thread.hpp" | |
ab0ee2ca | 18 | |
c9e313bc | 19 | #include <common/defaults.hpp> |
28ab034a | 20 | #include <common/dynamic-buffer.hpp> |
c9e313bc SM |
21 | #include <common/error.hpp> |
22 | #include <common/futex.hpp> | |
c9e313bc | 23 | #include <common/hashtable/utils.hpp> |
c9e313bc | 24 | #include <common/macros.hpp> |
d4333eb9 | 25 | #include <common/pthread-lock.hpp> |
28ab034a JG |
26 | #include <common/sessiond-comm/sessiond-comm.hpp> |
27 | #include <common/unix.hpp> | |
56047f5a | 28 | #include <common/urcu.hpp> |
28ab034a | 29 | |
c9e313bc SM |
30 | #include <lttng/action/action-internal.hpp> |
31 | #include <lttng/action/list-internal.hpp> | |
c9e313bc | 32 | #include <lttng/condition/buffer-usage-internal.hpp> |
28ab034a JG |
33 | #include <lttng/condition/condition-internal.hpp> |
34 | #include <lttng/condition/condition.h> | |
35 | #include <lttng/condition/event-rule-matches-internal.hpp> | |
c9e313bc SM |
36 | #include <lttng/condition/session-consumed-size-internal.hpp> |
37 | #include <lttng/condition/session-rotation-internal.hpp> | |
c9e313bc | 38 | #include <lttng/domain-internal.hpp> |
c9e313bc | 39 | #include <lttng/event-rule/event-rule-internal.hpp> |
319dcddc | 40 | #include <lttng/location-internal.hpp> |
28ab034a JG |
41 | #include <lttng/notification/channel-internal.hpp> |
42 | #include <lttng/notification/notification-internal.hpp> | |
43 | #include <lttng/trigger/trigger-internal.hpp> | |
1da26331 | 44 | |
28ab034a JG |
45 | #include <fcntl.h> |
46 | #include <inttypes.h> | |
ab0ee2ca JG |
47 | #include <time.h> |
48 | #include <unistd.h> | |
28ab034a JG |
49 | #include <urcu.h> |
50 | #include <urcu/rculfhash.h> | |
1da26331 | 51 | |
28ab034a | 52 | #define CLIENT_POLL_EVENTS_IN (LPOLLIN | LPOLLRDHUP) |
9016dbfc | 53 | #define CLIENT_POLL_EVENTS_IN_OUT (CLIENT_POLL_EVENTS_IN | LPOLLOUT) |
ab0ee2ca | 54 | |
82b3cbf4 JR |
55 | /* The tracers currently limit the capture size to PIPE_BUF (4kb on linux). */ |
56 | #define MAX_CAPTURE_SIZE (PIPE_BUF) | |
57 | ||
51eab943 JG |
58 | enum lttng_object_type { |
59 | LTTNG_OBJECT_TYPE_UNKNOWN, | |
60 | LTTNG_OBJECT_TYPE_NONE, | |
61 | LTTNG_OBJECT_TYPE_CHANNEL, | |
62 | LTTNG_OBJECT_TYPE_SESSION, | |
63 | }; | |
64 | ||
ab0ee2ca JG |
65 | struct lttng_channel_trigger_list { |
66 | struct channel_key channel_key; | |
ea9a44f0 | 67 | /* List of struct lttng_trigger_list_element. */ |
ab0ee2ca | 68 | struct cds_list_head list; |
ea9a44f0 | 69 | /* Node in the channel_triggers_ht */ |
ab0ee2ca | 70 | struct cds_lfht_node channel_triggers_ht_node; |
83b934ad MD |
71 | /* call_rcu delayed reclaim. */ |
72 | struct rcu_head rcu_node; | |
ab0ee2ca JG |
73 | }; |
74 | ||
ea9a44f0 JG |
75 | /* |
76 | * List of triggers applying to a given session. | |
77 | * | |
78 | * See: | |
79 | * - lttng_session_trigger_list_create() | |
80 | * - lttng_session_trigger_list_build() | |
81 | * - lttng_session_trigger_list_destroy() | |
82 | * - lttng_session_trigger_list_add() | |
83 | */ | |
84 | struct lttng_session_trigger_list { | |
319dcddc | 85 | char *session_name; |
ea9a44f0 JG |
86 | /* List of struct lttng_trigger_list_element. */ |
87 | struct cds_list_head list; | |
88 | /* Node in the session_triggers_ht */ | |
89 | struct cds_lfht_node session_triggers_ht_node; | |
90 | /* | |
91 | * Weak reference to the notification system's session triggers | |
92 | * hashtable. | |
93 | * | |
94 | * The session trigger list structure structure is owned by | |
95 | * the session's session_info. | |
96 | * | |
97 | * The session_info is kept alive the the channel_infos holding a | |
98 | * reference to it (reference counting). When those channels are | |
99 | * destroyed (at runtime or on teardown), the reference they hold | |
100 | * to the session_info are released. On destruction of session_info, | |
101 | * session_info_destroy() will remove the list of triggers applying | |
102 | * to this session from the notification system's state. | |
103 | * | |
104 | * This implies that the session_triggers_ht must be destroyed | |
105 | * after the channels. | |
106 | */ | |
107 | struct cds_lfht *session_triggers_ht; | |
108 | /* Used for delayed RCU reclaim. */ | |
109 | struct rcu_head rcu_node; | |
110 | }; | |
111 | ||
f1494934 JG |
112 | namespace { |
113 | struct lttng_trigger_list_element { | |
114 | /* No ownership of the trigger object is assumed. */ | |
115 | struct lttng_trigger *trigger; | |
116 | struct cds_list_head node; | |
117 | }; | |
118 | ||
ab0ee2ca JG |
119 | struct lttng_trigger_ht_element { |
120 | struct lttng_trigger *trigger; | |
121 | struct cds_lfht_node node; | |
242388e4 | 122 | struct cds_lfht_node node_by_name_uid; |
091fa780 | 123 | struct cds_list_head client_list_trigger_node; |
83b934ad MD |
124 | /* call_rcu delayed reclaim. */ |
125 | struct rcu_head rcu_node; | |
ab0ee2ca JG |
126 | }; |
127 | ||
128 | struct lttng_condition_list_element { | |
129 | struct lttng_condition *condition; | |
130 | struct cds_list_head node; | |
131 | }; | |
132 | ||
ab0ee2ca JG |
133 | struct channel_state_sample { |
134 | struct channel_key key; | |
135 | struct cds_lfht_node channel_state_ht_node; | |
136 | uint64_t highest_usage; | |
137 | uint64_t lowest_usage; | |
83b934ad MD |
138 | /* call_rcu delayed reclaim. */ |
139 | struct rcu_head rcu_node; | |
ab0ee2ca | 140 | }; |
f1494934 | 141 | } /* namespace */ |
ab0ee2ca | 142 | |
e4db5ace | 143 | static unsigned long hash_channel_key(struct channel_key *key); |
ed327204 | 144 | static int evaluate_buffer_condition(const struct lttng_condition *condition, |
28ab034a JG |
145 | struct lttng_evaluation **evaluation, |
146 | const struct notification_thread_state *state, | |
147 | const struct channel_state_sample *previous_sample, | |
148 | const struct channel_state_sample *latest_sample, | |
149 | struct channel_info *channel_info); | |
150 | static int send_evaluation_to_clients(const struct lttng_trigger *trigger, | |
151 | const struct lttng_evaluation *evaluation, | |
152 | struct notification_client_list *client_list, | |
153 | struct notification_thread_state *state, | |
154 | uid_t channel_uid, | |
155 | gid_t channel_gid); | |
8abe313a | 156 | |
ea9a44f0 | 157 | /* session_info API */ |
28ab034a JG |
158 | static void session_info_destroy(void *_data); |
159 | static void session_info_get(struct session_info *session_info); | |
160 | static void session_info_put(struct session_info *session_info); | |
161 | static struct session_info *session_info_create(uint64_t id, | |
162 | const char *name, | |
163 | uid_t uid, | |
164 | gid_t gid, | |
165 | struct lttng_session_trigger_list *trigger_list, | |
166 | struct cds_lfht *sessions_ht); | |
167 | static void session_info_add_channel(struct session_info *session_info, | |
168 | struct channel_info *channel_info); | |
169 | static void session_info_remove_channel(struct session_info *session_info, | |
170 | struct channel_info *channel_info); | |
8abe313a | 171 | |
ea9a44f0 | 172 | /* lttng_session_trigger_list API */ |
28ab034a JG |
173 | static struct lttng_session_trigger_list * |
174 | lttng_session_trigger_list_create(const char *session_name, struct cds_lfht *session_triggers_ht); | |
175 | static struct lttng_session_trigger_list * | |
176 | lttng_session_trigger_list_build(const struct notification_thread_state *state, | |
177 | const char *session_name); | |
178 | static void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list *list); | |
179 | static int lttng_session_trigger_list_add(struct lttng_session_trigger_list *list, | |
180 | struct lttng_trigger *trigger); | |
181 | ||
182 | static int client_handle_transmission_status(struct notification_client *client, | |
183 | enum client_transmission_status transmission_status, | |
184 | struct notification_thread_state *state); | |
185 | ||
186 | static int handle_one_event_notifier_notification(struct notification_thread_state *state, | |
187 | int pipe, | |
188 | enum lttng_domain_type domain); | |
189 | ||
190 | static void free_lttng_trigger_ht_element_rcu(struct rcu_head *node); | |
191 | ||
192 | static int match_client_socket(struct cds_lfht_node *node, const void *key) | |
ab0ee2ca JG |
193 | { |
194 | /* This double-cast is intended to supress pointer-to-cast warning. */ | |
ac1889bf | 195 | const int socket = (int) (intptr_t) key; |
28ab034a JG |
196 | const struct notification_client *client = |
197 | caa_container_of(node, struct notification_client, client_socket_ht_node); | |
ab0ee2ca | 198 | |
ac1889bf JG |
199 | return client->socket == socket; |
200 | } | |
201 | ||
28ab034a | 202 | static int match_client_id(struct cds_lfht_node *node, const void *key) |
ac1889bf JG |
203 | { |
204 | /* This double-cast is intended to supress pointer-to-cast warning. */ | |
205 | const notification_client_id id = *((notification_client_id *) key); | |
28ab034a JG |
206 | const struct notification_client *client = |
207 | lttng::utils::container_of(node, ¬ification_client::client_id_ht_node); | |
ab0ee2ca | 208 | |
ac1889bf | 209 | return client->id == id; |
ab0ee2ca JG |
210 | } |
211 | ||
28ab034a | 212 | static int match_channel_trigger_list(struct cds_lfht_node *node, const void *key) |
ab0ee2ca JG |
213 | { |
214 | struct channel_key *channel_key = (struct channel_key *) key; | |
215 | struct lttng_channel_trigger_list *trigger_list; | |
216 | ||
28ab034a JG |
217 | trigger_list = |
218 | caa_container_of(node, struct lttng_channel_trigger_list, channel_triggers_ht_node); | |
ab0ee2ca | 219 | |
07c4863f JG |
220 | return !((channel_key->key != trigger_list->channel_key.key) || |
221 | (channel_key->domain != trigger_list->channel_key.domain)); | |
ab0ee2ca JG |
222 | } |
223 | ||
28ab034a | 224 | static int match_session_trigger_list(struct cds_lfht_node *node, const void *key) |
51eab943 JG |
225 | { |
226 | const char *session_name = (const char *) key; | |
227 | struct lttng_session_trigger_list *trigger_list; | |
228 | ||
28ab034a JG |
229 | trigger_list = |
230 | caa_container_of(node, struct lttng_session_trigger_list, session_triggers_ht_node); | |
51eab943 JG |
231 | |
232 | return !!(strcmp(trigger_list->session_name, session_name) == 0); | |
233 | } | |
234 | ||
28ab034a | 235 | static int match_channel_state_sample(struct cds_lfht_node *node, const void *key) |
ab0ee2ca JG |
236 | { |
237 | struct channel_key *channel_key = (struct channel_key *) key; | |
238 | struct channel_state_sample *sample; | |
239 | ||
28ab034a | 240 | sample = caa_container_of(node, struct channel_state_sample, channel_state_ht_node); |
ab0ee2ca | 241 | |
07c4863f JG |
242 | return !((channel_key->key != sample->key.key) || |
243 | (channel_key->domain != sample->key.domain)); | |
ab0ee2ca JG |
244 | } |
245 | ||
28ab034a | 246 | static int match_channel_info(struct cds_lfht_node *node, const void *key) |
ab0ee2ca JG |
247 | { |
248 | struct channel_key *channel_key = (struct channel_key *) key; | |
249 | struct channel_info *channel_info; | |
250 | ||
28ab034a | 251 | channel_info = caa_container_of(node, struct channel_info, channels_ht_node); |
ab0ee2ca | 252 | |
07c4863f JG |
253 | return !((channel_key->key != channel_info->key.key) || |
254 | (channel_key->domain != channel_info->key.domain)); | |
ab0ee2ca JG |
255 | } |
256 | ||
28ab034a | 257 | static int match_trigger(struct cds_lfht_node *node, const void *key) |
ab0ee2ca | 258 | { |
242388e4 JR |
259 | struct lttng_trigger *trigger_key = (struct lttng_trigger *) key; |
260 | struct lttng_trigger_ht_element *trigger_ht_element; | |
ab0ee2ca | 261 | |
28ab034a | 262 | trigger_ht_element = caa_container_of(node, struct lttng_trigger_ht_element, node); |
ab0ee2ca | 263 | |
242388e4 | 264 | return !!lttng_trigger_is_equal(trigger_key, trigger_ht_element->trigger); |
ab0ee2ca JG |
265 | } |
266 | ||
28ab034a | 267 | static int match_trigger_token(struct cds_lfht_node *node, const void *key) |
e7c93cf9 | 268 | { |
7966af57 | 269 | const uint64_t *_key = (uint64_t *) key; |
e7c93cf9 JR |
270 | struct notification_trigger_tokens_ht_element *element; |
271 | ||
28ab034a | 272 | element = caa_container_of(node, struct notification_trigger_tokens_ht_element, node); |
e7c93cf9 JR |
273 | return *_key == element->token; |
274 | } | |
275 | ||
28ab034a | 276 | static int match_client_list_condition(struct cds_lfht_node *node, const void *key) |
ab0ee2ca JG |
277 | { |
278 | struct lttng_condition *condition_key = (struct lttng_condition *) key; | |
279 | struct notification_client_list *client_list; | |
f82f93a1 | 280 | const struct lttng_condition *condition; |
ab0ee2ca | 281 | |
a0377dfe | 282 | LTTNG_ASSERT(condition_key); |
ab0ee2ca | 283 | |
28ab034a JG |
284 | client_list = caa_container_of( |
285 | node, struct notification_client_list, notification_trigger_clients_ht_node); | |
091fa780 | 286 | condition = client_list->condition; |
ab0ee2ca JG |
287 | |
288 | return !!lttng_condition_is_equal(condition_key, condition); | |
289 | } | |
290 | ||
28ab034a | 291 | static int match_session_info(struct cds_lfht_node *node, const void *key) |
f82f93a1 | 292 | { |
139a8d25 | 293 | const auto session_id = *((uint64_t *) key); |
28ab034a JG |
294 | const auto *session_info = |
295 | lttng::utils::container_of(node, &session_info::sessions_ht_node); | |
f82f93a1 | 296 | |
139a8d25 JG |
297 | return session_id == session_info->id; |
298 | } | |
299 | ||
28ab034a | 300 | static unsigned long hash_session_info_id(uint64_t id) |
139a8d25 JG |
301 | { |
302 | return hash_key_u64(&id, lttng_ht_seed); | |
303 | } | |
304 | ||
28ab034a | 305 | static unsigned long hash_session_info(const struct session_info *session_info) |
139a8d25 JG |
306 | { |
307 | return hash_session_info_id(session_info->id); | |
308 | } | |
309 | ||
28ab034a JG |
310 | static struct session_info *get_session_info_by_id(const struct notification_thread_state *state, |
311 | uint64_t id) | |
139a8d25 JG |
312 | { |
313 | struct cds_lfht_iter iter; | |
314 | struct cds_lfht_node *node; | |
07c4863f | 315 | const lttng::urcu::read_lock_guard read_lock_guard; |
139a8d25 | 316 | |
28ab034a JG |
317 | cds_lfht_lookup( |
318 | state->sessions_ht, hash_session_info_id(id), match_session_info, &id, &iter); | |
139a8d25 JG |
319 | node = cds_lfht_iter_get_node(&iter); |
320 | ||
321 | if (node) { | |
28ab034a JG |
322 | auto session_info = |
323 | lttng::utils::container_of(node, &session_info::sessions_ht_node); | |
139a8d25 JG |
324 | |
325 | session_info_get(session_info); | |
326 | return session_info; | |
327 | } | |
328 | ||
cd9adb8b | 329 | return nullptr; |
139a8d25 JG |
330 | } |
331 | ||
28ab034a JG |
332 | static struct session_info *get_session_info_by_name(const struct notification_thread_state *state, |
333 | const char *name) | |
139a8d25 JG |
334 | { |
335 | uint64_t session_id; | |
336 | const auto found = sample_session_id_by_name(name, &session_id); | |
337 | ||
cd9adb8b | 338 | return found ? get_session_info_by_id(state, session_id) : nullptr; |
f82f93a1 JG |
339 | } |
340 | ||
28ab034a | 341 | static const char *notification_command_type_str(enum notification_thread_command_type type) |
6900ae81 FD |
342 | { |
343 | switch (type) { | |
344 | case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER: | |
345 | return "REGISTER_TRIGGER"; | |
346 | case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER: | |
347 | return "UNREGISTER_TRIGGER"; | |
348 | case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL: | |
349 | return "ADD_CHANNEL"; | |
350 | case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL: | |
351 | return "REMOVE_CHANNEL"; | |
139a8d25 JG |
352 | case NOTIFICATION_COMMAND_TYPE_ADD_SESSION: |
353 | return "ADD_SESSION"; | |
354 | case NOTIFICATION_COMMAND_TYPE_REMOVE_SESSION: | |
355 | return "REMOVE_SESSION"; | |
6900ae81 FD |
356 | case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING: |
357 | return "SESSION_ROTATION_ONGOING"; | |
358 | case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED: | |
359 | return "SESSION_ROTATION_COMPLETED"; | |
360 | case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE: | |
361 | return "ADD_TRACER_EVENT_SOURCE"; | |
362 | case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE: | |
363 | return "REMOVE_TRACER_EVENT_SOURCE"; | |
364 | case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS: | |
365 | return "LIST_TRIGGERS"; | |
8790759c FD |
366 | case NOTIFICATION_COMMAND_TYPE_GET_TRIGGER: |
367 | return "GET_TRIGGER"; | |
6900ae81 FD |
368 | case NOTIFICATION_COMMAND_TYPE_QUIT: |
369 | return "QUIT"; | |
370 | case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE: | |
371 | return "CLIENT_COMMUNICATION_UPDATE"; | |
372 | default: | |
373 | abort(); | |
374 | } | |
375 | } | |
376 | ||
242388e4 JR |
377 | /* |
378 | * Match trigger based on name and credentials only. | |
379 | * Name duplication is NOT allowed for the same uid. | |
380 | */ | |
28ab034a | 381 | static int match_trigger_by_name_uid(struct cds_lfht_node *node, const void *key) |
242388e4 JR |
382 | { |
383 | bool match = false; | |
0efb2ad7 | 384 | const char *element_trigger_name; |
242388e4 JR |
385 | const char *key_name; |
386 | enum lttng_trigger_status status; | |
387 | const struct lttng_credentials *key_creds; | |
388 | const struct lttng_credentials *node_creds; | |
28ab034a | 389 | const struct lttng_trigger *trigger_key = (const struct lttng_trigger *) key; |
242388e4 | 390 | const struct lttng_trigger_ht_element *trigger_ht_element = |
28ab034a | 391 | caa_container_of(node, struct lttng_trigger_ht_element, node_by_name_uid); |
242388e4 | 392 | |
28ab034a | 393 | status = lttng_trigger_get_name(trigger_ht_element->trigger, &element_trigger_name); |
cd9adb8b | 394 | element_trigger_name = status == LTTNG_TRIGGER_STATUS_OK ? element_trigger_name : nullptr; |
242388e4 JR |
395 | |
396 | status = lttng_trigger_get_name(trigger_key, &key_name); | |
cd9adb8b | 397 | key_name = status == LTTNG_TRIGGER_STATUS_OK ? key_name : nullptr; |
242388e4 | 398 | |
0efb2ad7 JG |
399 | /* |
400 | * Compare the names. | |
401 | * Consider null names as not equal. This is to maintain backwards | |
402 | * compatibility with pre-2.13 anonymous triggers. Multiples anonymous | |
403 | * triggers are allowed for a given user. | |
404 | */ | |
405 | if (!element_trigger_name || !key_name) { | |
406 | goto end; | |
407 | } | |
408 | ||
409 | if (strcmp(element_trigger_name, key_name) != 0) { | |
242388e4 JR |
410 | goto end; |
411 | } | |
412 | ||
413 | /* Compare the owners' UIDs. */ | |
414 | key_creds = lttng_trigger_get_credentials(trigger_key); | |
415 | node_creds = lttng_trigger_get_credentials(trigger_ht_element->trigger); | |
416 | ||
417 | match = lttng_credentials_is_equal_uid(key_creds, node_creds); | |
418 | ||
419 | end: | |
420 | return match; | |
421 | } | |
422 | ||
423 | /* | |
424 | * Hash trigger based on name and credentials only. | |
425 | */ | |
28ab034a | 426 | static unsigned long hash_trigger_by_name_uid(const struct lttng_trigger *trigger) |
242388e4 JR |
427 | { |
428 | unsigned long hash = 0; | |
429 | const struct lttng_credentials *trigger_creds; | |
430 | const char *trigger_name; | |
431 | enum lttng_trigger_status status; | |
432 | ||
433 | status = lttng_trigger_get_name(trigger, &trigger_name); | |
434 | if (status == LTTNG_TRIGGER_STATUS_OK) { | |
435 | hash = hash_key_str(trigger_name, lttng_ht_seed); | |
436 | } | |
437 | ||
438 | trigger_creds = lttng_trigger_get_credentials(trigger); | |
439 | hash ^= hash_key_ulong((void *) (unsigned long) LTTNG_OPTIONAL_GET(trigger_creds->uid), | |
28ab034a | 440 | lttng_ht_seed); |
242388e4 JR |
441 | |
442 | return hash; | |
443 | } | |
444 | ||
28ab034a | 445 | static unsigned long hash_channel_key(struct channel_key *key) |
e4db5ace | 446 | { |
07c4863f JG |
447 | const unsigned long key_hash = hash_key_u64(&key->key, lttng_ht_seed); |
448 | const unsigned long domain_hash = | |
28ab034a | 449 | hash_key_ulong((void *) (unsigned long) key->domain, lttng_ht_seed); |
e4db5ace JR |
450 | |
451 | return key_hash ^ domain_hash; | |
452 | } | |
453 | ||
28ab034a | 454 | static unsigned long hash_client_socket(int socket) |
ac1889bf JG |
455 | { |
456 | return hash_key_ulong((void *) (unsigned long) socket, lttng_ht_seed); | |
457 | } | |
458 | ||
28ab034a | 459 | static unsigned long hash_client_id(notification_client_id id) |
ac1889bf JG |
460 | { |
461 | return hash_key_u64(&id, lttng_ht_seed); | |
462 | } | |
463 | ||
f82f93a1 JG |
464 | /* |
465 | * Get the type of object to which a given condition applies. Bindings let | |
466 | * the notification system evaluate a trigger's condition when a given | |
467 | * object's state is updated. | |
468 | * | |
469 | * For instance, a condition bound to a channel will be evaluated everytime | |
470 | * the channel's state is changed by a channel monitoring sample. | |
471 | */ | |
28ab034a | 472 | static enum lttng_object_type get_condition_binding_object(const struct lttng_condition *condition) |
f82f93a1 JG |
473 | { |
474 | switch (lttng_condition_get_type(condition)) { | |
475 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
476 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
e742b055 | 477 | return LTTNG_OBJECT_TYPE_CHANNEL; |
f82f93a1 JG |
478 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: |
479 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
319dcddc | 480 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: |
f82f93a1 | 481 | return LTTNG_OBJECT_TYPE_SESSION; |
8dbb86b8 | 482 | case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES: |
959e3c66 | 483 | return LTTNG_OBJECT_TYPE_NONE; |
f82f93a1 JG |
484 | default: |
485 | return LTTNG_OBJECT_TYPE_UNKNOWN; | |
486 | } | |
487 | } | |
488 | ||
28ab034a | 489 | static void free_channel_info_rcu(struct rcu_head *node) |
83b934ad | 490 | { |
0114db0e | 491 | free(lttng::utils::container_of(node, &channel_info::rcu_node)); |
83b934ad MD |
492 | } |
493 | ||
28ab034a | 494 | static void channel_info_destroy(struct channel_info *channel_info) |
ab0ee2ca JG |
495 | { |
496 | if (!channel_info) { | |
497 | return; | |
498 | } | |
499 | ||
8abe313a | 500 | if (channel_info->session_info) { |
28ab034a | 501 | session_info_remove_channel(channel_info->session_info, channel_info); |
8abe313a | 502 | session_info_put(channel_info->session_info); |
ab0ee2ca | 503 | } |
8abe313a JG |
504 | if (channel_info->name) { |
505 | free(channel_info->name); | |
ab0ee2ca | 506 | } |
83b934ad MD |
507 | call_rcu(&channel_info->rcu_node, free_channel_info_rcu); |
508 | } | |
509 | ||
28ab034a | 510 | static void free_session_info_rcu(struct rcu_head *node) |
83b934ad | 511 | { |
0114db0e | 512 | free(lttng::utils::container_of(node, &session_info::rcu_node)); |
ab0ee2ca JG |
513 | } |
514 | ||
8abe313a | 515 | /* Don't call directly, use the ref-counting mechanism. */ |
28ab034a | 516 | static void session_info_destroy(void *_data) |
ab0ee2ca | 517 | { |
7966af57 | 518 | struct session_info *session_info = (struct session_info *) _data; |
3b68d0a3 | 519 | int ret; |
ab0ee2ca | 520 | |
a0377dfe | 521 | LTTNG_ASSERT(session_info); |
8abe313a | 522 | if (session_info->channel_infos_ht) { |
cd9adb8b | 523 | ret = cds_lfht_destroy(session_info->channel_infos_ht, nullptr); |
3b68d0a3 | 524 | if (ret) { |
bd0514a5 | 525 | ERR("Failed to destroy channel information hash table"); |
3b68d0a3 | 526 | } |
8abe313a | 527 | } |
ea9a44f0 | 528 | lttng_session_trigger_list_destroy(session_info->trigger_list); |
1eee26c5 | 529 | |
07c4863f | 530 | const lttng::urcu::read_lock_guard read_lock; |
28ab034a | 531 | cds_lfht_del(session_info->sessions_ht, &session_info->sessions_ht_node); |
8abe313a | 532 | free(session_info->name); |
319dcddc | 533 | lttng_trace_archive_location_put(session_info->last_state_sample.rotation.location); |
83b934ad | 534 | call_rcu(&session_info->rcu_node, free_session_info_rcu); |
8abe313a | 535 | } |
ab0ee2ca | 536 | |
28ab034a | 537 | static void session_info_get(struct session_info *session_info) |
8abe313a JG |
538 | { |
539 | if (!session_info) { | |
540 | return; | |
541 | } | |
542 | lttng_ref_get(&session_info->ref); | |
543 | } | |
544 | ||
28ab034a | 545 | static void session_info_put(struct session_info *session_info) |
8abe313a JG |
546 | { |
547 | if (!session_info) { | |
548 | return; | |
ab0ee2ca | 549 | } |
8abe313a JG |
550 | lttng_ref_put(&session_info->ref); |
551 | } | |
552 | ||
28ab034a JG |
553 | static struct session_info *session_info_create(uint64_t id, |
554 | const char *name, | |
555 | uid_t uid, | |
556 | gid_t gid, | |
557 | struct lttng_session_trigger_list *trigger_list, | |
558 | struct cds_lfht *sessions_ht) | |
8abe313a JG |
559 | { |
560 | struct session_info *session_info; | |
ab0ee2ca | 561 | |
a0377dfe | 562 | LTTNG_ASSERT(name); |
ab0ee2ca | 563 | |
64803277 | 564 | session_info = zmalloc<struct session_info>(); |
8abe313a JG |
565 | if (!session_info) { |
566 | goto end; | |
567 | } | |
139a8d25 | 568 | |
8abe313a JG |
569 | lttng_ref_init(&session_info->ref, session_info_destroy); |
570 | ||
28ab034a | 571 | session_info->channel_infos_ht = cds_lfht_new( |
cd9adb8b | 572 | DEFAULT_HT_SIZE, 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, nullptr); |
8abe313a | 573 | if (!session_info->channel_infos_ht) { |
ab0ee2ca JG |
574 | goto error; |
575 | } | |
8abe313a JG |
576 | |
577 | cds_lfht_node_init(&session_info->sessions_ht_node); | |
139a8d25 | 578 | session_info->id = id; |
8abe313a JG |
579 | session_info->name = strdup(name); |
580 | if (!session_info->name) { | |
ab0ee2ca JG |
581 | goto error; |
582 | } | |
139a8d25 | 583 | |
8abe313a JG |
584 | session_info->uid = uid; |
585 | session_info->gid = gid; | |
ea9a44f0 | 586 | session_info->trigger_list = trigger_list; |
1eee26c5 | 587 | session_info->sessions_ht = sessions_ht; |
8abe313a JG |
588 | end: |
589 | return session_info; | |
590 | error: | |
591 | session_info_put(session_info); | |
cd9adb8b | 592 | return nullptr; |
8abe313a JG |
593 | } |
594 | ||
28ab034a JG |
595 | static void session_info_add_channel(struct session_info *session_info, |
596 | struct channel_info *channel_info) | |
8abe313a | 597 | { |
07c4863f | 598 | const lttng::urcu::read_lock_guard read_lock; |
8abe313a | 599 | cds_lfht_add(session_info->channel_infos_ht, |
28ab034a JG |
600 | hash_channel_key(&channel_info->key), |
601 | &channel_info->session_info_channels_ht_node); | |
8abe313a JG |
602 | } |
603 | ||
28ab034a JG |
604 | static void session_info_remove_channel(struct session_info *session_info, |
605 | struct channel_info *channel_info) | |
8abe313a | 606 | { |
07c4863f | 607 | const lttng::urcu::read_lock_guard read_lock; |
28ab034a | 608 | cds_lfht_del(session_info->channel_infos_ht, &channel_info->session_info_channels_ht_node); |
8abe313a JG |
609 | } |
610 | ||
28ab034a JG |
611 | static struct channel_info *channel_info_create(const char *channel_name, |
612 | struct channel_key *channel_key, | |
613 | uint64_t channel_capacity, | |
614 | struct session_info *session_info) | |
8abe313a | 615 | { |
64803277 | 616 | struct channel_info *channel_info = zmalloc<struct channel_info>(); |
8abe313a JG |
617 | |
618 | if (!channel_info) { | |
619 | goto end; | |
620 | } | |
621 | ||
ab0ee2ca | 622 | cds_lfht_node_init(&channel_info->channels_ht_node); |
8abe313a JG |
623 | cds_lfht_node_init(&channel_info->session_info_channels_ht_node); |
624 | memcpy(&channel_info->key, channel_key, sizeof(*channel_key)); | |
625 | channel_info->capacity = channel_capacity; | |
626 | ||
627 | channel_info->name = strdup(channel_name); | |
628 | if (!channel_info->name) { | |
629 | goto error; | |
630 | } | |
631 | ||
632 | /* | |
633 | * Set the references between session and channel infos: | |
634 | * - channel_info holds a strong reference to session_info | |
635 | * - session_info holds a weak reference to channel_info | |
636 | */ | |
637 | session_info_get(session_info); | |
638 | session_info_add_channel(session_info, channel_info); | |
639 | channel_info->session_info = session_info; | |
ab0ee2ca | 640 | end: |
8abe313a | 641 | return channel_info; |
ab0ee2ca | 642 | error: |
8abe313a | 643 | channel_info_destroy(channel_info); |
cd9adb8b | 644 | return nullptr; |
ab0ee2ca JG |
645 | } |
646 | ||
505b2d90 JG |
647 | bool notification_client_list_get(struct notification_client_list *list) |
648 | { | |
649 | return urcu_ref_get_unless_zero(&list->ref); | |
650 | } | |
651 | ||
28ab034a | 652 | static void free_notification_client_list_rcu(struct rcu_head *node) |
505b2d90 | 653 | { |
28ab034a | 654 | free(caa_container_of(node, struct notification_client_list, rcu_node)); |
505b2d90 JG |
655 | } |
656 | ||
28ab034a | 657 | static void notification_client_list_release(struct urcu_ref *list_ref) |
505b2d90 JG |
658 | { |
659 | struct notification_client_list *list = | |
28ab034a | 660 | lttng::utils::container_of(list_ref, ¬ification_client_list::ref); |
505b2d90 JG |
661 | struct notification_client_list_element *client_list_element, *tmp; |
662 | ||
091fa780 FD |
663 | lttng_condition_put(list->condition); |
664 | ||
505b2d90 | 665 | if (list->notification_trigger_clients_ht) { |
07c4863f | 666 | const lttng::urcu::read_lock_guard read_lock; |
091fa780 | 667 | |
505b2d90 | 668 | cds_lfht_del(list->notification_trigger_clients_ht, |
28ab034a | 669 | &list->notification_trigger_clients_ht_node); |
cd9adb8b | 670 | list->notification_trigger_clients_ht = nullptr; |
505b2d90 | 671 | } |
28ab034a | 672 | cds_list_for_each_entry_safe (client_list_element, tmp, &list->clients_list, node) { |
505b2d90 JG |
673 | free(client_list_element); |
674 | } | |
091fa780 | 675 | |
a0377dfe | 676 | LTTNG_ASSERT(cds_list_empty(&list->triggers_list)); |
091fa780 | 677 | |
505b2d90 JG |
678 | pthread_mutex_destroy(&list->lock); |
679 | call_rcu(&list->rcu_node, free_notification_client_list_rcu); | |
680 | } | |
681 | ||
28ab034a JG |
682 | static bool condition_applies_to_client(const struct lttng_condition *condition, |
683 | struct notification_client *client) | |
091fa780 FD |
684 | { |
685 | bool applies = false; | |
686 | struct lttng_condition_list_element *condition_list_element; | |
687 | ||
28ab034a JG |
688 | cds_list_for_each_entry (condition_list_element, &client->condition_list, node) { |
689 | applies = lttng_condition_is_equal(condition_list_element->condition, condition); | |
091fa780 FD |
690 | if (applies) { |
691 | break; | |
692 | } | |
693 | } | |
694 | ||
695 | return applies; | |
696 | } | |
697 | ||
28ab034a JG |
698 | static struct notification_client_list * |
699 | notification_client_list_create(struct notification_thread_state *state, | |
700 | const struct lttng_condition *condition) | |
505b2d90 | 701 | { |
091fa780 | 702 | struct notification_client_list *client_list; |
505b2d90 | 703 | |
64803277 | 704 | client_list = zmalloc<notification_client_list>(); |
505b2d90 | 705 | if (!client_list) { |
091fa780 FD |
706 | PERROR("Failed to allocate notification client list"); |
707 | goto end; | |
505b2d90 | 708 | } |
091fa780 | 709 | |
cd9adb8b | 710 | pthread_mutex_init(&client_list->lock, nullptr); |
091fa780 FD |
711 | /* |
712 | * The trigger that owns the condition has the first reference to this | |
713 | * client list. | |
714 | */ | |
505b2d90 JG |
715 | urcu_ref_init(&client_list->ref); |
716 | cds_lfht_node_init(&client_list->notification_trigger_clients_ht_node); | |
091fa780 FD |
717 | CDS_INIT_LIST_HEAD(&client_list->clients_list); |
718 | CDS_INIT_LIST_HEAD(&client_list->triggers_list); | |
505b2d90 | 719 | |
091fa780 FD |
720 | /* |
721 | * Create a copy of the condition so that it's independent of any | |
722 | * trigger. The client list may outlive the trigger object (which owns | |
723 | * the condition) that is used to create it. | |
724 | */ | |
725 | client_list->condition = lttng_condition_copy(condition); | |
726 | ||
6e7ffe66 JG |
727 | for (auto *client : lttng::urcu::lfht_iteration_adapter< |
728 | notification_client, | |
729 | decltype(notification_client::client_socket_ht_node), | |
730 | ¬ification_client::client_socket_ht_node>(*state->client_socket_ht)) { | |
731 | struct notification_client_list_element *client_list_element; | |
091fa780 | 732 | |
6e7ffe66 JG |
733 | if (!condition_applies_to_client(condition, client)) { |
734 | continue; | |
735 | } | |
505b2d90 | 736 | |
6e7ffe66 JG |
737 | client_list_element = zmalloc<notification_client_list_element>(); |
738 | if (!client_list_element) { | |
739 | goto error_put_client_list; | |
56047f5a | 740 | } |
6e7ffe66 JG |
741 | |
742 | CDS_INIT_LIST_HEAD(&client_list_element->node); | |
743 | client_list_element->client = client; | |
744 | cds_list_add(&client_list_element->node, &client_list->clients_list); | |
091fa780 FD |
745 | } |
746 | ||
28ab034a | 747 | client_list->notification_trigger_clients_ht = state->notification_trigger_clients_ht; |
505b2d90 | 748 | |
091fa780 FD |
749 | /* |
750 | * Add the client list to the global list of client list. | |
751 | */ | |
56047f5a | 752 | { |
07c4863f | 753 | const lttng::urcu::read_lock_guard read_lock; |
56047f5a JG |
754 | |
755 | cds_lfht_add_unique(state->notification_trigger_clients_ht, | |
756 | lttng_condition_hash(client_list->condition), | |
757 | match_client_list_condition, | |
758 | client_list->condition, | |
759 | &client_list->notification_trigger_clients_ht_node); | |
760 | } | |
091fa780 FD |
761 | goto end; |
762 | ||
763 | error_put_client_list: | |
764 | notification_client_list_put(client_list); | |
cd9adb8b | 765 | client_list = nullptr; |
091fa780 FD |
766 | |
767 | end: | |
768 | return client_list; | |
505b2d90 JG |
769 | } |
770 | ||
505b2d90 JG |
771 | void notification_client_list_put(struct notification_client_list *list) |
772 | { | |
773 | if (!list) { | |
774 | return; | |
775 | } | |
776 | return urcu_ref_put(&list->ref, notification_client_list_release); | |
777 | } | |
778 | ||
779 | /* Provides a reference to the returned list. */ | |
28ab034a JG |
780 | static struct notification_client_list * |
781 | get_client_list_from_condition(struct notification_thread_state *state, | |
782 | const struct lttng_condition *condition) | |
ed327204 JG |
783 | { |
784 | struct cds_lfht_node *node; | |
785 | struct cds_lfht_iter iter; | |
cd9adb8b | 786 | struct notification_client_list *list = nullptr; |
ed327204 | 787 | |
07c4863f | 788 | const lttng::urcu::read_lock_guard read_lock; |
ed327204 JG |
789 | cds_lfht_lookup(state->notification_trigger_clients_ht, |
790 | lttng_condition_hash(condition), | |
791 | match_client_list_condition, | |
792 | condition, | |
793 | &iter); | |
794 | node = cds_lfht_iter_get_node(&iter); | |
505b2d90 | 795 | if (node) { |
28ab034a JG |
796 | list = lttng::utils::container_of( |
797 | node, ¬ification_client_list::notification_trigger_clients_ht_node); | |
cd9adb8b | 798 | list = notification_client_list_get(list) ? list : nullptr; |
505b2d90 | 799 | } |
ed327204 | 800 | |
505b2d90 | 801 | return list; |
ed327204 JG |
802 | } |
803 | ||
28ab034a JG |
804 | static int evaluate_channel_condition_for_client(const struct lttng_condition *condition, |
805 | struct notification_thread_state *state, | |
806 | struct lttng_evaluation **evaluation, | |
807 | uid_t *session_uid, | |
808 | gid_t *session_gid) | |
e4db5ace JR |
809 | { |
810 | int ret; | |
811 | struct cds_lfht_iter iter; | |
812 | struct cds_lfht_node *node; | |
cd9adb8b JG |
813 | struct channel_info *channel_info = nullptr; |
814 | struct channel_key *channel_key = nullptr; | |
815 | struct channel_state_sample *last_sample = nullptr; | |
e4db5ace | 816 | |
07c4863f | 817 | const lttng::urcu::read_lock_guard read_lock; |
505b2d90 | 818 | |
f82f93a1 | 819 | /* Find the channel associated with the condition. */ |
6e7ffe66 JG |
820 | for (auto *channel_trigger_list : lttng::urcu::lfht_iteration_adapter< |
821 | lttng_channel_trigger_list, | |
822 | decltype(lttng_channel_trigger_list::channel_triggers_ht_node), | |
823 | <tng_channel_trigger_list::channel_triggers_ht_node>( | |
824 | *state->channel_triggers_ht)) { | |
e4db5ace JR |
825 | struct lttng_trigger_list_element *element; |
826 | ||
28ab034a | 827 | cds_list_for_each_entry (element, &channel_trigger_list->list, node) { |
9b63a4aa | 828 | const struct lttng_condition *current_condition = |
28ab034a | 829 | lttng_trigger_get_const_condition(element->trigger); |
e4db5ace | 830 | |
a0377dfe | 831 | LTTNG_ASSERT(current_condition); |
28ab034a | 832 | if (!lttng_condition_is_equal(condition, current_condition)) { |
e4db5ace JR |
833 | continue; |
834 | } | |
835 | ||
836 | /* Found the trigger, save the channel key. */ | |
837 | channel_key = &channel_trigger_list->channel_key; | |
838 | break; | |
839 | } | |
840 | if (channel_key) { | |
841 | /* The channel key was found stop iteration. */ | |
842 | break; | |
843 | } | |
844 | } | |
845 | ||
28ab034a | 846 | if (!channel_key) { |
e4db5ace | 847 | /* No channel found; normal exit. */ |
bd0514a5 | 848 | DBG("No known channel associated with newly subscribed-to condition"); |
e4db5ace JR |
849 | ret = 0; |
850 | goto end; | |
851 | } | |
852 | ||
853 | /* Fetch channel info for the matching channel. */ | |
854 | cds_lfht_lookup(state->channels_ht, | |
855 | hash_channel_key(channel_key), | |
856 | match_channel_info, | |
857 | channel_key, | |
858 | &iter); | |
859 | node = cds_lfht_iter_get_node(&iter); | |
a0377dfe | 860 | LTTNG_ASSERT(node); |
28ab034a | 861 | channel_info = caa_container_of(node, struct channel_info, channels_ht_node); |
e4db5ace JR |
862 | |
863 | /* Retrieve the channel's last sample, if it exists. */ | |
864 | cds_lfht_lookup(state->channel_state_ht, | |
865 | hash_channel_key(channel_key), | |
866 | match_channel_state_sample, | |
867 | channel_key, | |
868 | &iter); | |
869 | node = cds_lfht_iter_get_node(&iter); | |
870 | if (node) { | |
28ab034a JG |
871 | last_sample = |
872 | caa_container_of(node, struct channel_state_sample, channel_state_ht_node); | |
e4db5ace JR |
873 | } else { |
874 | /* Nothing to evaluate, no sample was ever taken. Normal exit */ | |
bd0514a5 | 875 | DBG("No channel sample associated with newly subscribed-to condition"); |
e4db5ace JR |
876 | ret = 0; |
877 | goto end; | |
878 | } | |
879 | ||
28ab034a | 880 | ret = evaluate_buffer_condition( |
cd9adb8b | 881 | condition, evaluation, state, nullptr, last_sample, channel_info); |
e4db5ace | 882 | if (ret) { |
bd0514a5 | 883 | WARN("Fatal error occurred while evaluating a newly subscribed-to condition"); |
e4db5ace JR |
884 | goto end; |
885 | } | |
886 | ||
f82f93a1 JG |
887 | *session_uid = channel_info->session_info->uid; |
888 | *session_gid = channel_info->session_info->gid; | |
889 | end: | |
890 | return ret; | |
891 | } | |
892 | ||
28ab034a | 893 | static const char *get_condition_session_name(const struct lttng_condition *condition) |
f82f93a1 | 894 | { |
cd9adb8b | 895 | const char *session_name = nullptr; |
f82f93a1 JG |
896 | enum lttng_condition_status status; |
897 | ||
898 | switch (lttng_condition_get_type(condition)) { | |
899 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
900 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
28ab034a | 901 | status = lttng_condition_buffer_usage_get_session_name(condition, &session_name); |
f82f93a1 JG |
902 | break; |
903 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: | |
28ab034a JG |
904 | status = lttng_condition_session_consumed_size_get_session_name(condition, |
905 | &session_name); | |
f82f93a1 JG |
906 | break; |
907 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: | |
908 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
28ab034a JG |
909 | status = |
910 | lttng_condition_session_rotation_get_session_name(condition, &session_name); | |
f82f93a1 JG |
911 | break; |
912 | default: | |
913 | abort(); | |
914 | } | |
915 | if (status != LTTNG_CONDITION_STATUS_OK) { | |
bd0514a5 | 916 | ERR("Failed to retrieve session rotation condition's session name"); |
f82f93a1 JG |
917 | goto end; |
918 | } | |
919 | end: | |
920 | return session_name; | |
921 | } | |
922 | ||
28ab034a JG |
923 | static bool evaluate_session_rotation_ongoing_condition(const struct lttng_condition *condition |
924 | __attribute__((unused)), | |
925 | const struct session_state_sample *sample) | |
319dcddc JG |
926 | { |
927 | return sample->rotation.ongoing; | |
928 | } | |
929 | ||
28ab034a JG |
930 | static bool evaluate_session_consumed_size_condition(const struct lttng_condition *condition, |
931 | const struct session_state_sample *sample) | |
319dcddc JG |
932 | { |
933 | uint64_t threshold; | |
934 | const struct lttng_condition_session_consumed_size *size_condition = | |
28ab034a JG |
935 | lttng::utils::container_of(condition, |
936 | <tng_condition_session_consumed_size::parent); | |
319dcddc JG |
937 | |
938 | threshold = size_condition->consumed_threshold_bytes.value; | |
28ab034a JG |
939 | DBG("Session consumed size condition being evaluated: threshold = %" PRIu64 |
940 | ", current size = %" PRIu64, | |
941 | threshold, | |
942 | sample->consumed_data_size); | |
319dcddc JG |
943 | return sample->consumed_data_size >= threshold; |
944 | } | |
945 | ||
946 | /* | |
947 | * `new_state` can be NULL to indicate that we are not evaluating a | |
948 | * state transition. A client subscribed or a trigger was registered and | |
949 | * we wish to perform an initial evaluation. | |
950 | */ | |
28ab034a JG |
951 | static int evaluate_session_condition(const struct lttng_condition *condition, |
952 | const struct session_info *session_info, | |
953 | const struct session_state_sample *new_state, | |
954 | struct lttng_evaluation **evaluation) | |
f82f93a1 JG |
955 | { |
956 | int ret; | |
319dcddc | 957 | bool previous_result, newest_result; |
f82f93a1 | 958 | |
319dcddc JG |
959 | switch (lttng_condition_get_type(condition)) { |
960 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: | |
961 | if (new_state) { | |
962 | previous_result = evaluate_session_rotation_ongoing_condition( | |
28ab034a JG |
963 | condition, &session_info->last_state_sample); |
964 | newest_result = | |
965 | evaluate_session_rotation_ongoing_condition(condition, new_state); | |
319dcddc JG |
966 | } else { |
967 | previous_result = false; | |
968 | newest_result = evaluate_session_rotation_ongoing_condition( | |
28ab034a | 969 | condition, &session_info->last_state_sample); |
319dcddc JG |
970 | } |
971 | break; | |
972 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: | |
973 | if (new_state) { | |
974 | previous_result = evaluate_session_consumed_size_condition( | |
28ab034a JG |
975 | condition, &session_info->last_state_sample); |
976 | newest_result = | |
977 | evaluate_session_consumed_size_condition(condition, new_state); | |
319dcddc JG |
978 | } else { |
979 | previous_result = false; | |
980 | newest_result = evaluate_session_consumed_size_condition( | |
28ab034a | 981 | condition, &session_info->last_state_sample); |
319dcddc JG |
982 | } |
983 | break; | |
984 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
985 | /* | |
986 | * Note that LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED is | |
987 | * evaluated differently to only consider state transitions without regard for the | |
988 | * initial state. This is a deliberate choice as it is unlikely that a user would | |
989 | * expect an action to occur for a rotation that occurred long before the trigger or | |
990 | * subscription occurred. | |
991 | */ | |
992 | if (!new_state) { | |
993 | ret = 0; | |
994 | goto end; | |
995 | } | |
f82f93a1 | 996 | |
319dcddc JG |
997 | previous_result = !session_info->last_state_sample.rotation.ongoing; |
998 | newest_result = !new_state->rotation.ongoing; | |
999 | break; | |
1000 | default: | |
1001 | ret = 0; | |
1002 | goto end; | |
1003 | } | |
1004 | ||
1005 | if (!newest_result || (previous_result == newest_result)) { | |
1006 | /* Not a state transition, evaluate to false. */ | |
f82f93a1 JG |
1007 | ret = 0; |
1008 | goto end; | |
1009 | } | |
1010 | ||
f82f93a1 JG |
1011 | switch (lttng_condition_get_type(condition)) { |
1012 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: | |
319dcddc | 1013 | { |
28ab034a JG |
1014 | const auto rotation_id = new_state ? new_state->rotation.id : |
1015 | session_info->last_state_sample.rotation.id; | |
f82f93a1 | 1016 | |
319dcddc JG |
1017 | *evaluation = lttng_evaluation_session_rotation_ongoing_create(rotation_id); |
1018 | break; | |
1019 | } | |
1020 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
1021 | { | |
d5ea8944 JG |
1022 | const auto& sample = new_state ? *new_state : session_info->last_state_sample; |
1023 | const auto rotation_id = sample.rotation.id; | |
319dcddc JG |
1024 | |
1025 | /* Callee acquires a reference to location. */ | |
1026 | *evaluation = lttng_evaluation_session_rotation_completed_create( | |
28ab034a | 1027 | rotation_id, sample.rotation.location); |
319dcddc JG |
1028 | break; |
1029 | } | |
1030 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: | |
1031 | { | |
1032 | const auto latest_session_consumed_total = new_state ? | |
28ab034a JG |
1033 | new_state->consumed_data_size : |
1034 | session_info->last_state_sample.consumed_data_size; | |
319dcddc JG |
1035 | |
1036 | *evaluation = lttng_evaluation_session_consumed_size_create( | |
28ab034a | 1037 | latest_session_consumed_total); |
f82f93a1 | 1038 | break; |
319dcddc | 1039 | } |
f82f93a1 | 1040 | default: |
319dcddc | 1041 | abort(); |
f82f93a1 JG |
1042 | } |
1043 | ||
319dcddc JG |
1044 | if (!*evaluation) { |
1045 | /* Fatal error. */ | |
1046 | ERR("Failed to create session condition evaluation: session name = `%s`", | |
28ab034a | 1047 | session_info->name); |
319dcddc JG |
1048 | ret = -1; |
1049 | goto end; | |
1050 | } | |
f82f93a1 | 1051 | |
319dcddc | 1052 | ret = 0; |
f82f93a1 JG |
1053 | end: |
1054 | return ret; | |
1055 | } | |
1056 | ||
28ab034a JG |
1057 | static int evaluate_condition_for_client(const struct lttng_trigger *trigger, |
1058 | const struct lttng_condition *condition, | |
1059 | struct notification_client *client, | |
1060 | struct notification_thread_state *state) | |
f82f93a1 JG |
1061 | { |
1062 | int ret; | |
cd9adb8b | 1063 | struct lttng_evaluation *evaluation = nullptr; |
505b2d90 JG |
1064 | struct notification_client_list client_list = { |
1065 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
1c9a0b0e | 1066 | .ref = {}, |
cd9adb8b | 1067 | .condition = nullptr, |
1c9a0b0e MJ |
1068 | .triggers_list = {}, |
1069 | .clients_list = {}, | |
cd9adb8b | 1070 | .notification_trigger_clients_ht = nullptr, |
1c9a0b0e MJ |
1071 | .notification_trigger_clients_ht_node = {}, |
1072 | .rcu_node = {}, | |
505b2d90 | 1073 | }; |
1c9a0b0e | 1074 | struct notification_client_list_element client_list_element = {}; |
f82f93a1 JG |
1075 | uid_t object_uid = 0; |
1076 | gid_t object_gid = 0; | |
1077 | ||
a0377dfe FD |
1078 | LTTNG_ASSERT(trigger); |
1079 | LTTNG_ASSERT(condition); | |
1080 | LTTNG_ASSERT(client); | |
1081 | LTTNG_ASSERT(state); | |
f82f93a1 JG |
1082 | |
1083 | switch (get_condition_binding_object(condition)) { | |
1084 | case LTTNG_OBJECT_TYPE_SESSION: | |
319dcddc JG |
1085 | { |
1086 | /* Find the session associated with the condition. */ | |
1087 | const auto *session_name = get_condition_session_name(condition); | |
1088 | auto session_info = get_session_info_by_name(state, session_name); | |
1089 | if (!session_info) { | |
1090 | /* Not an error, the session doesn't exist yet. */ | |
1091 | DBG("Session not found while evaluating session condition for client: session name = `%s`", | |
28ab034a | 1092 | session_name); |
319dcddc JG |
1093 | ret = 0; |
1094 | goto end; | |
1095 | } | |
1096 | ||
1097 | object_uid = session_info->uid; | |
1098 | object_gid = session_info->gid; | |
1099 | ||
cd9adb8b | 1100 | ret = evaluate_session_condition(condition, session_info, nullptr, &evaluation); |
319dcddc | 1101 | session_info_put(session_info); |
f82f93a1 | 1102 | break; |
319dcddc | 1103 | } |
f82f93a1 | 1104 | case LTTNG_OBJECT_TYPE_CHANNEL: |
28ab034a JG |
1105 | ret = evaluate_channel_condition_for_client( |
1106 | condition, state, &evaluation, &object_uid, &object_gid); | |
f82f93a1 JG |
1107 | break; |
1108 | case LTTNG_OBJECT_TYPE_NONE: | |
bd0514a5 | 1109 | DBG("Newly subscribed-to condition not bound to object, nothing to evaluate"); |
f82f93a1 JG |
1110 | ret = 0; |
1111 | goto end; | |
1112 | case LTTNG_OBJECT_TYPE_UNKNOWN: | |
1113 | default: | |
1114 | ret = -1; | |
1115 | goto end; | |
1116 | } | |
af0c318d JG |
1117 | if (ret) { |
1118 | /* Fatal error. */ | |
1119 | goto end; | |
1120 | } | |
e4db5ace JR |
1121 | if (!evaluation) { |
1122 | /* Evaluation yielded nothing. Normal exit. */ | |
bd0514a5 | 1123 | DBG("Newly subscribed-to condition evaluated to false, nothing to report to client"); |
e4db5ace JR |
1124 | ret = 0; |
1125 | goto end; | |
1126 | } | |
1127 | ||
1128 | /* | |
1129 | * Create a temporary client list with the client currently | |
1130 | * subscribing. | |
1131 | */ | |
505b2d90 | 1132 | cds_lfht_node_init(&client_list.notification_trigger_clients_ht_node); |
091fa780 | 1133 | CDS_INIT_LIST_HEAD(&client_list.clients_list); |
e4db5ace JR |
1134 | |
1135 | CDS_INIT_LIST_HEAD(&client_list_element.node); | |
1136 | client_list_element.client = client; | |
091fa780 | 1137 | cds_list_add(&client_list_element.node, &client_list.clients_list); |
e4db5ace JR |
1138 | |
1139 | /* Send evaluation result to the newly-subscribed client. */ | |
bd0514a5 | 1140 | DBG("Newly subscribed-to condition evaluated to true, notifying client"); |
28ab034a JG |
1141 | ret = send_evaluation_to_clients( |
1142 | trigger, evaluation, &client_list, state, object_uid, object_gid); | |
e4db5ace JR |
1143 | |
1144 | end: | |
1145 | return ret; | |
1146 | } | |
1147 | ||
28ab034a JG |
1148 | static int notification_thread_client_subscribe(struct notification_client *client, |
1149 | struct lttng_condition *condition, | |
1150 | struct notification_thread_state *state, | |
1151 | enum lttng_notification_channel_status *_status) | |
ab0ee2ca JG |
1152 | { |
1153 | int ret = 0; | |
cd9adb8b JG |
1154 | struct notification_client_list *client_list = nullptr; |
1155 | struct lttng_condition_list_element *condition_list_element = nullptr; | |
1156 | struct notification_client_list_element *client_list_element = nullptr; | |
a3ed2a4e | 1157 | struct lttng_trigger_ht_element *trigger_ht_element; |
28ab034a | 1158 | enum lttng_notification_channel_status status = LTTNG_NOTIFICATION_CHANNEL_STATUS_OK; |
ab0ee2ca JG |
1159 | |
1160 | /* | |
1161 | * Ensure that the client has not already subscribed to this condition | |
1162 | * before. | |
1163 | */ | |
28ab034a JG |
1164 | cds_list_for_each_entry (condition_list_element, &client->condition_list, node) { |
1165 | if (lttng_condition_is_equal(condition_list_element->condition, condition)) { | |
ab0ee2ca JG |
1166 | status = LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED; |
1167 | goto end; | |
1168 | } | |
1169 | } | |
1170 | ||
64803277 | 1171 | condition_list_element = zmalloc<lttng_condition_list_element>(); |
ab0ee2ca JG |
1172 | if (!condition_list_element) { |
1173 | ret = -1; | |
1174 | goto error; | |
1175 | } | |
64803277 | 1176 | client_list_element = zmalloc<notification_client_list_element>(); |
ab0ee2ca JG |
1177 | if (!client_list_element) { |
1178 | ret = -1; | |
1179 | goto error; | |
1180 | } | |
1181 | ||
ab0ee2ca JG |
1182 | /* |
1183 | * Add the newly-subscribed condition to the client's subscription list. | |
1184 | */ | |
1185 | CDS_INIT_LIST_HEAD(&condition_list_element->node); | |
1186 | condition_list_element->condition = condition; | |
cd9adb8b | 1187 | condition = nullptr; |
ab0ee2ca JG |
1188 | cds_list_add(&condition_list_element->node, &client->condition_list); |
1189 | ||
28ab034a | 1190 | client_list = get_client_list_from_condition(state, condition_list_element->condition); |
ed327204 | 1191 | if (!client_list) { |
2ae99f0b JG |
1192 | /* |
1193 | * No notification-emiting trigger registered with this | |
1194 | * condition. We don't evaluate the condition right away | |
1195 | * since this trigger is not registered yet. | |
1196 | */ | |
4fb43b68 | 1197 | free(client_list_element); |
505b2d90 | 1198 | goto end; |
ab0ee2ca JG |
1199 | } |
1200 | ||
2ae99f0b JG |
1201 | /* |
1202 | * The condition to which the client just subscribed is evaluated | |
1203 | * at this point so that conditions that are already TRUE result | |
1204 | * in a notification being sent out. | |
505b2d90 | 1205 | * |
57644a7f JG |
1206 | * Note the iteration on all triggers which share an identical |
1207 | * `condition` than the one to which the client is registering. This is | |
1208 | * done to ensure that the client receives a distinct notification for | |
1209 | * all triggers that have a `notify` action that have this condition. | |
2ae99f0b | 1210 | */ |
091fa780 | 1211 | pthread_mutex_lock(&client_list->lock); |
28ab034a JG |
1212 | cds_list_for_each_entry ( |
1213 | trigger_ht_element, &client_list->triggers_list, client_list_trigger_node) { | |
1214 | if (evaluate_condition_for_client(trigger_ht_element->trigger, | |
1215 | condition_list_element->condition, | |
1216 | client, | |
1217 | state)) { | |
bd0514a5 | 1218 | WARN("Evaluation of a condition on client subscription failed, aborting."); |
091fa780 FD |
1219 | ret = -1; |
1220 | free(client_list_element); | |
ecc7ed07 | 1221 | pthread_mutex_unlock(&client_list->lock); |
091fa780 FD |
1222 | goto end; |
1223 | } | |
e4db5ace | 1224 | } |
091fa780 | 1225 | pthread_mutex_unlock(&client_list->lock); |
e4db5ace JR |
1226 | |
1227 | /* | |
1228 | * Add the client to the list of clients interested in a given trigger | |
1229 | * if a "notification" trigger with a corresponding condition was | |
1230 | * added prior. | |
1231 | */ | |
ab0ee2ca JG |
1232 | client_list_element->client = client; |
1233 | CDS_INIT_LIST_HEAD(&client_list_element->node); | |
505b2d90 JG |
1234 | |
1235 | pthread_mutex_lock(&client_list->lock); | |
091fa780 | 1236 | cds_list_add(&client_list_element->node, &client_list->clients_list); |
505b2d90 | 1237 | pthread_mutex_unlock(&client_list->lock); |
ab0ee2ca JG |
1238 | end: |
1239 | if (_status) { | |
1240 | *_status = status; | |
1241 | } | |
505b2d90 JG |
1242 | if (client_list) { |
1243 | notification_client_list_put(client_list); | |
1244 | } | |
5a6306f7 | 1245 | lttng_condition_destroy(condition); |
ab0ee2ca JG |
1246 | return ret; |
1247 | error: | |
1248 | free(condition_list_element); | |
1249 | free(client_list_element); | |
5a6306f7 | 1250 | lttng_condition_destroy(condition); |
ab0ee2ca JG |
1251 | return ret; |
1252 | } | |
1253 | ||
28ab034a JG |
1254 | static int notification_thread_client_unsubscribe(struct notification_client *client, |
1255 | struct lttng_condition *condition, | |
1256 | struct notification_thread_state *state, | |
1257 | enum lttng_notification_channel_status *_status) | |
ab0ee2ca | 1258 | { |
ab0ee2ca | 1259 | struct notification_client_list *client_list; |
28ab034a JG |
1260 | struct lttng_condition_list_element *condition_list_element, *condition_tmp; |
1261 | struct notification_client_list_element *client_list_element, *client_tmp; | |
ab0ee2ca | 1262 | bool condition_found = false; |
28ab034a | 1263 | enum lttng_notification_channel_status status = LTTNG_NOTIFICATION_CHANNEL_STATUS_OK; |
ab0ee2ca JG |
1264 | |
1265 | /* Remove the condition from the client's condition list. */ | |
28ab034a JG |
1266 | cds_list_for_each_entry_safe ( |
1267 | condition_list_element, condition_tmp, &client->condition_list, node) { | |
1268 | if (!lttng_condition_is_equal(condition_list_element->condition, condition)) { | |
ab0ee2ca JG |
1269 | continue; |
1270 | } | |
1271 | ||
1272 | cds_list_del(&condition_list_element->node); | |
1273 | /* | |
1274 | * The caller may be iterating on the client's conditions to | |
1275 | * tear down a client's connection. In this case, the condition | |
1276 | * will be destroyed at the end. | |
1277 | */ | |
1278 | if (condition != condition_list_element->condition) { | |
28ab034a | 1279 | lttng_condition_destroy(condition_list_element->condition); |
ab0ee2ca JG |
1280 | } |
1281 | free(condition_list_element); | |
1282 | condition_found = true; | |
1283 | break; | |
1284 | } | |
1285 | ||
1286 | if (!condition_found) { | |
1287 | status = LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION; | |
1288 | goto end; | |
1289 | } | |
1290 | ||
1291 | /* | |
1292 | * Remove the client from the list of clients interested the trigger | |
1293 | * matching the condition. | |
1294 | */ | |
ed327204 JG |
1295 | client_list = get_client_list_from_condition(state, condition); |
1296 | if (!client_list) { | |
505b2d90 | 1297 | goto end; |
ab0ee2ca JG |
1298 | } |
1299 | ||
505b2d90 | 1300 | pthread_mutex_lock(&client_list->lock); |
28ab034a JG |
1301 | cds_list_for_each_entry_safe ( |
1302 | client_list_element, client_tmp, &client_list->clients_list, node) { | |
505b2d90 | 1303 | if (client_list_element->client->id != client->id) { |
ab0ee2ca JG |
1304 | continue; |
1305 | } | |
1306 | cds_list_del(&client_list_element->node); | |
1307 | free(client_list_element); | |
1308 | break; | |
1309 | } | |
505b2d90 JG |
1310 | pthread_mutex_unlock(&client_list->lock); |
1311 | notification_client_list_put(client_list); | |
cd9adb8b | 1312 | client_list = nullptr; |
ab0ee2ca JG |
1313 | end: |
1314 | lttng_condition_destroy(condition); | |
1315 | if (_status) { | |
1316 | *_status = status; | |
1317 | } | |
1318 | return 0; | |
1319 | } | |
1320 | ||
28ab034a | 1321 | static void free_notification_client_rcu(struct rcu_head *node) |
83b934ad | 1322 | { |
0114db0e | 1323 | free(lttng::utils::container_of(node, ¬ification_client::rcu_node)); |
83b934ad MD |
1324 | } |
1325 | ||
28ab034a | 1326 | static void notification_client_destroy(struct notification_client *client) |
ab0ee2ca | 1327 | { |
ab0ee2ca JG |
1328 | if (!client) { |
1329 | return; | |
1330 | } | |
1331 | ||
505b2d90 JG |
1332 | /* |
1333 | * The client object is not reachable by other threads, no need to lock | |
1334 | * the client here. | |
1335 | */ | |
ab0ee2ca JG |
1336 | if (client->socket >= 0) { |
1337 | (void) lttcomm_close_unix_sock(client->socket); | |
ac1889bf | 1338 | client->socket = -1; |
ab0ee2ca | 1339 | } |
505b2d90 | 1340 | client->communication.active = false; |
882093ee JR |
1341 | lttng_payload_reset(&client->communication.inbound.payload); |
1342 | lttng_payload_reset(&client->communication.outbound.payload); | |
505b2d90 | 1343 | pthread_mutex_destroy(&client->lock); |
83b934ad | 1344 | call_rcu(&client->rcu_node, free_notification_client_rcu); |
ab0ee2ca JG |
1345 | } |
1346 | ||
1347 | /* | |
1348 | * Call with rcu_read_lock held (and hold for the lifetime of the returned | |
1349 | * client pointer). | |
1350 | */ | |
28ab034a JG |
1351 | static struct notification_client *get_client_from_socket(int socket, |
1352 | struct notification_thread_state *state) | |
ab0ee2ca JG |
1353 | { |
1354 | struct cds_lfht_iter iter; | |
1355 | struct cds_lfht_node *node; | |
cd9adb8b | 1356 | struct notification_client *client = nullptr; |
ab0ee2ca | 1357 | |
48b7cdc2 FD |
1358 | ASSERT_RCU_READ_LOCKED(); |
1359 | ||
ab0ee2ca | 1360 | cds_lfht_lookup(state->client_socket_ht, |
ac1889bf JG |
1361 | hash_client_socket(socket), |
1362 | match_client_socket, | |
ab0ee2ca JG |
1363 | (void *) (unsigned long) socket, |
1364 | &iter); | |
1365 | node = cds_lfht_iter_get_node(&iter); | |
1366 | if (!node) { | |
1367 | goto end; | |
1368 | } | |
1369 | ||
28ab034a | 1370 | client = caa_container_of(node, struct notification_client, client_socket_ht_node); |
ab0ee2ca JG |
1371 | end: |
1372 | return client; | |
1373 | } | |
1374 | ||
f2b3ef9f JG |
1375 | /* |
1376 | * Call with rcu_read_lock held (and hold for the lifetime of the returned | |
1377 | * client pointer). | |
1378 | */ | |
28ab034a JG |
1379 | static struct notification_client *get_client_from_id(notification_client_id id, |
1380 | struct notification_thread_state *state) | |
f2b3ef9f JG |
1381 | { |
1382 | struct cds_lfht_iter iter; | |
1383 | struct cds_lfht_node *node; | |
cd9adb8b | 1384 | struct notification_client *client = nullptr; |
f2b3ef9f | 1385 | |
48b7cdc2 FD |
1386 | ASSERT_RCU_READ_LOCKED(); |
1387 | ||
28ab034a | 1388 | cds_lfht_lookup(state->client_id_ht, hash_client_id(id), match_client_id, &id, &iter); |
f2b3ef9f JG |
1389 | node = cds_lfht_iter_get_node(&iter); |
1390 | if (!node) { | |
1391 | goto end; | |
1392 | } | |
1393 | ||
28ab034a | 1394 | client = caa_container_of(node, struct notification_client, client_id_ht_node); |
f2b3ef9f JG |
1395 | end: |
1396 | return client; | |
1397 | } | |
1398 | ||
28ab034a JG |
1399 | static bool buffer_usage_condition_applies_to_channel(const struct lttng_condition *condition, |
1400 | const struct channel_info *channel_info) | |
ab0ee2ca JG |
1401 | { |
1402 | enum lttng_condition_status status; | |
e8360425 | 1403 | enum lttng_domain_type condition_domain; |
cd9adb8b JG |
1404 | const char *condition_session_name = nullptr; |
1405 | const char *condition_channel_name = nullptr; | |
ab0ee2ca | 1406 | |
28ab034a | 1407 | status = lttng_condition_buffer_usage_get_domain_type(condition, &condition_domain); |
a0377dfe | 1408 | LTTNG_ASSERT(status == LTTNG_CONDITION_STATUS_OK); |
e8360425 | 1409 | if (channel_info->key.domain != condition_domain) { |
ab0ee2ca JG |
1410 | goto fail; |
1411 | } | |
1412 | ||
28ab034a | 1413 | status = lttng_condition_buffer_usage_get_session_name(condition, &condition_session_name); |
a0377dfe | 1414 | LTTNG_ASSERT((status == LTTNG_CONDITION_STATUS_OK) && condition_session_name); |
e8360425 | 1415 | |
28ab034a | 1416 | status = lttng_condition_buffer_usage_get_channel_name(condition, &condition_channel_name); |
a0377dfe | 1417 | LTTNG_ASSERT((status == LTTNG_CONDITION_STATUS_OK) && condition_channel_name); |
e8360425 | 1418 | |
5c7248cd | 1419 | if (strcmp(channel_info->session_info->name, condition_session_name) != 0) { |
e8360425 JD |
1420 | goto fail; |
1421 | } | |
5c7248cd | 1422 | if (strcmp(channel_info->name, condition_channel_name) != 0) { |
ab0ee2ca JG |
1423 | goto fail; |
1424 | } | |
1425 | ||
e8360425 JD |
1426 | return true; |
1427 | fail: | |
1428 | return false; | |
1429 | } | |
1430 | ||
28ab034a JG |
1431 | static bool trigger_applies_to_channel(const struct lttng_trigger *trigger, |
1432 | const struct channel_info *channel_info) | |
51eab943 JG |
1433 | { |
1434 | const struct lttng_condition *condition; | |
e8360425 | 1435 | bool trigger_applies; |
ab0ee2ca | 1436 | |
51eab943 | 1437 | condition = lttng_trigger_get_const_condition(trigger); |
e8360425 | 1438 | if (!condition) { |
ab0ee2ca JG |
1439 | goto fail; |
1440 | } | |
e8360425 JD |
1441 | |
1442 | switch (lttng_condition_get_type(condition)) { | |
1443 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
1444 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
28ab034a JG |
1445 | trigger_applies = |
1446 | buffer_usage_condition_applies_to_channel(condition, channel_info); | |
e8360425 | 1447 | break; |
e8360425 | 1448 | default: |
ab0ee2ca JG |
1449 | goto fail; |
1450 | } | |
1451 | ||
e8360425 | 1452 | return trigger_applies; |
ab0ee2ca JG |
1453 | fail: |
1454 | return false; | |
1455 | } | |
1456 | ||
ed327204 | 1457 | /* Must be called with RCU read lock held. */ |
28ab034a JG |
1458 | static struct lttng_session_trigger_list * |
1459 | get_session_trigger_list(struct notification_thread_state *state, const char *session_name) | |
ed327204 | 1460 | { |
cd9adb8b | 1461 | struct lttng_session_trigger_list *list = nullptr; |
ed327204 JG |
1462 | struct cds_lfht_node *node; |
1463 | struct cds_lfht_iter iter; | |
1464 | ||
48b7cdc2 FD |
1465 | ASSERT_RCU_READ_LOCKED(); |
1466 | ||
ed327204 JG |
1467 | cds_lfht_lookup(state->session_triggers_ht, |
1468 | hash_key_str(session_name, lttng_ht_seed), | |
1469 | match_session_trigger_list, | |
1470 | session_name, | |
1471 | &iter); | |
1472 | node = cds_lfht_iter_get_node(&iter); | |
1473 | if (!node) { | |
1474 | /* | |
1475 | * Not an error, the list of triggers applying to that session | |
1476 | * will be initialized when the session is created. | |
1477 | */ | |
bd0514a5 | 1478 | DBG("No trigger list found for session \"%s\" as it is not yet known to the notification system", |
28ab034a | 1479 | session_name); |
ed327204 JG |
1480 | goto end; |
1481 | } | |
1482 | ||
28ab034a | 1483 | list = caa_container_of(node, struct lttng_session_trigger_list, session_triggers_ht_node); |
ed327204 JG |
1484 | end: |
1485 | return list; | |
1486 | } | |
1487 | ||
ea9a44f0 JG |
1488 | /* |
1489 | * Allocate an empty lttng_session_trigger_list for the session named | |
1490 | * 'session_name'. | |
ea9a44f0 | 1491 | */ |
28ab034a JG |
1492 | static struct lttng_session_trigger_list * |
1493 | lttng_session_trigger_list_create(const char *session_name, struct cds_lfht *session_triggers_ht) | |
ea9a44f0 | 1494 | { |
cd9adb8b | 1495 | struct lttng_session_trigger_list *list = nullptr; |
319dcddc JG |
1496 | char *session_name_copy = strdup(session_name); |
1497 | ||
1498 | if (!session_name_copy) { | |
1499 | PERROR("Failed to allocate session name while building trigger list"); | |
1500 | goto end; | |
1501 | } | |
ea9a44f0 | 1502 | |
64803277 | 1503 | list = zmalloc<lttng_session_trigger_list>(); |
ea9a44f0 | 1504 | if (!list) { |
319dcddc | 1505 | PERROR("Failed to allocate session trigger list while building trigger list"); |
ea9a44f0 JG |
1506 | goto end; |
1507 | } | |
319dcddc JG |
1508 | |
1509 | list->session_name = session_name_copy; | |
ea9a44f0 JG |
1510 | CDS_INIT_LIST_HEAD(&list->list); |
1511 | cds_lfht_node_init(&list->session_triggers_ht_node); | |
1512 | list->session_triggers_ht = session_triggers_ht; | |
1513 | ||
ea9a44f0 | 1514 | /* Publish the list through the session_triggers_ht. */ |
56047f5a | 1515 | { |
07c4863f | 1516 | const lttng::urcu::read_lock_guard read_lock; |
56047f5a JG |
1517 | cds_lfht_add(session_triggers_ht, |
1518 | hash_key_str(session_name, lttng_ht_seed), | |
1519 | &list->session_triggers_ht_node); | |
1520 | } | |
ea9a44f0 JG |
1521 | end: |
1522 | return list; | |
1523 | } | |
1524 | ||
28ab034a | 1525 | static void free_session_trigger_list_rcu(struct rcu_head *node) |
ea9a44f0 | 1526 | { |
319dcddc | 1527 | struct lttng_session_trigger_list *list = |
28ab034a | 1528 | caa_container_of(node, struct lttng_session_trigger_list, rcu_node); |
319dcddc JG |
1529 | |
1530 | free(list->session_name); | |
1531 | free(list); | |
ea9a44f0 JG |
1532 | } |
1533 | ||
28ab034a | 1534 | static void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list *list) |
ea9a44f0 JG |
1535 | { |
1536 | struct lttng_trigger_list_element *trigger_list_element, *tmp; | |
1537 | ||
1538 | /* Empty the list element by element, and then free the list itself. */ | |
28ab034a | 1539 | cds_list_for_each_entry_safe (trigger_list_element, tmp, &list->list, node) { |
ea9a44f0 JG |
1540 | cds_list_del(&trigger_list_element->node); |
1541 | free(trigger_list_element); | |
1542 | } | |
07c4863f | 1543 | const lttng::urcu::read_lock_guard read_lock; |
ea9a44f0 | 1544 | /* Unpublish the list from the session_triggers_ht. */ |
28ab034a | 1545 | cds_lfht_del(list->session_triggers_ht, &list->session_triggers_ht_node); |
ea9a44f0 JG |
1546 | call_rcu(&list->rcu_node, free_session_trigger_list_rcu); |
1547 | } | |
1548 | ||
28ab034a JG |
1549 | static int lttng_session_trigger_list_add(struct lttng_session_trigger_list *list, |
1550 | struct lttng_trigger *trigger) | |
ea9a44f0 JG |
1551 | { |
1552 | int ret = 0; | |
28ab034a | 1553 | struct lttng_trigger_list_element *new_element = zmalloc<lttng_trigger_list_element>(); |
ea9a44f0 JG |
1554 | |
1555 | if (!new_element) { | |
1556 | ret = -1; | |
1557 | goto end; | |
1558 | } | |
1559 | CDS_INIT_LIST_HEAD(&new_element->node); | |
1560 | new_element->trigger = trigger; | |
1561 | cds_list_add(&new_element->node, &list->list); | |
1562 | end: | |
1563 | return ret; | |
1564 | } | |
1565 | ||
28ab034a JG |
1566 | static bool trigger_applies_to_session(const struct lttng_trigger *trigger, |
1567 | const char *session_name) | |
ea9a44f0 JG |
1568 | { |
1569 | bool applies = false; | |
1570 | const struct lttng_condition *condition; | |
1571 | ||
1572 | condition = lttng_trigger_get_const_condition(trigger); | |
1573 | switch (lttng_condition_get_type(condition)) { | |
1574 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: | |
1575 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
319dcddc | 1576 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: |
ea9a44f0 | 1577 | { |
ea9a44f0 JG |
1578 | const char *condition_session_name; |
1579 | ||
319dcddc | 1580 | condition_session_name = get_condition_session_name(condition); |
a0377dfe | 1581 | LTTNG_ASSERT(condition_session_name); |
ea9a44f0 JG |
1582 | applies = !strcmp(condition_session_name, session_name); |
1583 | break; | |
1584 | } | |
1585 | default: | |
1586 | goto end; | |
1587 | } | |
1588 | end: | |
1589 | return applies; | |
1590 | } | |
1591 | ||
1592 | /* | |
1593 | * Allocate and initialize an lttng_session_trigger_list which contains | |
1594 | * all triggers that apply to the session named 'session_name'. | |
ea9a44f0 | 1595 | */ |
28ab034a JG |
1596 | static struct lttng_session_trigger_list * |
1597 | lttng_session_trigger_list_build(const struct notification_thread_state *state, | |
1598 | const char *session_name) | |
ea9a44f0 JG |
1599 | { |
1600 | int trigger_count = 0; | |
cd9adb8b | 1601 | struct lttng_session_trigger_list *session_trigger_list = nullptr; |
ea9a44f0 | 1602 | |
28ab034a JG |
1603 | session_trigger_list = |
1604 | lttng_session_trigger_list_create(session_name, state->session_triggers_ht); | |
ea9a44f0 | 1605 | |
6e7ffe66 JG |
1606 | for (auto *trigger_ht_element : |
1607 | lttng::urcu::lfht_iteration_adapter<lttng_trigger_ht_element, | |
1608 | decltype(lttng_trigger_ht_element::node), | |
1609 | <tng_trigger_ht_element::node>( | |
1610 | *state->triggers_ht)) { | |
1611 | if (!trigger_applies_to_session(trigger_ht_element->trigger, session_name)) { | |
1612 | continue; | |
1613 | } | |
56047f5a | 1614 | |
6e7ffe66 JG |
1615 | const auto ret = lttng_session_trigger_list_add(session_trigger_list, |
1616 | trigger_ht_element->trigger); | |
1617 | if (ret) { | |
1618 | goto error; | |
56047f5a | 1619 | } |
6e7ffe66 JG |
1620 | |
1621 | trigger_count++; | |
ea9a44f0 JG |
1622 | } |
1623 | ||
28ab034a | 1624 | DBG("Found %i triggers that apply to newly created session", trigger_count); |
ea9a44f0 JG |
1625 | return session_trigger_list; |
1626 | error: | |
1627 | lttng_session_trigger_list_destroy(session_trigger_list); | |
cd9adb8b | 1628 | return nullptr; |
ea9a44f0 JG |
1629 | } |
1630 | ||
28ab034a JG |
1631 | static struct session_info *create_and_publish_session_info(struct notification_thread_state *state, |
1632 | uint64_t id, | |
1633 | const char *name, | |
1634 | uid_t uid, | |
1635 | gid_t gid) | |
8abe313a | 1636 | { |
cd9adb8b | 1637 | struct session_info *session = nullptr; |
ea9a44f0 | 1638 | struct lttng_session_trigger_list *trigger_list; |
8abe313a | 1639 | |
07c4863f | 1640 | const lttng::urcu::read_lock_guard read_lock; |
ea9a44f0 JG |
1641 | trigger_list = lttng_session_trigger_list_build(state, name); |
1642 | if (!trigger_list) { | |
1643 | goto error; | |
8abe313a JG |
1644 | } |
1645 | ||
28ab034a | 1646 | session = session_info_create(id, name, uid, gid, trigger_list, state->sessions_ht); |
8abe313a | 1647 | if (!session) { |
bd0514a5 | 1648 | ERR("Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)", |
28ab034a JG |
1649 | name, |
1650 | uid, | |
1651 | gid); | |
b248644d | 1652 | lttng_session_trigger_list_destroy(trigger_list); |
ea9a44f0 | 1653 | goto error; |
8abe313a | 1654 | } |
139a8d25 JG |
1655 | |
1656 | /* Transferred ownership to the new session. */ | |
cd9adb8b | 1657 | trigger_list = nullptr; |
577983cb | 1658 | |
28ab034a JG |
1659 | if (cds_lfht_add_unique(state->sessions_ht, |
1660 | hash_session_info(session), | |
1661 | match_session_info, | |
1662 | &id, | |
1663 | &session->sessions_ht_node) != &session->sessions_ht_node) { | |
139a8d25 JG |
1664 | ERR("Duplicate session found: name = `%s`, id = %" PRIu64, name, id); |
1665 | goto error; | |
1666 | } | |
1667 | ||
8abe313a | 1668 | return session; |
ea9a44f0 | 1669 | error: |
ea9a44f0 | 1670 | session_info_put(session); |
cd9adb8b | 1671 | return nullptr; |
8abe313a JG |
1672 | } |
1673 | ||
28ab034a JG |
1674 | static int handle_notification_thread_command_add_channel(struct notification_thread_state *state, |
1675 | uint64_t session_id, | |
1676 | const char *channel_name, | |
1677 | enum lttng_domain_type channel_domain, | |
1678 | uint64_t channel_key_int, | |
1679 | uint64_t channel_capacity, | |
1680 | enum lttng_error_code *cmd_result) | |
ab0ee2ca JG |
1681 | { |
1682 | struct cds_list_head trigger_list; | |
cd9adb8b | 1683 | struct channel_info *new_channel_info = nullptr; |
8abe313a JG |
1684 | struct channel_key channel_key = { |
1685 | .key = channel_key_int, | |
1686 | .domain = channel_domain, | |
1687 | }; | |
cd9adb8b | 1688 | struct lttng_channel_trigger_list *channel_trigger_list = nullptr; |
ab0ee2ca | 1689 | int trigger_count = 0; |
cd9adb8b | 1690 | struct session_info *session_info = nullptr; |
07c4863f | 1691 | const lttng::urcu::read_lock_guard read_lock; |
ab0ee2ca | 1692 | |
28ab034a JG |
1693 | DBG("Adding channel: channel name = `%s`, session id = %" PRIu64 ", channel key = %" PRIu64 |
1694 | ", domain = %s", | |
1695 | channel_name, | |
1696 | session_id, | |
1697 | channel_key_int, | |
1698 | lttng_domain_type_str(channel_domain)); | |
ab0ee2ca JG |
1699 | |
1700 | CDS_INIT_LIST_HEAD(&trigger_list); | |
1701 | ||
139a8d25 | 1702 | session_info = get_session_info_by_id(state, session_id); |
8abe313a | 1703 | if (!session_info) { |
139a8d25 JG |
1704 | /* Fatal logic error. */ |
1705 | ERR("Failed to find session while adding channel: session id = %" PRIu64, | |
28ab034a | 1706 | session_id); |
ab0ee2ca JG |
1707 | goto error; |
1708 | } | |
1709 | ||
28ab034a JG |
1710 | new_channel_info = |
1711 | channel_info_create(channel_name, &channel_key, channel_capacity, session_info); | |
8abe313a JG |
1712 | if (!new_channel_info) { |
1713 | goto error; | |
1714 | } | |
ab0ee2ca JG |
1715 | |
1716 | /* Build a list of all triggers applying to the new channel. */ | |
6e7ffe66 JG |
1717 | for (auto *trigger_ht_element : |
1718 | lttng::urcu::lfht_iteration_adapter<lttng_trigger_ht_element, | |
1719 | decltype(lttng_trigger_ht_element::node), | |
1720 | <tng_trigger_ht_element::node>( | |
1721 | *state->triggers_ht)) { | |
ab0ee2ca JG |
1722 | struct lttng_trigger_list_element *new_element; |
1723 | ||
28ab034a | 1724 | if (!trigger_applies_to_channel(trigger_ht_element->trigger, new_channel_info)) { |
ab0ee2ca JG |
1725 | continue; |
1726 | } | |
1727 | ||
64803277 | 1728 | new_element = zmalloc<lttng_trigger_list_element>(); |
ab0ee2ca JG |
1729 | if (!new_element) { |
1730 | goto error; | |
1731 | } | |
1732 | CDS_INIT_LIST_HEAD(&new_element->node); | |
1733 | new_element->trigger = trigger_ht_element->trigger; | |
1734 | cds_list_add(&new_element->node, &trigger_list); | |
1735 | trigger_count++; | |
1736 | } | |
1737 | ||
28ab034a | 1738 | DBG("Found %i triggers that apply to newly added channel", trigger_count); |
64803277 | 1739 | channel_trigger_list = zmalloc<lttng_channel_trigger_list>(); |
ab0ee2ca JG |
1740 | if (!channel_trigger_list) { |
1741 | goto error; | |
1742 | } | |
8abe313a | 1743 | channel_trigger_list->channel_key = new_channel_info->key; |
ab0ee2ca JG |
1744 | CDS_INIT_LIST_HEAD(&channel_trigger_list->list); |
1745 | cds_lfht_node_init(&channel_trigger_list->channel_triggers_ht_node); | |
1746 | cds_list_splice(&trigger_list, &channel_trigger_list->list); | |
1747 | ||
ab0ee2ca JG |
1748 | /* Add channel to the channel_ht which owns the channel_infos. */ |
1749 | cds_lfht_add(state->channels_ht, | |
28ab034a JG |
1750 | hash_channel_key(&new_channel_info->key), |
1751 | &new_channel_info->channels_ht_node); | |
ab0ee2ca JG |
1752 | /* |
1753 | * Add the list of triggers associated with this channel to the | |
1754 | * channel_triggers_ht. | |
1755 | */ | |
1756 | cds_lfht_add(state->channel_triggers_ht, | |
28ab034a JG |
1757 | hash_channel_key(&new_channel_info->key), |
1758 | &channel_trigger_list->channel_triggers_ht_node); | |
1eee26c5 | 1759 | session_info_put(session_info); |
ab0ee2ca JG |
1760 | *cmd_result = LTTNG_OK; |
1761 | return 0; | |
1762 | error: | |
ab0ee2ca | 1763 | channel_info_destroy(new_channel_info); |
8abe313a | 1764 | session_info_put(session_info); |
ab0ee2ca JG |
1765 | return 1; |
1766 | } | |
1767 | ||
28ab034a JG |
1768 | static int handle_notification_thread_command_add_session(struct notification_thread_state *state, |
1769 | uint64_t session_id, | |
1770 | const char *session_name, | |
1771 | uid_t session_uid, | |
1772 | gid_t session_gid, | |
1773 | enum lttng_error_code *cmd_result) | |
139a8d25 JG |
1774 | { |
1775 | int ret; | |
1776 | ||
28ab034a JG |
1777 | DBG("Adding session: session name = `%s`, session id = %" PRIu64 |
1778 | ", session uid = %d, session gid = %d", | |
1779 | session_name, | |
1780 | session_id, | |
1781 | session_uid, | |
1782 | session_gid); | |
139a8d25 | 1783 | |
28ab034a JG |
1784 | auto session = create_and_publish_session_info( |
1785 | state, session_id, session_name, session_uid, session_gid); | |
139a8d25 | 1786 | if (!session) { |
28ab034a JG |
1787 | PERROR("Failed to add session: session name = `%s`, session id = %" PRIu64 |
1788 | ", session uid = %d, session gid = %d", | |
1789 | session_name, | |
1790 | session_id, | |
1791 | session_uid, | |
1792 | session_gid); | |
139a8d25 JG |
1793 | ret = -1; |
1794 | *cmd_result = LTTNG_ERR_NOMEM; | |
1795 | goto end; | |
1796 | } | |
1797 | ||
1798 | /* | |
1799 | * Note that the reference to `session` is not released; this reference is | |
1800 | * the "global" reference that is used to allow look-ups. This reference will | |
1801 | * only be released when the session is removed. See | |
1802 | * handle_notification_thread_command_remove_session. | |
1803 | */ | |
1804 | ret = 0; | |
1805 | *cmd_result = LTTNG_OK; | |
1806 | end: | |
1807 | return ret; | |
1808 | } | |
1809 | ||
28ab034a JG |
1810 | static int |
1811 | handle_notification_thread_command_remove_session(struct notification_thread_state *state, | |
1812 | uint64_t session_id, | |
1813 | enum lttng_error_code *cmd_result) | |
139a8d25 JG |
1814 | { |
1815 | int ret; | |
1816 | ||
1817 | DBG("Removing session: session id = %" PRIu64, session_id); | |
1818 | ||
1819 | auto session = get_session_info_by_id(state, session_id); | |
1820 | if (!session) { | |
1821 | ERR("Failed to remove session: session id = %" PRIu64, session_id); | |
1822 | ret = -1; | |
1823 | *cmd_result = LTTNG_ERR_NO_SESSION; | |
1824 | goto end; | |
1825 | } | |
1826 | ||
1827 | /* Release the reference returned by the look-up, and then release the global reference. */ | |
1828 | session_info_put(session); | |
1829 | session_info_put(session); | |
1830 | ret = 0; | |
1831 | *cmd_result = LTTNG_OK; | |
1832 | end: | |
1833 | return ret; | |
1834 | } | |
1835 | ||
28ab034a | 1836 | static void free_channel_trigger_list_rcu(struct rcu_head *node) |
83b934ad | 1837 | { |
28ab034a | 1838 | free(caa_container_of(node, struct lttng_channel_trigger_list, rcu_node)); |
83b934ad MD |
1839 | } |
1840 | ||
28ab034a | 1841 | static void free_channel_state_sample_rcu(struct rcu_head *node) |
83b934ad | 1842 | { |
28ab034a | 1843 | free(caa_container_of(node, struct channel_state_sample, rcu_node)); |
83b934ad MD |
1844 | } |
1845 | ||
28ab034a JG |
1846 | static int |
1847 | handle_notification_thread_command_remove_channel(struct notification_thread_state *state, | |
1848 | uint64_t channel_key, | |
1849 | enum lttng_domain_type domain, | |
1850 | enum lttng_error_code *cmd_result) | |
ab0ee2ca JG |
1851 | { |
1852 | struct cds_lfht_node *node; | |
1853 | struct cds_lfht_iter iter; | |
1854 | struct lttng_channel_trigger_list *trigger_list; | |
1855 | struct lttng_trigger_list_element *trigger_list_element, *tmp; | |
1856 | struct channel_key key = { .key = channel_key, .domain = domain }; | |
1857 | struct channel_info *channel_info; | |
1858 | ||
bd0514a5 | 1859 | DBG("Removing channel key = %" PRIu64 " in %s domain", |
28ab034a JG |
1860 | channel_key, |
1861 | lttng_domain_type_str(domain)); | |
ab0ee2ca | 1862 | |
07c4863f | 1863 | const lttng::urcu::read_lock_guard read_lock; |
ab0ee2ca JG |
1864 | |
1865 | cds_lfht_lookup(state->channel_triggers_ht, | |
1866 | hash_channel_key(&key), | |
1867 | match_channel_trigger_list, | |
1868 | &key, | |
1869 | &iter); | |
1870 | node = cds_lfht_iter_get_node(&iter); | |
1871 | /* | |
1872 | * There is a severe internal error if we are being asked to remove a | |
1873 | * channel that doesn't exist. | |
1874 | */ | |
1875 | if (!node) { | |
bd0514a5 | 1876 | ERR("Channel being removed is unknown to the notification thread"); |
ab0ee2ca JG |
1877 | goto end; |
1878 | } | |
1879 | ||
1880 | /* Free the list of triggers associated with this channel. */ | |
28ab034a JG |
1881 | trigger_list = |
1882 | caa_container_of(node, struct lttng_channel_trigger_list, channel_triggers_ht_node); | |
1883 | cds_list_for_each_entry_safe (trigger_list_element, tmp, &trigger_list->list, node) { | |
ab0ee2ca JG |
1884 | cds_list_del(&trigger_list_element->node); |
1885 | free(trigger_list_element); | |
1886 | } | |
1887 | cds_lfht_del(state->channel_triggers_ht, node); | |
83b934ad | 1888 | call_rcu(&trigger_list->rcu_node, free_channel_trigger_list_rcu); |
ab0ee2ca JG |
1889 | |
1890 | /* Free sampled channel state. */ | |
1891 | cds_lfht_lookup(state->channel_state_ht, | |
1892 | hash_channel_key(&key), | |
1893 | match_channel_state_sample, | |
1894 | &key, | |
1895 | &iter); | |
1896 | node = cds_lfht_iter_get_node(&iter); | |
1897 | /* | |
1898 | * This is expected to be NULL if the channel is destroyed before we | |
1899 | * received a sample. | |
1900 | */ | |
1901 | if (node) { | |
28ab034a JG |
1902 | struct channel_state_sample *sample = |
1903 | caa_container_of(node, struct channel_state_sample, channel_state_ht_node); | |
ab0ee2ca JG |
1904 | |
1905 | cds_lfht_del(state->channel_state_ht, node); | |
83b934ad | 1906 | call_rcu(&sample->rcu_node, free_channel_state_sample_rcu); |
ab0ee2ca JG |
1907 | } |
1908 | ||
1909 | /* Remove the channel from the channels_ht and free it. */ | |
28ab034a JG |
1910 | cds_lfht_lookup( |
1911 | state->channels_ht, hash_channel_key(&key), match_channel_info, &key, &iter); | |
ab0ee2ca | 1912 | node = cds_lfht_iter_get_node(&iter); |
a0377dfe | 1913 | LTTNG_ASSERT(node); |
28ab034a | 1914 | channel_info = caa_container_of(node, struct channel_info, channels_ht_node); |
ab0ee2ca JG |
1915 | cds_lfht_del(state->channels_ht, node); |
1916 | channel_info_destroy(channel_info); | |
1917 | end: | |
ab0ee2ca JG |
1918 | *cmd_result = LTTNG_OK; |
1919 | return 0; | |
1920 | } | |
1921 | ||
28ab034a JG |
1922 | static int |
1923 | handle_notification_thread_command_session_rotation(struct notification_thread_state *state, | |
1924 | enum notification_thread_command_type cmd_type, | |
1925 | uint64_t session_id, | |
1926 | uint64_t trace_archive_chunk_id, | |
1927 | struct lttng_trace_archive_location *location, | |
1928 | enum lttng_error_code *_cmd_result) | |
0ca52944 | 1929 | { |
ed327204 JG |
1930 | int ret = 0; |
1931 | enum lttng_error_code cmd_result = LTTNG_OK; | |
1932 | struct lttng_session_trigger_list *trigger_list; | |
1933 | struct lttng_trigger_list_element *trigger_list_element; | |
1934 | struct session_info *session_info; | |
139a8d25 | 1935 | struct lttng_credentials session_creds; |
319dcddc | 1936 | struct session_state_sample new_session_state; |
0ca52944 | 1937 | |
07c4863f | 1938 | const lttng::urcu::read_lock_guard read_lock; |
ed327204 | 1939 | |
139a8d25 | 1940 | session_info = get_session_info_by_id(state, session_id); |
ed327204 | 1941 | if (!session_info) { |
139a8d25 JG |
1942 | /* Fatal logic error. */ |
1943 | ERR("Failed to find session while handling rotation state change: session id = %" PRIu64, | |
28ab034a | 1944 | session_id); |
ed327204 | 1945 | ret = -1; |
139a8d25 | 1946 | cmd_result = LTTNG_ERR_FATAL; |
ed327204 JG |
1947 | goto end; |
1948 | } | |
1949 | ||
319dcddc JG |
1950 | new_session_state = session_info->last_state_sample; |
1951 | if (location) { | |
1952 | lttng_trace_archive_location_get(location); | |
1953 | new_session_state.rotation.location = location; | |
1954 | } else { | |
cd9adb8b | 1955 | new_session_state.rotation.location = nullptr; |
319dcddc JG |
1956 | } |
1957 | ||
139a8d25 JG |
1958 | session_creds = { |
1959 | .uid = LTTNG_OPTIONAL_INIT_VALUE(session_info->uid), | |
1960 | .gid = LTTNG_OPTIONAL_INIT_VALUE(session_info->gid), | |
1961 | }; | |
1962 | ||
319dcddc | 1963 | new_session_state.rotation.ongoing = cmd_type == |
28ab034a | 1964 | NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING; |
319dcddc JG |
1965 | new_session_state.rotation.id = trace_archive_chunk_id; |
1966 | ||
139a8d25 | 1967 | trigger_list = get_session_trigger_list(state, session_info->name); |
319dcddc | 1968 | LTTNG_ASSERT(trigger_list); |
ed327204 | 1969 | |
28ab034a | 1970 | cds_list_for_each_entry (trigger_list_element, &trigger_list->list, node) { |
ed327204 | 1971 | const struct lttng_condition *condition; |
f2b3ef9f | 1972 | struct lttng_trigger *trigger; |
ed327204 | 1973 | struct notification_client_list *client_list; |
cd9adb8b | 1974 | struct lttng_evaluation *evaluation = nullptr; |
f2b3ef9f | 1975 | enum action_executor_status executor_status; |
ed327204 JG |
1976 | |
1977 | trigger = trigger_list_element->trigger; | |
1978 | condition = lttng_trigger_get_const_condition(trigger); | |
a0377dfe | 1979 | LTTNG_ASSERT(condition); |
ed327204 | 1980 | |
319dcddc | 1981 | ret = evaluate_session_condition( |
28ab034a | 1982 | condition, session_info, &new_session_state, &evaluation); |
319dcddc JG |
1983 | if (ret) { |
1984 | ret = -1; | |
1985 | cmd_result = LTTNG_ERR_NOMEM; | |
1986 | goto end; | |
ed327204 JG |
1987 | } |
1988 | ||
1989 | if (!evaluation) { | |
319dcddc | 1990 | continue; |
ed327204 JG |
1991 | } |
1992 | ||
f2b3ef9f JG |
1993 | /* |
1994 | * Ownership of `evaluation` transferred to the action executor | |
319dcddc JG |
1995 | * no matter the result. The callee acquires a reference to the |
1996 | * client list: we can release our own. | |
f2b3ef9f | 1997 | */ |
319dcddc | 1998 | client_list = get_client_list_from_condition(state, condition); |
72365501 | 1999 | executor_status = action_executor_enqueue_trigger( |
28ab034a | 2000 | state->executor, trigger, evaluation, &session_creds, client_list); |
319dcddc | 2001 | notification_client_list_put(client_list); |
cd9adb8b | 2002 | evaluation = nullptr; |
f2b3ef9f JG |
2003 | switch (executor_status) { |
2004 | case ACTION_EXECUTOR_STATUS_OK: | |
2005 | break; | |
2006 | case ACTION_EXECUTOR_STATUS_ERROR: | |
2007 | case ACTION_EXECUTOR_STATUS_INVALID: | |
2008 | /* | |
2009 | * TODO Add trigger identification (name/id) when | |
2010 | * it is added to the API. | |
2011 | */ | |
2012 | ERR("Fatal error occurred while enqueuing action associated with session rotation trigger"); | |
2013 | ret = -1; | |
319dcddc | 2014 | goto end; |
f2b3ef9f JG |
2015 | case ACTION_EXECUTOR_STATUS_OVERFLOW: |
2016 | /* | |
2017 | * TODO Add trigger identification (name/id) when | |
2018 | * it is added to the API. | |
2019 | * | |
2020 | * Not a fatal error. | |
2021 | */ | |
2022 | WARN("No space left when enqueuing action associated with session rotation trigger"); | |
2023 | ret = 0; | |
319dcddc | 2024 | goto end; |
f2b3ef9f JG |
2025 | default: |
2026 | abort(); | |
2027 | } | |
ed327204 | 2028 | } |
319dcddc | 2029 | |
ed327204 | 2030 | end: |
319dcddc JG |
2031 | if (session_info) { |
2032 | /* Ownership of new_session_state::location is transferred. */ | |
2033 | lttng_trace_archive_location_put(session_info->last_state_sample.rotation.location); | |
2034 | session_info->last_state_sample = new_session_state; | |
2035 | } | |
2036 | ||
ed327204 JG |
2037 | session_info_put(session_info); |
2038 | *_cmd_result = cmd_result; | |
ed327204 | 2039 | return ret; |
0ca52944 JG |
2040 | } |
2041 | ||
28ab034a JG |
2042 | static int |
2043 | handle_notification_thread_command_add_tracer_event_source(struct notification_thread_state *state, | |
2044 | int tracer_event_source_fd, | |
2045 | enum lttng_domain_type domain_type, | |
2046 | enum lttng_error_code *_cmd_result) | |
d02d7404 JR |
2047 | { |
2048 | int ret = 0; | |
2049 | enum lttng_error_code cmd_result = LTTNG_OK; | |
cd9adb8b | 2050 | struct notification_event_tracer_event_source_element *element = nullptr; |
d02d7404 | 2051 | |
64803277 | 2052 | element = zmalloc<notification_event_tracer_event_source_element>(); |
d02d7404 JR |
2053 | if (!element) { |
2054 | cmd_result = LTTNG_ERR_NOMEM; | |
2055 | ret = -1; | |
2056 | goto end; | |
2057 | } | |
2058 | ||
d02d7404 JR |
2059 | element->fd = tracer_event_source_fd; |
2060 | element->domain = domain_type; | |
2061 | ||
2062 | cds_list_add(&element->node, &state->tracer_event_sources_list); | |
2063 | ||
bd0514a5 | 2064 | DBG3("Adding tracer event source fd to poll set: tracer_event_source_fd = %d, domain = '%s'", |
28ab034a JG |
2065 | tracer_event_source_fd, |
2066 | lttng_domain_type_str(domain_type)); | |
d02d7404 JR |
2067 | |
2068 | /* Adding the read side pipe to the event poll. */ | |
1524f98c | 2069 | ret = lttng_poll_add(&state->events, tracer_event_source_fd, LPOLLPRI | LPOLLIN); |
d02d7404 | 2070 | if (ret < 0) { |
bd0514a5 | 2071 | ERR("Failed to add tracer event source to poll set: tracer_event_source_fd = %d, domain = '%s'", |
28ab034a JG |
2072 | tracer_event_source_fd, |
2073 | lttng_domain_type_str(element->domain)); | |
d02d7404 JR |
2074 | cds_list_del(&element->node); |
2075 | free(element); | |
2076 | goto end; | |
2077 | } | |
2078 | ||
2079 | element->is_fd_in_poll_set = true; | |
2080 | ||
2081 | end: | |
2082 | *_cmd_result = cmd_result; | |
2083 | return ret; | |
2084 | } | |
2085 | ||
28ab034a JG |
2086 | static int drain_event_notifier_notification_pipe(struct notification_thread_state *state, |
2087 | int pipe, | |
2088 | enum lttng_domain_type domain) | |
8b524060 | 2089 | { |
1c9a0b0e | 2090 | struct lttng_poll_event events = {}; |
8b524060 FD |
2091 | int ret; |
2092 | ||
2093 | ret = lttng_poll_create(&events, 1, LTTNG_CLOEXEC); | |
2094 | if (ret < 0) { | |
bd0514a5 | 2095 | ERR("Error creating lttng_poll_event"); |
8b524060 FD |
2096 | goto end; |
2097 | } | |
2098 | ||
2099 | ret = lttng_poll_add(&events, pipe, LPOLLIN); | |
2100 | if (ret < 0) { | |
bd0514a5 | 2101 | ERR("Error adding fd event notifier notification pipe to lttng_poll_event: fd = %d", |
28ab034a | 2102 | pipe); |
8b524060 FD |
2103 | goto end; |
2104 | } | |
2105 | ||
2106 | while (true) { | |
2107 | /* | |
2108 | * Continue to consume notifications as long as there are new | |
2109 | * ones coming in. The tracer has been asked to stop producing | |
2110 | * them. | |
2111 | * | |
2112 | * LPOLLIN is explicitly checked since LPOLLHUP is implicitly | |
2113 | * monitored (on Linux, at least) and will be returned when | |
2114 | * the pipe is closed but empty. | |
2115 | */ | |
2116 | ret = lttng_poll_wait_interruptible(&events, 0); | |
a17b133b | 2117 | if (ret == 0 || (LTTNG_POLL_GETEV(&events, 0) & LPOLLIN) == 0) { |
8b524060 FD |
2118 | /* No more notification to be read on this pipe. */ |
2119 | ret = 0; | |
2120 | goto end; | |
2121 | } else if (ret < 0) { | |
2122 | PERROR("Failed on lttng_poll_wait_interruptible() call"); | |
2123 | ret = -1; | |
2124 | goto end; | |
2125 | } | |
2126 | ||
2127 | ret = handle_one_event_notifier_notification(state, pipe, domain); | |
2128 | if (ret) { | |
bd0514a5 | 2129 | ERR("Error consuming an event notifier notification from pipe: fd = %d", |
28ab034a | 2130 | pipe); |
8b524060 FD |
2131 | } |
2132 | } | |
2133 | end: | |
2134 | lttng_poll_clean(&events); | |
2135 | return ret; | |
2136 | } | |
2137 | ||
28ab034a | 2138 | static struct notification_event_tracer_event_source_element * |
34bf4f69 | 2139 | find_tracer_event_source_element(struct notification_thread_state *state, |
28ab034a | 2140 | int tracer_event_source_fd) |
d02d7404 | 2141 | { |
34bf4f69 | 2142 | struct notification_event_tracer_event_source_element *source_element; |
d02d7404 | 2143 | |
28ab034a | 2144 | cds_list_for_each_entry (source_element, &state->tracer_event_sources_list, node) { |
34bf4f69 FD |
2145 | if (source_element->fd == tracer_event_source_fd) { |
2146 | goto end; | |
d02d7404 | 2147 | } |
d02d7404 JR |
2148 | } |
2149 | ||
cd9adb8b | 2150 | source_element = nullptr; |
34bf4f69 | 2151 | end: |
97cf926d | 2152 | return source_element; |
34bf4f69 | 2153 | } |
d02d7404 | 2154 | |
28ab034a JG |
2155 | static int remove_tracer_event_source_from_pollset( |
2156 | struct notification_thread_state *state, | |
2157 | struct notification_event_tracer_event_source_element *source_element) | |
34bf4f69 FD |
2158 | { |
2159 | int ret = 0; | |
2160 | ||
a0377dfe | 2161 | LTTNG_ASSERT(source_element->is_fd_in_poll_set); |
d02d7404 | 2162 | |
bd0514a5 | 2163 | DBG3("Removing tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'", |
28ab034a JG |
2164 | source_element->fd, |
2165 | lttng_domain_type_str(source_element->domain)); | |
d02d7404 JR |
2166 | |
2167 | /* Removing the fd from the event poll set. */ | |
34bf4f69 | 2168 | ret = lttng_poll_del(&state->events, source_element->fd); |
d02d7404 | 2169 | if (ret < 0) { |
bd0514a5 | 2170 | ERR("Failed to remove tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'", |
28ab034a JG |
2171 | source_element->fd, |
2172 | lttng_domain_type_str(source_element->domain)); | |
34bf4f69 | 2173 | ret = -1; |
d02d7404 JR |
2174 | goto end; |
2175 | } | |
2176 | ||
173900f6 JG |
2177 | source_element->is_fd_in_poll_set = false; |
2178 | ||
2ddbdc7a FD |
2179 | /* |
2180 | * Force the notification thread to restart the poll() loop to ensure | |
2181 | * that any events from the removed fd are removed. | |
2182 | */ | |
2183 | state->restart_poll = true; | |
2184 | ||
28ab034a JG |
2185 | ret = drain_event_notifier_notification_pipe( |
2186 | state, source_element->fd, source_element->domain); | |
8b524060 | 2187 | if (ret) { |
bd0514a5 | 2188 | ERR("Error draining event notifier notification: tracer_event_source_fd = %d, domain = %s", |
28ab034a JG |
2189 | source_element->fd, |
2190 | lttng_domain_type_str(source_element->domain)); | |
34bf4f69 | 2191 | ret = -1; |
8b524060 FD |
2192 | goto end; |
2193 | } | |
2194 | ||
d02d7404 | 2195 | end: |
d02d7404 JR |
2196 | return ret; |
2197 | } | |
2198 | ||
28ab034a JG |
2199 | int handle_notification_thread_tracer_event_source_died(struct notification_thread_state *state, |
2200 | int tracer_event_source_fd) | |
d02d7404 | 2201 | { |
34bf4f69 FD |
2202 | int ret = 0; |
2203 | struct notification_event_tracer_event_source_element *source_element; | |
2204 | ||
28ab034a | 2205 | source_element = find_tracer_event_source_element(state, tracer_event_source_fd); |
34bf4f69 | 2206 | |
a0377dfe | 2207 | LTTNG_ASSERT(source_element); |
34bf4f69 FD |
2208 | |
2209 | ret = remove_tracer_event_source_from_pollset(state, source_element); | |
2210 | if (ret) { | |
2211 | ERR("Failed to remove dead tracer event source from poll set"); | |
2212 | } | |
d02d7404 | 2213 | |
34bf4f69 FD |
2214 | return ret; |
2215 | } | |
2216 | ||
28ab034a JG |
2217 | static int handle_notification_thread_command_remove_tracer_event_source( |
2218 | struct notification_thread_state *state, | |
2219 | int tracer_event_source_fd, | |
2220 | enum lttng_error_code *_cmd_result) | |
34bf4f69 FD |
2221 | { |
2222 | int ret = 0; | |
2223 | enum lttng_error_code cmd_result = LTTNG_OK; | |
cd9adb8b | 2224 | struct notification_event_tracer_event_source_element *source_element = nullptr; |
34bf4f69 | 2225 | |
28ab034a | 2226 | source_element = find_tracer_event_source_element(state, tracer_event_source_fd); |
34bf4f69 | 2227 | |
a0377dfe | 2228 | LTTNG_ASSERT(source_element); |
34bf4f69 FD |
2229 | |
2230 | /* Remove the tracer source from the list. */ | |
2231 | cds_list_del(&source_element->node); | |
2232 | ||
2233 | if (!source_element->is_fd_in_poll_set) { | |
2234 | /* Skip the poll set removal. */ | |
2235 | goto end; | |
2236 | } | |
2237 | ||
2238 | ret = remove_tracer_event_source_from_pollset(state, source_element); | |
2239 | if (ret) { | |
2240 | ERR("Failed to remove tracer event source from poll set"); | |
2241 | cmd_result = LTTNG_ERR_FATAL; | |
2242 | } | |
2243 | ||
2244 | end: | |
2245 | free(source_element); | |
2246 | *_cmd_result = cmd_result; | |
d02d7404 JR |
2247 | return ret; |
2248 | } | |
2249 | ||
28ab034a JG |
2250 | static int |
2251 | handle_notification_thread_command_list_triggers(struct notification_thread_handle *handle | |
2252 | __attribute__((unused)), | |
2253 | struct notification_thread_state *state, | |
2254 | uid_t client_uid, | |
2255 | struct lttng_triggers **triggers, | |
2256 | enum lttng_error_code *_cmd_result) | |
fbc9f37d JR |
2257 | { |
2258 | int ret = 0; | |
2259 | enum lttng_error_code cmd_result = LTTNG_OK; | |
cd9adb8b | 2260 | struct lttng_triggers *local_triggers = nullptr; |
fbc9f37d JR |
2261 | const struct lttng_credentials *creds; |
2262 | ||
fbc9f37d JR |
2263 | local_triggers = lttng_triggers_create(); |
2264 | if (!local_triggers) { | |
2265 | /* Not a fatal error. */ | |
2266 | cmd_result = LTTNG_ERR_NOMEM; | |
2267 | goto end; | |
2268 | } | |
2269 | ||
6e7ffe66 JG |
2270 | for (auto *trigger_ht_element : |
2271 | lttng::urcu::lfht_iteration_adapter<lttng_trigger_ht_element, | |
2272 | decltype(lttng_trigger_ht_element::node), | |
2273 | <tng_trigger_ht_element::node>( | |
2274 | *state->triggers_ht)) { | |
fbc9f37d JR |
2275 | /* |
2276 | * Only return the triggers to which the client has access. | |
2277 | * The root user has visibility over all triggers. | |
2278 | */ | |
2279 | creds = lttng_trigger_get_credentials(trigger_ht_element->trigger); | |
2280 | if (client_uid != lttng_credentials_get_uid(creds) && client_uid != 0) { | |
2281 | continue; | |
2282 | } | |
2283 | ||
28ab034a | 2284 | ret = lttng_triggers_add(local_triggers, trigger_ht_element->trigger); |
fbc9f37d JR |
2285 | if (ret < 0) { |
2286 | /* Not a fatal error. */ | |
2287 | ret = 0; | |
2288 | cmd_result = LTTNG_ERR_NOMEM; | |
2289 | goto end; | |
2290 | } | |
2291 | } | |
2292 | ||
2293 | /* Transferring ownership to the caller. */ | |
2294 | *triggers = local_triggers; | |
cd9adb8b | 2295 | local_triggers = nullptr; |
fbc9f37d JR |
2296 | |
2297 | end: | |
fbc9f37d JR |
2298 | lttng_triggers_destroy(local_triggers); |
2299 | *_cmd_result = cmd_result; | |
2300 | return ret; | |
2301 | } | |
2302 | ||
8790759c | 2303 | static inline void get_trigger_info_for_log(const struct lttng_trigger *trigger, |
28ab034a JG |
2304 | const char **trigger_name, |
2305 | uid_t *trigger_owner_uid) | |
8790759c FD |
2306 | { |
2307 | enum lttng_trigger_status trigger_status; | |
2308 | ||
2309 | trigger_status = lttng_trigger_get_name(trigger, trigger_name); | |
2310 | switch (trigger_status) { | |
2311 | case LTTNG_TRIGGER_STATUS_OK: | |
2312 | break; | |
2313 | case LTTNG_TRIGGER_STATUS_UNSET: | |
0efb2ad7 | 2314 | *trigger_name = "(anonymous)"; |
8790759c FD |
2315 | break; |
2316 | default: | |
2317 | abort(); | |
2318 | } | |
2319 | ||
28ab034a | 2320 | trigger_status = lttng_trigger_get_owner_uid(trigger, trigger_owner_uid); |
a0377dfe | 2321 | LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK); |
8790759c FD |
2322 | } |
2323 | ||
28ab034a JG |
2324 | static int handle_notification_thread_command_get_trigger(struct notification_thread_state *state, |
2325 | const struct lttng_trigger *trigger, | |
2326 | struct lttng_trigger **registered_trigger, | |
2327 | enum lttng_error_code *_cmd_result) | |
8790759c FD |
2328 | { |
2329 | int ret = -1; | |
8790759c FD |
2330 | enum lttng_error_code cmd_result = LTTNG_ERR_TRIGGER_NOT_FOUND; |
2331 | const char *trigger_name; | |
2332 | uid_t trigger_owner_uid; | |
2333 | ||
6e7ffe66 JG |
2334 | for (auto *trigger_ht_element : |
2335 | lttng::urcu::lfht_iteration_adapter<lttng_trigger_ht_element, | |
2336 | decltype(lttng_trigger_ht_element::node), | |
2337 | <tng_trigger_ht_element::node>( | |
2338 | *state->triggers_ht)) { | |
2339 | if (lttng_trigger_is_equal(trigger, trigger_ht_element->trigger)) { | |
2340 | /* Take one reference on the return trigger. */ | |
2341 | *registered_trigger = trigger_ht_element->trigger; | |
2342 | lttng_trigger_get(*registered_trigger); | |
2343 | ret = 0; | |
2344 | cmd_result = LTTNG_OK; | |
2345 | goto end; | |
8790759c FD |
2346 | } |
2347 | } | |
2348 | ||
2349 | /* Not a fatal error if the trigger is not found. */ | |
2350 | get_trigger_info_for_log(trigger, &trigger_name, &trigger_owner_uid); | |
87661585 | 2351 | DBG("Failed to retrieve registered version of trigger: trigger name = '%s', trigger owner uid = %d", |
28ab034a JG |
2352 | trigger_name, |
2353 | (int) trigger_owner_uid); | |
8790759c FD |
2354 | |
2355 | ret = 0; | |
2356 | ||
2357 | end: | |
8790759c FD |
2358 | *_cmd_result = cmd_result; |
2359 | return ret; | |
2360 | } | |
2361 | ||
28ab034a | 2362 | static bool condition_is_supported(struct lttng_condition *condition) |
1da26331 | 2363 | { |
959e3c66 | 2364 | bool is_supported; |
1da26331 JG |
2365 | |
2366 | switch (lttng_condition_get_type(condition)) { | |
2367 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
2368 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
2369 | { | |
959e3c66 | 2370 | int ret; |
1da26331 JG |
2371 | enum lttng_domain_type domain; |
2372 | ||
28ab034a | 2373 | ret = lttng_condition_buffer_usage_get_domain_type(condition, &domain); |
a0377dfe | 2374 | LTTNG_ASSERT(ret == 0); |
1da26331 JG |
2375 | |
2376 | if (domain != LTTNG_DOMAIN_KERNEL) { | |
959e3c66 | 2377 | is_supported = true; |
1da26331 JG |
2378 | goto end; |
2379 | } | |
2380 | ||
2381 | /* | |
2382 | * Older kernel tracers don't expose the API to monitor their | |
2383 | * buffers. Therefore, we reject triggers that require that | |
2384 | * mechanism to be available to be evaluated. | |
959e3c66 JR |
2385 | * |
2386 | * Assume unsupported on error. | |
1da26331 | 2387 | */ |
959e3c66 JR |
2388 | is_supported = kernel_supports_ring_buffer_snapshot_sample_positions() == 1; |
2389 | break; | |
2390 | } | |
8dbb86b8 | 2391 | case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES: |
959e3c66 JR |
2392 | { |
2393 | const struct lttng_event_rule *event_rule; | |
2394 | enum lttng_domain_type domain; | |
2395 | const enum lttng_condition_status status = | |
28ab034a | 2396 | lttng_condition_event_rule_matches_get_rule(condition, &event_rule); |
959e3c66 | 2397 | |
a0377dfe | 2398 | LTTNG_ASSERT(status == LTTNG_CONDITION_STATUS_OK); |
959e3c66 JR |
2399 | |
2400 | domain = lttng_event_rule_get_domain_type(event_rule); | |
2401 | if (domain != LTTNG_DOMAIN_KERNEL) { | |
2402 | is_supported = true; | |
2403 | goto end; | |
2404 | } | |
2405 | ||
2406 | /* | |
2407 | * Older kernel tracers can't emit notification. Therefore, we | |
2408 | * reject triggers that require that mechanism to be available | |
2409 | * to be evaluated. | |
2410 | * | |
2411 | * Assume unsupported on error. | |
2412 | */ | |
2413 | is_supported = kernel_supports_event_notifiers() == 1; | |
1da26331 JG |
2414 | break; |
2415 | } | |
2416 | default: | |
959e3c66 | 2417 | is_supported = true; |
1da26331 JG |
2418 | } |
2419 | end: | |
959e3c66 | 2420 | return is_supported; |
1da26331 JG |
2421 | } |
2422 | ||
51eab943 | 2423 | /* Must be called with RCU read lock held. */ |
28ab034a JG |
2424 | static int bind_trigger_to_matching_session(struct lttng_trigger *trigger, |
2425 | struct notification_thread_state *state) | |
51eab943 JG |
2426 | { |
2427 | int ret = 0; | |
51eab943 JG |
2428 | const struct lttng_condition *condition; |
2429 | const char *session_name; | |
2430 | struct lttng_session_trigger_list *trigger_list; | |
2431 | ||
48b7cdc2 FD |
2432 | ASSERT_RCU_READ_LOCKED(); |
2433 | ||
51eab943 | 2434 | condition = lttng_trigger_get_const_condition(trigger); |
319dcddc | 2435 | session_name = get_condition_session_name(condition); |
51eab943 | 2436 | |
ed327204 JG |
2437 | trigger_list = get_session_trigger_list(state, session_name); |
2438 | if (!trigger_list) { | |
bd0514a5 | 2439 | DBG("Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system", |
28ab034a | 2440 | session_name); |
51eab943 | 2441 | goto end; |
ed327204 | 2442 | } |
51eab943 | 2443 | |
28ab034a | 2444 | DBG("Newly registered trigger bound to session \"%s\"", session_name); |
51eab943 JG |
2445 | ret = lttng_session_trigger_list_add(trigger_list, trigger); |
2446 | end: | |
2447 | return ret; | |
2448 | } | |
2449 | ||
2450 | /* Must be called with RCU read lock held. */ | |
28ab034a JG |
2451 | static int bind_trigger_to_matching_channels(struct lttng_trigger *trigger, |
2452 | struct notification_thread_state *state) | |
51eab943 JG |
2453 | { |
2454 | int ret = 0; | |
2455 | struct cds_lfht_node *node; | |
51eab943 | 2456 | |
6e7ffe66 JG |
2457 | for (auto *channel : |
2458 | lttng::urcu::lfht_iteration_adapter<channel_info, | |
2459 | decltype(channel_info::channels_ht_node), | |
2460 | &channel_info::channels_ht_node>( | |
2461 | *state->channels_ht)) { | |
51eab943 JG |
2462 | struct lttng_trigger_list_element *trigger_list_element; |
2463 | struct lttng_channel_trigger_list *trigger_list; | |
a5d64ae7 | 2464 | struct cds_lfht_iter lookup_iter; |
51eab943 JG |
2465 | |
2466 | if (!trigger_applies_to_channel(trigger, channel)) { | |
2467 | continue; | |
2468 | } | |
2469 | ||
2470 | cds_lfht_lookup(state->channel_triggers_ht, | |
2471 | hash_channel_key(&channel->key), | |
2472 | match_channel_trigger_list, | |
2473 | &channel->key, | |
a5d64ae7 MD |
2474 | &lookup_iter); |
2475 | node = cds_lfht_iter_get_node(&lookup_iter); | |
a0377dfe | 2476 | LTTNG_ASSERT(node); |
28ab034a JG |
2477 | trigger_list = caa_container_of( |
2478 | node, struct lttng_channel_trigger_list, channel_triggers_ht_node); | |
51eab943 | 2479 | |
64803277 | 2480 | trigger_list_element = zmalloc<lttng_trigger_list_element>(); |
51eab943 JG |
2481 | if (!trigger_list_element) { |
2482 | ret = -1; | |
2483 | goto end; | |
2484 | } | |
2485 | CDS_INIT_LIST_HEAD(&trigger_list_element->node); | |
2486 | trigger_list_element->trigger = trigger; | |
2487 | cds_list_add(&trigger_list_element->node, &trigger_list->list); | |
28ab034a | 2488 | DBG("Newly registered trigger bound to channel \"%s\"", channel->name); |
51eab943 JG |
2489 | } |
2490 | end: | |
2491 | return ret; | |
2492 | } | |
2493 | ||
28ab034a | 2494 | static bool is_trigger_action_notify(const struct lttng_trigger *trigger) |
f2b3ef9f JG |
2495 | { |
2496 | bool is_notify = false; | |
28ab034a | 2497 | const struct lttng_action *action = lttng_trigger_get_const_action(trigger); |
f2b3ef9f JG |
2498 | enum lttng_action_type action_type; |
2499 | ||
a0377dfe | 2500 | LTTNG_ASSERT(action); |
17182cfd | 2501 | action_type = lttng_action_get_type(action); |
f2b3ef9f JG |
2502 | if (action_type == LTTNG_ACTION_TYPE_NOTIFY) { |
2503 | is_notify = true; | |
2504 | goto end; | |
7c2fae7c | 2505 | } else if (action_type != LTTNG_ACTION_TYPE_LIST) { |
f2b3ef9f JG |
2506 | goto end; |
2507 | } | |
2508 | ||
b17ed2ad | 2509 | for (auto inner_action : lttng::ctl::const_action_list_view(action)) { |
2460203a | 2510 | if (lttng_action_get_type(inner_action) == LTTNG_ACTION_TYPE_NOTIFY) { |
f2b3ef9f JG |
2511 | is_notify = true; |
2512 | goto end; | |
2513 | } | |
2514 | } | |
2515 | ||
2516 | end: | |
2517 | return is_notify; | |
2518 | } | |
2519 | ||
242388e4 | 2520 | static bool trigger_name_taken(struct notification_thread_state *state, |
28ab034a | 2521 | const struct lttng_trigger *trigger) |
242388e4 JR |
2522 | { |
2523 | struct cds_lfht_iter iter; | |
2524 | ||
2525 | /* | |
2526 | * No duplicata is allowed in the triggers_by_name_uid_ht. | |
2527 | * The match is done against the trigger name and uid. | |
2528 | */ | |
2529 | cds_lfht_lookup(state->triggers_by_name_uid_ht, | |
2530 | hash_trigger_by_name_uid(trigger), | |
2531 | match_trigger_by_name_uid, | |
2532 | trigger, | |
2533 | &iter); | |
2534 | return !!cds_lfht_iter_get_node(&iter); | |
2535 | } | |
2536 | ||
28ab034a JG |
2537 | static enum lttng_error_code generate_trigger_name(struct notification_thread_state *state, |
2538 | struct lttng_trigger *trigger, | |
2539 | const char **name) | |
242388e4 JR |
2540 | { |
2541 | enum lttng_error_code ret_code = LTTNG_OK; | |
2542 | bool taken = false; | |
2543 | enum lttng_trigger_status status; | |
2544 | ||
2545 | do { | |
28ab034a JG |
2546 | const int ret = |
2547 | lttng_trigger_generate_name(trigger, state->trigger_id.name_offset++); | |
242388e4 JR |
2548 | if (ret) { |
2549 | /* The only reason this can fail right now. */ | |
2550 | ret_code = LTTNG_ERR_NOMEM; | |
2551 | break; | |
2552 | } | |
2553 | ||
2554 | status = lttng_trigger_get_name(trigger, name); | |
a0377dfe | 2555 | LTTNG_ASSERT(status == LTTNG_TRIGGER_STATUS_OK); |
242388e4 JR |
2556 | |
2557 | taken = trigger_name_taken(state, trigger); | |
2558 | } while (taken || state->trigger_id.name_offset == UINT64_MAX); | |
2559 | ||
2560 | return ret_code; | |
2561 | } | |
2562 | ||
28ab034a JG |
2563 | static inline void |
2564 | notif_thread_state_remove_trigger_ht_elem(struct notification_thread_state *state, | |
2565 | struct lttng_trigger_ht_element *trigger_ht_element) | |
2758e38b | 2566 | { |
a0377dfe FD |
2567 | LTTNG_ASSERT(state); |
2568 | LTTNG_ASSERT(trigger_ht_element); | |
2758e38b FD |
2569 | |
2570 | cds_lfht_del(state->triggers_ht, &trigger_ht_element->node); | |
2571 | cds_lfht_del(state->triggers_by_name_uid_ht, &trigger_ht_element->node_by_name_uid); | |
2572 | } | |
2573 | ||
28ab034a JG |
2574 | static enum lttng_error_code setup_tracer_notifier(struct notification_thread_state *state, |
2575 | struct lttng_trigger *trigger) | |
6487ad53 FD |
2576 | { |
2577 | enum lttng_error_code ret; | |
2578 | enum event_notifier_error_accounting_status error_accounting_status; | |
2579 | struct cds_lfht_node *node; | |
2580 | uint64_t error_counter_index = 0; | |
2581 | struct lttng_condition *condition = lttng_trigger_get_condition(trigger); | |
cd9adb8b | 2582 | struct notification_trigger_tokens_ht_element *trigger_tokens_ht_element = nullptr; |
6487ad53 | 2583 | |
64803277 | 2584 | trigger_tokens_ht_element = zmalloc<notification_trigger_tokens_ht_element>(); |
6487ad53 FD |
2585 | if (!trigger_tokens_ht_element) { |
2586 | ret = LTTNG_ERR_NOMEM; | |
2587 | goto end; | |
2588 | } | |
2589 | ||
2590 | /* Add trigger token to the trigger_tokens_ht. */ | |
2591 | cds_lfht_node_init(&trigger_tokens_ht_element->node); | |
2592 | trigger_tokens_ht_element->token = LTTNG_OPTIONAL_GET(trigger->tracer_token); | |
2593 | trigger_tokens_ht_element->trigger = trigger; | |
2594 | ||
2595 | node = cds_lfht_add_unique(state->trigger_tokens_ht, | |
28ab034a JG |
2596 | hash_key_u64(&trigger_tokens_ht_element->token, lttng_ht_seed), |
2597 | match_trigger_token, | |
2598 | &trigger_tokens_ht_element->token, | |
2599 | &trigger_tokens_ht_element->node); | |
6487ad53 FD |
2600 | if (node != &trigger_tokens_ht_element->node) { |
2601 | ret = LTTNG_ERR_TRIGGER_EXISTS; | |
2602 | goto error_free_ht_element; | |
2603 | } | |
2604 | ||
2605 | error_accounting_status = event_notifier_error_accounting_register_event_notifier( | |
28ab034a | 2606 | trigger, &error_counter_index); |
6487ad53 | 2607 | if (error_accounting_status != EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK) { |
28ab034a JG |
2608 | if (error_accounting_status == |
2609 | EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NO_INDEX_AVAILABLE) { | |
7c2fae7c | 2610 | DBG("Trigger list error accounting counter full."); |
6487ad53 FD |
2611 | ret = LTTNG_ERR_EVENT_NOTIFIER_ERROR_ACCOUNTING_FULL; |
2612 | } else { | |
2613 | ERR("Error registering trigger for error accounting"); | |
2614 | ret = LTTNG_ERR_EVENT_NOTIFIER_REGISTRATION; | |
2615 | } | |
2616 | ||
2617 | goto error_remove_ht_element; | |
2618 | } | |
2619 | ||
28ab034a | 2620 | lttng_condition_event_rule_matches_set_error_counter_index(condition, error_counter_index); |
6487ad53 FD |
2621 | |
2622 | ret = LTTNG_OK; | |
2623 | goto end; | |
2624 | ||
2625 | error_remove_ht_element: | |
2626 | cds_lfht_del(state->trigger_tokens_ht, &trigger_tokens_ht_element->node); | |
2627 | error_free_ht_element: | |
2628 | free(trigger_tokens_ht_element); | |
2629 | end: | |
2630 | return ret; | |
2631 | } | |
2632 | ||
ab0ee2ca | 2633 | /* |
f2b3ef9f | 2634 | * FIXME A client's credentials are not checked when registering a trigger. |
ab0ee2ca | 2635 | * |
1da26331 | 2636 | * The effects of this are benign since: |
ab0ee2ca | 2637 | * - The client will succeed in registering the trigger, as it is valid, |
51eab943 | 2638 | * - The trigger will, internally, be bound to the channel/session, |
ab0ee2ca JG |
2639 | * - The notifications will not be sent since the client's credentials |
2640 | * are checked against the channel at that moment. | |
1da26331 JG |
2641 | * |
2642 | * If this function returns a non-zero value, it means something is | |
50ca7858 | 2643 | * fundamentally broken and the whole subsystem/thread will be torn down. |
1da26331 JG |
2644 | * |
2645 | * If a non-fatal error occurs, just set the cmd_result to the appropriate | |
2646 | * error code. | |
ab0ee2ca | 2647 | */ |
28ab034a JG |
2648 | static int |
2649 | handle_notification_thread_command_register_trigger(struct notification_thread_state *state, | |
2650 | struct lttng_trigger *trigger, | |
2651 | bool is_trigger_anonymous, | |
2652 | enum lttng_error_code *cmd_result) | |
ab0ee2ca JG |
2653 | { |
2654 | int ret = 0; | |
2655 | struct lttng_condition *condition; | |
cd9adb8b JG |
2656 | struct notification_client_list *client_list = nullptr; |
2657 | struct lttng_trigger_ht_element *trigger_ht_element = nullptr; | |
ab0ee2ca | 2658 | struct cds_lfht_node *node; |
28ab034a | 2659 | const char *trigger_name; |
ab0ee2ca | 2660 | bool free_trigger = true; |
cd9adb8b | 2661 | struct lttng_evaluation *evaluation = nullptr; |
f2b3ef9f | 2662 | struct lttng_credentials object_creds; |
ff588497 JR |
2663 | uid_t object_uid; |
2664 | gid_t object_gid; | |
f2b3ef9f | 2665 | enum action_executor_status executor_status; |
28ab034a | 2666 | const uint64_t trigger_tracer_token = state->trigger_id.next_tracer_token++; |
ab0ee2ca | 2667 | |
07c4863f | 2668 | const lttng::urcu::read_lock_guard read_lock; |
ab0ee2ca | 2669 | |
e6887944 JR |
2670 | /* Set the trigger's tracer token. */ |
2671 | lttng_trigger_set_tracer_token(trigger, trigger_tracer_token); | |
2672 | ||
0efb2ad7 | 2673 | if (!is_trigger_anonymous) { |
28ab034a | 2674 | if (lttng_trigger_get_name(trigger, &trigger_name) == LTTNG_TRIGGER_STATUS_UNSET) { |
0efb2ad7 | 2675 | const enum lttng_error_code ret_code = |
28ab034a | 2676 | generate_trigger_name(state, trigger, &trigger_name); |
242388e4 | 2677 | |
0efb2ad7 JG |
2678 | if (ret_code != LTTNG_OK) { |
2679 | /* Fatal error. */ | |
2680 | ret = -1; | |
2681 | *cmd_result = ret_code; | |
2682 | goto error; | |
2683 | } | |
2684 | } else if (trigger_name_taken(state, trigger)) { | |
2685 | /* Not a fatal error. */ | |
2686 | *cmd_result = LTTNG_ERR_TRIGGER_EXISTS; | |
2687 | ret = 0; | |
242388e4 JR |
2688 | goto error; |
2689 | } | |
0efb2ad7 JG |
2690 | } else { |
2691 | trigger_name = "(anonymous)"; | |
242388e4 JR |
2692 | } |
2693 | ||
ab0ee2ca | 2694 | condition = lttng_trigger_get_condition(trigger); |
a0377dfe | 2695 | LTTNG_ASSERT(condition); |
1da26331 | 2696 | |
959e3c66 JR |
2697 | /* Some conditions require tracers to implement a minimal ABI version. */ |
2698 | if (!condition_is_supported(condition)) { | |
1da26331 JG |
2699 | *cmd_result = LTTNG_ERR_NOT_SUPPORTED; |
2700 | goto error; | |
1da26331 JG |
2701 | } |
2702 | ||
64803277 | 2703 | trigger_ht_element = zmalloc<lttng_trigger_ht_element>(); |
ab0ee2ca JG |
2704 | if (!trigger_ht_element) { |
2705 | ret = -1; | |
2706 | goto error; | |
2707 | } | |
2708 | ||
2709 | /* Add trigger to the trigger_ht. */ | |
2710 | cds_lfht_node_init(&trigger_ht_element->node); | |
242388e4 | 2711 | cds_lfht_node_init(&trigger_ht_element->node_by_name_uid); |
ab0ee2ca JG |
2712 | trigger_ht_element->trigger = trigger; |
2713 | ||
2714 | node = cds_lfht_add_unique(state->triggers_ht, | |
28ab034a JG |
2715 | lttng_condition_hash(condition), |
2716 | match_trigger, | |
2717 | trigger, | |
2718 | &trigger_ht_element->node); | |
ab0ee2ca JG |
2719 | if (node != &trigger_ht_element->node) { |
2720 | /* Not a fatal error, simply report it to the client. */ | |
2721 | *cmd_result = LTTNG_ERR_TRIGGER_EXISTS; | |
2722 | goto error_free_ht_element; | |
2723 | } | |
2724 | ||
242388e4 | 2725 | node = cds_lfht_add_unique(state->triggers_by_name_uid_ht, |
28ab034a JG |
2726 | hash_trigger_by_name_uid(trigger), |
2727 | match_trigger_by_name_uid, | |
2728 | trigger, | |
2729 | &trigger_ht_element->node_by_name_uid); | |
242388e4 | 2730 | if (node != &trigger_ht_element->node_by_name_uid) { |
75e540c6 JG |
2731 | /* Internal error: add to triggers_ht should have failed. */ |
2732 | ret = -1; | |
242388e4 JR |
2733 | goto error_free_ht_element; |
2734 | } | |
2735 | ||
9c374932 JR |
2736 | /* From this point consider the trigger registered. */ |
2737 | lttng_trigger_set_as_registered(trigger); | |
2738 | ||
6487ad53 FD |
2739 | /* |
2740 | * Some triggers might need a tracer notifier depending on its | |
2741 | * condition and actions. | |
2742 | */ | |
2743 | if (lttng_trigger_needs_tracer_notifier(trigger)) { | |
2744 | enum lttng_error_code error_code; | |
e7c93cf9 | 2745 | |
6487ad53 FD |
2746 | error_code = setup_tracer_notifier(state, trigger); |
2747 | if (error_code != LTTNG_OK) { | |
28ab034a | 2748 | notif_thread_state_remove_trigger_ht_elem(state, trigger_ht_element); |
6487ad53 FD |
2749 | if (error_code == LTTNG_ERR_NOMEM) { |
2750 | ret = -1; | |
2751 | } else { | |
2752 | *cmd_result = error_code; | |
2753 | ret = 0; | |
90aa04a1 FD |
2754 | } |
2755 | ||
6487ad53 | 2756 | goto error_free_ht_element; |
90aa04a1 | 2757 | } |
e7c93cf9 JR |
2758 | } |
2759 | ||
ab0ee2ca JG |
2760 | /* |
2761 | * The rest only applies to triggers that have a "notify" action. | |
2762 | * It is not skipped as this is the only action type currently | |
2763 | * supported. | |
2764 | */ | |
f2b3ef9f | 2765 | if (is_trigger_action_notify(trigger)) { |
091fa780 FD |
2766 | /* |
2767 | * Find or create the client list of this condition. It may | |
2768 | * already be present if another trigger is already registered | |
2769 | * with the same condition. | |
2770 | */ | |
2771 | client_list = get_client_list_from_condition(state, condition); | |
f2b3ef9f | 2772 | if (!client_list) { |
091fa780 FD |
2773 | /* |
2774 | * No client list for this condition yet. We create new | |
2775 | * one and build it up. | |
2776 | */ | |
2777 | client_list = notification_client_list_create(state, condition); | |
2778 | if (!client_list) { | |
28ab034a JG |
2779 | ERR("Error creating notification client list for trigger %s", |
2780 | trigger->name); | |
a3c9aa3c | 2781 | ret = -1; |
091fa780 | 2782 | goto error_free_ht_element; |
f2b3ef9f | 2783 | } |
ab0ee2ca | 2784 | } |
f2b3ef9f | 2785 | |
091fa780 FD |
2786 | CDS_INIT_LIST_HEAD(&trigger_ht_element->client_list_trigger_node); |
2787 | ||
2788 | pthread_mutex_lock(&client_list->lock); | |
28ab034a JG |
2789 | cds_list_add(&trigger_ht_element->client_list_trigger_node, |
2790 | &client_list->triggers_list); | |
091fa780 | 2791 | pthread_mutex_unlock(&client_list->lock); |
ab0ee2ca JG |
2792 | } |
2793 | ||
091fa780 FD |
2794 | /* |
2795 | * Ownership of the trigger and of its wrapper was transfered to | |
2796 | * the triggers_ht. Same for token ht element if necessary. | |
2797 | */ | |
cd9adb8b | 2798 | trigger_ht_element = nullptr; |
091fa780 FD |
2799 | free_trigger = false; |
2800 | ||
f82f93a1 | 2801 | switch (get_condition_binding_object(condition)) { |
51eab943 JG |
2802 | case LTTNG_OBJECT_TYPE_SESSION: |
2803 | /* Add the trigger to the list if it matches a known session. */ | |
2804 | ret = bind_trigger_to_matching_session(trigger, state); | |
2805 | if (ret) { | |
091fa780 | 2806 | goto error_free_ht_element; |
ab0ee2ca | 2807 | } |
f82f93a1 | 2808 | break; |
51eab943 JG |
2809 | case LTTNG_OBJECT_TYPE_CHANNEL: |
2810 | /* | |
2811 | * Add the trigger to list of triggers bound to the channels | |
2812 | * currently known. | |
2813 | */ | |
2814 | ret = bind_trigger_to_matching_channels(trigger, state); | |
2815 | if (ret) { | |
091fa780 | 2816 | goto error_free_ht_element; |
ab0ee2ca | 2817 | } |
51eab943 JG |
2818 | break; |
2819 | case LTTNG_OBJECT_TYPE_NONE: | |
2820 | break; | |
2821 | default: | |
f2b3ef9f | 2822 | ERR("Unknown object type on which to bind a newly registered trigger was encountered"); |
51eab943 | 2823 | ret = -1; |
091fa780 | 2824 | goto error_free_ht_element; |
ab0ee2ca JG |
2825 | } |
2826 | ||
2ae99f0b | 2827 | /* |
f2b3ef9f JG |
2828 | * The new trigger's condition must be evaluated against the current |
2829 | * state. | |
2830 | * | |
2831 | * In the case of `notify` action, nothing preventing clients from | |
2832 | * subscribing to a condition before the corresponding trigger is | |
2833 | * registered, we have to evaluate this new condition right away. | |
2ae99f0b JG |
2834 | * |
2835 | * At some point, we were waiting for the next "evaluation" (e.g. on | |
2836 | * reception of a channel sample) to evaluate this new condition, but | |
2837 | * that was broken. | |
2838 | * | |
2839 | * The reason it was broken is that waiting for the next sample | |
2840 | * does not allow us to properly handle transitions for edge-triggered | |
2841 | * conditions. | |
2842 | * | |
2843 | * Consider this example: when we handle a new channel sample, we | |
2844 | * evaluate each conditions twice: once with the previous state, and | |
2845 | * again with the newest state. We then use those two results to | |
2846 | * determine whether a state change happened: a condition was false and | |
2847 | * became true. If a state change happened, we have to notify clients. | |
2848 | * | |
2849 | * Now, if a client subscribes to a given notification and registers | |
2850 | * a trigger *after* that subscription, we have to make sure the | |
2851 | * condition is evaluated at this point while considering only the | |
2852 | * current state. Otherwise, the next evaluation cycle may only see | |
2853 | * that the evaluations remain the same (true for samples n-1 and n) and | |
2854 | * the client will never know that the condition has been met. | |
2855 | */ | |
f2b3ef9f JG |
2856 | switch (get_condition_binding_object(condition)) { |
2857 | case LTTNG_OBJECT_TYPE_SESSION: | |
319dcddc JG |
2858 | { |
2859 | /* Find the session associated with the condition. */ | |
2860 | const auto *session_name = get_condition_session_name(condition); | |
2861 | auto session_info = get_session_info_by_name(state, session_name); | |
2862 | if (!session_info) { | |
2863 | /* Not an error, the session doesn't exist yet. */ | |
2864 | DBG("Session not found while evaluating session condition during registration of trigger: session name = `%s`", | |
28ab034a | 2865 | session_name); |
319dcddc JG |
2866 | ret = 0; |
2867 | goto success; | |
2868 | } | |
2869 | ||
2870 | LTTNG_OPTIONAL_SET(&object_creds.uid, session_info->uid); | |
2871 | LTTNG_OPTIONAL_SET(&object_creds.gid, session_info->gid); | |
2872 | ||
cd9adb8b | 2873 | ret = evaluate_session_condition(condition, session_info, nullptr, &evaluation); |
319dcddc | 2874 | session_info_put(session_info); |
b42ada90 | 2875 | break; |
319dcddc | 2876 | } |
f2b3ef9f | 2877 | case LTTNG_OBJECT_TYPE_CHANNEL: |
28ab034a JG |
2878 | ret = evaluate_channel_condition_for_client( |
2879 | condition, state, &evaluation, &object_uid, &object_gid); | |
bc8daafb JG |
2880 | LTTNG_OPTIONAL_SET(&object_creds.uid, object_uid); |
2881 | LTTNG_OPTIONAL_SET(&object_creds.gid, object_gid); | |
f2b3ef9f JG |
2882 | break; |
2883 | case LTTNG_OBJECT_TYPE_NONE: | |
2884 | ret = 0; | |
242388e4 | 2885 | break; |
f2b3ef9f JG |
2886 | case LTTNG_OBJECT_TYPE_UNKNOWN: |
2887 | default: | |
2888 | ret = -1; | |
242388e4 | 2889 | break; |
f2b3ef9f JG |
2890 | } |
2891 | ||
2892 | if (ret) { | |
2893 | /* Fatal error. */ | |
091fa780 | 2894 | goto error_free_ht_element; |
f2b3ef9f JG |
2895 | } |
2896 | ||
28ab034a | 2897 | DBG("Newly registered trigger's condition evaluated to %s", evaluation ? "true" : "false"); |
f2b3ef9f JG |
2898 | if (!evaluation) { |
2899 | /* Evaluation yielded nothing. Normal exit. */ | |
2900 | ret = 0; | |
091fa780 | 2901 | goto success; |
2ae99f0b JG |
2902 | } |
2903 | ||
2904 | /* | |
f2b3ef9f JG |
2905 | * Ownership of `evaluation` transferred to the action executor |
2906 | * no matter the result. | |
2ae99f0b | 2907 | */ |
28ab034a JG |
2908 | executor_status = action_executor_enqueue_trigger( |
2909 | state->executor, trigger, evaluation, &object_creds, client_list); | |
cd9adb8b | 2910 | evaluation = nullptr; |
f2b3ef9f JG |
2911 | switch (executor_status) { |
2912 | case ACTION_EXECUTOR_STATUS_OK: | |
2913 | break; | |
2914 | case ACTION_EXECUTOR_STATUS_ERROR: | |
2915 | case ACTION_EXECUTOR_STATUS_INVALID: | |
2916 | /* | |
2917 | * TODO Add trigger identification (name/id) when | |
2918 | * it is added to the API. | |
2919 | */ | |
2920 | ERR("Fatal error occurred while enqueuing action associated to newly registered trigger"); | |
2921 | ret = -1; | |
091fa780 | 2922 | goto error_free_ht_element; |
f2b3ef9f JG |
2923 | case ACTION_EXECUTOR_STATUS_OVERFLOW: |
2924 | /* | |
2925 | * TODO Add trigger identification (name/id) when | |
2926 | * it is added to the API. | |
2927 | * | |
2928 | * Not a fatal error. | |
2929 | */ | |
2930 | WARN("No space left when enqueuing action associated to newly registered trigger"); | |
2931 | ret = 0; | |
091fa780 | 2932 | goto success; |
f2b3ef9f JG |
2933 | default: |
2934 | abort(); | |
2935 | } | |
2ae99f0b | 2936 | |
091fa780 | 2937 | success: |
ab0ee2ca | 2938 | *cmd_result = LTTNG_OK; |
e6887944 | 2939 | DBG("Registered trigger: name = `%s`, tracer token = %" PRIu64, |
28ab034a JG |
2940 | trigger_name, |
2941 | trigger_tracer_token); | |
091fa780 | 2942 | goto end; |
505b2d90 | 2943 | |
ab0ee2ca | 2944 | error_free_ht_element: |
242388e4 JR |
2945 | if (trigger_ht_element) { |
2946 | /* Delayed removal due to RCU constraint on delete. */ | |
28ab034a | 2947 | call_rcu(&trigger_ht_element->rcu_node, free_lttng_trigger_ht_element_rcu); |
242388e4 | 2948 | } |
ab0ee2ca JG |
2949 | error: |
2950 | if (free_trigger) { | |
9c374932 JR |
2951 | /* |
2952 | * Other objects might have a reference to the trigger, mark it | |
2953 | * as unregistered. | |
2954 | */ | |
2955 | lttng_trigger_set_as_unregistered(trigger); | |
ab0ee2ca JG |
2956 | lttng_trigger_destroy(trigger); |
2957 | } | |
091fa780 | 2958 | end: |
ab0ee2ca JG |
2959 | return ret; |
2960 | } | |
2961 | ||
28ab034a | 2962 | static void free_lttng_trigger_ht_element_rcu(struct rcu_head *node) |
83b934ad | 2963 | { |
28ab034a | 2964 | free(caa_container_of(node, struct lttng_trigger_ht_element, rcu_node)); |
83b934ad MD |
2965 | } |
2966 | ||
28ab034a | 2967 | static void free_notification_trigger_tokens_ht_element_rcu(struct rcu_head *node) |
e7c93cf9 | 2968 | { |
28ab034a | 2969 | free(caa_container_of(node, struct notification_trigger_tokens_ht_element, rcu_node)); |
e7c93cf9 JR |
2970 | } |
2971 | ||
28ab034a JG |
2972 | static void teardown_tracer_notifier(struct notification_thread_state *state, |
2973 | const struct lttng_trigger *trigger) | |
6487ad53 | 2974 | { |
6e7ffe66 JG |
2975 | for (auto *trigger_tokens_ht_element : lttng::urcu::lfht_iteration_adapter< |
2976 | notification_trigger_tokens_ht_element, | |
2977 | decltype(notification_trigger_tokens_ht_element::node), | |
2978 | ¬ification_trigger_tokens_ht_element::node>(*state->trigger_tokens_ht)) { | |
2979 | if (!lttng_trigger_is_equal(trigger, trigger_tokens_ht_element->trigger)) { | |
2980 | continue; | |
2981 | } | |
6487ad53 | 2982 | |
6e7ffe66 JG |
2983 | event_notifier_error_accounting_unregister_event_notifier( |
2984 | trigger_tokens_ht_element->trigger); | |
6487ad53 | 2985 | |
6e7ffe66 JG |
2986 | /* TODO talk to all app and remove it */ |
2987 | DBG("Removed trigger from tokens_ht"); | |
2988 | cds_lfht_del(state->trigger_tokens_ht, &trigger_tokens_ht_element->node); | |
6487ad53 | 2989 | |
6e7ffe66 JG |
2990 | call_rcu(&trigger_tokens_ht_element->rcu_node, |
2991 | free_notification_trigger_tokens_ht_element_rcu); | |
6487ad53 | 2992 | |
6e7ffe66 | 2993 | break; |
6487ad53 FD |
2994 | } |
2995 | } | |
2996 | ||
28ab034a JG |
2997 | static void |
2998 | remove_trigger_from_session_trigger_list(struct lttng_session_trigger_list *trigger_list, | |
2999 | const struct lttng_trigger *trigger) | |
319dcddc JG |
3000 | { |
3001 | bool found = false; | |
3002 | struct lttng_trigger_list_element *trigger_element, *tmp; | |
3003 | ||
3004 | cds_list_for_each_entry_safe (trigger_element, tmp, &trigger_list->list, node) { | |
3005 | if (!lttng_trigger_is_equal(trigger, trigger_element->trigger)) { | |
3006 | continue; | |
3007 | } | |
3008 | ||
3009 | DBG("Removed trigger from session_triggers_ht"); | |
3010 | cds_list_del(&trigger_element->node); | |
3011 | free(trigger_element); | |
3012 | /* A trigger can only appear once per session. */ | |
3013 | found = true; | |
3014 | break; | |
3015 | } | |
3016 | ||
3017 | if (!found) { | |
3018 | ERR("Failed to find trigger associated with session: session name = `%s`", | |
28ab034a | 3019 | trigger_list->session_name); |
319dcddc JG |
3020 | } |
3021 | ||
3022 | LTTNG_ASSERT(found); | |
3023 | } | |
3024 | ||
28ab034a JG |
3025 | static int |
3026 | handle_notification_thread_command_unregister_trigger(struct notification_thread_state *state, | |
3027 | const struct lttng_trigger *trigger, | |
3028 | enum lttng_error_code *_cmd_reply) | |
ab0ee2ca JG |
3029 | { |
3030 | struct cds_lfht_iter iter; | |
ed327204 | 3031 | struct cds_lfht_node *triggers_ht_node; |
ab0ee2ca | 3032 | struct notification_client_list *client_list; |
cd9adb8b | 3033 | struct lttng_trigger_ht_element *trigger_ht_element = nullptr; |
28ab034a | 3034 | const struct lttng_condition *condition = lttng_trigger_get_const_condition(trigger); |
ab0ee2ca JG |
3035 | enum lttng_error_code cmd_reply; |
3036 | ||
07c4863f | 3037 | const lttng::urcu::read_lock_guard read_lock; |
ab0ee2ca | 3038 | |
28ab034a JG |
3039 | cds_lfht_lookup( |
3040 | state->triggers_ht, lttng_condition_hash(condition), match_trigger, trigger, &iter); | |
ab0ee2ca JG |
3041 | triggers_ht_node = cds_lfht_iter_get_node(&iter); |
3042 | if (!triggers_ht_node) { | |
3043 | cmd_reply = LTTNG_ERR_TRIGGER_NOT_FOUND; | |
3044 | goto end; | |
3045 | } else { | |
3046 | cmd_reply = LTTNG_OK; | |
3047 | } | |
3048 | ||
28ab034a JG |
3049 | trigger_ht_element = |
3050 | caa_container_of(triggers_ht_node, struct lttng_trigger_ht_element, node); | |
9c374932 | 3051 | |
319dcddc JG |
3052 | switch (get_condition_binding_object(condition)) { |
3053 | case LTTNG_OBJECT_TYPE_CHANNEL: | |
3054 | { | |
319dcddc JG |
3055 | /* |
3056 | * Remove trigger from channel_triggers_ht. | |
3057 | * | |
3058 | * Note that multiple channels may have matched the trigger's | |
3059 | * condition (e.g. all instances of a given channel in per-pid buffering | |
3060 | * mode). | |
3061 | * | |
3062 | * Iterate on all lists since we don't know the target channels' keys. | |
3063 | */ | |
6e7ffe66 JG |
3064 | for (auto *trigger_list : lttng::urcu::lfht_iteration_adapter< |
3065 | lttng_channel_trigger_list, | |
3066 | decltype(lttng_channel_trigger_list::channel_triggers_ht_node), | |
3067 | <tng_channel_trigger_list::channel_triggers_ht_node>( | |
3068 | *state->channel_triggers_ht)) { | |
319dcddc JG |
3069 | struct lttng_trigger_list_element *trigger_element, *tmp; |
3070 | ||
28ab034a JG |
3071 | cds_list_for_each_entry_safe ( |
3072 | trigger_element, tmp, &trigger_list->list, node) { | |
319dcddc JG |
3073 | if (!lttng_trigger_is_equal(trigger, trigger_element->trigger)) { |
3074 | continue; | |
3075 | } | |
3076 | ||
3077 | DBG("Removed trigger from channel_triggers_ht"); | |
3078 | cds_list_del(&trigger_element->node); | |
6dcdb62c | 3079 | free(trigger_element); |
319dcddc JG |
3080 | /* A trigger can only appear once per channel */ |
3081 | break; | |
ab0ee2ca | 3082 | } |
319dcddc JG |
3083 | } |
3084 | break; | |
3085 | } | |
3086 | case LTTNG_OBJECT_TYPE_SESSION: | |
3087 | { | |
28ab034a JG |
3088 | auto session = |
3089 | get_session_info_by_name(state, get_condition_session_name(condition)); | |
ab0ee2ca | 3090 | |
319dcddc JG |
3091 | /* Session doesn't exist, no trigger to remove. */ |
3092 | if (!session) { | |
e4db5ace | 3093 | break; |
ab0ee2ca | 3094 | } |
319dcddc JG |
3095 | |
3096 | auto session_trigger_list = get_session_trigger_list(state, session->name); | |
3097 | remove_trigger_from_session_trigger_list(session_trigger_list, trigger); | |
3098 | session_info_put(session); | |
3099 | } | |
3100 | case LTTNG_OBJECT_TYPE_NONE: | |
3101 | break; | |
3102 | default: | |
3103 | abort(); | |
ab0ee2ca JG |
3104 | } |
3105 | ||
6487ad53 FD |
3106 | if (lttng_trigger_needs_tracer_notifier(trigger)) { |
3107 | teardown_tracer_notifier(state, trigger); | |
e7c93cf9 JR |
3108 | } |
3109 | ||
51367634 JR |
3110 | if (is_trigger_action_notify(trigger)) { |
3111 | /* | |
3112 | * Remove and release the client list from | |
3113 | * notification_trigger_clients_ht. | |
3114 | */ | |
3115 | client_list = get_client_list_from_condition(state, condition); | |
a0377dfe | 3116 | LTTNG_ASSERT(client_list); |
ed327204 | 3117 | |
091fa780 FD |
3118 | pthread_mutex_lock(&client_list->lock); |
3119 | cds_list_del(&trigger_ht_element->client_list_trigger_node); | |
3120 | pthread_mutex_unlock(&client_list->lock); | |
3121 | ||
51367634 JR |
3122 | /* Put new reference and the hashtable's reference. */ |
3123 | notification_client_list_put(client_list); | |
3124 | notification_client_list_put(client_list); | |
cd9adb8b | 3125 | client_list = nullptr; |
51367634 | 3126 | } |
ab0ee2ca | 3127 | |
2758e38b FD |
3128 | /* Remove trigger from triggers_ht. */ |
3129 | notif_thread_state_remove_trigger_ht_elem(state, trigger_ht_element); | |
ab0ee2ca | 3130 | |
7ca172c1 | 3131 | /* Release the ownership of the trigger. */ |
ab0ee2ca | 3132 | lttng_trigger_destroy(trigger_ht_element->trigger); |
83b934ad | 3133 | call_rcu(&trigger_ht_element->rcu_node, free_lttng_trigger_ht_element_rcu); |
ab0ee2ca | 3134 | end: |
ab0ee2ca JG |
3135 | if (_cmd_reply) { |
3136 | *_cmd_reply = cmd_reply; | |
3137 | } | |
3138 | return 0; | |
3139 | } | |
3140 | ||
d4333eb9 | 3141 | static notification_thread_command *pop_cmd_queue(notification_thread_handle *handle) |
f370852f | 3142 | { |
07c4863f | 3143 | const lttng::pthread::lock_guard queue_lock(handle->cmd_queue.lock); |
f370852f | 3144 | |
d4333eb9 JG |
3145 | uint64_t counter; |
3146 | const auto read_ret = lttng_read(handle->cmd_queue.event_fd, &counter, sizeof(counter)); | |
3147 | if (read_ret != sizeof(counter)) { | |
3148 | if (read_ret < 0) { | |
3149 | LTTNG_THROW_POSIX("Failed to read counter value from event_fd", errno); | |
3150 | } else { | |
3151 | LTTNG_THROW_ERROR(lttng::format( | |
3152 | "Failed to read counter value from event_fd because of a truncated read: ret={}, expected read size={}", | |
3153 | read_ret, | |
3154 | sizeof(counter))); | |
3155 | } | |
f370852f JR |
3156 | } |
3157 | ||
d4333eb9 | 3158 | auto command = cds_list_first_entry( |
28ab034a | 3159 | &handle->cmd_queue.list, struct notification_thread_command, cmd_list_node); |
d4333eb9 JG |
3160 | cds_list_del(&((command)->cmd_list_node)); |
3161 | return command; | |
f370852f JR |
3162 | } |
3163 | ||
ab0ee2ca | 3164 | /* Returns 0 on success, 1 on exit requested, negative value on error. */ |
28ab034a JG |
3165 | int handle_notification_thread_command(struct notification_thread_handle *handle, |
3166 | struct notification_thread_state *state) | |
ab0ee2ca JG |
3167 | { |
3168 | int ret; | |
d4333eb9 | 3169 | struct notification_thread_command *cmd = nullptr; |
ab0ee2ca | 3170 | |
d4333eb9 JG |
3171 | try { |
3172 | cmd = pop_cmd_queue(handle); | |
3173 | } catch (const std::exception& ex) { | |
3174 | ERR("Failed to get next notification thread command: %s", ex.what()); | |
ab0ee2ca JG |
3175 | goto error; |
3176 | } | |
3177 | ||
28ab034a | 3178 | DBG("Received `%s` command", notification_command_type_str(cmd->type)); |
ab0ee2ca JG |
3179 | switch (cmd->type) { |
3180 | case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER: | |
28ab034a JG |
3181 | ret = handle_notification_thread_command_register_trigger( |
3182 | state, | |
3183 | cmd->parameters.register_trigger.trigger, | |
3184 | cmd->parameters.register_trigger.is_trigger_anonymous, | |
3185 | &cmd->reply_code); | |
ab0ee2ca JG |
3186 | break; |
3187 | case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER: | |
ab0ee2ca | 3188 | ret = handle_notification_thread_command_unregister_trigger( |
28ab034a | 3189 | state, cmd->parameters.unregister_trigger.trigger, &cmd->reply_code); |
ab0ee2ca JG |
3190 | break; |
3191 | case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL: | |
ab0ee2ca | 3192 | ret = handle_notification_thread_command_add_channel( |
28ab034a JG |
3193 | state, |
3194 | cmd->parameters.add_channel.session.id, | |
3195 | cmd->parameters.add_channel.channel.name, | |
3196 | cmd->parameters.add_channel.channel.domain, | |
3197 | cmd->parameters.add_channel.channel.key, | |
3198 | cmd->parameters.add_channel.channel.capacity, | |
3199 | &cmd->reply_code); | |
ab0ee2ca JG |
3200 | break; |
3201 | case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL: | |
ab0ee2ca | 3202 | ret = handle_notification_thread_command_remove_channel( |
28ab034a JG |
3203 | state, |
3204 | cmd->parameters.remove_channel.key, | |
3205 | cmd->parameters.remove_channel.domain, | |
3206 | &cmd->reply_code); | |
ab0ee2ca | 3207 | break; |
139a8d25 | 3208 | case NOTIFICATION_COMMAND_TYPE_ADD_SESSION: |
28ab034a JG |
3209 | ret = handle_notification_thread_command_add_session( |
3210 | state, | |
3211 | cmd->parameters.add_session.session_id, | |
3212 | cmd->parameters.add_session.session_name, | |
3213 | cmd->parameters.add_session.session_uid, | |
3214 | cmd->parameters.add_session.session_gid, | |
3215 | &cmd->reply_code); | |
139a8d25 JG |
3216 | break; |
3217 | case NOTIFICATION_COMMAND_TYPE_REMOVE_SESSION: | |
3218 | ret = handle_notification_thread_command_remove_session( | |
28ab034a | 3219 | state, cmd->parameters.remove_session.session_id, &cmd->reply_code); |
139a8d25 | 3220 | break; |
0ca52944 | 3221 | case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING: |
0ca52944 | 3222 | case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED: |
ed327204 | 3223 | ret = handle_notification_thread_command_session_rotation( |
28ab034a JG |
3224 | state, |
3225 | cmd->type, | |
3226 | cmd->parameters.session_rotation.session_id, | |
3227 | cmd->parameters.session_rotation.trace_archive_chunk_id, | |
3228 | cmd->parameters.session_rotation.location, | |
3229 | &cmd->reply_code); | |
0ca52944 | 3230 | break; |
d02d7404 JR |
3231 | case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE: |
3232 | ret = handle_notification_thread_command_add_tracer_event_source( | |
28ab034a JG |
3233 | state, |
3234 | cmd->parameters.tracer_event_source.tracer_event_source_fd, | |
3235 | cmd->parameters.tracer_event_source.domain, | |
3236 | &cmd->reply_code); | |
d02d7404 JR |
3237 | break; |
3238 | case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE: | |
3239 | ret = handle_notification_thread_command_remove_tracer_event_source( | |
28ab034a JG |
3240 | state, |
3241 | cmd->parameters.tracer_event_source.tracer_event_source_fd, | |
3242 | &cmd->reply_code); | |
d02d7404 | 3243 | break; |
fbc9f37d JR |
3244 | case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS: |
3245 | { | |
cd9adb8b | 3246 | struct lttng_triggers *triggers = nullptr; |
fbc9f37d JR |
3247 | |
3248 | ret = handle_notification_thread_command_list_triggers( | |
28ab034a JG |
3249 | handle, |
3250 | state, | |
3251 | cmd->parameters.list_triggers.uid, | |
3252 | &triggers, | |
3253 | &cmd->reply_code); | |
fbc9f37d JR |
3254 | cmd->reply.list_triggers.triggers = triggers; |
3255 | ret = 0; | |
3256 | break; | |
3257 | } | |
ab0ee2ca | 3258 | case NOTIFICATION_COMMAND_TYPE_QUIT: |
ab0ee2ca JG |
3259 | cmd->reply_code = LTTNG_OK; |
3260 | ret = 1; | |
3261 | goto end; | |
8790759c FD |
3262 | case NOTIFICATION_COMMAND_TYPE_GET_TRIGGER: |
3263 | { | |
cd9adb8b | 3264 | struct lttng_trigger *trigger = nullptr; |
8790759c | 3265 | |
28ab034a JG |
3266 | ret = handle_notification_thread_command_get_trigger( |
3267 | state, cmd->parameters.get_trigger.trigger, &trigger, &cmd->reply_code); | |
8790759c FD |
3268 | cmd->reply.get_trigger.trigger = trigger; |
3269 | break; | |
3270 | } | |
f2b3ef9f JG |
3271 | case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE: |
3272 | { | |
3273 | const enum client_transmission_status client_status = | |
28ab034a | 3274 | cmd->parameters.client_communication_update.status; |
f2b3ef9f | 3275 | const notification_client_id client_id = |
28ab034a | 3276 | cmd->parameters.client_communication_update.id; |
f2b3ef9f JG |
3277 | struct notification_client *client; |
3278 | ||
07c4863f | 3279 | const lttng::urcu::read_lock_guard read_lock; |
f2b3ef9f JG |
3280 | client = get_client_from_id(client_id, state); |
3281 | ||
3282 | if (!client) { | |
3283 | /* | |
3284 | * Client error was probably already picked-up by the | |
3285 | * notification thread or it has disconnected | |
3286 | * gracefully while this command was queued. | |
3287 | */ | |
3288 | DBG("Failed to find notification client to update communication status, client id = %" PRIu64, | |
28ab034a | 3289 | client_id); |
f2b3ef9f JG |
3290 | ret = 0; |
3291 | } else { | |
28ab034a | 3292 | ret = client_handle_transmission_status(client, client_status, state); |
f2b3ef9f | 3293 | } |
f2b3ef9f JG |
3294 | break; |
3295 | } | |
ab0ee2ca | 3296 | default: |
bd0514a5 | 3297 | ERR("Unknown internal command received"); |
ab0ee2ca JG |
3298 | goto error_unlock; |
3299 | } | |
3300 | ||
3301 | if (ret) { | |
3302 | goto error_unlock; | |
3303 | } | |
32670d71 | 3304 | |
ab0ee2ca | 3305 | end: |
d4333eb9 JG |
3306 | if (cmd) { |
3307 | if (cmd->is_async) { | |
3308 | delete cmd; | |
3309 | cmd = nullptr; | |
3310 | } else { | |
3311 | cmd->command_completed_waker->wake(); | |
3312 | } | |
0ab399e0 | 3313 | } |
32670d71 | 3314 | |
ab0ee2ca | 3315 | return ret; |
32670d71 | 3316 | |
ab0ee2ca JG |
3317 | error_unlock: |
3318 | /* Wake-up and return a fatal error to the calling thread. */ | |
ab0ee2ca | 3319 | cmd->reply_code = LTTNG_ERR_FATAL; |
32670d71 | 3320 | |
ab0ee2ca | 3321 | error: |
32670d71 JG |
3322 | ret = -1; |
3323 | goto end; | |
ab0ee2ca JG |
3324 | } |
3325 | ||
28ab034a | 3326 | static int socket_set_non_blocking(int socket) |
ab0ee2ca JG |
3327 | { |
3328 | int ret, flags; | |
3329 | ||
3330 | /* Set the pipe as non-blocking. */ | |
3331 | ret = fcntl(socket, F_GETFL, 0); | |
3332 | if (ret == -1) { | |
3333 | PERROR("fcntl get socket flags"); | |
3334 | goto end; | |
3335 | } | |
3336 | flags = ret; | |
3337 | ||
3338 | ret = fcntl(socket, F_SETFL, flags | O_NONBLOCK); | |
3339 | if (ret == -1) { | |
3340 | PERROR("fcntl set O_NONBLOCK socket flag"); | |
3341 | goto end; | |
3342 | } | |
3343 | DBG("Client socket (fd = %i) set as non-blocking", socket); | |
3344 | end: | |
3345 | return ret; | |
3346 | } | |
3347 | ||
28ab034a | 3348 | static int client_reset_inbound_state(struct notification_client *client) |
ab0ee2ca JG |
3349 | { |
3350 | int ret; | |
3351 | ||
882093ee | 3352 | lttng_payload_clear(&client->communication.inbound.payload); |
ab0ee2ca JG |
3353 | |
3354 | client->communication.inbound.bytes_to_receive = | |
28ab034a JG |
3355 | sizeof(struct lttng_notification_channel_message); |
3356 | client->communication.inbound.msg_type = LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN; | |
ab0ee2ca JG |
3357 | LTTNG_SOCK_SET_UID_CRED(&client->communication.inbound.creds, -1); |
3358 | LTTNG_SOCK_SET_GID_CRED(&client->communication.inbound.creds, -1); | |
28ab034a JG |
3359 | ret = lttng_dynamic_buffer_set_size(&client->communication.inbound.payload.buffer, |
3360 | client->communication.inbound.bytes_to_receive); | |
882093ee | 3361 | |
14fa22f8 | 3362 | return ret; |
ab0ee2ca JG |
3363 | } |
3364 | ||
28ab034a | 3365 | int handle_notification_thread_client_connect(struct notification_thread_state *state) |
ab0ee2ca JG |
3366 | { |
3367 | int ret; | |
3368 | struct notification_client *client; | |
3369 | ||
bd0514a5 | 3370 | DBG("Handling new notification channel client connection"); |
ab0ee2ca | 3371 | |
64803277 | 3372 | client = zmalloc<notification_client>(); |
ab0ee2ca JG |
3373 | if (!client) { |
3374 | /* Fatal error. */ | |
3375 | ret = -1; | |
3376 | goto error; | |
3377 | } | |
6c24d3fd | 3378 | |
cd9adb8b | 3379 | pthread_mutex_init(&client->lock, nullptr); |
ac1889bf | 3380 | client->id = state->next_notification_client_id++; |
ab0ee2ca | 3381 | CDS_INIT_LIST_HEAD(&client->condition_list); |
882093ee JR |
3382 | lttng_payload_init(&client->communication.inbound.payload); |
3383 | lttng_payload_init(&client->communication.outbound.payload); | |
01ea340e | 3384 | client->communication.inbound.expect_creds = true; |
505b2d90 | 3385 | |
14fa22f8 JG |
3386 | ret = client_reset_inbound_state(client); |
3387 | if (ret) { | |
bd0514a5 | 3388 | ERR("Failed to reset client communication's inbound state"); |
e742b055 | 3389 | ret = 0; |
14fa22f8 JG |
3390 | goto error; |
3391 | } | |
ab0ee2ca JG |
3392 | |
3393 | ret = lttcomm_accept_unix_sock(state->notification_channel_socket); | |
3394 | if (ret < 0) { | |
bd0514a5 | 3395 | ERR("Failed to accept new notification channel client connection"); |
ab0ee2ca JG |
3396 | ret = 0; |
3397 | goto error; | |
3398 | } | |
3399 | ||
3400 | client->socket = ret; | |
3401 | ||
3402 | ret = socket_set_non_blocking(client->socket); | |
3403 | if (ret) { | |
bd0514a5 | 3404 | ERR("Failed to set new notification channel client connection socket as non-blocking"); |
ab0ee2ca JG |
3405 | goto error; |
3406 | } | |
3407 | ||
3408 | ret = lttcomm_setsockopt_creds_unix_sock(client->socket); | |
3409 | if (ret < 0) { | |
bd0514a5 | 3410 | ERR("Failed to set socket options on new notification channel client socket"); |
ab0ee2ca JG |
3411 | ret = 0; |
3412 | goto error; | |
3413 | } | |
3414 | ||
9016dbfc | 3415 | client->communication.current_poll_events = CLIENT_POLL_EVENTS_IN; |
28ab034a JG |
3416 | ret = lttng_poll_add( |
3417 | &state->events, client->socket, client->communication.current_poll_events); | |
ab0ee2ca | 3418 | if (ret < 0) { |
bd0514a5 | 3419 | ERR("Failed to add notification channel client socket to poll set"); |
ab0ee2ca JG |
3420 | ret = 0; |
3421 | goto error; | |
3422 | } | |
28ab034a | 3423 | DBG("Added new notification channel client socket (%i) to poll set", client->socket); |
ab0ee2ca | 3424 | |
56047f5a | 3425 | { |
07c4863f | 3426 | const lttng::urcu::read_lock_guard read_lock; |
56047f5a JG |
3427 | |
3428 | cds_lfht_add(state->client_socket_ht, | |
3429 | hash_client_socket(client->socket), | |
3430 | &client->client_socket_ht_node); | |
3431 | cds_lfht_add(state->client_id_ht, | |
3432 | hash_client_id(client->id), | |
3433 | &client->client_id_ht_node); | |
3434 | } | |
ab0ee2ca JG |
3435 | |
3436 | return ret; | |
6c24d3fd | 3437 | |
ab0ee2ca | 3438 | error: |
f46376a1 | 3439 | notification_client_destroy(client); |
ab0ee2ca JG |
3440 | return ret; |
3441 | } | |
3442 | ||
6c24d3fd JG |
3443 | /* |
3444 | * RCU read-lock must be held by the caller. | |
3445 | * Client lock must _not_ be held by the caller. | |
3446 | */ | |
28ab034a JG |
3447 | static int notification_thread_client_disconnect(struct notification_client *client, |
3448 | struct notification_thread_state *state) | |
505b2d90 JG |
3449 | { |
3450 | int ret; | |
3451 | struct lttng_condition_list_element *condition_list_element, *tmp; | |
3452 | ||
48b7cdc2 FD |
3453 | ASSERT_RCU_READ_LOCKED(); |
3454 | ||
505b2d90 | 3455 | /* Acquire the client lock to disable its communication atomically. */ |
6c24d3fd | 3456 | pthread_mutex_lock(&client->lock); |
505b2d90 | 3457 | client->communication.active = false; |
6c24d3fd JG |
3458 | cds_lfht_del(state->client_socket_ht, &client->client_socket_ht_node); |
3459 | cds_lfht_del(state->client_id_ht, &client->client_id_ht_node); | |
3460 | pthread_mutex_unlock(&client->lock); | |
3461 | ||
505b2d90 JG |
3462 | ret = lttng_poll_del(&state->events, client->socket); |
3463 | if (ret) { | |
28ab034a | 3464 | ERR("Failed to remove client socket %d from poll set", client->socket); |
505b2d90 JG |
3465 | } |
3466 | ||
505b2d90 | 3467 | /* Release all conditions to which the client was subscribed. */ |
28ab034a JG |
3468 | cds_list_for_each_entry_safe (condition_list_element, tmp, &client->condition_list, node) { |
3469 | (void) notification_thread_client_unsubscribe( | |
cd9adb8b | 3470 | client, condition_list_element->condition, state, nullptr); |
505b2d90 JG |
3471 | } |
3472 | ||
3473 | /* | |
3474 | * Client no longer accessible to other threads (through the | |
3475 | * client lists). | |
3476 | */ | |
f46376a1 | 3477 | notification_client_destroy(client); |
505b2d90 JG |
3478 | return ret; |
3479 | } | |
3480 | ||
28ab034a JG |
3481 | int handle_notification_thread_client_disconnect(int client_socket, |
3482 | struct notification_thread_state *state) | |
ab0ee2ca JG |
3483 | { |
3484 | int ret = 0; | |
3485 | struct notification_client *client; | |
3486 | ||
07c4863f | 3487 | const lttng::urcu::read_lock_guard read_lock; |
56047f5a | 3488 | |
28ab034a | 3489 | DBG("Closing client connection (socket fd = %i)", client_socket); |
ab0ee2ca JG |
3490 | client = get_client_from_socket(client_socket, state); |
3491 | if (!client) { | |
3492 | /* Internal state corruption, fatal error. */ | |
28ab034a | 3493 | ERR("Unable to find client (socket fd = %i)", client_socket); |
ab0ee2ca JG |
3494 | ret = -1; |
3495 | goto end; | |
3496 | } | |
3497 | ||
505b2d90 | 3498 | ret = notification_thread_client_disconnect(client, state); |
ab0ee2ca | 3499 | end: |
ab0ee2ca JG |
3500 | return ret; |
3501 | } | |
3502 | ||
28ab034a | 3503 | int handle_notification_thread_client_disconnect_all(struct notification_thread_state *state) |
ab0ee2ca | 3504 | { |
ab0ee2ca JG |
3505 | bool error_encoutered = false; |
3506 | ||
bd0514a5 | 3507 | DBG("Closing all client connections"); |
ab0ee2ca | 3508 | |
6e7ffe66 JG |
3509 | for (auto *client : lttng::urcu::lfht_iteration_adapter< |
3510 | notification_client, | |
3511 | decltype(notification_client::client_socket_ht_node), | |
3512 | ¬ification_client::client_socket_ht_node>(*state->client_socket_ht)) { | |
3513 | int ret; | |
56047f5a | 3514 | |
6e7ffe66 JG |
3515 | ret = notification_thread_client_disconnect(client, state); |
3516 | if (ret) { | |
3517 | error_encoutered = true; | |
ab0ee2ca JG |
3518 | } |
3519 | } | |
56047f5a | 3520 | |
ab0ee2ca JG |
3521 | return error_encoutered ? 1 : 0; |
3522 | } | |
3523 | ||
28ab034a | 3524 | int handle_notification_thread_trigger_unregister_all(struct notification_thread_state *state) |
ab0ee2ca | 3525 | { |
4149ace8 | 3526 | bool error_occurred = false; |
ab0ee2ca | 3527 | |
6e7ffe66 JG |
3528 | for (auto *trigger_ht_element : |
3529 | lttng::urcu::lfht_iteration_adapter<lttng_trigger_ht_element, | |
3530 | decltype(lttng_trigger_ht_element::node), | |
3531 | <tng_trigger_ht_element::node>( | |
3532 | *state->triggers_ht)) { | |
07c4863f | 3533 | const int ret = handle_notification_thread_command_unregister_trigger( |
cd9adb8b | 3534 | state, trigger_ht_element->trigger, nullptr); |
ab0ee2ca | 3535 | if (ret) { |
4149ace8 | 3536 | error_occurred = true; |
ab0ee2ca JG |
3537 | } |
3538 | } | |
6e7ffe66 | 3539 | |
4149ace8 | 3540 | return error_occurred ? -1 : 0; |
ab0ee2ca JG |
3541 | } |
3542 | ||
28ab034a | 3543 | static bool client_has_outbound_data_left(const struct notification_client *client) |
9016dbfc | 3544 | { |
28ab034a JG |
3545 | const struct lttng_payload_view pv = |
3546 | lttng_payload_view_from_payload(&client->communication.outbound.payload, 0, -1); | |
9016dbfc JG |
3547 | const bool has_data = pv.buffer.size != 0; |
3548 | const bool has_fds = lttng_payload_view_get_fd_handle_count(&pv); | |
3549 | ||
3550 | return has_data || has_fds; | |
3551 | } | |
3552 | ||
28ab034a JG |
3553 | static int client_handle_transmission_status(struct notification_client *client, |
3554 | enum client_transmission_status transmission_status, | |
3555 | struct notification_thread_state *state) | |
dd877ea6 JG |
3556 | { |
3557 | int ret = 0; | |
3558 | ||
3559 | switch (transmission_status) { | |
3560 | case CLIENT_TRANSMISSION_STATUS_COMPLETE: | |
dd877ea6 | 3561 | case CLIENT_TRANSMISSION_STATUS_QUEUED: |
9016dbfc JG |
3562 | { |
3563 | int current_poll_events; | |
3564 | int new_poll_events; | |
dd877ea6 JG |
3565 | /* |
3566 | * We want to be notified whenever there is buffer space | |
9016dbfc JG |
3567 | * available to send the rest of the payload if we are |
3568 | * waiting to send data to the client. | |
3569 | * | |
3570 | * The state of the outbound queue being sampled here is | |
3571 | * fine since: | |
3572 | * - it is okay to wake-up "for nothing" in case we see | |
3573 | * that data is left, but another thread succeeds in | |
3574 | * flushing it before us when handling the client "out" | |
3575 | * event. We will simply stop monitoring that event the next | |
3576 | * time it wakes us up and we see no data left to be sent, | |
3577 | * - if another thread fails to flush the entire client | |
3578 | * outgoing queue, it will issue a "communication update" | |
3579 | * command and cause the client's (e)poll mask to be | |
3580 | * re-evaluated. | |
3581 | * | |
3582 | * The situation we seek to avoid would be to disable the | |
3583 | * monitoring of "out" client events indefinitely when there is | |
3584 | * data to be sent, which can't happen because of the | |
3585 | * aforementioned "communication update" mechanism. | |
dd877ea6 | 3586 | */ |
9016dbfc JG |
3587 | pthread_mutex_lock(&client->lock); |
3588 | current_poll_events = client->communication.current_poll_events; | |
3589 | new_poll_events = client_has_outbound_data_left(client) ? | |
28ab034a JG |
3590 | CLIENT_POLL_EVENTS_IN_OUT : |
3591 | CLIENT_POLL_EVENTS_IN; | |
9016dbfc JG |
3592 | client->communication.current_poll_events = new_poll_events; |
3593 | pthread_mutex_unlock(&client->lock); | |
3594 | ||
3595 | /* Update the monitored event set only if it changed. */ | |
3596 | if (current_poll_events != new_poll_events) { | |
28ab034a | 3597 | ret = lttng_poll_mod(&state->events, client->socket, new_poll_events); |
9016dbfc JG |
3598 | if (ret) { |
3599 | goto end; | |
3600 | } | |
dd877ea6 | 3601 | } |
9016dbfc | 3602 | |
dd877ea6 | 3603 | break; |
9016dbfc | 3604 | } |
dd877ea6 JG |
3605 | case CLIENT_TRANSMISSION_STATUS_FAIL: |
3606 | ret = notification_thread_client_disconnect(client, state); | |
3607 | if (ret) { | |
3608 | goto end; | |
3609 | } | |
3610 | break; | |
3611 | case CLIENT_TRANSMISSION_STATUS_ERROR: | |
3612 | ret = -1; | |
3613 | goto end; | |
3614 | default: | |
3615 | abort(); | |
3616 | } | |
3617 | end: | |
3618 | return ret; | |
3619 | } | |
3620 | ||
505b2d90 | 3621 | /* Client lock must be acquired by caller. */ |
28ab034a JG |
3622 | static enum client_transmission_status |
3623 | client_flush_outgoing_queue(struct notification_client *client) | |
ab0ee2ca JG |
3624 | { |
3625 | ssize_t ret; | |
3626 | size_t to_send_count; | |
dd877ea6 | 3627 | enum client_transmission_status status; |
28ab034a JG |
3628 | struct lttng_payload_view pv = |
3629 | lttng_payload_view_from_payload(&client->communication.outbound.payload, 0, -1); | |
3630 | const int fds_to_send_count = lttng_payload_view_get_fd_handle_count(&pv); | |
ab0ee2ca | 3631 | |
505b2d90 JG |
3632 | ASSERT_LOCKED(client->lock); |
3633 | ||
f2b3ef9f JG |
3634 | if (!client->communication.active) { |
3635 | status = CLIENT_TRANSMISSION_STATUS_FAIL; | |
3636 | goto end; | |
3637 | } | |
3638 | ||
882093ee JR |
3639 | if (pv.buffer.size == 0) { |
3640 | /* | |
3641 | * If both data and fds are equal to zero, we are in an invalid | |
3642 | * state. | |
3643 | */ | |
a0377dfe | 3644 | LTTNG_ASSERT(fds_to_send_count != 0); |
882093ee JR |
3645 | goto send_fds; |
3646 | } | |
3647 | ||
3648 | /* Send data. */ | |
3649 | to_send_count = pv.buffer.size; | |
28ab034a | 3650 | DBG("Flushing client (socket fd = %i) outgoing queue", client->socket); |
ab0ee2ca | 3651 | |
28ab034a | 3652 | ret = lttcomm_send_unix_sock_non_block(client->socket, pv.buffer.data, to_send_count); |
44c180ca | 3653 | if ((ret >= 0 && ret < to_send_count)) { |
bd0514a5 | 3654 | DBG("Client (socket fd = %i) outgoing queue could not be completely flushed", |
28ab034a | 3655 | client->socket); |
7966af57 | 3656 | to_send_count -= std::max(ret, (ssize_t) 0); |
ab0ee2ca | 3657 | |
6f110534 | 3658 | memmove(client->communication.outbound.payload.buffer.data, |
28ab034a JG |
3659 | pv.buffer.data + pv.buffer.size - to_send_count, |
3660 | to_send_count); | |
3661 | ret = lttng_dynamic_buffer_set_size(&client->communication.outbound.payload.buffer, | |
3662 | to_send_count); | |
ab0ee2ca JG |
3663 | if (ret) { |
3664 | goto error; | |
3665 | } | |
6c24d3fd | 3666 | |
dd877ea6 | 3667 | status = CLIENT_TRANSMISSION_STATUS_QUEUED; |
882093ee | 3668 | goto end; |
ab0ee2ca | 3669 | } else if (ret < 0) { |
6c24d3fd | 3670 | /* Generic error, disable the client's communication. */ |
bd0514a5 | 3671 | ERR("Failed to flush outgoing queue, disconnecting client (socket fd = %i)", |
28ab034a | 3672 | client->socket); |
6c24d3fd | 3673 | client->communication.active = false; |
dd877ea6 | 3674 | status = CLIENT_TRANSMISSION_STATUS_FAIL; |
882093ee | 3675 | goto end; |
ab0ee2ca | 3676 | } else { |
882093ee JR |
3677 | /* |
3678 | * No error and flushed the queue completely. | |
3679 | * | |
3680 | * The payload buffer size is used later to | |
3681 | * check if there is notifications queued. So albeit that the | |
3682 | * direct caller knows that the transmission is complete, we | |
3683 | * need to set the buffer size to zero. | |
3684 | */ | |
28ab034a JG |
3685 | ret = lttng_dynamic_buffer_set_size(&client->communication.outbound.payload.buffer, |
3686 | 0); | |
ab0ee2ca JG |
3687 | if (ret) { |
3688 | goto error; | |
3689 | } | |
882093ee | 3690 | } |
6c24d3fd | 3691 | |
882093ee JR |
3692 | send_fds: |
3693 | /* No fds to send, transmission is complete. */ | |
3694 | if (fds_to_send_count == 0) { | |
dd877ea6 | 3695 | status = CLIENT_TRANSMISSION_STATUS_COMPLETE; |
882093ee | 3696 | goto end; |
dd877ea6 | 3697 | } |
882093ee | 3698 | |
28ab034a | 3699 | ret = lttcomm_send_payload_view_fds_unix_sock_non_block(client->socket, &pv); |
882093ee JR |
3700 | if (ret < 0) { |
3701 | /* Generic error, disable the client's communication. */ | |
bd0514a5 | 3702 | ERR("Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)", |
28ab034a | 3703 | client->socket); |
882093ee JR |
3704 | client->communication.active = false; |
3705 | status = CLIENT_TRANSMISSION_STATUS_FAIL; | |
3706 | goto end; | |
3707 | } else if (ret == 0) { | |
3708 | /* Nothing could be sent. */ | |
3709 | status = CLIENT_TRANSMISSION_STATUS_QUEUED; | |
3710 | } else { | |
3711 | /* Fd passing is an all or nothing kind of thing. */ | |
3712 | status = CLIENT_TRANSMISSION_STATUS_COMPLETE; | |
3713 | /* | |
3714 | * The payload _fd_array count is used later to | |
3715 | * check if there is notifications queued. So although the | |
3716 | * direct caller knows that the transmission is complete, we | |
3717 | * need to clear the _fd_array for the queuing check. | |
3718 | */ | |
3719 | lttng_dynamic_pointer_array_clear( | |
28ab034a | 3720 | &client->communication.outbound.payload._fd_handles); |
882093ee JR |
3721 | } |
3722 | ||
f2b3ef9f | 3723 | end: |
882093ee JR |
3724 | if (status == CLIENT_TRANSMISSION_STATUS_COMPLETE) { |
3725 | client->communication.outbound.queued_command_reply = false; | |
3726 | client->communication.outbound.dropped_notification = false; | |
3727 | lttng_payload_clear(&client->communication.outbound.payload); | |
3728 | } | |
3729 | ||
f2b3ef9f | 3730 | return status; |
ab0ee2ca | 3731 | error: |
f2b3ef9f | 3732 | return CLIENT_TRANSMISSION_STATUS_ERROR; |
ab0ee2ca JG |
3733 | } |
3734 | ||
6c24d3fd | 3735 | /* Client lock must _not_ be held by the caller. */ |
28ab034a JG |
3736 | static int client_send_command_reply(struct notification_client *client, |
3737 | struct notification_thread_state *state, | |
3738 | enum lttng_notification_channel_status status) | |
ab0ee2ca JG |
3739 | { |
3740 | int ret; | |
3741 | struct lttng_notification_channel_command_reply reply = { | |
3742 | .status = (int8_t) status, | |
3743 | }; | |
6bd79809 | 3744 | struct lttng_notification_channel_message msg; |
ab0ee2ca | 3745 | char buffer[sizeof(msg) + sizeof(reply)]; |
f2b3ef9f | 3746 | enum client_transmission_status transmission_status; |
ab0ee2ca | 3747 | |
6bd79809 JG |
3748 | msg.type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY; |
3749 | msg.size = sizeof(reply); | |
3750 | msg.fds = 0; | |
3751 | ||
6c24d3fd JG |
3752 | memcpy(buffer, &msg, sizeof(msg)); |
3753 | memcpy(buffer + sizeof(msg), &reply, sizeof(reply)); | |
bd0514a5 | 3754 | DBG("Send command reply (%i)", (int) status); |
505b2d90 | 3755 | |
6c24d3fd | 3756 | pthread_mutex_lock(&client->lock); |
ab0ee2ca JG |
3757 | if (client->communication.outbound.queued_command_reply) { |
3758 | /* Protocol error. */ | |
6c24d3fd | 3759 | goto error_unlock; |
ab0ee2ca JG |
3760 | } |
3761 | ||
ab0ee2ca JG |
3762 | /* Enqueue buffer to outgoing queue and flush it. */ |
3763 | ret = lttng_dynamic_buffer_append( | |
28ab034a | 3764 | &client->communication.outbound.payload.buffer, buffer, sizeof(buffer)); |
ab0ee2ca | 3765 | if (ret) { |
6c24d3fd | 3766 | goto error_unlock; |
ab0ee2ca JG |
3767 | } |
3768 | ||
f2b3ef9f | 3769 | transmission_status = client_flush_outgoing_queue(client); |
882093ee JR |
3770 | |
3771 | if (client_has_outbound_data_left(client)) { | |
6c24d3fd JG |
3772 | /* Queue could not be emptied. */ |
3773 | client->communication.outbound.queued_command_reply = true; | |
3774 | } | |
3775 | ||
3776 | pthread_mutex_unlock(&client->lock); | |
28ab034a | 3777 | ret = client_handle_transmission_status(client, transmission_status, state); |
ab0ee2ca JG |
3778 | if (ret) { |
3779 | goto error; | |
3780 | } | |
3781 | ||
ab0ee2ca | 3782 | return 0; |
6c24d3fd JG |
3783 | error_unlock: |
3784 | pthread_mutex_unlock(&client->lock); | |
ab0ee2ca JG |
3785 | error: |
3786 | return -1; | |
3787 | } | |
3788 | ||
28ab034a JG |
3789 | static int client_handle_message_unknown(struct notification_client *client, |
3790 | struct notification_thread_state *state | |
3791 | __attribute__((unused))) | |
505b2d90 JG |
3792 | { |
3793 | int ret; | |
505b2d90 JG |
3794 | /* |
3795 | * Receiving message header. The function will be called again | |
3796 | * once the rest of the message as been received and can be | |
3797 | * interpreted. | |
3798 | */ | |
3799 | const struct lttng_notification_channel_message *msg; | |
3800 | ||
a0377dfe | 3801 | LTTNG_ASSERT(sizeof(*msg) == client->communication.inbound.payload.buffer.size); |
505b2d90 | 3802 | msg = (const struct lttng_notification_channel_message *) |
28ab034a | 3803 | client->communication.inbound.payload.buffer.data; |
505b2d90 | 3804 | |
28ab034a JG |
3805 | if (msg->size == 0 || msg->size > DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE) { |
3806 | ERR("Invalid notification channel message: length = %u", msg->size); | |
505b2d90 JG |
3807 | ret = -1; |
3808 | goto end; | |
3809 | } | |
3810 | ||
3811 | switch (msg->type) { | |
3812 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE: | |
3813 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE: | |
3814 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE: | |
3815 | break; | |
3816 | default: | |
3817 | ret = -1; | |
bd0514a5 | 3818 | ERR("Invalid notification channel message: unexpected message type"); |
505b2d90 JG |
3819 | goto end; |
3820 | } | |
3821 | ||
3822 | client->communication.inbound.bytes_to_receive = msg->size; | |
882093ee | 3823 | client->communication.inbound.fds_to_receive = msg->fds; |
505b2d90 | 3824 | client->communication.inbound.msg_type = |
28ab034a JG |
3825 | (enum lttng_notification_channel_message_type) msg->type; |
3826 | ret = lttng_dynamic_buffer_set_size(&client->communication.inbound.payload.buffer, | |
3827 | msg->size); | |
882093ee JR |
3828 | |
3829 | /* msg is not valid anymore due to lttng_dynamic_buffer_set_size. */ | |
cd9adb8b | 3830 | msg = nullptr; |
505b2d90 | 3831 | end: |
505b2d90 JG |
3832 | return ret; |
3833 | } | |
3834 | ||
28ab034a JG |
3835 | static int client_handle_message_handshake(struct notification_client *client, |
3836 | struct notification_thread_state *state) | |
505b2d90 JG |
3837 | { |
3838 | int ret; | |
3839 | struct lttng_notification_channel_command_handshake *handshake_client; | |
3840 | const struct lttng_notification_channel_command_handshake handshake_reply = { | |
28ab034a JG |
3841 | .major = LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR, |
3842 | .minor = LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR, | |
505b2d90 | 3843 | }; |
6bd79809 | 3844 | struct lttng_notification_channel_message msg_header; |
28ab034a | 3845 | enum lttng_notification_channel_status status = LTTNG_NOTIFICATION_CHANNEL_STATUS_OK; |
505b2d90 | 3846 | char send_buffer[sizeof(msg_header) + sizeof(handshake_reply)]; |
505b2d90 | 3847 | |
6bd79809 JG |
3848 | msg_header.type = LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE; |
3849 | msg_header.size = sizeof(handshake_reply); | |
3850 | msg_header.fds = 0; | |
3851 | ||
505b2d90 | 3852 | memcpy(send_buffer, &msg_header, sizeof(msg_header)); |
28ab034a | 3853 | memcpy(send_buffer + sizeof(msg_header), &handshake_reply, sizeof(handshake_reply)); |
505b2d90 | 3854 | |
28ab034a JG |
3855 | handshake_client = (struct lttng_notification_channel_command_handshake *) |
3856 | client->communication.inbound.payload.buffer.data; | |
505b2d90 JG |
3857 | client->major = handshake_client->major; |
3858 | client->minor = handshake_client->minor; | |
3859 | if (!client->communication.inbound.creds_received) { | |
bd0514a5 | 3860 | ERR("No credentials received from client"); |
505b2d90 JG |
3861 | ret = -1; |
3862 | goto end; | |
3863 | } | |
3864 | ||
28ab034a JG |
3865 | client->uid = LTTNG_SOCK_GET_UID_CRED(&client->communication.inbound.creds); |
3866 | client->gid = LTTNG_SOCK_GET_GID_CRED(&client->communication.inbound.creds); | |
3867 | client->is_sessiond = LTTNG_SOCK_GET_PID_CRED(&client->communication.inbound.creds) == | |
3868 | getpid(); | |
f2bda80e | 3869 | DBG("Received handshake from client: uid = %u, gid = %u, protocol version = %i.%i, client is sessiond = %s", |
28ab034a JG |
3870 | client->uid, |
3871 | client->gid, | |
3872 | (int) client->major, | |
3873 | (int) client->minor, | |
3874 | client->is_sessiond ? "true" : "false"); | |
505b2d90 | 3875 | |
28ab034a | 3876 | if (handshake_client->major != LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR) { |
505b2d90 JG |
3877 | status = LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION; |
3878 | } | |
3879 | ||
6c24d3fd JG |
3880 | pthread_mutex_lock(&client->lock); |
3881 | /* Outgoing queue will be flushed when the command reply is sent. */ | |
505b2d90 | 3882 | ret = lttng_dynamic_buffer_append( |
28ab034a | 3883 | &client->communication.outbound.payload.buffer, send_buffer, sizeof(send_buffer)); |
505b2d90 | 3884 | if (ret) { |
bd0514a5 | 3885 | ERR("Failed to send protocol version to notification channel client"); |
6c24d3fd | 3886 | goto end_unlock; |
505b2d90 JG |
3887 | } |
3888 | ||
3889 | client->validated = true; | |
3890 | client->communication.active = true; | |
6c24d3fd | 3891 | pthread_mutex_unlock(&client->lock); |
505b2d90 | 3892 | |
6c24d3fd JG |
3893 | /* Set reception state to receive the next message header. */ |
3894 | ret = client_reset_inbound_state(client); | |
505b2d90 | 3895 | if (ret) { |
bd0514a5 | 3896 | ERR("Failed to reset client communication's inbound state"); |
505b2d90 JG |
3897 | goto end; |
3898 | } | |
3899 | ||
6c24d3fd | 3900 | /* Flushes the outgoing queue. */ |
505b2d90 JG |
3901 | ret = client_send_command_reply(client, state, status); |
3902 | if (ret) { | |
bd0514a5 | 3903 | ERR("Failed to send reply to notification channel client"); |
505b2d90 JG |
3904 | goto end; |
3905 | } | |
3906 | ||
6c24d3fd JG |
3907 | goto end; |
3908 | end_unlock: | |
505b2d90 | 3909 | pthread_mutex_unlock(&client->lock); |
6c24d3fd | 3910 | end: |
505b2d90 JG |
3911 | return ret; |
3912 | } | |
3913 | ||
28ab034a JG |
3914 | static int client_handle_message_subscription(struct notification_client *client, |
3915 | enum lttng_notification_channel_message_type msg_type, | |
3916 | struct notification_thread_state *state) | |
505b2d90 JG |
3917 | { |
3918 | int ret; | |
3919 | struct lttng_condition *condition; | |
28ab034a | 3920 | enum lttng_notification_channel_status status = LTTNG_NOTIFICATION_CHANNEL_STATUS_OK; |
505b2d90 | 3921 | struct lttng_payload_view condition_view = |
28ab034a | 3922 | lttng_payload_view_from_payload(&client->communication.inbound.payload, 0, -1); |
505b2d90 JG |
3923 | size_t expected_condition_size; |
3924 | ||
6c24d3fd JG |
3925 | /* |
3926 | * No need to lock client to sample the inbound state as the only | |
3927 | * other thread accessing clients (action executor) only uses the | |
3928 | * outbound state. | |
3929 | */ | |
882093ee | 3930 | expected_condition_size = client->communication.inbound.payload.buffer.size; |
505b2d90 JG |
3931 | ret = lttng_condition_create_from_payload(&condition_view, &condition); |
3932 | if (ret != expected_condition_size) { | |
bd0514a5 | 3933 | ERR("Malformed condition received from client"); |
505b2d90 JG |
3934 | goto end; |
3935 | } | |
3936 | ||
5a6306f7 | 3937 | /* Ownership of condition is always transferred. */ |
505b2d90 | 3938 | if (msg_type == LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE) { |
28ab034a | 3939 | ret = notification_thread_client_subscribe(client, condition, state, &status); |
505b2d90 | 3940 | } else { |
28ab034a | 3941 | ret = notification_thread_client_unsubscribe(client, condition, state, &status); |
505b2d90 | 3942 | } |
505b2d90 | 3943 | |
505b2d90 | 3944 | if (ret) { |
6c24d3fd | 3945 | goto end; |
505b2d90 JG |
3946 | } |
3947 | ||
3948 | /* Set reception state to receive the next message header. */ | |
3949 | ret = client_reset_inbound_state(client); | |
3950 | if (ret) { | |
bd0514a5 | 3951 | ERR("Failed to reset client communication's inbound state"); |
6c24d3fd JG |
3952 | goto end; |
3953 | } | |
3954 | ||
3955 | ret = client_send_command_reply(client, state, status); | |
3956 | if (ret) { | |
bd0514a5 | 3957 | ERR("Failed to send reply to notification channel client"); |
6c24d3fd | 3958 | goto end; |
505b2d90 JG |
3959 | } |
3960 | ||
505b2d90 JG |
3961 | end: |
3962 | return ret; | |
3963 | } | |
3964 | ||
28ab034a JG |
3965 | static int client_dispatch_message(struct notification_client *client, |
3966 | struct notification_thread_state *state) | |
ab0ee2ca JG |
3967 | { |
3968 | int ret = 0; | |
3969 | ||
3970 | if (client->communication.inbound.msg_type != | |
28ab034a JG |
3971 | LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE && |
3972 | client->communication.inbound.msg_type != | |
3973 | LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN && | |
3974 | !client->validated) { | |
bd0514a5 | 3975 | WARN("client attempted a command before handshake"); |
ab0ee2ca JG |
3976 | ret = -1; |
3977 | goto end; | |
3978 | } | |
3979 | ||
3980 | switch (client->communication.inbound.msg_type) { | |
3981 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN: | |
3982 | { | |
505b2d90 | 3983 | ret = client_handle_message_unknown(client, state); |
ab0ee2ca JG |
3984 | break; |
3985 | } | |
3986 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE: | |
3987 | { | |
505b2d90 | 3988 | ret = client_handle_message_handshake(client, state); |
ab0ee2ca JG |
3989 | break; |
3990 | } | |
3991 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE: | |
3992 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE: | |
3993 | { | |
28ab034a JG |
3994 | ret = client_handle_message_subscription( |
3995 | client, client->communication.inbound.msg_type, state); | |
ab0ee2ca JG |
3996 | break; |
3997 | } | |
3998 | default: | |
3999 | abort(); | |
4000 | } | |
4001 | end: | |
4002 | return ret; | |
4003 | } | |
4004 | ||
4005 | /* Incoming data from client. */ | |
28ab034a | 4006 | int handle_notification_thread_client_in(struct notification_thread_state *state, int socket) |
ab0ee2ca | 4007 | { |
14fa22f8 | 4008 | int ret = 0; |
ab0ee2ca JG |
4009 | struct notification_client *client; |
4010 | ssize_t recv_ret; | |
4011 | size_t offset; | |
4012 | ||
07c4863f | 4013 | const lttng::urcu::read_lock_guard read_lock; |
ab0ee2ca JG |
4014 | client = get_client_from_socket(socket, state); |
4015 | if (!client) { | |
4016 | /* Internal error, abort. */ | |
4017 | ret = -1; | |
4018 | goto end; | |
4019 | } | |
4020 | ||
982583bd | 4021 | if (client->communication.inbound.bytes_to_receive == 0 && |
28ab034a | 4022 | client->communication.inbound.fds_to_receive != 0) { |
982583bd JR |
4023 | /* Only FDs left to receive. */ |
4024 | goto receive_fds; | |
4025 | } | |
4026 | ||
882093ee | 4027 | offset = client->communication.inbound.payload.buffer.size - |
28ab034a | 4028 | client->communication.inbound.bytes_to_receive; |
01ea340e | 4029 | if (client->communication.inbound.expect_creds) { |
28ab034a JG |
4030 | recv_ret = lttcomm_recv_creds_unix_sock( |
4031 | socket, | |
4032 | client->communication.inbound.payload.buffer.data + offset, | |
4033 | client->communication.inbound.bytes_to_receive, | |
4034 | &client->communication.inbound.creds); | |
ab0ee2ca | 4035 | if (recv_ret > 0) { |
01ea340e | 4036 | client->communication.inbound.expect_creds = false; |
ab0ee2ca JG |
4037 | client->communication.inbound.creds_received = true; |
4038 | } | |
4039 | } else { | |
28ab034a JG |
4040 | recv_ret = lttcomm_recv_unix_sock_non_block( |
4041 | socket, | |
4042 | client->communication.inbound.payload.buffer.data + offset, | |
4043 | client->communication.inbound.bytes_to_receive); | |
ab0ee2ca | 4044 | } |
505b2d90 JG |
4045 | if (recv_ret >= 0) { |
4046 | client->communication.inbound.bytes_to_receive -= recv_ret; | |
882093ee JR |
4047 | } else { |
4048 | goto error_disconnect_client; | |
505b2d90 | 4049 | } |
6c24d3fd | 4050 | |
882093ee JR |
4051 | if (client->communication.inbound.bytes_to_receive != 0) { |
4052 | /* Message incomplete wait for more data. */ | |
4053 | ret = 0; | |
4054 | goto end; | |
ab0ee2ca JG |
4055 | } |
4056 | ||
982583bd | 4057 | receive_fds: |
a0377dfe | 4058 | LTTNG_ASSERT(client->communication.inbound.bytes_to_receive == 0); |
882093ee JR |
4059 | |
4060 | /* Receive fds. */ | |
4061 | if (client->communication.inbound.fds_to_receive != 0) { | |
4062 | ret = lttcomm_recv_payload_fds_unix_sock_non_block( | |
28ab034a JG |
4063 | client->socket, |
4064 | client->communication.inbound.fds_to_receive, | |
4065 | &client->communication.inbound.payload); | |
882093ee | 4066 | if (ret > 0) { |
ab0ee2ca | 4067 | /* |
882093ee JR |
4068 | * Fds received. non blocking fds passing is all |
4069 | * or nothing. | |
ab0ee2ca | 4070 | */ |
882093ee JR |
4071 | ssize_t expected_size; |
4072 | ||
28ab034a | 4073 | expected_size = sizeof(int) * client->communication.inbound.fds_to_receive; |
a0377dfe | 4074 | LTTNG_ASSERT(ret == expected_size); |
882093ee JR |
4075 | client->communication.inbound.fds_to_receive = 0; |
4076 | } else if (ret == 0) { | |
4077 | /* Received nothing. */ | |
4078 | ret = 0; | |
4079 | goto end; | |
4080 | } else { | |
ab0ee2ca JG |
4081 | goto error_disconnect_client; |
4082 | } | |
ab0ee2ca | 4083 | } |
882093ee JR |
4084 | |
4085 | /* At this point the message is complete.*/ | |
a0377dfe | 4086 | LTTNG_ASSERT(client->communication.inbound.bytes_to_receive == 0 && |
28ab034a | 4087 | client->communication.inbound.fds_to_receive == 0); |
882093ee JR |
4088 | ret = client_dispatch_message(client, state); |
4089 | if (ret) { | |
4090 | /* | |
4091 | * Only returns an error if this client must be | |
4092 | * disconnected. | |
4093 | */ | |
4094 | goto error_disconnect_client; | |
4095 | } | |
4096 | ||
ab0ee2ca JG |
4097 | end: |
4098 | return ret; | |
882093ee | 4099 | |
ab0ee2ca | 4100 | error_disconnect_client: |
505b2d90 | 4101 | ret = notification_thread_client_disconnect(client, state); |
98b5ff34 | 4102 | goto end; |
ab0ee2ca JG |
4103 | } |
4104 | ||
4105 | /* Client ready to receive outgoing data. */ | |
28ab034a | 4106 | int handle_notification_thread_client_out(struct notification_thread_state *state, int socket) |
ab0ee2ca JG |
4107 | { |
4108 | int ret; | |
4109 | struct notification_client *client; | |
f2b3ef9f | 4110 | enum client_transmission_status transmission_status; |
ab0ee2ca | 4111 | |
07c4863f | 4112 | const lttng::urcu::read_lock_guard read_lock; |
ab0ee2ca JG |
4113 | client = get_client_from_socket(socket, state); |
4114 | if (!client) { | |
4115 | /* Internal error, abort. */ | |
4116 | ret = -1; | |
4117 | goto end; | |
4118 | } | |
4119 | ||
505b2d90 | 4120 | pthread_mutex_lock(&client->lock); |
9016dbfc JG |
4121 | if (!client_has_outbound_data_left(client)) { |
4122 | /* | |
4123 | * A client "out" event can be received when no payload is left | |
4124 | * to send under some circumstances. | |
4125 | * | |
4126 | * Many threads can flush a client's outgoing queue and, if they | |
4127 | * had to queue their message (socket was full), will use the | |
4128 | * "communication update" command to signal the (e)poll thread | |
4129 | * to monitor for space being made available in the socket. | |
4130 | * | |
4131 | * Commands are sent over an internal pipe serviced by the same | |
4132 | * thread as the client sockets. | |
4133 | * | |
4134 | * When space is made available in the socket, there is a race | |
4135 | * between the (e)poll thread and the other threads that may | |
4136 | * wish to use the client's socket to flush its outgoing queue. | |
4137 | * | |
4138 | * A non-(e)poll thread may attempt (and succeed) in flushing | |
4139 | * the queue before the (e)poll thread gets a chance to service | |
4140 | * the client's "out" event. | |
4141 | * | |
4142 | * In this situation, the (e)poll thread processing the client | |
4143 | * out event will see an empty payload: there is nothing to do | |
4144 | * except unsubscribing (e)poll "out" events. | |
4145 | * | |
4146 | * Note that this thread is the (e)poll thread so it can modify | |
4147 | * the (e)poll mask directly without using a communication | |
4148 | * update command. Other threads that flush the outgoing queue | |
4149 | * will use the "communication update" command to wake up this | |
4150 | * thread and force it to monitor "out" events. | |
4151 | * | |
4152 | * When other threads succeed in emptying the outgoing queue, | |
4153 | * they don't need to update the (e)poll mask: if the "out" | |
4154 | * event is monitored, it will fire once and the (e)poll | |
4155 | * thread will reach this condition, causing the event to | |
4156 | * stop being monitored. | |
4157 | */ | |
4158 | transmission_status = CLIENT_TRANSMISSION_STATUS_COMPLETE; | |
4159 | } else { | |
4160 | transmission_status = client_flush_outgoing_queue(client); | |
4161 | } | |
6c24d3fd JG |
4162 | pthread_mutex_unlock(&client->lock); |
4163 | ||
28ab034a | 4164 | ret = client_handle_transmission_status(client, transmission_status, state); |
ab0ee2ca JG |
4165 | if (ret) { |
4166 | goto end; | |
4167 | } | |
4168 | end: | |
4169 | return ret; | |
4170 | } | |
4171 | ||
28ab034a JG |
4172 | static bool evaluate_buffer_usage_condition(const struct lttng_condition *condition, |
4173 | const struct channel_state_sample *sample, | |
4174 | uint64_t buffer_capacity) | |
ab0ee2ca JG |
4175 | { |
4176 | bool result = false; | |
4177 | uint64_t threshold; | |
4178 | enum lttng_condition_type condition_type; | |
28ab034a JG |
4179 | const struct lttng_condition_buffer_usage *use_condition = |
4180 | lttng::utils::container_of(condition, <tng_condition_buffer_usage::parent); | |
ab0ee2ca | 4181 | |
ab0ee2ca JG |
4182 | if (use_condition->threshold_bytes.set) { |
4183 | threshold = use_condition->threshold_bytes.value; | |
4184 | } else { | |
4185 | /* | |
4186 | * Threshold was expressed as a ratio. | |
4187 | * | |
4188 | * TODO the threshold (in bytes) of conditions expressed | |
4189 | * as a ratio of total buffer size could be cached to | |
4190 | * forego this double-multiplication or it could be performed | |
4191 | * as fixed-point math. | |
4192 | * | |
d49487dc | 4193 | * Note that caching should accommodates the case where the |
ab0ee2ca JG |
4194 | * condition applies to multiple channels (i.e. don't assume |
4195 | * that all channels matching my_chann* have the same size...) | |
4196 | */ | |
4197 | threshold = (uint64_t) (use_condition->threshold_ratio.value * | |
28ab034a | 4198 | (double) buffer_capacity); |
ab0ee2ca JG |
4199 | } |
4200 | ||
4201 | condition_type = lttng_condition_get_type(condition); | |
4202 | if (condition_type == LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW) { | |
28ab034a JG |
4203 | DBG("Low buffer usage condition being evaluated: threshold = %" PRIu64 |
4204 | ", highest usage = %" PRIu64, | |
4205 | threshold, | |
4206 | sample->highest_usage); | |
ab0ee2ca JG |
4207 | |
4208 | /* | |
4209 | * The low condition should only be triggered once _all_ of the | |
4210 | * streams in a channel have gone below the "low" threshold. | |
4211 | */ | |
4212 | if (sample->highest_usage <= threshold) { | |
4213 | result = true; | |
4214 | } | |
4215 | } else { | |
28ab034a JG |
4216 | DBG("High buffer usage condition being evaluated: threshold = %" PRIu64 |
4217 | ", highest usage = %" PRIu64, | |
4218 | threshold, | |
4219 | sample->highest_usage); | |
ab0ee2ca JG |
4220 | |
4221 | /* | |
4222 | * For high buffer usage scenarios, we want to trigger whenever | |
4223 | * _any_ of the streams has reached the "high" threshold. | |
4224 | */ | |
4225 | if (sample->highest_usage >= threshold) { | |
4226 | result = true; | |
4227 | } | |
4228 | } | |
e8360425 | 4229 | |
ab0ee2ca JG |
4230 | return result; |
4231 | } | |
4232 | ||
28ab034a JG |
4233 | static int evaluate_buffer_condition(const struct lttng_condition *condition, |
4234 | struct lttng_evaluation **evaluation, | |
4235 | const struct notification_thread_state *state | |
4236 | __attribute__((unused)), | |
4237 | const struct channel_state_sample *previous_sample, | |
4238 | const struct channel_state_sample *latest_sample, | |
4239 | struct channel_info *channel_info) | |
ab0ee2ca JG |
4240 | { |
4241 | int ret = 0; | |
4242 | enum lttng_condition_type condition_type; | |
e8360425 JD |
4243 | const bool previous_sample_available = !!previous_sample; |
4244 | bool previous_sample_result = false; | |
ab0ee2ca JG |
4245 | bool latest_sample_result; |
4246 | ||
4247 | condition_type = lttng_condition_get_type(condition); | |
ab0ee2ca | 4248 | |
e8360425 JD |
4249 | switch (condition_type) { |
4250 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
4251 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
4252 | if (caa_likely(previous_sample_available)) { | |
28ab034a JG |
4253 | previous_sample_result = evaluate_buffer_usage_condition( |
4254 | condition, previous_sample, channel_info->capacity); | |
e8360425 JD |
4255 | } |
4256 | latest_sample_result = evaluate_buffer_usage_condition( | |
28ab034a | 4257 | condition, latest_sample, channel_info->capacity); |
e8360425 | 4258 | break; |
e8360425 JD |
4259 | default: |
4260 | /* Unknown condition type; internal error. */ | |
4261 | abort(); | |
4262 | } | |
ab0ee2ca | 4263 | |
28ab034a | 4264 | if (!latest_sample_result || (previous_sample_result == latest_sample_result)) { |
ab0ee2ca JG |
4265 | /* |
4266 | * Only trigger on a condition evaluation transition. | |
4267 | * | |
4268 | * NOTE: This edge-triggered logic may not be appropriate for | |
4269 | * future condition types. | |
4270 | */ | |
4271 | goto end; | |
4272 | } | |
4273 | ||
e8360425 JD |
4274 | if (!evaluation || !latest_sample_result) { |
4275 | goto end; | |
4276 | } | |
4277 | ||
4278 | switch (condition_type) { | |
4279 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
4280 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
ab0ee2ca | 4281 | *evaluation = lttng_evaluation_buffer_usage_create( |
28ab034a | 4282 | condition_type, latest_sample->highest_usage, channel_info->capacity); |
e8360425 | 4283 | break; |
e8360425 JD |
4284 | default: |
4285 | abort(); | |
4286 | } | |
4287 | ||
4288 | if (!*evaluation) { | |
4289 | ret = -1; | |
4290 | goto end; | |
ab0ee2ca JG |
4291 | } |
4292 | end: | |
4293 | return ret; | |
4294 | } | |
4295 | ||
28ab034a | 4296 | static int client_notification_overflow(struct notification_client *client) |
ab0ee2ca | 4297 | { |
f2b3ef9f | 4298 | int ret = 0; |
6bd79809 JG |
4299 | struct lttng_notification_channel_message msg; |
4300 | ||
4301 | msg.type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED; | |
4302 | msg.size = 0; | |
4303 | msg.fds = 0; | |
ab0ee2ca | 4304 | |
505b2d90 JG |
4305 | ASSERT_LOCKED(client->lock); |
4306 | ||
28ab034a | 4307 | DBG("Dropping notification addressed to client (socket fd = %i)", client->socket); |
f2b3ef9f JG |
4308 | if (client->communication.outbound.dropped_notification) { |
4309 | /* | |
4310 | * The client already has a "notification dropped" message | |
4311 | * in its outgoing queue. Nothing to do since all | |
4312 | * of those messages are coalesced. | |
4313 | */ | |
4314 | goto end; | |
4315 | } | |
4316 | ||
4317 | client->communication.outbound.dropped_notification = true; | |
ab0ee2ca | 4318 | ret = lttng_dynamic_buffer_append( |
28ab034a | 4319 | &client->communication.outbound.payload.buffer, &msg, sizeof(msg)); |
f2b3ef9f JG |
4320 | if (ret) { |
4321 | PERROR("Failed to enqueue \"dropped notification\" message in client's (socket fd = %i) outgoing queue", | |
28ab034a | 4322 | client->socket); |
f2b3ef9f JG |
4323 | } |
4324 | end: | |
ab0ee2ca JG |
4325 | return ret; |
4326 | } | |
4327 | ||
28ab034a JG |
4328 | static int client_handle_transmission_status_wrapper(struct notification_client *client, |
4329 | enum client_transmission_status status, | |
4330 | void *user_data) | |
f2b3ef9f | 4331 | { |
28ab034a JG |
4332 | return client_handle_transmission_status( |
4333 | client, status, (struct notification_thread_state *) user_data); | |
f2b3ef9f JG |
4334 | } |
4335 | ||
28ab034a JG |
4336 | static int send_evaluation_to_clients(const struct lttng_trigger *trigger, |
4337 | const struct lttng_evaluation *evaluation, | |
4338 | struct notification_client_list *client_list, | |
4339 | struct notification_thread_state *state, | |
4340 | uid_t object_uid, | |
4341 | gid_t object_gid) | |
f2b3ef9f | 4342 | { |
ff588497 JR |
4343 | const struct lttng_credentials creds = { |
4344 | .uid = LTTNG_OPTIONAL_INIT_VALUE(object_uid), | |
4345 | .gid = LTTNG_OPTIONAL_INIT_VALUE(object_gid), | |
4346 | }; | |
4347 | ||
f2b3ef9f | 4348 | return notification_client_list_send_evaluation(client_list, |
28ab034a JG |
4349 | trigger, |
4350 | evaluation, | |
4351 | &creds, | |
4352 | client_handle_transmission_status_wrapper, | |
4353 | state); | |
f2b3ef9f JG |
4354 | } |
4355 | ||
f37d0f86 JG |
4356 | /* |
4357 | * Permission checks relative to notification channel clients are performed | |
4358 | * here. Notice how object, client, and trigger credentials are involved in | |
4359 | * this check. | |
4360 | * | |
4361 | * The `object` credentials are the credentials associated with the "subject" | |
4362 | * of a condition. For instance, a `rotation completed` condition applies | |
4363 | * to a session. When that condition is met, it will produce an evaluation | |
4364 | * against a session. Hence, in this case, the `object` credentials are the | |
4365 | * credentials of the "subject" session. | |
4366 | * | |
4367 | * The `trigger` credentials are the credentials of the user that registered the | |
4368 | * trigger. | |
4369 | * | |
4370 | * The `client` credentials are the credentials of the user that created a given | |
4371 | * notification channel. | |
4372 | * | |
4373 | * In terms of visibility, it is expected that non-privilieged users can only | |
4374 | * register triggers against "their" objects (their own sessions and | |
4375 | * applications they are allowed to interact with). They can then open a | |
4376 | * notification channel and subscribe to notifications associated with those | |
4377 | * triggers. | |
4378 | * | |
4379 | * As for privilieged users, they can register triggers against the objects of | |
4380 | * other users. They can then subscribe to the notifications associated to their | |
4381 | * triggers. Privilieged users _can't_ subscribe to the notifications of | |
4382 | * triggers owned by other users; they must create their own triggers. | |
4383 | * | |
4384 | * This is more a concern of usability than security. It would be difficult for | |
4385 | * a root user reliably subscribe to a specific set of conditions without | |
4386 | * interference from external users (those could, for instance, unregister | |
4387 | * their triggers). | |
4388 | */ | |
28ab034a JG |
4389 | int notification_client_list_send_evaluation(struct notification_client_list *client_list, |
4390 | const struct lttng_trigger *trigger, | |
4391 | const struct lttng_evaluation *evaluation, | |
4392 | const struct lttng_credentials *source_object_creds, | |
4393 | report_client_transmission_result_cb client_report, | |
4394 | void *user_data) | |
ab0ee2ca JG |
4395 | { |
4396 | int ret = 0; | |
c0a66c84 | 4397 | struct lttng_payload msg_payload; |
ab0ee2ca | 4398 | struct notification_client_list_element *client_list_element, *tmp; |
9b63a4aa | 4399 | const struct lttng_notification notification = { |
52d55cf9 | 4400 | .trigger = (struct lttng_trigger *) trigger, |
9b63a4aa JG |
4401 | .evaluation = (struct lttng_evaluation *) evaluation, |
4402 | }; | |
6bd79809 | 4403 | struct lttng_notification_channel_message msg_header; |
28ab034a | 4404 | const struct lttng_credentials *trigger_creds = lttng_trigger_get_credentials(trigger); |
ab0ee2ca | 4405 | |
c0a66c84 | 4406 | lttng_payload_init(&msg_payload); |
ab0ee2ca | 4407 | |
6bd79809 JG |
4408 | msg_header.type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION; |
4409 | msg_header.size = 0; | |
4410 | msg_header.fds = 0; | |
4411 | ||
28ab034a | 4412 | ret = lttng_dynamic_buffer_append(&msg_payload.buffer, &msg_header, sizeof(msg_header)); |
ab0ee2ca JG |
4413 | if (ret) { |
4414 | goto end; | |
4415 | } | |
4416 | ||
c0a66c84 | 4417 | ret = lttng_notification_serialize(¬ification, &msg_payload); |
ab0ee2ca | 4418 | if (ret) { |
bd0514a5 | 4419 | ERR("Failed to serialize notification"); |
ab0ee2ca JG |
4420 | ret = -1; |
4421 | goto end; | |
4422 | } | |
4423 | ||
3647288f | 4424 | /* Update payload size. */ |
28ab034a JG |
4425 | ((struct lttng_notification_channel_message *) msg_payload.buffer.data)->size = |
4426 | (uint32_t) (msg_payload.buffer.size - sizeof(msg_header)); | |
3647288f | 4427 | |
882093ee JR |
4428 | /* Update the payload number of fds. */ |
4429 | { | |
28ab034a JG |
4430 | const struct lttng_payload_view pv = |
4431 | lttng_payload_view_from_payload(&msg_payload, 0, -1); | |
882093ee | 4432 | |
28ab034a JG |
4433 | ((struct lttng_notification_channel_message *) msg_payload.buffer.data)->fds = |
4434 | (uint32_t) lttng_payload_view_get_fd_handle_count(&pv); | |
882093ee JR |
4435 | } |
4436 | ||
505b2d90 | 4437 | pthread_mutex_lock(&client_list->lock); |
28ab034a | 4438 | cds_list_for_each_entry_safe (client_list_element, tmp, &client_list->clients_list, node) { |
f2b3ef9f | 4439 | enum client_transmission_status transmission_status; |
28ab034a | 4440 | struct notification_client *client = client_list_element->client; |
ab0ee2ca | 4441 | |
505b2d90 JG |
4442 | ret = 0; |
4443 | pthread_mutex_lock(&client->lock); | |
6c24d3fd JG |
4444 | if (!client->communication.active) { |
4445 | /* | |
4446 | * Skip inactive client (protocol error or | |
4447 | * disconnecting). | |
4448 | */ | |
4449 | DBG("Skipping client at it is marked as inactive"); | |
4450 | goto skip_client; | |
4451 | } | |
4452 | ||
f2bda80e JG |
4453 | if (lttng_trigger_is_hidden(trigger) && !client->is_sessiond) { |
4454 | /* | |
4455 | * Notifications resulting from an hidden trigger are | |
4456 | * only sent to the session daemon. | |
4457 | */ | |
70eb2f6f JG |
4458 | DBG("Skipping client as the trigger is hidden and the client is not the session daemon"); |
4459 | goto skip_client; | |
f2bda80e JG |
4460 | } |
4461 | ||
f2b3ef9f | 4462 | if (source_object_creds) { |
ff588497 | 4463 | if (client->uid != lttng_credentials_get_uid(source_object_creds) && |
28ab034a JG |
4464 | client->gid != lttng_credentials_get_gid(source_object_creds) && |
4465 | client->uid != 0) { | |
f2b3ef9f JG |
4466 | /* |
4467 | * Client is not allowed to monitor this | |
4468 | * object. | |
4469 | */ | |
bd0514a5 | 4470 | DBG("Skipping client at it does not have the object permission to receive notification for this trigger"); |
6c24d3fd | 4471 | goto skip_client; |
f2b3ef9f | 4472 | } |
90843f49 JR |
4473 | } |
4474 | ||
f712e5f6 | 4475 | if (client->uid != lttng_credentials_get_uid(trigger_creds)) { |
bd0514a5 | 4476 | DBG("Skipping client at it does not have the permission to receive notification for this trigger"); |
6c24d3fd | 4477 | goto skip_client; |
ab0ee2ca JG |
4478 | } |
4479 | ||
bd0514a5 | 4480 | DBG("Sending notification to client (fd = %i, %zu bytes)", |
28ab034a JG |
4481 | client->socket, |
4482 | msg_payload.buffer.size); | |
882093ee JR |
4483 | |
4484 | if (client_has_outbound_data_left(client)) { | |
ab0ee2ca JG |
4485 | /* |
4486 | * Outgoing data is already buffered for this client; | |
4487 | * drop the notification and enqueue a "dropped | |
4488 | * notification" message if this is the first dropped | |
4489 | * notification since the socket spilled-over to the | |
4490 | * queue. | |
4491 | */ | |
f2b3ef9f JG |
4492 | ret = client_notification_overflow(client); |
4493 | if (ret) { | |
6c24d3fd JG |
4494 | /* Fatal error. */ |
4495 | goto skip_client; | |
ab0ee2ca | 4496 | } |
ab0ee2ca JG |
4497 | } |
4498 | ||
882093ee | 4499 | ret = lttng_payload_copy(&msg_payload, &client->communication.outbound.payload); |
ab0ee2ca | 4500 | if (ret) { |
6c24d3fd JG |
4501 | /* Fatal error. */ |
4502 | goto skip_client; | |
ab0ee2ca JG |
4503 | } |
4504 | ||
f2b3ef9f | 4505 | transmission_status = client_flush_outgoing_queue(client); |
6c24d3fd | 4506 | pthread_mutex_unlock(&client->lock); |
f2b3ef9f | 4507 | ret = client_report(client, transmission_status, user_data); |
ab0ee2ca | 4508 | if (ret) { |
6c24d3fd JG |
4509 | /* Fatal error. */ |
4510 | goto end_unlock_list; | |
505b2d90 | 4511 | } |
6c24d3fd JG |
4512 | |
4513 | continue; | |
4514 | ||
28ab034a | 4515 | skip_client: |
505b2d90 JG |
4516 | pthread_mutex_unlock(&client->lock); |
4517 | if (ret) { | |
6c24d3fd | 4518 | /* Fatal error. */ |
505b2d90 | 4519 | goto end_unlock_list; |
ab0ee2ca JG |
4520 | } |
4521 | } | |
4522 | ret = 0; | |
505b2d90 JG |
4523 | |
4524 | end_unlock_list: | |
4525 | pthread_mutex_unlock(&client_list->lock); | |
ab0ee2ca | 4526 | end: |
c0a66c84 | 4527 | lttng_payload_reset(&msg_payload); |
ab0ee2ca JG |
4528 | return ret; |
4529 | } | |
4530 | ||
28ab034a JG |
4531 | static struct lttng_event_notifier_notification * |
4532 | recv_one_event_notifier_notification(int notification_pipe_read_fd, enum lttng_domain_type domain) | |
94078603 JR |
4533 | { |
4534 | int ret; | |
b9a8d78f | 4535 | uint64_t token; |
cd9adb8b JG |
4536 | struct lttng_event_notifier_notification *notification = nullptr; |
4537 | char *capture_buffer = nullptr; | |
82b3cbf4 | 4538 | size_t capture_buffer_size; |
94078603 JR |
4539 | void *reception_buffer; |
4540 | size_t reception_size; | |
4541 | ||
fc4b93fa | 4542 | struct lttng_ust_abi_event_notifier_notification ust_notification; |
b8e2fb80 | 4543 | struct lttng_kernel_abi_event_notifier_notification kernel_notification; |
94078603 | 4544 | |
b9a8d78f | 4545 | /* Init lttng_event_notifier_notification */ |
28ab034a | 4546 | switch (domain) { |
94078603 JR |
4547 | case LTTNG_DOMAIN_UST: |
4548 | reception_buffer = (void *) &ust_notification; | |
4549 | reception_size = sizeof(ust_notification); | |
94078603 JR |
4550 | break; |
4551 | case LTTNG_DOMAIN_KERNEL: | |
4552 | reception_buffer = (void *) &kernel_notification; | |
4553 | reception_size = sizeof(kernel_notification); | |
94078603 JR |
4554 | break; |
4555 | default: | |
4556 | abort(); | |
4557 | } | |
4558 | ||
4559 | /* | |
4560 | * The monitoring pipe only holds messages smaller than PIPE_BUF, | |
4561 | * ensuring that read/write of tracer notifications are atomic. | |
4562 | */ | |
28ab034a | 4563 | ret = lttng_read(notification_pipe_read_fd, reception_buffer, reception_size); |
94078603 JR |
4564 | if (ret != reception_size) { |
4565 | PERROR("Failed to read from event source notification pipe: fd = %d, size to read = %zu, ret = %d", | |
28ab034a JG |
4566 | notification_pipe_read_fd, |
4567 | reception_size, | |
4568 | ret); | |
94078603 JR |
4569 | ret = -1; |
4570 | goto end; | |
4571 | } | |
4572 | ||
28ab034a | 4573 | switch (domain) { |
94078603 | 4574 | case LTTNG_DOMAIN_UST: |
b9a8d78f | 4575 | token = ust_notification.token; |
82b3cbf4 | 4576 | capture_buffer_size = ust_notification.capture_buf_size; |
94078603 JR |
4577 | break; |
4578 | case LTTNG_DOMAIN_KERNEL: | |
b9a8d78f | 4579 | token = kernel_notification.token; |
bbd6675c | 4580 | capture_buffer_size = kernel_notification.capture_buf_size; |
94078603 JR |
4581 | break; |
4582 | default: | |
4583 | abort(); | |
4584 | } | |
4585 | ||
82b3cbf4 | 4586 | if (capture_buffer_size == 0) { |
cd9adb8b | 4587 | capture_buffer = nullptr; |
82b3cbf4 JR |
4588 | goto skip_capture; |
4589 | } | |
4590 | ||
4591 | if (capture_buffer_size > MAX_CAPTURE_SIZE) { | |
bd0514a5 | 4592 | ERR("Event notifier has a capture payload size which exceeds the maximum allowed size: capture_payload_size = %zu bytes, max allowed size = %d bytes", |
28ab034a JG |
4593 | capture_buffer_size, |
4594 | MAX_CAPTURE_SIZE); | |
82b3cbf4 JR |
4595 | goto end; |
4596 | } | |
4597 | ||
64803277 | 4598 | capture_buffer = calloc<char>(capture_buffer_size); |
82b3cbf4 | 4599 | if (!capture_buffer) { |
bd0514a5 | 4600 | ERR("Failed to allocate capture buffer"); |
82b3cbf4 JR |
4601 | goto end; |
4602 | } | |
4603 | ||
4604 | /* Fetch additional payload (capture). */ | |
4605 | ret = lttng_read(notification_pipe_read_fd, capture_buffer, capture_buffer_size); | |
4606 | if (ret != capture_buffer_size) { | |
28ab034a | 4607 | ERR("Failed to read from event source pipe (fd = %i)", notification_pipe_read_fd); |
82b3cbf4 JR |
4608 | goto end; |
4609 | } | |
4610 | ||
4611 | skip_capture: | |
28ab034a JG |
4612 | notification = lttng_event_notifier_notification_create( |
4613 | token, domain, capture_buffer, capture_buffer_size); | |
cd9adb8b | 4614 | if (notification == nullptr) { |
82b3cbf4 JR |
4615 | goto end; |
4616 | } | |
4617 | ||
4618 | /* | |
4619 | * Ownership transfered to the lttng_event_notifier_notification object. | |
4620 | */ | |
cd9adb8b | 4621 | capture_buffer = nullptr; |
82b3cbf4 | 4622 | |
b9a8d78f | 4623 | end: |
82b3cbf4 | 4624 | free(capture_buffer); |
b9a8d78f FD |
4625 | return notification; |
4626 | } | |
4627 | ||
28ab034a JG |
4628 | static int |
4629 | dispatch_one_event_notifier_notification(struct notification_thread_state *state, | |
4630 | struct lttng_event_notifier_notification *notification) | |
b9a8d78f | 4631 | { |
b9a8d78f FD |
4632 | struct cds_lfht_node *node; |
4633 | struct cds_lfht_iter iter; | |
4634 | struct notification_trigger_tokens_ht_element *element; | |
cd9adb8b | 4635 | struct lttng_evaluation *evaluation = nullptr; |
b9a8d78f | 4636 | enum action_executor_status executor_status; |
cd9adb8b | 4637 | struct notification_client_list *client_list = nullptr; |
8b524060 | 4638 | int ret; |
7c920b63 | 4639 | unsigned int capture_count = 0; |
b9a8d78f | 4640 | |
94078603 | 4641 | /* Find triggers associated with this token. */ |
07c4863f | 4642 | const lttng::urcu::read_lock_guard read_lock; |
94078603 | 4643 | cds_lfht_lookup(state->trigger_tokens_ht, |
b9a8d78f | 4644 | hash_key_u64(¬ification->tracer_token, lttng_ht_seed), |
28ab034a JG |
4645 | match_trigger_token, |
4646 | ¬ification->tracer_token, | |
4647 | &iter); | |
94078603 | 4648 | node = cds_lfht_iter_get_node(&iter); |
f1446131 | 4649 | if (caa_unlikely(!node)) { |
94078603 | 4650 | /* |
f1446131 JR |
4651 | * This is not an error, slow consumption of the tracer |
4652 | * notifications can lead to situations where a trigger is | |
4653 | * removed but we still get tracer notifications matching a | |
4654 | * trigger that no longer exists. | |
94078603 JR |
4655 | */ |
4656 | ret = 0; | |
4657 | goto end_unlock; | |
4658 | } | |
4659 | ||
28ab034a | 4660 | element = caa_container_of(node, struct notification_trigger_tokens_ht_element, node); |
94078603 | 4661 | |
8dbb86b8 | 4662 | if (lttng_condition_event_rule_matches_get_capture_descriptor_count( |
28ab034a JG |
4663 | lttng_trigger_get_const_condition(element->trigger), &capture_count) != |
4664 | LTTNG_CONDITION_STATUS_OK) { | |
7c920b63 PP |
4665 | ERR("Failed to get capture count"); |
4666 | ret = -1; | |
4667 | goto end; | |
4668 | } | |
4669 | ||
4670 | if (!notification->capture_buffer && capture_count != 0) { | |
4671 | ERR("Expected capture but capture buffer is null"); | |
4672 | ret = -1; | |
4673 | goto end; | |
4674 | } | |
4675 | ||
8dbb86b8 | 4676 | evaluation = lttng_evaluation_event_rule_matches_create( |
28ab034a JG |
4677 | lttng::utils::container_of(lttng_trigger_get_const_condition(element->trigger), |
4678 | <tng_condition_event_rule_matches::parent), | |
4679 | notification->capture_buffer, | |
4680 | notification->capture_buf_size, | |
4681 | false); | |
7c920b63 | 4682 | |
cd9adb8b | 4683 | if (evaluation == nullptr) { |
8c1d25ff | 4684 | ERR("Failed to create event rule matches evaluation while creating and enqueuing action executor job"); |
f1446131 JR |
4685 | ret = -1; |
4686 | goto end_unlock; | |
4687 | } | |
8c1d25ff | 4688 | |
28ab034a JG |
4689 | client_list = get_client_list_from_condition( |
4690 | state, lttng_trigger_get_const_condition(element->trigger)); | |
4691 | executor_status = action_executor_enqueue_trigger( | |
cd9adb8b | 4692 | state->executor, element->trigger, evaluation, nullptr, client_list); |
f1446131 JR |
4693 | switch (executor_status) { |
4694 | case ACTION_EXECUTOR_STATUS_OK: | |
4695 | ret = 0; | |
4696 | break; | |
4697 | case ACTION_EXECUTOR_STATUS_OVERFLOW: | |
4698 | { | |
28ab034a | 4699 | struct notification_client_list_element *client_list_element, *tmp; |
f1446131 JR |
4700 | |
4701 | /* | |
4702 | * Not a fatal error; this is expected and simply means the | |
4703 | * executor has too much work queued already. | |
4704 | */ | |
4705 | ret = 0; | |
4706 | ||
4707 | /* No clients subscribed to notifications for this trigger. */ | |
4708 | if (!client_list) { | |
4709 | break; | |
4710 | } | |
4711 | ||
4712 | /* Warn clients that a notification (or more) was dropped. */ | |
4713 | pthread_mutex_lock(&client_list->lock); | |
28ab034a JG |
4714 | cds_list_for_each_entry_safe ( |
4715 | client_list_element, tmp, &client_list->clients_list, node) { | |
f1446131 | 4716 | enum client_transmission_status transmission_status; |
28ab034a | 4717 | struct notification_client *client = client_list_element->client; |
f1446131 JR |
4718 | |
4719 | pthread_mutex_lock(&client->lock); | |
4720 | ret = client_notification_overflow(client); | |
4721 | if (ret) { | |
4722 | /* Fatal error. */ | |
4723 | goto next_client; | |
4724 | } | |
4725 | ||
28ab034a JG |
4726 | transmission_status = client_flush_outgoing_queue(client); |
4727 | ret = client_handle_transmission_status(client, transmission_status, state); | |
f1446131 JR |
4728 | if (ret) { |
4729 | /* Fatal error. */ | |
4730 | goto next_client; | |
4731 | } | |
28ab034a | 4732 | next_client: |
f1446131 JR |
4733 | pthread_mutex_unlock(&client->lock); |
4734 | if (ret) { | |
4735 | break; | |
4736 | } | |
4737 | } | |
4738 | ||
4739 | pthread_mutex_unlock(&client_list->lock); | |
4740 | break; | |
4741 | } | |
98dfca79 | 4742 | case ACTION_EXECUTOR_STATUS_INVALID: |
f1446131 JR |
4743 | case ACTION_EXECUTOR_STATUS_ERROR: |
4744 | /* Fatal error, shut down everything. */ | |
4745 | ERR("Fatal error encoutered while enqueuing action to the action executor"); | |
4746 | ret = -1; | |
4747 | goto end_unlock; | |
4748 | default: | |
4749 | /* Unhandled error. */ | |
4750 | abort(); | |
4751 | } | |
94078603 JR |
4752 | |
4753 | end_unlock: | |
f1446131 | 4754 | notification_client_list_put(client_list); |
7c920b63 | 4755 | end: |
8b524060 FD |
4756 | return ret; |
4757 | } | |
4758 | ||
28ab034a JG |
4759 | static int handle_one_event_notifier_notification(struct notification_thread_state *state, |
4760 | int pipe, | |
4761 | enum lttng_domain_type domain) | |
8b524060 | 4762 | { |
82b3cbf4 | 4763 | int ret = 0; |
cd9adb8b | 4764 | struct lttng_event_notifier_notification *notification = nullptr; |
8b524060 FD |
4765 | |
4766 | notification = recv_one_event_notifier_notification(pipe, domain); | |
cd9adb8b | 4767 | if (notification == nullptr) { |
82b3cbf4 | 4768 | /* Reception failed, don't consider it fatal. */ |
bd0514a5 | 4769 | ERR("Error receiving an event notifier notification from tracer: fd = %i, domain = %s", |
28ab034a JG |
4770 | pipe, |
4771 | lttng_domain_type_str(domain)); | |
8b524060 FD |
4772 | goto end; |
4773 | } | |
4774 | ||
4775 | ret = dispatch_one_event_notifier_notification(state, notification); | |
4776 | if (ret) { | |
bd0514a5 | 4777 | ERR("Error dispatching an event notifier notification from tracer: fd = %i, domain = %s", |
28ab034a JG |
4778 | pipe, |
4779 | lttng_domain_type_str(domain)); | |
8b524060 FD |
4780 | goto end; |
4781 | } | |
4782 | ||
94078603 | 4783 | end: |
8b524060 | 4784 | lttng_event_notifier_notification_destroy(notification); |
94078603 JR |
4785 | return ret; |
4786 | } | |
4787 | ||
8b524060 | 4788 | int handle_notification_thread_event_notification(struct notification_thread_state *state, |
28ab034a JG |
4789 | int pipe, |
4790 | enum lttng_domain_type domain) | |
8b524060 FD |
4791 | { |
4792 | return handle_one_event_notifier_notification(state, pipe, domain); | |
4793 | } | |
4794 | ||
28ab034a JG |
4795 | int handle_notification_thread_channel_sample(struct notification_thread_state *state, |
4796 | int pipe, | |
4797 | enum lttng_domain_type domain) | |
ab0ee2ca JG |
4798 | { |
4799 | int ret = 0; | |
4800 | struct lttcomm_consumer_channel_monitor_msg sample_msg; | |
cd9adb8b | 4801 | struct channel_info *channel_info = nullptr; |
ab0ee2ca JG |
4802 | struct cds_lfht_node *node; |
4803 | struct cds_lfht_iter iter; | |
319dcddc JG |
4804 | struct lttng_channel_trigger_list *channel_trigger_list; |
4805 | struct lttng_session_trigger_list *session_trigger_list; | |
ab0ee2ca JG |
4806 | struct lttng_trigger_list_element *trigger_list_element; |
4807 | bool previous_sample_available = false; | |
319dcddc JG |
4808 | struct channel_state_sample channel_previous_sample, channel_new_sample; |
4809 | struct session_state_sample session_new_sample; | |
4810 | struct lttng_credentials channel_creds = {}; | |
4811 | struct lttng_credentials session_creds = {}; | |
4812 | struct session_info *session; | |
07c4863f | 4813 | const lttng::urcu::read_lock_guard read_lock; |
ab0ee2ca JG |
4814 | |
4815 | /* | |
4816 | * The monitoring pipe only holds messages smaller than PIPE_BUF, | |
4817 | * ensuring that read/write of sampling messages are atomic. | |
4818 | */ | |
7c2551ef | 4819 | ret = lttng_read(pipe, &sample_msg, sizeof(sample_msg)); |
ab0ee2ca | 4820 | if (ret != sizeof(sample_msg)) { |
28ab034a | 4821 | ERR("Failed to read from monitoring pipe (fd = %i)", pipe); |
ab0ee2ca JG |
4822 | ret = -1; |
4823 | goto end; | |
4824 | } | |
4825 | ||
4826 | ret = 0; | |
319dcddc JG |
4827 | channel_new_sample.key.key = sample_msg.key; |
4828 | channel_new_sample.key.domain = domain; | |
4829 | channel_new_sample.highest_usage = sample_msg.highest; | |
4830 | channel_new_sample.lowest_usage = sample_msg.lowest; | |
ab0ee2ca | 4831 | |
319dcddc JG |
4832 | session = get_session_info_by_id(state, sample_msg.session_id); |
4833 | if (!session) { | |
4834 | DBG("Received a sample for an unknown session from consumerd: session id = %" PRIu64, | |
28ab034a | 4835 | sample_msg.session_id); |
319dcddc JG |
4836 | goto end_unlock; |
4837 | } | |
4838 | ||
4839 | session_new_sample = session->last_state_sample; | |
4840 | session_new_sample.consumed_data_size += sample_msg.consumed_since_last_sample; | |
4841 | session_creds = { | |
4842 | .uid = LTTNG_OPTIONAL_INIT_VALUE(session->uid), | |
4843 | .gid = LTTNG_OPTIONAL_INIT_VALUE(session->gid), | |
4844 | }; | |
4845 | ||
4846 | session_trigger_list = get_session_trigger_list(state, session->name); | |
4847 | LTTNG_ASSERT(session_trigger_list); | |
28ab034a | 4848 | cds_list_for_each_entry (trigger_list_element, &session_trigger_list->list, node) { |
319dcddc JG |
4849 | const struct lttng_condition *condition; |
4850 | struct lttng_trigger *trigger; | |
cd9adb8b JG |
4851 | struct notification_client_list *client_list = nullptr; |
4852 | struct lttng_evaluation *evaluation = nullptr; | |
319dcddc JG |
4853 | enum action_executor_status executor_status; |
4854 | ||
4855 | ret = 0; | |
4856 | trigger = trigger_list_element->trigger; | |
4857 | condition = lttng_trigger_get_const_condition(trigger); | |
4858 | LTTNG_ASSERT(condition); | |
4859 | ||
4860 | ret = evaluate_session_condition( | |
28ab034a | 4861 | condition, session, &session_new_sample, &evaluation); |
319dcddc JG |
4862 | if (caa_unlikely(ret)) { |
4863 | break; | |
4864 | } | |
4865 | ||
4866 | if (caa_likely(!evaluation)) { | |
4867 | continue; | |
4868 | } | |
4869 | ||
4870 | /* | |
4871 | * Ownership of `evaluation` transferred to the action executor | |
4872 | * no matter the result. The callee acquires a reference to the | |
4873 | * client list: we can release our own. | |
4874 | */ | |
4875 | client_list = get_client_list_from_condition(state, condition); | |
4876 | executor_status = action_executor_enqueue_trigger( | |
28ab034a | 4877 | state->executor, trigger, evaluation, &session_creds, client_list); |
319dcddc | 4878 | notification_client_list_put(client_list); |
cd9adb8b | 4879 | evaluation = nullptr; |
319dcddc JG |
4880 | switch (executor_status) { |
4881 | case ACTION_EXECUTOR_STATUS_OK: | |
4882 | break; | |
4883 | case ACTION_EXECUTOR_STATUS_ERROR: | |
4884 | case ACTION_EXECUTOR_STATUS_INVALID: | |
4885 | /* | |
4886 | * TODO Add trigger identification (name/id) when | |
4887 | * it is added to the API. | |
4888 | */ | |
4889 | ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger"); | |
4890 | ret = -1; | |
4891 | goto end_unlock; | |
4892 | case ACTION_EXECUTOR_STATUS_OVERFLOW: | |
4893 | /* | |
4894 | * TODO Add trigger identification (name/id) when | |
4895 | * it is added to the API. | |
4896 | * | |
4897 | * Not a fatal error. | |
4898 | */ | |
4899 | WARN("No space left when enqueuing action associated with buffer-condition trigger"); | |
4900 | ret = 0; | |
4901 | goto end_unlock; | |
4902 | default: | |
4903 | abort(); | |
4904 | } | |
4905 | } | |
4906 | ||
ab0ee2ca JG |
4907 | /* Retrieve the channel's informations */ |
4908 | cds_lfht_lookup(state->channels_ht, | |
319dcddc | 4909 | hash_channel_key(&channel_new_sample.key), |
ab0ee2ca | 4910 | match_channel_info, |
319dcddc | 4911 | &channel_new_sample.key, |
ab0ee2ca JG |
4912 | &iter); |
4913 | node = cds_lfht_iter_get_node(&iter); | |
e0b5f87b | 4914 | if (caa_unlikely(!node)) { |
ab0ee2ca JG |
4915 | /* |
4916 | * Not an error since the consumer can push a sample to the pipe | |
4917 | * and the rest of the session daemon could notify us of the | |
4918 | * channel's destruction before we get a chance to process that | |
4919 | * sample. | |
4920 | */ | |
28ab034a JG |
4921 | DBG("Received a sample for an unknown channel from consumerd, key = %" PRIu64 |
4922 | " in %s domain", | |
4923 | channel_new_sample.key.key, | |
4924 | lttng_domain_type_str(domain)); | |
ab0ee2ca JG |
4925 | goto end_unlock; |
4926 | } | |
319dcddc | 4927 | |
28ab034a JG |
4928 | channel_info = caa_container_of(node, struct channel_info, channels_ht_node); |
4929 | DBG("Handling channel sample for channel %s (key = %" PRIu64 | |
4930 | ") in session %s (highest usage = %" PRIu64 ", lowest usage = %" PRIu64 | |
4931 | ", consumed since last sample = %" PRIu64 ")", | |
4932 | channel_info->name, | |
4933 | channel_new_sample.key.key, | |
4934 | channel_info->session_info->name, | |
4935 | channel_new_sample.highest_usage, | |
4936 | channel_new_sample.lowest_usage, | |
4937 | sample_msg.consumed_since_last_sample); | |
ab0ee2ca JG |
4938 | |
4939 | /* Retrieve the channel's last sample, if it exists, and update it. */ | |
4940 | cds_lfht_lookup(state->channel_state_ht, | |
319dcddc | 4941 | hash_channel_key(&channel_new_sample.key), |
ab0ee2ca | 4942 | match_channel_state_sample, |
319dcddc | 4943 | &channel_new_sample.key, |
ab0ee2ca JG |
4944 | &iter); |
4945 | node = cds_lfht_iter_get_node(&iter); | |
e0b5f87b | 4946 | if (caa_likely(node)) { |
ab0ee2ca JG |
4947 | struct channel_state_sample *stored_sample; |
4948 | ||
4949 | /* Update the sample stored. */ | |
28ab034a JG |
4950 | stored_sample = |
4951 | caa_container_of(node, struct channel_state_sample, channel_state_ht_node); | |
e8360425 | 4952 | |
28ab034a | 4953 | memcpy(&channel_previous_sample, stored_sample, sizeof(channel_previous_sample)); |
319dcddc JG |
4954 | stored_sample->highest_usage = channel_new_sample.highest_usage; |
4955 | stored_sample->lowest_usage = channel_new_sample.lowest_usage; | |
ab0ee2ca JG |
4956 | previous_sample_available = true; |
4957 | } else { | |
4958 | /* | |
4959 | * This is the channel's first sample, allocate space for and | |
4960 | * store the new sample. | |
4961 | */ | |
4962 | struct channel_state_sample *stored_sample; | |
4963 | ||
64803277 | 4964 | stored_sample = zmalloc<channel_state_sample>(); |
ab0ee2ca JG |
4965 | if (!stored_sample) { |
4966 | ret = -1; | |
4967 | goto end_unlock; | |
4968 | } | |
4969 | ||
319dcddc | 4970 | memcpy(stored_sample, &channel_new_sample, sizeof(*stored_sample)); |
ab0ee2ca JG |
4971 | cds_lfht_node_init(&stored_sample->channel_state_ht_node); |
4972 | cds_lfht_add(state->channel_state_ht, | |
28ab034a JG |
4973 | hash_channel_key(&stored_sample->key), |
4974 | &stored_sample->channel_state_ht_node); | |
ab0ee2ca JG |
4975 | } |
4976 | ||
4977 | /* Find triggers associated with this channel. */ | |
4978 | cds_lfht_lookup(state->channel_triggers_ht, | |
319dcddc | 4979 | hash_channel_key(&channel_new_sample.key), |
ab0ee2ca | 4980 | match_channel_trigger_list, |
319dcddc | 4981 | &channel_new_sample.key, |
ab0ee2ca JG |
4982 | &iter); |
4983 | node = cds_lfht_iter_get_node(&iter); | |
319dcddc | 4984 | LTTNG_ASSERT(node); |
ab0ee2ca | 4985 | |
28ab034a | 4986 | channel_creds = (typeof(channel_creds)){ |
ff588497 JR |
4987 | .uid = LTTNG_OPTIONAL_INIT_VALUE(channel_info->session_info->uid), |
4988 | .gid = LTTNG_OPTIONAL_INIT_VALUE(channel_info->session_info->gid), | |
f2b3ef9f JG |
4989 | }; |
4990 | ||
28ab034a JG |
4991 | channel_trigger_list = |
4992 | caa_container_of(node, struct lttng_channel_trigger_list, channel_triggers_ht_node); | |
4993 | cds_list_for_each_entry (trigger_list_element, &channel_trigger_list->list, node) { | |
9b63a4aa | 4994 | const struct lttng_condition *condition; |
f2b3ef9f | 4995 | struct lttng_trigger *trigger; |
cd9adb8b JG |
4996 | struct notification_client_list *client_list = nullptr; |
4997 | struct lttng_evaluation *evaluation = nullptr; | |
f2b3ef9f | 4998 | enum action_executor_status executor_status; |
ab0ee2ca | 4999 | |
505b2d90 | 5000 | ret = 0; |
ab0ee2ca | 5001 | trigger = trigger_list_element->trigger; |
9b63a4aa | 5002 | condition = lttng_trigger_get_const_condition(trigger); |
a0377dfe | 5003 | LTTNG_ASSERT(condition); |
ab0ee2ca | 5004 | |
28ab034a JG |
5005 | ret = evaluate_buffer_condition( |
5006 | condition, | |
5007 | &evaluation, | |
5008 | state, | |
cd9adb8b | 5009 | previous_sample_available ? &channel_previous_sample : nullptr, |
28ab034a JG |
5010 | &channel_new_sample, |
5011 | channel_info); | |
e8360425 | 5012 | if (caa_unlikely(ret)) { |
319dcddc | 5013 | break; |
ab0ee2ca JG |
5014 | } |
5015 | ||
e8360425 | 5016 | if (caa_likely(!evaluation)) { |
319dcddc | 5017 | continue; |
ab0ee2ca JG |
5018 | } |
5019 | ||
f2b3ef9f JG |
5020 | /* |
5021 | * Ownership of `evaluation` transferred to the action executor | |
319dcddc JG |
5022 | * no matter the result. The callee acquires a reference to the |
5023 | * client list: we can release our own. | |
f2b3ef9f | 5024 | */ |
319dcddc | 5025 | client_list = get_client_list_from_condition(state, condition); |
72365501 | 5026 | executor_status = action_executor_enqueue_trigger( |
28ab034a | 5027 | state->executor, trigger, evaluation, &channel_creds, client_list); |
319dcddc | 5028 | notification_client_list_put(client_list); |
cd9adb8b | 5029 | evaluation = nullptr; |
f2b3ef9f JG |
5030 | switch (executor_status) { |
5031 | case ACTION_EXECUTOR_STATUS_OK: | |
5032 | break; | |
5033 | case ACTION_EXECUTOR_STATUS_ERROR: | |
5034 | case ACTION_EXECUTOR_STATUS_INVALID: | |
5035 | /* | |
5036 | * TODO Add trigger identification (name/id) when | |
5037 | * it is added to the API. | |
5038 | */ | |
5039 | ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger"); | |
5040 | ret = -1; | |
319dcddc | 5041 | goto end_unlock; |
f2b3ef9f JG |
5042 | case ACTION_EXECUTOR_STATUS_OVERFLOW: |
5043 | /* | |
5044 | * TODO Add trigger identification (name/id) when | |
5045 | * it is added to the API. | |
5046 | * | |
5047 | * Not a fatal error. | |
5048 | */ | |
5049 | WARN("No space left when enqueuing action associated with buffer-condition trigger"); | |
5050 | ret = 0; | |
319dcddc | 5051 | goto end_unlock; |
f2b3ef9f JG |
5052 | default: |
5053 | abort(); | |
5054 | } | |
ab0ee2ca JG |
5055 | } |
5056 | end_unlock: | |
319dcddc JG |
5057 | if (session) { |
5058 | session->last_state_sample = session_new_sample; | |
5059 | } | |
5060 | session_info_put(session); | |
ab0ee2ca JG |
5061 | end: |
5062 | return ret; | |
5063 | } |