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_enc