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/utils.hpp>
22 #include <common/trace-chunk.hpp>
23 #include <common/sessiond-comm/sessiond-comm.hpp>
24 #include <lttng/location-internal.hpp>
25 #include "lttng-sessiond.hpp"
28 #include "session.hpp"
30 #include "trace-ust.hpp"
34 struct ltt_session_destroy_notifier_element
{
35 ltt_session_destroy_notifier notifier
;
39 struct ltt_session_clear_notifier_element
{
40 ltt_session_clear_notifier notifier
;
47 * No ltt_session.lock is taken here because those data structure are widely
48 * spread across the lttng-tools code base so before calling functions below
49 * that can read/write a session, the caller MUST acquire the session lock
50 * using session_lock() and session_unlock().
54 * Init tracing session list.
56 * Please see session.h for more explanation and correct usage of the list.
58 static struct ltt_session_list ltt_session_list
= {
59 .lock
= PTHREAD_MUTEX_INITIALIZER
,
60 .removal_cond
= PTHREAD_COND_INITIALIZER
,
62 .head
= CDS_LIST_HEAD_INIT(ltt_session_list
.head
),
65 /* These characters are forbidden in a session name. Used by validate_name. */
66 static const char *forbidden_name_chars
= "/";
68 /* Global hash table to keep the sessions, indexed by id. */
69 static struct lttng_ht
*ltt_sessions_ht_by_id
= NULL
;
70 /* Global hash table to keep the sessions, indexed by name. */
71 static struct lttng_ht
*ltt_sessions_ht_by_name
= NULL
;
74 * Validate the session name for forbidden characters.
76 * Return 0 on success else -1 meaning a forbidden char. has been found.
78 static int validate_name(const char *name
)
85 tmp_name
= strdup(name
);
92 tok
= strpbrk(tmp_name
, forbidden_name_chars
);
94 DBG("Session name %s contains a forbidden character", name
);
95 /* Forbidden character has been found. */
107 * Add a ltt_session structure to the global list.
109 * The caller MUST acquire the session list lock before.
110 * Returns the unique identifier for the session.
112 static uint64_t add_session_list(struct ltt_session
*ls
)
116 cds_list_add(&ls
->list
, <t_session_list
.head
);
117 return ltt_session_list
.next_uuid
++;
121 * Delete a ltt_session structure to the global list.
123 * The caller MUST acquire the session list lock before.
125 static void del_session_list(struct ltt_session
*ls
)
129 cds_list_del(&ls
->list
);
133 * Return a pointer to the session list.
135 struct ltt_session_list
*session_get_list(void)
137 return <t_session_list
;
141 * Returns once the session list is empty.
143 void session_list_wait_empty(void)
145 pthread_mutex_lock(<t_session_list
.lock
);
146 while (!cds_list_empty(<t_session_list
.head
)) {
147 pthread_cond_wait(<t_session_list
.removal_cond
,
148 <t_session_list
.lock
);
150 pthread_mutex_unlock(<t_session_list
.lock
);
154 * Acquire session list lock
156 void session_lock_list(void)
158 pthread_mutex_lock(<t_session_list
.lock
);
162 * Try to acquire session list lock
164 int session_trylock_list(void)
166 return pthread_mutex_trylock(<t_session_list
.lock
);
170 * Release session list lock
172 void session_unlock_list(void)
174 pthread_mutex_unlock(<t_session_list
.lock
);
178 * Get the session's consumer destination type.
180 * The caller must hold the session lock.
182 enum consumer_dst_type
session_get_consumer_destination_type(
183 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
?
191 session
->kernel_session
->consumer
->type
:
192 session
->ust_session
->consumer
->type
;
196 * Get the session's consumer network hostname.
197 * The caller must ensure that the destination is of type "net".
199 * The caller must hold the session lock.
201 const char *session_get_net_consumer_hostname(const struct ltt_session
*session
)
203 const char *hostname
= NULL
;
204 const struct consumer_output
*output
;
206 output
= session
->kernel_session
?
207 session
->kernel_session
->consumer
:
208 session
->ust_session
->consumer
;
211 * hostname is assumed to be the same for both control and data
214 switch (output
->dst
.net
.control
.dtype
) {
216 hostname
= output
->dst
.net
.control
.dst
.ipv4
;
219 hostname
= output
->dst
.net
.control
.dst
.ipv6
;
228 * Get the session's consumer network control and data ports.
229 * The caller must ensure that the destination is of type "net".
231 * The caller must hold the session lock.
233 void session_get_net_consumer_ports(const struct ltt_session
*session
,
234 uint16_t *control_port
, uint16_t *data_port
)
236 const struct consumer_output
*output
;
238 output
= session
->kernel_session
?
239 session
->kernel_session
->consumer
:
240 session
->ust_session
->consumer
;
241 *control_port
= output
->dst
.net
.control
.port
;
242 *data_port
= output
->dst
.net
.data
.port
;
246 * Get the location of the latest trace archive produced by a rotation.
248 * The caller must hold the session lock.
250 struct lttng_trace_archive_location
*session_get_trace_archive_location(
251 const struct ltt_session
*session
)
254 struct lttng_trace_archive_location
*location
= NULL
;
255 char *chunk_path
= NULL
;
257 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
||
258 !session
->last_archived_chunk_name
) {
262 switch (session_get_consumer_destination_type(session
)) {
263 case CONSUMER_DST_LOCAL
:
264 ret
= asprintf(&chunk_path
,
265 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
266 session_get_base_path(session
),
267 session
->last_archived_chunk_name
);
271 location
= lttng_trace_archive_location_local_create(
274 case CONSUMER_DST_NET
:
276 const char *hostname
;
277 uint16_t control_port
, data_port
;
279 hostname
= session_get_net_consumer_hostname(session
);
280 session_get_net_consumer_ports(session
,
283 location
= lttng_trace_archive_location_relay_create(
285 LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
,
286 control_port
, data_port
, session
->last_chunk_path
);
298 * Allocate the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name HT.
300 * The session list lock must be held.
302 static int ltt_sessions_ht_alloc(void)
306 DBG("Allocating ltt_sessions_ht_by_id");
307 ltt_sessions_ht_by_id
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
308 if (!ltt_sessions_ht_by_id
) {
310 ERR("Failed to allocate ltt_sessions_ht_by_id");
314 DBG("Allocating ltt_sessions_ht_by_name");
315 ltt_sessions_ht_by_name
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
316 if (!ltt_sessions_ht_by_name
) {
318 ERR("Failed to allocate ltt_sessions_ht_by_name");
327 * Destroy the ltt_sessions_ht_by_id HT.
329 * The session list lock must be held.
331 static void ltt_sessions_ht_destroy(void)
333 if (ltt_sessions_ht_by_id
) {
334 lttng_ht_destroy(ltt_sessions_ht_by_id
);
335 ltt_sessions_ht_by_id
= NULL
;
338 if (ltt_sessions_ht_by_name
) {
339 lttng_ht_destroy(ltt_sessions_ht_by_name
);
340 ltt_sessions_ht_by_name
= NULL
;
347 * Add a ltt_session to the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name.
348 * If unallocated, the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name. HTs
349 * are allocated. The session list lock must be held.
351 static void add_session_ht(struct ltt_session
*ls
)
357 if (!ltt_sessions_ht_by_id
) {
358 ret
= ltt_sessions_ht_alloc();
360 ERR("Error allocating the sessions HT");
365 /* Should always be present with ltt_sessions_ht_by_id. */
366 LTTNG_ASSERT(ltt_sessions_ht_by_name
);
368 lttng_ht_node_init_u64(&ls
->node
, ls
->id
);
369 lttng_ht_add_unique_u64(ltt_sessions_ht_by_id
, &ls
->node
);
371 lttng_ht_node_init_str(&ls
->node_by_name
, ls
->name
);
372 lttng_ht_add_unique_str(ltt_sessions_ht_by_name
, &ls
->node_by_name
);
379 * Test if ltt_sessions_ht_by_id/name are empty.
380 * Return `false` if hash table objects are null.
381 * The session list lock must be held.
383 static bool ltt_sessions_ht_empty(void)
387 if (!ltt_sessions_ht_by_id
) {
388 /* The hash tables do not exist yet. */
392 LTTNG_ASSERT(ltt_sessions_ht_by_name
);
394 if (lttng_ht_get_count(ltt_sessions_ht_by_id
) != 0) {
400 * At this point it is expected that the `ltt_sessions_ht_by_name` ht is
403 * The removal from both hash tables is done in two different
405 * - removal from `ltt_sessions_ht_by_name` is done during
406 * `session_destroy()`
407 * - removal from `ltt_sessions_ht_by_id` is done later
408 * in `session_release()` on the last reference put.
410 * This means that it is possible for `ltt_sessions_ht_by_name` to be
411 * empty but for `ltt_sessions_ht_by_id` to still contain objects when
412 * multiple sessions exists. The reverse is false, hence this sanity
415 LTTNG_ASSERT(lttng_ht_get_count(ltt_sessions_ht_by_name
) == 0);
422 * Remove a ltt_session from the ltt_sessions_ht_by_id.
423 * If empty, the ltt_sessions_ht_by_id/name HTs are freed.
424 * The session list lock must be held.
426 static void del_session_ht(struct ltt_session
*ls
)
428 struct lttng_ht_iter iter
;
432 LTTNG_ASSERT(ltt_sessions_ht_by_id
);
433 LTTNG_ASSERT(ltt_sessions_ht_by_name
);
435 iter
.iter
.node
= &ls
->node
.node
;
436 ret
= lttng_ht_del(ltt_sessions_ht_by_id
, &iter
);
439 if (ltt_sessions_ht_empty()) {
440 DBG("Empty ltt_sessions_ht_by_id/name, destroying hast tables");
441 ltt_sessions_ht_destroy();
446 * Acquire session lock
448 void session_lock(struct ltt_session
*session
)
450 LTTNG_ASSERT(session
);
452 pthread_mutex_lock(&session
->lock
);
456 * Release session lock
458 void session_unlock(struct ltt_session
*session
)
460 LTTNG_ASSERT(session
);
462 pthread_mutex_unlock(&session
->lock
);
466 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
;
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
= NULL
;
485 if (session
->ust_session
) {
486 lttng_trace_chunk_put(
487 session
->ust_session
->current_trace_chunk
);
488 session
->ust_session
->current_trace_chunk
= NULL
;
490 if (session
->kernel_session
) {
491 lttng_trace_chunk_put(
492 session
->kernel_session
->current_trace_chunk
);
493 session
->kernel_session
->current_trace_chunk
= NULL
;
495 if (!new_trace_chunk
) {
499 chunk_status
= lttng_trace_chunk_get_id(new_trace_chunk
, &chunk_id
);
500 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
503 refs_to_acquire
+= !!session
->ust_session
;
504 refs_to_acquire
+= !!session
->kernel_session
;
506 for (refs_acquired
= 0; refs_acquired
< refs_to_acquire
;
508 if (!lttng_trace_chunk_get(new_trace_chunk
)) {
509 ERR("Failed to acquire reference to new trace chunk of session \"%s\"",
515 if (session
->ust_session
) {
516 const uint64_t relayd_id
=
517 session
->ust_session
->consumer
->net_seq_index
;
518 const bool is_local_trace
=
519 session
->ust_session
->consumer
->type
==
522 session
->ust_session
->current_trace_chunk
= new_trace_chunk
;
523 if (is_local_trace
) {
524 enum lttng_error_code ret_error_code
;
526 ret_error_code
= ust_app_create_channel_subdirectories(
527 session
->ust_session
);
528 if (ret_error_code
!= LTTNG_OK
) {
532 cds_lfht_for_each_entry(
533 session
->ust_session
->consumer
->socks
->ht
,
534 &iter
, socket
, node
.node
) {
535 pthread_mutex_lock(socket
->lock
);
536 ret
= consumer_create_trace_chunk(socket
,
538 session
->id
, new_trace_chunk
,
539 DEFAULT_UST_TRACE_DIR
);
540 pthread_mutex_unlock(socket
->lock
);
546 if (session
->kernel_session
) {
547 const uint64_t relayd_id
=
548 session
->kernel_session
->consumer
->net_seq_index
;
549 const bool is_local_trace
=
550 session
->kernel_session
->consumer
->type
==
553 session
->kernel_session
->current_trace_chunk
= new_trace_chunk
;
554 if (is_local_trace
) {
555 enum lttng_error_code ret_error_code
;
557 ret_error_code
= kernel_create_channel_subdirectories(
558 session
->kernel_session
);
559 if (ret_error_code
!= LTTNG_OK
) {
563 cds_lfht_for_each_entry(
564 session
->kernel_session
->consumer
->socks
->ht
,
565 &iter
, socket
, node
.node
) {
566 pthread_mutex_lock(socket
->lock
);
567 ret
= consumer_create_trace_chunk(socket
,
569 session
->id
, new_trace_chunk
,
570 DEFAULT_KERNEL_TRACE_DIR
);
571 pthread_mutex_unlock(socket
->lock
);
579 * Update local current trace chunk state last, only if all remote
580 * creations succeeded.
582 session
->current_trace_chunk
= new_trace_chunk
;
583 LTTNG_OPTIONAL_SET(&session
->most_recent_chunk_id
, chunk_id
);
585 if (_current_trace_chunk
) {
586 *_current_trace_chunk
= current_trace_chunk
;
587 current_trace_chunk
= NULL
;
591 lttng_trace_chunk_put(current_trace_chunk
);
594 if (session
->ust_session
) {
595 session
->ust_session
->current_trace_chunk
= NULL
;
597 if (session
->kernel_session
) {
598 session
->kernel_session
->current_trace_chunk
= NULL
;
601 * Release references taken in the case where all references could not
604 refs_to_release
= refs_to_acquire
- refs_acquired
;
605 for (i
= 0; i
< refs_to_release
; i
++) {
606 lttng_trace_chunk_put(new_trace_chunk
);
612 struct lttng_trace_chunk
*session_create_new_trace_chunk(
613 const struct ltt_session
*session
,
614 const struct consumer_output
*consumer_output_override
,
615 const char *session_base_path_override
,
616 const char *chunk_name_override
)
619 struct lttng_trace_chunk
*trace_chunk
= NULL
;
620 enum lttng_trace_chunk_status chunk_status
;
621 const time_t chunk_creation_ts
= time(NULL
);
623 const char *base_path
;
624 struct lttng_directory_handle
*session_output_directory
= NULL
;
625 const struct lttng_credentials session_credentials
= {
626 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session
->uid
),
627 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session
->gid
),
629 uint64_t next_chunk_id
;
630 const struct consumer_output
*output
;
631 const char *new_path
;
633 if (consumer_output_override
) {
634 output
= consumer_output_override
;
636 LTTNG_ASSERT(session
->ust_session
|| session
->kernel_session
);
637 output
= session
->ust_session
?
638 session
->ust_session
->consumer
:
639 session
->kernel_session
->consumer
;
642 is_local_trace
= output
->type
== CONSUMER_DST_LOCAL
;
643 base_path
= session_base_path_override
? :
644 consumer_output_get_base_path(output
);
646 if (chunk_creation_ts
== (time_t) -1) {
647 PERROR("Failed to sample time while creation session \"%s\" trace chunk",
652 next_chunk_id
= session
->most_recent_chunk_id
.is_set
?
653 session
->most_recent_chunk_id
.value
+ 1 : 0;
655 if (session
->current_trace_chunk
&&
656 !lttng_trace_chunk_get_name_overridden(session
->current_trace_chunk
)) {
657 chunk_status
= lttng_trace_chunk_rename_path(session
->current_trace_chunk
,
658 DEFAULT_CHUNK_TMP_OLD_DIRECTORY
);
659 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
663 if (!session
->current_trace_chunk
) {
664 if (!session
->rotated
) {
670 new_path
= DEFAULT_CHUNK_TMP_NEW_DIRECTORY
;
673 trace_chunk
= lttng_trace_chunk_create(next_chunk_id
,
674 chunk_creation_ts
, new_path
);
679 if (chunk_name_override
) {
680 chunk_status
= lttng_trace_chunk_override_name(trace_chunk
,
681 chunk_name_override
);
682 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
687 if (!is_local_trace
) {
689 * No need to set crendentials and output directory
690 * for remote trace chunks.
695 chunk_status
= lttng_trace_chunk_set_credentials(trace_chunk
,
696 &session_credentials
);
697 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
701 DBG("Creating base output directory of session \"%s\" at %s",
702 session
->name
, base_path
);
703 ret
= utils_mkdir_recursive(base_path
, S_IRWXU
| S_IRWXG
,
704 session
->uid
, session
->gid
);
708 session_output_directory
= lttng_directory_handle_create(base_path
);
709 if (!session_output_directory
) {
712 chunk_status
= lttng_trace_chunk_set_as_owner(trace_chunk
,
713 session_output_directory
);
714 lttng_directory_handle_put(session_output_directory
);
715 session_output_directory
= NULL
;
716 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
722 lttng_directory_handle_put(session_output_directory
);
723 lttng_trace_chunk_put(trace_chunk
);
728 int session_close_trace_chunk(struct ltt_session
*session
,
729 struct lttng_trace_chunk
*trace_chunk
,
730 enum lttng_trace_chunk_command_type close_command
,
731 char *closed_trace_chunk_path
)
734 bool error_occurred
= false;
735 struct cds_lfht_iter iter
;
736 struct consumer_socket
*socket
;
737 enum lttng_trace_chunk_status chunk_status
;
738 const time_t chunk_close_timestamp
= time(NULL
);
739 const char *new_path
;
741 chunk_status
= lttng_trace_chunk_set_close_command(
742 trace_chunk
, close_command
);
743 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
748 if (chunk_close_timestamp
== (time_t) -1) {
749 ERR("Failed to sample the close timestamp of the current trace chunk of session \"%s\"",
755 if (close_command
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE
&& !session
->rotated
) {
756 /* New chunk stays in session output directory. */
759 /* Use chunk name for new chunk. */
762 if (session
->current_trace_chunk
&&
763 !lttng_trace_chunk_get_name_overridden(session
->current_trace_chunk
)) {
764 /* Rename new chunk path. */
765 chunk_status
= lttng_trace_chunk_rename_path(session
->current_trace_chunk
,
767 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
772 if (!lttng_trace_chunk_get_name_overridden(trace_chunk
) &&
773 close_command
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
) {
774 const char *old_path
;
776 if (!session
->rotated
) {
781 /* We need to move back the .tmp_old_chunk to its rightful place. */
782 chunk_status
= lttng_trace_chunk_rename_path(trace_chunk
,
784 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
789 if (close_command
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
) {
790 session
->rotated
= true;
792 chunk_status
= lttng_trace_chunk_set_close_timestamp(trace_chunk
,
793 chunk_close_timestamp
);
794 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
795 ERR("Failed to set the close timestamp of the current trace chunk of session \"%s\"",
801 if (session
->ust_session
) {
802 const uint64_t relayd_id
=
803 session
->ust_session
->consumer
->net_seq_index
;
805 cds_lfht_for_each_entry(
806 session
->ust_session
->consumer
->socks
->ht
,
807 &iter
, socket
, node
.node
) {
808 pthread_mutex_lock(socket
->lock
);
809 ret
= consumer_close_trace_chunk(socket
,
812 trace_chunk
, closed_trace_chunk_path
);
813 pthread_mutex_unlock(socket
->lock
);
815 ERR("Failed to close trace chunk on user space consumer");
816 error_occurred
= true;
820 if (session
->kernel_session
) {
821 const uint64_t relayd_id
=
822 session
->kernel_session
->consumer
->net_seq_index
;
824 cds_lfht_for_each_entry(
825 session
->kernel_session
->consumer
->socks
->ht
,
826 &iter
, socket
, node
.node
) {
827 pthread_mutex_lock(socket
->lock
);
828 ret
= consumer_close_trace_chunk(socket
,
831 trace_chunk
, closed_trace_chunk_path
);
832 pthread_mutex_unlock(socket
->lock
);
834 ERR("Failed to close trace chunk on kernel consumer");
835 error_occurred
= true;
839 ret
= error_occurred
? -1 : 0;
845 * This function skips the metadata channel as the begin/end timestamps of a
846 * metadata packet are useless.
848 * Moreover, opening a packet after a "clear" will cause problems for live
849 * sessions as it will introduce padding that was not part of the first trace
850 * chunk. The relay daemon expects the content of the metadata stream of
851 * successive metadata trace chunks to be strict supersets of one another.
853 * For example, flushing a packet at the beginning of the metadata stream of
854 * a trace chunk resulting from a "clear" session command will cause the
855 * size of the metadata stream of the new trace chunk to not match the size of
856 * the metadata stream of the original chunk. This will confuse the relay
857 * daemon as the same "offset" in a metadata stream will no longer point
858 * to the same content.
861 enum lttng_error_code
session_kernel_open_packets(struct ltt_session
*session
)
863 enum lttng_error_code ret
= LTTNG_OK
;
864 struct consumer_socket
*socket
;
865 struct lttng_ht_iter iter
;
866 struct cds_lfht_node
*node
;
867 struct ltt_kernel_channel
*chan
;
871 cds_lfht_first(session
->kernel_session
->consumer
->socks
->ht
, &iter
.iter
);
872 node
= cds_lfht_iter_get_node(&iter
.iter
);
873 socket
= container_of(node
, typeof(*socket
), node
.node
);
875 cds_list_for_each_entry(chan
,
876 &session
->kernel_session
->channel_list
.head
, list
) {
879 DBG("Open packet of kernel channel: channel key = %" PRIu64
880 ", session name = %s, session_id = %" PRIu64
,
881 chan
->key
, session
->name
, session
->id
);
883 open_ret
= consumer_open_channel_packets(socket
, chan
->key
);
885 /* General error (no known error expected). */
896 enum lttng_error_code
session_open_packets(struct ltt_session
*session
)
898 enum lttng_error_code ret
= LTTNG_OK
;
900 DBG("Opening packets of session channels: session name = %s, session id = %" PRIu64
,
901 session
->name
, session
->id
);
903 if (session
->ust_session
) {
904 ret
= ust_app_open_packets(session
);
905 if (ret
!= LTTNG_OK
) {
910 if (session
->kernel_session
) {
911 ret
= session_kernel_open_packets(session
);
912 if (ret
!= LTTNG_OK
) {
922 * Set a session's current trace chunk.
924 * Must be called with the session lock held.
926 int session_set_trace_chunk(struct ltt_session
*session
,
927 struct lttng_trace_chunk
*new_trace_chunk
,
928 struct lttng_trace_chunk
**current_trace_chunk
)
930 ASSERT_LOCKED(session
->lock
);
931 return _session_set_trace_chunk_no_lock_check(session
, new_trace_chunk
,
932 current_trace_chunk
);
936 void session_notify_destruction(const struct ltt_session
*session
)
939 const size_t count
= lttng_dynamic_array_get_count(
940 &session
->destroy_notifiers
);
942 for (i
= 0; i
< count
; i
++) {
943 const struct ltt_session_destroy_notifier_element
*element
=
944 (ltt_session_destroy_notifier_element
*) lttng_dynamic_array_get_element(
945 &session
->destroy_notifiers
, i
);
947 element
->notifier(session
, element
->user_data
);
952 * Fire each clear notifier once, and remove them from the array.
954 void session_notify_clear(struct ltt_session
*session
)
957 const size_t count
= lttng_dynamic_array_get_count(
958 &session
->clear_notifiers
);
960 for (i
= 0; i
< count
; i
++) {
961 const struct ltt_session_clear_notifier_element
*element
=
962 (ltt_session_clear_notifier_element
*) lttng_dynamic_array_get_element(
963 &session
->clear_notifiers
, i
);
965 element
->notifier(session
, element
->user_data
);
967 lttng_dynamic_array_clear(&session
->clear_notifiers
);
971 void session_release(struct urcu_ref
*ref
)
974 struct ltt_ust_session
*usess
;
975 struct ltt_kernel_session
*ksess
;
976 struct ltt_session
*session
= container_of(ref
, typeof(*session
), ref
);
977 const bool session_published
= session
->published
;
979 LTTNG_ASSERT(!session
->chunk_being_archived
);
981 usess
= session
->ust_session
;
982 ksess
= session
->kernel_session
;
984 /* Clean kernel session teardown, keeping data for destroy notifier. */
985 kernel_destroy_session(ksess
);
987 /* UST session teardown, keeping data for destroy notifier. */
989 /* Close any relayd session */
990 consumer_output_send_destroy_relayd(usess
->consumer
);
992 /* Destroy every UST application related to this session. */
993 ret
= ust_app_destroy_trace_all(usess
);
995 ERR("Error in ust_app_destroy_trace_all");
998 /* Clean up the rest, keeping destroy notifier data. */
999 trace_ust_destroy_session(usess
);
1003 * Must notify the kernel thread here to update it's poll set in order to
1004 * remove the channel(s)' fd just destroyed.
1006 ret
= notify_thread_pipe(the_kernel_poll_pipe
[1]);
1008 PERROR("write kernel poll pipe");
1011 DBG("Destroying session %s (id %" PRIu64
")", session
->name
, session
->id
);
1013 snapshot_destroy(&session
->snapshot
);
1015 pthread_mutex_destroy(&session
->lock
);
1017 if (session_published
) {
1018 ASSERT_LOCKED(ltt_session_list
.lock
);
1019 del_session_list(session
);
1020 del_session_ht(session
);
1022 session_notify_destruction(session
);
1024 consumer_output_put(session
->consumer
);
1025 kernel_free_session(ksess
);
1026 session
->kernel_session
= NULL
;
1028 trace_ust_free_session(usess
);
1029 session
->ust_session
= NULL
;
1031 lttng_dynamic_array_reset(&session
->destroy_notifiers
);
1032 lttng_dynamic_array_reset(&session
->clear_notifiers
);
1033 free(session
->last_archived_chunk_name
);
1034 free(session
->base_path
);
1036 if (session_published
) {
1038 * Broadcast after free-ing to ensure the memory is
1039 * reclaimed before the main thread exits.
1041 ASSERT_LOCKED(ltt_session_list
.lock
);
1042 pthread_cond_broadcast(<t_session_list
.removal_cond
);
1047 * Acquire a reference to a session.
1048 * This function may fail (return false); its return value must be checked.
1050 bool session_get(struct ltt_session
*session
)
1052 return urcu_ref_get_unless_zero(&session
->ref
);
1056 * Release a reference to a session.
1058 void session_put(struct ltt_session
*session
)
1064 * The session list lock must be held as any session_put()
1065 * may cause the removal of the session from the session_list.
1067 ASSERT_LOCKED(ltt_session_list
.lock
);
1068 LTTNG_ASSERT(session
->ref
.refcount
);
1069 urcu_ref_put(&session
->ref
, session_release
);
1073 * Destroy a session.
1075 * This method does not immediately release/free the session as other
1076 * components may still hold a reference to the session. However,
1077 * the session should no longer be presented to the user.
1079 * Releases the session list's reference to the session
1080 * and marks it as destroyed. Iterations on the session list should be
1081 * mindful of the "destroyed" flag.
1083 void session_destroy(struct ltt_session
*session
)
1086 struct lttng_ht_iter iter
;
1088 LTTNG_ASSERT(!session
->destroyed
);
1089 session
->destroyed
= true;
1092 * Remove immediately from the "session by name" hash table. Only one
1093 * session is expected to exist with a given name for at any given time.
1095 * Even if a session still technically exists for a little while longer,
1096 * there is no point in performing action on a "destroyed" session.
1098 iter
.iter
.node
= &session
->node_by_name
.node
;
1099 ret
= lttng_ht_del(ltt_sessions_ht_by_name
, &iter
);
1102 session_put(session
);
1105 int session_add_destroy_notifier(struct ltt_session
*session
,
1106 ltt_session_destroy_notifier notifier
, void *user_data
)
1108 const struct ltt_session_destroy_notifier_element element
= {
1109 .notifier
= notifier
,
1110 .user_data
= user_data
1113 return lttng_dynamic_array_add_element(&session
->destroy_notifiers
,
1117 int session_add_clear_notifier(struct ltt_session
*session
,
1118 ltt_session_clear_notifier notifier
, void *user_data
)
1120 const struct ltt_session_clear_notifier_element element
= {
1121 .notifier
= notifier
,
1122 .user_data
= user_data
1125 return lttng_dynamic_array_add_element(&session
->clear_notifiers
,
1130 * Return a ltt_session structure ptr that matches name. If no session found,
1131 * NULL is returned. This must be called with the session list lock held using
1132 * session_lock_list and session_unlock_list.
1133 * A reference to the session is implicitly acquired by this function.
1135 struct ltt_session
*session_find_by_name(const char *name
)
1137 struct ltt_session
*iter
;
1140 ASSERT_LOCKED(ltt_session_list
.lock
);
1142 DBG2("Trying to find session by name %s", name
);
1144 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
1145 if (!strncmp(iter
->name
, name
, NAME_MAX
) &&
1153 return session_get(iter
) ? iter
: NULL
;
1157 * Return an ltt_session that matches the id. If no session is found,
1158 * NULL is returned. This must be called with rcu_read_lock and
1159 * session list lock held (to guarantee the lifetime of the session).
1161 struct ltt_session
*session_find_by_id(uint64_t id
)
1163 struct lttng_ht_node_u64
*node
;
1164 struct lttng_ht_iter iter
;
1165 struct ltt_session
*ls
;
1167 ASSERT_RCU_READ_LOCKED();
1168 ASSERT_LOCKED(ltt_session_list
.lock
);
1170 if (!ltt_sessions_ht_by_id
) {
1174 lttng_ht_lookup(ltt_sessions_ht_by_id
, &id
, &iter
);
1175 node
= lttng_ht_iter_get_node_u64(&iter
);
1179 ls
= caa_container_of(node
, struct ltt_session
, node
);
1181 DBG3("Session %" PRIu64
" found by id.", id
);
1182 return session_get(ls
) ? ls
: NULL
;
1185 DBG3("Session %" PRIu64
" NOT found by id", id
);
1190 * Create a new session and add it to the session list.
1191 * Session list lock must be held by the caller.
1193 enum lttng_error_code
session_create(const char *name
, uid_t uid
, gid_t gid
,
1194 struct ltt_session
**out_session
)
1197 enum lttng_error_code ret_code
;
1198 struct ltt_session
*new_session
= NULL
;
1200 ASSERT_LOCKED(ltt_session_list
.lock
);
1202 struct ltt_session
*clashing_session
;
1204 clashing_session
= session_find_by_name(name
);
1205 if (clashing_session
) {
1206 session_put(clashing_session
);
1207 ret_code
= LTTNG_ERR_EXIST_SESS
;
1211 new_session
= zmalloc
<ltt_session
>();
1213 PERROR("Failed to allocate an ltt_session structure");
1214 ret_code
= LTTNG_ERR_NOMEM
;
1218 lttng_dynamic_array_init(&new_session
->destroy_notifiers
,
1219 sizeof(struct ltt_session_destroy_notifier_element
),
1221 lttng_dynamic_array_init(&new_session
->clear_notifiers
,
1222 sizeof(struct ltt_session_clear_notifier_element
),
1224 urcu_ref_init(&new_session
->ref
);
1225 pthread_mutex_init(&new_session
->lock
, NULL
);
1227 new_session
->creation_time
= time(NULL
);
1228 if (new_session
->creation_time
== (time_t) -1) {
1229 PERROR("Failed to sample session creation time");
1230 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1234 /* Create default consumer output. */
1235 new_session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
1236 if (new_session
->consumer
== NULL
) {
1237 ret_code
= LTTNG_ERR_NOMEM
;
1242 ret
= lttng_strncpy(new_session
->name
, name
, sizeof(new_session
->name
));
1244 ret_code
= LTTNG_ERR_SESSION_INVALID_CHAR
;
1247 ret
= validate_name(name
);
1249 ret_code
= LTTNG_ERR_SESSION_INVALID_CHAR
;
1254 bool found_name
= false;
1256 struct tm
*timeinfo
;
1258 timeinfo
= localtime(&new_session
->creation_time
);
1260 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1263 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1264 for (i
= 0; i
< INT_MAX
; i
++) {
1265 struct ltt_session
*clashing_session
;
1268 ret
= snprintf(new_session
->name
,
1269 sizeof(new_session
->name
),
1271 DEFAULT_SESSION_NAME
,
1274 ret
= snprintf(new_session
->name
,
1275 sizeof(new_session
->name
),
1277 DEFAULT_SESSION_NAME
, i
,
1280 new_session
->name_contains_creation_time
= true;
1281 if (ret
== -1 || ret
>= sizeof(new_session
->name
)) {
1283 * Null-terminate in case the name is used
1284 * in logging statements.
1286 new_session
->name
[sizeof(new_session
->name
) - 1] = '\0';
1287 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1292 session_find_by_name(new_session
->name
);
1293 session_put(clashing_session
);
1294 if (!clashing_session
) {
1300 DBG("Generated session name \"%s\"", new_session
->name
);
1301 new_session
->has_auto_generated_name
= true;
1303 ERR("Failed to auto-generate a session name");
1304 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1309 ret
= gethostname(new_session
->hostname
, sizeof(new_session
->hostname
));
1311 if (errno
== ENAMETOOLONG
) {
1312 new_session
->hostname
[sizeof(new_session
->hostname
) - 1] = '\0';
1313 ERR("Hostname exceeds the maximal permitted length and has been truncated to %s",
1314 new_session
->hostname
);
1316 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1321 new_session
->uid
= uid
;
1322 new_session
->gid
= gid
;
1324 ret
= snapshot_init(&new_session
->snapshot
);
1326 ret_code
= LTTNG_ERR_NOMEM
;
1330 new_session
->rotation_state
= LTTNG_ROTATION_STATE_NO_ROTATION
;
1332 /* Add new session to the session list. */
1333 new_session
->id
= add_session_list(new_session
);
1336 * Add the new session to the ltt_sessions_ht_by_id.
1337 * No ownership is taken by the hash table; it is merely
1338 * a wrapper around the session list used for faster access
1341 add_session_ht(new_session
);
1342 new_session
->published
= true;
1345 * Consumer is left to NULL since the create_session_uri command will
1346 * set it up and, if valid, assign it to the session.
1348 DBG("Tracing session %s created with ID %" PRIu64
" by uid = %d, gid = %d",
1349 new_session
->name
, new_session
->id
, new_session
->uid
,
1351 ret_code
= LTTNG_OK
;
1354 (void) session_get(new_session
);
1355 *out_session
= new_session
;
1359 session_put(new_session
);
1365 * Check if the UID matches the session. Root user has access to all
1368 bool session_access_ok(struct ltt_session
*session
, uid_t uid
)
1370 LTTNG_ASSERT(session
);
1371 return (uid
== session
->uid
) || uid
== 0;
1375 * Set a session's rotation state and reset all associated state.
1377 * This function resets the rotation state (check timers, pending
1378 * flags, etc.) and sets the result of the last rotation. The result
1379 * can be queries by a liblttng-ctl client.
1381 * Be careful of the result passed to this function. For instance,
1382 * on failure to launch a rotation, a client will expect the rotation
1383 * state to be set to "NO_ROTATION". If an error occurred while the
1384 * rotation was "ONGOING", result should be set to "ERROR", which will
1385 * allow a client to report it.
1387 * Must be called with the session and session_list locks held.
1389 int session_reset_rotation_state(struct ltt_session
*session
,
1390 enum lttng_rotation_state result
)
1394 ASSERT_LOCKED(ltt_session_list
.lock
);
1395 ASSERT_LOCKED(session
->lock
);
1397 session
->rotation_state
= result
;
1398 if (session
->rotation_pending_check_timer_enabled
) {
1399 ret
= timer_session_rotation_pending_check_stop(session
);
1401 if (session
->chunk_being_archived
) {
1403 enum lttng_trace_chunk_status chunk_status
;
1405 chunk_status
= lttng_trace_chunk_get_id(
1406 session
->chunk_being_archived
,
1408 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1409 LTTNG_OPTIONAL_SET(&session
->last_archived_chunk_id
,
1411 lttng_trace_chunk_put(session
->chunk_being_archived
);
1412 session
->chunk_being_archived
= NULL
;
1414 * Fire the clear reply notifiers if we are completing a clear
1417 session_notify_clear(session
);
1423 * Sample the id of a session looked up via its name.
1424 * Here the term "sampling" hint the caller that this return the id at a given
1425 * point in time with no guarantee that the session for which the id was
1426 * sampled still exist at that point.
1428 * Return 0 when the session is not found,
1429 * Return 1 when the session is found and set `id`.
1431 bool sample_session_id_by_name(const char *name
, uint64_t *id
)
1434 struct lttng_ht_node_str
*node
;
1435 struct lttng_ht_iter iter
;
1436 struct ltt_session
*ls
;
1440 if (!ltt_sessions_ht_by_name
) {
1445 lttng_ht_lookup(ltt_sessions_ht_by_name
, name
, &iter
);
1446 node
= lttng_ht_iter_get_node_str(&iter
);
1452 ls
= caa_container_of(node
, struct ltt_session
, node_by_name
);
1456 DBG3("Session id `%" PRIu64
"` sampled for session `%s", *id
, name
);