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