Clean-up: sessiond: Extract trigger_ht_element removal function
[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>
93ac2c44
JR
24#include <lttng/action/group-internal.h>
25#include <lttng/domain-internal.h>
ab0ee2ca
JG
26#include <lttng/notification/notification-internal.h>
27#include <lttng/condition/condition-internal.h>
28#include <lttng/condition/buffer-usage-internal.h>
e8360425 29#include <lttng/condition/session-consumed-size-internal.h>
ea9a44f0 30#include <lttng/condition/session-rotation-internal.h>
e393070a 31#include <lttng/condition/on-event-internal.h>
d02d7404 32#include <lttng/domain-internal.h>
ab0ee2ca 33#include <lttng/notification/channel-internal.h>
85c06c44 34#include <lttng/trigger/trigger-internal.h>
959e3c66 35#include <lttng/event-rule/event-rule-internal.h>
1da26331 36
ab0ee2ca
JG
37#include <time.h>
38#include <unistd.h>
39#include <assert.h>
40#include <inttypes.h>
d53ea4e4 41#include <fcntl.h>
ab0ee2ca 42
e98a976a 43#include "condition-internal.h"
90aa04a1 44#include "event-notifier-error-accounting.h"
1da26331
JG
45#include "notification-thread.h"
46#include "notification-thread-events.h"
47#include "notification-thread-commands.h"
48#include "lttng-sessiond.h"
49#include "kernel.h"
50
ab0ee2ca
JG
51#define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
52#define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
53
82b3cbf4
JR
54/* The tracers currently limit the capture size to PIPE_BUF (4kb on linux). */
55#define MAX_CAPTURE_SIZE (PIPE_BUF)
56
51eab943
JG
57enum lttng_object_type {
58 LTTNG_OBJECT_TYPE_UNKNOWN,
59 LTTNG_OBJECT_TYPE_NONE,
60 LTTNG_OBJECT_TYPE_CHANNEL,
61 LTTNG_OBJECT_TYPE_SESSION,
62};
63
ab0ee2ca 64struct lttng_trigger_list_element {
9e0bb80e 65 /* No ownership of the trigger object is assumed. */
f2b3ef9f 66 struct lttng_trigger *trigger;
ab0ee2ca
JG
67 struct cds_list_head node;
68};
69
70struct lttng_channel_trigger_list {
71 struct channel_key channel_key;
ea9a44f0 72 /* List of struct lttng_trigger_list_element. */
ab0ee2ca 73 struct cds_list_head list;
ea9a44f0 74 /* Node in the channel_triggers_ht */
ab0ee2ca 75 struct cds_lfht_node channel_triggers_ht_node;
83b934ad
MD
76 /* call_rcu delayed reclaim. */
77 struct rcu_head rcu_node;
ab0ee2ca
JG
78};
79
ea9a44f0
JG
80/*
81 * List of triggers applying to a given session.
82 *
83 * See:
84 * - lttng_session_trigger_list_create()
85 * - lttng_session_trigger_list_build()
86 * - lttng_session_trigger_list_destroy()
87 * - lttng_session_trigger_list_add()
88 */
89struct lttng_session_trigger_list {
90 /*
91 * Not owned by this; points to the session_info structure's
92 * session name.
93 */
94 const char *session_name;
95 /* List of struct lttng_trigger_list_element. */
96 struct cds_list_head list;
97 /* Node in the session_triggers_ht */
98 struct cds_lfht_node session_triggers_ht_node;
99 /*
100 * Weak reference to the notification system's session triggers
101 * hashtable.
102 *
103 * The session trigger list structure structure is owned by
104 * the session's session_info.
105 *
106 * The session_info is kept alive the the channel_infos holding a
107 * reference to it (reference counting). When those channels are
108 * destroyed (at runtime or on teardown), the reference they hold
109 * to the session_info are released. On destruction of session_info,
110 * session_info_destroy() will remove the list of triggers applying
111 * to this session from the notification system's state.
112 *
113 * This implies that the session_triggers_ht must be destroyed
114 * after the channels.
115 */
116 struct cds_lfht *session_triggers_ht;
117 /* Used for delayed RCU reclaim. */
118 struct rcu_head rcu_node;
119};
120
ab0ee2ca
JG
121struct lttng_trigger_ht_element {
122 struct lttng_trigger *trigger;
123 struct cds_lfht_node node;
242388e4 124 struct cds_lfht_node node_by_name_uid;
83b934ad
MD
125 /* call_rcu delayed reclaim. */
126 struct rcu_head rcu_node;
ab0ee2ca
JG
127};
128
129struct lttng_condition_list_element {
130 struct lttng_condition *condition;
131 struct cds_list_head node;
132};
133
ab0ee2ca
JG
134struct channel_state_sample {
135 struct channel_key key;
136 struct cds_lfht_node channel_state_ht_node;
137 uint64_t highest_usage;
138 uint64_t lowest_usage;
e8360425 139 uint64_t channel_total_consumed;
83b934ad
MD
140 /* call_rcu delayed reclaim. */
141 struct rcu_head rcu_node;
ab0ee2ca
JG
142};
143
e4db5ace 144static unsigned long hash_channel_key(struct channel_key *key);
ed327204 145static int evaluate_buffer_condition(const struct lttng_condition *condition,
e4db5ace 146 struct lttng_evaluation **evaluation,
e8360425
JD
147 const struct notification_thread_state *state,
148 const struct channel_state_sample *previous_sample,
149 const struct channel_state_sample *latest_sample,
150 uint64_t previous_session_consumed_total,
151 uint64_t latest_session_consumed_total,
152 struct channel_info *channel_info);
e4db5ace 153static
9b63a4aa
JG
154int send_evaluation_to_clients(const struct lttng_trigger *trigger,
155 const struct lttng_evaluation *evaluation,
e4db5ace
JR
156 struct notification_client_list *client_list,
157 struct notification_thread_state *state,
158 uid_t channel_uid, gid_t channel_gid);
159
8abe313a 160
ea9a44f0 161/* session_info API */
8abe313a
JG
162static
163void session_info_destroy(void *_data);
164static
165void session_info_get(struct session_info *session_info);
166static
167void session_info_put(struct session_info *session_info);
168static
169struct session_info *session_info_create(const char *name,
ea9a44f0 170 uid_t uid, gid_t gid,
1eee26c5
JG
171 struct lttng_session_trigger_list *trigger_list,
172 struct cds_lfht *sessions_ht);
8abe313a
JG
173static
174void session_info_add_channel(struct session_info *session_info,
175 struct channel_info *channel_info);
176static
177void session_info_remove_channel(struct session_info *session_info,
178 struct channel_info *channel_info);
179
ea9a44f0
JG
180/* lttng_session_trigger_list API */
181static
182struct lttng_session_trigger_list *lttng_session_trigger_list_create(
183 const char *session_name,
184 struct cds_lfht *session_triggers_ht);
185static
186struct lttng_session_trigger_list *lttng_session_trigger_list_build(
187 const struct notification_thread_state *state,
188 const char *session_name);
189static
190void lttng_session_trigger_list_destroy(
191 struct lttng_session_trigger_list *list);
192static
193int lttng_session_trigger_list_add(struct lttng_session_trigger_list *list,
f2b3ef9f 194 struct lttng_trigger *trigger);
ea9a44f0 195
f2b3ef9f
JG
196static
197int client_handle_transmission_status(
198 struct notification_client *client,
199 enum client_transmission_status transmission_status,
200 struct notification_thread_state *state);
ea9a44f0 201
8b524060
FD
202static
203int handle_one_event_notifier_notification(
204 struct notification_thread_state *state,
205 int pipe, enum lttng_domain_type domain);
206
242388e4
JR
207static
208void free_lttng_trigger_ht_element_rcu(struct rcu_head *node);
209
ab0ee2ca 210static
ac1889bf 211int match_client_socket(struct cds_lfht_node *node, const void *key)
ab0ee2ca
JG
212{
213 /* This double-cast is intended to supress pointer-to-cast warning. */
ac1889bf
JG
214 const int socket = (int) (intptr_t) key;
215 const struct notification_client *client = caa_container_of(node,
216 struct notification_client, client_socket_ht_node);
ab0ee2ca 217
ac1889bf
JG
218 return client->socket == socket;
219}
220
221static
222int match_client_id(struct cds_lfht_node *node, const void *key)
223{
224 /* This double-cast is intended to supress pointer-to-cast warning. */
225 const notification_client_id id = *((notification_client_id *) key);
226 const struct notification_client *client = caa_container_of(
227 node, struct notification_client, client_id_ht_node);
ab0ee2ca 228
ac1889bf 229 return client->id == id;
ab0ee2ca
JG
230}
231
232static
233int match_channel_trigger_list(struct cds_lfht_node *node, const void *key)
234{
235 struct channel_key *channel_key = (struct channel_key *) key;
236 struct lttng_channel_trigger_list *trigger_list;
237
238 trigger_list = caa_container_of(node, struct lttng_channel_trigger_list,
239 channel_triggers_ht_node);
240
241 return !!((channel_key->key == trigger_list->channel_key.key) &&
242 (channel_key->domain == trigger_list->channel_key.domain));
243}
244
51eab943
JG
245static
246int match_session_trigger_list(struct cds_lfht_node *node, const void *key)
247{
248 const char *session_name = (const char *) key;
249 struct lttng_session_trigger_list *trigger_list;
250
251 trigger_list = caa_container_of(node, struct lttng_session_trigger_list,
252 session_triggers_ht_node);
253
254 return !!(strcmp(trigger_list->session_name, session_name) == 0);
255}
256
ab0ee2ca
JG
257static
258int match_channel_state_sample(struct cds_lfht_node *node, const void *key)
259{
260 struct channel_key *channel_key = (struct channel_key *) key;
261 struct channel_state_sample *sample;
262
263 sample = caa_container_of(node, struct channel_state_sample,
264 channel_state_ht_node);
265
266 return !!((channel_key->key == sample->key.key) &&
267 (channel_key->domain == sample->key.domain));
268}
269
270static
271int match_channel_info(struct cds_lfht_node *node, const void *key)
272{
273 struct channel_key *channel_key = (struct channel_key *) key;
274 struct channel_info *channel_info;
275
276 channel_info = caa_container_of(node, struct channel_info,
277 channels_ht_node);
278
279 return !!((channel_key->key == channel_info->key.key) &&
280 (channel_key->domain == channel_info->key.domain));
281}
282
283static
242388e4 284int match_trigger(struct cds_lfht_node *node, const void *key)
ab0ee2ca 285{
242388e4
JR
286 struct lttng_trigger *trigger_key = (struct lttng_trigger *) key;
287 struct lttng_trigger_ht_element *trigger_ht_element;
ab0ee2ca 288
242388e4 289 trigger_ht_element = caa_container_of(node, struct lttng_trigger_ht_element,
ab0ee2ca 290 node);
ab0ee2ca 291
242388e4 292 return !!lttng_trigger_is_equal(trigger_key, trigger_ht_element->trigger);
ab0ee2ca
JG
293}
294
e7c93cf9
JR
295static
296int match_trigger_token(struct cds_lfht_node *node, const void *key)
297{
298 const uint64_t *_key = key;
299 struct notification_trigger_tokens_ht_element *element;
300
301 element = caa_container_of(node,
302 struct notification_trigger_tokens_ht_element, node);
303 return *_key == element->token;
304}
305
ab0ee2ca
JG
306static
307int match_client_list_condition(struct cds_lfht_node *node, const void *key)
308{
309 struct lttng_condition *condition_key = (struct lttng_condition *) key;
310 struct notification_client_list *client_list;
f82f93a1 311 const struct lttng_condition *condition;
ab0ee2ca
JG
312
313 assert(condition_key);
314
315 client_list = caa_container_of(node, struct notification_client_list,
505b2d90 316 notification_trigger_clients_ht_node);
f82f93a1 317 condition = lttng_trigger_get_const_condition(client_list->trigger);
ab0ee2ca
JG
318
319 return !!lttng_condition_is_equal(condition_key, condition);
320}
321
f82f93a1
JG
322static
323int match_session(struct cds_lfht_node *node, const void *key)
324{
325 const char *name = key;
326 struct session_info *session_info = caa_container_of(
327 node, struct session_info, sessions_ht_node);
328
329 return !strcmp(session_info->name, name);
330}
331
6900ae81
FD
332static
333const char *notification_command_type_str(
334 enum notification_thread_command_type type)
335{
336 switch (type) {
337 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER:
338 return "REGISTER_TRIGGER";
339 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER:
340 return "UNREGISTER_TRIGGER";
341 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL:
342 return "ADD_CHANNEL";
343 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL:
344 return "REMOVE_CHANNEL";
345 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING:
346 return "SESSION_ROTATION_ONGOING";
347 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED:
348 return "SESSION_ROTATION_COMPLETED";
349 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE:
350 return "ADD_TRACER_EVENT_SOURCE";
351 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE:
352 return "REMOVE_TRACER_EVENT_SOURCE";
353 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS:
354 return "LIST_TRIGGERS";
355 case NOTIFICATION_COMMAND_TYPE_QUIT:
356 return "QUIT";
357 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE:
358 return "CLIENT_COMMUNICATION_UPDATE";
359 default:
360 abort();
361 }
362}
363
242388e4
JR
364/*
365 * Match trigger based on name and credentials only.
366 * Name duplication is NOT allowed for the same uid.
367 */
368static
369int match_trigger_by_name_uid(struct cds_lfht_node *node,
370 const void *key)
371{
372 bool match = false;
373 const char *name;
374 const char *key_name;
375 enum lttng_trigger_status status;
376 const struct lttng_credentials *key_creds;
377 const struct lttng_credentials *node_creds;
378 const struct lttng_trigger *trigger_key =
379 (const struct lttng_trigger *) key;
380 const struct lttng_trigger_ht_element *trigger_ht_element =
381 caa_container_of(node,
382 struct lttng_trigger_ht_element,
383 node_by_name_uid);
384
385 status = lttng_trigger_get_name(trigger_ht_element->trigger, &name);
386 assert(status == LTTNG_TRIGGER_STATUS_OK);
387
388 status = lttng_trigger_get_name(trigger_key, &key_name);
389 assert(status == LTTNG_TRIGGER_STATUS_OK);
390
391 /* Compare the names. */
392 if (strcmp(name, key_name) != 0) {
393 goto end;
394 }
395
396 /* Compare the owners' UIDs. */
397 key_creds = lttng_trigger_get_credentials(trigger_key);
398 node_creds = lttng_trigger_get_credentials(trigger_ht_element->trigger);
399
400 match = lttng_credentials_is_equal_uid(key_creds, node_creds);
401
402end:
403 return match;
404}
405
406/*
407 * Hash trigger based on name and credentials only.
408 */
409static
410unsigned long hash_trigger_by_name_uid(const struct lttng_trigger *trigger)
411{
412 unsigned long hash = 0;
413 const struct lttng_credentials *trigger_creds;
414 const char *trigger_name;
415 enum lttng_trigger_status status;
416
417 status = lttng_trigger_get_name(trigger, &trigger_name);
418 if (status == LTTNG_TRIGGER_STATUS_OK) {
419 hash = hash_key_str(trigger_name, lttng_ht_seed);
420 }
421
422 trigger_creds = lttng_trigger_get_credentials(trigger);
423 hash ^= hash_key_ulong((void *) (unsigned long) LTTNG_OPTIONAL_GET(trigger_creds->uid),
424 lttng_ht_seed);
425
426 return hash;
427}
428
e4db5ace
JR
429static
430unsigned long hash_channel_key(struct channel_key *key)
431{
432 unsigned long key_hash = hash_key_u64(&key->key, lttng_ht_seed);
433 unsigned long domain_hash = hash_key_ulong(
434 (void *) (unsigned long) key->domain, lttng_ht_seed);
435
436 return key_hash ^ domain_hash;
437}
438
ac1889bf
JG
439static
440unsigned long hash_client_socket(int socket)
441{
442 return hash_key_ulong((void *) (unsigned long) socket, lttng_ht_seed);
443}
444
445static
446unsigned long hash_client_id(notification_client_id id)
447{
448 return hash_key_u64(&id, lttng_ht_seed);
449}
450
f82f93a1
JG
451/*
452 * Get the type of object to which a given condition applies. Bindings let
453 * the notification system evaluate a trigger's condition when a given
454 * object's state is updated.
455 *
456 * For instance, a condition bound to a channel will be evaluated everytime
457 * the channel's state is changed by a channel monitoring sample.
458 */
459static
460enum lttng_object_type get_condition_binding_object(
461 const struct lttng_condition *condition)
462{
463 switch (lttng_condition_get_type(condition)) {
464 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
465 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
466 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
e742b055 467 return LTTNG_OBJECT_TYPE_CHANNEL;
f82f93a1
JG
468 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
469 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
470 return LTTNG_OBJECT_TYPE_SESSION;
d602bd6a 471 case LTTNG_CONDITION_TYPE_ON_EVENT:
959e3c66 472 return LTTNG_OBJECT_TYPE_NONE;
f82f93a1
JG
473 default:
474 return LTTNG_OBJECT_TYPE_UNKNOWN;
475 }
476}
477
83b934ad
MD
478static
479void free_channel_info_rcu(struct rcu_head *node)
480{
481 free(caa_container_of(node, struct channel_info, rcu_node));
482}
483
ab0ee2ca
JG
484static
485void channel_info_destroy(struct channel_info *channel_info)
486{
487 if (!channel_info) {
488 return;
489 }
490
8abe313a
JG
491 if (channel_info->session_info) {
492 session_info_remove_channel(channel_info->session_info,
493 channel_info);
494 session_info_put(channel_info->session_info);
ab0ee2ca 495 }
8abe313a
JG
496 if (channel_info->name) {
497 free(channel_info->name);
ab0ee2ca 498 }
83b934ad
MD
499 call_rcu(&channel_info->rcu_node, free_channel_info_rcu);
500}
501
502static
503void free_session_info_rcu(struct rcu_head *node)
504{
505 free(caa_container_of(node, struct session_info, rcu_node));
ab0ee2ca
JG
506}
507
8abe313a 508/* Don't call directly, use the ref-counting mechanism. */
ab0ee2ca 509static
8abe313a 510void session_info_destroy(void *_data)
ab0ee2ca 511{
8abe313a 512 struct session_info *session_info = _data;
3b68d0a3 513 int ret;
ab0ee2ca 514
8abe313a
JG
515 assert(session_info);
516 if (session_info->channel_infos_ht) {
3b68d0a3
JR
517 ret = cds_lfht_destroy(session_info->channel_infos_ht, NULL);
518 if (ret) {
4c3f302b 519 ERR("[notification-thread] Failed to destroy channel information hash table");
3b68d0a3 520 }
8abe313a 521 }
ea9a44f0 522 lttng_session_trigger_list_destroy(session_info->trigger_list);
1eee26c5
JG
523
524 rcu_read_lock();
525 cds_lfht_del(session_info->sessions_ht,
526 &session_info->sessions_ht_node);
527 rcu_read_unlock();
8abe313a 528 free(session_info->name);
83b934ad 529 call_rcu(&session_info->rcu_node, free_session_info_rcu);
8abe313a 530}
ab0ee2ca 531
8abe313a
JG
532static
533void session_info_get(struct session_info *session_info)
534{
535 if (!session_info) {
536 return;
537 }
538 lttng_ref_get(&session_info->ref);
539}
540
541static
542void session_info_put(struct session_info *session_info)
543{
544 if (!session_info) {
545 return;
ab0ee2ca 546 }
8abe313a
JG
547 lttng_ref_put(&session_info->ref);
548}
549
550static
ea9a44f0 551struct session_info *session_info_create(const char *name, uid_t uid, gid_t gid,
1eee26c5
JG
552 struct lttng_session_trigger_list *trigger_list,
553 struct cds_lfht *sessions_ht)
8abe313a
JG
554{
555 struct session_info *session_info;
ab0ee2ca 556
8abe313a 557 assert(name);
ab0ee2ca 558
8abe313a
JG
559 session_info = zmalloc(sizeof(*session_info));
560 if (!session_info) {
561 goto end;
562 }
563 lttng_ref_init(&session_info->ref, session_info_destroy);
564
565 session_info->channel_infos_ht = cds_lfht_new(DEFAULT_HT_SIZE,
566 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL);
567 if (!session_info->channel_infos_ht) {
ab0ee2ca
JG
568 goto error;
569 }
8abe313a
JG
570
571 cds_lfht_node_init(&session_info->sessions_ht_node);
572 session_info->name = strdup(name);
573 if (!session_info->name) {
ab0ee2ca
JG
574 goto error;
575 }
8abe313a
JG
576 session_info->uid = uid;
577 session_info->gid = gid;
ea9a44f0 578 session_info->trigger_list = trigger_list;
1eee26c5 579 session_info->sessions_ht = sessions_ht;
8abe313a
JG
580end:
581 return session_info;
582error:
583 session_info_put(session_info);
584 return NULL;
585}
586
587static
588void session_info_add_channel(struct session_info *session_info,
589 struct channel_info *channel_info)
590{
591 rcu_read_lock();
592 cds_lfht_add(session_info->channel_infos_ht,
593 hash_channel_key(&channel_info->key),
594 &channel_info->session_info_channels_ht_node);
595 rcu_read_unlock();
596}
597
598static
599void session_info_remove_channel(struct session_info *session_info,
600 struct channel_info *channel_info)
601{
602 rcu_read_lock();
603 cds_lfht_del(session_info->channel_infos_ht,
604 &channel_info->session_info_channels_ht_node);
605 rcu_read_unlock();
606}
607
608static
609struct channel_info *channel_info_create(const char *channel_name,
610 struct channel_key *channel_key, uint64_t channel_capacity,
611 struct session_info *session_info)
612{
613 struct channel_info *channel_info = zmalloc(sizeof(*channel_info));
614
615 if (!channel_info) {
616 goto end;
617 }
618
ab0ee2ca 619 cds_lfht_node_init(&channel_info->channels_ht_node);
8abe313a
JG
620 cds_lfht_node_init(&channel_info->session_info_channels_ht_node);
621 memcpy(&channel_info->key, channel_key, sizeof(*channel_key));
622 channel_info->capacity = channel_capacity;
623
624 channel_info->name = strdup(channel_name);
625 if (!channel_info->name) {
626 goto error;
627 }
628
629 /*
630 * Set the references between session and channel infos:
631 * - channel_info holds a strong reference to session_info
632 * - session_info holds a weak reference to channel_info
633 */
634 session_info_get(session_info);
635 session_info_add_channel(session_info, channel_info);
636 channel_info->session_info = session_info;
ab0ee2ca 637end:
8abe313a 638 return channel_info;
ab0ee2ca 639error:
8abe313a 640 channel_info_destroy(channel_info);
ab0ee2ca
JG
641 return NULL;
642}
643
f2b3ef9f 644LTTNG_HIDDEN
505b2d90
JG
645bool notification_client_list_get(struct notification_client_list *list)
646{
647 return urcu_ref_get_unless_zero(&list->ref);
648}
649
650static
651void free_notification_client_list_rcu(struct rcu_head *node)
652{
653 free(caa_container_of(node, struct notification_client_list,
654 rcu_node));
655}
656
657static
658void notification_client_list_release(struct urcu_ref *list_ref)
659{
660 struct notification_client_list *list =
661 container_of(list_ref, typeof(*list), ref);
662 struct notification_client_list_element *client_list_element, *tmp;
663
664 if (list->notification_trigger_clients_ht) {
665 rcu_read_lock();
666 cds_lfht_del(list->notification_trigger_clients_ht,
667 &list->notification_trigger_clients_ht_node);
668 rcu_read_unlock();
669 list->notification_trigger_clients_ht = NULL;
670 }
671 cds_list_for_each_entry_safe(client_list_element, tmp,
672 &list->list, node) {
673 free(client_list_element);
674 }
675 pthread_mutex_destroy(&list->lock);
676 call_rcu(&list->rcu_node, free_notification_client_list_rcu);
677}
678
679static
680struct notification_client_list *notification_client_list_create(
681 const struct lttng_trigger *trigger)
682{
683 struct notification_client_list *client_list =
684 zmalloc(sizeof(*client_list));
685
686 if (!client_list) {
687 goto error;
688 }
689 pthread_mutex_init(&client_list->lock, NULL);
690 urcu_ref_init(&client_list->ref);
691 cds_lfht_node_init(&client_list->notification_trigger_clients_ht_node);
692 CDS_INIT_LIST_HEAD(&client_list->list);
693 client_list->trigger = trigger;
694error:
695 return client_list;
696}
697
698static
699void publish_notification_client_list(
700 struct notification_thread_state *state,
701 struct notification_client_list *list)
702{
703 const struct lttng_condition *condition =
704 lttng_trigger_get_const_condition(list->trigger);
705
706 assert(!list->notification_trigger_clients_ht);
f2b3ef9f 707 notification_client_list_get(list);
505b2d90
JG
708
709 list->notification_trigger_clients_ht =
710 state->notification_trigger_clients_ht;
711
712 rcu_read_lock();
713 cds_lfht_add(state->notification_trigger_clients_ht,
714 lttng_condition_hash(condition),
715 &list->notification_trigger_clients_ht_node);
716 rcu_read_unlock();
717}
718
f2b3ef9f 719LTTNG_HIDDEN
505b2d90
JG
720void notification_client_list_put(struct notification_client_list *list)
721{
722 if (!list) {
723 return;
724 }
725 return urcu_ref_put(&list->ref, notification_client_list_release);
726}
727
728/* Provides a reference to the returned list. */
ed327204
JG
729static
730struct notification_client_list *get_client_list_from_condition(
731 struct notification_thread_state *state,
732 const struct lttng_condition *condition)
733{
734 struct cds_lfht_node *node;
735 struct cds_lfht_iter iter;
505b2d90 736 struct notification_client_list *list = NULL;
ed327204 737
505b2d90 738 rcu_read_lock();
ed327204
JG
739 cds_lfht_lookup(state->notification_trigger_clients_ht,
740 lttng_condition_hash(condition),
741 match_client_list_condition,
742 condition,
743 &iter);
744 node = cds_lfht_iter_get_node(&iter);
505b2d90
JG
745 if (node) {
746 list = container_of(node, struct notification_client_list,
747 notification_trigger_clients_ht_node);
748 list = notification_client_list_get(list) ? list : NULL;
749 }
ed327204 750
505b2d90
JG
751 rcu_read_unlock();
752 return list;
ed327204
JG
753}
754
e4db5ace 755static
f82f93a1
JG
756int evaluate_channel_condition_for_client(
757 const struct lttng_condition *condition,
758 struct notification_thread_state *state,
759 struct lttng_evaluation **evaluation,
760 uid_t *session_uid, gid_t *session_gid)
e4db5ace
JR
761{
762 int ret;
763 struct cds_lfht_iter iter;
764 struct cds_lfht_node *node;
765 struct channel_info *channel_info = NULL;
766 struct channel_key *channel_key = NULL;
767 struct channel_state_sample *last_sample = NULL;
768 struct lttng_channel_trigger_list *channel_trigger_list = NULL;
e4db5ace 769
505b2d90
JG
770 rcu_read_lock();
771
f82f93a1 772 /* Find the channel associated with the condition. */
e4db5ace 773 cds_lfht_for_each_entry(state->channel_triggers_ht, &iter,
f82f93a1 774 channel_trigger_list, channel_triggers_ht_node) {
e4db5ace
JR
775 struct lttng_trigger_list_element *element;
776
777 cds_list_for_each_entry(element, &channel_trigger_list->list, node) {
9b63a4aa 778 const struct lttng_condition *current_condition =
f82f93a1 779 lttng_trigger_get_const_condition(
e4db5ace
JR
780 element->trigger);
781
782 assert(current_condition);
783 if (!lttng_condition_is_equal(condition,
2ae99f0b 784 current_condition)) {
e4db5ace
JR
785 continue;
786 }
787
788 /* Found the trigger, save the channel key. */
789 channel_key = &channel_trigger_list->channel_key;
790 break;
791 }
792 if (channel_key) {
793 /* The channel key was found stop iteration. */
794 break;
795 }
796 }
797
798 if (!channel_key){
799 /* No channel found; normal exit. */
f82f93a1 800 DBG("[notification-thread] No known channel associated with newly subscribed-to condition");
e4db5ace
JR
801 ret = 0;
802 goto end;
803 }
804
805 /* Fetch channel info for the matching channel. */
806 cds_lfht_lookup(state->channels_ht,
807 hash_channel_key(channel_key),
808 match_channel_info,
809 channel_key,
810 &iter);
811 node = cds_lfht_iter_get_node(&iter);
812 assert(node);
813 channel_info = caa_container_of(node, struct channel_info,
814 channels_ht_node);
815
816 /* Retrieve the channel's last sample, if it exists. */
817 cds_lfht_lookup(state->channel_state_ht,
818 hash_channel_key(channel_key),
819 match_channel_state_sample,
820 channel_key,
821 &iter);
822 node = cds_lfht_iter_get_node(&iter);
823 if (node) {
824 last_sample = caa_container_of(node,
825 struct channel_state_sample,
826 channel_state_ht_node);
827 } else {
828 /* Nothing to evaluate, no sample was ever taken. Normal exit */
829 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
830 ret = 0;
831 goto end;
832 }
833
f82f93a1 834 ret = evaluate_buffer_condition(condition, evaluation, state,
e8360425
JD
835 NULL, last_sample,
836 0, channel_info->session_info->consumed_data_size,
837 channel_info);
e4db5ace 838 if (ret) {
066a3c82 839 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
e4db5ace
JR
840 goto end;
841 }
842
f82f93a1
JG
843 *session_uid = channel_info->session_info->uid;
844 *session_gid = channel_info->session_info->gid;
845end:
505b2d90 846 rcu_read_unlock();
f82f93a1
JG
847 return ret;
848}
849
850static
851const char *get_condition_session_name(const struct lttng_condition *condition)
852{
853 const char *session_name = NULL;
854 enum lttng_condition_status status;
855
856 switch (lttng_condition_get_type(condition)) {
857 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
858 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
859 status = lttng_condition_buffer_usage_get_session_name(
860 condition, &session_name);
861 break;
862 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
863 status = lttng_condition_session_consumed_size_get_session_name(
864 condition, &session_name);
865 break;
866 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
867 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
868 status = lttng_condition_session_rotation_get_session_name(
869 condition, &session_name);
870 break;
871 default:
872 abort();
873 }
874 if (status != LTTNG_CONDITION_STATUS_OK) {
875 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
876 goto end;
877 }
878end:
879 return session_name;
880}
881
f82f93a1
JG
882static
883int evaluate_session_condition_for_client(
884 const struct lttng_condition *condition,
885 struct notification_thread_state *state,
886 struct lttng_evaluation **evaluation,
887 uid_t *session_uid, gid_t *session_gid)
888{
889 int ret;
890 struct cds_lfht_iter iter;
891 struct cds_lfht_node *node;
892 const char *session_name;
893 struct session_info *session_info = NULL;
894
505b2d90 895 rcu_read_lock();
f82f93a1
JG
896 session_name = get_condition_session_name(condition);
897
898 /* Find the session associated with the trigger. */
899 cds_lfht_lookup(state->sessions_ht,
900 hash_key_str(session_name, lttng_ht_seed),
901 match_session,
902 session_name,
903 &iter);
904 node = cds_lfht_iter_get_node(&iter);
905 if (!node) {
906 DBG("[notification-thread] No known session matching name \"%s\"",
907 session_name);
908 ret = 0;
909 goto end;
910 }
911
912 session_info = caa_container_of(node, struct session_info,
913 sessions_ht_node);
914 session_info_get(session_info);
915
916 /*
917 * Evaluation is performed in-line here since only one type of
918 * session-bound condition is handled for the moment.
919 */
920 switch (lttng_condition_get_type(condition)) {
921 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
922 if (!session_info->rotation.ongoing) {
be558f88 923 ret = 0;
f82f93a1
JG
924 goto end_session_put;
925 }
926
927 *evaluation = lttng_evaluation_session_rotation_ongoing_create(
928 session_info->rotation.id);
929 if (!*evaluation) {
930 /* Fatal error. */
931 ERR("[notification-thread] Failed to create session rotation ongoing evaluation for session \"%s\"",
932 session_info->name);
933 ret = -1;
934 goto end_session_put;
935 }
936 ret = 0;
937 break;
938 default:
939 ret = 0;
940 goto end_session_put;
941 }
942
943 *session_uid = session_info->uid;
944 *session_gid = session_info->gid;
945
946end_session_put:
947 session_info_put(session_info);
948end:
505b2d90 949 rcu_read_unlock();
f82f93a1
JG
950 return ret;
951}
952
f82f93a1
JG
953static
954int evaluate_condition_for_client(const struct lttng_trigger *trigger,
955 const struct lttng_condition *condition,
956 struct notification_client *client,
957 struct notification_thread_state *state)
958{
959 int ret;
960 struct lttng_evaluation *evaluation = NULL;
505b2d90
JG
961 struct notification_client_list client_list = {
962 .lock = PTHREAD_MUTEX_INITIALIZER,
963 };
f82f93a1
JG
964 struct notification_client_list_element client_list_element = { 0 };
965 uid_t object_uid = 0;
966 gid_t object_gid = 0;
967
968 assert(trigger);
969 assert(condition);
970 assert(client);
971 assert(state);
972
973 switch (get_condition_binding_object(condition)) {
974 case LTTNG_OBJECT_TYPE_SESSION:
975 ret = evaluate_session_condition_for_client(condition, state,
976 &evaluation, &object_uid, &object_gid);
977 break;
978 case LTTNG_OBJECT_TYPE_CHANNEL:
979 ret = evaluate_channel_condition_for_client(condition, state,
980 &evaluation, &object_uid, &object_gid);
981 break;
982 case LTTNG_OBJECT_TYPE_NONE:
7e802d0a 983 DBG("[notification-thread] Newly subscribed-to condition not bound to object, nothing to evaluate");
f82f93a1
JG
984 ret = 0;
985 goto end;
986 case LTTNG_OBJECT_TYPE_UNKNOWN:
987 default:
988 ret = -1;
989 goto end;
990 }
af0c318d
JG
991 if (ret) {
992 /* Fatal error. */
993 goto end;
994 }
e4db5ace
JR
995 if (!evaluation) {
996 /* Evaluation yielded nothing. Normal exit. */
997 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
998 ret = 0;
999 goto end;
1000 }
1001
1002 /*
1003 * Create a temporary client list with the client currently
1004 * subscribing.
1005 */
505b2d90 1006 cds_lfht_node_init(&client_list.notification_trigger_clients_ht_node);
e4db5ace
JR
1007 CDS_INIT_LIST_HEAD(&client_list.list);
1008 client_list.trigger = trigger;
1009
1010 CDS_INIT_LIST_HEAD(&client_list_element.node);
1011 client_list_element.client = client;
1012 cds_list_add(&client_list_element.node, &client_list.list);
1013
1014 /* Send evaluation result to the newly-subscribed client. */
1015 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
1016 ret = send_evaluation_to_clients(trigger, evaluation, &client_list,
f82f93a1 1017 state, object_uid, object_gid);
e4db5ace
JR
1018
1019end:
1020 return ret;
1021}
1022
ab0ee2ca
JG
1023static
1024int notification_thread_client_subscribe(struct notification_client *client,
1025 struct lttng_condition *condition,
1026 struct notification_thread_state *state,
1027 enum lttng_notification_channel_status *_status)
1028{
1029 int ret = 0;
505b2d90 1030 struct notification_client_list *client_list = NULL;
ab0ee2ca
JG
1031 struct lttng_condition_list_element *condition_list_element = NULL;
1032 struct notification_client_list_element *client_list_element = NULL;
1033 enum lttng_notification_channel_status status =
1034 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK;
1035
1036 /*
1037 * Ensure that the client has not already subscribed to this condition
1038 * before.
1039 */
1040 cds_list_for_each_entry(condition_list_element, &client->condition_list, node) {
1041 if (lttng_condition_is_equal(condition_list_element->condition,
1042 condition)) {
1043 status = LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED;
1044 goto end;
1045 }
1046 }
1047
1048 condition_list_element = zmalloc(sizeof(*condition_list_element));
1049 if (!condition_list_element) {
1050 ret = -1;
1051 goto error;
1052 }
1053 client_list_element = zmalloc(sizeof(*client_list_element));
1054 if (!client_list_element) {
1055 ret = -1;
1056 goto error;
1057 }
1058
ab0ee2ca
JG
1059 /*
1060 * Add the newly-subscribed condition to the client's subscription list.
1061 */
1062 CDS_INIT_LIST_HEAD(&condition_list_element->node);
1063 condition_list_element->condition = condition;
1064 cds_list_add(&condition_list_element->node, &client->condition_list);
1065
ed327204
JG
1066 client_list = get_client_list_from_condition(state, condition);
1067 if (!client_list) {
2ae99f0b
JG
1068 /*
1069 * No notification-emiting trigger registered with this
1070 * condition. We don't evaluate the condition right away
1071 * since this trigger is not registered yet.
1072 */
4fb43b68 1073 free(client_list_element);
505b2d90 1074 goto end;
ab0ee2ca
JG
1075 }
1076
2ae99f0b
JG
1077 /*
1078 * The condition to which the client just subscribed is evaluated
1079 * at this point so that conditions that are already TRUE result
1080 * in a notification being sent out.
505b2d90
JG
1081 *
1082 * The client_list's trigger is used without locking the list itself.
1083 * This is correct since the list doesn't own the trigger and the
1084 * object is immutable.
2ae99f0b 1085 */
e4db5ace
JR
1086 if (evaluate_condition_for_client(client_list->trigger, condition,
1087 client, state)) {
1088 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
1089 ret = -1;
4bd5b4af 1090 free(client_list_element);
505b2d90 1091 goto end;
e4db5ace
JR
1092 }
1093
1094 /*
1095 * Add the client to the list of clients interested in a given trigger
1096 * if a "notification" trigger with a corresponding condition was
1097 * added prior.
1098 */
ab0ee2ca
JG
1099 client_list_element->client = client;
1100 CDS_INIT_LIST_HEAD(&client_list_element->node);
505b2d90
JG
1101
1102 pthread_mutex_lock(&client_list->lock);
ab0ee2ca 1103 cds_list_add(&client_list_element->node, &client_list->list);
505b2d90 1104 pthread_mutex_unlock(&client_list->lock);
ab0ee2ca
JG
1105end:
1106 if (_status) {
1107 *_status = status;
1108 }
505b2d90
JG
1109 if (client_list) {
1110 notification_client_list_put(client_list);
1111 }
ab0ee2ca
JG
1112 return ret;
1113error:
1114 free(condition_list_element);
1115 free(client_list_element);
1116 return ret;
1117}
1118
1119static
1120int notification_thread_client_unsubscribe(
1121 struct notification_client *client,
1122 struct lttng_condition *condition,
1123 struct notification_thread_state *state,
1124 enum lttng_notification_channel_status *_status)
1125{
ab0ee2ca
JG
1126 struct notification_client_list *client_list;
1127 struct lttng_condition_list_element *condition_list_element,
1128 *condition_tmp;
1129 struct notification_client_list_element *client_list_element,
1130 *client_tmp;
1131 bool condition_found = false;
1132 enum lttng_notification_channel_status status =
1133 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK;
1134
1135 /* Remove the condition from the client's condition list. */
1136 cds_list_for_each_entry_safe(condition_list_element, condition_tmp,
1137 &client->condition_list, node) {
1138 if (!lttng_condition_is_equal(condition_list_element->condition,
1139 condition)) {
1140 continue;
1141 }
1142
1143 cds_list_del(&condition_list_element->node);
1144 /*
1145 * The caller may be iterating on the client's conditions to
1146 * tear down a client's connection. In this case, the condition
1147 * will be destroyed at the end.
1148 */
1149 if (condition != condition_list_element->condition) {
1150 lttng_condition_destroy(
1151 condition_list_element->condition);
1152 }
1153 free(condition_list_element);
1154 condition_found = true;
1155 break;
1156 }
1157
1158 if (!condition_found) {
1159 status = LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION;
1160 goto end;
1161 }
1162
1163 /*
1164 * Remove the client from the list of clients interested the trigger
1165 * matching the condition.
1166 */
ed327204
JG
1167 client_list = get_client_list_from_condition(state, condition);
1168 if (!client_list) {
505b2d90 1169 goto end;
ab0ee2ca
JG
1170 }
1171
505b2d90 1172 pthread_mutex_lock(&client_list->lock);
ab0ee2ca
JG
1173 cds_list_for_each_entry_safe(client_list_element, client_tmp,
1174 &client_list->list, node) {
505b2d90 1175 if (client_list_element->client->id != client->id) {
ab0ee2ca
JG
1176 continue;
1177 }
1178 cds_list_del(&client_list_element->node);
1179 free(client_list_element);
1180 break;
1181 }
505b2d90
JG
1182 pthread_mutex_unlock(&client_list->lock);
1183 notification_client_list_put(client_list);
1184 client_list = NULL;
ab0ee2ca
JG
1185end:
1186 lttng_condition_destroy(condition);
1187 if (_status) {
1188 *_status = status;
1189 }
1190 return 0;
1191}
1192
83b934ad
MD
1193static
1194void free_notification_client_rcu(struct rcu_head *node)
1195{
1196 free(caa_container_of(node, struct notification_client, rcu_node));
1197}
1198
ab0ee2ca
JG
1199static
1200void notification_client_destroy(struct notification_client *client,
1201 struct notification_thread_state *state)
1202{
ab0ee2ca
JG
1203 if (!client) {
1204 return;
1205 }
1206
505b2d90
JG
1207 /*
1208 * The client object is not reachable by other threads, no need to lock
1209 * the client here.
1210 */
ab0ee2ca
JG
1211 if (client->socket >= 0) {
1212 (void) lttcomm_close_unix_sock(client->socket);
ac1889bf 1213 client->socket = -1;
ab0ee2ca 1214 }
505b2d90 1215 client->communication.active = false;
882093ee
JR
1216 lttng_payload_reset(&client->communication.inbound.payload);
1217 lttng_payload_reset(&client->communication.outbound.payload);
505b2d90 1218 pthread_mutex_destroy(&client->lock);
83b934ad 1219 call_rcu(&client->rcu_node, free_notification_client_rcu);
ab0ee2ca
JG
1220}
1221
1222/*
1223 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1224 * client pointer).
1225 */
1226static
1227struct notification_client *get_client_from_socket(int socket,
1228 struct notification_thread_state *state)
1229{
1230 struct cds_lfht_iter iter;
1231 struct cds_lfht_node *node;
1232 struct notification_client *client = NULL;
1233
1234 cds_lfht_lookup(state->client_socket_ht,
ac1889bf
JG
1235 hash_client_socket(socket),
1236 match_client_socket,
ab0ee2ca
JG
1237 (void *) (unsigned long) socket,
1238 &iter);
1239 node = cds_lfht_iter_get_node(&iter);
1240 if (!node) {
1241 goto end;
1242 }
1243
1244 client = caa_container_of(node, struct notification_client,
1245 client_socket_ht_node);
1246end:
1247 return client;
1248}
1249
f2b3ef9f
JG
1250/*
1251 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1252 * client pointer).
1253 */
1254static
1255struct notification_client *get_client_from_id(notification_client_id id,
1256 struct notification_thread_state *state)
1257{
1258 struct cds_lfht_iter iter;
1259 struct cds_lfht_node *node;
1260 struct notification_client *client = NULL;
1261
1262 cds_lfht_lookup(state->client_id_ht,
1263 hash_client_id(id),
1264 match_client_id,
1265 &id,
1266 &iter);
1267 node = cds_lfht_iter_get_node(&iter);
1268 if (!node) {
1269 goto end;
1270 }
1271
1272 client = caa_container_of(node, struct notification_client,
1273 client_id_ht_node);
1274end:
1275 return client;
1276}
1277
ab0ee2ca 1278static
e8360425 1279bool buffer_usage_condition_applies_to_channel(
51eab943
JG
1280 const struct lttng_condition *condition,
1281 const struct channel_info *channel_info)
ab0ee2ca
JG
1282{
1283 enum lttng_condition_status status;
e8360425
JD
1284 enum lttng_domain_type condition_domain;
1285 const char *condition_session_name = NULL;
1286 const char *condition_channel_name = NULL;
ab0ee2ca 1287
e8360425
JD
1288 status = lttng_condition_buffer_usage_get_domain_type(condition,
1289 &condition_domain);
1290 assert(status == LTTNG_CONDITION_STATUS_OK);
1291 if (channel_info->key.domain != condition_domain) {
ab0ee2ca
JG
1292 goto fail;
1293 }
1294
e8360425
JD
1295 status = lttng_condition_buffer_usage_get_session_name(
1296 condition, &condition_session_name);
1297 assert((status == LTTNG_CONDITION_STATUS_OK) && condition_session_name);
1298
1299 status = lttng_condition_buffer_usage_get_channel_name(
1300 condition, &condition_channel_name);
1301 assert((status == LTTNG_CONDITION_STATUS_OK) && condition_channel_name);
1302
1303 if (strcmp(channel_info->session_info->name, condition_session_name)) {
1304 goto fail;
1305 }
1306 if (strcmp(channel_info->name, condition_channel_name)) {
ab0ee2ca
JG
1307 goto fail;
1308 }
1309
e8360425
JD
1310 return true;
1311fail:
1312 return false;
1313}
1314
1315static
1316bool session_consumed_size_condition_applies_to_channel(
51eab943
JG
1317 const struct lttng_condition *condition,
1318 const struct channel_info *channel_info)
e8360425
JD
1319{
1320 enum lttng_condition_status status;
1321 const char *condition_session_name = NULL;
1322
1323 status = lttng_condition_session_consumed_size_get_session_name(
1324 condition, &condition_session_name);
1325 assert((status == LTTNG_CONDITION_STATUS_OK) && condition_session_name);
1326
1327 if (strcmp(channel_info->session_info->name, condition_session_name)) {
ab0ee2ca
JG
1328 goto fail;
1329 }
1330
e8360425
JD
1331 return true;
1332fail:
1333 return false;
1334}
ab0ee2ca 1335
51eab943
JG
1336static
1337bool trigger_applies_to_channel(const struct lttng_trigger *trigger,
1338 const struct channel_info *channel_info)
1339{
1340 const struct lttng_condition *condition;
e8360425 1341 bool trigger_applies;
ab0ee2ca 1342
51eab943 1343 condition = lttng_trigger_get_const_condition(trigger);
e8360425 1344 if (!condition) {
ab0ee2ca
JG
1345 goto fail;
1346 }
e8360425
JD
1347
1348 switch (lttng_condition_get_type(condition)) {
1349 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
1350 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
1351 trigger_applies = buffer_usage_condition_applies_to_channel(
1352 condition, channel_info);
1353 break;
1354 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
1355 trigger_applies = session_consumed_size_condition_applies_to_channel(
1356 condition, channel_info);
1357 break;
1358 default:
ab0ee2ca
JG
1359 goto fail;
1360 }
1361
e8360425 1362 return trigger_applies;
ab0ee2ca
JG
1363fail:
1364 return false;
1365}
1366
1367static
1368bool trigger_applies_to_client(struct lttng_trigger *trigger,
1369 struct notification_client *client)
1370{
1371 bool applies = false;
1372 struct lttng_condition_list_element *condition_list_element;
1373
1374 cds_list_for_each_entry(condition_list_element, &client->condition_list,
1375 node) {
1376 applies = lttng_condition_is_equal(
1377 condition_list_element->condition,
1378 lttng_trigger_get_condition(trigger));
1379 if (applies) {
1380 break;
1381 }
1382 }
1383 return applies;
1384}
1385
ed327204
JG
1386/* Must be called with RCU read lock held. */
1387static
1388struct lttng_session_trigger_list *get_session_trigger_list(
1389 struct notification_thread_state *state,
1390 const char *session_name)
1391{
1392 struct lttng_session_trigger_list *list = NULL;
1393 struct cds_lfht_node *node;
1394 struct cds_lfht_iter iter;
1395
1396 cds_lfht_lookup(state->session_triggers_ht,
1397 hash_key_str(session_name, lttng_ht_seed),
1398 match_session_trigger_list,
1399 session_name,
1400 &iter);
1401 node = cds_lfht_iter_get_node(&iter);
1402 if (!node) {
1403 /*
1404 * Not an error, the list of triggers applying to that session
1405 * will be initialized when the session is created.
1406 */
1407 DBG("[notification-thread] No trigger list found for session \"%s\" as it is not yet known to the notification system",
1408 session_name);
1409 goto end;
1410 }
1411
e742b055 1412 list = caa_container_of(node,
ed327204
JG
1413 struct lttng_session_trigger_list,
1414 session_triggers_ht_node);
1415end:
1416 return list;
1417}
1418
ea9a44f0
JG
1419/*
1420 * Allocate an empty lttng_session_trigger_list for the session named
1421 * 'session_name'.
1422 *
1423 * No ownership of 'session_name' is assumed by the session trigger list.
1424 * It is the caller's responsability to ensure the session name is alive
1425 * for as long as this list is.
1426 */
1427static
1428struct lttng_session_trigger_list *lttng_session_trigger_list_create(
1429 const char *session_name,
1430 struct cds_lfht *session_triggers_ht)
1431{
1432 struct lttng_session_trigger_list *list;
1433
1434 list = zmalloc(sizeof(*list));
1435 if (!list) {
1436 goto end;
1437 }
1438 list->session_name = session_name;
1439 CDS_INIT_LIST_HEAD(&list->list);
1440 cds_lfht_node_init(&list->session_triggers_ht_node);
1441 list->session_triggers_ht = session_triggers_ht;
1442
1443 rcu_read_lock();
1444 /* Publish the list through the session_triggers_ht. */
1445 cds_lfht_add(session_triggers_ht,
1446 hash_key_str(session_name, lttng_ht_seed),
1447 &list->session_triggers_ht_node);
1448 rcu_read_unlock();
1449end:
1450 return list;
1451}
1452
1453static
1454void free_session_trigger_list_rcu(struct rcu_head *node)
1455{
1456 free(caa_container_of(node, struct lttng_session_trigger_list,
1457 rcu_node));
1458}
1459
1460static
1461void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list *list)
1462{
1463 struct lttng_trigger_list_element *trigger_list_element, *tmp;
1464
1465 /* Empty the list element by element, and then free the list itself. */
1466 cds_list_for_each_entry_safe(trigger_list_element, tmp,
1467 &list->list, node) {
1468 cds_list_del(&trigger_list_element->node);
1469 free(trigger_list_element);
1470 }
1471 rcu_read_lock();
1472 /* Unpublish the list from the session_triggers_ht. */
1473 cds_lfht_del(list->session_triggers_ht,
1474 &list->session_triggers_ht_node);
1475 rcu_read_unlock();
1476 call_rcu(&list->rcu_node, free_session_trigger_list_rcu);
1477}
1478
1479static
1480int lttng_session_trigger_list_add(struct lttng_session_trigger_list *list,
f2b3ef9f 1481 struct lttng_trigger *trigger)
ea9a44f0
JG
1482{
1483 int ret = 0;
1484 struct lttng_trigger_list_element *new_element =
1485 zmalloc(sizeof(*new_element));
1486
1487 if (!new_element) {
1488 ret = -1;
1489 goto end;
1490 }
1491 CDS_INIT_LIST_HEAD(&new_element->node);
1492 new_element->trigger = trigger;
1493 cds_list_add(&new_element->node, &list->list);
1494end:
1495 return ret;
1496}
1497
1498static
1499bool trigger_applies_to_session(const struct lttng_trigger *trigger,
1500 const char *session_name)
1501{
1502 bool applies = false;
1503 const struct lttng_condition *condition;
1504
1505 condition = lttng_trigger_get_const_condition(trigger);
1506 switch (lttng_condition_get_type(condition)) {
1507 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
1508 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
1509 {
1510 enum lttng_condition_status condition_status;
1511 const char *condition_session_name;
1512
1513 condition_status = lttng_condition_session_rotation_get_session_name(
1514 condition, &condition_session_name);
1515 if (condition_status != LTTNG_CONDITION_STATUS_OK) {
1516 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
1517 goto end;
1518 }
1519
1520 assert(condition_session_name);
1521 applies = !strcmp(condition_session_name, session_name);
1522 break;
1523 }
1524 default:
1525 goto end;
1526 }
1527end:
1528 return applies;
1529}
1530
1531/*
1532 * Allocate and initialize an lttng_session_trigger_list which contains
1533 * all triggers that apply to the session named 'session_name'.
1534 *
1535 * No ownership of 'session_name' is assumed by the session trigger list.
1536 * It is the caller's responsability to ensure the session name is alive
1537 * for as long as this list is.
1538 */
1539static
1540struct lttng_session_trigger_list *lttng_session_trigger_list_build(
1541 const struct notification_thread_state *state,
1542 const char *session_name)
1543{
1544 int trigger_count = 0;
1545 struct lttng_session_trigger_list *session_trigger_list = NULL;
1546 struct lttng_trigger_ht_element *trigger_ht_element = NULL;
1547 struct cds_lfht_iter iter;
1548
1549 session_trigger_list = lttng_session_trigger_list_create(session_name,
1550 state->session_triggers_ht);
1551
1552 /* Add all triggers applying to the session named 'session_name'. */
1553 cds_lfht_for_each_entry(state->triggers_ht, &iter, trigger_ht_element,
1554 node) {
1555 int ret;
1556
1557 if (!trigger_applies_to_session(trigger_ht_element->trigger,
1558 session_name)) {
1559 continue;
1560 }
1561
1562 ret = lttng_session_trigger_list_add(session_trigger_list,
1563 trigger_ht_element->trigger);
1564 if (ret) {
1565 goto error;
1566 }
1567
1568 trigger_count++;
1569 }
1570
1571 DBG("[notification-thread] Found %i triggers that apply to newly created session",
1572 trigger_count);
1573 return session_trigger_list;
1574error:
1575 lttng_session_trigger_list_destroy(session_trigger_list);
1576 return NULL;
1577}
1578
8abe313a
JG
1579static
1580struct session_info *find_or_create_session_info(
1581 struct notification_thread_state *state,
1582 const char *name, uid_t uid, gid_t gid)
1583{
1584 struct session_info *session = NULL;
1585 struct cds_lfht_node *node;
1586 struct cds_lfht_iter iter;
ea9a44f0 1587 struct lttng_session_trigger_list *trigger_list;
8abe313a
JG
1588
1589 rcu_read_lock();
1590 cds_lfht_lookup(state->sessions_ht,
1591 hash_key_str(name, lttng_ht_seed),
1592 match_session,
1593 name,
1594 &iter);
1595 node = cds_lfht_iter_get_node(&iter);
1596 if (node) {
1597 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1598 name, uid, gid);
1599 session = caa_container_of(node, struct session_info,
1600 sessions_ht_node);
1601 assert(session->uid == uid);
1602 assert(session->gid == gid);
1eee26c5
JG
1603 session_info_get(session);
1604 goto end;
ea9a44f0
JG
1605 }
1606
1607 trigger_list = lttng_session_trigger_list_build(state, name);
1608 if (!trigger_list) {
1609 goto error;
8abe313a
JG
1610 }
1611
1eee26c5
JG
1612 session = session_info_create(name, uid, gid, trigger_list,
1613 state->sessions_ht);
8abe313a
JG
1614 if (!session) {
1615 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1616 name, uid, gid);
b248644d 1617 lttng_session_trigger_list_destroy(trigger_list);
ea9a44f0 1618 goto error;
8abe313a 1619 }
ea9a44f0 1620 trigger_list = NULL;
577983cb
JG
1621
1622 cds_lfht_add(state->sessions_ht, hash_key_str(name, lttng_ht_seed),
9b63a4aa 1623 &session->sessions_ht_node);
1eee26c5 1624end:
8abe313a
JG
1625 rcu_read_unlock();
1626 return session;
ea9a44f0
JG
1627error:
1628 rcu_read_unlock();
1629 session_info_put(session);
1630 return NULL;
8abe313a
JG
1631}
1632
ab0ee2ca
JG
1633static
1634int handle_notification_thread_command_add_channel(
8abe313a
JG
1635 struct notification_thread_state *state,
1636 const char *session_name, uid_t session_uid, gid_t session_gid,
1637 const char *channel_name, enum lttng_domain_type channel_domain,
1638 uint64_t channel_key_int, uint64_t channel_capacity,
1639 enum lttng_error_code *cmd_result)
ab0ee2ca
JG
1640{
1641 struct cds_list_head trigger_list;
8abe313a
JG
1642 struct channel_info *new_channel_info = NULL;
1643 struct channel_key channel_key = {
1644 .key = channel_key_int,
1645 .domain = channel_domain,
1646 };
ab0ee2ca
JG
1647 struct lttng_channel_trigger_list *channel_trigger_list = NULL;
1648 struct lttng_trigger_ht_element *trigger_ht_element = NULL;
1649 int trigger_count = 0;
1650 struct cds_lfht_iter iter;
8abe313a 1651 struct session_info *session_info = NULL;
ab0ee2ca
JG
1652
1653 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64 " in %s domain",
8abe313a 1654 channel_name, session_name, channel_key_int,
526200e4 1655 lttng_domain_type_str(channel_domain));
ab0ee2ca
JG
1656
1657 CDS_INIT_LIST_HEAD(&trigger_list);
1658
8abe313a
JG
1659 session_info = find_or_create_session_info(state, session_name,
1660 session_uid, session_gid);
1661 if (!session_info) {
a9577b76 1662 /* Allocation error or an internal error occurred. */
ab0ee2ca
JG
1663 goto error;
1664 }
1665
8abe313a
JG
1666 new_channel_info = channel_info_create(channel_name, &channel_key,
1667 channel_capacity, session_info);
1668 if (!new_channel_info) {
1669 goto error;
1670 }
ab0ee2ca 1671
83b934ad 1672 rcu_read_lock();
ab0ee2ca
JG
1673 /* Build a list of all triggers applying to the new channel. */
1674 cds_lfht_for_each_entry(state->triggers_ht, &iter, trigger_ht_element,
1675 node) {
1676 struct lttng_trigger_list_element *new_element;
1677
1678 if (!trigger_applies_to_channel(trigger_ht_element->trigger,
8abe313a 1679 new_channel_info)) {
ab0ee2ca
JG
1680 continue;
1681 }
1682
1683 new_element = zmalloc(sizeof(*new_element));
1684 if (!new_element) {
83b934ad 1685 rcu_read_unlock();
ab0ee2ca
JG
1686 goto error;
1687 }
1688 CDS_INIT_LIST_HEAD(&new_element->node);
1689 new_element->trigger = trigger_ht_element->trigger;
1690 cds_list_add(&new_element->node, &trigger_list);
1691 trigger_count++;
1692 }
83b934ad 1693 rcu_read_unlock();
ab0ee2ca
JG
1694
1695 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1696 trigger_count);
1697 channel_trigger_list = zmalloc(sizeof(*channel_trigger_list));
1698 if (!channel_trigger_list) {
1699 goto error;
1700 }
8abe313a 1701 channel_trigger_list->channel_key = new_channel_info->key;
ab0ee2ca
JG
1702 CDS_INIT_LIST_HEAD(&channel_trigger_list->list);
1703 cds_lfht_node_init(&channel_trigger_list->channel_triggers_ht_node);
1704 cds_list_splice(&trigger_list, &channel_trigger_list->list);
1705
1706 rcu_read_lock();
1707 /* Add channel to the channel_ht which owns the channel_infos. */
1708 cds_lfht_add(state->channels_ht,
8abe313a 1709 hash_channel_key(&new_channel_info->key),
ab0ee2ca
JG
1710 &new_channel_info->channels_ht_node);
1711 /*
1712 * Add the list of triggers associated with this channel to the
1713 * channel_triggers_ht.
1714 */
1715 cds_lfht_add(state->channel_triggers_ht,
8abe313a 1716 hash_channel_key(&new_channel_info->key),
ab0ee2ca
JG
1717 &channel_trigger_list->channel_triggers_ht_node);
1718 rcu_read_unlock();
1eee26c5 1719 session_info_put(session_info);
ab0ee2ca
JG
1720 *cmd_result = LTTNG_OK;
1721 return 0;
1722error:
ab0ee2ca 1723 channel_info_destroy(new_channel_info);
8abe313a 1724 session_info_put(session_info);
ab0ee2ca
JG
1725 return 1;
1726}
1727
83b934ad
MD
1728static
1729void free_channel_trigger_list_rcu(struct rcu_head *node)
1730{
1731 free(caa_container_of(node, struct lttng_channel_trigger_list,
1732 rcu_node));
1733}
1734
1735static
1736void free_channel_state_sample_rcu(struct rcu_head *node)
1737{
1738 free(caa_container_of(node, struct channel_state_sample,
1739 rcu_node));
1740}
1741
ab0ee2ca
JG
1742static
1743int handle_notification_thread_command_remove_channel(
1744 struct notification_thread_state *state,
1745 uint64_t channel_key, enum lttng_domain_type domain,
1746 enum lttng_error_code *cmd_result)
1747{
1748 struct cds_lfht_node *node;
1749 struct cds_lfht_iter iter;
1750 struct lttng_channel_trigger_list *trigger_list;
1751 struct lttng_trigger_list_element *trigger_list_element, *tmp;
1752 struct channel_key key = { .key = channel_key, .domain = domain };
1753 struct channel_info *channel_info;
1754
1755 DBG("[notification-thread] Removing channel key = %" PRIu64 " in %s domain",
526200e4 1756 channel_key, lttng_domain_type_str(domain));
ab0ee2ca
JG
1757
1758 rcu_read_lock();
1759
1760 cds_lfht_lookup(state->channel_triggers_ht,
1761 hash_channel_key(&key),
1762 match_channel_trigger_list,
1763 &key,
1764 &iter);
1765 node = cds_lfht_iter_get_node(&iter);
1766 /*
1767 * There is a severe internal error if we are being asked to remove a
1768 * channel that doesn't exist.
1769 */
1770 if (!node) {
1771 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1772 goto end;
1773 }
1774
1775 /* Free the list of triggers associated with this channel. */
1776 trigger_list = caa_container_of(node, struct lttng_channel_trigger_list,
1777 channel_triggers_ht_node);
1778 cds_list_for_each_entry_safe(trigger_list_element, tmp,
1779 &trigger_list->list, node) {
1780 cds_list_del(&trigger_list_element->node);
1781 free(trigger_list_element);
1782 }
1783 cds_lfht_del(state->channel_triggers_ht, node);
83b934ad 1784 call_rcu(&trigger_list->rcu_node, free_channel_trigger_list_rcu);
ab0ee2ca
JG
1785
1786 /* Free sampled channel state. */
1787 cds_lfht_lookup(state->channel_state_ht,
1788 hash_channel_key(&key),
1789 match_channel_state_sample,
1790 &key,
1791 &iter);
1792 node = cds_lfht_iter_get_node(&iter);
1793 /*
1794 * This is expected to be NULL if the channel is destroyed before we
1795 * received a sample.
1796 */
1797 if (node) {
1798 struct channel_state_sample *sample = caa_container_of(node,
1799 struct channel_state_sample,
1800 channel_state_ht_node);
1801
1802 cds_lfht_del(state->channel_state_ht, node);
83b934ad 1803 call_rcu(&sample->rcu_node, free_channel_state_sample_rcu);
ab0ee2ca
JG
1804 }
1805
1806 /* Remove the channel from the channels_ht and free it. */
1807 cds_lfht_lookup(state->channels_ht,
1808 hash_channel_key(&key),
1809 match_channel_info,
1810 &key,
1811 &iter);
1812 node = cds_lfht_iter_get_node(&iter);
1813 assert(node);
1814 channel_info = caa_container_of(node, struct channel_info,
1815 channels_ht_node);
1816 cds_lfht_del(state->channels_ht, node);
1817 channel_info_destroy(channel_info);
1818end:
1819 rcu_read_unlock();
1820 *cmd_result = LTTNG_OK;
1821 return 0;
1822}
1823
0ca52944 1824static
ed327204 1825int handle_notification_thread_command_session_rotation(
0ca52944 1826 struct notification_thread_state *state,
ed327204
JG
1827 enum notification_thread_command_type cmd_type,
1828 const char *session_name, uid_t session_uid, gid_t session_gid,
1829 uint64_t trace_archive_chunk_id,
1830 struct lttng_trace_archive_location *location,
1831 enum lttng_error_code *_cmd_result)
0ca52944 1832{
ed327204
JG
1833 int ret = 0;
1834 enum lttng_error_code cmd_result = LTTNG_OK;
1835 struct lttng_session_trigger_list *trigger_list;
1836 struct lttng_trigger_list_element *trigger_list_element;
1837 struct session_info *session_info;
f2b3ef9f 1838 const struct lttng_credentials session_creds = {
ff588497
JR
1839 .uid = LTTNG_OPTIONAL_INIT_VALUE(session_uid),
1840 .gid = LTTNG_OPTIONAL_INIT_VALUE(session_gid),
f2b3ef9f 1841 };
0ca52944 1842
ed327204
JG
1843 rcu_read_lock();
1844
1845 session_info = find_or_create_session_info(state, session_name,
1846 session_uid, session_gid);
1847 if (!session_info) {
a9577b76 1848 /* Allocation error or an internal error occurred. */
ed327204
JG
1849 ret = -1;
1850 cmd_result = LTTNG_ERR_NOMEM;
1851 goto end;
1852 }
1853
1854 session_info->rotation.ongoing =
1855 cmd_type == NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING;
1856 session_info->rotation.id = trace_archive_chunk_id;
1857 trigger_list = get_session_trigger_list(state, session_name);
1858 if (!trigger_list) {
1859 DBG("[notification-thread] No triggers applying to session \"%s\" found",
1860 session_name);
1861 goto end;
1862 }
1863
1864 cds_list_for_each_entry(trigger_list_element, &trigger_list->list,
1865 node) {
1866 const struct lttng_condition *condition;
f2b3ef9f 1867 struct lttng_trigger *trigger;
ed327204
JG
1868 struct notification_client_list *client_list;
1869 struct lttng_evaluation *evaluation = NULL;
1870 enum lttng_condition_type condition_type;
f2b3ef9f 1871 enum action_executor_status executor_status;
ed327204
JG
1872
1873 trigger = trigger_list_element->trigger;
1874 condition = lttng_trigger_get_const_condition(trigger);
1875 assert(condition);
1876 condition_type = lttng_condition_get_type(condition);
1877
1878 if (condition_type == LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING &&
1879 cmd_type != NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING) {
1880 continue;
1881 } else if (condition_type == LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED &&
1882 cmd_type != NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED) {
1883 continue;
1884 }
1885
ed327204 1886 client_list = get_client_list_from_condition(state, condition);
ed327204
JG
1887 if (cmd_type == NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING) {
1888 evaluation = lttng_evaluation_session_rotation_ongoing_create(
1889 trace_archive_chunk_id);
1890 } else {
1891 evaluation = lttng_evaluation_session_rotation_completed_create(
1892 trace_archive_chunk_id, location);
1893 }
1894
1895 if (!evaluation) {
1896 /* Internal error */
1897 ret = -1;
1898 cmd_result = LTTNG_ERR_UNK;
505b2d90 1899 goto put_list;
ed327204
JG
1900 }
1901
f2b3ef9f
JG
1902 /*
1903 * Ownership of `evaluation` transferred to the action executor
1904 * no matter the result.
1905 */
1906 executor_status = action_executor_enqueue(state->executor,
1907 trigger, evaluation, &session_creds,
1908 client_list);
1909 evaluation = NULL;
1910 switch (executor_status) {
1911 case ACTION_EXECUTOR_STATUS_OK:
1912 break;
1913 case ACTION_EXECUTOR_STATUS_ERROR:
1914 case ACTION_EXECUTOR_STATUS_INVALID:
1915 /*
1916 * TODO Add trigger identification (name/id) when
1917 * it is added to the API.
1918 */
1919 ERR("Fatal error occurred while enqueuing action associated with session rotation trigger");
1920 ret = -1;
1921 goto put_list;
1922 case ACTION_EXECUTOR_STATUS_OVERFLOW:
1923 /*
1924 * TODO Add trigger identification (name/id) when
1925 * it is added to the API.
1926 *
1927 * Not a fatal error.
1928 */
1929 WARN("No space left when enqueuing action associated with session rotation trigger");
1930 ret = 0;
1931 goto put_list;
1932 default:
1933 abort();
1934 }
1935
505b2d90
JG
1936put_list:
1937 notification_client_list_put(client_list);
ed327204 1938 if (caa_unlikely(ret)) {
505b2d90 1939 break;
ed327204
JG
1940 }
1941 }
1942end:
1943 session_info_put(session_info);
1944 *_cmd_result = cmd_result;
1945 rcu_read_unlock();
1946 return ret;
0ca52944
JG
1947}
1948
d02d7404
JR
1949static
1950int handle_notification_thread_command_add_tracer_event_source(
1951 struct notification_thread_state *state,
1952 int tracer_event_source_fd,
1953 enum lttng_domain_type domain_type,
1954 enum lttng_error_code *_cmd_result)
1955{
1956 int ret = 0;
1957 enum lttng_error_code cmd_result = LTTNG_OK;
1958 struct notification_event_tracer_event_source_element *element = NULL;
1959
1960 element = zmalloc(sizeof(*element));
1961 if (!element) {
1962 cmd_result = LTTNG_ERR_NOMEM;
1963 ret = -1;
1964 goto end;
1965 }
1966
d02d7404
JR
1967 element->fd = tracer_event_source_fd;
1968 element->domain = domain_type;
1969
1970 cds_list_add(&element->node, &state->tracer_event_sources_list);
1971
1972 DBG3("[notification-thread] Adding tracer event source fd to poll set: tracer_event_source_fd = %d, domain = '%s'",
1973 tracer_event_source_fd,
1974 lttng_domain_type_str(domain_type));
1975
1976 /* Adding the read side pipe to the event poll. */
1977 ret = lttng_poll_add(&state->events, tracer_event_source_fd, LPOLLIN | LPOLLERR);
1978 if (ret < 0) {
1979 ERR("[notification-thread] Failed to add tracer event source to poll set: tracer_event_source_fd = %d, domain = '%s'",
1980 tracer_event_source_fd,
1981 lttng_domain_type_str(element->domain));
1982 cds_list_del(&element->node);
1983 free(element);
1984 goto end;
1985 }
1986
1987 element->is_fd_in_poll_set = true;
1988
1989end:
1990 *_cmd_result = cmd_result;
1991 return ret;
1992}
1993
8b524060
FD
1994static
1995int drain_event_notifier_notification_pipe(
1996 struct notification_thread_state *state,
1997 int pipe, enum lttng_domain_type domain)
1998{
1999 struct lttng_poll_event events = {0};
2000 int ret;
2001
2002 ret = lttng_poll_create(&events, 1, LTTNG_CLOEXEC);
2003 if (ret < 0) {
2004 ERR("[notification-thread] Error creating lttng_poll_event");
2005 goto end;
2006 }
2007
2008 ret = lttng_poll_add(&events, pipe, LPOLLIN);
2009 if (ret < 0) {
2010 ERR("[notification-thread] Error adding fd event notifier notification pipe to lttng_poll_event: fd = %d",
2011 pipe);
2012 goto end;
2013 }
2014
2015 while (true) {
2016 /*
2017 * Continue to consume notifications as long as there are new
2018 * ones coming in. The tracer has been asked to stop producing
2019 * them.
2020 *
2021 * LPOLLIN is explicitly checked since LPOLLHUP is implicitly
2022 * monitored (on Linux, at least) and will be returned when
2023 * the pipe is closed but empty.
2024 */
2025 ret = lttng_poll_wait_interruptible(&events, 0);
a17b133b 2026 if (ret == 0 || (LTTNG_POLL_GETEV(&events, 0) & LPOLLIN) == 0) {
8b524060
FD
2027 /* No more notification to be read on this pipe. */
2028 ret = 0;
2029 goto end;
2030 } else if (ret < 0) {
2031 PERROR("Failed on lttng_poll_wait_interruptible() call");
2032 ret = -1;
2033 goto end;
2034 }
2035
2036 ret = handle_one_event_notifier_notification(state, pipe, domain);
2037 if (ret) {
2038 ERR("[notification-thread] Error consuming an event notifier notification from pipe: fd = %d",
2039 pipe);
2040 }
2041 }
2042end:
2043 lttng_poll_clean(&events);
2044 return ret;
2045}
2046
d02d7404
JR
2047static
2048int handle_notification_thread_command_remove_tracer_event_source(
2049 struct notification_thread_state *state,
2050 int tracer_event_source_fd,
2051 enum lttng_error_code *_cmd_result)
2052{
2053 int ret = 0;
2fb5e9b7 2054 bool found = false;
d02d7404
JR
2055 enum lttng_error_code cmd_result = LTTNG_OK;
2056 struct notification_event_tracer_event_source_element *source_element = NULL, *tmp;
2057
2058 cds_list_for_each_entry_safe(source_element, tmp,
2059 &state->tracer_event_sources_list, node) {
2060 if (source_element->fd != tracer_event_source_fd) {
2061 continue;
2062 }
2063
2064 DBG("[notification-thread] Removed tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2065 tracer_event_source_fd,
2066 lttng_domain_type_str(source_element->domain));
2067 cds_list_del(&source_element->node);
2fb5e9b7 2068 found = true;
d02d7404
JR
2069 break;
2070 }
2071
2fb5e9b7
JG
2072 if (!found) {
2073 /*
2074 * This is temporarily allowed since the poll activity set is
2075 * not properly cleaned-up for the moment. This is adressed in
2076 * an upcoming fix.
2077 */
2078 source_element = NULL;
2079 goto end;
2080 }
d02d7404
JR
2081
2082 if (!source_element->is_fd_in_poll_set) {
2083 /* Skip the poll set removal. */
2084 goto end;
2085 }
2086
2087 DBG3("[notification-thread] Removing tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2088 tracer_event_source_fd,
2089 lttng_domain_type_str(source_element->domain));
2090
2091 /* Removing the fd from the event poll set. */
2092 ret = lttng_poll_del(&state->events, tracer_event_source_fd);
2093 if (ret < 0) {
2094 ERR("[notification-thread] Failed to remove tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2095 tracer_event_source_fd,
2096 lttng_domain_type_str(source_element->domain));
2097 cmd_result = LTTNG_ERR_FATAL;
2098 goto end;
2099 }
2100
173900f6
JG
2101 source_element->is_fd_in_poll_set = false;
2102
8b524060
FD
2103 ret = drain_event_notifier_notification_pipe(state, tracer_event_source_fd,
2104 source_element->domain);
2105 if (ret) {
2106 ERR("[notification-thread] Error draining event notifier notification: tracer_event_source_fd = %d, domain = %s",
2107 tracer_event_source_fd,
2108 lttng_domain_type_str(source_element->domain));
2109 cmd_result = LTTNG_ERR_FATAL;
2110 goto end;
2111 }
2112
2113 /*
2114 * The drain_event_notifier_notification_pipe() call might have read
2115 * data from an fd that we received in event in the latest _poll_wait()
2116 * call. Make sure the thread call poll_wait() again to ensure we have
2117 * a clean state.
2118 */
2119 state->restart_poll = true;
2120
d02d7404
JR
2121end:
2122 free(source_element);
2123 *_cmd_result = cmd_result;
2124 return ret;
2125}
2126
90aa04a1
FD
2127static
2128int condition_on_event_update_error_count(struct lttng_trigger *trigger)
2129{
2130 int ret = 0;
2131 uint64_t error_count = 0;
2132 struct lttng_condition *condition;
2133 enum event_notifier_error_accounting_status status;
2134
2135 condition = lttng_trigger_get_condition(trigger);
2136 assert(lttng_condition_get_type(condition) ==
2137 LTTNG_CONDITION_TYPE_ON_EVENT);
2138
2139 status = event_notifier_error_accounting_get_count(trigger, &error_count);
2140 if (status != EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK) {
2141 uid_t trigger_owner_uid;
2142 const char *trigger_name;
2143 const enum lttng_trigger_status trigger_status =
2144 lttng_trigger_get_owner_uid(
2145 trigger, &trigger_owner_uid);
2146
2147 assert(trigger_status == LTTNG_TRIGGER_STATUS_OK);
2148 if (lttng_trigger_get_name(trigger, &trigger_name) != LTTNG_TRIGGER_STATUS_OK) {
2149 trigger_name = "(unnamed)";
2150 }
2151
2152 ERR("Failed to get event notifier error count of trigger for update: trigger owner = %d, trigger name = '%s'",
2153 trigger_owner_uid, trigger_name);
2154 ret = -1;
2155 }
2156
2157 lttng_condition_on_event_set_error_count(condition, error_count);
2158 return ret;
2159}
2160
d02d7404
JR
2161int handle_notification_thread_remove_tracer_event_source_no_result(
2162 struct notification_thread_state *state,
2163 int tracer_event_source_fd)
2164{
2165 int ret;
2166 enum lttng_error_code cmd_result;
2167
2168 ret = handle_notification_thread_command_remove_tracer_event_source(
2169 state, tracer_event_source_fd, &cmd_result);
2170 (void) cmd_result;
2171 return ret;
2172}
2173
90aa04a1
FD
2174static
2175bool action_type_needs_tracer_notifier(enum lttng_action_type action_type)
2176{
2177 switch (action_type) {
2178 case LTTNG_ACTION_TYPE_NOTIFY:
2179 case LTTNG_ACTION_TYPE_START_SESSION:
2180 case LTTNG_ACTION_TYPE_STOP_SESSION:
2181 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION:
2182 case LTTNG_ACTION_TYPE_ROTATE_SESSION:
2183 return true;
2184 case LTTNG_ACTION_TYPE_GROUP:
2185 case LTTNG_ACTION_TYPE_UNKNOWN:
2186 default:
2187 abort();
2188 }
2189}
2190
2191static
2192bool action_needs_tracer_notifier(const struct lttng_action *action)
2193{
2194 bool needs_tracer_notifier = false;
2195 unsigned int i, count;
2196 enum lttng_action_status action_status;
2197 enum lttng_action_type action_type;
2198
2199 assert(action);
2200 /* If there is only one action. Check if it needs a tracer notifier. */
2201 action_type = lttng_action_get_type(action);
2202 if (action_type != LTTNG_ACTION_TYPE_GROUP) {
2203 needs_tracer_notifier = action_type_needs_tracer_notifier(
2204 action_type);
2205 goto end;
2206 }
2207
2208 /*
2209 * Iterate over all the actions of the action group and check if any of
2210 * them needs a tracer notifier.
2211 */
2212 action_status = lttng_action_group_get_count(action, &count);
2213 assert(action_status == LTTNG_ACTION_STATUS_OK);
2214 for (i = 0; i < count; i++) {
2215 const struct lttng_action *inner_action =
2216 lttng_action_group_get_at_index(action, i);
2217
2218 action_type = lttng_action_get_type(inner_action);
2219 if (action_type_needs_tracer_notifier(action_type)) {
2220 needs_tracer_notifier = true;
2221 goto end;
2222 }
2223 }
2224
2225end:
2226 return needs_tracer_notifier;
2227}
2228
2229/*
2230 * A given trigger needs a tracer notifier if
2231 * it has an event-rule condition,
2232 * AND
2233 * it has one or more sessiond-execution action.
2234 */
2235static
2236bool trigger_needs_tracer_notifier(const struct lttng_trigger *trigger)
2237{
2238 bool needs_tracer_notifier = false;
2239 const struct lttng_condition *condition =
2240 lttng_trigger_get_const_condition(trigger);
2241 const struct lttng_action *action =
2242 lttng_trigger_get_const_action(trigger);
2243
2244 switch (lttng_condition_get_type(condition)) {
2245 case LTTNG_CONDITION_TYPE_ON_EVENT:
2246 needs_tracer_notifier = action_needs_tracer_notifier(action);
2247 goto end;
2248 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
2249 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
2250 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
2251 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
2252 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
2253 goto end;
2254 case LTTNG_CONDITION_TYPE_UNKNOWN:
2255 default:
2256 abort();
2257 }
2258end:
2259 return needs_tracer_notifier;
2260}
2261
fbc9f37d
JR
2262static int handle_notification_thread_command_list_triggers(
2263 struct notification_thread_handle *handle,
2264 struct notification_thread_state *state,
2265 uid_t client_uid,
2266 struct lttng_triggers **triggers,
2267 enum lttng_error_code *_cmd_result)
2268{
2269 int ret = 0;
2270 enum lttng_error_code cmd_result = LTTNG_OK;
2271 struct cds_lfht_iter iter;
2272 struct lttng_trigger_ht_element *trigger_ht_element;
2273 struct lttng_triggers *local_triggers = NULL;
2274 const struct lttng_credentials *creds;
2275
2276 rcu_read_lock();
2277
2278 local_triggers = lttng_triggers_create();
2279 if (!local_triggers) {
2280 /* Not a fatal error. */
2281 cmd_result = LTTNG_ERR_NOMEM;
2282 goto end;
2283 }
2284
2285 cds_lfht_for_each_entry(state->triggers_ht, &iter,
2286 trigger_ht_element, node) {
2287 /*
2288 * Only return the triggers to which the client has access.
2289 * The root user has visibility over all triggers.
2290 */
2291 creds = lttng_trigger_get_credentials(trigger_ht_element->trigger);
2292 if (client_uid != lttng_credentials_get_uid(creds) && client_uid != 0) {
2293 continue;
2294 }
2295
90aa04a1
FD
2296 if (trigger_needs_tracer_notifier(trigger_ht_element->trigger)) {
2297 ret = condition_on_event_update_error_count(
2298 trigger_ht_element->trigger);
2299 assert(!ret);
2300 }
2301
fbc9f37d
JR
2302 ret = lttng_triggers_add(local_triggers,
2303 trigger_ht_element->trigger);
2304 if (ret < 0) {
2305 /* Not a fatal error. */
2306 ret = 0;
2307 cmd_result = LTTNG_ERR_NOMEM;
2308 goto end;
2309 }
2310 }
2311
2312 /* Transferring ownership to the caller. */
2313 *triggers = local_triggers;
2314 local_triggers = NULL;
2315
2316end:
2317 rcu_read_unlock();
2318 lttng_triggers_destroy(local_triggers);
2319 *_cmd_result = cmd_result;
2320 return ret;
2321}
2322
1da26331 2323static
959e3c66 2324bool condition_is_supported(struct lttng_condition *condition)
1da26331 2325{
959e3c66 2326 bool is_supported;
1da26331
JG
2327
2328 switch (lttng_condition_get_type(condition)) {
2329 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
2330 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
2331 {
959e3c66 2332 int ret;
1da26331
JG
2333 enum lttng_domain_type domain;
2334
2335 ret = lttng_condition_buffer_usage_get_domain_type(condition,
2336 &domain);
959e3c66 2337 assert(ret == 0);
1da26331
JG
2338
2339 if (domain != LTTNG_DOMAIN_KERNEL) {
959e3c66 2340 is_supported = true;
1da26331
JG
2341 goto end;
2342 }
2343
2344 /*
2345 * Older kernel tracers don't expose the API to monitor their
2346 * buffers. Therefore, we reject triggers that require that
2347 * mechanism to be available to be evaluated.
959e3c66
JR
2348 *
2349 * Assume unsupported on error.
1da26331 2350 */
959e3c66
JR
2351 is_supported = kernel_supports_ring_buffer_snapshot_sample_positions() == 1;
2352 break;
2353 }
d602bd6a 2354 case LTTNG_CONDITION_TYPE_ON_EVENT:
959e3c66
JR
2355 {
2356 const struct lttng_event_rule *event_rule;
2357 enum lttng_domain_type domain;
2358 const enum lttng_condition_status status =
d602bd6a 2359 lttng_condition_on_event_get_rule(
959e3c66
JR
2360 condition, &event_rule);
2361
2362 assert(status == LTTNG_CONDITION_STATUS_OK);
2363
2364 domain = lttng_event_rule_get_domain_type(event_rule);
2365 if (domain != LTTNG_DOMAIN_KERNEL) {
2366 is_supported = true;
2367 goto end;
2368 }
2369
2370 /*
2371 * Older kernel tracers can't emit notification. Therefore, we
2372 * reject triggers that require that mechanism to be available
2373 * to be evaluated.
2374 *
2375 * Assume unsupported on error.
2376 */
2377 is_supported = kernel_supports_event_notifiers() == 1;
1da26331
JG
2378 break;
2379 }
2380 default:
959e3c66 2381 is_supported = true;
1da26331
JG
2382 }
2383end:
959e3c66 2384 return is_supported;
1da26331
JG
2385}
2386
51eab943
JG
2387/* Must be called with RCU read lock held. */
2388static
f2b3ef9f 2389int bind_trigger_to_matching_session(struct lttng_trigger *trigger,
51eab943
JG
2390 struct notification_thread_state *state)
2391{
2392 int ret = 0;
51eab943
JG
2393 const struct lttng_condition *condition;
2394 const char *session_name;
2395 struct lttng_session_trigger_list *trigger_list;
2396
2397 condition = lttng_trigger_get_const_condition(trigger);
2398 switch (lttng_condition_get_type(condition)) {
2399 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
2400 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
2401 {
2402 enum lttng_condition_status status;
2403
2404 status = lttng_condition_session_rotation_get_session_name(
2405 condition, &session_name);
2406 if (status != LTTNG_CONDITION_STATUS_OK) {
2407 ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
2408 ret = -1;
2409 goto end;
2410 }
2411 break;
2412 }
2413 default:
2414 ret = -1;
2415 goto end;
2416 }
2417
ed327204
JG
2418 trigger_list = get_session_trigger_list(state, session_name);
2419 if (!trigger_list) {
51eab943
JG
2420 DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
2421 session_name);
2422 goto end;
51eab943 2423
ed327204 2424 }
51eab943
JG
2425
2426 DBG("[notification-thread] Newly registered trigger bound to session \"%s\"",
2427 session_name);
2428 ret = lttng_session_trigger_list_add(trigger_list, trigger);
2429end:
2430 return ret;
2431}
2432
2433/* Must be called with RCU read lock held. */
2434static
f2b3ef9f 2435int bind_trigger_to_matching_channels(struct lttng_trigger *trigger,
51eab943
JG
2436 struct notification_thread_state *state)
2437{
2438 int ret = 0;
2439 struct cds_lfht_node *node;
2440 struct cds_lfht_iter iter;
2441 struct channel_info *channel;
2442
2443 cds_lfht_for_each_entry(state->channels_ht, &iter, channel,
2444 channels_ht_node) {
2445 struct lttng_trigger_list_element *trigger_list_element;
2446 struct lttng_channel_trigger_list *trigger_list;
a5d64ae7 2447 struct cds_lfht_iter lookup_iter;
51eab943
JG
2448
2449 if (!trigger_applies_to_channel(trigger, channel)) {
2450 continue;
2451 }
2452
2453 cds_lfht_lookup(state->channel_triggers_ht,
2454 hash_channel_key(&channel->key),
2455 match_channel_trigger_list,
2456 &channel->key,
a5d64ae7
MD
2457 &lookup_iter);
2458 node = cds_lfht_iter_get_node(&lookup_iter);
51eab943
JG
2459 assert(node);
2460 trigger_list = caa_container_of(node,
2461 struct lttng_channel_trigger_list,
2462 channel_triggers_ht_node);
2463
2464 trigger_list_element = zmalloc(sizeof(*trigger_list_element));
2465 if (!trigger_list_element) {
2466 ret = -1;
2467 goto end;
2468 }
2469 CDS_INIT_LIST_HEAD(&trigger_list_element->node);
2470 trigger_list_element->trigger = trigger;
2471 cds_list_add(&trigger_list_element->node, &trigger_list->list);
2472 DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"",
2473 channel->name);
2474 }
2475end:
2476 return ret;
2477}
2478
f2b3ef9f
JG
2479static
2480bool is_trigger_action_notify(const struct lttng_trigger *trigger)
2481{
2482 bool is_notify = false;
2483 unsigned int i, count;
2484 enum lttng_action_status action_status;
2485 const struct lttng_action *action =
2486 lttng_trigger_get_const_action(trigger);
2487 enum lttng_action_type action_type;
2488
2489 assert(action);
17182cfd 2490 action_type = lttng_action_get_type(action);
f2b3ef9f
JG
2491 if (action_type == LTTNG_ACTION_TYPE_NOTIFY) {
2492 is_notify = true;
2493 goto end;
2494 } else if (action_type != LTTNG_ACTION_TYPE_GROUP) {
2495 goto end;
2496 }
2497
2498 action_status = lttng_action_group_get_count(action, &count);
2499 assert(action_status == LTTNG_ACTION_STATUS_OK);
2500
2501 for (i = 0; i < count; i++) {
2502 const struct lttng_action *inner_action =
2503 lttng_action_group_get_at_index(
2504 action, i);
2505
17182cfd 2506 action_type = lttng_action_get_type(inner_action);
f2b3ef9f
JG
2507 if (action_type == LTTNG_ACTION_TYPE_NOTIFY) {
2508 is_notify = true;
2509 goto end;
2510 }
2511 }
2512
2513end:
2514 return is_notify;
2515}
2516
242388e4
JR
2517static bool trigger_name_taken(struct notification_thread_state *state,
2518 const struct lttng_trigger *trigger)
2519{
2520 struct cds_lfht_iter iter;
2521
2522 /*
2523 * No duplicata is allowed in the triggers_by_name_uid_ht.
2524 * The match is done against the trigger name and uid.
2525 */
2526 cds_lfht_lookup(state->triggers_by_name_uid_ht,
2527 hash_trigger_by_name_uid(trigger),
2528 match_trigger_by_name_uid,
2529 trigger,
2530 &iter);
2531 return !!cds_lfht_iter_get_node(&iter);
2532}
2533
2534static
2535enum lttng_error_code generate_trigger_name(
2536 struct notification_thread_state *state,
2537 struct lttng_trigger *trigger, const char **name)
2538{
2539 enum lttng_error_code ret_code = LTTNG_OK;
2540 bool taken = false;
2541 enum lttng_trigger_status status;
2542
2543 do {
2544 const int ret = lttng_trigger_generate_name(trigger,
2545 state->trigger_id.name_offset++);
2546 if (ret) {
2547 /* The only reason this can fail right now. */
2548 ret_code = LTTNG_ERR_NOMEM;
2549 break;
2550 }
2551
2552 status = lttng_trigger_get_name(trigger, name);
2553 assert(status == LTTNG_TRIGGER_STATUS_OK);
2554
2555 taken = trigger_name_taken(state, trigger);
2556 } while (taken || state->trigger_id.name_offset == UINT64_MAX);
2557
2558 return ret_code;
2559}
2560
2758e38b
FD
2561static inline
2562void notif_thread_state_remove_trigger_ht_elem(
2563 struct notification_thread_state *state,
2564 struct lttng_trigger_ht_element *trigger_ht_element)
2565{
2566 assert(state);
2567 assert(trigger_ht_element);
2568
2569 cds_lfht_del(state->triggers_ht, &trigger_ht_element->node);
2570 cds_lfht_del(state->triggers_by_name_uid_ht, &trigger_ht_element->node_by_name_uid);
2571}
2572
ab0ee2ca 2573/*
f2b3ef9f 2574 * FIXME A client's credentials are not checked when registering a trigger.
ab0ee2ca 2575 *
1da26331 2576 * The effects of this are benign since:
ab0ee2ca 2577 * - The client will succeed in registering the trigger, as it is valid,
51eab943 2578 * - The trigger will, internally, be bound to the channel/session,
ab0ee2ca
JG
2579 * - The notifications will not be sent since the client's credentials
2580 * are checked against the channel at that moment.
1da26331
JG
2581 *
2582 * If this function returns a non-zero value, it means something is
50ca7858 2583 * fundamentally broken and the whole subsystem/thread will be torn down.
1da26331
JG
2584 *
2585 * If a non-fatal error occurs, just set the cmd_result to the appropriate
2586 * error code.
ab0ee2ca
JG
2587 */
2588static
2589int handle_notification_thread_command_register_trigger(
8abe313a
JG
2590 struct notification_thread_state *state,
2591 struct lttng_trigger *trigger,
2592 enum lttng_error_code *cmd_result)
ab0ee2ca
JG
2593{
2594 int ret = 0;
2595 struct lttng_condition *condition;
2596 struct notification_client *client;
2597 struct notification_client_list *client_list = NULL;
2598 struct lttng_trigger_ht_element *trigger_ht_element = NULL;
f2b3ef9f 2599 struct notification_client_list_element *client_list_element;
e7c93cf9 2600 struct notification_trigger_tokens_ht_element *trigger_tokens_ht_element = NULL;
ab0ee2ca
JG
2601 struct cds_lfht_node *node;
2602 struct cds_lfht_iter iter;
242388e4 2603 const char* trigger_name;
ab0ee2ca 2604 bool free_trigger = true;
f2b3ef9f
JG
2605 struct lttng_evaluation *evaluation = NULL;
2606 struct lttng_credentials object_creds;
ff588497
JR
2607 uid_t object_uid;
2608 gid_t object_gid;
f2b3ef9f 2609 enum action_executor_status executor_status;
e6887944
JR
2610 const uint64_t trigger_tracer_token =
2611 state->trigger_id.next_tracer_token++;
ab0ee2ca
JG
2612
2613 rcu_read_lock();
2614
e6887944
JR
2615 /* Set the trigger's tracer token. */
2616 lttng_trigger_set_tracer_token(trigger, trigger_tracer_token);
2617
242388e4
JR
2618 if (lttng_trigger_get_name(trigger, &trigger_name) ==
2619 LTTNG_TRIGGER_STATUS_UNSET) {
2620 const enum lttng_error_code ret_code = generate_trigger_name(
2621 state, trigger, &trigger_name);
2622
2623 if (ret_code != LTTNG_OK) {
2624 /* Fatal error. */
2625 ret = -1;
2626 *cmd_result = ret_code;
2627 goto error;
2628 }
2629 } else if (trigger_name_taken(state, trigger)) {
2630 /* Not a fatal error. */
2631 *cmd_result = LTTNG_ERR_TRIGGER_EXISTS;
2632 ret = 0;
2633 goto error;
2634 }
2635
ab0ee2ca 2636 condition = lttng_trigger_get_condition(trigger);
1da26331
JG
2637 assert(condition);
2638
959e3c66
JR
2639 /* Some conditions require tracers to implement a minimal ABI version. */
2640 if (!condition_is_supported(condition)) {
1da26331
JG
2641 *cmd_result = LTTNG_ERR_NOT_SUPPORTED;
2642 goto error;
1da26331
JG
2643 }
2644
ab0ee2ca
JG
2645 trigger_ht_element = zmalloc(sizeof(*trigger_ht_element));
2646 if (!trigger_ht_element) {
2647 ret = -1;
2648 goto error;
2649 }
2650
2651 /* Add trigger to the trigger_ht. */
2652 cds_lfht_node_init(&trigger_ht_element->node);
242388e4 2653 cds_lfht_node_init(&trigger_ht_element->node_by_name_uid);
ab0ee2ca
JG
2654 trigger_ht_element->trigger = trigger;
2655
2656 node = cds_lfht_add_unique(state->triggers_ht,
2657 lttng_condition_hash(condition),
242388e4
JR
2658 match_trigger,
2659 trigger,
ab0ee2ca
JG
2660 &trigger_ht_element->node);
2661 if (node != &trigger_ht_element->node) {
2662 /* Not a fatal error, simply report it to the client. */
2663 *cmd_result = LTTNG_ERR_TRIGGER_EXISTS;
2664 goto error_free_ht_element;
2665 }
2666
242388e4
JR
2667 node = cds_lfht_add_unique(state->triggers_by_name_uid_ht,
2668 hash_trigger_by_name_uid(trigger),
2669 match_trigger_by_name_uid,
2670 trigger,
2671 &trigger_ht_element->node_by_name_uid);
2672 if (node != &trigger_ht_element->node_by_name_uid) {
2673 /* Not a fatal error, simply report it to the client. */
2674 cds_lfht_del(state->triggers_ht, &trigger_ht_element->node);
2675 *cmd_result = LTTNG_ERR_TRIGGER_EXISTS;
2676 goto error_free_ht_element;
2677 }
2678
d602bd6a 2679 if (lttng_condition_get_type(condition) == LTTNG_CONDITION_TYPE_ON_EVENT) {
e7c93cf9
JR
2680 trigger_tokens_ht_element = zmalloc(sizeof(*trigger_tokens_ht_element));
2681 if (!trigger_tokens_ht_element) {
2682 /* Fatal error. */
2683 ret = -1;
2758e38b
FD
2684 notif_thread_state_remove_trigger_ht_elem(state,
2685 trigger_ht_element);
e7c93cf9
JR
2686 goto error_free_ht_element;
2687 }
2688
2689 /* Add trigger token to the trigger_tokens_ht. */
2690 cds_lfht_node_init(&trigger_tokens_ht_element->node);
2691 trigger_tokens_ht_element->token =
2692 LTTNG_OPTIONAL_GET(trigger->tracer_token);
2693 trigger_tokens_ht_element->trigger = trigger;
2694
2695 node = cds_lfht_add_unique(state->trigger_tokens_ht,
2696 hash_key_u64(&trigger_tokens_ht_element->token,
2697 lttng_ht_seed),
2698 match_trigger_token,
2699 &trigger_tokens_ht_element->token,
2700 &trigger_tokens_ht_element->node);
2701 if (node != &trigger_tokens_ht_element->node) {
2702 /* Internal corruption, fatal error. */
2703 ret = -1;
2704 *cmd_result = LTTNG_ERR_TRIGGER_EXISTS;
2758e38b
FD
2705 notif_thread_state_remove_trigger_ht_elem(state,
2706 trigger_ht_element);
e7c93cf9
JR
2707 goto error_free_ht_element;
2708 }
90aa04a1
FD
2709
2710 if (trigger_needs_tracer_notifier(trigger)) {
2711 uint64_t error_counter_index = 0;
2712 enum event_notifier_error_accounting_status error_accounting_status;
2713
2714 error_accounting_status = event_notifier_error_accounting_register_event_notifier(
2715 trigger, &error_counter_index);
2716 if (error_accounting_status != EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK) {
2717 if (error_accounting_status == EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NO_INDEX_AVAILABLE) {
2718 DBG("Event notifier group error accounting map is full");
2719 *cmd_result = LTTNG_ERR_EVENT_NOTIFIER_ERROR_ACCOUNTING_FULL;
2720 } else {
2721 ERR("Failed to register event notifier for error accounting");
2722 *cmd_result = LTTNG_ERR_EVENT_NOTIFIER_REGISTRATION;
2723 }
2724
2758e38b
FD
2725 cds_lfht_del(state->trigger_tokens_ht,
2726 &trigger_tokens_ht_element->node);
2727 notif_thread_state_remove_trigger_ht_elem(state,
2728 trigger_ht_element);
90aa04a1
FD
2729 goto error_free_ht_element;
2730 }
2731
2732 lttng_condition_on_event_set_error_counter_index(
2733 condition, error_counter_index);
2734 }
2735
e7c93cf9
JR
2736 }
2737
ab0ee2ca
JG
2738 /*
2739 * Ownership of the trigger and of its wrapper was transfered to
e7c93cf9 2740 * the triggers_ht. Same for token ht element if necessary.
ab0ee2ca 2741 */
e7c93cf9 2742 trigger_tokens_ht_element = NULL;
ab0ee2ca
JG
2743 trigger_ht_element = NULL;
2744 free_trigger = false;
2745
2746 /*
2747 * The rest only applies to triggers that have a "notify" action.
2748 * It is not skipped as this is the only action type currently
2749 * supported.
2750 */
f2b3ef9f
JG
2751 if (is_trigger_action_notify(trigger)) {
2752 client_list = notification_client_list_create(trigger);
2753 if (!client_list) {
2754 ret = -1;
2755 goto error_free_ht_element;
ab0ee2ca
JG
2756 }
2757
f2b3ef9f
JG
2758 /* Build a list of clients to which this new trigger applies. */
2759 cds_lfht_for_each_entry (state->client_socket_ht, &iter, client,
2760 client_socket_ht_node) {
2761 if (!trigger_applies_to_client(trigger, client)) {
2762 continue;
2763 }
2764
2765 client_list_element =
2766 zmalloc(sizeof(*client_list_element));
2767 if (!client_list_element) {
2768 ret = -1;
2769 goto error_put_client_list;
2770 }
2771
2772 CDS_INIT_LIST_HEAD(&client_list_element->node);
2773 client_list_element->client = client;
2774 cds_list_add(&client_list_element->node,
2775 &client_list->list);
ab0ee2ca 2776 }
f2b3ef9f
JG
2777
2778 /*
2779 * Client list ownership transferred to the
2780 * notification_trigger_clients_ht.
2781 */
2782 publish_notification_client_list(state, client_list);
ab0ee2ca
JG
2783 }
2784
f82f93a1 2785 switch (get_condition_binding_object(condition)) {
51eab943
JG
2786 case LTTNG_OBJECT_TYPE_SESSION:
2787 /* Add the trigger to the list if it matches a known session. */
2788 ret = bind_trigger_to_matching_session(trigger, state);
2789 if (ret) {
505b2d90 2790 goto error_put_client_list;
ab0ee2ca 2791 }
f82f93a1 2792 break;
51eab943
JG
2793 case LTTNG_OBJECT_TYPE_CHANNEL:
2794 /*
2795 * Add the trigger to list of triggers bound to the channels
2796 * currently known.
2797 */
2798 ret = bind_trigger_to_matching_channels(trigger, state);
2799 if (ret) {
505b2d90 2800 goto error_put_client_list;
ab0ee2ca 2801 }
51eab943
JG
2802 break;
2803 case LTTNG_OBJECT_TYPE_NONE:
2804 break;
2805 default:
f2b3ef9f 2806 ERR("Unknown object type on which to bind a newly registered trigger was encountered");
51eab943 2807 ret = -1;
505b2d90 2808 goto error_put_client_list;
ab0ee2ca
JG
2809 }
2810
2ae99f0b 2811 /*
f2b3ef9f
JG
2812 * The new trigger's condition must be evaluated against the current
2813 * state.
2814 *
2815 * In the case of `notify` action, nothing preventing clients from
2816 * subscribing to a condition before the corresponding trigger is
2817 * registered, we have to evaluate this new condition right away.
2ae99f0b
JG
2818 *
2819 * At some point, we were waiting for the next "evaluation" (e.g. on
2820 * reception of a channel sample) to evaluate this new condition, but
2821 * that was broken.
2822 *
2823 * The reason it was broken is that waiting for the next sample
2824 * does not allow us to properly handle transitions for edge-triggered
2825 * conditions.
2826 *
2827 * Consider this example: when we handle a new channel sample, we
2828 * evaluate each conditions twice: once with the previous state, and
2829 * again with the newest state. We then use those two results to
2830 * determine whether a state change happened: a condition was false and
2831 * became true. If a state change happened, we have to notify clients.
2832 *
2833 * Now, if a client subscribes to a given notification and registers
2834 * a trigger *after* that subscription, we have to make sure the
2835 * condition is evaluated at this point while considering only the
2836 * current state. Otherwise, the next evaluation cycle may only see
2837 * that the evaluations remain the same (true for samples n-1 and n) and
2838 * the client will never know that the condition has been met.
2839 */
f2b3ef9f
JG
2840 switch (get_condition_binding_object(condition)) {
2841 case LTTNG_OBJECT_TYPE_SESSION:
2842 ret = evaluate_session_condition_for_client(condition, state,
ff588497
JR
2843 &evaluation, &object_uid,
2844 &object_gid);
bc8daafb
JG
2845 LTTNG_OPTIONAL_SET(&object_creds.uid, object_uid);
2846 LTTNG_OPTIONAL_SET(&object_creds.gid, object_gid);
b42ada90 2847 break;
f2b3ef9f
JG
2848 case LTTNG_OBJECT_TYPE_CHANNEL:
2849 ret = evaluate_channel_condition_for_client(condition, state,
ff588497
JR
2850 &evaluation, &object_uid,
2851 &object_gid);
bc8daafb
JG
2852 LTTNG_OPTIONAL_SET(&object_creds.uid, object_uid);
2853 LTTNG_OPTIONAL_SET(&object_creds.gid, object_gid);
f2b3ef9f
JG
2854 break;
2855 case LTTNG_OBJECT_TYPE_NONE:
2856 ret = 0;
242388e4 2857 break;
f2b3ef9f
JG
2858 case LTTNG_OBJECT_TYPE_UNKNOWN:
2859 default:
2860 ret = -1;
242388e4 2861 break;
f2b3ef9f
JG
2862 }
2863
2864 if (ret) {
2865 /* Fatal error. */
2866 goto error_put_client_list;
2867 }
2868
2869 DBG("Newly registered trigger's condition evaluated to %s",
2870 evaluation ? "true" : "false");
2871 if (!evaluation) {
2872 /* Evaluation yielded nothing. Normal exit. */
2873 ret = 0;
242388e4 2874 goto end;
2ae99f0b
JG
2875 }
2876
2877 /*
f2b3ef9f
JG
2878 * Ownership of `evaluation` transferred to the action executor
2879 * no matter the result.
2ae99f0b 2880 */
f2b3ef9f
JG
2881 executor_status = action_executor_enqueue(state->executor, trigger,
2882 evaluation, &object_creds, client_list);
2883 evaluation = NULL;
2884 switch (executor_status) {
2885 case ACTION_EXECUTOR_STATUS_OK:
2886 break;
2887 case ACTION_EXECUTOR_STATUS_ERROR:
2888 case ACTION_EXECUTOR_STATUS_INVALID:
2889 /*
2890 * TODO Add trigger identification (name/id) when
2891 * it is added to the API.
2892 */
2893 ERR("Fatal error occurred while enqueuing action associated to newly registered trigger");
2894 ret = -1;
2895 goto error_put_client_list;
2896 case ACTION_EXECUTOR_STATUS_OVERFLOW:
2897 /*
2898 * TODO Add trigger identification (name/id) when
2899 * it is added to the API.
2900 *
2901 * Not a fatal error.
2902 */
2903 WARN("No space left when enqueuing action associated to newly registered trigger");
2904 ret = 0;
242388e4 2905 goto end;
f2b3ef9f
JG
2906 default:
2907 abort();
2908 }
2ae99f0b 2909
242388e4 2910end:
ab0ee2ca 2911 *cmd_result = LTTNG_OK;
e6887944
JR
2912 DBG("Registered trigger: name = `%s`, tracer token = %" PRIu64,
2913 trigger_name, trigger_tracer_token);
505b2d90
JG
2914
2915error_put_client_list:
2916 notification_client_list_put(client_list);
2917
ab0ee2ca 2918error_free_ht_element:
242388e4
JR
2919 if (trigger_ht_element) {
2920 /* Delayed removal due to RCU constraint on delete. */
2921 call_rcu(&trigger_ht_element->rcu_node,
2922 free_lttng_trigger_ht_element_rcu);
2923 }
2924
e7c93cf9 2925 free(trigger_tokens_ht_element);
ab0ee2ca
JG
2926error:
2927 if (free_trigger) {
ab0ee2ca
JG
2928 lttng_trigger_destroy(trigger);
2929 }
2930 rcu_read_unlock();
2931 return ret;
2932}
2933
83b934ad
MD
2934static
2935void free_lttng_trigger_ht_element_rcu(struct rcu_head *node)
2936{
2937 free(caa_container_of(node, struct lttng_trigger_ht_element,
2938 rcu_node));
2939}
2940
e7c93cf9
JR
2941static
2942void free_notification_trigger_tokens_ht_element_rcu(struct rcu_head *node)
2943{
2944 free(caa_container_of(node, struct notification_trigger_tokens_ht_element,
2945 rcu_node));
2946}
2947
cc2295b5 2948static
ab0ee2ca
JG
2949int handle_notification_thread_command_unregister_trigger(
2950 struct notification_thread_state *state,
ac16173e 2951 const struct lttng_trigger *trigger,
ab0ee2ca
JG
2952 enum lttng_error_code *_cmd_reply)
2953{
2954 struct cds_lfht_iter iter;
ed327204 2955 struct cds_lfht_node *triggers_ht_node;
ab0ee2ca
JG
2956 struct lttng_channel_trigger_list *trigger_list;
2957 struct notification_client_list *client_list;
ab0ee2ca 2958 struct lttng_trigger_ht_element *trigger_ht_element = NULL;
ac16173e 2959 const struct lttng_condition *condition = lttng_trigger_get_const_condition(
ab0ee2ca 2960 trigger);
ab0ee2ca
JG
2961 enum lttng_error_code cmd_reply;
2962
2963 rcu_read_lock();
2964
2965 cds_lfht_lookup(state->triggers_ht,
2966 lttng_condition_hash(condition),
242388e4
JR
2967 match_trigger,
2968 trigger,
ab0ee2ca
JG
2969 &iter);
2970 triggers_ht_node = cds_lfht_iter_get_node(&iter);
2971 if (!triggers_ht_node) {
2972 cmd_reply = LTTNG_ERR_TRIGGER_NOT_FOUND;
2973 goto end;
2974 } else {
2975 cmd_reply = LTTNG_OK;
2976 }
2977
2978 /* Remove trigger from channel_triggers_ht. */
2979 cds_lfht_for_each_entry(state->channel_triggers_ht, &iter, trigger_list,
2980 channel_triggers_ht_node) {
2981 struct lttng_trigger_list_element *trigger_element, *tmp;
2982
2983 cds_list_for_each_entry_safe(trigger_element, tmp,
2984 &trigger_list->list, node) {
e8f7fa11 2985 if (!lttng_trigger_is_equal(trigger, trigger_element->trigger)) {
ab0ee2ca
JG
2986 continue;
2987 }
2988
2989 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
2990 cds_list_del(&trigger_element->node);
e4db5ace
JR
2991 /* A trigger can only appear once per channel */
2992 break;
ab0ee2ca
JG
2993 }
2994 }
2995
e7c93cf9 2996 if (lttng_condition_get_type(condition) ==
d602bd6a 2997 LTTNG_CONDITION_TYPE_ON_EVENT) {
e7c93cf9
JR
2998 struct notification_trigger_tokens_ht_element
2999 *trigger_tokens_ht_element;
3000
3001 cds_lfht_for_each_entry (state->trigger_tokens_ht, &iter,
3002 trigger_tokens_ht_element, node) {
3003 if (!lttng_trigger_is_equal(trigger,
3004 trigger_tokens_ht_element->trigger)) {
3005 continue;
3006 }
3007
90aa04a1
FD
3008 if (trigger_needs_tracer_notifier(trigger_tokens_ht_element->trigger)) {
3009 event_notifier_error_accounting_unregister_event_notifier(
3010 trigger_tokens_ht_element->trigger);
3011 }
3012
e7c93cf9
JR
3013 DBG("[notification-thread] Removed trigger from tokens_ht");
3014 cds_lfht_del(state->trigger_tokens_ht,
3015 &trigger_tokens_ht_element->node);
3016 call_rcu(&trigger_tokens_ht_element->rcu_node,
3017 free_notification_trigger_tokens_ht_element_rcu);
3018
3019 break;
3020 }
3021 }
3022
51367634
JR
3023 if (is_trigger_action_notify(trigger)) {
3024 /*
3025 * Remove and release the client list from
3026 * notification_trigger_clients_ht.
3027 */
3028 client_list = get_client_list_from_condition(state, condition);
3029 assert(client_list);
ed327204 3030
51367634
JR
3031 /* Put new reference and the hashtable's reference. */
3032 notification_client_list_put(client_list);
3033 notification_client_list_put(client_list);
3034 client_list = NULL;
3035 }
ab0ee2ca 3036
ab0ee2ca
JG
3037 trigger_ht_element = caa_container_of(triggers_ht_node,
3038 struct lttng_trigger_ht_element, node);
2758e38b
FD
3039
3040 /* Remove trigger from triggers_ht. */
3041 notif_thread_state_remove_trigger_ht_elem(state, trigger_ht_element);
ab0ee2ca 3042
7ca172c1 3043 /* Release the ownership of the trigger. */
ab0ee2ca 3044 lttng_trigger_destroy(trigger_ht_element->trigger);
83b934ad 3045 call_rcu(&trigger_ht_element->rcu_node, free_lttng_trigger_ht_element_rcu);
ab0ee2ca
JG
3046end:
3047 rcu_read_unlock();
3048 if (_cmd_reply) {
3049 *_cmd_reply = cmd_reply;
3050 }
3051 return 0;
3052}
3053
3054/* Returns 0 on success, 1 on exit requested, negative value on error. */
3055int handle_notification_thread_command(
3056 struct notification_thread_handle *handle,
3057 struct notification_thread_state *state)
3058{
3059 int ret;
3060 uint64_t counter;
3061 struct notification_thread_command *cmd;
3062
8abe313a 3063 /* Read the event pipe to put it back into a quiescent state. */
18aeca05 3064 ret = lttng_read(lttng_pipe_get_readfd(handle->cmd_queue.event_pipe), &counter,
8abe313a 3065 sizeof(counter));
18aeca05 3066 if (ret != sizeof(counter)) {
ab0ee2ca
JG
3067 goto error;
3068 }
3069
3070 pthread_mutex_lock(&handle->cmd_queue.lock);
3071 cmd = cds_list_first_entry(&handle->cmd_queue.list,
3072 struct notification_thread_command, cmd_list_node);
97a71ea6
JR
3073 cds_list_del(&cmd->cmd_list_node);
3074 pthread_mutex_unlock(&handle->cmd_queue.lock);
6900ae81
FD
3075
3076 DBG("[notification-thread] Received `%s` command",
3077 notification_command_type_str(cmd->type));
ab0ee2ca
JG
3078 switch (cmd->type) {
3079 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER:
ac16173e
JG
3080 ret = handle_notification_thread_command_register_trigger(state,
3081 cmd->parameters.register_trigger.trigger,
ab0ee2ca
JG
3082 &cmd->reply_code);
3083 break;
3084 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER:
ab0ee2ca 3085 ret = handle_notification_thread_command_unregister_trigger(
ac16173e
JG
3086 state,
3087 cmd->parameters.unregister_trigger.trigger,
ab0ee2ca
JG
3088 &cmd->reply_code);
3089 break;
3090 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL:
ab0ee2ca 3091 ret = handle_notification_thread_command_add_channel(
8abe313a
JG
3092 state,
3093 cmd->parameters.add_channel.session.name,
3094 cmd->parameters.add_channel.session.uid,
3095 cmd->parameters.add_channel.session.gid,
3096 cmd->parameters.add_channel.channel.name,
3097 cmd->parameters.add_channel.channel.domain,
3098 cmd->parameters.add_channel.channel.key,
3099 cmd->parameters.add_channel.channel.capacity,
ab0ee2ca
JG
3100 &cmd->reply_code);
3101 break;
3102 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL:
ab0ee2ca
JG
3103 ret = handle_notification_thread_command_remove_channel(
3104 state, cmd->parameters.remove_channel.key,
3105 cmd->parameters.remove_channel.domain,
3106 &cmd->reply_code);
3107 break;
0ca52944 3108 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING:
0ca52944 3109 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED:
ed327204 3110 ret = handle_notification_thread_command_session_rotation(
0ca52944 3111 state,
ed327204
JG
3112 cmd->type,
3113 cmd->parameters.session_rotation.session_name,
3114 cmd->parameters.session_rotation.uid,
3115 cmd->parameters.session_rotation.gid,
3116 cmd->parameters.session_rotation.trace_archive_chunk_id,
3117 cmd->parameters.session_rotation.location,
0ca52944
JG
3118 &cmd->reply_code);
3119 break;
d02d7404
JR
3120 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE:
3121 ret = handle_notification_thread_command_add_tracer_event_source(
3122 state,
3123 cmd->parameters.tracer_event_source.tracer_event_source_fd,
3124 cmd->parameters.tracer_event_source.domain,
3125 &cmd->reply_code);
3126 break;
3127 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE:
3128 ret = handle_notification_thread_command_remove_tracer_event_source(
3129 state,
3130 cmd->parameters.tracer_event_source.tracer_event_source_fd,
3131 &cmd->reply_code);
3132 break;
fbc9f37d
JR
3133 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS:
3134 {
3135 struct lttng_triggers *triggers = NULL;
3136
3137 ret = handle_notification_thread_command_list_triggers(
3138 handle,
3139 state,
3140 cmd->parameters.list_triggers.uid,
3141 &triggers,
3142 &cmd->reply_code);
3143 cmd->reply.list_triggers.triggers = triggers;
3144 ret = 0;
3145 break;
3146 }
ab0ee2ca 3147 case NOTIFICATION_COMMAND_TYPE_QUIT:
ab0ee2ca
JG
3148 cmd->reply_code = LTTNG_OK;
3149 ret = 1;
3150 goto end;
f2b3ef9f
JG
3151 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE:
3152 {
3153 const enum client_transmission_status client_status =
3154 cmd->parameters.client_communication_update
3155 .status;
3156 const notification_client_id client_id =
3157 cmd->parameters.client_communication_update.id;
3158 struct notification_client *client;
3159
3160 rcu_read_lock();
3161 client = get_client_from_id(client_id, state);
3162
3163 if (!client) {
3164 /*
3165 * Client error was probably already picked-up by the
3166 * notification thread or it has disconnected
3167 * gracefully while this command was queued.
3168 */
3169 DBG("Failed to find notification client to update communication status, client id = %" PRIu64,
3170 client_id);
3171 ret = 0;
3172 } else {
f2b3ef9f
JG
3173 ret = client_handle_transmission_status(
3174 client, client_status, state);
f2b3ef9f
JG
3175 }
3176 rcu_read_unlock();
3177 break;
3178 }
ab0ee2ca
JG
3179 default:
3180 ERR("[notification-thread] Unknown internal command received");
3181 goto error_unlock;
3182 }
3183
3184 if (ret) {
3185 goto error_unlock;
3186 }
3187end:
0ab399e0
JG
3188 if (cmd->is_async) {
3189 free(cmd);
3190 cmd = NULL;
3191 } else {
3192 lttng_waiter_wake_up(&cmd->reply_waiter);
3193 }
ab0ee2ca
JG
3194 return ret;
3195error_unlock:
3196 /* Wake-up and return a fatal error to the calling thread. */
8ada111f 3197 lttng_waiter_wake_up(&cmd->reply_waiter);
ab0ee2ca
JG
3198 cmd->reply_code = LTTNG_ERR_FATAL;
3199error:
3200 /* Indicate a fatal error to the caller. */
3201 return -1;
3202}
3203
ab0ee2ca
JG
3204static
3205int socket_set_non_blocking(int socket)
3206{
3207 int ret, flags;
3208
3209 /* Set the pipe as non-blocking. */
3210 ret = fcntl(socket, F_GETFL, 0);
3211 if (ret == -1) {
3212 PERROR("fcntl get socket flags");
3213 goto end;
3214 }
3215 flags = ret;
3216
3217 ret = fcntl(socket, F_SETFL, flags | O_NONBLOCK);
3218 if (ret == -1) {
3219 PERROR("fcntl set O_NONBLOCK socket flag");
3220 goto end;
3221 }
3222 DBG("Client socket (fd = %i) set as non-blocking", socket);
3223end:
3224 return ret;
3225}
3226
3227static
14fa22f8 3228int client_reset_inbound_state(struct notification_client *client)
ab0ee2ca
JG
3229{
3230 int ret;
3231
882093ee
JR
3232
3233 lttng_payload_clear(&client->communication.inbound.payload);
ab0ee2ca
JG
3234
3235 client->communication.inbound.bytes_to_receive =
3236 sizeof(struct lttng_notification_channel_message);
3237 client->communication.inbound.msg_type =
3238 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN;
ab0ee2ca
JG
3239 LTTNG_SOCK_SET_UID_CRED(&client->communication.inbound.creds, -1);
3240 LTTNG_SOCK_SET_GID_CRED(&client->communication.inbound.creds, -1);
14fa22f8 3241 ret = lttng_dynamic_buffer_set_size(
882093ee 3242 &client->communication.inbound.payload.buffer,
14fa22f8 3243 client->communication.inbound.bytes_to_receive);
882093ee 3244
14fa22f8 3245 return ret;
ab0ee2ca
JG
3246}
3247
3248int handle_notification_thread_client_connect(
3249 struct notification_thread_state *state)
3250{
3251 int ret;
3252 struct notification_client *client;
3253
3254 DBG("[notification-thread] Handling new notification channel client connection");
3255
3256 client = zmalloc(sizeof(*client));
3257 if (!client) {
3258 /* Fatal error. */
3259 ret = -1;
3260 goto error;
3261 }
6c24d3fd 3262
505b2d90 3263 pthread_mutex_init(&client->lock, NULL);
ac1889bf 3264 client->id = state->next_notification_client_id++;
ab0ee2ca 3265 CDS_INIT_LIST_HEAD(&client->condition_list);
882093ee
JR
3266 lttng_payload_init(&client->communication.inbound.payload);
3267 lttng_payload_init(&client->communication.outbound.payload);
01ea340e 3268 client->communication.inbound.expect_creds = true;
505b2d90 3269
14fa22f8
JG
3270 ret = client_reset_inbound_state(client);
3271 if (ret) {
3272 ERR("[notification-thread] Failed to reset client communication's inbound state");
e742b055 3273 ret = 0;
14fa22f8
JG
3274 goto error;
3275 }
ab0ee2ca
JG
3276
3277 ret = lttcomm_accept_unix_sock(state->notification_channel_socket);
3278 if (ret < 0) {
3279 ERR("[notification-thread] Failed to accept new notification channel client connection");
3280 ret = 0;
3281 goto error;
3282 }
3283
3284 client->socket = ret;
3285
3286 ret = socket_set_non_blocking(client->socket);
3287 if (ret) {
3288 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
3289 goto error;
3290 }
3291
3292 ret = lttcomm_setsockopt_creds_unix_sock(client->socket);
3293 if (ret < 0) {
3294 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
3295 ret = 0;
3296 goto error;
3297 }
3298
3299 ret = lttng_poll_add(&state->events, client->socket,
3300 LPOLLIN | LPOLLERR |
3301 LPOLLHUP | LPOLLRDHUP);
3302 if (ret < 0) {
3303 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
3304 ret = 0;
3305 goto error;
3306 }
3307 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
3308 client->socket);
3309
ab0ee2ca
JG
3310 rcu_read_lock();
3311 cds_lfht_add(state->client_socket_ht,
3312 hash_client_socket(client->socket),
3313 &client->client_socket_ht_node);
ac1889bf
JG
3314 cds_lfht_add(state->client_id_ht,
3315 hash_client_id(client->id),
3316 &client->client_id_ht_node);
ab0ee2ca
JG
3317 rcu_read_unlock();
3318
3319 return ret;
6c24d3fd 3320
ab0ee2ca
JG
3321error:
3322 notification_client_destroy(client, state);
3323 return ret;
3324}
3325
6c24d3fd
JG
3326/*
3327 * RCU read-lock must be held by the caller.
3328 * Client lock must _not_ be held by the caller.
3329 */
505b2d90
JG
3330static
3331int notification_thread_client_disconnect(
3332 struct notification_client *client,
ab0ee2ca 3333 struct notification_thread_state *state)
505b2d90
JG
3334{
3335 int ret;
3336 struct lttng_condition_list_element *condition_list_element, *tmp;
3337
3338 /* Acquire the client lock to disable its communication atomically. */
6c24d3fd 3339 pthread_mutex_lock(&client->lock);
505b2d90 3340 client->communication.active = false;
6c24d3fd
JG
3341 cds_lfht_del(state->client_socket_ht, &client->client_socket_ht_node);
3342 cds_lfht_del(state->client_id_ht, &client->client_id_ht_node);
3343 pthread_mutex_unlock(&client->lock);
3344
505b2d90
JG
3345 ret = lttng_poll_del(&state->events, client->socket);
3346 if (ret) {
3347 ERR("[notification-thread] Failed to remove client socket %d from poll set",
3348 client->socket);
3349 }
3350
505b2d90
JG
3351 /* Release all conditions to which the client was subscribed. */
3352 cds_list_for_each_entry_safe(condition_list_element, tmp,
3353 &client->condition_list, node) {
3354 (void) notification_thread_client_unsubscribe(client,
3355 condition_list_element->condition, state, NULL);
3356 }
3357
3358 /*
3359 * Client no longer accessible to other threads (through the
3360 * client lists).
3361 */
3362 notification_client_destroy(client, state);
3363 return ret;
3364}
3365
3366int handle_notification_thread_client_disconnect(
3367 int client_socket, struct notification_thread_state *state)
ab0ee2ca
JG
3368{
3369 int ret = 0;
3370 struct notification_client *client;
3371
3372 rcu_read_lock();
3373 DBG("[notification-thread] Closing client connection (socket fd = %i)",
3374 client_socket);
3375 client = get_client_from_socket(client_socket, state);
3376 if (!client) {
3377 /* Internal state corruption, fatal error. */
3378 ERR("[notification-thread] Unable to find client (socket fd = %i)",
3379 client_socket);
3380 ret = -1;
3381 goto end;
3382 }
3383
505b2d90 3384 ret = notification_thread_client_disconnect(client, state);
ab0ee2ca
JG
3385end:
3386 rcu_read_unlock();
3387 return ret;
3388}
3389
3390int handle_notification_thread_client_disconnect_all(
3391 struct notification_thread_state *state)
3392{
3393 struct cds_lfht_iter iter;
3394 struct notification_client *client;
3395 bool error_encoutered = false;
3396
3397 rcu_read_lock();
3398 DBG("[notification-thread] Closing all client connections");
3399 cds_lfht_for_each_entry(state->client_socket_ht, &iter, client,
505b2d90 3400 client_socket_ht_node) {
ab0ee2ca
JG
3401 int ret;
3402
505b2d90
JG
3403 ret = notification_thread_client_disconnect(
3404 client, state);
ab0ee2ca
JG
3405 if (ret) {
3406 error_encoutered = true;
3407 }
3408 }
3409 rcu_read_unlock();
3410 return error_encoutered ? 1 : 0;
3411}
3412
3413int handle_notification_thread_trigger_unregister_all(
3414 struct notification_thread_state *state)
3415{
4149ace8 3416 bool error_occurred = false;
ab0ee2ca
JG
3417 struct cds_lfht_iter iter;
3418 struct lttng_trigger_ht_element *trigger_ht_element;
3419
763de384 3420 rcu_read_lock();
ab0ee2ca
JG
3421 cds_lfht_for_each_entry(state->triggers_ht, &iter, trigger_ht_element,
3422 node) {
3423 int ret = handle_notification_thread_command_unregister_trigger(
3424 state, trigger_ht_element->trigger, NULL);
3425 if (ret) {
4149ace8 3426 error_occurred = true;
ab0ee2ca
JG
3427 }
3428 }
763de384 3429 rcu_read_unlock();
4149ace8 3430 return error_occurred ? -1 : 0;
ab0ee2ca
JG
3431}
3432
dd877ea6
JG
3433static
3434int client_handle_transmission_status(
3435 struct notification_client *client,
3436 enum client_transmission_status transmission_status,
3437 struct notification_thread_state *state)
3438{
3439 int ret = 0;
3440
3441 switch (transmission_status) {
3442 case CLIENT_TRANSMISSION_STATUS_COMPLETE:
3443 ret = lttng_poll_mod(&state->events, client->socket,
3444 CLIENT_POLL_MASK_IN);
3445 if (ret) {
3446 goto end;
3447 }
3448
dd877ea6
JG
3449 break;
3450 case CLIENT_TRANSMISSION_STATUS_QUEUED:
3451 /*
3452 * We want to be notified whenever there is buffer space
3453 * available to send the rest of the payload.
3454 */
3455 ret = lttng_poll_mod(&state->events, client->socket,
3456 CLIENT_POLL_MASK_IN_OUT);
3457 if (ret) {
3458 goto end;
3459 }
3460 break;
3461 case CLIENT_TRANSMISSION_STATUS_FAIL:
3462 ret = notification_thread_client_disconnect(client, state);
3463 if (ret) {
3464 goto end;
3465 }
3466 break;
3467 case CLIENT_TRANSMISSION_STATUS_ERROR:
3468 ret = -1;
3469 goto end;
3470 default:
3471 abort();
3472 }
3473end:
3474 return ret;
3475}
3476
505b2d90 3477/* Client lock must be acquired by caller. */
ab0ee2ca 3478static
dd877ea6 3479enum client_transmission_status client_flush_outgoing_queue(
f2b3ef9f 3480 struct notification_client *client)
ab0ee2ca
JG
3481{
3482 ssize_t ret;
3483 size_t to_send_count;
dd877ea6 3484 enum client_transmission_status status;
882093ee
JR
3485 struct lttng_payload_view pv = lttng_payload_view_from_payload(
3486 &client->communication.outbound.payload, 0, -1);
3487 const int fds_to_send_count =
3488 lttng_payload_view_get_fd_handle_count(&pv);
ab0ee2ca 3489
505b2d90
JG
3490 ASSERT_LOCKED(client->lock);
3491
f2b3ef9f
JG
3492 if (!client->communication.active) {
3493 status = CLIENT_TRANSMISSION_STATUS_FAIL;
3494 goto end;
3495 }
3496
882093ee
JR
3497 if (pv.buffer.size == 0) {
3498 /*
3499 * If both data and fds are equal to zero, we are in an invalid
3500 * state.
3501 */
3502 assert(fds_to_send_count != 0);
3503 goto send_fds;
3504 }
3505
3506 /* Send data. */
3507 to_send_count = pv.buffer.size;
ab0ee2ca
JG
3508 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
3509 client->socket);
3510
3511 ret = lttcomm_send_unix_sock_non_block(client->socket,
882093ee 3512 pv.buffer.data,
ab0ee2ca 3513 to_send_count);
44c180ca 3514 if ((ret >= 0 && ret < to_send_count)) {
ab0ee2ca
JG
3515 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
3516 client->socket);
3517 to_send_count -= max(ret, 0);
3518
6f110534 3519 memmove(client->communication.outbound.payload.buffer.data,
882093ee
JR
3520 pv.buffer.data +
3521 pv.buffer.size - to_send_count,
ab0ee2ca
JG
3522 to_send_count);
3523 ret = lttng_dynamic_buffer_set_size(
882093ee 3524 &client->communication.outbound.payload.buffer,
ab0ee2ca
JG
3525 to_send_count);
3526 if (ret) {
3527 goto error;
3528 }
6c24d3fd 3529
dd877ea6 3530 status = CLIENT_TRANSMISSION_STATUS_QUEUED;
882093ee 3531 goto end;
ab0ee2ca 3532 } else if (ret < 0) {
6c24d3fd 3533 /* Generic error, disable the client's communication. */
dd877ea6 3534 ERR("[notification-thread] Failed to flush outgoing queue, disconnecting client (socket fd = %i)",
ab0ee2ca 3535 client->socket);
6c24d3fd 3536 client->communication.active = false;
dd877ea6 3537 status = CLIENT_TRANSMISSION_STATUS_FAIL;
882093ee 3538 goto end;
ab0ee2ca 3539 } else {
882093ee
JR
3540 /*
3541 * No error and flushed the queue completely.
3542 *
3543 * The payload buffer size is used later to
3544 * check if there is notifications queued. So albeit that the
3545 * direct caller knows that the transmission is complete, we
3546 * need to set the buffer size to zero.
3547 */
ab0ee2ca 3548 ret = lttng_dynamic_buffer_set_size(
882093ee 3549 &client->communication.outbound.payload.buffer, 0);
ab0ee2ca
JG
3550 if (ret) {
3551 goto error;
3552 }
882093ee 3553 }
6c24d3fd 3554
882093ee
JR
3555send_fds:
3556 /* No fds to send, transmission is complete. */
3557 if (fds_to_send_count == 0) {
dd877ea6 3558 status = CLIENT_TRANSMISSION_STATUS_COMPLETE;
882093ee 3559 goto end;
dd877ea6 3560 }
882093ee
JR
3561
3562 ret = lttcomm_send_payload_view_fds_unix_sock_non_block(
3563 client->socket, &pv);
3564 if (ret < 0) {
3565 /* Generic error, disable the client's communication. */
3566 ERR("[notification-thread] Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)",
3567 client->socket);
3568 client->communication.active = false;
3569 status = CLIENT_TRANSMISSION_STATUS_FAIL;
3570 goto end;
3571 } else if (ret == 0) {
3572 /* Nothing could be sent. */
3573 status = CLIENT_TRANSMISSION_STATUS_QUEUED;
3574 } else {
3575 /* Fd passing is an all or nothing kind of thing. */
3576 status = CLIENT_TRANSMISSION_STATUS_COMPLETE;
3577 /*
3578 * The payload _fd_array count is used later to
3579 * check if there is notifications queued. So although the
3580 * direct caller knows that the transmission is complete, we
3581 * need to clear the _fd_array for the queuing check.
3582 */
3583 lttng_dynamic_pointer_array_clear(
3584 &client->communication.outbound.payload
3585 ._fd_handles);
3586 }
3587
f2b3ef9f 3588end:
882093ee
JR
3589 if (status == CLIENT_TRANSMISSION_STATUS_COMPLETE) {
3590 client->communication.outbound.queued_command_reply = false;
3591 client->communication.outbound.dropped_notification = false;
3592 lttng_payload_clear(&client->communication.outbound.payload);
3593 }
3594
f2b3ef9f 3595 return status;
ab0ee2ca 3596error:
f2b3ef9f 3597 return CLIENT_TRANSMISSION_STATUS_ERROR;
ab0ee2ca
JG
3598}
3599
882093ee
JR
3600static
3601bool client_has_outbound_data_left(
3602 const struct notification_client *client)
3603{
3604 const struct lttng_payload_view pv = lttng_payload_view_from_payload(
3605 &client->communication.outbound.payload, 0, -1);
3606 const bool has_data = pv.buffer.size != 0;
3607 const bool has_fds = lttng_payload_view_get_fd_handle_count(&pv);
3608
3609 return has_data || has_fds;
3610}
3611
6c24d3fd 3612/* Client lock must _not_ be held by the caller. */
ab0ee2ca
JG
3613static
3614int client_send_command_reply(struct notification_client *client,
3615 struct notification_thread_state *state,
3616 enum lttng_notification_channel_status status)
3617{
3618 int ret;
3619 struct lttng_notification_channel_command_reply reply = {
3620 .status = (int8_t) status,
3621 };
3622 struct lttng_notification_channel_message msg = {
3623 .type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY,
3624 .size = sizeof(reply),
3625 };
3626 char buffer[sizeof(msg) + sizeof(reply)];
f2b3ef9f 3627 enum client_transmission_status transmission_status;
ab0ee2ca 3628
6c24d3fd
JG
3629 memcpy(buffer, &msg, sizeof(msg));
3630 memcpy(buffer + sizeof(msg), &reply, sizeof(reply));
3631 DBG("[notification-thread] Send command reply (%i)", (int) status);
505b2d90 3632
6c24d3fd 3633 pthread_mutex_lock(&client->lock);
ab0ee2ca
JG
3634 if (client->communication.outbound.queued_command_reply) {
3635 /* Protocol error. */
6c24d3fd 3636 goto error_unlock;
ab0ee2ca
JG
3637 }
3638
ab0ee2ca
JG
3639 /* Enqueue buffer to outgoing queue and flush it. */
3640 ret = lttng_dynamic_buffer_append(
882093ee 3641 &client->communication.outbound.payload.buffer,
ab0ee2ca
JG
3642 buffer, sizeof(buffer));
3643 if (ret) {
6c24d3fd 3644 goto error_unlock;
ab0ee2ca
JG
3645 }
3646
f2b3ef9f 3647 transmission_status = client_flush_outgoing_queue(client);
882093ee
JR
3648
3649 if (client_has_outbound_data_left(client)) {
6c24d3fd
JG
3650 /* Queue could not be emptied. */
3651 client->communication.outbound.queued_command_reply = true;
3652 }
3653
3654 pthread_mutex_unlock(&client->lock);
f2b3ef9f
JG
3655 ret = client_handle_transmission_status(
3656 client, transmission_status, state);
ab0ee2ca
JG
3657 if (ret) {
3658 goto error;
3659 }
3660
ab0ee2ca 3661 return 0;
6c24d3fd
JG
3662error_unlock:
3663 pthread_mutex_unlock(&client->lock);
ab0ee2ca
JG
3664error:
3665 return -1;
3666}
3667
505b2d90
JG
3668static
3669int client_handle_message_unknown(struct notification_client *client,
3670 struct notification_thread_state *state)
3671{
3672 int ret;
505b2d90
JG
3673 /*
3674 * Receiving message header. The function will be called again
3675 * once the rest of the message as been received and can be
3676 * interpreted.
3677 */
3678 const struct lttng_notification_channel_message *msg;
3679
882093ee 3680 assert(sizeof(*msg) == client->communication.inbound.payload.buffer.size);
505b2d90 3681 msg = (const struct lttng_notification_channel_message *)
882093ee 3682 client->communication.inbound.payload.buffer.data;
505b2d90
JG
3683
3684 if (msg->size == 0 ||
3685 msg->size > DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE) {
3686 ERR("[notification-thread] Invalid notification channel message: length = %u",
3687 msg->size);
3688 ret = -1;
3689 goto end;
3690 }
3691
3692 switch (msg->type) {
3693 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE:
3694 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE:
3695 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE:
3696 break;
3697 default:
3698 ret = -1;
3699 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
3700 goto end;
3701 }
3702
3703 client->communication.inbound.bytes_to_receive = msg->size;
882093ee 3704 client->communication.inbound.fds_to_receive = msg->fds;
505b2d90
JG
3705 client->communication.inbound.msg_type =
3706 (enum lttng_notification_channel_message_type) msg->type;
3707 ret = lttng_dynamic_buffer_set_size(
882093ee
JR
3708 &client->communication.inbound.payload.buffer, msg->size);
3709
3710 /* msg is not valid anymore due to lttng_dynamic_buffer_set_size. */
3711 msg = NULL;
505b2d90 3712end:
505b2d90
JG
3713 return ret;
3714}
3715
3716static
3717int client_handle_message_handshake(struct notification_client *client,
3718 struct notification_thread_state *state)
3719{
3720 int ret;
3721 struct lttng_notification_channel_command_handshake *handshake_client;
3722 const struct lttng_notification_channel_command_handshake handshake_reply = {
3723 .major = LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR,
3724 .minor = LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR,
3725 };
3726 const struct lttng_notification_channel_message msg_header = {
3727 .type = LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE,
3728 .size = sizeof(handshake_reply),
3729 };
3730 enum lttng_notification_channel_status status =
3731 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK;
3732 char send_buffer[sizeof(msg_header) + sizeof(handshake_reply)];
505b2d90
JG
3733
3734 memcpy(send_buffer, &msg_header, sizeof(msg_header));
3735 memcpy(send_buffer + sizeof(msg_header), &handshake_reply,
3736 sizeof(handshake_reply));
3737
3738 handshake_client =
3739 (struct lttng_notification_channel_command_handshake *)
882093ee 3740 client->communication.inbound.payload.buffer
505b2d90
JG
3741 .data;
3742 client->major = handshake_client->major;
3743 client->minor = handshake_client->minor;
3744 if (!client->communication.inbound.creds_received) {
3745 ERR("[notification-thread] No credentials received from client");
3746 ret = -1;
3747 goto end;
3748 }
3749
3750 client->uid = LTTNG_SOCK_GET_UID_CRED(
3751 &client->communication.inbound.creds);
3752 client->gid = LTTNG_SOCK_GET_GID_CRED(
3753 &client->communication.inbound.creds);
3754 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
3755 client->uid, client->gid, (int) client->major,
3756 (int) client->minor);
3757
3758 if (handshake_client->major !=
3759 LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR) {
3760 status = LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION;
3761 }
3762
6c24d3fd
JG
3763 pthread_mutex_lock(&client->lock);
3764 /* Outgoing queue will be flushed when the command reply is sent. */
505b2d90 3765 ret = lttng_dynamic_buffer_append(
882093ee 3766 &client->communication.outbound.payload.buffer, send_buffer,
505b2d90
JG
3767 sizeof(send_buffer));
3768 if (ret) {
3769 ERR("[notification-thread] Failed to send protocol version to notification channel client");
6c24d3fd 3770 goto end_unlock;
505b2d90
JG
3771 }
3772
3773 client->validated = true;
3774 client->communication.active = true;
6c24d3fd 3775 pthread_mutex_unlock(&client->lock);
505b2d90 3776
6c24d3fd
JG
3777 /* Set reception state to receive the next message header. */
3778 ret = client_reset_inbound_state(client);
505b2d90 3779 if (ret) {
6c24d3fd 3780 ERR("[notification-thread] Failed to reset client communication's inbound state");
505b2d90
JG
3781 goto end;
3782 }
3783
6c24d3fd 3784 /* Flushes the outgoing queue. */
505b2d90
JG
3785 ret = client_send_command_reply(client, state, status);
3786 if (ret) {
3787 ERR("[notification-thread] Failed to send reply to notification channel client");
3788 goto end;
3789 }
3790
6c24d3fd
JG
3791 goto end;
3792end_unlock:
505b2d90 3793 pthread_mutex_unlock(&client->lock);
6c24d3fd 3794end:
505b2d90
JG
3795 return ret;
3796}
3797
3798static
3799int client_handle_message_subscription(
3800 struct notification_client *client,
3801 enum lttng_notification_channel_message_type msg_type,
3802 struct notification_thread_state *state)
3803{
3804 int ret;
3805 struct lttng_condition *condition;
3806 enum lttng_notification_channel_status status =
3807 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK;
3808 struct lttng_payload_view condition_view =
882093ee
JR
3809 lttng_payload_view_from_payload(
3810 &client->communication.inbound.payload,
505b2d90
JG
3811 0, -1);
3812 size_t expected_condition_size;
3813
6c24d3fd
JG
3814 /*
3815 * No need to lock client to sample the inbound state as the only
3816 * other thread accessing clients (action executor) only uses the
3817 * outbound state.
3818 */
882093ee 3819 expected_condition_size = client->communication.inbound.payload.buffer.size;
505b2d90
JG
3820 ret = lttng_condition_create_from_payload(&condition_view, &condition);
3821 if (ret != expected_condition_size) {
3822 ERR("[notification-thread] Malformed condition received from client");
3823 goto end;
3824 }
3825
3826 if (msg_type == LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE) {
3827 ret = notification_thread_client_subscribe(
3828 client, condition, state, &status);
3829 } else {
3830 ret = notification_thread_client_unsubscribe(
3831 client, condition, state, &status);
3832 }
505b2d90 3833
505b2d90 3834 if (ret) {
6c24d3fd 3835 goto end;
505b2d90
JG
3836 }
3837
3838 /* Set reception state to receive the next message header. */
3839 ret = client_reset_inbound_state(client);
3840 if (ret) {
3841 ERR("[notification-thread] Failed to reset client communication's inbound state");
6c24d3fd
JG
3842 goto end;
3843 }
3844
3845 ret = client_send_command_reply(client, state, status);
3846 if (ret) {
3847 ERR("[notification-thread] Failed to send reply to notification channel client");
3848 goto end;
505b2d90
JG
3849 }
3850
505b2d90
JG
3851end:
3852 return ret;
3853}
3854
ab0ee2ca
JG
3855static
3856int client_dispatch_message(struct notification_client *client,
3857 struct notification_thread_state *state)
3858{
3859 int ret = 0;
3860
3861 if (client->communication.inbound.msg_type !=
3862 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE &&
3863 client->communication.inbound.msg_type !=
3864 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN &&
3865 !client->validated) {
3866 WARN("[notification-thread] client attempted a command before handshake");
3867 ret = -1;
3868 goto end;
3869 }
3870
3871 switch (client->communication.inbound.msg_type) {
3872 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN:
3873 {
505b2d90 3874 ret = client_handle_message_unknown(client, state);
ab0ee2ca
JG
3875 break;
3876 }
3877 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE:
3878 {
505b2d90 3879 ret = client_handle_message_handshake(client, state);
ab0ee2ca
JG
3880 break;
3881 }
3882 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE:
3883 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE:
3884 {
505b2d90
JG
3885 ret = client_handle_message_subscription(client,
3886 client->communication.inbound.msg_type, state);
ab0ee2ca
JG
3887 break;
3888 }
3889 default:
3890 abort();
3891 }
3892end:
3893 return ret;
3894}
3895
3896/* Incoming data from client. */
3897int handle_notification_thread_client_in(
3898 struct notification_thread_state *state, int socket)
3899{
14fa22f8 3900 int ret = 0;
ab0ee2ca
JG
3901 struct notification_client *client;
3902 ssize_t recv_ret;
3903 size_t offset;
3904
98b5ff34 3905 rcu_read_lock();
ab0ee2ca
JG
3906 client = get_client_from_socket(socket, state);
3907 if (!client) {
3908 /* Internal error, abort. */
3909 ret = -1;
3910 goto end;
3911 }
3912
882093ee 3913 offset = client->communication.inbound.payload.buffer.size -
14fa22f8 3914 client->communication.inbound.bytes_to_receive;
01ea340e 3915 if (client->communication.inbound.expect_creds) {
ab0ee2ca 3916 recv_ret = lttcomm_recv_creds_unix_sock(socket,
882093ee 3917 client->communication.inbound.payload.buffer.data + offset,
ab0ee2ca
JG
3918 client->communication.inbound.bytes_to_receive,
3919 &client->communication.inbound.creds);
3920 if (recv_ret > 0) {
01ea340e 3921 client->communication.inbound.expect_creds = false;
ab0ee2ca
JG
3922 client->communication.inbound.creds_received = true;
3923 }
3924 } else {
3925 recv_ret = lttcomm_recv_unix_sock_non_block(socket,
882093ee 3926 client->communication.inbound.payload.buffer.data + offset,
ab0ee2ca
JG
3927 client->communication.inbound.bytes_to_receive);
3928 }
505b2d90
JG
3929 if (recv_ret >= 0) {
3930 client->communication.inbound.bytes_to_receive -= recv_ret;
882093ee
JR
3931 } else {
3932 goto error_disconnect_client;
505b2d90 3933 }
6c24d3fd 3934
882093ee
JR
3935 if (client->communication.inbound.bytes_to_receive != 0) {
3936 /* Message incomplete wait for more data. */
3937 ret = 0;
3938 goto end;
ab0ee2ca
JG
3939 }
3940
882093ee
JR
3941 assert(client->communication.inbound.bytes_to_receive == 0);
3942
3943 /* Receive fds. */
3944 if (client->communication.inbound.fds_to_receive != 0) {
3945 ret = lttcomm_recv_payload_fds_unix_sock_non_block(
3946 client->socket,
3947 client->communication.inbound.fds_to_receive,
3948 &client->communication.inbound.payload);
3949 if (ret > 0) {
ab0ee2ca 3950 /*
882093ee
JR
3951 * Fds received. non blocking fds passing is all
3952 * or nothing.
ab0ee2ca 3953 */
882093ee
JR
3954 ssize_t expected_size;
3955
3956 expected_size = sizeof(int) *
3957 client->communication.inbound
3958 .fds_to_receive;
3959 assert(ret == expected_size);
3960 client->communication.inbound.fds_to_receive = 0;
3961 } else if (ret == 0) {
3962 /* Received nothing. */
3963 ret = 0;
3964 goto end;
3965 } else {
ab0ee2ca
JG
3966 goto error_disconnect_client;
3967 }
ab0ee2ca 3968 }
882093ee
JR
3969
3970 /* At this point the message is complete.*/
3971 assert(client->communication.inbound.bytes_to_receive == 0 &&
3972 client->communication.inbound.fds_to_receive == 0);
3973 ret = client_dispatch_message(client, state);
3974 if (ret) {
3975 /*
3976 * Only returns an error if this client must be
3977 * disconnected.
3978 */
3979 goto error_disconnect_client;
3980 }
3981
ab0ee2ca 3982end:
98b5ff34 3983 rcu_read_unlock();
ab0ee2ca 3984 return ret;
882093ee 3985
ab0ee2ca 3986error_disconnect_client:
505b2d90 3987 ret = notification_thread_client_disconnect(client, state);
98b5ff34 3988 goto end;
ab0ee2ca
JG
3989}
3990
3991/* Client ready to receive outgoing data. */
3992int handle_notification_thread_client_out(
3993 struct notification_thread_state *state, int socket)
3994{
3995 int ret;
3996 struct notification_client *client;
f2b3ef9f 3997 enum client_transmission_status transmission_status;
ab0ee2ca 3998
98b5ff34 3999 rcu_read_lock();
ab0ee2ca
JG
4000 client = get_client_from_socket(socket, state);
4001 if (!client) {
4002 /* Internal error, abort. */
4003 ret = -1;
4004 goto end;
4005 }
4006
505b2d90 4007 pthread_mutex_lock(&client->lock);
f2b3ef9f 4008 transmission_status = client_flush_outgoing_queue(client);
6c24d3fd
JG
4009 pthread_mutex_unlock(&client->lock);
4010
f2b3ef9f
JG
4011 ret = client_handle_transmission_status(
4012 client, transmission_status, state);
ab0ee2ca
JG
4013 if (ret) {
4014 goto end;
4015 }
4016end:
98b5ff34 4017 rcu_read_unlock();
ab0ee2ca
JG
4018 return ret;
4019}
4020
4021static
e8360425
JD
4022bool evaluate_buffer_usage_condition(const struct lttng_condition *condition,
4023 const struct channel_state_sample *sample,
4024 uint64_t buffer_capacity)
ab0ee2ca
JG
4025{
4026 bool result = false;
4027 uint64_t threshold;
4028 enum lttng_condition_type condition_type;
e8360425 4029 const struct lttng_condition_buffer_usage *use_condition = container_of(
ab0ee2ca
JG
4030 condition, struct lttng_condition_buffer_usage,
4031 parent);
4032
ab0ee2ca
JG
4033 if (use_condition->threshold_bytes.set) {
4034 threshold = use_condition->threshold_bytes.value;
4035 } else {
4036 /*
4037 * Threshold was expressed as a ratio.
4038 *
4039 * TODO the threshold (in bytes) of conditions expressed
4040 * as a ratio of total buffer size could be cached to
4041 * forego this double-multiplication or it could be performed
4042 * as fixed-point math.
4043 *
d49487dc 4044 * Note that caching should accommodates the case where the
ab0ee2ca
JG
4045 * condition applies to multiple channels (i.e. don't assume
4046 * that all channels matching my_chann* have the same size...)
4047 */
4048 threshold = (uint64_t) (use_condition->threshold_ratio.value *
4049 (double) buffer_capacity);
4050 }
4051
4052 condition_type = lttng_condition_get_type(condition);
4053 if (condition_type == LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW) {
4054 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64 ", highest usage = %" PRIu64,
4055 threshold, sample->highest_usage);
4056
4057 /*
4058 * The low condition should only be triggered once _all_ of the
4059 * streams in a channel have gone below the "low" threshold.
4060 */
4061 if (sample->highest_usage <= threshold) {
4062 result = true;
4063 }
4064 } else {
4065 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64 ", highest usage = %" PRIu64,
4066 threshold, sample->highest_usage);
4067
4068 /*
4069 * For high buffer usage scenarios, we want to trigger whenever
4070 * _any_ of the streams has reached the "high" threshold.
4071 */
4072 if (sample->highest_usage >= threshold) {
4073 result = true;
4074 }
4075 }
e8360425 4076
ab0ee2ca
JG
4077 return result;
4078}
4079
4080static
e8360425
JD
4081bool evaluate_session_consumed_size_condition(
4082 const struct lttng_condition *condition,
4083 uint64_t session_consumed_size)
4084{
4085 uint64_t threshold;
4086 const struct lttng_condition_session_consumed_size *size_condition =
4087 container_of(condition,
4088 struct lttng_condition_session_consumed_size,
4089 parent);
4090
4091 threshold = size_condition->consumed_threshold_bytes.value;
4092 DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64 ", current size = %" PRIu64,
4093 threshold, session_consumed_size);
4094 return session_consumed_size >= threshold;
4095}
4096
4097static
ed327204 4098int evaluate_buffer_condition(const struct lttng_condition *condition,
ab0ee2ca 4099 struct lttng_evaluation **evaluation,
e8360425
JD
4100 const struct notification_thread_state *state,
4101 const struct channel_state_sample *previous_sample,
4102 const struct channel_state_sample *latest_sample,
4103 uint64_t previous_session_consumed_total,
4104 uint64_t latest_session_consumed_total,
4105 struct channel_info *channel_info)
ab0ee2ca
JG
4106{
4107 int ret = 0;
4108 enum lttng_condition_type condition_type;
e8360425
JD
4109 const bool previous_sample_available = !!previous_sample;
4110 bool previous_sample_result = false;
ab0ee2ca
JG
4111 bool latest_sample_result;
4112
4113 condition_type = lttng_condition_get_type(condition);
ab0ee2ca 4114
e8360425
JD
4115 switch (condition_type) {
4116 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
4117 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
4118 if (caa_likely(previous_sample_available)) {
4119 previous_sample_result =
4120 evaluate_buffer_usage_condition(condition,
4121 previous_sample, channel_info->capacity);
4122 }
4123 latest_sample_result = evaluate_buffer_usage_condition(
4124 condition, latest_sample,
4125 channel_info->capacity);
4126 break;
4127 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
4128 if (caa_likely(previous_sample_available)) {
4129 previous_sample_result =
4130 evaluate_session_consumed_size_condition(
4131 condition,
4132 previous_session_consumed_total);
4133 }
4134 latest_sample_result =
4135 evaluate_session_consumed_size_condition(
4136 condition,
4137 latest_session_consumed_total);
4138 break;
4139 default:
4140 /* Unknown condition type; internal error. */
4141 abort();
4142 }
ab0ee2ca
JG
4143
4144 if (!latest_sample_result ||
4145 (previous_sample_result == latest_sample_result)) {
4146 /*
4147 * Only trigger on a condition evaluation transition.
4148 *
4149 * NOTE: This edge-triggered logic may not be appropriate for
4150 * future condition types.
4151 */
4152 goto end;
4153 }
4154
e8360425
JD
4155 if (!evaluation || !latest_sample_result) {
4156 goto end;
4157 }
4158
4159 switch (condition_type) {
4160 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
4161 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
ab0ee2ca
JG
4162 *evaluation = lttng_evaluation_buffer_usage_create(
4163 condition_type,
4164 latest_sample->highest_usage,
e8360425
JD
4165 channel_info->capacity);
4166 break;
4167 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
4168 *evaluation = lttng_evaluation_session_consumed_size_create(
e8360425
JD
4169 latest_session_consumed_total);
4170 break;
4171 default:
4172 abort();
4173 }
4174
4175 if (!*evaluation) {
4176 ret = -1;
4177 goto end;
ab0ee2ca
JG
4178 }
4179end:
4180 return ret;
4181}
4182
4183static
f2b3ef9f 4184int client_notification_overflow(struct notification_client *client)
ab0ee2ca 4185{
f2b3ef9f
JG
4186 int ret = 0;
4187 const struct lttng_notification_channel_message msg = {
ab0ee2ca 4188 .type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED,
ab0ee2ca
JG
4189 };
4190
505b2d90
JG
4191 ASSERT_LOCKED(client->lock);
4192
f2b3ef9f
JG
4193 DBG("Dropping notification addressed to client (socket fd = %i)",
4194 client->socket);
4195 if (client->communication.outbound.dropped_notification) {
4196 /*
4197 * The client already has a "notification dropped" message
4198 * in its outgoing queue. Nothing to do since all
4199 * of those messages are coalesced.
4200 */
4201 goto end;
4202 }
4203
4204 client->communication.outbound.dropped_notification = true;
ab0ee2ca 4205 ret = lttng_dynamic_buffer_append(
882093ee 4206 &client->communication.outbound.payload.buffer, &msg,
ab0ee2ca 4207 sizeof(msg));
f2b3ef9f
JG
4208 if (ret) {
4209 PERROR("Failed to enqueue \"dropped notification\" message in client's (socket fd = %i) outgoing queue",
4210 client->socket);
4211 }
4212end:
ab0ee2ca
JG
4213 return ret;
4214}
4215
f2b3ef9f
JG
4216static int client_handle_transmission_status_wrapper(
4217 struct notification_client *client,
4218 enum client_transmission_status status,
4219 void *user_data)
4220{
4221 return client_handle_transmission_status(client, status,
4222 (struct notification_thread_state *) user_data);
4223}
4224
4225static
4226int send_evaluation_to_clients(const struct lttng_trigger *trigger,
4227 const struct lttng_evaluation *evaluation,
4228 struct notification_client_list* client_list,
4229 struct notification_thread_state *state,
4230 uid_t object_uid, gid_t object_gid)
4231{
ff588497
JR
4232 const struct lttng_credentials creds = {
4233 .uid = LTTNG_OPTIONAL_INIT_VALUE(object_uid),
4234 .gid = LTTNG_OPTIONAL_INIT_VALUE(object_gid),
4235 };
4236
f2b3ef9f
JG
4237 return notification_client_list_send_evaluation(client_list,
4238 lttng_trigger_get_const_condition(trigger), evaluation,
4239 lttng_trigger_get_credentials(trigger),
ff588497 4240 &creds,
f2b3ef9f
JG
4241 client_handle_transmission_status_wrapper, state);
4242}
4243
f37d0f86
JG
4244/*
4245 * Permission checks relative to notification channel clients are performed
4246 * here. Notice how object, client, and trigger credentials are involved in
4247 * this check.
4248 *
4249 * The `object` credentials are the credentials associated with the "subject"
4250 * of a condition. For instance, a `rotation completed` condition applies
4251 * to a session. When that condition is met, it will produce an evaluation
4252 * against a session. Hence, in this case, the `object` credentials are the
4253 * credentials of the "subject" session.
4254 *
4255 * The `trigger` credentials are the credentials of the user that registered the
4256 * trigger.
4257 *
4258 * The `client` credentials are the credentials of the user that created a given
4259 * notification channel.
4260 *
4261 * In terms of visibility, it is expected that non-privilieged users can only
4262 * register triggers against "their" objects (their own sessions and
4263 * applications they are allowed to interact with). They can then open a
4264 * notification channel and subscribe to notifications associated with those
4265 * triggers.
4266 *
4267 * As for privilieged users, they can register triggers against the objects of
4268 * other users. They can then subscribe to the notifications associated to their
4269 * triggers. Privilieged users _can't_ subscribe to the notifications of
4270 * triggers owned by other users; they must create their own triggers.
4271 *
4272 * This is more a concern of usability than security. It would be difficult for
4273 * a root user reliably subscribe to a specific set of conditions without
4274 * interference from external users (those could, for instance, unregister
4275 * their triggers).
4276 */
f2b3ef9f
JG
4277LTTNG_HIDDEN
4278int notification_client_list_send_evaluation(
4279 struct notification_client_list *client_list,
4280 const struct lttng_condition *condition,
9b63a4aa 4281 const struct lttng_evaluation *evaluation,
f2b3ef9f
JG
4282 const struct lttng_credentials *trigger_creds,
4283 const struct lttng_credentials *source_object_creds,
4284 report_client_transmission_result_cb client_report,
4285 void *user_data)
ab0ee2ca
JG
4286{
4287 int ret = 0;
c0a66c84 4288 struct lttng_payload msg_payload;
ab0ee2ca 4289 struct notification_client_list_element *client_list_element, *tmp;
9b63a4aa 4290 const struct lttng_notification notification = {
f2b3ef9f 4291 .condition = (struct lttng_condition *) condition,
9b63a4aa
JG
4292 .evaluation = (struct lttng_evaluation *) evaluation,
4293 };
3647288f
JG
4294 struct lttng_notification_channel_message msg_header = {
4295 .type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION,
4296 };
ab0ee2ca 4297
c0a66c84 4298 lttng_payload_init(&msg_payload);
ab0ee2ca 4299
c0a66c84 4300 ret = lttng_dynamic_buffer_append(&msg_payload.buffer, &msg_header,
3647288f 4301 sizeof(msg_header));
ab0ee2ca
JG
4302 if (ret) {
4303 goto end;
4304 }
4305
c0a66c84 4306 ret = lttng_notification_serialize(&notification, &msg_payload);
ab0ee2ca 4307 if (ret) {
ab0ee2ca
JG
4308 ERR("[notification-thread] Failed to serialize notification");
4309 ret = -1;
4310 goto end;
4311 }
4312
3647288f 4313 /* Update payload size. */
505b2d90
JG
4314 ((struct lttng_notification_channel_message *) msg_payload.buffer.data)
4315 ->size = (uint32_t)(
4316 msg_payload.buffer.size - sizeof(msg_header));
3647288f 4317
882093ee
JR
4318 /* Update the payload number of fds. */
4319 {
4320 const struct lttng_payload_view pv = lttng_payload_view_from_payload(
4321 &msg_payload, 0, -1);
4322
4323 ((struct lttng_notification_channel_message *)
4324 msg_payload.buffer.data)->fds = (uint32_t)
4325 lttng_payload_view_get_fd_handle_count(&pv);
4326 }
4327
505b2d90 4328 pthread_mutex_lock(&client_list->lock);
ab0ee2ca
JG
4329 cds_list_for_each_entry_safe(client_list_element, tmp,
4330 &client_list->list, node) {
f2b3ef9f 4331 enum client_transmission_status transmission_status;
ab0ee2ca
JG
4332 struct notification_client *client =
4333 client_list_element->client;
4334
505b2d90
JG
4335 ret = 0;
4336 pthread_mutex_lock(&client->lock);
6c24d3fd
JG
4337 if (!client->communication.active) {
4338 /*
4339 * Skip inactive client (protocol error or
4340 * disconnecting).
4341 */
4342 DBG("Skipping client at it is marked as inactive");
4343 goto skip_client;
4344 }
4345
f2b3ef9f 4346 if (source_object_creds) {
ff588497
JR
4347 if (client->uid != lttng_credentials_get_uid(source_object_creds) &&
4348 client->gid != lttng_credentials_get_gid(source_object_creds) &&
f2b3ef9f
JG
4349 client->uid != 0) {
4350 /*
4351 * Client is not allowed to monitor this
4352 * object.
4353 */
4354 DBG("[notification-thread] Skipping client at it does not have the object permission to receive notification for this trigger");
6c24d3fd 4355 goto skip_client;
f2b3ef9f 4356 }
90843f49
JR
4357 }
4358
ff588497 4359 if (client->uid != lttng_credentials_get_uid(trigger_creds) && client->gid != lttng_credentials_get_gid(trigger_creds)) {
90843f49 4360 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this trigger");
6c24d3fd 4361 goto skip_client;
ab0ee2ca
JG
4362 }
4363
4364 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
c0a66c84 4365 client->socket, msg_payload.buffer.size);
882093ee
JR
4366
4367 if (client_has_outbound_data_left(client)) {
ab0ee2ca
JG
4368 /*
4369 * Outgoing data is already buffered for this client;
4370 * drop the notification and enqueue a "dropped
4371 * notification" message if this is the first dropped
4372 * notification since the socket spilled-over to the
4373 * queue.
4374 */
f2b3ef9f
JG
4375 ret = client_notification_overflow(client);
4376 if (ret) {
6c24d3fd
JG
4377 /* Fatal error. */
4378 goto skip_client;
ab0ee2ca 4379 }
ab0ee2ca
JG
4380 }
4381
882093ee 4382 ret = lttng_payload_copy(&msg_payload, &client->communication.outbound.payload);
ab0ee2ca 4383 if (ret) {
6c24d3fd
JG
4384 /* Fatal error. */
4385 goto skip_client;
ab0ee2ca
JG
4386 }
4387
f2b3ef9f 4388 transmission_status = client_flush_outgoing_queue(client);
6c24d3fd 4389 pthread_mutex_unlock(&client->lock);
f2b3ef9f 4390 ret = client_report(client, transmission_status, user_data);
ab0ee2ca 4391 if (ret) {
6c24d3fd
JG
4392 /* Fatal error. */
4393 goto end_unlock_list;
505b2d90 4394 }
6c24d3fd
JG
4395
4396 continue;
4397
4398skip_client:
505b2d90
JG
4399 pthread_mutex_unlock(&client->lock);
4400 if (ret) {
6c24d3fd 4401 /* Fatal error. */
505b2d90 4402 goto end_unlock_list;
ab0ee2ca
JG
4403 }
4404 }
4405 ret = 0;
505b2d90
JG
4406
4407end_unlock_list:
4408 pthread_mutex_unlock(&client_list->lock);
ab0ee2ca 4409end:
c0a66c84 4410 lttng_payload_reset(&msg_payload);
ab0ee2ca
JG
4411 return ret;
4412}
4413
8b524060
FD
4414static
4415struct lttng_event_notifier_notification *recv_one_event_notifier_notification(
b9a8d78f 4416 int notification_pipe_read_fd, enum lttng_domain_type domain)
94078603
JR
4417{
4418 int ret;
b9a8d78f
FD
4419 uint64_t token;
4420 struct lttng_event_notifier_notification *notification = NULL;
82b3cbf4
JR
4421 char *capture_buffer = NULL;
4422 size_t capture_buffer_size;
94078603
JR
4423 void *reception_buffer;
4424 size_t reception_size;
4425
fc4b93fa 4426 struct lttng_ust_abi_event_notifier_notification ust_notification;
b9a8d78f 4427 struct lttng_kernel_event_notifier_notification kernel_notification;
94078603 4428
b9a8d78f 4429 /* Init lttng_event_notifier_notification */
94078603
JR
4430 switch(domain) {
4431 case LTTNG_DOMAIN_UST:
4432 reception_buffer = (void *) &ust_notification;
4433 reception_size = sizeof(ust_notification);
94078603
JR
4434 break;
4435 case LTTNG_DOMAIN_KERNEL:
4436 reception_buffer = (void *) &kernel_notification;
4437 reception_size = sizeof(kernel_notification);
94078603
JR
4438 break;
4439 default:
4440 abort();
4441 }
4442
4443 /*
4444 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4445 * ensuring that read/write of tracer notifications are atomic.
4446 */
4447 ret = lttng_read(notification_pipe_read_fd, reception_buffer,
4448 reception_size);
4449 if (ret != reception_size) {
4450 PERROR("Failed to read from event source notification pipe: fd = %d, size to read = %zu, ret = %d",
4451 notification_pipe_read_fd, reception_size, ret);
4452 ret = -1;
4453 goto end;
4454 }
4455
4456 switch(domain) {
4457 case LTTNG_DOMAIN_UST:
b9a8d78f 4458 token = ust_notification.token;
82b3cbf4 4459 capture_buffer_size = ust_notification.capture_buf_size;
94078603
JR
4460 break;
4461 case LTTNG_DOMAIN_KERNEL:
b9a8d78f 4462 token = kernel_notification.token;
bbd6675c 4463 capture_buffer_size = kernel_notification.capture_buf_size;
94078603
JR
4464 break;
4465 default:
4466 abort();
4467 }
4468
82b3cbf4
JR
4469 if (capture_buffer_size == 0) {
4470 capture_buffer = NULL;
4471 goto skip_capture;
4472 }
4473
4474 if (capture_buffer_size > MAX_CAPTURE_SIZE) {
4475 ERR("[notification-thread] Event notifier has a capture payload size which exceeds the maximum allowed size: capture_payload_size = %zu bytes, max allowed size = %d bytes",
4476 capture_buffer_size, MAX_CAPTURE_SIZE);
4477 goto end;
4478 }
4479
4480 capture_buffer = zmalloc(capture_buffer_size);
4481 if (!capture_buffer) {
4482 ERR("[notification-thread] Failed to allocate capture buffer");
4483 goto end;
4484 }
4485
4486 /* Fetch additional payload (capture). */
4487 ret = lttng_read(notification_pipe_read_fd, capture_buffer, capture_buffer_size);
4488 if (ret != capture_buffer_size) {
4489 ERR("[notification-thread] Failed to read from event source pipe (fd = %i)",
4490 notification_pipe_read_fd);
4491 goto end;
4492 }
4493
4494skip_capture:
4495 notification = lttng_event_notifier_notification_create(token, domain,
4496 capture_buffer, capture_buffer_size);
4497 if (notification == NULL) {
4498 goto end;
4499 }
4500
4501 /*
4502 * Ownership transfered to the lttng_event_notifier_notification object.
4503 */
4504 capture_buffer = NULL;
4505
b9a8d78f 4506end:
82b3cbf4 4507 free(capture_buffer);
b9a8d78f
FD
4508 return notification;
4509}
4510
8b524060
FD
4511static
4512int dispatch_one_event_notifier_notification(struct notification_thread_state *state,
4513 struct lttng_event_notifier_notification *notification)
b9a8d78f 4514{
b9a8d78f
FD
4515 struct cds_lfht_node *node;
4516 struct cds_lfht_iter iter;
4517 struct notification_trigger_tokens_ht_element *element;
8b524060 4518 enum lttng_trigger_status trigger_status;
b9a8d78f 4519 struct lttng_evaluation *evaluation = NULL;
b9a8d78f
FD
4520 enum action_executor_status executor_status;
4521 struct notification_client_list *client_list = NULL;
4522 const char *trigger_name;
8b524060 4523 int ret;
7c920b63 4524 unsigned int capture_count = 0;
b9a8d78f 4525
94078603
JR
4526 /* Find triggers associated with this token. */
4527 rcu_read_lock();
4528 cds_lfht_lookup(state->trigger_tokens_ht,
b9a8d78f
FD
4529 hash_key_u64(&notification->tracer_token, lttng_ht_seed),
4530 match_trigger_token, &notification->tracer_token, &iter);
94078603 4531 node = cds_lfht_iter_get_node(&iter);
f1446131 4532 if (caa_unlikely(!node)) {
94078603 4533 /*
f1446131
JR
4534 * This is not an error, slow consumption of the tracer
4535 * notifications can lead to situations where a trigger is
4536 * removed but we still get tracer notifications matching a
4537 * trigger that no longer exists.
94078603
JR
4538 */
4539 ret = 0;
4540 goto end_unlock;
4541 }
4542
4543 element = caa_container_of(node,
4544 struct notification_trigger_tokens_ht_element,
4545 node);
4546
f1446131
JR
4547 if (!lttng_trigger_should_fire(element->trigger)) {
4548 ret = 0;
4549 goto end_unlock;
4550 }
94078603 4551
f1446131 4552 lttng_trigger_fire(element->trigger);
94078603 4553
b9a8d78f
FD
4554 trigger_status = lttng_trigger_get_name(element->trigger, &trigger_name);
4555 assert(trigger_status == LTTNG_TRIGGER_STATUS_OK);
4556
d602bd6a 4557 if (lttng_condition_on_event_get_capture_descriptor_count(
7c920b63
PP
4558 lttng_trigger_get_const_condition(element->trigger),
4559 &capture_count) != LTTNG_CONDITION_STATUS_OK) {
4560 ERR("Failed to get capture count");
4561 ret = -1;
4562 goto end;
4563 }
4564
4565 if (!notification->capture_buffer && capture_count != 0) {
4566 ERR("Expected capture but capture buffer is null");
4567 ret = -1;
4568 goto end;
4569 }
4570
d602bd6a 4571 evaluation = lttng_evaluation_on_event_create(
7c920b63
PP
4572 container_of(lttng_trigger_get_const_condition(
4573 element->trigger),
d602bd6a 4574 struct lttng_condition_on_event,
7c920b63
PP
4575 parent),
4576 trigger_name,
4577 notification->capture_buffer,
4578 notification->capture_buf_size, false);
4579
f1446131 4580 if (evaluation == NULL) {
b9a8d78f 4581 ERR("[notification-thread] Failed to create event rule hit evaluation while creating and enqueuing action executor job");
f1446131
JR
4582 ret = -1;
4583 goto end_unlock;
4584 }
f1446131
JR
4585 client_list = get_client_list_from_condition(state,
4586 lttng_trigger_get_const_condition(element->trigger));
4587 executor_status = action_executor_enqueue(state->executor,
4588 element->trigger, evaluation, NULL, client_list);
4589 switch (executor_status) {
4590 case ACTION_EXECUTOR_STATUS_OK:
4591 ret = 0;
4592 break;
4593 case ACTION_EXECUTOR_STATUS_OVERFLOW:
4594 {
4595 struct notification_client_list_element *client_list_element,
4596 *tmp;
4597
4598 /*
4599 * Not a fatal error; this is expected and simply means the
4600 * executor has too much work queued already.
4601 */
4602 ret = 0;
4603
4604 /* No clients subscribed to notifications for this trigger. */
4605 if (!client_list) {
4606 break;
4607 }
4608
4609 /* Warn clients that a notification (or more) was dropped. */
4610 pthread_mutex_lock(&client_list->lock);
4611 cds_list_for_each_entry_safe(client_list_element, tmp,
4612 &client_list->list, node) {
4613 enum client_transmission_status transmission_status;
4614 struct notification_client *client =
4615 client_list_element->client;
4616
4617 pthread_mutex_lock(&client->lock);
4618 ret = client_notification_overflow(client);
4619 if (ret) {
4620 /* Fatal error. */
4621 goto next_client;
4622 }
4623
4624 transmission_status =
4625 client_flush_outgoing_queue(client);
4626 ret = client_handle_transmission_status(
4627 client, transmission_status, state);
4628 if (ret) {
4629 /* Fatal error. */
4630 goto next_client;
4631 }
4632next_client:
4633 pthread_mutex_unlock(&client->lock);
4634 if (ret) {
4635 break;
4636 }
4637 }
4638
4639 pthread_mutex_unlock(&client_list->lock);
4640 break;
4641 }
98dfca79 4642 case ACTION_EXECUTOR_STATUS_INVALID:
f1446131
JR
4643 case ACTION_EXECUTOR_STATUS_ERROR:
4644 /* Fatal error, shut down everything. */
4645 ERR("Fatal error encoutered while enqueuing action to the action executor");
4646 ret = -1;
4647 goto end_unlock;
4648 default:
4649 /* Unhandled error. */
4650 abort();
4651 }
94078603
JR
4652
4653end_unlock:
f1446131 4654 notification_client_list_put(client_list);
94078603 4655 rcu_read_unlock();
7c920b63 4656end:
8b524060
FD
4657 return ret;
4658}
4659
4660static
4661int handle_one_event_notifier_notification(
4662 struct notification_thread_state *state,
4663 int pipe, enum lttng_domain_type domain)
4664{
82b3cbf4 4665 int ret = 0;
8b524060
FD
4666 struct lttng_event_notifier_notification *notification = NULL;
4667
4668 notification = recv_one_event_notifier_notification(pipe, domain);
4669 if (notification == NULL) {
82b3cbf4 4670 /* Reception failed, don't consider it fatal. */
8b524060
FD
4671 ERR("[notification-thread] Error receiving an event notifier notification from tracer: fd = %i, domain = %s",
4672 pipe, lttng_domain_type_str(domain));
8b524060
FD
4673 goto end;
4674 }
4675
4676 ret = dispatch_one_event_notifier_notification(state, notification);
4677 if (ret) {
4678 ERR("[notification-thread] Error dispatching an event notifier notification from tracer: fd = %i, domain = %s",
4679 pipe, lttng_domain_type_str(domain));
4680 goto end;
4681 }
4682
94078603 4683end:
8b524060 4684 lttng_event_notifier_notification_destroy(notification);
94078603
JR
4685 return ret;
4686}
4687
8b524060
FD
4688int handle_notification_thread_event_notification(struct notification_thread_state *state,
4689 int pipe, enum lttng_domain_type domain)
4690{
4691 return handle_one_event_notifier_notification(state, pipe, domain);
4692}
4693
ab0ee2ca
JG
4694int handle_notification_thread_channel_sample(
4695 struct notification_thread_state *state, int pipe,
4696 enum lttng_domain_type domain)
4697{
4698 int ret = 0;
4699 struct lttcomm_consumer_channel_monitor_msg sample_msg;
ab0ee2ca
JG
4700 struct channel_info *channel_info;
4701 struct cds_lfht_node *node;
4702 struct cds_lfht_iter iter;
4703 struct lttng_channel_trigger_list *trigger_list;
4704 struct lttng_trigger_list_element *trigger_list_element;
4705 bool previous_sample_available = false;
e8360425
JD
4706 struct channel_state_sample previous_sample, latest_sample;
4707 uint64_t previous_session_consumed_total, latest_session_consumed_total;
f2b3ef9f 4708 struct lttng_credentials channel_creds;
ab0ee2ca
JG
4709
4710 /*
4711 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4712 * ensuring that read/write of sampling messages are atomic.
4713 */
7c2551ef 4714 ret = lttng_read(pipe, &sample_msg, sizeof(sample_msg));
ab0ee2ca
JG
4715 if (ret != sizeof(sample_msg)) {
4716 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
4717 pipe);
4718 ret = -1;
4719 goto end;
4720 }
4721
4722 ret = 0;
4723 latest_sample.key.key = sample_msg.key;
4724 latest_sample.key.domain = domain;
4725 latest_sample.highest_usage = sample_msg.highest;
4726 latest_sample.lowest_usage = sample_msg.lowest;
e8360425 4727 latest_sample.channel_total_consumed = sample_msg.total_consumed;
ab0ee2ca
JG
4728
4729 rcu_read_lock();
4730
4731 /* Retrieve the channel's informations */
4732 cds_lfht_lookup(state->channels_ht,
4733 hash_channel_key(&latest_sample.key),
4734 match_channel_info,
4735 &latest_sample.key,
4736 &iter);
4737 node = cds_lfht_iter_get_node(&iter);
e0b5f87b 4738 if (caa_unlikely(!node)) {
ab0ee2ca
JG
4739 /*
4740 * Not an error since the consumer can push a sample to the pipe
4741 * and the rest of the session daemon could notify us of the
4742 * channel's destruction before we get a chance to process that
4743 * sample.
4744 */
4745 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64 " in %s domain",
4746 latest_sample.key.key,
526200e4 4747 lttng_domain_type_str(domain));
ab0ee2ca
JG
4748 goto end_unlock;
4749 }
4750 channel_info = caa_container_of(node, struct channel_info,
4751 channels_ht_node);
e8360425 4752 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64 ") in session %s (highest usage = %" PRIu64 ", lowest usage = %" PRIu64", total consumed = %" PRIu64")",
8abe313a 4753 channel_info->name,
ab0ee2ca 4754 latest_sample.key.key,
8abe313a 4755 channel_info->session_info->name,
ab0ee2ca 4756 latest_sample.highest_usage,
e8360425
JD
4757 latest_sample.lowest_usage,
4758 latest_sample.channel_total_consumed);
4759
4760 previous_session_consumed_total =
4761 channel_info->session_info->consumed_data_size;
ab0ee2ca
JG
4762
4763 /* Retrieve the channel's last sample, if it exists, and update it. */
4764 cds_lfht_lookup(state->channel_state_ht,
4765 hash_channel_key(&latest_sample.key),
4766 match_channel_state_sample,
4767 &latest_sample.key,
4768 &iter);
4769 node = cds_lfht_iter_get_node(&iter);
e0b5f87b 4770 if (caa_likely(node)) {
ab0ee2ca
JG
4771 struct channel_state_sample *stored_sample;
4772
4773 /* Update the sample stored. */
4774 stored_sample = caa_container_of(node,
4775 struct channel_state_sample,
4776 channel_state_ht_node);
e8360425 4777
ab0ee2ca
JG
4778 memcpy(&previous_sample, stored_sample,
4779 sizeof(previous_sample));
4780 stored_sample->highest_usage = latest_sample.highest_usage;
4781 stored_sample->lowest_usage = latest_sample.lowest_usage;
0f0479d7 4782 stored_sample->channel_total_consumed = latest_sample.channel_total_consumed;
ab0ee2ca 4783 previous_sample_available = true;
e8360425
JD
4784
4785 latest_session_consumed_total =
4786 previous_session_consumed_total +
4787 (latest_sample.channel_total_consumed - previous_sample.channel_total_consumed);
ab0ee2ca
JG
4788 } else {
4789 /*
4790 * This is the channel's first sample, allocate space for and
4791 * store the new sample.
4792 */
4793 struct channel_state_sample *stored_sample;
4794
4795 stored_sample = zmalloc(sizeof(*stored_sample));
4796 if (!stored_sample) {
4797 ret = -1;
4798 goto end_unlock;
4799 }
4800
4801 memcpy(stored_sample, &latest_sample, sizeof(*stored_sample));
4802 cds_lfht_node_init(&stored_sample->channel_state_ht_node);
4803 cds_lfht_add(state->channel_state_ht,
4804 hash_channel_key(&stored_sample->key),
4805 &stored_sample->channel_state_ht_node);
e8360425
JD
4806
4807 latest_session_consumed_total =
4808 previous_session_consumed_total +
4809 latest_sample.channel_total_consumed;
ab0ee2ca
JG
4810 }
4811
e8360425
JD
4812 channel_info->session_info->consumed_data_size =
4813 latest_session_consumed_total;
4814
ab0ee2ca
JG
4815 /* Find triggers associated with this channel. */
4816 cds_lfht_lookup(state->channel_triggers_ht,
4817 hash_channel_key(&latest_sample.key),
4818 match_channel_trigger_list,
4819 &latest_sample.key,
4820 &iter);
4821 node = cds_lfht_iter_get_node(&iter);
e0b5f87b 4822 if (caa_likely(!node)) {
ab0ee2ca
JG
4823 goto end_unlock;
4824 }
4825
f2b3ef9f 4826 channel_creds = (typeof(channel_creds)) {
ff588497
JR
4827 .uid = LTTNG_OPTIONAL_INIT_VALUE(channel_info->session_info->uid),
4828 .gid = LTTNG_OPTIONAL_INIT_VALUE(channel_info->session_info->gid),
f2b3ef9f
JG
4829 };
4830
ab0ee2ca
JG
4831 trigger_list = caa_container_of(node, struct lttng_channel_trigger_list,
4832 channel_triggers_ht_node);
4833 cds_list_for_each_entry(trigger_list_element, &trigger_list->list,
e742b055 4834 node) {
9b63a4aa 4835 const struct lttng_condition *condition;
f2b3ef9f 4836 struct lttng_trigger *trigger;
505b2d90 4837 struct notification_client_list *client_list = NULL;
ab0ee2ca 4838 struct lttng_evaluation *evaluation = NULL;
f2b3ef9f 4839 enum action_executor_status executor_status;
ab0ee2ca 4840
505b2d90 4841 ret = 0;
ab0ee2ca 4842 trigger = trigger_list_element->trigger;
9b63a4aa 4843 condition = lttng_trigger_get_const_condition(trigger);
ab0ee2ca 4844 assert(condition);
ab0ee2ca
JG
4845
4846 /*
4847 * Check if any client is subscribed to the result of this
4848 * evaluation.
4849 */
ed327204 4850 client_list = get_client_list_from_condition(state, condition);
ab0ee2ca 4851
ed327204 4852 ret = evaluate_buffer_condition(condition, &evaluation, state,
ab0ee2ca 4853 previous_sample_available ? &previous_sample : NULL,
e8360425
JD
4854 &latest_sample,
4855 previous_session_consumed_total,
4856 latest_session_consumed_total,
4857 channel_info);
4858 if (caa_unlikely(ret)) {
505b2d90 4859 goto put_list;
ab0ee2ca
JG
4860 }
4861
e8360425 4862 if (caa_likely(!evaluation)) {
505b2d90 4863 goto put_list;
ab0ee2ca
JG
4864 }
4865
f8522f5c
JR
4866 if (!lttng_trigger_should_fire(trigger)) {
4867 goto put_list;
4868 }
4869
4870 lttng_trigger_fire(trigger);
4871
f2b3ef9f
JG
4872 /*
4873 * Ownership of `evaluation` transferred to the action executor
4874 * no matter the result.
4875 */
4876 executor_status = action_executor_enqueue(state->executor,
4877 trigger, evaluation, &channel_creds,
4878 client_list);
4879 evaluation = NULL;
4880 switch (executor_status) {
4881 case ACTION_EXECUTOR_STATUS_OK:
4882 break;
4883 case ACTION_EXECUTOR_STATUS_ERROR:
4884 case ACTION_EXECUTOR_STATUS_INVALID:
4885 /*
4886 * TODO Add trigger identification (name/id) when
4887 * it is added to the API.
4888 */
4889 ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger");
4890 ret = -1;
4891 goto put_list;
4892 case ACTION_EXECUTOR_STATUS_OVERFLOW:
4893 /*
4894 * TODO Add trigger identification (name/id) when
4895 * it is added to the API.
4896 *
4897 * Not a fatal error.
4898 */
4899 WARN("No space left when enqueuing action associated with buffer-condition trigger");
4900 ret = 0;
4901 goto put_list;
4902 default:
4903 abort();
4904 }
4905
505b2d90
JG
4906put_list:
4907 notification_client_list_put(client_list);
e0b5f87b 4908 if (caa_unlikely(ret)) {
505b2d90 4909 break;
ab0ee2ca
JG
4910 }
4911 }
4912end_unlock:
4913 rcu_read_unlock();
4914end:
4915 return ret;
4916}
This page took 0.332625 seconds and 4 git commands to generate.