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