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