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