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