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