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