2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
17 #include <sys/types.h>
20 #include <common/common.hpp>
21 #include <common/sessiond-comm/sessiond-comm.hpp>
22 #include <common/trace-chunk.hpp>
23 #include <common/urcu.hpp>
24 #include <common/utils.hpp>
26 #include "lttng-sessiond.hpp"
27 #include <lttng/location-internal.hpp>
31 #include "session.hpp"
33 #include "trace-ust.hpp"
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
= NULL
;
63 /* Global hash table to keep the sessions, indexed by name. */
64 struct lttng_ht
*ltt_sessions_ht_by_name
= NULL
;
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(void)
143 return &the_session_list
;
147 * Returns once the session list is empty.
149 void session_list_wait_empty(void)
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
,
154 &the_session_list
.lock
);
156 pthread_mutex_unlock(&the_session_list
.lock
);
160 * Acquire session list lock
162 void session_lock_list(void)
164 pthread_mutex_lock(&the_session_list
.lock
);
168 * Try to acquire session list lock
170 int session_trylock_list(void)
172 return pthread_mutex_trylock(&the_session_list
.lock
);
176 * Release session list lock
178 void session_unlock_list(void)
180 pthread_mutex_unlock(&the_session_list
.lock
);
184 * Get the session's consumer destination type.
186 * The caller must hold the session lock.
188 enum consumer_dst_type
session_get_consumer_destination_type(
189 const struct ltt_session
*session
)
192 * The output information is duplicated in both of those session types.
193 * Hence, it doesn't matter from which it is retrieved. However, it is
194 * possible for only one of them to be set.
196 return session
->kernel_session
?
197 session
->kernel_session
->consumer
->type
:
198 session
->ust_session
->consumer
->type
;
202 * Get the session's consumer network hostname.
203 * The caller must ensure that the destination is of type "net".
205 * The caller must hold the session lock.
207 const char *session_get_net_consumer_hostname(const struct ltt_session
*session
)
209 const char *hostname
= NULL
;
210 const struct consumer_output
*output
;
212 output
= session
->kernel_session
?
213 session
->kernel_session
->consumer
:
214 session
->ust_session
->consumer
;
217 * hostname is assumed to be the same for both control and data
220 switch (output
->dst
.net
.control
.dtype
) {
222 hostname
= output
->dst
.net
.control
.dst
.ipv4
;
225 hostname
= output
->dst
.net
.control
.dst
.ipv6
;
234 * Get the session's consumer network control and data ports.
235 * The caller must ensure that the destination is of type "net".
237 * The caller must hold the session lock.
239 void session_get_net_consumer_ports(const struct ltt_session
*session
,
240 uint16_t *control_port
, uint16_t *data_port
)
242 const struct consumer_output
*output
;
244 output
= session
->kernel_session
?
245 session
->kernel_session
->consumer
:
246 session
->ust_session
->consumer
;
247 *control_port
= output
->dst
.net
.control
.port
;
248 *data_port
= output
->dst
.net
.data
.port
;
252 * Get the location of the latest trace archive produced by a rotation.
254 * The caller must hold the session lock.
256 struct lttng_trace_archive_location
*session_get_trace_archive_location(
257 const struct ltt_session
*session
)
260 struct lttng_trace_archive_location
*location
= NULL
;
261 char *chunk_path
= NULL
;
263 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
||
264 !session
->last_archived_chunk_name
) {
268 switch (session_get_consumer_destination_type(session
)) {
269 case CONSUMER_DST_LOCAL
:
270 ret
= asprintf(&chunk_path
,
271 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
272 session_get_base_path(session
),
273 session
->last_archived_chunk_name
);
277 location
= lttng_trace_archive_location_local_create(
280 case CONSUMER_DST_NET
:
282 const char *hostname
;
283 uint16_t control_port
, data_port
;
285 hostname
= session_get_net_consumer_hostname(session
);
286 session_get_net_consumer_ports(session
,
289 location
= lttng_trace_archive_location_relay_create(
291 LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
,
292 control_port
, data_port
, session
->last_chunk_path
);
304 * Allocate the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name HT.
306 * The session list lock must be held.
308 static int ltt_sessions_ht_alloc(void)
312 DBG("Allocating ltt_sessions_ht_by_id");
313 ltt_sessions_ht_by_id
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
314 if (!ltt_sessions_ht_by_id
) {
316 ERR("Failed to allocate ltt_sessions_ht_by_id");
320 DBG("Allocating ltt_sessions_ht_by_name");
321 ltt_sessions_ht_by_name
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
322 if (!ltt_sessions_ht_by_name
) {
324 ERR("Failed to allocate ltt_sessions_ht_by_name");
333 * Destroy the ltt_sessions_ht_by_id HT.
335 * The session list lock must be held.
337 static void ltt_sessions_ht_destroy(void)
339 if (ltt_sessions_ht_by_id
) {
340 lttng_ht_destroy(ltt_sessions_ht_by_id
);
341 ltt_sessions_ht_by_id
= NULL
;
344 if (ltt_sessions_ht_by_name
) {
345 lttng_ht_destroy(ltt_sessions_ht_by_name
);
346 ltt_sessions_ht_by_name
= NULL
;
353 * Add a ltt_session to the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name.
354 * If unallocated, the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name. HTs
355 * are allocated. The session list lock must be held.
357 static void add_session_ht(struct ltt_session
*ls
)
363 if (!ltt_sessions_ht_by_id
) {
364 ret
= ltt_sessions_ht_alloc();
366 ERR("Error allocating the sessions HT");
371 /* Should always be present with ltt_sessions_ht_by_id. */
372 LTTNG_ASSERT(ltt_sessions_ht_by_name
);
374 lttng_ht_node_init_u64(&ls
->node
, ls
->id
);
375 lttng_ht_add_unique_u64(ltt_sessions_ht_by_id
, &ls
->node
);
377 lttng_ht_node_init_str(&ls
->node_by_name
, ls
->name
);
378 lttng_ht_add_unique_str(ltt_sessions_ht_by_name
, &ls
->node_by_name
);
385 * Test if ltt_sessions_ht_by_id/name are empty.
386 * Return `false` if hash table objects are null.
387 * The session list lock must be held.
389 static bool ltt_sessions_ht_empty(void)
393 if (!ltt_sessions_ht_by_id
) {
394 /* The hash tables do not exist yet. */
398 LTTNG_ASSERT(ltt_sessions_ht_by_name
);
400 if (lttng_ht_get_count(ltt_sessions_ht_by_id
) != 0) {
406 * At this point it is expected that the `ltt_sessions_ht_by_name` ht is
409 * The removal from both hash tables is done in two different
411 * - removal from `ltt_sessions_ht_by_name` is done during
412 * `session_destroy()`
413 * - removal from `ltt_sessions_ht_by_id` is done later
414 * in `session_release()` on the last reference put.
416 * This means that it is possible for `ltt_sessions_ht_by_name` to be
417 * empty but for `ltt_sessions_ht_by_id` to still contain objects when
418 * multiple sessions exists. The reverse is false, hence this sanity
421 LTTNG_ASSERT(lttng_ht_get_count(ltt_sessions_ht_by_name
) == 0);
428 * Remove a ltt_session from the ltt_sessions_ht_by_id.
429 * If empty, the ltt_sessions_ht_by_id/name HTs are freed.
430 * The session list lock must be held.
432 static void del_session_ht(struct ltt_session
*ls
)
434 struct lttng_ht_iter iter
;
438 LTTNG_ASSERT(ltt_sessions_ht_by_id
);
439 LTTNG_ASSERT(ltt_sessions_ht_by_name
);
441 iter
.iter
.node
= &ls
->node
.node
;
442 ret
= lttng_ht_del(ltt_sessions_ht_by_id
, &iter
);
445 if (ltt_sessions_ht_empty()) {
446 DBG("Empty ltt_sessions_ht_by_id/name, destroying hast tables");
447 ltt_sessions_ht_destroy();
452 * Acquire session lock
454 void session_lock(struct ltt_session
*session
)
456 LTTNG_ASSERT(session
);
458 pthread_mutex_lock(&session
->lock
);
462 * Release session lock
464 void session_unlock(struct ltt_session
*session
)
466 LTTNG_ASSERT(session
);
468 pthread_mutex_unlock(&session
->lock
);
472 int _session_set_trace_chunk_no_lock_check(struct ltt_session
*session
,
473 struct lttng_trace_chunk
*new_trace_chunk
,
474 struct lttng_trace_chunk
**_current_trace_chunk
)
477 unsigned int i
, refs_to_acquire
= 0, refs_acquired
= 0, refs_to_release
= 0;
478 struct cds_lfht_iter iter
;
479 struct consumer_socket
*socket
;
480 struct lttng_trace_chunk
*current_trace_chunk
;
482 enum lttng_trace_chunk_status chunk_status
;
486 * Ownership of current trace chunk is transferred to
487 * `current_trace_chunk`.
489 current_trace_chunk
= session
->current_trace_chunk
;
490 session
->current_trace_chunk
= NULL
;
491 if (session
->ust_session
) {
492 lttng_trace_chunk_put(
493 session
->ust_session
->current_trace_chunk
);
494 session
->ust_session
->current_trace_chunk
= NULL
;
496 if (session
->kernel_session
) {
497 lttng_trace_chunk_put(
498 session
->kernel_session
->current_trace_chunk
);
499 session
->kernel_session
->current_trace_chunk
= NULL
;
501 if (!new_trace_chunk
) {
505 chunk_status
= lttng_trace_chunk_get_id(new_trace_chunk
, &chunk_id
);
506 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
509 refs_to_acquire
+= !!session
->ust_session
;
510 refs_to_acquire
+= !!session
->kernel_session
;
512 for (refs_acquired
= 0; refs_acquired
< refs_to_acquire
;
514 if (!lttng_trace_chunk_get(new_trace_chunk
)) {
515 ERR("Failed to acquire reference to new trace chunk of session \"%s\"",
521 if (session
->ust_session
) {
522 const uint64_t relayd_id
=
523 session
->ust_session
->consumer
->net_seq_index
;
524 const bool is_local_trace
=
525 session
->ust_session
->consumer
->type
==
528 session
->ust_session
->current_trace_chunk
= new_trace_chunk
;
529 if (is_local_trace
) {
530 enum lttng_error_code ret_error_code
;
532 ret_error_code
= ust_app_create_channel_subdirectories(
533 session
->ust_session
);
534 if (ret_error_code
!= LTTNG_OK
) {
538 cds_lfht_for_each_entry(
539 session
->ust_session
->consumer
->socks
->ht
,
540 &iter
, socket
, node
.node
) {
541 pthread_mutex_lock(socket
->lock
);
542 ret
= consumer_create_trace_chunk(socket
,
544 session
->id
, new_trace_chunk
,
545 DEFAULT_UST_TRACE_DIR
);
546 pthread_mutex_unlock(socket
->lock
);
552 if (session
->kernel_session
) {
553 const uint64_t relayd_id
=
554 session
->kernel_session
->consumer
->net_seq_index
;
555 const bool is_local_trace
=
556 session
->kernel_session
->consumer
->type
==
559 session
->kernel_session
->current_trace_chunk
= new_trace_chunk
;
560 if (is_local_trace
) {
561 enum lttng_error_code ret_error_code
;
563 ret_error_code
= kernel_create_channel_subdirectories(
564 session
->kernel_session
);
565 if (ret_error_code
!= LTTNG_OK
) {
569 cds_lfht_for_each_entry(
570 session
->kernel_session
->consumer
->socks
->ht
,
571 &iter
, socket
, node
.node
) {
572 pthread_mutex_lock(socket
->lock
);
573 ret
= consumer_create_trace_chunk(socket
,
575 session
->id
, new_trace_chunk
,
576 DEFAULT_KERNEL_TRACE_DIR
);
577 pthread_mutex_unlock(socket
->lock
);
585 * Update local current trace chunk state last, only if all remote
586 * creations succeeded.
588 session
->current_trace_chunk
= new_trace_chunk
;
589 LTTNG_OPTIONAL_SET(&session
->most_recent_chunk_id
, chunk_id
);
591 if (_current_trace_chunk
) {
592 *_current_trace_chunk
= current_trace_chunk
;
593 current_trace_chunk
= NULL
;
597 lttng_trace_chunk_put(current_trace_chunk
);
600 if (session
->ust_session
) {
601 session
->ust_session
->current_trace_chunk
= NULL
;
603 if (session
->kernel_session
) {
604 session
->kernel_session
->current_trace_chunk
= NULL
;
607 * Release references taken in the case where all references could not
610 refs_to_release
= refs_to_acquire
- refs_acquired
;
611 for (i
= 0; i
< refs_to_release
; i
++) {
612 lttng_trace_chunk_put(new_trace_chunk
);
618 struct lttng_trace_chunk
*session_create_new_trace_chunk(
619 const struct ltt_session
*session
,
620 const struct consumer_output
*consumer_output_override
,
621 const char *session_base_path_override
,
622 const char *chunk_name_override
)
625 struct lttng_trace_chunk
*trace_chunk
= NULL
;
626 enum lttng_trace_chunk_status chunk_status
;
627 const time_t chunk_creation_ts
= time(NULL
);
629 const char *base_path
;
630 struct lttng_directory_handle
*session_output_directory
= NULL
;
631 const struct lttng_credentials session_credentials
= {
632 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session
->uid
),
633 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session
->gid
),
635 uint64_t next_chunk_id
;
636 const struct consumer_output
*output
;
637 const char *new_path
;
639 if (consumer_output_override
) {
640 output
= consumer_output_override
;
642 LTTNG_ASSERT(session
->ust_session
|| session
->kernel_session
);
643 output
= session
->ust_session
?
644 session
->ust_session
->consumer
:
645 session
->kernel_session
->consumer
;
648 is_local_trace
= output
->type
== CONSUMER_DST_LOCAL
;
649 base_path
= session_base_path_override
? :
650 consumer_output_get_base_path(output
);
652 if (chunk_creation_ts
== (time_t) -1) {
653 PERROR("Failed to sample time while creation session \"%s\" trace chunk",
658 next_chunk_id
= session
->most_recent_chunk_id
.is_set
?
659 session
->most_recent_chunk_id
.value
+ 1 : 0;
661 if (session
->current_trace_chunk
&&
662 !lttng_trace_chunk_get_name_overridden(session
->current_trace_chunk
)) {
663 chunk_status
= lttng_trace_chunk_rename_path(session
->current_trace_chunk
,
664 DEFAULT_CHUNK_TMP_OLD_DIRECTORY
);
665 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
669 if (!session
->current_trace_chunk
) {
670 if (!session
->rotated
) {
676 new_path
= DEFAULT_CHUNK_TMP_NEW_DIRECTORY
;
679 trace_chunk
= lttng_trace_chunk_create(next_chunk_id
,
680 chunk_creation_ts
, new_path
);
685 if (chunk_name_override
) {
686 chunk_status
= lttng_trace_chunk_override_name(trace_chunk
,
687 chunk_name_override
);
688 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
693 if (!is_local_trace
) {
695 * No need to set crendentials and output directory
696 * for remote trace chunks.
701 chunk_status
= lttng_trace_chunk_set_credentials(trace_chunk
,
702 &session_credentials
);
703 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
707 DBG("Creating base output directory of session \"%s\" at %s",
708 session
->name
, base_path
);
709 ret
= utils_mkdir_recursive(base_path
, S_IRWXU
| S_IRWXG
,
710 session
->uid
, session
->gid
);
714 session_output_directory
= lttng_directory_handle_create(base_path
);
715 if (!session_output_directory
) {
718 chunk_status
= lttng_trace_chunk_set_as_owner(trace_chunk
,
719 session_output_directory
);
720 lttng_directory_handle_put(session_output_directory
);
721 session_output_directory
= NULL
;
722 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
728 lttng_directory_handle_put(session_output_directory
);
729 lttng_trace_chunk_put(trace_chunk
);
734 int session_close_trace_chunk(struct ltt_session
*session
,
735 struct lttng_trace_chunk
*trace_chunk
,
736 enum lttng_trace_chunk_command_type close_command
,
737 char *closed_trace_chunk_path
)
740 bool error_occurred
= false;
741 struct cds_lfht_iter iter
;
742 struct consumer_socket
*socket
;
743 enum lttng_trace_chunk_status chunk_status
;
744 const time_t chunk_close_timestamp
= time(NULL
);
745 const char *new_path
;
747 chunk_status
= lttng_trace_chunk_set_close_command(
748 trace_chunk
, close_command
);
749 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
754 if (chunk_close_timestamp
== (time_t) -1) {
755 ERR("Failed to sample the close timestamp of the current trace chunk of session \"%s\"",
761 if (close_command
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE
&& !session
->rotated
) {
762 /* New chunk stays in session output directory. */
765 /* Use chunk name for new chunk. */
768 if (session
->current_trace_chunk
&&
769 !lttng_trace_chunk_get_name_overridden(session
->current_trace_chunk
)) {
770 /* Rename new chunk path. */
771 chunk_status
= lttng_trace_chunk_rename_path(session
->current_trace_chunk
,
773 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
778 if (!lttng_trace_chunk_get_name_overridden(trace_chunk
) &&
779 close_command
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
) {
780 const char *old_path
;
782 if (!session
->rotated
) {
787 /* We need to move back the .tmp_old_chunk to its rightful place. */
788 chunk_status
= lttng_trace_chunk_rename_path(trace_chunk
,
790 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
795 if (close_command
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
) {
796 session
->rotated
= true;
798 chunk_status
= lttng_trace_chunk_set_close_timestamp(trace_chunk
,
799 chunk_close_timestamp
);
800 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
801 ERR("Failed to set the close timestamp of the current trace chunk of session \"%s\"",
807 if (session
->ust_session
) {
808 const uint64_t relayd_id
=
809 session
->ust_session
->consumer
->net_seq_index
;
811 cds_lfht_for_each_entry(
812 session
->ust_session
->consumer
->socks
->ht
,
813 &iter
, socket
, node
.node
) {
814 pthread_mutex_lock(socket
->lock
);
815 ret
= consumer_close_trace_chunk(socket
,
818 trace_chunk
, closed_trace_chunk_path
);
819 pthread_mutex_unlock(socket
->lock
);
821 ERR("Failed to close trace chunk on user space consumer");
822 error_occurred
= true;
826 if (session
->kernel_session
) {
827 const uint64_t relayd_id
=
828 session
->kernel_session
->consumer
->net_seq_index
;
830 cds_lfht_for_each_entry(
831 session
->kernel_session
->consumer
->socks
->ht
,
832 &iter
, socket
, node
.node
) {
833 pthread_mutex_lock(socket
->lock
);
834 ret
= consumer_close_trace_chunk(socket
,
837 trace_chunk
, closed_trace_chunk_path
);
838 pthread_mutex_unlock(socket
->lock
);
840 ERR("Failed to close trace chunk on kernel consumer");
841 error_occurred
= true;
845 ret
= error_occurred
? -1 : 0;
851 * This function skips the metadata channel as the begin/end timestamps of a
852 * metadata packet are useless.
854 * Moreover, opening a packet after a "clear" will cause problems for live
855 * sessions as it will introduce padding that was not part of the first trace
856 * chunk. The relay daemon expects the content of the metadata stream of
857 * successive metadata trace chunks to be strict supersets of one another.
859 * For example, flushing a packet at the beginning of the metadata stream of
860 * a trace chunk resulting from a "clear" session command will cause the
861 * size of the metadata stream of the new trace chunk to not match the size of
862 * the metadata stream of the original chunk. This will confuse the relay
863 * daemon as the same "offset" in a metadata stream will no longer point
864 * to the same content.
867 enum lttng_error_code
session_kernel_open_packets(struct ltt_session
*session
)
869 enum lttng_error_code ret
= LTTNG_OK
;
870 struct consumer_socket
*socket
;
871 struct lttng_ht_iter iter
;
872 struct cds_lfht_node
*node
;
873 struct ltt_kernel_channel
*chan
;
877 cds_lfht_first(session
->kernel_session
->consumer
->socks
->ht
, &iter
.iter
);
878 node
= cds_lfht_iter_get_node(&iter
.iter
);
879 socket
= caa_container_of(node
, typeof(*socket
), node
.node
);
881 cds_list_for_each_entry(chan
,
882 &session
->kernel_session
->channel_list
.head
, list
) {
885 DBG("Open packet of kernel channel: channel key = %" PRIu64
886 ", session name = %s, session_id = %" PRIu64
,
887 chan
->key
, session
->name
, session
->id
);
889 open_ret
= consumer_open_channel_packets(socket
, chan
->key
);
891 /* General error (no known error expected). */
902 enum lttng_error_code
session_open_packets(struct ltt_session
*session
)
904 enum lttng_error_code ret
= LTTNG_OK
;
906 DBG("Opening packets of session channels: session name = %s, session id = %" PRIu64
,
907 session
->name
, session
->id
);
909 if (session
->ust_session
) {
910 ret
= ust_app_open_packets(session
);
911 if (ret
!= LTTNG_OK
) {
916 if (session
->kernel_session
) {
917 ret
= session_kernel_open_packets(session
);
918 if (ret
!= LTTNG_OK
) {
928 * Set a session's current trace chunk.
930 * Must be called with the session lock held.
932 int session_set_trace_chunk(struct ltt_session
*session
,
933 struct lttng_trace_chunk
*new_trace_chunk
,
934 struct lttng_trace_chunk
**current_trace_chunk
)
936 ASSERT_LOCKED(session
->lock
);
937 return _session_set_trace_chunk_no_lock_check(session
, new_trace_chunk
,
938 current_trace_chunk
);
942 void session_notify_destruction(const struct ltt_session
*session
)
945 const size_t count
= lttng_dynamic_array_get_count(
946 &session
->destroy_notifiers
);
948 for (i
= 0; i
< count
; i
++) {
949 const struct ltt_session_destroy_notifier_element
*element
=
950 (ltt_session_destroy_notifier_element
*) lttng_dynamic_array_get_element(
951 &session
->destroy_notifiers
, i
);
953 element
->notifier(session
, element
->user_data
);
958 * Fire each clear notifier once, and remove them from the array.
960 void session_notify_clear(struct ltt_session
*session
)
963 const size_t count
= lttng_dynamic_array_get_count(
964 &session
->clear_notifiers
);
966 for (i
= 0; i
< count
; i
++) {
967 const struct ltt_session_clear_notifier_element
*element
=
968 (ltt_session_clear_notifier_element
*) lttng_dynamic_array_get_element(
969 &session
->clear_notifiers
, i
);
971 element
->notifier(session
, element
->user_data
);
973 lttng_dynamic_array_clear(&session
->clear_notifiers
);
977 void session_release(struct urcu_ref
*ref
)
980 struct ltt_ust_session
*usess
;
981 struct ltt_kernel_session
*ksess
;
982 struct ltt_session
*session
= lttng::utils::container_of(ref
, <t_session::ref
);
983 const bool session_published
= session
->published
;
985 LTTNG_ASSERT(!session
->chunk_being_archived
);
987 usess
= session
->ust_session
;
988 ksess
= session
->kernel_session
;
990 /* Clean kernel session teardown, keeping data for destroy notifier. */
991 kernel_destroy_session(ksess
);
993 /* UST session teardown, keeping data for destroy notifier. */
995 /* Close any relayd session */
996 consumer_output_send_destroy_relayd(usess
->consumer
);
998 /* Destroy every UST application related to this session. */
999 ret
= ust_app_destroy_trace_all(usess
);
1001 ERR("Error in ust_app_destroy_trace_all");
1004 /* Clean up the rest, keeping destroy notifier data. */
1005 trace_ust_destroy_session(usess
);
1009 * Must notify the kernel thread here to update it's poll set in order to
1010 * remove the channel(s)' fd just destroyed.
1012 ret
= notify_thread_pipe(the_kernel_poll_pipe
[1]);
1014 PERROR("write kernel poll pipe");
1017 DBG("Destroying session %s (id %" PRIu64
")", session
->name
, session
->id
);
1019 snapshot_destroy(&session
->snapshot
);
1021 pthread_mutex_destroy(&session
->lock
);
1023 if (session_published
) {
1024 ASSERT_LOCKED(the_session_list
.lock
);
1025 del_session_list(session
);
1026 del_session_ht(session
);
1028 session_notify_destruction(session
);
1030 consumer_output_put(session
->consumer
);
1031 kernel_free_session(ksess
);
1032 session
->kernel_session
= NULL
;
1034 trace_ust_free_session(usess
);
1035 session
->ust_session
= NULL
;
1037 lttng_dynamic_array_reset(&session
->destroy_notifiers
);
1038 lttng_dynamic_array_reset(&session
->clear_notifiers
);
1039 free(session
->last_archived_chunk_name
);
1040 free(session
->base_path
);
1041 lttng_trigger_put(session
->rotate_trigger
);
1043 if (session_published
) {
1045 * Broadcast after free-ing to ensure the memory is
1046 * reclaimed before the main thread exits.
1048 ASSERT_LOCKED(the_session_list
.lock
);
1049 pthread_cond_broadcast(&the_session_list
.removal_cond
);
1054 * Acquire a reference to a session.
1055 * This function may fail (return false); its return value must be checked.
1057 bool session_get(struct ltt_session
*session
)
1059 return urcu_ref_get_unless_zero(&session
->ref
);
1063 * Release a reference to a session.
1065 void session_put(struct ltt_session
*session
)
1071 * The session list lock must be held as any session_put()
1072 * may cause the removal of the session from the session_list.
1074 ASSERT_LOCKED(the_session_list
.lock
);
1075 LTTNG_ASSERT(session
->ref
.refcount
);
1076 urcu_ref_put(&session
->ref
, session_release
);
1080 * Destroy a session.
1082 * This method does not immediately release/free the session as other
1083 * components may still hold a reference to the session. However,
1084 * the session should no longer be presented to the user.
1086 * Releases the session list's reference to the session
1087 * and marks it as destroyed. Iterations on the session list should be
1088 * mindful of the "destroyed" flag.
1090 void session_destroy(struct ltt_session
*session
)
1093 struct lttng_ht_iter iter
;
1095 LTTNG_ASSERT(!session
->destroyed
);
1096 session
->destroyed
= true;
1099 * Remove immediately from the "session by name" hash table. Only one
1100 * session is expected to exist with a given name for at any given time.
1102 * Even if a session still technically exists for a little while longer,
1103 * there is no point in performing action on a "destroyed" session.
1105 iter
.iter
.node
= &session
->node_by_name
.node
;
1106 ret
= lttng_ht_del(ltt_sessions_ht_by_name
, &iter
);
1109 session_put(session
);
1112 int session_add_destroy_notifier(struct ltt_session
*session
,
1113 ltt_session_destroy_notifier notifier
, void *user_data
)
1115 const struct ltt_session_destroy_notifier_element element
= {
1116 .notifier
= notifier
,
1117 .user_data
= user_data
1120 return lttng_dynamic_array_add_element(&session
->destroy_notifiers
,
1124 int session_add_clear_notifier(struct ltt_session
*session
,
1125 ltt_session_clear_notifier notifier
, void *user_data
)
1127 const struct ltt_session_clear_notifier_element element
= {
1128 .notifier
= notifier
,
1129 .user_data
= user_data
1132 return lttng_dynamic_array_add_element(&session
->clear_notifiers
,
1137 * Return a ltt_session structure ptr that matches name. If no session found,
1138 * NULL is returned. This must be called with the session list lock held using
1139 * session_lock_list and session_unlock_list.
1140 * A reference to the session is implicitly acquired by this function.
1142 struct ltt_session
*session_find_by_name(const char *name
)
1144 struct ltt_session
*iter
;
1147 ASSERT_LOCKED(the_session_list
.lock
);
1149 DBG2("Trying to find session by name %s", name
);
1151 cds_list_for_each_entry(iter
, &the_session_list
.head
, list
) {
1152 if (!strncmp(iter
->name
, name
, NAME_MAX
) &&
1160 return session_get(iter
) ? iter
: NULL
;
1164 * Return an ltt_session that matches the id. If no session is found,
1165 * NULL is returned. This must be called with rcu_read_lock and
1166 * session list lock held (to guarantee the lifetime of the session).
1168 struct ltt_session
*session_find_by_id(uint64_t id
)
1170 struct lttng_ht_node_u64
*node
;
1171 struct lttng_ht_iter iter
;
1172 struct ltt_session
*ls
;
1174 ASSERT_RCU_READ_LOCKED();
1175 ASSERT_LOCKED(the_session_list
.lock
);
1177 if (!ltt_sessions_ht_by_id
) {
1181 lttng_ht_lookup(ltt_sessions_ht_by_id
, &id
, &iter
);
1182 node
= lttng_ht_iter_get_node_u64(&iter
);
1186 ls
= lttng::utils::container_of(node
, <t_session::node
);
1188 DBG3("Session %" PRIu64
" found by id.", id
);
1189 return session_get(ls
) ? ls
: NULL
;
1192 DBG3("Session %" PRIu64
" NOT found by id", id
);
1197 * Create a new session and add it to the session list.
1198 * Session list lock must be held by the caller.
1200 enum lttng_error_code
session_create(const char *name
, uid_t uid
, gid_t gid
,
1201 struct ltt_session
**out_session
)
1204 enum lttng_error_code ret_code
;
1205 struct ltt_session
*new_session
= NULL
;
1207 ASSERT_LOCKED(the_session_list
.lock
);
1209 struct ltt_session
*clashing_session
;
1211 clashing_session
= session_find_by_name(name
);
1212 if (clashing_session
) {
1213 session_put(clashing_session
);
1214 ret_code
= LTTNG_ERR_EXIST_SESS
;
1218 new_session
= zmalloc
<ltt_session
>();
1220 PERROR("Failed to allocate an ltt_session structure");
1221 ret_code
= LTTNG_ERR_NOMEM
;
1225 lttng_dynamic_array_init(&new_session
->destroy_notifiers
,
1226 sizeof(struct ltt_session_destroy_notifier_element
),
1228 lttng_dynamic_array_init(&new_session
->clear_notifiers
,
1229 sizeof(struct ltt_session_clear_notifier_element
),
1231 urcu_ref_init(&new_session
->ref
);
1232 pthread_mutex_init(&new_session
->lock
, NULL
);
1234 new_session
->creation_time
= time(NULL
);
1235 if (new_session
->creation_time
== (time_t) -1) {
1236 PERROR("Failed to sample session creation time");
1237 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1241 /* Create default consumer output. */
1242 new_session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
1243 if (new_session
->consumer
== NULL
) {
1244 ret_code
= LTTNG_ERR_NOMEM
;
1249 ret
= lttng_strncpy(new_session
->name
, name
, sizeof(new_session
->name
));
1251 ret_code
= LTTNG_ERR_SESSION_INVALID_CHAR
;
1254 ret
= validate_name(name
);
1256 ret_code
= LTTNG_ERR_SESSION_INVALID_CHAR
;
1261 bool found_name
= false;
1263 struct tm
*timeinfo
;
1265 timeinfo
= localtime(&new_session
->creation_time
);
1267 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1270 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1271 for (i
= 0; i
< INT_MAX
; i
++) {
1272 struct ltt_session
*clashing_session
;
1275 ret
= snprintf(new_session
->name
,
1276 sizeof(new_session
->name
),
1278 DEFAULT_SESSION_NAME
,
1281 ret
= snprintf(new_session
->name
,
1282 sizeof(new_session
->name
),
1284 DEFAULT_SESSION_NAME
, i
,
1287 new_session
->name_contains_creation_time
= true;
1288 if (ret
== -1 || ret
>= sizeof(new_session
->name
)) {
1290 * Null-terminate in case the name is used
1291 * in logging statements.
1293 new_session
->name
[sizeof(new_session
->name
) - 1] = '\0';
1294 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1299 session_find_by_name(new_session
->name
);
1300 session_put(clashing_session
);
1301 if (!clashing_session
) {
1307 DBG("Generated session name \"%s\"", new_session
->name
);
1308 new_session
->has_auto_generated_name
= true;
1310 ERR("Failed to auto-generate a session name");
1311 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1316 ret
= gethostname(new_session
->hostname
, sizeof(new_session
->hostname
));
1318 if (errno
== ENAMETOOLONG
) {
1319 new_session
->hostname
[sizeof(new_session
->hostname
) - 1] = '\0';
1320 ERR("Hostname exceeds the maximal permitted length and has been truncated to %s",
1321 new_session
->hostname
);
1323 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1328 new_session
->uid
= uid
;
1329 new_session
->gid
= gid
;
1331 ret
= snapshot_init(&new_session
->snapshot
);
1333 ret_code
= LTTNG_ERR_NOMEM
;
1337 new_session
->rotation_state
= LTTNG_ROTATION_STATE_NO_ROTATION
;
1339 /* Add new session to the session list. */
1340 new_session
->id
= add_session_list(new_session
);
1343 * Add the new session to the ltt_sessions_ht_by_id.
1344 * No ownership is taken by the hash table; it is merely
1345 * a wrapper around the session list used for faster access
1348 add_session_ht(new_session
);
1349 new_session
->published
= true;
1352 * Consumer is left to NULL since the create_session_uri command will
1353 * set it up and, if valid, assign it to the session.
1355 DBG("Tracing session %s created with ID %" PRIu64
" by uid = %d, gid = %d",
1356 new_session
->name
, new_session
->id
, new_session
->uid
,
1358 ret_code
= LTTNG_OK
;
1361 (void) session_get(new_session
);
1362 *out_session
= new_session
;
1366 session_put(new_session
);
1372 * Check if the UID matches the session. Root user has access to all
1375 bool session_access_ok(struct ltt_session
*session
, uid_t uid
)
1377 LTTNG_ASSERT(session
);
1378 return (uid
== session
->uid
) || uid
== 0;
1382 * Set a session's rotation state and reset all associated state.
1384 * This function resets the rotation state (check timers, pending
1385 * flags, etc.) and sets the result of the last rotation. The result
1386 * can be queries by a liblttng-ctl client.
1388 * Be careful of the result passed to this function. For instance,
1389 * on failure to launch a rotation, a client will expect the rotation
1390 * state to be set to "NO_ROTATION". If an error occurred while the
1391 * rotation was "ONGOING", result should be set to "ERROR", which will
1392 * allow a client to report it.
1394 * Must be called with the session and session_list locks held.
1396 int session_reset_rotation_state(struct ltt_session
*session
,
1397 enum lttng_rotation_state result
)
1401 ASSERT_LOCKED(the_session_list
.lock
);
1402 ASSERT_LOCKED(session
->lock
);
1404 session
->rotation_state
= result
;
1405 if (session
->rotation_pending_check_timer_enabled
) {
1406 ret
= timer_session_rotation_pending_check_stop(session
);
1408 if (session
->chunk_being_archived
) {
1410 enum lttng_trace_chunk_status chunk_status
;
1412 chunk_status
= lttng_trace_chunk_get_id(
1413 session
->chunk_being_archived
,
1415 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1416 LTTNG_OPTIONAL_SET(&session
->last_archived_chunk_id
,
1418 lttng_trace_chunk_put(session
->chunk_being_archived
);
1419 session
->chunk_being_archived
= NULL
;
1421 * Fire the clear reply notifiers if we are completing a clear
1424 session_notify_clear(session
);
1430 * Sample the id of a session looked up via its name.
1431 * Here the term "sampling" hint the caller that this return the id at a given
1432 * point in time with no guarantee that the session for which the id was
1433 * sampled still exist at that point.
1435 * Return 0 when the session is not found,
1436 * Return 1 when the session is found and set `id`.
1438 bool sample_session_id_by_name(const char *name
, uint64_t *id
)
1441 struct lttng_ht_node_str
*node
;
1442 struct lttng_ht_iter iter
;
1443 struct ltt_session
*ls
;
1447 if (!ltt_sessions_ht_by_name
) {
1452 lttng_ht_lookup(ltt_sessions_ht_by_name
, name
, &iter
);
1453 node
= lttng_ht_iter_get_node_str(&iter
);
1459 ls
= lttng::utils::container_of(node
, <t_session::node_by_name
);
1463 DBG3("Session id `%" PRIu64
"` sampled for session `%s", *id
, name
);
1469 void ls::details::locked_session_release(ltt_session
*session
)
1471 session_unlock(session
);
1472 session_put(session
);
1475 ltt_session::locked_ptr
ls::find_locked_session_by_id(ltt_session::id_t id
)
1477 lttng::urcu::read_lock_guard rcu_lock
;
1478 auto session
= session_find_by_id(id
);
1485 * The pointer falling out of scope will unlock and release the reference to the
1488 session_lock(session
);
1489 return ltt_session::locked_ptr(session
);
1492 ltt_session::sptr
ls::find_session_by_id(ltt_session::id_t id
)
1494 lttng::urcu::read_lock_guard rcu_lock
;
1495 auto session
= session_find_by_id(id
);
1501 return {session
, session_put
};