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"
44 struct ltt_session_destroy_notifier_element
{
45 ltt_session_destroy_notifier notifier
;
52 * No ltt_session.lock is taken here because those data structure are widely
53 * spread across the lttng-tools code base so before caling functions below
54 * that can read/write a session, the caller MUST acquire the session lock
55 * using session_lock() and session_unlock().
59 * Init tracing session list.
61 * Please see session.h for more explanation and correct usage of the list.
63 static struct ltt_session_list ltt_session_list
= {
64 .head
= CDS_LIST_HEAD_INIT(ltt_session_list
.head
),
65 .lock
= PTHREAD_MUTEX_INITIALIZER
,
66 .removal_cond
= PTHREAD_COND_INITIALIZER
,
70 /* These characters are forbidden in a session name. Used by validate_name. */
71 static const char *forbidden_name_chars
= "/";
73 /* Global hash table to keep the sessions, indexed by id. */
74 static struct lttng_ht
*ltt_sessions_ht_by_id
= NULL
;
77 * Validate the session name for forbidden characters.
79 * Return 0 on success else -1 meaning a forbidden char. has been found.
81 static int validate_name(const char *name
)
88 tmp_name
= strdup(name
);
95 tok
= strpbrk(tmp_name
, forbidden_name_chars
);
97 DBG("Session name %s contains a forbidden character", name
);
98 /* Forbidden character has been found. */
110 * Add a ltt_session structure to the global list.
112 * The caller MUST acquire the session list lock before.
113 * Returns the unique identifier for the session.
115 static uint64_t add_session_list(struct ltt_session
*ls
)
119 cds_list_add(&ls
->list
, <t_session_list
.head
);
120 return ltt_session_list
.next_uuid
++;
124 * Delete a ltt_session structure to the global list.
126 * The caller MUST acquire the session list lock before.
128 static void del_session_list(struct ltt_session
*ls
)
132 cds_list_del(&ls
->list
);
136 * Return a pointer to the session list.
138 struct ltt_session_list
*session_get_list(void)
140 return <t_session_list
;
144 * Returns once the session list is empty.
146 void session_list_wait_empty(void)
148 pthread_mutex_lock(<t_session_list
.lock
);
149 while (!cds_list_empty(<t_session_list
.head
)) {
150 pthread_cond_wait(<t_session_list
.removal_cond
,
151 <t_session_list
.lock
);
153 pthread_mutex_unlock(<t_session_list
.lock
);
157 * Acquire session list lock
159 void session_lock_list(void)
161 pthread_mutex_lock(<t_session_list
.lock
);
165 * Try to acquire session list lock
167 int session_trylock_list(void)
169 return pthread_mutex_trylock(<t_session_list
.lock
);
173 * Release session list lock
175 void session_unlock_list(void)
177 pthread_mutex_unlock(<t_session_list
.lock
);
181 * Get the session's consumer destination type.
183 * The caller must hold the session lock.
185 enum consumer_dst_type
session_get_consumer_destination_type(
186 const struct ltt_session
*session
)
189 * The output information is duplicated in both of those session types.
190 * Hence, it doesn't matter from which it is retrieved. However, it is
191 * possible for only one of them to be set.
193 return session
->kernel_session
?
194 session
->kernel_session
->consumer
->type
:
195 session
->ust_session
->consumer
->type
;
199 * Get the session's consumer network hostname.
200 * The caller must ensure that the destination is of type "net".
202 * The caller must hold the session lock.
204 const char *session_get_net_consumer_hostname(const struct ltt_session
*session
)
206 const char *hostname
= NULL
;
207 const struct consumer_output
*output
;
209 output
= session
->kernel_session
?
210 session
->kernel_session
->consumer
:
211 session
->ust_session
->consumer
;
214 * hostname is assumed to be the same for both control and data
217 switch (output
->dst
.net
.control
.dtype
) {
219 hostname
= output
->dst
.net
.control
.dst
.ipv4
;
222 hostname
= output
->dst
.net
.control
.dst
.ipv6
;
231 * Get the session's consumer network control and data ports.
232 * The caller must ensure that the destination is of type "net".
234 * The caller must hold the session lock.
236 void session_get_net_consumer_ports(const struct ltt_session
*session
,
237 uint16_t *control_port
, uint16_t *data_port
)
239 const struct consumer_output
*output
;
241 output
= session
->kernel_session
?
242 session
->kernel_session
->consumer
:
243 session
->ust_session
->consumer
;
244 *control_port
= output
->dst
.net
.control
.port
;
245 *data_port
= output
->dst
.net
.data
.port
;
249 * Get the location of the latest trace archive produced by a rotation.
251 * The caller must hold the session lock.
253 struct lttng_trace_archive_location
*session_get_trace_archive_location(
254 const struct ltt_session
*session
)
257 struct lttng_trace_archive_location
*location
= NULL
;
258 char *chunk_path
= NULL
;
260 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
||
261 !session
->last_archived_chunk_name
) {
265 switch (session_get_consumer_destination_type(session
)) {
266 case CONSUMER_DST_LOCAL
:
267 ret
= asprintf(&chunk_path
,
268 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
269 session_get_base_path(session
),
270 session
->last_archived_chunk_name
);
274 location
= lttng_trace_archive_location_local_create(
277 case CONSUMER_DST_NET
:
279 const char *hostname
;
280 uint16_t control_port
, data_port
;
282 hostname
= session_get_net_consumer_hostname(session
);
283 session_get_net_consumer_ports(session
,
286 location
= lttng_trace_archive_location_relay_create(
288 LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
,
289 control_port
, data_port
, session
->last_chunk_path
);
301 * Allocate the ltt_sessions_ht_by_id HT.
303 * The session list lock must be held.
305 int ltt_sessions_ht_alloc(void)
309 DBG("Allocating ltt_sessions_ht_by_id");
310 ltt_sessions_ht_by_id
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
311 if (!ltt_sessions_ht_by_id
) {
313 ERR("Failed to allocate ltt_sessions_ht_by_id");
321 * Destroy the ltt_sessions_ht_by_id HT.
323 * The session list lock must be held.
325 static void ltt_sessions_ht_destroy(void)
327 if (!ltt_sessions_ht_by_id
) {
330 ht_cleanup_push(ltt_sessions_ht_by_id
);
331 ltt_sessions_ht_by_id
= NULL
;
335 * Add a ltt_session to the ltt_sessions_ht_by_id.
336 * If unallocated, the ltt_sessions_ht_by_id HT is allocated.
337 * The session list lock must be held.
339 static void add_session_ht(struct ltt_session
*ls
)
345 if (!ltt_sessions_ht_by_id
) {
346 ret
= ltt_sessions_ht_alloc();
348 ERR("Error allocating the sessions HT");
352 lttng_ht_node_init_u64(&ls
->node
, ls
->id
);
353 lttng_ht_add_unique_u64(ltt_sessions_ht_by_id
, &ls
->node
);
360 * Test if ltt_sessions_ht_by_id is empty.
361 * Return 1 if empty, 0 if not empty.
362 * The session list lock must be held.
364 static int ltt_sessions_ht_empty(void)
368 if (!ltt_sessions_ht_by_id
) {
373 ret
= lttng_ht_get_count(ltt_sessions_ht_by_id
) ? 0 : 1;
379 * Remove a ltt_session from the ltt_sessions_ht_by_id.
380 * If empty, the ltt_sessions_ht_by_id HT is freed.
381 * The session list lock must be held.
383 static void del_session_ht(struct ltt_session
*ls
)
385 struct lttng_ht_iter iter
;
389 assert(ltt_sessions_ht_by_id
);
391 iter
.iter
.node
= &ls
->node
.node
;
392 ret
= lttng_ht_del(ltt_sessions_ht_by_id
, &iter
);
395 if (ltt_sessions_ht_empty()) {
396 DBG("Empty ltt_sessions_ht_by_id, destroying it");
397 ltt_sessions_ht_destroy();
402 * Acquire session lock
404 void session_lock(struct ltt_session
*session
)
408 pthread_mutex_lock(&session
->lock
);
412 * Release session lock
414 void session_unlock(struct ltt_session
*session
)
418 pthread_mutex_unlock(&session
->lock
);
422 int _session_set_trace_chunk_no_lock_check(struct ltt_session
*session
,
423 struct lttng_trace_chunk
*new_trace_chunk
,
424 struct lttng_trace_chunk
**_current_trace_chunk
)
427 unsigned int i
, refs_to_acquire
= 0, refs_acquired
= 0, refs_to_release
= 0;
428 struct cds_lfht_iter iter
;
429 struct consumer_socket
*socket
;
430 struct lttng_trace_chunk
*current_trace_chunk
;
432 enum lttng_trace_chunk_status chunk_status
;
436 * Ownership of current trace chunk is transferred to
437 * `current_trace_chunk`.
439 current_trace_chunk
= session
->current_trace_chunk
;
440 session
->current_trace_chunk
= NULL
;
441 if (session
->ust_session
) {
442 lttng_trace_chunk_put(
443 session
->ust_session
->current_trace_chunk
);
444 session
->ust_session
->current_trace_chunk
= NULL
;
446 if (session
->kernel_session
) {
447 lttng_trace_chunk_put(
448 session
->kernel_session
->current_trace_chunk
);
449 session
->kernel_session
->current_trace_chunk
= NULL
;
451 if (!new_trace_chunk
) {
455 chunk_status
= lttng_trace_chunk_get_id(new_trace_chunk
, &chunk_id
);
456 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
459 refs_to_acquire
+= !!session
->ust_session
;
460 refs_to_acquire
+= !!session
->kernel_session
;
462 for (refs_acquired
= 0; refs_acquired
< refs_to_acquire
;
464 if (!lttng_trace_chunk_get(new_trace_chunk
)) {
465 ERR("Failed to acquire reference to new trace chunk of session \"%s\"",
471 if (session
->ust_session
) {
472 const uint64_t relayd_id
=
473 session
->ust_session
->consumer
->net_seq_index
;
474 const bool is_local_trace
=
475 session
->ust_session
->consumer
->type
==
478 session
->ust_session
->current_trace_chunk
= new_trace_chunk
;
479 if (is_local_trace
) {
480 enum lttng_error_code ret_error_code
;
482 ret_error_code
= ust_app_create_channel_subdirectories(
483 session
->ust_session
);
484 if (ret_error_code
!= LTTNG_OK
) {
485 ret
= -ret_error_code
;
489 cds_lfht_for_each_entry(
490 session
->ust_session
->consumer
->socks
->ht
,
491 &iter
, socket
, node
.node
) {
492 pthread_mutex_lock(socket
->lock
);
493 ret
= consumer_create_trace_chunk(socket
,
495 session
->id
, new_trace_chunk
);
496 pthread_mutex_unlock(socket
->lock
);
502 if (session
->kernel_session
) {
503 const uint64_t relayd_id
=
504 session
->kernel_session
->consumer
->net_seq_index
;
505 const bool is_local_trace
=
506 session
->kernel_session
->consumer
->type
==
509 session
->kernel_session
->current_trace_chunk
= new_trace_chunk
;
510 if (is_local_trace
) {
511 enum lttng_error_code ret_error_code
;
513 ret_error_code
= kernel_create_channel_subdirectories(
514 session
->kernel_session
);
515 if (ret_error_code
!= LTTNG_OK
) {
516 ret
= -ret_error_code
;
520 cds_lfht_for_each_entry(
521 session
->kernel_session
->consumer
->socks
->ht
,
522 &iter
, socket
, node
.node
) {
523 pthread_mutex_lock(socket
->lock
);
524 ret
= consumer_create_trace_chunk(socket
,
526 session
->id
, new_trace_chunk
);
527 pthread_mutex_unlock(socket
->lock
);
535 * Update local current trace chunk state last, only if all remote
536 * creations succeeded.
538 session
->current_trace_chunk
= new_trace_chunk
;
539 LTTNG_OPTIONAL_SET(&session
->most_recent_chunk_id
, chunk_id
);
541 if (_current_trace_chunk
) {
542 *_current_trace_chunk
= current_trace_chunk
;
543 current_trace_chunk
= NULL
;
547 lttng_trace_chunk_put(current_trace_chunk
);
550 if (session
->ust_session
) {
551 session
->ust_session
->current_trace_chunk
= NULL
;
553 if (session
->kernel_session
) {
554 session
->kernel_session
->current_trace_chunk
= NULL
;
557 * Release references taken in the case where all references could not
560 refs_to_release
= refs_to_acquire
- refs_acquired
;
561 for (i
= 0; i
< refs_to_release
; i
++) {
562 lttng_trace_chunk_put(new_trace_chunk
);
568 struct lttng_trace_chunk
*session_create_new_trace_chunk(
569 const struct ltt_session
*session
,
570 const struct consumer_output
*consumer_output_override
,
571 const char *session_base_path_override
,
572 const char *chunk_name_override
)
575 struct lttng_trace_chunk
*trace_chunk
= NULL
;
576 enum lttng_trace_chunk_status chunk_status
;
577 const time_t chunk_creation_ts
= time(NULL
);
579 const char *base_path
;
580 struct lttng_directory_handle session_output_directory
;
581 const struct lttng_credentials session_credentials
= {
585 uint64_t next_chunk_id
;
586 const struct consumer_output
*output
;
588 if (consumer_output_override
) {
589 output
= consumer_output_override
;
591 assert(session
->ust_session
|| session
->kernel_session
);
592 output
= session
->ust_session
?
593 session
->ust_session
->consumer
:
594 session
->kernel_session
->consumer
;
597 is_local_trace
= output
->type
== CONSUMER_DST_LOCAL
;
598 base_path
= session_base_path_override
? :
599 consumer_output_get_base_path(output
);
601 if (chunk_creation_ts
== (time_t) -1) {
602 PERROR("Failed to sample time while creation session \"%s\" trace chunk",
607 next_chunk_id
= session
->most_recent_chunk_id
.is_set
?
608 session
->most_recent_chunk_id
.value
+ 1 : 0;
610 trace_chunk
= lttng_trace_chunk_create(next_chunk_id
,
616 if (chunk_name_override
) {
617 chunk_status
= lttng_trace_chunk_override_name(trace_chunk
,
618 chunk_name_override
);
619 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
624 if (!is_local_trace
) {
626 * No need to set crendentials and output directory
627 * for remote trace chunks.
632 chunk_status
= lttng_trace_chunk_set_credentials(trace_chunk
,
633 &session_credentials
);
634 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
638 DBG("Creating base output directory of session \"%s\" at %s",
639 session
->name
, base_path
);
640 ret
= utils_mkdir_recursive(base_path
, S_IRWXU
| S_IRWXG
,
641 session
->uid
, session
->gid
);
645 ret
= lttng_directory_handle_init(&session_output_directory
,
650 chunk_status
= lttng_trace_chunk_set_as_owner(trace_chunk
,
651 &session_output_directory
);
652 lttng_directory_handle_fini(&session_output_directory
);
653 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
659 lttng_trace_chunk_put(trace_chunk
);
664 int session_close_trace_chunk(const struct ltt_session
*session
,
665 struct lttng_trace_chunk
*trace_chunk
,
666 const enum lttng_trace_chunk_command_type
*close_command
,
667 char *closed_trace_chunk_path
)
670 bool error_occurred
= false;
671 struct cds_lfht_iter iter
;
672 struct consumer_socket
*socket
;
673 enum lttng_trace_chunk_status chunk_status
;
674 const time_t chunk_close_timestamp
= time(NULL
);
677 chunk_status
= lttng_trace_chunk_set_close_command(
678 trace_chunk
, *close_command
);
679 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
685 if (chunk_close_timestamp
== (time_t) -1) {
686 ERR("Failed to sample the close timestamp of the current trace chunk of session \"%s\"",
691 chunk_status
= lttng_trace_chunk_set_close_timestamp(trace_chunk
,
692 chunk_close_timestamp
);
693 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
694 ERR("Failed to set the close timestamp of the current trace chunk of session \"%s\"",
700 if (session
->ust_session
) {
701 const uint64_t relayd_id
=
702 session
->ust_session
->consumer
->net_seq_index
;
704 cds_lfht_for_each_entry(
705 session
->ust_session
->consumer
->socks
->ht
,
706 &iter
, socket
, node
.node
) {
707 pthread_mutex_lock(socket
->lock
);
708 ret
= consumer_close_trace_chunk(socket
,
711 trace_chunk
, closed_trace_chunk_path
);
712 pthread_mutex_unlock(socket
->lock
);
714 ERR("Failed to close trace chunk on user space consumer");
715 error_occurred
= true;
719 if (session
->kernel_session
) {
720 const uint64_t relayd_id
=
721 session
->kernel_session
->consumer
->net_seq_index
;
723 cds_lfht_for_each_entry(
724 session
->kernel_session
->consumer
->socks
->ht
,
725 &iter
, socket
, node
.node
) {
726 pthread_mutex_lock(socket
->lock
);
727 ret
= consumer_close_trace_chunk(socket
,
730 trace_chunk
, closed_trace_chunk_path
);
731 pthread_mutex_unlock(socket
->lock
);
733 ERR("Failed to close trace chunk on kernel consumer");
734 error_occurred
= true;
738 ret
= error_occurred
? -1 : 0;
744 * Set a session's current trace chunk.
746 * Must be called with the session lock held.
748 int session_set_trace_chunk(struct ltt_session
*session
,
749 struct lttng_trace_chunk
*new_trace_chunk
,
750 struct lttng_trace_chunk
**current_trace_chunk
)
752 ASSERT_LOCKED(session
->lock
);
753 return _session_set_trace_chunk_no_lock_check(session
, new_trace_chunk
,
754 current_trace_chunk
);
758 void session_notify_destruction(const struct ltt_session
*session
)
761 const size_t count
= lttng_dynamic_array_get_count(
762 &session
->destroy_notifiers
);
764 for (i
= 0; i
< count
; i
++) {
765 const struct ltt_session_destroy_notifier_element
*element
=
766 lttng_dynamic_array_get_element(
767 &session
->destroy_notifiers
, i
);
769 element
->notifier(session
, element
->user_data
);
774 void session_release(struct urcu_ref
*ref
)
777 struct ltt_ust_session
*usess
;
778 struct ltt_kernel_session
*ksess
;
779 struct ltt_session
*session
= container_of(ref
, typeof(*session
), ref
);
780 const bool session_published
= session
->published
;
782 assert(!session
->chunk_being_archived
);
784 usess
= session
->ust_session
;
785 ksess
= session
->kernel_session
;
787 /* Clean kernel session teardown, keeping data for destroy notifier. */
788 kernel_destroy_session(ksess
);
790 /* UST session teardown, keeping data for destroy notifier. */
792 /* Close any relayd session */
793 consumer_output_send_destroy_relayd(usess
->consumer
);
795 /* Destroy every UST application related to this session. */
796 ret
= ust_app_destroy_trace_all(usess
);
798 ERR("Error in ust_app_destroy_trace_all");
801 /* Clean up the rest, keeping destroy notifier data. */
802 trace_ust_destroy_session(usess
);
806 * Must notify the kernel thread here to update it's poll set in order to
807 * remove the channel(s)' fd just destroyed.
809 ret
= notify_thread_pipe(kernel_poll_pipe
[1]);
811 PERROR("write kernel poll pipe");
814 DBG("Destroying session %s (id %" PRIu64
")", session
->name
, session
->id
);
816 consumer_output_put(session
->consumer
);
817 snapshot_destroy(&session
->snapshot
);
819 pthread_mutex_destroy(&session
->lock
);
821 if (session_published
) {
822 ASSERT_LOCKED(ltt_session_list
.lock
);
823 del_session_list(session
);
824 del_session_ht(session
);
826 session_notify_destruction(session
);
828 kernel_free_session(ksess
);
829 session
->kernel_session
= NULL
;
831 trace_ust_free_session(usess
);
832 session
->ust_session
= NULL
;
834 lttng_dynamic_array_reset(&session
->destroy_notifiers
);
835 free(session
->last_archived_chunk_name
);
836 free(session
->base_path
);
838 if (session_published
) {
840 * Broadcast after free-ing to ensure the memory is
841 * reclaimed before the main thread exits.
843 pthread_cond_broadcast(<t_session_list
.removal_cond
);
848 * Acquire a reference to a session.
849 * This function may fail (return false); its return value must be checked.
851 bool session_get(struct ltt_session
*session
)
853 return urcu_ref_get_unless_zero(&session
->ref
);
857 * Release a reference to a session.
859 void session_put(struct ltt_session
*session
)
865 * The session list lock must be held as any session_put()
866 * may cause the removal of the session from the session_list.
868 ASSERT_LOCKED(ltt_session_list
.lock
);
869 assert(session
->ref
.refcount
);
870 urcu_ref_put(&session
->ref
, session_release
);
876 * This method does not immediately release/free the session as other
877 * components may still hold a reference to the session. However,
878 * the session should no longer be presented to the user.
880 * Releases the session list's reference to the session
881 * and marks it as destroyed. Iterations on the session list should be
882 * mindful of the "destroyed" flag.
884 void session_destroy(struct ltt_session
*session
)
886 assert(!session
->destroyed
);
887 session
->destroyed
= true;
888 session_put(session
);
891 int session_add_destroy_notifier(struct ltt_session
*session
,
892 ltt_session_destroy_notifier notifier
, void *user_data
)
894 const struct ltt_session_destroy_notifier_element element
= {
895 .notifier
= notifier
,
896 .user_data
= user_data
899 return lttng_dynamic_array_add_element(&session
->destroy_notifiers
,
904 * Return a ltt_session structure ptr that matches name. If no session found,
905 * NULL is returned. This must be called with the session list lock held using
906 * session_lock_list and session_unlock_list.
907 * A reference to the session is implicitly acquired by this function.
909 struct ltt_session
*session_find_by_name(const char *name
)
911 struct ltt_session
*iter
;
914 ASSERT_LOCKED(ltt_session_list
.lock
);
916 DBG2("Trying to find session by name %s", name
);
918 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
919 if (!strncmp(iter
->name
, name
, NAME_MAX
) &&
927 return session_get(iter
) ? iter
: NULL
;
931 * Return an ltt_session that matches the id. If no session is found,
932 * NULL is returned. This must be called with rcu_read_lock and
933 * session list lock held (to guarantee the lifetime of the session).
935 struct ltt_session
*session_find_by_id(uint64_t id
)
937 struct lttng_ht_node_u64
*node
;
938 struct lttng_ht_iter iter
;
939 struct ltt_session
*ls
;
941 ASSERT_LOCKED(ltt_session_list
.lock
);
943 if (!ltt_sessions_ht_by_id
) {
947 lttng_ht_lookup(ltt_sessions_ht_by_id
, &id
, &iter
);
948 node
= lttng_ht_iter_get_node_u64(&iter
);
952 ls
= caa_container_of(node
, struct ltt_session
, node
);
954 DBG3("Session %" PRIu64
" found by id.", id
);
955 return session_get(ls
) ? ls
: NULL
;
958 DBG3("Session %" PRIu64
" NOT found by id", id
);
963 * Create a new session and add it to the session list.
964 * Session list lock must be held by the caller.
966 enum lttng_error_code
session_create(const char *name
, uid_t uid
, gid_t gid
,
967 const char *base_path
, struct ltt_session
**out_session
)
970 enum lttng_error_code ret_code
;
971 struct ltt_session
*new_session
= NULL
;
973 ASSERT_LOCKED(ltt_session_list
.lock
);
975 struct ltt_session
*clashing_session
;
977 clashing_session
= session_find_by_name(name
);
978 if (clashing_session
) {
979 session_put(clashing_session
);
980 ret_code
= LTTNG_ERR_EXIST_SESS
;
984 new_session
= zmalloc(sizeof(struct ltt_session
));
986 PERROR("Failed to allocate an ltt_session structure");
987 ret_code
= LTTNG_ERR_NOMEM
;
991 lttng_dynamic_array_init(&new_session
->destroy_notifiers
,
992 sizeof(struct ltt_session_destroy_notifier_element
),
994 urcu_ref_init(&new_session
->ref
);
995 pthread_mutex_init(&new_session
->lock
, NULL
);
997 new_session
->creation_time
= time(NULL
);
998 if (new_session
->creation_time
== (time_t) -1) {
999 PERROR("Failed to sample session creation time");
1000 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1004 /* Create default consumer output. */
1005 new_session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
1006 if (new_session
->consumer
== NULL
) {
1007 ret_code
= LTTNG_ERR_NOMEM
;
1012 ret
= lttng_strncpy(new_session
->name
, name
, sizeof(new_session
->name
));
1014 ret_code
= LTTNG_ERR_SESSION_INVALID_CHAR
;
1017 ret
= validate_name(name
);
1019 ret_code
= LTTNG_ERR_SESSION_INVALID_CHAR
;
1024 bool found_name
= false;
1026 struct tm
*timeinfo
;
1028 timeinfo
= localtime(&new_session
->creation_time
);
1030 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1033 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1034 for (i
= 0; i
< INT_MAX
; i
++) {
1035 struct ltt_session
*clashing_session
;
1038 ret
= snprintf(new_session
->name
,
1039 sizeof(new_session
->name
),
1041 DEFAULT_SESSION_NAME
,
1044 ret
= snprintf(new_session
->name
,
1045 sizeof(new_session
->name
),
1047 DEFAULT_SESSION_NAME
, i
,
1050 new_session
->name_contains_creation_time
= true;
1051 if (ret
== -1 || ret
>= sizeof(new_session
->name
)) {
1053 * Null-terminate in case the name is used
1054 * in logging statements.
1056 new_session
->name
[sizeof(new_session
->name
) - 1] = '\0';
1057 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1062 session_find_by_name(new_session
->name
);
1063 session_put(clashing_session
);
1064 if (!clashing_session
) {
1070 DBG("Generated session name \"%s\"", new_session
->name
);
1071 new_session
->has_auto_generated_name
= true;
1073 ERR("Failed to auto-generate a session name");
1074 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1079 ret
= gethostname(new_session
->hostname
, sizeof(new_session
->hostname
));
1081 if (errno
== ENAMETOOLONG
) {
1082 new_session
->hostname
[sizeof(new_session
->hostname
) - 1] = '\0';
1083 ERR("Hostname exceeds the maximal permitted length and has been truncated to %s",
1084 new_session
->hostname
);
1086 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1092 new_session
->base_path
= strdup(base_path
);
1093 if (!new_session
->base_path
) {
1094 ERR("Failed to allocate base path of session \"%s\"",
1096 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1101 new_session
->uid
= uid
;
1102 new_session
->gid
= gid
;
1104 ret
= snapshot_init(&new_session
->snapshot
);
1106 ret_code
= LTTNG_ERR_NOMEM
;
1110 new_session
->rotation_state
= LTTNG_ROTATION_STATE_NO_ROTATION
;
1112 /* Add new session to the session list. */
1113 new_session
->id
= add_session_list(new_session
);
1116 * Add the new session to the ltt_sessions_ht_by_id.
1117 * No ownership is taken by the hash table; it is merely
1118 * a wrapper around the session list used for faster access
1121 add_session_ht(new_session
);
1122 new_session
->published
= true;
1125 * Consumer is left to NULL since the create_session_uri command will
1126 * set it up and, if valid, assign it to the session.
1128 DBG("Tracing session %s created with ID %" PRIu64
" by uid = %d, gid = %d",
1129 new_session
->name
, new_session
->id
, new_session
->uid
,
1131 ret_code
= LTTNG_OK
;
1134 (void) session_get(new_session
);
1135 *out_session
= new_session
;
1139 session_put(new_session
);
1145 * Check if the UID or GID match the session. Root user has access to all
1148 int session_access_ok(struct ltt_session
*session
, uid_t uid
, gid_t gid
)
1152 if (uid
!= session
->uid
&& gid
!= session
->gid
&& uid
!= 0) {
1160 * Set a session's rotation state and reset all associated state.
1162 * This function resets the rotation state (check timers, pending
1163 * flags, etc.) and sets the result of the last rotation. The result
1164 * can be queries by a liblttng-ctl client.
1166 * Be careful of the result passed to this function. For instance,
1167 * on failure to launch a rotation, a client will expect the rotation
1168 * state to be set to "NO_ROTATION". If an error occured while the
1169 * rotation was "ONGOING", result should be set to "ERROR", which will
1170 * allow a client to report it.
1172 * Must be called with the session and session_list locks held.
1174 int session_reset_rotation_state(struct ltt_session
*session
,
1175 enum lttng_rotation_state result
)
1179 ASSERT_LOCKED(ltt_session_list
.lock
);
1180 ASSERT_LOCKED(session
->lock
);
1182 session
->rotation_state
= result
;
1183 if (session
->rotation_pending_check_timer_enabled
) {
1184 ret
= timer_session_rotation_pending_check_stop(session
);
1186 if (session
->chunk_being_archived
) {
1188 enum lttng_trace_chunk_status chunk_status
;
1190 chunk_status
= lttng_trace_chunk_get_id(
1191 session
->chunk_being_archived
,
1193 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1194 LTTNG_OPTIONAL_SET(&session
->last_archived_chunk_id
,
1196 lttng_trace_chunk_put(session
->chunk_being_archived
);
1197 session
->chunk_being_archived
= NULL
;