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