2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
4 * SPDX-License-Identifier: GPL-2.0-only
17 #include <sys/types.h>
20 #include <common/common.h>
21 #include <common/utils.h>
22 #include <common/trace-chunk.h>
23 #include <common/sessiond-comm/sessiond-comm.h>
24 #include <lttng/location-internal.h>
25 #include "lttng-sessiond.h"
30 #include "trace-ust.h"
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 caling 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 .head
= CDS_LIST_HEAD_INIT(ltt_session_list
.head
),
60 .lock
= PTHREAD_MUTEX_INITIALIZER
,
61 .removal_cond
= PTHREAD_COND_INITIALIZER
,
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
;
72 * Validate the session name for forbidden characters.
74 * Return 0 on success else -1 meaning a forbidden char. has been found.
76 static int validate_name(const char *name
)
83 tmp_name
= strdup(name
);
90 tok
= strpbrk(tmp_name
, forbidden_name_chars
);
92 DBG("Session name %s contains a forbidden character", name
);
93 /* Forbidden character has been found. */
105 * Add a ltt_session structure to the global list.
107 * The caller MUST acquire the session list lock before.
108 * Returns the unique identifier for the session.
110 static uint64_t add_session_list(struct ltt_session
*ls
)
114 cds_list_add(&ls
->list
, <t_session_list
.head
);
115 return ltt_session_list
.next_uuid
++;
119 * Delete a ltt_session structure to the global list.
121 * The caller MUST acquire the session list lock before.
123 static void del_session_list(struct ltt_session
*ls
)
127 cds_list_del(&ls
->list
);
131 * Return a pointer to the session list.
133 struct ltt_session_list
*session_get_list(void)
135 return <t_session_list
;
139 * Returns once the session list is empty.
141 void session_list_wait_empty(void)
143 pthread_mutex_lock(<t_session_list
.lock
);
144 while (!cds_list_empty(<t_session_list
.head
)) {
145 pthread_cond_wait(<t_session_list
.removal_cond
,
146 <t_session_list
.lock
);
148 pthread_mutex_unlock(<t_session_list
.lock
);
152 * Acquire session list lock
154 void session_lock_list(void)
156 pthread_mutex_lock(<t_session_list
.lock
);
160 * Try to acquire session list lock
162 int session_trylock_list(void)
164 return pthread_mutex_trylock(<t_session_list
.lock
);
168 * Release session list lock
170 void session_unlock_list(void)
172 pthread_mutex_unlock(<t_session_list
.lock
);
176 * Get the session's consumer destination type.
178 * The caller must hold the session lock.
180 enum consumer_dst_type
session_get_consumer_destination_type(
181 const struct ltt_session
*session
)
184 * The output information is duplicated in both of those session types.
185 * Hence, it doesn't matter from which it is retrieved. However, it is
186 * possible for only one of them to be set.
188 return session
->kernel_session
?
189 session
->kernel_session
->consumer
->type
:
190 session
->ust_session
->consumer
->type
;
194 * Get the session's consumer network hostname.
195 * The caller must ensure that the destination is of type "net".
197 * The caller must hold the session lock.
199 const char *session_get_net_consumer_hostname(const struct ltt_session
*session
)
201 const char *hostname
= NULL
;
202 const struct consumer_output
*output
;
204 output
= session
->kernel_session
?
205 session
->kernel_session
->consumer
:
206 session
->ust_session
->consumer
;
209 * hostname is assumed to be the same for both control and data
212 switch (output
->dst
.net
.control
.dtype
) {
214 hostname
= output
->dst
.net
.control
.dst
.ipv4
;
217 hostname
= output
->dst
.net
.control
.dst
.ipv6
;
226 * Get the session's consumer network control and data ports.
227 * The caller must ensure that the destination is of type "net".
229 * The caller must hold the session lock.
231 void session_get_net_consumer_ports(const struct ltt_session
*session
,
232 uint16_t *control_port
, uint16_t *data_port
)
234 const struct consumer_output
*output
;
236 output
= session
->kernel_session
?
237 session
->kernel_session
->consumer
:
238 session
->ust_session
->consumer
;
239 *control_port
= output
->dst
.net
.control
.port
;
240 *data_port
= output
->dst
.net
.data
.port
;
244 * Get the location of the latest trace archive produced by a rotation.
246 * The caller must hold the session lock.
248 struct lttng_trace_archive_location
*session_get_trace_archive_location(
249 const struct ltt_session
*session
)
252 struct lttng_trace_archive_location
*location
= NULL
;
253 char *chunk_path
= NULL
;
255 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
||
256 !session
->last_archived_chunk_name
) {
260 switch (session_get_consumer_destination_type(session
)) {
261 case CONSUMER_DST_LOCAL
:
262 ret
= asprintf(&chunk_path
,
263 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
264 session_get_base_path(session
),
265 session
->last_archived_chunk_name
);
269 location
= lttng_trace_archive_location_local_create(
272 case CONSUMER_DST_NET
:
274 const char *hostname
;
275 uint16_t control_port
, data_port
;
277 hostname
= session_get_net_consumer_hostname(session
);
278 session_get_net_consumer_ports(session
,
281 location
= lttng_trace_archive_location_relay_create(
283 LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
,
284 control_port
, data_port
, session
->last_chunk_path
);
296 * Allocate the ltt_sessions_ht_by_id HT.
298 * The session list lock must be held.
300 static int ltt_sessions_ht_alloc(void)
304 DBG("Allocating ltt_sessions_ht_by_id");
305 ltt_sessions_ht_by_id
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
306 if (!ltt_sessions_ht_by_id
) {
308 ERR("Failed to allocate ltt_sessions_ht_by_id");
316 * Destroy the ltt_sessions_ht_by_id HT.
318 * The session list lock must be held.
320 static void ltt_sessions_ht_destroy(void)
322 if (!ltt_sessions_ht_by_id
) {
325 ht_cleanup_push(ltt_sessions_ht_by_id
);
326 ltt_sessions_ht_by_id
= NULL
;
330 * Add a ltt_session to the ltt_sessions_ht_by_id.
331 * If unallocated, the ltt_sessions_ht_by_id HT is allocated.
332 * The session list lock must be held.
334 static void add_session_ht(struct ltt_session
*ls
)
340 if (!ltt_sessions_ht_by_id
) {
341 ret
= ltt_sessions_ht_alloc();
343 ERR("Error allocating the sessions HT");
347 lttng_ht_node_init_u64(&ls
->node
, ls
->id
);
348 lttng_ht_add_unique_u64(ltt_sessions_ht_by_id
, &ls
->node
);
355 * Test if ltt_sessions_ht_by_id is empty.
356 * Return 1 if empty, 0 if not empty.
357 * The session list lock must be held.
359 static int ltt_sessions_ht_empty(void)
363 if (!ltt_sessions_ht_by_id
) {
368 ret
= lttng_ht_get_count(ltt_sessions_ht_by_id
) ? 0 : 1;
374 * Remove a ltt_session from the ltt_sessions_ht_by_id.
375 * If empty, the ltt_sessions_ht_by_id HT is freed.
376 * The session list lock must be held.
378 static void del_session_ht(struct ltt_session
*ls
)
380 struct lttng_ht_iter iter
;
384 assert(ltt_sessions_ht_by_id
);
386 iter
.iter
.node
= &ls
->node
.node
;
387 ret
= lttng_ht_del(ltt_sessions_ht_by_id
, &iter
);
390 if (ltt_sessions_ht_empty()) {
391 DBG("Empty ltt_sessions_ht_by_id, destroying it");
392 ltt_sessions_ht_destroy();
397 * Acquire session lock
399 void session_lock(struct ltt_session
*session
)
403 pthread_mutex_lock(&session
->lock
);
407 * Release session lock
409 void session_unlock(struct ltt_session
*session
)
413 pthread_mutex_unlock(&session
->lock
);
417 int _session_set_trace_chunk_no_lock_check(struct ltt_session
*session
,
418 struct lttng_trace_chunk
*new_trace_chunk
,
419 struct lttng_trace_chunk
**_current_trace_chunk
)
422 unsigned int i
, refs_to_acquire
= 0, refs_acquired
= 0, refs_to_release
= 0;
423 struct cds_lfht_iter iter
;
424 struct consumer_socket
*socket
;
425 struct lttng_trace_chunk
*current_trace_chunk
;
427 enum lttng_trace_chunk_status chunk_status
;
431 * Ownership of current trace chunk is transferred to
432 * `current_trace_chunk`.
434 current_trace_chunk
= session
->current_trace_chunk
;
435 session
->current_trace_chunk
= NULL
;
436 if (session
->ust_session
) {
437 lttng_trace_chunk_put(
438 session
->ust_session
->current_trace_chunk
);
439 session
->ust_session
->current_trace_chunk
= NULL
;
441 if (session
->kernel_session
) {
442 lttng_trace_chunk_put(
443 session
->kernel_session
->current_trace_chunk
);
444 session
->kernel_session
->current_trace_chunk
= NULL
;
446 if (!new_trace_chunk
) {
450 chunk_status
= lttng_trace_chunk_get_id(new_trace_chunk
, &chunk_id
);
451 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
454 refs_to_acquire
+= !!session
->ust_session
;
455 refs_to_acquire
+= !!session
->kernel_session
;
457 for (refs_acquired
= 0; refs_acquired
< refs_to_acquire
;
459 if (!lttng_trace_chunk_get(new_trace_chunk
)) {
460 ERR("Failed to acquire reference to new trace chunk of session \"%s\"",
466 if (session
->ust_session
) {
467 const uint64_t relayd_id
=
468 session
->ust_session
->consumer
->net_seq_index
;
469 const bool is_local_trace
=
470 session
->ust_session
->consumer
->type
==
473 session
->ust_session
->current_trace_chunk
= new_trace_chunk
;
474 if (is_local_trace
) {
475 enum lttng_error_code ret_error_code
;
477 ret_error_code
= ust_app_create_channel_subdirectories(
478 session
->ust_session
);
479 if (ret_error_code
!= LTTNG_OK
) {
483 cds_lfht_for_each_entry(
484 session
->ust_session
->consumer
->socks
->ht
,
485 &iter
, socket
, node
.node
) {
486 pthread_mutex_lock(socket
->lock
);
487 ret
= consumer_create_trace_chunk(socket
,
489 session
->id
, new_trace_chunk
,
490 DEFAULT_UST_TRACE_DIR
);
491 pthread_mutex_unlock(socket
->lock
);
497 if (session
->kernel_session
) {
498 const uint64_t relayd_id
=
499 session
->kernel_session
->consumer
->net_seq_index
;
500 const bool is_local_trace
=
501 session
->kernel_session
->consumer
->type
==
504 session
->kernel_session
->current_trace_chunk
= new_trace_chunk
;
505 if (is_local_trace
) {
506 enum lttng_error_code ret_error_code
;
508 ret_error_code
= kernel_create_channel_subdirectories(
509 session
->kernel_session
);
510 if (ret_error_code
!= LTTNG_OK
) {
514 cds_lfht_for_each_entry(
515 session
->kernel_session
->consumer
->socks
->ht
,
516 &iter
, socket
, node
.node
) {
517 pthread_mutex_lock(socket
->lock
);
518 ret
= consumer_create_trace_chunk(socket
,
520 session
->id
, new_trace_chunk
,
521 DEFAULT_KERNEL_TRACE_DIR
);
522 pthread_mutex_unlock(socket
->lock
);
530 * Update local current trace chunk state last, only if all remote
531 * creations succeeded.
533 session
->current_trace_chunk
= new_trace_chunk
;
534 LTTNG_OPTIONAL_SET(&session
->most_recent_chunk_id
, chunk_id
);
536 if (_current_trace_chunk
) {
537 *_current_trace_chunk
= current_trace_chunk
;
538 current_trace_chunk
= NULL
;
542 lttng_trace_chunk_put(current_trace_chunk
);
545 if (session
->ust_session
) {
546 session
->ust_session
->current_trace_chunk
= NULL
;
548 if (session
->kernel_session
) {
549 session
->kernel_session
->current_trace_chunk
= NULL
;
552 * Release references taken in the case where all references could not
555 refs_to_release
= refs_to_acquire
- refs_acquired
;
556 for (i
= 0; i
< refs_to_release
; i
++) {
557 lttng_trace_chunk_put(new_trace_chunk
);
563 struct lttng_trace_chunk
*session_create_new_trace_chunk(
564 const struct ltt_session
*session
,
565 const struct consumer_output
*consumer_output_override
,
566 const char *session_base_path_override
,
567 const char *chunk_name_override
)
570 struct lttng_trace_chunk
*trace_chunk
= NULL
;
571 enum lttng_trace_chunk_status chunk_status
;
572 const time_t chunk_creation_ts
= time(NULL
);
574 const char *base_path
;
575 struct lttng_directory_handle
*session_output_directory
= NULL
;
576 const struct lttng_credentials session_credentials
= {
577 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session
->uid
),
578 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session
->gid
),
580 uint64_t next_chunk_id
;
581 const struct consumer_output
*output
;
582 const char *new_path
;
584 if (consumer_output_override
) {
585 output
= consumer_output_override
;
587 assert(session
->ust_session
|| session
->kernel_session
);
588 output
= session
->ust_session
?
589 session
->ust_session
->consumer
:
590 session
->kernel_session
->consumer
;
593 is_local_trace
= output
->type
== CONSUMER_DST_LOCAL
;
594 base_path
= session_base_path_override
? :
595 consumer_output_get_base_path(output
);
597 if (chunk_creation_ts
== (time_t) -1) {
598 PERROR("Failed to sample time while creation session \"%s\" trace chunk",
603 next_chunk_id
= session
->most_recent_chunk_id
.is_set
?
604 session
->most_recent_chunk_id
.value
+ 1 : 0;
606 if (session
->current_trace_chunk
&&
607 !lttng_trace_chunk_get_name_overridden(session
->current_trace_chunk
)) {
608 chunk_status
= lttng_trace_chunk_rename_path(session
->current_trace_chunk
,
609 DEFAULT_CHUNK_TMP_OLD_DIRECTORY
);
610 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
614 if (!session
->current_trace_chunk
) {
615 if (!session
->rotated
) {
621 new_path
= DEFAULT_CHUNK_TMP_NEW_DIRECTORY
;
624 trace_chunk
= lttng_trace_chunk_create(next_chunk_id
,
625 chunk_creation_ts
, new_path
);
630 if (chunk_name_override
) {
631 chunk_status
= lttng_trace_chunk_override_name(trace_chunk
,
632 chunk_name_override
);
633 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
638 if (!is_local_trace
) {
640 * No need to set crendentials and output directory
641 * for remote trace chunks.
646 chunk_status
= lttng_trace_chunk_set_credentials(trace_chunk
,
647 &session_credentials
);
648 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
652 DBG("Creating base output directory of session \"%s\" at %s",
653 session
->name
, base_path
);
654 ret
= utils_mkdir_recursive(base_path
, S_IRWXU
| S_IRWXG
,
655 session
->uid
, session
->gid
);
659 session_output_directory
= lttng_directory_handle_create(base_path
);
660 if (!session_output_directory
) {
663 chunk_status
= lttng_trace_chunk_set_as_owner(trace_chunk
,
664 session_output_directory
);
665 lttng_directory_handle_put(session_output_directory
);
666 session_output_directory
= NULL
;
667 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
673 lttng_directory_handle_put(session_output_directory
);
674 lttng_trace_chunk_put(trace_chunk
);
679 int session_close_trace_chunk(struct ltt_session
*session
,
680 struct lttng_trace_chunk
*trace_chunk
,
681 enum lttng_trace_chunk_command_type close_command
,
682 char *closed_trace_chunk_path
)
685 bool error_occurred
= false;
686 struct cds_lfht_iter iter
;
687 struct consumer_socket
*socket
;
688 enum lttng_trace_chunk_status chunk_status
;
689 const time_t chunk_close_timestamp
= time(NULL
);
690 const char *new_path
;
692 chunk_status
= lttng_trace_chunk_set_close_command(
693 trace_chunk
, close_command
);
694 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
699 if (chunk_close_timestamp
== (time_t) -1) {
700 ERR("Failed to sample the close timestamp of the current trace chunk of session \"%s\"",
706 if (close_command
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE
&& !session
->rotated
) {
707 /* New chunk stays in session output directory. */
710 /* Use chunk name for new chunk. */
713 if (session
->current_trace_chunk
&&
714 !lttng_trace_chunk_get_name_overridden(session
->current_trace_chunk
)) {
715 /* Rename new chunk path. */
716 chunk_status
= lttng_trace_chunk_rename_path(session
->current_trace_chunk
,
718 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
723 if (!lttng_trace_chunk_get_name_overridden(trace_chunk
) &&
724 close_command
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
) {
725 const char *old_path
;
727 if (!session
->rotated
) {
732 /* We need to move back the .tmp_old_chunk to its rightful place. */
733 chunk_status
= lttng_trace_chunk_rename_path(trace_chunk
,
735 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
740 if (close_command
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
) {
741 session
->rotated
= true;
743 chunk_status
= lttng_trace_chunk_set_close_timestamp(trace_chunk
,
744 chunk_close_timestamp
);
745 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
746 ERR("Failed to set the close timestamp of the current trace chunk of session \"%s\"",
752 if (session
->ust_session
) {
753 const uint64_t relayd_id
=
754 session
->ust_session
->consumer
->net_seq_index
;
756 cds_lfht_for_each_entry(
757 session
->ust_session
->consumer
->socks
->ht
,
758 &iter
, socket
, node
.node
) {
759 pthread_mutex_lock(socket
->lock
);
760 ret
= consumer_close_trace_chunk(socket
,
763 trace_chunk
, closed_trace_chunk_path
);
764 pthread_mutex_unlock(socket
->lock
);
766 ERR("Failed to close trace chunk on user space consumer");
767 error_occurred
= true;
771 if (session
->kernel_session
) {
772 const uint64_t relayd_id
=
773 session
->kernel_session
->consumer
->net_seq_index
;
775 cds_lfht_for_each_entry(
776 session
->kernel_session
->consumer
->socks
->ht
,
777 &iter
, socket
, node
.node
) {
778 pthread_mutex_lock(socket
->lock
);
779 ret
= consumer_close_trace_chunk(socket
,
782 trace_chunk
, closed_trace_chunk_path
);
783 pthread_mutex_unlock(socket
->lock
);
785 ERR("Failed to close trace chunk on kernel consumer");
786 error_occurred
= true;
790 ret
= error_occurred
? -1 : 0;
796 * This function skips the metadata channel as the begin/end timestamps of a
797 * metadata packet are useless.
799 * Moreover, opening a packet after a "clear" will cause problems for live
800 * sessions as it will introduce padding that was not part of the first trace
801 * chunk. The relay daemon expects the content of the metadata stream of
802 * successive metadata trace chunks to be strict supersets of one another.
804 * For example, flushing a packet at the beginning of the metadata stream of
805 * a trace chunk resulting from a "clear" session command will cause the
806 * size of the metadata stream of the new trace chunk to not match the size of
807 * the metadata stream of the original chunk. This will confuse the relay
808 * daemon as the same "offset" in a metadata stream will no longer point
809 * to the same content.
812 enum lttng_error_code
session_kernel_open_packets(struct ltt_session
*session
)
814 enum lttng_error_code ret
= LTTNG_OK
;
815 struct consumer_socket
*socket
;
816 struct lttng_ht_iter iter
;
817 struct cds_lfht_node
*node
;
818 struct ltt_kernel_channel
*chan
;
822 cds_lfht_first(session
->kernel_session
->consumer
->socks
->ht
, &iter
.iter
);
823 node
= cds_lfht_iter_get_node(&iter
.iter
);
824 socket
= container_of(node
, typeof(*socket
), node
.node
);
826 cds_list_for_each_entry(chan
,
827 &session
->kernel_session
->channel_list
.head
, list
) {
830 DBG("Open packet of kernel channel: channel key = %" PRIu64
831 ", session name = %s, session_id = %" PRIu64
,
832 chan
->key
, session
->name
, session
->id
);
834 open_ret
= consumer_open_channel_packets(socket
, chan
->key
);
836 /* General error (no known error expected). */
847 enum lttng_error_code
session_open_packets(struct ltt_session
*session
)
849 enum lttng_error_code ret
= LTTNG_OK
;
851 DBG("Opening packets of session channels: session name = %s, session id = %" PRIu64
,
852 session
->name
, session
->id
);
854 if (session
->ust_session
) {
855 ret
= ust_app_open_packets(session
);
856 if (ret
!= LTTNG_OK
) {
861 if (session
->kernel_session
) {
862 ret
= session_kernel_open_packets(session
);
863 if (ret
!= LTTNG_OK
) {
873 * Set a session's current trace chunk.
875 * Must be called with the session lock held.
877 int session_set_trace_chunk(struct ltt_session
*session
,
878 struct lttng_trace_chunk
*new_trace_chunk
,
879 struct lttng_trace_chunk
**current_trace_chunk
)
881 ASSERT_LOCKED(session
->lock
);
882 return _session_set_trace_chunk_no_lock_check(session
, new_trace_chunk
,
883 current_trace_chunk
);
887 void session_notify_destruction(const struct ltt_session
*session
)
890 const size_t count
= lttng_dynamic_array_get_count(
891 &session
->destroy_notifiers
);
893 for (i
= 0; i
< count
; i
++) {
894 const struct ltt_session_destroy_notifier_element
*element
=
895 lttng_dynamic_array_get_element(
896 &session
->destroy_notifiers
, i
);
898 element
->notifier(session
, element
->user_data
);
903 * Fire each clear notifier once, and remove them from the array.
905 void session_notify_clear(struct ltt_session
*session
)
908 const size_t count
= lttng_dynamic_array_get_count(
909 &session
->clear_notifiers
);
911 for (i
= 0; i
< count
; i
++) {
912 const struct ltt_session_clear_notifier_element
*element
=
913 lttng_dynamic_array_get_element(
914 &session
->clear_notifiers
, i
);
916 element
->notifier(session
, element
->user_data
);
918 lttng_dynamic_array_clear(&session
->clear_notifiers
);
922 void session_release(struct urcu_ref
*ref
)
925 struct ltt_ust_session
*usess
;
926 struct ltt_kernel_session
*ksess
;
927 struct ltt_session
*session
= container_of(ref
, typeof(*session
), ref
);
928 const bool session_published
= session
->published
;
930 assert(!session
->chunk_being_archived
);
932 usess
= session
->ust_session
;
933 ksess
= session
->kernel_session
;
935 /* Clean kernel session teardown, keeping data for destroy notifier. */
936 kernel_destroy_session(ksess
);
938 /* UST session teardown, keeping data for destroy notifier. */
940 /* Close any relayd session */
941 consumer_output_send_destroy_relayd(usess
->consumer
);
943 /* Destroy every UST application related to this session. */
944 ret
= ust_app_destroy_trace_all(usess
);
946 ERR("Error in ust_app_destroy_trace_all");
949 /* Clean up the rest, keeping destroy notifier data. */
950 trace_ust_destroy_session(usess
);
954 * Must notify the kernel thread here to update it's poll set in order to
955 * remove the channel(s)' fd just destroyed.
957 ret
= notify_thread_pipe(kernel_poll_pipe
[1]);
959 PERROR("write kernel poll pipe");
962 DBG("Destroying session %s (id %" PRIu64
")", session
->name
, session
->id
);
964 snapshot_destroy(&session
->snapshot
);
966 pthread_mutex_destroy(&session
->lock
);
968 if (session_published
) {
969 ASSERT_LOCKED(ltt_session_list
.lock
);
970 del_session_list(session
);
971 del_session_ht(session
);
973 session_notify_destruction(session
);
975 consumer_output_put(session
->consumer
);
976 kernel_free_session(ksess
);
977 session
->kernel_session
= NULL
;
979 trace_ust_free_session(usess
);
980 session
->ust_session
= NULL
;
982 lttng_dynamic_array_reset(&session
->destroy_notifiers
);
983 lttng_dynamic_array_reset(&session
->clear_notifiers
);
984 free(session
->last_archived_chunk_name
);
985 free(session
->base_path
);
987 if (session_published
) {
989 * Broadcast after free-ing to ensure the memory is
990 * reclaimed before the main thread exits.
992 ASSERT_LOCKED(ltt_session_list
.lock
);
993 pthread_cond_broadcast(<t_session_list
.removal_cond
);
998 * Acquire a reference to a session.
999 * This function may fail (return false); its return value must be checked.
1001 bool session_get(struct ltt_session
*session
)
1003 return urcu_ref_get_unless_zero(&session
->ref
);
1007 * Release a reference to a session.
1009 void session_put(struct ltt_session
*session
)
1015 * The session list lock must be held as any session_put()
1016 * may cause the removal of the session from the session_list.
1018 ASSERT_LOCKED(ltt_session_list
.lock
);
1019 assert(session
->ref
.refcount
);
1020 urcu_ref_put(&session
->ref
, session_release
);
1024 * Destroy a session.
1026 * This method does not immediately release/free the session as other
1027 * components may still hold a reference to the session. However,
1028 * the session should no longer be presented to the user.
1030 * Releases the session list's reference to the session
1031 * and marks it as destroyed. Iterations on the session list should be
1032 * mindful of the "destroyed" flag.
1034 void session_destroy(struct ltt_session
*session
)
1036 assert(!session
->destroyed
);
1037 session
->destroyed
= true;
1038 session_put(session
);
1041 int session_add_destroy_notifier(struct ltt_session
*session
,
1042 ltt_session_destroy_notifier notifier
, void *user_data
)
1044 const struct ltt_session_destroy_notifier_element element
= {
1045 .notifier
= notifier
,
1046 .user_data
= user_data
1049 return lttng_dynamic_array_add_element(&session
->destroy_notifiers
,
1053 int session_add_clear_notifier(struct ltt_session
*session
,
1054 ltt_session_clear_notifier notifier
, void *user_data
)
1056 const struct ltt_session_clear_notifier_element element
= {
1057 .notifier
= notifier
,
1058 .user_data
= user_data
1061 return lttng_dynamic_array_add_element(&session
->clear_notifiers
,
1066 * Return a ltt_session structure ptr that matches name. If no session found,
1067 * NULL is returned. This must be called with the session list lock held using
1068 * session_lock_list and session_unlock_list.
1069 * A reference to the session is implicitly acquired by this function.
1071 struct ltt_session
*session_find_by_name(const char *name
)
1073 struct ltt_session
*iter
;
1076 ASSERT_LOCKED(ltt_session_list
.lock
);
1078 DBG2("Trying to find session by name %s", name
);
1080 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
1081 if (!strncmp(iter
->name
, name
, NAME_MAX
) &&
1089 return session_get(iter
) ? iter
: NULL
;
1093 * Return an ltt_session that matches the id. If no session is found,
1094 * NULL is returned. This must be called with rcu_read_lock and
1095 * session list lock held (to guarantee the lifetime of the session).
1097 struct ltt_session
*session_find_by_id(uint64_t id
)
1099 struct lttng_ht_node_u64
*node
;
1100 struct lttng_ht_iter iter
;
1101 struct ltt_session
*ls
;
1103 ASSERT_LOCKED(ltt_session_list
.lock
);
1105 if (!ltt_sessions_ht_by_id
) {
1109 lttng_ht_lookup(ltt_sessions_ht_by_id
, &id
, &iter
);
1110 node
= lttng_ht_iter_get_node_u64(&iter
);
1114 ls
= caa_container_of(node
, struct ltt_session
, node
);
1116 DBG3("Session %" PRIu64
" found by id.", id
);
1117 return session_get(ls
) ? ls
: NULL
;
1120 DBG3("Session %" PRIu64
" NOT found by id", id
);
1125 * Create a new session and add it to the session list.
1126 * Session list lock must be held by the caller.
1128 enum lttng_error_code
session_create(const char *name
, uid_t uid
, gid_t gid
,
1129 struct ltt_session
**out_session
)
1132 enum lttng_error_code ret_code
;
1133 struct ltt_session
*new_session
= NULL
;
1135 ASSERT_LOCKED(ltt_session_list
.lock
);
1137 struct ltt_session
*clashing_session
;
1139 clashing_session
= session_find_by_name(name
);
1140 if (clashing_session
) {
1141 session_put(clashing_session
);
1142 ret_code
= LTTNG_ERR_EXIST_SESS
;
1146 new_session
= zmalloc(sizeof(struct ltt_session
));
1148 PERROR("Failed to allocate an ltt_session structure");
1149 ret_code
= LTTNG_ERR_NOMEM
;
1153 lttng_dynamic_array_init(&new_session
->destroy_notifiers
,
1154 sizeof(struct ltt_session_destroy_notifier_element
),
1156 lttng_dynamic_array_init(&new_session
->clear_notifiers
,
1157 sizeof(struct ltt_session_clear_notifier_element
),
1159 urcu_ref_init(&new_session
->ref
);
1160 pthread_mutex_init(&new_session
->lock
, NULL
);
1162 new_session
->creation_time
= time(NULL
);
1163 if (new_session
->creation_time
== (time_t) -1) {
1164 PERROR("Failed to sample session creation time");
1165 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1169 /* Create default consumer output. */
1170 new_session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
1171 if (new_session
->consumer
== NULL
) {
1172 ret_code
= LTTNG_ERR_NOMEM
;
1177 ret
= lttng_strncpy(new_session
->name
, name
, sizeof(new_session
->name
));
1179 ret_code
= LTTNG_ERR_SESSION_INVALID_CHAR
;
1182 ret
= validate_name(name
);
1184 ret_code
= LTTNG_ERR_SESSION_INVALID_CHAR
;
1189 bool found_name
= false;
1191 struct tm
*timeinfo
;
1193 timeinfo
= localtime(&new_session
->creation_time
);
1195 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1198 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1199 for (i
= 0; i
< INT_MAX
; i
++) {
1200 struct ltt_session
*clashing_session
;
1203 ret
= snprintf(new_session
->name
,
1204 sizeof(new_session
->name
),
1206 DEFAULT_SESSION_NAME
,
1209 ret
= snprintf(new_session
->name
,
1210 sizeof(new_session
->name
),
1212 DEFAULT_SESSION_NAME
, i
,
1215 new_session
->name_contains_creation_time
= true;
1216 if (ret
== -1 || ret
>= sizeof(new_session
->name
)) {
1218 * Null-terminate in case the name is used
1219 * in logging statements.
1221 new_session
->name
[sizeof(new_session
->name
) - 1] = '\0';
1222 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1227 session_find_by_name(new_session
->name
);
1228 session_put(clashing_session
);
1229 if (!clashing_session
) {
1235 DBG("Generated session name \"%s\"", new_session
->name
);
1236 new_session
->has_auto_generated_name
= true;
1238 ERR("Failed to auto-generate a session name");
1239 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1244 ret
= gethostname(new_session
->hostname
, sizeof(new_session
->hostname
));
1246 if (errno
== ENAMETOOLONG
) {
1247 new_session
->hostname
[sizeof(new_session
->hostname
) - 1] = '\0';
1248 ERR("Hostname exceeds the maximal permitted length and has been truncated to %s",
1249 new_session
->hostname
);
1251 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1256 new_session
->uid
= uid
;
1257 new_session
->gid
= gid
;
1259 ret
= snapshot_init(&new_session
->snapshot
);
1261 ret_code
= LTTNG_ERR_NOMEM
;
1265 new_session
->rotation_state
= LTTNG_ROTATION_STATE_NO_ROTATION
;
1267 /* Add new session to the session list. */
1268 new_session
->id
= add_session_list(new_session
);
1271 * Add the new session to the ltt_sessions_ht_by_id.
1272 * No ownership is taken by the hash table; it is merely
1273 * a wrapper around the session list used for faster access
1276 add_session_ht(new_session
);
1277 new_session
->published
= true;
1280 * Consumer is left to NULL since the create_session_uri command will
1281 * set it up and, if valid, assign it to the session.
1283 DBG("Tracing session %s created with ID %" PRIu64
" by uid = %d, gid = %d",
1284 new_session
->name
, new_session
->id
, new_session
->uid
,
1286 ret_code
= LTTNG_OK
;
1289 (void) session_get(new_session
);
1290 *out_session
= new_session
;
1294 session_put(new_session
);
1300 * Check if the UID matches the session. Root user has access to all
1303 bool session_access_ok(struct ltt_session
*session
, uid_t uid
)
1306 return (uid
== session
->uid
) || uid
== 0;
1310 * Set a session's rotation state and reset all associated state.
1312 * This function resets the rotation state (check timers, pending
1313 * flags, etc.) and sets the result of the last rotation. The result
1314 * can be queries by a liblttng-ctl client.
1316 * Be careful of the result passed to this function. For instance,
1317 * on failure to launch a rotation, a client will expect the rotation
1318 * state to be set to "NO_ROTATION". If an error occurred while the
1319 * rotation was "ONGOING", result should be set to "ERROR", which will
1320 * allow a client to report it.
1322 * Must be called with the session and session_list locks held.
1324 int session_reset_rotation_state(struct ltt_session
*session
,
1325 enum lttng_rotation_state result
)
1329 ASSERT_LOCKED(ltt_session_list
.lock
);
1330 ASSERT_LOCKED(session
->lock
);
1332 session
->rotation_state
= result
;
1333 if (session
->rotation_pending_check_timer_enabled
) {
1334 ret
= timer_session_rotation_pending_check_stop(session
);
1336 if (session
->chunk_being_archived
) {
1338 enum lttng_trace_chunk_status chunk_status
;
1340 chunk_status
= lttng_trace_chunk_get_id(
1341 session
->chunk_being_archived
,
1343 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1344 LTTNG_OPTIONAL_SET(&session
->last_archived_chunk_id
,
1346 lttng_trace_chunk_put(session
->chunk_being_archived
);
1347 session
->chunk_being_archived
= NULL
;
1349 * Fire the clear reply notifiers if we are completing a clear
1352 session_notify_clear(session
);