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