sessiond: notification-thread: switch logging to debug on GET_TRIGGER
[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) {
4c3f302b 533 ERR("[notification-thread] 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. */
f82f93a1 873 DBG("[notification-thread] 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 */
902 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
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) {
066a3c82 912 WARN("[notification-thread] 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) {
948 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
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) {
979 DBG("[notification-thread] No known session matching name \"%s\"",
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. */
1004 ERR("[notification-thread] Failed to create session rotation ongoing evaluation for session \"%s\"",
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:
7e802d0a 1056 DBG("[notification-thread] 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. */
1070 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
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. */
1087 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
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;
1137 cds_list_add(&condition_list_element->node, &client->condition_list);
1138
ed327204
JG
1139 client_list = get_client_list_from_condition(state, condition);
1140 if (!client_list) {
2ae99f0b
JG
1141 /*
1142 * No notification-emiting trigger registered with this
1143 * condition. We don't evaluate the condition right away
1144 * since this trigger is not registered yet.
1145 */
4fb43b68 1146 free(client_list_element);
505b2d90 1147 goto end;
ab0ee2ca
JG
1148 }
1149
2ae99f0b
JG
1150 /*
1151 * The condition to which the client just subscribed is evaluated
1152 * at this point so that conditions that are already TRUE result
1153 * in a notification being sent out.
505b2d90 1154 *
57644a7f
JG
1155 * Note the iteration on all triggers which share an identical
1156 * `condition` than the one to which the client is registering. This is
1157 * done to ensure that the client receives a distinct notification for
1158 * all triggers that have a `notify` action that have this condition.
2ae99f0b 1159 */
091fa780
FD
1160 pthread_mutex_lock(&client_list->lock);
1161 cds_list_for_each_entry(trigger_ht_element,
1162 &client_list->triggers_list, client_list_trigger_node) {
1163 if (evaluate_condition_for_client(trigger_ht_element->trigger, condition,
1164 client, state)) {
1165 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
1166 ret = -1;
1167 free(client_list_element);
ecc7ed07 1168 pthread_mutex_unlock(&client_list->lock);
091fa780
FD
1169 goto end;
1170 }
e4db5ace 1171 }
091fa780 1172 pthread_mutex_unlock(&client_list->lock);
e4db5ace
JR
1173
1174 /*
1175 * Add the client to the list of clients interested in a given trigger
1176 * if a "notification" trigger with a corresponding condition was
1177 * added prior.
1178 */
ab0ee2ca
JG
1179 client_list_element->client = client;
1180 CDS_INIT_LIST_HEAD(&client_list_element->node);
505b2d90
JG
1181
1182 pthread_mutex_lock(&client_list->lock);
091fa780 1183 cds_list_add(&client_list_element->node, &client_list->clients_list);
505b2d90 1184 pthread_mutex_unlock(&client_list->lock);
ab0ee2ca
JG
1185end:
1186 if (_status) {
1187 *_status = status;
1188 }
505b2d90
JG
1189 if (client_list) {
1190 notification_client_list_put(client_list);
1191 }
ab0ee2ca
JG
1192 return ret;
1193error:
1194 free(condition_list_element);
1195 free(client_list_element);
1196 return ret;
1197}
1198
1199static
1200int notification_thread_client_unsubscribe(
1201 struct notification_client *client,
1202 struct lttng_condition *condition,
1203 struct notification_thread_state *state,
1204 enum lttng_notification_channel_status *_status)
1205{
ab0ee2ca
JG
1206 struct notification_client_list *client_list;
1207 struct lttng_condition_list_element *condition_list_element,
1208 *condition_tmp;
1209 struct notification_client_list_element *client_list_element,
1210 *client_tmp;
1211 bool condition_found = false;
1212 enum lttng_notification_channel_status status =
1213 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK;
1214
1215 /* Remove the condition from the client's condition list. */
1216 cds_list_for_each_entry_safe(condition_list_element, condition_tmp,
1217 &client->condition_list, node) {
1218 if (!lttng_condition_is_equal(condition_list_element->condition,
1219 condition)) {
1220 continue;
1221 }
1222
1223 cds_list_del(&condition_list_element->node);
1224 /*
1225 * The caller may be iterating on the client's conditions to
1226 * tear down a client's connection. In this case, the condition
1227 * will be destroyed at the end.
1228 */
1229 if (condition != condition_list_element->condition) {
1230 lttng_condition_destroy(
1231 condition_list_element->condition);
1232 }
1233 free(condition_list_element);
1234 condition_found = true;
1235 break;
1236 }
1237
1238 if (!condition_found) {
1239 status = LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION;
1240 goto end;
1241 }
1242
1243 /*
1244 * Remove the client from the list of clients interested the trigger
1245 * matching the condition.
1246 */
ed327204
JG
1247 client_list = get_client_list_from_condition(state, condition);
1248 if (!client_list) {
505b2d90 1249 goto end;
ab0ee2ca
JG
1250 }
1251
505b2d90 1252 pthread_mutex_lock(&client_list->lock);
ab0ee2ca 1253 cds_list_for_each_entry_safe(client_list_element, client_tmp,
091fa780 1254 &client_list->clients_list, node) {
505b2d90 1255 if (client_list_element->client->id != client->id) {
ab0ee2ca
JG
1256 continue;
1257 }
1258 cds_list_del(&client_list_element->node);
1259 free(client_list_element);
1260 break;
1261 }
505b2d90
JG
1262 pthread_mutex_unlock(&client_list->lock);
1263 notification_client_list_put(client_list);
1264 client_list = NULL;
ab0ee2ca
JG
1265end:
1266 lttng_condition_destroy(condition);
1267 if (_status) {
1268 *_status = status;
1269 }
1270 return 0;
1271}
1272
83b934ad
MD
1273static
1274void free_notification_client_rcu(struct rcu_head *node)
1275{
1276 free(caa_container_of(node, struct notification_client, rcu_node));
1277}
1278
ab0ee2ca
JG
1279static
1280void notification_client_destroy(struct notification_client *client,
1281 struct notification_thread_state *state)
1282{
ab0ee2ca
JG
1283 if (!client) {
1284 return;
1285 }
1286
505b2d90
JG
1287 /*
1288 * The client object is not reachable by other threads, no need to lock
1289 * the client here.
1290 */
ab0ee2ca
JG
1291 if (client->socket >= 0) {
1292 (void) lttcomm_close_unix_sock(client->socket);
ac1889bf 1293 client->socket = -1;
ab0ee2ca 1294 }
505b2d90 1295 client->communication.active = false;
882093ee
JR
1296 lttng_payload_reset(&client->communication.inbound.payload);
1297 lttng_payload_reset(&client->communication.outbound.payload);
505b2d90 1298 pthread_mutex_destroy(&client->lock);
83b934ad 1299 call_rcu(&client->rcu_node, free_notification_client_rcu);
ab0ee2ca
JG
1300}
1301
1302/*
1303 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1304 * client pointer).
1305 */
1306static
1307struct notification_client *get_client_from_socket(int socket,
1308 struct notification_thread_state *state)
1309{
1310 struct cds_lfht_iter iter;
1311 struct cds_lfht_node *node;
1312 struct notification_client *client = NULL;
1313
1314 cds_lfht_lookup(state->client_socket_ht,
ac1889bf
JG
1315 hash_client_socket(socket),
1316 match_client_socket,
ab0ee2ca
JG
1317 (void *) (unsigned long) socket,
1318 &iter);
1319 node = cds_lfht_iter_get_node(&iter);
1320 if (!node) {
1321 goto end;
1322 }
1323
1324 client = caa_container_of(node, struct notification_client,
1325 client_socket_ht_node);
1326end:
1327 return client;
1328}
1329
f2b3ef9f
JG
1330/*
1331 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1332 * client pointer).
1333 */
1334static
1335struct notification_client *get_client_from_id(notification_client_id id,
1336 struct notification_thread_state *state)
1337{
1338 struct cds_lfht_iter iter;
1339 struct cds_lfht_node *node;
1340 struct notification_client *client = NULL;
1341
1342 cds_lfht_lookup(state->client_id_ht,
1343 hash_client_id(id),
1344 match_client_id,
1345 &id,
1346 &iter);
1347 node = cds_lfht_iter_get_node(&iter);
1348 if (!node) {
1349 goto end;
1350 }
1351
1352 client = caa_container_of(node, struct notification_client,
1353 client_id_ht_node);
1354end:
1355 return client;
1356}
1357
ab0ee2ca 1358static
e8360425 1359bool buffer_usage_condition_applies_to_channel(
51eab943
JG
1360 const struct lttng_condition *condition,
1361 const struct channel_info *channel_info)
ab0ee2ca
JG
1362{
1363 enum lttng_condition_status status;
e8360425
JD
1364 enum lttng_domain_type condition_domain;
1365 const char *condition_session_name = NULL;
1366 const char *condition_channel_name = NULL;
ab0ee2ca 1367
e8360425
JD
1368 status = lttng_condition_buffer_usage_get_domain_type(condition,
1369 &condition_domain);
1370 assert(status == LTTNG_CONDITION_STATUS_OK);
1371 if (channel_info->key.domain != condition_domain) {
ab0ee2ca
JG
1372 goto fail;
1373 }
1374
e8360425
JD
1375 status = lttng_condition_buffer_usage_get_session_name(
1376 condition, &condition_session_name);
1377 assert((status == LTTNG_CONDITION_STATUS_OK) && condition_session_name);
1378
1379 status = lttng_condition_buffer_usage_get_channel_name(
1380 condition, &condition_channel_name);
1381 assert((status == LTTNG_CONDITION_STATUS_OK) && condition_channel_name);
1382
1383 if (strcmp(channel_info->session_info->name, condition_session_name)) {
1384 goto fail;
1385 }
1386 if (strcmp(channel_info->name, condition_channel_name)) {
ab0ee2ca
JG
1387 goto fail;
1388 }
1389
e8360425
JD
1390 return true;
1391fail:
1392 return false;
1393}
1394
1395static
1396bool session_consumed_size_condition_applies_to_channel(
51eab943
JG
1397 const struct lttng_condition *condition,
1398 const struct channel_info *channel_info)
e8360425
JD
1399{
1400 enum lttng_condition_status status;
1401 const char *condition_session_name = NULL;
1402
1403 status = lttng_condition_session_consumed_size_get_session_name(
1404 condition, &condition_session_name);
1405 assert((status == LTTNG_CONDITION_STATUS_OK) && condition_session_name);
1406
1407 if (strcmp(channel_info->session_info->name, condition_session_name)) {
ab0ee2ca
JG
1408 goto fail;
1409 }
1410
e8360425
JD
1411 return true;
1412fail:
1413 return false;
1414}
ab0ee2ca 1415
51eab943
JG
1416static
1417bool trigger_applies_to_channel(const struct lttng_trigger *trigger,
1418 const struct channel_info *channel_info)
1419{
1420 const struct lttng_condition *condition;
e8360425 1421 bool trigger_applies;
ab0ee2ca 1422
51eab943 1423 condition = lttng_trigger_get_const_condition(trigger);
e8360425 1424 if (!condition) {
ab0ee2ca
JG
1425 goto fail;
1426 }
e8360425
JD
1427
1428 switch (lttng_condition_get_type(condition)) {
1429 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
1430 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
1431 trigger_applies = buffer_usage_condition_applies_to_channel(
1432 condition, channel_info);
1433 break;
1434 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
1435 trigger_applies = session_consumed_size_condition_applies_to_channel(
1436 condition, channel_info);
1437 break;
1438 default:
ab0ee2ca
JG
1439 goto fail;
1440 }
1441
e8360425 1442 return trigger_applies;
ab0ee2ca
JG
1443fail:
1444 return false;
1445}
1446
ed327204
JG
1447/* Must be called with RCU read lock held. */
1448static
1449struct lttng_session_trigger_list *get_session_trigger_list(
1450 struct notification_thread_state *state,
1451 const char *session_name)
1452{
1453 struct lttng_session_trigger_list *list = NULL;
1454 struct cds_lfht_node *node;
1455 struct cds_lfht_iter iter;
1456
1457 cds_lfht_lookup(state->session_triggers_ht,
1458 hash_key_str(session_name, lttng_ht_seed),
1459 match_session_trigger_list,
1460 session_name,
1461 &iter);
1462 node = cds_lfht_iter_get_node(&iter);
1463 if (!node) {
1464 /*
1465 * Not an error, the list of triggers applying to that session
1466 * will be initialized when the session is created.
1467 */
1468 DBG("[notification-thread] No trigger list found for session \"%s\" as it is not yet known to the notification system",
1469 session_name);
1470 goto end;
1471 }
1472
e742b055 1473 list = caa_container_of(node,
ed327204
JG
1474 struct lttng_session_trigger_list,
1475 session_triggers_ht_node);
1476end:
1477 return list;
1478}
1479
ea9a44f0
JG
1480/*
1481 * Allocate an empty lttng_session_trigger_list for the session named
1482 * 'session_name'.
1483 *
1484 * No ownership of 'session_name' is assumed by the session trigger list.
1485 * It is the caller's responsability to ensure the session name is alive
1486 * for as long as this list is.
1487 */
1488static
1489struct lttng_session_trigger_list *lttng_session_trigger_list_create(
1490 const char *session_name,
1491 struct cds_lfht *session_triggers_ht)
1492{
1493 struct lttng_session_trigger_list *list;
1494
1495 list = zmalloc(sizeof(*list));
1496 if (!list) {
1497 goto end;
1498 }
1499 list->session_name = session_name;
1500 CDS_INIT_LIST_HEAD(&list->list);
1501 cds_lfht_node_init(&list->session_triggers_ht_node);
1502 list->session_triggers_ht = session_triggers_ht;
1503
1504 rcu_read_lock();
1505 /* Publish the list through the session_triggers_ht. */
1506 cds_lfht_add(session_triggers_ht,
1507 hash_key_str(session_name, lttng_ht_seed),
1508 &list->session_triggers_ht_node);
1509 rcu_read_unlock();
1510end:
1511 return list;
1512}
1513
1514static
1515void free_session_trigger_list_rcu(struct rcu_head *node)
1516{
1517 free(caa_container_of(node, struct lttng_session_trigger_list,
1518 rcu_node));
1519}
1520
1521static
1522void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list *list)
1523{
1524 struct lttng_trigger_list_element *trigger_list_element, *tmp;
1525
1526 /* Empty the list element by element, and then free the list itself. */
1527 cds_list_for_each_entry_safe(trigger_list_element, tmp,
1528 &list->list, node) {
1529 cds_list_del(&trigger_list_element->node);
1530 free(trigger_list_element);
1531 }
1532 rcu_read_lock();
1533 /* Unpublish the list from the session_triggers_ht. */
1534 cds_lfht_del(list->session_triggers_ht,
1535 &list->session_triggers_ht_node);
1536 rcu_read_unlock();
1537 call_rcu(&list->rcu_node, free_session_trigger_list_rcu);
1538}
1539
1540static
1541int lttng_session_trigger_list_add(struct lttng_session_trigger_list *list,
f2b3ef9f 1542 struct lttng_trigger *trigger)
ea9a44f0
JG
1543{
1544 int ret = 0;
1545 struct lttng_trigger_list_element *new_element =
1546 zmalloc(sizeof(*new_element));
1547
1548 if (!new_element) {
1549 ret = -1;
1550 goto end;
1551 }
1552 CDS_INIT_LIST_HEAD(&new_element->node);
1553 new_element->trigger = trigger;
1554 cds_list_add(&new_element->node, &list->list);
1555end:
1556 return ret;
1557}
1558
1559static
1560bool trigger_applies_to_session(const struct lttng_trigger *trigger,
1561 const char *session_name)
1562{
1563 bool applies = false;
1564 const struct lttng_condition *condition;
1565
1566 condition = lttng_trigger_get_const_condition(trigger);
1567 switch (lttng_condition_get_type(condition)) {
1568 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
1569 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
1570 {
1571 enum lttng_condition_status condition_status;
1572 const char *condition_session_name;
1573
1574 condition_status = lttng_condition_session_rotation_get_session_name(
1575 condition, &condition_session_name);
1576 if (condition_status != LTTNG_CONDITION_STATUS_OK) {
1577 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
1578 goto end;
1579 }
1580
1581 assert(condition_session_name);
1582 applies = !strcmp(condition_session_name, session_name);
1583 break;
1584 }
1585 default:
1586 goto end;
1587 }
1588end:
1589 return applies;
1590}
1591
1592/*
1593 * Allocate and initialize an lttng_session_trigger_list which contains
1594 * all triggers that apply to the session named 'session_name'.
1595 *
1596 * No ownership of 'session_name' is assumed by the session trigger list.
1597 * It is the caller's responsability to ensure the session name is alive
1598 * for as long as this list is.
1599 */
1600static
1601struct lttng_session_trigger_list *lttng_session_trigger_list_build(
1602 const struct notification_thread_state *state,
1603 const char *session_name)
1604{
1605 int trigger_count = 0;
1606 struct lttng_session_trigger_list *session_trigger_list = NULL;
1607 struct lttng_trigger_ht_element *trigger_ht_element = NULL;
1608 struct cds_lfht_iter iter;
1609
1610 session_trigger_list = lttng_session_trigger_list_create(session_name,
1611 state->session_triggers_ht);
1612
1613 /* Add all triggers applying to the session named 'session_name'. */
1614 cds_lfht_for_each_entry(state->triggers_ht, &iter, trigger_ht_element,
1615 node) {
1616 int ret;
1617
1618 if (!trigger_applies_to_session(trigger_ht_element->trigger,
1619 session_name)) {
1620 continue;
1621 }
1622
1623 ret = lttng_session_trigger_list_add(session_trigger_list,
1624 trigger_ht_element->trigger);
1625 if (ret) {
1626 goto error;
1627 }
1628
1629 trigger_count++;
1630 }
1631
1632 DBG("[notification-thread] Found %i triggers that apply to newly created session",
1633 trigger_count);
1634 return session_trigger_list;
1635error:
1636 lttng_session_trigger_list_destroy(session_trigger_list);
1637 return NULL;
1638}
1639
8abe313a
JG
1640static
1641struct session_info *find_or_create_session_info(
1642 struct notification_thread_state *state,
1643 const char *name, uid_t uid, gid_t gid)
1644{
1645 struct session_info *session = NULL;
1646 struct cds_lfht_node *node;
1647 struct cds_lfht_iter iter;
ea9a44f0 1648 struct lttng_session_trigger_list *trigger_list;
8abe313a
JG
1649
1650 rcu_read_lock();
1651 cds_lfht_lookup(state->sessions_ht,
1652 hash_key_str(name, lttng_ht_seed),
1653 match_session,
1654 name,
1655 &iter);
1656 node = cds_lfht_iter_get_node(&iter);
1657 if (node) {
1658 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1659 name, uid, gid);
1660 session = caa_container_of(node, struct session_info,
1661 sessions_ht_node);
1662 assert(session->uid == uid);
1663 assert(session->gid == gid);
1eee26c5
JG
1664 session_info_get(session);
1665 goto end;
ea9a44f0
JG
1666 }
1667
1668 trigger_list = lttng_session_trigger_list_build(state, name);
1669 if (!trigger_list) {
1670 goto error;
8abe313a
JG
1671 }
1672
1eee26c5
JG
1673 session = session_info_create(name, uid, gid, trigger_list,
1674 state->sessions_ht);
8abe313a
JG
1675 if (!session) {
1676 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1677 name, uid, gid);
b248644d 1678 lttng_session_trigger_list_destroy(trigger_list);
ea9a44f0 1679 goto error;
8abe313a 1680 }
ea9a44f0 1681 trigger_list = NULL;
577983cb
JG
1682
1683 cds_lfht_add(state->sessions_ht, hash_key_str(name, lttng_ht_seed),
9b63a4aa 1684 &session->sessions_ht_node);
1eee26c5 1685end:
8abe313a
JG
1686 rcu_read_unlock();
1687 return session;
ea9a44f0
JG
1688error:
1689 rcu_read_unlock();
1690 session_info_put(session);
1691 return NULL;
8abe313a
JG
1692}
1693
ab0ee2ca
JG
1694static
1695int handle_notification_thread_command_add_channel(
8abe313a
JG
1696 struct notification_thread_state *state,
1697 const char *session_name, uid_t session_uid, gid_t session_gid,
1698 const char *channel_name, enum lttng_domain_type channel_domain,
1699 uint64_t channel_key_int, uint64_t channel_capacity,
1700 enum lttng_error_code *cmd_result)
ab0ee2ca
JG
1701{
1702 struct cds_list_head trigger_list;
8abe313a
JG
1703 struct channel_info *new_channel_info = NULL;
1704 struct channel_key channel_key = {
1705 .key = channel_key_int,
1706 .domain = channel_domain,
1707 };
ab0ee2ca
JG
1708 struct lttng_channel_trigger_list *channel_trigger_list = NULL;
1709 struct lttng_trigger_ht_element *trigger_ht_element = NULL;
1710 int trigger_count = 0;
1711 struct cds_lfht_iter iter;
8abe313a 1712 struct session_info *session_info = NULL;
ab0ee2ca
JG
1713
1714 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64 " in %s domain",
8abe313a 1715 channel_name, session_name, channel_key_int,
526200e4 1716 lttng_domain_type_str(channel_domain));
ab0ee2ca
JG
1717
1718 CDS_INIT_LIST_HEAD(&trigger_list);
1719
8abe313a
JG
1720 session_info = find_or_create_session_info(state, session_name,
1721 session_uid, session_gid);
1722 if (!session_info) {
a9577b76 1723 /* Allocation error or an internal error occurred. */
ab0ee2ca
JG
1724 goto error;
1725 }
1726
8abe313a
JG
1727 new_channel_info = channel_info_create(channel_name, &channel_key,
1728 channel_capacity, session_info);
1729 if (!new_channel_info) {
1730 goto error;
1731 }
ab0ee2ca 1732
83b934ad 1733 rcu_read_lock();
ab0ee2ca
JG
1734 /* Build a list of all triggers applying to the new channel. */
1735 cds_lfht_for_each_entry(state->triggers_ht, &iter, trigger_ht_element,
1736 node) {
1737 struct lttng_trigger_list_element *new_element;
1738
1739 if (!trigger_applies_to_channel(trigger_ht_element->trigger,
8abe313a 1740 new_channel_info)) {
ab0ee2ca
JG
1741 continue;
1742 }
1743
1744 new_element = zmalloc(sizeof(*new_element));
1745 if (!new_element) {
83b934ad 1746 rcu_read_unlock();
ab0ee2ca
JG
1747 goto error;
1748 }
1749 CDS_INIT_LIST_HEAD(&new_element->node);
1750 new_element->trigger = trigger_ht_element->trigger;
1751 cds_list_add(&new_element->node, &trigger_list);
1752 trigger_count++;
1753 }
83b934ad 1754 rcu_read_unlock();
ab0ee2ca
JG
1755
1756 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1757 trigger_count);
1758 channel_trigger_list = zmalloc(sizeof(*channel_trigger_list));
1759 if (!channel_trigger_list) {
1760 goto error;
1761 }
8abe313a 1762 channel_trigger_list->channel_key = new_channel_info->key;
ab0ee2ca
JG
1763 CDS_INIT_LIST_HEAD(&channel_trigger_list->list);
1764 cds_lfht_node_init(&channel_trigger_list->channel_triggers_ht_node);
1765 cds_list_splice(&trigger_list, &channel_trigger_list->list);
1766
1767 rcu_read_lock();
1768 /* Add channel to the channel_ht which owns the channel_infos. */
1769 cds_lfht_add(state->channels_ht,
8abe313a 1770 hash_channel_key(&new_channel_info->key),
ab0ee2ca
JG
1771 &new_channel_info->channels_ht_node);
1772 /*
1773 * Add the list of triggers associated with this channel to the
1774 * channel_triggers_ht.
1775 */
1776 cds_lfht_add(state->channel_triggers_ht,
8abe313a 1777 hash_channel_key(&new_channel_info->key),
ab0ee2ca
JG
1778 &channel_trigger_list->channel_triggers_ht_node);
1779 rcu_read_unlock();
1eee26c5 1780 session_info_put(session_info);
ab0ee2ca
JG
1781 *cmd_result = LTTNG_OK;
1782 return 0;
1783error:
ab0ee2ca 1784 channel_info_destroy(new_channel_info);
8abe313a 1785 session_info_put(session_info);
ab0ee2ca
JG
1786 return 1;
1787}
1788
83b934ad
MD
1789static
1790void free_channel_trigger_list_rcu(struct rcu_head *node)
1791{
1792 free(caa_container_of(node, struct lttng_channel_trigger_list,
1793 rcu_node));
1794}
1795
1796static
1797void free_channel_state_sample_rcu(struct rcu_head *node)
1798{
1799 free(caa_container_of(node, struct channel_state_sample,
1800 rcu_node));
1801}
1802
ab0ee2ca
JG
1803static
1804int handle_notification_thread_command_remove_channel(
1805 struct notification_thread_state *state,
1806 uint64_t channel_key, enum lttng_domain_type domain,
1807 enum lttng_error_code *cmd_result)
1808{
1809 struct cds_lfht_node *node;
1810 struct cds_lfht_iter iter;
1811 struct lttng_channel_trigger_list *trigger_list;
1812 struct lttng_trigger_list_element *trigger_list_element, *tmp;
1813 struct channel_key key = { .key = channel_key, .domain = domain };
1814 struct channel_info *channel_info;
1815
1816 DBG("[notification-thread] Removing channel key = %" PRIu64 " in %s domain",
526200e4 1817 channel_key, lttng_domain_type_str(domain));
ab0ee2ca
JG
1818
1819 rcu_read_lock();
1820
1821 cds_lfht_lookup(state->channel_triggers_ht,
1822 hash_channel_key(&key),
1823 match_channel_trigger_list,
1824 &key,
1825 &iter);
1826 node = cds_lfht_iter_get_node(&iter);
1827 /*
1828 * There is a severe internal error if we are being asked to remove a
1829 * channel that doesn't exist.
1830 */
1831 if (!node) {
1832 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1833 goto end;
1834 }
1835
1836 /* Free the list of triggers associated with this channel. */
1837 trigger_list = caa_container_of(node, struct lttng_channel_trigger_list,
1838 channel_triggers_ht_node);
1839 cds_list_for_each_entry_safe(trigger_list_element, tmp,
1840 &trigger_list->list, node) {
1841 cds_list_del(&trigger_list_element->node);
1842 free(trigger_list_element);
1843 }
1844 cds_lfht_del(state->channel_triggers_ht, node);
83b934ad 1845 call_rcu(&trigger_list->rcu_node, free_channel_trigger_list_rcu);
ab0ee2ca
JG
1846
1847 /* Free sampled channel state. */
1848 cds_lfht_lookup(state->channel_state_ht,
1849 hash_channel_key(&key),
1850 match_channel_state_sample,
1851 &key,
1852 &iter);
1853 node = cds_lfht_iter_get_node(&iter);
1854 /*
1855 * This is expected to be NULL if the channel is destroyed before we
1856 * received a sample.
1857 */
1858 if (node) {
1859 struct channel_state_sample *sample = caa_container_of(node,
1860 struct channel_state_sample,
1861 channel_state_ht_node);
1862
1863 cds_lfht_del(state->channel_state_ht, node);
83b934ad 1864 call_rcu(&sample->rcu_node, free_channel_state_sample_rcu);
ab0ee2ca
JG
1865 }
1866
1867 /* Remove the channel from the channels_ht and free it. */
1868 cds_lfht_lookup(state->channels_ht,
1869 hash_channel_key(&key),
1870 match_channel_info,
1871 &key,
1872 &iter);
1873 node = cds_lfht_iter_get_node(&iter);
1874 assert(node);
1875 channel_info = caa_container_of(node, struct channel_info,
1876 channels_ht_node);
1877 cds_lfht_del(state->channels_ht, node);
1878 channel_info_destroy(channel_info);
1879end:
1880 rcu_read_unlock();
1881 *cmd_result = LTTNG_OK;
1882 return 0;
1883}
1884
0ca52944 1885static
ed327204 1886int handle_notification_thread_command_session_rotation(
0ca52944 1887 struct notification_thread_state *state,
ed327204
JG
1888 enum notification_thread_command_type cmd_type,
1889 const char *session_name, uid_t session_uid, gid_t session_gid,
1890 uint64_t trace_archive_chunk_id,
1891 struct lttng_trace_archive_location *location,
1892 enum lttng_error_code *_cmd_result)
0ca52944 1893{
ed327204
JG
1894 int ret = 0;
1895 enum lttng_error_code cmd_result = LTTNG_OK;
1896 struct lttng_session_trigger_list *trigger_list;
1897 struct lttng_trigger_list_element *trigger_list_element;
1898 struct session_info *session_info;
f2b3ef9f 1899 const struct lttng_credentials session_creds = {
ff588497
JR
1900 .uid = LTTNG_OPTIONAL_INIT_VALUE(session_uid),
1901 .gid = LTTNG_OPTIONAL_INIT_VALUE(session_gid),
f2b3ef9f 1902 };
0ca52944 1903
ed327204
JG
1904 rcu_read_lock();
1905
1906 session_info = find_or_create_session_info(state, session_name,
1907 session_uid, session_gid);
1908 if (!session_info) {
a9577b76 1909 /* Allocation error or an internal error occurred. */
ed327204
JG
1910 ret = -1;
1911 cmd_result = LTTNG_ERR_NOMEM;
1912 goto end;
1913 }
1914
1915 session_info->rotation.ongoing =
1916 cmd_type == NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING;
1917 session_info->rotation.id = trace_archive_chunk_id;
1918 trigger_list = get_session_trigger_list(state, session_name);
1919 if (!trigger_list) {
1920 DBG("[notification-thread] No triggers applying to session \"%s\" found",
1921 session_name);
1922 goto end;
1923 }
1924
1925 cds_list_for_each_entry(trigger_list_element, &trigger_list->list,
1926 node) {
1927 const struct lttng_condition *condition;
f2b3ef9f 1928 struct lttng_trigger *trigger;
ed327204
JG
1929 struct notification_client_list *client_list;
1930 struct lttng_evaluation *evaluation = NULL;
1931 enum lttng_condition_type condition_type;
f2b3ef9f 1932 enum action_executor_status executor_status;
ed327204
JG
1933
1934 trigger = trigger_list_element->trigger;
1935 condition = lttng_trigger_get_const_condition(trigger);
1936 assert(condition);
1937 condition_type = lttng_condition_get_type(condition);
1938
1939 if (condition_type == LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING &&
1940 cmd_type != NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING) {
1941 continue;
1942 } else if (condition_type == LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED &&
1943 cmd_type != NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED) {
1944 continue;
1945 }
1946
ed327204 1947 client_list = get_client_list_from_condition(state, condition);
ed327204
JG
1948 if (cmd_type == NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING) {
1949 evaluation = lttng_evaluation_session_rotation_ongoing_create(
1950 trace_archive_chunk_id);
1951 } else {
1952 evaluation = lttng_evaluation_session_rotation_completed_create(
1953 trace_archive_chunk_id, location);
1954 }
1955
1956 if (!evaluation) {
1957 /* Internal error */
1958 ret = -1;
1959 cmd_result = LTTNG_ERR_UNK;
505b2d90 1960 goto put_list;
ed327204
JG
1961 }
1962
f2b3ef9f
JG
1963 /*
1964 * Ownership of `evaluation` transferred to the action executor
1965 * no matter the result.
1966 */
72365501
JR
1967 executor_status = action_executor_enqueue_trigger(
1968 state->executor, trigger, evaluation,
1969 &session_creds, client_list);
f2b3ef9f
JG
1970 evaluation = NULL;
1971 switch (executor_status) {
1972 case ACTION_EXECUTOR_STATUS_OK:
1973 break;
1974 case ACTION_EXECUTOR_STATUS_ERROR:
1975 case ACTION_EXECUTOR_STATUS_INVALID:
1976 /*
1977 * TODO Add trigger identification (name/id) when
1978 * it is added to the API.
1979 */
1980 ERR("Fatal error occurred while enqueuing action associated with session rotation trigger");
1981 ret = -1;
1982 goto put_list;
1983 case ACTION_EXECUTOR_STATUS_OVERFLOW:
1984 /*
1985 * TODO Add trigger identification (name/id) when
1986 * it is added to the API.
1987 *
1988 * Not a fatal error.
1989 */
1990 WARN("No space left when enqueuing action associated with session rotation trigger");
1991 ret = 0;
1992 goto put_list;
1993 default:
1994 abort();
1995 }
1996
505b2d90
JG
1997put_list:
1998 notification_client_list_put(client_list);
ed327204 1999 if (caa_unlikely(ret)) {
505b2d90 2000 break;
ed327204
JG
2001 }
2002 }
2003end:
2004 session_info_put(session_info);
2005 *_cmd_result = cmd_result;
2006 rcu_read_unlock();
2007 return ret;
0ca52944
JG
2008}
2009
d02d7404
JR
2010static
2011int handle_notification_thread_command_add_tracer_event_source(
2012 struct notification_thread_state *state,
2013 int tracer_event_source_fd,
2014 enum lttng_domain_type domain_type,
2015 enum lttng_error_code *_cmd_result)
2016{
2017 int ret = 0;
2018 enum lttng_error_code cmd_result = LTTNG_OK;
2019 struct notification_event_tracer_event_source_element *element = NULL;
2020
2021 element = zmalloc(sizeof(*element));
2022 if (!element) {
2023 cmd_result = LTTNG_ERR_NOMEM;
2024 ret = -1;
2025 goto end;
2026 }
2027
d02d7404
JR
2028 element->fd = tracer_event_source_fd;
2029 element->domain = domain_type;
2030
2031 cds_list_add(&element->node, &state->tracer_event_sources_list);
2032
2033 DBG3("[notification-thread] Adding tracer event source fd to poll set: tracer_event_source_fd = %d, domain = '%s'",
2034 tracer_event_source_fd,
2035 lttng_domain_type_str(domain_type));
2036
2037 /* Adding the read side pipe to the event poll. */
2038 ret = lttng_poll_add(&state->events, tracer_event_source_fd, LPOLLIN | LPOLLERR);
2039 if (ret < 0) {
2040 ERR("[notification-thread] Failed to add tracer event source to poll set: tracer_event_source_fd = %d, domain = '%s'",
2041 tracer_event_source_fd,
2042 lttng_domain_type_str(element->domain));
2043 cds_list_del(&element->node);
2044 free(element);
2045 goto end;
2046 }
2047
2048 element->is_fd_in_poll_set = true;
2049
2050end:
2051 *_cmd_result = cmd_result;
2052 return ret;
2053}
2054
8b524060
FD
2055static
2056int drain_event_notifier_notification_pipe(
2057 struct notification_thread_state *state,
2058 int pipe, enum lttng_domain_type domain)
2059{
2060 struct lttng_poll_event events = {0};
2061 int ret;
2062
2063 ret = lttng_poll_create(&events, 1, LTTNG_CLOEXEC);
2064 if (ret < 0) {
2065 ERR("[notification-thread] Error creating lttng_poll_event");
2066 goto end;
2067 }
2068
2069 ret = lttng_poll_add(&events, pipe, LPOLLIN);
2070 if (ret < 0) {
2071 ERR("[notification-thread] Error adding fd event notifier notification pipe to lttng_poll_event: fd = %d",
2072 pipe);
2073 goto end;
2074 }
2075
2076 while (true) {
2077 /*
2078 * Continue to consume notifications as long as there are new
2079 * ones coming in. The tracer has been asked to stop producing
2080 * them.
2081 *
2082 * LPOLLIN is explicitly checked since LPOLLHUP is implicitly
2083 * monitored (on Linux, at least) and will be returned when
2084 * the pipe is closed but empty.
2085 */
2086 ret = lttng_poll_wait_interruptible(&events, 0);
a17b133b 2087 if (ret == 0 || (LTTNG_POLL_GETEV(&events, 0) & LPOLLIN) == 0) {
8b524060
FD
2088 /* No more notification to be read on this pipe. */
2089 ret = 0;
2090 goto end;
2091 } else if (ret < 0) {
2092 PERROR("Failed on lttng_poll_wait_interruptible() call");
2093 ret = -1;
2094 goto end;
2095 }
2096
2097 ret = handle_one_event_notifier_notification(state, pipe, domain);
2098 if (ret) {
2099 ERR("[notification-thread] Error consuming an event notifier notification from pipe: fd = %d",
2100 pipe);
2101 }
2102 }
2103end:
2104 lttng_poll_clean(&events);
2105 return ret;
2106}
2107
d02d7404
JR
2108static
2109int handle_notification_thread_command_remove_tracer_event_source(
2110 struct notification_thread_state *state,
2111 int tracer_event_source_fd,
2112 enum lttng_error_code *_cmd_result)
2113{
2114 int ret = 0;
2fb5e9b7 2115 bool found = false;
d02d7404
JR
2116 enum lttng_error_code cmd_result = LTTNG_OK;
2117 struct notification_event_tracer_event_source_element *source_element = NULL, *tmp;
2118
2119 cds_list_for_each_entry_safe(source_element, tmp,
2120 &state->tracer_event_sources_list, node) {
2121 if (source_element->fd != tracer_event_source_fd) {
2122 continue;
2123 }
2124
2125 DBG("[notification-thread] Removed tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2126 tracer_event_source_fd,
2127 lttng_domain_type_str(source_element->domain));
2128 cds_list_del(&source_element->node);
2fb5e9b7 2129 found = true;
d02d7404
JR
2130 break;
2131 }
2132
2fb5e9b7
JG
2133 if (!found) {
2134 /*
2135 * This is temporarily allowed since the poll activity set is
2136 * not properly cleaned-up for the moment. This is adressed in
2137 * an upcoming fix.
2138 */
2139 source_element = NULL;
2140 goto end;
2141 }
d02d7404
JR
2142
2143 if (!source_element->is_fd_in_poll_set) {
2144 /* Skip the poll set removal. */
2145 goto end;
2146 }
2147
2148 DBG3("[notification-thread] Removing tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2149 tracer_event_source_fd,
2150 lttng_domain_type_str(source_element->domain));
2151
2152 /* Removing the fd from the event poll set. */
2153 ret = lttng_poll_del(&state->events, tracer_event_source_fd);
2154 if (ret < 0) {
2155 ERR("[notification-thread] Failed to remove tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2156 tracer_event_source_fd,
2157 lttng_domain_type_str(source_element->domain));
2158 cmd_result = LTTNG_ERR_FATAL;
2159 goto end;
2160 }
2161
173900f6
JG
2162 source_element->is_fd_in_poll_set = false;
2163
8b524060
FD
2164 ret = drain_event_notifier_notification_pipe(state, tracer_event_source_fd,
2165 source_element->domain);
2166 if (ret) {
2167 ERR("[notification-thread] Error draining event notifier notification: tracer_event_source_fd = %d, domain = %s",
2168 tracer_event_source_fd,
2169 lttng_domain_type_str(source_element->domain));
2170 cmd_result = LTTNG_ERR_FATAL;
2171 goto end;
2172 }
2173
2174 /*
2175 * The drain_event_notifier_notification_pipe() call might have read
2176 * data from an fd that we received in event in the latest _poll_wait()
2177 * call. Make sure the thread call poll_wait() again to ensure we have
2178 * a clean state.
2179 */
2180 state->restart_poll = true;
2181
d02d7404
JR
2182end:
2183 free(source_element);
2184 *_cmd_result = cmd_result;
2185 return ret;
2186}
2187
2188int handle_notification_thread_remove_tracer_event_source_no_result(
2189 struct notification_thread_state *state,
2190 int tracer_event_source_fd)
2191{
2192 int ret;
2193 enum lttng_error_code cmd_result;
2194
2195 ret = handle_notification_thread_command_remove_tracer_event_source(
2196 state, tracer_event_source_fd, &cmd_result);
2197 (void) cmd_result;
2198 return ret;
2199}
2200
fbc9f37d
JR
2201static int handle_notification_thread_command_list_triggers(
2202 struct notification_thread_handle *handle,
2203 struct notification_thread_state *state,
2204 uid_t client_uid,
2205 struct lttng_triggers **triggers,
2206 enum lttng_error_code *_cmd_result)
2207{
2208 int ret = 0;
2209 enum lttng_error_code cmd_result = LTTNG_OK;
2210 struct cds_lfht_iter iter;
2211 struct lttng_trigger_ht_element *trigger_ht_element;
2212 struct lttng_triggers *local_triggers = NULL;
2213 const struct lttng_credentials *creds;
2214
2215 rcu_read_lock();
2216
2217 local_triggers = lttng_triggers_create();
2218 if (!local_triggers) {
2219 /* Not a fatal error. */
2220 cmd_result = LTTNG_ERR_NOMEM;
2221 goto end;
2222 }
2223
2224 cds_lfht_for_each_entry(state->triggers_ht, &iter,
2225 trigger_ht_element, node) {
2226 /*
2227 * Only return the triggers to which the client has access.
2228 * The root user has visibility over all triggers.
2229 */
2230 creds = lttng_trigger_get_credentials(trigger_ht_element->trigger);
2231 if (client_uid != lttng_credentials_get_uid(creds) && client_uid != 0) {
2232 continue;
2233 }
2234
2235 ret = lttng_triggers_add(local_triggers,
2236 trigger_ht_element->trigger);
2237 if (ret < 0) {
2238 /* Not a fatal error. */
2239 ret = 0;
2240 cmd_result = LTTNG_ERR_NOMEM;
2241 goto end;
2242 }
2243 }
2244
2245 /* Transferring ownership to the caller. */
2246 *triggers = local_triggers;
2247 local_triggers = NULL;
2248
2249end:
2250 rcu_read_unlock();
2251 lttng_triggers_destroy(local_triggers);
2252 *_cmd_result = cmd_result;
2253 return ret;
2254}
2255
8790759c
FD
2256static inline void get_trigger_info_for_log(const struct lttng_trigger *trigger,
2257 const char **trigger_name,
2258 uid_t *trigger_owner_uid)
2259{
2260 enum lttng_trigger_status trigger_status;
2261
2262 trigger_status = lttng_trigger_get_name(trigger, trigger_name);
2263 switch (trigger_status) {
2264 case LTTNG_TRIGGER_STATUS_OK:
2265 break;
2266 case LTTNG_TRIGGER_STATUS_UNSET:
0efb2ad7 2267 *trigger_name = "(anonymous)";
8790759c
FD
2268 break;
2269 default:
2270 abort();
2271 }
2272
2273 trigger_status = lttng_trigger_get_owner_uid(trigger,
2274 trigger_owner_uid);
2275 assert(trigger_status == LTTNG_TRIGGER_STATUS_OK);
2276}
2277
2278static int handle_notification_thread_command_get_trigger(
2279 struct notification_thread_state *state,
2280 const struct lttng_trigger *trigger,
2281 struct lttng_trigger **registered_trigger,
2282 enum lttng_error_code *_cmd_result)
2283{
2284 int ret = -1;
2285 struct cds_lfht_iter iter;
2286 struct lttng_trigger_ht_element *trigger_ht_element;
2287 enum lttng_error_code cmd_result = LTTNG_ERR_TRIGGER_NOT_FOUND;
2288 const char *trigger_name;
2289 uid_t trigger_owner_uid;
2290
2291 rcu_read_lock();
2292
2293 cds_lfht_for_each_entry(
2294 state->triggers_ht, &iter, trigger_ht_element, node) {
2295 if (lttng_trigger_is_equal(
2296 trigger, trigger_ht_element->trigger)) {
2297 /* Take one reference on the return trigger. */
2298 *registered_trigger = trigger_ht_element->trigger;
2299 lttng_trigger_get(*registered_trigger);
2300 ret = 0;
2301 cmd_result = LTTNG_OK;
2302 goto end;
2303 }
2304 }
2305
2306 /* Not a fatal error if the trigger is not found. */
2307 get_trigger_info_for_log(trigger, &trigger_name, &trigger_owner_uid);
87661585 2308 DBG("Failed to retrieve registered version of trigger: trigger name = '%s', trigger owner uid = %d",
8790759c
FD
2309 trigger_name, (int) trigger_owner_uid);
2310
2311 ret = 0;
2312
2313end:
2314 rcu_read_unlock();
2315 *_cmd_result = cmd_result;
2316 return ret;
2317}
2318
1da26331 2319static
959e3c66 2320bool condition_is_supported(struct lttng_condition *condition)
1da26331 2321{
959e3c66 2322 bool is_supported;
1da26331
JG
2323
2324 switch (lttng_condition_get_type(condition)) {
2325 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
2326 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
2327 {
959e3c66 2328 int ret;
1da26331
JG
2329 enum lttng_domain_type domain;
2330
2331 ret = lttng_condition_buffer_usage_get_domain_type(condition,
2332 &domain);
959e3c66 2333 assert(ret == 0);
1da26331
JG
2334
2335 if (domain != LTTNG_DOMAIN_KERNEL) {
959e3c66 2336 is_supported = true;
1da26331
JG
2337 goto end;
2338 }
2339
2340 /*
2341 * Older kernel tracers don't expose the API to monitor their
2342 * buffers. Therefore, we reject triggers that require that
2343 * mechanism to be available to be evaluated.
959e3c66
JR
2344 *
2345 * Assume unsupported on error.
1da26331 2346 */
959e3c66
JR
2347 is_supported = kernel_supports_ring_buffer_snapshot_sample_positions() == 1;
2348 break;
2349 }
8dbb86b8 2350 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES:
959e3c66
JR
2351 {
2352 const struct lttng_event_rule *event_rule;
2353 enum lttng_domain_type domain;
2354 const enum lttng_condition_status status =
8dbb86b8 2355 lttng_condition_event_rule_matches_get_rule(
959e3c66
JR
2356 condition, &event_rule);
2357
2358 assert(status == LTTNG_CONDITION_STATUS_OK);
2359
2360 domain = lttng_event_rule_get_domain_type(event_rule);
2361 if (domain != LTTNG_DOMAIN_KERNEL) {
2362 is_supported = true;
2363 goto end;
2364 }
2365
2366 /*
2367 * Older kernel tracers can't emit notification. Therefore, we
2368 * reject triggers that require that mechanism to be available
2369 * to be evaluated.
2370 *
2371 * Assume unsupported on error.
2372 */
2373 is_supported = kernel_supports_event_notifiers() == 1;
1da26331
JG
2374 break;
2375 }
2376 default:
959e3c66 2377 is_supported = true;
1da26331
JG
2378 }
2379end:
959e3c66 2380 return is_supported;
1da26331
JG
2381}
2382
51eab943
JG
2383/* Must be called with RCU read lock held. */
2384static
f2b3ef9f 2385int bind_trigger_to_matching_session(struct lttng_trigger *trigger,
51eab943
JG
2386 struct notification_thread_state *state)
2387{
2388 int ret = 0;
51eab943
JG
2389 const struct lttng_condition *condition;
2390 const char *session_name;
2391 struct lttng_session_trigger_list *trigger_list;
2392
2393 condition = lttng_trigger_get_const_condition(trigger);
2394 switch (lttng_condition_get_type(condition)) {
2395 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
2396 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
2397 {
2398 enum lttng_condition_status status;
2399
2400 status = lttng_condition_session_rotation_get_session_name(
2401 condition, &session_name);
2402 if (status != LTTNG_CONDITION_STATUS_OK) {
2403 ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
2404 ret = -1;
2405 goto end;
2406 }
2407 break;
2408 }
2409 default:
2410 ret = -1;
2411 goto end;
2412 }
2413
ed327204
JG
2414 trigger_list = get_session_trigger_list(state, session_name);
2415 if (!trigger_list) {
51eab943
JG
2416 DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
2417 session_name);
2418 goto end;
51eab943 2419
ed327204 2420 }
51eab943
JG
2421
2422 DBG("[notification-thread] Newly registered trigger bound to session \"%s\"",
2423 session_name);
2424 ret = lttng_session_trigger_list_add(trigger_list, trigger);
2425end:
2426 return ret;
2427}
2428
2429/* Must be called with RCU read lock held. */
2430static
f2b3ef9f 2431int bind_trigger_to_matching_channels(struct lttng_trigger *trigger,
51eab943
JG
2432 struct notification_thread_state *state)
2433{
2434 int ret = 0;
2435 struct cds_lfht_node *node;
2436 struct cds_lfht_iter iter;
2437 struct channel_info *channel;
2438
2439 cds_lfht_for_each_entry(state->channels_ht, &iter, channel,
2440 channels_ht_node) {
2441 struct lttng_trigger_list_element *trigger_list_element;
2442 struct lttng_channel_trigger_list *trigger_list;
a5d64ae7 2443 struct cds_lfht_iter lookup_iter;
51eab943
JG
2444
2445 if (!trigger_applies_to_channel(trigger, channel)) {
2446 continue;
2447 }
2448
2449 cds_lfht_lookup(state->channel_triggers_ht,
2450 hash_channel_key(&channel->key),
2451 match_channel_trigger_list,
2452 &channel->key,
a5d64ae7
MD
2453 &lookup_iter);
2454 node = cds_lfht_iter_get_node(&lookup_iter);
51eab943
JG
2455 assert(node);
2456 trigger_list = caa_container_of(node,
2457 struct lttng_channel_trigger_list,
2458 channel_triggers_ht_node);
2459
2460 trigger_list_element = zmalloc(sizeof(*trigger_list_element));
2461 if (!trigger_list_element) {
2462 ret = -1;
2463 goto end;
2464 }
2465 CDS_INIT_LIST_HEAD(&trigger_list_element->node);
2466 trigger_list_element->trigger = trigger;
2467 cds_list_add(&trigger_list_element->node, &trigger_list->list);
2468 DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"",
2469 channel->name);
2470 }
2471end:
2472 return ret;
2473}
2474
f2b3ef9f
JG
2475static
2476bool is_trigger_action_notify(const struct lttng_trigger *trigger)
2477{
2478 bool is_notify = false;
2479 unsigned int i, count;
2480 enum lttng_action_status action_status;
2481 const struct lttng_action *action =
2482 lttng_trigger_get_const_action(trigger);
2483 enum lttng_action_type action_type;
2484
2485 assert(action);
17182cfd 2486 action_type = lttng_action_get_type(action);
f2b3ef9f
JG
2487 if (action_type == LTTNG_ACTION_TYPE_NOTIFY) {
2488 is_notify = true;
2489 goto end;
2490 } else if (action_type != LTTNG_ACTION_TYPE_GROUP) {
2491 goto end;
2492 }
2493
702f26c8 2494 action_status = lttng_action_list_get_count(action, &count);
f2b3ef9f
JG
2495 assert(action_status == LTTNG_ACTION_STATUS_OK);
2496
2497 for (i = 0; i < count; i++) {
2498 const struct lttng_action *inner_action =
702f26c8 2499 lttng_action_list_get_at_index(
f2b3ef9f
JG
2500 action, i);
2501
17182cfd 2502 action_type = lttng_action_get_type(inner_action);
f2b3ef9f
JG
2503 if (action_type == LTTNG_ACTION_TYPE_NOTIFY) {
2504 is_notify = true;
2505 goto end;
2506 }
2507 }
2508
2509end:
2510 return is_notify;
2511}
2512
242388e4
JR
2513static bool trigger_name_taken(struct notification_thread_state *state,
2514 const struct lttng_trigger *trigger)
2515{
2516 struct cds_lfht_iter iter;
2517
2518 /*
2519 * No duplicata is allowed in the triggers_by_name_uid_ht.
2520 * The match is done against the trigger name and uid.
2521 */
2522 cds_lfht_lookup(state->triggers_by_name_uid_ht,
2523 hash_trigger_by_name_uid(trigger),
2524 match_trigger_by_name_uid,
2525 trigger,
2526 &iter);
2527 return !!cds_lfht_iter_get_node(&iter);
2528}
2529
2530static
2531enum lttng_error_code generate_trigger_name(
2532 struct notification_thread_state *state,
2533 struct lttng_trigger *trigger, const char **name)
2534{
2535 enum lttng_error_code ret_code = LTTNG_OK;
2536 bool taken = false;
2537 enum lttng_trigger_status status;
2538
2539 do {
2540 const int ret = lttng_trigger_generate_name(trigger,
2541 state->trigger_id.name_offset++);
2542 if (ret) {
2543 /* The only reason this can fail right now. */
2544 ret_code = LTTNG_ERR_NOMEM;
2545 break;
2546 }
2547
2548 status = lttng_trigger_get_name(trigger, name);
2549 assert(status == LTTNG_TRIGGER_STATUS_OK);
2550
2551 taken = trigger_name_taken(state, trigger);
2552 } while (taken || state->trigger_id.name_offset == UINT64_MAX);
2553
2554 return ret_code;
2555}
2556
2758e38b
FD
2557static inline
2558void notif_thread_state_remove_trigger_ht_elem(
2559 struct notification_thread_state *state,
2560 struct lttng_trigger_ht_element *trigger_ht_element)
2561{
2562 assert(state);
2563 assert(trigger_ht_element);
2564
2565 cds_lfht_del(state->triggers_ht, &trigger_ht_element->node);
2566 cds_lfht_del(state->triggers_by_name_uid_ht, &trigger_ht_element->node_by_name_uid);
2567}
2568
6487ad53
FD
2569static
2570enum lttng_error_code setup_tracer_notifier(
2571 struct notification_thread_state *state,
2572 struct lttng_trigger *trigger)
2573{
2574 enum lttng_error_code ret;
2575 enum event_notifier_error_accounting_status error_accounting_status;
2576 struct cds_lfht_node *node;
2577 uint64_t error_counter_index = 0;
2578 struct lttng_condition *condition = lttng_trigger_get_condition(trigger);
2579 struct notification_trigger_tokens_ht_element *trigger_tokens_ht_element = NULL;
2580
2581 trigger_tokens_ht_element = zmalloc(sizeof(*trigger_tokens_ht_element));
2582 if (!trigger_tokens_ht_element) {
2583 ret = LTTNG_ERR_NOMEM;
2584 goto end;
2585 }
2586
2587 /* Add trigger token to the trigger_tokens_ht. */
2588 cds_lfht_node_init(&trigger_tokens_ht_element->node);
2589 trigger_tokens_ht_element->token = LTTNG_OPTIONAL_GET(trigger->tracer_token);
2590 trigger_tokens_ht_element->trigger = trigger;
2591
2592 node = cds_lfht_add_unique(state->trigger_tokens_ht,
2593 hash_key_u64(&trigger_tokens_ht_element->token, lttng_ht_seed),
2594 match_trigger_token,
2595 &trigger_tokens_ht_element->token,
2596 &trigger_tokens_ht_element->node);
2597 if (node != &trigger_tokens_ht_element->node) {
2598 ret = LTTNG_ERR_TRIGGER_EXISTS;
2599 goto error_free_ht_element;
2600 }
2601
2602 error_accounting_status = event_notifier_error_accounting_register_event_notifier(
2603 trigger, &error_counter_index);
2604 if (error_accounting_status != EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK) {
2605 if (error_accounting_status == EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NO_INDEX_AVAILABLE) {
2606 DBG("Trigger group error accounting counter full.");
2607 ret = LTTNG_ERR_EVENT_NOTIFIER_ERROR_ACCOUNTING_FULL;
2608 } else {
2609 ERR("Error registering trigger for error accounting");
2610 ret = LTTNG_ERR_EVENT_NOTIFIER_REGISTRATION;
2611 }
2612
2613 goto error_remove_ht_element;
2614 }
2615
8dbb86b8 2616 lttng_condition_event_rule_matches_set_error_counter_index(
6487ad53
FD
2617 condition, error_counter_index);
2618
2619 ret = LTTNG_OK;
2620 goto end;
2621
2622error_remove_ht_element:
2623 cds_lfht_del(state->trigger_tokens_ht, &trigger_tokens_ht_element->node);
2624error_free_ht_element:
2625 free(trigger_tokens_ht_element);
2626end:
2627 return ret;
2628}
2629
ab0ee2ca 2630/*
f2b3ef9f 2631 * FIXME A client's credentials are not checked when registering a trigger.
ab0ee2ca 2632 *
1da26331 2633 * The effects of this are benign since:
ab0ee2ca 2634 * - The client will succeed in registering the trigger, as it is valid,
51eab943 2635 * - The trigger will, internally, be bound to the channel/session,
ab0ee2ca
JG
2636 * - The notifications will not be sent since the client's credentials
2637 * are checked against the channel at that moment.
1da26331
JG
2638 *
2639 * If this function returns a non-zero value, it means something is
50ca7858 2640 * fundamentally broken and the whole subsystem/thread will be torn down.
1da26331
JG
2641 *
2642 * If a non-fatal error occurs, just set the cmd_result to the appropriate
2643 * error code.
ab0ee2ca
JG
2644 */
2645static
2646int handle_notification_thread_command_register_trigger(
8abe313a
JG
2647 struct notification_thread_state *state,
2648 struct lttng_trigger *trigger,
0efb2ad7 2649 bool is_trigger_anonymous,
8abe313a 2650 enum lttng_error_code *cmd_result)
ab0ee2ca
JG
2651{
2652 int ret = 0;
2653 struct lttng_condition *condition;
ab0ee2ca
JG
2654 struct notification_client_list *client_list = NULL;
2655 struct lttng_trigger_ht_element *trigger_ht_element = NULL;
ab0ee2ca 2656 struct cds_lfht_node *node;
242388e4 2657 const char* trigger_name;
ab0ee2ca 2658 bool free_trigger = true;
f2b3ef9f
JG
2659 struct lttng_evaluation *evaluation = NULL;
2660 struct lttng_credentials object_creds;
ff588497
JR
2661 uid_t object_uid;
2662 gid_t object_gid;
f2b3ef9f 2663 enum action_executor_status executor_status;
e6887944
JR
2664 const uint64_t trigger_tracer_token =
2665 state->trigger_id.next_tracer_token++;
ab0ee2ca
JG
2666
2667 rcu_read_lock();
2668
e6887944
JR
2669 /* Set the trigger's tracer token. */
2670 lttng_trigger_set_tracer_token(trigger, trigger_tracer_token);
2671
0efb2ad7
JG
2672 if (!is_trigger_anonymous) {
2673 if (lttng_trigger_get_name(trigger, &trigger_name) ==
2674 LTTNG_TRIGGER_STATUS_UNSET) {
2675 const enum lttng_error_code ret_code =
2676 generate_trigger_name(state, trigger,
2677 &trigger_name);
242388e4 2678
0efb2ad7
JG
2679 if (ret_code != LTTNG_OK) {
2680 /* Fatal error. */
2681 ret = -1;
2682 *cmd_result = ret_code;
2683 goto error;
2684 }
2685 } else if (trigger_name_taken(state, trigger)) {
2686 /* Not a fatal error. */
2687 *cmd_result = LTTNG_ERR_TRIGGER_EXISTS;
2688 ret = 0;
242388e4
JR
2689 goto error;
2690 }
0efb2ad7
JG
2691 } else {
2692 trigger_name = "(anonymous)";
242388e4
JR
2693 }
2694
ab0ee2ca 2695 condition = lttng_trigger_get_condition(trigger);
1da26331
JG
2696 assert(condition);
2697
959e3c66
JR
2698 /* Some conditions require tracers to implement a minimal ABI version. */
2699 if (!condition_is_supported(condition)) {
1da26331
JG
2700 *cmd_result = LTTNG_ERR_NOT_SUPPORTED;
2701 goto error;
1da26331
JG
2702 }
2703
ab0ee2ca
JG
2704 trigger_ht_element = zmalloc(sizeof(*trigger_ht_element));
2705 if (!trigger_ht_element) {
2706 ret = -1;
2707 goto error;
2708 }
2709
2710 /* Add trigger to the trigger_ht. */
2711 cds_lfht_node_init(&trigger_ht_element->node);
242388e4 2712 cds_lfht_node_init(&trigger_ht_element->node_by_name_uid);
ab0ee2ca
JG
2713 trigger_ht_element->trigger = trigger;
2714
2715 node = cds_lfht_add_unique(state->triggers_ht,
2716 lttng_condition_hash(condition),
242388e4
JR
2717 match_trigger,
2718 trigger,
ab0ee2ca
JG
2719 &trigger_ht_element->node);
2720 if (node != &trigger_ht_element->node) {
2721 /* Not a fatal error, simply report it to the client. */
2722 *cmd_result = LTTNG_ERR_TRIGGER_EXISTS;
2723 goto error_free_ht_element;
2724 }
2725
242388e4
JR
2726 node = cds_lfht_add_unique(state->triggers_by_name_uid_ht,
2727 hash_trigger_by_name_uid(trigger),
2728 match_trigger_by_name_uid,
2729 trigger,
2730 &trigger_ht_element->node_by_name_uid);
2731 if (node != &trigger_ht_element->node_by_name_uid) {
2732 /* Not a fatal error, simply report it to the client. */
2733 cds_lfht_del(state->triggers_ht, &trigger_ht_element->node);
2734 *cmd_result = LTTNG_ERR_TRIGGER_EXISTS;
2735 goto error_free_ht_element;
2736 }
2737
9c374932
JR
2738 /* From this point consider the trigger registered. */
2739 lttng_trigger_set_as_registered(trigger);
2740
6487ad53
FD
2741 /*
2742 * Some triggers might need a tracer notifier depending on its
2743 * condition and actions.
2744 */
2745 if (lttng_trigger_needs_tracer_notifier(trigger)) {
2746 enum lttng_error_code error_code;
e7c93cf9 2747
6487ad53
FD
2748 error_code = setup_tracer_notifier(state, trigger);
2749 if (error_code != LTTNG_OK) {
2758e38b
FD
2750 notif_thread_state_remove_trigger_ht_elem(state,
2751 trigger_ht_element);
6487ad53
FD
2752 if (error_code == LTTNG_ERR_NOMEM) {
2753 ret = -1;
2754 } else {
2755 *cmd_result = error_code;
2756 ret = 0;
90aa04a1
FD
2757 }
2758
6487ad53 2759 goto error_free_ht_element;
90aa04a1 2760 }
e7c93cf9
JR
2761 }
2762
ab0ee2ca
JG
2763 /*
2764 * The rest only applies to triggers that have a "notify" action.
2765 * It is not skipped as this is the only action type currently
2766 * supported.
2767 */
f2b3ef9f 2768 if (is_trigger_action_notify(trigger)) {
091fa780
FD
2769 /*
2770 * Find or create the client list of this condition. It may
2771 * already be present if another trigger is already registered
2772 * with the same condition.
2773 */
2774 client_list = get_client_list_from_condition(state, condition);
f2b3ef9f 2775 if (!client_list) {
091fa780
FD
2776 /*
2777 * No client list for this condition yet. We create new
2778 * one and build it up.
2779 */
2780 client_list = notification_client_list_create(state, condition);
2781 if (!client_list) {
2782 ERR("Error creating notification client list for trigger %s", trigger->name);
2783 goto error_free_ht_element;
f2b3ef9f 2784 }
ab0ee2ca 2785 }
f2b3ef9f 2786
091fa780
FD
2787 CDS_INIT_LIST_HEAD(&trigger_ht_element->client_list_trigger_node);
2788
2789 pthread_mutex_lock(&client_list->lock);
2790 cds_list_add(&trigger_ht_element->client_list_trigger_node, &client_list->triggers_list);
2791 pthread_mutex_unlock(&client_list->lock);
ab0ee2ca
JG
2792 }
2793
091fa780
FD
2794 /*
2795 * Ownership of the trigger and of its wrapper was transfered to
2796 * the triggers_ht. Same for token ht element if necessary.
2797 */
2798 trigger_ht_element = NULL;
2799 free_trigger = false;
2800
f82f93a1 2801 switch (get_condition_binding_object(condition)) {
51eab943
JG
2802 case LTTNG_OBJECT_TYPE_SESSION:
2803 /* Add the trigger to the list if it matches a known session. */
2804 ret = bind_trigger_to_matching_session(trigger, state);
2805 if (ret) {
091fa780 2806 goto error_free_ht_element;
ab0ee2ca 2807 }
f82f93a1 2808 break;
51eab943
JG
2809 case LTTNG_OBJECT_TYPE_CHANNEL:
2810 /*
2811 * Add the trigger to list of triggers bound to the channels
2812 * currently known.
2813 */
2814 ret = bind_trigger_to_matching_channels(trigger, state);
2815 if (ret) {
091fa780 2816 goto error_free_ht_element;
ab0ee2ca 2817 }
51eab943
JG
2818 break;
2819 case LTTNG_OBJECT_TYPE_NONE:
2820 break;
2821 default:
f2b3ef9f 2822 ERR("Unknown object type on which to bind a newly registered trigger was encountered");
51eab943 2823 ret = -1;
091fa780 2824 goto error_free_ht_element;
ab0ee2ca
JG
2825 }
2826
2ae99f0b 2827 /*
f2b3ef9f
JG
2828 * The new trigger's condition must be evaluated against the current
2829 * state.
2830 *
2831 * In the case of `notify` action, nothing preventing clients from
2832 * subscribing to a condition before the corresponding trigger is
2833 * registered, we have to evaluate this new condition right away.
2ae99f0b
JG
2834 *
2835 * At some point, we were waiting for the next "evaluation" (e.g. on
2836 * reception of a channel sample) to evaluate this new condition, but
2837 * that was broken.
2838 *
2839 * The reason it was broken is that waiting for the next sample
2840 * does not allow us to properly handle transitions for edge-triggered
2841 * conditions.
2842 *
2843 * Consider this example: when we handle a new channel sample, we
2844 * evaluate each conditions twice: once with the previous state, and
2845 * again with the newest state. We then use those two results to
2846 * determine whether a state change happened: a condition was false and
2847 * became true. If a state change happened, we have to notify clients.
2848 *
2849 * Now, if a client subscribes to a given notification and registers
2850 * a trigger *after* that subscription, we have to make sure the
2851 * condition is evaluated at this point while considering only the
2852 * current state. Otherwise, the next evaluation cycle may only see
2853 * that the evaluations remain the same (true for samples n-1 and n) and
2854 * the client will never know that the condition has been met.
2855 */
f2b3ef9f
JG
2856 switch (get_condition_binding_object(condition)) {
2857 case LTTNG_OBJECT_TYPE_SESSION:
2858 ret = evaluate_session_condition_for_client(condition, state,
ff588497
JR
2859 &evaluation, &object_uid,
2860 &object_gid);
bc8daafb
JG
2861 LTTNG_OPTIONAL_SET(&object_creds.uid, object_uid);
2862 LTTNG_OPTIONAL_SET(&object_creds.gid, object_gid);
b42ada90 2863 break;
f2b3ef9f
JG
2864 case LTTNG_OBJECT_TYPE_CHANNEL:
2865 ret = evaluate_channel_condition_for_client(condition, state,
ff588497
JR
2866 &evaluation, &object_uid,
2867 &object_gid);
bc8daafb
JG
2868 LTTNG_OPTIONAL_SET(&object_creds.uid, object_uid);
2869 LTTNG_OPTIONAL_SET(&object_creds.gid, object_gid);
f2b3ef9f
JG
2870 break;
2871 case LTTNG_OBJECT_TYPE_NONE:
2872 ret = 0;
242388e4 2873 break;
f2b3ef9f
JG
2874 case LTTNG_OBJECT_TYPE_UNKNOWN:
2875 default:
2876 ret = -1;
242388e4 2877 break;
f2b3ef9f
JG
2878 }
2879
2880 if (ret) {
2881 /* Fatal error. */
091fa780 2882 goto error_free_ht_element;
f2b3ef9f
JG
2883 }
2884
2885 DBG("Newly registered trigger's condition evaluated to %s",
2886 evaluation ? "true" : "false");
2887 if (!evaluation) {
2888 /* Evaluation yielded nothing. Normal exit. */
2889 ret = 0;
091fa780 2890 goto success;
2ae99f0b
JG
2891 }
2892
2893 /*
f2b3ef9f
JG
2894 * Ownership of `evaluation` transferred to the action executor
2895 * no matter the result.
2ae99f0b 2896 */
72365501
JR
2897 executor_status = action_executor_enqueue_trigger(state->executor,
2898 trigger, evaluation, &object_creds, client_list);
f2b3ef9f
JG
2899 evaluation = NULL;
2900 switch (executor_status) {
2901 case ACTION_EXECUTOR_STATUS_OK:
2902 break;
2903 case ACTION_EXECUTOR_STATUS_ERROR:
2904 case ACTION_EXECUTOR_STATUS_INVALID:
2905 /*
2906 * TODO Add trigger identification (name/id) when
2907 * it is added to the API.
2908 */
2909 ERR("Fatal error occurred while enqueuing action associated to newly registered trigger");
2910 ret = -1;
091fa780 2911 goto error_free_ht_element;
f2b3ef9f
JG
2912 case ACTION_EXECUTOR_STATUS_OVERFLOW:
2913 /*
2914 * TODO Add trigger identification (name/id) when
2915 * it is added to the API.
2916 *
2917 * Not a fatal error.
2918 */
2919 WARN("No space left when enqueuing action associated to newly registered trigger");
2920 ret = 0;
091fa780 2921 goto success;
f2b3ef9f
JG
2922 default:
2923 abort();
2924 }
2ae99f0b 2925
091fa780 2926success:
ab0ee2ca 2927 *cmd_result = LTTNG_OK;
e6887944
JR
2928 DBG("Registered trigger: name = `%s`, tracer token = %" PRIu64,
2929 trigger_name, trigger_tracer_token);
091fa780 2930 goto end;
505b2d90 2931
ab0ee2ca 2932error_free_ht_element:
242388e4
JR
2933 if (trigger_ht_element) {
2934 /* Delayed removal due to RCU constraint on delete. */
2935 call_rcu(&trigger_ht_element->rcu_node,
2936 free_lttng_trigger_ht_element_rcu);
2937 }
ab0ee2ca
JG
2938error:
2939 if (free_trigger) {
9c374932
JR
2940 /*
2941 * Other objects might have a reference to the trigger, mark it
2942 * as unregistered.
2943 */
2944 lttng_trigger_set_as_unregistered(trigger);
ab0ee2ca
JG
2945 lttng_trigger_destroy(trigger);
2946 }
091fa780 2947end:
ab0ee2ca
JG
2948 rcu_read_unlock();
2949 return ret;
2950}
2951
83b934ad
MD
2952static
2953void free_lttng_trigger_ht_element_rcu(struct rcu_head *node)
2954{
2955 free(caa_container_of(node, struct lttng_trigger_ht_element,
2956 rcu_node));
2957}
2958
e7c93cf9
JR
2959static
2960void free_notification_trigger_tokens_ht_element_rcu(struct rcu_head *node)
2961{
2962 free(caa_container_of(node, struct notification_trigger_tokens_ht_element,
2963 rcu_node));
2964}
2965
6487ad53
FD
2966static
2967void teardown_tracer_notifier(struct notification_thread_state *state,
2968 const struct lttng_trigger *trigger)
2969{
2970 struct cds_lfht_iter iter;
2971 struct notification_trigger_tokens_ht_element *trigger_tokens_ht_element;
2972
2973 cds_lfht_for_each_entry(state->trigger_tokens_ht, &iter,
2974 trigger_tokens_ht_element, node) {
2975
2976 if (!lttng_trigger_is_equal(trigger,
2977 trigger_tokens_ht_element->trigger)) {
2978 continue;
2979 }
2980
2981 event_notifier_error_accounting_unregister_event_notifier(
2982 trigger_tokens_ht_element->trigger);
2983
2984 /* TODO talk to all app and remove it */
2985 DBG("[notification-thread] Removed trigger from tokens_ht");
2986 cds_lfht_del(state->trigger_tokens_ht,
2987 &trigger_tokens_ht_element->node);
2988
2989 call_rcu(&trigger_tokens_ht_element->rcu_node,
2990 free_notification_trigger_tokens_ht_element_rcu);
2991
2992 break;
2993 }
2994}
2995
cc2295b5 2996static
ab0ee2ca
JG
2997int handle_notification_thread_command_unregister_trigger(
2998 struct notification_thread_state *state,
ac16173e 2999 const struct lttng_trigger *trigger,
ab0ee2ca
JG
3000 enum lttng_error_code *_cmd_reply)
3001{
3002 struct cds_lfht_iter iter;
ed327204 3003 struct cds_lfht_node *triggers_ht_node;
ab0ee2ca
JG
3004 struct lttng_channel_trigger_list *trigger_list;
3005 struct notification_client_list *client_list;
ab0ee2ca 3006 struct lttng_trigger_ht_element *trigger_ht_element = NULL;
ac16173e 3007 const struct lttng_condition *condition = lttng_trigger_get_const_condition(
ab0ee2ca 3008 trigger);
ab0ee2ca
JG
3009 enum lttng_error_code cmd_reply;
3010
3011 rcu_read_lock();
3012
3013 cds_lfht_lookup(state->triggers_ht,
3014 lttng_condition_hash(condition),
242388e4
JR
3015 match_trigger,
3016 trigger,
ab0ee2ca
JG
3017 &iter);
3018 triggers_ht_node = cds_lfht_iter_get_node(&iter);
3019 if (!triggers_ht_node) {
3020 cmd_reply = LTTNG_ERR_TRIGGER_NOT_FOUND;
3021 goto end;
3022 } else {
3023 cmd_reply = LTTNG_OK;
3024 }
3025
9c374932
JR
3026 trigger_ht_element = caa_container_of(triggers_ht_node,
3027 struct lttng_trigger_ht_element, node);
3028
ab0ee2ca
JG
3029 /* Remove trigger from channel_triggers_ht. */
3030 cds_lfht_for_each_entry(state->channel_triggers_ht, &iter, trigger_list,
3031 channel_triggers_ht_node) {
3032 struct lttng_trigger_list_element *trigger_element, *tmp;
3033
3034 cds_list_for_each_entry_safe(trigger_element, tmp,
3035 &trigger_list->list, node) {
e8f7fa11 3036 if (!lttng_trigger_is_equal(trigger, trigger_element->trigger)) {
ab0ee2ca
JG
3037 continue;
3038 }
3039
3040 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
3041 cds_list_del(&trigger_element->node);
e4db5ace
JR
3042 /* A trigger can only appear once per channel */
3043 break;
ab0ee2ca
JG
3044 }
3045 }
3046
6487ad53
FD
3047 if (lttng_trigger_needs_tracer_notifier(trigger)) {
3048 teardown_tracer_notifier(state, trigger);
e7c93cf9
JR
3049 }
3050
51367634
JR
3051 if (is_trigger_action_notify(trigger)) {
3052 /*
3053 * Remove and release the client list from
3054 * notification_trigger_clients_ht.
3055 */
3056 client_list = get_client_list_from_condition(state, condition);
3057 assert(client_list);
ed327204 3058
091fa780
FD
3059 pthread_mutex_lock(&client_list->lock);
3060 cds_list_del(&trigger_ht_element->client_list_trigger_node);
3061 pthread_mutex_unlock(&client_list->lock);
3062
51367634
JR
3063 /* Put new reference and the hashtable's reference. */
3064 notification_client_list_put(client_list);
3065 notification_client_list_put(client_list);
3066 client_list = NULL;
3067 }
ab0ee2ca 3068
2758e38b
FD
3069 /* Remove trigger from triggers_ht. */
3070 notif_thread_state_remove_trigger_ht_elem(state, trigger_ht_element);
ab0ee2ca 3071
7ca172c1 3072 /* Release the ownership of the trigger. */
ab0ee2ca 3073 lttng_trigger_destroy(trigger_ht_element->trigger);
83b934ad 3074 call_rcu(&trigger_ht_element->rcu_node, free_lttng_trigger_ht_element_rcu);
ab0ee2ca
JG
3075end:
3076 rcu_read_unlock();
3077 if (_cmd_reply) {
3078 *_cmd_reply = cmd_reply;
3079 }
3080 return 0;
3081}
3082
3083/* Returns 0 on success, 1 on exit requested, negative value on error. */
3084int handle_notification_thread_command(
3085 struct notification_thread_handle *handle,
3086 struct notification_thread_state *state)
3087{
3088 int ret;
3089 uint64_t counter;
3090 struct notification_thread_command *cmd;
3091
8abe313a 3092 /* Read the event pipe to put it back into a quiescent state. */
18aeca05 3093 ret = lttng_read(lttng_pipe_get_readfd(handle->cmd_queue.event_pipe), &counter,
8abe313a 3094 sizeof(counter));
18aeca05 3095 if (ret != sizeof(counter)) {
ab0ee2ca
JG
3096 goto error;
3097 }
3098
3099 pthread_mutex_lock(&handle->cmd_queue.lock);
3100 cmd = cds_list_first_entry(&handle->cmd_queue.list,
3101 struct notification_thread_command, cmd_list_node);
97a71ea6
JR
3102 cds_list_del(&cmd->cmd_list_node);
3103 pthread_mutex_unlock(&handle->cmd_queue.lock);
6900ae81
FD
3104
3105 DBG("[notification-thread] Received `%s` command",
3106 notification_command_type_str(cmd->type));
ab0ee2ca
JG
3107 switch (cmd->type) {
3108 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER:
ac16173e
JG
3109 ret = handle_notification_thread_command_register_trigger(state,
3110 cmd->parameters.register_trigger.trigger,
0efb2ad7 3111 cmd->parameters.register_trigger.is_trigger_anonymous,
ab0ee2ca
JG
3112 &cmd->reply_code);
3113 break;
3114 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER:
ab0ee2ca 3115 ret = handle_notification_thread_command_unregister_trigger(
ac16173e
JG
3116 state,
3117 cmd->parameters.unregister_trigger.trigger,
ab0ee2ca
JG
3118 &cmd->reply_code);
3119 break;
3120 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL:
ab0ee2ca 3121 ret = handle_notification_thread_command_add_channel(
8abe313a
JG
3122 state,
3123 cmd->parameters.add_channel.session.name,
3124 cmd->parameters.add_channel.session.uid,
3125 cmd->parameters.add_channel.session.gid,
3126 cmd->parameters.add_channel.channel.name,
3127 cmd->parameters.add_channel.channel.domain,
3128 cmd->parameters.add_channel.channel.key,
3129 cmd->parameters.add_channel.channel.capacity,
ab0ee2ca
JG
3130 &cmd->reply_code);
3131 break;
3132 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL:
ab0ee2ca
JG
3133 ret = handle_notification_thread_command_remove_channel(
3134 state, cmd->parameters.remove_channel.key,
3135 cmd->parameters.remove_channel.domain,
3136 &cmd->reply_code);
3137 break;
0ca52944 3138 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING:
0ca52944 3139 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED:
ed327204 3140 ret = handle_notification_thread_command_session_rotation(
0ca52944 3141 state,
ed327204
JG
3142 cmd->type,
3143 cmd->parameters.session_rotation.session_name,
3144 cmd->parameters.session_rotation.uid,
3145 cmd->parameters.session_rotation.gid,
3146 cmd->parameters.session_rotation.trace_archive_chunk_id,
3147 cmd->parameters.session_rotation.location,
0ca52944
JG
3148 &cmd->reply_code);
3149 break;
d02d7404
JR
3150 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE:
3151 ret = handle_notification_thread_command_add_tracer_event_source(
3152 state,
3153 cmd->parameters.tracer_event_source.tracer_event_source_fd,
3154 cmd->parameters.tracer_event_source.domain,
3155 &cmd->reply_code);
3156 break;
3157 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE:
3158 ret = handle_notification_thread_command_remove_tracer_event_source(
3159 state,
3160 cmd->parameters.tracer_event_source.tracer_event_source_fd,
3161 &cmd->reply_code);
3162 break;
fbc9f37d
JR
3163 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS:
3164 {
3165 struct lttng_triggers *triggers = NULL;
3166
3167 ret = handle_notification_thread_command_list_triggers(
3168 handle,
3169 state,
3170 cmd->parameters.list_triggers.uid,
3171 &triggers,
3172 &cmd->reply_code);
3173 cmd->reply.list_triggers.triggers = triggers;
3174 ret = 0;
3175 break;
3176 }
ab0ee2ca 3177 case NOTIFICATION_COMMAND_TYPE_QUIT:
ab0ee2ca
JG
3178 cmd->reply_code = LTTNG_OK;
3179 ret = 1;
3180 goto end;
8790759c
FD
3181 case NOTIFICATION_COMMAND_TYPE_GET_TRIGGER:
3182 {
3183 struct lttng_trigger *trigger = NULL;
3184
3185 ret = handle_notification_thread_command_get_trigger(state,
3186 cmd->parameters.get_trigger.trigger, &trigger,
3187 &cmd->reply_code);
3188 cmd->reply.get_trigger.trigger = trigger;
3189 break;
3190 }
f2b3ef9f
JG
3191 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE:
3192 {
3193 const enum client_transmission_status client_status =
3194 cmd->parameters.client_communication_update
3195 .status;
3196 const notification_client_id client_id =
3197 cmd->parameters.client_communication_update.id;
3198 struct notification_client *client;
3199
3200 rcu_read_lock();
3201 client = get_client_from_id(client_id, state);
3202
3203 if (!client) {
3204 /*
3205 * Client error was probably already picked-up by the
3206 * notification thread or it has disconnected
3207 * gracefully while this command was queued.
3208 */
3209 DBG("Failed to find notification client to update communication status, client id = %" PRIu64,
3210 client_id);
3211 ret = 0;
3212 } else {
f2b3ef9f
JG
3213 ret = client_handle_transmission_status(
3214 client, client_status, state);
f2b3ef9f
JG
3215 }
3216 rcu_read_unlock();
3217 break;
3218 }
ab0ee2ca
JG
3219 default:
3220 ERR("[notification-thread] Unknown internal command received");
3221 goto error_unlock;
3222 }
3223
3224 if (ret) {
3225 goto error_unlock;
3226 }
3227end:
0ab399e0
JG
3228 if (cmd->is_async) {
3229 free(cmd);
3230 cmd = NULL;
3231 } else {
3232 lttng_waiter_wake_up(&cmd->reply_waiter);
3233 }
ab0ee2ca
JG
3234 return ret;
3235error_unlock:
3236 /* Wake-up and return a fatal error to the calling thread. */
8ada111f 3237 lttng_waiter_wake_up(&cmd->reply_waiter);
ab0ee2ca
JG
3238 cmd->reply_code = LTTNG_ERR_FATAL;
3239error:
3240 /* Indicate a fatal error to the caller. */
3241 return -1;
3242}
3243
ab0ee2ca
JG
3244static
3245int socket_set_non_blocking(int socket)
3246{
3247 int ret, flags;
3248
3249 /* Set the pipe as non-blocking. */
3250 ret = fcntl(socket, F_GETFL, 0);
3251 if (ret == -1) {
3252 PERROR("fcntl get socket flags");
3253 goto end;
3254 }
3255 flags = ret;
3256
3257 ret = fcntl(socket, F_SETFL, flags | O_NONBLOCK);
3258 if (ret == -1) {
3259 PERROR("fcntl set O_NONBLOCK socket flag");
3260 goto end;
3261 }
3262 DBG("Client socket (fd = %i) set as non-blocking", socket);
3263end:
3264 return ret;
3265}
3266
3267static
14fa22f8 3268int client_reset_inbound_state(struct notification_client *client)
ab0ee2ca
JG
3269{
3270 int ret;
3271
882093ee
JR
3272
3273 lttng_payload_clear(&client->communication.inbound.payload);
ab0ee2ca
JG
3274
3275 client->communication.inbound.bytes_to_receive =
3276 sizeof(struct lttng_notification_channel_message);
3277 client->communication.inbound.msg_type =
3278 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN;
ab0ee2ca
JG
3279 LTTNG_SOCK_SET_UID_CRED(&client->communication.inbound.creds, -1);
3280 LTTNG_SOCK_SET_GID_CRED(&client->communication.inbound.creds, -1);
14fa22f8 3281 ret = lttng_dynamic_buffer_set_size(
882093ee 3282 &client->communication.inbound.payload.buffer,
14fa22f8 3283 client->communication.inbound.bytes_to_receive);
882093ee 3284
14fa22f8 3285 return ret;
ab0ee2ca
JG
3286}
3287
3288int handle_notification_thread_client_connect(
3289 struct notification_thread_state *state)
3290{
3291 int ret;
3292 struct notification_client *client;
3293
3294 DBG("[notification-thread] Handling new notification channel client connection");
3295
3296 client = zmalloc(sizeof(*client));
3297 if (!client) {
3298 /* Fatal error. */
3299 ret = -1;
3300 goto error;
3301 }
6c24d3fd 3302
505b2d90 3303 pthread_mutex_init(&client->lock, NULL);
ac1889bf 3304 client->id = state->next_notification_client_id++;
ab0ee2ca 3305 CDS_INIT_LIST_HEAD(&client->condition_list);
882093ee
JR
3306 lttng_payload_init(&client->communication.inbound.payload);
3307 lttng_payload_init(&client->communication.outbound.payload);
01ea340e 3308 client->communication.inbound.expect_creds = true;
505b2d90 3309
14fa22f8
JG
3310 ret = client_reset_inbound_state(client);
3311 if (ret) {
3312 ERR("[notification-thread] Failed to reset client communication's inbound state");
e742b055 3313 ret = 0;
14fa22f8
JG
3314 goto error;
3315 }
ab0ee2ca
JG
3316
3317 ret = lttcomm_accept_unix_sock(state->notification_channel_socket);
3318 if (ret < 0) {
3319 ERR("[notification-thread] Failed to accept new notification channel client connection");
3320 ret = 0;
3321 goto error;
3322 }
3323
3324 client->socket = ret;
3325
3326 ret = socket_set_non_blocking(client->socket);
3327 if (ret) {
3328 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
3329 goto error;
3330 }
3331
3332 ret = lttcomm_setsockopt_creds_unix_sock(client->socket);
3333 if (ret < 0) {
3334 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
3335 ret = 0;
3336 goto error;
3337 }
3338
3339 ret = lttng_poll_add(&state->events, client->socket,
3340 LPOLLIN | LPOLLERR |
3341 LPOLLHUP | LPOLLRDHUP);
3342 if (ret < 0) {
3343 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
3344 ret = 0;
3345 goto error;
3346 }
3347 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
3348 client->socket);
3349
ab0ee2ca
JG
3350 rcu_read_lock();
3351 cds_lfht_add(state->client_socket_ht,
3352 hash_client_socket(client->socket),
3353 &client->client_socket_ht_node);
ac1889bf
JG
3354 cds_lfht_add(state->client_id_ht,
3355 hash_client_id(client->id),
3356 &client->client_id_ht_node);
ab0ee2ca
JG
3357 rcu_read_unlock();
3358
3359 return ret;
6c24d3fd 3360
ab0ee2ca
JG
3361error:
3362 notification_client_destroy(client, state);
3363 return ret;
3364}
3365
6c24d3fd
JG
3366/*
3367 * RCU read-lock must be held by the caller.
3368 * Client lock must _not_ be held by the caller.
3369 */
505b2d90
JG
3370static
3371int notification_thread_client_disconnect(
3372 struct notification_client *client,
ab0ee2ca 3373 struct notification_thread_state *state)
505b2d90
JG
3374{
3375 int ret;
3376 struct lttng_condition_list_element *condition_list_element, *tmp;
3377
3378 /* Acquire the client lock to disable its communication atomically. */
6c24d3fd 3379 pthread_mutex_lock(&client->lock);
505b2d90 3380 client->communication.active = false;
6c24d3fd
JG
3381 cds_lfht_del(state->client_socket_ht, &client->client_socket_ht_node);
3382 cds_lfht_del(state->client_id_ht, &client->client_id_ht_node);
3383 pthread_mutex_unlock(&client->lock);
3384
505b2d90
JG
3385 ret = lttng_poll_del(&state->events, client->socket);
3386 if (ret) {
3387 ERR("[notification-thread] Failed to remove client socket %d from poll set",
3388 client->socket);
3389 }
3390
505b2d90
JG
3391 /* Release all conditions to which the client was subscribed. */
3392 cds_list_for_each_entry_safe(condition_list_element, tmp,
3393 &client->condition_list, node) {
3394 (void) notification_thread_client_unsubscribe(client,
3395 condition_list_element->condition, state, NULL);
3396 }
3397
3398 /*
3399 * Client no longer accessible to other threads (through the
3400 * client lists).
3401 */
3402 notification_client_destroy(client, state);
3403 return ret;
3404}
3405
3406int handle_notification_thread_client_disconnect(
3407 int client_socket, struct notification_thread_state *state)
ab0ee2ca
JG
3408{
3409 int ret = 0;
3410 struct notification_client *client;
3411
3412 rcu_read_lock();
3413 DBG("[notification-thread] Closing client connection (socket fd = %i)",
3414 client_socket);
3415 client = get_client_from_socket(client_socket, state);
3416 if (!client) {
3417 /* Internal state corruption, fatal error. */
3418 ERR("[notification-thread] Unable to find client (socket fd = %i)",
3419 client_socket);
3420 ret = -1;
3421 goto end;
3422 }
3423
505b2d90 3424 ret = notification_thread_client_disconnect(client, state);
ab0ee2ca
JG
3425end:
3426 rcu_read_unlock();
3427 return ret;
3428}
3429
3430int handle_notification_thread_client_disconnect_all(
3431 struct notification_thread_state *state)
3432{
3433 struct cds_lfht_iter iter;
3434 struct notification_client *client;
3435 bool error_encoutered = false;
3436
3437 rcu_read_lock();
3438 DBG("[notification-thread] Closing all client connections");
3439 cds_lfht_for_each_entry(state->client_socket_ht, &iter, client,
505b2d90 3440 client_socket_ht_node) {
ab0ee2ca
JG
3441 int ret;
3442
505b2d90
JG
3443 ret = notification_thread_client_disconnect(
3444 client, state);
ab0ee2ca
JG
3445 if (ret) {
3446 error_encoutered = true;
3447 }
3448 }
3449 rcu_read_unlock();
3450 return error_encoutered ? 1 : 0;
3451}
3452
3453int handle_notification_thread_trigger_unregister_all(
3454 struct notification_thread_state *state)
3455{
4149ace8 3456 bool error_occurred = false;
ab0ee2ca
JG
3457 struct cds_lfht_iter iter;
3458 struct lttng_trigger_ht_element *trigger_ht_element;
3459
763de384 3460 rcu_read_lock();
ab0ee2ca
JG
3461 cds_lfht_for_each_entry(state->triggers_ht, &iter, trigger_ht_element,
3462 node) {
3463 int ret = handle_notification_thread_command_unregister_trigger(
3464 state, trigger_ht_element->trigger, NULL);
3465 if (ret) {
4149ace8 3466 error_occurred = true;
ab0ee2ca
JG
3467 }
3468 }
763de384 3469 rcu_read_unlock();
4149ace8 3470 return error_occurred ? -1 : 0;
ab0ee2ca
JG
3471}
3472
dd877ea6
JG
3473static
3474int client_handle_transmission_status(
3475 struct notification_client *client,
3476 enum client_transmission_status transmission_status,
3477 struct notification_thread_state *state)
3478{
3479 int ret = 0;
3480
3481 switch (transmission_status) {
3482 case CLIENT_TRANSMISSION_STATUS_COMPLETE:
3483 ret = lttng_poll_mod(&state->events, client->socket,
3484 CLIENT_POLL_MASK_IN);
3485 if (ret) {
3486 goto end;
3487 }
3488
dd877ea6
JG
3489 break;
3490 case CLIENT_TRANSMISSION_STATUS_QUEUED:
3491 /*
3492 * We want to be notified whenever there is buffer space
3493 * available to send the rest of the payload.
3494 */
3495 ret = lttng_poll_mod(&state->events, client->socket,
3496 CLIENT_POLL_MASK_IN_OUT);
3497 if (ret) {
3498 goto end;
3499 }
3500 break;
3501 case CLIENT_TRANSMISSION_STATUS_FAIL:
3502 ret = notification_thread_client_disconnect(client, state);
3503 if (ret) {
3504 goto end;
3505 }
3506 break;
3507 case CLIENT_TRANSMISSION_STATUS_ERROR:
3508 ret = -1;
3509 goto end;
3510 default:
3511 abort();
3512 }
3513end:
3514 return ret;
3515}
3516
505b2d90 3517/* Client lock must be acquired by caller. */
ab0ee2ca 3518static
dd877ea6 3519enum client_transmission_status client_flush_outgoing_queue(
f2b3ef9f 3520 struct notification_client *client)
ab0ee2ca
JG
3521{
3522 ssize_t ret;
3523 size_t to_send_count;
dd877ea6 3524 enum client_transmission_status status;
882093ee
JR
3525 struct lttng_payload_view pv = lttng_payload_view_from_payload(
3526 &client->communication.outbound.payload, 0, -1);
3527 const int fds_to_send_count =
3528 lttng_payload_view_get_fd_handle_count(&pv);
ab0ee2ca 3529
505b2d90
JG
3530 ASSERT_LOCKED(client->lock);
3531
f2b3ef9f
JG
3532 if (!client->communication.active) {
3533 status = CLIENT_TRANSMISSION_STATUS_FAIL;
3534 goto end;
3535 }
3536
882093ee
JR
3537 if (pv.buffer.size == 0) {
3538 /*
3539 * If both data and fds are equal to zero, we are in an invalid
3540 * state.
3541 */
3542 assert(fds_to_send_count != 0);
3543 goto send_fds;
3544 }
3545
3546 /* Send data. */
3547 to_send_count = pv.buffer.size;
ab0ee2ca
JG
3548 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
3549 client->socket);
3550
3551 ret = lttcomm_send_unix_sock_non_block(client->socket,
882093ee 3552 pv.buffer.data,
ab0ee2ca 3553 to_send_count);
44c180ca 3554 if ((ret >= 0 && ret < to_send_count)) {
ab0ee2ca
JG
3555 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
3556 client->socket);
3557 to_send_count -= max(ret, 0);
3558
6f110534 3559 memmove(client->communication.outbound.payload.buffer.data,
882093ee
JR
3560 pv.buffer.data +
3561 pv.buffer.size - to_send_count,
ab0ee2ca
JG
3562 to_send_count);
3563 ret = lttng_dynamic_buffer_set_size(
882093ee 3564 &client->communication.outbound.payload.buffer,
ab0ee2ca
JG
3565 to_send_count);
3566 if (ret) {
3567 goto error;
3568 }
6c24d3fd 3569
dd877ea6 3570 status = CLIENT_TRANSMISSION_STATUS_QUEUED;
882093ee 3571 goto end;
ab0ee2ca 3572 } else if (ret < 0) {
6c24d3fd 3573 /* Generic error, disable the client's communication. */
dd877ea6 3574 ERR("[notification-thread] Failed to flush outgoing queue, disconnecting client (socket fd = %i)",
ab0ee2ca 3575 client->socket);
6c24d3fd 3576 client->communication.active = false;
dd877ea6 3577 status = CLIENT_TRANSMISSION_STATUS_FAIL;
882093ee 3578 goto end;
ab0ee2ca 3579 } else {
882093ee
JR
3580 /*
3581 * No error and flushed the queue completely.
3582 *
3583 * The payload buffer size is used later to
3584 * check if there is notifications queued. So albeit that the
3585 * direct caller knows that the transmission is complete, we
3586 * need to set the buffer size to zero.
3587 */
ab0ee2ca 3588 ret = lttng_dynamic_buffer_set_size(
882093ee 3589 &client->communication.outbound.payload.buffer, 0);
ab0ee2ca
JG
3590 if (ret) {
3591 goto error;
3592 }
882093ee 3593 }
6c24d3fd 3594
882093ee
JR
3595send_fds:
3596 /* No fds to send, transmission is complete. */
3597 if (fds_to_send_count == 0) {
dd877ea6 3598 status = CLIENT_TRANSMISSION_STATUS_COMPLETE;
882093ee 3599 goto end;
dd877ea6 3600 }
882093ee
JR
3601
3602 ret = lttcomm_send_payload_view_fds_unix_sock_non_block(
3603 client->socket, &pv);
3604 if (ret < 0) {
3605 /* Generic error, disable the client's communication. */
3606 ERR("[notification-thread] Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)",
3607 client->socket);
3608 client->communication.active = false;
3609 status = CLIENT_TRANSMISSION_STATUS_FAIL;
3610 goto end;
3611 } else if (ret == 0) {
3612 /* Nothing could be sent. */
3613 status = CLIENT_TRANSMISSION_STATUS_QUEUED;
3614 } else {
3615 /* Fd passing is an all or nothing kind of thing. */
3616 status = CLIENT_TRANSMISSION_STATUS_COMPLETE;
3617 /*
3618 * The payload _fd_array count is used later to
3619 * check if there is notifications queued. So although the
3620 * direct caller knows that the transmission is complete, we
3621 * need to clear the _fd_array for the queuing check.
3622 */
3623 lttng_dynamic_pointer_array_clear(
3624 &client->communication.outbound.payload
3625 ._fd_handles);
3626 }
3627
f2b3ef9f 3628end:
882093ee
JR
3629 if (status == CLIENT_TRANSMISSION_STATUS_COMPLETE) {
3630 client->communication.outbound.queued_command_reply = false;
3631 client->communication.outbound.dropped_notification = false;
3632 lttng_payload_clear(&client->communication.outbound.payload);
3633 }
3634
f2b3ef9f 3635 return status;
ab0ee2ca 3636error:
f2b3ef9f 3637 return CLIENT_TRANSMISSION_STATUS_ERROR;
ab0ee2ca
JG
3638}
3639
882093ee
JR
3640static
3641bool client_has_outbound_data_left(
3642 const struct notification_client *client)
3643{
3644 const struct lttng_payload_view pv = lttng_payload_view_from_payload(
3645 &client->communication.outbound.payload, 0, -1);
3646 const bool has_data = pv.buffer.size != 0;
3647 const bool has_fds = lttng_payload_view_get_fd_handle_count(&pv);
3648
3649 return has_data || has_fds;
3650}
3651
6c24d3fd 3652/* Client lock must _not_ be held by the caller. */
ab0ee2ca
JG
3653static
3654int client_send_command_reply(struct notification_client *client,
3655 struct notification_thread_state *state,
3656 enum lttng_notification_channel_status status)
3657{
3658 int ret;
3659 struct lttng_notification_channel_command_reply reply = {
3660 .status = (int8_t) status,
3661 };
3662 struct lttng_notification_channel_message msg = {
3663 .type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY,
3664 .size = sizeof(reply),
3665 };
3666 char buffer[sizeof(msg) + sizeof(reply)];
f2b3ef9f 3667 enum client_transmission_status transmission_status;
ab0ee2ca 3668
6c24d3fd
JG
3669 memcpy(buffer, &msg, sizeof(msg));
3670 memcpy(buffer + sizeof(msg), &reply, sizeof(reply));
3671 DBG("[notification-thread] Send command reply (%i)", (int) status);
505b2d90 3672
6c24d3fd 3673 pthread_mutex_lock(&client->lock);
ab0ee2ca
JG
3674 if (client->communication.outbound.queued_command_reply) {
3675 /* Protocol error. */
6c24d3fd 3676 goto error_unlock;
ab0ee2ca
JG
3677 }
3678
ab0ee2ca
JG
3679 /* Enqueue buffer to outgoing queue and flush it. */
3680 ret = lttng_dynamic_buffer_append(
882093ee 3681 &client->communication.outbound.payload.buffer,
ab0ee2ca
JG
3682 buffer, sizeof(buffer));
3683 if (ret) {
6c24d3fd 3684 goto error_unlock;
ab0ee2ca
JG
3685 }
3686
f2b3ef9f 3687 transmission_status = client_flush_outgoing_queue(client);
882093ee
JR
3688
3689 if (client_has_outbound_data_left(client)) {
6c24d3fd
JG
3690 /* Queue could not be emptied. */
3691 client->communication.outbound.queued_command_reply = true;
3692 }
3693
3694 pthread_mutex_unlock(&client->lock);
f2b3ef9f
JG
3695 ret = client_handle_transmission_status(
3696 client, transmission_status, state);
ab0ee2ca
JG
3697 if (ret) {
3698 goto error;
3699 }
3700
ab0ee2ca 3701 return 0;
6c24d3fd
JG
3702error_unlock:
3703 pthread_mutex_unlock(&client->lock);
ab0ee2ca
JG
3704error:
3705 return -1;
3706}
3707
505b2d90
JG
3708static
3709int client_handle_message_unknown(struct notification_client *client,
3710 struct notification_thread_state *state)
3711{
3712 int ret;
505b2d90
JG
3713 /*
3714 * Receiving message header. The function will be called again
3715 * once the rest of the message as been received and can be
3716 * interpreted.
3717 */
3718 const struct lttng_notification_channel_message *msg;
3719
882093ee 3720 assert(sizeof(*msg) == client->communication.inbound.payload.buffer.size);
505b2d90 3721 msg = (const struct lttng_notification_channel_message *)
882093ee 3722 client->communication.inbound.payload.buffer.data;
505b2d90
JG
3723
3724 if (msg->size == 0 ||
3725 msg->size > DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE) {
3726 ERR("[notification-thread] Invalid notification channel message: length = %u",
3727 msg->size);
3728 ret = -1;
3729 goto end;
3730 }
3731
3732 switch (msg->type) {
3733 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE:
3734 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE:
3735 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE:
3736 break;
3737 default:
3738 ret = -1;
3739 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
3740 goto end;
3741 }
3742
3743 client->communication.inbound.bytes_to_receive = msg->size;
882093ee 3744 client->communication.inbound.fds_to_receive = msg->fds;
505b2d90
JG
3745 client->communication.inbound.msg_type =
3746 (enum lttng_notification_channel_message_type) msg->type;
3747 ret = lttng_dynamic_buffer_set_size(
882093ee
JR
3748 &client->communication.inbound.payload.buffer, msg->size);
3749
3750 /* msg is not valid anymore due to lttng_dynamic_buffer_set_size. */
3751 msg = NULL;
505b2d90 3752end:
505b2d90
JG
3753 return ret;
3754}
3755
3756static
3757int client_handle_message_handshake(struct notification_client *client,
3758 struct notification_thread_state *state)
3759{
3760 int ret;
3761 struct lttng_notification_channel_command_handshake *handshake_client;
3762 const struct lttng_notification_channel_command_handshake handshake_reply = {
3763 .major = LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR,
3764 .minor = LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR,
3765 };
3766 const struct lttng_notification_channel_message msg_header = {
3767 .type = LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE,
3768 .size = sizeof(handshake_reply),
3769 };
3770 enum lttng_notification_channel_status status =
3771 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK;
3772 char send_buffer[sizeof(msg_header) + sizeof(handshake_reply)];
505b2d90
JG
3773
3774 memcpy(send_buffer, &msg_header, sizeof(msg_header));
3775 memcpy(send_buffer + sizeof(msg_header), &handshake_reply,
3776 sizeof(handshake_reply));
3777
3778 handshake_client =
3779 (struct lttng_notification_channel_command_handshake *)
882093ee 3780 client->communication.inbound.payload.buffer
505b2d90
JG
3781 .data;
3782 client->major = handshake_client->major;
3783 client->minor = handshake_client->minor;
3784 if (!client->communication.inbound.creds_received) {
3785 ERR("[notification-thread] No credentials received from client");
3786 ret = -1;
3787 goto end;
3788 }
3789
3790 client->uid = LTTNG_SOCK_GET_UID_CRED(
3791 &client->communication.inbound.creds);
3792 client->gid = LTTNG_SOCK_GET_GID_CRED(
3793 &client->communication.inbound.creds);
3794 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
3795 client->uid, client->gid, (int) client->major,
3796 (int) client->minor);
3797
3798 if (handshake_client->major !=
3799 LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR) {
3800 status = LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION;
3801 }
3802
6c24d3fd
JG
3803 pthread_mutex_lock(&client->lock);
3804 /* Outgoing queue will be flushed when the command reply is sent. */
505b2d90 3805 ret = lttng_dynamic_buffer_append(
882093ee 3806 &client->communication.outbound.payload.buffer, send_buffer,
505b2d90
JG
3807 sizeof(send_buffer));
3808 if (ret) {
3809 ERR("[notification-thread] Failed to send protocol version to notification channel client");
6c24d3fd 3810 goto end_unlock;
505b2d90
JG
3811 }
3812
3813 client->validated = true;
3814 client->communication.active = true;
6c24d3fd 3815 pthread_mutex_unlock(&client->lock);
505b2d90 3816
6c24d3fd
JG
3817 /* Set reception state to receive the next message header. */
3818 ret = client_reset_inbound_state(client);
505b2d90 3819 if (ret) {
6c24d3fd 3820 ERR("[notification-thread] Failed to reset client communication's inbound state");
505b2d90
JG
3821 goto end;
3822 }
3823
6c24d3fd 3824 /* Flushes the outgoing queue. */
505b2d90
JG
3825 ret = client_send_command_reply(client, state, status);
3826 if (ret) {
3827 ERR("[notification-thread] Failed to send reply to notification channel client");
3828 goto end;
3829 }
3830
6c24d3fd
JG
3831 goto end;
3832end_unlock:
505b2d90 3833 pthread_mutex_unlock(&client->lock);
6c24d3fd 3834end:
505b2d90
JG
3835 return ret;
3836}
3837
3838static
3839int client_handle_message_subscription(
3840 struct notification_client *client,
3841 enum lttng_notification_channel_message_type msg_type,
3842 struct notification_thread_state *state)
3843{
3844 int ret;
3845 struct lttng_condition *condition;
3846 enum lttng_notification_channel_status status =
3847 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK;
3848 struct lttng_payload_view condition_view =
882093ee
JR
3849 lttng_payload_view_from_payload(
3850 &client->communication.inbound.payload,
505b2d90
JG
3851 0, -1);
3852 size_t expected_condition_size;
3853
6c24d3fd
JG
3854 /*
3855 * No need to lock client to sample the inbound state as the only
3856 * other thread accessing clients (action executor) only uses the
3857 * outbound state.
3858 */
882093ee 3859 expected_condition_size = client->communication.inbound.payload.buffer.size;
505b2d90
JG
3860 ret = lttng_condition_create_from_payload(&condition_view, &condition);
3861 if (ret != expected_condition_size) {
3862 ERR("[notification-thread] Malformed condition received from client");
3863 goto end;
3864 }
3865
3866 if (msg_type == LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE) {
3867 ret = notification_thread_client_subscribe(
3868 client, condition, state, &status);
3869 } else {
3870 ret = notification_thread_client_unsubscribe(
3871 client, condition, state, &status);
3872 }
505b2d90 3873
505b2d90 3874 if (ret) {
6c24d3fd 3875 goto end;
505b2d90
JG
3876 }
3877
3878 /* Set reception state to receive the next message header. */
3879 ret = client_reset_inbound_state(client);
3880 if (ret) {
3881 ERR("[notification-thread] Failed to reset client communication's inbound state");
6c24d3fd
JG
3882 goto end;
3883 }
3884
3885 ret = client_send_command_reply(client, state, status);
3886 if (ret) {
3887 ERR("[notification-thread] Failed to send reply to notification channel client");
3888 goto end;
505b2d90
JG
3889 }
3890
505b2d90
JG
3891end:
3892 return ret;
3893}
3894
ab0ee2ca
JG
3895static
3896int client_dispatch_message(struct notification_client *client,
3897 struct notification_thread_state *state)
3898{
3899 int ret = 0;
3900
3901 if (client->communication.inbound.msg_type !=
3902 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE &&
3903 client->communication.inbound.msg_type !=
3904 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN &&
3905 !client->validated) {
3906 WARN("[notification-thread] client attempted a command before handshake");
3907 ret = -1;
3908 goto end;
3909 }
3910
3911 switch (client->communication.inbound.msg_type) {
3912 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN:
3913 {
505b2d90 3914 ret = client_handle_message_unknown(client, state);
ab0ee2ca
JG
3915 break;
3916 }
3917 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE:
3918 {
505b2d90 3919 ret = client_handle_message_handshake(client, state);
ab0ee2ca
JG
3920 break;
3921 }
3922 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE:
3923 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE:
3924 {
505b2d90
JG
3925 ret = client_handle_message_subscription(client,
3926 client->communication.inbound.msg_type, state);
ab0ee2ca
JG
3927 break;
3928 }
3929 default:
3930 abort();
3931 }
3932end:
3933 return ret;
3934}
3935
3936/* Incoming data from client. */
3937int handle_notification_thread_client_in(
3938 struct notification_thread_state *state, int socket)
3939{
14fa22f8 3940 int ret = 0;
ab0ee2ca
JG
3941 struct notification_client *client;
3942 ssize_t recv_ret;
3943 size_t offset;
3944
98b5ff34 3945 rcu_read_lock();
ab0ee2ca
JG
3946 client = get_client_from_socket(socket, state);
3947 if (!client) {
3948 /* Internal error, abort. */
3949 ret = -1;
3950 goto end;
3951 }
3952
882093ee 3953 offset = client->communication.inbound.payload.buffer.size -
14fa22f8 3954 client->communication.inbound.bytes_to_receive;
01ea340e 3955 if (client->communication.inbound.expect_creds) {
ab0ee2ca 3956 recv_ret = lttcomm_recv_creds_unix_sock(socket,
882093ee 3957 client->communication.inbound.payload.buffer.data + offset,
ab0ee2ca
JG
3958 client->communication.inbound.bytes_to_receive,
3959 &client->communication.inbound.creds);
3960 if (recv_ret > 0) {
01ea340e 3961 client->communication.inbound.expect_creds = false;
ab0ee2ca
JG
3962 client->communication.inbound.creds_received = true;
3963 }
3964 } else {
3965 recv_ret = lttcomm_recv_unix_sock_non_block(socket,
882093ee 3966 client->communication.inbound.payload.buffer.data + offset,
ab0ee2ca
JG
3967 client->communication.inbound.bytes_to_receive);
3968 }
505b2d90
JG
3969 if (recv_ret >= 0) {
3970 client->communication.inbound.bytes_to_receive -= recv_ret;
882093ee
JR
3971 } else {
3972 goto error_disconnect_client;
505b2d90 3973 }
6c24d3fd 3974
882093ee
JR
3975 if (client->communication.inbound.bytes_to_receive != 0) {
3976 /* Message incomplete wait for more data. */
3977 ret = 0;
3978 goto end;
ab0ee2ca
JG
3979 }
3980
882093ee
JR
3981 assert(client->communication.inbound.bytes_to_receive == 0);
3982
3983 /* Receive fds. */
3984 if (client->communication.inbound.fds_to_receive != 0) {
3985 ret = lttcomm_recv_payload_fds_unix_sock_non_block(
3986 client->socket,
3987 client->communication.inbound.fds_to_receive,
3988 &client->communication.inbound.payload);
3989 if (ret > 0) {
ab0ee2ca 3990 /*
882093ee
JR
3991 * Fds received. non blocking fds passing is all
3992 * or nothing.
ab0ee2ca 3993 */
882093ee
JR
3994 ssize_t expected_size;
3995
3996 expected_size = sizeof(int) *
3997 client->communication.inbound
3998 .fds_to_receive;
3999 assert(ret == expected_size);
4000 client->communication.inbound.fds_to_receive = 0;
4001 } else if (ret == 0) {
4002 /* Received nothing. */
4003 ret = 0;
4004 goto end;
4005 } else {
ab0ee2ca
JG
4006 goto error_disconnect_client;
4007 }
ab0ee2ca 4008 }
882093ee
JR
4009
4010 /* At this point the message is complete.*/
4011 assert(client->communication.inbound.bytes_to_receive == 0 &&
4012 client->communication.inbound.fds_to_receive == 0);
4013 ret = client_dispatch_message(client, state);
4014 if (ret) {
4015 /*
4016 * Only returns an error if this client must be
4017 * disconnected.
4018 */
4019 goto error_disconnect_client;
4020 }
4021
ab0ee2ca 4022end:
98b5ff34 4023 rcu_read_unlock();
ab0ee2ca 4024 return ret;
882093ee 4025
ab0ee2ca 4026error_disconnect_client:
505b2d90 4027 ret = notification_thread_client_disconnect(client, state);
98b5ff34 4028 goto end;
ab0ee2ca
JG
4029}
4030
4031/* Client ready to receive outgoing data. */
4032int handle_notification_thread_client_out(
4033 struct notification_thread_state *state, int socket)
4034{
4035 int ret;
4036 struct notification_client *client;
f2b3ef9f 4037 enum client_transmission_status transmission_status;
ab0ee2ca 4038
98b5ff34 4039 rcu_read_lock();
ab0ee2ca
JG
4040 client = get_client_from_socket(socket, state);
4041 if (!client) {
4042 /* Internal error, abort. */
4043 ret = -1;
4044 goto end;
4045 }
4046
505b2d90 4047 pthread_mutex_lock(&client->lock);
f2b3ef9f 4048 transmission_status = client_flush_outgoing_queue(client);
6c24d3fd
JG
4049 pthread_mutex_unlock(&client->lock);
4050
f2b3ef9f
JG
4051 ret = client_handle_transmission_status(
4052 client, transmission_status, state);
ab0ee2ca
JG
4053 if (ret) {
4054 goto end;
4055 }
4056end:
98b5ff34 4057 rcu_read_unlock();
ab0ee2ca
JG
4058 return ret;
4059}
4060
4061static
e8360425
JD
4062bool evaluate_buffer_usage_condition(const struct lttng_condition *condition,
4063 const struct channel_state_sample *sample,
4064 uint64_t buffer_capacity)
ab0ee2ca
JG
4065{
4066 bool result = false;
4067 uint64_t threshold;
4068 enum lttng_condition_type condition_type;
e8360425 4069 const struct lttng_condition_buffer_usage *use_condition = container_of(
ab0ee2ca
JG
4070 condition, struct lttng_condition_buffer_usage,
4071 parent);
4072
ab0ee2ca
JG
4073 if (use_condition->threshold_bytes.set) {
4074 threshold = use_condition->threshold_bytes.value;
4075 } else {
4076 /*
4077 * Threshold was expressed as a ratio.
4078 *
4079 * TODO the threshold (in bytes) of conditions expressed
4080 * as a ratio of total buffer size could be cached to
4081 * forego this double-multiplication or it could be performed
4082 * as fixed-point math.
4083 *
d49487dc 4084 * Note that caching should accommodates the case where the
ab0ee2ca
JG
4085 * condition applies to multiple channels (i.e. don't assume
4086 * that all channels matching my_chann* have the same size...)
4087 */
4088 threshold = (uint64_t) (use_condition->threshold_ratio.value *
4089 (double) buffer_capacity);
4090 }
4091
4092 condition_type = lttng_condition_get_type(condition);
4093 if (condition_type == LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW) {
4094 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64 ", highest usage = %" PRIu64,
4095 threshold, sample->highest_usage);
4096
4097 /*
4098 * The low condition should only be triggered once _all_ of the
4099 * streams in a channel have gone below the "low" threshold.
4100 */
4101 if (sample->highest_usage <= threshold) {
4102 result = true;
4103 }
4104 } else {
4105 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64 ", highest usage = %" PRIu64,
4106 threshold, sample->highest_usage);
4107
4108 /*
4109 * For high buffer usage scenarios, we want to trigger whenever
4110 * _any_ of the streams has reached the "high" threshold.
4111 */
4112 if (sample->highest_usage >= threshold) {
4113 result = true;
4114 }
4115 }
e8360425 4116
ab0ee2ca
JG
4117 return result;
4118}
4119
4120static
e8360425
JD
4121bool evaluate_session_consumed_size_condition(
4122 const struct lttng_condition *condition,
4123 uint64_t session_consumed_size)
4124{
4125 uint64_t threshold;
4126 const struct lttng_condition_session_consumed_size *size_condition =
4127 container_of(condition,
4128 struct lttng_condition_session_consumed_size,
4129 parent);
4130
4131 threshold = size_condition->consumed_threshold_bytes.value;
4132 DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64 ", current size = %" PRIu64,
4133 threshold, session_consumed_size);
4134 return session_consumed_size >= threshold;
4135}
4136
4137static
ed327204 4138int evaluate_buffer_condition(const struct lttng_condition *condition,
ab0ee2ca 4139 struct lttng_evaluation **evaluation,
e8360425
JD
4140 const struct notification_thread_state *state,
4141 const struct channel_state_sample *previous_sample,
4142 const struct channel_state_sample *latest_sample,
4143 uint64_t previous_session_consumed_total,
4144 uint64_t latest_session_consumed_total,
4145 struct channel_info *channel_info)
ab0ee2ca
JG
4146{
4147 int ret = 0;
4148 enum lttng_condition_type condition_type;
e8360425
JD
4149 const bool previous_sample_available = !!previous_sample;
4150 bool previous_sample_result = false;
ab0ee2ca
JG
4151 bool latest_sample_result;
4152
4153 condition_type = lttng_condition_get_type(condition);
ab0ee2ca 4154
e8360425
JD
4155 switch (condition_type) {
4156 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
4157 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
4158 if (caa_likely(previous_sample_available)) {
4159 previous_sample_result =
4160 evaluate_buffer_usage_condition(condition,
4161 previous_sample, channel_info->capacity);
4162 }
4163 latest_sample_result = evaluate_buffer_usage_condition(
4164 condition, latest_sample,
4165 channel_info->capacity);
4166 break;
4167 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
4168 if (caa_likely(previous_sample_available)) {
4169 previous_sample_result =
4170 evaluate_session_consumed_size_condition(
4171 condition,
4172 previous_session_consumed_total);
4173 }
4174 latest_sample_result =
4175 evaluate_session_consumed_size_condition(
4176 condition,
4177 latest_session_consumed_total);
4178 break;
4179 default:
4180 /* Unknown condition type; internal error. */
4181 abort();
4182 }
ab0ee2ca
JG
4183
4184 if (!latest_sample_result ||
4185 (previous_sample_result == latest_sample_result)) {
4186 /*
4187 * Only trigger on a condition evaluation transition.
4188 *
4189 * NOTE: This edge-triggered logic may not be appropriate for
4190 * future condition types.
4191 */
4192 goto end;
4193 }
4194
e8360425
JD
4195 if (!evaluation || !latest_sample_result) {
4196 goto end;
4197 }
4198
4199 switch (condition_type) {
4200 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
4201 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
ab0ee2ca
JG
4202 *evaluation = lttng_evaluation_buffer_usage_create(
4203 condition_type,
4204 latest_sample->highest_usage,
e8360425
JD
4205 channel_info->capacity);
4206 break;
4207 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
4208 *evaluation = lttng_evaluation_session_consumed_size_create(
e8360425
JD
4209 latest_session_consumed_total);
4210 break;
4211 default:
4212 abort();
4213 }
4214
4215 if (!*evaluation) {
4216 ret = -1;
4217 goto end;
ab0ee2ca
JG
4218 }
4219end:
4220 return ret;
4221}
4222
4223static
f2b3ef9f 4224int client_notification_overflow(struct notification_client *client)
ab0ee2ca 4225{
f2b3ef9f
JG
4226 int ret = 0;
4227 const struct lttng_notification_channel_message msg = {
ab0ee2ca 4228 .type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED,
ab0ee2ca
JG
4229 };
4230
505b2d90
JG
4231 ASSERT_LOCKED(client->lock);
4232
f2b3ef9f
JG
4233 DBG("Dropping notification addressed to client (socket fd = %i)",
4234 client->socket);
4235 if (client->communication.outbound.dropped_notification) {
4236 /*
4237 * The client already has a "notification dropped" message
4238 * in its outgoing queue. Nothing to do since all
4239 * of those messages are coalesced.
4240 */
4241 goto end;
4242 }
4243
4244 client->communication.outbound.dropped_notification = true;
ab0ee2ca 4245 ret = lttng_dynamic_buffer_append(
882093ee 4246 &client->communication.outbound.payload.buffer, &msg,
ab0ee2ca 4247 sizeof(msg));
f2b3ef9f
JG
4248 if (ret) {
4249 PERROR("Failed to enqueue \"dropped notification\" message in client's (socket fd = %i) outgoing queue",
4250 client->socket);
4251 }
4252end:
ab0ee2ca
JG
4253 return ret;
4254}
4255
f2b3ef9f
JG
4256static int client_handle_transmission_status_wrapper(
4257 struct notification_client *client,
4258 enum client_transmission_status status,
4259 void *user_data)
4260{
4261 return client_handle_transmission_status(client, status,
4262 (struct notification_thread_state *) user_data);
4263}
4264
4265static
4266int send_evaluation_to_clients(const struct lttng_trigger *trigger,
4267 const struct lttng_evaluation *evaluation,
4268 struct notification_client_list* client_list,
4269 struct notification_thread_state *state,
4270 uid_t object_uid, gid_t object_gid)
4271{
ff588497
JR
4272 const struct lttng_credentials creds = {
4273 .uid = LTTNG_OPTIONAL_INIT_VALUE(object_uid),
4274 .gid = LTTNG_OPTIONAL_INIT_VALUE(object_gid),
4275 };
4276
f2b3ef9f 4277 return notification_client_list_send_evaluation(client_list,
52d55cf9 4278 trigger, evaluation,
ff588497 4279 &creds,
f2b3ef9f
JG
4280 client_handle_transmission_status_wrapper, state);
4281}
4282
f37d0f86
JG
4283/*
4284 * Permission checks relative to notification channel clients are performed
4285 * here. Notice how object, client, and trigger credentials are involved in
4286 * this check.
4287 *
4288 * The `object` credentials are the credentials associated with the "subject"
4289 * of a condition. For instance, a `rotation completed` condition applies
4290 * to a session. When that condition is met, it will produce an evaluation
4291 * against a session. Hence, in this case, the `object` credentials are the
4292 * credentials of the "subject" session.
4293 *
4294 * The `trigger` credentials are the credentials of the user that registered the
4295 * trigger.
4296 *
4297 * The `client` credentials are the credentials of the user that created a given
4298 * notification channel.
4299 *
4300 * In terms of visibility, it is expected that non-privilieged users can only
4301 * register triggers against "their" objects (their own sessions and
4302 * applications they are allowed to interact with). They can then open a
4303 * notification channel and subscribe to notifications associated with those
4304 * triggers.
4305 *
4306 * As for privilieged users, they can register triggers against the objects of
4307 * other users. They can then subscribe to the notifications associated to their
4308 * triggers. Privilieged users _can't_ subscribe to the notifications of
4309 * triggers owned by other users; they must create their own triggers.
4310 *
4311 * This is more a concern of usability than security. It would be difficult for
4312 * a root user reliably subscribe to a specific set of conditions without
4313 * interference from external users (those could, for instance, unregister
4314 * their triggers).
4315 */
f2b3ef9f
JG
4316LTTNG_HIDDEN
4317int notification_client_list_send_evaluation(
4318 struct notification_client_list *client_list,
52d55cf9 4319 const struct lttng_trigger *trigger,
9b63a4aa 4320 const struct lttng_evaluation *evaluation,
f2b3ef9f
JG
4321 const struct lttng_credentials *source_object_creds,
4322 report_client_transmission_result_cb client_report,
4323 void *user_data)
ab0ee2ca
JG
4324{
4325 int ret = 0;
c0a66c84 4326 struct lttng_payload msg_payload;
ab0ee2ca 4327 struct notification_client_list_element *client_list_element, *tmp;
9b63a4aa 4328 const struct lttng_notification notification = {
52d55cf9 4329 .trigger = (struct lttng_trigger *) trigger,
9b63a4aa
JG
4330 .evaluation = (struct lttng_evaluation *) evaluation,
4331 };
3647288f
JG
4332 struct lttng_notification_channel_message msg_header = {
4333 .type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION,
4334 };
52d55cf9
JG
4335 const struct lttng_credentials *trigger_creds =
4336 lttng_trigger_get_credentials(trigger);
ab0ee2ca 4337
c0a66c84 4338 lttng_payload_init(&msg_payload);
ab0ee2ca 4339
c0a66c84 4340 ret = lttng_dynamic_buffer_append(&msg_payload.buffer, &msg_header,
3647288f 4341 sizeof(msg_header));
ab0ee2ca
JG
4342 if (ret) {
4343 goto end;
4344 }
4345
c0a66c84 4346 ret = lttng_notification_serialize(&notification, &msg_payload);
ab0ee2ca 4347 if (ret) {
ab0ee2ca
JG
4348 ERR("[notification-thread] Failed to serialize notification");
4349 ret = -1;
4350 goto end;
4351 }
4352
3647288f 4353 /* Update payload size. */
505b2d90
JG
4354 ((struct lttng_notification_channel_message *) msg_payload.buffer.data)
4355 ->size = (uint32_t)(
4356 msg_payload.buffer.size - sizeof(msg_header));
3647288f 4357
882093ee
JR
4358 /* Update the payload number of fds. */
4359 {
4360 const struct lttng_payload_view pv = lttng_payload_view_from_payload(
4361 &msg_payload, 0, -1);
4362
4363 ((struct lttng_notification_channel_message *)
4364 msg_payload.buffer.data)->fds = (uint32_t)
4365 lttng_payload_view_get_fd_handle_count(&pv);
4366 }
4367
505b2d90 4368 pthread_mutex_lock(&client_list->lock);
ab0ee2ca 4369 cds_list_for_each_entry_safe(client_list_element, tmp,
091fa780 4370 &client_list->clients_list, node) {
f2b3ef9f 4371 enum client_transmission_status transmission_status;
ab0ee2ca
JG
4372 struct notification_client *client =
4373 client_list_element->client;
4374
505b2d90
JG
4375 ret = 0;
4376 pthread_mutex_lock(&client->lock);
6c24d3fd
JG
4377 if (!client->communication.active) {
4378 /*
4379 * Skip inactive client (protocol error or
4380 * disconnecting).
4381 */
4382 DBG("Skipping client at it is marked as inactive");
4383 goto skip_client;
4384 }
4385
f2b3ef9f 4386 if (source_object_creds) {
ff588497
JR
4387 if (client->uid != lttng_credentials_get_uid(source_object_creds) &&
4388 client->gid != lttng_credentials_get_gid(source_object_creds) &&
f2b3ef9f
JG
4389 client->uid != 0) {
4390 /*
4391 * Client is not allowed to monitor this
4392 * object.
4393 */
4394 DBG("[notification-thread] Skipping client at it does not have the object permission to receive notification for this trigger");
6c24d3fd 4395 goto skip_client;
f2b3ef9f 4396 }
90843f49
JR
4397 }
4398
f712e5f6 4399 if (client->uid != lttng_credentials_get_uid(trigger_creds)) {
90843f49 4400 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this trigger");
6c24d3fd 4401 goto skip_client;
ab0ee2ca
JG
4402 }
4403
4404 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
c0a66c84 4405 client->socket, msg_payload.buffer.size);
882093ee
JR
4406
4407 if (client_has_outbound_data_left(client)) {
ab0ee2ca
JG
4408 /*
4409 * Outgoing data is already buffered for this client;
4410 * drop the notification and enqueue a "dropped
4411 * notification" message if this is the first dropped
4412 * notification since the socket spilled-over to the
4413 * queue.
4414 */
f2b3ef9f
JG
4415 ret = client_notification_overflow(client);
4416 if (ret) {
6c24d3fd
JG
4417 /* Fatal error. */
4418 goto skip_client;
ab0ee2ca 4419 }
ab0ee2ca
JG
4420 }
4421
882093ee 4422 ret = lttng_payload_copy(&msg_payload, &client->communication.outbound.payload);
ab0ee2ca 4423 if (ret) {
6c24d3fd
JG
4424 /* Fatal error. */
4425 goto skip_client;
ab0ee2ca
JG
4426 }
4427
f2b3ef9f 4428 transmission_status = client_flush_outgoing_queue(client);
6c24d3fd 4429 pthread_mutex_unlock(&client->lock);
f2b3ef9f 4430 ret = client_report(client, transmission_status, user_data);
ab0ee2ca 4431 if (ret) {
6c24d3fd
JG
4432 /* Fatal error. */
4433 goto end_unlock_list;
505b2d90 4434 }
6c24d3fd
JG
4435
4436 continue;
4437
4438skip_client:
505b2d90
JG
4439 pthread_mutex_unlock(&client->lock);
4440 if (ret) {
6c24d3fd 4441 /* Fatal error. */
505b2d90 4442 goto end_unlock_list;
ab0ee2ca
JG
4443 }
4444 }
4445 ret = 0;
505b2d90
JG
4446
4447end_unlock_list:
4448 pthread_mutex_unlock(&client_list->lock);
ab0ee2ca 4449end:
c0a66c84 4450 lttng_payload_reset(&msg_payload);
ab0ee2ca
JG
4451 return ret;
4452}
4453
8b524060
FD
4454static
4455struct lttng_event_notifier_notification *recv_one_event_notifier_notification(
b9a8d78f 4456 int notification_pipe_read_fd, enum lttng_domain_type domain)
94078603
JR
4457{
4458 int ret;
b9a8d78f
FD
4459 uint64_t token;
4460 struct lttng_event_notifier_notification *notification = NULL;
82b3cbf4
JR
4461 char *capture_buffer = NULL;
4462 size_t capture_buffer_size;
94078603
JR
4463 void *reception_buffer;
4464 size_t reception_size;
4465
fc4b93fa 4466 struct lttng_ust_abi_event_notifier_notification ust_notification;
b9a8d78f 4467 struct lttng_kernel_event_notifier_notification kernel_notification;
94078603 4468
b9a8d78f 4469 /* Init lttng_event_notifier_notification */
94078603
JR
4470 switch(domain) {
4471 case LTTNG_DOMAIN_UST:
4472 reception_buffer = (void *) &ust_notification;
4473 reception_size = sizeof(ust_notification);
94078603
JR
4474 break;
4475 case LTTNG_DOMAIN_KERNEL:
4476 reception_buffer = (void *) &kernel_notification;
4477 reception_size = sizeof(kernel_notification);
94078603
JR
4478 break;
4479 default:
4480 abort();
4481 }
4482
4483 /*
4484 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4485 * ensuring that read/write of tracer notifications are atomic.
4486 */
4487 ret = lttng_read(notification_pipe_read_fd, reception_buffer,
4488 reception_size);
4489 if (ret != reception_size) {
4490 PERROR("Failed to read from event source notification pipe: fd = %d, size to read = %zu, ret = %d",
4491 notification_pipe_read_fd, reception_size, ret);
4492 ret = -1;
4493 goto end;
4494 }
4495
4496 switch(domain) {
4497 case LTTNG_DOMAIN_UST:
b9a8d78f 4498 token = ust_notification.token;
82b3cbf4 4499 capture_buffer_size = ust_notification.capture_buf_size;
94078603
JR
4500 break;
4501 case LTTNG_DOMAIN_KERNEL:
b9a8d78f 4502 token = kernel_notification.token;
bbd6675c 4503 capture_buffer_size = kernel_notification.capture_buf_size;
94078603
JR
4504 break;
4505 default:
4506 abort();
4507 }
4508
82b3cbf4
JR
4509 if (capture_buffer_size == 0) {
4510 capture_buffer = NULL;
4511 goto skip_capture;
4512 }
4513
4514 if (capture_buffer_size > MAX_CAPTURE_SIZE) {
4515 ERR("[notification-thread] Event notifier has a capture payload size which exceeds the maximum allowed size: capture_payload_size = %zu bytes, max allowed size = %d bytes",
4516 capture_buffer_size, MAX_CAPTURE_SIZE);
4517 goto end;
4518 }
4519
4520 capture_buffer = zmalloc(capture_buffer_size);
4521 if (!capture_buffer) {
4522 ERR("[notification-thread] Failed to allocate capture buffer");
4523 goto end;
4524 }
4525
4526 /* Fetch additional payload (capture). */
4527 ret = lttng_read(notification_pipe_read_fd, capture_buffer, capture_buffer_size);
4528 if (ret != capture_buffer_size) {
4529 ERR("[notification-thread] Failed to read from event source pipe (fd = %i)",
4530 notification_pipe_read_fd);
4531 goto end;
4532 }
4533
4534skip_capture:
4535 notification = lttng_event_notifier_notification_create(token, domain,
4536 capture_buffer, capture_buffer_size);
4537 if (notification == NULL) {
4538 goto end;
4539 }
4540
4541 /*
4542 * Ownership transfered to the lttng_event_notifier_notification object.
4543 */
4544 capture_buffer = NULL;
4545
b9a8d78f 4546end:
82b3cbf4 4547 free(capture_buffer);
b9a8d78f
FD
4548 return notification;
4549}
4550
8b524060
FD
4551static
4552int dispatch_one_event_notifier_notification(struct notification_thread_state *state,
4553 struct lttng_event_notifier_notification *notification)
b9a8d78f 4554{
b9a8d78f
FD
4555 struct cds_lfht_node *node;
4556 struct cds_lfht_iter iter;
4557 struct notification_trigger_tokens_ht_element *element;
4558 struct lttng_evaluation *evaluation = NULL;
b9a8d78f
FD
4559 enum action_executor_status executor_status;
4560 struct notification_client_list *client_list = NULL;
8b524060 4561 int ret;
7c920b63 4562 unsigned int capture_count = 0;
b9a8d78f 4563
94078603
JR
4564 /* Find triggers associated with this token. */
4565 rcu_read_lock();
4566 cds_lfht_lookup(state->trigger_tokens_ht,
b9a8d78f
FD
4567 hash_key_u64(&notification->tracer_token, lttng_ht_seed),
4568 match_trigger_token, &notification->tracer_token, &iter);
94078603 4569 node = cds_lfht_iter_get_node(&iter);
f1446131 4570 if (caa_unlikely(!node)) {
94078603 4571 /*
f1446131
JR
4572 * This is not an error, slow consumption of the tracer
4573 * notifications can lead to situations where a trigger is
4574 * removed but we still get tracer notifications matching a
4575 * trigger that no longer exists.
94078603
JR
4576 */
4577 ret = 0;
4578 goto end_unlock;
4579 }
4580
4581 element = caa_container_of(node,
4582 struct notification_trigger_tokens_ht_element,
4583 node);
4584
8dbb86b8 4585 if (lttng_condition_event_rule_matches_get_capture_descriptor_count(
7c920b63
PP
4586 lttng_trigger_get_const_condition(element->trigger),
4587 &capture_count) != LTTNG_CONDITION_STATUS_OK) {
4588 ERR("Failed to get capture count");
4589 ret = -1;
4590 goto end;
4591 }
4592
4593 if (!notification->capture_buffer && capture_count != 0) {
4594 ERR("Expected capture but capture buffer is null");
4595 ret = -1;
4596 goto end;
4597 }
4598
8dbb86b8 4599 evaluation = lttng_evaluation_event_rule_matches_create(
7c920b63
PP
4600 container_of(lttng_trigger_get_const_condition(
4601 element->trigger),
8dbb86b8 4602 struct lttng_condition_event_rule_matches,
7c920b63 4603 parent),
7c920b63
PP
4604 notification->capture_buffer,
4605 notification->capture_buf_size, false);
4606
f1446131 4607 if (evaluation == NULL) {
b9a8d78f 4608 ERR("[notification-thread] Failed to create event rule hit evaluation while creating and enqueuing action executor job");
f1446131
JR
4609 ret = -1;
4610 goto end_unlock;
4611 }
f1446131
JR
4612 client_list = get_client_list_from_condition(state,
4613 lttng_trigger_get_const_condition(element->trigger));
72365501 4614 executor_status = action_executor_enqueue_trigger(state->executor,
f1446131
JR
4615 element->trigger, evaluation, NULL, client_list);
4616 switch (executor_status) {
4617 case ACTION_EXECUTOR_STATUS_OK:
4618 ret = 0;
4619 break;
4620 case ACTION_EXECUTOR_STATUS_OVERFLOW:
4621 {
4622 struct notification_client_list_element *client_list_element,
4623 *tmp;
4624
4625 /*
4626 * Not a fatal error; this is expected and simply means the
4627 * executor has too much work queued already.
4628 */
4629 ret = 0;
4630
4631 /* No clients subscribed to notifications for this trigger. */
4632 if (!client_list) {
4633 break;
4634 }
4635
4636 /* Warn clients that a notification (or more) was dropped. */
4637 pthread_mutex_lock(&client_list->lock);
4638 cds_list_for_each_entry_safe(client_list_element, tmp,
091fa780 4639 &client_list->clients_list, node) {
f1446131
JR
4640 enum client_transmission_status transmission_status;
4641 struct notification_client *client =
4642 client_list_element->client;
4643
4644 pthread_mutex_lock(&client->lock);
4645 ret = client_notification_overflow(client);
4646 if (ret) {
4647 /* Fatal error. */
4648 goto next_client;
4649 }
4650
4651 transmission_status =
4652 client_flush_outgoing_queue(client);
4653 ret = client_handle_transmission_status(
4654 client, transmission_status, state);
4655 if (ret) {
4656 /* Fatal error. */
4657 goto next_client;
4658 }
4659next_client:
4660 pthread_mutex_unlock(&client->lock);
4661 if (ret) {
4662 break;
4663 }
4664 }
4665
4666 pthread_mutex_unlock(&client_list->lock);
4667 break;
4668 }
98dfca79 4669 case ACTION_EXECUTOR_STATUS_INVALID:
f1446131
JR
4670 case ACTION_EXECUTOR_STATUS_ERROR:
4671 /* Fatal error, shut down everything. */
4672 ERR("Fatal error encoutered while enqueuing action to the action executor");
4673 ret = -1;
4674 goto end_unlock;
4675 default:
4676 /* Unhandled error. */
4677 abort();
4678 }
94078603
JR
4679
4680end_unlock:
f1446131 4681 notification_client_list_put(client_list);
94078603 4682 rcu_read_unlock();
7c920b63 4683end:
8b524060
FD
4684 return ret;
4685}
4686
4687static
4688int handle_one_event_notifier_notification(
4689 struct notification_thread_state *state,
4690 int pipe, enum lttng_domain_type domain)
4691{
82b3cbf4 4692 int ret = 0;
8b524060
FD
4693 struct lttng_event_notifier_notification *notification = NULL;
4694
4695 notification = recv_one_event_notifier_notification(pipe, domain);
4696 if (notification == NULL) {
82b3cbf4 4697 /* Reception failed, don't consider it fatal. */
8b524060
FD
4698 ERR("[notification-thread] Error receiving an event notifier notification from tracer: fd = %i, domain = %s",
4699 pipe, lttng_domain_type_str(domain));
8b524060
FD
4700 goto end;
4701 }
4702
4703 ret = dispatch_one_event_notifier_notification(state, notification);
4704 if (ret) {
4705 ERR("[notification-thread] Error dispatching an event notifier notification from tracer: fd = %i, domain = %s",
4706 pipe, lttng_domain_type_str(domain));
4707 goto end;
4708 }
4709
94078603 4710end:
8b524060 4711 lttng_event_notifier_notification_destroy(notification);
94078603
JR
4712 return ret;
4713}
4714
8b524060
FD
4715int handle_notification_thread_event_notification(struct notification_thread_state *state,
4716 int pipe, enum lttng_domain_type domain)
4717{
4718 return handle_one_event_notifier_notification(state, pipe, domain);
4719}
4720
ab0ee2ca
JG
4721int handle_notification_thread_channel_sample(
4722 struct notification_thread_state *state, int pipe,
4723 enum lttng_domain_type domain)
4724{
4725 int ret = 0;
4726 struct lttcomm_consumer_channel_monitor_msg sample_msg;
ab0ee2ca
JG
4727 struct channel_info *channel_info;
4728 struct cds_lfht_node *node;
4729 struct cds_lfht_iter iter;
4730 struct lttng_channel_trigger_list *trigger_list;
4731 struct lttng_trigger_list_element *trigger_list_element;
4732 bool previous_sample_available = false;
e8360425
JD
4733 struct channel_state_sample previous_sample, latest_sample;
4734 uint64_t previous_session_consumed_total, latest_session_consumed_total;
f2b3ef9f 4735 struct lttng_credentials channel_creds;
ab0ee2ca
JG
4736
4737 /*
4738 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4739 * ensuring that read/write of sampling messages are atomic.
4740 */
7c2551ef 4741 ret = lttng_read(pipe, &sample_msg, sizeof(sample_msg));
ab0ee2ca
JG
4742 if (ret != sizeof(sample_msg)) {
4743 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
4744 pipe);
4745 ret = -1;
4746 goto end;
4747 }
4748
4749 ret = 0;
4750 latest_sample.key.key = sample_msg.key;
4751 latest_sample.key.domain = domain;
4752 latest_sample.highest_usage = sample_msg.highest;
4753 latest_sample.lowest_usage = sample_msg.lowest;
e8360425 4754 latest_sample.channel_total_consumed = sample_msg.total_consumed;
ab0ee2ca
JG
4755
4756 rcu_read_lock();
4757
4758 /* Retrieve the channel's informations */
4759 cds_lfht_lookup(state->channels_ht,
4760 hash_channel_key(&latest_sample.key),
4761 match_channel_info,
4762 &latest_sample.key,
4763 &iter);
4764 node = cds_lfht_iter_get_node(&iter);
e0b5f87b 4765 if (caa_unlikely(!node)) {
ab0ee2ca
JG
4766 /*
4767 * Not an error since the consumer can push a sample to the pipe
4768 * and the rest of the session daemon could notify us of the
4769 * channel's destruction before we get a chance to process that
4770 * sample.
4771 */
4772 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64 " in %s domain",
4773 latest_sample.key.key,
526200e4 4774 lttng_domain_type_str(domain));
ab0ee2ca
JG
4775 goto end_unlock;
4776 }
4777 channel_info = caa_container_of(node, struct channel_info,
4778 channels_ht_node);
e8360425 4779 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64 ") in session %s (highest usage = %" PRIu64 ", lowest usage = %" PRIu64", total consumed = %" PRIu64")",
8abe313a 4780 channel_info->name,
ab0ee2ca 4781 latest_sample.key.key,
8abe313a 4782 channel_info->session_info->name,
ab0ee2ca 4783 latest_sample.highest_usage,
e8360425
JD
4784 latest_sample.lowest_usage,
4785 latest_sample.channel_total_consumed);
4786
4787 previous_session_consumed_total =
4788 channel_info->session_info->consumed_data_size;
ab0ee2ca
JG
4789
4790 /* Retrieve the channel's last sample, if it exists, and update it. */
4791 cds_lfht_lookup(state->channel_state_ht,
4792 hash_channel_key(&latest_sample.key),
4793 match_channel_state_sample,
4794 &latest_sample.key,
4795 &iter);
4796 node = cds_lfht_iter_get_node(&iter);
e0b5f87b 4797 if (caa_likely(node)) {
ab0ee2ca
JG
4798 struct channel_state_sample *stored_sample;
4799
4800 /* Update the sample stored. */
4801 stored_sample = caa_container_of(node,
4802 struct channel_state_sample,
4803 channel_state_ht_node);
e8360425 4804
ab0ee2ca
JG
4805 memcpy(&previous_sample, stored_sample,
4806 sizeof(previous_sample));
4807 stored_sample->highest_usage = latest_sample.highest_usage;
4808 stored_sample->lowest_usage = latest_sample.lowest_usage;
0f0479d7 4809 stored_sample->channel_total_consumed = latest_sample.channel_total_consumed;
ab0ee2ca 4810 previous_sample_available = true;
e8360425
JD
4811
4812 latest_session_consumed_total =
4813 previous_session_consumed_total +
4814 (latest_sample.channel_total_consumed - previous_sample.channel_total_consumed);
ab0ee2ca
JG
4815 } else {
4816 /*
4817 * This is the channel's first sample, allocate space for and
4818 * store the new sample.
4819 */
4820 struct channel_state_sample *stored_sample;
4821
4822 stored_sample = zmalloc(sizeof(*stored_sample));
4823 if (!stored_sample) {
4824 ret = -1;
4825 goto end_unlock;
4826 }
4827
4828 memcpy(stored_sample, &latest_sample, sizeof(*stored_sample));
4829 cds_lfht_node_init(&stored_sample->channel_state_ht_node);
4830 cds_lfht_add(state->channel_state_ht,
4831 hash_channel_key(&stored_sample->key),
4832 &stored_sample->channel_state_ht_node);
e8360425
JD
4833
4834 latest_session_consumed_total =
4835 previous_session_consumed_total +
4836 latest_sample.channel_total_consumed;
ab0ee2ca
JG
4837 }
4838
e8360425
JD
4839 channel_info->session_info->consumed_data_size =
4840 latest_session_consumed_total;
4841
ab0ee2ca
JG
4842 /* Find triggers associated with this channel. */
4843 cds_lfht_lookup(state->channel_triggers_ht,
4844 hash_channel_key(&latest_sample.key),
4845 match_channel_trigger_list,
4846 &latest_sample.key,
4847 &iter);
4848 node = cds_lfht_iter_get_node(&iter);
e0b5f87b 4849 if (caa_likely(!node)) {
ab0ee2ca
JG
4850 goto end_unlock;
4851 }
4852
f2b3ef9f 4853 channel_creds = (typeof(channel_creds)) {
ff588497
JR
4854 .uid = LTTNG_OPTIONAL_INIT_VALUE(channel_info->session_info->uid),
4855 .gid = LTTNG_OPTIONAL_INIT_VALUE(channel_info->session_info->gid),
f2b3ef9f
JG
4856 };
4857
ab0ee2ca
JG
4858 trigger_list = caa_container_of(node, struct lttng_channel_trigger_list,
4859 channel_triggers_ht_node);
4860 cds_list_for_each_entry(trigger_list_element, &trigger_list->list,
e742b055 4861 node) {
9b63a4aa 4862 const struct lttng_condition *condition;
f2b3ef9f 4863 struct lttng_trigger *trigger;
505b2d90 4864 struct notification_client_list *client_list = NULL;
ab0ee2ca 4865 struct lttng_evaluation *evaluation = NULL;
f2b3ef9f 4866 enum action_executor_status executor_status;
ab0ee2ca 4867
505b2d90 4868 ret = 0;
ab0ee2ca 4869 trigger = trigger_list_element->trigger;
9b63a4aa 4870 condition = lttng_trigger_get_const_condition(trigger);
ab0ee2ca 4871 assert(condition);
ab0ee2ca
JG
4872
4873 /*
4874 * Check if any client is subscribed to the result of this
4875 * evaluation.
4876 */
ed327204 4877 client_list = get_client_list_from_condition(state, condition);
ab0ee2ca 4878
ed327204 4879 ret = evaluate_buffer_condition(condition, &evaluation, state,
ab0ee2ca 4880 previous_sample_available ? &previous_sample : NULL,
e8360425
JD
4881 &latest_sample,
4882 previous_session_consumed_total,
4883 latest_session_consumed_total,
4884 channel_info);
4885 if (caa_unlikely(ret)) {
505b2d90 4886 goto put_list;
ab0ee2ca
JG
4887 }
4888
e8360425 4889 if (caa_likely(!evaluation)) {
505b2d90 4890 goto put_list;
ab0ee2ca
JG
4891 }
4892
f2b3ef9f
JG
4893 /*
4894 * Ownership of `evaluation` transferred to the action executor
4895 * no matter the result.
4896 */
72365501
JR
4897 executor_status = action_executor_enqueue_trigger(
4898 state->executor, trigger, evaluation,
4899 &channel_creds, client_list);
f2b3ef9f
JG
4900 evaluation = NULL;
4901 switch (executor_status) {
4902 case ACTION_EXECUTOR_STATUS_OK:
4903 break;
4904 case ACTION_EXECUTOR_STATUS_ERROR:
4905 case ACTION_EXECUTOR_STATUS_INVALID:
4906 /*
4907 * TODO Add trigger identification (name/id) when
4908 * it is added to the API.
4909 */
4910 ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger");
4911 ret = -1;
4912 goto put_list;
4913 case ACTION_EXECUTOR_STATUS_OVERFLOW:
4914 /*
4915 * TODO Add trigger identification (name/id) when
4916 * it is added to the API.
4917 *
4918 * Not a fatal error.
4919 */
4920 WARN("No space left when enqueuing action associated with buffer-condition trigger");
4921 ret = 0;
4922 goto put_list;
4923 default:
4924 abort();
4925 }
4926
505b2d90
JG
4927put_list:
4928 notification_client_list_put(client_list);
e0b5f87b 4929 if (caa_unlikely(ret)) {
505b2d90 4930 break;
ab0ee2ca
JG
4931 }
4932 }
4933end_unlock:
4934 rcu_read_unlock();
4935end:
4936 return ret;
4937}
This page took 0.311629 seconds and 4 git commands to generate.