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"
46 * No ltt_session.lock is taken here because those data structure are widely
47 * spread across the lttng-tools code base so before caling functions below
48 * that can read/write a session, the caller MUST acquire the session lock
49 * using session_lock() and session_unlock().
53 * Init tracing session list.
55 * Please see session.h for more explanation and correct usage of the list.
57 static struct ltt_session_list ltt_session_list
= {
58 .head
= CDS_LIST_HEAD_INIT(ltt_session_list
.head
),
59 .lock
= PTHREAD_MUTEX_INITIALIZER
,
60 .removal_cond
= PTHREAD_COND_INITIALIZER
,
64 /* These characters are forbidden in a session name. Used by validate_name. */
65 static const char *forbidden_name_chars
= "/";
67 /* Global hash table to keep the sessions, indexed by id. */
68 static struct lttng_ht
*ltt_sessions_ht_by_id
= NULL
;
71 * Validate the session name for forbidden characters.
73 * Return 0 on success else -1 meaning a forbidden char. has been found.
75 static int validate_name(const char *name
)
82 tmp_name
= strdup(name
);
89 tok
= strpbrk(tmp_name
, forbidden_name_chars
);
91 DBG("Session name %s contains a forbidden character", name
);
92 /* Forbidden character has been found. */
104 * Add a ltt_session structure to the global list.
106 * The caller MUST acquire the session list lock before.
107 * Returns the unique identifier for the session.
109 static uint64_t add_session_list(struct ltt_session
*ls
)
113 cds_list_add(&ls
->list
, <t_session_list
.head
);
114 return ltt_session_list
.next_uuid
++;
118 * Delete a ltt_session structure to the global list.
120 * The caller MUST acquire the session list lock before.
122 static void del_session_list(struct ltt_session
*ls
)
126 cds_list_del(&ls
->list
);
130 * Return a pointer to the session list.
132 struct ltt_session_list
*session_get_list(void)
134 return <t_session_list
;
138 * Returns once the session list is empty.
140 void session_list_wait_empty(void)
142 pthread_mutex_lock(<t_session_list
.lock
);
143 while (!cds_list_empty(<t_session_list
.head
)) {
144 pthread_cond_wait(<t_session_list
.removal_cond
,
145 <t_session_list
.lock
);
147 pthread_mutex_unlock(<t_session_list
.lock
);
151 * Acquire session list lock
153 void session_lock_list(void)
155 pthread_mutex_lock(<t_session_list
.lock
);
159 * Try to acquire session list lock
161 int session_trylock_list(void)
163 return pthread_mutex_trylock(<t_session_list
.lock
);
167 * Release session list lock
169 void session_unlock_list(void)
171 pthread_mutex_unlock(<t_session_list
.lock
);
175 * Get the session's consumer destination type.
177 * The caller must hold the session lock.
179 enum consumer_dst_type
session_get_consumer_destination_type(
180 const struct ltt_session
*session
)
183 * The output information is duplicated in both of those session types.
184 * Hence, it doesn't matter from which it is retrieved. However, it is
185 * possible for only one of them to be set.
187 return session
->kernel_session
?
188 session
->kernel_session
->consumer
->type
:
189 session
->ust_session
->consumer
->type
;
193 * Get the session's consumer network hostname.
194 * The caller must ensure that the destination is of type "net".
196 * The caller must hold the session lock.
198 const char *session_get_net_consumer_hostname(const struct ltt_session
*session
)
200 const char *hostname
= NULL
;
201 const struct consumer_output
*output
;
203 output
= session
->kernel_session
?
204 session
->kernel_session
->consumer
:
205 session
->ust_session
->consumer
;
208 * hostname is assumed to be the same for both control and data
211 switch (output
->dst
.net
.control
.dtype
) {
213 hostname
= output
->dst
.net
.control
.dst
.ipv4
;
216 hostname
= output
->dst
.net
.control
.dst
.ipv6
;
225 * Get the session's consumer network control and data ports.
226 * The caller must ensure that the destination is of type "net".
228 * The caller must hold the session lock.
230 void session_get_net_consumer_ports(const struct ltt_session
*session
,
231 uint16_t *control_port
, uint16_t *data_port
)
233 const struct consumer_output
*output
;
235 output
= session
->kernel_session
?
236 session
->kernel_session
->consumer
:
237 session
->ust_session
->consumer
;
238 *control_port
= output
->dst
.net
.control
.port
;
239 *data_port
= output
->dst
.net
.data
.port
;
243 * Get the location of the latest trace archive produced by a rotation.
245 * The caller must hold the session lock.
247 struct lttng_trace_archive_location
*session_get_trace_archive_location(
248 struct ltt_session
*session
)
251 struct lttng_trace_archive_location
*location
= NULL
;
252 char *chunk_path
= NULL
;
254 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
||
255 !session
->last_archived_chunk_name
) {
259 ret
= asprintf(&chunk_path
, "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
260 session_get_base_path(session
),
261 session
->last_archived_chunk_name
);
266 switch (session_get_consumer_destination_type(session
)) {
267 case CONSUMER_DST_LOCAL
:
268 location
= lttng_trace_archive_location_local_create(
271 case CONSUMER_DST_NET
:
273 const char *hostname
;
274 uint16_t control_port
, data_port
;
276 hostname
= session_get_net_consumer_hostname(session
);
277 session_get_net_consumer_ports(session
,
280 location
= lttng_trace_archive_location_relay_create(
282 LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
,
283 control_port
, data_port
, chunk_path
);
295 * Allocate the ltt_sessions_ht_by_id HT.
297 * The session list lock must be held.
299 int ltt_sessions_ht_alloc(void)
303 DBG("Allocating ltt_sessions_ht_by_id");
304 ltt_sessions_ht_by_id
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
305 if (!ltt_sessions_ht_by_id
) {
307 ERR("Failed to allocate ltt_sessions_ht_by_id");
315 * Destroy the ltt_sessions_ht_by_id HT.
317 * The session list lock must be held.
319 static void ltt_sessions_ht_destroy(void)
321 if (!ltt_sessions_ht_by_id
) {
324 ht_cleanup_push(ltt_sessions_ht_by_id
);
325 ltt_sessions_ht_by_id
= NULL
;
329 * Add a ltt_session to the ltt_sessions_ht_by_id.
330 * If unallocated, the ltt_sessions_ht_by_id HT is allocated.
331 * The session list lock must be held.
333 static void add_session_ht(struct ltt_session
*ls
)
339 if (!ltt_sessions_ht_by_id
) {
340 ret
= ltt_sessions_ht_alloc();
342 ERR("Error allocating the sessions HT");
346 lttng_ht_node_init_u64(&ls
->node
, ls
->id
);
347 lttng_ht_add_unique_u64(ltt_sessions_ht_by_id
, &ls
->node
);
354 * Test if ltt_sessions_ht_by_id is empty.
355 * Return 1 if empty, 0 if not empty.
356 * The session list lock must be held.
358 static int ltt_sessions_ht_empty(void)
362 if (!ltt_sessions_ht_by_id
) {
367 ret
= lttng_ht_get_count(ltt_sessions_ht_by_id
) ? 0 : 1;
373 * Remove a ltt_session from the ltt_sessions_ht_by_id.
374 * If empty, the ltt_sessions_ht_by_id HT is freed.
375 * The session list lock must be held.
377 static void del_session_ht(struct ltt_session
*ls
)
379 struct lttng_ht_iter iter
;
383 assert(ltt_sessions_ht_by_id
);
385 iter
.iter
.node
= &ls
->node
.node
;
386 ret
= lttng_ht_del(ltt_sessions_ht_by_id
, &iter
);
389 if (ltt_sessions_ht_empty()) {
390 DBG("Empty ltt_sessions_ht_by_id, destroying it");
391 ltt_sessions_ht_destroy();
396 * Acquire session lock
398 void session_lock(struct ltt_session
*session
)
402 pthread_mutex_lock(&session
->lock
);
406 * Release session lock
408 void session_unlock(struct ltt_session
*session
)
412 pthread_mutex_unlock(&session
->lock
);
416 int _session_set_trace_chunk_no_lock_check(struct ltt_session
*session
,
417 struct lttng_trace_chunk
*new_trace_chunk
,
418 struct lttng_trace_chunk
**_current_trace_chunk
)
421 unsigned int i
, refs_to_acquire
= 0, refs_acquired
= 0, refs_to_release
= 0;
422 struct cds_lfht_iter iter
;
423 struct consumer_socket
*socket
;
424 struct lttng_trace_chunk
*current_trace_chunk
;
426 enum lttng_trace_chunk_status chunk_status
;
427 const uint64_t relayd_id
= session
->consumer
->net_seq_index
;
428 const bool is_local_trace
= relayd_id
== -1ULL;
432 * Ownership of current trace chunk is transferred to
433 * `current_trace_chunk`.
435 current_trace_chunk
= session
->current_trace_chunk
;
436 session
->current_trace_chunk
= NULL
;
437 if (session
->ust_session
) {
438 lttng_trace_chunk_put(
439 session
->ust_session
->current_trace_chunk
);
440 session
->ust_session
->current_trace_chunk
= NULL
;
442 if (session
->kernel_session
) {
443 lttng_trace_chunk_put(
444 session
->kernel_session
->current_trace_chunk
);
445 session
->kernel_session
->current_trace_chunk
= NULL
;
447 if (!new_trace_chunk
) {
451 chunk_status
= lttng_trace_chunk_get_id(new_trace_chunk
, &chunk_id
);
452 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
455 refs_to_acquire
+= !!session
->ust_session
;
456 refs_to_acquire
+= !!session
->kernel_session
;
458 for (refs_acquired
= 0; refs_acquired
< refs_to_acquire
;
460 if (!lttng_trace_chunk_get(new_trace_chunk
)) {
461 ERR("Failed to acquire reference to new trace chunk of session \"%s\"",
467 if (session
->ust_session
) {
468 session
->ust_session
->current_trace_chunk
= new_trace_chunk
;
469 if (is_local_trace
) {
470 enum lttng_error_code ret_error_code
;
472 ret_error_code
= ust_app_create_channel_subdirectories(
473 session
->ust_session
);
474 if (ret_error_code
!= LTTNG_OK
) {
475 ret
= -ret_error_code
;
479 cds_lfht_for_each_entry(
480 session
->ust_session
->consumer
->socks
->ht
,
481 &iter
, socket
, node
.node
) {
482 pthread_mutex_lock(socket
->lock
);
483 ret
= consumer_create_trace_chunk(socket
,
485 session
->id
, new_trace_chunk
);
486 pthread_mutex_unlock(socket
->lock
);
492 if (session
->kernel_session
) {
493 session
->kernel_session
->current_trace_chunk
= new_trace_chunk
;
494 if (is_local_trace
) {
495 enum lttng_error_code ret_error_code
;
497 ret_error_code
= kernel_create_channel_subdirectories(
498 session
->kernel_session
);
499 if (ret_error_code
!= LTTNG_OK
) {
500 ret
= -ret_error_code
;
504 cds_lfht_for_each_entry(
505 session
->kernel_session
->consumer
->socks
->ht
,
506 &iter
, socket
, node
.node
) {
507 pthread_mutex_lock(socket
->lock
);
508 ret
= consumer_create_trace_chunk(socket
,
510 session
->id
, new_trace_chunk
);
511 pthread_mutex_unlock(socket
->lock
);
519 * Update local current trace chunk state last, only if all remote
520 * creations succeeded.
522 session
->current_trace_chunk
= new_trace_chunk
;
523 LTTNG_OPTIONAL_SET(&session
->most_recent_chunk_id
, chunk_id
);
525 if (_current_trace_chunk
) {
526 *_current_trace_chunk
= current_trace_chunk
;
527 current_trace_chunk
= NULL
;
531 lttng_trace_chunk_put(current_trace_chunk
);
534 if (session
->ust_session
) {
535 session
->ust_session
->current_trace_chunk
= NULL
;
537 if (session
->kernel_session
) {
538 session
->kernel_session
->current_trace_chunk
= NULL
;
541 * Release references taken in the case where all references could not
544 refs_to_release
= refs_to_acquire
- refs_acquired
;
545 for (i
= 0; i
< refs_to_release
; i
++) {
546 lttng_trace_chunk_put(new_trace_chunk
);
553 bool output_supports_trace_chunks(const struct ltt_session
*session
)
555 if (session
->consumer
->type
== CONSUMER_DST_LOCAL
) {
558 struct consumer_output
*output
;
560 if (session
->ust_session
) {
561 output
= session
->ust_session
->consumer
;
562 } else if (session
->kernel_session
) {
563 output
= session
->kernel_session
->consumer
;
568 if (output
->relay_major_version
> 2) {
570 } else if (output
->relay_major_version
== 2 &&
571 output
->relay_minor_version
>= 11) {
578 struct lttng_trace_chunk
*session_create_new_trace_chunk(
579 struct ltt_session
*session
,
580 const char *session_base_path_override
,
581 const char *chunk_name_override
)
584 struct lttng_trace_chunk
*trace_chunk
= NULL
;
585 enum lttng_trace_chunk_status chunk_status
;
586 const time_t chunk_creation_ts
= time(NULL
);
587 const bool is_local_trace
=
588 session
->consumer
->type
== CONSUMER_DST_LOCAL
;
589 const char *base_path
= session_base_path_override
? :
590 session_get_base_path(session
);
591 struct lttng_directory_handle session_output_directory
;
592 const struct lttng_credentials session_credentials
= {
596 uint64_t next_chunk_id
;
598 if (chunk_creation_ts
== (time_t) -1) {
599 PERROR("Failed to sample time while creation session \"%s\" trace chunk",
604 if (!output_supports_trace_chunks(session
)) {
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
)
668 bool error_occurred
= false;
669 struct cds_lfht_iter iter
;
670 struct consumer_socket
*socket
;
671 enum lttng_trace_chunk_status chunk_status
;
672 const time_t chunk_close_timestamp
= time(NULL
);
674 if (chunk_close_timestamp
== (time_t) -1) {
675 ERR("Failed to sample the close timestamp of the current trace chunk of session \"%s\"",
680 chunk_status
= lttng_trace_chunk_set_close_timestamp(trace_chunk
,
681 chunk_close_timestamp
);
682 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
683 ERR("Failed to set the close timestamp of the current trace chunk of session \"%s\"",
689 if (session
->ust_session
) {
690 cds_lfht_for_each_entry(
691 session
->ust_session
->consumer
->socks
->ht
,
692 &iter
, socket
, node
.node
) {
693 pthread_mutex_lock(socket
->lock
);
694 ret
= consumer_close_trace_chunk(socket
,
695 session
->consumer
->net_seq_index
,
698 pthread_mutex_unlock(socket
->lock
);
700 ERR("Failed to close trace chunk on user space consumer");
701 error_occurred
= true;
705 if (session
->kernel_session
) {
706 cds_lfht_for_each_entry(
707 session
->kernel_session
->consumer
->socks
->ht
,
708 &iter
, socket
, node
.node
) {
709 pthread_mutex_lock(socket
->lock
);
710 ret
= consumer_close_trace_chunk(socket
,
711 session
->consumer
->net_seq_index
,
714 pthread_mutex_unlock(socket
->lock
);
716 ERR("Failed to close trace chunk on kernel consumer");
717 error_occurred
= true;
721 ret
= error_occurred
? -1 : 0;
727 * Set a session's current trace chunk.
729 * Must be called with the session lock held.
731 int session_set_trace_chunk(struct ltt_session
*session
,
732 struct lttng_trace_chunk
*new_trace_chunk
,
733 struct lttng_trace_chunk
**current_trace_chunk
)
735 ASSERT_LOCKED(session
->lock
);
736 return _session_set_trace_chunk_no_lock_check(session
, new_trace_chunk
,
737 current_trace_chunk
);
741 void session_release(struct urcu_ref
*ref
)
744 struct ltt_ust_session
*usess
;
745 struct ltt_kernel_session
*ksess
;
746 struct ltt_session
*session
= container_of(ref
, typeof(*session
), ref
);
748 assert(!session
->chunk_being_archived
);
750 usess
= session
->ust_session
;
751 ksess
= session
->kernel_session
;
752 if (session
->current_trace_chunk
) {
753 ret
= session_close_trace_chunk(session
, session
->current_trace_chunk
);
755 ERR("Failed to close the current trace chunk of session \"%s\" during its release",
758 ret
= _session_set_trace_chunk_no_lock_check(session
, NULL
, NULL
);
760 ERR("Failed to release the current trace chunk of session \"%s\" during its release",
765 /* Clean kernel session teardown */
766 kernel_destroy_session(ksess
);
767 session
->kernel_session
= NULL
;
769 /* UST session teardown */
771 /* Close any relayd session */
772 consumer_output_send_destroy_relayd(usess
->consumer
);
774 /* Destroy every UST application related to this session. */
775 ret
= ust_app_destroy_trace_all(usess
);
777 ERR("Error in ust_app_destroy_trace_all");
780 /* Clean up the rest. */
781 trace_ust_destroy_session(usess
);
782 session
->ust_session
= NULL
;
786 * Must notify the kernel thread here to update it's poll set in order to
787 * remove the channel(s)' fd just destroyed.
789 ret
= notify_thread_pipe(kernel_poll_pipe
[1]);
791 PERROR("write kernel poll pipe");
794 DBG("Destroying session %s (id %" PRIu64
")", session
->name
, session
->id
);
796 consumer_output_put(session
->consumer
);
797 snapshot_destroy(&session
->snapshot
);
799 pthread_mutex_destroy(&session
->lock
);
801 if (session
->published
) {
802 ASSERT_LOCKED(ltt_session_list
.lock
);
803 del_session_list(session
);
804 del_session_ht(session
);
805 pthread_cond_broadcast(<t_session_list
.removal_cond
);
807 free(session
->last_archived_chunk_name
);
812 * Acquire a reference to a session.
813 * This function may fail (return false); its return value must be checked.
815 bool session_get(struct ltt_session
*session
)
817 return urcu_ref_get_unless_zero(&session
->ref
);
821 * Release a reference to a session.
823 void session_put(struct ltt_session
*session
)
829 * The session list lock must be held as any session_put()
830 * may cause the removal of the session from the session_list.
832 ASSERT_LOCKED(ltt_session_list
.lock
);
833 assert(session
->ref
.refcount
);
834 urcu_ref_put(&session
->ref
, session_release
);
840 * This method does not immediately release/free the session as other
841 * components may still hold a reference to the session. However,
842 * the session should no longer be presented to the user.
844 * Releases the session list's reference to the session
845 * and marks it as destroyed. Iterations on the session list should be
846 * mindful of the "destroyed" flag.
848 void session_destroy(struct ltt_session
*session
)
850 assert(!session
->destroyed
);
851 session
->destroyed
= true;
852 session_put(session
);
856 * Return a ltt_session structure ptr that matches name. If no session found,
857 * NULL is returned. This must be called with the session list lock held using
858 * session_lock_list and session_unlock_list.
859 * A reference to the session is implicitly acquired by this function.
861 struct ltt_session
*session_find_by_name(const char *name
)
863 struct ltt_session
*iter
;
866 ASSERT_LOCKED(ltt_session_list
.lock
);
868 DBG2("Trying to find session by name %s", name
);
870 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
871 if (!strncmp(iter
->name
, name
, NAME_MAX
) &&
879 return session_get(iter
) ? iter
: NULL
;
883 * Return an ltt_session that matches the id. If no session is found,
884 * NULL is returned. This must be called with rcu_read_lock and
885 * session list lock held (to guarantee the lifetime of the session).
887 struct ltt_session
*session_find_by_id(uint64_t id
)
889 struct lttng_ht_node_u64
*node
;
890 struct lttng_ht_iter iter
;
891 struct ltt_session
*ls
;
893 ASSERT_LOCKED(ltt_session_list
.lock
);
895 if (!ltt_sessions_ht_by_id
) {
899 lttng_ht_lookup(ltt_sessions_ht_by_id
, &id
, &iter
);
900 node
= lttng_ht_iter_get_node_u64(&iter
);
904 ls
= caa_container_of(node
, struct ltt_session
, node
);
906 DBG3("Session %" PRIu64
" found by id.", id
);
907 return session_get(ls
) ? ls
: NULL
;
910 DBG3("Session %" PRIu64
" NOT found by id", id
);
915 * Create a new session and add it to the session list.
916 * Session list lock must be held by the caller.
918 enum lttng_error_code
session_create(const char *name
, uid_t uid
, gid_t gid
,
919 struct ltt_session
**out_session
)
922 enum lttng_error_code ret_code
;
923 struct ltt_session
*new_session
= NULL
;
925 ASSERT_LOCKED(ltt_session_list
.lock
);
927 struct ltt_session
*clashing_session
;
929 clashing_session
= session_find_by_name(name
);
930 if (clashing_session
) {
931 session_put(clashing_session
);
932 ret_code
= LTTNG_ERR_EXIST_SESS
;
936 new_session
= zmalloc(sizeof(struct ltt_session
));
938 PERROR("Failed to allocate an ltt_session structure");
939 ret_code
= LTTNG_ERR_NOMEM
;
943 urcu_ref_init(&new_session
->ref
);
944 pthread_mutex_init(&new_session
->lock
, NULL
);
946 new_session
->creation_time
= time(NULL
);
947 if (new_session
->creation_time
== (time_t) -1) {
948 PERROR("Failed to sample session creation time");
949 ret_code
= LTTNG_ERR_SESSION_FAIL
;
953 /* Create default consumer output. */
954 new_session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
955 if (new_session
->consumer
== NULL
) {
956 ret_code
= LTTNG_ERR_NOMEM
;
961 ret
= lttng_strncpy(new_session
->name
, name
, sizeof(new_session
->name
));
963 ret_code
= LTTNG_ERR_SESSION_INVALID_CHAR
;
966 ret
= validate_name(name
);
968 ret_code
= LTTNG_ERR_SESSION_INVALID_CHAR
;
973 bool found_name
= false;
977 timeinfo
= localtime(&new_session
->creation_time
);
979 ret_code
= LTTNG_ERR_SESSION_FAIL
;
982 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
983 for (i
= 0; i
< INT_MAX
; i
++) {
984 struct ltt_session
*clashing_session
;
987 ret
= snprintf(new_session
->name
,
988 sizeof(new_session
->name
),
990 DEFAULT_SESSION_NAME
,
993 ret
= snprintf(new_session
->name
,
994 sizeof(new_session
->name
),
996 DEFAULT_SESSION_NAME
, i
,
999 if (ret
== -1 || ret
>= sizeof(new_session
->name
)) {
1001 * Null-terminate in case the name is used
1002 * in logging statements.
1004 new_session
->name
[sizeof(new_session
->name
) - 1] = '\0';
1005 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1010 session_find_by_name(new_session
->name
);
1011 session_put(clashing_session
);
1012 if (!clashing_session
) {
1018 DBG("Generated session name \"%s\"", new_session
->name
);
1019 new_session
->has_auto_generated_name
= true;
1021 ERR("Failed to auto-generate a session name");
1022 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1027 ret
= gethostname(new_session
->hostname
, sizeof(new_session
->hostname
));
1029 if (errno
== ENAMETOOLONG
) {
1030 new_session
->hostname
[sizeof(new_session
->hostname
) - 1] = '\0';
1031 ERR("Hostname exceeds the maximal permitted length and has been truncated to %s",
1032 new_session
->hostname
);
1034 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1039 new_session
->uid
= uid
;
1040 new_session
->gid
= gid
;
1042 ret
= snapshot_init(&new_session
->snapshot
);
1044 ret_code
= LTTNG_ERR_NOMEM
;
1048 new_session
->rotation_state
= LTTNG_ROTATION_STATE_NO_ROTATION
;
1050 /* Add new session to the session list. */
1051 new_session
->id
= add_session_list(new_session
);
1054 * Add the new session to the ltt_sessions_ht_by_id.
1055 * No ownership is taken by the hash table; it is merely
1056 * a wrapper around the session list used for faster access
1059 add_session_ht(new_session
);
1060 new_session
->published
= true;
1063 * Consumer is left to NULL since the create_session_uri command will
1064 * set it up and, if valid, assign it to the session.
1066 DBG("Tracing session %s created with ID %" PRIu64
" by uid = %d, gid = %d",
1067 new_session
->name
, new_session
->id
, new_session
->uid
,
1069 ret_code
= LTTNG_OK
;
1072 (void) session_get(new_session
);
1073 *out_session
= new_session
;
1077 session_put(new_session
);
1083 * Check if the UID or GID match the session. Root user has access to all
1086 int session_access_ok(struct ltt_session
*session
, uid_t uid
, gid_t gid
)
1090 if (uid
!= session
->uid
&& gid
!= session
->gid
&& uid
!= 0) {
1098 * Set a session's rotation state and reset all associated state.
1100 * This function resets the rotation state (check timers, pending
1101 * flags, etc.) and sets the result of the last rotation. The result
1102 * can be queries by a liblttng-ctl client.
1104 * Be careful of the result passed to this function. For instance,
1105 * on failure to launch a rotation, a client will expect the rotation
1106 * state to be set to "NO_ROTATION". If an error occured while the
1107 * rotation was "ONGOING", result should be set to "ERROR", which will
1108 * allow a client to report it.
1110 * Must be called with the session and session_list locks held.
1112 int session_reset_rotation_state(struct ltt_session
*session
,
1113 enum lttng_rotation_state result
)
1117 ASSERT_LOCKED(ltt_session_list
.lock
);
1118 ASSERT_LOCKED(session
->lock
);
1120 session
->rotation_state
= result
;
1121 if (session
->rotation_pending_check_timer_enabled
) {
1122 ret
= timer_session_rotation_pending_check_stop(session
);
1124 if (session
->chunk_being_archived
) {
1126 enum lttng_trace_chunk_status chunk_status
;
1128 chunk_status
= lttng_trace_chunk_get_id(
1129 session
->chunk_being_archived
,
1131 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1132 LTTNG_OPTIONAL_SET(&session
->last_archived_chunk_id
,
1134 lttng_trace_chunk_put(session
->chunk_being_archived
);
1135 session
->chunk_being_archived
= NULL
;