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;
882093ee
JR
3235 struct lttng_payload_view pv = lttng_payload_view_from_payload(
3236 &client->communication.outbound.payload, 0, -1);
3237 const int fds_to_send_count =
3238 lttng_payload_view_get_fd_handle_count(&pv);
ab0ee2ca 3239
505b2d90
JG
3240 ASSERT_LOCKED(client->lock);
3241
f2b3ef9f
JG
3242 if (!client->communication.active) {
3243 status = CLIENT_TRANSMISSION_STATUS_FAIL;
3244 goto end;
3245 }
3246
882093ee
JR
3247 if (pv.buffer.size == 0) {
3248 /*
3249 * If both data and fds are equal to zero, we are in an invalid
3250 * state.
3251 */
3252 assert(fds_to_send_count != 0);
3253 goto send_fds;
3254 }
3255
3256 /* Send data. */
3257 to_send_count = pv.buffer.size;
ab0ee2ca
JG
3258 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
3259 client->socket);
3260
3261 ret = lttcomm_send_unix_sock_non_block(client->socket,
882093ee 3262 pv.buffer.data,
ab0ee2ca 3263 to_send_count);
44c180ca 3264 if ((ret >= 0 && ret < to_send_count)) {
ab0ee2ca
JG
3265 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
3266 client->socket);
3267 to_send_count -= max(ret, 0);
3268
6f110534 3269 memmove(client->communication.outbound.payload.buffer.data,
882093ee
JR
3270 pv.buffer.data +
3271 pv.buffer.size - to_send_count,
ab0ee2ca
JG
3272 to_send_count);
3273 ret = lttng_dynamic_buffer_set_size(
882093ee 3274 &client->communication.outbound.payload.buffer,
ab0ee2ca
JG
3275 to_send_count);
3276 if (ret) {
3277 goto error;
3278 }
6c24d3fd 3279
dd877ea6 3280 status = CLIENT_TRANSMISSION_STATUS_QUEUED;
882093ee 3281 goto end;
ab0ee2ca 3282 } else if (ret < 0) {
6c24d3fd 3283 /* Generic error, disable the client's communication. */
dd877ea6 3284 ERR("[notification-thread] Failed to flush outgoing queue, disconnecting client (socket fd = %i)",
ab0ee2ca 3285 client->socket);
6c24d3fd 3286 client->communication.active = false;
dd877ea6 3287 status = CLIENT_TRANSMISSION_STATUS_FAIL;
882093ee 3288 goto end;
ab0ee2ca 3289 } else {
882093ee
JR
3290 /*
3291 * No error and flushed the queue completely.
3292 *
3293 * The payload buffer size is used later to
3294 * check if there is notifications queued. So albeit that the
3295 * direct caller knows that the transmission is complete, we
3296 * need to set the buffer size to zero.
3297 */
ab0ee2ca 3298 ret = lttng_dynamic_buffer_set_size(
882093ee 3299 &client->communication.outbound.payload.buffer, 0);
ab0ee2ca
JG
3300 if (ret) {
3301 goto error;
3302 }
882093ee 3303 }
6c24d3fd 3304
882093ee
JR
3305send_fds:
3306 /* No fds to send, transmission is complete. */
3307 if (fds_to_send_count == 0) {
dd877ea6 3308 status = CLIENT_TRANSMISSION_STATUS_COMPLETE;
882093ee 3309 goto end;
dd877ea6 3310 }
882093ee
JR
3311
3312 ret = lttcomm_send_payload_view_fds_unix_sock_non_block(
3313 client->socket, &pv);
3314 if (ret < 0) {
3315 /* Generic error, disable the client's communication. */
3316 ERR("[notification-thread] Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)",
3317 client->socket);
3318 client->communication.active = false;
3319 status = CLIENT_TRANSMISSION_STATUS_FAIL;
3320 goto end;
3321 } else if (ret == 0) {
3322 /* Nothing could be sent. */
3323 status = CLIENT_TRANSMISSION_STATUS_QUEUED;
3324 } else {
3325 /* Fd passing is an all or nothing kind of thing. */
3326 status = CLIENT_TRANSMISSION_STATUS_COMPLETE;
3327 /*
3328 * The payload _fd_array count is used later to
3329 * check if there is notifications queued. So although the
3330 * direct caller knows that the transmission is complete, we
3331 * need to clear the _fd_array for the queuing check.
3332 */
3333 lttng_dynamic_pointer_array_clear(
3334 &client->communication.outbound.payload
3335 ._fd_handles);
3336 }
3337
f2b3ef9f 3338end:
882093ee
JR
3339 if (status == CLIENT_TRANSMISSION_STATUS_COMPLETE) {
3340 client->communication.outbound.queued_command_reply = false;
3341 client->communication.outbound.dropped_notification = false;
3342 lttng_payload_clear(&client->communication.outbound.payload);
3343 }
3344
f2b3ef9f 3345 return status;
ab0ee2ca 3346error:
f2b3ef9f 3347 return CLIENT_TRANSMISSION_STATUS_ERROR;
ab0ee2ca
JG
3348}
3349
882093ee
JR
3350static
3351bool client_has_outbound_data_left(
3352 const struct notification_client *client)
3353{
3354 const struct lttng_payload_view pv = lttng_payload_view_from_payload(
3355 &client->communication.outbound.payload, 0, -1);
3356 const bool has_data = pv.buffer.size != 0;
3357 const bool has_fds = lttng_payload_view_get_fd_handle_count(&pv);
3358
3359 return has_data || has_fds;
3360}
3361
6c24d3fd 3362/* Client lock must _not_ be held by the caller. */
ab0ee2ca
JG
3363static
3364int client_send_command_reply(struct notification_client *client,
3365 struct notification_thread_state *state,
3366 enum lttng_notification_channel_status status)
3367{
3368 int ret;
3369 struct lttng_notification_channel_command_reply reply = {
3370 .status = (int8_t) status,
3371 };
3372 struct lttng_notification_channel_message msg = {
3373 .type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY,
3374 .size = sizeof(reply),
3375 };
3376 char buffer[sizeof(msg) + sizeof(reply)];
f2b3ef9f 3377 enum client_transmission_status transmission_status;
ab0ee2ca 3378
6c24d3fd
JG
3379 memcpy(buffer, &msg, sizeof(msg));
3380 memcpy(buffer + sizeof(msg), &reply, sizeof(reply));
3381 DBG("[notification-thread] Send command reply (%i)", (int) status);
505b2d90 3382
6c24d3fd 3383 pthread_mutex_lock(&client->lock);
ab0ee2ca
JG
3384 if (client->communication.outbound.queued_command_reply) {
3385 /* Protocol error. */
6c24d3fd 3386 goto error_unlock;
ab0ee2ca
JG
3387 }
3388
ab0ee2ca
JG
3389 /* Enqueue buffer to outgoing queue and flush it. */
3390 ret = lttng_dynamic_buffer_append(
882093ee 3391 &client->communication.outbound.payload.buffer,
ab0ee2ca
JG
3392 buffer, sizeof(buffer));
3393 if (ret) {
6c24d3fd 3394 goto error_unlock;
ab0ee2ca
JG
3395 }
3396
f2b3ef9f 3397 transmission_status = client_flush_outgoing_queue(client);
882093ee
JR
3398
3399 if (client_has_outbound_data_left(client)) {
6c24d3fd
JG
3400 /* Queue could not be emptied. */
3401 client->communication.outbound.queued_command_reply = true;
3402 }
3403
3404 pthread_mutex_unlock(&client->lock);
f2b3ef9f
JG
3405 ret = client_handle_transmission_status(
3406 client, transmission_status, state);
ab0ee2ca
JG
3407 if (ret) {
3408 goto error;
3409 }
3410
ab0ee2ca 3411 return 0;
6c24d3fd
JG
3412error_unlock:
3413 pthread_mutex_unlock(&client->lock);
ab0ee2ca
JG
3414error:
3415 return -1;
3416}
3417
505b2d90
JG
3418static
3419int client_handle_message_unknown(struct notification_client *client,
3420 struct notification_thread_state *state)
3421{
3422 int ret;
505b2d90
JG
3423 /*
3424 * Receiving message header. The function will be called again
3425 * once the rest of the message as been received and can be
3426 * interpreted.
3427 */
3428 const struct lttng_notification_channel_message *msg;
3429
882093ee 3430 assert(sizeof(*msg) == client->communication.inbound.payload.buffer.size);
505b2d90 3431 msg = (const struct lttng_notification_channel_message *)
882093ee 3432 client->communication.inbound.payload.buffer.data;
505b2d90
JG
3433
3434 if (msg->size == 0 ||
3435 msg->size > DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE) {
3436 ERR("[notification-thread] Invalid notification channel message: length = %u",
3437 msg->size);
3438 ret = -1;
3439 goto end;
3440 }
3441
3442 switch (msg->type) {
3443 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE:
3444 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE:
3445 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE:
3446 break;
3447 default:
3448 ret = -1;
3449 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
3450 goto end;
3451 }
3452
3453 client->communication.inbound.bytes_to_receive = msg->size;
882093ee 3454 client->communication.inbound.fds_to_receive = msg->fds;
505b2d90
JG
3455 client->communication.inbound.msg_type =
3456 (enum lttng_notification_channel_message_type) msg->type;
3457 ret = lttng_dynamic_buffer_set_size(
882093ee
JR
3458 &client->communication.inbound.payload.buffer, msg->size);
3459
3460 /* msg is not valid anymore due to lttng_dynamic_buffer_set_size. */
3461 msg = NULL;
505b2d90 3462end:
505b2d90
JG
3463 return ret;
3464}
3465
3466static
3467int client_handle_message_handshake(struct notification_client *client,
3468 struct notification_thread_state *state)
3469{
3470 int ret;
3471 struct lttng_notification_channel_command_handshake *handshake_client;
3472 const struct lttng_notification_channel_command_handshake handshake_reply = {
3473 .major = LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR,
3474 .minor = LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR,
3475 };
3476 const struct lttng_notification_channel_message msg_header = {
3477 .type = LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE,
3478 .size = sizeof(handshake_reply),
3479 };
3480 enum lttng_notification_channel_status status =
3481 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK;
3482 char send_buffer[sizeof(msg_header) + sizeof(handshake_reply)];
505b2d90
JG
3483
3484 memcpy(send_buffer, &msg_header, sizeof(msg_header));
3485 memcpy(send_buffer + sizeof(msg_header), &handshake_reply,
3486 sizeof(handshake_reply));
3487
3488 handshake_client =
3489 (struct lttng_notification_channel_command_handshake *)
882093ee 3490 client->communication.inbound.payload.buffer
505b2d90
JG
3491 .data;
3492 client->major = handshake_client->major;
3493 client->minor = handshake_client->minor;
3494 if (!client->communication.inbound.creds_received) {
3495 ERR("[notification-thread] No credentials received from client");
3496 ret = -1;
3497 goto end;
3498 }
3499
3500 client->uid = LTTNG_SOCK_GET_UID_CRED(
3501 &client->communication.inbound.creds);
3502 client->gid = LTTNG_SOCK_GET_GID_CRED(
3503 &client->communication.inbound.creds);
3504 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
3505 client->uid, client->gid, (int) client->major,
3506 (int) client->minor);
3507
3508 if (handshake_client->major !=
3509 LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR) {
3510 status = LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION;
3511 }
3512
6c24d3fd
JG
3513 pthread_mutex_lock(&client->lock);
3514 /* Outgoing queue will be flushed when the command reply is sent. */
505b2d90 3515 ret = lttng_dynamic_buffer_append(
882093ee 3516 &client->communication.outbound.payload.buffer, send_buffer,
505b2d90
JG
3517 sizeof(send_buffer));
3518 if (ret) {
3519 ERR("[notification-thread] Failed to send protocol version to notification channel client");
6c24d3fd 3520 goto end_unlock;
505b2d90
JG
3521 }
3522
3523 client->validated = true;
3524 client->communication.active = true;
6c24d3fd 3525 pthread_mutex_unlock(&client->lock);
505b2d90 3526
6c24d3fd
JG
3527 /* Set reception state to receive the next message header. */
3528 ret = client_reset_inbound_state(client);
505b2d90 3529 if (ret) {
6c24d3fd 3530 ERR("[notification-thread] Failed to reset client communication's inbound state");
505b2d90
JG
3531 goto end;
3532 }
3533
6c24d3fd 3534 /* Flushes the outgoing queue. */
505b2d90
JG
3535 ret = client_send_command_reply(client, state, status);
3536 if (ret) {
3537 ERR("[notification-thread] Failed to send reply to notification channel client");
3538 goto end;
3539 }
3540
6c24d3fd
JG
3541 goto end;
3542end_unlock:
505b2d90 3543 pthread_mutex_unlock(&client->lock);
6c24d3fd 3544end:
505b2d90
JG
3545 return ret;
3546}
3547
3548static
3549int client_handle_message_subscription(
3550 struct notification_client *client,
3551 enum lttng_notification_channel_message_type msg_type,
3552 struct notification_thread_state *state)
3553{
3554 int ret;
3555 struct lttng_condition *condition;
3556 enum lttng_notification_channel_status status =
3557 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK;
3558 struct lttng_payload_view condition_view =
882093ee
JR
3559 lttng_payload_view_from_payload(
3560 &client->communication.inbound.payload,
505b2d90
JG
3561 0, -1);
3562 size_t expected_condition_size;
3563
6c24d3fd
JG
3564 /*
3565 * No need to lock client to sample the inbound state as the only
3566 * other thread accessing clients (action executor) only uses the
3567 * outbound state.
3568 */
882093ee 3569 expected_condition_size = client->communication.inbound.payload.buffer.size;
505b2d90
JG
3570 ret = lttng_condition_create_from_payload(&condition_view, &condition);
3571 if (ret != expected_condition_size) {
3572 ERR("[notification-thread] Malformed condition received from client");
3573 goto end;
3574 }
3575
3576 if (msg_type == LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE) {
3577 ret = notification_thread_client_subscribe(
3578 client, condition, state, &status);
3579 } else {
3580 ret = notification_thread_client_unsubscribe(
3581 client, condition, state, &status);
3582 }
505b2d90 3583
505b2d90 3584 if (ret) {
6c24d3fd 3585 goto end;
505b2d90
JG
3586 }
3587
3588 /* Set reception state to receive the next message header. */
3589 ret = client_reset_inbound_state(client);
3590 if (ret) {
3591 ERR("[notification-thread] Failed to reset client communication's inbound state");
6c24d3fd
JG
3592 goto end;
3593 }
3594
3595 ret = client_send_command_reply(client, state, status);
3596 if (ret) {
3597 ERR("[notification-thread] Failed to send reply to notification channel client");
3598 goto end;
505b2d90
JG
3599 }
3600
505b2d90
JG
3601end:
3602 return ret;
3603}
3604
ab0ee2ca
JG
3605static
3606int client_dispatch_message(struct notification_client *client,
3607 struct notification_thread_state *state)
3608{
3609 int ret = 0;
3610
3611 if (client->communication.inbound.msg_type !=
3612 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE &&
3613 client->communication.inbound.msg_type !=
3614 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN &&
3615 !client->validated) {
3616 WARN("[notification-thread] client attempted a command before handshake");
3617 ret = -1;
3618 goto end;
3619 }
3620
3621 switch (client->communication.inbound.msg_type) {
3622 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN:
3623 {
505b2d90 3624 ret = client_handle_message_unknown(client, state);
ab0ee2ca
JG
3625 break;
3626 }
3627 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE:
3628 {
505b2d90 3629 ret = client_handle_message_handshake(client, state);
ab0ee2ca
JG
3630 break;
3631 }
3632 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE:
3633 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE:
3634 {
505b2d90
JG
3635 ret = client_handle_message_subscription(client,
3636 client->communication.inbound.msg_type, state);
ab0ee2ca
JG
3637 break;
3638 }
3639 default:
3640 abort();
3641 }
3642end:
3643 return ret;
3644}
3645
3646/* Incoming data from client. */
3647int handle_notification_thread_client_in(
3648 struct notification_thread_state *state, int socket)
3649{
14fa22f8 3650 int ret = 0;
ab0ee2ca
JG
3651 struct notification_client *client;
3652 ssize_t recv_ret;
3653 size_t offset;
3654
98b5ff34 3655 rcu_read_lock();
ab0ee2ca
JG
3656 client = get_client_from_socket(socket, state);
3657 if (!client) {
3658 /* Internal error, abort. */
3659 ret = -1;
3660 goto end;
3661 }
3662
882093ee 3663 offset = client->communication.inbound.payload.buffer.size -
14fa22f8 3664 client->communication.inbound.bytes_to_receive;
01ea340e 3665 if (client->communication.inbound.expect_creds) {
ab0ee2ca 3666 recv_ret = lttcomm_recv_creds_unix_sock(socket,
882093ee 3667 client->communication.inbound.payload.buffer.data + offset,
ab0ee2ca
JG
3668 client->communication.inbound.bytes_to_receive,
3669 &client->communication.inbound.creds);
3670 if (recv_ret > 0) {
01ea340e 3671 client->communication.inbound.expect_creds = false;
ab0ee2ca
JG
3672 client->communication.inbound.creds_received = true;
3673 }
3674 } else {
3675 recv_ret = lttcomm_recv_unix_sock_non_block(socket,
882093ee 3676 client->communication.inbound.payload.buffer.data + offset,
ab0ee2ca
JG
3677 client->communication.inbound.bytes_to_receive);
3678 }
505b2d90
JG
3679 if (recv_ret >= 0) {
3680 client->communication.inbound.bytes_to_receive -= recv_ret;
882093ee
JR
3681 } else {
3682 goto error_disconnect_client;
505b2d90 3683 }
6c24d3fd 3684
882093ee
JR
3685 if (client->communication.inbound.bytes_to_receive != 0) {
3686 /* Message incomplete wait for more data. */
3687 ret = 0;
3688 goto end;
ab0ee2ca
JG
3689 }
3690
882093ee
JR
3691 assert(client->communication.inbound.bytes_to_receive == 0);
3692
3693 /* Receive fds. */
3694 if (client->communication.inbound.fds_to_receive != 0) {
3695 ret = lttcomm_recv_payload_fds_unix_sock_non_block(
3696 client->socket,
3697 client->communication.inbound.fds_to_receive,
3698 &client->communication.inbound.payload);
3699 if (ret > 0) {
ab0ee2ca 3700 /*
882093ee
JR
3701 * Fds received. non blocking fds passing is all
3702 * or nothing.
ab0ee2ca 3703 */
882093ee
JR
3704 ssize_t expected_size;
3705
3706 expected_size = sizeof(int) *
3707 client->communication.inbound
3708 .fds_to_receive;
3709 assert(ret == expected_size);
3710 client->communication.inbound.fds_to_receive = 0;
3711 } else if (ret == 0) {
3712 /* Received nothing. */
3713 ret = 0;
3714 goto end;
3715 } else {
ab0ee2ca
JG
3716 goto error_disconnect_client;
3717 }
ab0ee2ca 3718 }
882093ee
JR
3719
3720 /* At this point the message is complete.*/
3721 assert(client->communication.inbound.bytes_to_receive == 0 &&
3722 client->communication.inbound.fds_to_receive == 0);
3723 ret = client_dispatch_message(client, state);
3724 if (ret) {
3725 /*
3726 * Only returns an error if this client must be
3727 * disconnected.
3728 */
3729 goto error_disconnect_client;
3730 }
3731
ab0ee2ca 3732end:
98b5ff34 3733 rcu_read_unlock();
ab0ee2ca 3734 return ret;
882093ee 3735
ab0ee2ca 3736error_disconnect_client:
505b2d90 3737 ret = notification_thread_client_disconnect(client, state);
98b5ff34 3738 goto end;
ab0ee2ca
JG
3739}
3740
3741/* Client ready to receive outgoing data. */
3742int handle_notification_thread_client_out(
3743 struct notification_thread_state *state, int socket)
3744{
3745 int ret;
3746 struct notification_client *client;
f2b3ef9f 3747 enum client_transmission_status transmission_status;
ab0ee2ca 3748
98b5ff34 3749 rcu_read_lock();
ab0ee2ca
JG
3750 client = get_client_from_socket(socket, state);
3751 if (!client) {
3752 /* Internal error, abort. */
3753 ret = -1;
3754 goto end;
3755 }
3756
505b2d90 3757 pthread_mutex_lock(&client->lock);
f2b3ef9f 3758 transmission_status = client_flush_outgoing_queue(client);
6c24d3fd
JG
3759 pthread_mutex_unlock(&client->lock);
3760
f2b3ef9f
JG
3761 ret = client_handle_transmission_status(
3762 client, transmission_status, state);
ab0ee2ca
JG
3763 if (ret) {
3764 goto end;
3765 }
3766end:
98b5ff34 3767 rcu_read_unlock();
ab0ee2ca
JG
3768 return ret;
3769}
3770
3771static
e8360425
JD
3772bool evaluate_buffer_usage_condition(const struct lttng_condition *condition,
3773 const struct channel_state_sample *sample,
3774 uint64_t buffer_capacity)
ab0ee2ca
JG
3775{
3776 bool result = false;
3777 uint64_t threshold;
3778 enum lttng_condition_type condition_type;
e8360425 3779 const struct lttng_condition_buffer_usage *use_condition = container_of(
ab0ee2ca
JG
3780 condition, struct lttng_condition_buffer_usage,
3781 parent);
3782
ab0ee2ca
JG
3783 if (use_condition->threshold_bytes.set) {
3784 threshold = use_condition->threshold_bytes.value;
3785 } else {
3786 /*
3787 * Threshold was expressed as a ratio.
3788 *
3789 * TODO the threshold (in bytes) of conditions expressed
3790 * as a ratio of total buffer size could be cached to
3791 * forego this double-multiplication or it could be performed
3792 * as fixed-point math.
3793 *
d49487dc 3794 * Note that caching should accommodates the case where the
ab0ee2ca
JG
3795 * condition applies to multiple channels (i.e. don't assume
3796 * that all channels matching my_chann* have the same size...)
3797 */
3798 threshold = (uint64_t) (use_condition->threshold_ratio.value *
3799 (double) buffer_capacity);
3800 }
3801
3802 condition_type = lttng_condition_get_type(condition);
3803 if (condition_type == LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW) {
3804 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64 ", highest usage = %" PRIu64,
3805 threshold, sample->highest_usage);
3806
3807 /*
3808 * The low condition should only be triggered once _all_ of the
3809 * streams in a channel have gone below the "low" threshold.
3810 */
3811 if (sample->highest_usage <= threshold) {
3812 result = true;
3813 }
3814 } else {
3815 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64 ", highest usage = %" PRIu64,
3816 threshold, sample->highest_usage);
3817
3818 /*
3819 * For high buffer usage scenarios, we want to trigger whenever
3820 * _any_ of the streams has reached the "high" threshold.
3821 */
3822 if (sample->highest_usage >= threshold) {
3823 result = true;
3824 }
3825 }
e8360425 3826
ab0ee2ca
JG
3827 return result;
3828}
3829
3830static
e8360425
JD
3831bool evaluate_session_consumed_size_condition(
3832 const struct lttng_condition *condition,
3833 uint64_t session_consumed_size)
3834{
3835 uint64_t threshold;
3836 const struct lttng_condition_session_consumed_size *size_condition =
3837 container_of(condition,
3838 struct lttng_condition_session_consumed_size,
3839 parent);
3840
3841 threshold = size_condition->consumed_threshold_bytes.value;
3842 DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64 ", current size = %" PRIu64,
3843 threshold, session_consumed_size);
3844 return session_consumed_size >= threshold;
3845}
3846
3847static
ed327204 3848int evaluate_buffer_condition(const struct lttng_condition *condition,
ab0ee2ca 3849 struct lttng_evaluation **evaluation,
e8360425
JD
3850 const struct notification_thread_state *state,
3851 const struct channel_state_sample *previous_sample,
3852 const struct channel_state_sample *latest_sample,
3853 uint64_t previous_session_consumed_total,
3854 uint64_t latest_session_consumed_total,
3855 struct channel_info *channel_info)
ab0ee2ca
JG
3856{
3857 int ret = 0;
3858 enum lttng_condition_type condition_type;
e8360425
JD
3859 const bool previous_sample_available = !!previous_sample;
3860 bool previous_sample_result = false;
ab0ee2ca
JG
3861 bool latest_sample_result;
3862
3863 condition_type = lttng_condition_get_type(condition);
ab0ee2ca 3864
e8360425
JD
3865 switch (condition_type) {
3866 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
3867 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
3868 if (caa_likely(previous_sample_available)) {
3869 previous_sample_result =
3870 evaluate_buffer_usage_condition(condition,
3871 previous_sample, channel_info->capacity);
3872 }
3873 latest_sample_result = evaluate_buffer_usage_condition(
3874 condition, latest_sample,
3875 channel_info->capacity);
3876 break;
3877 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
3878 if (caa_likely(previous_sample_available)) {
3879 previous_sample_result =
3880 evaluate_session_consumed_size_condition(
3881 condition,
3882 previous_session_consumed_total);
3883 }
3884 latest_sample_result =
3885 evaluate_session_consumed_size_condition(
3886 condition,
3887 latest_session_consumed_total);
3888 break;
3889 default:
3890 /* Unknown condition type; internal error. */
3891 abort();
3892 }
ab0ee2ca
JG
3893
3894 if (!latest_sample_result ||
3895 (previous_sample_result == latest_sample_result)) {
3896 /*
3897 * Only trigger on a condition evaluation transition.
3898 *
3899 * NOTE: This edge-triggered logic may not be appropriate for
3900 * future condition types.
3901 */
3902 goto end;
3903 }
3904
e8360425
JD
3905 if (!evaluation || !latest_sample_result) {
3906 goto end;
3907 }
3908
3909 switch (condition_type) {
3910 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
3911 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
ab0ee2ca
JG
3912 *evaluation = lttng_evaluation_buffer_usage_create(
3913 condition_type,
3914 latest_sample->highest_usage,
e8360425
JD
3915 channel_info->capacity);
3916 break;
3917 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
3918 *evaluation = lttng_evaluation_session_consumed_size_create(
e8360425
JD
3919 latest_session_consumed_total);
3920 break;
3921 default:
3922 abort();
3923 }
3924
3925 if (!*evaluation) {
3926 ret = -1;
3927 goto end;
ab0ee2ca
JG
3928 }
3929end:
3930 return ret;
3931}
3932
3933static
f2b3ef9f 3934int client_notification_overflow(struct notification_client *client)
ab0ee2ca 3935{
f2b3ef9f
JG
3936 int ret = 0;
3937 const struct lttng_notification_channel_message msg = {
ab0ee2ca 3938 .type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED,
ab0ee2ca
JG
3939 };
3940
505b2d90
JG
3941 ASSERT_LOCKED(client->lock);
3942
f2b3ef9f
JG
3943 DBG("Dropping notification addressed to client (socket fd = %i)",
3944 client->socket);
3945 if (client->communication.outbound.dropped_notification) {
3946 /*
3947 * The client already has a "notification dropped" message
3948 * in its outgoing queue. Nothing to do since all
3949 * of those messages are coalesced.
3950 */
3951 goto end;
3952 }
3953
3954 client->communication.outbound.dropped_notification = true;
ab0ee2ca 3955 ret = lttng_dynamic_buffer_append(
882093ee 3956 &client->communication.outbound.payload.buffer, &msg,
ab0ee2ca 3957 sizeof(msg));
f2b3ef9f
JG
3958 if (ret) {
3959 PERROR("Failed to enqueue \"dropped notification\" message in client's (socket fd = %i) outgoing queue",
3960 client->socket);
3961 }
3962end:
ab0ee2ca
JG
3963 return ret;
3964}
3965
f2b3ef9f
JG
3966static int client_handle_transmission_status_wrapper(
3967 struct notification_client *client,
3968 enum client_transmission_status status,
3969 void *user_data)
3970{
3971 return client_handle_transmission_status(client, status,
3972 (struct notification_thread_state *) user_data);
3973}
3974
3975static
3976int send_evaluation_to_clients(const struct lttng_trigger *trigger,
3977 const struct lttng_evaluation *evaluation,
3978 struct notification_client_list* client_list,
3979 struct notification_thread_state *state,
3980 uid_t object_uid, gid_t object_gid)
3981{
ff588497
JR
3982 const struct lttng_credentials creds = {
3983 .uid = LTTNG_OPTIONAL_INIT_VALUE(object_uid),
3984 .gid = LTTNG_OPTIONAL_INIT_VALUE(object_gid),
3985 };
3986
f2b3ef9f
JG
3987 return notification_client_list_send_evaluation(client_list,
3988 lttng_trigger_get_const_condition(trigger), evaluation,
3989 lttng_trigger_get_credentials(trigger),
ff588497 3990 &creds,
f2b3ef9f
JG
3991 client_handle_transmission_status_wrapper, state);
3992}
3993
f37d0f86
JG
3994/*
3995 * Permission checks relative to notification channel clients are performed
3996 * here. Notice how object, client, and trigger credentials are involved in
3997 * this check.
3998 *
3999 * The `object` credentials are the credentials associated with the "subject"
4000 * of a condition. For instance, a `rotation completed` condition applies
4001 * to a session. When that condition is met, it will produce an evaluation
4002 * against a session. Hence, in this case, the `object` credentials are the
4003 * credentials of the "subject" session.
4004 *
4005 * The `trigger` credentials are the credentials of the user that registered the
4006 * trigger.
4007 *
4008 * The `client` credentials are the credentials of the user that created a given
4009 * notification channel.
4010 *
4011 * In terms of visibility, it is expected that non-privilieged users can only
4012 * register triggers against "their" objects (their own sessions and
4013 * applications they are allowed to interact with). They can then open a
4014 * notification channel and subscribe to notifications associated with those
4015 * triggers.
4016 *
4017 * As for privilieged users, they can register triggers against the objects of
4018 * other users. They can then subscribe to the notifications associated to their
4019 * triggers. Privilieged users _can't_ subscribe to the notifications of
4020 * triggers owned by other users; they must create their own triggers.
4021 *
4022 * This is more a concern of usability than security. It would be difficult for
4023 * a root user reliably subscribe to a specific set of conditions without
4024 * interference from external users (those could, for instance, unregister
4025 * their triggers).
4026 */
f2b3ef9f
JG
4027LTTNG_HIDDEN
4028int notification_client_list_send_evaluation(
4029 struct notification_client_list *client_list,
4030 const struct lttng_condition *condition,
9b63a4aa 4031 const struct lttng_evaluation *evaluation,
f2b3ef9f
JG
4032 const struct lttng_credentials *trigger_creds,
4033 const struct lttng_credentials *source_object_creds,
4034 report_client_transmission_result_cb client_report,
4035 void *user_data)
ab0ee2ca
JG
4036{
4037 int ret = 0;
c0a66c84 4038 struct lttng_payload msg_payload;
ab0ee2ca 4039 struct notification_client_list_element *client_list_element, *tmp;
9b63a4aa 4040 const struct lttng_notification notification = {
f2b3ef9f 4041 .condition = (struct lttng_condition *) condition,
9b63a4aa
JG
4042 .evaluation = (struct lttng_evaluation *) evaluation,
4043 };
3647288f
JG
4044 struct lttng_notification_channel_message msg_header = {
4045 .type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION,
4046 };
ab0ee2ca 4047
c0a66c84 4048 lttng_payload_init(&msg_payload);
ab0ee2ca 4049
c0a66c84 4050 ret = lttng_dynamic_buffer_append(&msg_payload.buffer, &msg_header,
3647288f 4051 sizeof(msg_header));
ab0ee2ca
JG
4052 if (ret) {
4053 goto end;
4054 }
4055
c0a66c84 4056 ret = lttng_notification_serialize(&notification, &msg_payload);
ab0ee2ca 4057 if (ret) {
ab0ee2ca
JG
4058 ERR("[notification-thread] Failed to serialize notification");
4059 ret = -1;
4060 goto end;
4061 }
4062
3647288f 4063 /* Update payload size. */
505b2d90
JG
4064 ((struct lttng_notification_channel_message *) msg_payload.buffer.data)
4065 ->size = (uint32_t)(
4066 msg_payload.buffer.size - sizeof(msg_header));
3647288f 4067
882093ee
JR
4068 /* Update the payload number of fds. */
4069 {
4070 const struct lttng_payload_view pv = lttng_payload_view_from_payload(
4071 &msg_payload, 0, -1);
4072
4073 ((struct lttng_notification_channel_message *)
4074 msg_payload.buffer.data)->fds = (uint32_t)
4075 lttng_payload_view_get_fd_handle_count(&pv);
4076 }
4077
505b2d90 4078 pthread_mutex_lock(&client_list->lock);
ab0ee2ca
JG
4079 cds_list_for_each_entry_safe(client_list_element, tmp,
4080 &client_list->list, node) {
f2b3ef9f 4081 enum client_transmission_status transmission_status;
ab0ee2ca
JG
4082 struct notification_client *client =
4083 client_list_element->client;
4084
505b2d90
JG
4085 ret = 0;
4086 pthread_mutex_lock(&client->lock);
6c24d3fd
JG
4087 if (!client->communication.active) {
4088 /*
4089 * Skip inactive client (protocol error or
4090 * disconnecting).
4091 */
4092 DBG("Skipping client at it is marked as inactive");
4093 goto skip_client;
4094 }
4095
f2b3ef9f 4096 if (source_object_creds) {
ff588497
JR
4097 if (client->uid != lttng_credentials_get_uid(source_object_creds) &&
4098 client->gid != lttng_credentials_get_gid(source_object_creds) &&
f2b3ef9f
JG
4099 client->uid != 0) {
4100 /*
4101 * Client is not allowed to monitor this
4102 * object.
4103 */
4104 DBG("[notification-thread] Skipping client at it does not have the object permission to receive notification for this trigger");
6c24d3fd 4105 goto skip_client;
f2b3ef9f 4106 }
90843f49
JR
4107 }
4108
ff588497 4109 if (client->uid != lttng_credentials_get_uid(trigger_creds) && client->gid != lttng_credentials_get_gid(trigger_creds)) {
90843f49 4110 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this trigger");
6c24d3fd 4111 goto skip_client;
ab0ee2ca
JG
4112 }
4113
4114 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
c0a66c84 4115 client->socket, msg_payload.buffer.size);
882093ee
JR
4116
4117 if (client_has_outbound_data_left(client)) {
ab0ee2ca
JG
4118 /*
4119 * Outgoing data is already buffered for this client;
4120 * drop the notification and enqueue a "dropped
4121 * notification" message if this is the first dropped
4122 * notification since the socket spilled-over to the
4123 * queue.
4124 */
f2b3ef9f
JG
4125 ret = client_notification_overflow(client);
4126 if (ret) {
6c24d3fd
JG
4127 /* Fatal error. */
4128 goto skip_client;
ab0ee2ca 4129 }
ab0ee2ca
JG
4130 }
4131
882093ee 4132 ret = lttng_payload_copy(&msg_payload, &client->communication.outbound.payload);
ab0ee2ca 4133 if (ret) {
6c24d3fd
JG
4134 /* Fatal error. */
4135 goto skip_client;
ab0ee2ca
JG
4136 }
4137
f2b3ef9f 4138 transmission_status = client_flush_outgoing_queue(client);
6c24d3fd 4139 pthread_mutex_unlock(&client->lock);
f2b3ef9f 4140 ret = client_report(client, transmission_status, user_data);
ab0ee2ca 4141 if (ret) {
6c24d3fd
JG
4142 /* Fatal error. */
4143 goto end_unlock_list;
505b2d90 4144 }
6c24d3fd
JG
4145
4146 continue;
4147
4148skip_client:
505b2d90
JG
4149 pthread_mutex_unlock(&client->lock);
4150 if (ret) {
6c24d3fd 4151 /* Fatal error. */
505b2d90 4152 goto end_unlock_list;
ab0ee2ca
JG
4153 }
4154 }
4155 ret = 0;
505b2d90
JG
4156
4157end_unlock_list:
4158 pthread_mutex_unlock(&client_list->lock);
ab0ee2ca 4159end:
c0a66c84 4160 lttng_payload_reset(&msg_payload);
ab0ee2ca
JG
4161 return ret;
4162}
4163
b9a8d78f
FD
4164static struct lttng_event_notifier_notification *receive_notification(
4165 int notification_pipe_read_fd, enum lttng_domain_type domain)
94078603
JR
4166{
4167 int ret;
b9a8d78f
FD
4168 uint64_t token;
4169 struct lttng_event_notifier_notification *notification = NULL;
94078603
JR
4170 void *reception_buffer;
4171 size_t reception_size;
4172
b9a8d78f
FD
4173 struct lttng_ust_event_notifier_notification ust_notification;
4174 struct lttng_kernel_event_notifier_notification kernel_notification;
94078603 4175
b9a8d78f 4176 /* Init lttng_event_notifier_notification */
94078603
JR
4177 switch(domain) {
4178 case LTTNG_DOMAIN_UST:
4179 reception_buffer = (void *) &ust_notification;
4180 reception_size = sizeof(ust_notification);
94078603
JR
4181 break;
4182 case LTTNG_DOMAIN_KERNEL:
4183 reception_buffer = (void *) &kernel_notification;
4184 reception_size = sizeof(kernel_notification);
94078603
JR
4185 break;
4186 default:
4187 abort();
4188 }
4189
4190 /*
4191 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4192 * ensuring that read/write of tracer notifications are atomic.
4193 */
4194 ret = lttng_read(notification_pipe_read_fd, reception_buffer,
4195 reception_size);
4196 if (ret != reception_size) {
4197 PERROR("Failed to read from event source notification pipe: fd = %d, size to read = %zu, ret = %d",
4198 notification_pipe_read_fd, reception_size, ret);
4199 ret = -1;
4200 goto end;
4201 }
4202
4203 switch(domain) {
4204 case LTTNG_DOMAIN_UST:
b9a8d78f 4205 token = ust_notification.token;
94078603
JR
4206 break;
4207 case LTTNG_DOMAIN_KERNEL:
b9a8d78f 4208 token = kernel_notification.token;
94078603
JR
4209 break;
4210 default:
4211 abort();
4212 }
4213
b9a8d78f
FD
4214 notification = lttng_event_notifier_notification_create(
4215 token, domain);
4216end:
4217 return notification;
4218}
4219
4220int handle_notification_thread_event_notification(struct notification_thread_state *state,
4221 int pipe,
4222 enum lttng_domain_type domain)
4223{
4224 int ret;
4225 enum lttng_trigger_status trigger_status;
4226 struct cds_lfht_node *node;
4227 struct cds_lfht_iter iter;
4228 struct notification_trigger_tokens_ht_element *element;
4229 struct lttng_evaluation *evaluation = NULL;
4230 struct lttng_event_notifier_notification *notification = NULL;
4231 enum action_executor_status executor_status;
4232 struct notification_client_list *client_list = NULL;
4233 const char *trigger_name;
4234
4235 notification = receive_notification(pipe, domain);
4236 if (notification == NULL) {
4237 ERR("[notification-thread] Error receiving notification from tracer (fd = %i, domain = %s)",
4238 pipe, lttng_domain_type_str(domain));
4239 ret = -1;
4240 goto end;
4241 }
4242
94078603
JR
4243 /* Find triggers associated with this token. */
4244 rcu_read_lock();
4245 cds_lfht_lookup(state->trigger_tokens_ht,
b9a8d78f
FD
4246 hash_key_u64(&notification->tracer_token, lttng_ht_seed),
4247 match_trigger_token, &notification->tracer_token, &iter);
94078603 4248 node = cds_lfht_iter_get_node(&iter);
f1446131 4249 if (caa_unlikely(!node)) {
94078603 4250 /*
f1446131
JR
4251 * This is not an error, slow consumption of the tracer
4252 * notifications can lead to situations where a trigger is
4253 * removed but we still get tracer notifications matching a
4254 * trigger that no longer exists.
94078603
JR
4255 */
4256 ret = 0;
4257 goto end_unlock;
4258 }
4259
4260 element = caa_container_of(node,
4261 struct notification_trigger_tokens_ht_element,
4262 node);
4263
f1446131
JR
4264 if (!lttng_trigger_should_fire(element->trigger)) {
4265 ret = 0;
4266 goto end_unlock;
4267 }
94078603 4268
f1446131 4269 lttng_trigger_fire(element->trigger);
94078603 4270
b9a8d78f
FD
4271 trigger_status = lttng_trigger_get_name(element->trigger, &trigger_name);
4272 assert(trigger_status == LTTNG_TRIGGER_STATUS_OK);
4273
4274 evaluation = lttng_evaluation_event_rule_create(
4275 trigger_name);
f1446131 4276 if (evaluation == NULL) {
b9a8d78f 4277 ERR("[notification-thread] Failed to create event rule hit evaluation while creating and enqueuing action executor job");
f1446131
JR
4278 ret = -1;
4279 goto end_unlock;
4280 }
4281
4282 client_list = get_client_list_from_condition(state,
4283 lttng_trigger_get_const_condition(element->trigger));
4284 executor_status = action_executor_enqueue(state->executor,
4285 element->trigger, evaluation, NULL, client_list);
4286 switch (executor_status) {
4287 case ACTION_EXECUTOR_STATUS_OK:
4288 ret = 0;
4289 break;
4290 case ACTION_EXECUTOR_STATUS_OVERFLOW:
4291 {
4292 struct notification_client_list_element *client_list_element,
4293 *tmp;
4294
4295 /*
4296 * Not a fatal error; this is expected and simply means the
4297 * executor has too much work queued already.
4298 */
4299 ret = 0;
4300
4301 /* No clients subscribed to notifications for this trigger. */
4302 if (!client_list) {
4303 break;
4304 }
4305
4306 /* Warn clients that a notification (or more) was dropped. */
4307 pthread_mutex_lock(&client_list->lock);
4308 cds_list_for_each_entry_safe(client_list_element, tmp,
4309 &client_list->list, node) {
4310 enum client_transmission_status transmission_status;
4311 struct notification_client *client =
4312 client_list_element->client;
4313
4314 pthread_mutex_lock(&client->lock);
4315 ret = client_notification_overflow(client);
4316 if (ret) {
4317 /* Fatal error. */
4318 goto next_client;
4319 }
4320
4321 transmission_status =
4322 client_flush_outgoing_queue(client);
4323 ret = client_handle_transmission_status(
4324 client, transmission_status, state);
4325 if (ret) {
4326 /* Fatal error. */
4327 goto next_client;
4328 }
4329next_client:
4330 pthread_mutex_unlock(&client->lock);
4331 if (ret) {
4332 break;
4333 }
4334 }
4335
4336 pthread_mutex_unlock(&client_list->lock);
4337 break;
4338 }
4339 case ACTION_EXECUTOR_STATUS_ERROR:
4340 /* Fatal error, shut down everything. */
4341 ERR("Fatal error encoutered while enqueuing action to the action executor");
4342 ret = -1;
4343 goto end_unlock;
4344 default:
4345 /* Unhandled error. */
4346 abort();
4347 }
94078603
JR
4348
4349end_unlock:
b9a8d78f 4350 lttng_event_notifier_notification_destroy(notification);
f1446131 4351 notification_client_list_put(client_list);
94078603
JR
4352 rcu_read_unlock();
4353end:
4354 return ret;
4355}
4356
ab0ee2ca
JG
4357int handle_notification_thread_channel_sample(
4358 struct notification_thread_state *state, int pipe,
4359 enum lttng_domain_type domain)
4360{
4361 int ret = 0;
4362 struct lttcomm_consumer_channel_monitor_msg sample_msg;
ab0ee2ca
JG
4363 struct channel_info *channel_info;
4364 struct cds_lfht_node *node;
4365 struct cds_lfht_iter iter;
4366 struct lttng_channel_trigger_list *trigger_list;
4367 struct lttng_trigger_list_element *trigger_list_element;
4368 bool previous_sample_available = false;
e8360425
JD
4369 struct channel_state_sample previous_sample, latest_sample;
4370 uint64_t previous_session_consumed_total, latest_session_consumed_total;
f2b3ef9f 4371 struct lttng_credentials channel_creds;
ab0ee2ca
JG
4372
4373 /*
4374 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4375 * ensuring that read/write of sampling messages are atomic.
4376 */
7c2551ef 4377 ret = lttng_read(pipe, &sample_msg, sizeof(sample_msg));
ab0ee2ca
JG
4378 if (ret != sizeof(sample_msg)) {
4379 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
4380 pipe);
4381 ret = -1;
4382 goto end;
4383 }
4384
4385 ret = 0;
4386 latest_sample.key.key = sample_msg.key;
4387 latest_sample.key.domain = domain;
4388 latest_sample.highest_usage = sample_msg.highest;
4389 latest_sample.lowest_usage = sample_msg.lowest;
e8360425 4390 latest_sample.channel_total_consumed = sample_msg.total_consumed;
ab0ee2ca
JG
4391
4392 rcu_read_lock();
4393
4394 /* Retrieve the channel's informations */
4395 cds_lfht_lookup(state->channels_ht,
4396 hash_channel_key(&latest_sample.key),
4397 match_channel_info,
4398 &latest_sample.key,
4399 &iter);
4400 node = cds_lfht_iter_get_node(&iter);
e0b5f87b 4401 if (caa_unlikely(!node)) {
ab0ee2ca
JG
4402 /*
4403 * Not an error since the consumer can push a sample to the pipe
4404 * and the rest of the session daemon could notify us of the
4405 * channel's destruction before we get a chance to process that
4406 * sample.
4407 */
4408 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64 " in %s domain",
4409 latest_sample.key.key,
526200e4 4410 lttng_domain_type_str(domain));
ab0ee2ca
JG
4411 goto end_unlock;
4412 }
4413 channel_info = caa_container_of(node, struct channel_info,
4414 channels_ht_node);
e8360425 4415 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64 ") in session %s (highest usage = %" PRIu64 ", lowest usage = %" PRIu64", total consumed = %" PRIu64")",
8abe313a 4416 channel_info->name,
ab0ee2ca 4417 latest_sample.key.key,
8abe313a 4418 channel_info->session_info->name,
ab0ee2ca 4419 latest_sample.highest_usage,
e8360425
JD
4420 latest_sample.lowest_usage,
4421 latest_sample.channel_total_consumed);
4422
4423 previous_session_consumed_total =
4424 channel_info->session_info->consumed_data_size;
ab0ee2ca
JG
4425
4426 /* Retrieve the channel's last sample, if it exists, and update it. */
4427 cds_lfht_lookup(state->channel_state_ht,
4428 hash_channel_key(&latest_sample.key),
4429 match_channel_state_sample,
4430 &latest_sample.key,
4431 &iter);
4432 node = cds_lfht_iter_get_node(&iter);
e0b5f87b 4433 if (caa_likely(node)) {
ab0ee2ca
JG
4434 struct channel_state_sample *stored_sample;
4435
4436 /* Update the sample stored. */
4437 stored_sample = caa_container_of(node,
4438 struct channel_state_sample,
4439 channel_state_ht_node);
e8360425 4440
ab0ee2ca
JG
4441 memcpy(&previous_sample, stored_sample,
4442 sizeof(previous_sample));
4443 stored_sample->highest_usage = latest_sample.highest_usage;
4444 stored_sample->lowest_usage = latest_sample.lowest_usage;
0f0479d7 4445 stored_sample->channel_total_consumed = latest_sample.channel_total_consumed;
ab0ee2ca 4446 previous_sample_available = true;
e8360425
JD
4447
4448 latest_session_consumed_total =
4449 previous_session_consumed_total +
4450 (latest_sample.channel_total_consumed - previous_sample.channel_total_consumed);
ab0ee2ca
JG
4451 } else {
4452 /*
4453 * This is the channel's first sample, allocate space for and
4454 * store the new sample.
4455 */
4456 struct channel_state_sample *stored_sample;
4457
4458 stored_sample = zmalloc(sizeof(*stored_sample));
4459 if (!stored_sample) {
4460 ret = -1;
4461 goto end_unlock;
4462 }
4463
4464 memcpy(stored_sample, &latest_sample, sizeof(*stored_sample));
4465 cds_lfht_node_init(&stored_sample->channel_state_ht_node);
4466 cds_lfht_add(state->channel_state_ht,
4467 hash_channel_key(&stored_sample->key),
4468 &stored_sample->channel_state_ht_node);
e8360425
JD
4469
4470 latest_session_consumed_total =
4471 previous_session_consumed_total +
4472 latest_sample.channel_total_consumed;
ab0ee2ca
JG
4473 }
4474
e8360425
JD
4475 channel_info->session_info->consumed_data_size =
4476 latest_session_consumed_total;
4477
ab0ee2ca
JG
4478 /* Find triggers associated with this channel. */
4479 cds_lfht_lookup(state->channel_triggers_ht,
4480 hash_channel_key(&latest_sample.key),
4481 match_channel_trigger_list,
4482 &latest_sample.key,
4483 &iter);
4484 node = cds_lfht_iter_get_node(&iter);
e0b5f87b 4485 if (caa_likely(!node)) {
ab0ee2ca
JG
4486 goto end_unlock;
4487 }
4488
f2b3ef9f 4489 channel_creds = (typeof(channel_creds)) {
ff588497
JR
4490 .uid = LTTNG_OPTIONAL_INIT_VALUE(channel_info->session_info->uid),
4491 .gid = LTTNG_OPTIONAL_INIT_VALUE(channel_info->session_info->gid),
f2b3ef9f
JG
4492 };
4493
ab0ee2ca
JG
4494 trigger_list = caa_container_of(node, struct lttng_channel_trigger_list,
4495 channel_triggers_ht_node);
4496 cds_list_for_each_entry(trigger_list_element, &trigger_list->list,
e742b055 4497 node) {
9b63a4aa 4498 const struct lttng_condition *condition;
f2b3ef9f 4499 struct lttng_trigger *trigger;
505b2d90 4500 struct notification_client_list *client_list = NULL;
ab0ee2ca 4501 struct lttng_evaluation *evaluation = NULL;
f2b3ef9f 4502 enum action_executor_status executor_status;
ab0ee2ca 4503
505b2d90 4504 ret = 0;
ab0ee2ca 4505 trigger = trigger_list_element->trigger;
9b63a4aa 4506 condition = lttng_trigger_get_const_condition(trigger);
ab0ee2ca 4507 assert(condition);
ab0ee2ca
JG
4508
4509 /*
4510 * Check if any client is subscribed to the result of this
4511 * evaluation.
4512 */
ed327204 4513 client_list = get_client_list_from_condition(state, condition);
ab0ee2ca 4514
ed327204 4515 ret = evaluate_buffer_condition(condition, &evaluation, state,
ab0ee2ca 4516 previous_sample_available ? &previous_sample : NULL,
e8360425
JD
4517 &latest_sample,
4518 previous_session_consumed_total,
4519 latest_session_consumed_total,
4520 channel_info);
4521 if (caa_unlikely(ret)) {
505b2d90 4522 goto put_list;
ab0ee2ca
JG
4523 }
4524
e8360425 4525 if (caa_likely(!evaluation)) {
505b2d90 4526 goto put_list;
ab0ee2ca
JG
4527 }
4528
f8522f5c
JR
4529 if (!lttng_trigger_should_fire(trigger)) {
4530 goto put_list;
4531 }
4532
4533 lttng_trigger_fire(trigger);
4534
f2b3ef9f
JG
4535 /*
4536 * Ownership of `evaluation` transferred to the action executor
4537 * no matter the result.
4538 */
4539 executor_status = action_executor_enqueue(state->executor,
4540 trigger, evaluation, &channel_creds,
4541 client_list);
4542 evaluation = NULL;
4543 switch (executor_status) {
4544 case ACTION_EXECUTOR_STATUS_OK:
4545 break;
4546 case ACTION_EXECUTOR_STATUS_ERROR:
4547 case ACTION_EXECUTOR_STATUS_INVALID:
4548 /*
4549 * TODO Add trigger identification (name/id) when
4550 * it is added to the API.
4551 */
4552 ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger");
4553 ret = -1;
4554 goto put_list;
4555 case ACTION_EXECUTOR_STATUS_OVERFLOW:
4556 /*
4557 * TODO Add trigger identification (name/id) when
4558 * it is added to the API.
4559 *
4560 * Not a fatal error.
4561 */
4562 WARN("No space left when enqueuing action associated with buffer-condition trigger");
4563 ret = 0;
4564 goto put_list;
4565 default:
4566 abort();
4567 }
4568
505b2d90
JG
4569put_list:
4570 notification_client_list_put(client_list);
e0b5f87b 4571 if (caa_unlikely(ret)) {
505b2d90 4572 break;
ab0ee2ca
JG
4573 }
4574 }
4575end_unlock:
4576 rcu_read_unlock();
4577end:
4578 return ret;
4579}
This page took 0.261827 seconds and 4 git commands to generate.