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