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
;
75 * Validate the session name for forbidden characters.
77 * Return 0 on success else -1 meaning a forbidden char. has been found.
79 static int validate_name(const char *name
)
86 tmp_name
= strdup(name
);
93 tok
= strpbrk(tmp_name
, forbidden_name_chars
);
95 DBG("Session name %s contains a forbidden character", name
);
96 /* Forbidden character has been found. */
108 * Add a ltt_session structure to the global list.
110 * The caller MUST acquire the session list lock before.
111 * Returns the unique identifier for the session.
113 static uint64_t add_session_list(struct ltt_session
*ls
)
117 cds_list_add(&ls
->list
, &the_session_list
.head
);
118 return the_session_list
.next_uuid
++;
122 * Delete a ltt_session structure to the global list.
124 * The caller MUST acquire the session list lock before.
126 static void del_session_list(struct ltt_session
*ls
)
130 cds_list_del(&ls
->list
);
134 * Return a pointer to the session list.
136 struct ltt_session_list
*session_get_list()
138 return &the_session_list
;
142 * Returns once the session list is empty.
144 void session_list_wait_empty()
146 std::unique_lock
<std::mutex
> list_lock(the_session_list
.lock
);
148 /* Keep waiting until the session list is empty. */
149 the_session_list
.removal_cond
.wait(list_lock
,
150 [] { return cds_list_empty(&the_session_list
.head
); });
154 * Acquire session list lock
156 void session_lock_list() noexcept
158 the_session_list
.lock
.lock();
162 * Try to acquire session list lock
164 int session_trylock_list() noexcept
166 /* Return 0 if successfully acquired. */
167 return the_session_list
.lock
.try_lock() ? 0 : 1;
171 * Release session list lock
173 void session_unlock_list() noexcept
175 the_session_list
.lock
.unlock();
179 * Get the session's consumer destination type.
181 * The caller must hold the session lock.
183 enum consumer_dst_type
session_get_consumer_destination_type(const struct ltt_session
*session
)
186 * The output information is duplicated in both of those session types.
187 * Hence, it doesn't matter from which it is retrieved. However, it is
188 * possible for only one of them to be set.
190 return session
->kernel_session
? session
->kernel_session
->consumer
->type
:
191 session
->ust_session
->consumer
->type
;
195 * Get the session's consumer network hostname.
196 * The caller must ensure that the destination is of type "net".
198 * The caller must hold the session lock.
200 const char *session_get_net_consumer_hostname(const struct ltt_session
*session
)
202 const char *hostname
= nullptr;
203 const struct consumer_output
*output
;
205 output
= session
->kernel_session
? session
->kernel_session
->consumer
:
206 session
->ust_session
->consumer
;
209 * hostname is assumed to be the same for both control and data
212 switch (output
->dst
.net
.control
.dtype
) {
214 hostname
= output
->dst
.net
.control
.dst
.ipv4
;
217 hostname
= output
->dst
.net
.control
.dst
.ipv6
;
226 * Get the session's consumer network control and data ports.
227 * The caller must ensure that the destination is of type "net".
229 * The caller must hold the session lock.
231 void session_get_net_consumer_ports(const struct ltt_session
*session
,
232 uint16_t *control_port
,
235 const struct consumer_output
*output
;
237 output
= session
->kernel_session
? session
->kernel_session
->consumer
:
238 session
->ust_session
->consumer
;
239 *control_port
= output
->dst
.net
.control
.port
;
240 *data_port
= output
->dst
.net
.data
.port
;
244 * Get the location of the latest trace archive produced by a rotation.
246 * The caller must hold the session lock.
248 struct lttng_trace_archive_location
*
249 session_get_trace_archive_location(const struct ltt_session
*session
)
252 struct lttng_trace_archive_location
*location
= nullptr;
253 char *chunk_path
= nullptr;
255 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
||
256 !session
->last_archived_chunk_name
) {
260 switch (session_get_consumer_destination_type(session
)) {
261 case CONSUMER_DST_LOCAL
:
262 ret
= asprintf(&chunk_path
,
263 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
264 session_get_base_path(session
),
265 session
->last_archived_chunk_name
);
269 location
= lttng_trace_archive_location_local_create(chunk_path
);
271 case CONSUMER_DST_NET
:
273 const char *hostname
;
274 uint16_t control_port
, data_port
;
276 hostname
= session_get_net_consumer_hostname(session
);
277 session_get_net_consumer_ports(session
, &control_port
, &data_port
);
278 location
= lttng_trace_archive_location_relay_create(
280 LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
,
283 session
->last_chunk_path
);
295 * Allocate the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name HT.
297 * The session list lock must be held.
299 static int ltt_sessions_ht_alloc()
303 DBG("Allocating ltt_sessions_ht_by_id");
304 ltt_sessions_ht_by_id
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
305 if (!ltt_sessions_ht_by_id
) {
307 ERR("Failed to allocate ltt_sessions_ht_by_id");
311 DBG("Allocating ltt_sessions_ht_by_name");
312 ltt_sessions_ht_by_name
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
313 if (!ltt_sessions_ht_by_name
) {
315 ERR("Failed to allocate ltt_sessions_ht_by_name");
324 * Destroy the ltt_sessions_ht_by_id HT.
326 * The session list lock must be held.
328 static void ltt_sessions_ht_destroy()
330 if (ltt_sessions_ht_by_id
) {
331 lttng_ht_destroy(ltt_sessions_ht_by_id
);
332 ltt_sessions_ht_by_id
= nullptr;
335 if (ltt_sessions_ht_by_name
) {
336 lttng_ht_destroy(ltt_sessions_ht_by_name
);
337 ltt_sessions_ht_by_name
= nullptr;
344 * Add a ltt_session to the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name.
345 * If unallocated, the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name. HTs
346 * are allocated. The session list lock must be held.
348 static void add_session_ht(struct ltt_session
*ls
)
354 if (!ltt_sessions_ht_by_id
) {
355 ret
= ltt_sessions_ht_alloc();
357 ERR("Error allocating the sessions HT");
362 /* Should always be present with ltt_sessions_ht_by_id. */
363 LTTNG_ASSERT(ltt_sessions_ht_by_name
);
365 lttng_ht_node_init_u64(&ls
->node
, ls
->id
);
366 lttng_ht_add_unique_u64(ltt_sessions_ht_by_id
, &ls
->node
);
368 lttng_ht_node_init_str(&ls
->node_by_name
, ls
->name
);
369 lttng_ht_add_unique_str(ltt_sessions_ht_by_name
, &ls
->node_by_name
);
376 * Test if ltt_sessions_ht_by_id/name are empty.
377 * Return `false` if hash table objects are null.
378 * The session list lock must be held.
380 static bool ltt_sessions_ht_empty()
384 if (!ltt_sessions_ht_by_id
) {
385 /* The hash tables do not exist yet. */
389 LTTNG_ASSERT(ltt_sessions_ht_by_name
);
391 if (lttng_ht_get_count(ltt_sessions_ht_by_id
) != 0) {
397 * At this point it is expected that the `ltt_sessions_ht_by_name` ht is
400 * The removal from both hash tables is done in two different
402 * - removal from `ltt_sessions_ht_by_name` is done during
403 * `session_destroy()`
404 * - removal from `ltt_sessions_ht_by_id` is done later
405 * in `session_release()` on the last reference put.
407 * This means that it is possible for `ltt_sessions_ht_by_name` to be
408 * empty but for `ltt_sessions_ht_by_id` to still contain objects when
409 * multiple sessions exists. The reverse is false, hence this sanity
412 LTTNG_ASSERT(lttng_ht_get_count(ltt_sessions_ht_by_name
) == 0);
419 * Remove a ltt_session from the ltt_sessions_ht_by_id.
420 * If empty, the ltt_sessions_ht_by_id/name HTs are freed.
421 * The session list lock must be held.
423 static void del_session_ht(struct ltt_session
*ls
)
425 struct lttng_ht_iter iter
;
429 LTTNG_ASSERT(ltt_sessions_ht_by_id
);
430 LTTNG_ASSERT(ltt_sessions_ht_by_name
);
432 iter
.iter
.node
= &ls
->node
.node
;
433 ret
= lttng_ht_del(ltt_sessions_ht_by_id
, &iter
);
436 if (ltt_sessions_ht_empty()) {
437 DBG("Empty ltt_sessions_ht_by_id/name, destroying hast tables");
438 ltt_sessions_ht_destroy();
443 * Acquire session lock
445 void session_lock(struct ltt_session
*session
)
447 LTTNG_ASSERT(session
);
449 pthread_mutex_lock(&session
->lock
);
453 * Release session lock
455 void session_unlock(struct ltt_session
*session
)
457 LTTNG_ASSERT(session
);
459 pthread_mutex_unlock(&session
->lock
);
462 static int _session_set_trace_chunk_no_lock_check(struct ltt_session
*session
,
463 struct lttng_trace_chunk
*new_trace_chunk
,
464 struct lttng_trace_chunk
**_current_trace_chunk
)
467 unsigned int i
, refs_to_acquire
= 0, refs_acquired
= 0, refs_to_release
= 0;
468 struct cds_lfht_iter iter
;
469 struct consumer_socket
*socket
;
470 struct lttng_trace_chunk
*current_trace_chunk
;
472 enum lttng_trace_chunk_status chunk_status
;
474 lttng::urcu::read_lock_guard read_lock
;
476 * Ownership of current trace chunk is transferred to
477 * `current_trace_chunk`.
479 current_trace_chunk
= session
->current_trace_chunk
;
480 session
->current_trace_chunk
= nullptr;
481 if (session
->ust_session
) {
482 lttng_trace_chunk_put(session
->ust_session
->current_trace_chunk
);
483 session
->ust_session
->current_trace_chunk
= nullptr;
485 if (session
->kernel_session
) {
486 lttng_trace_chunk_put(session
->kernel_session
->current_trace_chunk
);
487 session
->kernel_session
->current_trace_chunk
= nullptr;
489 if (!new_trace_chunk
) {
493 chunk_status
= lttng_trace_chunk_get_id(new_trace_chunk
, &chunk_id
);
494 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
497 refs_to_acquire
+= !!session
->ust_session
;
498 refs_to_acquire
+= !!session
->kernel_session
;
500 for (refs_acquired
= 0; refs_acquired
< refs_to_acquire
; refs_acquired
++) {
501 if (!lttng_trace_chunk_get(new_trace_chunk
)) {
502 ERR("Failed to acquire reference to new trace chunk of session \"%s\"",
508 if (session
->ust_session
) {
509 const uint64_t relayd_id
= session
->ust_session
->consumer
->net_seq_index
;
510 const bool is_local_trace
= session
->ust_session
->consumer
->type
==
513 session
->ust_session
->current_trace_chunk
= new_trace_chunk
;
514 if (is_local_trace
) {
515 enum lttng_error_code ret_error_code
;
518 ust_app_create_channel_subdirectories(session
->ust_session
);
519 if (ret_error_code
!= LTTNG_OK
) {
523 cds_lfht_for_each_entry (
524 session
->ust_session
->consumer
->socks
->ht
, &iter
, socket
, node
.node
) {
525 pthread_mutex_lock(socket
->lock
);
526 ret
= consumer_create_trace_chunk(socket
,
530 DEFAULT_UST_TRACE_DIR
);
531 pthread_mutex_unlock(socket
->lock
);
537 if (session
->kernel_session
) {
538 const uint64_t relayd_id
= session
->kernel_session
->consumer
->net_seq_index
;
539 const bool is_local_trace
= session
->kernel_session
->consumer
->type
==
542 session
->kernel_session
->current_trace_chunk
= new_trace_chunk
;
543 if (is_local_trace
) {
544 enum lttng_error_code ret_error_code
;
547 kernel_create_channel_subdirectories(session
->kernel_session
);
548 if (ret_error_code
!= LTTNG_OK
) {
552 cds_lfht_for_each_entry (
553 session
->kernel_session
->consumer
->socks
->ht
, &iter
, socket
, node
.node
) {
554 pthread_mutex_lock(socket
->lock
);
555 ret
= consumer_create_trace_chunk(socket
,
559 DEFAULT_KERNEL_TRACE_DIR
);
560 pthread_mutex_unlock(socket
->lock
);
568 * Update local current trace chunk state last, only if all remote
569 * creations succeeded.
571 session
->current_trace_chunk
= new_trace_chunk
;
572 LTTNG_OPTIONAL_SET(&session
->most_recent_chunk_id
, chunk_id
);
574 if (_current_trace_chunk
) {
575 *_current_trace_chunk
= current_trace_chunk
;
576 current_trace_chunk
= nullptr;
579 lttng_trace_chunk_put(current_trace_chunk
);
582 if (session
->ust_session
) {
583 session
->ust_session
->current_trace_chunk
= nullptr;
585 if (session
->kernel_session
) {
586 session
->kernel_session
->current_trace_chunk
= nullptr;
589 * Release references taken in the case where all references could not
592 refs_to_release
= refs_to_acquire
- refs_acquired
;
593 for (i
= 0; i
< refs_to_release
; i
++) {
594 lttng_trace_chunk_put(new_trace_chunk
);
600 struct lttng_trace_chunk
*
601 session_create_new_trace_chunk(const struct ltt_session
*session
,
602 const struct consumer_output
*consumer_output_override
,
603 const char *session_base_path_override
,
604 const char *chunk_name_override
)
607 struct lttng_trace_chunk
*trace_chunk
= nullptr;
608 enum lttng_trace_chunk_status chunk_status
;
609 const time_t chunk_creation_ts
= time(nullptr);
611 const char *base_path
;
612 struct lttng_directory_handle
*session_output_directory
= nullptr;
613 const struct lttng_credentials session_credentials
= {
614 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session
->uid
),
615 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session
->gid
),
617 uint64_t next_chunk_id
;
618 const struct consumer_output
*output
;
619 const char *new_path
;
621 if (consumer_output_override
) {
622 output
= consumer_output_override
;
624 LTTNG_ASSERT(session
->ust_session
|| session
->kernel_session
);
625 output
= session
->ust_session
? session
->ust_session
->consumer
:
626 session
->kernel_session
->consumer
;
629 is_local_trace
= output
->type
== CONSUMER_DST_LOCAL
;
630 base_path
= session_base_path_override
?: consumer_output_get_base_path(output
);
632 if (chunk_creation_ts
== (time_t) -1) {
633 PERROR("Failed to sample time while creation session \"%s\" trace chunk",
639 session
->most_recent_chunk_id
.is_set
? session
->most_recent_chunk_id
.value
+ 1 : 0;
641 if (session
->current_trace_chunk
&&
642 !lttng_trace_chunk_get_name_overridden(session
->current_trace_chunk
)) {
643 chunk_status
= lttng_trace_chunk_rename_path(session
->current_trace_chunk
,
644 DEFAULT_CHUNK_TMP_OLD_DIRECTORY
);
645 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
649 if (!session
->current_trace_chunk
) {
650 if (!session
->rotated
) {
656 new_path
= DEFAULT_CHUNK_TMP_NEW_DIRECTORY
;
659 trace_chunk
= lttng_trace_chunk_create(next_chunk_id
, chunk_creation_ts
, new_path
);
664 if (chunk_name_override
) {
665 chunk_status
= lttng_trace_chunk_override_name(trace_chunk
, chunk_name_override
);
666 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
671 if (!is_local_trace
) {
673 * No need to set crendentials and output directory
674 * for remote trace chunks.
679 chunk_status
= lttng_trace_chunk_set_credentials(trace_chunk
, &session_credentials
);
680 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
684 DBG("Creating base output directory of session \"%s\" at %s", session
->name
, base_path
);
685 ret
= utils_mkdir_recursive(base_path
, S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
689 session_output_directory
= lttng_directory_handle_create(base_path
);
690 if (!session_output_directory
) {
693 chunk_status
= lttng_trace_chunk_set_as_owner(trace_chunk
, session_output_directory
);
694 lttng_directory_handle_put(session_output_directory
);
695 session_output_directory
= nullptr;
696 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
702 lttng_directory_handle_put(session_output_directory
);
703 lttng_trace_chunk_put(trace_chunk
);
704 trace_chunk
= nullptr;
708 int session_close_trace_chunk(struct ltt_session
*session
,
709 struct lttng_trace_chunk
*trace_chunk
,
710 enum lttng_trace_chunk_command_type close_command
,
711 char *closed_trace_chunk_path
)
714 bool error_occurred
= false;
715 struct cds_lfht_iter iter
;
716 struct consumer_socket
*socket
;
717 enum lttng_trace_chunk_status chunk_status
;
718 const time_t chunk_close_timestamp
= time(nullptr);
719 const char *new_path
;
721 chunk_status
= lttng_trace_chunk_set_close_command(trace_chunk
, close_command
);
722 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
727 if (chunk_close_timestamp
== (time_t) -1) {
728 ERR("Failed to sample the close timestamp of the current trace chunk of session \"%s\"",
734 if (close_command
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE
&& !session
->rotated
) {
735 /* New chunk stays in session output directory. */
738 /* Use chunk name for new chunk. */
741 if (session
->current_trace_chunk
&&
742 !lttng_trace_chunk_get_name_overridden(session
->current_trace_chunk
)) {
743 /* Rename new chunk path. */
745 lttng_trace_chunk_rename_path(session
->current_trace_chunk
, new_path
);
746 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
751 if (!lttng_trace_chunk_get_name_overridden(trace_chunk
) &&
752 close_command
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
) {
753 const char *old_path
;
755 if (!session
->rotated
) {
760 /* We need to move back the .tmp_old_chunk to its rightful place. */
761 chunk_status
= lttng_trace_chunk_rename_path(trace_chunk
, old_path
);
762 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
767 if (close_command
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
) {
768 session
->rotated
= true;
770 chunk_status
= lttng_trace_chunk_set_close_timestamp(trace_chunk
, chunk_close_timestamp
);
771 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
772 ERR("Failed to set the close timestamp of the current trace chunk of session \"%s\"",
778 if (session
->ust_session
) {
779 const uint64_t relayd_id
= session
->ust_session
->consumer
->net_seq_index
;
781 cds_lfht_for_each_entry (
782 session
->ust_session
->consumer
->socks
->ht
, &iter
, socket
, node
.node
) {
783 pthread_mutex_lock(socket
->lock
);
784 ret
= consumer_close_trace_chunk(socket
,
788 closed_trace_chunk_path
);
789 pthread_mutex_unlock(socket
->lock
);
791 ERR("Failed to close trace chunk on user space consumer");
792 error_occurred
= true;
796 if (session
->kernel_session
) {
797 const uint64_t relayd_id
= session
->kernel_session
->consumer
->net_seq_index
;
799 cds_lfht_for_each_entry (
800 session
->kernel_session
->consumer
->socks
->ht
, &iter
, socket
, node
.node
) {
801 pthread_mutex_lock(socket
->lock
);
802 ret
= consumer_close_trace_chunk(socket
,
806 closed_trace_chunk_path
);
807 pthread_mutex_unlock(socket
->lock
);
809 ERR("Failed to close trace chunk on kernel consumer");
810 error_occurred
= true;
814 ret
= error_occurred
? -1 : 0;
820 * This function skips the metadata channel as the begin/end timestamps of a
821 * metadata packet are useless.
823 * Moreover, opening a packet after a "clear" will cause problems for live
824 * sessions as it will introduce padding that was not part of the first trace
825 * chunk. The relay daemon expects the content of the metadata stream of
826 * successive metadata trace chunks to be strict supersets of one another.
828 * For example, flushing a packet at the beginning of the metadata stream of
829 * a trace chunk resulting from a "clear" session command will cause the
830 * size of the metadata stream of the new trace chunk to not match the size of
831 * the metadata stream of the original chunk. This will confuse the relay
832 * daemon as the same "offset" in a metadata stream will no longer point
833 * to the same content.
835 static enum lttng_error_code
session_kernel_open_packets(struct ltt_session
*session
)
837 enum lttng_error_code ret
= LTTNG_OK
;
838 struct consumer_socket
*socket
;
839 struct lttng_ht_iter iter
;
840 struct cds_lfht_node
*node
;
841 struct ltt_kernel_channel
*chan
;
843 lttng::urcu::read_lock_guard read_lock
;
845 cds_lfht_first(session
->kernel_session
->consumer
->socks
->ht
, &iter
.iter
);
846 node
= cds_lfht_iter_get_node(&iter
.iter
);
847 socket
= caa_container_of(node
, typeof(*socket
), node
.node
);
849 cds_list_for_each_entry (chan
, &session
->kernel_session
->channel_list
.head
, list
) {
852 DBG("Open packet of kernel channel: channel key = %" PRIu64
853 ", session name = %s, session_id = %" PRIu64
,
858 open_ret
= consumer_open_channel_packets(socket
, chan
->key
);
860 /* General error (no known error expected). */
870 enum lttng_error_code
session_open_packets(struct ltt_session
*session
)
872 enum lttng_error_code ret
= LTTNG_OK
;
874 DBG("Opening packets of session channels: session name = %s, session id = %" PRIu64
,
878 if (session
->ust_session
) {
879 ret
= ust_app_open_packets(session
);
880 if (ret
!= LTTNG_OK
) {
885 if (session
->kernel_session
) {
886 ret
= session_kernel_open_packets(session
);
887 if (ret
!= LTTNG_OK
) {
897 * Set a session's current trace chunk.
899 * Must be called with the session lock held.
901 int session_set_trace_chunk(struct ltt_session
*session
,
902 struct lttng_trace_chunk
*new_trace_chunk
,
903 struct lttng_trace_chunk
**current_trace_chunk
)
905 ASSERT_LOCKED(session
->lock
);
906 return _session_set_trace_chunk_no_lock_check(
907 session
, new_trace_chunk
, current_trace_chunk
);
910 static void session_notify_destruction(const struct ltt_session
*session
)
913 const size_t count
= lttng_dynamic_array_get_count(&session
->destroy_notifiers
);
915 for (i
= 0; i
< count
; i
++) {
916 const struct ltt_session_destroy_notifier_element
*element
=
917 (ltt_session_destroy_notifier_element
*) lttng_dynamic_array_get_element(
918 &session
->destroy_notifiers
, i
);
920 element
->notifier(session
, element
->user_data
);
925 * Fire each clear notifier once, and remove them from the array.
927 void session_notify_clear(ltt_session
& session
)
930 const size_t count
= lttng_dynamic_array_get_count(&session
.clear_notifiers
);
932 for (i
= 0; i
< count
; i
++) {
933 const struct ltt_session_clear_notifier_element
*element
=
934 (ltt_session_clear_notifier_element
*) lttng_dynamic_array_get_element(
935 &session
.clear_notifiers
, i
);
937 element
->notifier(&session
, element
->user_data
);
939 lttng_dynamic_array_clear(&session
.clear_notifiers
);
942 static void session_release(struct urcu_ref
*ref
)
945 struct ltt_ust_session
*usess
;
946 struct ltt_kernel_session
*ksess
;
947 struct ltt_session
*session
= lttng::utils::container_of(ref
, <t_session::ref
);
948 const bool session_published
= session
->published
;
950 LTTNG_ASSERT(!session
->chunk_being_archived
);
952 usess
= session
->ust_session
;
953 ksess
= session
->kernel_session
;
955 /* Clean kernel session teardown, keeping data for destroy notifier. */
956 kernel_destroy_session(ksess
);
958 /* UST session teardown, keeping data for destroy notifier. */
960 /* Close any relayd session */
961 consumer_output_send_destroy_relayd(usess
->consumer
);
963 /* Destroy every UST application related to this session. */
964 ret
= ust_app_destroy_trace_all(usess
);
966 ERR("Error in ust_app_destroy_trace_all");
969 /* Clean up the rest, keeping destroy notifier data. */
970 trace_ust_destroy_session(usess
);
974 * Must notify the kernel thread here to update it's poll set in order to
975 * remove the channel(s)' fd just destroyed.
977 ret
= notify_thread_pipe(the_kernel_poll_pipe
[1]);
979 PERROR("write kernel poll pipe");
982 DBG("Destroying session %s (id %" PRIu64
")", session
->name
, session
->id
);
984 snapshot_destroy(&session
->snapshot
);
986 pthread_mutex_destroy(&session
->lock
);
988 if (session_published
) {
989 ASSERT_SESSION_LIST_LOCKED();
990 del_session_list(session
);
991 del_session_ht(session
);
993 session_notify_destruction(session
);
995 consumer_output_put(session
->consumer
);
996 kernel_free_session(ksess
);
997 session
->kernel_session
= nullptr;
999 trace_ust_free_session(usess
);
1000 session
->ust_session
= nullptr;
1002 lttng_dynamic_array_reset(&session
->destroy_notifiers
);
1003 lttng_dynamic_array_reset(&session
->clear_notifiers
);
1004 free(session
->last_archived_chunk_name
);
1005 free(session
->base_path
);
1006 lttng_trigger_put(session
->rotate_trigger
);
1008 if (session_published
) {
1010 * Notify after free-ing to ensure the memory is
1011 * reclaimed before the main thread exits (and prevent memory leak
1014 ASSERT_SESSION_LIST_LOCKED();
1015 the_session_list
.removal_cond
.notify_all();
1020 * Acquire a reference to a session.
1021 * This function may fail (return false); its return value must be checked.
1023 bool session_get(struct ltt_session
*session
)
1025 return urcu_ref_get_unless_zero(&session
->ref
);
1029 * Release a reference to a session.
1031 void session_put(struct ltt_session
*session
)
1037 * The session list lock must be held as any session_put()
1038 * may cause the removal of the session from the session_list.
1040 ASSERT_SESSION_LIST_LOCKED();
1041 LTTNG_ASSERT(session
->ref
.refcount
);
1042 urcu_ref_put(&session
->ref
, session_release
);
1046 * Destroy a session.
1048 * This method does not immediately release/free the session as other
1049 * components may still hold a reference to the session. However,
1050 * the session should no longer be presented to the user.
1052 * Releases the session list's reference to the session
1053 * and marks it as destroyed. Iterations on the session list should be
1054 * mindful of the "destroyed" flag.
1056 void session_destroy(struct ltt_session
*session
)
1059 struct lttng_ht_iter iter
;
1061 LTTNG_ASSERT(!session
->destroyed
);
1062 session
->destroyed
= true;
1065 * Remove immediately from the "session by name" hash table. Only one
1066 * session is expected to exist with a given name for at any given time.
1068 * Even if a session still technically exists for a little while longer,
1069 * there is no point in performing action on a "destroyed" session.
1071 iter
.iter
.node
= &session
->node_by_name
.node
;
1072 ret
= lttng_ht_del(ltt_sessions_ht_by_name
, &iter
);
1075 session_put(session
);
1078 int session_add_destroy_notifier(struct ltt_session
*session
,
1079 ltt_session_destroy_notifier notifier
,
1082 const struct ltt_session_destroy_notifier_element element
= { .notifier
= notifier
,
1083 .user_data
= user_data
};
1085 return lttng_dynamic_array_add_element(&session
->destroy_notifiers
, &element
);
1088 int session_add_clear_notifier(struct ltt_session
*session
,
1089 ltt_session_clear_notifier notifier
,
1092 const struct ltt_session_clear_notifier_element element
= { .notifier
= notifier
,
1093 .user_data
= user_data
};
1095 return lttng_dynamic_array_add_element(&session
->clear_notifiers
, &element
);
1099 * Return a ltt_session structure ptr that matches name. If no session found,
1100 * NULL is returned. This must be called with the session list lock held using
1101 * session_lock_list and session_unlock_list.
1102 * A reference to the session is implicitly acquired by this function.
1104 struct ltt_session
*session_find_by_name(const char *name
)
1106 struct ltt_session
*iter
;
1109 ASSERT_SESSION_LIST_LOCKED();
1111 DBG2("Trying to find session by name %s", name
);
1113 cds_list_for_each_entry (iter
, &the_session_list
.head
, list
) {
1114 if (!strncmp(iter
->name
, name
, NAME_MAX
) && !iter
->destroyed
) {
1121 return session_get(iter
) ? iter
: nullptr;
1125 * Return an ltt_session that matches the id. If no session is found,
1126 * NULL is returned. This must be called with rcu_read_lock and
1127 * session list lock held (to guarantee the lifetime of the session).
1129 struct ltt_session
*session_find_by_id(uint64_t id
)
1131 struct lttng_ht_node_u64
*node
;
1132 struct lttng_ht_iter iter
;
1133 struct ltt_session
*ls
;
1135 ASSERT_RCU_READ_LOCKED();
1136 ASSERT_SESSION_LIST_LOCKED();
1138 if (!ltt_sessions_ht_by_id
) {
1142 lttng_ht_lookup(ltt_sessions_ht_by_id
, &id
, &iter
);
1143 node
= lttng_ht_iter_get_node_u64(&iter
);
1144 if (node
== nullptr) {
1147 ls
= lttng::utils::container_of(node
, <t_session::node
);
1149 DBG3("Session %" PRIu64
" found by id.", id
);
1150 return session_get(ls
) ? ls
: nullptr;
1153 DBG3("Session %" PRIu64
" NOT found by id", id
);
1158 * Create a new session and add it to the session list.
1159 * Session list lock must be held by the caller.
1161 enum lttng_error_code
1162 session_create(const char *name
, uid_t uid
, gid_t gid
, struct ltt_session
**out_session
)
1165 enum lttng_error_code ret_code
;
1166 struct ltt_session
*new_session
= nullptr;
1168 ASSERT_SESSION_LIST_LOCKED();
1170 struct ltt_session
*clashing_session
;
1172 clashing_session
= session_find_by_name(name
);
1173 if (clashing_session
) {
1174 session_put(clashing_session
);
1175 ret_code
= LTTNG_ERR_EXIST_SESS
;
1179 new_session
= zmalloc
<ltt_session
>();
1181 PERROR("Failed to allocate an ltt_session structure");
1182 ret_code
= LTTNG_ERR_NOMEM
;
1186 lttng_dynamic_array_init(&new_session
->destroy_notifiers
,
1187 sizeof(struct ltt_session_destroy_notifier_element
),
1189 lttng_dynamic_array_init(&new_session
->clear_notifiers
,
1190 sizeof(struct ltt_session_clear_notifier_element
),
1192 urcu_ref_init(&new_session
->ref
);
1193 pthread_mutex_init(&new_session
->lock
, nullptr);
1195 new_session
->creation_time
= time(nullptr);
1196 if (new_session
->creation_time
== (time_t) -1) {
1197 PERROR("Failed to sample session creation time");
1198 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1202 /* Create default consumer output. */
1203 new_session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
1204 if (new_session
->consumer
== nullptr) {
1205 ret_code
= LTTNG_ERR_NOMEM
;
1210 ret
= lttng_strncpy(new_session
->name
, name
, sizeof(new_session
->name
));
1212 ret_code
= LTTNG_ERR_SESSION_INVALID_CHAR
;
1215 ret
= validate_name(name
);
1217 ret_code
= LTTNG_ERR_SESSION_INVALID_CHAR
;
1222 bool found_name
= false;
1224 struct tm
*timeinfo
;
1226 timeinfo
= localtime(&new_session
->creation_time
);
1228 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1231 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1232 for (i
= 0; i
< INT_MAX
; i
++) {
1233 struct ltt_session
*clashing_session
;
1236 ret
= snprintf(new_session
->name
,
1237 sizeof(new_session
->name
),
1239 DEFAULT_SESSION_NAME
,
1242 ret
= snprintf(new_session
->name
,
1243 sizeof(new_session
->name
),
1245 DEFAULT_SESSION_NAME
,
1249 new_session
->name_contains_creation_time
= true;
1250 if (ret
== -1 || ret
>= sizeof(new_session
->name
)) {
1252 * Null-terminate in case the name is used
1253 * in logging statements.
1255 new_session
->name
[sizeof(new_session
->name
) - 1] = '\0';
1256 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1260 clashing_session
= session_find_by_name(new_session
->name
);
1261 session_put(clashing_session
);
1262 if (!clashing_session
) {
1268 DBG("Generated session name \"%s\"", new_session
->name
);
1269 new_session
->has_auto_generated_name
= true;
1271 ERR("Failed to auto-generate a session name");
1272 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1277 ret
= gethostname(new_session
->hostname
, sizeof(new_session
->hostname
));
1279 if (errno
== ENAMETOOLONG
) {
1280 new_session
->hostname
[sizeof(new_session
->hostname
) - 1] = '\0';
1281 ERR("Hostname exceeds the maximal permitted length and has been truncated to %s",
1282 new_session
->hostname
);
1284 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1289 new_session
->uid
= uid
;
1290 new_session
->gid
= gid
;
1292 ret
= snapshot_init(&new_session
->snapshot
);
1294 ret_code
= LTTNG_ERR_NOMEM
;
1298 new_session
->rotation_state
= LTTNG_ROTATION_STATE_NO_ROTATION
;
1300 /* Add new session to the session list. */
1301 new_session
->id
= add_session_list(new_session
);
1304 * Add the new session to the ltt_sessions_ht_by_id.
1305 * No ownership is taken by the hash table; it is merely
1306 * a wrapper around the session list used for faster access
1309 add_session_ht(new_session
);
1310 new_session
->published
= true;
1313 * Consumer is left to NULL since the create_session_uri command will
1314 * set it up and, if valid, assign it to the session.
1316 DBG("Tracing session %s created with ID %" PRIu64
" by uid = %d, gid = %d",
1321 ret_code
= LTTNG_OK
;
1324 (void) session_get(new_session
);
1325 *out_session
= new_session
;
1329 session_put(new_session
);
1330 new_session
= nullptr;
1335 * Check if the UID matches the session. Root user has access to all
1338 bool session_access_ok(struct ltt_session
*session
, uid_t uid
)
1340 LTTNG_ASSERT(session
);
1341 return (uid
== session
->uid
) || uid
== 0;
1345 * Set a session's rotation state and reset all associated state.
1347 * This function resets the rotation state (check timers, pending
1348 * flags, etc.) and sets the result of the last rotation. The result
1349 * can be queries by a liblttng-ctl client.
1351 * Be careful of the result passed to this function. For instance,
1352 * on failure to launch a rotation, a client will expect the rotation
1353 * state to be set to "NO_ROTATION". If an error occurred while the
1354 * rotation was "ONGOING", result should be set to "ERROR", which will
1355 * allow a client to report it.
1357 * Must be called with the session and session_list locks held.
1359 int session_reset_rotation_state(ltt_session
& session
, enum lttng_rotation_state result
)
1363 ASSERT_SESSION_LIST_LOCKED();
1364 ASSERT_LOCKED(session
.lock
);
1366 session
.rotation_state
= result
;
1367 if (session
.rotation_pending_check_timer_enabled
) {
1368 ret
= timer_session_rotation_pending_check_stop(session
);
1370 if (session
.chunk_being_archived
) {
1372 enum lttng_trace_chunk_status chunk_status
;
1374 chunk_status
= lttng_trace_chunk_get_id(session
.chunk_being_archived
, &chunk_id
);
1375 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1376 LTTNG_OPTIONAL_SET(&session
.last_archived_chunk_id
, chunk_id
);
1377 lttng_trace_chunk_put(session
.chunk_being_archived
);
1378 session
.chunk_being_archived
= nullptr;
1380 * Fire the clear reply notifiers if we are completing a clear
1383 session_notify_clear(session
);
1389 * Sample the id of a session looked up via its name.
1390 * Here the term "sampling" hint the caller that this return the id at a given
1391 * point in time with no guarantee that the session for which the id was
1392 * sampled still exist at that point.
1394 * Return 0 when the session is not found,
1395 * Return 1 when the session is found and set `id`.
1397 bool sample_session_id_by_name(const char *name
, uint64_t *id
)
1400 struct lttng_ht_node_str
*node
;
1401 struct lttng_ht_iter iter
;
1402 struct ltt_session
*ls
;
1404 lttng::urcu::read_lock_guard read_lock
;
1406 if (!ltt_sessions_ht_by_name
) {
1411 lttng_ht_lookup(ltt_sessions_ht_by_name
, name
, &iter
);
1412 node
= lttng_ht_iter_get_node_str(&iter
);
1413 if (node
== nullptr) {
1418 ls
= lttng::utils::container_of(node
, <t_session::node_by_name
);
1422 DBG3("Session id `%" PRIu64
"` sampled for session `%s", *id
, name
);
1427 void ls::details::locked_session_release(ltt_session
*session
)
1433 session_unlock(session
);
1434 session_put(session
);
1437 ltt_session::locked_ref
ls::find_locked_session_by_id(ltt_session::id_t id
)
1439 lttng::urcu::read_lock_guard rcu_lock
;
1440 auto session
= session_find_by_id(id
);
1447 * The pointer falling out of scope will unlock and release the reference to the
1450 session_lock(session
);
1451 return ltt_session::locked_ref(session
);
1454 ltt_session::sptr
ls::find_session_by_id(ltt_session::id_t id
)
1456 lttng::urcu::read_lock_guard rcu_lock
;
1457 auto session
= session_find_by_id(id
);
1463 return { session
, session_put
};