2 * Copyright (C) 2013 Julien Desfossez <jdesfossez@efficios.com>
3 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
4 * Copyright (C) 2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
11 #include "ctf-trace.hpp"
12 #include "lttng-relayd.hpp"
13 #include "session.hpp"
14 #include "sessiond-trace-chunks.hpp"
18 #include <common/common.hpp>
19 #include <common/compat/path.hpp>
20 #include <common/defaults.hpp>
21 #include <common/fd-tracker/utils.hpp>
22 #include <common/time.hpp>
23 #include <common/urcu.hpp>
24 #include <common/utils.hpp>
25 #include <common/uuid.hpp>
28 #include <urcu/rculist.h>
30 /* Global session id used in the session creation. */
31 static uint64_t last_relay_session_id
;
32 static pthread_mutex_t last_relay_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
34 static int init_session_output_path_group_by_host(struct relay_session
*session
)
40 * hostname/session_name
44 char *session_directory
= nullptr;
47 if (session
->output_path
[0] != '\0') {
51 * If base path is set, it overrides the session name for the
52 * session relative base path. No timestamp is appended if the
53 * base path is overridden.
55 * If the session name already contains the creation time (e.g.
56 * auto-<timestamp>, don't append yet another timestamp after
57 * the session name in the generated path.
59 * Otherwise, generate the path with session_name-<timestamp>.
61 if (session
->base_path
[0] != '\0') {
62 ret
= asprintf(&session_directory
, "%s/%s", session
->hostname
, session
->base_path
);
63 } else if (session
->session_name_contains_creation_time
) {
65 &session_directory
, "%s/%s", session
->hostname
, session
->session_name
);
67 char session_creation_datetime
[DATETIME_STR_LEN
];
69 ret
= time_to_datetime_str(LTTNG_OPTIONAL_GET(session
->creation_time
),
70 session_creation_datetime
,
71 sizeof(session_creation_datetime
));
73 ERR("Failed to format session creation timestamp while initializing session output directory handle");
78 ret
= asprintf(&session_directory
,
81 session
->session_name
,
82 session_creation_datetime
);
85 PERROR("Failed to format session directory name");
89 if (strlen(session_directory
) >= LTTNG_PATH_MAX
) {
90 ERR("Session output directory exceeds maximal length");
94 strcpy(session
->output_path
, session_directory
);
98 free(session_directory
);
102 static int init_session_output_path_group_by_session(struct relay_session
*session
)
107 * session_name/hostname-creation_time/base_path
109 * For session name including the datetime, use it as the complete name
110 * since. Do not perform modification on it since the datetime is an
111 * integral part of the name and how a user identify a session.
114 char *session_directory
= nullptr;
115 char creation_datetime
[DATETIME_STR_LEN
];
117 if (session
->output_path
[0] != '\0') {
118 /* output_path as been generated already */
122 ret
= time_to_datetime_str(LTTNG_OPTIONAL_GET(session
->creation_time
),
124 sizeof(creation_datetime
));
126 ERR("Failed to format session creation timestamp while initializing session output directory handle");
131 ret
= asprintf(&session_directory
,
133 session
->session_name
,
136 session
->base_path
[0] != '\0' ? "/" : "",
139 PERROR("Failed to format session directory name");
143 if (strlen(session_directory
) >= LTTNG_PATH_MAX
) {
144 ERR("Session output directory exceeds maximal length");
149 strcpy(session
->output_path
, session_directory
);
153 free(session_directory
);
157 static int init_session_output_path(struct relay_session
*session
)
161 switch (opt_group_output_by
) {
162 case RELAYD_GROUP_OUTPUT_BY_HOST
:
163 ret
= init_session_output_path_group_by_host(session
);
165 case RELAYD_GROUP_OUTPUT_BY_SESSION
:
166 ret
= init_session_output_path_group_by_session(session
);
168 case RELAYD_GROUP_OUTPUT_BY_UNKNOWN
:
177 static struct lttng_directory_handle
*
178 session_create_output_directory_handle(struct relay_session
*session
)
182 * relayd_output_path/session_directory
183 * e.g. /home/user/lttng-traces/hostname/session_name
185 char *full_session_path
= nullptr;
186 struct lttng_directory_handle
*handle
= nullptr;
188 pthread_mutex_lock(&session
->lock
);
189 full_session_path
= create_output_path(session
->output_path
);
190 if (!full_session_path
) {
194 ret
= utils_mkdir_recursive(full_session_path
, S_IRWXU
| S_IRWXG
, -1, -1);
196 ERR("Failed to create session output path \"%s\"", full_session_path
);
200 handle
= fd_tracker_create_directory_handle(the_fd_tracker
, full_session_path
);
202 pthread_mutex_unlock(&session
->lock
);
203 free(full_session_path
);
207 static int session_set_anonymous_chunk(struct relay_session
*session
)
210 struct lttng_trace_chunk
*chunk
= nullptr;
211 enum lttng_trace_chunk_status status
;
212 struct lttng_directory_handle
*output_directory
;
214 output_directory
= session_create_output_directory_handle(session
);
215 if (!output_directory
) {
219 chunk
= lttng_trace_chunk_create_anonymous();
224 lttng_trace_chunk_set_fd_tracker(chunk
, the_fd_tracker
);
225 status
= lttng_trace_chunk_set_credentials_current_user(chunk
);
226 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
231 status
= lttng_trace_chunk_set_as_owner(chunk
, output_directory
);
232 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
237 session
->current_trace_chunk
= chunk
;
240 lttng_trace_chunk_put(chunk
);
241 lttng_directory_handle_put(output_directory
);
246 * Check if a name is safe to use in a path.
248 * A name that is deemed "path-safe":
249 * - Does not contains a path separator (/ or \, platform dependant),
250 * - Does not start with a '.' (hidden file/folder),
253 static bool is_name_path_safe(const char *name
)
255 const size_t name_len
= strlen(name
);
259 WARN("An empty name is not allowed to be used in a path");
262 /* Does not start with '.'. */
263 if (name
[0] == '.') {
264 WARN("Name \"%s\" is not allowed to be used in a path since it starts with '.'",
268 /* Does not contain a path-separator. */
269 if (strchr(name
, LTTNG_PATH_SEPARATOR
)) {
270 WARN("Name \"%s\" is not allowed to be used in a path since it contains a path separator",
279 * Create a new session by assigning a new session ID.
281 * Return allocated session or else NULL.
283 struct relay_session
*session_create(const char *session_name
,
284 const char *hostname
,
285 const char *base_path
,
288 const lttng_uuid
& sessiond_uuid
,
289 const uint64_t *id_sessiond
,
290 const uint64_t *current_chunk_id
,
291 const time_t *creation_time
,
294 bool session_name_contains_creation_time
)
297 struct relay_session
*session
= nullptr;
299 LTTNG_ASSERT(session_name
);
300 LTTNG_ASSERT(hostname
);
301 LTTNG_ASSERT(base_path
);
303 if (!is_name_path_safe(session_name
)) {
304 ERR("Refusing to create session as the provided session name is not path-safe");
307 if (!is_name_path_safe(hostname
)) {
308 ERR("Refusing to create session as the provided hostname is not path-safe");
311 if (strstr(base_path
, "../")) {
312 ERR("Invalid session base path walks up the path hierarchy: \"%s\"", base_path
);
316 session
= zmalloc
<relay_session
>();
318 PERROR("Failed to allocate session");
322 pthread_mutex_lock(&last_relay_session_id_lock
);
323 session
->id
= ++last_relay_session_id
;
324 pthread_mutex_unlock(&last_relay_session_id_lock
);
326 lttng_ht_node_init_u64(&session
->session_n
, session
->id
);
327 urcu_ref_init(&session
->ref
);
328 CDS_INIT_LIST_HEAD(&session
->recv_list
);
329 pthread_mutex_init(&session
->lock
, nullptr);
330 pthread_mutex_init(&session
->recv_list_lock
, nullptr);
332 if (lttng_strncpy(session
->session_name
, session_name
, sizeof(session
->session_name
))) {
333 WARN("Session name exceeds maximal allowed length");
336 if (lttng_strncpy(session
->hostname
, hostname
, sizeof(session
->hostname
))) {
337 WARN("Hostname exceeds maximal allowed length");
340 if (lttng_strncpy(session
->base_path
, base_path
, sizeof(session
->base_path
))) {
341 WARN("Base path exceeds maximal allowed length");
345 LTTNG_OPTIONAL_SET(&session
->creation_time
, *creation_time
);
347 LTTNG_OPTIONAL_SET(&session
->creation_time
, time(nullptr));
348 if (session
->creation_time
.value
== (time_t) -1) {
349 PERROR("Failed to sample session creation time");
353 session
->session_name_contains_creation_time
= session_name_contains_creation_time
;
355 session
->ctf_traces_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
356 if (!session
->ctf_traces_ht
) {
360 session
->major
= major
;
361 session
->minor
= minor
;
363 session
->live_timer
= live_timer
;
364 session
->snapshot
= snapshot
;
365 session
->sessiond_uuid
= sessiond_uuid
;
368 LTTNG_OPTIONAL_SET(&session
->id_sessiond
, *id_sessiond
);
371 if (major
== 2 && minor
>= 11) {
372 /* Only applies for 2.11+ peers using trace chunks. */
373 ret
= init_session_output_path(session
);
379 ret
= sessiond_trace_chunk_registry_session_created(sessiond_trace_chunk_registry
,
385 if (id_sessiond
&& current_chunk_id
) {
386 enum lttng_trace_chunk_status chunk_status
;
387 struct lttng_directory_handle
*session_output_directory
;
389 session
->current_trace_chunk
=
390 sessiond_trace_chunk_registry_get_chunk(sessiond_trace_chunk_registry
,
391 session
->sessiond_uuid
,
392 session
->id_sessiond
.value
,
394 if (!session
->current_trace_chunk
) {
395 char uuid_str
[LTTNG_UUID_STR_LEN
];
397 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
398 ERR("Could not find trace chunk: sessiond = {%s}, sessiond session id = %" PRIu64
399 ", trace chunk id = %" PRIu64
,
406 chunk_status
= lttng_trace_chunk_get_session_output_directory_handle(
407 session
->current_trace_chunk
, &session_output_directory
);
408 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
412 LTTNG_ASSERT(session_output_directory
);
413 session
->output_directory
= session_output_directory
;
414 } else if (!id_sessiond
) {
416 * Pre-2.11 peers will not announce trace chunks. An
417 * anonymous trace chunk which will remain set for the
418 * duration of the session is created.
420 ret
= session_set_anonymous_chunk(session
);
425 session
->output_directory
= session_create_output_directory_handle(session
);
426 if (!session
->output_directory
) {
431 lttng_ht_add_unique_u64(sessions_ht
, &session
->session_n
);
435 session_put(session
);
439 /* Should be called with RCU read-side lock held. */
440 bool session_get(struct relay_session
*session
)
442 return urcu_ref_get_unless_zero(&session
->ref
);
446 * Lookup a session within the session hash table using the session id
447 * as key. A session reference is taken when a session is returned.
448 * session_put() must be called on that session.
450 * Return session or NULL if not found.
452 struct relay_session
*session_get_by_id(uint64_t id
)
454 struct relay_session
*session
= nullptr;
455 struct lttng_ht_node_u64
*node
;
456 struct lttng_ht_iter iter
;
458 lttng::urcu::read_lock_guard read_lock
;
459 lttng_ht_lookup(sessions_ht
, &id
, &iter
);
460 node
= lttng_ht_iter_get_node
<lttng_ht_node_u64
>(&iter
);
462 DBG("Session find by ID %" PRIu64
" id NOT found", id
);
465 session
= lttng::utils::container_of(node
, &relay_session::session_n
);
466 DBG("Session find by ID %" PRIu64
" id found", id
);
467 if (!session_get(session
)) {
475 * Check if any of the relay sessions originating from the same
476 * session daemon session have the 'ongoing_rotation' state set.
478 * The caller must hold the lock of session.
480 bool session_has_ongoing_rotation(const struct relay_session
*session
)
482 bool ongoing_rotation
= false;
483 struct lttng_ht_iter iter
;
484 struct relay_session
*iterated_session
;
486 ASSERT_LOCKED(session
->lock
);
488 if (!session
->id_sessiond
.is_set
) {
490 * The peer that created this session is too old to
491 * support rotations; we can assume no rotations are ongoing.
496 if (session
->ongoing_rotation
) {
497 ongoing_rotation
= true;
502 * Sample the 'ongoing_rotation' status of all relay sessions that
503 * originate from the same session daemon session.
506 lttng::urcu::read_lock_guard read_lock
;
508 cds_lfht_for_each_entry (
509 sessions_ht
->ht
, &iter
.iter
, iterated_session
, session_n
.node
) {
510 if (!session_get(iterated_session
)) {
514 if (session
== iterated_session
) {
515 /* Skip this session. */
516 goto next_session_no_unlock
;
519 pthread_mutex_lock(&iterated_session
->lock
);
521 if (!iterated_session
->id_sessiond
.is_set
) {
523 * Session belongs to a peer that doesn't support
529 if (session
->sessiond_uuid
!= iterated_session
->sessiond_uuid
) {
530 /* Sessions do not originate from the same sessiond. */
534 if (LTTNG_OPTIONAL_GET(session
->id_sessiond
) !=
535 LTTNG_OPTIONAL_GET(iterated_session
->id_sessiond
)) {
537 * Sessions do not originate from the same sessiond
543 ongoing_rotation
= iterated_session
->ongoing_rotation
;
546 pthread_mutex_unlock(&iterated_session
->lock
);
547 next_session_no_unlock
:
548 session_put(iterated_session
);
550 if (ongoing_rotation
) {
557 return ongoing_rotation
;
560 static void rcu_destroy_session(struct rcu_head
*rcu_head
)
562 struct relay_session
*session
= caa_container_of(rcu_head
, struct relay_session
, rcu_node
);
564 * Since each trace has a reference on the session, it means
565 * that if we are at the point where we teardown the session, no
566 * trace belonging to that session exist at this point.
567 * Calling lttng_ht_destroy in call_rcu worker thread so we
568 * don't hold the RCU read-side lock while calling it.
570 lttng_ht_destroy(session
->ctf_traces_ht
);
575 * Delete session from the given hash table.
577 * Return lttng ht del error code being 0 on success and 1 on failure.
579 static int session_delete(struct relay_session
*session
)
581 struct lttng_ht_iter iter
;
583 iter
.iter
.node
= &session
->session_n
.node
;
584 return lttng_ht_del(sessions_ht
, &iter
);
587 static void destroy_session(struct relay_session
*session
)
591 ret
= session_delete(session
);
593 lttng_trace_chunk_put(session
->current_trace_chunk
);
594 session
->current_trace_chunk
= nullptr;
595 lttng_trace_chunk_put(session
->pending_closure_trace_chunk
);
596 session
->pending_closure_trace_chunk
= nullptr;
597 ret
= sessiond_trace_chunk_registry_session_destroyed(sessiond_trace_chunk_registry
,
598 session
->sessiond_uuid
);
600 lttng_directory_handle_put(session
->output_directory
);
601 session
->output_directory
= nullptr;
602 call_rcu(&session
->rcu_node
, rcu_destroy_session
);
605 static void session_release(struct urcu_ref
*ref
)
607 struct relay_session
*session
= lttng::utils::container_of(ref
, &relay_session::ref
);
609 destroy_session(session
);
612 void session_put(struct relay_session
*session
)
617 lttng::urcu::read_lock_guard read_lock
;
618 urcu_ref_put(&session
->ref
, session_release
);
621 int session_close(struct relay_session
*session
)
624 struct ctf_trace
*trace
;
625 struct lttng_ht_iter iter
;
626 struct relay_stream
*stream
;
628 pthread_mutex_lock(&session
->lock
);
629 DBG("closing session %" PRIu64
": is conn already closed %d",
631 session
->connection_closed
);
632 session
->connection_closed
= true;
633 pthread_mutex_unlock(&session
->lock
);
636 lttng::urcu::read_lock_guard read_lock
;
638 cds_lfht_for_each_entry (session
->ctf_traces_ht
->ht
, &iter
.iter
, trace
, node
.node
) {
639 ret
= ctf_trace_close(trace
);
645 cds_list_for_each_entry_rcu(stream
, &session
->recv_list
, recv_node
)
647 /* Close streams which have not been published yet. */
648 try_stream_close(stream
);
657 /* Put self-reference from create. */
658 session_put(session
);
662 int session_abort(struct relay_session
*session
)
670 pthread_mutex_lock(&session
->lock
);
671 DBG("aborting session %" PRIu64
, session
->id
);
672 session
->aborted
= true;
673 pthread_mutex_unlock(&session
->lock
);
677 void print_sessions()
679 struct lttng_ht_iter iter
;
680 struct relay_session
*session
;
687 lttng::urcu::read_lock_guard read_lock
;
689 cds_lfht_for_each_entry (sessions_ht
->ht
, &iter
.iter
, session
, session_n
.node
) {
690 if (!session_get(session
)) {
693 DBG("session %p refcount %ld session %" PRIu64
,
695 session
->ref
.refcount
,
697 session_put(session
);