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