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