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
= {
72 .lock
= PTHREAD_MUTEX_INITIALIZER
,
73 .removal_cond
= PTHREAD_COND_INITIALIZER
,
75 .head
= CDS_LIST_HEAD_INIT(the_session_list
.head
),
80 * Validate the session name for forbidden characters.
82 * Return 0 on success else -1 meaning a forbidden char. has been found.
84 static int validate_name(const char *name
)
91 tmp_name
= strdup(name
);
98 tok
= strpbrk(tmp_name
, forbidden_name_chars
);
100 DBG("Session name %s contains a forbidden character", name
);
101 /* Forbidden character has been found. */
113 * Add a ltt_session structure to the global list.
115 * The caller MUST acquire the session list lock before.
116 * Returns the unique identifier for the session.
118 static uint64_t add_session_list(struct ltt_session
*ls
)
122 cds_list_add(&ls
->list
, &the_session_list
.head
);
123 return the_session_list
.next_uuid
++;
127 * Delete a ltt_session structure to the global list.
129 * The caller MUST acquire the session list lock before.
131 static void del_session_list(struct ltt_session
*ls
)
135 cds_list_del(&ls
->list
);
139 * Return a pointer to the session list.
141 struct ltt_session_list
*session_get_list()
143 return &the_session_list
;
147 * Returns once the session list is empty.
149 void session_list_wait_empty()
151 pthread_mutex_lock(&the_session_list
.lock
);
152 while (!cds_list_empty(&the_session_list
.head
)) {
153 pthread_cond_wait(&the_session_list
.removal_cond
, &the_session_list
.lock
);
155 pthread_mutex_unlock(&the_session_list
.lock
);
159 * Acquire session list lock
161 void session_lock_list()
163 pthread_mutex_lock(&the_session_list
.lock
);
167 * Try to acquire session list lock
169 int session_trylock_list()
171 return pthread_mutex_trylock(&the_session_list
.lock
);
175 * Release session list lock
177 void session_unlock_list()
179 pthread_mutex_unlock(&the_session_list
.lock
);
183 * Get the session's consumer destination type.
185 * The caller must hold the session lock.
187 enum consumer_dst_type
session_get_consumer_destination_type(const struct ltt_session
*session
)
190 * The output information is duplicated in both of those session types.
191 * Hence, it doesn't matter from which it is retrieved. However, it is
192 * possible for only one of them to be set.
194 return session
->kernel_session
? session
->kernel_session
->consumer
->type
:
195 session
->ust_session
->consumer
->type
;
199 * Get the session's consumer network hostname.
200 * The caller must ensure that the destination is of type "net".
202 * The caller must hold the session lock.
204 const char *session_get_net_consumer_hostname(const struct ltt_session
*session
)
206 const char *hostname
= nullptr;
207 const struct consumer_output
*output
;
209 output
= session
->kernel_session
? session
->kernel_session
->consumer
:
210 session
->ust_session
->consumer
;
213 * hostname is assumed to be the same for both control and data
216 switch (output
->dst
.net
.control
.dtype
) {
218 hostname
= output
->dst
.net
.control
.dst
.ipv4
;
221 hostname
= output
->dst
.net
.control
.dst
.ipv6
;
230 * Get the session's consumer network control and data ports.
231 * The caller must ensure that the destination is of type "net".
233 * The caller must hold the session lock.
235 void session_get_net_consumer_ports(const struct ltt_session
*session
,
236 uint16_t *control_port
,
239 const struct consumer_output
*output
;
241 output
= session
->kernel_session
? session
->kernel_session
->consumer
:
242 session
->ust_session
->consumer
;
243 *control_port
= output
->dst
.net
.control
.port
;
244 *data_port
= output
->dst
.net
.data
.port
;
248 * Get the location of the latest trace archive produced by a rotation.
250 * The caller must hold the session lock.
252 struct lttng_trace_archive_location
*
253 session_get_trace_archive_location(const struct ltt_session
*session
)
256 struct lttng_trace_archive_location
*location
= nullptr;
257 char *chunk_path
= nullptr;
259 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
||
260 !session
->last_archived_chunk_name
) {
264 switch (session_get_consumer_destination_type(session
)) {
265 case CONSUMER_DST_LOCAL
:
266 ret
= asprintf(&chunk_path
,
267 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
268 session_get_base_path(session
),
269 session
->last_archived_chunk_name
);
273 location
= lttng_trace_archive_location_local_create(chunk_path
);
275 case CONSUMER_DST_NET
:
277 const char *hostname
;
278 uint16_t control_port
, data_port
;
280 hostname
= session_get_net_consumer_hostname(session
);
281 session_get_net_consumer_ports(session
, &control_port
, &data_port
);
282 location
= lttng_trace_archive_location_relay_create(
284 LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
,
287 session
->last_chunk_path
);
299 * Allocate the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name HT.
301 * The session list lock must be held.
303 static int ltt_sessions_ht_alloc()
307 DBG("Allocating ltt_sessions_ht_by_id");
308 ltt_sessions_ht_by_id
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
309 if (!ltt_sessions_ht_by_id
) {
311 ERR("Failed to allocate ltt_sessions_ht_by_id");
315 DBG("Allocating ltt_sessions_ht_by_name");
316 ltt_sessions_ht_by_name
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
317 if (!ltt_sessions_ht_by_name
) {
319 ERR("Failed to allocate ltt_sessions_ht_by_name");
328 * Destroy the ltt_sessions_ht_by_id HT.
330 * The session list lock must be held.
332 static void ltt_sessions_ht_destroy()
334 if (ltt_sessions_ht_by_id
) {
335 lttng_ht_destroy(ltt_sessions_ht_by_id
);
336 ltt_sessions_ht_by_id
= nullptr;
339 if (ltt_sessions_ht_by_name
) {
340 lttng_ht_destroy(ltt_sessions_ht_by_name
);
341 ltt_sessions_ht_by_name
= nullptr;
348 * Add a ltt_session to the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name.
349 * If unallocated, the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name. HTs
350 * are allocated. The session list lock must be held.
352 static void add_session_ht(struct ltt_session
*ls
)
358 if (!ltt_sessions_ht_by_id
) {
359 ret
= ltt_sessions_ht_alloc();
361 ERR("Error allocating the sessions HT");
366 /* Should always be present with ltt_sessions_ht_by_id. */
367 LTTNG_ASSERT(ltt_sessions_ht_by_name
);
369 lttng_ht_node_init_u64(&ls
->node
, ls
->id
);
370 lttng_ht_add_unique_u64(ltt_sessions_ht_by_id
, &ls
->node
);
372 lttng_ht_node_init_str(&ls
->node_by_name
, ls
->name
);
373 lttng_ht_add_unique_str(ltt_sessions_ht_by_name
, &ls
->node_by_name
);
380 * Test if ltt_sessions_ht_by_id/name are empty.
381 * Return `false` if hash table objects are null.
382 * The session list lock must be held.
384 static bool ltt_sessions_ht_empty()
388 if (!ltt_sessions_ht_by_id
) {
389 /* The hash tables do not exist yet. */
393 LTTNG_ASSERT(ltt_sessions_ht_by_name
);
395 if (lttng_ht_get_count(ltt_sessions_ht_by_id
) != 0) {
401 * At this point it is expected that the `ltt_sessions_ht_by_name` ht is
404 * The removal from both hash tables is done in two different
406 * - removal from `ltt_sessions_ht_by_name` is done during
407 * `session_destroy()`
408 * - removal from `ltt_sessions_ht_by_id` is done later
409 * in `session_release()` on the last reference put.
411 * This means that it is possible for `ltt_sessions_ht_by_name` to be
412 * empty but for `ltt_sessions_ht_by_id` to still contain objects when
413 * multiple sessions exists. The reverse is false, hence this sanity
416 LTTNG_ASSERT(lttng_ht_get_count(ltt_sessions_ht_by_name
) == 0);
423 * Remove a ltt_session from the ltt_sessions_ht_by_id.
424 * If empty, the ltt_sessions_ht_by_id/name HTs are freed.
425 * The session list lock must be held.
427 static void del_session_ht(struct ltt_session
*ls
)
429 struct lttng_ht_iter iter
;
433 LTTNG_ASSERT(ltt_sessions_ht_by_id
);
434 LTTNG_ASSERT(ltt_sessions_ht_by_name
);
436 iter
.iter
.node
= &ls
->node
.node
;
437 ret
= lttng_ht_del(ltt_sessions_ht_by_id
, &iter
);
440 if (ltt_sessions_ht_empty()) {
441 DBG("Empty ltt_sessions_ht_by_id/name, destroying hast tables");
442 ltt_sessions_ht_destroy();
447 * Acquire session lock
449 void session_lock(struct ltt_session
*session
)
451 LTTNG_ASSERT(session
);
453 pthread_mutex_lock(&session
->lock
);
457 * Release session lock
459 void session_unlock(struct ltt_session
*session
)
461 LTTNG_ASSERT(session
);
463 pthread_mutex_unlock(&session
->lock
);
466 static int _session_set_trace_chunk_no_lock_check(struct ltt_session
*session
,
467 struct lttng_trace_chunk
*new_trace_chunk
,
468 struct lttng_trace_chunk
**_current_trace_chunk
)
471 unsigned int i
, refs_to_acquire
= 0, refs_acquired
= 0, refs_to_release
= 0;
472 struct cds_lfht_iter iter
;
473 struct consumer_socket
*socket
;
474 struct lttng_trace_chunk
*current_trace_chunk
;
476 enum lttng_trace_chunk_status chunk_status
;
478 lttng::urcu::read_lock_guard read_lock
;
480 * Ownership of current trace chunk is transferred to
481 * `current_trace_chunk`.
483 current_trace_chunk
= session
->current_trace_chunk
;
484 session
->current_trace_chunk
= nullptr;
485 if (session
->ust_session
) {
486 lttng_trace_chunk_put(session
->ust_session
->current_trace_chunk
);
487 session
->ust_session
->current_trace_chunk
= nullptr;
489 if (session
->kernel_session
) {
490 lttng_trace_chunk_put(session
->kernel_session
->current_trace_chunk
);
491 session
->kernel_session
->current_trace_chunk
= nullptr;
493 if (!new_trace_chunk
) {
497 chunk_status
= lttng_trace_chunk_get_id(new_trace_chunk
, &chunk_id
);
498 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
501 refs_to_acquire
+= !!session
->ust_session
;
502 refs_to_acquire
+= !!session
->kernel_session
;
504 for (refs_acquired
= 0; refs_acquired
< refs_to_acquire
; refs_acquired
++) {
505 if (!lttng_trace_chunk_get(new_trace_chunk
)) {
506 ERR("Failed to acquire reference to new trace chunk of session \"%s\"",
512 if (session
->ust_session
) {
513 const uint64_t relayd_id
= session
->ust_session
->consumer
->net_seq_index
;
514 const bool is_local_trace
= session
->ust_session
->consumer
->type
==
517 session
->ust_session
->current_trace_chunk
= new_trace_chunk
;
518 if (is_local_trace
) {
519 enum lttng_error_code ret_error_code
;
522 ust_app_create_channel_subdirectories(session
->ust_session
);
523 if (ret_error_code
!= LTTNG_OK
) {
527 cds_lfht_for_each_entry (
528 session
->ust_session
->consumer
->socks
->ht
, &iter
, socket
, node
.node
) {
529 pthread_mutex_lock(socket
->lock
);
530 ret
= consumer_create_trace_chunk(socket
,
534 DEFAULT_UST_TRACE_DIR
);
535 pthread_mutex_unlock(socket
->lock
);
541 if (session
->kernel_session
) {
542 const uint64_t relayd_id
= session
->kernel_session
->consumer
->net_seq_index
;
543 const bool is_local_trace
= session
->kernel_session
->consumer
->type
==
546 session
->kernel_session
->current_trace_chunk
= new_trace_chunk
;
547 if (is_local_trace
) {
548 enum lttng_error_code ret_error_code
;
551 kernel_create_channel_subdirectories(session
->kernel_session
);
552 if (ret_error_code
!= LTTNG_OK
) {
556 cds_lfht_for_each_entry (
557 session
->kernel_session
->consumer
->socks
->ht
, &iter
, socket
, node
.node
) {
558 pthread_mutex_lock(socket
->lock
);
559 ret
= consumer_create_trace_chunk(socket
,
563 DEFAULT_KERNEL_TRACE_DIR
);
564 pthread_mutex_unlock(socket
->lock
);
572 * Update local current trace chunk state last, only if all remote
573 * creations succeeded.
575 session
->current_trace_chunk
= new_trace_chunk
;
576 LTTNG_OPTIONAL_SET(&session
->most_recent_chunk_id
, chunk_id
);
578 if (_current_trace_chunk
) {
579 *_current_trace_chunk
= current_trace_chunk
;
580 current_trace_chunk
= nullptr;
583 lttng_trace_chunk_put(current_trace_chunk
);
586 if (session
->ust_session
) {
587 session
->ust_session
->current_trace_chunk
= nullptr;
589 if (session
->kernel_session
) {
590 session
->kernel_session
->current_trace_chunk
= nullptr;
593 * Release references taken in the case where all references could not
596 refs_to_release
= refs_to_acquire
- refs_acquired
;
597 for (i
= 0; i
< refs_to_release
; i
++) {
598 lttng_trace_chunk_put(new_trace_chunk
);
604 struct lttng_trace_chunk
*
605 session_create_new_trace_chunk(const struct ltt_session
*session
,
606 const struct consumer_output
*consumer_output_override
,
607 const char *session_base_path_override
,
608 const char *chunk_name_override
)
611 struct lttng_trace_chunk
*trace_chunk
= nullptr;
612 enum lttng_trace_chunk_status chunk_status
;
613 const time_t chunk_creation_ts
= time(nullptr);
615 const char *base_path
;
616 struct lttng_directory_handle
*session_output_directory
= nullptr;
617 const struct lttng_credentials session_credentials
= {
618 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session
->uid
),
619 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session
->gid
),
621 uint64_t next_chunk_id
;
622 const struct consumer_output
*output
;
623 const char *new_path
;
625 if (consumer_output_override
) {
626 output
= consumer_output_override
;
628 LTTNG_ASSERT(session
->ust_session
|| session
->kernel_session
);
629 output
= session
->ust_session
? session
->ust_session
->consumer
:
630 session
->kernel_session
->consumer
;
633 is_local_trace
= output
->type
== CONSUMER_DST_LOCAL
;
634 base_path
= session_base_path_override
?: consumer_output_get_base_path(output
);
636 if (chunk_creation_ts
== (time_t) -1) {
637 PERROR("Failed to sample time while creation session \"%s\" trace chunk",
643 session
->most_recent_chunk_id
.is_set
? session
->most_recent_chunk_id
.value
+ 1 : 0;
645 if (session
->current_trace_chunk
&&
646 !lttng_trace_chunk_get_name_overridden(session
->current_trace_chunk
)) {
647 chunk_status
= lttng_trace_chunk_rename_path(session
->current_trace_chunk
,
648 DEFAULT_CHUNK_TMP_OLD_DIRECTORY
);
649 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
653 if (!session
->current_trace_chunk
) {
654 if (!session
->rotated
) {
660 new_path
= DEFAULT_CHUNK_TMP_NEW_DIRECTORY
;
663 trace_chunk
= lttng_trace_chunk_create(next_chunk_id
, chunk_creation_ts
, new_path
);
668 if (chunk_name_override
) {
669 chunk_status
= lttng_trace_chunk_override_name(trace_chunk
, chunk_name_override
);
670 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
675 if (!is_local_trace
) {
677 * No need to set crendentials and output directory
678 * for remote trace chunks.
683 chunk_status
= lttng_trace_chunk_set_credentials(trace_chunk
, &session_credentials
);
684 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
688 DBG("Creating base output directory of session \"%s\" at %s", session
->name
, base_path
);
689 ret
= utils_mkdir_recursive(base_path
, S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
693 session_output_directory
= lttng_directory_handle_create(base_path
);
694 if (!session_output_directory
) {
697 chunk_status
= lttng_trace_chunk_set_as_owner(trace_chunk
, session_output_directory
);
698 lttng_directory_handle_put(session_output_directory
);
699 session_output_directory
= nullptr;
700 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
706 lttng_directory_handle_put(session_output_directory
);
707 lttng_trace_chunk_put(trace_chunk
);
708 trace_chunk
= nullptr;
712 int session_close_trace_chunk(struct ltt_session
*session
,
713 struct lttng_trace_chunk
*trace_chunk
,
714 enum lttng_trace_chunk_command_type close_command
,
715 char *closed_trace_chunk_path
)
718 bool error_occurred
= false;
719 struct cds_lfht_iter iter
;
720 struct consumer_socket
*socket
;
721 enum lttng_trace_chunk_status chunk_status
;
722 const time_t chunk_close_timestamp
= time(nullptr);
723 const char *new_path
;
725 chunk_status
= lttng_trace_chunk_set_close_command(trace_chunk
, close_command
);
726 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
731 if (chunk_close_timestamp
== (time_t) -1) {
732 ERR("Failed to sample the close timestamp of the current trace chunk of session \"%s\"",
738 if (close_command
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE
&& !session
->rotated
) {
739 /* New chunk stays in session output directory. */
742 /* Use chunk name for new chunk. */
745 if (session
->current_trace_chunk
&&
746 !lttng_trace_chunk_get_name_overridden(session
->current_trace_chunk
)) {
747 /* Rename new chunk path. */
749 lttng_trace_chunk_rename_path(session
->current_trace_chunk
, new_path
);
750 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
755 if (!lttng_trace_chunk_get_name_overridden(trace_chunk
) &&
756 close_command
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
) {
757 const char *old_path
;
759 if (!session
->rotated
) {
764 /* We need to move back the .tmp_old_chunk to its rightful place. */
765 chunk_status
= lttng_trace_chunk_rename_path(trace_chunk
, old_path
);
766 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
771 if (close_command
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
) {
772 session
->rotated
= true;
774 chunk_status
= lttng_trace_chunk_set_close_timestamp(trace_chunk
, chunk_close_timestamp
);
775 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
776 ERR("Failed to set the close timestamp of the current trace chunk of session \"%s\"",
782 if (session
->ust_session
) {
783 const uint64_t relayd_id
= session
->ust_session
->consumer
->net_seq_index
;
785 cds_lfht_for_each_entry (
786 session
->ust_session
->consumer
->socks
->ht
, &iter
, socket
, node
.node
) {
787 pthread_mutex_lock(socket
->lock
);
788 ret
= consumer_close_trace_chunk(socket
,
792 closed_trace_chunk_path
);
793 pthread_mutex_unlock(socket
->lock
);
795 ERR("Failed to close trace chunk on user space consumer");
796 error_occurred
= true;
800 if (session
->kernel_session
) {
801 const uint64_t relayd_id
= session
->kernel_session
->consumer
->net_seq_index
;
803 cds_lfht_for_each_entry (
804 session
->kernel_session
->consumer
->socks
->ht
, &iter
, socket
, node
.node
) {
805 pthread_mutex_lock(socket
->lock
);
806 ret
= consumer_close_trace_chunk(socket
,
810 closed_trace_chunk_path
);
811 pthread_mutex_unlock(socket
->lock
);
813 ERR("Failed to close trace chunk on kernel consumer");
814 error_occurred
= true;
818 ret
= error_occurred
? -1 : 0;
824 * This function skips the metadata channel as the begin/end timestamps of a
825 * metadata packet are useless.
827 * Moreover, opening a packet after a "clear" will cause problems for live
828 * sessions as it will introduce padding that was not part of the first trace
829 * chunk. The relay daemon expects the content of the metadata stream of
830 * successive metadata trace chunks to be strict supersets of one another.
832 * For example, flushing a packet at the beginning of the metadata stream of
833 * a trace chunk resulting from a "clear" session command will cause the
834 * size of the metadata stream of the new trace chunk to not match the size of
835 * the metadata stream of the original chunk. This will confuse the relay
836 * daemon as the same "offset" in a metadata stream will no longer point
837 * to the same content.
839 static enum lttng_error_code
session_kernel_open_packets(struct ltt_session
*session
)
841 enum lttng_error_code ret
= LTTNG_OK
;
842 struct consumer_socket
*socket
;
843 struct lttng_ht_iter iter
;
844 struct cds_lfht_node
*node
;
845 struct ltt_kernel_channel
*chan
;
847 lttng::urcu::read_lock_guard read_lock
;
849 cds_lfht_first(session
->kernel_session
->consumer
->socks
->ht
, &iter
.iter
);
850 node
= cds_lfht_iter_get_node(&iter
.iter
);
851 socket
= caa_container_of(node
, typeof(*socket
), node
.node
);
853 cds_list_for_each_entry (chan
, &session
->kernel_session
->channel_list
.head
, list
) {
856 DBG("Open packet of kernel channel: channel key = %" PRIu64
857 ", session name = %s, session_id = %" PRIu64
,
862 open_ret
= consumer_open_channel_packets(socket
, chan
->key
);
864 /* General error (no known error expected). */
874 enum lttng_error_code
session_open_packets(struct ltt_session
*session
)
876 enum lttng_error_code ret
= LTTNG_OK
;
878 DBG("Opening packets of session channels: session name = %s, session id = %" PRIu64
,
882 if (session
->ust_session
) {
883 ret
= ust_app_open_packets(session
);
884 if (ret
!= LTTNG_OK
) {
889 if (session
->kernel_session
) {
890 ret
= session_kernel_open_packets(session
);
891 if (ret
!= LTTNG_OK
) {
901 * Set a session's current trace chunk.
903 * Must be called with the session lock held.
905 int session_set_trace_chunk(struct ltt_session
*session
,
906 struct lttng_trace_chunk
*new_trace_chunk
,
907 struct lttng_trace_chunk
**current_trace_chunk
)
909 ASSERT_LOCKED(session
->lock
);
910 return _session_set_trace_chunk_no_lock_check(
911 session
, new_trace_chunk
, current_trace_chunk
);
914 static void session_notify_destruction(const struct ltt_session
*session
)
917 const size_t count
= lttng_dynamic_array_get_count(&session
->destroy_notifiers
);
919 for (i
= 0; i
< count
; i
++) {
920 const struct ltt_session_destroy_notifier_element
*element
=
921 (ltt_session_destroy_notifier_element
*) lttng_dynamic_array_get_element(
922 &session
->destroy_notifiers
, i
);
924 element
->notifier(session
, element
->user_data
);
929 * Fire each clear notifier once, and remove them from the array.
931 void session_notify_clear(struct ltt_session
*session
)
934 const size_t count
= lttng_dynamic_array_get_count(&session
->clear_notifiers
);
936 for (i
= 0; i
< count
; i
++) {
937 const struct ltt_session_clear_notifier_element
*element
=
938 (ltt_session_clear_notifier_element
*) lttng_dynamic_array_get_element(
939 &session
->clear_notifiers
, i
);
941 element
->notifier(session
, element
->user_data
);
943 lttng_dynamic_array_clear(&session
->clear_notifiers
);
946 static void session_release(struct urcu_ref
*ref
)
949 struct ltt_ust_session
*usess
;
950 struct ltt_kernel_session
*ksess
;
951 struct ltt_session
*session
= lttng::utils::container_of(ref
, <t_session::ref
);
952 const bool session_published
= session
->published
;
954 LTTNG_ASSERT(!session
->chunk_being_archived
);
956 usess
= session
->ust_session
;
957 ksess
= session
->kernel_session
;
959 /* Clean kernel session teardown, keeping data for destroy notifier. */
960 kernel_destroy_session(ksess
);
962 /* UST session teardown, keeping data for destroy notifier. */
964 /* Close any relayd session */
965 consumer_output_send_destroy_relayd(usess
->consumer
);
967 /* Destroy every UST application related to this session. */
968 ret
= ust_app_destroy_trace_all(usess
);
970 ERR("Error in ust_app_destroy_trace_all");
973 /* Clean up the rest, keeping destroy notifier data. */
974 trace_ust_destroy_session(usess
);
978 * Must notify the kernel thread here to update it's poll set in order to
979 * remove the channel(s)' fd just destroyed.
981 ret
= notify_thread_pipe(the_kernel_poll_pipe
[1]);
983 PERROR("write kernel poll pipe");
986 DBG("Destroying session %s (id %" PRIu64
")", session
->name
, session
->id
);
988 snapshot_destroy(&session
->snapshot
);
990 pthread_mutex_destroy(&session
->lock
);
992 if (session_published
) {
993 ASSERT_LOCKED(the_session_list
.lock
);
994 del_session_list(session
);
995 del_session_ht(session
);
997 session_notify_destruction(session
);
999 consumer_output_put(session
->consumer
);
1000 kernel_free_session(ksess
);
1001 session
->kernel_session
= nullptr;
1003 trace_ust_free_session(usess
);
1004 session
->ust_session
= nullptr;
1006 lttng_dynamic_array_reset(&session
->destroy_notifiers
);
1007 lttng_dynamic_array_reset(&session
->clear_notifiers
);
1008 free(session
->last_archived_chunk_name
);
1009 free(session
->base_path
);
1010 lttng_trigger_put(session
->rotate_trigger
);
1012 if (session_published
) {
1014 * Broadcast after free-ing to ensure the memory is
1015 * reclaimed before the main thread exits.
1017 ASSERT_LOCKED(the_session_list
.lock
);
1018 pthread_cond_broadcast(&the_session_list
.removal_cond
);
1023 * Acquire a reference to a session.
1024 * This function may fail (return false); its return value must be checked.
1026 bool session_get(struct ltt_session
*session
)
1028 return urcu_ref_get_unless_zero(&session
->ref
);
1032 * Release a reference to a session.
1034 void session_put(struct ltt_session
*session
)
1040 * The session list lock must be held as any session_put()
1041 * may cause the removal of the session from the session_list.
1043 ASSERT_LOCKED(the_session_list
.lock
);
1044 LTTNG_ASSERT(session
->ref
.refcount
);
1045 urcu_ref_put(&session
->ref
, session_release
);
1049 * Destroy a session.
1051 * This method does not immediately release/free the session as other
1052 * components may still hold a reference to the session. However,
1053 * the session should no longer be presented to the user.
1055 * Releases the session list's reference to the session
1056 * and marks it as destroyed. Iterations on the session list should be
1057 * mindful of the "destroyed" flag.
1059 void session_destroy(struct ltt_session
*session
)
1062 struct lttng_ht_iter iter
;
1064 LTTNG_ASSERT(!session
->destroyed
);
1065 session
->destroyed
= true;
1068 * Remove immediately from the "session by name" hash table. Only one
1069 * session is expected to exist with a given name for at any given time.
1071 * Even if a session still technically exists for a little while longer,
1072 * there is no point in performing action on a "destroyed" session.
1074 iter
.iter
.node
= &session
->node_by_name
.node
;
1075 ret
= lttng_ht_del(ltt_sessions_ht_by_name
, &iter
);
1078 session_put(session
);
1081 int session_add_destroy_notifier(struct ltt_session
*session
,
1082 ltt_session_destroy_notifier notifier
,
1085 const struct ltt_session_destroy_notifier_element element
= { .notifier
= notifier
,
1086 .user_data
= user_data
};
1088 return lttng_dynamic_array_add_element(&session
->destroy_notifiers
, &element
);
1091 int session_add_clear_notifier(struct ltt_session
*session
,
1092 ltt_session_clear_notifier notifier
,
1095 const struct ltt_session_clear_notifier_element element
= { .notifier
= notifier
,
1096 .user_data
= user_data
};
1098 return lttng_dynamic_array_add_element(&session
->clear_notifiers
, &element
);
1102 * Return a ltt_session structure ptr that matches name. If no session found,
1103 * NULL is returned. This must be called with the session list lock held using
1104 * session_lock_list and session_unlock_list.
1105 * A reference to the session is implicitly acquired by this function.
1107 struct ltt_session
*session_find_by_name(const char *name
)
1109 struct ltt_session
*iter
;
1112 ASSERT_LOCKED(the_session_list
.lock
);
1114 DBG2("Trying to find session by name %s", name
);
1116 cds_list_for_each_entry (iter
, &the_session_list
.head
, list
) {
1117 if (!strncmp(iter
->name
, name
, NAME_MAX
) && !iter
->destroyed
) {
1124 return session_get(iter
) ? iter
: nullptr;
1128 * Return an ltt_session that matches the id. If no session is found,
1129 * NULL is returned. This must be called with rcu_read_lock and
1130 * session list lock held (to guarantee the lifetime of the session).
1132 struct ltt_session
*session_find_by_id(uint64_t id
)
1134 struct lttng_ht_node_u64
*node
;
1135 struct lttng_ht_iter iter
;
1136 struct ltt_session
*ls
;
1138 ASSERT_RCU_READ_LOCKED();
1139 ASSERT_LOCKED(the_session_list
.lock
);
1141 if (!ltt_sessions_ht_by_id
) {
1145 lttng_ht_lookup(ltt_sessions_ht_by_id
, &id
, &iter
);
1146 node
= lttng_ht_iter_get_node_u64(&iter
);
1147 if (node
== nullptr) {
1150 ls
= lttng::utils::container_of(node
, <t_session::node
);
1152 DBG3("Session %" PRIu64
" found by id.", id
);
1153 return session_get(ls
) ? ls
: nullptr;
1156 DBG3("Session %" PRIu64
" NOT found by id", id
);
1161 * Create a new session and add it to the session list.
1162 * Session list lock must be held by the caller.
1164 enum lttng_error_code
1165 session_create(const char *name
, uid_t uid
, gid_t gid
, struct ltt_session
**out_session
)
1168 enum lttng_error_code ret_code
;
1169 struct ltt_session
*new_session
= nullptr;
1171 ASSERT_LOCKED(the_session_list
.lock
);
1173 struct ltt_session
*clashing_session
;
1175 clashing_session
= session_find_by_name(name
);
1176 if (clashing_session
) {
1177 session_put(clashing_session
);
1178 ret_code
= LTTNG_ERR_EXIST_SESS
;
1182 new_session
= zmalloc
<ltt_session
>();
1184 PERROR("Failed to allocate an ltt_session structure");
1185 ret_code
= LTTNG_ERR_NOMEM
;
1189 lttng_dynamic_array_init(&new_session
->destroy_notifiers
,
1190 sizeof(struct ltt_session_destroy_notifier_element
),
1192 lttng_dynamic_array_init(&new_session
->clear_notifiers
,
1193 sizeof(struct ltt_session_clear_notifier_element
),
1195 urcu_ref_init(&new_session
->ref
);
1196 pthread_mutex_init(&new_session
->lock
, nullptr);
1198 new_session
->creation_time
= time(nullptr);
1199 if (new_session
->creation_time
== (time_t) -1) {
1200 PERROR("Failed to sample session creation time");
1201 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1205 /* Create default consumer output. */
1206 new_session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
1207 if (new_session
->consumer
== nullptr) {
1208 ret_code
= LTTNG_ERR_NOMEM
;
1213 ret
= lttng_strncpy(new_session
->name
, name
, sizeof(new_session
->name
));
1215 ret_code
= LTTNG_ERR_SESSION_INVALID_CHAR
;
1218 ret
= validate_name(name
);
1220 ret_code
= LTTNG_ERR_SESSION_INVALID_CHAR
;
1225 bool found_name
= false;
1227 struct tm
*timeinfo
;
1229 timeinfo
= localtime(&new_session
->creation_time
);
1231 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1234 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1235 for (i
= 0; i
< INT_MAX
; i
++) {
1236 struct ltt_session
*clashing_session
;
1239 ret
= snprintf(new_session
->name
,
1240 sizeof(new_session
->name
),
1242 DEFAULT_SESSION_NAME
,
1245 ret
= snprintf(new_session
->name
,
1246 sizeof(new_session
->name
),
1248 DEFAULT_SESSION_NAME
,
1252 new_session
->name_contains_creation_time
= true;
1253 if (ret
== -1 || ret
>= sizeof(new_session
->name
)) {
1255 * Null-terminate in case the name is used
1256 * in logging statements.
1258 new_session
->name
[sizeof(new_session
->name
) - 1] = '\0';
1259 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1263 clashing_session
= session_find_by_name(new_session
->name
);
1264 session_put(clashing_session
);
1265 if (!clashing_session
) {
1271 DBG("Generated session name \"%s\"", new_session
->name
);
1272 new_session
->has_auto_generated_name
= true;
1274 ERR("Failed to auto-generate a session name");
1275 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1280 ret
= gethostname(new_session
->hostname
, sizeof(new_session
->hostname
));
1282 if (errno
== ENAMETOOLONG
) {
1283 new_session
->hostname
[sizeof(new_session
->hostname
) - 1] = '\0';
1284 ERR("Hostname exceeds the maximal permitted length and has been truncated to %s",
1285 new_session
->hostname
);
1287 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1292 new_session
->uid
= uid
;
1293 new_session
->gid
= gid
;
1295 ret
= snapshot_init(&new_session
->snapshot
);
1297 ret_code
= LTTNG_ERR_NOMEM
;
1301 new_session
->rotation_state
= LTTNG_ROTATION_STATE_NO_ROTATION
;
1303 /* Add new session to the session list. */
1304 new_session
->id
= add_session_list(new_session
);
1307 * Add the new session to the ltt_sessions_ht_by_id.
1308 * No ownership is taken by the hash table; it is merely
1309 * a wrapper around the session list used for faster access
1312 add_session_ht(new_session
);
1313 new_session
->published
= true;
1316 * Consumer is left to NULL since the create_session_uri command will
1317 * set it up and, if valid, assign it to the session.
1319 DBG("Tracing session %s created with ID %" PRIu64
" by uid = %d, gid = %d",
1324 ret_code
= LTTNG_OK
;
1327 (void) session_get(new_session
);
1328 *out_session
= new_session
;
1332 session_put(new_session
);
1333 new_session
= nullptr;
1338 * Check if the UID matches the session. Root user has access to all
1341 bool session_access_ok(struct ltt_session
*session
, uid_t uid
)
1343 LTTNG_ASSERT(session
);
1344 return (uid
== session
->uid
) || uid
== 0;
1348 * Set a session's rotation state and reset all associated state.
1350 * This function resets the rotation state (check timers, pending
1351 * flags, etc.) and sets the result of the last rotation. The result
1352 * can be queries by a liblttng-ctl client.
1354 * Be careful of the result passed to this function. For instance,
1355 * on failure to launch a rotation, a client will expect the rotation
1356 * state to be set to "NO_ROTATION". If an error occurred while the
1357 * rotation was "ONGOING", result should be set to "ERROR", which will
1358 * allow a client to report it.
1360 * Must be called with the session and session_list locks held.
1362 int session_reset_rotation_state(struct ltt_session
*session
, enum lttng_rotation_state result
)
1366 ASSERT_LOCKED(the_session_list
.lock
);
1367 ASSERT_LOCKED(session
->lock
);
1369 session
->rotation_state
= result
;
1370 if (session
->rotation_pending_check_timer_enabled
) {
1371 ret
= timer_session_rotation_pending_check_stop(session
);
1373 if (session
->chunk_being_archived
) {
1375 enum lttng_trace_chunk_status chunk_status
;
1377 chunk_status
= lttng_trace_chunk_get_id(session
->chunk_being_archived
, &chunk_id
);
1378 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1379 LTTNG_OPTIONAL_SET(&session
->last_archived_chunk_id
, chunk_id
);
1380 lttng_trace_chunk_put(session
->chunk_being_archived
);
1381 session
->chunk_being_archived
= nullptr;
1383 * Fire the clear reply notifiers if we are completing a clear
1386 session_notify_clear(session
);
1392 * Sample the id of a session looked up via its name.
1393 * Here the term "sampling" hint the caller that this return the id at a given
1394 * point in time with no guarantee that the session for which the id was
1395 * sampled still exist at that point.
1397 * Return 0 when the session is not found,
1398 * Return 1 when the session is found and set `id`.
1400 bool sample_session_id_by_name(const char *name
, uint64_t *id
)
1403 struct lttng_ht_node_str
*node
;
1404 struct lttng_ht_iter iter
;
1405 struct ltt_session
*ls
;
1407 lttng::urcu::read_lock_guard read_lock
;
1409 if (!ltt_sessions_ht_by_name
) {
1414 lttng_ht_lookup(ltt_sessions_ht_by_name
, name
, &iter
);
1415 node
= lttng_ht_iter_get_node_str(&iter
);
1416 if (node
== nullptr) {
1421 ls
= lttng::utils::container_of(node
, <t_session::node_by_name
);
1425 DBG3("Session id `%" PRIu64
"` sampled for session `%s", *id
, name
);
1430 void ls::details::locked_session_release(ltt_session
*session
)
1432 session_unlock(session
);
1433 session_put(session
);
1436 ltt_session::locked_ptr
ls::find_locked_session_by_id(ltt_session::id_t id
)
1438 lttng::urcu::read_lock_guard rcu_lock
;
1439 auto session
= session_find_by_id(id
);
1446 * The pointer falling out of scope will unlock and release the reference to the
1449 session_lock(session
);
1450 return ltt_session::locked_ptr(session
);
1453 ltt_session::sptr
ls::find_session_by_id(ltt_session::id_t id
)
1455 lttng::urcu::read_lock_guard rcu_lock
;
1456 auto session
= session_find_by_id(id
);
1462 return { session
, session_put
};