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