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"
35 struct ltt_session_destroy_notifier_element
{
36 ltt_session_destroy_notifier notifier
;
40 struct ltt_session_clear_notifier_element
{
41 ltt_session_clear_notifier notifier
;
48 * No ltt_session.lock is taken here because those data structure are widely
49 * spread across the lttng-tools code base so before calling functions below
50 * that can read/write a session, the caller MUST acquire the session lock
51 * using session_lock() and session_unlock().
54 /* These characters are forbidden in a session name. Used by validate_name. */
55 const char *forbidden_name_chars
= "/";
57 /* Global hash table to keep the sessions, indexed by id. */
58 struct lttng_ht
*ltt_sessions_ht_by_id
= NULL
;
59 /* Global hash table to keep the sessions, indexed by name. */
60 struct lttng_ht
*ltt_sessions_ht_by_name
= NULL
;
63 * Init tracing session list.
65 * Please see session.h for more explanation and correct usage of the list.
67 struct ltt_session_list the_session_list
= {
68 .lock
= PTHREAD_MUTEX_INITIALIZER
,
69 .removal_cond
= PTHREAD_COND_INITIALIZER
,
71 .head
= CDS_LIST_HEAD_INIT(the_session_list
.head
),
76 * Validate the session name for forbidden characters.
78 * Return 0 on success else -1 meaning a forbidden char. has been found.
80 static int validate_name(const char *name
)
87 tmp_name
= strdup(name
);
94 tok
= strpbrk(tmp_name
, forbidden_name_chars
);
96 DBG("Session name %s contains a forbidden character", name
);
97 /* Forbidden character has been found. */
109 * Add a ltt_session structure to the global list.
111 * The caller MUST acquire the session list lock before.
112 * Returns the unique identifier for the session.
114 static uint64_t add_session_list(struct ltt_session
*ls
)
118 cds_list_add(&ls
->list
, &the_session_list
.head
);
119 return the_session_list
.next_uuid
++;
123 * Delete a ltt_session structure to the global list.
125 * The caller MUST acquire the session list lock before.
127 static void del_session_list(struct ltt_session
*ls
)
131 cds_list_del(&ls
->list
);
135 * Return a pointer to the session list.
137 struct ltt_session_list
*session_get_list(void)
139 return &the_session_list
;
143 * Returns once the session list is empty.
145 void session_list_wait_empty(void)
147 pthread_mutex_lock(&the_session_list
.lock
);
148 while (!cds_list_empty(&the_session_list
.head
)) {
149 pthread_cond_wait(&the_session_list
.removal_cond
,
150 &the_session_list
.lock
);
152 pthread_mutex_unlock(&the_session_list
.lock
);
156 * Acquire session list lock
158 void session_lock_list(void)
160 pthread_mutex_lock(&the_session_list
.lock
);
164 * Try to acquire session list lock
166 int session_trylock_list(void)
168 return pthread_mutex_trylock(&the_session_list
.lock
);
172 * Release session list lock
174 void session_unlock_list(void)
176 pthread_mutex_unlock(&the_session_list
.lock
);
180 * Get the session's consumer destination type.
182 * The caller must hold the session lock.
184 enum consumer_dst_type
session_get_consumer_destination_type(
185 const struct ltt_session
*session
)
188 * The output information is duplicated in both of those session types.
189 * Hence, it doesn't matter from which it is retrieved. However, it is
190 * possible for only one of them to be set.
192 return session
->kernel_session
?
193 session
->kernel_session
->consumer
->type
:
194 session
->ust_session
->consumer
->type
;
198 * Get the session's consumer network hostname.
199 * The caller must ensure that the destination is of type "net".
201 * The caller must hold the session lock.
203 const char *session_get_net_consumer_hostname(const struct ltt_session
*session
)
205 const char *hostname
= NULL
;
206 const struct consumer_output
*output
;
208 output
= session
->kernel_session
?
209 session
->kernel_session
->consumer
:
210 session
->ust_session
->consumer
;
213 * hostname is assumed to be the same for both control and data
216 switch (output
->dst
.net
.control
.dtype
) {
218 hostname
= output
->dst
.net
.control
.dst
.ipv4
;
221 hostname
= output
->dst
.net
.control
.dst
.ipv6
;
230 * Get the session's consumer network control and data ports.
231 * The caller must ensure that the destination is of type "net".
233 * The caller must hold the session lock.
235 void session_get_net_consumer_ports(const struct ltt_session
*session
,
236 uint16_t *control_port
, uint16_t *data_port
)
238 const struct consumer_output
*output
;
240 output
= session
->kernel_session
?
241 session
->kernel_session
->consumer
:
242 session
->ust_session
->consumer
;
243 *control_port
= output
->dst
.net
.control
.port
;
244 *data_port
= output
->dst
.net
.data
.port
;
248 * Get the location of the latest trace archive produced by a rotation.
250 * The caller must hold the session lock.
252 struct lttng_trace_archive_location
*session_get_trace_archive_location(
253 const struct ltt_session
*session
)
256 struct lttng_trace_archive_location
*location
= NULL
;
257 char *chunk_path
= NULL
;
259 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
||
260 !session
->last_archived_chunk_name
) {
264 switch (session_get_consumer_destination_type(session
)) {
265 case CONSUMER_DST_LOCAL
:
266 ret
= asprintf(&chunk_path
,
267 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
268 session_get_base_path(session
),
269 session
->last_archived_chunk_name
);
273 location
= lttng_trace_archive_location_local_create(
276 case CONSUMER_DST_NET
:
278 const char *hostname
;
279 uint16_t control_port
, data_port
;
281 hostname
= session_get_net_consumer_hostname(session
);
282 session_get_net_consumer_ports(session
,
285 location
= lttng_trace_archive_location_relay_create(
287 LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
,
288 control_port
, data_port
, session
->last_chunk_path
);
300 * Allocate the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name HT.
302 * The session list lock must be held.
304 static int ltt_sessions_ht_alloc(void)
308 DBG("Allocating ltt_sessions_ht_by_id");
309 ltt_sessions_ht_by_id
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
310 if (!ltt_sessions_ht_by_id
) {
312 ERR("Failed to allocate ltt_sessions_ht_by_id");
316 DBG("Allocating ltt_sessions_ht_by_name");
317 ltt_sessions_ht_by_name
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
318 if (!ltt_sessions_ht_by_name
) {
320 ERR("Failed to allocate ltt_sessions_ht_by_name");
329 * Destroy the ltt_sessions_ht_by_id HT.
331 * The session list lock must be held.
333 static void ltt_sessions_ht_destroy(void)
335 if (ltt_sessions_ht_by_id
) {
336 lttng_ht_destroy(ltt_sessions_ht_by_id
);
337 ltt_sessions_ht_by_id
= NULL
;
340 if (ltt_sessions_ht_by_name
) {
341 lttng_ht_destroy(ltt_sessions_ht_by_name
);
342 ltt_sessions_ht_by_name
= NULL
;
349 * Add a ltt_session to the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name.
350 * If unallocated, the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name. HTs
351 * are allocated. The session list lock must be held.
353 static void add_session_ht(struct ltt_session
*ls
)
359 if (!ltt_sessions_ht_by_id
) {
360 ret
= ltt_sessions_ht_alloc();
362 ERR("Error allocating the sessions HT");
367 /* Should always be present with ltt_sessions_ht_by_id. */
368 LTTNG_ASSERT(ltt_sessions_ht_by_name
);
370 lttng_ht_node_init_u64(&ls
->node
, ls
->id
);
371 lttng_ht_add_unique_u64(ltt_sessions_ht_by_id
, &ls
->node
);
373 lttng_ht_node_init_str(&ls
->node_by_name
, ls
->name
);
374 lttng_ht_add_unique_str(ltt_sessions_ht_by_name
, &ls
->node_by_name
);
381 * Test if ltt_sessions_ht_by_id/name are empty.
382 * Return `false` if hash table objects are null.
383 * The session list lock must be held.
385 static bool ltt_sessions_ht_empty(void)
389 if (!ltt_sessions_ht_by_id
) {
390 /* The hash tables do not exist yet. */
394 LTTNG_ASSERT(ltt_sessions_ht_by_name
);
396 if (lttng_ht_get_count(ltt_sessions_ht_by_id
) != 0) {
402 * At this point it is expected that the `ltt_sessions_ht_by_name` ht is
405 * The removal from both hash tables is done in two different
407 * - removal from `ltt_sessions_ht_by_name` is done during
408 * `session_destroy()`
409 * - removal from `ltt_sessions_ht_by_id` is done later
410 * in `session_release()` on the last reference put.
412 * This means that it is possible for `ltt_sessions_ht_by_name` to be
413 * empty but for `ltt_sessions_ht_by_id` to still contain objects when
414 * multiple sessions exists. The reverse is false, hence this sanity
417 LTTNG_ASSERT(lttng_ht_get_count(ltt_sessions_ht_by_name
) == 0);
424 * Remove a ltt_session from the ltt_sessions_ht_by_id.
425 * If empty, the ltt_sessions_ht_by_id/name HTs are freed.
426 * The session list lock must be held.
428 static void del_session_ht(struct ltt_session
*ls
)
430 struct lttng_ht_iter iter
;
434 LTTNG_ASSERT(ltt_sessions_ht_by_id
);
435 LTTNG_ASSERT(ltt_sessions_ht_by_name
);
437 iter
.iter
.node
= &ls
->node
.node
;
438 ret
= lttng_ht_del(ltt_sessions_ht_by_id
, &iter
);
441 if (ltt_sessions_ht_empty()) {
442 DBG("Empty ltt_sessions_ht_by_id/name, destroying hast tables");
443 ltt_sessions_ht_destroy();
448 * Acquire session lock
450 void session_lock(struct ltt_session
*session
)
452 LTTNG_ASSERT(session
);
454 pthread_mutex_lock(&session
->lock
);
458 * Release session lock
460 void session_unlock(struct ltt_session
*session
)
462 LTTNG_ASSERT(session
);
464 pthread_mutex_unlock(&session
->lock
);
468 int _session_set_trace_chunk_no_lock_check(struct ltt_session
*session
,
469 struct lttng_trace_chunk
*new_trace_chunk
,
470 struct lttng_trace_chunk
**_current_trace_chunk
)
473 unsigned int i
, refs_to_acquire
= 0, refs_acquired
= 0, refs_to_release
= 0;
474 struct cds_lfht_iter iter
;
475 struct consumer_socket
*socket
;
476 struct lttng_trace_chunk
*current_trace_chunk
;
478 enum lttng_trace_chunk_status chunk_status
;
482 * Ownership of current trace chunk is transferred to
483 * `current_trace_chunk`.
485 current_trace_chunk
= session
->current_trace_chunk
;
486 session
->current_trace_chunk
= NULL
;
487 if (session
->ust_session
) {
488 lttng_trace_chunk_put(
489 session
->ust_session
->current_trace_chunk
);
490 session
->ust_session
->current_trace_chunk
= NULL
;
492 if (session
->kernel_session
) {
493 lttng_trace_chunk_put(
494 session
->kernel_session
->current_trace_chunk
);
495 session
->kernel_session
->current_trace_chunk
= NULL
;
497 if (!new_trace_chunk
) {
501 chunk_status
= lttng_trace_chunk_get_id(new_trace_chunk
, &chunk_id
);
502 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
505 refs_to_acquire
+= !!session
->ust_session
;
506 refs_to_acquire
+= !!session
->kernel_session
;
508 for (refs_acquired
= 0; refs_acquired
< refs_to_acquire
;
510 if (!lttng_trace_chunk_get(new_trace_chunk
)) {
511 ERR("Failed to acquire reference to new trace chunk of session \"%s\"",
517 if (session
->ust_session
) {
518 const uint64_t relayd_id
=
519 session
->ust_session
->consumer
->net_seq_index
;
520 const bool is_local_trace
=
521 session
->ust_session
->consumer
->type
==
524 session
->ust_session
->current_trace_chunk
= new_trace_chunk
;
525 if (is_local_trace
) {
526 enum lttng_error_code ret_error_code
;
528 ret_error_code
= ust_app_create_channel_subdirectories(
529 session
->ust_session
);
530 if (ret_error_code
!= LTTNG_OK
) {
534 cds_lfht_for_each_entry(
535 session
->ust_session
->consumer
->socks
->ht
,
536 &iter
, socket
, node
.node
) {
537 pthread_mutex_lock(socket
->lock
);
538 ret
= consumer_create_trace_chunk(socket
,
540 session
->id
, new_trace_chunk
,
541 DEFAULT_UST_TRACE_DIR
);
542 pthread_mutex_unlock(socket
->lock
);
548 if (session
->kernel_session
) {
549 const uint64_t relayd_id
=
550 session
->kernel_session
->consumer
->net_seq_index
;
551 const bool is_local_trace
=
552 session
->kernel_session
->consumer
->type
==
555 session
->kernel_session
->current_trace_chunk
= new_trace_chunk
;
556 if (is_local_trace
) {
557 enum lttng_error_code ret_error_code
;
559 ret_error_code
= kernel_create_channel_subdirectories(
560 session
->kernel_session
);
561 if (ret_error_code
!= LTTNG_OK
) {
565 cds_lfht_for_each_entry(
566 session
->kernel_session
->consumer
->socks
->ht
,
567 &iter
, socket
, node
.node
) {
568 pthread_mutex_lock(socket
->lock
);
569 ret
= consumer_create_trace_chunk(socket
,
571 session
->id
, new_trace_chunk
,
572 DEFAULT_KERNEL_TRACE_DIR
);
573 pthread_mutex_unlock(socket
->lock
);
581 * Update local current trace chunk state last, only if all remote
582 * creations succeeded.
584 session
->current_trace_chunk
= new_trace_chunk
;
585 LTTNG_OPTIONAL_SET(&session
->most_recent_chunk_id
, chunk_id
);
587 if (_current_trace_chunk
) {
588 *_current_trace_chunk
= current_trace_chunk
;
589 current_trace_chunk
= NULL
;
593 lttng_trace_chunk_put(current_trace_chunk
);
596 if (session
->ust_session
) {
597 session
->ust_session
->current_trace_chunk
= NULL
;
599 if (session
->kernel_session
) {
600 session
->kernel_session
->current_trace_chunk
= NULL
;
603 * Release references taken in the case where all references could not
606 refs_to_release
= refs_to_acquire
- refs_acquired
;
607 for (i
= 0; i
< refs_to_release
; i
++) {
608 lttng_trace_chunk_put(new_trace_chunk
);
614 struct lttng_trace_chunk
*session_create_new_trace_chunk(
615 const struct ltt_session
*session
,
616 const struct consumer_output
*consumer_output_override
,
617 const char *session_base_path_override
,
618 const char *chunk_name_override
)
621 struct lttng_trace_chunk
*trace_chunk
= NULL
;
622 enum lttng_trace_chunk_status chunk_status
;
623 const time_t chunk_creation_ts
= time(NULL
);
625 const char *base_path
;
626 struct lttng_directory_handle
*session_output_directory
= NULL
;
627 const struct lttng_credentials session_credentials
= {
628 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session
->uid
),
629 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session
->gid
),
631 uint64_t next_chunk_id
;
632 const struct consumer_output
*output
;
633 const char *new_path
;
635 if (consumer_output_override
) {
636 output
= consumer_output_override
;
638 LTTNG_ASSERT(session
->ust_session
|| session
->kernel_session
);
639 output
= session
->ust_session
?
640 session
->ust_session
->consumer
:
641 session
->kernel_session
->consumer
;
644 is_local_trace
= output
->type
== CONSUMER_DST_LOCAL
;
645 base_path
= session_base_path_override
? :
646 consumer_output_get_base_path(output
);
648 if (chunk_creation_ts
== (time_t) -1) {
649 PERROR("Failed to sample time while creation session \"%s\" trace chunk",
654 next_chunk_id
= session
->most_recent_chunk_id
.is_set
?
655 session
->most_recent_chunk_id
.value
+ 1 : 0;
657 if (session
->current_trace_chunk
&&
658 !lttng_trace_chunk_get_name_overridden(session
->current_trace_chunk
)) {
659 chunk_status
= lttng_trace_chunk_rename_path(session
->current_trace_chunk
,
660 DEFAULT_CHUNK_TMP_OLD_DIRECTORY
);
661 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
665 if (!session
->current_trace_chunk
) {
666 if (!session
->rotated
) {
672 new_path
= DEFAULT_CHUNK_TMP_NEW_DIRECTORY
;
675 trace_chunk
= lttng_trace_chunk_create(next_chunk_id
,
676 chunk_creation_ts
, new_path
);
681 if (chunk_name_override
) {
682 chunk_status
= lttng_trace_chunk_override_name(trace_chunk
,
683 chunk_name_override
);
684 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
689 if (!is_local_trace
) {
691 * No need to set crendentials and output directory
692 * for remote trace chunks.
697 chunk_status
= lttng_trace_chunk_set_credentials(trace_chunk
,
698 &session_credentials
);
699 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
703 DBG("Creating base output directory of session \"%s\" at %s",
704 session
->name
, base_path
);
705 ret
= utils_mkdir_recursive(base_path
, S_IRWXU
| S_IRWXG
,
706 session
->uid
, session
->gid
);
710 session_output_directory
= lttng_directory_handle_create(base_path
);
711 if (!session_output_directory
) {
714 chunk_status
= lttng_trace_chunk_set_as_owner(trace_chunk
,
715 session_output_directory
);
716 lttng_directory_handle_put(session_output_directory
);
717 session_output_directory
= NULL
;
718 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
724 lttng_directory_handle_put(session_output_directory
);
725 lttng_trace_chunk_put(trace_chunk
);
730 int session_close_trace_chunk(struct ltt_session
*session
,
731 struct lttng_trace_chunk
*trace_chunk
,
732 enum lttng_trace_chunk_command_type close_command
,
733 char *closed_trace_chunk_path
)
736 bool error_occurred
= false;
737 struct cds_lfht_iter iter
;
738 struct consumer_socket
*socket
;
739 enum lttng_trace_chunk_status chunk_status
;
740 const time_t chunk_close_timestamp
= time(NULL
);
741 const char *new_path
;
743 chunk_status
= lttng_trace_chunk_set_close_command(
744 trace_chunk
, close_command
);
745 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
750 if (chunk_close_timestamp
== (time_t) -1) {
751 ERR("Failed to sample the close timestamp of the current trace chunk of session \"%s\"",
757 if (close_command
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE
&& !session
->rotated
) {
758 /* New chunk stays in session output directory. */
761 /* Use chunk name for new chunk. */
764 if (session
->current_trace_chunk
&&
765 !lttng_trace_chunk_get_name_overridden(session
->current_trace_chunk
)) {
766 /* Rename new chunk path. */
767 chunk_status
= lttng_trace_chunk_rename_path(session
->current_trace_chunk
,
769 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
774 if (!lttng_trace_chunk_get_name_overridden(trace_chunk
) &&
775 close_command
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
) {
776 const char *old_path
;
778 if (!session
->rotated
) {
783 /* We need to move back the .tmp_old_chunk to its rightful place. */
784 chunk_status
= lttng_trace_chunk_rename_path(trace_chunk
,
786 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
791 if (close_command
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
) {
792 session
->rotated
= true;
794 chunk_status
= lttng_trace_chunk_set_close_timestamp(trace_chunk
,
795 chunk_close_timestamp
);
796 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
797 ERR("Failed to set the close timestamp of the current trace chunk of session \"%s\"",
803 if (session
->ust_session
) {
804 const uint64_t relayd_id
=
805 session
->ust_session
->consumer
->net_seq_index
;
807 cds_lfht_for_each_entry(
808 session
->ust_session
->consumer
->socks
->ht
,
809 &iter
, socket
, node
.node
) {
810 pthread_mutex_lock(socket
->lock
);
811 ret
= consumer_close_trace_chunk(socket
,
814 trace_chunk
, closed_trace_chunk_path
);
815 pthread_mutex_unlock(socket
->lock
);
817 ERR("Failed to close trace chunk on user space consumer");
818 error_occurred
= true;
822 if (session
->kernel_session
) {
823 const uint64_t relayd_id
=
824 session
->kernel_session
->consumer
->net_seq_index
;
826 cds_lfht_for_each_entry(
827 session
->kernel_session
->consumer
->socks
->ht
,
828 &iter
, socket
, node
.node
) {
829 pthread_mutex_lock(socket
->lock
);
830 ret
= consumer_close_trace_chunk(socket
,
833 trace_chunk
, closed_trace_chunk_path
);
834 pthread_mutex_unlock(socket
->lock
);
836 ERR("Failed to close trace chunk on kernel consumer");
837 error_occurred
= true;
841 ret
= error_occurred
? -1 : 0;
847 * This function skips the metadata channel as the begin/end timestamps of a
848 * metadata packet are useless.
850 * Moreover, opening a packet after a "clear" will cause problems for live
851 * sessions as it will introduce padding that was not part of the first trace
852 * chunk. The relay daemon expects the content of the metadata stream of
853 * successive metadata trace chunks to be strict supersets of one another.
855 * For example, flushing a packet at the beginning of the metadata stream of
856 * a trace chunk resulting from a "clear" session command will cause the
857 * size of the metadata stream of the new trace chunk to not match the size of
858 * the metadata stream of the original chunk. This will confuse the relay
859 * daemon as the same "offset" in a metadata stream will no longer point
860 * to the same content.
863 enum lttng_error_code
session_kernel_open_packets(struct ltt_session
*session
)
865 enum lttng_error_code ret
= LTTNG_OK
;
866 struct consumer_socket
*socket
;
867 struct lttng_ht_iter iter
;
868 struct cds_lfht_node
*node
;
869 struct ltt_kernel_channel
*chan
;
873 cds_lfht_first(session
->kernel_session
->consumer
->socks
->ht
, &iter
.iter
);
874 node
= cds_lfht_iter_get_node(&iter
.iter
);
875 socket
= container_of(node
, typeof(*socket
), node
.node
);
877 cds_list_for_each_entry(chan
,
878 &session
->kernel_session
->channel_list
.head
, list
) {
881 DBG("Open packet of kernel channel: channel key = %" PRIu64
882 ", session name = %s, session_id = %" PRIu64
,
883 chan
->key
, session
->name
, session
->id
);
885 open_ret
= consumer_open_channel_packets(socket
, chan
->key
);
887 /* General error (no known error expected). */
898 enum lttng_error_code
session_open_packets(struct ltt_session
*session
)
900 enum lttng_error_code ret
= LTTNG_OK
;
902 DBG("Opening packets of session channels: session name = %s, session id = %" PRIu64
,
903 session
->name
, session
->id
);
905 if (session
->ust_session
) {
906 ret
= ust_app_open_packets(session
);
907 if (ret
!= LTTNG_OK
) {
912 if (session
->kernel_session
) {
913 ret
= session_kernel_open_packets(session
);
914 if (ret
!= LTTNG_OK
) {
924 * Set a session's current trace chunk.
926 * Must be called with the session lock held.
928 int session_set_trace_chunk(struct ltt_session
*session
,
929 struct lttng_trace_chunk
*new_trace_chunk
,
930 struct lttng_trace_chunk
**current_trace_chunk
)
932 ASSERT_LOCKED(session
->lock
);
933 return _session_set_trace_chunk_no_lock_check(session
, new_trace_chunk
,
934 current_trace_chunk
);
938 void session_notify_destruction(const struct ltt_session
*session
)
941 const size_t count
= lttng_dynamic_array_get_count(
942 &session
->destroy_notifiers
);
944 for (i
= 0; i
< count
; i
++) {
945 const struct ltt_session_destroy_notifier_element
*element
=
946 (ltt_session_destroy_notifier_element
*) lttng_dynamic_array_get_element(
947 &session
->destroy_notifiers
, i
);
949 element
->notifier(session
, element
->user_data
);
954 * Fire each clear notifier once, and remove them from the array.
956 void session_notify_clear(struct ltt_session
*session
)
959 const size_t count
= lttng_dynamic_array_get_count(
960 &session
->clear_notifiers
);
962 for (i
= 0; i
< count
; i
++) {
963 const struct ltt_session_clear_notifier_element
*element
=
964 (ltt_session_clear_notifier_element
*) lttng_dynamic_array_get_element(
965 &session
->clear_notifiers
, i
);
967 element
->notifier(session
, element
->user_data
);
969 lttng_dynamic_array_clear(&session
->clear_notifiers
);
973 void session_release(struct urcu_ref
*ref
)
976 struct ltt_ust_session
*usess
;
977 struct ltt_kernel_session
*ksess
;
978 struct ltt_session
*session
= container_of(ref
, typeof(*session
), ref
);
979 const bool session_published
= session
->published
;
981 LTTNG_ASSERT(!session
->chunk_being_archived
);
983 usess
= session
->ust_session
;
984 ksess
= session
->kernel_session
;
986 /* Clean kernel session teardown, keeping data for destroy notifier. */
987 kernel_destroy_session(ksess
);
989 /* UST session teardown, keeping data for destroy notifier. */
991 /* Close any relayd session */
992 consumer_output_send_destroy_relayd(usess
->consumer
);
994 /* Destroy every UST application related to this session. */
995 ret
= ust_app_destroy_trace_all(usess
);
997 ERR("Error in ust_app_destroy_trace_all");
1000 /* Clean up the rest, keeping destroy notifier data. */
1001 trace_ust_destroy_session(usess
);
1005 * Must notify the kernel thread here to update it's poll set in order to
1006 * remove the channel(s)' fd just destroyed.
1008 ret
= notify_thread_pipe(the_kernel_poll_pipe
[1]);
1010 PERROR("write kernel poll pipe");
1013 DBG("Destroying session %s (id %" PRIu64
")", session
->name
, session
->id
);
1015 snapshot_destroy(&session
->snapshot
);
1017 pthread_mutex_destroy(&session
->lock
);
1019 if (session_published
) {
1020 ASSERT_LOCKED(the_session_list
.lock
);
1021 del_session_list(session
);
1022 del_session_ht(session
);
1024 session_notify_destruction(session
);
1026 consumer_output_put(session
->consumer
);
1027 kernel_free_session(ksess
);
1028 session
->kernel_session
= NULL
;
1030 trace_ust_free_session(usess
);
1031 session
->ust_session
= NULL
;
1033 lttng_dynamic_array_reset(&session
->destroy_notifiers
);
1034 lttng_dynamic_array_reset(&session
->clear_notifiers
);
1035 free(session
->last_archived_chunk_name
);
1036 free(session
->base_path
);
1037 lttng_trigger_put(session
->rotate_trigger
);
1039 if (session_published
) {
1041 * Broadcast after free-ing to ensure the memory is
1042 * reclaimed before the main thread exits.
1044 ASSERT_LOCKED(the_session_list
.lock
);
1045 pthread_cond_broadcast(&the_session_list
.removal_cond
);
1050 * Acquire a reference to a session.
1051 * This function may fail (return false); its return value must be checked.
1053 bool session_get(struct ltt_session
*session
)
1055 return urcu_ref_get_unless_zero(&session
->ref
);
1059 * Release a reference to a session.
1061 void session_put(struct ltt_session
*session
)
1067 * The session list lock must be held as any session_put()
1068 * may cause the removal of the session from the session_list.
1070 ASSERT_LOCKED(the_session_list
.lock
);
1071 LTTNG_ASSERT(session
->ref
.refcount
);
1072 urcu_ref_put(&session
->ref
, session_release
);
1076 * Destroy a session.
1078 * This method does not immediately release/free the session as other
1079 * components may still hold a reference to the session. However,
1080 * the session should no longer be presented to the user.
1082 * Releases the session list's reference to the session
1083 * and marks it as destroyed. Iterations on the session list should be
1084 * mindful of the "destroyed" flag.
1086 void session_destroy(struct ltt_session
*session
)
1089 struct lttng_ht_iter iter
;
1091 LTTNG_ASSERT(!session
->destroyed
);
1092 session
->destroyed
= true;
1095 * Remove immediately from the "session by name" hash table. Only one
1096 * session is expected to exist with a given name for at any given time.
1098 * Even if a session still technically exists for a little while longer,
1099 * there is no point in performing action on a "destroyed" session.
1101 iter
.iter
.node
= &session
->node_by_name
.node
;
1102 ret
= lttng_ht_del(ltt_sessions_ht_by_name
, &iter
);
1105 session_put(session
);
1108 int session_add_destroy_notifier(struct ltt_session
*session
,
1109 ltt_session_destroy_notifier notifier
, void *user_data
)
1111 const struct ltt_session_destroy_notifier_element element
= {
1112 .notifier
= notifier
,
1113 .user_data
= user_data
1116 return lttng_dynamic_array_add_element(&session
->destroy_notifiers
,
1120 int session_add_clear_notifier(struct ltt_session
*session
,
1121 ltt_session_clear_notifier notifier
, void *user_data
)
1123 const struct ltt_session_clear_notifier_element element
= {
1124 .notifier
= notifier
,
1125 .user_data
= user_data
1128 return lttng_dynamic_array_add_element(&session
->clear_notifiers
,
1133 * Return a ltt_session structure ptr that matches name. If no session found,
1134 * NULL is returned. This must be called with the session list lock held using
1135 * session_lock_list and session_unlock_list.
1136 * A reference to the session is implicitly acquired by this function.
1138 struct ltt_session
*session_find_by_name(const char *name
)
1140 struct ltt_session
*iter
;
1143 ASSERT_LOCKED(the_session_list
.lock
);
1145 DBG2("Trying to find session by name %s", name
);
1147 cds_list_for_each_entry(iter
, &the_session_list
.head
, list
) {
1148 if (!strncmp(iter
->name
, name
, NAME_MAX
) &&
1156 return session_get(iter
) ? iter
: NULL
;
1160 * Return an ltt_session that matches the id. If no session is found,
1161 * NULL is returned. This must be called with rcu_read_lock and
1162 * session list lock held (to guarantee the lifetime of the session).
1164 struct ltt_session
*session_find_by_id(uint64_t id
)
1166 struct lttng_ht_node_u64
*node
;
1167 struct lttng_ht_iter iter
;
1168 struct ltt_session
*ls
;
1170 ASSERT_RCU_READ_LOCKED();
1171 ASSERT_LOCKED(the_session_list
.lock
);
1173 if (!ltt_sessions_ht_by_id
) {
1177 lttng_ht_lookup(ltt_sessions_ht_by_id
, &id
, &iter
);
1178 node
= lttng_ht_iter_get_node_u64(&iter
);
1182 ls
= caa_container_of(node
, struct ltt_session
, node
);
1184 DBG3("Session %" PRIu64
" found by id.", id
);
1185 return session_get(ls
) ? ls
: NULL
;
1188 DBG3("Session %" PRIu64
" NOT found by id", id
);
1193 * Create a new session and add it to the session list.
1194 * Session list lock must be held by the caller.
1196 enum lttng_error_code
session_create(const char *name
, uid_t uid
, gid_t gid
,
1197 struct ltt_session
**out_session
)
1200 enum lttng_error_code ret_code
;
1201 struct ltt_session
*new_session
= NULL
;
1203 ASSERT_LOCKED(the_session_list
.lock
);
1205 struct ltt_session
*clashing_session
;
1207 clashing_session
= session_find_by_name(name
);
1208 if (clashing_session
) {
1209 session_put(clashing_session
);
1210 ret_code
= LTTNG_ERR_EXIST_SESS
;
1214 new_session
= zmalloc
<ltt_session
>();
1216 PERROR("Failed to allocate an ltt_session structure");
1217 ret_code
= LTTNG_ERR_NOMEM
;
1221 lttng_dynamic_array_init(&new_session
->destroy_notifiers
,
1222 sizeof(struct ltt_session_destroy_notifier_element
),
1224 lttng_dynamic_array_init(&new_session
->clear_notifiers
,
1225 sizeof(struct ltt_session_clear_notifier_element
),
1227 urcu_ref_init(&new_session
->ref
);
1228 pthread_mutex_init(&new_session
->lock
, NULL
);
1230 new_session
->creation_time
= time(NULL
);
1231 if (new_session
->creation_time
== (time_t) -1) {
1232 PERROR("Failed to sample session creation time");
1233 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1237 /* Create default consumer output. */
1238 new_session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
1239 if (new_session
->consumer
== NULL
) {
1240 ret_code
= LTTNG_ERR_NOMEM
;
1245 ret
= lttng_strncpy(new_session
->name
, name
, sizeof(new_session
->name
));
1247 ret_code
= LTTNG_ERR_SESSION_INVALID_CHAR
;
1250 ret
= validate_name(name
);
1252 ret_code
= LTTNG_ERR_SESSION_INVALID_CHAR
;
1257 bool found_name
= false;
1259 struct tm
*timeinfo
;
1261 timeinfo
= localtime(&new_session
->creation_time
);
1263 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1266 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1267 for (i
= 0; i
< INT_MAX
; i
++) {
1268 struct ltt_session
*clashing_session
;
1271 ret
= snprintf(new_session
->name
,
1272 sizeof(new_session
->name
),
1274 DEFAULT_SESSION_NAME
,
1277 ret
= snprintf(new_session
->name
,
1278 sizeof(new_session
->name
),
1280 DEFAULT_SESSION_NAME
, i
,
1283 new_session
->name_contains_creation_time
= true;
1284 if (ret
== -1 || ret
>= sizeof(new_session
->name
)) {
1286 * Null-terminate in case the name is used
1287 * in logging statements.
1289 new_session
->name
[sizeof(new_session
->name
) - 1] = '\0';
1290 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1295 session_find_by_name(new_session
->name
);
1296 session_put(clashing_session
);
1297 if (!clashing_session
) {
1303 DBG("Generated session name \"%s\"", new_session
->name
);
1304 new_session
->has_auto_generated_name
= true;
1306 ERR("Failed to auto-generate a session name");
1307 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1312 ret
= gethostname(new_session
->hostname
, sizeof(new_session
->hostname
));
1314 if (errno
== ENAMETOOLONG
) {
1315 new_session
->hostname
[sizeof(new_session
->hostname
) - 1] = '\0';
1316 ERR("Hostname exceeds the maximal permitted length and has been truncated to %s",
1317 new_session
->hostname
);
1319 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1324 new_session
->uid
= uid
;
1325 new_session
->gid
= gid
;
1327 ret
= snapshot_init(&new_session
->snapshot
);
1329 ret_code
= LTTNG_ERR_NOMEM
;
1333 new_session
->rotation_state
= LTTNG_ROTATION_STATE_NO_ROTATION
;
1335 /* Add new session to the session list. */
1336 new_session
->id
= add_session_list(new_session
);
1339 * Add the new session to the ltt_sessions_ht_by_id.
1340 * No ownership is taken by the hash table; it is merely
1341 * a wrapper around the session list used for faster access
1344 add_session_ht(new_session
);
1345 new_session
->published
= true;
1348 * Consumer is left to NULL since the create_session_uri command will
1349 * set it up and, if valid, assign it to the session.
1351 DBG("Tracing session %s created with ID %" PRIu64
" by uid = %d, gid = %d",
1352 new_session
->name
, new_session
->id
, new_session
->uid
,
1354 ret_code
= LTTNG_OK
;
1357 (void) session_get(new_session
);
1358 *out_session
= new_session
;
1362 session_put(new_session
);
1368 * Check if the UID matches the session. Root user has access to all
1371 bool session_access_ok(struct ltt_session
*session
, uid_t uid
)
1373 LTTNG_ASSERT(session
);
1374 return (uid
== session
->uid
) || uid
== 0;
1378 * Set a session's rotation state and reset all associated state.
1380 * This function resets the rotation state (check timers, pending
1381 * flags, etc.) and sets the result of the last rotation. The result
1382 * can be queries by a liblttng-ctl client.
1384 * Be careful of the result passed to this function. For instance,
1385 * on failure to launch a rotation, a client will expect the rotation
1386 * state to be set to "NO_ROTATION". If an error occurred while the
1387 * rotation was "ONGOING", result should be set to "ERROR", which will
1388 * allow a client to report it.
1390 * Must be called with the session and session_list locks held.
1392 int session_reset_rotation_state(struct ltt_session
*session
,
1393 enum lttng_rotation_state result
)
1397 ASSERT_LOCKED(the_session_list
.lock
);
1398 ASSERT_LOCKED(session
->lock
);
1400 session
->rotation_state
= result
;
1401 if (session
->rotation_pending_check_timer_enabled
) {
1402 ret
= timer_session_rotation_pending_check_stop(session
);
1404 if (session
->chunk_being_archived
) {
1406 enum lttng_trace_chunk_status chunk_status
;
1408 chunk_status
= lttng_trace_chunk_get_id(
1409 session
->chunk_being_archived
,
1411 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1412 LTTNG_OPTIONAL_SET(&session
->last_archived_chunk_id
,
1414 lttng_trace_chunk_put(session
->chunk_being_archived
);
1415 session
->chunk_being_archived
= NULL
;
1417 * Fire the clear reply notifiers if we are completing a clear
1420 session_notify_clear(session
);
1426 * Sample the id of a session looked up via its name.
1427 * Here the term "sampling" hint the caller that this return the id at a given
1428 * point in time with no guarantee that the session for which the id was
1429 * sampled still exist at that point.
1431 * Return 0 when the session is not found,
1432 * Return 1 when the session is found and set `id`.
1434 bool sample_session_id_by_name(const char *name
, uint64_t *id
)
1437 struct lttng_ht_node_str
*node
;
1438 struct lttng_ht_iter iter
;
1439 struct ltt_session
*ls
;
1443 if (!ltt_sessions_ht_by_name
) {
1448 lttng_ht_lookup(ltt_sessions_ht_by_name
, name
, &iter
);
1449 node
= lttng_ht_iter_get_node_str(&iter
);
1455 ls
= caa_container_of(node
, struct ltt_session
, node_by_name
);
1459 DBG3("Session id `%" PRIu64
"` sampled for session `%s", *id
, name
);