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