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 JG |
8 | #include "lttng/action/action.h" |
9 | #include "lttng/trigger/trigger-internal.h" | |
ab0ee2ca JG |
10 | #define _LGPL_SOURCE |
11 | #include <urcu.h> | |
12 | #include <urcu/rculfhash.h> | |
13 | ||
ab0ee2ca JG |
14 | #include <common/defaults.h> |
15 | #include <common/error.h> | |
16 | #include <common/futex.h> | |
17 | #include <common/unix.h> | |
18 | #include <common/dynamic-buffer.h> | |
19 | #include <common/hashtable/utils.h> | |
20 | #include <common/sessiond-comm/sessiond-comm.h> | |
21 | #include <common/macros.h> | |
22 | #include <lttng/condition/condition.h> | |
9b63a4aa | 23 | #include <lttng/action/action-internal.h> |
ab0ee2ca JG |
24 | #include <lttng/notification/notification-internal.h> |
25 | #include <lttng/condition/condition-internal.h> | |
26 | #include <lttng/condition/buffer-usage-internal.h> | |
e8360425 | 27 | #include <lttng/condition/session-consumed-size-internal.h> |
ea9a44f0 | 28 | #include <lttng/condition/session-rotation-internal.h> |
959e3c66 | 29 | #include <lttng/condition/event-rule-internal.h> |
ab0ee2ca | 30 | #include <lttng/notification/channel-internal.h> |
85c06c44 | 31 | #include <lttng/trigger/trigger-internal.h> |
959e3c66 | 32 | #include <lttng/event-rule/event-rule-internal.h> |
1da26331 | 33 | |
ab0ee2ca JG |
34 | #include <time.h> |
35 | #include <unistd.h> | |
36 | #include <assert.h> | |
37 | #include <inttypes.h> | |
d53ea4e4 | 38 | #include <fcntl.h> |
ab0ee2ca | 39 | |
e98a976a | 40 | #include "condition-internal.h" |
1da26331 JG |
41 | #include "notification-thread.h" |
42 | #include "notification-thread-events.h" | |
43 | #include "notification-thread-commands.h" | |
44 | #include "lttng-sessiond.h" | |
45 | #include "kernel.h" | |
46 | ||
ab0ee2ca JG |
47 | #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP) |
48 | #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT) | |
49 | ||
51eab943 JG |
50 | enum lttng_object_type { |
51 | LTTNG_OBJECT_TYPE_UNKNOWN, | |
52 | LTTNG_OBJECT_TYPE_NONE, | |
53 | LTTNG_OBJECT_TYPE_CHANNEL, | |
54 | LTTNG_OBJECT_TYPE_SESSION, | |
55 | }; | |
56 | ||
ab0ee2ca | 57 | struct lttng_trigger_list_element { |
9e0bb80e | 58 | /* No ownership of the trigger object is assumed. */ |
f2b3ef9f | 59 | struct lttng_trigger *trigger; |
ab0ee2ca JG |
60 | struct cds_list_head node; |
61 | }; | |
62 | ||
63 | struct lttng_channel_trigger_list { | |
64 | struct channel_key channel_key; | |
ea9a44f0 | 65 | /* List of struct lttng_trigger_list_element. */ |
ab0ee2ca | 66 | struct cds_list_head list; |
ea9a44f0 | 67 | /* Node in the channel_triggers_ht */ |
ab0ee2ca | 68 | struct cds_lfht_node channel_triggers_ht_node; |
83b934ad MD |
69 | /* call_rcu delayed reclaim. */ |
70 | struct rcu_head rcu_node; | |
ab0ee2ca JG |
71 | }; |
72 | ||
ea9a44f0 JG |
73 | /* |
74 | * List of triggers applying to a given session. | |
75 | * | |
76 | * See: | |
77 | * - lttng_session_trigger_list_create() | |
78 | * - lttng_session_trigger_list_build() | |
79 | * - lttng_session_trigger_list_destroy() | |
80 | * - lttng_session_trigger_list_add() | |
81 | */ | |
82 | struct lttng_session_trigger_list { | |
83 | /* | |
84 | * Not owned by this; points to the session_info structure's | |
85 | * session name. | |
86 | */ | |
87 | const char *session_name; | |
88 | /* List of struct lttng_trigger_list_element. */ | |
89 | struct cds_list_head list; | |
90 | /* Node in the session_triggers_ht */ | |
91 | struct cds_lfht_node session_triggers_ht_node; | |
92 | /* | |
93 | * Weak reference to the notification system's session triggers | |
94 | * hashtable. | |
95 | * | |
96 | * The session trigger list structure structure is owned by | |
97 | * the session's session_info. | |
98 | * | |
99 | * The session_info is kept alive the the channel_infos holding a | |
100 | * reference to it (reference counting). When those channels are | |
101 | * destroyed (at runtime or on teardown), the reference they hold | |
102 | * to the session_info are released. On destruction of session_info, | |
103 | * session_info_destroy() will remove the list of triggers applying | |
104 | * to this session from the notification system's state. | |
105 | * | |
106 | * This implies that the session_triggers_ht must be destroyed | |
107 | * after the channels. | |
108 | */ | |
109 | struct cds_lfht *session_triggers_ht; | |
110 | /* Used for delayed RCU reclaim. */ | |
111 | struct rcu_head rcu_node; | |
112 | }; | |
113 | ||
ab0ee2ca JG |
114 | struct lttng_trigger_ht_element { |
115 | struct lttng_trigger *trigger; | |
116 | struct cds_lfht_node node; | |
242388e4 | 117 | struct cds_lfht_node node_by_name_uid; |
83b934ad MD |
118 | /* call_rcu delayed reclaim. */ |
119 | struct rcu_head rcu_node; | |
ab0ee2ca JG |
120 | }; |
121 | ||
122 | struct lttng_condition_list_element { | |
123 | struct lttng_condition *condition; | |
124 | struct cds_list_head node; | |
125 | }; | |
126 | ||
ab0ee2ca JG |
127 | struct channel_state_sample { |
128 | struct channel_key key; | |
129 | struct cds_lfht_node channel_state_ht_node; | |
130 | uint64_t highest_usage; | |
131 | uint64_t lowest_usage; | |
e8360425 | 132 | uint64_t channel_total_consumed; |
83b934ad MD |
133 | /* call_rcu delayed reclaim. */ |
134 | struct rcu_head rcu_node; | |
ab0ee2ca JG |
135 | }; |
136 | ||
e4db5ace | 137 | static unsigned long hash_channel_key(struct channel_key *key); |
ed327204 | 138 | static int evaluate_buffer_condition(const struct lttng_condition *condition, |
e4db5ace | 139 | struct lttng_evaluation **evaluation, |
e8360425 JD |
140 | const struct notification_thread_state *state, |
141 | const struct channel_state_sample *previous_sample, | |
142 | const struct channel_state_sample *latest_sample, | |
143 | uint64_t previous_session_consumed_total, | |
144 | uint64_t latest_session_consumed_total, | |
145 | struct channel_info *channel_info); | |
e4db5ace | 146 | static |
9b63a4aa JG |
147 | int send_evaluation_to_clients(const struct lttng_trigger *trigger, |
148 | const struct lttng_evaluation *evaluation, | |
e4db5ace JR |
149 | struct notification_client_list *client_list, |
150 | struct notification_thread_state *state, | |
151 | uid_t channel_uid, gid_t channel_gid); | |
152 | ||
8abe313a | 153 | |
ea9a44f0 | 154 | /* session_info API */ |
8abe313a JG |
155 | static |
156 | void session_info_destroy(void *_data); | |
157 | static | |
158 | void session_info_get(struct session_info *session_info); | |
159 | static | |
160 | void session_info_put(struct session_info *session_info); | |
161 | static | |
162 | struct session_info *session_info_create(const char *name, | |
ea9a44f0 | 163 | uid_t uid, gid_t gid, |
1eee26c5 JG |
164 | struct lttng_session_trigger_list *trigger_list, |
165 | struct cds_lfht *sessions_ht); | |
8abe313a JG |
166 | static |
167 | void session_info_add_channel(struct session_info *session_info, | |
168 | struct channel_info *channel_info); | |
169 | static | |
170 | void session_info_remove_channel(struct session_info *session_info, | |
171 | struct channel_info *channel_info); | |
172 | ||
ea9a44f0 JG |
173 | /* lttng_session_trigger_list API */ |
174 | static | |
175 | struct lttng_session_trigger_list *lttng_session_trigger_list_create( | |
176 | const char *session_name, | |
177 | struct cds_lfht *session_triggers_ht); | |
178 | static | |
179 | struct lttng_session_trigger_list *lttng_session_trigger_list_build( | |
180 | const struct notification_thread_state *state, | |
181 | const char *session_name); | |
182 | static | |
183 | void lttng_session_trigger_list_destroy( | |
184 | struct lttng_session_trigger_list *list); | |
185 | static | |
186 | int lttng_session_trigger_list_add(struct lttng_session_trigger_list *list, | |
f2b3ef9f | 187 | struct lttng_trigger *trigger); |
ea9a44f0 | 188 | |
f2b3ef9f JG |
189 | static |
190 | int client_handle_transmission_status( | |
191 | struct notification_client *client, | |
192 | enum client_transmission_status transmission_status, | |
193 | struct notification_thread_state *state); | |
ea9a44f0 | 194 | |
242388e4 JR |
195 | static |
196 | void free_lttng_trigger_ht_element_rcu(struct rcu_head *node); | |
197 | ||
ab0ee2ca | 198 | static |
ac1889bf | 199 | int match_client_socket(struct cds_lfht_node *node, const void *key) |
ab0ee2ca JG |
200 | { |
201 | /* This double-cast is intended to supress pointer-to-cast warning. */ | |
ac1889bf JG |
202 | const int socket = (int) (intptr_t) key; |
203 | const struct notification_client *client = caa_container_of(node, | |
204 | struct notification_client, client_socket_ht_node); | |
ab0ee2ca | 205 | |
ac1889bf JG |
206 | return client->socket == socket; |
207 | } | |
208 | ||
209 | static | |
210 | int match_client_id(struct cds_lfht_node *node, const void *key) | |
211 | { | |
212 | /* This double-cast is intended to supress pointer-to-cast warning. */ | |
213 | const notification_client_id id = *((notification_client_id *) key); | |
214 | const struct notification_client *client = caa_container_of( | |
215 | node, struct notification_client, client_id_ht_node); | |
ab0ee2ca | 216 | |
ac1889bf | 217 | return client->id == id; |
ab0ee2ca JG |
218 | } |
219 | ||
220 | static | |
221 | int match_channel_trigger_list(struct cds_lfht_node *node, const void *key) | |
222 | { | |
223 | struct channel_key *channel_key = (struct channel_key *) key; | |
224 | struct lttng_channel_trigger_list *trigger_list; | |
225 | ||
226 | trigger_list = caa_container_of(node, struct lttng_channel_trigger_list, | |
227 | channel_triggers_ht_node); | |
228 | ||
229 | return !!((channel_key->key == trigger_list->channel_key.key) && | |
230 | (channel_key->domain == trigger_list->channel_key.domain)); | |
231 | } | |
232 | ||
51eab943 JG |
233 | static |
234 | int match_session_trigger_list(struct cds_lfht_node *node, const void *key) | |
235 | { | |
236 | const char *session_name = (const char *) key; | |
237 | struct lttng_session_trigger_list *trigger_list; | |
238 | ||
239 | trigger_list = caa_container_of(node, struct lttng_session_trigger_list, | |
240 | session_triggers_ht_node); | |
241 | ||
242 | return !!(strcmp(trigger_list->session_name, session_name) == 0); | |
243 | } | |
244 | ||
ab0ee2ca JG |
245 | static |
246 | int match_channel_state_sample(struct cds_lfht_node *node, const void *key) | |
247 | { | |
248 | struct channel_key *channel_key = (struct channel_key *) key; | |
249 | struct channel_state_sample *sample; | |
250 | ||
251 | sample = caa_container_of(node, struct channel_state_sample, | |
252 | channel_state_ht_node); | |
253 | ||
254 | return !!((channel_key->key == sample->key.key) && | |
255 | (channel_key->domain == sample->key.domain)); | |
256 | } | |
257 | ||
258 | static | |
259 | int match_channel_info(struct cds_lfht_node *node, const void *key) | |
260 | { | |
261 | struct channel_key *channel_key = (struct channel_key *) key; | |
262 | struct channel_info *channel_info; | |
263 | ||
264 | channel_info = caa_container_of(node, struct channel_info, | |
265 | channels_ht_node); | |
266 | ||
267 | return !!((channel_key->key == channel_info->key.key) && | |
268 | (channel_key->domain == channel_info->key.domain)); | |
269 | } | |
270 | ||
271 | static | |
242388e4 | 272 | int match_trigger(struct cds_lfht_node *node, const void *key) |
ab0ee2ca | 273 | { |
242388e4 JR |
274 | struct lttng_trigger *trigger_key = (struct lttng_trigger *) key; |
275 | struct lttng_trigger_ht_element *trigger_ht_element; | |
ab0ee2ca | 276 | |
242388e4 | 277 | trigger_ht_element = caa_container_of(node, struct lttng_trigger_ht_element, |
ab0ee2ca | 278 | node); |
ab0ee2ca | 279 | |
242388e4 | 280 | return !!lttng_trigger_is_equal(trigger_key, trigger_ht_element->trigger); |
ab0ee2ca JG |
281 | } |
282 | ||
ab0ee2ca JG |
283 | static |
284 | int match_client_list_condition(struct cds_lfht_node *node, const void *key) | |
285 | { | |
286 | struct lttng_condition *condition_key = (struct lttng_condition *) key; | |
287 | struct notification_client_list *client_list; | |
f82f93a1 | 288 | const struct lttng_condition *condition; |
ab0ee2ca JG |
289 | |
290 | assert(condition_key); | |
291 | ||
292 | client_list = caa_container_of(node, struct notification_client_list, | |
505b2d90 | 293 | notification_trigger_clients_ht_node); |
f82f93a1 | 294 | condition = lttng_trigger_get_const_condition(client_list->trigger); |
ab0ee2ca JG |
295 | |
296 | return !!lttng_condition_is_equal(condition_key, condition); | |
297 | } | |
298 | ||
f82f93a1 JG |
299 | static |
300 | int match_session(struct cds_lfht_node *node, const void *key) | |
301 | { | |
302 | const char *name = key; | |
303 | struct session_info *session_info = caa_container_of( | |
304 | node, struct session_info, sessions_ht_node); | |
305 | ||
306 | return !strcmp(session_info->name, name); | |
307 | } | |
308 | ||
242388e4 JR |
309 | /* |
310 | * Match trigger based on name and credentials only. | |
311 | * Name duplication is NOT allowed for the same uid. | |
312 | */ | |
313 | static | |
314 | int match_trigger_by_name_uid(struct cds_lfht_node *node, | |
315 | const void *key) | |
316 | { | |
317 | bool match = false; | |
318 | const char *name; | |
319 | const char *key_name; | |
320 | enum lttng_trigger_status status; | |
321 | const struct lttng_credentials *key_creds; | |
322 | const struct lttng_credentials *node_creds; | |
323 | const struct lttng_trigger *trigger_key = | |
324 | (const struct lttng_trigger *) key; | |
325 | const struct lttng_trigger_ht_element *trigger_ht_element = | |
326 | caa_container_of(node, | |
327 | struct lttng_trigger_ht_element, | |
328 | node_by_name_uid); | |
329 | ||
330 | status = lttng_trigger_get_name(trigger_ht_element->trigger, &name); | |
331 | assert(status == LTTNG_TRIGGER_STATUS_OK); | |
332 | ||
333 | status = lttng_trigger_get_name(trigger_key, &key_name); | |
334 | assert(status == LTTNG_TRIGGER_STATUS_OK); | |
335 | ||
336 | /* Compare the names. */ | |
337 | if (strcmp(name, key_name) != 0) { | |
338 | goto end; | |
339 | } | |
340 | ||
341 | /* Compare the owners' UIDs. */ | |
342 | key_creds = lttng_trigger_get_credentials(trigger_key); | |
343 | node_creds = lttng_trigger_get_credentials(trigger_ht_element->trigger); | |
344 | ||
345 | match = lttng_credentials_is_equal_uid(key_creds, node_creds); | |
346 | ||
347 | end: | |
348 | return match; | |
349 | } | |
350 | ||
351 | /* | |
352 | * Hash trigger based on name and credentials only. | |
353 | */ | |
354 | static | |
355 | unsigned long hash_trigger_by_name_uid(const struct lttng_trigger *trigger) | |
356 | { | |
357 | unsigned long hash = 0; | |
358 | const struct lttng_credentials *trigger_creds; | |
359 | const char *trigger_name; | |
360 | enum lttng_trigger_status status; | |
361 | ||
362 | status = lttng_trigger_get_name(trigger, &trigger_name); | |
363 | if (status == LTTNG_TRIGGER_STATUS_OK) { | |
364 | hash = hash_key_str(trigger_name, lttng_ht_seed); | |
365 | } | |
366 | ||
367 | trigger_creds = lttng_trigger_get_credentials(trigger); | |
368 | hash ^= hash_key_ulong((void *) (unsigned long) LTTNG_OPTIONAL_GET(trigger_creds->uid), | |
369 | lttng_ht_seed); | |
370 | ||
371 | return hash; | |
372 | } | |
373 | ||
e4db5ace JR |
374 | static |
375 | unsigned long hash_channel_key(struct channel_key *key) | |
376 | { | |
377 | unsigned long key_hash = hash_key_u64(&key->key, lttng_ht_seed); | |
378 | unsigned long domain_hash = hash_key_ulong( | |
379 | (void *) (unsigned long) key->domain, lttng_ht_seed); | |
380 | ||
381 | return key_hash ^ domain_hash; | |
382 | } | |
383 | ||
ac1889bf JG |
384 | static |
385 | unsigned long hash_client_socket(int socket) | |
386 | { | |
387 | return hash_key_ulong((void *) (unsigned long) socket, lttng_ht_seed); | |
388 | } | |
389 | ||
390 | static | |
391 | unsigned long hash_client_id(notification_client_id id) | |
392 | { | |
393 | return hash_key_u64(&id, lttng_ht_seed); | |
394 | } | |
395 | ||
f82f93a1 JG |
396 | /* |
397 | * Get the type of object to which a given condition applies. Bindings let | |
398 | * the notification system evaluate a trigger's condition when a given | |
399 | * object's state is updated. | |
400 | * | |
401 | * For instance, a condition bound to a channel will be evaluated everytime | |
402 | * the channel's state is changed by a channel monitoring sample. | |
403 | */ | |
404 | static | |
405 | enum lttng_object_type get_condition_binding_object( | |
406 | const struct lttng_condition *condition) | |
407 | { | |
408 | switch (lttng_condition_get_type(condition)) { | |
409 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
410 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
411 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: | |
e742b055 | 412 | return LTTNG_OBJECT_TYPE_CHANNEL; |
f82f93a1 JG |
413 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: |
414 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
415 | return LTTNG_OBJECT_TYPE_SESSION; | |
959e3c66 JR |
416 | case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT: |
417 | return LTTNG_OBJECT_TYPE_NONE; | |
f82f93a1 JG |
418 | default: |
419 | return LTTNG_OBJECT_TYPE_UNKNOWN; | |
420 | } | |
421 | } | |
422 | ||
83b934ad MD |
423 | static |
424 | void free_channel_info_rcu(struct rcu_head *node) | |
425 | { | |
426 | free(caa_container_of(node, struct channel_info, rcu_node)); | |
427 | } | |
428 | ||
ab0ee2ca JG |
429 | static |
430 | void channel_info_destroy(struct channel_info *channel_info) | |
431 | { | |
432 | if (!channel_info) { | |
433 | return; | |
434 | } | |
435 | ||
8abe313a JG |
436 | if (channel_info->session_info) { |
437 | session_info_remove_channel(channel_info->session_info, | |
438 | channel_info); | |
439 | session_info_put(channel_info->session_info); | |
ab0ee2ca | 440 | } |
8abe313a JG |
441 | if (channel_info->name) { |
442 | free(channel_info->name); | |
ab0ee2ca | 443 | } |
83b934ad MD |
444 | call_rcu(&channel_info->rcu_node, free_channel_info_rcu); |
445 | } | |
446 | ||
447 | static | |
448 | void free_session_info_rcu(struct rcu_head *node) | |
449 | { | |
450 | free(caa_container_of(node, struct session_info, rcu_node)); | |
ab0ee2ca JG |
451 | } |
452 | ||
8abe313a | 453 | /* Don't call directly, use the ref-counting mechanism. */ |
ab0ee2ca | 454 | static |
8abe313a | 455 | void session_info_destroy(void *_data) |
ab0ee2ca | 456 | { |
8abe313a | 457 | struct session_info *session_info = _data; |
3b68d0a3 | 458 | int ret; |
ab0ee2ca | 459 | |
8abe313a JG |
460 | assert(session_info); |
461 | if (session_info->channel_infos_ht) { | |
3b68d0a3 JR |
462 | ret = cds_lfht_destroy(session_info->channel_infos_ht, NULL); |
463 | if (ret) { | |
4c3f302b | 464 | ERR("[notification-thread] Failed to destroy channel information hash table"); |
3b68d0a3 | 465 | } |
8abe313a | 466 | } |
ea9a44f0 | 467 | lttng_session_trigger_list_destroy(session_info->trigger_list); |
1eee26c5 JG |
468 | |
469 | rcu_read_lock(); | |
470 | cds_lfht_del(session_info->sessions_ht, | |
471 | &session_info->sessions_ht_node); | |
472 | rcu_read_unlock(); | |
8abe313a | 473 | free(session_info->name); |
83b934ad | 474 | call_rcu(&session_info->rcu_node, free_session_info_rcu); |
8abe313a | 475 | } |
ab0ee2ca | 476 | |
8abe313a JG |
477 | static |
478 | void session_info_get(struct session_info *session_info) | |
479 | { | |
480 | if (!session_info) { | |
481 | return; | |
482 | } | |
483 | lttng_ref_get(&session_info->ref); | |
484 | } | |
485 | ||
486 | static | |
487 | void session_info_put(struct session_info *session_info) | |
488 | { | |
489 | if (!session_info) { | |
490 | return; | |
ab0ee2ca | 491 | } |
8abe313a JG |
492 | lttng_ref_put(&session_info->ref); |
493 | } | |
494 | ||
495 | static | |
ea9a44f0 | 496 | struct session_info *session_info_create(const char *name, uid_t uid, gid_t gid, |
1eee26c5 JG |
497 | struct lttng_session_trigger_list *trigger_list, |
498 | struct cds_lfht *sessions_ht) | |
8abe313a JG |
499 | { |
500 | struct session_info *session_info; | |
ab0ee2ca | 501 | |
8abe313a | 502 | assert(name); |
ab0ee2ca | 503 | |
8abe313a JG |
504 | session_info = zmalloc(sizeof(*session_info)); |
505 | if (!session_info) { | |
506 | goto end; | |
507 | } | |
508 | lttng_ref_init(&session_info->ref, session_info_destroy); | |
509 | ||
510 | session_info->channel_infos_ht = cds_lfht_new(DEFAULT_HT_SIZE, | |
511 | 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL); | |
512 | if (!session_info->channel_infos_ht) { | |
ab0ee2ca JG |
513 | goto error; |
514 | } | |
8abe313a JG |
515 | |
516 | cds_lfht_node_init(&session_info->sessions_ht_node); | |
517 | session_info->name = strdup(name); | |
518 | if (!session_info->name) { | |
ab0ee2ca JG |
519 | goto error; |
520 | } | |
8abe313a JG |
521 | session_info->uid = uid; |
522 | session_info->gid = gid; | |
ea9a44f0 | 523 | session_info->trigger_list = trigger_list; |
1eee26c5 | 524 | session_info->sessions_ht = sessions_ht; |
8abe313a JG |
525 | end: |
526 | return session_info; | |
527 | error: | |
528 | session_info_put(session_info); | |
529 | return NULL; | |
530 | } | |
531 | ||
532 | static | |
533 | void session_info_add_channel(struct session_info *session_info, | |
534 | struct channel_info *channel_info) | |
535 | { | |
536 | rcu_read_lock(); | |
537 | cds_lfht_add(session_info->channel_infos_ht, | |
538 | hash_channel_key(&channel_info->key), | |
539 | &channel_info->session_info_channels_ht_node); | |
540 | rcu_read_unlock(); | |
541 | } | |
542 | ||
543 | static | |
544 | void session_info_remove_channel(struct session_info *session_info, | |
545 | struct channel_info *channel_info) | |
546 | { | |
547 | rcu_read_lock(); | |
548 | cds_lfht_del(session_info->channel_infos_ht, | |
549 | &channel_info->session_info_channels_ht_node); | |
550 | rcu_read_unlock(); | |
551 | } | |
552 | ||
553 | static | |
554 | struct channel_info *channel_info_create(const char *channel_name, | |
555 | struct channel_key *channel_key, uint64_t channel_capacity, | |
556 | struct session_info *session_info) | |
557 | { | |
558 | struct channel_info *channel_info = zmalloc(sizeof(*channel_info)); | |
559 | ||
560 | if (!channel_info) { | |
561 | goto end; | |
562 | } | |
563 | ||
ab0ee2ca | 564 | cds_lfht_node_init(&channel_info->channels_ht_node); |
8abe313a JG |
565 | cds_lfht_node_init(&channel_info->session_info_channels_ht_node); |
566 | memcpy(&channel_info->key, channel_key, sizeof(*channel_key)); | |
567 | channel_info->capacity = channel_capacity; | |
568 | ||
569 | channel_info->name = strdup(channel_name); | |
570 | if (!channel_info->name) { | |
571 | goto error; | |
572 | } | |
573 | ||
574 | /* | |
575 | * Set the references between session and channel infos: | |
576 | * - channel_info holds a strong reference to session_info | |
577 | * - session_info holds a weak reference to channel_info | |
578 | */ | |
579 | session_info_get(session_info); | |
580 | session_info_add_channel(session_info, channel_info); | |
581 | channel_info->session_info = session_info; | |
ab0ee2ca | 582 | end: |
8abe313a | 583 | return channel_info; |
ab0ee2ca | 584 | error: |
8abe313a | 585 | channel_info_destroy(channel_info); |
ab0ee2ca JG |
586 | return NULL; |
587 | } | |
588 | ||
f2b3ef9f | 589 | LTTNG_HIDDEN |
505b2d90 JG |
590 | bool notification_client_list_get(struct notification_client_list *list) |
591 | { | |
592 | return urcu_ref_get_unless_zero(&list->ref); | |
593 | } | |
594 | ||
595 | static | |
596 | void free_notification_client_list_rcu(struct rcu_head *node) | |
597 | { | |
598 | free(caa_container_of(node, struct notification_client_list, | |
599 | rcu_node)); | |
600 | } | |
601 | ||
602 | static | |
603 | void notification_client_list_release(struct urcu_ref *list_ref) | |
604 | { | |
605 | struct notification_client_list *list = | |
606 | container_of(list_ref, typeof(*list), ref); | |
607 | struct notification_client_list_element *client_list_element, *tmp; | |
608 | ||
609 | if (list->notification_trigger_clients_ht) { | |
610 | rcu_read_lock(); | |
611 | cds_lfht_del(list->notification_trigger_clients_ht, | |
612 | &list->notification_trigger_clients_ht_node); | |
613 | rcu_read_unlock(); | |
614 | list->notification_trigger_clients_ht = NULL; | |
615 | } | |
616 | cds_list_for_each_entry_safe(client_list_element, tmp, | |
617 | &list->list, node) { | |
618 | free(client_list_element); | |
619 | } | |
620 | pthread_mutex_destroy(&list->lock); | |
621 | call_rcu(&list->rcu_node, free_notification_client_list_rcu); | |
622 | } | |
623 | ||
624 | static | |
625 | struct notification_client_list *notification_client_list_create( | |
626 | const struct lttng_trigger *trigger) | |
627 | { | |
628 | struct notification_client_list *client_list = | |
629 | zmalloc(sizeof(*client_list)); | |
630 | ||
631 | if (!client_list) { | |
632 | goto error; | |
633 | } | |
634 | pthread_mutex_init(&client_list->lock, NULL); | |
635 | urcu_ref_init(&client_list->ref); | |
636 | cds_lfht_node_init(&client_list->notification_trigger_clients_ht_node); | |
637 | CDS_INIT_LIST_HEAD(&client_list->list); | |
638 | client_list->trigger = trigger; | |
639 | error: | |
640 | return client_list; | |
641 | } | |
642 | ||
643 | static | |
644 | void publish_notification_client_list( | |
645 | struct notification_thread_state *state, | |
646 | struct notification_client_list *list) | |
647 | { | |
648 | const struct lttng_condition *condition = | |
649 | lttng_trigger_get_const_condition(list->trigger); | |
650 | ||
651 | assert(!list->notification_trigger_clients_ht); | |
f2b3ef9f | 652 | notification_client_list_get(list); |
505b2d90 JG |
653 | |
654 | list->notification_trigger_clients_ht = | |
655 | state->notification_trigger_clients_ht; | |
656 | ||
657 | rcu_read_lock(); | |
658 | cds_lfht_add(state->notification_trigger_clients_ht, | |
659 | lttng_condition_hash(condition), | |
660 | &list->notification_trigger_clients_ht_node); | |
661 | rcu_read_unlock(); | |
662 | } | |
663 | ||
f2b3ef9f | 664 | LTTNG_HIDDEN |
505b2d90 JG |
665 | void notification_client_list_put(struct notification_client_list *list) |
666 | { | |
667 | if (!list) { | |
668 | return; | |
669 | } | |
670 | return urcu_ref_put(&list->ref, notification_client_list_release); | |
671 | } | |
672 | ||
673 | /* Provides a reference to the returned list. */ | |
ed327204 JG |
674 | static |
675 | struct notification_client_list *get_client_list_from_condition( | |
676 | struct notification_thread_state *state, | |
677 | const struct lttng_condition *condition) | |
678 | { | |
679 | struct cds_lfht_node *node; | |
680 | struct cds_lfht_iter iter; | |
505b2d90 | 681 | struct notification_client_list *list = NULL; |
ed327204 | 682 | |
505b2d90 | 683 | rcu_read_lock(); |
ed327204 JG |
684 | cds_lfht_lookup(state->notification_trigger_clients_ht, |
685 | lttng_condition_hash(condition), | |
686 | match_client_list_condition, | |
687 | condition, | |
688 | &iter); | |
689 | node = cds_lfht_iter_get_node(&iter); | |
505b2d90 JG |
690 | if (node) { |
691 | list = container_of(node, struct notification_client_list, | |
692 | notification_trigger_clients_ht_node); | |
693 | list = notification_client_list_get(list) ? list : NULL; | |
694 | } | |
ed327204 | 695 | |
505b2d90 JG |
696 | rcu_read_unlock(); |
697 | return list; | |
ed327204 JG |
698 | } |
699 | ||
e4db5ace | 700 | static |
f82f93a1 JG |
701 | int evaluate_channel_condition_for_client( |
702 | const struct lttng_condition *condition, | |
703 | struct notification_thread_state *state, | |
704 | struct lttng_evaluation **evaluation, | |
705 | uid_t *session_uid, gid_t *session_gid) | |
e4db5ace JR |
706 | { |
707 | int ret; | |
708 | struct cds_lfht_iter iter; | |
709 | struct cds_lfht_node *node; | |
710 | struct channel_info *channel_info = NULL; | |
711 | struct channel_key *channel_key = NULL; | |
712 | struct channel_state_sample *last_sample = NULL; | |
713 | struct lttng_channel_trigger_list *channel_trigger_list = NULL; | |
e4db5ace | 714 | |
505b2d90 JG |
715 | rcu_read_lock(); |
716 | ||
f82f93a1 | 717 | /* Find the channel associated with the condition. */ |
e4db5ace | 718 | cds_lfht_for_each_entry(state->channel_triggers_ht, &iter, |
f82f93a1 | 719 | channel_trigger_list, channel_triggers_ht_node) { |
e4db5ace JR |
720 | struct lttng_trigger_list_element *element; |
721 | ||
722 | cds_list_for_each_entry(element, &channel_trigger_list->list, node) { | |
9b63a4aa | 723 | const struct lttng_condition *current_condition = |
f82f93a1 | 724 | lttng_trigger_get_const_condition( |
e4db5ace JR |
725 | element->trigger); |
726 | ||
727 | assert(current_condition); | |
728 | if (!lttng_condition_is_equal(condition, | |
2ae99f0b | 729 | current_condition)) { |
e4db5ace JR |
730 | continue; |
731 | } | |
732 | ||
733 | /* Found the trigger, save the channel key. */ | |
734 | channel_key = &channel_trigger_list->channel_key; | |
735 | break; | |
736 | } | |
737 | if (channel_key) { | |
738 | /* The channel key was found stop iteration. */ | |
739 | break; | |
740 | } | |
741 | } | |
742 | ||
743 | if (!channel_key){ | |
744 | /* No channel found; normal exit. */ | |
f82f93a1 | 745 | DBG("[notification-thread] No known channel associated with newly subscribed-to condition"); |
e4db5ace JR |
746 | ret = 0; |
747 | goto end; | |
748 | } | |
749 | ||
750 | /* Fetch channel info for the matching channel. */ | |
751 | cds_lfht_lookup(state->channels_ht, | |
752 | hash_channel_key(channel_key), | |
753 | match_channel_info, | |
754 | channel_key, | |
755 | &iter); | |
756 | node = cds_lfht_iter_get_node(&iter); | |
757 | assert(node); | |
758 | channel_info = caa_container_of(node, struct channel_info, | |
759 | channels_ht_node); | |
760 | ||
761 | /* Retrieve the channel's last sample, if it exists. */ | |
762 | cds_lfht_lookup(state->channel_state_ht, | |
763 | hash_channel_key(channel_key), | |
764 | match_channel_state_sample, | |
765 | channel_key, | |
766 | &iter); | |
767 | node = cds_lfht_iter_get_node(&iter); | |
768 | if (node) { | |
769 | last_sample = caa_container_of(node, | |
770 | struct channel_state_sample, | |
771 | channel_state_ht_node); | |
772 | } else { | |
773 | /* Nothing to evaluate, no sample was ever taken. Normal exit */ | |
774 | DBG("[notification-thread] No channel sample associated with newly subscribed-to condition"); | |
775 | ret = 0; | |
776 | goto end; | |
777 | } | |
778 | ||
f82f93a1 | 779 | ret = evaluate_buffer_condition(condition, evaluation, state, |
e8360425 JD |
780 | NULL, last_sample, |
781 | 0, channel_info->session_info->consumed_data_size, | |
782 | channel_info); | |
e4db5ace | 783 | if (ret) { |
066a3c82 | 784 | WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition"); |
e4db5ace JR |
785 | goto end; |
786 | } | |
787 | ||
f82f93a1 JG |
788 | *session_uid = channel_info->session_info->uid; |
789 | *session_gid = channel_info->session_info->gid; | |
790 | end: | |
505b2d90 | 791 | rcu_read_unlock(); |
f82f93a1 JG |
792 | return ret; |
793 | } | |
794 | ||
795 | static | |
796 | const char *get_condition_session_name(const struct lttng_condition *condition) | |
797 | { | |
798 | const char *session_name = NULL; | |
799 | enum lttng_condition_status status; | |
800 | ||
801 | switch (lttng_condition_get_type(condition)) { | |
802 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
803 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
804 | status = lttng_condition_buffer_usage_get_session_name( | |
805 | condition, &session_name); | |
806 | break; | |
807 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: | |
808 | status = lttng_condition_session_consumed_size_get_session_name( | |
809 | condition, &session_name); | |
810 | break; | |
811 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: | |
812 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
813 | status = lttng_condition_session_rotation_get_session_name( | |
814 | condition, &session_name); | |
815 | break; | |
816 | default: | |
817 | abort(); | |
818 | } | |
819 | if (status != LTTNG_CONDITION_STATUS_OK) { | |
820 | ERR("[notification-thread] Failed to retrieve session rotation condition's session name"); | |
821 | goto end; | |
822 | } | |
823 | end: | |
824 | return session_name; | |
825 | } | |
826 | ||
f82f93a1 JG |
827 | static |
828 | int evaluate_session_condition_for_client( | |
829 | const struct lttng_condition *condition, | |
830 | struct notification_thread_state *state, | |
831 | struct lttng_evaluation **evaluation, | |
832 | uid_t *session_uid, gid_t *session_gid) | |
833 | { | |
834 | int ret; | |
835 | struct cds_lfht_iter iter; | |
836 | struct cds_lfht_node *node; | |
837 | const char *session_name; | |
838 | struct session_info *session_info = NULL; | |
839 | ||
505b2d90 | 840 | rcu_read_lock(); |
f82f93a1 JG |
841 | session_name = get_condition_session_name(condition); |
842 | ||
843 | /* Find the session associated with the trigger. */ | |
844 | cds_lfht_lookup(state->sessions_ht, | |
845 | hash_key_str(session_name, lttng_ht_seed), | |
846 | match_session, | |
847 | session_name, | |
848 | &iter); | |
849 | node = cds_lfht_iter_get_node(&iter); | |
850 | if (!node) { | |
851 | DBG("[notification-thread] No known session matching name \"%s\"", | |
852 | session_name); | |
853 | ret = 0; | |
854 | goto end; | |
855 | } | |
856 | ||
857 | session_info = caa_container_of(node, struct session_info, | |
858 | sessions_ht_node); | |
859 | session_info_get(session_info); | |
860 | ||
861 | /* | |
862 | * Evaluation is performed in-line here since only one type of | |
863 | * session-bound condition is handled for the moment. | |
864 | */ | |
865 | switch (lttng_condition_get_type(condition)) { | |
866 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: | |
867 | if (!session_info->rotation.ongoing) { | |
be558f88 | 868 | ret = 0; |
f82f93a1 JG |
869 | goto end_session_put; |
870 | } | |
871 | ||
872 | *evaluation = lttng_evaluation_session_rotation_ongoing_create( | |
873 | session_info->rotation.id); | |
874 | if (!*evaluation) { | |
875 | /* Fatal error. */ | |
876 | ERR("[notification-thread] Failed to create session rotation ongoing evaluation for session \"%s\"", | |
877 | session_info->name); | |
878 | ret = -1; | |
879 | goto end_session_put; | |
880 | } | |
881 | ret = 0; | |
882 | break; | |
883 | default: | |
884 | ret = 0; | |
885 | goto end_session_put; | |
886 | } | |
887 | ||
888 | *session_uid = session_info->uid; | |
889 | *session_gid = session_info->gid; | |
890 | ||
891 | end_session_put: | |
892 | session_info_put(session_info); | |
893 | end: | |
505b2d90 | 894 | rcu_read_unlock(); |
f82f93a1 JG |
895 | return ret; |
896 | } | |
897 | ||
f82f93a1 JG |
898 | static |
899 | int evaluate_condition_for_client(const struct lttng_trigger *trigger, | |
900 | const struct lttng_condition *condition, | |
901 | struct notification_client *client, | |
902 | struct notification_thread_state *state) | |
903 | { | |
904 | int ret; | |
905 | struct lttng_evaluation *evaluation = NULL; | |
505b2d90 JG |
906 | struct notification_client_list client_list = { |
907 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
908 | }; | |
f82f93a1 JG |
909 | struct notification_client_list_element client_list_element = { 0 }; |
910 | uid_t object_uid = 0; | |
911 | gid_t object_gid = 0; | |
912 | ||
913 | assert(trigger); | |
914 | assert(condition); | |
915 | assert(client); | |
916 | assert(state); | |
917 | ||
918 | switch (get_condition_binding_object(condition)) { | |
919 | case LTTNG_OBJECT_TYPE_SESSION: | |
920 | ret = evaluate_session_condition_for_client(condition, state, | |
921 | &evaluation, &object_uid, &object_gid); | |
922 | break; | |
923 | case LTTNG_OBJECT_TYPE_CHANNEL: | |
924 | ret = evaluate_channel_condition_for_client(condition, state, | |
925 | &evaluation, &object_uid, &object_gid); | |
926 | break; | |
927 | case LTTNG_OBJECT_TYPE_NONE: | |
928 | ret = 0; | |
929 | goto end; | |
930 | case LTTNG_OBJECT_TYPE_UNKNOWN: | |
931 | default: | |
932 | ret = -1; | |
933 | goto end; | |
934 | } | |
af0c318d JG |
935 | if (ret) { |
936 | /* Fatal error. */ | |
937 | goto end; | |
938 | } | |
e4db5ace JR |
939 | if (!evaluation) { |
940 | /* Evaluation yielded nothing. Normal exit. */ | |
941 | DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client"); | |
942 | ret = 0; | |
943 | goto end; | |
944 | } | |
945 | ||
946 | /* | |
947 | * Create a temporary client list with the client currently | |
948 | * subscribing. | |
949 | */ | |
505b2d90 | 950 | cds_lfht_node_init(&client_list.notification_trigger_clients_ht_node); |
e4db5ace JR |
951 | CDS_INIT_LIST_HEAD(&client_list.list); |
952 | client_list.trigger = trigger; | |
953 | ||
954 | CDS_INIT_LIST_HEAD(&client_list_element.node); | |
955 | client_list_element.client = client; | |
956 | cds_list_add(&client_list_element.node, &client_list.list); | |
957 | ||
958 | /* Send evaluation result to the newly-subscribed client. */ | |
959 | DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client"); | |
960 | ret = send_evaluation_to_clients(trigger, evaluation, &client_list, | |
f82f93a1 | 961 | state, object_uid, object_gid); |
e4db5ace JR |
962 | |
963 | end: | |
964 | return ret; | |
965 | } | |
966 | ||
ab0ee2ca JG |
967 | static |
968 | int notification_thread_client_subscribe(struct notification_client *client, | |
969 | struct lttng_condition *condition, | |
970 | struct notification_thread_state *state, | |
971 | enum lttng_notification_channel_status *_status) | |
972 | { | |
973 | int ret = 0; | |
505b2d90 | 974 | struct notification_client_list *client_list = NULL; |
ab0ee2ca JG |
975 | struct lttng_condition_list_element *condition_list_element = NULL; |
976 | struct notification_client_list_element *client_list_element = NULL; | |
977 | enum lttng_notification_channel_status status = | |
978 | LTTNG_NOTIFICATION_CHANNEL_STATUS_OK; | |
979 | ||
980 | /* | |
981 | * Ensure that the client has not already subscribed to this condition | |
982 | * before. | |
983 | */ | |
984 | cds_list_for_each_entry(condition_list_element, &client->condition_list, node) { | |
985 | if (lttng_condition_is_equal(condition_list_element->condition, | |
986 | condition)) { | |
987 | status = LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED; | |
988 | goto end; | |
989 | } | |
990 | } | |
991 | ||
992 | condition_list_element = zmalloc(sizeof(*condition_list_element)); | |
993 | if (!condition_list_element) { | |
994 | ret = -1; | |
995 | goto error; | |
996 | } | |
997 | client_list_element = zmalloc(sizeof(*client_list_element)); | |
998 | if (!client_list_element) { | |
999 | ret = -1; | |
1000 | goto error; | |
1001 | } | |
1002 | ||
ab0ee2ca JG |
1003 | /* |
1004 | * Add the newly-subscribed condition to the client's subscription list. | |
1005 | */ | |
1006 | CDS_INIT_LIST_HEAD(&condition_list_element->node); | |
1007 | condition_list_element->condition = condition; | |
1008 | cds_list_add(&condition_list_element->node, &client->condition_list); | |
1009 | ||
ed327204 JG |
1010 | client_list = get_client_list_from_condition(state, condition); |
1011 | if (!client_list) { | |
2ae99f0b JG |
1012 | /* |
1013 | * No notification-emiting trigger registered with this | |
1014 | * condition. We don't evaluate the condition right away | |
1015 | * since this trigger is not registered yet. | |
1016 | */ | |
4fb43b68 | 1017 | free(client_list_element); |
505b2d90 | 1018 | goto end; |
ab0ee2ca JG |
1019 | } |
1020 | ||
2ae99f0b JG |
1021 | /* |
1022 | * The condition to which the client just subscribed is evaluated | |
1023 | * at this point so that conditions that are already TRUE result | |
1024 | * in a notification being sent out. | |
505b2d90 JG |
1025 | * |
1026 | * The client_list's trigger is used without locking the list itself. | |
1027 | * This is correct since the list doesn't own the trigger and the | |
1028 | * object is immutable. | |
2ae99f0b | 1029 | */ |
e4db5ace JR |
1030 | if (evaluate_condition_for_client(client_list->trigger, condition, |
1031 | client, state)) { | |
1032 | WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting."); | |
1033 | ret = -1; | |
4bd5b4af | 1034 | free(client_list_element); |
505b2d90 | 1035 | goto end; |
e4db5ace JR |
1036 | } |
1037 | ||
1038 | /* | |
1039 | * Add the client to the list of clients interested in a given trigger | |
1040 | * if a "notification" trigger with a corresponding condition was | |
1041 | * added prior. | |
1042 | */ | |
ab0ee2ca JG |
1043 | client_list_element->client = client; |
1044 | CDS_INIT_LIST_HEAD(&client_list_element->node); | |
505b2d90 JG |
1045 | |
1046 | pthread_mutex_lock(&client_list->lock); | |
ab0ee2ca | 1047 | cds_list_add(&client_list_element->node, &client_list->list); |
505b2d90 | 1048 | pthread_mutex_unlock(&client_list->lock); |
ab0ee2ca JG |
1049 | end: |
1050 | if (_status) { | |
1051 | *_status = status; | |
1052 | } | |
505b2d90 JG |
1053 | if (client_list) { |
1054 | notification_client_list_put(client_list); | |
1055 | } | |
ab0ee2ca JG |
1056 | return ret; |
1057 | error: | |
1058 | free(condition_list_element); | |
1059 | free(client_list_element); | |
1060 | return ret; | |
1061 | } | |
1062 | ||
1063 | static | |
1064 | int notification_thread_client_unsubscribe( | |
1065 | struct notification_client *client, | |
1066 | struct lttng_condition *condition, | |
1067 | struct notification_thread_state *state, | |
1068 | enum lttng_notification_channel_status *_status) | |
1069 | { | |
ab0ee2ca JG |
1070 | struct notification_client_list *client_list; |
1071 | struct lttng_condition_list_element *condition_list_element, | |
1072 | *condition_tmp; | |
1073 | struct notification_client_list_element *client_list_element, | |
1074 | *client_tmp; | |
1075 | bool condition_found = false; | |
1076 | enum lttng_notification_channel_status status = | |
1077 | LTTNG_NOTIFICATION_CHANNEL_STATUS_OK; | |
1078 | ||
1079 | /* Remove the condition from the client's condition list. */ | |
1080 | cds_list_for_each_entry_safe(condition_list_element, condition_tmp, | |
1081 | &client->condition_list, node) { | |
1082 | if (!lttng_condition_is_equal(condition_list_element->condition, | |
1083 | condition)) { | |
1084 | continue; | |
1085 | } | |
1086 | ||
1087 | cds_list_del(&condition_list_element->node); | |
1088 | /* | |
1089 | * The caller may be iterating on the client's conditions to | |
1090 | * tear down a client's connection. In this case, the condition | |
1091 | * will be destroyed at the end. | |
1092 | */ | |
1093 | if (condition != condition_list_element->condition) { | |
1094 | lttng_condition_destroy( | |
1095 | condition_list_element->condition); | |
1096 | } | |
1097 | free(condition_list_element); | |
1098 | condition_found = true; | |
1099 | break; | |
1100 | } | |
1101 | ||
1102 | if (!condition_found) { | |
1103 | status = LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION; | |
1104 | goto end; | |
1105 | } | |
1106 | ||
1107 | /* | |
1108 | * Remove the client from the list of clients interested the trigger | |
1109 | * matching the condition. | |
1110 | */ | |
ed327204 JG |
1111 | client_list = get_client_list_from_condition(state, condition); |
1112 | if (!client_list) { | |
505b2d90 | 1113 | goto end; |
ab0ee2ca JG |
1114 | } |
1115 | ||
505b2d90 | 1116 | pthread_mutex_lock(&client_list->lock); |
ab0ee2ca JG |
1117 | cds_list_for_each_entry_safe(client_list_element, client_tmp, |
1118 | &client_list->list, node) { | |
505b2d90 | 1119 | if (client_list_element->client->id != client->id) { |
ab0ee2ca JG |
1120 | continue; |
1121 | } | |
1122 | cds_list_del(&client_list_element->node); | |
1123 | free(client_list_element); | |
1124 | break; | |
1125 | } | |
505b2d90 JG |
1126 | pthread_mutex_unlock(&client_list->lock); |
1127 | notification_client_list_put(client_list); | |
1128 | client_list = NULL; | |
ab0ee2ca JG |
1129 | end: |
1130 | lttng_condition_destroy(condition); | |
1131 | if (_status) { | |
1132 | *_status = status; | |
1133 | } | |
1134 | return 0; | |
1135 | } | |
1136 | ||
83b934ad MD |
1137 | static |
1138 | void free_notification_client_rcu(struct rcu_head *node) | |
1139 | { | |
1140 | free(caa_container_of(node, struct notification_client, rcu_node)); | |
1141 | } | |
1142 | ||
ab0ee2ca JG |
1143 | static |
1144 | void notification_client_destroy(struct notification_client *client, | |
1145 | struct notification_thread_state *state) | |
1146 | { | |
ab0ee2ca JG |
1147 | if (!client) { |
1148 | return; | |
1149 | } | |
1150 | ||
505b2d90 JG |
1151 | /* |
1152 | * The client object is not reachable by other threads, no need to lock | |
1153 | * the client here. | |
1154 | */ | |
ab0ee2ca JG |
1155 | if (client->socket >= 0) { |
1156 | (void) lttcomm_close_unix_sock(client->socket); | |
ac1889bf | 1157 | client->socket = -1; |
ab0ee2ca | 1158 | } |
505b2d90 | 1159 | client->communication.active = false; |
882093ee JR |
1160 | lttng_payload_reset(&client->communication.inbound.payload); |
1161 | lttng_payload_reset(&client->communication.outbound.payload); | |
505b2d90 | 1162 | pthread_mutex_destroy(&client->lock); |
83b934ad | 1163 | call_rcu(&client->rcu_node, free_notification_client_rcu); |
ab0ee2ca JG |
1164 | } |
1165 | ||
1166 | /* | |
1167 | * Call with rcu_read_lock held (and hold for the lifetime of the returned | |
1168 | * client pointer). | |
1169 | */ | |
1170 | static | |
1171 | struct notification_client *get_client_from_socket(int socket, | |
1172 | struct notification_thread_state *state) | |
1173 | { | |
1174 | struct cds_lfht_iter iter; | |
1175 | struct cds_lfht_node *node; | |
1176 | struct notification_client *client = NULL; | |
1177 | ||
1178 | cds_lfht_lookup(state->client_socket_ht, | |
ac1889bf JG |
1179 | hash_client_socket(socket), |
1180 | match_client_socket, | |
ab0ee2ca JG |
1181 | (void *) (unsigned long) socket, |
1182 | &iter); | |
1183 | node = cds_lfht_iter_get_node(&iter); | |
1184 | if (!node) { | |
1185 | goto end; | |
1186 | } | |
1187 | ||
1188 | client = caa_container_of(node, struct notification_client, | |
1189 | client_socket_ht_node); | |
1190 | end: | |
1191 | return client; | |
1192 | } | |
1193 | ||
f2b3ef9f JG |
1194 | /* |
1195 | * Call with rcu_read_lock held (and hold for the lifetime of the returned | |
1196 | * client pointer). | |
1197 | */ | |
1198 | static | |
1199 | struct notification_client *get_client_from_id(notification_client_id id, | |
1200 | struct notification_thread_state *state) | |
1201 | { | |
1202 | struct cds_lfht_iter iter; | |
1203 | struct cds_lfht_node *node; | |
1204 | struct notification_client *client = NULL; | |
1205 | ||
1206 | cds_lfht_lookup(state->client_id_ht, | |
1207 | hash_client_id(id), | |
1208 | match_client_id, | |
1209 | &id, | |
1210 | &iter); | |
1211 | node = cds_lfht_iter_get_node(&iter); | |
1212 | if (!node) { | |
1213 | goto end; | |
1214 | } | |
1215 | ||
1216 | client = caa_container_of(node, struct notification_client, | |
1217 | client_id_ht_node); | |
1218 | end: | |
1219 | return client; | |
1220 | } | |
1221 | ||
ab0ee2ca | 1222 | static |
e8360425 | 1223 | bool buffer_usage_condition_applies_to_channel( |
51eab943 JG |
1224 | const struct lttng_condition *condition, |
1225 | const struct channel_info *channel_info) | |
ab0ee2ca JG |
1226 | { |
1227 | enum lttng_condition_status status; | |
e8360425 JD |
1228 | enum lttng_domain_type condition_domain; |
1229 | const char *condition_session_name = NULL; | |
1230 | const char *condition_channel_name = NULL; | |
ab0ee2ca | 1231 | |
e8360425 JD |
1232 | status = lttng_condition_buffer_usage_get_domain_type(condition, |
1233 | &condition_domain); | |
1234 | assert(status == LTTNG_CONDITION_STATUS_OK); | |
1235 | if (channel_info->key.domain != condition_domain) { | |
ab0ee2ca JG |
1236 | goto fail; |
1237 | } | |
1238 | ||
e8360425 JD |
1239 | status = lttng_condition_buffer_usage_get_session_name( |
1240 | condition, &condition_session_name); | |
1241 | assert((status == LTTNG_CONDITION_STATUS_OK) && condition_session_name); | |
1242 | ||
1243 | status = lttng_condition_buffer_usage_get_channel_name( | |
1244 | condition, &condition_channel_name); | |
1245 | assert((status == LTTNG_CONDITION_STATUS_OK) && condition_channel_name); | |
1246 | ||
1247 | if (strcmp(channel_info->session_info->name, condition_session_name)) { | |
1248 | goto fail; | |
1249 | } | |
1250 | if (strcmp(channel_info->name, condition_channel_name)) { | |
ab0ee2ca JG |
1251 | goto fail; |
1252 | } | |
1253 | ||
e8360425 JD |
1254 | return true; |
1255 | fail: | |
1256 | return false; | |
1257 | } | |
1258 | ||
1259 | static | |
1260 | bool session_consumed_size_condition_applies_to_channel( | |
51eab943 JG |
1261 | const struct lttng_condition *condition, |
1262 | const struct channel_info *channel_info) | |
e8360425 JD |
1263 | { |
1264 | enum lttng_condition_status status; | |
1265 | const char *condition_session_name = NULL; | |
1266 | ||
1267 | status = lttng_condition_session_consumed_size_get_session_name( | |
1268 | condition, &condition_session_name); | |
1269 | assert((status == LTTNG_CONDITION_STATUS_OK) && condition_session_name); | |
1270 | ||
1271 | if (strcmp(channel_info->session_info->name, condition_session_name)) { | |
ab0ee2ca JG |
1272 | goto fail; |
1273 | } | |
1274 | ||
e8360425 JD |
1275 | return true; |
1276 | fail: | |
1277 | return false; | |
1278 | } | |
ab0ee2ca | 1279 | |
51eab943 JG |
1280 | static |
1281 | bool trigger_applies_to_channel(const struct lttng_trigger *trigger, | |
1282 | const struct channel_info *channel_info) | |
1283 | { | |
1284 | const struct lttng_condition *condition; | |
e8360425 | 1285 | bool trigger_applies; |
ab0ee2ca | 1286 | |
51eab943 | 1287 | condition = lttng_trigger_get_const_condition(trigger); |
e8360425 | 1288 | if (!condition) { |
ab0ee2ca JG |
1289 | goto fail; |
1290 | } | |
e8360425 JD |
1291 | |
1292 | switch (lttng_condition_get_type(condition)) { | |
1293 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
1294 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
1295 | trigger_applies = buffer_usage_condition_applies_to_channel( | |
1296 | condition, channel_info); | |
1297 | break; | |
1298 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: | |
1299 | trigger_applies = session_consumed_size_condition_applies_to_channel( | |
1300 | condition, channel_info); | |
1301 | break; | |
1302 | default: | |
ab0ee2ca JG |
1303 | goto fail; |
1304 | } | |
1305 | ||
e8360425 | 1306 | return trigger_applies; |
ab0ee2ca JG |
1307 | fail: |
1308 | return false; | |
1309 | } | |
1310 | ||
1311 | static | |
1312 | bool trigger_applies_to_client(struct lttng_trigger *trigger, | |
1313 | struct notification_client *client) | |
1314 | { | |
1315 | bool applies = false; | |
1316 | struct lttng_condition_list_element *condition_list_element; | |
1317 | ||
1318 | cds_list_for_each_entry(condition_list_element, &client->condition_list, | |
1319 | node) { | |
1320 | applies = lttng_condition_is_equal( | |
1321 | condition_list_element->condition, | |
1322 | lttng_trigger_get_condition(trigger)); | |
1323 | if (applies) { | |
1324 | break; | |
1325 | } | |
1326 | } | |
1327 | return applies; | |
1328 | } | |
1329 | ||
ed327204 JG |
1330 | /* Must be called with RCU read lock held. */ |
1331 | static | |
1332 | struct lttng_session_trigger_list *get_session_trigger_list( | |
1333 | struct notification_thread_state *state, | |
1334 | const char *session_name) | |
1335 | { | |
1336 | struct lttng_session_trigger_list *list = NULL; | |
1337 | struct cds_lfht_node *node; | |
1338 | struct cds_lfht_iter iter; | |
1339 | ||
1340 | cds_lfht_lookup(state->session_triggers_ht, | |
1341 | hash_key_str(session_name, lttng_ht_seed), | |
1342 | match_session_trigger_list, | |
1343 | session_name, | |
1344 | &iter); | |
1345 | node = cds_lfht_iter_get_node(&iter); | |
1346 | if (!node) { | |
1347 | /* | |
1348 | * Not an error, the list of triggers applying to that session | |
1349 | * will be initialized when the session is created. | |
1350 | */ | |
1351 | DBG("[notification-thread] No trigger list found for session \"%s\" as it is not yet known to the notification system", | |
1352 | session_name); | |
1353 | goto end; | |
1354 | } | |
1355 | ||
e742b055 | 1356 | list = caa_container_of(node, |
ed327204 JG |
1357 | struct lttng_session_trigger_list, |
1358 | session_triggers_ht_node); | |
1359 | end: | |
1360 | return list; | |
1361 | } | |
1362 | ||
ea9a44f0 JG |
1363 | /* |
1364 | * Allocate an empty lttng_session_trigger_list for the session named | |
1365 | * 'session_name'. | |
1366 | * | |
1367 | * No ownership of 'session_name' is assumed by the session trigger list. | |
1368 | * It is the caller's responsability to ensure the session name is alive | |
1369 | * for as long as this list is. | |
1370 | */ | |
1371 | static | |
1372 | struct lttng_session_trigger_list *lttng_session_trigger_list_create( | |
1373 | const char *session_name, | |
1374 | struct cds_lfht *session_triggers_ht) | |
1375 | { | |
1376 | struct lttng_session_trigger_list *list; | |
1377 | ||
1378 | list = zmalloc(sizeof(*list)); | |
1379 | if (!list) { | |
1380 | goto end; | |
1381 | } | |
1382 | list->session_name = session_name; | |
1383 | CDS_INIT_LIST_HEAD(&list->list); | |
1384 | cds_lfht_node_init(&list->session_triggers_ht_node); | |
1385 | list->session_triggers_ht = session_triggers_ht; | |
1386 | ||
1387 | rcu_read_lock(); | |
1388 | /* Publish the list through the session_triggers_ht. */ | |
1389 | cds_lfht_add(session_triggers_ht, | |
1390 | hash_key_str(session_name, lttng_ht_seed), | |
1391 | &list->session_triggers_ht_node); | |
1392 | rcu_read_unlock(); | |
1393 | end: | |
1394 | return list; | |
1395 | } | |
1396 | ||
1397 | static | |
1398 | void free_session_trigger_list_rcu(struct rcu_head *node) | |
1399 | { | |
1400 | free(caa_container_of(node, struct lttng_session_trigger_list, | |
1401 | rcu_node)); | |
1402 | } | |
1403 | ||
1404 | static | |
1405 | void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list *list) | |
1406 | { | |
1407 | struct lttng_trigger_list_element *trigger_list_element, *tmp; | |
1408 | ||
1409 | /* Empty the list element by element, and then free the list itself. */ | |
1410 | cds_list_for_each_entry_safe(trigger_list_element, tmp, | |
1411 | &list->list, node) { | |
1412 | cds_list_del(&trigger_list_element->node); | |
1413 | free(trigger_list_element); | |
1414 | } | |
1415 | rcu_read_lock(); | |
1416 | /* Unpublish the list from the session_triggers_ht. */ | |
1417 | cds_lfht_del(list->session_triggers_ht, | |
1418 | &list->session_triggers_ht_node); | |
1419 | rcu_read_unlock(); | |
1420 | call_rcu(&list->rcu_node, free_session_trigger_list_rcu); | |
1421 | } | |
1422 | ||
1423 | static | |
1424 | int lttng_session_trigger_list_add(struct lttng_session_trigger_list *list, | |
f2b3ef9f | 1425 | struct lttng_trigger *trigger) |
ea9a44f0 JG |
1426 | { |
1427 | int ret = 0; | |
1428 | struct lttng_trigger_list_element *new_element = | |
1429 | zmalloc(sizeof(*new_element)); | |
1430 | ||
1431 | if (!new_element) { | |
1432 | ret = -1; | |
1433 | goto end; | |
1434 | } | |
1435 | CDS_INIT_LIST_HEAD(&new_element->node); | |
1436 | new_element->trigger = trigger; | |
1437 | cds_list_add(&new_element->node, &list->list); | |
1438 | end: | |
1439 | return ret; | |
1440 | } | |
1441 | ||
1442 | static | |
1443 | bool trigger_applies_to_session(const struct lttng_trigger *trigger, | |
1444 | const char *session_name) | |
1445 | { | |
1446 | bool applies = false; | |
1447 | const struct lttng_condition *condition; | |
1448 | ||
1449 | condition = lttng_trigger_get_const_condition(trigger); | |
1450 | switch (lttng_condition_get_type(condition)) { | |
1451 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: | |
1452 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
1453 | { | |
1454 | enum lttng_condition_status condition_status; | |
1455 | const char *condition_session_name; | |
1456 | ||
1457 | condition_status = lttng_condition_session_rotation_get_session_name( | |
1458 | condition, &condition_session_name); | |
1459 | if (condition_status != LTTNG_CONDITION_STATUS_OK) { | |
1460 | ERR("[notification-thread] Failed to retrieve session rotation condition's session name"); | |
1461 | goto end; | |
1462 | } | |
1463 | ||
1464 | assert(condition_session_name); | |
1465 | applies = !strcmp(condition_session_name, session_name); | |
1466 | break; | |
1467 | } | |
1468 | default: | |
1469 | goto end; | |
1470 | } | |
1471 | end: | |
1472 | return applies; | |
1473 | } | |
1474 | ||
1475 | /* | |
1476 | * Allocate and initialize an lttng_session_trigger_list which contains | |
1477 | * all triggers that apply to the session named 'session_name'. | |
1478 | * | |
1479 | * No ownership of 'session_name' is assumed by the session trigger list. | |
1480 | * It is the caller's responsability to ensure the session name is alive | |
1481 | * for as long as this list is. | |
1482 | */ | |
1483 | static | |
1484 | struct lttng_session_trigger_list *lttng_session_trigger_list_build( | |
1485 | const struct notification_thread_state *state, | |
1486 | const char *session_name) | |
1487 | { | |
1488 | int trigger_count = 0; | |
1489 | struct lttng_session_trigger_list *session_trigger_list = NULL; | |
1490 | struct lttng_trigger_ht_element *trigger_ht_element = NULL; | |
1491 | struct cds_lfht_iter iter; | |
1492 | ||
1493 | session_trigger_list = lttng_session_trigger_list_create(session_name, | |
1494 | state->session_triggers_ht); | |
1495 | ||
1496 | /* Add all triggers applying to the session named 'session_name'. */ | |
1497 | cds_lfht_for_each_entry(state->triggers_ht, &iter, trigger_ht_element, | |
1498 | node) { | |
1499 | int ret; | |
1500 | ||
1501 | if (!trigger_applies_to_session(trigger_ht_element->trigger, | |
1502 | session_name)) { | |
1503 | continue; | |
1504 | } | |
1505 | ||
1506 | ret = lttng_session_trigger_list_add(session_trigger_list, | |
1507 | trigger_ht_element->trigger); | |
1508 | if (ret) { | |
1509 | goto error; | |
1510 | } | |
1511 | ||
1512 | trigger_count++; | |
1513 | } | |
1514 | ||
1515 | DBG("[notification-thread] Found %i triggers that apply to newly created session", | |
1516 | trigger_count); | |
1517 | return session_trigger_list; | |
1518 | error: | |
1519 | lttng_session_trigger_list_destroy(session_trigger_list); | |
1520 | return NULL; | |
1521 | } | |
1522 | ||
8abe313a JG |
1523 | static |
1524 | struct session_info *find_or_create_session_info( | |
1525 | struct notification_thread_state *state, | |
1526 | const char *name, uid_t uid, gid_t gid) | |
1527 | { | |
1528 | struct session_info *session = NULL; | |
1529 | struct cds_lfht_node *node; | |
1530 | struct cds_lfht_iter iter; | |
ea9a44f0 | 1531 | struct lttng_session_trigger_list *trigger_list; |
8abe313a JG |
1532 | |
1533 | rcu_read_lock(); | |
1534 | cds_lfht_lookup(state->sessions_ht, | |
1535 | hash_key_str(name, lttng_ht_seed), | |
1536 | match_session, | |
1537 | name, | |
1538 | &iter); | |
1539 | node = cds_lfht_iter_get_node(&iter); | |
1540 | if (node) { | |
1541 | DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)", | |
1542 | name, uid, gid); | |
1543 | session = caa_container_of(node, struct session_info, | |
1544 | sessions_ht_node); | |
1545 | assert(session->uid == uid); | |
1546 | assert(session->gid == gid); | |
1eee26c5 JG |
1547 | session_info_get(session); |
1548 | goto end; | |
ea9a44f0 JG |
1549 | } |
1550 | ||
1551 | trigger_list = lttng_session_trigger_list_build(state, name); | |
1552 | if (!trigger_list) { | |
1553 | goto error; | |
8abe313a JG |
1554 | } |
1555 | ||
1eee26c5 JG |
1556 | session = session_info_create(name, uid, gid, trigger_list, |
1557 | state->sessions_ht); | |
8abe313a JG |
1558 | if (!session) { |
1559 | ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)", | |
1560 | name, uid, gid); | |
b248644d | 1561 | lttng_session_trigger_list_destroy(trigger_list); |
ea9a44f0 | 1562 | goto error; |
8abe313a | 1563 | } |
ea9a44f0 | 1564 | trigger_list = NULL; |
577983cb JG |
1565 | |
1566 | cds_lfht_add(state->sessions_ht, hash_key_str(name, lttng_ht_seed), | |
9b63a4aa | 1567 | &session->sessions_ht_node); |
1eee26c5 | 1568 | end: |
8abe313a JG |
1569 | rcu_read_unlock(); |
1570 | return session; | |
ea9a44f0 JG |
1571 | error: |
1572 | rcu_read_unlock(); | |
1573 | session_info_put(session); | |
1574 | return NULL; | |
8abe313a JG |
1575 | } |
1576 | ||
ab0ee2ca JG |
1577 | static |
1578 | int handle_notification_thread_command_add_channel( | |
8abe313a JG |
1579 | struct notification_thread_state *state, |
1580 | const char *session_name, uid_t session_uid, gid_t session_gid, | |
1581 | const char *channel_name, enum lttng_domain_type channel_domain, | |
1582 | uint64_t channel_key_int, uint64_t channel_capacity, | |
1583 | enum lttng_error_code *cmd_result) | |
ab0ee2ca JG |
1584 | { |
1585 | struct cds_list_head trigger_list; | |
8abe313a JG |
1586 | struct channel_info *new_channel_info = NULL; |
1587 | struct channel_key channel_key = { | |
1588 | .key = channel_key_int, | |
1589 | .domain = channel_domain, | |
1590 | }; | |
ab0ee2ca JG |
1591 | struct lttng_channel_trigger_list *channel_trigger_list = NULL; |
1592 | struct lttng_trigger_ht_element *trigger_ht_element = NULL; | |
1593 | int trigger_count = 0; | |
1594 | struct cds_lfht_iter iter; | |
8abe313a | 1595 | struct session_info *session_info = NULL; |
ab0ee2ca JG |
1596 | |
1597 | DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64 " in %s domain", | |
8abe313a JG |
1598 | channel_name, session_name, channel_key_int, |
1599 | channel_domain == LTTNG_DOMAIN_KERNEL ? "kernel" : "user space"); | |
ab0ee2ca JG |
1600 | |
1601 | CDS_INIT_LIST_HEAD(&trigger_list); | |
1602 | ||
8abe313a JG |
1603 | session_info = find_or_create_session_info(state, session_name, |
1604 | session_uid, session_gid); | |
1605 | if (!session_info) { | |
a9577b76 | 1606 | /* Allocation error or an internal error occurred. */ |
ab0ee2ca JG |
1607 | goto error; |
1608 | } | |
1609 | ||
8abe313a JG |
1610 | new_channel_info = channel_info_create(channel_name, &channel_key, |
1611 | channel_capacity, session_info); | |
1612 | if (!new_channel_info) { | |
1613 | goto error; | |
1614 | } | |
ab0ee2ca | 1615 | |
83b934ad | 1616 | rcu_read_lock(); |
ab0ee2ca JG |
1617 | /* Build a list of all triggers applying to the new channel. */ |
1618 | cds_lfht_for_each_entry(state->triggers_ht, &iter, trigger_ht_element, | |
1619 | node) { | |
1620 | struct lttng_trigger_list_element *new_element; | |
1621 | ||
1622 | if (!trigger_applies_to_channel(trigger_ht_element->trigger, | |
8abe313a | 1623 | new_channel_info)) { |
ab0ee2ca JG |
1624 | continue; |
1625 | } | |
1626 | ||
1627 | new_element = zmalloc(sizeof(*new_element)); | |
1628 | if (!new_element) { | |
83b934ad | 1629 | rcu_read_unlock(); |
ab0ee2ca JG |
1630 | goto error; |
1631 | } | |
1632 | CDS_INIT_LIST_HEAD(&new_element->node); | |
1633 | new_element->trigger = trigger_ht_element->trigger; | |
1634 | cds_list_add(&new_element->node, &trigger_list); | |
1635 | trigger_count++; | |
1636 | } | |
83b934ad | 1637 | rcu_read_unlock(); |
ab0ee2ca JG |
1638 | |
1639 | DBG("[notification-thread] Found %i triggers that apply to newly added channel", | |
1640 | trigger_count); | |
1641 | channel_trigger_list = zmalloc(sizeof(*channel_trigger_list)); | |
1642 | if (!channel_trigger_list) { | |
1643 | goto error; | |
1644 | } | |
8abe313a | 1645 | channel_trigger_list->channel_key = new_channel_info->key; |
ab0ee2ca JG |
1646 | CDS_INIT_LIST_HEAD(&channel_trigger_list->list); |
1647 | cds_lfht_node_init(&channel_trigger_list->channel_triggers_ht_node); | |
1648 | cds_list_splice(&trigger_list, &channel_trigger_list->list); | |
1649 | ||
1650 | rcu_read_lock(); | |
1651 | /* Add channel to the channel_ht which owns the channel_infos. */ | |
1652 | cds_lfht_add(state->channels_ht, | |
8abe313a | 1653 | hash_channel_key(&new_channel_info->key), |
ab0ee2ca JG |
1654 | &new_channel_info->channels_ht_node); |
1655 | /* | |
1656 | * Add the list of triggers associated with this channel to the | |
1657 | * channel_triggers_ht. | |
1658 | */ | |
1659 | cds_lfht_add(state->channel_triggers_ht, | |
8abe313a | 1660 | hash_channel_key(&new_channel_info->key), |
ab0ee2ca JG |
1661 | &channel_trigger_list->channel_triggers_ht_node); |
1662 | rcu_read_unlock(); | |
1eee26c5 | 1663 | session_info_put(session_info); |
ab0ee2ca JG |
1664 | *cmd_result = LTTNG_OK; |
1665 | return 0; | |
1666 | error: | |
ab0ee2ca | 1667 | channel_info_destroy(new_channel_info); |
8abe313a | 1668 | session_info_put(session_info); |
ab0ee2ca JG |
1669 | return 1; |
1670 | } | |
1671 | ||
83b934ad MD |
1672 | static |
1673 | void free_channel_trigger_list_rcu(struct rcu_head *node) | |
1674 | { | |
1675 | free(caa_container_of(node, struct lttng_channel_trigger_list, | |
1676 | rcu_node)); | |
1677 | } | |
1678 | ||
1679 | static | |
1680 | void free_channel_state_sample_rcu(struct rcu_head *node) | |
1681 | { | |
1682 | free(caa_container_of(node, struct channel_state_sample, | |
1683 | rcu_node)); | |
1684 | } | |
1685 | ||
ab0ee2ca JG |
1686 | static |
1687 | int handle_notification_thread_command_remove_channel( | |
1688 | struct notification_thread_state *state, | |
1689 | uint64_t channel_key, enum lttng_domain_type domain, | |
1690 | enum lttng_error_code *cmd_result) | |
1691 | { | |
1692 | struct cds_lfht_node *node; | |
1693 | struct cds_lfht_iter iter; | |
1694 | struct lttng_channel_trigger_list *trigger_list; | |
1695 | struct lttng_trigger_list_element *trigger_list_element, *tmp; | |
1696 | struct channel_key key = { .key = channel_key, .domain = domain }; | |
1697 | struct channel_info *channel_info; | |
1698 | ||
1699 | DBG("[notification-thread] Removing channel key = %" PRIu64 " in %s domain", | |
1700 | channel_key, domain == LTTNG_DOMAIN_KERNEL ? "kernel" : "user space"); | |
1701 | ||
1702 | rcu_read_lock(); | |
1703 | ||
1704 | cds_lfht_lookup(state->channel_triggers_ht, | |
1705 | hash_channel_key(&key), | |
1706 | match_channel_trigger_list, | |
1707 | &key, | |
1708 | &iter); | |
1709 | node = cds_lfht_iter_get_node(&iter); | |
1710 | /* | |
1711 | * There is a severe internal error if we are being asked to remove a | |
1712 | * channel that doesn't exist. | |
1713 | */ | |
1714 | if (!node) { | |
1715 | ERR("[notification-thread] Channel being removed is unknown to the notification thread"); | |
1716 | goto end; | |
1717 | } | |
1718 | ||
1719 | /* Free the list of triggers associated with this channel. */ | |
1720 | trigger_list = caa_container_of(node, struct lttng_channel_trigger_list, | |
1721 | channel_triggers_ht_node); | |
1722 | cds_list_for_each_entry_safe(trigger_list_element, tmp, | |
1723 | &trigger_list->list, node) { | |
1724 | cds_list_del(&trigger_list_element->node); | |
1725 | free(trigger_list_element); | |
1726 | } | |
1727 | cds_lfht_del(state->channel_triggers_ht, node); | |
83b934ad | 1728 | call_rcu(&trigger_list->rcu_node, free_channel_trigger_list_rcu); |
ab0ee2ca JG |
1729 | |
1730 | /* Free sampled channel state. */ | |
1731 | cds_lfht_lookup(state->channel_state_ht, | |
1732 | hash_channel_key(&key), | |
1733 | match_channel_state_sample, | |
1734 | &key, | |
1735 | &iter); | |
1736 | node = cds_lfht_iter_get_node(&iter); | |
1737 | /* | |
1738 | * This is expected to be NULL if the channel is destroyed before we | |
1739 | * received a sample. | |
1740 | */ | |
1741 | if (node) { | |
1742 | struct channel_state_sample *sample = caa_container_of(node, | |
1743 | struct channel_state_sample, | |
1744 | channel_state_ht_node); | |
1745 | ||
1746 | cds_lfht_del(state->channel_state_ht, node); | |
83b934ad | 1747 | call_rcu(&sample->rcu_node, free_channel_state_sample_rcu); |
ab0ee2ca JG |
1748 | } |
1749 | ||
1750 | /* Remove the channel from the channels_ht and free it. */ | |
1751 | cds_lfht_lookup(state->channels_ht, | |
1752 | hash_channel_key(&key), | |
1753 | match_channel_info, | |
1754 | &key, | |
1755 | &iter); | |
1756 | node = cds_lfht_iter_get_node(&iter); | |
1757 | assert(node); | |
1758 | channel_info = caa_container_of(node, struct channel_info, | |
1759 | channels_ht_node); | |
1760 | cds_lfht_del(state->channels_ht, node); | |
1761 | channel_info_destroy(channel_info); | |
1762 | end: | |
1763 | rcu_read_unlock(); | |
1764 | *cmd_result = LTTNG_OK; | |
1765 | return 0; | |
1766 | } | |
1767 | ||
0ca52944 | 1768 | static |
ed327204 | 1769 | int handle_notification_thread_command_session_rotation( |
0ca52944 | 1770 | struct notification_thread_state *state, |
ed327204 JG |
1771 | enum notification_thread_command_type cmd_type, |
1772 | const char *session_name, uid_t session_uid, gid_t session_gid, | |
1773 | uint64_t trace_archive_chunk_id, | |
1774 | struct lttng_trace_archive_location *location, | |
1775 | enum lttng_error_code *_cmd_result) | |
0ca52944 | 1776 | { |
ed327204 JG |
1777 | int ret = 0; |
1778 | enum lttng_error_code cmd_result = LTTNG_OK; | |
1779 | struct lttng_session_trigger_list *trigger_list; | |
1780 | struct lttng_trigger_list_element *trigger_list_element; | |
1781 | struct session_info *session_info; | |
f2b3ef9f | 1782 | const struct lttng_credentials session_creds = { |
ff588497 JR |
1783 | .uid = LTTNG_OPTIONAL_INIT_VALUE(session_uid), |
1784 | .gid = LTTNG_OPTIONAL_INIT_VALUE(session_gid), | |
f2b3ef9f | 1785 | }; |
0ca52944 | 1786 | |
ed327204 JG |
1787 | rcu_read_lock(); |
1788 | ||
1789 | session_info = find_or_create_session_info(state, session_name, | |
1790 | session_uid, session_gid); | |
1791 | if (!session_info) { | |
a9577b76 | 1792 | /* Allocation error or an internal error occurred. */ |
ed327204 JG |
1793 | ret = -1; |
1794 | cmd_result = LTTNG_ERR_NOMEM; | |
1795 | goto end; | |
1796 | } | |
1797 | ||
1798 | session_info->rotation.ongoing = | |
1799 | cmd_type == NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING; | |
1800 | session_info->rotation.id = trace_archive_chunk_id; | |
1801 | trigger_list = get_session_trigger_list(state, session_name); | |
1802 | if (!trigger_list) { | |
1803 | DBG("[notification-thread] No triggers applying to session \"%s\" found", | |
1804 | session_name); | |
1805 | goto end; | |
1806 | } | |
1807 | ||
1808 | cds_list_for_each_entry(trigger_list_element, &trigger_list->list, | |
1809 | node) { | |
1810 | const struct lttng_condition *condition; | |
f2b3ef9f | 1811 | struct lttng_trigger *trigger; |
ed327204 JG |
1812 | struct notification_client_list *client_list; |
1813 | struct lttng_evaluation *evaluation = NULL; | |
1814 | enum lttng_condition_type condition_type; | |
f2b3ef9f | 1815 | enum action_executor_status executor_status; |
ed327204 JG |
1816 | |
1817 | trigger = trigger_list_element->trigger; | |
1818 | condition = lttng_trigger_get_const_condition(trigger); | |
1819 | assert(condition); | |
1820 | condition_type = lttng_condition_get_type(condition); | |
1821 | ||
1822 | if (condition_type == LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING && | |
1823 | cmd_type != NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING) { | |
1824 | continue; | |
1825 | } else if (condition_type == LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED && | |
1826 | cmd_type != NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED) { | |
1827 | continue; | |
1828 | } | |
1829 | ||
ed327204 | 1830 | client_list = get_client_list_from_condition(state, condition); |
ed327204 JG |
1831 | if (cmd_type == NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING) { |
1832 | evaluation = lttng_evaluation_session_rotation_ongoing_create( | |
1833 | trace_archive_chunk_id); | |
1834 | } else { | |
1835 | evaluation = lttng_evaluation_session_rotation_completed_create( | |
1836 | trace_archive_chunk_id, location); | |
1837 | } | |
1838 | ||
1839 | if (!evaluation) { | |
1840 | /* Internal error */ | |
1841 | ret = -1; | |
1842 | cmd_result = LTTNG_ERR_UNK; | |
505b2d90 | 1843 | goto put_list; |
ed327204 JG |
1844 | } |
1845 | ||
f2b3ef9f JG |
1846 | /* |
1847 | * Ownership of `evaluation` transferred to the action executor | |
1848 | * no matter the result. | |
1849 | */ | |
1850 | executor_status = action_executor_enqueue(state->executor, | |
1851 | trigger, evaluation, &session_creds, | |
1852 | client_list); | |
1853 | evaluation = NULL; | |
1854 | switch (executor_status) { | |
1855 | case ACTION_EXECUTOR_STATUS_OK: | |
1856 | break; | |
1857 | case ACTION_EXECUTOR_STATUS_ERROR: | |
1858 | case ACTION_EXECUTOR_STATUS_INVALID: | |
1859 | /* | |
1860 | * TODO Add trigger identification (name/id) when | |
1861 | * it is added to the API. | |
1862 | */ | |
1863 | ERR("Fatal error occurred while enqueuing action associated with session rotation trigger"); | |
1864 | ret = -1; | |
1865 | goto put_list; | |
1866 | case ACTION_EXECUTOR_STATUS_OVERFLOW: | |
1867 | /* | |
1868 | * TODO Add trigger identification (name/id) when | |
1869 | * it is added to the API. | |
1870 | * | |
1871 | * Not a fatal error. | |
1872 | */ | |
1873 | WARN("No space left when enqueuing action associated with session rotation trigger"); | |
1874 | ret = 0; | |
1875 | goto put_list; | |
1876 | default: | |
1877 | abort(); | |
1878 | } | |
1879 | ||
505b2d90 JG |
1880 | put_list: |
1881 | notification_client_list_put(client_list); | |
ed327204 | 1882 | if (caa_unlikely(ret)) { |
505b2d90 | 1883 | break; |
ed327204 JG |
1884 | } |
1885 | } | |
1886 | end: | |
1887 | session_info_put(session_info); | |
1888 | *_cmd_result = cmd_result; | |
1889 | rcu_read_unlock(); | |
1890 | return ret; | |
0ca52944 JG |
1891 | } |
1892 | ||
fbc9f37d JR |
1893 | static int handle_notification_thread_command_list_triggers( |
1894 | struct notification_thread_handle *handle, | |
1895 | struct notification_thread_state *state, | |
1896 | uid_t client_uid, | |
1897 | struct lttng_triggers **triggers, | |
1898 | enum lttng_error_code *_cmd_result) | |
1899 | { | |
1900 | int ret = 0; | |
1901 | enum lttng_error_code cmd_result = LTTNG_OK; | |
1902 | struct cds_lfht_iter iter; | |
1903 | struct lttng_trigger_ht_element *trigger_ht_element; | |
1904 | struct lttng_triggers *local_triggers = NULL; | |
1905 | const struct lttng_credentials *creds; | |
1906 | ||
1907 | rcu_read_lock(); | |
1908 | ||
1909 | local_triggers = lttng_triggers_create(); | |
1910 | if (!local_triggers) { | |
1911 | /* Not a fatal error. */ | |
1912 | cmd_result = LTTNG_ERR_NOMEM; | |
1913 | goto end; | |
1914 | } | |
1915 | ||
1916 | cds_lfht_for_each_entry(state->triggers_ht, &iter, | |
1917 | trigger_ht_element, node) { | |
1918 | /* | |
1919 | * Only return the triggers to which the client has access. | |
1920 | * The root user has visibility over all triggers. | |
1921 | */ | |
1922 | creds = lttng_trigger_get_credentials(trigger_ht_element->trigger); | |
1923 | if (client_uid != lttng_credentials_get_uid(creds) && client_uid != 0) { | |
1924 | continue; | |
1925 | } | |
1926 | ||
1927 | ret = lttng_triggers_add(local_triggers, | |
1928 | trigger_ht_element->trigger); | |
1929 | if (ret < 0) { | |
1930 | /* Not a fatal error. */ | |
1931 | ret = 0; | |
1932 | cmd_result = LTTNG_ERR_NOMEM; | |
1933 | goto end; | |
1934 | } | |
1935 | } | |
1936 | ||
1937 | /* Transferring ownership to the caller. */ | |
1938 | *triggers = local_triggers; | |
1939 | local_triggers = NULL; | |
1940 | ||
1941 | end: | |
1942 | rcu_read_unlock(); | |
1943 | lttng_triggers_destroy(local_triggers); | |
1944 | *_cmd_result = cmd_result; | |
1945 | return ret; | |
1946 | } | |
1947 | ||
1da26331 | 1948 | static |
959e3c66 | 1949 | bool condition_is_supported(struct lttng_condition *condition) |
1da26331 | 1950 | { |
959e3c66 | 1951 | bool is_supported; |
1da26331 JG |
1952 | |
1953 | switch (lttng_condition_get_type(condition)) { | |
1954 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
1955 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
1956 | { | |
959e3c66 | 1957 | int ret; |
1da26331 JG |
1958 | enum lttng_domain_type domain; |
1959 | ||
1960 | ret = lttng_condition_buffer_usage_get_domain_type(condition, | |
1961 | &domain); | |
959e3c66 | 1962 | assert(ret == 0); |
1da26331 JG |
1963 | |
1964 | if (domain != LTTNG_DOMAIN_KERNEL) { | |
959e3c66 | 1965 | is_supported = true; |
1da26331 JG |
1966 | goto end; |
1967 | } | |
1968 | ||
1969 | /* | |
1970 | * Older kernel tracers don't expose the API to monitor their | |
1971 | * buffers. Therefore, we reject triggers that require that | |
1972 | * mechanism to be available to be evaluated. | |
959e3c66 JR |
1973 | * |
1974 | * Assume unsupported on error. | |
1da26331 | 1975 | */ |
959e3c66 JR |
1976 | is_supported = kernel_supports_ring_buffer_snapshot_sample_positions() == 1; |
1977 | break; | |
1978 | } | |
1979 | case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT: | |
1980 | { | |
1981 | const struct lttng_event_rule *event_rule; | |
1982 | enum lttng_domain_type domain; | |
1983 | const enum lttng_condition_status status = | |
1984 | lttng_condition_event_rule_get_rule( | |
1985 | condition, &event_rule); | |
1986 | ||
1987 | assert(status == LTTNG_CONDITION_STATUS_OK); | |
1988 | ||
1989 | domain = lttng_event_rule_get_domain_type(event_rule); | |
1990 | if (domain != LTTNG_DOMAIN_KERNEL) { | |
1991 | is_supported = true; | |
1992 | goto end; | |
1993 | } | |
1994 | ||
1995 | /* | |
1996 | * Older kernel tracers can't emit notification. Therefore, we | |
1997 | * reject triggers that require that mechanism to be available | |
1998 | * to be evaluated. | |
1999 | * | |
2000 | * Assume unsupported on error. | |
2001 | */ | |
2002 | is_supported = kernel_supports_event_notifiers() == 1; | |
1da26331 JG |
2003 | break; |
2004 | } | |
2005 | default: | |
959e3c66 | 2006 | is_supported = true; |
1da26331 JG |
2007 | } |
2008 | end: | |
959e3c66 | 2009 | return is_supported; |
1da26331 JG |
2010 | } |
2011 | ||
51eab943 JG |
2012 | /* Must be called with RCU read lock held. */ |
2013 | static | |
f2b3ef9f | 2014 | int bind_trigger_to_matching_session(struct lttng_trigger *trigger, |
51eab943 JG |
2015 | struct notification_thread_state *state) |
2016 | { | |
2017 | int ret = 0; | |
51eab943 JG |
2018 | const struct lttng_condition *condition; |
2019 | const char *session_name; | |
2020 | struct lttng_session_trigger_list *trigger_list; | |
2021 | ||
2022 | condition = lttng_trigger_get_const_condition(trigger); | |
2023 | switch (lttng_condition_get_type(condition)) { | |
2024 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: | |
2025 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
2026 | { | |
2027 | enum lttng_condition_status status; | |
2028 | ||
2029 | status = lttng_condition_session_rotation_get_session_name( | |
2030 | condition, &session_name); | |
2031 | if (status != LTTNG_CONDITION_STATUS_OK) { | |
2032 | ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name"); | |
2033 | ret = -1; | |
2034 | goto end; | |
2035 | } | |
2036 | break; | |
2037 | } | |
2038 | default: | |
2039 | ret = -1; | |
2040 | goto end; | |
2041 | } | |
2042 | ||
ed327204 JG |
2043 | trigger_list = get_session_trigger_list(state, session_name); |
2044 | if (!trigger_list) { | |
51eab943 JG |
2045 | DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system", |
2046 | session_name); | |
2047 | goto end; | |
51eab943 | 2048 | |
ed327204 | 2049 | } |
51eab943 JG |
2050 | |
2051 | DBG("[notification-thread] Newly registered trigger bound to session \"%s\"", | |
2052 | session_name); | |
2053 | ret = lttng_session_trigger_list_add(trigger_list, trigger); | |
2054 | end: | |
2055 | return ret; | |
2056 | } | |
2057 | ||
2058 | /* Must be called with RCU read lock held. */ | |
2059 | static | |
f2b3ef9f | 2060 | int bind_trigger_to_matching_channels(struct lttng_trigger *trigger, |
51eab943 JG |
2061 | struct notification_thread_state *state) |
2062 | { | |
2063 | int ret = 0; | |
2064 | struct cds_lfht_node *node; | |
2065 | struct cds_lfht_iter iter; | |
2066 | struct channel_info *channel; | |
2067 | ||
2068 | cds_lfht_for_each_entry(state->channels_ht, &iter, channel, | |
2069 | channels_ht_node) { | |
2070 | struct lttng_trigger_list_element *trigger_list_element; | |
2071 | struct lttng_channel_trigger_list *trigger_list; | |
a5d64ae7 | 2072 | struct cds_lfht_iter lookup_iter; |
51eab943 JG |
2073 | |
2074 | if (!trigger_applies_to_channel(trigger, channel)) { | |
2075 | continue; | |
2076 | } | |
2077 | ||
2078 | cds_lfht_lookup(state->channel_triggers_ht, | |
2079 | hash_channel_key(&channel->key), | |
2080 | match_channel_trigger_list, | |
2081 | &channel->key, | |
a5d64ae7 MD |
2082 | &lookup_iter); |
2083 | node = cds_lfht_iter_get_node(&lookup_iter); | |
51eab943 JG |
2084 | assert(node); |
2085 | trigger_list = caa_container_of(node, | |
2086 | struct lttng_channel_trigger_list, | |
2087 | channel_triggers_ht_node); | |
2088 | ||
2089 | trigger_list_element = zmalloc(sizeof(*trigger_list_element)); | |
2090 | if (!trigger_list_element) { | |
2091 | ret = -1; | |
2092 | goto end; | |
2093 | } | |
2094 | CDS_INIT_LIST_HEAD(&trigger_list_element->node); | |
2095 | trigger_list_element->trigger = trigger; | |
2096 | cds_list_add(&trigger_list_element->node, &trigger_list->list); | |
2097 | DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"", | |
2098 | channel->name); | |
2099 | } | |
2100 | end: | |
2101 | return ret; | |
2102 | } | |
2103 | ||
f2b3ef9f JG |
2104 | static |
2105 | bool is_trigger_action_notify(const struct lttng_trigger *trigger) | |
2106 | { | |
2107 | bool is_notify = false; | |
2108 | unsigned int i, count; | |
2109 | enum lttng_action_status action_status; | |
2110 | const struct lttng_action *action = | |
2111 | lttng_trigger_get_const_action(trigger); | |
2112 | enum lttng_action_type action_type; | |
2113 | ||
2114 | assert(action); | |
17182cfd | 2115 | action_type = lttng_action_get_type(action); |
f2b3ef9f JG |
2116 | if (action_type == LTTNG_ACTION_TYPE_NOTIFY) { |
2117 | is_notify = true; | |
2118 | goto end; | |
2119 | } else if (action_type != LTTNG_ACTION_TYPE_GROUP) { | |
2120 | goto end; | |
2121 | } | |
2122 | ||
2123 | action_status = lttng_action_group_get_count(action, &count); | |
2124 | assert(action_status == LTTNG_ACTION_STATUS_OK); | |
2125 | ||
2126 | for (i = 0; i < count; i++) { | |
2127 | const struct lttng_action *inner_action = | |
2128 | lttng_action_group_get_at_index( | |
2129 | action, i); | |
2130 | ||
17182cfd | 2131 | action_type = lttng_action_get_type(inner_action); |
f2b3ef9f JG |
2132 | if (action_type == LTTNG_ACTION_TYPE_NOTIFY) { |
2133 | is_notify = true; | |
2134 | goto end; | |
2135 | } | |
2136 | } | |
2137 | ||
2138 | end: | |
2139 | return is_notify; | |
2140 | } | |
2141 | ||
242388e4 JR |
2142 | static bool trigger_name_taken(struct notification_thread_state *state, |
2143 | const struct lttng_trigger *trigger) | |
2144 | { | |
2145 | struct cds_lfht_iter iter; | |
2146 | ||
2147 | /* | |
2148 | * No duplicata is allowed in the triggers_by_name_uid_ht. | |
2149 | * The match is done against the trigger name and uid. | |
2150 | */ | |
2151 | cds_lfht_lookup(state->triggers_by_name_uid_ht, | |
2152 | hash_trigger_by_name_uid(trigger), | |
2153 | match_trigger_by_name_uid, | |
2154 | trigger, | |
2155 | &iter); | |
2156 | return !!cds_lfht_iter_get_node(&iter); | |
2157 | } | |
2158 | ||
2159 | static | |
2160 | enum lttng_error_code generate_trigger_name( | |
2161 | struct notification_thread_state *state, | |
2162 | struct lttng_trigger *trigger, const char **name) | |
2163 | { | |
2164 | enum lttng_error_code ret_code = LTTNG_OK; | |
2165 | bool taken = false; | |
2166 | enum lttng_trigger_status status; | |
2167 | ||
2168 | do { | |
2169 | const int ret = lttng_trigger_generate_name(trigger, | |
2170 | state->trigger_id.name_offset++); | |
2171 | if (ret) { | |
2172 | /* The only reason this can fail right now. */ | |
2173 | ret_code = LTTNG_ERR_NOMEM; | |
2174 | break; | |
2175 | } | |
2176 | ||
2177 | status = lttng_trigger_get_name(trigger, name); | |
2178 | assert(status == LTTNG_TRIGGER_STATUS_OK); | |
2179 | ||
2180 | taken = trigger_name_taken(state, trigger); | |
2181 | } while (taken || state->trigger_id.name_offset == UINT64_MAX); | |
2182 | ||
2183 | return ret_code; | |
2184 | } | |
2185 | ||
ab0ee2ca | 2186 | /* |
f2b3ef9f | 2187 | * FIXME A client's credentials are not checked when registering a trigger. |
ab0ee2ca | 2188 | * |
1da26331 | 2189 | * The effects of this are benign since: |
ab0ee2ca | 2190 | * - The client will succeed in registering the trigger, as it is valid, |
51eab943 | 2191 | * - The trigger will, internally, be bound to the channel/session, |
ab0ee2ca JG |
2192 | * - The notifications will not be sent since the client's credentials |
2193 | * are checked against the channel at that moment. | |
1da26331 JG |
2194 | * |
2195 | * If this function returns a non-zero value, it means something is | |
50ca7858 | 2196 | * fundamentally broken and the whole subsystem/thread will be torn down. |
1da26331 JG |
2197 | * |
2198 | * If a non-fatal error occurs, just set the cmd_result to the appropriate | |
2199 | * error code. | |
ab0ee2ca JG |
2200 | */ |
2201 | static | |
2202 | int handle_notification_thread_command_register_trigger( | |
8abe313a JG |
2203 | struct notification_thread_state *state, |
2204 | struct lttng_trigger *trigger, | |
2205 | enum lttng_error_code *cmd_result) | |
ab0ee2ca JG |
2206 | { |
2207 | int ret = 0; | |
2208 | struct lttng_condition *condition; | |
2209 | struct notification_client *client; | |
2210 | struct notification_client_list *client_list = NULL; | |
2211 | struct lttng_trigger_ht_element *trigger_ht_element = NULL; | |
f2b3ef9f | 2212 | struct notification_client_list_element *client_list_element; |
ab0ee2ca JG |
2213 | struct cds_lfht_node *node; |
2214 | struct cds_lfht_iter iter; | |
242388e4 | 2215 | const char* trigger_name; |
ab0ee2ca | 2216 | bool free_trigger = true; |
f2b3ef9f JG |
2217 | struct lttng_evaluation *evaluation = NULL; |
2218 | struct lttng_credentials object_creds; | |
ff588497 JR |
2219 | uid_t object_uid; |
2220 | gid_t object_gid; | |
f2b3ef9f | 2221 | enum action_executor_status executor_status; |
e6887944 JR |
2222 | const uint64_t trigger_tracer_token = |
2223 | state->trigger_id.next_tracer_token++; | |
ab0ee2ca JG |
2224 | |
2225 | rcu_read_lock(); | |
2226 | ||
e6887944 JR |
2227 | /* Set the trigger's tracer token. */ |
2228 | lttng_trigger_set_tracer_token(trigger, trigger_tracer_token); | |
2229 | ||
242388e4 JR |
2230 | if (lttng_trigger_get_name(trigger, &trigger_name) == |
2231 | LTTNG_TRIGGER_STATUS_UNSET) { | |
2232 | const enum lttng_error_code ret_code = generate_trigger_name( | |
2233 | state, trigger, &trigger_name); | |
2234 | ||
2235 | if (ret_code != LTTNG_OK) { | |
2236 | /* Fatal error. */ | |
2237 | ret = -1; | |
2238 | *cmd_result = ret_code; | |
2239 | goto error; | |
2240 | } | |
2241 | } else if (trigger_name_taken(state, trigger)) { | |
2242 | /* Not a fatal error. */ | |
2243 | *cmd_result = LTTNG_ERR_TRIGGER_EXISTS; | |
2244 | ret = 0; | |
2245 | goto error; | |
2246 | } | |
2247 | ||
ab0ee2ca | 2248 | condition = lttng_trigger_get_condition(trigger); |
1da26331 JG |
2249 | assert(condition); |
2250 | ||
959e3c66 JR |
2251 | /* Some conditions require tracers to implement a minimal ABI version. */ |
2252 | if (!condition_is_supported(condition)) { | |
1da26331 JG |
2253 | *cmd_result = LTTNG_ERR_NOT_SUPPORTED; |
2254 | goto error; | |
1da26331 JG |
2255 | } |
2256 | ||
ab0ee2ca JG |
2257 | trigger_ht_element = zmalloc(sizeof(*trigger_ht_element)); |
2258 | if (!trigger_ht_element) { | |
2259 | ret = -1; | |
2260 | goto error; | |
2261 | } | |
2262 | ||
2263 | /* Add trigger to the trigger_ht. */ | |
2264 | cds_lfht_node_init(&trigger_ht_element->node); | |
242388e4 | 2265 | cds_lfht_node_init(&trigger_ht_element->node_by_name_uid); |
ab0ee2ca JG |
2266 | trigger_ht_element->trigger = trigger; |
2267 | ||
2268 | node = cds_lfht_add_unique(state->triggers_ht, | |
2269 | lttng_condition_hash(condition), | |
242388e4 JR |
2270 | match_trigger, |
2271 | trigger, | |
ab0ee2ca JG |
2272 | &trigger_ht_element->node); |
2273 | if (node != &trigger_ht_element->node) { | |
2274 | /* Not a fatal error, simply report it to the client. */ | |
2275 | *cmd_result = LTTNG_ERR_TRIGGER_EXISTS; | |
2276 | goto error_free_ht_element; | |
2277 | } | |
2278 | ||
242388e4 JR |
2279 | node = cds_lfht_add_unique(state->triggers_by_name_uid_ht, |
2280 | hash_trigger_by_name_uid(trigger), | |
2281 | match_trigger_by_name_uid, | |
2282 | trigger, | |
2283 | &trigger_ht_element->node_by_name_uid); | |
2284 | if (node != &trigger_ht_element->node_by_name_uid) { | |
2285 | /* Not a fatal error, simply report it to the client. */ | |
2286 | cds_lfht_del(state->triggers_ht, &trigger_ht_element->node); | |
2287 | *cmd_result = LTTNG_ERR_TRIGGER_EXISTS; | |
2288 | goto error_free_ht_element; | |
2289 | } | |
2290 | ||
ab0ee2ca JG |
2291 | /* |
2292 | * Ownership of the trigger and of its wrapper was transfered to | |
2293 | * the triggers_ht. | |
2294 | */ | |
2295 | trigger_ht_element = NULL; | |
2296 | free_trigger = false; | |
2297 | ||
2298 | /* | |
2299 | * The rest only applies to triggers that have a "notify" action. | |
2300 | * It is not skipped as this is the only action type currently | |
2301 | * supported. | |
2302 | */ | |
f2b3ef9f JG |
2303 | if (is_trigger_action_notify(trigger)) { |
2304 | client_list = notification_client_list_create(trigger); | |
2305 | if (!client_list) { | |
2306 | ret = -1; | |
2307 | goto error_free_ht_element; | |
ab0ee2ca JG |
2308 | } |
2309 | ||
f2b3ef9f JG |
2310 | /* Build a list of clients to which this new trigger applies. */ |
2311 | cds_lfht_for_each_entry (state->client_socket_ht, &iter, client, | |
2312 | client_socket_ht_node) { | |
2313 | if (!trigger_applies_to_client(trigger, client)) { | |
2314 | continue; | |
2315 | } | |
2316 | ||
2317 | client_list_element = | |
2318 | zmalloc(sizeof(*client_list_element)); | |
2319 | if (!client_list_element) { | |
2320 | ret = -1; | |
2321 | goto error_put_client_list; | |
2322 | } | |
2323 | ||
2324 | CDS_INIT_LIST_HEAD(&client_list_element->node); | |
2325 | client_list_element->client = client; | |
2326 | cds_list_add(&client_list_element->node, | |
2327 | &client_list->list); | |
ab0ee2ca | 2328 | } |
f2b3ef9f JG |
2329 | |
2330 | /* | |
2331 | * Client list ownership transferred to the | |
2332 | * notification_trigger_clients_ht. | |
2333 | */ | |
2334 | publish_notification_client_list(state, client_list); | |
ab0ee2ca JG |
2335 | } |
2336 | ||
f82f93a1 | 2337 | switch (get_condition_binding_object(condition)) { |
51eab943 JG |
2338 | case LTTNG_OBJECT_TYPE_SESSION: |
2339 | /* Add the trigger to the list if it matches a known session. */ | |
2340 | ret = bind_trigger_to_matching_session(trigger, state); | |
2341 | if (ret) { | |
505b2d90 | 2342 | goto error_put_client_list; |
ab0ee2ca | 2343 | } |
f82f93a1 | 2344 | break; |
51eab943 JG |
2345 | case LTTNG_OBJECT_TYPE_CHANNEL: |
2346 | /* | |
2347 | * Add the trigger to list of triggers bound to the channels | |
2348 | * currently known. | |
2349 | */ | |
2350 | ret = bind_trigger_to_matching_channels(trigger, state); | |
2351 | if (ret) { | |
505b2d90 | 2352 | goto error_put_client_list; |
ab0ee2ca | 2353 | } |
51eab943 JG |
2354 | break; |
2355 | case LTTNG_OBJECT_TYPE_NONE: | |
2356 | break; | |
2357 | default: | |
f2b3ef9f | 2358 | ERR("Unknown object type on which to bind a newly registered trigger was encountered"); |
51eab943 | 2359 | ret = -1; |
505b2d90 | 2360 | goto error_put_client_list; |
ab0ee2ca JG |
2361 | } |
2362 | ||
2ae99f0b | 2363 | /* |
f2b3ef9f JG |
2364 | * The new trigger's condition must be evaluated against the current |
2365 | * state. | |
2366 | * | |
2367 | * In the case of `notify` action, nothing preventing clients from | |
2368 | * subscribing to a condition before the corresponding trigger is | |
2369 | * registered, we have to evaluate this new condition right away. | |
2ae99f0b JG |
2370 | * |
2371 | * At some point, we were waiting for the next "evaluation" (e.g. on | |
2372 | * reception of a channel sample) to evaluate this new condition, but | |
2373 | * that was broken. | |
2374 | * | |
2375 | * The reason it was broken is that waiting for the next sample | |
2376 | * does not allow us to properly handle transitions for edge-triggered | |
2377 | * conditions. | |
2378 | * | |
2379 | * Consider this example: when we handle a new channel sample, we | |
2380 | * evaluate each conditions twice: once with the previous state, and | |
2381 | * again with the newest state. We then use those two results to | |
2382 | * determine whether a state change happened: a condition was false and | |
2383 | * became true. If a state change happened, we have to notify clients. | |
2384 | * | |
2385 | * Now, if a client subscribes to a given notification and registers | |
2386 | * a trigger *after* that subscription, we have to make sure the | |
2387 | * condition is evaluated at this point while considering only the | |
2388 | * current state. Otherwise, the next evaluation cycle may only see | |
2389 | * that the evaluations remain the same (true for samples n-1 and n) and | |
2390 | * the client will never know that the condition has been met. | |
2391 | */ | |
f2b3ef9f JG |
2392 | switch (get_condition_binding_object(condition)) { |
2393 | case LTTNG_OBJECT_TYPE_SESSION: | |
2394 | ret = evaluate_session_condition_for_client(condition, state, | |
ff588497 JR |
2395 | &evaluation, &object_uid, |
2396 | &object_gid); | |
f2b3ef9f JG |
2397 | break; |
2398 | case LTTNG_OBJECT_TYPE_CHANNEL: | |
2399 | ret = evaluate_channel_condition_for_client(condition, state, | |
ff588497 JR |
2400 | &evaluation, &object_uid, |
2401 | &object_gid); | |
f2b3ef9f JG |
2402 | break; |
2403 | case LTTNG_OBJECT_TYPE_NONE: | |
2404 | ret = 0; | |
242388e4 | 2405 | break; |
f2b3ef9f JG |
2406 | case LTTNG_OBJECT_TYPE_UNKNOWN: |
2407 | default: | |
2408 | ret = -1; | |
242388e4 | 2409 | break; |
f2b3ef9f JG |
2410 | } |
2411 | ||
2412 | if (ret) { | |
2413 | /* Fatal error. */ | |
2414 | goto error_put_client_list; | |
2415 | } | |
2416 | ||
ff588497 JR |
2417 | LTTNG_OPTIONAL_SET(&object_creds.uid, object_uid); |
2418 | LTTNG_OPTIONAL_SET(&object_creds.gid, object_gid); | |
2419 | ||
f2b3ef9f JG |
2420 | DBG("Newly registered trigger's condition evaluated to %s", |
2421 | evaluation ? "true" : "false"); | |
2422 | if (!evaluation) { | |
2423 | /* Evaluation yielded nothing. Normal exit. */ | |
2424 | ret = 0; | |
242388e4 | 2425 | goto end; |
2ae99f0b JG |
2426 | } |
2427 | ||
2428 | /* | |
f2b3ef9f JG |
2429 | * Ownership of `evaluation` transferred to the action executor |
2430 | * no matter the result. | |
2ae99f0b | 2431 | */ |
f2b3ef9f JG |
2432 | executor_status = action_executor_enqueue(state->executor, trigger, |
2433 | evaluation, &object_creds, client_list); | |
2434 | evaluation = NULL; | |
2435 | switch (executor_status) { | |
2436 | case ACTION_EXECUTOR_STATUS_OK: | |
2437 | break; | |
2438 | case ACTION_EXECUTOR_STATUS_ERROR: | |
2439 | case ACTION_EXECUTOR_STATUS_INVALID: | |
2440 | /* | |
2441 | * TODO Add trigger identification (name/id) when | |
2442 | * it is added to the API. | |
2443 | */ | |
2444 | ERR("Fatal error occurred while enqueuing action associated to newly registered trigger"); | |
2445 | ret = -1; | |
2446 | goto error_put_client_list; | |
2447 | case ACTION_EXECUTOR_STATUS_OVERFLOW: | |
2448 | /* | |
2449 | * TODO Add trigger identification (name/id) when | |
2450 | * it is added to the API. | |
2451 | * | |
2452 | * Not a fatal error. | |
2453 | */ | |
2454 | WARN("No space left when enqueuing action associated to newly registered trigger"); | |
2455 | ret = 0; | |
242388e4 | 2456 | goto end; |
f2b3ef9f JG |
2457 | default: |
2458 | abort(); | |
2459 | } | |
2ae99f0b | 2460 | |
242388e4 | 2461 | end: |
ab0ee2ca | 2462 | *cmd_result = LTTNG_OK; |
e6887944 JR |
2463 | DBG("Registered trigger: name = `%s`, tracer token = %" PRIu64, |
2464 | trigger_name, trigger_tracer_token); | |
505b2d90 JG |
2465 | |
2466 | error_put_client_list: | |
2467 | notification_client_list_put(client_list); | |
2468 | ||
ab0ee2ca | 2469 | error_free_ht_element: |
242388e4 JR |
2470 | if (trigger_ht_element) { |
2471 | /* Delayed removal due to RCU constraint on delete. */ | |
2472 | call_rcu(&trigger_ht_element->rcu_node, | |
2473 | free_lttng_trigger_ht_element_rcu); | |
2474 | } | |
2475 | ||
ab0ee2ca JG |
2476 | error: |
2477 | if (free_trigger) { | |
ab0ee2ca JG |
2478 | lttng_trigger_destroy(trigger); |
2479 | } | |
2480 | rcu_read_unlock(); | |
2481 | return ret; | |
2482 | } | |
2483 | ||
83b934ad MD |
2484 | static |
2485 | void free_lttng_trigger_ht_element_rcu(struct rcu_head *node) | |
2486 | { | |
2487 | free(caa_container_of(node, struct lttng_trigger_ht_element, | |
2488 | rcu_node)); | |
2489 | } | |
2490 | ||
cc2295b5 | 2491 | static |
ab0ee2ca JG |
2492 | int handle_notification_thread_command_unregister_trigger( |
2493 | struct notification_thread_state *state, | |
2494 | struct lttng_trigger *trigger, | |
2495 | enum lttng_error_code *_cmd_reply) | |
2496 | { | |
2497 | struct cds_lfht_iter iter; | |
ed327204 | 2498 | struct cds_lfht_node *triggers_ht_node; |
ab0ee2ca JG |
2499 | struct lttng_channel_trigger_list *trigger_list; |
2500 | struct notification_client_list *client_list; | |
ab0ee2ca JG |
2501 | struct lttng_trigger_ht_element *trigger_ht_element = NULL; |
2502 | struct lttng_condition *condition = lttng_trigger_get_condition( | |
2503 | trigger); | |
ab0ee2ca JG |
2504 | enum lttng_error_code cmd_reply; |
2505 | ||
2506 | rcu_read_lock(); | |
2507 | ||
2508 | cds_lfht_lookup(state->triggers_ht, | |
2509 | lttng_condition_hash(condition), | |
242388e4 JR |
2510 | match_trigger, |
2511 | trigger, | |
ab0ee2ca JG |
2512 | &iter); |
2513 | triggers_ht_node = cds_lfht_iter_get_node(&iter); | |
2514 | if (!triggers_ht_node) { | |
2515 | cmd_reply = LTTNG_ERR_TRIGGER_NOT_FOUND; | |
2516 | goto end; | |
2517 | } else { | |
2518 | cmd_reply = LTTNG_OK; | |
2519 | } | |
2520 | ||
2521 | /* Remove trigger from channel_triggers_ht. */ | |
2522 | cds_lfht_for_each_entry(state->channel_triggers_ht, &iter, trigger_list, | |
2523 | channel_triggers_ht_node) { | |
2524 | struct lttng_trigger_list_element *trigger_element, *tmp; | |
2525 | ||
2526 | cds_list_for_each_entry_safe(trigger_element, tmp, | |
2527 | &trigger_list->list, node) { | |
e8f7fa11 | 2528 | if (!lttng_trigger_is_equal(trigger, trigger_element->trigger)) { |
ab0ee2ca JG |
2529 | continue; |
2530 | } | |
2531 | ||
2532 | DBG("[notification-thread] Removed trigger from channel_triggers_ht"); | |
2533 | cds_list_del(&trigger_element->node); | |
e4db5ace JR |
2534 | /* A trigger can only appear once per channel */ |
2535 | break; | |
ab0ee2ca JG |
2536 | } |
2537 | } | |
2538 | ||
51367634 JR |
2539 | if (is_trigger_action_notify(trigger)) { |
2540 | /* | |
2541 | * Remove and release the client list from | |
2542 | * notification_trigger_clients_ht. | |
2543 | */ | |
2544 | client_list = get_client_list_from_condition(state, condition); | |
2545 | assert(client_list); | |
ed327204 | 2546 | |
51367634 JR |
2547 | /* Put new reference and the hashtable's reference. */ |
2548 | notification_client_list_put(client_list); | |
2549 | notification_client_list_put(client_list); | |
2550 | client_list = NULL; | |
2551 | } | |
ab0ee2ca JG |
2552 | |
2553 | /* Remove trigger from triggers_ht. */ | |
2554 | trigger_ht_element = caa_container_of(triggers_ht_node, | |
2555 | struct lttng_trigger_ht_element, node); | |
242388e4 | 2556 | cds_lfht_del(state->triggers_by_name_uid_ht, &trigger_ht_element->node_by_name_uid); |
ab0ee2ca JG |
2557 | cds_lfht_del(state->triggers_ht, triggers_ht_node); |
2558 | ||
7ca172c1 | 2559 | /* Release the ownership of the trigger. */ |
ab0ee2ca | 2560 | lttng_trigger_destroy(trigger_ht_element->trigger); |
83b934ad | 2561 | call_rcu(&trigger_ht_element->rcu_node, free_lttng_trigger_ht_element_rcu); |
ab0ee2ca JG |
2562 | end: |
2563 | rcu_read_unlock(); | |
2564 | if (_cmd_reply) { | |
2565 | *_cmd_reply = cmd_reply; | |
2566 | } | |
2567 | return 0; | |
2568 | } | |
2569 | ||
2570 | /* Returns 0 on success, 1 on exit requested, negative value on error. */ | |
2571 | int handle_notification_thread_command( | |
2572 | struct notification_thread_handle *handle, | |
2573 | struct notification_thread_state *state) | |
2574 | { | |
2575 | int ret; | |
2576 | uint64_t counter; | |
2577 | struct notification_thread_command *cmd; | |
2578 | ||
8abe313a | 2579 | /* Read the event pipe to put it back into a quiescent state. */ |
18aeca05 | 2580 | ret = lttng_read(lttng_pipe_get_readfd(handle->cmd_queue.event_pipe), &counter, |
8abe313a | 2581 | sizeof(counter)); |
18aeca05 | 2582 | if (ret != sizeof(counter)) { |
ab0ee2ca JG |
2583 | goto error; |
2584 | } | |
2585 | ||
2586 | pthread_mutex_lock(&handle->cmd_queue.lock); | |
2587 | cmd = cds_list_first_entry(&handle->cmd_queue.list, | |
2588 | struct notification_thread_command, cmd_list_node); | |
97a71ea6 JR |
2589 | cds_list_del(&cmd->cmd_list_node); |
2590 | pthread_mutex_unlock(&handle->cmd_queue.lock); | |
ab0ee2ca JG |
2591 | switch (cmd->type) { |
2592 | case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER: | |
2593 | DBG("[notification-thread] Received register trigger command"); | |
2594 | ret = handle_notification_thread_command_register_trigger( | |
2595 | state, cmd->parameters.trigger, | |
2596 | &cmd->reply_code); | |
2597 | break; | |
2598 | case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER: | |
2599 | DBG("[notification-thread] Received unregister trigger command"); | |
2600 | ret = handle_notification_thread_command_unregister_trigger( | |
2601 | state, cmd->parameters.trigger, | |
2602 | &cmd->reply_code); | |
2603 | break; | |
2604 | case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL: | |
2605 | DBG("[notification-thread] Received add channel command"); | |
2606 | ret = handle_notification_thread_command_add_channel( | |
8abe313a JG |
2607 | state, |
2608 | cmd->parameters.add_channel.session.name, | |
2609 | cmd->parameters.add_channel.session.uid, | |
2610 | cmd->parameters.add_channel.session.gid, | |
2611 | cmd->parameters.add_channel.channel.name, | |
2612 | cmd->parameters.add_channel.channel.domain, | |
2613 | cmd->parameters.add_channel.channel.key, | |
2614 | cmd->parameters.add_channel.channel.capacity, | |
ab0ee2ca JG |
2615 | &cmd->reply_code); |
2616 | break; | |
2617 | case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL: | |
2618 | DBG("[notification-thread] Received remove channel command"); | |
2619 | ret = handle_notification_thread_command_remove_channel( | |
2620 | state, cmd->parameters.remove_channel.key, | |
2621 | cmd->parameters.remove_channel.domain, | |
2622 | &cmd->reply_code); | |
2623 | break; | |
0ca52944 | 2624 | case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING: |
0ca52944 | 2625 | case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED: |
ed327204 JG |
2626 | DBG("[notification-thread] Received session rotation %s command", |
2627 | cmd->type == NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING ? | |
2628 | "ongoing" : "completed"); | |
2629 | ret = handle_notification_thread_command_session_rotation( | |
0ca52944 | 2630 | state, |
ed327204 JG |
2631 | cmd->type, |
2632 | cmd->parameters.session_rotation.session_name, | |
2633 | cmd->parameters.session_rotation.uid, | |
2634 | cmd->parameters.session_rotation.gid, | |
2635 | cmd->parameters.session_rotation.trace_archive_chunk_id, | |
2636 | cmd->parameters.session_rotation.location, | |
0ca52944 JG |
2637 | &cmd->reply_code); |
2638 | break; | |
fbc9f37d JR |
2639 | case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS: |
2640 | { | |
2641 | struct lttng_triggers *triggers = NULL; | |
2642 | ||
2643 | ret = handle_notification_thread_command_list_triggers( | |
2644 | handle, | |
2645 | state, | |
2646 | cmd->parameters.list_triggers.uid, | |
2647 | &triggers, | |
2648 | &cmd->reply_code); | |
2649 | cmd->reply.list_triggers.triggers = triggers; | |
2650 | ret = 0; | |
2651 | break; | |
2652 | } | |
ab0ee2ca JG |
2653 | case NOTIFICATION_COMMAND_TYPE_QUIT: |
2654 | DBG("[notification-thread] Received quit command"); | |
2655 | cmd->reply_code = LTTNG_OK; | |
2656 | ret = 1; | |
2657 | goto end; | |
f2b3ef9f JG |
2658 | case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE: |
2659 | { | |
2660 | const enum client_transmission_status client_status = | |
2661 | cmd->parameters.client_communication_update | |
2662 | .status; | |
2663 | const notification_client_id client_id = | |
2664 | cmd->parameters.client_communication_update.id; | |
2665 | struct notification_client *client; | |
2666 | ||
2667 | rcu_read_lock(); | |
2668 | client = get_client_from_id(client_id, state); | |
2669 | ||
2670 | if (!client) { | |
2671 | /* | |
2672 | * Client error was probably already picked-up by the | |
2673 | * notification thread or it has disconnected | |
2674 | * gracefully while this command was queued. | |
2675 | */ | |
2676 | DBG("Failed to find notification client to update communication status, client id = %" PRIu64, | |
2677 | client_id); | |
2678 | ret = 0; | |
2679 | } else { | |
f2b3ef9f JG |
2680 | ret = client_handle_transmission_status( |
2681 | client, client_status, state); | |
f2b3ef9f JG |
2682 | } |
2683 | rcu_read_unlock(); | |
2684 | break; | |
2685 | } | |
ab0ee2ca JG |
2686 | default: |
2687 | ERR("[notification-thread] Unknown internal command received"); | |
2688 | goto error_unlock; | |
2689 | } | |
2690 | ||
2691 | if (ret) { | |
2692 | goto error_unlock; | |
2693 | } | |
2694 | end: | |
0ab399e0 JG |
2695 | if (cmd->is_async) { |
2696 | free(cmd); | |
2697 | cmd = NULL; | |
2698 | } else { | |
2699 | lttng_waiter_wake_up(&cmd->reply_waiter); | |
2700 | } | |
ab0ee2ca JG |
2701 | return ret; |
2702 | error_unlock: | |
2703 | /* Wake-up and return a fatal error to the calling thread. */ | |
8ada111f | 2704 | lttng_waiter_wake_up(&cmd->reply_waiter); |
ab0ee2ca JG |
2705 | cmd->reply_code = LTTNG_ERR_FATAL; |
2706 | error: | |
2707 | /* Indicate a fatal error to the caller. */ | |
2708 | return -1; | |
2709 | } | |
2710 | ||
ab0ee2ca JG |
2711 | static |
2712 | int socket_set_non_blocking(int socket) | |
2713 | { | |
2714 | int ret, flags; | |
2715 | ||
2716 | /* Set the pipe as non-blocking. */ | |
2717 | ret = fcntl(socket, F_GETFL, 0); | |
2718 | if (ret == -1) { | |
2719 | PERROR("fcntl get socket flags"); | |
2720 | goto end; | |
2721 | } | |
2722 | flags = ret; | |
2723 | ||
2724 | ret = fcntl(socket, F_SETFL, flags | O_NONBLOCK); | |
2725 | if (ret == -1) { | |
2726 | PERROR("fcntl set O_NONBLOCK socket flag"); | |
2727 | goto end; | |
2728 | } | |
2729 | DBG("Client socket (fd = %i) set as non-blocking", socket); | |
2730 | end: | |
2731 | return ret; | |
2732 | } | |
2733 | ||
2734 | static | |
14fa22f8 | 2735 | int client_reset_inbound_state(struct notification_client *client) |
ab0ee2ca JG |
2736 | { |
2737 | int ret; | |
2738 | ||
882093ee JR |
2739 | |
2740 | lttng_payload_clear(&client->communication.inbound.payload); | |
ab0ee2ca JG |
2741 | |
2742 | client->communication.inbound.bytes_to_receive = | |
2743 | sizeof(struct lttng_notification_channel_message); | |
2744 | client->communication.inbound.msg_type = | |
2745 | LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN; | |
ab0ee2ca JG |
2746 | LTTNG_SOCK_SET_UID_CRED(&client->communication.inbound.creds, -1); |
2747 | LTTNG_SOCK_SET_GID_CRED(&client->communication.inbound.creds, -1); | |
14fa22f8 | 2748 | ret = lttng_dynamic_buffer_set_size( |
882093ee | 2749 | &client->communication.inbound.payload.buffer, |
14fa22f8 | 2750 | client->communication.inbound.bytes_to_receive); |
882093ee | 2751 | |
14fa22f8 | 2752 | return ret; |
ab0ee2ca JG |
2753 | } |
2754 | ||
2755 | int handle_notification_thread_client_connect( | |
2756 | struct notification_thread_state *state) | |
2757 | { | |
2758 | int ret; | |
2759 | struct notification_client *client; | |
2760 | ||
2761 | DBG("[notification-thread] Handling new notification channel client connection"); | |
2762 | ||
2763 | client = zmalloc(sizeof(*client)); | |
2764 | if (!client) { | |
2765 | /* Fatal error. */ | |
2766 | ret = -1; | |
2767 | goto error; | |
2768 | } | |
6c24d3fd | 2769 | |
505b2d90 | 2770 | pthread_mutex_init(&client->lock, NULL); |
ac1889bf | 2771 | client->id = state->next_notification_client_id++; |
ab0ee2ca | 2772 | CDS_INIT_LIST_HEAD(&client->condition_list); |
882093ee JR |
2773 | lttng_payload_init(&client->communication.inbound.payload); |
2774 | lttng_payload_init(&client->communication.outbound.payload); | |
01ea340e | 2775 | client->communication.inbound.expect_creds = true; |
505b2d90 | 2776 | |
14fa22f8 JG |
2777 | ret = client_reset_inbound_state(client); |
2778 | if (ret) { | |
2779 | ERR("[notification-thread] Failed to reset client communication's inbound state"); | |
e742b055 | 2780 | ret = 0; |
14fa22f8 JG |
2781 | goto error; |
2782 | } | |
ab0ee2ca JG |
2783 | |
2784 | ret = lttcomm_accept_unix_sock(state->notification_channel_socket); | |
2785 | if (ret < 0) { | |
2786 | ERR("[notification-thread] Failed to accept new notification channel client connection"); | |
2787 | ret = 0; | |
2788 | goto error; | |
2789 | } | |
2790 | ||
2791 | client->socket = ret; | |
2792 | ||
2793 | ret = socket_set_non_blocking(client->socket); | |
2794 | if (ret) { | |
2795 | ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking"); | |
2796 | goto error; | |
2797 | } | |
2798 | ||
2799 | ret = lttcomm_setsockopt_creds_unix_sock(client->socket); | |
2800 | if (ret < 0) { | |
2801 | ERR("[notification-thread] Failed to set socket options on new notification channel client socket"); | |
2802 | ret = 0; | |
2803 | goto error; | |
2804 | } | |
2805 | ||
2806 | ret = lttng_poll_add(&state->events, client->socket, | |
2807 | LPOLLIN | LPOLLERR | | |
2808 | LPOLLHUP | LPOLLRDHUP); | |
2809 | if (ret < 0) { | |
2810 | ERR("[notification-thread] Failed to add notification channel client socket to poll set"); | |
2811 | ret = 0; | |
2812 | goto error; | |
2813 | } | |
2814 | DBG("[notification-thread] Added new notification channel client socket (%i) to poll set", | |
2815 | client->socket); | |
2816 | ||
ab0ee2ca JG |
2817 | rcu_read_lock(); |
2818 | cds_lfht_add(state->client_socket_ht, | |
2819 | hash_client_socket(client->socket), | |
2820 | &client->client_socket_ht_node); | |
ac1889bf JG |
2821 | cds_lfht_add(state->client_id_ht, |
2822 | hash_client_id(client->id), | |
2823 | &client->client_id_ht_node); | |
ab0ee2ca JG |
2824 | rcu_read_unlock(); |
2825 | ||
2826 | return ret; | |
6c24d3fd | 2827 | |
ab0ee2ca JG |
2828 | error: |
2829 | notification_client_destroy(client, state); | |
2830 | return ret; | |
2831 | } | |
2832 | ||
6c24d3fd JG |
2833 | /* |
2834 | * RCU read-lock must be held by the caller. | |
2835 | * Client lock must _not_ be held by the caller. | |
2836 | */ | |
505b2d90 JG |
2837 | static |
2838 | int notification_thread_client_disconnect( | |
2839 | struct notification_client *client, | |
ab0ee2ca | 2840 | struct notification_thread_state *state) |
505b2d90 JG |
2841 | { |
2842 | int ret; | |
2843 | struct lttng_condition_list_element *condition_list_element, *tmp; | |
2844 | ||
2845 | /* Acquire the client lock to disable its communication atomically. */ | |
6c24d3fd | 2846 | pthread_mutex_lock(&client->lock); |
505b2d90 | 2847 | client->communication.active = false; |
6c24d3fd JG |
2848 | cds_lfht_del(state->client_socket_ht, &client->client_socket_ht_node); |
2849 | cds_lfht_del(state->client_id_ht, &client->client_id_ht_node); | |
2850 | pthread_mutex_unlock(&client->lock); | |
2851 | ||
505b2d90 JG |
2852 | ret = lttng_poll_del(&state->events, client->socket); |
2853 | if (ret) { | |
2854 | ERR("[notification-thread] Failed to remove client socket %d from poll set", | |
2855 | client->socket); | |
2856 | } | |
2857 | ||
505b2d90 JG |
2858 | /* Release all conditions to which the client was subscribed. */ |
2859 | cds_list_for_each_entry_safe(condition_list_element, tmp, | |
2860 | &client->condition_list, node) { | |
2861 | (void) notification_thread_client_unsubscribe(client, | |
2862 | condition_list_element->condition, state, NULL); | |
2863 | } | |
2864 | ||
2865 | /* | |
2866 | * Client no longer accessible to other threads (through the | |
2867 | * client lists). | |
2868 | */ | |
2869 | notification_client_destroy(client, state); | |
2870 | return ret; | |
2871 | } | |
2872 | ||
2873 | int handle_notification_thread_client_disconnect( | |
2874 | int client_socket, struct notification_thread_state *state) | |
ab0ee2ca JG |
2875 | { |
2876 | int ret = 0; | |
2877 | struct notification_client *client; | |
2878 | ||
2879 | rcu_read_lock(); | |
2880 | DBG("[notification-thread] Closing client connection (socket fd = %i)", | |
2881 | client_socket); | |
2882 | client = get_client_from_socket(client_socket, state); | |
2883 | if (!client) { | |
2884 | /* Internal state corruption, fatal error. */ | |
2885 | ERR("[notification-thread] Unable to find client (socket fd = %i)", | |
2886 | client_socket); | |
2887 | ret = -1; | |
2888 | goto end; | |
2889 | } | |
2890 | ||
505b2d90 | 2891 | ret = notification_thread_client_disconnect(client, state); |
ab0ee2ca JG |
2892 | end: |
2893 | rcu_read_unlock(); | |
2894 | return ret; | |
2895 | } | |
2896 | ||
2897 | int handle_notification_thread_client_disconnect_all( | |
2898 | struct notification_thread_state *state) | |
2899 | { | |
2900 | struct cds_lfht_iter iter; | |
2901 | struct notification_client *client; | |
2902 | bool error_encoutered = false; | |
2903 | ||
2904 | rcu_read_lock(); | |
2905 | DBG("[notification-thread] Closing all client connections"); | |
2906 | cds_lfht_for_each_entry(state->client_socket_ht, &iter, client, | |
505b2d90 | 2907 | client_socket_ht_node) { |
ab0ee2ca JG |
2908 | int ret; |
2909 | ||
505b2d90 JG |
2910 | ret = notification_thread_client_disconnect( |
2911 | client, state); | |
ab0ee2ca JG |
2912 | if (ret) { |
2913 | error_encoutered = true; | |
2914 | } | |
2915 | } | |
2916 | rcu_read_unlock(); | |
2917 | return error_encoutered ? 1 : 0; | |
2918 | } | |
2919 | ||
2920 | int handle_notification_thread_trigger_unregister_all( | |
2921 | struct notification_thread_state *state) | |
2922 | { | |
4149ace8 | 2923 | bool error_occurred = false; |
ab0ee2ca JG |
2924 | struct cds_lfht_iter iter; |
2925 | struct lttng_trigger_ht_element *trigger_ht_element; | |
2926 | ||
763de384 | 2927 | rcu_read_lock(); |
ab0ee2ca JG |
2928 | cds_lfht_for_each_entry(state->triggers_ht, &iter, trigger_ht_element, |
2929 | node) { | |
2930 | int ret = handle_notification_thread_command_unregister_trigger( | |
2931 | state, trigger_ht_element->trigger, NULL); | |
2932 | if (ret) { | |
4149ace8 | 2933 | error_occurred = true; |
ab0ee2ca JG |
2934 | } |
2935 | } | |
763de384 | 2936 | rcu_read_unlock(); |
4149ace8 | 2937 | return error_occurred ? -1 : 0; |
ab0ee2ca JG |
2938 | } |
2939 | ||
dd877ea6 JG |
2940 | static |
2941 | int client_handle_transmission_status( | |
2942 | struct notification_client *client, | |
2943 | enum client_transmission_status transmission_status, | |
2944 | struct notification_thread_state *state) | |
2945 | { | |
2946 | int ret = 0; | |
2947 | ||
2948 | switch (transmission_status) { | |
2949 | case CLIENT_TRANSMISSION_STATUS_COMPLETE: | |
2950 | ret = lttng_poll_mod(&state->events, client->socket, | |
2951 | CLIENT_POLL_MASK_IN); | |
2952 | if (ret) { | |
2953 | goto end; | |
2954 | } | |
2955 | ||
dd877ea6 JG |
2956 | break; |
2957 | case CLIENT_TRANSMISSION_STATUS_QUEUED: | |
2958 | /* | |
2959 | * We want to be notified whenever there is buffer space | |
2960 | * available to send the rest of the payload. | |
2961 | */ | |
2962 | ret = lttng_poll_mod(&state->events, client->socket, | |
2963 | CLIENT_POLL_MASK_IN_OUT); | |
2964 | if (ret) { | |
2965 | goto end; | |
2966 | } | |
2967 | break; | |
2968 | case CLIENT_TRANSMISSION_STATUS_FAIL: | |
2969 | ret = notification_thread_client_disconnect(client, state); | |
2970 | if (ret) { | |
2971 | goto end; | |
2972 | } | |
2973 | break; | |
2974 | case CLIENT_TRANSMISSION_STATUS_ERROR: | |
2975 | ret = -1; | |
2976 | goto end; | |
2977 | default: | |
2978 | abort(); | |
2979 | } | |
2980 | end: | |
2981 | return ret; | |
2982 | } | |
2983 | ||
505b2d90 | 2984 | /* Client lock must be acquired by caller. */ |
ab0ee2ca | 2985 | static |
dd877ea6 | 2986 | enum client_transmission_status client_flush_outgoing_queue( |
f2b3ef9f | 2987 | struct notification_client *client) |
ab0ee2ca JG |
2988 | { |
2989 | ssize_t ret; | |
2990 | size_t to_send_count; | |
dd877ea6 | 2991 | enum client_transmission_status status; |
882093ee JR |
2992 | struct lttng_payload_view pv = lttng_payload_view_from_payload( |
2993 | &client->communication.outbound.payload, 0, -1); | |
2994 | const int fds_to_send_count = | |
2995 | lttng_payload_view_get_fd_handle_count(&pv); | |
ab0ee2ca | 2996 | |
505b2d90 JG |
2997 | ASSERT_LOCKED(client->lock); |
2998 | ||
f2b3ef9f JG |
2999 | if (!client->communication.active) { |
3000 | status = CLIENT_TRANSMISSION_STATUS_FAIL; | |
3001 | goto end; | |
3002 | } | |
3003 | ||
882093ee JR |
3004 | if (pv.buffer.size == 0) { |
3005 | /* | |
3006 | * If both data and fds are equal to zero, we are in an invalid | |
3007 | * state. | |
3008 | */ | |
3009 | assert(fds_to_send_count != 0); | |
3010 | goto send_fds; | |
3011 | } | |
3012 | ||
3013 | /* Send data. */ | |
3014 | to_send_count = pv.buffer.size; | |
ab0ee2ca JG |
3015 | DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue", |
3016 | client->socket); | |
3017 | ||
3018 | ret = lttcomm_send_unix_sock_non_block(client->socket, | |
882093ee | 3019 | pv.buffer.data, |
ab0ee2ca | 3020 | to_send_count); |
44c180ca | 3021 | if ((ret >= 0 && ret < to_send_count)) { |
ab0ee2ca JG |
3022 | DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed", |
3023 | client->socket); | |
3024 | to_send_count -= max(ret, 0); | |
3025 | ||
6f110534 | 3026 | memmove(client->communication.outbound.payload.buffer.data, |
882093ee JR |
3027 | pv.buffer.data + |
3028 | pv.buffer.size - to_send_count, | |
ab0ee2ca JG |
3029 | to_send_count); |
3030 | ret = lttng_dynamic_buffer_set_size( | |
882093ee | 3031 | &client->communication.outbound.payload.buffer, |
ab0ee2ca JG |
3032 | to_send_count); |
3033 | if (ret) { | |
3034 | goto error; | |
3035 | } | |
6c24d3fd | 3036 | |
dd877ea6 | 3037 | status = CLIENT_TRANSMISSION_STATUS_QUEUED; |
882093ee | 3038 | goto end; |
ab0ee2ca | 3039 | } else if (ret < 0) { |
6c24d3fd | 3040 | /* Generic error, disable the client's communication. */ |
dd877ea6 | 3041 | ERR("[notification-thread] Failed to flush outgoing queue, disconnecting client (socket fd = %i)", |
ab0ee2ca | 3042 | client->socket); |
6c24d3fd | 3043 | client->communication.active = false; |
dd877ea6 | 3044 | status = CLIENT_TRANSMISSION_STATUS_FAIL; |
882093ee | 3045 | goto end; |
ab0ee2ca | 3046 | } else { |
882093ee JR |
3047 | /* |
3048 | * No error and flushed the queue completely. | |
3049 | * | |
3050 | * The payload buffer size is used later to | |
3051 | * check if there is notifications queued. So albeit that the | |
3052 | * direct caller knows that the transmission is complete, we | |
3053 | * need to set the buffer size to zero. | |
3054 | */ | |
ab0ee2ca | 3055 | ret = lttng_dynamic_buffer_set_size( |
882093ee | 3056 | &client->communication.outbound.payload.buffer, 0); |
ab0ee2ca JG |
3057 | if (ret) { |
3058 | goto error; | |
3059 | } | |
882093ee | 3060 | } |
6c24d3fd | 3061 | |
882093ee JR |
3062 | send_fds: |
3063 | /* No fds to send, transmission is complete. */ | |
3064 | if (fds_to_send_count == 0) { | |
dd877ea6 | 3065 | status = CLIENT_TRANSMISSION_STATUS_COMPLETE; |
882093ee | 3066 | goto end; |
dd877ea6 | 3067 | } |
882093ee JR |
3068 | |
3069 | ret = lttcomm_send_payload_view_fds_unix_sock_non_block( | |
3070 | client->socket, &pv); | |
3071 | if (ret < 0) { | |
3072 | /* Generic error, disable the client's communication. */ | |
3073 | ERR("[notification-thread] Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)", | |
3074 | client->socket); | |
3075 | client->communication.active = false; | |
3076 | status = CLIENT_TRANSMISSION_STATUS_FAIL; | |
3077 | goto end; | |
3078 | } else if (ret == 0) { | |
3079 | /* Nothing could be sent. */ | |
3080 | status = CLIENT_TRANSMISSION_STATUS_QUEUED; | |
3081 | } else { | |
3082 | /* Fd passing is an all or nothing kind of thing. */ | |
3083 | status = CLIENT_TRANSMISSION_STATUS_COMPLETE; | |
3084 | /* | |
3085 | * The payload _fd_array count is used later to | |
3086 | * check if there is notifications queued. So although the | |
3087 | * direct caller knows that the transmission is complete, we | |
3088 | * need to clear the _fd_array for the queuing check. | |
3089 | */ | |
3090 | lttng_dynamic_pointer_array_clear( | |
3091 | &client->communication.outbound.payload | |
3092 | ._fd_handles); | |
3093 | } | |
3094 | ||
f2b3ef9f | 3095 | end: |
882093ee JR |
3096 | if (status == CLIENT_TRANSMISSION_STATUS_COMPLETE) { |
3097 | client->communication.outbound.queued_command_reply = false; | |
3098 | client->communication.outbound.dropped_notification = false; | |
3099 | lttng_payload_clear(&client->communication.outbound.payload); | |
3100 | } | |
3101 | ||
f2b3ef9f | 3102 | return status; |
ab0ee2ca | 3103 | error: |
f2b3ef9f | 3104 | return CLIENT_TRANSMISSION_STATUS_ERROR; |
ab0ee2ca JG |
3105 | } |
3106 | ||
882093ee JR |
3107 | static |
3108 | bool client_has_outbound_data_left( | |
3109 | const struct notification_client *client) | |
3110 | { | |
3111 | const struct lttng_payload_view pv = lttng_payload_view_from_payload( | |
3112 | &client->communication.outbound.payload, 0, -1); | |
3113 | const bool has_data = pv.buffer.size != 0; | |
3114 | const bool has_fds = lttng_payload_view_get_fd_handle_count(&pv); | |
3115 | ||
3116 | return has_data || has_fds; | |
3117 | } | |
3118 | ||
6c24d3fd | 3119 | /* Client lock must _not_ be held by the caller. */ |
ab0ee2ca JG |
3120 | static |
3121 | int client_send_command_reply(struct notification_client *client, | |
3122 | struct notification_thread_state *state, | |
3123 | enum lttng_notification_channel_status status) | |
3124 | { | |
3125 | int ret; | |
3126 | struct lttng_notification_channel_command_reply reply = { | |
3127 | .status = (int8_t) status, | |
3128 | }; | |
3129 | struct lttng_notification_channel_message msg = { | |
3130 | .type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY, | |
3131 | .size = sizeof(reply), | |
3132 | }; | |
3133 | char buffer[sizeof(msg) + sizeof(reply)]; | |
f2b3ef9f | 3134 | enum client_transmission_status transmission_status; |
ab0ee2ca | 3135 | |
6c24d3fd JG |
3136 | memcpy(buffer, &msg, sizeof(msg)); |
3137 | memcpy(buffer + sizeof(msg), &reply, sizeof(reply)); | |
3138 | DBG("[notification-thread] Send command reply (%i)", (int) status); | |
505b2d90 | 3139 | |
6c24d3fd | 3140 | pthread_mutex_lock(&client->lock); |
ab0ee2ca JG |
3141 | if (client->communication.outbound.queued_command_reply) { |
3142 | /* Protocol error. */ | |
6c24d3fd | 3143 | goto error_unlock; |
ab0ee2ca JG |
3144 | } |
3145 | ||
ab0ee2ca JG |
3146 | /* Enqueue buffer to outgoing queue and flush it. */ |
3147 | ret = lttng_dynamic_buffer_append( | |
882093ee | 3148 | &client->communication.outbound.payload.buffer, |
ab0ee2ca JG |
3149 | buffer, sizeof(buffer)); |
3150 | if (ret) { | |
6c24d3fd | 3151 | goto error_unlock; |
ab0ee2ca JG |
3152 | } |
3153 | ||
f2b3ef9f | 3154 | transmission_status = client_flush_outgoing_queue(client); |
882093ee JR |
3155 | |
3156 | if (client_has_outbound_data_left(client)) { | |
6c24d3fd JG |
3157 | /* Queue could not be emptied. */ |
3158 | client->communication.outbound.queued_command_reply = true; | |
3159 | } | |
3160 | ||
3161 | pthread_mutex_unlock(&client->lock); | |
f2b3ef9f JG |
3162 | ret = client_handle_transmission_status( |
3163 | client, transmission_status, state); | |
ab0ee2ca JG |
3164 | if (ret) { |
3165 | goto error; | |
3166 | } | |
3167 | ||
ab0ee2ca | 3168 | return 0; |
6c24d3fd JG |
3169 | error_unlock: |
3170 | pthread_mutex_unlock(&client->lock); | |
ab0ee2ca JG |
3171 | error: |
3172 | return -1; | |
3173 | } | |
3174 | ||
505b2d90 JG |
3175 | static |
3176 | int client_handle_message_unknown(struct notification_client *client, | |
3177 | struct notification_thread_state *state) | |
3178 | { | |
3179 | int ret; | |
505b2d90 JG |
3180 | /* |
3181 | * Receiving message header. The function will be called again | |
3182 | * once the rest of the message as been received and can be | |
3183 | * interpreted. | |
3184 | */ | |
3185 | const struct lttng_notification_channel_message *msg; | |
3186 | ||
882093ee | 3187 | assert(sizeof(*msg) == client->communication.inbound.payload.buffer.size); |
505b2d90 | 3188 | msg = (const struct lttng_notification_channel_message *) |
882093ee | 3189 | client->communication.inbound.payload.buffer.data; |
505b2d90 JG |
3190 | |
3191 | if (msg->size == 0 || | |
3192 | msg->size > DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE) { | |
3193 | ERR("[notification-thread] Invalid notification channel message: length = %u", | |
3194 | msg->size); | |
3195 | ret = -1; | |
3196 | goto end; | |
3197 | } | |
3198 | ||
3199 | switch (msg->type) { | |
3200 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE: | |
3201 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE: | |
3202 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE: | |
3203 | break; | |
3204 | default: | |
3205 | ret = -1; | |
3206 | ERR("[notification-thread] Invalid notification channel message: unexpected message type"); | |
3207 | goto end; | |
3208 | } | |
3209 | ||
3210 | client->communication.inbound.bytes_to_receive = msg->size; | |
882093ee | 3211 | client->communication.inbound.fds_to_receive = msg->fds; |
505b2d90 JG |
3212 | client->communication.inbound.msg_type = |
3213 | (enum lttng_notification_channel_message_type) msg->type; | |
3214 | ret = lttng_dynamic_buffer_set_size( | |
882093ee JR |
3215 | &client->communication.inbound.payload.buffer, msg->size); |
3216 | ||
3217 | /* msg is not valid anymore due to lttng_dynamic_buffer_set_size. */ | |
3218 | msg = NULL; | |
505b2d90 | 3219 | end: |
505b2d90 JG |
3220 | return ret; |
3221 | } | |
3222 | ||
3223 | static | |
3224 | int client_handle_message_handshake(struct notification_client *client, | |
3225 | struct notification_thread_state *state) | |
3226 | { | |
3227 | int ret; | |
3228 | struct lttng_notification_channel_command_handshake *handshake_client; | |
3229 | const struct lttng_notification_channel_command_handshake handshake_reply = { | |
3230 | .major = LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR, | |
3231 | .minor = LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR, | |
3232 | }; | |
3233 | const struct lttng_notification_channel_message msg_header = { | |
3234 | .type = LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE, | |
3235 | .size = sizeof(handshake_reply), | |
3236 | }; | |
3237 | enum lttng_notification_channel_status status = | |
3238 | LTTNG_NOTIFICATION_CHANNEL_STATUS_OK; | |
3239 | char send_buffer[sizeof(msg_header) + sizeof(handshake_reply)]; | |
505b2d90 JG |
3240 | |
3241 | memcpy(send_buffer, &msg_header, sizeof(msg_header)); | |
3242 | memcpy(send_buffer + sizeof(msg_header), &handshake_reply, | |
3243 | sizeof(handshake_reply)); | |
3244 | ||
3245 | handshake_client = | |
3246 | (struct lttng_notification_channel_command_handshake *) | |
882093ee | 3247 | client->communication.inbound.payload.buffer |
505b2d90 JG |
3248 | .data; |
3249 | client->major = handshake_client->major; | |
3250 | client->minor = handshake_client->minor; | |
3251 | if (!client->communication.inbound.creds_received) { | |
3252 | ERR("[notification-thread] No credentials received from client"); | |
3253 | ret = -1; | |
3254 | goto end; | |
3255 | } | |
3256 | ||
3257 | client->uid = LTTNG_SOCK_GET_UID_CRED( | |
3258 | &client->communication.inbound.creds); | |
3259 | client->gid = LTTNG_SOCK_GET_GID_CRED( | |
3260 | &client->communication.inbound.creds); | |
3261 | DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i", | |
3262 | client->uid, client->gid, (int) client->major, | |
3263 | (int) client->minor); | |
3264 | ||
3265 | if (handshake_client->major != | |
3266 | LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR) { | |
3267 | status = LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION; | |
3268 | } | |
3269 | ||
6c24d3fd JG |
3270 | pthread_mutex_lock(&client->lock); |
3271 | /* Outgoing queue will be flushed when the command reply is sent. */ | |
505b2d90 | 3272 | ret = lttng_dynamic_buffer_append( |
882093ee | 3273 | &client->communication.outbound.payload.buffer, send_buffer, |
505b2d90 JG |
3274 | sizeof(send_buffer)); |
3275 | if (ret) { | |
3276 | ERR("[notification-thread] Failed to send protocol version to notification channel client"); | |
6c24d3fd | 3277 | goto end_unlock; |
505b2d90 JG |
3278 | } |
3279 | ||
3280 | client->validated = true; | |
3281 | client->communication.active = true; | |
6c24d3fd | 3282 | pthread_mutex_unlock(&client->lock); |
505b2d90 | 3283 | |
6c24d3fd JG |
3284 | /* Set reception state to receive the next message header. */ |
3285 | ret = client_reset_inbound_state(client); | |
505b2d90 | 3286 | if (ret) { |
6c24d3fd | 3287 | ERR("[notification-thread] Failed to reset client communication's inbound state"); |
505b2d90 JG |
3288 | goto end; |
3289 | } | |
3290 | ||
6c24d3fd | 3291 | /* Flushes the outgoing queue. */ |
505b2d90 JG |
3292 | ret = client_send_command_reply(client, state, status); |
3293 | if (ret) { | |
3294 | ERR("[notification-thread] Failed to send reply to notification channel client"); | |
3295 | goto end; | |
3296 | } | |
3297 | ||
6c24d3fd JG |
3298 | goto end; |
3299 | end_unlock: | |
505b2d90 | 3300 | pthread_mutex_unlock(&client->lock); |
6c24d3fd | 3301 | end: |
505b2d90 JG |
3302 | return ret; |
3303 | } | |
3304 | ||
3305 | static | |
3306 | int client_handle_message_subscription( | |
3307 | struct notification_client *client, | |
3308 | enum lttng_notification_channel_message_type msg_type, | |
3309 | struct notification_thread_state *state) | |
3310 | { | |
3311 | int ret; | |
3312 | struct lttng_condition *condition; | |
3313 | enum lttng_notification_channel_status status = | |
3314 | LTTNG_NOTIFICATION_CHANNEL_STATUS_OK; | |
3315 | struct lttng_payload_view condition_view = | |
882093ee JR |
3316 | lttng_payload_view_from_payload( |
3317 | &client->communication.inbound.payload, | |
505b2d90 JG |
3318 | 0, -1); |
3319 | size_t expected_condition_size; | |
3320 | ||
6c24d3fd JG |
3321 | /* |
3322 | * No need to lock client to sample the inbound state as the only | |
3323 | * other thread accessing clients (action executor) only uses the | |
3324 | * outbound state. | |
3325 | */ | |
882093ee | 3326 | expected_condition_size = client->communication.inbound.payload.buffer.size; |
505b2d90 JG |
3327 | ret = lttng_condition_create_from_payload(&condition_view, &condition); |
3328 | if (ret != expected_condition_size) { | |
3329 | ERR("[notification-thread] Malformed condition received from client"); | |
3330 | goto end; | |
3331 | } | |
3332 | ||
3333 | if (msg_type == LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE) { | |
3334 | ret = notification_thread_client_subscribe( | |
3335 | client, condition, state, &status); | |
3336 | } else { | |
3337 | ret = notification_thread_client_unsubscribe( | |
3338 | client, condition, state, &status); | |
3339 | } | |
505b2d90 | 3340 | |
505b2d90 | 3341 | if (ret) { |
6c24d3fd | 3342 | goto end; |
505b2d90 JG |
3343 | } |
3344 | ||
3345 | /* Set reception state to receive the next message header. */ | |
3346 | ret = client_reset_inbound_state(client); | |
3347 | if (ret) { | |
3348 | ERR("[notification-thread] Failed to reset client communication's inbound state"); | |
6c24d3fd JG |
3349 | goto end; |
3350 | } | |
3351 | ||
3352 | ret = client_send_command_reply(client, state, status); | |
3353 | if (ret) { | |
3354 | ERR("[notification-thread] Failed to send reply to notification channel client"); | |
3355 | goto end; | |
505b2d90 JG |
3356 | } |
3357 | ||
505b2d90 JG |
3358 | end: |
3359 | return ret; | |
3360 | } | |
3361 | ||
ab0ee2ca JG |
3362 | static |
3363 | int client_dispatch_message(struct notification_client *client, | |
3364 | struct notification_thread_state *state) | |
3365 | { | |
3366 | int ret = 0; | |
3367 | ||
3368 | if (client->communication.inbound.msg_type != | |
3369 | LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE && | |
3370 | client->communication.inbound.msg_type != | |
3371 | LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN && | |
3372 | !client->validated) { | |
3373 | WARN("[notification-thread] client attempted a command before handshake"); | |
3374 | ret = -1; | |
3375 | goto end; | |
3376 | } | |
3377 | ||
3378 | switch (client->communication.inbound.msg_type) { | |
3379 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN: | |
3380 | { | |
505b2d90 | 3381 | ret = client_handle_message_unknown(client, state); |
ab0ee2ca JG |
3382 | break; |
3383 | } | |
3384 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE: | |
3385 | { | |
505b2d90 | 3386 | ret = client_handle_message_handshake(client, state); |
ab0ee2ca JG |
3387 | break; |
3388 | } | |
3389 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE: | |
3390 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE: | |
3391 | { | |
505b2d90 JG |
3392 | ret = client_handle_message_subscription(client, |
3393 | client->communication.inbound.msg_type, state); | |
ab0ee2ca JG |
3394 | break; |
3395 | } | |
3396 | default: | |
3397 | abort(); | |
3398 | } | |
3399 | end: | |
3400 | return ret; | |
3401 | } | |
3402 | ||
3403 | /* Incoming data from client. */ | |
3404 | int handle_notification_thread_client_in( | |
3405 | struct notification_thread_state *state, int socket) | |
3406 | { | |
14fa22f8 | 3407 | int ret = 0; |
ab0ee2ca JG |
3408 | struct notification_client *client; |
3409 | ssize_t recv_ret; | |
3410 | size_t offset; | |
3411 | ||
98b5ff34 | 3412 | rcu_read_lock(); |
ab0ee2ca JG |
3413 | client = get_client_from_socket(socket, state); |
3414 | if (!client) { | |
3415 | /* Internal error, abort. */ | |
3416 | ret = -1; | |
3417 | goto end; | |
3418 | } | |
3419 | ||
882093ee | 3420 | offset = client->communication.inbound.payload.buffer.size - |
14fa22f8 | 3421 | client->communication.inbound.bytes_to_receive; |
01ea340e | 3422 | if (client->communication.inbound.expect_creds) { |
ab0ee2ca | 3423 | recv_ret = lttcomm_recv_creds_unix_sock(socket, |
882093ee | 3424 | client->communication.inbound.payload.buffer.data + offset, |
ab0ee2ca JG |
3425 | client->communication.inbound.bytes_to_receive, |
3426 | &client->communication.inbound.creds); | |
3427 | if (recv_ret > 0) { | |
01ea340e | 3428 | client->communication.inbound.expect_creds = false; |
ab0ee2ca JG |
3429 | client->communication.inbound.creds_received = true; |
3430 | } | |
3431 | } else { | |
3432 | recv_ret = lttcomm_recv_unix_sock_non_block(socket, | |
882093ee | 3433 | client->communication.inbound.payload.buffer.data + offset, |
ab0ee2ca JG |
3434 | client->communication.inbound.bytes_to_receive); |
3435 | } | |
505b2d90 JG |
3436 | if (recv_ret >= 0) { |
3437 | client->communication.inbound.bytes_to_receive -= recv_ret; | |
882093ee JR |
3438 | } else { |
3439 | goto error_disconnect_client; | |
505b2d90 | 3440 | } |
6c24d3fd | 3441 | |
882093ee JR |
3442 | if (client->communication.inbound.bytes_to_receive != 0) { |
3443 | /* Message incomplete wait for more data. */ | |
3444 | ret = 0; | |
3445 | goto end; | |
ab0ee2ca JG |
3446 | } |
3447 | ||
882093ee JR |
3448 | assert(client->communication.inbound.bytes_to_receive == 0); |
3449 | ||
3450 | /* Receive fds. */ | |
3451 | if (client->communication.inbound.fds_to_receive != 0) { | |
3452 | ret = lttcomm_recv_payload_fds_unix_sock_non_block( | |
3453 | client->socket, | |
3454 | client->communication.inbound.fds_to_receive, | |
3455 | &client->communication.inbound.payload); | |
3456 | if (ret > 0) { | |
ab0ee2ca | 3457 | /* |
882093ee JR |
3458 | * Fds received. non blocking fds passing is all |
3459 | * or nothing. | |
ab0ee2ca | 3460 | */ |
882093ee JR |
3461 | ssize_t expected_size; |
3462 | ||
3463 | expected_size = sizeof(int) * | |
3464 | client->communication.inbound | |
3465 | .fds_to_receive; | |
3466 | assert(ret == expected_size); | |
3467 | client->communication.inbound.fds_to_receive = 0; | |
3468 | } else if (ret == 0) { | |
3469 | /* Received nothing. */ | |
3470 | ret = 0; | |
3471 | goto end; | |
3472 | } else { | |
ab0ee2ca JG |
3473 | goto error_disconnect_client; |
3474 | } | |
ab0ee2ca | 3475 | } |
882093ee JR |
3476 | |
3477 | /* At this point the message is complete.*/ | |
3478 | assert(client->communication.inbound.bytes_to_receive == 0 && | |
3479 | client->communication.inbound.fds_to_receive == 0); | |
3480 | ret = client_dispatch_message(client, state); | |
3481 | if (ret) { | |
3482 | /* | |
3483 | * Only returns an error if this client must be | |
3484 | * disconnected. | |
3485 | */ | |
3486 | goto error_disconnect_client; | |
3487 | } | |
3488 | ||
ab0ee2ca | 3489 | end: |
98b5ff34 | 3490 | rcu_read_unlock(); |
ab0ee2ca | 3491 | return ret; |
882093ee | 3492 | |
ab0ee2ca | 3493 | error_disconnect_client: |
505b2d90 | 3494 | ret = notification_thread_client_disconnect(client, state); |
98b5ff34 | 3495 | goto end; |
ab0ee2ca JG |
3496 | } |
3497 | ||
3498 | /* Client ready to receive outgoing data. */ | |
3499 | int handle_notification_thread_client_out( | |
3500 | struct notification_thread_state *state, int socket) | |
3501 | { | |
3502 | int ret; | |
3503 | struct notification_client *client; | |
f2b3ef9f | 3504 | enum client_transmission_status transmission_status; |
ab0ee2ca | 3505 | |
98b5ff34 | 3506 | rcu_read_lock(); |
ab0ee2ca JG |
3507 | client = get_client_from_socket(socket, state); |
3508 | if (!client) { | |
3509 | /* Internal error, abort. */ | |
3510 | ret = -1; | |
3511 | goto end; | |
3512 | } | |
3513 | ||
505b2d90 | 3514 | pthread_mutex_lock(&client->lock); |
f2b3ef9f | 3515 | transmission_status = client_flush_outgoing_queue(client); |
6c24d3fd JG |
3516 | pthread_mutex_unlock(&client->lock); |
3517 | ||
f2b3ef9f JG |
3518 | ret = client_handle_transmission_status( |
3519 | client, transmission_status, state); | |
ab0ee2ca JG |
3520 | if (ret) { |
3521 | goto end; | |
3522 | } | |
3523 | end: | |
98b5ff34 | 3524 | rcu_read_unlock(); |
ab0ee2ca JG |
3525 | return ret; |
3526 | } | |
3527 | ||
3528 | static | |
e8360425 JD |
3529 | bool evaluate_buffer_usage_condition(const struct lttng_condition *condition, |
3530 | const struct channel_state_sample *sample, | |
3531 | uint64_t buffer_capacity) | |
ab0ee2ca JG |
3532 | { |
3533 | bool result = false; | |
3534 | uint64_t threshold; | |
3535 | enum lttng_condition_type condition_type; | |
e8360425 | 3536 | const struct lttng_condition_buffer_usage *use_condition = container_of( |
ab0ee2ca JG |
3537 | condition, struct lttng_condition_buffer_usage, |
3538 | parent); | |
3539 | ||
ab0ee2ca JG |
3540 | if (use_condition->threshold_bytes.set) { |
3541 | threshold = use_condition->threshold_bytes.value; | |
3542 | } else { | |
3543 | /* | |
3544 | * Threshold was expressed as a ratio. | |
3545 | * | |
3546 | * TODO the threshold (in bytes) of conditions expressed | |
3547 | * as a ratio of total buffer size could be cached to | |
3548 | * forego this double-multiplication or it could be performed | |
3549 | * as fixed-point math. | |
3550 | * | |
d49487dc | 3551 | * Note that caching should accommodates the case where the |
ab0ee2ca JG |
3552 | * condition applies to multiple channels (i.e. don't assume |
3553 | * that all channels matching my_chann* have the same size...) | |
3554 | */ | |
3555 | threshold = (uint64_t) (use_condition->threshold_ratio.value * | |
3556 | (double) buffer_capacity); | |
3557 | } | |
3558 | ||
3559 | condition_type = lttng_condition_get_type(condition); | |
3560 | if (condition_type == LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW) { | |
3561 | DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64 ", highest usage = %" PRIu64, | |
3562 | threshold, sample->highest_usage); | |
3563 | ||
3564 | /* | |
3565 | * The low condition should only be triggered once _all_ of the | |
3566 | * streams in a channel have gone below the "low" threshold. | |
3567 | */ | |
3568 | if (sample->highest_usage <= threshold) { | |
3569 | result = true; | |
3570 | } | |
3571 | } else { | |
3572 | DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64 ", highest usage = %" PRIu64, | |
3573 | threshold, sample->highest_usage); | |
3574 | ||
3575 | /* | |
3576 | * For high buffer usage scenarios, we want to trigger whenever | |
3577 | * _any_ of the streams has reached the "high" threshold. | |
3578 | */ | |
3579 | if (sample->highest_usage >= threshold) { | |
3580 | result = true; | |
3581 | } | |
3582 | } | |
e8360425 | 3583 | |
ab0ee2ca JG |
3584 | return result; |
3585 | } | |
3586 | ||
3587 | static | |
e8360425 JD |
3588 | bool evaluate_session_consumed_size_condition( |
3589 | const struct lttng_condition *condition, | |
3590 | uint64_t session_consumed_size) | |
3591 | { | |
3592 | uint64_t threshold; | |
3593 | const struct lttng_condition_session_consumed_size *size_condition = | |
3594 | container_of(condition, | |
3595 | struct lttng_condition_session_consumed_size, | |
3596 | parent); | |
3597 | ||
3598 | threshold = size_condition->consumed_threshold_bytes.value; | |
3599 | DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64 ", current size = %" PRIu64, | |
3600 | threshold, session_consumed_size); | |
3601 | return session_consumed_size >= threshold; | |
3602 | } | |
3603 | ||
3604 | static | |
ed327204 | 3605 | int evaluate_buffer_condition(const struct lttng_condition *condition, |
ab0ee2ca | 3606 | struct lttng_evaluation **evaluation, |
e8360425 JD |
3607 | const struct notification_thread_state *state, |
3608 | const struct channel_state_sample *previous_sample, | |
3609 | const struct channel_state_sample *latest_sample, | |
3610 | uint64_t previous_session_consumed_total, | |
3611 | uint64_t latest_session_consumed_total, | |
3612 | struct channel_info *channel_info) | |
ab0ee2ca JG |
3613 | { |
3614 | int ret = 0; | |
3615 | enum lttng_condition_type condition_type; | |
e8360425 JD |
3616 | const bool previous_sample_available = !!previous_sample; |
3617 | bool previous_sample_result = false; | |
ab0ee2ca JG |
3618 | bool latest_sample_result; |
3619 | ||
3620 | condition_type = lttng_condition_get_type(condition); | |
ab0ee2ca | 3621 | |
e8360425 JD |
3622 | switch (condition_type) { |
3623 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
3624 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
3625 | if (caa_likely(previous_sample_available)) { | |
3626 | previous_sample_result = | |
3627 | evaluate_buffer_usage_condition(condition, | |
3628 | previous_sample, channel_info->capacity); | |
3629 | } | |
3630 | latest_sample_result = evaluate_buffer_usage_condition( | |
3631 | condition, latest_sample, | |
3632 | channel_info->capacity); | |
3633 | break; | |
3634 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: | |
3635 | if (caa_likely(previous_sample_available)) { | |
3636 | previous_sample_result = | |
3637 | evaluate_session_consumed_size_condition( | |
3638 | condition, | |
3639 | previous_session_consumed_total); | |
3640 | } | |
3641 | latest_sample_result = | |
3642 | evaluate_session_consumed_size_condition( | |
3643 | condition, | |
3644 | latest_session_consumed_total); | |
3645 | break; | |
3646 | default: | |
3647 | /* Unknown condition type; internal error. */ | |
3648 | abort(); | |
3649 | } | |
ab0ee2ca JG |
3650 | |
3651 | if (!latest_sample_result || | |
3652 | (previous_sample_result == latest_sample_result)) { | |
3653 | /* | |
3654 | * Only trigger on a condition evaluation transition. | |
3655 | * | |
3656 | * NOTE: This edge-triggered logic may not be appropriate for | |
3657 | * future condition types. | |
3658 | */ | |
3659 | goto end; | |
3660 | } | |
3661 | ||
e8360425 JD |
3662 | if (!evaluation || !latest_sample_result) { |
3663 | goto end; | |
3664 | } | |
3665 | ||
3666 | switch (condition_type) { | |
3667 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
3668 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
ab0ee2ca JG |
3669 | *evaluation = lttng_evaluation_buffer_usage_create( |
3670 | condition_type, | |
3671 | latest_sample->highest_usage, | |
e8360425 JD |
3672 | channel_info->capacity); |
3673 | break; | |
3674 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: | |
3675 | *evaluation = lttng_evaluation_session_consumed_size_create( | |
e8360425 JD |
3676 | latest_session_consumed_total); |
3677 | break; | |
3678 | default: | |
3679 | abort(); | |
3680 | } | |
3681 | ||
3682 | if (!*evaluation) { | |
3683 | ret = -1; | |
3684 | goto end; | |
ab0ee2ca JG |
3685 | } |
3686 | end: | |
3687 | return ret; | |
3688 | } | |
3689 | ||
3690 | static | |
f2b3ef9f | 3691 | int client_notification_overflow(struct notification_client *client) |
ab0ee2ca | 3692 | { |
f2b3ef9f JG |
3693 | int ret = 0; |
3694 | const struct lttng_notification_channel_message msg = { | |
ab0ee2ca | 3695 | .type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED, |
ab0ee2ca JG |
3696 | }; |
3697 | ||
505b2d90 JG |
3698 | ASSERT_LOCKED(client->lock); |
3699 | ||
f2b3ef9f JG |
3700 | DBG("Dropping notification addressed to client (socket fd = %i)", |
3701 | client->socket); | |
3702 | if (client->communication.outbound.dropped_notification) { | |
3703 | /* | |
3704 | * The client already has a "notification dropped" message | |
3705 | * in its outgoing queue. Nothing to do since all | |
3706 | * of those messages are coalesced. | |
3707 | */ | |
3708 | goto end; | |
3709 | } | |
3710 | ||
3711 | client->communication.outbound.dropped_notification = true; | |
ab0ee2ca | 3712 | ret = lttng_dynamic_buffer_append( |
882093ee | 3713 | &client->communication.outbound.payload.buffer, &msg, |
ab0ee2ca | 3714 | sizeof(msg)); |
f2b3ef9f JG |
3715 | if (ret) { |
3716 | PERROR("Failed to enqueue \"dropped notification\" message in client's (socket fd = %i) outgoing queue", | |
3717 | client->socket); | |
3718 | } | |
3719 | end: | |
ab0ee2ca JG |
3720 | return ret; |
3721 | } | |
3722 | ||
f2b3ef9f JG |
3723 | static int client_handle_transmission_status_wrapper( |
3724 | struct notification_client *client, | |
3725 | enum client_transmission_status status, | |
3726 | void *user_data) | |
3727 | { | |
3728 | return client_handle_transmission_status(client, status, | |
3729 | (struct notification_thread_state *) user_data); | |
3730 | } | |
3731 | ||
3732 | static | |
3733 | int send_evaluation_to_clients(const struct lttng_trigger *trigger, | |
3734 | const struct lttng_evaluation *evaluation, | |
3735 | struct notification_client_list* client_list, | |
3736 | struct notification_thread_state *state, | |
3737 | uid_t object_uid, gid_t object_gid) | |
3738 | { | |
ff588497 JR |
3739 | const struct lttng_credentials creds = { |
3740 | .uid = LTTNG_OPTIONAL_INIT_VALUE(object_uid), | |
3741 | .gid = LTTNG_OPTIONAL_INIT_VALUE(object_gid), | |
3742 | }; | |
3743 | ||
f2b3ef9f JG |
3744 | return notification_client_list_send_evaluation(client_list, |
3745 | lttng_trigger_get_const_condition(trigger), evaluation, | |
3746 | lttng_trigger_get_credentials(trigger), | |
ff588497 | 3747 | &creds, |
f2b3ef9f JG |
3748 | client_handle_transmission_status_wrapper, state); |
3749 | } | |
3750 | ||
f37d0f86 JG |
3751 | /* |
3752 | * Permission checks relative to notification channel clients are performed | |
3753 | * here. Notice how object, client, and trigger credentials are involved in | |
3754 | * this check. | |
3755 | * | |
3756 | * The `object` credentials are the credentials associated with the "subject" | |
3757 | * of a condition. For instance, a `rotation completed` condition applies | |
3758 | * to a session. When that condition is met, it will produce an evaluation | |
3759 | * against a session. Hence, in this case, the `object` credentials are the | |
3760 | * credentials of the "subject" session. | |
3761 | * | |
3762 | * The `trigger` credentials are the credentials of the user that registered the | |
3763 | * trigger. | |
3764 | * | |
3765 | * The `client` credentials are the credentials of the user that created a given | |
3766 | * notification channel. | |
3767 | * | |
3768 | * In terms of visibility, it is expected that non-privilieged users can only | |
3769 | * register triggers against "their" objects (their own sessions and | |
3770 | * applications they are allowed to interact with). They can then open a | |
3771 | * notification channel and subscribe to notifications associated with those | |
3772 | * triggers. | |
3773 | * | |
3774 | * As for privilieged users, they can register triggers against the objects of | |
3775 | * other users. They can then subscribe to the notifications associated to their | |
3776 | * triggers. Privilieged users _can't_ subscribe to the notifications of | |
3777 | * triggers owned by other users; they must create their own triggers. | |
3778 | * | |
3779 | * This is more a concern of usability than security. It would be difficult for | |
3780 | * a root user reliably subscribe to a specific set of conditions without | |
3781 | * interference from external users (those could, for instance, unregister | |
3782 | * their triggers). | |
3783 | */ | |
f2b3ef9f JG |
3784 | LTTNG_HIDDEN |
3785 | int notification_client_list_send_evaluation( | |
3786 | struct notification_client_list *client_list, | |
3787 | const struct lttng_condition *condition, | |
9b63a4aa | 3788 | const struct lttng_evaluation *evaluation, |
f2b3ef9f JG |
3789 | const struct lttng_credentials *trigger_creds, |
3790 | const struct lttng_credentials *source_object_creds, | |
3791 | report_client_transmission_result_cb client_report, | |
3792 | void *user_data) | |
ab0ee2ca JG |
3793 | { |
3794 | int ret = 0; | |
c0a66c84 | 3795 | struct lttng_payload msg_payload; |
ab0ee2ca | 3796 | struct notification_client_list_element *client_list_element, *tmp; |
9b63a4aa | 3797 | const struct lttng_notification notification = { |
f2b3ef9f | 3798 | .condition = (struct lttng_condition *) condition, |
9b63a4aa JG |
3799 | .evaluation = (struct lttng_evaluation *) evaluation, |
3800 | }; | |
3647288f JG |
3801 | struct lttng_notification_channel_message msg_header = { |
3802 | .type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION, | |
3803 | }; | |
ab0ee2ca | 3804 | |
c0a66c84 | 3805 | lttng_payload_init(&msg_payload); |
ab0ee2ca | 3806 | |
c0a66c84 | 3807 | ret = lttng_dynamic_buffer_append(&msg_payload.buffer, &msg_header, |
3647288f | 3808 | sizeof(msg_header)); |
ab0ee2ca JG |
3809 | if (ret) { |
3810 | goto end; | |
3811 | } | |
3812 | ||
c0a66c84 | 3813 | ret = lttng_notification_serialize(¬ification, &msg_payload); |
ab0ee2ca | 3814 | if (ret) { |
ab0ee2ca JG |
3815 | ERR("[notification-thread] Failed to serialize notification"); |
3816 | ret = -1; | |
3817 | goto end; | |
3818 | } | |
3819 | ||
3647288f | 3820 | /* Update payload size. */ |
505b2d90 JG |
3821 | ((struct lttng_notification_channel_message *) msg_payload.buffer.data) |
3822 | ->size = (uint32_t)( | |
3823 | msg_payload.buffer.size - sizeof(msg_header)); | |
3647288f | 3824 | |
882093ee JR |
3825 | /* Update the payload number of fds. */ |
3826 | { | |
3827 | const struct lttng_payload_view pv = lttng_payload_view_from_payload( | |
3828 | &msg_payload, 0, -1); | |
3829 | ||
3830 | ((struct lttng_notification_channel_message *) | |
3831 | msg_payload.buffer.data)->fds = (uint32_t) | |
3832 | lttng_payload_view_get_fd_handle_count(&pv); | |
3833 | } | |
3834 | ||
505b2d90 | 3835 | pthread_mutex_lock(&client_list->lock); |
ab0ee2ca JG |
3836 | cds_list_for_each_entry_safe(client_list_element, tmp, |
3837 | &client_list->list, node) { | |
f2b3ef9f | 3838 | enum client_transmission_status transmission_status; |
ab0ee2ca JG |
3839 | struct notification_client *client = |
3840 | client_list_element->client; | |
3841 | ||
505b2d90 JG |
3842 | ret = 0; |
3843 | pthread_mutex_lock(&client->lock); | |
6c24d3fd JG |
3844 | if (!client->communication.active) { |
3845 | /* | |
3846 | * Skip inactive client (protocol error or | |
3847 | * disconnecting). | |
3848 | */ | |
3849 | DBG("Skipping client at it is marked as inactive"); | |
3850 | goto skip_client; | |
3851 | } | |
3852 | ||
f2b3ef9f | 3853 | if (source_object_creds) { |
ff588497 JR |
3854 | if (client->uid != lttng_credentials_get_uid(source_object_creds) && |
3855 | client->gid != lttng_credentials_get_gid(source_object_creds) && | |
f2b3ef9f JG |
3856 | client->uid != 0) { |
3857 | /* | |
3858 | * Client is not allowed to monitor this | |
3859 | * object. | |
3860 | */ | |
3861 | DBG("[notification-thread] Skipping client at it does not have the object permission to receive notification for this trigger"); | |
6c24d3fd | 3862 | goto skip_client; |
f2b3ef9f | 3863 | } |
90843f49 JR |
3864 | } |
3865 | ||
ff588497 | 3866 | if (client->uid != lttng_credentials_get_uid(trigger_creds) && client->gid != lttng_credentials_get_gid(trigger_creds)) { |
90843f49 | 3867 | DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this trigger"); |
6c24d3fd | 3868 | goto skip_client; |
ab0ee2ca JG |
3869 | } |
3870 | ||
3871 | DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)", | |
c0a66c84 | 3872 | client->socket, msg_payload.buffer.size); |
882093ee JR |
3873 | |
3874 | if (client_has_outbound_data_left(client)) { | |
ab0ee2ca JG |
3875 | /* |
3876 | * Outgoing data is already buffered for this client; | |
3877 | * drop the notification and enqueue a "dropped | |
3878 | * notification" message if this is the first dropped | |
3879 | * notification since the socket spilled-over to the | |
3880 | * queue. | |
3881 | */ | |
f2b3ef9f JG |
3882 | ret = client_notification_overflow(client); |
3883 | if (ret) { | |
6c24d3fd JG |
3884 | /* Fatal error. */ |
3885 | goto skip_client; | |
ab0ee2ca | 3886 | } |
ab0ee2ca JG |
3887 | } |
3888 | ||
882093ee | 3889 | ret = lttng_payload_copy(&msg_payload, &client->communication.outbound.payload); |
ab0ee2ca | 3890 | if (ret) { |
6c24d3fd JG |
3891 | /* Fatal error. */ |
3892 | goto skip_client; | |
ab0ee2ca JG |
3893 | } |
3894 | ||
f2b3ef9f | 3895 | transmission_status = client_flush_outgoing_queue(client); |
6c24d3fd | 3896 | pthread_mutex_unlock(&client->lock); |
f2b3ef9f | 3897 | ret = client_report(client, transmission_status, user_data); |
ab0ee2ca | 3898 | if (ret) { |
6c24d3fd JG |
3899 | /* Fatal error. */ |
3900 | goto end_unlock_list; | |
505b2d90 | 3901 | } |
6c24d3fd JG |
3902 | |
3903 | continue; | |
3904 | ||
3905 | skip_client: | |
505b2d90 JG |
3906 | pthread_mutex_unlock(&client->lock); |
3907 | if (ret) { | |
6c24d3fd | 3908 | /* Fatal error. */ |
505b2d90 | 3909 | goto end_unlock_list; |
ab0ee2ca JG |
3910 | } |
3911 | } | |
3912 | ret = 0; | |
505b2d90 JG |
3913 | |
3914 | end_unlock_list: | |
3915 | pthread_mutex_unlock(&client_list->lock); | |
ab0ee2ca | 3916 | end: |
c0a66c84 | 3917 | lttng_payload_reset(&msg_payload); |
ab0ee2ca JG |
3918 | return ret; |
3919 | } | |
3920 | ||
3921 | int handle_notification_thread_channel_sample( | |
3922 | struct notification_thread_state *state, int pipe, | |
3923 | enum lttng_domain_type domain) | |
3924 | { | |
3925 | int ret = 0; | |
3926 | struct lttcomm_consumer_channel_monitor_msg sample_msg; | |
ab0ee2ca JG |
3927 | struct channel_info *channel_info; |
3928 | struct cds_lfht_node *node; | |
3929 | struct cds_lfht_iter iter; | |
3930 | struct lttng_channel_trigger_list *trigger_list; | |
3931 | struct lttng_trigger_list_element *trigger_list_element; | |
3932 | bool previous_sample_available = false; | |
e8360425 JD |
3933 | struct channel_state_sample previous_sample, latest_sample; |
3934 | uint64_t previous_session_consumed_total, latest_session_consumed_total; | |
f2b3ef9f | 3935 | struct lttng_credentials channel_creds; |
ab0ee2ca JG |
3936 | |
3937 | /* | |
3938 | * The monitoring pipe only holds messages smaller than PIPE_BUF, | |
3939 | * ensuring that read/write of sampling messages are atomic. | |
3940 | */ | |
7c2551ef | 3941 | ret = lttng_read(pipe, &sample_msg, sizeof(sample_msg)); |
ab0ee2ca JG |
3942 | if (ret != sizeof(sample_msg)) { |
3943 | ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)", | |
3944 | pipe); | |
3945 | ret = -1; | |
3946 | goto end; | |
3947 | } | |
3948 | ||
3949 | ret = 0; | |
3950 | latest_sample.key.key = sample_msg.key; | |
3951 | latest_sample.key.domain = domain; | |
3952 | latest_sample.highest_usage = sample_msg.highest; | |
3953 | latest_sample.lowest_usage = sample_msg.lowest; | |
e8360425 | 3954 | latest_sample.channel_total_consumed = sample_msg.total_consumed; |
ab0ee2ca JG |
3955 | |
3956 | rcu_read_lock(); | |
3957 | ||
3958 | /* Retrieve the channel's informations */ | |
3959 | cds_lfht_lookup(state->channels_ht, | |
3960 | hash_channel_key(&latest_sample.key), | |
3961 | match_channel_info, | |
3962 | &latest_sample.key, | |
3963 | &iter); | |
3964 | node = cds_lfht_iter_get_node(&iter); | |
e0b5f87b | 3965 | if (caa_unlikely(!node)) { |
ab0ee2ca JG |
3966 | /* |
3967 | * Not an error since the consumer can push a sample to the pipe | |
3968 | * and the rest of the session daemon could notify us of the | |
3969 | * channel's destruction before we get a chance to process that | |
3970 | * sample. | |
3971 | */ | |
3972 | DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64 " in %s domain", | |
3973 | latest_sample.key.key, | |
3974 | domain == LTTNG_DOMAIN_KERNEL ? "kernel" : | |
3975 | "user space"); | |
3976 | goto end_unlock; | |
3977 | } | |
3978 | channel_info = caa_container_of(node, struct channel_info, | |
3979 | channels_ht_node); | |
e8360425 | 3980 | DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64 ") in session %s (highest usage = %" PRIu64 ", lowest usage = %" PRIu64", total consumed = %" PRIu64")", |
8abe313a | 3981 | channel_info->name, |
ab0ee2ca | 3982 | latest_sample.key.key, |
8abe313a | 3983 | channel_info->session_info->name, |
ab0ee2ca | 3984 | latest_sample.highest_usage, |
e8360425 JD |
3985 | latest_sample.lowest_usage, |
3986 | latest_sample.channel_total_consumed); | |
3987 | ||
3988 | previous_session_consumed_total = | |
3989 | channel_info->session_info->consumed_data_size; | |
ab0ee2ca JG |
3990 | |
3991 | /* Retrieve the channel's last sample, if it exists, and update it. */ | |
3992 | cds_lfht_lookup(state->channel_state_ht, | |
3993 | hash_channel_key(&latest_sample.key), | |
3994 | match_channel_state_sample, | |
3995 | &latest_sample.key, | |
3996 | &iter); | |
3997 | node = cds_lfht_iter_get_node(&iter); | |
e0b5f87b | 3998 | if (caa_likely(node)) { |
ab0ee2ca JG |
3999 | struct channel_state_sample *stored_sample; |
4000 | ||
4001 | /* Update the sample stored. */ | |
4002 | stored_sample = caa_container_of(node, | |
4003 | struct channel_state_sample, | |
4004 | channel_state_ht_node); | |
e8360425 | 4005 | |
ab0ee2ca JG |
4006 | memcpy(&previous_sample, stored_sample, |
4007 | sizeof(previous_sample)); | |
4008 | stored_sample->highest_usage = latest_sample.highest_usage; | |
4009 | stored_sample->lowest_usage = latest_sample.lowest_usage; | |
0f0479d7 | 4010 | stored_sample->channel_total_consumed = latest_sample.channel_total_consumed; |
ab0ee2ca | 4011 | previous_sample_available = true; |
e8360425 JD |
4012 | |
4013 | latest_session_consumed_total = | |
4014 | previous_session_consumed_total + | |
4015 | (latest_sample.channel_total_consumed - previous_sample.channel_total_consumed); | |
ab0ee2ca JG |
4016 | } else { |
4017 | /* | |
4018 | * This is the channel's first sample, allocate space for and | |
4019 | * store the new sample. | |
4020 | */ | |
4021 | struct channel_state_sample *stored_sample; | |
4022 | ||
4023 | stored_sample = zmalloc(sizeof(*stored_sample)); | |
4024 | if (!stored_sample) { | |
4025 | ret = -1; | |
4026 | goto end_unlock; | |
4027 | } | |
4028 | ||
4029 | memcpy(stored_sample, &latest_sample, sizeof(*stored_sample)); | |
4030 | cds_lfht_node_init(&stored_sample->channel_state_ht_node); | |
4031 | cds_lfht_add(state->channel_state_ht, | |
4032 | hash_channel_key(&stored_sample->key), | |
4033 | &stored_sample->channel_state_ht_node); | |
e8360425 JD |
4034 | |
4035 | latest_session_consumed_total = | |
4036 | previous_session_consumed_total + | |
4037 | latest_sample.channel_total_consumed; | |
ab0ee2ca JG |
4038 | } |
4039 | ||
e8360425 JD |
4040 | channel_info->session_info->consumed_data_size = |
4041 | latest_session_consumed_total; | |
4042 | ||
ab0ee2ca JG |
4043 | /* Find triggers associated with this channel. */ |
4044 | cds_lfht_lookup(state->channel_triggers_ht, | |
4045 | hash_channel_key(&latest_sample.key), | |
4046 | match_channel_trigger_list, | |
4047 | &latest_sample.key, | |
4048 | &iter); | |
4049 | node = cds_lfht_iter_get_node(&iter); | |
e0b5f87b | 4050 | if (caa_likely(!node)) { |
ab0ee2ca JG |
4051 | goto end_unlock; |
4052 | } | |
4053 | ||
f2b3ef9f | 4054 | channel_creds = (typeof(channel_creds)) { |
ff588497 JR |
4055 | .uid = LTTNG_OPTIONAL_INIT_VALUE(channel_info->session_info->uid), |
4056 | .gid = LTTNG_OPTIONAL_INIT_VALUE(channel_info->session_info->gid), | |
f2b3ef9f JG |
4057 | }; |
4058 | ||
ab0ee2ca JG |
4059 | trigger_list = caa_container_of(node, struct lttng_channel_trigger_list, |
4060 | channel_triggers_ht_node); | |
4061 | cds_list_for_each_entry(trigger_list_element, &trigger_list->list, | |
e742b055 | 4062 | node) { |
9b63a4aa | 4063 | const struct lttng_condition *condition; |
f2b3ef9f | 4064 | struct lttng_trigger *trigger; |
505b2d90 | 4065 | struct notification_client_list *client_list = NULL; |
ab0ee2ca | 4066 | struct lttng_evaluation *evaluation = NULL; |
f2b3ef9f | 4067 | enum action_executor_status executor_status; |
ab0ee2ca | 4068 | |
505b2d90 | 4069 | ret = 0; |
ab0ee2ca | 4070 | trigger = trigger_list_element->trigger; |
9b63a4aa | 4071 | condition = lttng_trigger_get_const_condition(trigger); |
ab0ee2ca | 4072 | assert(condition); |
ab0ee2ca JG |
4073 | |
4074 | /* | |
4075 | * Check if any client is subscribed to the result of this | |
4076 | * evaluation. | |
4077 | */ | |
ed327204 | 4078 | client_list = get_client_list_from_condition(state, condition); |
ab0ee2ca | 4079 | |
ed327204 | 4080 | ret = evaluate_buffer_condition(condition, &evaluation, state, |
ab0ee2ca | 4081 | previous_sample_available ? &previous_sample : NULL, |
e8360425 JD |
4082 | &latest_sample, |
4083 | previous_session_consumed_total, | |
4084 | latest_session_consumed_total, | |
4085 | channel_info); | |
4086 | if (caa_unlikely(ret)) { | |
505b2d90 | 4087 | goto put_list; |
ab0ee2ca JG |
4088 | } |
4089 | ||
e8360425 | 4090 | if (caa_likely(!evaluation)) { |
505b2d90 | 4091 | goto put_list; |
ab0ee2ca JG |
4092 | } |
4093 | ||
f8522f5c JR |
4094 | if (!lttng_trigger_should_fire(trigger)) { |
4095 | goto put_list; | |
4096 | } | |
4097 | ||
4098 | lttng_trigger_fire(trigger); | |
4099 | ||
f2b3ef9f JG |
4100 | /* |
4101 | * Ownership of `evaluation` transferred to the action executor | |
4102 | * no matter the result. | |
4103 | */ | |
4104 | executor_status = action_executor_enqueue(state->executor, | |
4105 | trigger, evaluation, &channel_creds, | |
4106 | client_list); | |
4107 | evaluation = NULL; | |
4108 | switch (executor_status) { | |
4109 | case ACTION_EXECUTOR_STATUS_OK: | |
4110 | break; | |
4111 | case ACTION_EXECUTOR_STATUS_ERROR: | |
4112 | case ACTION_EXECUTOR_STATUS_INVALID: | |
4113 | /* | |
4114 | * TODO Add trigger identification (name/id) when | |
4115 | * it is added to the API. | |
4116 | */ | |
4117 | ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger"); | |
4118 | ret = -1; | |
4119 | goto put_list; | |
4120 | case ACTION_EXECUTOR_STATUS_OVERFLOW: | |
4121 | /* | |
4122 | * TODO Add trigger identification (name/id) when | |
4123 | * it is added to the API. | |
4124 | * | |
4125 | * Not a fatal error. | |
4126 | */ | |
4127 | WARN("No space left when enqueuing action associated with buffer-condition trigger"); | |
4128 | ret = 0; | |
4129 | goto put_list; | |
4130 | default: | |
4131 | abort(); | |
4132 | } | |
4133 | ||
505b2d90 JG |
4134 | put_list: |
4135 | notification_client_list_put(client_list); | |
e0b5f87b | 4136 | if (caa_unlikely(ret)) { |
505b2d90 | 4137 | break; |
ab0ee2ca JG |
4138 | } |
4139 | } | |
4140 | end_unlock: | |
4141 | rcu_read_unlock(); | |
4142 | end: | |
4143 | return ret; | |
4144 | } |