2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
11 #include "lttng-sessiond.hpp"
12 #include "session.hpp"
14 #include "trace-ust.hpp"
17 #include <common/common.hpp>
18 #include <common/sessiond-comm/sessiond-comm.hpp>
19 #include <common/trace-chunk.hpp>
20 #include <common/urcu.hpp>
21 #include <common/utils.hpp>
23 #include <lttng/location-internal.hpp>
33 #include <sys/types.h>
37 struct ltt_session_destroy_notifier_element
{
38 ltt_session_destroy_notifier notifier
;
42 struct ltt_session_clear_notifier_element
{
43 ltt_session_clear_notifier notifier
;
47 namespace ls
= lttng::sessiond
;
52 * No ltt_session.lock is taken here because those data structure are widely
53 * spread across the lttng-tools code base so before calling functions below
54 * that can read/write a session, the caller MUST acquire the session lock
55 * using session_lock() and session_unlock().
58 /* These characters are forbidden in a session name. Used by validate_name. */
59 const char *forbidden_name_chars
= "/";
61 /* Global hash table to keep the sessions, indexed by id. */
62 struct lttng_ht
*ltt_sessions_ht_by_id
= nullptr;
63 /* Global hash table to keep the sessions, indexed by name. */
64 struct lttng_ht
*ltt_sessions_ht_by_name
= nullptr;
67 * Init tracing session list.
69 * Please see session.h for more explanation and correct usage of the list.
71 struct ltt_session_list the_session_list
;
74 * Return a ltt_session structure ptr that matches name. If no session found,
75 * NULL is returned. This must be called with the session list lock held using
76 * session_lock_list and session_unlock_list.
77 * A reference to the session is implicitly acquired by this function.
79 struct ltt_session
*session_find_by_name(const char *name
)
81 struct ltt_session
*iter
;
84 ASSERT_SESSION_LIST_LOCKED();
86 DBG2("Trying to find session by name %s", name
);
88 cds_list_for_each_entry (iter
, &the_session_list
.head
, list
) {
89 if (!strncmp(iter
->name
, name
, NAME_MAX
) && !iter
->destroyed
) {
96 return session_get(iter
) ? iter
: nullptr;
100 * Return an ltt_session that matches the id. If no session is found,
101 * NULL is returned. This must be called with rcu_read_lock and
102 * session list lock held (to guarantee the lifetime of the session).
104 struct ltt_session
*session_find_by_id(uint64_t id
)
106 struct lttng_ht_node_u64
*node
;
107 struct lttng_ht_iter iter
;
108 struct ltt_session
*ls
;
110 ASSERT_RCU_READ_LOCKED();
111 ASSERT_SESSION_LIST_LOCKED();
113 if (!ltt_sessions_ht_by_id
) {
117 lttng_ht_lookup(ltt_sessions_ht_by_id
, &id
, &iter
);
118 node
= lttng_ht_iter_get_node_u64(&iter
);
119 if (node
== nullptr) {
122 ls
= lttng::utils::container_of(node
, <t_session::node
);
124 DBG3("Session %" PRIu64
" found by id.", id
);
125 return session_get(ls
) ? ls
: nullptr;
128 DBG3("Session %" PRIu64
" NOT found by id", id
);
134 * Validate the session name for forbidden characters.
136 * Return 0 on success else -1 meaning a forbidden char. has been found.
138 static int validate_name(const char *name
)
141 char *tok
, *tmp_name
;
145 tmp_name
= strdup(name
);
152 tok
= strpbrk(tmp_name
, forbidden_name_chars
);
154 DBG("Session name %s contains a forbidden character", name
);
155 /* Forbidden character has been found. */
167 * Add a ltt_session structure to the global list.
169 * The caller MUST acquire the session list lock before.
170 * Returns the unique identifier for the session.
172 static uint64_t add_session_list(struct ltt_session
*ls
)
176 cds_list_add(&ls
->list
, &the_session_list
.head
);
177 return the_session_list
.next_uuid
++;
181 * Delete a ltt_session structure to the global list.
183 * The caller MUST acquire the session list lock before.
185 static void del_session_list(struct ltt_session
*ls
)
189 cds_list_del(&ls
->list
);
193 * Return a pointer to the session list.
195 struct ltt_session_list
*session_get_list()
197 return &the_session_list
;
201 * Returns once the session list is empty.
203 void session_list_wait_empty(std::unique_lock
<std::mutex
> list_lock
)
205 /* Keep waiting until the session list is empty. */
206 the_session_list
.removal_cond
.wait(list_lock
,
207 [] { return cds_list_empty(&the_session_list
.head
); });
211 * Try to acquire session list lock
213 int session_trylock_list() noexcept
215 /* Return 0 if successfully acquired. */
216 return the_session_list
.lock
.try_lock() ? 0 : 1;
220 * Get the session's consumer destination type.
222 * The caller must hold the session lock.
224 enum consumer_dst_type
session_get_consumer_destination_type(const struct ltt_session
*session
)
227 * The output information is duplicated in both of those session types.
228 * Hence, it doesn't matter from which it is retrieved. However, it is
229 * possible for only one of them to be set.
231 return session
->kernel_session
? session
->kernel_session
->consumer
->type
:
232 session
->ust_session
->consumer
->type
;
236 * Get the session's consumer network hostname.
237 * The caller must ensure that the destination is of type "net".
239 * The caller must hold the session lock.
241 const char *session_get_net_consumer_hostname(const struct ltt_session
*session
)
243 const char *hostname
= nullptr;
244 const struct consumer_output
*output
;
246 output
= session
->kernel_session
? session
->kernel_session
->consumer
:
247 session
->ust_session
->consumer
;
250 * hostname is assumed to be the same for both control and data
253 switch (output
->dst
.net
.control
.dtype
) {
255 hostname
= output
->dst
.net
.control
.dst
.ipv4
;
258 hostname
= output
->dst
.net
.control
.dst
.ipv6
;
267 * Get the session's consumer network control and data ports.
268 * The caller must ensure that the destination is of type "net".
270 * The caller must hold the session lock.
272 void session_get_net_consumer_ports(const struct ltt_session
*session
,
273 uint16_t *control_port
,
276 const struct consumer_output
*output
;
278 output
= session
->kernel_session
? session
->kernel_session
->consumer
:
279 session
->ust_session
->consumer
;
280 *control_port
= output
->dst
.net
.control
.port
;
281 *data_port
= output
->dst
.net
.data
.port
;
285 * Get the location of the latest trace archive produced by a rotation.
287 * The caller must hold the session lock.
289 struct lttng_trace_archive_location
*
290 session_get_trace_archive_location(const struct ltt_session
*session
)
293 struct lttng_trace_archive_location
*location
= nullptr;
294 char *chunk_path
= nullptr;
296 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
||
297 !session
->last_archived_chunk_name
) {
301 switch (session_get_consumer_destination_type(session
)) {
302 case CONSUMER_DST_LOCAL
:
303 ret
= asprintf(&chunk_path
,
304 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
305 session_get_base_path(session
),
306 session
->last_archived_chunk_name
);
310 location
= lttng_trace_archive_location_local_create(chunk_path
);
312 case CONSUMER_DST_NET
:
314 const char *hostname
;
315 uint16_t control_port
, data_port
;
317 hostname
= session_get_net_consumer_hostname(session
);
318 session_get_net_consumer_ports(session
, &control_port
, &data_port
);
319 location
= lttng_trace_archive_location_relay_create(
321 LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
,
324 session
->last_chunk_path
);
336 * Allocate the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name HT.
338 * The session list lock must be held.
340 static int ltt_sessions_ht_alloc()
344 DBG("Allocating ltt_sessions_ht_by_id");
345 ltt_sessions_ht_by_id
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
346 if (!ltt_sessions_ht_by_id
) {
348 ERR("Failed to allocate ltt_sessions_ht_by_id");
352 DBG("Allocating ltt_sessions_ht_by_name");
353 ltt_sessions_ht_by_name
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
354 if (!ltt_sessions_ht_by_name
) {
356 ERR("Failed to allocate ltt_sessions_ht_by_name");
365 * Destroy the ltt_sessions_ht_by_id HT.
367 * The session list lock must be held.
369 static void ltt_sessions_ht_destroy()
371 if (ltt_sessions_ht_by_id
) {
372 lttng_ht_destroy(ltt_sessions_ht_by_id
);
373 ltt_sessions_ht_by_id
= nullptr;
376 if (ltt_sessions_ht_by_name
) {
377 lttng_ht_destroy(ltt_sessions_ht_by_name
);
378 ltt_sessions_ht_by_name
= nullptr;
385 * Add a ltt_session to the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name.
386 * If unallocated, the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name. HTs
387 * are allocated. The session list lock must be held.
389 static void add_session_ht(struct ltt_session
*ls
)
395 if (!ltt_sessions_ht_by_id
) {
396 ret
= ltt_sessions_ht_alloc();
398 ERR("Error allocating the sessions HT");
403 /* Should always be present with ltt_sessions_ht_by_id. */
404 LTTNG_ASSERT(ltt_sessions_ht_by_name
);
406 lttng_ht_node_init_u64(&ls
->node
, ls
->id
);
407 lttng_ht_add_unique_u64(ltt_sessions_ht_by_id
, &ls
->node
);
409 lttng_ht_node_init_str(&ls
->node_by_name
, ls
->name
);
410 lttng_ht_add_unique_str(ltt_sessions_ht_by_name
, &ls
->node_by_name
);
417 * Test if ltt_sessions_ht_by_id/name are empty.
418 * Return `false` if hash table objects are null.
419 * The session list lock must be held.
421 static bool ltt_sessions_ht_empty()
425 if (!ltt_sessions_ht_by_id
) {
426 /* The hash tables do not exist yet. */
430 LTTNG_ASSERT(ltt_sessions_ht_by_name
);
432 if (lttng_ht_get_count(ltt_sessions_ht_by_id
) != 0) {
438 * At this point it is expected that the `ltt_sessions_ht_by_name` ht is
441 * The removal from both hash tables is done in two different
443 * - removal from `ltt_sessions_ht_by_name` is done during
444 * `session_destroy()`
445 * - removal from `ltt_sessions_ht_by_id` is done later
446 * in `session_release()` on the last reference put.
448 * This means that it is possible for `ltt_sessions_ht_by_name` to be
449 * empty but for `ltt_sessions_ht_by_id` to still contain objects when
450 * multiple sessions exists. The reverse is false, hence this sanity
453 LTTNG_ASSERT(lttng_ht_get_count(ltt_sessions_ht_by_name
) == 0);
460 * Remove a ltt_session from the ltt_sessions_ht_by_id.
461 * If empty, the ltt_sessions_ht_by_id/name HTs are freed.
462 * The session list lock must be held.
464 static void del_session_ht(struct ltt_session
*ls
)
466 struct lttng_ht_iter iter
;
470 LTTNG_ASSERT(ltt_sessions_ht_by_id
);
471 LTTNG_ASSERT(ltt_sessions_ht_by_name
);
473 iter
.iter
.node
= &ls
->node
.node
;
474 ret
= lttng_ht_del(ltt_sessions_ht_by_id
, &iter
);
477 if (ltt_sessions_ht_empty()) {
478 DBG("Empty ltt_sessions_ht_by_id/name, destroying hast tables");
479 ltt_sessions_ht_destroy();
484 * Acquire session lock
486 void session_lock(struct ltt_session
*session
)
488 LTTNG_ASSERT(session
);
492 void ltt_session::lock() const noexcept
494 pthread_mutex_lock(&_lock
);
497 void ltt_session::unlock() const noexcept
499 ltt_session::_const_session_unlock(*this);
503 * Release session lock
505 void session_unlock(struct ltt_session
*session
)
507 LTTNG_ASSERT(session
);
511 void ltt_session::_const_session_unlock(const ltt_session
&session
)
513 pthread_mutex_unlock(&session
._lock
);
516 static int _session_set_trace_chunk_no_lock_check(struct ltt_session
*session
,
517 struct lttng_trace_chunk
*new_trace_chunk
,
518 struct lttng_trace_chunk
**_current_trace_chunk
)
521 unsigned int i
, refs_to_acquire
= 0, refs_acquired
= 0, refs_to_release
= 0;
522 struct cds_lfht_iter iter
;
523 struct consumer_socket
*socket
;
524 struct lttng_trace_chunk
*current_trace_chunk
;
526 enum lttng_trace_chunk_status chunk_status
;
528 lttng::urcu::read_lock_guard read_lock
;
530 * Ownership of current trace chunk is transferred to
531 * `current_trace_chunk`.
533 current_trace_chunk
= session
->current_trace_chunk
;
534 session
->current_trace_chunk
= nullptr;
535 if (session
->ust_session
) {
536 lttng_trace_chunk_put(session
->ust_session
->current_trace_chunk
);
537 session
->ust_session
->current_trace_chunk
= nullptr;
539 if (session
->kernel_session
) {
540 lttng_trace_chunk_put(session
->kernel_session
->current_trace_chunk
);
541 session
->kernel_session
->current_trace_chunk
= nullptr;
543 if (!new_trace_chunk
) {
547 chunk_status
= lttng_trace_chunk_get_id(new_trace_chunk
, &chunk_id
);
548 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
551 refs_to_acquire
+= !!session
->ust_session
;
552 refs_to_acquire
+= !!session
->kernel_session
;
554 for (refs_acquired
= 0; refs_acquired
< refs_to_acquire
; refs_acquired
++) {
555 if (!lttng_trace_chunk_get(new_trace_chunk
)) {
556 ERR("Failed to acquire reference to new trace chunk of session \"%s\"",
562 if (session
->ust_session
) {
563 const uint64_t relayd_id
= session
->ust_session
->consumer
->net_seq_index
;
564 const bool is_local_trace
= session
->ust_session
->consumer
->type
==
567 session
->ust_session
->current_trace_chunk
= new_trace_chunk
;
568 if (is_local_trace
) {
569 enum lttng_error_code ret_error_code
;
572 ust_app_create_channel_subdirectories(session
->ust_session
);
573 if (ret_error_code
!= LTTNG_OK
) {
577 cds_lfht_for_each_entry (
578 session
->ust_session
->consumer
->socks
->ht
, &iter
, socket
, node
.node
) {
579 pthread_mutex_lock(socket
->lock
);
580 ret
= consumer_create_trace_chunk(socket
,
584 DEFAULT_UST_TRACE_DIR
);
585 pthread_mutex_unlock(socket
->lock
);
591 if (session
->kernel_session
) {
592 const uint64_t relayd_id
= session
->kernel_session
->consumer
->net_seq_index
;
593 const bool is_local_trace
= session
->kernel_session
->consumer
->type
==
596 session
->kernel_session
->current_trace_chunk
= new_trace_chunk
;
597 if (is_local_trace
) {
598 enum lttng_error_code ret_error_code
;
601 kernel_create_channel_subdirectories(session
->kernel_session
);
602 if (ret_error_code
!= LTTNG_OK
) {
606 cds_lfht_for_each_entry (
607 session
->kernel_session
->consumer
->socks
->ht
, &iter
, socket
, node
.node
) {
608 pthread_mutex_lock(socket
->lock
);
609 ret
= consumer_create_trace_chunk(socket
,
613 DEFAULT_KERNEL_TRACE_DIR
);
614 pthread_mutex_unlock(socket
->lock
);
622 * Update local current trace chunk state last, only if all remote
623 * creations succeeded.
625 session
->current_trace_chunk
= new_trace_chunk
;
626 LTTNG_OPTIONAL_SET(&session
->most_recent_chunk_id
, chunk_id
);
628 if (_current_trace_chunk
) {
629 *_current_trace_chunk
= current_trace_chunk
;
630 current_trace_chunk
= nullptr;
633 lttng_trace_chunk_put(current_trace_chunk
);
636 if (session
->ust_session
) {
637 session
->ust_session
->current_trace_chunk
= nullptr;
639 if (session
->kernel_session
) {
640 session
->kernel_session
->current_trace_chunk
= nullptr;
643 * Release references taken in the case where all references could not
646 refs_to_release
= refs_to_acquire
- refs_acquired
;
647 for (i
= 0; i
< refs_to_release
; i
++) {
648 lttng_trace_chunk_put(new_trace_chunk
);
654 struct lttng_trace_chunk
*
655 session_create_new_trace_chunk(const struct ltt_session
*session
,
656 const struct consumer_output
*consumer_output_override
,
657 const char *session_base_path_override
,
658 const char *chunk_name_override
)
661 struct lttng_trace_chunk
*trace_chunk
= nullptr;
662 enum lttng_trace_chunk_status chunk_status
;
663 const time_t chunk_creation_ts
= time(nullptr);
665 const char *base_path
;
666 struct lttng_directory_handle
*session_output_directory
= nullptr;
667 const struct lttng_credentials session_credentials
= {
668 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session
->uid
),
669 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session
->gid
),
671 uint64_t next_chunk_id
;
672 const struct consumer_output
*output
;
673 const char *new_path
;
675 if (consumer_output_override
) {
676 output
= consumer_output_override
;
678 LTTNG_ASSERT(session
->ust_session
|| session
->kernel_session
);
679 output
= session
->ust_session
? session
->ust_session
->consumer
:
680 session
->kernel_session
->consumer
;
683 is_local_trace
= output
->type
== CONSUMER_DST_LOCAL
;
684 base_path
= session_base_path_override
?: consumer_output_get_base_path(output
);
686 if (chunk_creation_ts
== (time_t) -1) {
687 PERROR("Failed to sample time while creation session \"%s\" trace chunk",
693 session
->most_recent_chunk_id
.is_set
? session
->most_recent_chunk_id
.value
+ 1 : 0;
695 if (session
->current_trace_chunk
&&
696 !lttng_trace_chunk_get_name_overridden(session
->current_trace_chunk
)) {
697 chunk_status
= lttng_trace_chunk_rename_path(session
->current_trace_chunk
,
698 DEFAULT_CHUNK_TMP_OLD_DIRECTORY
);
699 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
703 if (!session
->current_trace_chunk
) {
704 if (!session
->rotated
) {
710 new_path
= DEFAULT_CHUNK_TMP_NEW_DIRECTORY
;
713 trace_chunk
= lttng_trace_chunk_create(next_chunk_id
, chunk_creation_ts
, new_path
);
718 if (chunk_name_override
) {
719 chunk_status
= lttng_trace_chunk_override_name(trace_chunk
, chunk_name_override
);
720 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
725 if (!is_local_trace
) {
727 * No need to set crendentials and output directory
728 * for remote trace chunks.
733 chunk_status
= lttng_trace_chunk_set_credentials(trace_chunk
, &session_credentials
);
734 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
738 DBG("Creating base output directory of session \"%s\" at %s", session
->name
, base_path
);
739 ret
= utils_mkdir_recursive(base_path
, S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
743 session_output_directory
= lttng_directory_handle_create(base_path
);
744 if (!session_output_directory
) {
747 chunk_status
= lttng_trace_chunk_set_as_owner(trace_chunk
, session_output_directory
);
748 lttng_directory_handle_put(session_output_directory
);
749 session_output_directory
= nullptr;
750 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
756 lttng_directory_handle_put(session_output_directory
);
757 lttng_trace_chunk_put(trace_chunk
);
758 trace_chunk
= nullptr;
762 int session_close_trace_chunk(struct ltt_session
*session
,
763 struct lttng_trace_chunk
*trace_chunk
,
764 enum lttng_trace_chunk_command_type close_command
,
765 char *closed_trace_chunk_path
)
768 bool error_occurred
= false;
769 struct cds_lfht_iter iter
;
770 struct consumer_socket
*socket
;
771 enum lttng_trace_chunk_status chunk_status
;
772 const time_t chunk_close_timestamp
= time(nullptr);
773 const char *new_path
;
775 chunk_status
= lttng_trace_chunk_set_close_command(trace_chunk
, close_command
);
776 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
781 if (chunk_close_timestamp
== (time_t) -1) {
782 ERR("Failed to sample the close timestamp of the current trace chunk of session \"%s\"",
788 if (close_command
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE
&& !session
->rotated
) {
789 /* New chunk stays in session output directory. */
792 /* Use chunk name for new chunk. */
795 if (session
->current_trace_chunk
&&
796 !lttng_trace_chunk_get_name_overridden(session
->current_trace_chunk
)) {
797 /* Rename new chunk path. */
799 lttng_trace_chunk_rename_path(session
->current_trace_chunk
, new_path
);
800 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
805 if (!lttng_trace_chunk_get_name_overridden(trace_chunk
) &&
806 close_command
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
) {
807 const char *old_path
;
809 if (!session
->rotated
) {
814 /* We need to move back the .tmp_old_chunk to its rightful place. */
815 chunk_status
= lttng_trace_chunk_rename_path(trace_chunk
, old_path
);
816 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
821 if (close_command
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
) {
822 session
->rotated
= true;
824 chunk_status
= lttng_trace_chunk_set_close_timestamp(trace_chunk
, chunk_close_timestamp
);
825 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
826 ERR("Failed to set the close timestamp of the current trace chunk of session \"%s\"",
832 if (session
->ust_session
) {
833 const uint64_t relayd_id
= session
->ust_session
->consumer
->net_seq_index
;
835 cds_lfht_for_each_entry (
836 session
->ust_session
->consumer
->socks
->ht
, &iter
, socket
, node
.node
) {
837 pthread_mutex_lock(socket
->lock
);
838 ret
= consumer_close_trace_chunk(socket
,
842 closed_trace_chunk_path
);
843 pthread_mutex_unlock(socket
->lock
);
845 ERR("Failed to close trace chunk on user space consumer");
846 error_occurred
= true;
850 if (session
->kernel_session
) {
851 const uint64_t relayd_id
= session
->kernel_session
->consumer
->net_seq_index
;
853 cds_lfht_for_each_entry (
854 session
->kernel_session
->consumer
->socks
->ht
, &iter
, socket
, node
.node
) {
855 pthread_mutex_lock(socket
->lock
);
856 ret
= consumer_close_trace_chunk(socket
,
860 closed_trace_chunk_path
);
861 pthread_mutex_unlock(socket
->lock
);
863 ERR("Failed to close trace chunk on kernel consumer");
864 error_occurred
= true;
868 ret
= error_occurred
? -1 : 0;
874 * This function skips the metadata channel as the begin/end timestamps of a
875 * metadata packet are useless.
877 * Moreover, opening a packet after a "clear" will cause problems for live
878 * sessions as it will introduce padding that was not part of the first trace
879 * chunk. The relay daemon expects the content of the metadata stream of
880 * successive metadata trace chunks to be strict supersets of one another.
882 * For example, flushing a packet at the beginning of the metadata stream of
883 * a trace chunk resulting from a "clear" session command will cause the
884 * size of the metadata stream of the new trace chunk to not match the size of
885 * the metadata stream of the original chunk. This will confuse the relay
886 * daemon as the same "offset" in a metadata stream will no longer point
887 * to the same content.
889 static enum lttng_error_code
session_kernel_open_packets(struct ltt_session
*session
)
891 enum lttng_error_code ret
= LTTNG_OK
;
892 struct consumer_socket
*socket
;
893 struct lttng_ht_iter iter
;
894 struct cds_lfht_node
*node
;
895 struct ltt_kernel_channel
*chan
;
897 lttng::urcu::read_lock_guard read_lock
;
899 cds_lfht_first(session
->kernel_session
->consumer
->socks
->ht
, &iter
.iter
);
900 node
= cds_lfht_iter_get_node(&iter
.iter
);
901 socket
= caa_container_of(node
, typeof(*socket
), node
.node
);
903 cds_list_for_each_entry (chan
, &session
->kernel_session
->channel_list
.head
, list
) {
906 DBG("Open packet of kernel channel: channel key = %" PRIu64
907 ", session name = %s, session_id = %" PRIu64
,
912 open_ret
= consumer_open_channel_packets(socket
, chan
->key
);
914 /* General error (no known error expected). */
924 enum lttng_error_code
session_open_packets(struct ltt_session
*session
)
926 enum lttng_error_code ret
= LTTNG_OK
;
928 DBG("Opening packets of session channels: session name = %s, session id = %" PRIu64
,
932 if (session
->ust_session
) {
933 ret
= ust_app_open_packets(session
);
934 if (ret
!= LTTNG_OK
) {
939 if (session
->kernel_session
) {
940 ret
= session_kernel_open_packets(session
);
941 if (ret
!= LTTNG_OK
) {
951 * Set a session's current trace chunk.
953 * Must be called with the session lock held.
955 int session_set_trace_chunk(struct ltt_session
*session
,
956 struct lttng_trace_chunk
*new_trace_chunk
,
957 struct lttng_trace_chunk
**current_trace_chunk
)
959 ASSERT_LOCKED(session
->_lock
);
960 return _session_set_trace_chunk_no_lock_check(
961 session
, new_trace_chunk
, current_trace_chunk
);
964 static void session_notify_destruction(const struct ltt_session
*session
)
967 const size_t count
= lttng_dynamic_array_get_count(&session
->destroy_notifiers
);
969 for (i
= 0; i
< count
; i
++) {
970 const struct ltt_session_destroy_notifier_element
*element
=
971 (ltt_session_destroy_notifier_element
*) lttng_dynamic_array_get_element(
972 &session
->destroy_notifiers
, i
);
974 element
->notifier(session
, element
->user_data
);
979 * Fire each clear notifier once, and remove them from the array.
981 void session_notify_clear(ltt_session
& session
)
984 const size_t count
= lttng_dynamic_array_get_count(&session
.clear_notifiers
);
986 for (i
= 0; i
< count
; i
++) {
987 const struct ltt_session_clear_notifier_element
*element
=
988 (ltt_session_clear_notifier_element
*) lttng_dynamic_array_get_element(
989 &session
.clear_notifiers
, i
);
991 element
->notifier(&session
, element
->user_data
);
993 lttng_dynamic_array_clear(&session
.clear_notifiers
);
996 static void session_release(struct urcu_ref
*ref
)
999 struct ltt_ust_session
*usess
;
1000 struct ltt_kernel_session
*ksess
;
1001 struct ltt_session
*session
= lttng::utils::container_of(ref
, <t_session::ref_count
);
1002 const bool session_published
= session
->published
;
1004 LTTNG_ASSERT(!session
->chunk_being_archived
);
1006 usess
= session
->ust_session
;
1007 ksess
= session
->kernel_session
;
1009 /* Clean kernel session teardown, keeping data for destroy notifier. */
1010 kernel_destroy_session(ksess
);
1012 /* UST session teardown, keeping data for destroy notifier. */
1014 /* Close any relayd session */
1015 consumer_output_send_destroy_relayd(usess
->consumer
);
1017 /* Destroy every UST application related to this session. */
1018 ret
= ust_app_destroy_trace_all(usess
);
1020 ERR("Error in ust_app_destroy_trace_all");
1023 /* Clean up the rest, keeping destroy notifier data. */
1024 trace_ust_destroy_session(usess
);
1028 * Must notify the kernel thread here to update it's poll set in order to
1029 * remove the channel(s)' fd just destroyed.
1031 ret
= notify_thread_pipe(the_kernel_poll_pipe
[1]);
1033 PERROR("write kernel poll pipe");
1036 DBG("Destroying session %s (id %" PRIu64
")", session
->name
, session
->id
);
1038 snapshot_destroy(&session
->snapshot
);
1040 pthread_mutex_destroy(&session
->_lock
);
1042 if (session_published
) {
1043 ASSERT_SESSION_LIST_LOCKED();
1044 del_session_list(session
);
1045 del_session_ht(session
);
1047 session_notify_destruction(session
);
1049 consumer_output_put(session
->consumer
);
1050 kernel_free_session(ksess
);
1051 session
->kernel_session
= nullptr;
1053 trace_ust_free_session(usess
);
1054 session
->ust_session
= nullptr;
1056 lttng_dynamic_array_reset(&session
->destroy_notifiers
);
1057 lttng_dynamic_array_reset(&session
->clear_notifiers
);
1058 free(session
->last_archived_chunk_name
);
1059 free(session
->base_path
);
1060 lttng_trigger_put(session
->rotate_trigger
);
1062 if (session_published
) {
1064 * Notify after free-ing to ensure the memory is
1065 * reclaimed before the main thread exits (and prevent memory leak
1068 ASSERT_SESSION_LIST_LOCKED();
1069 the_session_list
.removal_cond
.notify_all();
1074 * Acquire a reference to a session.
1075 * This function may fail (return false); its return value must be checked.
1077 bool session_get(struct ltt_session
*session
)
1079 return urcu_ref_get_unless_zero(&session
->ref_count
);
1083 * Release a reference to a session.
1085 void session_put(struct ltt_session
*session
)
1091 * The session list lock must be held as any session_put()
1092 * may cause the removal of the session from the session_list.
1094 ASSERT_SESSION_LIST_LOCKED();
1095 LTTNG_ASSERT(session
->ref_count
.refcount
);
1096 urcu_ref_put(&session
->ref_count
, session_release
);
1100 * Destroy a session.
1102 * This method does not immediately release/free the session as other
1103 * components may still hold a reference to the session. However,
1104 * the session should no longer be presented to the user.
1106 * Releases the session list's reference to the session
1107 * and marks it as destroyed. Iterations on the session list should be
1108 * mindful of the "destroyed" flag.
1110 void session_destroy(struct ltt_session
*session
)
1113 struct lttng_ht_iter iter
;
1115 LTTNG_ASSERT(!session
->destroyed
);
1116 session
->destroyed
= true;
1119 * Remove immediately from the "session by name" hash table. Only one
1120 * session is expected to exist with a given name for at any given time.
1122 * Even if a session still technically exists for a little while longer,
1123 * there is no point in performing action on a "destroyed" session.
1125 iter
.iter
.node
= &session
->node_by_name
.node
;
1126 ret
= lttng_ht_del(ltt_sessions_ht_by_name
, &iter
);
1129 session_put(session
);
1132 int session_add_destroy_notifier(struct ltt_session
*session
,
1133 ltt_session_destroy_notifier notifier
,
1136 const struct ltt_session_destroy_notifier_element element
= { .notifier
= notifier
,
1137 .user_data
= user_data
};
1139 return lttng_dynamic_array_add_element(&session
->destroy_notifiers
, &element
);
1142 int session_add_clear_notifier(struct ltt_session
*session
,
1143 ltt_session_clear_notifier notifier
,
1146 const struct ltt_session_clear_notifier_element element
= { .notifier
= notifier
,
1147 .user_data
= user_data
};
1149 return lttng_dynamic_array_add_element(&session
->clear_notifiers
, &element
);
1153 * Create a new session and add it to the session list.
1154 * Session list lock must be held by the caller.
1156 enum lttng_error_code
1157 session_create(const char *name
, uid_t uid
, gid_t gid
, struct ltt_session
**out_session
)
1160 enum lttng_error_code ret_code
;
1161 struct ltt_session
*new_session
= nullptr;
1163 ASSERT_SESSION_LIST_LOCKED();
1165 struct ltt_session
*clashing_session
;
1167 clashing_session
= session_find_by_name(name
);
1168 if (clashing_session
) {
1169 session_put(clashing_session
);
1170 ret_code
= LTTNG_ERR_EXIST_SESS
;
1174 new_session
= zmalloc
<ltt_session
>();
1176 PERROR("Failed to allocate an ltt_session structure");
1177 ret_code
= LTTNG_ERR_NOMEM
;
1181 lttng_dynamic_array_init(&new_session
->destroy_notifiers
,
1182 sizeof(struct ltt_session_destroy_notifier_element
),
1184 lttng_dynamic_array_init(&new_session
->clear_notifiers
,
1185 sizeof(struct ltt_session_clear_notifier_element
),
1187 urcu_ref_init(&new_session
->ref_count
);
1188 pthread_mutex_init(&new_session
->_lock
, nullptr);
1190 new_session
->creation_time
= time(nullptr);
1191 if (new_session
->creation_time
== (time_t) -1) {
1192 PERROR("Failed to sample session creation time");
1193 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1197 /* Create default consumer output. */
1198 new_session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
1199 if (new_session
->consumer
== nullptr) {
1200 ret_code
= LTTNG_ERR_NOMEM
;
1205 ret
= lttng_strncpy(new_session
->name
, name
, sizeof(new_session
->name
));
1207 ret_code
= LTTNG_ERR_SESSION_INVALID_CHAR
;
1210 ret
= validate_name(name
);
1212 ret_code
= LTTNG_ERR_SESSION_INVALID_CHAR
;
1217 bool found_name
= false;
1219 struct tm
*timeinfo
;
1221 timeinfo
= localtime(&new_session
->creation_time
);
1223 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1226 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1227 for (i
= 0; i
< INT_MAX
; i
++) {
1228 struct ltt_session
*clashing_session
;
1231 ret
= snprintf(new_session
->name
,
1232 sizeof(new_session
->name
),
1234 DEFAULT_SESSION_NAME
,
1237 ret
= snprintf(new_session
->name
,
1238 sizeof(new_session
->name
),
1240 DEFAULT_SESSION_NAME
,
1244 new_session
->name_contains_creation_time
= true;
1245 if (ret
== -1 || ret
>= sizeof(new_session
->name
)) {
1247 * Null-terminate in case the name is used
1248 * in logging statements.
1250 new_session
->name
[sizeof(new_session
->name
) - 1] = '\0';
1251 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1255 clashing_session
= session_find_by_name(new_session
->name
);
1256 session_put(clashing_session
);
1257 if (!clashing_session
) {
1263 DBG("Generated session name \"%s\"", new_session
->name
);
1264 new_session
->has_auto_generated_name
= true;
1266 ERR("Failed to auto-generate a session name");
1267 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1272 ret
= gethostname(new_session
->hostname
, sizeof(new_session
->hostname
));
1274 if (errno
== ENAMETOOLONG
) {
1275 new_session
->hostname
[sizeof(new_session
->hostname
) - 1] = '\0';
1276 ERR("Hostname exceeds the maximal permitted length and has been truncated to %s",
1277 new_session
->hostname
);
1279 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1284 new_session
->uid
= uid
;
1285 new_session
->gid
= gid
;
1287 ret
= snapshot_init(&new_session
->snapshot
);
1289 ret_code
= LTTNG_ERR_NOMEM
;
1293 new_session
->rotation_state
= LTTNG_ROTATION_STATE_NO_ROTATION
;
1295 /* Add new session to the session list. */
1296 new_session
->id
= add_session_list(new_session
);
1299 * Add the new session to the ltt_sessions_ht_by_id.
1300 * No ownership is taken by the hash table; it is merely
1301 * a wrapper around the session list used for faster access
1304 add_session_ht(new_session
);
1305 new_session
->published
= true;
1308 * Consumer is left to NULL since the create_session_uri command will
1309 * set it up and, if valid, assign it to the session.
1311 DBG("Tracing session %s created with ID %" PRIu64
" by uid = %d, gid = %d",
1316 ret_code
= LTTNG_OK
;
1319 (void) session_get(new_session
);
1320 *out_session
= new_session
;
1324 session_put(new_session
);
1325 new_session
= nullptr;
1330 * Check if the UID matches the session. Root user has access to all
1333 bool session_access_ok(struct ltt_session
*session
, uid_t uid
)
1335 LTTNG_ASSERT(session
);
1336 return (uid
== session
->uid
) || uid
== 0;
1340 * Set a session's rotation state and reset all associated state.
1342 * This function resets the rotation state (check timers, pending
1343 * flags, etc.) and sets the result of the last rotation. The result
1344 * can be queries by a liblttng-ctl client.
1346 * Be careful of the result passed to this function. For instance,
1347 * on failure to launch a rotation, a client will expect the rotation
1348 * state to be set to "NO_ROTATION". If an error occurred while the
1349 * rotation was "ONGOING", result should be set to "ERROR", which will
1350 * allow a client to report it.
1352 * Must be called with the session and session_list locks held.
1354 int session_reset_rotation_state(ltt_session
& session
, enum lttng_rotation_state result
)
1358 ASSERT_SESSION_LIST_LOCKED();
1359 ASSERT_LOCKED(session
._lock
);
1361 session
.rotation_state
= result
;
1362 if (session
.rotation_pending_check_timer_enabled
) {
1363 ret
= timer_session_rotation_pending_check_stop(session
);
1365 if (session
.chunk_being_archived
) {
1367 enum lttng_trace_chunk_status chunk_status
;
1369 chunk_status
= lttng_trace_chunk_get_id(session
.chunk_being_archived
, &chunk_id
);
1370 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1371 LTTNG_OPTIONAL_SET(&session
.last_archived_chunk_id
, chunk_id
);
1372 lttng_trace_chunk_put(session
.chunk_being_archived
);
1373 session
.chunk_being_archived
= nullptr;
1375 * Fire the clear reply notifiers if we are completing a clear
1378 session_notify_clear(session
);
1384 * Sample the id of a session looked up via its name.
1385 * Here the term "sampling" hint the caller that this return the id at a given
1386 * point in time with no guarantee that the session for which the id was
1387 * sampled still exist at that point.
1389 * Return 0 when the session is not found,
1390 * Return 1 when the session is found and set `id`.
1392 bool sample_session_id_by_name(const char *name
, uint64_t *id
)
1395 struct lttng_ht_node_str
*node
;
1396 struct lttng_ht_iter iter
;
1397 struct ltt_session
*ls
;
1399 lttng::urcu::read_lock_guard read_lock
;
1401 if (!ltt_sessions_ht_by_name
) {
1406 lttng_ht_lookup(ltt_sessions_ht_by_name
, name
, &iter
);
1407 node
= lttng_ht_iter_get_node_str(&iter
);
1408 if (node
== nullptr) {
1413 ls
= lttng::utils::container_of(node
, <t_session::node_by_name
);
1417 DBG3("Session id `%" PRIu64
"` sampled for session `%s", *id
, name
);
1422 void ltt_session::_locked_session_release(ltt_session
*session
)
1428 session_unlock(session
);
1429 session_put(session
);
1432 void ltt_session::_locked_const_session_release(const ltt_session
*session
)
1438 ltt_session::_const_session_unlock(*session
);
1439 ltt_session::_const_session_put(session
);
1442 ltt_session::locked_ref
ltt_session::find_locked_session(ltt_session::id_t id
)
1444 lttng::urcu::read_lock_guard rcu_lock
;
1445 auto session
= session_find_by_id(id
);
1448 LTTNG_THROW_SESSION_NOT_FOUND_BY_ID_ERROR(id
);
1452 * The pointer falling out of scope will unlock and release the reference to the
1455 session_lock(session
);
1456 return ltt_session::locked_ref(session
);
1459 ltt_session::locked_ref
ltt_session::find_locked_session(lttng::c_string_view name
)
1461 lttng::urcu::read_lock_guard rcu_lock
;
1462 auto session
= session_find_by_name(name
.data());
1465 LTTNG_THROW_SESSION_NOT_FOUND_BY_NAME_ERROR(name
.data());
1468 session_lock(session
);
1469 return ltt_session::locked_ref(session
);
1472 ltt_session::const_locked_ref
ltt_session::find_locked_const_session(ltt_session::id_t id
)
1474 lttng::urcu::read_lock_guard rcu_lock
;
1475 auto session
= session_find_by_id(id
);
1478 LTTNG_THROW_SESSION_NOT_FOUND_BY_ID_ERROR(id
);
1481 session_lock(session
);
1482 return ltt_session::const_locked_ref(session
);
1485 ltt_session::const_locked_ref
ltt_session::find_locked_const_session(lttng::c_string_view name
)
1487 lttng::urcu::read_lock_guard rcu_lock
;
1488 auto session
= session_find_by_name(name
.data());
1491 LTTNG_THROW_SESSION_NOT_FOUND_BY_NAME_ERROR(name
.data());
1494 session_lock(session
);
1495 return ltt_session::const_locked_ref(session
);
1498 ltt_session::ref
ltt_session::find_session(ltt_session::id_t id
)
1500 lttng::urcu::read_lock_guard rcu_lock
;
1501 auto session
= session_find_by_id(id
);
1504 LTTNG_THROW_SESSION_NOT_FOUND_BY_ID_ERROR(id
);
1507 return ltt_session::ref(session
);
1510 ltt_session::ref
ltt_session::find_session(lttng::c_string_view name
)
1512 lttng::urcu::read_lock_guard rcu_lock
;
1513 auto session
= session_find_by_name(name
.data());
1516 LTTNG_THROW_SESSION_NOT_FOUND_BY_NAME_ERROR(name
.data());
1519 return ltt_session::ref(session
);
1522 ltt_session::const_ref
ltt_session::find_const_session(ltt_session::id_t id
)
1524 lttng::urcu::read_lock_guard rcu_lock
;
1525 auto session
= session_find_by_id(id
);
1528 LTTNG_THROW_SESSION_NOT_FOUND_BY_ID_ERROR(id
);
1531 return ltt_session::const_ref(session
);
1534 ltt_session::const_ref
ltt_session::find_const_session(lttng::c_string_view name
)
1536 lttng::urcu::read_lock_guard rcu_lock
;
1537 auto session
= session_find_by_name(name
.data());
1540 LTTNG_THROW_SESSION_NOT_FOUND_BY_NAME_ERROR(name
.data());
1543 return ltt_session::const_ref(session
);
1546 void ltt_session::_const_session_put(const ltt_session
*session
)
1549 * The session list lock must be held as any session_put()
1550 * may cause the removal of the session from the session_list.
1552 ASSERT_SESSION_LIST_LOCKED();
1553 LTTNG_ASSERT(session
->ref_count
.refcount
);
1554 urcu_ref_put(&session
->ref_count
, session_release
);
1557 std::unique_lock
<std::mutex
> ls::lock_session_list()
1559 return std::unique_lock
<std::mutex
>(the_session_list
.lock
);