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
;
70 struct consumer_create_chunk_transaction
{
71 struct consumer_socket
*socket
;
72 struct lttng_trace_chunk
*new_chunk
;
73 struct lttng_trace_chunk
*previous_chunk
;
74 bool new_chunk_created
;
78 * Validate the session name for forbidden characters.
80 * Return 0 on success else -1 meaning a forbidden char. has been found.
82 static int validate_name(const char *name
)
89 tmp_name
= strdup(name
);
96 tok
= strpbrk(tmp_name
, forbidden_name_chars
);
98 DBG("Session name %s contains a forbidden character", name
);
99 /* Forbidden character has been found. */
111 * Add a ltt_session structure to the global list.
113 * The caller MUST acquire the session list lock before.
114 * Returns the unique identifier for the session.
116 static uint64_t add_session_list(struct ltt_session
*ls
)
120 cds_list_add(&ls
->list
, <t_session_list
.head
);
121 return ltt_session_list
.next_uuid
++;
125 * Delete a ltt_session structure to the global list.
127 * The caller MUST acquire the session list lock before.
129 static void del_session_list(struct ltt_session
*ls
)
133 cds_list_del(&ls
->list
);
137 * Return a pointer to the session list.
139 struct ltt_session_list
*session_get_list(void)
141 return <t_session_list
;
145 * Returns once the session list is empty.
147 void session_list_wait_empty(void)
149 pthread_mutex_lock(<t_session_list
.lock
);
150 while (!cds_list_empty(<t_session_list
.head
)) {
151 pthread_cond_wait(<t_session_list
.removal_cond
,
152 <t_session_list
.lock
);
154 pthread_mutex_unlock(<t_session_list
.lock
);
158 * Acquire session list lock
160 void session_lock_list(void)
162 pthread_mutex_lock(<t_session_list
.lock
);
166 * Try to acquire session list lock
168 int session_trylock_list(void)
170 return pthread_mutex_trylock(<t_session_list
.lock
);
174 * Release session list lock
176 void session_unlock_list(void)
178 pthread_mutex_unlock(<t_session_list
.lock
);
182 * Get the session's consumer destination type.
184 * The caller must hold the session lock.
186 enum consumer_dst_type
session_get_consumer_destination_type(
187 const struct ltt_session
*session
)
190 * The output information is duplicated in both of those session types.
191 * Hence, it doesn't matter from which it is retrieved. However, it is
192 * possible for only one of them to be set.
194 return session
->kernel_session
?
195 session
->kernel_session
->consumer
->type
:
196 session
->ust_session
->consumer
->type
;
200 * Get the session's consumer network hostname.
201 * The caller must ensure that the destination is of type "net".
203 * The caller must hold the session lock.
205 const char *session_get_net_consumer_hostname(const struct ltt_session
*session
)
207 const char *hostname
= NULL
;
208 const struct consumer_output
*output
;
210 output
= session
->kernel_session
?
211 session
->kernel_session
->consumer
:
212 session
->ust_session
->consumer
;
215 * hostname is assumed to be the same for both control and data
218 switch (output
->dst
.net
.control
.dtype
) {
220 hostname
= output
->dst
.net
.control
.dst
.ipv4
;
223 hostname
= output
->dst
.net
.control
.dst
.ipv6
;
232 * Get the session's consumer network control and data ports.
233 * The caller must ensure that the destination is of type "net".
235 * The caller must hold the session lock.
237 void session_get_net_consumer_ports(const struct ltt_session
*session
,
238 uint16_t *control_port
, uint16_t *data_port
)
240 const struct consumer_output
*output
;
242 output
= session
->kernel_session
?
243 session
->kernel_session
->consumer
:
244 session
->ust_session
->consumer
;
245 *control_port
= output
->dst
.net
.control
.port
;
246 *data_port
= output
->dst
.net
.data
.port
;
250 * Get the location of the latest trace archive produced by a rotation.
252 * The caller must hold the session lock.
254 struct lttng_trace_archive_location
*session_get_trace_archive_location(
255 struct ltt_session
*session
)
257 struct lttng_trace_archive_location
*location
= NULL
;
259 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
263 switch (session_get_consumer_destination_type(session
)) {
264 case CONSUMER_DST_LOCAL
:
265 location
= lttng_trace_archive_location_local_create(
266 session
->rotation_chunk
.current_rotate_path
);
268 case CONSUMER_DST_NET
:
270 const char *hostname
;
271 uint16_t control_port
, data_port
;
273 hostname
= session_get_net_consumer_hostname(session
);
274 session_get_net_consumer_ports(session
,
277 location
= lttng_trace_archive_location_relay_create(
279 LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
,
280 control_port
, data_port
,
281 session
->rotation_chunk
.current_rotate_path
);
292 * Allocate the ltt_sessions_ht_by_id HT.
294 * The session list lock must be held.
296 int ltt_sessions_ht_alloc(void)
300 DBG("Allocating ltt_sessions_ht_by_id");
301 ltt_sessions_ht_by_id
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
302 if (!ltt_sessions_ht_by_id
) {
304 ERR("Failed to allocate ltt_sessions_ht_by_id");
312 * Destroy the ltt_sessions_ht_by_id HT.
314 * The session list lock must be held.
316 static void ltt_sessions_ht_destroy(void)
318 if (!ltt_sessions_ht_by_id
) {
321 ht_cleanup_push(ltt_sessions_ht_by_id
);
322 ltt_sessions_ht_by_id
= NULL
;
326 * Add a ltt_session to the ltt_sessions_ht_by_id.
327 * If unallocated, the ltt_sessions_ht_by_id HT is allocated.
328 * The session list lock must be held.
330 static void add_session_ht(struct ltt_session
*ls
)
336 if (!ltt_sessions_ht_by_id
) {
337 ret
= ltt_sessions_ht_alloc();
339 ERR("Error allocating the sessions HT");
343 lttng_ht_node_init_u64(&ls
->node
, ls
->id
);
344 lttng_ht_add_unique_u64(ltt_sessions_ht_by_id
, &ls
->node
);
351 * Test if ltt_sessions_ht_by_id is empty.
352 * Return 1 if empty, 0 if not empty.
353 * The session list lock must be held.
355 static int ltt_sessions_ht_empty(void)
359 if (!ltt_sessions_ht_by_id
) {
364 ret
= lttng_ht_get_count(ltt_sessions_ht_by_id
) ? 0 : 1;
370 * Remove a ltt_session from the ltt_sessions_ht_by_id.
371 * If empty, the ltt_sessions_ht_by_id HT is freed.
372 * The session list lock must be held.
374 static void del_session_ht(struct ltt_session
*ls
)
376 struct lttng_ht_iter iter
;
380 assert(ltt_sessions_ht_by_id
);
382 iter
.iter
.node
= &ls
->node
.node
;
383 ret
= lttng_ht_del(ltt_sessions_ht_by_id
, &iter
);
386 if (ltt_sessions_ht_empty()) {
387 DBG("Empty ltt_sessions_ht_by_id, destroying it");
388 ltt_sessions_ht_destroy();
393 * Acquire session lock
395 void session_lock(struct ltt_session
*session
)
399 pthread_mutex_lock(&session
->lock
);
403 * Release session lock
405 void session_unlock(struct ltt_session
*session
)
409 pthread_mutex_unlock(&session
->lock
);
413 int _session_set_trace_chunk_no_lock_check(struct ltt_session
*session
,
414 struct lttng_trace_chunk
*new_trace_chunk
)
417 unsigned int i
, refs_to_acquire
= 0, refs_acquired
= 0, refs_to_release
= 0;
418 unsigned int consumer_count
= 0;
420 * The maximum amount of consumers to reach is 3
421 * (32/64 userspace + kernel).
423 struct consumer_create_chunk_transaction transactions
[3] = {};
424 struct cds_lfht_iter iter
;
425 struct consumer_socket
*socket
;
426 bool close_error_occured
= false;
428 if (new_trace_chunk
) {
430 enum lttng_trace_chunk_status chunk_status
=
431 lttng_trace_chunk_get_id(new_trace_chunk
,
434 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
435 LTTNG_OPTIONAL_SET(&session
->last_trace_chunk_id
, chunk_id
)
438 if (new_trace_chunk
) {
440 refs_to_acquire
+= !!session
->ust_session
;
441 refs_to_acquire
+= !!session
->kernel_session
;
445 * Build a list of consumers to reach to announce the new trace chunk.
447 * Rolling back the annoucement in case of an error is important since
448 * not doing so would result in a leak; the chunk will not be
449 * "reclaimed" by the consumer(s) since they have no concept of the
450 * lifetime of a session.
452 if (session
->ust_session
) {
453 cds_lfht_for_each_entry(
454 session
->ust_session
->consumer
->socks
->ht
,
455 &iter
, socket
, node
.node
) {
456 transactions
[consumer_count
].socket
= socket
;
457 transactions
[consumer_count
].new_chunk
= new_trace_chunk
;
458 transactions
[consumer_count
].previous_chunk
=
459 session
->current_trace_chunk
;
461 assert(consumer_count
<= 3);
464 if (session
->kernel_session
) {
465 cds_lfht_for_each_entry(
466 session
->kernel_session
->consumer
->socks
->ht
,
467 &iter
, socket
, node
.node
) {
468 transactions
[consumer_count
].socket
= socket
;
469 transactions
[consumer_count
].new_chunk
= new_trace_chunk
;
470 transactions
[consumer_count
].previous_chunk
=
471 session
->current_trace_chunk
;
473 assert(consumer_count
<= 3);
476 for (refs_acquired
= 0; refs_acquired
< refs_to_acquire
; refs_acquired
++) {
477 if (new_trace_chunk
&& !lttng_trace_chunk_get(new_trace_chunk
)) {
478 ERR("Failed to acquire reference to new current trace chunk of session \"%s\"",
485 * Close the previous chunk on remote peers (consumers and relayd).
487 for (i
= 0; i
< consumer_count
; i
++) {
488 if (!transactions
[i
].previous_chunk
) {
491 pthread_mutex_lock(transactions
[i
].socket
->lock
);
492 ret
= consumer_close_trace_chunk(transactions
[i
].socket
,
493 session
->consumer
->net_seq_index
,
495 transactions
[i
].previous_chunk
);
496 pthread_mutex_unlock(transactions
[i
].socket
->lock
);
498 ERR("Failed to close trace chunk on consumer");
499 close_error_occured
= true;
503 if (close_error_occured
) {
505 * Skip the creation of the new trace chunk and report the
511 /* Create the new chunk on remote peers (consumers and relayd) */
512 if (new_trace_chunk
) {
513 for (i
= 0; i
< consumer_count
; i
++) {
514 pthread_mutex_lock(transactions
[i
].socket
->lock
);
515 ret
= consumer_create_trace_chunk(transactions
[i
].socket
,
516 session
->consumer
->net_seq_index
,
518 transactions
[i
].new_chunk
);
519 pthread_mutex_unlock(transactions
[i
].socket
->lock
);
521 ERR("Failed to create trace chunk on consumer");
524 /* This will have to be rolled-back on error. */
525 transactions
[i
].new_chunk_created
= true;
529 lttng_trace_chunk_put(session
->current_trace_chunk
);
530 session
->current_trace_chunk
= NULL
;
531 if (session
->ust_session
) {
532 lttng_trace_chunk_put(
533 session
->ust_session
->current_trace_chunk
);
534 session
->ust_session
->current_trace_chunk
= NULL
;
536 if (session
->kernel_session
) {
537 lttng_trace_chunk_put(
538 session
->kernel_session
->current_trace_chunk
);
539 session
->kernel_session
->current_trace_chunk
= NULL
;
543 * Update local current trace chunk state last, only if all remote
544 * annoucements succeeded.
546 session
->current_trace_chunk
= new_trace_chunk
;
547 if (session
->ust_session
) {
548 session
->ust_session
->current_trace_chunk
= new_trace_chunk
;
550 if (session
->kernel_session
) {
551 session
->kernel_session
->current_trace_chunk
=
558 * Release references taken in the case where all references could not
561 refs_to_release
= refs_to_acquire
- refs_acquired
;
562 for (i
= 0; i
< refs_to_release
; i
++) {
563 lttng_trace_chunk_put(new_trace_chunk
);
567 * Close the newly-created chunk from remote peers (consumers and
570 DBG("Rolling back the creation of the new trace chunk on consumers");
571 for (i
= 0; i
< consumer_count
; i
++) {
572 if (!transactions
[i
].new_chunk_created
) {
576 pthread_mutex_lock(transactions
[i
].socket
->lock
);
577 ret
= consumer_close_trace_chunk(transactions
[i
].socket
,
578 session
->consumer
->net_seq_index
,
580 transactions
[i
].new_chunk
);
581 pthread_mutex_unlock(transactions
[i
].socket
->lock
);
583 ERR("Failed to close trace chunk on consumer");
584 close_error_occured
= true;
592 bool output_supports_chunks(const struct ltt_session
*session
)
594 if (session
->consumer
->type
== CONSUMER_DST_LOCAL
) {
597 struct consumer_output
*output
;
599 if (session
->ust_session
) {
600 output
= session
->ust_session
->consumer
;
601 } else if (session
->kernel_session
) {
602 output
= session
->kernel_session
->consumer
;
607 if (output
->relay_major_version
> 2) {
609 } else if (output
->relay_major_version
== 2 &&
610 output
->relay_minor_version
>= 11) {
617 enum lttng_error_code
session_switch_trace_chunk(struct ltt_session
*session
,
618 const char *session_base_path_override
,
619 const char *chunk_name_override
)
622 enum lttng_error_code ret_code
= LTTNG_OK
;
623 struct lttng_trace_chunk
*trace_chunk
= NULL
;
624 enum lttng_trace_chunk_status chunk_status
;
625 const time_t timestamp_begin
= time(NULL
);
626 const bool is_local_trace
=
627 session
->consumer
->type
== CONSUMER_DST_LOCAL
;
628 const char *base_path
= session_base_path_override
? :
629 session_get_base_path(session
);
630 struct lttng_directory_handle session_output_directory
;
631 const struct lttng_credentials session_credentials
= {
635 uint64_t next_chunk_id
;
637 if (timestamp_begin
== (time_t) -1) {
638 PERROR("Failed to sample time while changing session \"%s\" trace chunk",
640 ret_code
= LTTNG_ERR_FATAL
;
643 session
->current_chunk_start_ts
= timestamp_begin
;
645 if (!output_supports_chunks(session
)) {
648 next_chunk_id
= session
->last_trace_chunk_id
.is_set
?
649 session
->last_trace_chunk_id
.value
+ 1 : 0;
651 trace_chunk
= lttng_trace_chunk_create(next_chunk_id
, timestamp_begin
);
653 ret_code
= LTTNG_ERR_FATAL
;
657 if (chunk_name_override
) {
658 chunk_status
= lttng_trace_chunk_override_name(trace_chunk
,
659 chunk_name_override
);
660 switch (chunk_status
) {
661 case LTTNG_TRACE_CHUNK_STATUS_OK
:
663 case LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT
:
664 ret_code
= LTTNG_ERR_INVALID
;
667 ret_code
= LTTNG_ERR_NOMEM
;
672 if (!is_local_trace
) {
674 * No need to set crendentials and output directory
675 * for remote trace chunks.
680 chunk_status
= lttng_trace_chunk_set_credentials(trace_chunk
,
681 &session_credentials
);
682 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
683 ret_code
= LTTNG_ERR_FATAL
;
687 if (!session
->current_trace_chunk
) {
688 DBG("Creating base output directory of session \"%s\" at %s",
689 session
->name
, base_path
);
691 ret
= utils_mkdir_recursive(base_path
, S_IRWXU
| S_IRWXG
,
692 session
->uid
, session
->gid
);
694 ret
= LTTNG_ERR_FATAL
;
697 ret
= lttng_directory_handle_init(&session_output_directory
,
700 ret
= LTTNG_ERR_FATAL
;
703 chunk_status
= lttng_trace_chunk_set_as_owner(trace_chunk
,
704 &session_output_directory
);
705 lttng_directory_handle_fini(&session_output_directory
);
706 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
707 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
711 ret
= session_set_trace_chunk(session
, trace_chunk
);
713 ret_code
= LTTNG_ERR_FATAL
;
717 lttng_trace_chunk_put(trace_chunk
);
723 * Set a session's current trace chunk.
725 * Must be called with the session lock held.
727 int session_set_trace_chunk(struct ltt_session
*session
,
728 struct lttng_trace_chunk
*new_trace_chunk
)
730 ASSERT_LOCKED(session
->lock
);
731 return _session_set_trace_chunk_no_lock_check(session
, new_trace_chunk
);
735 void session_release(struct urcu_ref
*ref
)
738 struct ltt_ust_session
*usess
;
739 struct ltt_kernel_session
*ksess
;
740 struct ltt_session
*session
= container_of(ref
, typeof(*session
), ref
);
742 usess
= session
->ust_session
;
743 ksess
= session
->kernel_session
;
744 (void) _session_set_trace_chunk_no_lock_check(session
, NULL
);
746 /* Clean kernel session teardown */
747 kernel_destroy_session(ksess
);
748 session
->kernel_session
= NULL
;
750 /* UST session teardown */
752 /* Close any relayd session */
753 consumer_output_send_destroy_relayd(usess
->consumer
);
755 /* Destroy every UST application related to this session. */
756 ret
= ust_app_destroy_trace_all(usess
);
758 ERR("Error in ust_app_destroy_trace_all");
761 /* Clean up the rest. */
762 trace_ust_destroy_session(usess
);
763 session
->ust_session
= NULL
;
767 * Must notify the kernel thread here to update it's poll set in order to
768 * remove the channel(s)' fd just destroyed.
770 ret
= notify_thread_pipe(kernel_poll_pipe
[1]);
772 PERROR("write kernel poll pipe");
775 DBG("Destroying session %s (id %" PRIu64
")", session
->name
, session
->id
);
777 consumer_output_put(session
->consumer
);
778 snapshot_destroy(&session
->snapshot
);
780 pthread_mutex_destroy(&session
->lock
);
782 if (session
->published
) {
783 ASSERT_LOCKED(ltt_session_list
.lock
);
784 del_session_list(session
);
785 del_session_ht(session
);
786 pthread_cond_broadcast(<t_session_list
.removal_cond
);
792 * Acquire a reference to a session.
793 * This function may fail (return false); its return value must be checked.
795 bool session_get(struct ltt_session
*session
)
797 return urcu_ref_get_unless_zero(&session
->ref
);
801 * Release a reference to a session.
803 void session_put(struct ltt_session
*session
)
809 * The session list lock must be held as any session_put()
810 * may cause the removal of the session from the session_list.
812 ASSERT_LOCKED(ltt_session_list
.lock
);
813 assert(session
->ref
.refcount
);
814 urcu_ref_put(&session
->ref
, session_release
);
820 * This method does not immediately release/free the session as other
821 * components may still hold a reference to the session. However,
822 * the session should no longer be presented to the user.
824 * Releases the session list's reference to the session
825 * and marks it as destroyed. Iterations on the session list should be
826 * mindful of the "destroyed" flag.
828 void session_destroy(struct ltt_session
*session
)
830 assert(!session
->destroyed
);
831 session
->destroyed
= true;
832 session_put(session
);
836 * Return a ltt_session structure ptr that matches name. If no session found,
837 * NULL is returned. This must be called with the session list lock held using
838 * session_lock_list and session_unlock_list.
839 * A reference to the session is implicitly acquired by this function.
841 struct ltt_session
*session_find_by_name(const char *name
)
843 struct ltt_session
*iter
;
846 ASSERT_LOCKED(ltt_session_list
.lock
);
848 DBG2("Trying to find session by name %s", name
);
850 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
851 if (!strncmp(iter
->name
, name
, NAME_MAX
) &&
859 return session_get(iter
) ? iter
: NULL
;
863 * Return an ltt_session that matches the id. If no session is found,
864 * NULL is returned. This must be called with rcu_read_lock and
865 * session list lock held (to guarantee the lifetime of the session).
867 struct ltt_session
*session_find_by_id(uint64_t id
)
869 struct lttng_ht_node_u64
*node
;
870 struct lttng_ht_iter iter
;
871 struct ltt_session
*ls
;
873 ASSERT_LOCKED(ltt_session_list
.lock
);
875 if (!ltt_sessions_ht_by_id
) {
879 lttng_ht_lookup(ltt_sessions_ht_by_id
, &id
, &iter
);
880 node
= lttng_ht_iter_get_node_u64(&iter
);
884 ls
= caa_container_of(node
, struct ltt_session
, node
);
886 DBG3("Session %" PRIu64
" found by id.", id
);
887 return session_get(ls
) ? ls
: NULL
;
890 DBG3("Session %" PRIu64
" NOT found by id", id
);
895 * Create a new session and add it to the session list.
896 * Session list lock must be held by the caller.
898 enum lttng_error_code
session_create(const char *name
, uid_t uid
, gid_t gid
,
899 struct ltt_session
**out_session
)
902 enum lttng_error_code ret_code
;
903 struct ltt_session
*new_session
= NULL
;
905 ASSERT_LOCKED(ltt_session_list
.lock
);
907 struct ltt_session
*clashing_session
;
909 clashing_session
= session_find_by_name(name
);
910 if (clashing_session
) {
911 session_put(clashing_session
);
912 ret_code
= LTTNG_ERR_EXIST_SESS
;
916 new_session
= zmalloc(sizeof(struct ltt_session
));
918 PERROR("Failed to allocate an ltt_session structure");
919 ret_code
= LTTNG_ERR_NOMEM
;
923 urcu_ref_init(&new_session
->ref
);
924 pthread_mutex_init(&new_session
->lock
, NULL
);
926 new_session
->creation_time
= time(NULL
);
927 if (new_session
->creation_time
== (time_t) -1) {
928 PERROR("Failed to sample session creation time");
929 ret_code
= LTTNG_ERR_SESSION_FAIL
;
933 /* Create default consumer output. */
934 new_session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
935 if (new_session
->consumer
== NULL
) {
936 ret_code
= LTTNG_ERR_NOMEM
;
941 ret
= lttng_strncpy(new_session
->name
, name
, sizeof(new_session
->name
));
943 ret_code
= LTTNG_ERR_SESSION_INVALID_CHAR
;
946 ret
= validate_name(name
);
948 ret_code
= LTTNG_ERR_SESSION_INVALID_CHAR
;
953 bool found_name
= false;
957 timeinfo
= localtime(&new_session
->creation_time
);
959 ret_code
= LTTNG_ERR_SESSION_FAIL
;
962 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
963 for (i
= 0; i
< INT_MAX
; i
++) {
964 struct ltt_session
*clashing_session
;
967 ret
= snprintf(new_session
->name
,
968 sizeof(new_session
->name
),
970 DEFAULT_SESSION_NAME
,
973 ret
= snprintf(new_session
->name
,
974 sizeof(new_session
->name
),
976 DEFAULT_SESSION_NAME
, i
,
979 if (ret
== -1 || ret
>= sizeof(new_session
->name
)) {
981 * Null-terminate in case the name is used
982 * in logging statements.
984 new_session
->name
[sizeof(new_session
->name
) - 1] = '\0';
985 ret_code
= LTTNG_ERR_SESSION_FAIL
;
990 session_find_by_name(new_session
->name
);
991 session_put(clashing_session
);
992 if (!clashing_session
) {
998 DBG("Generated session name \"%s\"", new_session
->name
);
999 new_session
->has_auto_generated_name
= true;
1001 ERR("Failed to auto-generate a session name");
1002 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1007 ret
= gethostname(new_session
->hostname
, sizeof(new_session
->hostname
));
1009 if (errno
== ENAMETOOLONG
) {
1010 new_session
->hostname
[sizeof(new_session
->hostname
) - 1] = '\0';
1011 ERR("Hostname exceeds the maximal permitted length and has been truncated to %s",
1012 new_session
->hostname
);
1014 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1019 new_session
->uid
= uid
;
1020 new_session
->gid
= gid
;
1022 ret
= snapshot_init(&new_session
->snapshot
);
1024 ret_code
= LTTNG_ERR_NOMEM
;
1028 new_session
->rotation_state
= LTTNG_ROTATION_STATE_NO_ROTATION
;
1030 /* Add new session to the session list. */
1031 new_session
->id
= add_session_list(new_session
);
1034 * Add the new session to the ltt_sessions_ht_by_id.
1035 * No ownership is taken by the hash table; it is merely
1036 * a wrapper around the session list used for faster access
1039 add_session_ht(new_session
);
1040 new_session
->published
= true;
1043 * Consumer is left to NULL since the create_session_uri command will
1044 * set it up and, if valid, assign it to the session.
1046 DBG("Tracing session %s created with ID %" PRIu64
" by uid = %d, gid = %d",
1047 new_session
->name
, new_session
->id
, new_session
->uid
,
1049 ret_code
= LTTNG_OK
;
1052 (void) session_get(new_session
);
1053 *out_session
= new_session
;
1057 session_put(new_session
);
1063 * Check if the UID or GID match the session. Root user has access to all
1066 int session_access_ok(struct ltt_session
*session
, uid_t uid
, gid_t gid
)
1070 if (uid
!= session
->uid
&& gid
!= session
->gid
&& uid
!= 0) {
1078 * Set a session's rotation state and reset all associated state.
1080 * This function resets the rotation state (check timers, pending
1081 * flags, etc.) and sets the result of the last rotation. The result
1082 * can be queries by a liblttng-ctl client.
1084 * Be careful of the result passed to this function. For instance,
1085 * on failure to launch a rotation, a client will expect the rotation
1086 * state to be set to "NO_ROTATION". If an error occured while the
1087 * rotation was "ONGOING", result should be set to "ERROR", which will
1088 * allow a client to report it.
1090 * Must be called with the session and session_list locks held.
1092 int session_reset_rotation_state(struct ltt_session
*session
,
1093 enum lttng_rotation_state result
)
1097 ASSERT_LOCKED(ltt_session_list
.lock
);
1098 ASSERT_LOCKED(session
->lock
);
1100 session
->rotation_pending_local
= false;
1101 session
->rotation_pending_relay
= false;
1102 session
->rotated_after_last_stop
= false;
1103 session
->rotation_state
= result
;
1104 if (session
->rotation_pending_check_timer_enabled
) {
1105 ret
= timer_session_rotation_pending_check_stop(session
);