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