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