Commit | Line | Data |
---|---|---|
ab0ee2ca JG |
1 | /* |
2 | * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms of the GNU General Public License, version 2 only, as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
11 | * more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along with | |
14 | * this program; if not, write to the Free Software Foundation, Inc., 51 | |
15 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
16 | */ | |
17 | ||
18 | #define _LGPL_SOURCE | |
19 | #include <urcu.h> | |
20 | #include <urcu/rculfhash.h> | |
21 | ||
ab0ee2ca JG |
22 | #include <common/defaults.h> |
23 | #include <common/error.h> | |
24 | #include <common/futex.h> | |
25 | #include <common/unix.h> | |
26 | #include <common/dynamic-buffer.h> | |
27 | #include <common/hashtable/utils.h> | |
28 | #include <common/sessiond-comm/sessiond-comm.h> | |
29 | #include <common/macros.h> | |
30 | #include <lttng/condition/condition.h> | |
9b63a4aa | 31 | #include <lttng/action/action-internal.h> |
ab0ee2ca JG |
32 | #include <lttng/notification/notification-internal.h> |
33 | #include <lttng/condition/condition-internal.h> | |
34 | #include <lttng/condition/buffer-usage-internal.h> | |
e8360425 | 35 | #include <lttng/condition/session-consumed-size-internal.h> |
ea9a44f0 | 36 | #include <lttng/condition/session-rotation-internal.h> |
ab0ee2ca | 37 | #include <lttng/notification/channel-internal.h> |
1da26331 | 38 | |
ab0ee2ca JG |
39 | #include <time.h> |
40 | #include <unistd.h> | |
41 | #include <assert.h> | |
42 | #include <inttypes.h> | |
d53ea4e4 | 43 | #include <fcntl.h> |
ab0ee2ca | 44 | |
1da26331 JG |
45 | #include "notification-thread.h" |
46 | #include "notification-thread-events.h" | |
47 | #include "notification-thread-commands.h" | |
48 | #include "lttng-sessiond.h" | |
49 | #include "kernel.h" | |
50 | ||
ab0ee2ca JG |
51 | #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP) |
52 | #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT) | |
53 | ||
51eab943 JG |
54 | enum lttng_object_type { |
55 | LTTNG_OBJECT_TYPE_UNKNOWN, | |
56 | LTTNG_OBJECT_TYPE_NONE, | |
57 | LTTNG_OBJECT_TYPE_CHANNEL, | |
58 | LTTNG_OBJECT_TYPE_SESSION, | |
59 | }; | |
60 | ||
ab0ee2ca | 61 | struct lttng_trigger_list_element { |
9e0bb80e JG |
62 | /* No ownership of the trigger object is assumed. */ |
63 | const struct lttng_trigger *trigger; | |
ab0ee2ca JG |
64 | struct cds_list_head node; |
65 | }; | |
66 | ||
67 | struct lttng_channel_trigger_list { | |
68 | struct channel_key channel_key; | |
ea9a44f0 | 69 | /* List of struct lttng_trigger_list_element. */ |
ab0ee2ca | 70 | struct cds_list_head list; |
ea9a44f0 | 71 | /* Node in the channel_triggers_ht */ |
ab0ee2ca | 72 | struct cds_lfht_node channel_triggers_ht_node; |
83b934ad MD |
73 | /* call_rcu delayed reclaim. */ |
74 | struct rcu_head rcu_node; | |
ab0ee2ca JG |
75 | }; |
76 | ||
ea9a44f0 JG |
77 | /* |
78 | * List of triggers applying to a given session. | |
79 | * | |
80 | * See: | |
81 | * - lttng_session_trigger_list_create() | |
82 | * - lttng_session_trigger_list_build() | |
83 | * - lttng_session_trigger_list_destroy() | |
84 | * - lttng_session_trigger_list_add() | |
85 | */ | |
86 | struct lttng_session_trigger_list { | |
87 | /* | |
88 | * Not owned by this; points to the session_info structure's | |
89 | * session name. | |
90 | */ | |
91 | const char *session_name; | |
92 | /* List of struct lttng_trigger_list_element. */ | |
93 | struct cds_list_head list; | |
94 | /* Node in the session_triggers_ht */ | |
95 | struct cds_lfht_node session_triggers_ht_node; | |
96 | /* | |
97 | * Weak reference to the notification system's session triggers | |
98 | * hashtable. | |
99 | * | |
100 | * The session trigger list structure structure is owned by | |
101 | * the session's session_info. | |
102 | * | |
103 | * The session_info is kept alive the the channel_infos holding a | |
104 | * reference to it (reference counting). When those channels are | |
105 | * destroyed (at runtime or on teardown), the reference they hold | |
106 | * to the session_info are released. On destruction of session_info, | |
107 | * session_info_destroy() will remove the list of triggers applying | |
108 | * to this session from the notification system's state. | |
109 | * | |
110 | * This implies that the session_triggers_ht must be destroyed | |
111 | * after the channels. | |
112 | */ | |
113 | struct cds_lfht *session_triggers_ht; | |
114 | /* Used for delayed RCU reclaim. */ | |
115 | struct rcu_head rcu_node; | |
116 | }; | |
117 | ||
ab0ee2ca JG |
118 | struct lttng_trigger_ht_element { |
119 | struct lttng_trigger *trigger; | |
120 | struct cds_lfht_node node; | |
83b934ad MD |
121 | /* call_rcu delayed reclaim. */ |
122 | struct rcu_head rcu_node; | |
ab0ee2ca JG |
123 | }; |
124 | ||
125 | struct lttng_condition_list_element { | |
126 | struct lttng_condition *condition; | |
127 | struct cds_list_head node; | |
128 | }; | |
129 | ||
130 | struct notification_client_list_element { | |
131 | struct notification_client *client; | |
132 | struct cds_list_head node; | |
133 | }; | |
134 | ||
135 | struct notification_client_list { | |
f82f93a1 | 136 | const struct lttng_trigger *trigger; |
ab0ee2ca JG |
137 | struct cds_list_head list; |
138 | struct cds_lfht_node notification_trigger_ht_node; | |
83b934ad MD |
139 | /* call_rcu delayed reclaim. */ |
140 | struct rcu_head rcu_node; | |
ab0ee2ca JG |
141 | }; |
142 | ||
143 | struct notification_client { | |
144 | int socket; | |
145 | /* Client protocol version. */ | |
146 | uint8_t major, minor; | |
147 | uid_t uid; | |
148 | gid_t gid; | |
149 | /* | |
5332364f | 150 | * Indicates if the credentials and versions of the client have been |
ab0ee2ca JG |
151 | * checked. |
152 | */ | |
153 | bool validated; | |
154 | /* | |
155 | * Conditions to which the client's notification channel is subscribed. | |
156 | * List of struct lttng_condition_list_node. The condition member is | |
157 | * owned by the client. | |
158 | */ | |
159 | struct cds_list_head condition_list; | |
160 | struct cds_lfht_node client_socket_ht_node; | |
161 | struct { | |
162 | struct { | |
14fa22f8 JG |
163 | /* |
164 | * During the reception of a message, the reception | |
165 | * buffers' "size" is set to contain the current | |
166 | * message's complete payload. | |
167 | */ | |
ab0ee2ca JG |
168 | struct lttng_dynamic_buffer buffer; |
169 | /* Bytes left to receive for the current message. */ | |
170 | size_t bytes_to_receive; | |
171 | /* Type of the message being received. */ | |
172 | enum lttng_notification_channel_message_type msg_type; | |
173 | /* | |
174 | * Indicates whether or not credentials are expected | |
175 | * from the client. | |
176 | */ | |
01ea340e | 177 | bool expect_creds; |
ab0ee2ca JG |
178 | /* |
179 | * Indicates whether or not credentials were received | |
180 | * from the client. | |
181 | */ | |
182 | bool creds_received; | |
14fa22f8 | 183 | /* Only used during credentials reception. */ |
ab0ee2ca JG |
184 | lttng_sock_cred creds; |
185 | } inbound; | |
186 | struct { | |
187 | /* | |
188 | * Indicates whether or not a notification addressed to | |
189 | * this client was dropped because a command reply was | |
190 | * already buffered. | |
191 | * | |
192 | * A notification is dropped whenever the buffer is not | |
193 | * empty. | |
194 | */ | |
195 | bool dropped_notification; | |
196 | /* | |
197 | * Indicates whether or not a command reply is already | |
198 | * buffered. In this case, it means that the client is | |
199 | * not consuming command replies before emitting a new | |
200 | * one. This could be caused by a protocol error or a | |
201 | * misbehaving/malicious client. | |
202 | */ | |
203 | bool queued_command_reply; | |
204 | struct lttng_dynamic_buffer buffer; | |
205 | } outbound; | |
206 | } communication; | |
83b934ad MD |
207 | /* call_rcu delayed reclaim. */ |
208 | struct rcu_head rcu_node; | |
ab0ee2ca JG |
209 | }; |
210 | ||
211 | struct channel_state_sample { | |
212 | struct channel_key key; | |
213 | struct cds_lfht_node channel_state_ht_node; | |
214 | uint64_t highest_usage; | |
215 | uint64_t lowest_usage; | |
e8360425 | 216 | uint64_t channel_total_consumed; |
83b934ad MD |
217 | /* call_rcu delayed reclaim. */ |
218 | struct rcu_head rcu_node; | |
ab0ee2ca JG |
219 | }; |
220 | ||
e4db5ace | 221 | static unsigned long hash_channel_key(struct channel_key *key); |
ed327204 | 222 | static int evaluate_buffer_condition(const struct lttng_condition *condition, |
e4db5ace | 223 | struct lttng_evaluation **evaluation, |
e8360425 JD |
224 | const struct notification_thread_state *state, |
225 | const struct channel_state_sample *previous_sample, | |
226 | const struct channel_state_sample *latest_sample, | |
227 | uint64_t previous_session_consumed_total, | |
228 | uint64_t latest_session_consumed_total, | |
229 | struct channel_info *channel_info); | |
e4db5ace | 230 | static |
9b63a4aa JG |
231 | int send_evaluation_to_clients(const struct lttng_trigger *trigger, |
232 | const struct lttng_evaluation *evaluation, | |
e4db5ace JR |
233 | struct notification_client_list *client_list, |
234 | struct notification_thread_state *state, | |
235 | uid_t channel_uid, gid_t channel_gid); | |
236 | ||
8abe313a | 237 | |
ea9a44f0 | 238 | /* session_info API */ |
8abe313a JG |
239 | static |
240 | void session_info_destroy(void *_data); | |
241 | static | |
242 | void session_info_get(struct session_info *session_info); | |
243 | static | |
244 | void session_info_put(struct session_info *session_info); | |
245 | static | |
246 | struct session_info *session_info_create(const char *name, | |
ea9a44f0 | 247 | uid_t uid, gid_t gid, |
1eee26c5 JG |
248 | struct lttng_session_trigger_list *trigger_list, |
249 | struct cds_lfht *sessions_ht); | |
8abe313a JG |
250 | static |
251 | void session_info_add_channel(struct session_info *session_info, | |
252 | struct channel_info *channel_info); | |
253 | static | |
254 | void session_info_remove_channel(struct session_info *session_info, | |
255 | struct channel_info *channel_info); | |
256 | ||
ea9a44f0 JG |
257 | /* lttng_session_trigger_list API */ |
258 | static | |
259 | struct lttng_session_trigger_list *lttng_session_trigger_list_create( | |
260 | const char *session_name, | |
261 | struct cds_lfht *session_triggers_ht); | |
262 | static | |
263 | struct lttng_session_trigger_list *lttng_session_trigger_list_build( | |
264 | const struct notification_thread_state *state, | |
265 | const char *session_name); | |
266 | static | |
267 | void lttng_session_trigger_list_destroy( | |
268 | struct lttng_session_trigger_list *list); | |
269 | static | |
270 | int lttng_session_trigger_list_add(struct lttng_session_trigger_list *list, | |
271 | const struct lttng_trigger *trigger); | |
272 | ||
273 | ||
ab0ee2ca JG |
274 | static |
275 | int match_client(struct cds_lfht_node *node, const void *key) | |
276 | { | |
277 | /* This double-cast is intended to supress pointer-to-cast warning. */ | |
278 | int socket = (int) (intptr_t) key; | |
279 | struct notification_client *client; | |
280 | ||
281 | client = caa_container_of(node, struct notification_client, | |
282 | client_socket_ht_node); | |
283 | ||
284 | return !!(client->socket == socket); | |
285 | } | |
286 | ||
287 | static | |
288 | int match_channel_trigger_list(struct cds_lfht_node *node, const void *key) | |
289 | { | |
290 | struct channel_key *channel_key = (struct channel_key *) key; | |
291 | struct lttng_channel_trigger_list *trigger_list; | |
292 | ||
293 | trigger_list = caa_container_of(node, struct lttng_channel_trigger_list, | |
294 | channel_triggers_ht_node); | |
295 | ||
296 | return !!((channel_key->key == trigger_list->channel_key.key) && | |
297 | (channel_key->domain == trigger_list->channel_key.domain)); | |
298 | } | |
299 | ||
51eab943 JG |
300 | static |
301 | int match_session_trigger_list(struct cds_lfht_node *node, const void *key) | |
302 | { | |
303 | const char *session_name = (const char *) key; | |
304 | struct lttng_session_trigger_list *trigger_list; | |
305 | ||
306 | trigger_list = caa_container_of(node, struct lttng_session_trigger_list, | |
307 | session_triggers_ht_node); | |
308 | ||
309 | return !!(strcmp(trigger_list->session_name, session_name) == 0); | |
310 | } | |
311 | ||
ab0ee2ca JG |
312 | static |
313 | int match_channel_state_sample(struct cds_lfht_node *node, const void *key) | |
314 | { | |
315 | struct channel_key *channel_key = (struct channel_key *) key; | |
316 | struct channel_state_sample *sample; | |
317 | ||
318 | sample = caa_container_of(node, struct channel_state_sample, | |
319 | channel_state_ht_node); | |
320 | ||
321 | return !!((channel_key->key == sample->key.key) && | |
322 | (channel_key->domain == sample->key.domain)); | |
323 | } | |
324 | ||
325 | static | |
326 | int match_channel_info(struct cds_lfht_node *node, const void *key) | |
327 | { | |
328 | struct channel_key *channel_key = (struct channel_key *) key; | |
329 | struct channel_info *channel_info; | |
330 | ||
331 | channel_info = caa_container_of(node, struct channel_info, | |
332 | channels_ht_node); | |
333 | ||
334 | return !!((channel_key->key == channel_info->key.key) && | |
335 | (channel_key->domain == channel_info->key.domain)); | |
336 | } | |
337 | ||
338 | static | |
339 | int match_condition(struct cds_lfht_node *node, const void *key) | |
340 | { | |
341 | struct lttng_condition *condition_key = (struct lttng_condition *) key; | |
342 | struct lttng_trigger_ht_element *trigger; | |
343 | struct lttng_condition *condition; | |
344 | ||
345 | trigger = caa_container_of(node, struct lttng_trigger_ht_element, | |
346 | node); | |
347 | condition = lttng_trigger_get_condition(trigger->trigger); | |
348 | assert(condition); | |
349 | ||
350 | return !!lttng_condition_is_equal(condition_key, condition); | |
351 | } | |
352 | ||
ab0ee2ca JG |
353 | static |
354 | int match_client_list_condition(struct cds_lfht_node *node, const void *key) | |
355 | { | |
356 | struct lttng_condition *condition_key = (struct lttng_condition *) key; | |
357 | struct notification_client_list *client_list; | |
f82f93a1 | 358 | const struct lttng_condition *condition; |
ab0ee2ca JG |
359 | |
360 | assert(condition_key); | |
361 | ||
362 | client_list = caa_container_of(node, struct notification_client_list, | |
363 | notification_trigger_ht_node); | |
f82f93a1 | 364 | condition = lttng_trigger_get_const_condition(client_list->trigger); |
ab0ee2ca JG |
365 | |
366 | return !!lttng_condition_is_equal(condition_key, condition); | |
367 | } | |
368 | ||
f82f93a1 JG |
369 | static |
370 | int match_session(struct cds_lfht_node *node, const void *key) | |
371 | { | |
372 | const char *name = key; | |
373 | struct session_info *session_info = caa_container_of( | |
374 | node, struct session_info, sessions_ht_node); | |
375 | ||
376 | return !strcmp(session_info->name, name); | |
377 | } | |
378 | ||
ab0ee2ca JG |
379 | static |
380 | unsigned long lttng_condition_buffer_usage_hash( | |
9b63a4aa | 381 | const struct lttng_condition *_condition) |
ab0ee2ca | 382 | { |
9a2746aa JG |
383 | unsigned long hash; |
384 | unsigned long condition_type; | |
ab0ee2ca JG |
385 | struct lttng_condition_buffer_usage *condition; |
386 | ||
387 | condition = container_of(_condition, | |
388 | struct lttng_condition_buffer_usage, parent); | |
389 | ||
9a2746aa JG |
390 | condition_type = (unsigned long) condition->parent.type; |
391 | hash = hash_key_ulong((void *) condition_type, lttng_ht_seed); | |
ab0ee2ca JG |
392 | if (condition->session_name) { |
393 | hash ^= hash_key_str(condition->session_name, lttng_ht_seed); | |
394 | } | |
395 | if (condition->channel_name) { | |
8f56701f | 396 | hash ^= hash_key_str(condition->channel_name, lttng_ht_seed); |
ab0ee2ca JG |
397 | } |
398 | if (condition->domain.set) { | |
399 | hash ^= hash_key_ulong( | |
400 | (void *) condition->domain.type, | |
401 | lttng_ht_seed); | |
402 | } | |
403 | if (condition->threshold_ratio.set) { | |
404 | uint64_t val; | |
405 | ||
406 | val = condition->threshold_ratio.value * (double) UINT32_MAX; | |
407 | hash ^= hash_key_u64(&val, lttng_ht_seed); | |
6633b0dd | 408 | } else if (condition->threshold_bytes.set) { |
ab0ee2ca JG |
409 | uint64_t val; |
410 | ||
411 | val = condition->threshold_bytes.value; | |
412 | hash ^= hash_key_u64(&val, lttng_ht_seed); | |
413 | } | |
414 | return hash; | |
415 | } | |
416 | ||
e8360425 JD |
417 | static |
418 | unsigned long lttng_condition_session_consumed_size_hash( | |
9b63a4aa | 419 | const struct lttng_condition *_condition) |
e8360425 | 420 | { |
9a2746aa JG |
421 | unsigned long hash; |
422 | unsigned long condition_type = | |
423 | (unsigned long) LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE; | |
e8360425 JD |
424 | struct lttng_condition_session_consumed_size *condition; |
425 | uint64_t val; | |
426 | ||
427 | condition = container_of(_condition, | |
428 | struct lttng_condition_session_consumed_size, parent); | |
429 | ||
9a2746aa | 430 | hash = hash_key_ulong((void *) condition_type, lttng_ht_seed); |
e8360425 JD |
431 | if (condition->session_name) { |
432 | hash ^= hash_key_str(condition->session_name, lttng_ht_seed); | |
433 | } | |
434 | val = condition->consumed_threshold_bytes.value; | |
435 | hash ^= hash_key_u64(&val, lttng_ht_seed); | |
436 | return hash; | |
437 | } | |
438 | ||
a8393880 JG |
439 | static |
440 | unsigned long lttng_condition_session_rotation_hash( | |
441 | const struct lttng_condition *_condition) | |
442 | { | |
443 | unsigned long hash, condition_type; | |
444 | struct lttng_condition_session_rotation *condition; | |
445 | ||
446 | condition = container_of(_condition, | |
447 | struct lttng_condition_session_rotation, parent); | |
448 | condition_type = (unsigned long) condition->parent.type; | |
449 | hash = hash_key_ulong((void *) condition_type, lttng_ht_seed); | |
450 | assert(condition->session_name); | |
451 | hash ^= hash_key_str(condition->session_name, lttng_ht_seed); | |
452 | return hash; | |
453 | } | |
9a2746aa | 454 | |
ab0ee2ca JG |
455 | /* |
456 | * The lttng_condition hashing code is kept in this file (rather than | |
457 | * condition.c) since it makes use of GPLv2 code (hashtable utils), which we | |
458 | * don't want to link in liblttng-ctl. | |
459 | */ | |
460 | static | |
9b63a4aa | 461 | unsigned long lttng_condition_hash(const struct lttng_condition *condition) |
ab0ee2ca JG |
462 | { |
463 | switch (condition->type) { | |
464 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
465 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
466 | return lttng_condition_buffer_usage_hash(condition); | |
e8360425 JD |
467 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: |
468 | return lttng_condition_session_consumed_size_hash(condition); | |
a8393880 JG |
469 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: |
470 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
471 | return lttng_condition_session_rotation_hash(condition); | |
ab0ee2ca JG |
472 | default: |
473 | ERR("[notification-thread] Unexpected condition type caught"); | |
474 | abort(); | |
475 | } | |
476 | } | |
477 | ||
e4db5ace JR |
478 | static |
479 | unsigned long hash_channel_key(struct channel_key *key) | |
480 | { | |
481 | unsigned long key_hash = hash_key_u64(&key->key, lttng_ht_seed); | |
482 | unsigned long domain_hash = hash_key_ulong( | |
483 | (void *) (unsigned long) key->domain, lttng_ht_seed); | |
484 | ||
485 | return key_hash ^ domain_hash; | |
486 | } | |
487 | ||
f82f93a1 JG |
488 | /* |
489 | * Get the type of object to which a given condition applies. Bindings let | |
490 | * the notification system evaluate a trigger's condition when a given | |
491 | * object's state is updated. | |
492 | * | |
493 | * For instance, a condition bound to a channel will be evaluated everytime | |
494 | * the channel's state is changed by a channel monitoring sample. | |
495 | */ | |
496 | static | |
497 | enum lttng_object_type get_condition_binding_object( | |
498 | const struct lttng_condition *condition) | |
499 | { | |
500 | switch (lttng_condition_get_type(condition)) { | |
501 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
502 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
503 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: | |
504 | return LTTNG_OBJECT_TYPE_CHANNEL; | |
505 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: | |
506 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
507 | return LTTNG_OBJECT_TYPE_SESSION; | |
508 | default: | |
509 | return LTTNG_OBJECT_TYPE_UNKNOWN; | |
510 | } | |
511 | } | |
512 | ||
83b934ad MD |
513 | static |
514 | void free_channel_info_rcu(struct rcu_head *node) | |
515 | { | |
516 | free(caa_container_of(node, struct channel_info, rcu_node)); | |
517 | } | |
518 | ||
ab0ee2ca JG |
519 | static |
520 | void channel_info_destroy(struct channel_info *channel_info) | |
521 | { | |
522 | if (!channel_info) { | |
523 | return; | |
524 | } | |
525 | ||
8abe313a JG |
526 | if (channel_info->session_info) { |
527 | session_info_remove_channel(channel_info->session_info, | |
528 | channel_info); | |
529 | session_info_put(channel_info->session_info); | |
ab0ee2ca | 530 | } |
8abe313a JG |
531 | if (channel_info->name) { |
532 | free(channel_info->name); | |
ab0ee2ca | 533 | } |
83b934ad MD |
534 | call_rcu(&channel_info->rcu_node, free_channel_info_rcu); |
535 | } | |
536 | ||
537 | static | |
538 | void free_session_info_rcu(struct rcu_head *node) | |
539 | { | |
540 | free(caa_container_of(node, struct session_info, rcu_node)); | |
ab0ee2ca JG |
541 | } |
542 | ||
8abe313a | 543 | /* Don't call directly, use the ref-counting mechanism. */ |
ab0ee2ca | 544 | static |
8abe313a | 545 | void session_info_destroy(void *_data) |
ab0ee2ca | 546 | { |
8abe313a | 547 | struct session_info *session_info = _data; |
3b68d0a3 | 548 | int ret; |
ab0ee2ca | 549 | |
8abe313a JG |
550 | assert(session_info); |
551 | if (session_info->channel_infos_ht) { | |
3b68d0a3 JR |
552 | ret = cds_lfht_destroy(session_info->channel_infos_ht, NULL); |
553 | if (ret) { | |
4c3f302b | 554 | ERR("[notification-thread] Failed to destroy channel information hash table"); |
3b68d0a3 | 555 | } |
8abe313a | 556 | } |
ea9a44f0 | 557 | lttng_session_trigger_list_destroy(session_info->trigger_list); |
1eee26c5 JG |
558 | |
559 | rcu_read_lock(); | |
560 | cds_lfht_del(session_info->sessions_ht, | |
561 | &session_info->sessions_ht_node); | |
562 | rcu_read_unlock(); | |
8abe313a | 563 | free(session_info->name); |
83b934ad | 564 | call_rcu(&session_info->rcu_node, free_session_info_rcu); |
8abe313a | 565 | } |
ab0ee2ca | 566 | |
8abe313a JG |
567 | static |
568 | void session_info_get(struct session_info *session_info) | |
569 | { | |
570 | if (!session_info) { | |
571 | return; | |
572 | } | |
573 | lttng_ref_get(&session_info->ref); | |
574 | } | |
575 | ||
576 | static | |
577 | void session_info_put(struct session_info *session_info) | |
578 | { | |
579 | if (!session_info) { | |
580 | return; | |
ab0ee2ca | 581 | } |
8abe313a JG |
582 | lttng_ref_put(&session_info->ref); |
583 | } | |
584 | ||
585 | static | |
ea9a44f0 | 586 | struct session_info *session_info_create(const char *name, uid_t uid, gid_t gid, |
1eee26c5 JG |
587 | struct lttng_session_trigger_list *trigger_list, |
588 | struct cds_lfht *sessions_ht) | |
8abe313a JG |
589 | { |
590 | struct session_info *session_info; | |
ab0ee2ca | 591 | |
8abe313a | 592 | assert(name); |
ab0ee2ca | 593 | |
8abe313a JG |
594 | session_info = zmalloc(sizeof(*session_info)); |
595 | if (!session_info) { | |
596 | goto end; | |
597 | } | |
598 | lttng_ref_init(&session_info->ref, session_info_destroy); | |
599 | ||
600 | session_info->channel_infos_ht = cds_lfht_new(DEFAULT_HT_SIZE, | |
601 | 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL); | |
602 | if (!session_info->channel_infos_ht) { | |
ab0ee2ca JG |
603 | goto error; |
604 | } | |
8abe313a JG |
605 | |
606 | cds_lfht_node_init(&session_info->sessions_ht_node); | |
607 | session_info->name = strdup(name); | |
608 | if (!session_info->name) { | |
ab0ee2ca JG |
609 | goto error; |
610 | } | |
8abe313a JG |
611 | session_info->uid = uid; |
612 | session_info->gid = gid; | |
ea9a44f0 | 613 | session_info->trigger_list = trigger_list; |
1eee26c5 | 614 | session_info->sessions_ht = sessions_ht; |
8abe313a JG |
615 | end: |
616 | return session_info; | |
617 | error: | |
618 | session_info_put(session_info); | |
619 | return NULL; | |
620 | } | |
621 | ||
622 | static | |
623 | void session_info_add_channel(struct session_info *session_info, | |
624 | struct channel_info *channel_info) | |
625 | { | |
626 | rcu_read_lock(); | |
627 | cds_lfht_add(session_info->channel_infos_ht, | |
628 | hash_channel_key(&channel_info->key), | |
629 | &channel_info->session_info_channels_ht_node); | |
630 | rcu_read_unlock(); | |
631 | } | |
632 | ||
633 | static | |
634 | void session_info_remove_channel(struct session_info *session_info, | |
635 | struct channel_info *channel_info) | |
636 | { | |
637 | rcu_read_lock(); | |
638 | cds_lfht_del(session_info->channel_infos_ht, | |
639 | &channel_info->session_info_channels_ht_node); | |
640 | rcu_read_unlock(); | |
641 | } | |
642 | ||
643 | static | |
644 | struct channel_info *channel_info_create(const char *channel_name, | |
645 | struct channel_key *channel_key, uint64_t channel_capacity, | |
646 | struct session_info *session_info) | |
647 | { | |
648 | struct channel_info *channel_info = zmalloc(sizeof(*channel_info)); | |
649 | ||
650 | if (!channel_info) { | |
651 | goto end; | |
652 | } | |
653 | ||
ab0ee2ca | 654 | cds_lfht_node_init(&channel_info->channels_ht_node); |
8abe313a JG |
655 | cds_lfht_node_init(&channel_info->session_info_channels_ht_node); |
656 | memcpy(&channel_info->key, channel_key, sizeof(*channel_key)); | |
657 | channel_info->capacity = channel_capacity; | |
658 | ||
659 | channel_info->name = strdup(channel_name); | |
660 | if (!channel_info->name) { | |
661 | goto error; | |
662 | } | |
663 | ||
664 | /* | |
665 | * Set the references between session and channel infos: | |
666 | * - channel_info holds a strong reference to session_info | |
667 | * - session_info holds a weak reference to channel_info | |
668 | */ | |
669 | session_info_get(session_info); | |
670 | session_info_add_channel(session_info, channel_info); | |
671 | channel_info->session_info = session_info; | |
ab0ee2ca | 672 | end: |
8abe313a | 673 | return channel_info; |
ab0ee2ca | 674 | error: |
8abe313a | 675 | channel_info_destroy(channel_info); |
ab0ee2ca JG |
676 | return NULL; |
677 | } | |
678 | ||
ed327204 JG |
679 | /* RCU read lock must be held by the caller. */ |
680 | static | |
681 | struct notification_client_list *get_client_list_from_condition( | |
682 | struct notification_thread_state *state, | |
683 | const struct lttng_condition *condition) | |
684 | { | |
685 | struct cds_lfht_node *node; | |
686 | struct cds_lfht_iter iter; | |
687 | ||
688 | cds_lfht_lookup(state->notification_trigger_clients_ht, | |
689 | lttng_condition_hash(condition), | |
690 | match_client_list_condition, | |
691 | condition, | |
692 | &iter); | |
693 | node = cds_lfht_iter_get_node(&iter); | |
694 | ||
695 | return node ? caa_container_of(node, | |
696 | struct notification_client_list, | |
697 | notification_trigger_ht_node) : NULL; | |
698 | } | |
699 | ||
e4db5ace JR |
700 | /* This function must be called with the RCU read lock held. */ |
701 | static | |
f82f93a1 JG |
702 | int evaluate_channel_condition_for_client( |
703 | const struct lttng_condition *condition, | |
704 | struct notification_thread_state *state, | |
705 | struct lttng_evaluation **evaluation, | |
706 | uid_t *session_uid, gid_t *session_gid) | |
e4db5ace JR |
707 | { |
708 | int ret; | |
709 | struct cds_lfht_iter iter; | |
710 | struct cds_lfht_node *node; | |
711 | struct channel_info *channel_info = NULL; | |
712 | struct channel_key *channel_key = NULL; | |
713 | struct channel_state_sample *last_sample = NULL; | |
714 | struct lttng_channel_trigger_list *channel_trigger_list = NULL; | |
e4db5ace | 715 | |
f82f93a1 | 716 | /* Find the channel associated with the condition. */ |
e4db5ace | 717 | cds_lfht_for_each_entry(state->channel_triggers_ht, &iter, |
f82f93a1 | 718 | channel_trigger_list, channel_triggers_ht_node) { |
e4db5ace JR |
719 | struct lttng_trigger_list_element *element; |
720 | ||
721 | cds_list_for_each_entry(element, &channel_trigger_list->list, node) { | |
9b63a4aa | 722 | const struct lttng_condition *current_condition = |
f82f93a1 | 723 | lttng_trigger_get_const_condition( |
e4db5ace JR |
724 | element->trigger); |
725 | ||
726 | assert(current_condition); | |
727 | if (!lttng_condition_is_equal(condition, | |
2ae99f0b | 728 | current_condition)) { |
e4db5ace JR |
729 | continue; |
730 | } | |
731 | ||
732 | /* Found the trigger, save the channel key. */ | |
733 | channel_key = &channel_trigger_list->channel_key; | |
734 | break; | |
735 | } | |
736 | if (channel_key) { | |
737 | /* The channel key was found stop iteration. */ | |
738 | break; | |
739 | } | |
740 | } | |
741 | ||
742 | if (!channel_key){ | |
743 | /* No channel found; normal exit. */ | |
f82f93a1 | 744 | DBG("[notification-thread] No known channel associated with newly subscribed-to condition"); |
e4db5ace JR |
745 | ret = 0; |
746 | goto end; | |
747 | } | |
748 | ||
749 | /* Fetch channel info for the matching channel. */ | |
750 | cds_lfht_lookup(state->channels_ht, | |
751 | hash_channel_key(channel_key), | |
752 | match_channel_info, | |
753 | channel_key, | |
754 | &iter); | |
755 | node = cds_lfht_iter_get_node(&iter); | |
756 | assert(node); | |
757 | channel_info = caa_container_of(node, struct channel_info, | |
758 | channels_ht_node); | |
759 | ||
760 | /* Retrieve the channel's last sample, if it exists. */ | |
761 | cds_lfht_lookup(state->channel_state_ht, | |
762 | hash_channel_key(channel_key), | |
763 | match_channel_state_sample, | |
764 | channel_key, | |
765 | &iter); | |
766 | node = cds_lfht_iter_get_node(&iter); | |
767 | if (node) { | |
768 | last_sample = caa_container_of(node, | |
769 | struct channel_state_sample, | |
770 | channel_state_ht_node); | |
771 | } else { | |
772 | /* Nothing to evaluate, no sample was ever taken. Normal exit */ | |
773 | DBG("[notification-thread] No channel sample associated with newly subscribed-to condition"); | |
774 | ret = 0; | |
775 | goto end; | |
776 | } | |
777 | ||
f82f93a1 | 778 | ret = evaluate_buffer_condition(condition, evaluation, state, |
e8360425 JD |
779 | NULL, last_sample, |
780 | 0, channel_info->session_info->consumed_data_size, | |
781 | channel_info); | |
e4db5ace | 782 | if (ret) { |
066a3c82 | 783 | WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition"); |
e4db5ace JR |
784 | goto end; |
785 | } | |
786 | ||
f82f93a1 JG |
787 | *session_uid = channel_info->session_info->uid; |
788 | *session_gid = channel_info->session_info->gid; | |
789 | end: | |
790 | return ret; | |
791 | } | |
792 | ||
793 | static | |
794 | const char *get_condition_session_name(const struct lttng_condition *condition) | |
795 | { | |
796 | const char *session_name = NULL; | |
797 | enum lttng_condition_status status; | |
798 | ||
799 | switch (lttng_condition_get_type(condition)) { | |
800 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
801 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
802 | status = lttng_condition_buffer_usage_get_session_name( | |
803 | condition, &session_name); | |
804 | break; | |
805 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: | |
806 | status = lttng_condition_session_consumed_size_get_session_name( | |
807 | condition, &session_name); | |
808 | break; | |
809 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: | |
810 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
811 | status = lttng_condition_session_rotation_get_session_name( | |
812 | condition, &session_name); | |
813 | break; | |
814 | default: | |
815 | abort(); | |
816 | } | |
817 | if (status != LTTNG_CONDITION_STATUS_OK) { | |
818 | ERR("[notification-thread] Failed to retrieve session rotation condition's session name"); | |
819 | goto end; | |
820 | } | |
821 | end: | |
822 | return session_name; | |
823 | } | |
824 | ||
825 | /* This function must be called with the RCU read lock held. */ | |
826 | static | |
827 | int evaluate_session_condition_for_client( | |
828 | const struct lttng_condition *condition, | |
829 | struct notification_thread_state *state, | |
830 | struct lttng_evaluation **evaluation, | |
831 | uid_t *session_uid, gid_t *session_gid) | |
832 | { | |
833 | int ret; | |
834 | struct cds_lfht_iter iter; | |
835 | struct cds_lfht_node *node; | |
836 | const char *session_name; | |
837 | struct session_info *session_info = NULL; | |
838 | ||
839 | session_name = get_condition_session_name(condition); | |
840 | ||
841 | /* Find the session associated with the trigger. */ | |
842 | cds_lfht_lookup(state->sessions_ht, | |
843 | hash_key_str(session_name, lttng_ht_seed), | |
844 | match_session, | |
845 | session_name, | |
846 | &iter); | |
847 | node = cds_lfht_iter_get_node(&iter); | |
848 | if (!node) { | |
849 | DBG("[notification-thread] No known session matching name \"%s\"", | |
850 | session_name); | |
851 | ret = 0; | |
852 | goto end; | |
853 | } | |
854 | ||
855 | session_info = caa_container_of(node, struct session_info, | |
856 | sessions_ht_node); | |
857 | session_info_get(session_info); | |
858 | ||
859 | /* | |
860 | * Evaluation is performed in-line here since only one type of | |
861 | * session-bound condition is handled for the moment. | |
862 | */ | |
863 | switch (lttng_condition_get_type(condition)) { | |
864 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: | |
865 | if (!session_info->rotation.ongoing) { | |
be558f88 | 866 | ret = 0; |
f82f93a1 JG |
867 | goto end_session_put; |
868 | } | |
869 | ||
870 | *evaluation = lttng_evaluation_session_rotation_ongoing_create( | |
871 | session_info->rotation.id); | |
872 | if (!*evaluation) { | |
873 | /* Fatal error. */ | |
874 | ERR("[notification-thread] Failed to create session rotation ongoing evaluation for session \"%s\"", | |
875 | session_info->name); | |
876 | ret = -1; | |
877 | goto end_session_put; | |
878 | } | |
879 | ret = 0; | |
880 | break; | |
881 | default: | |
882 | ret = 0; | |
883 | goto end_session_put; | |
884 | } | |
885 | ||
886 | *session_uid = session_info->uid; | |
887 | *session_gid = session_info->gid; | |
888 | ||
889 | end_session_put: | |
890 | session_info_put(session_info); | |
891 | end: | |
892 | return ret; | |
893 | } | |
894 | ||
895 | /* This function must be called with the RCU read lock held. */ | |
896 | static | |
897 | int evaluate_condition_for_client(const struct lttng_trigger *trigger, | |
898 | const struct lttng_condition *condition, | |
899 | struct notification_client *client, | |
900 | struct notification_thread_state *state) | |
901 | { | |
902 | int ret; | |
903 | struct lttng_evaluation *evaluation = NULL; | |
904 | struct notification_client_list client_list = { 0 }; | |
905 | struct notification_client_list_element client_list_element = { 0 }; | |
906 | uid_t object_uid = 0; | |
907 | gid_t object_gid = 0; | |
908 | ||
909 | assert(trigger); | |
910 | assert(condition); | |
911 | assert(client); | |
912 | assert(state); | |
913 | ||
914 | switch (get_condition_binding_object(condition)) { | |
915 | case LTTNG_OBJECT_TYPE_SESSION: | |
916 | ret = evaluate_session_condition_for_client(condition, state, | |
917 | &evaluation, &object_uid, &object_gid); | |
918 | break; | |
919 | case LTTNG_OBJECT_TYPE_CHANNEL: | |
920 | ret = evaluate_channel_condition_for_client(condition, state, | |
921 | &evaluation, &object_uid, &object_gid); | |
922 | break; | |
923 | case LTTNG_OBJECT_TYPE_NONE: | |
924 | ret = 0; | |
925 | goto end; | |
926 | case LTTNG_OBJECT_TYPE_UNKNOWN: | |
927 | default: | |
928 | ret = -1; | |
929 | goto end; | |
930 | } | |
931 | ||
e4db5ace JR |
932 | if (!evaluation) { |
933 | /* Evaluation yielded nothing. Normal exit. */ | |
934 | DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client"); | |
935 | ret = 0; | |
936 | goto end; | |
937 | } | |
938 | ||
939 | /* | |
940 | * Create a temporary client list with the client currently | |
941 | * subscribing. | |
942 | */ | |
943 | cds_lfht_node_init(&client_list.notification_trigger_ht_node); | |
944 | CDS_INIT_LIST_HEAD(&client_list.list); | |
945 | client_list.trigger = trigger; | |
946 | ||
947 | CDS_INIT_LIST_HEAD(&client_list_element.node); | |
948 | client_list_element.client = client; | |
949 | cds_list_add(&client_list_element.node, &client_list.list); | |
950 | ||
951 | /* Send evaluation result to the newly-subscribed client. */ | |
952 | DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client"); | |
953 | ret = send_evaluation_to_clients(trigger, evaluation, &client_list, | |
f82f93a1 | 954 | state, object_uid, object_gid); |
e4db5ace JR |
955 | |
956 | end: | |
957 | return ret; | |
958 | } | |
959 | ||
ab0ee2ca JG |
960 | static |
961 | int notification_thread_client_subscribe(struct notification_client *client, | |
962 | struct lttng_condition *condition, | |
963 | struct notification_thread_state *state, | |
964 | enum lttng_notification_channel_status *_status) | |
965 | { | |
966 | int ret = 0; | |
ab0ee2ca JG |
967 | struct notification_client_list *client_list; |
968 | struct lttng_condition_list_element *condition_list_element = NULL; | |
969 | struct notification_client_list_element *client_list_element = NULL; | |
970 | enum lttng_notification_channel_status status = | |
971 | LTTNG_NOTIFICATION_CHANNEL_STATUS_OK; | |
972 | ||
973 | /* | |
974 | * Ensure that the client has not already subscribed to this condition | |
975 | * before. | |
976 | */ | |
977 | cds_list_for_each_entry(condition_list_element, &client->condition_list, node) { | |
978 | if (lttng_condition_is_equal(condition_list_element->condition, | |
979 | condition)) { | |
980 | status = LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED; | |
981 | goto end; | |
982 | } | |
983 | } | |
984 | ||
985 | condition_list_element = zmalloc(sizeof(*condition_list_element)); | |
986 | if (!condition_list_element) { | |
987 | ret = -1; | |
988 | goto error; | |
989 | } | |
990 | client_list_element = zmalloc(sizeof(*client_list_element)); | |
991 | if (!client_list_element) { | |
992 | ret = -1; | |
993 | goto error; | |
994 | } | |
995 | ||
996 | rcu_read_lock(); | |
997 | ||
998 | /* | |
999 | * Add the newly-subscribed condition to the client's subscription list. | |
1000 | */ | |
1001 | CDS_INIT_LIST_HEAD(&condition_list_element->node); | |
1002 | condition_list_element->condition = condition; | |
1003 | cds_list_add(&condition_list_element->node, &client->condition_list); | |
1004 | ||
ed327204 JG |
1005 | client_list = get_client_list_from_condition(state, condition); |
1006 | if (!client_list) { | |
2ae99f0b JG |
1007 | /* |
1008 | * No notification-emiting trigger registered with this | |
1009 | * condition. We don't evaluate the condition right away | |
1010 | * since this trigger is not registered yet. | |
1011 | */ | |
4fb43b68 | 1012 | free(client_list_element); |
ab0ee2ca JG |
1013 | goto end_unlock; |
1014 | } | |
1015 | ||
2ae99f0b JG |
1016 | /* |
1017 | * The condition to which the client just subscribed is evaluated | |
1018 | * at this point so that conditions that are already TRUE result | |
1019 | * in a notification being sent out. | |
1020 | */ | |
e4db5ace JR |
1021 | if (evaluate_condition_for_client(client_list->trigger, condition, |
1022 | client, state)) { | |
1023 | WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting."); | |
1024 | ret = -1; | |
4bd5b4af | 1025 | free(client_list_element); |
e4db5ace JR |
1026 | goto end_unlock; |
1027 | } | |
1028 | ||
1029 | /* | |
1030 | * Add the client to the list of clients interested in a given trigger | |
1031 | * if a "notification" trigger with a corresponding condition was | |
1032 | * added prior. | |
1033 | */ | |
ab0ee2ca JG |
1034 | client_list_element->client = client; |
1035 | CDS_INIT_LIST_HEAD(&client_list_element->node); | |
1036 | cds_list_add(&client_list_element->node, &client_list->list); | |
1037 | end_unlock: | |
1038 | rcu_read_unlock(); | |
1039 | end: | |
1040 | if (_status) { | |
1041 | *_status = status; | |
1042 | } | |
1043 | return ret; | |
1044 | error: | |
1045 | free(condition_list_element); | |
1046 | free(client_list_element); | |
1047 | return ret; | |
1048 | } | |
1049 | ||
1050 | static | |
1051 | int notification_thread_client_unsubscribe( | |
1052 | struct notification_client *client, | |
1053 | struct lttng_condition *condition, | |
1054 | struct notification_thread_state *state, | |
1055 | enum lttng_notification_channel_status *_status) | |
1056 | { | |
ab0ee2ca JG |
1057 | struct notification_client_list *client_list; |
1058 | struct lttng_condition_list_element *condition_list_element, | |
1059 | *condition_tmp; | |
1060 | struct notification_client_list_element *client_list_element, | |
1061 | *client_tmp; | |
1062 | bool condition_found = false; | |
1063 | enum lttng_notification_channel_status status = | |
1064 | LTTNG_NOTIFICATION_CHANNEL_STATUS_OK; | |
1065 | ||
1066 | /* Remove the condition from the client's condition list. */ | |
1067 | cds_list_for_each_entry_safe(condition_list_element, condition_tmp, | |
1068 | &client->condition_list, node) { | |
1069 | if (!lttng_condition_is_equal(condition_list_element->condition, | |
1070 | condition)) { | |
1071 | continue; | |
1072 | } | |
1073 | ||
1074 | cds_list_del(&condition_list_element->node); | |
1075 | /* | |
1076 | * The caller may be iterating on the client's conditions to | |
1077 | * tear down a client's connection. In this case, the condition | |
1078 | * will be destroyed at the end. | |
1079 | */ | |
1080 | if (condition != condition_list_element->condition) { | |
1081 | lttng_condition_destroy( | |
1082 | condition_list_element->condition); | |
1083 | } | |
1084 | free(condition_list_element); | |
1085 | condition_found = true; | |
1086 | break; | |
1087 | } | |
1088 | ||
1089 | if (!condition_found) { | |
1090 | status = LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION; | |
1091 | goto end; | |
1092 | } | |
1093 | ||
1094 | /* | |
1095 | * Remove the client from the list of clients interested the trigger | |
1096 | * matching the condition. | |
1097 | */ | |
1098 | rcu_read_lock(); | |
ed327204 JG |
1099 | client_list = get_client_list_from_condition(state, condition); |
1100 | if (!client_list) { | |
ab0ee2ca JG |
1101 | goto end_unlock; |
1102 | } | |
1103 | ||
ab0ee2ca JG |
1104 | cds_list_for_each_entry_safe(client_list_element, client_tmp, |
1105 | &client_list->list, node) { | |
1106 | if (client_list_element->client->socket != client->socket) { | |
1107 | continue; | |
1108 | } | |
1109 | cds_list_del(&client_list_element->node); | |
1110 | free(client_list_element); | |
1111 | break; | |
1112 | } | |
1113 | end_unlock: | |
1114 | rcu_read_unlock(); | |
1115 | end: | |
1116 | lttng_condition_destroy(condition); | |
1117 | if (_status) { | |
1118 | *_status = status; | |
1119 | } | |
1120 | return 0; | |
1121 | } | |
1122 | ||
83b934ad MD |
1123 | static |
1124 | void free_notification_client_rcu(struct rcu_head *node) | |
1125 | { | |
1126 | free(caa_container_of(node, struct notification_client, rcu_node)); | |
1127 | } | |
1128 | ||
ab0ee2ca JG |
1129 | static |
1130 | void notification_client_destroy(struct notification_client *client, | |
1131 | struct notification_thread_state *state) | |
1132 | { | |
1133 | struct lttng_condition_list_element *condition_list_element, *tmp; | |
1134 | ||
1135 | if (!client) { | |
1136 | return; | |
1137 | } | |
1138 | ||
1139 | /* Release all conditions to which the client was subscribed. */ | |
1140 | cds_list_for_each_entry_safe(condition_list_element, tmp, | |
1141 | &client->condition_list, node) { | |
1142 | (void) notification_thread_client_unsubscribe(client, | |
1143 | condition_list_element->condition, state, NULL); | |
1144 | } | |
1145 | ||
1146 | if (client->socket >= 0) { | |
1147 | (void) lttcomm_close_unix_sock(client->socket); | |
1148 | } | |
1149 | lttng_dynamic_buffer_reset(&client->communication.inbound.buffer); | |
1150 | lttng_dynamic_buffer_reset(&client->communication.outbound.buffer); | |
83b934ad | 1151 | call_rcu(&client->rcu_node, free_notification_client_rcu); |
ab0ee2ca JG |
1152 | } |
1153 | ||
1154 | /* | |
1155 | * Call with rcu_read_lock held (and hold for the lifetime of the returned | |
1156 | * client pointer). | |
1157 | */ | |
1158 | static | |
1159 | struct notification_client *get_client_from_socket(int socket, | |
1160 | struct notification_thread_state *state) | |
1161 | { | |
1162 | struct cds_lfht_iter iter; | |
1163 | struct cds_lfht_node *node; | |
1164 | struct notification_client *client = NULL; | |
1165 | ||
1166 | cds_lfht_lookup(state->client_socket_ht, | |
1167 | hash_key_ulong((void *) (unsigned long) socket, lttng_ht_seed), | |
1168 | match_client, | |
1169 | (void *) (unsigned long) socket, | |
1170 | &iter); | |
1171 | node = cds_lfht_iter_get_node(&iter); | |
1172 | if (!node) { | |
1173 | goto end; | |
1174 | } | |
1175 | ||
1176 | client = caa_container_of(node, struct notification_client, | |
1177 | client_socket_ht_node); | |
1178 | end: | |
1179 | return client; | |
1180 | } | |
1181 | ||
1182 | static | |
e8360425 | 1183 | bool buffer_usage_condition_applies_to_channel( |
51eab943 JG |
1184 | const struct lttng_condition *condition, |
1185 | const struct channel_info *channel_info) | |
ab0ee2ca JG |
1186 | { |
1187 | enum lttng_condition_status status; | |
e8360425 JD |
1188 | enum lttng_domain_type condition_domain; |
1189 | const char *condition_session_name = NULL; | |
1190 | const char *condition_channel_name = NULL; | |
ab0ee2ca | 1191 | |
e8360425 JD |
1192 | status = lttng_condition_buffer_usage_get_domain_type(condition, |
1193 | &condition_domain); | |
1194 | assert(status == LTTNG_CONDITION_STATUS_OK); | |
1195 | if (channel_info->key.domain != condition_domain) { | |
ab0ee2ca JG |
1196 | goto fail; |
1197 | } | |
1198 | ||
e8360425 JD |
1199 | status = lttng_condition_buffer_usage_get_session_name( |
1200 | condition, &condition_session_name); | |
1201 | assert((status == LTTNG_CONDITION_STATUS_OK) && condition_session_name); | |
1202 | ||
1203 | status = lttng_condition_buffer_usage_get_channel_name( | |
1204 | condition, &condition_channel_name); | |
1205 | assert((status == LTTNG_CONDITION_STATUS_OK) && condition_channel_name); | |
1206 | ||
1207 | if (strcmp(channel_info->session_info->name, condition_session_name)) { | |
1208 | goto fail; | |
1209 | } | |
1210 | if (strcmp(channel_info->name, condition_channel_name)) { | |
ab0ee2ca JG |
1211 | goto fail; |
1212 | } | |
1213 | ||
e8360425 JD |
1214 | return true; |
1215 | fail: | |
1216 | return false; | |
1217 | } | |
1218 | ||
1219 | static | |
1220 | bool session_consumed_size_condition_applies_to_channel( | |
51eab943 JG |
1221 | const struct lttng_condition *condition, |
1222 | const struct channel_info *channel_info) | |
e8360425 JD |
1223 | { |
1224 | enum lttng_condition_status status; | |
1225 | const char *condition_session_name = NULL; | |
1226 | ||
1227 | status = lttng_condition_session_consumed_size_get_session_name( | |
1228 | condition, &condition_session_name); | |
1229 | assert((status == LTTNG_CONDITION_STATUS_OK) && condition_session_name); | |
1230 | ||
1231 | if (strcmp(channel_info->session_info->name, condition_session_name)) { | |
ab0ee2ca JG |
1232 | goto fail; |
1233 | } | |
1234 | ||
e8360425 JD |
1235 | return true; |
1236 | fail: | |
1237 | return false; | |
1238 | } | |
ab0ee2ca | 1239 | |
51eab943 JG |
1240 | static |
1241 | bool trigger_applies_to_channel(const struct lttng_trigger *trigger, | |
1242 | const struct channel_info *channel_info) | |
1243 | { | |
1244 | const struct lttng_condition *condition; | |
e8360425 | 1245 | bool trigger_applies; |
ab0ee2ca | 1246 | |
51eab943 | 1247 | condition = lttng_trigger_get_const_condition(trigger); |
e8360425 | 1248 | if (!condition) { |
ab0ee2ca JG |
1249 | goto fail; |
1250 | } | |
e8360425 JD |
1251 | |
1252 | switch (lttng_condition_get_type(condition)) { | |
1253 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
1254 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
1255 | trigger_applies = buffer_usage_condition_applies_to_channel( | |
1256 | condition, channel_info); | |
1257 | break; | |
1258 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: | |
1259 | trigger_applies = session_consumed_size_condition_applies_to_channel( | |
1260 | condition, channel_info); | |
1261 | break; | |
1262 | default: | |
ab0ee2ca JG |
1263 | goto fail; |
1264 | } | |
1265 | ||
e8360425 | 1266 | return trigger_applies; |
ab0ee2ca JG |
1267 | fail: |
1268 | return false; | |
1269 | } | |
1270 | ||
1271 | static | |
1272 | bool trigger_applies_to_client(struct lttng_trigger *trigger, | |
1273 | struct notification_client *client) | |
1274 | { | |
1275 | bool applies = false; | |
1276 | struct lttng_condition_list_element *condition_list_element; | |
1277 | ||
1278 | cds_list_for_each_entry(condition_list_element, &client->condition_list, | |
1279 | node) { | |
1280 | applies = lttng_condition_is_equal( | |
1281 | condition_list_element->condition, | |
1282 | lttng_trigger_get_condition(trigger)); | |
1283 | if (applies) { | |
1284 | break; | |
1285 | } | |
1286 | } | |
1287 | return applies; | |
1288 | } | |
1289 | ||
ed327204 JG |
1290 | /* Must be called with RCU read lock held. */ |
1291 | static | |
1292 | struct lttng_session_trigger_list *get_session_trigger_list( | |
1293 | struct notification_thread_state *state, | |
1294 | const char *session_name) | |
1295 | { | |
1296 | struct lttng_session_trigger_list *list = NULL; | |
1297 | struct cds_lfht_node *node; | |
1298 | struct cds_lfht_iter iter; | |
1299 | ||
1300 | cds_lfht_lookup(state->session_triggers_ht, | |
1301 | hash_key_str(session_name, lttng_ht_seed), | |
1302 | match_session_trigger_list, | |
1303 | session_name, | |
1304 | &iter); | |
1305 | node = cds_lfht_iter_get_node(&iter); | |
1306 | if (!node) { | |
1307 | /* | |
1308 | * Not an error, the list of triggers applying to that session | |
1309 | * will be initialized when the session is created. | |
1310 | */ | |
1311 | DBG("[notification-thread] No trigger list found for session \"%s\" as it is not yet known to the notification system", | |
1312 | session_name); | |
1313 | goto end; | |
1314 | } | |
1315 | ||
1316 | list = caa_container_of(node, | |
1317 | struct lttng_session_trigger_list, | |
1318 | session_triggers_ht_node); | |
1319 | end: | |
1320 | return list; | |
1321 | } | |
1322 | ||
ea9a44f0 JG |
1323 | /* |
1324 | * Allocate an empty lttng_session_trigger_list for the session named | |
1325 | * 'session_name'. | |
1326 | * | |
1327 | * No ownership of 'session_name' is assumed by the session trigger list. | |
1328 | * It is the caller's responsability to ensure the session name is alive | |
1329 | * for as long as this list is. | |
1330 | */ | |
1331 | static | |
1332 | struct lttng_session_trigger_list *lttng_session_trigger_list_create( | |
1333 | const char *session_name, | |
1334 | struct cds_lfht *session_triggers_ht) | |
1335 | { | |
1336 | struct lttng_session_trigger_list *list; | |
1337 | ||
1338 | list = zmalloc(sizeof(*list)); | |
1339 | if (!list) { | |
1340 | goto end; | |
1341 | } | |
1342 | list->session_name = session_name; | |
1343 | CDS_INIT_LIST_HEAD(&list->list); | |
1344 | cds_lfht_node_init(&list->session_triggers_ht_node); | |
1345 | list->session_triggers_ht = session_triggers_ht; | |
1346 | ||
1347 | rcu_read_lock(); | |
1348 | /* Publish the list through the session_triggers_ht. */ | |
1349 | cds_lfht_add(session_triggers_ht, | |
1350 | hash_key_str(session_name, lttng_ht_seed), | |
1351 | &list->session_triggers_ht_node); | |
1352 | rcu_read_unlock(); | |
1353 | end: | |
1354 | return list; | |
1355 | } | |
1356 | ||
1357 | static | |
1358 | void free_session_trigger_list_rcu(struct rcu_head *node) | |
1359 | { | |
1360 | free(caa_container_of(node, struct lttng_session_trigger_list, | |
1361 | rcu_node)); | |
1362 | } | |
1363 | ||
1364 | static | |
1365 | void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list *list) | |
1366 | { | |
1367 | struct lttng_trigger_list_element *trigger_list_element, *tmp; | |
1368 | ||
1369 | /* Empty the list element by element, and then free the list itself. */ | |
1370 | cds_list_for_each_entry_safe(trigger_list_element, tmp, | |
1371 | &list->list, node) { | |
1372 | cds_list_del(&trigger_list_element->node); | |
1373 | free(trigger_list_element); | |
1374 | } | |
1375 | rcu_read_lock(); | |
1376 | /* Unpublish the list from the session_triggers_ht. */ | |
1377 | cds_lfht_del(list->session_triggers_ht, | |
1378 | &list->session_triggers_ht_node); | |
1379 | rcu_read_unlock(); | |
1380 | call_rcu(&list->rcu_node, free_session_trigger_list_rcu); | |
1381 | } | |
1382 | ||
1383 | static | |
1384 | int lttng_session_trigger_list_add(struct lttng_session_trigger_list *list, | |
1385 | const struct lttng_trigger *trigger) | |
1386 | { | |
1387 | int ret = 0; | |
1388 | struct lttng_trigger_list_element *new_element = | |
1389 | zmalloc(sizeof(*new_element)); | |
1390 | ||
1391 | if (!new_element) { | |
1392 | ret = -1; | |
1393 | goto end; | |
1394 | } | |
1395 | CDS_INIT_LIST_HEAD(&new_element->node); | |
1396 | new_element->trigger = trigger; | |
1397 | cds_list_add(&new_element->node, &list->list); | |
1398 | end: | |
1399 | return ret; | |
1400 | } | |
1401 | ||
1402 | static | |
1403 | bool trigger_applies_to_session(const struct lttng_trigger *trigger, | |
1404 | const char *session_name) | |
1405 | { | |
1406 | bool applies = false; | |
1407 | const struct lttng_condition *condition; | |
1408 | ||
1409 | condition = lttng_trigger_get_const_condition(trigger); | |
1410 | switch (lttng_condition_get_type(condition)) { | |
1411 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: | |
1412 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
1413 | { | |
1414 | enum lttng_condition_status condition_status; | |
1415 | const char *condition_session_name; | |
1416 | ||
1417 | condition_status = lttng_condition_session_rotation_get_session_name( | |
1418 | condition, &condition_session_name); | |
1419 | if (condition_status != LTTNG_CONDITION_STATUS_OK) { | |
1420 | ERR("[notification-thread] Failed to retrieve session rotation condition's session name"); | |
1421 | goto end; | |
1422 | } | |
1423 | ||
1424 | assert(condition_session_name); | |
1425 | applies = !strcmp(condition_session_name, session_name); | |
1426 | break; | |
1427 | } | |
1428 | default: | |
1429 | goto end; | |
1430 | } | |
1431 | end: | |
1432 | return applies; | |
1433 | } | |
1434 | ||
1435 | /* | |
1436 | * Allocate and initialize an lttng_session_trigger_list which contains | |
1437 | * all triggers that apply to the session named 'session_name'. | |
1438 | * | |
1439 | * No ownership of 'session_name' is assumed by the session trigger list. | |
1440 | * It is the caller's responsability to ensure the session name is alive | |
1441 | * for as long as this list is. | |
1442 | */ | |
1443 | static | |
1444 | struct lttng_session_trigger_list *lttng_session_trigger_list_build( | |
1445 | const struct notification_thread_state *state, | |
1446 | const char *session_name) | |
1447 | { | |
1448 | int trigger_count = 0; | |
1449 | struct lttng_session_trigger_list *session_trigger_list = NULL; | |
1450 | struct lttng_trigger_ht_element *trigger_ht_element = NULL; | |
1451 | struct cds_lfht_iter iter; | |
1452 | ||
1453 | session_trigger_list = lttng_session_trigger_list_create(session_name, | |
1454 | state->session_triggers_ht); | |
1455 | ||
1456 | /* Add all triggers applying to the session named 'session_name'. */ | |
1457 | cds_lfht_for_each_entry(state->triggers_ht, &iter, trigger_ht_element, | |
1458 | node) { | |
1459 | int ret; | |
1460 | ||
1461 | if (!trigger_applies_to_session(trigger_ht_element->trigger, | |
1462 | session_name)) { | |
1463 | continue; | |
1464 | } | |
1465 | ||
1466 | ret = lttng_session_trigger_list_add(session_trigger_list, | |
1467 | trigger_ht_element->trigger); | |
1468 | if (ret) { | |
1469 | goto error; | |
1470 | } | |
1471 | ||
1472 | trigger_count++; | |
1473 | } | |
1474 | ||
1475 | DBG("[notification-thread] Found %i triggers that apply to newly created session", | |
1476 | trigger_count); | |
1477 | return session_trigger_list; | |
1478 | error: | |
1479 | lttng_session_trigger_list_destroy(session_trigger_list); | |
1480 | return NULL; | |
1481 | } | |
1482 | ||
8abe313a JG |
1483 | static |
1484 | struct session_info *find_or_create_session_info( | |
1485 | struct notification_thread_state *state, | |
1486 | const char *name, uid_t uid, gid_t gid) | |
1487 | { | |
1488 | struct session_info *session = NULL; | |
1489 | struct cds_lfht_node *node; | |
1490 | struct cds_lfht_iter iter; | |
ea9a44f0 | 1491 | struct lttng_session_trigger_list *trigger_list; |
8abe313a JG |
1492 | |
1493 | rcu_read_lock(); | |
1494 | cds_lfht_lookup(state->sessions_ht, | |
1495 | hash_key_str(name, lttng_ht_seed), | |
1496 | match_session, | |
1497 | name, | |
1498 | &iter); | |
1499 | node = cds_lfht_iter_get_node(&iter); | |
1500 | if (node) { | |
1501 | DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)", | |
1502 | name, uid, gid); | |
1503 | session = caa_container_of(node, struct session_info, | |
1504 | sessions_ht_node); | |
1505 | assert(session->uid == uid); | |
1506 | assert(session->gid == gid); | |
1eee26c5 JG |
1507 | session_info_get(session); |
1508 | goto end; | |
ea9a44f0 JG |
1509 | } |
1510 | ||
1511 | trigger_list = lttng_session_trigger_list_build(state, name); | |
1512 | if (!trigger_list) { | |
1513 | goto error; | |
8abe313a JG |
1514 | } |
1515 | ||
1eee26c5 JG |
1516 | session = session_info_create(name, uid, gid, trigger_list, |
1517 | state->sessions_ht); | |
8abe313a JG |
1518 | if (!session) { |
1519 | ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)", | |
1520 | name, uid, gid); | |
b248644d | 1521 | lttng_session_trigger_list_destroy(trigger_list); |
ea9a44f0 | 1522 | goto error; |
8abe313a | 1523 | } |
ea9a44f0 | 1524 | trigger_list = NULL; |
577983cb JG |
1525 | |
1526 | cds_lfht_add(state->sessions_ht, hash_key_str(name, lttng_ht_seed), | |
9b63a4aa | 1527 | &session->sessions_ht_node); |
1eee26c5 | 1528 | end: |
8abe313a JG |
1529 | rcu_read_unlock(); |
1530 | return session; | |
ea9a44f0 JG |
1531 | error: |
1532 | rcu_read_unlock(); | |
1533 | session_info_put(session); | |
1534 | return NULL; | |
8abe313a JG |
1535 | } |
1536 | ||
ab0ee2ca JG |
1537 | static |
1538 | int handle_notification_thread_command_add_channel( | |
8abe313a JG |
1539 | struct notification_thread_state *state, |
1540 | const char *session_name, uid_t session_uid, gid_t session_gid, | |
1541 | const char *channel_name, enum lttng_domain_type channel_domain, | |
1542 | uint64_t channel_key_int, uint64_t channel_capacity, | |
1543 | enum lttng_error_code *cmd_result) | |
ab0ee2ca JG |
1544 | { |
1545 | struct cds_list_head trigger_list; | |
8abe313a JG |
1546 | struct channel_info *new_channel_info = NULL; |
1547 | struct channel_key channel_key = { | |
1548 | .key = channel_key_int, | |
1549 | .domain = channel_domain, | |
1550 | }; | |
ab0ee2ca JG |
1551 | struct lttng_channel_trigger_list *channel_trigger_list = NULL; |
1552 | struct lttng_trigger_ht_element *trigger_ht_element = NULL; | |
1553 | int trigger_count = 0; | |
1554 | struct cds_lfht_iter iter; | |
8abe313a | 1555 | struct session_info *session_info = NULL; |
ab0ee2ca JG |
1556 | |
1557 | DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64 " in %s domain", | |
8abe313a JG |
1558 | channel_name, session_name, channel_key_int, |
1559 | channel_domain == LTTNG_DOMAIN_KERNEL ? "kernel" : "user space"); | |
ab0ee2ca JG |
1560 | |
1561 | CDS_INIT_LIST_HEAD(&trigger_list); | |
1562 | ||
8abe313a JG |
1563 | session_info = find_or_create_session_info(state, session_name, |
1564 | session_uid, session_gid); | |
1565 | if (!session_info) { | |
a9577b76 | 1566 | /* Allocation error or an internal error occurred. */ |
ab0ee2ca JG |
1567 | goto error; |
1568 | } | |
1569 | ||
8abe313a JG |
1570 | new_channel_info = channel_info_create(channel_name, &channel_key, |
1571 | channel_capacity, session_info); | |
1572 | if (!new_channel_info) { | |
1573 | goto error; | |
1574 | } | |
ab0ee2ca | 1575 | |
83b934ad | 1576 | rcu_read_lock(); |
ab0ee2ca JG |
1577 | /* Build a list of all triggers applying to the new channel. */ |
1578 | cds_lfht_for_each_entry(state->triggers_ht, &iter, trigger_ht_element, | |
1579 | node) { | |
1580 | struct lttng_trigger_list_element *new_element; | |
1581 | ||
1582 | if (!trigger_applies_to_channel(trigger_ht_element->trigger, | |
8abe313a | 1583 | new_channel_info)) { |
ab0ee2ca JG |
1584 | continue; |
1585 | } | |
1586 | ||
1587 | new_element = zmalloc(sizeof(*new_element)); | |
1588 | if (!new_element) { | |
83b934ad | 1589 | rcu_read_unlock(); |
ab0ee2ca JG |
1590 | goto error; |
1591 | } | |
1592 | CDS_INIT_LIST_HEAD(&new_element->node); | |
1593 | new_element->trigger = trigger_ht_element->trigger; | |
1594 | cds_list_add(&new_element->node, &trigger_list); | |
1595 | trigger_count++; | |
1596 | } | |
83b934ad | 1597 | rcu_read_unlock(); |
ab0ee2ca JG |
1598 | |
1599 | DBG("[notification-thread] Found %i triggers that apply to newly added channel", | |
1600 | trigger_count); | |
1601 | channel_trigger_list = zmalloc(sizeof(*channel_trigger_list)); | |
1602 | if (!channel_trigger_list) { | |
1603 | goto error; | |
1604 | } | |
8abe313a | 1605 | channel_trigger_list->channel_key = new_channel_info->key; |
ab0ee2ca JG |
1606 | CDS_INIT_LIST_HEAD(&channel_trigger_list->list); |
1607 | cds_lfht_node_init(&channel_trigger_list->channel_triggers_ht_node); | |
1608 | cds_list_splice(&trigger_list, &channel_trigger_list->list); | |
1609 | ||
1610 | rcu_read_lock(); | |
1611 | /* Add channel to the channel_ht which owns the channel_infos. */ | |
1612 | cds_lfht_add(state->channels_ht, | |
8abe313a | 1613 | hash_channel_key(&new_channel_info->key), |
ab0ee2ca JG |
1614 | &new_channel_info->channels_ht_node); |
1615 | /* | |
1616 | * Add the list of triggers associated with this channel to the | |
1617 | * channel_triggers_ht. | |
1618 | */ | |
1619 | cds_lfht_add(state->channel_triggers_ht, | |
8abe313a | 1620 | hash_channel_key(&new_channel_info->key), |
ab0ee2ca JG |
1621 | &channel_trigger_list->channel_triggers_ht_node); |
1622 | rcu_read_unlock(); | |
1eee26c5 | 1623 | session_info_put(session_info); |
ab0ee2ca JG |
1624 | *cmd_result = LTTNG_OK; |
1625 | return 0; | |
1626 | error: | |
ab0ee2ca | 1627 | channel_info_destroy(new_channel_info); |
8abe313a | 1628 | session_info_put(session_info); |
ab0ee2ca JG |
1629 | return 1; |
1630 | } | |
1631 | ||
83b934ad MD |
1632 | static |
1633 | void free_channel_trigger_list_rcu(struct rcu_head *node) | |
1634 | { | |
1635 | free(caa_container_of(node, struct lttng_channel_trigger_list, | |
1636 | rcu_node)); | |
1637 | } | |
1638 | ||
1639 | static | |
1640 | void free_channel_state_sample_rcu(struct rcu_head *node) | |
1641 | { | |
1642 | free(caa_container_of(node, struct channel_state_sample, | |
1643 | rcu_node)); | |
1644 | } | |
1645 | ||
ab0ee2ca JG |
1646 | static |
1647 | int handle_notification_thread_command_remove_channel( | |
1648 | struct notification_thread_state *state, | |
1649 | uint64_t channel_key, enum lttng_domain_type domain, | |
1650 | enum lttng_error_code *cmd_result) | |
1651 | { | |
1652 | struct cds_lfht_node *node; | |
1653 | struct cds_lfht_iter iter; | |
1654 | struct lttng_channel_trigger_list *trigger_list; | |
1655 | struct lttng_trigger_list_element *trigger_list_element, *tmp; | |
1656 | struct channel_key key = { .key = channel_key, .domain = domain }; | |
1657 | struct channel_info *channel_info; | |
1658 | ||
1659 | DBG("[notification-thread] Removing channel key = %" PRIu64 " in %s domain", | |
1660 | channel_key, domain == LTTNG_DOMAIN_KERNEL ? "kernel" : "user space"); | |
1661 | ||
1662 | rcu_read_lock(); | |
1663 | ||
1664 | cds_lfht_lookup(state->channel_triggers_ht, | |
1665 | hash_channel_key(&key), | |
1666 | match_channel_trigger_list, | |
1667 | &key, | |
1668 | &iter); | |
1669 | node = cds_lfht_iter_get_node(&iter); | |
1670 | /* | |
1671 | * There is a severe internal error if we are being asked to remove a | |
1672 | * channel that doesn't exist. | |
1673 | */ | |
1674 | if (!node) { | |
1675 | ERR("[notification-thread] Channel being removed is unknown to the notification thread"); | |
1676 | goto end; | |
1677 | } | |
1678 | ||
1679 | /* Free the list of triggers associated with this channel. */ | |
1680 | trigger_list = caa_container_of(node, struct lttng_channel_trigger_list, | |
1681 | channel_triggers_ht_node); | |
1682 | cds_list_for_each_entry_safe(trigger_list_element, tmp, | |
1683 | &trigger_list->list, node) { | |
1684 | cds_list_del(&trigger_list_element->node); | |
1685 | free(trigger_list_element); | |
1686 | } | |
1687 | cds_lfht_del(state->channel_triggers_ht, node); | |
83b934ad | 1688 | call_rcu(&trigger_list->rcu_node, free_channel_trigger_list_rcu); |
ab0ee2ca JG |
1689 | |
1690 | /* Free sampled channel state. */ | |
1691 | cds_lfht_lookup(state->channel_state_ht, | |
1692 | hash_channel_key(&key), | |
1693 | match_channel_state_sample, | |
1694 | &key, | |
1695 | &iter); | |
1696 | node = cds_lfht_iter_get_node(&iter); | |
1697 | /* | |
1698 | * This is expected to be NULL if the channel is destroyed before we | |
1699 | * received a sample. | |
1700 | */ | |
1701 | if (node) { | |
1702 | struct channel_state_sample *sample = caa_container_of(node, | |
1703 | struct channel_state_sample, | |
1704 | channel_state_ht_node); | |
1705 | ||
1706 | cds_lfht_del(state->channel_state_ht, node); | |
83b934ad | 1707 | call_rcu(&sample->rcu_node, free_channel_state_sample_rcu); |
ab0ee2ca JG |
1708 | } |
1709 | ||
1710 | /* Remove the channel from the channels_ht and free it. */ | |
1711 | cds_lfht_lookup(state->channels_ht, | |
1712 | hash_channel_key(&key), | |
1713 | match_channel_info, | |
1714 | &key, | |
1715 | &iter); | |
1716 | node = cds_lfht_iter_get_node(&iter); | |
1717 | assert(node); | |
1718 | channel_info = caa_container_of(node, struct channel_info, | |
1719 | channels_ht_node); | |
1720 | cds_lfht_del(state->channels_ht, node); | |
1721 | channel_info_destroy(channel_info); | |
1722 | end: | |
1723 | rcu_read_unlock(); | |
1724 | *cmd_result = LTTNG_OK; | |
1725 | return 0; | |
1726 | } | |
1727 | ||
0ca52944 | 1728 | static |
ed327204 | 1729 | int handle_notification_thread_command_session_rotation( |
0ca52944 | 1730 | struct notification_thread_state *state, |
ed327204 JG |
1731 | enum notification_thread_command_type cmd_type, |
1732 | const char *session_name, uid_t session_uid, gid_t session_gid, | |
1733 | uint64_t trace_archive_chunk_id, | |
1734 | struct lttng_trace_archive_location *location, | |
1735 | enum lttng_error_code *_cmd_result) | |
0ca52944 | 1736 | { |
ed327204 JG |
1737 | int ret = 0; |
1738 | enum lttng_error_code cmd_result = LTTNG_OK; | |
1739 | struct lttng_session_trigger_list *trigger_list; | |
1740 | struct lttng_trigger_list_element *trigger_list_element; | |
1741 | struct session_info *session_info; | |
0ca52944 | 1742 | |
ed327204 JG |
1743 | rcu_read_lock(); |
1744 | ||
1745 | session_info = find_or_create_session_info(state, session_name, | |
1746 | session_uid, session_gid); | |
1747 | if (!session_info) { | |
a9577b76 | 1748 | /* Allocation error or an internal error occurred. */ |
ed327204 JG |
1749 | ret = -1; |
1750 | cmd_result = LTTNG_ERR_NOMEM; | |
1751 | goto end; | |
1752 | } | |
1753 | ||
1754 | session_info->rotation.ongoing = | |
1755 | cmd_type == NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING; | |
1756 | session_info->rotation.id = trace_archive_chunk_id; | |
1757 | trigger_list = get_session_trigger_list(state, session_name); | |
1758 | if (!trigger_list) { | |
1759 | DBG("[notification-thread] No triggers applying to session \"%s\" found", | |
1760 | session_name); | |
1761 | goto end; | |
1762 | } | |
1763 | ||
1764 | cds_list_for_each_entry(trigger_list_element, &trigger_list->list, | |
1765 | node) { | |
1766 | const struct lttng_condition *condition; | |
1767 | const struct lttng_action *action; | |
1768 | const struct lttng_trigger *trigger; | |
1769 | struct notification_client_list *client_list; | |
1770 | struct lttng_evaluation *evaluation = NULL; | |
1771 | enum lttng_condition_type condition_type; | |
1772 | ||
1773 | trigger = trigger_list_element->trigger; | |
1774 | condition = lttng_trigger_get_const_condition(trigger); | |
1775 | assert(condition); | |
1776 | condition_type = lttng_condition_get_type(condition); | |
1777 | ||
1778 | if (condition_type == LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING && | |
1779 | cmd_type != NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING) { | |
1780 | continue; | |
1781 | } else if (condition_type == LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED && | |
1782 | cmd_type != NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED) { | |
1783 | continue; | |
1784 | } | |
1785 | ||
1786 | action = lttng_trigger_get_const_action(trigger); | |
1787 | ||
1788 | /* Notify actions are the only type currently supported. */ | |
1789 | assert(lttng_action_get_type_const(action) == | |
1790 | LTTNG_ACTION_TYPE_NOTIFY); | |
1791 | ||
1792 | client_list = get_client_list_from_condition(state, condition); | |
1793 | assert(client_list); | |
1794 | ||
1795 | if (cds_list_empty(&client_list->list)) { | |
1796 | /* | |
1797 | * No clients interested in the evaluation's result, | |
1798 | * skip it. | |
1799 | */ | |
1800 | continue; | |
1801 | } | |
1802 | ||
1803 | if (cmd_type == NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING) { | |
1804 | evaluation = lttng_evaluation_session_rotation_ongoing_create( | |
1805 | trace_archive_chunk_id); | |
1806 | } else { | |
1807 | evaluation = lttng_evaluation_session_rotation_completed_create( | |
1808 | trace_archive_chunk_id, location); | |
1809 | } | |
1810 | ||
1811 | if (!evaluation) { | |
1812 | /* Internal error */ | |
1813 | ret = -1; | |
1814 | cmd_result = LTTNG_ERR_UNK; | |
1815 | goto end; | |
1816 | } | |
1817 | ||
1818 | /* Dispatch evaluation result to all clients. */ | |
1819 | ret = send_evaluation_to_clients(trigger_list_element->trigger, | |
1820 | evaluation, client_list, state, | |
1821 | session_info->uid, | |
1822 | session_info->gid); | |
1823 | lttng_evaluation_destroy(evaluation); | |
1824 | if (caa_unlikely(ret)) { | |
1825 | goto end; | |
1826 | } | |
1827 | } | |
1828 | end: | |
1829 | session_info_put(session_info); | |
1830 | *_cmd_result = cmd_result; | |
1831 | rcu_read_unlock(); | |
1832 | return ret; | |
0ca52944 JG |
1833 | } |
1834 | ||
1da26331 JG |
1835 | static |
1836 | int condition_is_supported(struct lttng_condition *condition) | |
1837 | { | |
1838 | int ret; | |
1839 | ||
1840 | switch (lttng_condition_get_type(condition)) { | |
1841 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
1842 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
1843 | { | |
1844 | enum lttng_domain_type domain; | |
1845 | ||
1846 | ret = lttng_condition_buffer_usage_get_domain_type(condition, | |
1847 | &domain); | |
1848 | if (ret) { | |
1849 | ret = -1; | |
1850 | goto end; | |
1851 | } | |
1852 | ||
1853 | if (domain != LTTNG_DOMAIN_KERNEL) { | |
1854 | ret = 1; | |
1855 | goto end; | |
1856 | } | |
1857 | ||
1858 | /* | |
1859 | * Older kernel tracers don't expose the API to monitor their | |
1860 | * buffers. Therefore, we reject triggers that require that | |
1861 | * mechanism to be available to be evaluated. | |
1862 | */ | |
7d268848 | 1863 | ret = kernel_supports_ring_buffer_snapshot_sample_positions(); |
1da26331 JG |
1864 | break; |
1865 | } | |
1866 | default: | |
1867 | ret = 1; | |
1868 | } | |
1869 | end: | |
1870 | return ret; | |
1871 | } | |
1872 | ||
51eab943 JG |
1873 | /* Must be called with RCU read lock held. */ |
1874 | static | |
1875 | int bind_trigger_to_matching_session(const struct lttng_trigger *trigger, | |
1876 | struct notification_thread_state *state) | |
1877 | { | |
1878 | int ret = 0; | |
51eab943 JG |
1879 | const struct lttng_condition *condition; |
1880 | const char *session_name; | |
1881 | struct lttng_session_trigger_list *trigger_list; | |
1882 | ||
1883 | condition = lttng_trigger_get_const_condition(trigger); | |
1884 | switch (lttng_condition_get_type(condition)) { | |
1885 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: | |
1886 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
1887 | { | |
1888 | enum lttng_condition_status status; | |
1889 | ||
1890 | status = lttng_condition_session_rotation_get_session_name( | |
1891 | condition, &session_name); | |
1892 | if (status != LTTNG_CONDITION_STATUS_OK) { | |
1893 | ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name"); | |
1894 | ret = -1; | |
1895 | goto end; | |
1896 | } | |
1897 | break; | |
1898 | } | |
1899 | default: | |
1900 | ret = -1; | |
1901 | goto end; | |
1902 | } | |
1903 | ||
ed327204 JG |
1904 | trigger_list = get_session_trigger_list(state, session_name); |
1905 | if (!trigger_list) { | |
51eab943 JG |
1906 | DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system", |
1907 | session_name); | |
1908 | goto end; | |
51eab943 | 1909 | |
ed327204 | 1910 | } |
51eab943 JG |
1911 | |
1912 | DBG("[notification-thread] Newly registered trigger bound to session \"%s\"", | |
1913 | session_name); | |
1914 | ret = lttng_session_trigger_list_add(trigger_list, trigger); | |
1915 | end: | |
1916 | return ret; | |
1917 | } | |
1918 | ||
1919 | /* Must be called with RCU read lock held. */ | |
1920 | static | |
1921 | int bind_trigger_to_matching_channels(const struct lttng_trigger *trigger, | |
1922 | struct notification_thread_state *state) | |
1923 | { | |
1924 | int ret = 0; | |
1925 | struct cds_lfht_node *node; | |
1926 | struct cds_lfht_iter iter; | |
1927 | struct channel_info *channel; | |
1928 | ||
1929 | cds_lfht_for_each_entry(state->channels_ht, &iter, channel, | |
1930 | channels_ht_node) { | |
1931 | struct lttng_trigger_list_element *trigger_list_element; | |
1932 | struct lttng_channel_trigger_list *trigger_list; | |
a5d64ae7 | 1933 | struct cds_lfht_iter lookup_iter; |
51eab943 JG |
1934 | |
1935 | if (!trigger_applies_to_channel(trigger, channel)) { | |
1936 | continue; | |
1937 | } | |
1938 | ||
1939 | cds_lfht_lookup(state->channel_triggers_ht, | |
1940 | hash_channel_key(&channel->key), | |
1941 | match_channel_trigger_list, | |
1942 | &channel->key, | |
a5d64ae7 MD |
1943 | &lookup_iter); |
1944 | node = cds_lfht_iter_get_node(&lookup_iter); | |
51eab943 JG |
1945 | assert(node); |
1946 | trigger_list = caa_container_of(node, | |
1947 | struct lttng_channel_trigger_list, | |
1948 | channel_triggers_ht_node); | |
1949 | ||
1950 | trigger_list_element = zmalloc(sizeof(*trigger_list_element)); | |
1951 | if (!trigger_list_element) { | |
1952 | ret = -1; | |
1953 | goto end; | |
1954 | } | |
1955 | CDS_INIT_LIST_HEAD(&trigger_list_element->node); | |
1956 | trigger_list_element->trigger = trigger; | |
1957 | cds_list_add(&trigger_list_element->node, &trigger_list->list); | |
1958 | DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"", | |
1959 | channel->name); | |
1960 | } | |
1961 | end: | |
1962 | return ret; | |
1963 | } | |
1964 | ||
ab0ee2ca JG |
1965 | /* |
1966 | * FIXME A client's credentials are not checked when registering a trigger, nor | |
1967 | * are they stored alongside with the trigger. | |
1968 | * | |
1da26331 | 1969 | * The effects of this are benign since: |
ab0ee2ca | 1970 | * - The client will succeed in registering the trigger, as it is valid, |
51eab943 | 1971 | * - The trigger will, internally, be bound to the channel/session, |
ab0ee2ca JG |
1972 | * - The notifications will not be sent since the client's credentials |
1973 | * are checked against the channel at that moment. | |
1da26331 JG |
1974 | * |
1975 | * If this function returns a non-zero value, it means something is | |
50ca7858 | 1976 | * fundamentally broken and the whole subsystem/thread will be torn down. |
1da26331 JG |
1977 | * |
1978 | * If a non-fatal error occurs, just set the cmd_result to the appropriate | |
1979 | * error code. | |
ab0ee2ca JG |
1980 | */ |
1981 | static | |
1982 | int handle_notification_thread_command_register_trigger( | |
8abe313a JG |
1983 | struct notification_thread_state *state, |
1984 | struct lttng_trigger *trigger, | |
1985 | enum lttng_error_code *cmd_result) | |
ab0ee2ca JG |
1986 | { |
1987 | int ret = 0; | |
1988 | struct lttng_condition *condition; | |
1989 | struct notification_client *client; | |
1990 | struct notification_client_list *client_list = NULL; | |
1991 | struct lttng_trigger_ht_element *trigger_ht_element = NULL; | |
1992 | struct notification_client_list_element *client_list_element, *tmp; | |
1993 | struct cds_lfht_node *node; | |
1994 | struct cds_lfht_iter iter; | |
ab0ee2ca JG |
1995 | bool free_trigger = true; |
1996 | ||
1997 | rcu_read_lock(); | |
1998 | ||
1999 | condition = lttng_trigger_get_condition(trigger); | |
1da26331 JG |
2000 | assert(condition); |
2001 | ||
2002 | ret = condition_is_supported(condition); | |
2003 | if (ret < 0) { | |
2004 | goto error; | |
2005 | } else if (ret == 0) { | |
2006 | *cmd_result = LTTNG_ERR_NOT_SUPPORTED; | |
2007 | goto error; | |
2008 | } else { | |
2009 | /* Feature is supported, continue. */ | |
2010 | ret = 0; | |
2011 | } | |
2012 | ||
ab0ee2ca JG |
2013 | trigger_ht_element = zmalloc(sizeof(*trigger_ht_element)); |
2014 | if (!trigger_ht_element) { | |
2015 | ret = -1; | |
2016 | goto error; | |
2017 | } | |
2018 | ||
2019 | /* Add trigger to the trigger_ht. */ | |
2020 | cds_lfht_node_init(&trigger_ht_element->node); | |
2021 | trigger_ht_element->trigger = trigger; | |
2022 | ||
2023 | node = cds_lfht_add_unique(state->triggers_ht, | |
2024 | lttng_condition_hash(condition), | |
2025 | match_condition, | |
2026 | condition, | |
2027 | &trigger_ht_element->node); | |
2028 | if (node != &trigger_ht_element->node) { | |
2029 | /* Not a fatal error, simply report it to the client. */ | |
2030 | *cmd_result = LTTNG_ERR_TRIGGER_EXISTS; | |
2031 | goto error_free_ht_element; | |
2032 | } | |
2033 | ||
2034 | /* | |
2035 | * Ownership of the trigger and of its wrapper was transfered to | |
2036 | * the triggers_ht. | |
2037 | */ | |
2038 | trigger_ht_element = NULL; | |
2039 | free_trigger = false; | |
2040 | ||
2041 | /* | |
2042 | * The rest only applies to triggers that have a "notify" action. | |
2043 | * It is not skipped as this is the only action type currently | |
2044 | * supported. | |
2045 | */ | |
2046 | client_list = zmalloc(sizeof(*client_list)); | |
2047 | if (!client_list) { | |
2048 | ret = -1; | |
2049 | goto error_free_ht_element; | |
2050 | } | |
2051 | cds_lfht_node_init(&client_list->notification_trigger_ht_node); | |
2052 | CDS_INIT_LIST_HEAD(&client_list->list); | |
2053 | client_list->trigger = trigger; | |
2054 | ||
2055 | /* Build a list of clients to which this new trigger applies. */ | |
2056 | cds_lfht_for_each_entry(state->client_socket_ht, &iter, client, | |
2057 | client_socket_ht_node) { | |
2058 | if (!trigger_applies_to_client(trigger, client)) { | |
2059 | continue; | |
2060 | } | |
2061 | ||
2062 | client_list_element = zmalloc(sizeof(*client_list_element)); | |
2063 | if (!client_list_element) { | |
2064 | ret = -1; | |
2065 | goto error_free_client_list; | |
2066 | } | |
2067 | CDS_INIT_LIST_HEAD(&client_list_element->node); | |
2068 | client_list_element->client = client; | |
2069 | cds_list_add(&client_list_element->node, &client_list->list); | |
2070 | } | |
2071 | ||
2072 | cds_lfht_add(state->notification_trigger_clients_ht, | |
2073 | lttng_condition_hash(condition), | |
2074 | &client_list->notification_trigger_ht_node); | |
ab0ee2ca | 2075 | |
f82f93a1 | 2076 | switch (get_condition_binding_object(condition)) { |
51eab943 JG |
2077 | case LTTNG_OBJECT_TYPE_SESSION: |
2078 | /* Add the trigger to the list if it matches a known session. */ | |
2079 | ret = bind_trigger_to_matching_session(trigger, state); | |
2080 | if (ret) { | |
2081 | goto error_free_client_list; | |
ab0ee2ca | 2082 | } |
f82f93a1 | 2083 | break; |
51eab943 JG |
2084 | case LTTNG_OBJECT_TYPE_CHANNEL: |
2085 | /* | |
2086 | * Add the trigger to list of triggers bound to the channels | |
2087 | * currently known. | |
2088 | */ | |
2089 | ret = bind_trigger_to_matching_channels(trigger, state); | |
2090 | if (ret) { | |
ab0ee2ca JG |
2091 | goto error_free_client_list; |
2092 | } | |
51eab943 JG |
2093 | break; |
2094 | case LTTNG_OBJECT_TYPE_NONE: | |
2095 | break; | |
2096 | default: | |
2097 | ERR("[notification-thread] Unknown object type on which to bind a newly registered trigger was encountered"); | |
2098 | ret = -1; | |
2099 | goto error_free_client_list; | |
ab0ee2ca JG |
2100 | } |
2101 | ||
2ae99f0b JG |
2102 | /* |
2103 | * Since there is nothing preventing clients from subscribing to a | |
2104 | * condition before the corresponding trigger is registered, we have | |
2105 | * to evaluate this new condition right away. | |
2106 | * | |
2107 | * At some point, we were waiting for the next "evaluation" (e.g. on | |
2108 | * reception of a channel sample) to evaluate this new condition, but | |
2109 | * that was broken. | |
2110 | * | |
2111 | * The reason it was broken is that waiting for the next sample | |
2112 | * does not allow us to properly handle transitions for edge-triggered | |
2113 | * conditions. | |
2114 | * | |
2115 | * Consider this example: when we handle a new channel sample, we | |
2116 | * evaluate each conditions twice: once with the previous state, and | |
2117 | * again with the newest state. We then use those two results to | |
2118 | * determine whether a state change happened: a condition was false and | |
2119 | * became true. If a state change happened, we have to notify clients. | |
2120 | * | |
2121 | * Now, if a client subscribes to a given notification and registers | |
2122 | * a trigger *after* that subscription, we have to make sure the | |
2123 | * condition is evaluated at this point while considering only the | |
2124 | * current state. Otherwise, the next evaluation cycle may only see | |
2125 | * that the evaluations remain the same (true for samples n-1 and n) and | |
2126 | * the client will never know that the condition has been met. | |
2127 | */ | |
2128 | cds_list_for_each_entry_safe(client_list_element, tmp, | |
2129 | &client_list->list, node) { | |
2130 | ret = evaluate_condition_for_client(trigger, condition, | |
2131 | client_list_element->client, state); | |
2132 | if (ret) { | |
2133 | goto error_free_client_list; | |
2134 | } | |
2135 | } | |
2136 | ||
2137 | /* | |
2138 | * Client list ownership transferred to the | |
2139 | * notification_trigger_clients_ht. | |
2140 | */ | |
2141 | client_list = NULL; | |
2142 | ||
ab0ee2ca JG |
2143 | *cmd_result = LTTNG_OK; |
2144 | error_free_client_list: | |
2145 | if (client_list) { | |
2146 | cds_list_for_each_entry_safe(client_list_element, tmp, | |
2147 | &client_list->list, node) { | |
2148 | free(client_list_element); | |
2149 | } | |
2150 | free(client_list); | |
2151 | } | |
2152 | error_free_ht_element: | |
2153 | free(trigger_ht_element); | |
2154 | error: | |
2155 | if (free_trigger) { | |
2156 | struct lttng_action *action = lttng_trigger_get_action(trigger); | |
2157 | ||
2158 | lttng_condition_destroy(condition); | |
2159 | lttng_action_destroy(action); | |
2160 | lttng_trigger_destroy(trigger); | |
2161 | } | |
2162 | rcu_read_unlock(); | |
2163 | return ret; | |
2164 | } | |
2165 | ||
83b934ad MD |
2166 | static |
2167 | void free_notification_client_list_rcu(struct rcu_head *node) | |
2168 | { | |
2169 | free(caa_container_of(node, struct notification_client_list, | |
2170 | rcu_node)); | |
2171 | } | |
2172 | ||
2173 | static | |
2174 | void free_lttng_trigger_ht_element_rcu(struct rcu_head *node) | |
2175 | { | |
2176 | free(caa_container_of(node, struct lttng_trigger_ht_element, | |
2177 | rcu_node)); | |
2178 | } | |
2179 | ||
cc2295b5 | 2180 | static |
ab0ee2ca JG |
2181 | int handle_notification_thread_command_unregister_trigger( |
2182 | struct notification_thread_state *state, | |
2183 | struct lttng_trigger *trigger, | |
2184 | enum lttng_error_code *_cmd_reply) | |
2185 | { | |
2186 | struct cds_lfht_iter iter; | |
ed327204 | 2187 | struct cds_lfht_node *triggers_ht_node; |
ab0ee2ca JG |
2188 | struct lttng_channel_trigger_list *trigger_list; |
2189 | struct notification_client_list *client_list; | |
2190 | struct notification_client_list_element *client_list_element, *tmp; | |
2191 | struct lttng_trigger_ht_element *trigger_ht_element = NULL; | |
2192 | struct lttng_condition *condition = lttng_trigger_get_condition( | |
2193 | trigger); | |
2194 | struct lttng_action *action; | |
2195 | enum lttng_error_code cmd_reply; | |
2196 | ||
2197 | rcu_read_lock(); | |
2198 | ||
2199 | cds_lfht_lookup(state->triggers_ht, | |
2200 | lttng_condition_hash(condition), | |
2201 | match_condition, | |
2202 | condition, | |
2203 | &iter); | |
2204 | triggers_ht_node = cds_lfht_iter_get_node(&iter); | |
2205 | if (!triggers_ht_node) { | |
2206 | cmd_reply = LTTNG_ERR_TRIGGER_NOT_FOUND; | |
2207 | goto end; | |
2208 | } else { | |
2209 | cmd_reply = LTTNG_OK; | |
2210 | } | |
2211 | ||
2212 | /* Remove trigger from channel_triggers_ht. */ | |
2213 | cds_lfht_for_each_entry(state->channel_triggers_ht, &iter, trigger_list, | |
2214 | channel_triggers_ht_node) { | |
2215 | struct lttng_trigger_list_element *trigger_element, *tmp; | |
2216 | ||
2217 | cds_list_for_each_entry_safe(trigger_element, tmp, | |
2218 | &trigger_list->list, node) { | |
9b63a4aa JG |
2219 | const struct lttng_condition *current_condition = |
2220 | lttng_trigger_get_const_condition( | |
ab0ee2ca JG |
2221 | trigger_element->trigger); |
2222 | ||
2223 | assert(current_condition); | |
2224 | if (!lttng_condition_is_equal(condition, | |
2225 | current_condition)) { | |
2226 | continue; | |
2227 | } | |
2228 | ||
2229 | DBG("[notification-thread] Removed trigger from channel_triggers_ht"); | |
2230 | cds_list_del(&trigger_element->node); | |
e4db5ace JR |
2231 | /* A trigger can only appear once per channel */ |
2232 | break; | |
ab0ee2ca JG |
2233 | } |
2234 | } | |
2235 | ||
2236 | /* | |
2237 | * Remove and release the client list from | |
2238 | * notification_trigger_clients_ht. | |
2239 | */ | |
ed327204 JG |
2240 | client_list = get_client_list_from_condition(state, condition); |
2241 | assert(client_list); | |
2242 | ||
ab0ee2ca JG |
2243 | cds_list_for_each_entry_safe(client_list_element, tmp, |
2244 | &client_list->list, node) { | |
2245 | free(client_list_element); | |
2246 | } | |
ed327204 JG |
2247 | cds_lfht_del(state->notification_trigger_clients_ht, |
2248 | &client_list->notification_trigger_ht_node); | |
83b934ad | 2249 | call_rcu(&client_list->rcu_node, free_notification_client_list_rcu); |
ab0ee2ca JG |
2250 | |
2251 | /* Remove trigger from triggers_ht. */ | |
2252 | trigger_ht_element = caa_container_of(triggers_ht_node, | |
2253 | struct lttng_trigger_ht_element, node); | |
2254 | cds_lfht_del(state->triggers_ht, triggers_ht_node); | |
2255 | ||
2256 | condition = lttng_trigger_get_condition(trigger_ht_element->trigger); | |
2257 | lttng_condition_destroy(condition); | |
2258 | action = lttng_trigger_get_action(trigger_ht_element->trigger); | |
2259 | lttng_action_destroy(action); | |
2260 | lttng_trigger_destroy(trigger_ht_element->trigger); | |
83b934ad | 2261 | call_rcu(&trigger_ht_element->rcu_node, free_lttng_trigger_ht_element_rcu); |
ab0ee2ca JG |
2262 | end: |
2263 | rcu_read_unlock(); | |
2264 | if (_cmd_reply) { | |
2265 | *_cmd_reply = cmd_reply; | |
2266 | } | |
2267 | return 0; | |
2268 | } | |
2269 | ||
2270 | /* Returns 0 on success, 1 on exit requested, negative value on error. */ | |
2271 | int handle_notification_thread_command( | |
2272 | struct notification_thread_handle *handle, | |
2273 | struct notification_thread_state *state) | |
2274 | { | |
2275 | int ret; | |
2276 | uint64_t counter; | |
2277 | struct notification_thread_command *cmd; | |
2278 | ||
8abe313a | 2279 | /* Read the event pipe to put it back into a quiescent state. */ |
18aeca05 | 2280 | ret = lttng_read(lttng_pipe_get_readfd(handle->cmd_queue.event_pipe), &counter, |
8abe313a | 2281 | sizeof(counter)); |
18aeca05 | 2282 | if (ret != sizeof(counter)) { |
ab0ee2ca JG |
2283 | goto error; |
2284 | } | |
2285 | ||
2286 | pthread_mutex_lock(&handle->cmd_queue.lock); | |
2287 | cmd = cds_list_first_entry(&handle->cmd_queue.list, | |
2288 | struct notification_thread_command, cmd_list_node); | |
2289 | switch (cmd->type) { | |
2290 | case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER: | |
2291 | DBG("[notification-thread] Received register trigger command"); | |
2292 | ret = handle_notification_thread_command_register_trigger( | |
2293 | state, cmd->parameters.trigger, | |
2294 | &cmd->reply_code); | |
2295 | break; | |
2296 | case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER: | |
2297 | DBG("[notification-thread] Received unregister trigger command"); | |
2298 | ret = handle_notification_thread_command_unregister_trigger( | |
2299 | state, cmd->parameters.trigger, | |
2300 | &cmd->reply_code); | |
2301 | break; | |
2302 | case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL: | |
2303 | DBG("[notification-thread] Received add channel command"); | |
2304 | ret = handle_notification_thread_command_add_channel( | |
8abe313a JG |
2305 | state, |
2306 | cmd->parameters.add_channel.session.name, | |
2307 | cmd->parameters.add_channel.session.uid, | |
2308 | cmd->parameters.add_channel.session.gid, | |
2309 | cmd->parameters.add_channel.channel.name, | |
2310 | cmd->parameters.add_channel.channel.domain, | |
2311 | cmd->parameters.add_channel.channel.key, | |
2312 | cmd->parameters.add_channel.channel.capacity, | |
ab0ee2ca JG |
2313 | &cmd->reply_code); |
2314 | break; | |
2315 | case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL: | |
2316 | DBG("[notification-thread] Received remove channel command"); | |
2317 | ret = handle_notification_thread_command_remove_channel( | |
2318 | state, cmd->parameters.remove_channel.key, | |
2319 | cmd->parameters.remove_channel.domain, | |
2320 | &cmd->reply_code); | |
2321 | break; | |
0ca52944 | 2322 | case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING: |
0ca52944 | 2323 | case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED: |
ed327204 JG |
2324 | DBG("[notification-thread] Received session rotation %s command", |
2325 | cmd->type == NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING ? | |
2326 | "ongoing" : "completed"); | |
2327 | ret = handle_notification_thread_command_session_rotation( | |
0ca52944 | 2328 | state, |
ed327204 JG |
2329 | cmd->type, |
2330 | cmd->parameters.session_rotation.session_name, | |
2331 | cmd->parameters.session_rotation.uid, | |
2332 | cmd->parameters.session_rotation.gid, | |
2333 | cmd->parameters.session_rotation.trace_archive_chunk_id, | |
2334 | cmd->parameters.session_rotation.location, | |
0ca52944 JG |
2335 | &cmd->reply_code); |
2336 | break; | |
ab0ee2ca JG |
2337 | case NOTIFICATION_COMMAND_TYPE_QUIT: |
2338 | DBG("[notification-thread] Received quit command"); | |
2339 | cmd->reply_code = LTTNG_OK; | |
2340 | ret = 1; | |
2341 | goto end; | |
2342 | default: | |
2343 | ERR("[notification-thread] Unknown internal command received"); | |
2344 | goto error_unlock; | |
2345 | } | |
2346 | ||
2347 | if (ret) { | |
2348 | goto error_unlock; | |
2349 | } | |
2350 | end: | |
2351 | cds_list_del(&cmd->cmd_list_node); | |
8ada111f | 2352 | lttng_waiter_wake_up(&cmd->reply_waiter); |
ab0ee2ca JG |
2353 | pthread_mutex_unlock(&handle->cmd_queue.lock); |
2354 | return ret; | |
2355 | error_unlock: | |
2356 | /* Wake-up and return a fatal error to the calling thread. */ | |
8ada111f | 2357 | lttng_waiter_wake_up(&cmd->reply_waiter); |
ab0ee2ca JG |
2358 | pthread_mutex_unlock(&handle->cmd_queue.lock); |
2359 | cmd->reply_code = LTTNG_ERR_FATAL; | |
2360 | error: | |
2361 | /* Indicate a fatal error to the caller. */ | |
2362 | return -1; | |
2363 | } | |
2364 | ||
2365 | static | |
2366 | unsigned long hash_client_socket(int socket) | |
2367 | { | |
2368 | return hash_key_ulong((void *) (unsigned long) socket, lttng_ht_seed); | |
2369 | } | |
2370 | ||
2371 | static | |
2372 | int socket_set_non_blocking(int socket) | |
2373 | { | |
2374 | int ret, flags; | |
2375 | ||
2376 | /* Set the pipe as non-blocking. */ | |
2377 | ret = fcntl(socket, F_GETFL, 0); | |
2378 | if (ret == -1) { | |
2379 | PERROR("fcntl get socket flags"); | |
2380 | goto end; | |
2381 | } | |
2382 | flags = ret; | |
2383 | ||
2384 | ret = fcntl(socket, F_SETFL, flags | O_NONBLOCK); | |
2385 | if (ret == -1) { | |
2386 | PERROR("fcntl set O_NONBLOCK socket flag"); | |
2387 | goto end; | |
2388 | } | |
2389 | DBG("Client socket (fd = %i) set as non-blocking", socket); | |
2390 | end: | |
2391 | return ret; | |
2392 | } | |
2393 | ||
2394 | static | |
14fa22f8 | 2395 | int client_reset_inbound_state(struct notification_client *client) |
ab0ee2ca JG |
2396 | { |
2397 | int ret; | |
2398 | ||
2399 | ret = lttng_dynamic_buffer_set_size( | |
2400 | &client->communication.inbound.buffer, 0); | |
2401 | assert(!ret); | |
2402 | ||
2403 | client->communication.inbound.bytes_to_receive = | |
2404 | sizeof(struct lttng_notification_channel_message); | |
2405 | client->communication.inbound.msg_type = | |
2406 | LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN; | |
ab0ee2ca JG |
2407 | LTTNG_SOCK_SET_UID_CRED(&client->communication.inbound.creds, -1); |
2408 | LTTNG_SOCK_SET_GID_CRED(&client->communication.inbound.creds, -1); | |
14fa22f8 JG |
2409 | ret = lttng_dynamic_buffer_set_size( |
2410 | &client->communication.inbound.buffer, | |
2411 | client->communication.inbound.bytes_to_receive); | |
2412 | return ret; | |
ab0ee2ca JG |
2413 | } |
2414 | ||
2415 | int handle_notification_thread_client_connect( | |
2416 | struct notification_thread_state *state) | |
2417 | { | |
2418 | int ret; | |
2419 | struct notification_client *client; | |
2420 | ||
2421 | DBG("[notification-thread] Handling new notification channel client connection"); | |
2422 | ||
2423 | client = zmalloc(sizeof(*client)); | |
2424 | if (!client) { | |
2425 | /* Fatal error. */ | |
2426 | ret = -1; | |
2427 | goto error; | |
2428 | } | |
2429 | CDS_INIT_LIST_HEAD(&client->condition_list); | |
2430 | lttng_dynamic_buffer_init(&client->communication.inbound.buffer); | |
2431 | lttng_dynamic_buffer_init(&client->communication.outbound.buffer); | |
01ea340e | 2432 | client->communication.inbound.expect_creds = true; |
14fa22f8 JG |
2433 | ret = client_reset_inbound_state(client); |
2434 | if (ret) { | |
2435 | ERR("[notification-thread] Failed to reset client communication's inbound state"); | |
2436 | ret = 0; | |
2437 | goto error; | |
2438 | } | |
ab0ee2ca JG |
2439 | |
2440 | ret = lttcomm_accept_unix_sock(state->notification_channel_socket); | |
2441 | if (ret < 0) { | |
2442 | ERR("[notification-thread] Failed to accept new notification channel client connection"); | |
2443 | ret = 0; | |
2444 | goto error; | |
2445 | } | |
2446 | ||
2447 | client->socket = ret; | |
2448 | ||
2449 | ret = socket_set_non_blocking(client->socket); | |
2450 | if (ret) { | |
2451 | ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking"); | |
2452 | goto error; | |
2453 | } | |
2454 | ||
2455 | ret = lttcomm_setsockopt_creds_unix_sock(client->socket); | |
2456 | if (ret < 0) { | |
2457 | ERR("[notification-thread] Failed to set socket options on new notification channel client socket"); | |
2458 | ret = 0; | |
2459 | goto error; | |
2460 | } | |
2461 | ||
2462 | ret = lttng_poll_add(&state->events, client->socket, | |
2463 | LPOLLIN | LPOLLERR | | |
2464 | LPOLLHUP | LPOLLRDHUP); | |
2465 | if (ret < 0) { | |
2466 | ERR("[notification-thread] Failed to add notification channel client socket to poll set"); | |
2467 | ret = 0; | |
2468 | goto error; | |
2469 | } | |
2470 | DBG("[notification-thread] Added new notification channel client socket (%i) to poll set", | |
2471 | client->socket); | |
2472 | ||
ab0ee2ca JG |
2473 | rcu_read_lock(); |
2474 | cds_lfht_add(state->client_socket_ht, | |
2475 | hash_client_socket(client->socket), | |
2476 | &client->client_socket_ht_node); | |
2477 | rcu_read_unlock(); | |
2478 | ||
2479 | return ret; | |
2480 | error: | |
2481 | notification_client_destroy(client, state); | |
2482 | return ret; | |
2483 | } | |
2484 | ||
2485 | int handle_notification_thread_client_disconnect( | |
2486 | int client_socket, | |
2487 | struct notification_thread_state *state) | |
2488 | { | |
2489 | int ret = 0; | |
2490 | struct notification_client *client; | |
2491 | ||
2492 | rcu_read_lock(); | |
2493 | DBG("[notification-thread] Closing client connection (socket fd = %i)", | |
2494 | client_socket); | |
2495 | client = get_client_from_socket(client_socket, state); | |
2496 | if (!client) { | |
2497 | /* Internal state corruption, fatal error. */ | |
2498 | ERR("[notification-thread] Unable to find client (socket fd = %i)", | |
2499 | client_socket); | |
2500 | ret = -1; | |
2501 | goto end; | |
2502 | } | |
2503 | ||
2504 | ret = lttng_poll_del(&state->events, client_socket); | |
2505 | if (ret) { | |
2506 | ERR("[notification-thread] Failed to remove client socket from poll set"); | |
2507 | } | |
2508 | cds_lfht_del(state->client_socket_ht, | |
2509 | &client->client_socket_ht_node); | |
2510 | notification_client_destroy(client, state); | |
2511 | end: | |
2512 | rcu_read_unlock(); | |
2513 | return ret; | |
2514 | } | |
2515 | ||
2516 | int handle_notification_thread_client_disconnect_all( | |
2517 | struct notification_thread_state *state) | |
2518 | { | |
2519 | struct cds_lfht_iter iter; | |
2520 | struct notification_client *client; | |
2521 | bool error_encoutered = false; | |
2522 | ||
2523 | rcu_read_lock(); | |
2524 | DBG("[notification-thread] Closing all client connections"); | |
2525 | cds_lfht_for_each_entry(state->client_socket_ht, &iter, client, | |
2526 | client_socket_ht_node) { | |
2527 | int ret; | |
2528 | ||
2529 | ret = handle_notification_thread_client_disconnect( | |
2530 | client->socket, state); | |
2531 | if (ret) { | |
2532 | error_encoutered = true; | |
2533 | } | |
2534 | } | |
2535 | rcu_read_unlock(); | |
2536 | return error_encoutered ? 1 : 0; | |
2537 | } | |
2538 | ||
2539 | int handle_notification_thread_trigger_unregister_all( | |
2540 | struct notification_thread_state *state) | |
2541 | { | |
4149ace8 | 2542 | bool error_occurred = false; |
ab0ee2ca JG |
2543 | struct cds_lfht_iter iter; |
2544 | struct lttng_trigger_ht_element *trigger_ht_element; | |
2545 | ||
763de384 | 2546 | rcu_read_lock(); |
ab0ee2ca JG |
2547 | cds_lfht_for_each_entry(state->triggers_ht, &iter, trigger_ht_element, |
2548 | node) { | |
2549 | int ret = handle_notification_thread_command_unregister_trigger( | |
2550 | state, trigger_ht_element->trigger, NULL); | |
2551 | if (ret) { | |
4149ace8 | 2552 | error_occurred = true; |
ab0ee2ca JG |
2553 | } |
2554 | } | |
763de384 | 2555 | rcu_read_unlock(); |
4149ace8 | 2556 | return error_occurred ? -1 : 0; |
ab0ee2ca JG |
2557 | } |
2558 | ||
2559 | static | |
2560 | int client_flush_outgoing_queue(struct notification_client *client, | |
2561 | struct notification_thread_state *state) | |
2562 | { | |
2563 | ssize_t ret; | |
2564 | size_t to_send_count; | |
2565 | ||
2566 | assert(client->communication.outbound.buffer.size != 0); | |
2567 | to_send_count = client->communication.outbound.buffer.size; | |
2568 | DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue", | |
2569 | client->socket); | |
2570 | ||
2571 | ret = lttcomm_send_unix_sock_non_block(client->socket, | |
2572 | client->communication.outbound.buffer.data, | |
2573 | to_send_count); | |
2574 | if ((ret < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) || | |
2575 | (ret > 0 && ret < to_send_count)) { | |
2576 | DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed", | |
2577 | client->socket); | |
2578 | to_send_count -= max(ret, 0); | |
2579 | ||
2580 | memcpy(client->communication.outbound.buffer.data, | |
2581 | client->communication.outbound.buffer.data + | |
2582 | client->communication.outbound.buffer.size - to_send_count, | |
2583 | to_send_count); | |
2584 | ret = lttng_dynamic_buffer_set_size( | |
2585 | &client->communication.outbound.buffer, | |
2586 | to_send_count); | |
2587 | if (ret) { | |
2588 | goto error; | |
2589 | } | |
2590 | ||
2591 | /* | |
2592 | * We want to be notified whenever there is buffer space | |
2593 | * available to send the rest of the payload. | |
2594 | */ | |
2595 | ret = lttng_poll_mod(&state->events, client->socket, | |
2596 | CLIENT_POLL_MASK_IN_OUT); | |
2597 | if (ret) { | |
2598 | goto error; | |
2599 | } | |
2600 | } else if (ret < 0) { | |
2601 | /* Generic error, disconnect the client. */ | |
2602 | ERR("[notification-thread] Failed to send flush outgoing queue, disconnecting client (socket fd = %i)", | |
2603 | client->socket); | |
2604 | ret = handle_notification_thread_client_disconnect( | |
2605 | client->socket, state); | |
2606 | if (ret) { | |
2607 | goto error; | |
2608 | } | |
2609 | } else { | |
2610 | /* No error and flushed the queue completely. */ | |
2611 | ret = lttng_dynamic_buffer_set_size( | |
2612 | &client->communication.outbound.buffer, 0); | |
2613 | if (ret) { | |
2614 | goto error; | |
2615 | } | |
2616 | ret = lttng_poll_mod(&state->events, client->socket, | |
2617 | CLIENT_POLL_MASK_IN); | |
2618 | if (ret) { | |
2619 | goto error; | |
2620 | } | |
2621 | ||
2622 | client->communication.outbound.queued_command_reply = false; | |
2623 | client->communication.outbound.dropped_notification = false; | |
2624 | } | |
2625 | ||
2626 | return 0; | |
2627 | error: | |
2628 | return -1; | |
2629 | } | |
2630 | ||
2631 | static | |
2632 | int client_send_command_reply(struct notification_client *client, | |
2633 | struct notification_thread_state *state, | |
2634 | enum lttng_notification_channel_status status) | |
2635 | { | |
2636 | int ret; | |
2637 | struct lttng_notification_channel_command_reply reply = { | |
2638 | .status = (int8_t) status, | |
2639 | }; | |
2640 | struct lttng_notification_channel_message msg = { | |
2641 | .type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY, | |
2642 | .size = sizeof(reply), | |
2643 | }; | |
2644 | char buffer[sizeof(msg) + sizeof(reply)]; | |
2645 | ||
2646 | if (client->communication.outbound.queued_command_reply) { | |
2647 | /* Protocol error. */ | |
2648 | goto error; | |
2649 | } | |
2650 | ||
2651 | memcpy(buffer, &msg, sizeof(msg)); | |
2652 | memcpy(buffer + sizeof(msg), &reply, sizeof(reply)); | |
2653 | DBG("[notification-thread] Send command reply (%i)", (int) status); | |
2654 | ||
2655 | /* Enqueue buffer to outgoing queue and flush it. */ | |
2656 | ret = lttng_dynamic_buffer_append( | |
2657 | &client->communication.outbound.buffer, | |
2658 | buffer, sizeof(buffer)); | |
2659 | if (ret) { | |
2660 | goto error; | |
2661 | } | |
2662 | ||
2663 | ret = client_flush_outgoing_queue(client, state); | |
2664 | if (ret) { | |
2665 | goto error; | |
2666 | } | |
2667 | ||
2668 | if (client->communication.outbound.buffer.size != 0) { | |
2669 | /* Queue could not be emptied. */ | |
2670 | client->communication.outbound.queued_command_reply = true; | |
2671 | } | |
2672 | ||
2673 | return 0; | |
2674 | error: | |
2675 | return -1; | |
2676 | } | |
2677 | ||
2678 | static | |
2679 | int client_dispatch_message(struct notification_client *client, | |
2680 | struct notification_thread_state *state) | |
2681 | { | |
2682 | int ret = 0; | |
2683 | ||
2684 | if (client->communication.inbound.msg_type != | |
2685 | LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE && | |
2686 | client->communication.inbound.msg_type != | |
2687 | LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN && | |
2688 | !client->validated) { | |
2689 | WARN("[notification-thread] client attempted a command before handshake"); | |
2690 | ret = -1; | |
2691 | goto end; | |
2692 | } | |
2693 | ||
2694 | switch (client->communication.inbound.msg_type) { | |
2695 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN: | |
2696 | { | |
2697 | /* | |
2698 | * Receiving message header. The function will be called again | |
2699 | * once the rest of the message as been received and can be | |
2700 | * interpreted. | |
2701 | */ | |
2702 | const struct lttng_notification_channel_message *msg; | |
2703 | ||
2704 | assert(sizeof(*msg) == | |
2705 | client->communication.inbound.buffer.size); | |
2706 | msg = (const struct lttng_notification_channel_message *) | |
2707 | client->communication.inbound.buffer.data; | |
2708 | ||
2709 | if (msg->size == 0 || msg->size > DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE) { | |
2710 | ERR("[notification-thread] Invalid notification channel message: length = %u", msg->size); | |
2711 | ret = -1; | |
2712 | goto end; | |
2713 | } | |
2714 | ||
2715 | switch (msg->type) { | |
2716 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE: | |
2717 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE: | |
2718 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE: | |
2719 | break; | |
2720 | default: | |
2721 | ret = -1; | |
2722 | ERR("[notification-thread] Invalid notification channel message: unexpected message type"); | |
2723 | goto end; | |
2724 | } | |
2725 | ||
2726 | client->communication.inbound.bytes_to_receive = msg->size; | |
2727 | client->communication.inbound.msg_type = | |
2728 | (enum lttng_notification_channel_message_type) msg->type; | |
ab0ee2ca | 2729 | ret = lttng_dynamic_buffer_set_size( |
14fa22f8 | 2730 | &client->communication.inbound.buffer, msg->size); |
ab0ee2ca JG |
2731 | if (ret) { |
2732 | goto end; | |
2733 | } | |
2734 | break; | |
2735 | } | |
2736 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE: | |
2737 | { | |
2738 | struct lttng_notification_channel_command_handshake *handshake_client; | |
2739 | struct lttng_notification_channel_command_handshake handshake_reply = { | |
2740 | .major = LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR, | |
2741 | .minor = LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR, | |
2742 | }; | |
2743 | struct lttng_notification_channel_message msg_header = { | |
2744 | .type = LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE, | |
2745 | .size = sizeof(handshake_reply), | |
2746 | }; | |
2747 | enum lttng_notification_channel_status status = | |
2748 | LTTNG_NOTIFICATION_CHANNEL_STATUS_OK; | |
2749 | char send_buffer[sizeof(msg_header) + sizeof(handshake_reply)]; | |
2750 | ||
2751 | memcpy(send_buffer, &msg_header, sizeof(msg_header)); | |
2752 | memcpy(send_buffer + sizeof(msg_header), &handshake_reply, | |
2753 | sizeof(handshake_reply)); | |
2754 | ||
2755 | handshake_client = | |
2756 | (struct lttng_notification_channel_command_handshake *) | |
2757 | client->communication.inbound.buffer.data; | |
47a32869 | 2758 | client->major = handshake_client->major; |
ab0ee2ca JG |
2759 | client->minor = handshake_client->minor; |
2760 | if (!client->communication.inbound.creds_received) { | |
2761 | ERR("[notification-thread] No credentials received from client"); | |
2762 | ret = -1; | |
2763 | goto end; | |
2764 | } | |
2765 | ||
2766 | client->uid = LTTNG_SOCK_GET_UID_CRED( | |
2767 | &client->communication.inbound.creds); | |
2768 | client->gid = LTTNG_SOCK_GET_GID_CRED( | |
2769 | &client->communication.inbound.creds); | |
2770 | DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i", | |
2771 | client->uid, client->gid, (int) client->major, | |
2772 | (int) client->minor); | |
2773 | ||
2774 | if (handshake_client->major != LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR) { | |
2775 | status = LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION; | |
2776 | } | |
2777 | ||
2778 | ret = lttng_dynamic_buffer_append(&client->communication.outbound.buffer, | |
2779 | send_buffer, sizeof(send_buffer)); | |
2780 | if (ret) { | |
2781 | ERR("[notification-thread] Failed to send protocol version to notification channel client"); | |
2782 | goto end; | |
2783 | } | |
2784 | ||
2785 | ret = client_flush_outgoing_queue(client, state); | |
2786 | if (ret) { | |
2787 | goto end; | |
2788 | } | |
2789 | ||
2790 | ret = client_send_command_reply(client, state, status); | |
2791 | if (ret) { | |
2792 | ERR("[notification-thread] Failed to send reply to notification channel client"); | |
2793 | goto end; | |
2794 | } | |
2795 | ||
2796 | /* Set reception state to receive the next message header. */ | |
14fa22f8 JG |
2797 | ret = client_reset_inbound_state(client); |
2798 | if (ret) { | |
2799 | ERR("[notification-thread] Failed to reset client communication's inbound state"); | |
2800 | goto end; | |
2801 | } | |
ab0ee2ca JG |
2802 | client->validated = true; |
2803 | break; | |
2804 | } | |
2805 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE: | |
2806 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE: | |
2807 | { | |
2808 | struct lttng_condition *condition; | |
2809 | enum lttng_notification_channel_status status = | |
2810 | LTTNG_NOTIFICATION_CHANNEL_STATUS_OK; | |
2811 | const struct lttng_buffer_view condition_view = | |
2812 | lttng_buffer_view_from_dynamic_buffer( | |
2813 | &client->communication.inbound.buffer, | |
2814 | 0, -1); | |
2815 | size_t expected_condition_size = | |
2816 | client->communication.inbound.buffer.size; | |
2817 | ||
2818 | ret = lttng_condition_create_from_buffer(&condition_view, | |
2819 | &condition); | |
2820 | if (ret != expected_condition_size) { | |
2821 | ERR("[notification-thread] Malformed condition received from client"); | |
2822 | goto end; | |
2823 | } | |
2824 | ||
2825 | if (client->communication.inbound.msg_type == | |
2826 | LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE) { | |
ab0ee2ca JG |
2827 | ret = notification_thread_client_subscribe(client, |
2828 | condition, state, &status); | |
2829 | } else { | |
2830 | ret = notification_thread_client_unsubscribe(client, | |
2831 | condition, state, &status); | |
2832 | } | |
2833 | if (ret) { | |
2834 | goto end; | |
2835 | } | |
2836 | ||
2837 | ret = client_send_command_reply(client, state, status); | |
2838 | if (ret) { | |
2839 | ERR("[notification-thread] Failed to send reply to notification channel client"); | |
2840 | goto end; | |
2841 | } | |
2842 | ||
2843 | /* Set reception state to receive the next message header. */ | |
14fa22f8 JG |
2844 | ret = client_reset_inbound_state(client); |
2845 | if (ret) { | |
2846 | ERR("[notification-thread] Failed to reset client communication's inbound state"); | |
2847 | goto end; | |
2848 | } | |
ab0ee2ca JG |
2849 | break; |
2850 | } | |
2851 | default: | |
2852 | abort(); | |
2853 | } | |
2854 | end: | |
2855 | return ret; | |
2856 | } | |
2857 | ||
2858 | /* Incoming data from client. */ | |
2859 | int handle_notification_thread_client_in( | |
2860 | struct notification_thread_state *state, int socket) | |
2861 | { | |
14fa22f8 | 2862 | int ret = 0; |
ab0ee2ca JG |
2863 | struct notification_client *client; |
2864 | ssize_t recv_ret; | |
2865 | size_t offset; | |
2866 | ||
2867 | client = get_client_from_socket(socket, state); | |
2868 | if (!client) { | |
2869 | /* Internal error, abort. */ | |
2870 | ret = -1; | |
2871 | goto end; | |
2872 | } | |
2873 | ||
14fa22f8 JG |
2874 | offset = client->communication.inbound.buffer.size - |
2875 | client->communication.inbound.bytes_to_receive; | |
01ea340e | 2876 | if (client->communication.inbound.expect_creds) { |
ab0ee2ca JG |
2877 | recv_ret = lttcomm_recv_creds_unix_sock(socket, |
2878 | client->communication.inbound.buffer.data + offset, | |
2879 | client->communication.inbound.bytes_to_receive, | |
2880 | &client->communication.inbound.creds); | |
2881 | if (recv_ret > 0) { | |
01ea340e | 2882 | client->communication.inbound.expect_creds = false; |
ab0ee2ca JG |
2883 | client->communication.inbound.creds_received = true; |
2884 | } | |
2885 | } else { | |
2886 | recv_ret = lttcomm_recv_unix_sock_non_block(socket, | |
2887 | client->communication.inbound.buffer.data + offset, | |
2888 | client->communication.inbound.bytes_to_receive); | |
2889 | } | |
2890 | if (recv_ret < 0) { | |
2891 | goto error_disconnect_client; | |
2892 | } | |
2893 | ||
2894 | client->communication.inbound.bytes_to_receive -= recv_ret; | |
ab0ee2ca JG |
2895 | if (client->communication.inbound.bytes_to_receive == 0) { |
2896 | ret = client_dispatch_message(client, state); | |
2897 | if (ret) { | |
2898 | /* | |
2899 | * Only returns an error if this client must be | |
2900 | * disconnected. | |
2901 | */ | |
2902 | goto error_disconnect_client; | |
2903 | } | |
2904 | } else { | |
2905 | goto end; | |
2906 | } | |
2907 | end: | |
2908 | return ret; | |
2909 | error_disconnect_client: | |
2910 | ret = handle_notification_thread_client_disconnect(socket, state); | |
2911 | return ret; | |
2912 | } | |
2913 | ||
2914 | /* Client ready to receive outgoing data. */ | |
2915 | int handle_notification_thread_client_out( | |
2916 | struct notification_thread_state *state, int socket) | |
2917 | { | |
2918 | int ret; | |
2919 | struct notification_client *client; | |
2920 | ||
2921 | client = get_client_from_socket(socket, state); | |
2922 | if (!client) { | |
2923 | /* Internal error, abort. */ | |
2924 | ret = -1; | |
2925 | goto end; | |
2926 | } | |
2927 | ||
2928 | ret = client_flush_outgoing_queue(client, state); | |
2929 | if (ret) { | |
2930 | goto end; | |
2931 | } | |
2932 | end: | |
2933 | return ret; | |
2934 | } | |
2935 | ||
2936 | static | |
e8360425 JD |
2937 | bool evaluate_buffer_usage_condition(const struct lttng_condition *condition, |
2938 | const struct channel_state_sample *sample, | |
2939 | uint64_t buffer_capacity) | |
ab0ee2ca JG |
2940 | { |
2941 | bool result = false; | |
2942 | uint64_t threshold; | |
2943 | enum lttng_condition_type condition_type; | |
e8360425 | 2944 | const struct lttng_condition_buffer_usage *use_condition = container_of( |
ab0ee2ca JG |
2945 | condition, struct lttng_condition_buffer_usage, |
2946 | parent); | |
2947 | ||
ab0ee2ca JG |
2948 | if (use_condition->threshold_bytes.set) { |
2949 | threshold = use_condition->threshold_bytes.value; | |
2950 | } else { | |
2951 | /* | |
2952 | * Threshold was expressed as a ratio. | |
2953 | * | |
2954 | * TODO the threshold (in bytes) of conditions expressed | |
2955 | * as a ratio of total buffer size could be cached to | |
2956 | * forego this double-multiplication or it could be performed | |
2957 | * as fixed-point math. | |
2958 | * | |
2959 | * Note that caching should accomodate the case where the | |
2960 | * condition applies to multiple channels (i.e. don't assume | |
2961 | * that all channels matching my_chann* have the same size...) | |
2962 | */ | |
2963 | threshold = (uint64_t) (use_condition->threshold_ratio.value * | |
2964 | (double) buffer_capacity); | |
2965 | } | |
2966 | ||
2967 | condition_type = lttng_condition_get_type(condition); | |
2968 | if (condition_type == LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW) { | |
2969 | DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64 ", highest usage = %" PRIu64, | |
2970 | threshold, sample->highest_usage); | |
2971 | ||
2972 | /* | |
2973 | * The low condition should only be triggered once _all_ of the | |
2974 | * streams in a channel have gone below the "low" threshold. | |
2975 | */ | |
2976 | if (sample->highest_usage <= threshold) { | |
2977 | result = true; | |
2978 | } | |
2979 | } else { | |
2980 | DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64 ", highest usage = %" PRIu64, | |
2981 | threshold, sample->highest_usage); | |
2982 | ||
2983 | /* | |
2984 | * For high buffer usage scenarios, we want to trigger whenever | |
2985 | * _any_ of the streams has reached the "high" threshold. | |
2986 | */ | |
2987 | if (sample->highest_usage >= threshold) { | |
2988 | result = true; | |
2989 | } | |
2990 | } | |
e8360425 | 2991 | |
ab0ee2ca JG |
2992 | return result; |
2993 | } | |
2994 | ||
2995 | static | |
e8360425 JD |
2996 | bool evaluate_session_consumed_size_condition( |
2997 | const struct lttng_condition *condition, | |
2998 | uint64_t session_consumed_size) | |
2999 | { | |
3000 | uint64_t threshold; | |
3001 | const struct lttng_condition_session_consumed_size *size_condition = | |
3002 | container_of(condition, | |
3003 | struct lttng_condition_session_consumed_size, | |
3004 | parent); | |
3005 | ||
3006 | threshold = size_condition->consumed_threshold_bytes.value; | |
3007 | DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64 ", current size = %" PRIu64, | |
3008 | threshold, session_consumed_size); | |
3009 | return session_consumed_size >= threshold; | |
3010 | } | |
3011 | ||
3012 | static | |
ed327204 | 3013 | int evaluate_buffer_condition(const struct lttng_condition *condition, |
ab0ee2ca | 3014 | struct lttng_evaluation **evaluation, |
e8360425 JD |
3015 | const struct notification_thread_state *state, |
3016 | const struct channel_state_sample *previous_sample, | |
3017 | const struct channel_state_sample *latest_sample, | |
3018 | uint64_t previous_session_consumed_total, | |
3019 | uint64_t latest_session_consumed_total, | |
3020 | struct channel_info *channel_info) | |
ab0ee2ca JG |
3021 | { |
3022 | int ret = 0; | |
3023 | enum lttng_condition_type condition_type; | |
e8360425 JD |
3024 | const bool previous_sample_available = !!previous_sample; |
3025 | bool previous_sample_result = false; | |
ab0ee2ca JG |
3026 | bool latest_sample_result; |
3027 | ||
3028 | condition_type = lttng_condition_get_type(condition); | |
ab0ee2ca | 3029 | |
e8360425 JD |
3030 | switch (condition_type) { |
3031 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
3032 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
3033 | if (caa_likely(previous_sample_available)) { | |
3034 | previous_sample_result = | |
3035 | evaluate_buffer_usage_condition(condition, | |
3036 | previous_sample, channel_info->capacity); | |
3037 | } | |
3038 | latest_sample_result = evaluate_buffer_usage_condition( | |
3039 | condition, latest_sample, | |
3040 | channel_info->capacity); | |
3041 | break; | |
3042 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: | |
3043 | if (caa_likely(previous_sample_available)) { | |
3044 | previous_sample_result = | |
3045 | evaluate_session_consumed_size_condition( | |
3046 | condition, | |
3047 | previous_session_consumed_total); | |
3048 | } | |
3049 | latest_sample_result = | |
3050 | evaluate_session_consumed_size_condition( | |
3051 | condition, | |
3052 | latest_session_consumed_total); | |
3053 | break; | |
3054 | default: | |
3055 | /* Unknown condition type; internal error. */ | |
3056 | abort(); | |
3057 | } | |
ab0ee2ca JG |
3058 | |
3059 | if (!latest_sample_result || | |
3060 | (previous_sample_result == latest_sample_result)) { | |
3061 | /* | |
3062 | * Only trigger on a condition evaluation transition. | |
3063 | * | |
3064 | * NOTE: This edge-triggered logic may not be appropriate for | |
3065 | * future condition types. | |
3066 | */ | |
3067 | goto end; | |
3068 | } | |
3069 | ||
e8360425 JD |
3070 | if (!evaluation || !latest_sample_result) { |
3071 | goto end; | |
3072 | } | |
3073 | ||
3074 | switch (condition_type) { | |
3075 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
3076 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
ab0ee2ca JG |
3077 | *evaluation = lttng_evaluation_buffer_usage_create( |
3078 | condition_type, | |
3079 | latest_sample->highest_usage, | |
e8360425 JD |
3080 | channel_info->capacity); |
3081 | break; | |
3082 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: | |
3083 | *evaluation = lttng_evaluation_session_consumed_size_create( | |
e8360425 JD |
3084 | latest_session_consumed_total); |
3085 | break; | |
3086 | default: | |
3087 | abort(); | |
3088 | } | |
3089 | ||
3090 | if (!*evaluation) { | |
3091 | ret = -1; | |
3092 | goto end; | |
ab0ee2ca JG |
3093 | } |
3094 | end: | |
3095 | return ret; | |
3096 | } | |
3097 | ||
3098 | static | |
3099 | int client_enqueue_dropped_notification(struct notification_client *client, | |
3100 | struct notification_thread_state *state) | |
3101 | { | |
3102 | int ret; | |
3103 | struct lttng_notification_channel_message msg = { | |
3104 | .type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED, | |
3105 | .size = 0, | |
3106 | }; | |
3107 | ||
3108 | ret = lttng_dynamic_buffer_append( | |
3109 | &client->communication.outbound.buffer, &msg, | |
3110 | sizeof(msg)); | |
3111 | return ret; | |
3112 | } | |
3113 | ||
3114 | static | |
9b63a4aa JG |
3115 | int send_evaluation_to_clients(const struct lttng_trigger *trigger, |
3116 | const struct lttng_evaluation *evaluation, | |
ab0ee2ca JG |
3117 | struct notification_client_list* client_list, |
3118 | struct notification_thread_state *state, | |
3119 | uid_t channel_uid, gid_t channel_gid) | |
3120 | { | |
3121 | int ret = 0; | |
3122 | struct lttng_dynamic_buffer msg_buffer; | |
3123 | struct notification_client_list_element *client_list_element, *tmp; | |
9b63a4aa JG |
3124 | const struct lttng_notification notification = { |
3125 | .condition = (struct lttng_condition *) lttng_trigger_get_const_condition(trigger), | |
3126 | .evaluation = (struct lttng_evaluation *) evaluation, | |
3127 | }; | |
3647288f JG |
3128 | struct lttng_notification_channel_message msg_header = { |
3129 | .type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION, | |
3130 | }; | |
ab0ee2ca JG |
3131 | |
3132 | lttng_dynamic_buffer_init(&msg_buffer); | |
3133 | ||
3647288f JG |
3134 | ret = lttng_dynamic_buffer_append(&msg_buffer, &msg_header, |
3135 | sizeof(msg_header)); | |
ab0ee2ca JG |
3136 | if (ret) { |
3137 | goto end; | |
3138 | } | |
3139 | ||
9b63a4aa | 3140 | ret = lttng_notification_serialize(¬ification, &msg_buffer); |
ab0ee2ca | 3141 | if (ret) { |
ab0ee2ca JG |
3142 | ERR("[notification-thread] Failed to serialize notification"); |
3143 | ret = -1; | |
3144 | goto end; | |
3145 | } | |
3146 | ||
3647288f JG |
3147 | /* Update payload size. */ |
3148 | ((struct lttng_notification_channel_message * ) msg_buffer.data)->size = | |
3149 | (uint32_t) (msg_buffer.size - sizeof(msg_header)); | |
3150 | ||
ab0ee2ca JG |
3151 | cds_list_for_each_entry_safe(client_list_element, tmp, |
3152 | &client_list->list, node) { | |
3153 | struct notification_client *client = | |
3154 | client_list_element->client; | |
3155 | ||
3156 | if (client->uid != channel_uid && client->gid != channel_gid && | |
3157 | client->uid != 0) { | |
3158 | /* Client is not allowed to monitor this channel. */ | |
3159 | DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this channel"); | |
3160 | continue; | |
3161 | } | |
3162 | ||
3163 | DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)", | |
3164 | client->socket, msg_buffer.size); | |
3165 | if (client->communication.outbound.buffer.size) { | |
3166 | /* | |
3167 | * Outgoing data is already buffered for this client; | |
3168 | * drop the notification and enqueue a "dropped | |
3169 | * notification" message if this is the first dropped | |
3170 | * notification since the socket spilled-over to the | |
3171 | * queue. | |
3172 | */ | |
3173 | DBG("[notification-thread] Dropping notification addressed to client (socket fd = %i)", | |
3174 | client->socket); | |
3175 | if (!client->communication.outbound.dropped_notification) { | |
3176 | client->communication.outbound.dropped_notification = true; | |
3177 | ret = client_enqueue_dropped_notification( | |
3178 | client, state); | |
3179 | if (ret) { | |
3180 | goto end; | |
3181 | } | |
3182 | } | |
3183 | continue; | |
3184 | } | |
3185 | ||
3186 | ret = lttng_dynamic_buffer_append_buffer( | |
3187 | &client->communication.outbound.buffer, | |
3188 | &msg_buffer); | |
3189 | if (ret) { | |
3190 | goto end; | |
3191 | } | |
3192 | ||
3193 | ret = client_flush_outgoing_queue(client, state); | |
3194 | if (ret) { | |
3195 | goto end; | |
3196 | } | |
3197 | } | |
3198 | ret = 0; | |
3199 | end: | |
ab0ee2ca JG |
3200 | lttng_dynamic_buffer_reset(&msg_buffer); |
3201 | return ret; | |
3202 | } | |
3203 | ||
3204 | int handle_notification_thread_channel_sample( | |
3205 | struct notification_thread_state *state, int pipe, | |
3206 | enum lttng_domain_type domain) | |
3207 | { | |
3208 | int ret = 0; | |
3209 | struct lttcomm_consumer_channel_monitor_msg sample_msg; | |
ab0ee2ca JG |
3210 | struct channel_info *channel_info; |
3211 | struct cds_lfht_node *node; | |
3212 | struct cds_lfht_iter iter; | |
3213 | struct lttng_channel_trigger_list *trigger_list; | |
3214 | struct lttng_trigger_list_element *trigger_list_element; | |
3215 | bool previous_sample_available = false; | |
e8360425 JD |
3216 | struct channel_state_sample previous_sample, latest_sample; |
3217 | uint64_t previous_session_consumed_total, latest_session_consumed_total; | |
ab0ee2ca JG |
3218 | |
3219 | /* | |
3220 | * The monitoring pipe only holds messages smaller than PIPE_BUF, | |
3221 | * ensuring that read/write of sampling messages are atomic. | |
3222 | */ | |
7c2551ef | 3223 | ret = lttng_read(pipe, &sample_msg, sizeof(sample_msg)); |
ab0ee2ca JG |
3224 | if (ret != sizeof(sample_msg)) { |
3225 | ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)", | |
3226 | pipe); | |
3227 | ret = -1; | |
3228 | goto end; | |
3229 | } | |
3230 | ||
3231 | ret = 0; | |
3232 | latest_sample.key.key = sample_msg.key; | |
3233 | latest_sample.key.domain = domain; | |
3234 | latest_sample.highest_usage = sample_msg.highest; | |
3235 | latest_sample.lowest_usage = sample_msg.lowest; | |
e8360425 | 3236 | latest_sample.channel_total_consumed = sample_msg.total_consumed; |
ab0ee2ca JG |
3237 | |
3238 | rcu_read_lock(); | |
3239 | ||
3240 | /* Retrieve the channel's informations */ | |
3241 | cds_lfht_lookup(state->channels_ht, | |
3242 | hash_channel_key(&latest_sample.key), | |
3243 | match_channel_info, | |
3244 | &latest_sample.key, | |
3245 | &iter); | |
3246 | node = cds_lfht_iter_get_node(&iter); | |
e0b5f87b | 3247 | if (caa_unlikely(!node)) { |
ab0ee2ca JG |
3248 | /* |
3249 | * Not an error since the consumer can push a sample to the pipe | |
3250 | * and the rest of the session daemon could notify us of the | |
3251 | * channel's destruction before we get a chance to process that | |
3252 | * sample. | |
3253 | */ | |
3254 | DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64 " in %s domain", | |
3255 | latest_sample.key.key, | |
3256 | domain == LTTNG_DOMAIN_KERNEL ? "kernel" : | |
3257 | "user space"); | |
3258 | goto end_unlock; | |
3259 | } | |
3260 | channel_info = caa_container_of(node, struct channel_info, | |
3261 | channels_ht_node); | |
e8360425 | 3262 | DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64 ") in session %s (highest usage = %" PRIu64 ", lowest usage = %" PRIu64", total consumed = %" PRIu64")", |
8abe313a | 3263 | channel_info->name, |
ab0ee2ca | 3264 | latest_sample.key.key, |
8abe313a | 3265 | channel_info->session_info->name, |
ab0ee2ca | 3266 | latest_sample.highest_usage, |
e8360425 JD |
3267 | latest_sample.lowest_usage, |
3268 | latest_sample.channel_total_consumed); | |
3269 | ||
3270 | previous_session_consumed_total = | |
3271 | channel_info->session_info->consumed_data_size; | |
ab0ee2ca JG |
3272 | |
3273 | /* Retrieve the channel's last sample, if it exists, and update it. */ | |
3274 | cds_lfht_lookup(state->channel_state_ht, | |
3275 | hash_channel_key(&latest_sample.key), | |
3276 | match_channel_state_sample, | |
3277 | &latest_sample.key, | |
3278 | &iter); | |
3279 | node = cds_lfht_iter_get_node(&iter); | |
e0b5f87b | 3280 | if (caa_likely(node)) { |
ab0ee2ca JG |
3281 | struct channel_state_sample *stored_sample; |
3282 | ||
3283 | /* Update the sample stored. */ | |
3284 | stored_sample = caa_container_of(node, | |
3285 | struct channel_state_sample, | |
3286 | channel_state_ht_node); | |
e8360425 | 3287 | |
ab0ee2ca JG |
3288 | memcpy(&previous_sample, stored_sample, |
3289 | sizeof(previous_sample)); | |
3290 | stored_sample->highest_usage = latest_sample.highest_usage; | |
3291 | stored_sample->lowest_usage = latest_sample.lowest_usage; | |
0f0479d7 | 3292 | stored_sample->channel_total_consumed = latest_sample.channel_total_consumed; |
ab0ee2ca | 3293 | previous_sample_available = true; |
e8360425 JD |
3294 | |
3295 | latest_session_consumed_total = | |
3296 | previous_session_consumed_total + | |
3297 | (latest_sample.channel_total_consumed - previous_sample.channel_total_consumed); | |
ab0ee2ca JG |
3298 | } else { |
3299 | /* | |
3300 | * This is the channel's first sample, allocate space for and | |
3301 | * store the new sample. | |
3302 | */ | |
3303 | struct channel_state_sample *stored_sample; | |
3304 | ||
3305 | stored_sample = zmalloc(sizeof(*stored_sample)); | |
3306 | if (!stored_sample) { | |
3307 | ret = -1; | |
3308 | goto end_unlock; | |
3309 | } | |
3310 | ||
3311 | memcpy(stored_sample, &latest_sample, sizeof(*stored_sample)); | |
3312 | cds_lfht_node_init(&stored_sample->channel_state_ht_node); | |
3313 | cds_lfht_add(state->channel_state_ht, | |
3314 | hash_channel_key(&stored_sample->key), | |
3315 | &stored_sample->channel_state_ht_node); | |
e8360425 JD |
3316 | |
3317 | latest_session_consumed_total = | |
3318 | previous_session_consumed_total + | |
3319 | latest_sample.channel_total_consumed; | |
ab0ee2ca JG |
3320 | } |
3321 | ||
e8360425 JD |
3322 | channel_info->session_info->consumed_data_size = |
3323 | latest_session_consumed_total; | |
3324 | ||
ab0ee2ca JG |
3325 | /* Find triggers associated with this channel. */ |
3326 | cds_lfht_lookup(state->channel_triggers_ht, | |
3327 | hash_channel_key(&latest_sample.key), | |
3328 | match_channel_trigger_list, | |
3329 | &latest_sample.key, | |
3330 | &iter); | |
3331 | node = cds_lfht_iter_get_node(&iter); | |
e0b5f87b | 3332 | if (caa_likely(!node)) { |
ab0ee2ca JG |
3333 | goto end_unlock; |
3334 | } | |
3335 | ||
3336 | trigger_list = caa_container_of(node, struct lttng_channel_trigger_list, | |
3337 | channel_triggers_ht_node); | |
3338 | cds_list_for_each_entry(trigger_list_element, &trigger_list->list, | |
3339 | node) { | |
9b63a4aa JG |
3340 | const struct lttng_condition *condition; |
3341 | const struct lttng_action *action; | |
3342 | const struct lttng_trigger *trigger; | |
ab0ee2ca JG |
3343 | struct notification_client_list *client_list; |
3344 | struct lttng_evaluation *evaluation = NULL; | |
3345 | ||
3346 | trigger = trigger_list_element->trigger; | |
9b63a4aa | 3347 | condition = lttng_trigger_get_const_condition(trigger); |
ab0ee2ca | 3348 | assert(condition); |
9b63a4aa | 3349 | action = lttng_trigger_get_const_action(trigger); |
ab0ee2ca JG |
3350 | |
3351 | /* Notify actions are the only type currently supported. */ | |
9b63a4aa | 3352 | assert(lttng_action_get_type_const(action) == |
ab0ee2ca JG |
3353 | LTTNG_ACTION_TYPE_NOTIFY); |
3354 | ||
3355 | /* | |
3356 | * Check if any client is subscribed to the result of this | |
3357 | * evaluation. | |
3358 | */ | |
ed327204 JG |
3359 | client_list = get_client_list_from_condition(state, condition); |
3360 | assert(client_list); | |
ab0ee2ca JG |
3361 | if (cds_list_empty(&client_list->list)) { |
3362 | /* | |
3363 | * No clients interested in the evaluation's result, | |
3364 | * skip it. | |
3365 | */ | |
3366 | continue; | |
3367 | } | |
3368 | ||
ed327204 | 3369 | ret = evaluate_buffer_condition(condition, &evaluation, state, |
ab0ee2ca | 3370 | previous_sample_available ? &previous_sample : NULL, |
e8360425 JD |
3371 | &latest_sample, |
3372 | previous_session_consumed_total, | |
3373 | latest_session_consumed_total, | |
3374 | channel_info); | |
3375 | if (caa_unlikely(ret)) { | |
ab0ee2ca JG |
3376 | goto end_unlock; |
3377 | } | |
3378 | ||
e8360425 | 3379 | if (caa_likely(!evaluation)) { |
ab0ee2ca JG |
3380 | continue; |
3381 | } | |
3382 | ||
3383 | /* Dispatch evaluation result to all clients. */ | |
3384 | ret = send_evaluation_to_clients(trigger_list_element->trigger, | |
3385 | evaluation, client_list, state, | |
8abe313a JG |
3386 | channel_info->session_info->uid, |
3387 | channel_info->session_info->gid); | |
3388 | lttng_evaluation_destroy(evaluation); | |
e0b5f87b | 3389 | if (caa_unlikely(ret)) { |
ab0ee2ca JG |
3390 | goto end_unlock; |
3391 | } | |
3392 | } | |
3393 | end_unlock: | |
3394 | rcu_read_unlock(); | |
3395 | end: | |
3396 | return ret; | |
3397 | } |