2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/types.h>
30 #include <common/common.h>
31 #include <common/utils.h>
32 #include <common/trace-chunk.h>
33 #include <common/sessiond-comm/sessiond-comm.h>
34 #include <lttng/location-internal.h>
35 #include "lttng-sessiond.h"
40 #include "trace-ust.h"
43 struct ltt_session_destroy_notifier_element
{
44 ltt_session_destroy_notifier notifier
;
51 * No ltt_session.lock is taken here because those data structure are widely
52 * spread across the lttng-tools code base so before caling functions below
53 * that can read/write a session, the caller MUST acquire the session lock
54 * using session_lock() and session_unlock().
58 * Init tracing session list.
60 * Please see session.h for more explanation and correct usage of the list.
62 static struct ltt_session_list ltt_session_list
= {
63 .head
= CDS_LIST_HEAD_INIT(ltt_session_list
.head
),
64 .lock
= PTHREAD_MUTEX_INITIALIZER
,
65 .removal_cond
= PTHREAD_COND_INITIALIZER
,
69 /* These characters are forbidden in a session name. Used by validate_name. */
70 static const char *forbidden_name_chars
= "/";
72 /* Global hash table to keep the sessions, indexed by id. */
73 static struct lttng_ht
*ltt_sessions_ht_by_id
= NULL
;
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
, <t_session_list
.head
);
119 return ltt_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 <t_session_list
;
143 * Returns once the session list is empty.
145 void session_list_wait_empty(void)
147 pthread_mutex_lock(<t_session_list
.lock
);
148 while (!cds_list_empty(<t_session_list
.head
)) {
149 pthread_cond_wait(<t_session_list
.removal_cond
,
150 <t_session_list
.lock
);
152 pthread_mutex_unlock(<t_session_list
.lock
);
156 * Acquire session list lock
158 void session_lock_list(void)
160 pthread_mutex_lock(<t_session_list
.lock
);
164 * Try to acquire session list lock
166 int session_trylock_list(void)
168 return pthread_mutex_trylock(<t_session_list
.lock
);
172 * Release session list lock
174 void session_unlock_list(void)
176 pthread_mutex_unlock(<t_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 ret
= asprintf(&chunk_path
, "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
265 session_get_base_path(session
),
266 session
->last_archived_chunk_name
);
271 switch (session_get_consumer_destination_type(session
)) {
272 case CONSUMER_DST_LOCAL
:
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
, chunk_path
);
300 * Allocate the ltt_sessions_ht_by_id HT.
302 * The session list lock must be held.
304 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");
320 * Destroy the ltt_sessions_ht_by_id HT.
322 * The session list lock must be held.
324 static void ltt_sessions_ht_destroy(void)
326 if (!ltt_sessions_ht_by_id
) {
329 ht_cleanup_push(ltt_sessions_ht_by_id
);
330 ltt_sessions_ht_by_id
= NULL
;
334 * Add a ltt_session to the ltt_sessions_ht_by_id.
335 * If unallocated, the ltt_sessions_ht_by_id HT is allocated.
336 * The session list lock must be held.
338 static void add_session_ht(struct ltt_session
*ls
)
344 if (!ltt_sessions_ht_by_id
) {
345 ret
= ltt_sessions_ht_alloc();
347 ERR("Error allocating the sessions HT");
351 lttng_ht_node_init_u64(&ls
->node
, ls
->id
);
352 lttng_ht_add_unique_u64(ltt_sessions_ht_by_id
, &ls
->node
);
359 * Test if ltt_sessions_ht_by_id is empty.
360 * Return 1 if empty, 0 if not empty.
361 * The session list lock must be held.
363 static int ltt_sessions_ht_empty(void)
367 if (!ltt_sessions_ht_by_id
) {
372 ret
= lttng_ht_get_count(ltt_sessions_ht_by_id
) ? 0 : 1;
378 * Remove a ltt_session from the ltt_sessions_ht_by_id.
379 * If empty, the ltt_sessions_ht_by_id HT is freed.
380 * The session list lock must be held.
382 static void del_session_ht(struct ltt_session
*ls
)
384 struct lttng_ht_iter iter
;
388 assert(ltt_sessions_ht_by_id
);
390 iter
.iter
.node
= &ls
->node
.node
;
391 ret
= lttng_ht_del(ltt_sessions_ht_by_id
, &iter
);
394 if (ltt_sessions_ht_empty()) {
395 DBG("Empty ltt_sessions_ht_by_id, destroying it");
396 ltt_sessions_ht_destroy();
401 * Acquire session lock
403 void session_lock(struct ltt_session
*session
)
407 pthread_mutex_lock(&session
->lock
);
411 * Release session lock
413 void session_unlock(struct ltt_session
*session
)
417 pthread_mutex_unlock(&session
->lock
);
421 int _session_set_trace_chunk_no_lock_check(struct ltt_session
*session
,
422 struct lttng_trace_chunk
*new_trace_chunk
,
423 struct lttng_trace_chunk
**_current_trace_chunk
)
426 unsigned int i
, refs_to_acquire
= 0, refs_acquired
= 0, refs_to_release
= 0;
427 struct cds_lfht_iter iter
;
428 struct consumer_socket
*socket
;
429 struct lttng_trace_chunk
*current_trace_chunk
;
431 enum lttng_trace_chunk_status chunk_status
;
432 const uint64_t relayd_id
= session
->consumer
->net_seq_index
;
433 const bool is_local_trace
= relayd_id
== -1ULL;
437 * Ownership of current trace chunk is transferred to
438 * `current_trace_chunk`.
440 current_trace_chunk
= session
->current_trace_chunk
;
441 session
->current_trace_chunk
= NULL
;
442 if (session
->ust_session
) {
443 lttng_trace_chunk_put(
444 session
->ust_session
->current_trace_chunk
);
445 session
->ust_session
->current_trace_chunk
= NULL
;
447 if (session
->kernel_session
) {
448 lttng_trace_chunk_put(
449 session
->kernel_session
->current_trace_chunk
);
450 session
->kernel_session
->current_trace_chunk
= NULL
;
452 if (!new_trace_chunk
) {
456 chunk_status
= lttng_trace_chunk_get_id(new_trace_chunk
, &chunk_id
);
457 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
460 refs_to_acquire
+= !!session
->ust_session
;
461 refs_to_acquire
+= !!session
->kernel_session
;
463 for (refs_acquired
= 0; refs_acquired
< refs_to_acquire
;
465 if (!lttng_trace_chunk_get(new_trace_chunk
)) {
466 ERR("Failed to acquire reference to new trace chunk of session \"%s\"",
472 if (session
->ust_session
) {
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
) {
480 ret
= -ret_error_code
;
484 cds_lfht_for_each_entry(
485 session
->ust_session
->consumer
->socks
->ht
,
486 &iter
, socket
, node
.node
) {
487 pthread_mutex_lock(socket
->lock
);
488 ret
= consumer_create_trace_chunk(socket
,
490 session
->id
, new_trace_chunk
);
491 pthread_mutex_unlock(socket
->lock
);
497 if (session
->kernel_session
) {
498 session
->kernel_session
->current_trace_chunk
= new_trace_chunk
;
499 if (is_local_trace
) {
500 enum lttng_error_code ret_error_code
;
502 ret_error_code
= kernel_create_channel_subdirectories(
503 session
->kernel_session
);
504 if (ret_error_code
!= LTTNG_OK
) {
505 ret
= -ret_error_code
;
509 cds_lfht_for_each_entry(
510 session
->kernel_session
->consumer
->socks
->ht
,
511 &iter
, socket
, node
.node
) {
512 pthread_mutex_lock(socket
->lock
);
513 ret
= consumer_create_trace_chunk(socket
,
515 session
->id
, new_trace_chunk
);
516 pthread_mutex_unlock(socket
->lock
);
524 * Update local current trace chunk state last, only if all remote
525 * creations succeeded.
527 session
->current_trace_chunk
= new_trace_chunk
;
528 LTTNG_OPTIONAL_SET(&session
->most_recent_chunk_id
, chunk_id
);
530 if (_current_trace_chunk
) {
531 *_current_trace_chunk
= current_trace_chunk
;
532 current_trace_chunk
= NULL
;
536 lttng_trace_chunk_put(current_trace_chunk
);
539 if (session
->ust_session
) {
540 session
->ust_session
->current_trace_chunk
= NULL
;
542 if (session
->kernel_session
) {
543 session
->kernel_session
->current_trace_chunk
= NULL
;
546 * Release references taken in the case where all references could not
549 refs_to_release
= refs_to_acquire
- refs_acquired
;
550 for (i
= 0; i
< refs_to_release
; i
++) {
551 lttng_trace_chunk_put(new_trace_chunk
);
558 bool output_supports_trace_chunks(const struct ltt_session
*session
)
560 if (session
->consumer
->type
== CONSUMER_DST_LOCAL
) {
563 struct consumer_output
*output
;
565 if (session
->ust_session
) {
566 output
= session
->ust_session
->consumer
;
567 } else if (session
->kernel_session
) {
568 output
= session
->kernel_session
->consumer
;
573 if (output
->relay_major_version
> 2) {
575 } else if (output
->relay_major_version
== 2 &&
576 output
->relay_minor_version
>= 11) {
583 struct lttng_trace_chunk
*session_create_new_trace_chunk(
584 struct ltt_session
*session
,
585 const char *session_base_path_override
,
586 const char *chunk_name_override
)
589 struct lttng_trace_chunk
*trace_chunk
= NULL
;
590 enum lttng_trace_chunk_status chunk_status
;
591 const time_t chunk_creation_ts
= time(NULL
);
592 const bool is_local_trace
=
593 session
->consumer
->type
== CONSUMER_DST_LOCAL
;
594 const char *base_path
= session_base_path_override
? :
595 session_get_base_path(session
);
596 struct lttng_directory_handle session_output_directory
;
597 const struct lttng_credentials session_credentials
= {
601 uint64_t next_chunk_id
;
603 if (chunk_creation_ts
== (time_t) -1) {
604 PERROR("Failed to sample time while creation session \"%s\" trace chunk",
609 if (!output_supports_trace_chunks(session
)) {
612 next_chunk_id
= session
->most_recent_chunk_id
.is_set
?
613 session
->most_recent_chunk_id
.value
+ 1 : 0;
615 trace_chunk
= lttng_trace_chunk_create(next_chunk_id
,
621 if (chunk_name_override
) {
622 chunk_status
= lttng_trace_chunk_override_name(trace_chunk
,
623 chunk_name_override
);
624 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
629 if (!is_local_trace
) {
631 * No need to set crendentials and output directory
632 * for remote trace chunks.
637 chunk_status
= lttng_trace_chunk_set_credentials(trace_chunk
,
638 &session_credentials
);
639 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
643 DBG("Creating base output directory of session \"%s\" at %s",
644 session
->name
, base_path
);
645 ret
= utils_mkdir_recursive(base_path
, S_IRWXU
| S_IRWXG
,
646 session
->uid
, session
->gid
);
650 ret
= lttng_directory_handle_init(&session_output_directory
,
655 chunk_status
= lttng_trace_chunk_set_as_owner(trace_chunk
,
656 &session_output_directory
);
657 lttng_directory_handle_fini(&session_output_directory
);
658 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
664 lttng_trace_chunk_put(trace_chunk
);
669 int session_close_trace_chunk(const struct ltt_session
*session
,
670 struct lttng_trace_chunk
*trace_chunk
)
673 bool error_occurred
= false;
674 struct cds_lfht_iter iter
;
675 struct consumer_socket
*socket
;
676 enum lttng_trace_chunk_status chunk_status
;
677 const time_t chunk_close_timestamp
= time(NULL
);
679 if (chunk_close_timestamp
== (time_t) -1) {
680 ERR("Failed to sample the close timestamp of the current trace chunk of session \"%s\"",
685 chunk_status
= lttng_trace_chunk_set_close_timestamp(trace_chunk
,
686 chunk_close_timestamp
);
687 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
688 ERR("Failed to set the close timestamp of the current trace chunk of session \"%s\"",
694 if (session
->ust_session
) {
695 cds_lfht_for_each_entry(
696 session
->ust_session
->consumer
->socks
->ht
,
697 &iter
, socket
, node
.node
) {
698 pthread_mutex_lock(socket
->lock
);
699 ret
= consumer_close_trace_chunk(socket
,
700 session
->consumer
->net_seq_index
,
703 pthread_mutex_unlock(socket
->lock
);
705 ERR("Failed to close trace chunk on user space consumer");
706 error_occurred
= true;
710 if (session
->kernel_session
) {
711 cds_lfht_for_each_entry(
712 session
->kernel_session
->consumer
->socks
->ht
,
713 &iter
, socket
, node
.node
) {
714 pthread_mutex_lock(socket
->lock
);
715 ret
= consumer_close_trace_chunk(socket
,
716 session
->consumer
->net_seq_index
,
719 pthread_mutex_unlock(socket
->lock
);
721 ERR("Failed to close trace chunk on kernel consumer");
722 error_occurred
= true;
726 ret
= error_occurred
? -1 : 0;
732 * Set a session's current trace chunk.
734 * Must be called with the session lock held.
736 int session_set_trace_chunk(struct ltt_session
*session
,
737 struct lttng_trace_chunk
*new_trace_chunk
,
738 struct lttng_trace_chunk
**current_trace_chunk
)
740 ASSERT_LOCKED(session
->lock
);
741 return _session_set_trace_chunk_no_lock_check(session
, new_trace_chunk
,
742 current_trace_chunk
);
746 void session_notify_destruction(const struct ltt_session
*session
)
749 const size_t count
= lttng_dynamic_array_get_count(
750 &session
->destroy_notifiers
);
752 for (i
= 0; i
< count
; i
++) {
753 const struct ltt_session_destroy_notifier_element
*element
=
754 lttng_dynamic_array_get_element(
755 &session
->destroy_notifiers
, i
);
757 element
->notifier(session
, element
->user_data
);
762 void session_release(struct urcu_ref
*ref
)
765 struct ltt_ust_session
*usess
;
766 struct ltt_kernel_session
*ksess
;
767 struct ltt_session
*session
= container_of(ref
, typeof(*session
), ref
);
769 assert(!session
->chunk_being_archived
);
771 usess
= session
->ust_session
;
772 ksess
= session
->kernel_session
;
774 session_notify_destruction(session
);
775 lttng_dynamic_array_reset(&session
->destroy_notifiers
);
776 if (session
->current_trace_chunk
) {
777 ret
= session_close_trace_chunk(session
, session
->current_trace_chunk
);
779 ERR("Failed to close the current trace chunk of session \"%s\" during its release",
782 ret
= _session_set_trace_chunk_no_lock_check(session
, NULL
, NULL
);
784 ERR("Failed to release the current trace chunk of session \"%s\" during its release",
789 /* Clean kernel session teardown */
790 kernel_destroy_session(ksess
);
791 session
->kernel_session
= NULL
;
793 /* UST session teardown */
795 /* Close any relayd session */
796 consumer_output_send_destroy_relayd(usess
->consumer
);
798 /* Destroy every UST application related to this session. */
799 ret
= ust_app_destroy_trace_all(usess
);
801 ERR("Error in ust_app_destroy_trace_all");
804 /* Clean up the rest. */
805 trace_ust_destroy_session(usess
);
806 session
->ust_session
= NULL
;
810 * Must notify the kernel thread here to update it's poll set in order to
811 * remove the channel(s)' fd just destroyed.
813 ret
= notify_thread_pipe(kernel_poll_pipe
[1]);
815 PERROR("write kernel poll pipe");
818 DBG("Destroying session %s (id %" PRIu64
")", session
->name
, session
->id
);
820 consumer_output_put(session
->consumer
);
821 snapshot_destroy(&session
->snapshot
);
823 pthread_mutex_destroy(&session
->lock
);
825 if (session
->published
) {
826 ASSERT_LOCKED(ltt_session_list
.lock
);
827 del_session_list(session
);
828 del_session_ht(session
);
829 pthread_cond_broadcast(<t_session_list
.removal_cond
);
831 free(session
->last_archived_chunk_name
);
836 * Acquire a reference to a session.
837 * This function may fail (return false); its return value must be checked.
839 bool session_get(struct ltt_session
*session
)
841 return urcu_ref_get_unless_zero(&session
->ref
);
845 * Release a reference to a session.
847 void session_put(struct ltt_session
*session
)
853 * The session list lock must be held as any session_put()
854 * may cause the removal of the session from the session_list.
856 ASSERT_LOCKED(ltt_session_list
.lock
);
857 assert(session
->ref
.refcount
);
858 urcu_ref_put(&session
->ref
, session_release
);
864 * This method does not immediately release/free the session as other
865 * components may still hold a reference to the session. However,
866 * the session should no longer be presented to the user.
868 * Releases the session list's reference to the session
869 * and marks it as destroyed. Iterations on the session list should be
870 * mindful of the "destroyed" flag.
872 void session_destroy(struct ltt_session
*session
)
874 assert(!session
->destroyed
);
875 session
->destroyed
= true;
876 session_put(session
);
879 int session_add_destroy_notifier(struct ltt_session
*session
,
880 ltt_session_destroy_notifier notifier
, void *user_data
)
882 const struct ltt_session_destroy_notifier_element element
= {
883 .notifier
= notifier
,
884 .user_data
= user_data
887 return lttng_dynamic_array_add_element(&session
->destroy_notifiers
,
892 * Return a ltt_session structure ptr that matches name. If no session found,
893 * NULL is returned. This must be called with the session list lock held using
894 * session_lock_list and session_unlock_list.
895 * A reference to the session is implicitly acquired by this function.
897 struct ltt_session
*session_find_by_name(const char *name
)
899 struct ltt_session
*iter
;
902 ASSERT_LOCKED(ltt_session_list
.lock
);
904 DBG2("Trying to find session by name %s", name
);
906 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
907 if (!strncmp(iter
->name
, name
, NAME_MAX
) &&
915 return session_get(iter
) ? iter
: NULL
;
919 * Return an ltt_session that matches the id. If no session is found,
920 * NULL is returned. This must be called with rcu_read_lock and
921 * session list lock held (to guarantee the lifetime of the session).
923 struct ltt_session
*session_find_by_id(uint64_t id
)
925 struct lttng_ht_node_u64
*node
;
926 struct lttng_ht_iter iter
;
927 struct ltt_session
*ls
;
929 ASSERT_LOCKED(ltt_session_list
.lock
);
931 if (!ltt_sessions_ht_by_id
) {
935 lttng_ht_lookup(ltt_sessions_ht_by_id
, &id
, &iter
);
936 node
= lttng_ht_iter_get_node_u64(&iter
);
940 ls
= caa_container_of(node
, struct ltt_session
, node
);
942 DBG3("Session %" PRIu64
" found by id.", id
);
943 return session_get(ls
) ? ls
: NULL
;
946 DBG3("Session %" PRIu64
" NOT found by id", id
);
951 * Create a new session and add it to the session list.
952 * Session list lock must be held by the caller.
954 enum lttng_error_code
session_create(const char *name
, uid_t uid
, gid_t gid
,
955 struct ltt_session
**out_session
)
958 enum lttng_error_code ret_code
;
959 struct ltt_session
*new_session
= NULL
;
961 ASSERT_LOCKED(ltt_session_list
.lock
);
963 struct ltt_session
*clashing_session
;
965 clashing_session
= session_find_by_name(name
);
966 if (clashing_session
) {
967 session_put(clashing_session
);
968 ret_code
= LTTNG_ERR_EXIST_SESS
;
972 new_session
= zmalloc(sizeof(struct ltt_session
));
974 PERROR("Failed to allocate an ltt_session structure");
975 ret_code
= LTTNG_ERR_NOMEM
;
979 lttng_dynamic_array_init(&new_session
->destroy_notifiers
,
980 sizeof(struct ltt_session_destroy_notifier_element
),
982 urcu_ref_init(&new_session
->ref
);
983 pthread_mutex_init(&new_session
->lock
, NULL
);
985 new_session
->creation_time
= time(NULL
);
986 if (new_session
->creation_time
== (time_t) -1) {
987 PERROR("Failed to sample session creation time");
988 ret_code
= LTTNG_ERR_SESSION_FAIL
;
992 /* Create default consumer output. */
993 new_session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
994 if (new_session
->consumer
== NULL
) {
995 ret_code
= LTTNG_ERR_NOMEM
;
1000 ret
= lttng_strncpy(new_session
->name
, name
, sizeof(new_session
->name
));
1002 ret_code
= LTTNG_ERR_SESSION_INVALID_CHAR
;
1005 ret
= validate_name(name
);
1007 ret_code
= LTTNG_ERR_SESSION_INVALID_CHAR
;
1012 bool found_name
= false;
1014 struct tm
*timeinfo
;
1016 timeinfo
= localtime(&new_session
->creation_time
);
1018 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1021 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1022 for (i
= 0; i
< INT_MAX
; i
++) {
1023 struct ltt_session
*clashing_session
;
1026 ret
= snprintf(new_session
->name
,
1027 sizeof(new_session
->name
),
1029 DEFAULT_SESSION_NAME
,
1032 ret
= snprintf(new_session
->name
,
1033 sizeof(new_session
->name
),
1035 DEFAULT_SESSION_NAME
, i
,
1038 if (ret
== -1 || ret
>= sizeof(new_session
->name
)) {
1040 * Null-terminate in case the name is used
1041 * in logging statements.
1043 new_session
->name
[sizeof(new_session
->name
) - 1] = '\0';
1044 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1049 session_find_by_name(new_session
->name
);
1050 session_put(clashing_session
);
1051 if (!clashing_session
) {
1057 DBG("Generated session name \"%s\"", new_session
->name
);
1058 new_session
->has_auto_generated_name
= true;
1060 ERR("Failed to auto-generate a session name");
1061 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1066 ret
= gethostname(new_session
->hostname
, sizeof(new_session
->hostname
));
1068 if (errno
== ENAMETOOLONG
) {
1069 new_session
->hostname
[sizeof(new_session
->hostname
) - 1] = '\0';
1070 ERR("Hostname exceeds the maximal permitted length and has been truncated to %s",
1071 new_session
->hostname
);
1073 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1078 new_session
->uid
= uid
;
1079 new_session
->gid
= gid
;
1081 ret
= snapshot_init(&new_session
->snapshot
);
1083 ret_code
= LTTNG_ERR_NOMEM
;
1087 new_session
->rotation_state
= LTTNG_ROTATION_STATE_NO_ROTATION
;
1089 /* Add new session to the session list. */
1090 new_session
->id
= add_session_list(new_session
);
1093 * Add the new session to the ltt_sessions_ht_by_id.
1094 * No ownership is taken by the hash table; it is merely
1095 * a wrapper around the session list used for faster access
1098 add_session_ht(new_session
);
1099 new_session
->published
= true;
1102 * Consumer is left to NULL since the create_session_uri command will
1103 * set it up and, if valid, assign it to the session.
1105 DBG("Tracing session %s created with ID %" PRIu64
" by uid = %d, gid = %d",
1106 new_session
->name
, new_session
->id
, new_session
->uid
,
1108 ret_code
= LTTNG_OK
;
1111 (void) session_get(new_session
);
1112 *out_session
= new_session
;
1116 session_put(new_session
);
1122 * Check if the UID or GID match the session. Root user has access to all
1125 int session_access_ok(struct ltt_session
*session
, uid_t uid
, gid_t gid
)
1129 if (uid
!= session
->uid
&& gid
!= session
->gid
&& uid
!= 0) {
1137 * Set a session's rotation state and reset all associated state.
1139 * This function resets the rotation state (check timers, pending
1140 * flags, etc.) and sets the result of the last rotation. The result
1141 * can be queries by a liblttng-ctl client.
1143 * Be careful of the result passed to this function. For instance,
1144 * on failure to launch a rotation, a client will expect the rotation
1145 * state to be set to "NO_ROTATION". If an error occured while the
1146 * rotation was "ONGOING", result should be set to "ERROR", which will
1147 * allow a client to report it.
1149 * Must be called with the session and session_list locks held.
1151 int session_reset_rotation_state(struct ltt_session
*session
,
1152 enum lttng_rotation_state result
)
1156 ASSERT_LOCKED(ltt_session_list
.lock
);
1157 ASSERT_LOCKED(session
->lock
);
1159 session
->rotation_state
= result
;
1160 if (session
->rotation_pending_check_timer_enabled
) {
1161 ret
= timer_session_rotation_pending_check_stop(session
);
1163 if (session
->chunk_being_archived
) {
1165 enum lttng_trace_chunk_status chunk_status
;
1167 chunk_status
= lttng_trace_chunk_get_id(
1168 session
->chunk_being_archived
,
1170 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1171 LTTNG_OPTIONAL_SET(&session
->last_archived_chunk_id
,
1173 lttng_trace_chunk_put(session
->chunk_being_archived
);
1174 session
->chunk_being_archived
= NULL
;