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