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