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