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