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