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/utils.hpp>
24 #include <common/uuid.hpp>
27 #include <urcu/rculist.h>
29 /* Global session id used in the session creation. */
30 static uint64_t last_relay_session_id
;
31 static pthread_mutex_t last_relay_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
33 static int init_session_output_path_group_by_host(struct relay_session
*session
)
39 * hostname/session_name
43 char *session_directory
= nullptr;
46 if (session
->output_path
[0] != '\0') {
50 * If base path is set, it overrides the session name for the
51 * session relative base path. No timestamp is appended if the
52 * base path is overridden.
54 * If the session name already contains the creation time (e.g.
55 * auto-<timestamp>, don't append yet another timestamp after
56 * the session name in the generated path.
58 * Otherwise, generate the path with session_name-<timestamp>.
60 if (session
->base_path
[0] != '\0') {
61 ret
= asprintf(&session_directory
, "%s/%s", session
->hostname
, session
->base_path
);
62 } else if (session
->session_name_contains_creation_time
) {
64 &session_directory
, "%s/%s", session
->hostname
, session
->session_name
);
66 char session_creation_datetime
[DATETIME_STR_LEN
];
68 ret
= time_to_datetime_str(LTTNG_OPTIONAL_GET(session
->creation_time
),
69 session_creation_datetime
,
70 sizeof(session_creation_datetime
));
72 ERR("Failed to format session creation timestamp while initializing session output directory handle");
77 ret
= asprintf(&session_directory
,
80 session
->session_name
,
81 session_creation_datetime
);
84 PERROR("Failed to format session directory name");
88 if (strlen(session_directory
) >= LTTNG_PATH_MAX
) {
89 ERR("Session output directory exceeds maximal length");
93 strcpy(session
->output_path
, session_directory
);
97 free(session_directory
);
101 static int init_session_output_path_group_by_session(struct relay_session
*session
)
106 * session_name/hostname-creation_time/base_path
108 * For session name including the datetime, use it as the complete name
109 * since. Do not perform modification on it since the datetime is an
110 * integral part of the name and how a user identify a session.
113 char *session_directory
= nullptr;
114 char creation_datetime
[DATETIME_STR_LEN
];
116 if (session
->output_path
[0] != '\0') {
117 /* output_path as been generated already */
121 ret
= time_to_datetime_str(LTTNG_OPTIONAL_GET(session
->creation_time
),
123 sizeof(creation_datetime
));
125 ERR("Failed to format session creation timestamp while initializing session output directory handle");
130 ret
= asprintf(&session_directory
,
132 session
->session_name
,
135 session
->base_path
[0] != '\0' ? "/" : "",
138 PERROR("Failed to format session directory name");
142 if (strlen(session_directory
) >= LTTNG_PATH_MAX
) {
143 ERR("Session output directory exceeds maximal length");
148 strcpy(session
->output_path
, session_directory
);
152 free(session_directory
);
156 static int init_session_output_path(struct relay_session
*session
)
160 switch (opt_group_output_by
) {
161 case RELAYD_GROUP_OUTPUT_BY_HOST
:
162 ret
= init_session_output_path_group_by_host(session
);
164 case RELAYD_GROUP_OUTPUT_BY_SESSION
:
165 ret
= init_session_output_path_group_by_session(session
);
167 case RELAYD_GROUP_OUTPUT_BY_UNKNOWN
:
176 static struct lttng_directory_handle
*
177 session_create_output_directory_handle(struct relay_session
*session
)
181 * relayd_output_path/session_directory
182 * e.g. /home/user/lttng-traces/hostname/session_name
184 char *full_session_path
= nullptr;
185 struct lttng_directory_handle
*handle
= nullptr;
187 pthread_mutex_lock(&session
->lock
);
188 full_session_path
= create_output_path(session
->output_path
);
189 if (!full_session_path
) {
193 ret
= utils_mkdir_recursive(full_session_path
, S_IRWXU
| S_IRWXG
, -1, -1);
195 ERR("Failed to create session output path \"%s\"", full_session_path
);
199 handle
= fd_tracker_create_directory_handle(the_fd_tracker
, full_session_path
);
201 pthread_mutex_unlock(&session
->lock
);
202 free(full_session_path
);
206 static int session_set_anonymous_chunk(struct relay_session
*session
)
209 struct lttng_trace_chunk
*chunk
= nullptr;
210 enum lttng_trace_chunk_status status
;
211 struct lttng_directory_handle
*output_directory
;
213 output_directory
= session_create_output_directory_handle(session
);
214 if (!output_directory
) {
218 chunk
= lttng_trace_chunk_create_anonymous();
223 lttng_trace_chunk_set_fd_tracker(chunk
, the_fd_tracker
);
224 status
= lttng_trace_chunk_set_credentials_current_user(chunk
);
225 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
230 status
= lttng_trace_chunk_set_as_owner(chunk
, output_directory
);
231 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
236 session
->current_trace_chunk
= chunk
;
239 lttng_trace_chunk_put(chunk
);
240 lttng_directory_handle_put(output_directory
);
245 * Check if a name is safe to use in a path.
247 * A name that is deemed "path-safe":
248 * - Does not contains a path separator (/ or \, platform dependant),
249 * - Does not start with a '.' (hidden file/folder),
252 static bool is_name_path_safe(const char *name
)
254 const size_t name_len
= strlen(name
);
258 WARN("An empty name is not allowed to be used in a path");
261 /* Does not start with '.'. */
262 if (name
[0] == '.') {
263 WARN("Name \"%s\" is not allowed to be used in a path since it starts with '.'",
267 /* Does not contain a path-separator. */
268 if (strchr(name
, LTTNG_PATH_SEPARATOR
)) {
269 WARN("Name \"%s\" is not allowed to be used in a path since it contains a path separator",
278 * Create a new session by assigning a new session ID.
280 * Return allocated session or else NULL.
282 struct relay_session
*session_create(const char *session_name
,
283 const char *hostname
,
284 const char *base_path
,
287 const lttng_uuid
& sessiond_uuid
,
288 const uint64_t *id_sessiond
,
289 const uint64_t *current_chunk_id
,
290 const time_t *creation_time
,
293 bool session_name_contains_creation_time
)
296 struct relay_session
*session
= nullptr;
298 LTTNG_ASSERT(session_name
);
299 LTTNG_ASSERT(hostname
);
300 LTTNG_ASSERT(base_path
);
302 if (!is_name_path_safe(session_name
)) {
303 ERR("Refusing to create session as the provided session name is not path-safe");
306 if (!is_name_path_safe(hostname
)) {
307 ERR("Refusing to create session as the provided hostname is not path-safe");
310 if (strstr(base_path
, "../")) {
311 ERR("Invalid session base path walks up the path hierarchy: \"%s\"", base_path
);
315 session
= zmalloc
<relay_session
>();
317 PERROR("Failed to allocate session");
321 pthread_mutex_lock(&last_relay_session_id_lock
);
322 session
->id
= ++last_relay_session_id
;
323 pthread_mutex_unlock(&last_relay_session_id_lock
);
325 lttng_ht_node_init_u64(&session
->session_n
, session
->id
);
326 urcu_ref_init(&session
->ref
);
327 CDS_INIT_LIST_HEAD(&session
->recv_list
);
328 pthread_mutex_init(&session
->lock
, nullptr);
329 pthread_mutex_init(&session
->recv_list_lock
, nullptr);
331 if (lttng_strncpy(session
->session_name
, session_name
, sizeof(session
->session_name
))) {
332 WARN("Session name exceeds maximal allowed length");
335 if (lttng_strncpy(session
->hostname
, hostname
, sizeof(session
->hostname
))) {
336 WARN("Hostname exceeds maximal allowed length");
339 if (lttng_strncpy(session
->base_path
, base_path
, sizeof(session
->base_path
))) {
340 WARN("Base path exceeds maximal allowed length");
344 LTTNG_OPTIONAL_SET(&session
->creation_time
, *creation_time
);
346 LTTNG_OPTIONAL_SET(&session
->creation_time
, time(nullptr));
347 if (session
->creation_time
.value
== (time_t) -1) {
348 PERROR("Failed to sample session creation time");
352 session
->session_name_contains_creation_time
= session_name_contains_creation_time
;
354 session
->ctf_traces_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
355 if (!session
->ctf_traces_ht
) {
359 session
->major
= major
;
360 session
->minor
= minor
;
362 session
->live_timer
= live_timer
;
363 session
->snapshot
= snapshot
;
364 session
->sessiond_uuid
= sessiond_uuid
;
367 LTTNG_OPTIONAL_SET(&session
->id_sessiond
, *id_sessiond
);
370 if (major
== 2 && minor
>= 11) {
371 /* Only applies for 2.11+ peers using trace chunks. */
372 ret
= init_session_output_path(session
);
378 ret
= sessiond_trace_chunk_registry_session_created(sessiond_trace_chunk_registry
,
384 if (id_sessiond
&& current_chunk_id
) {
385 enum lttng_trace_chunk_status chunk_status
;
386 struct lttng_directory_handle
*session_output_directory
;
388 session
->current_trace_chunk
=
389 sessiond_trace_chunk_registry_get_chunk(sessiond_trace_chunk_registry
,
390 session
->sessiond_uuid
,
391 session
->id_sessiond
.value
,
393 if (!session
->current_trace_chunk
) {
394 char uuid_str
[LTTNG_UUID_STR_LEN
];
396 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
397 ERR("Could not find trace chunk: sessiond = {%s}, sessiond session id = %" PRIu64
398 ", trace chunk id = %" PRIu64
,
405 chunk_status
= lttng_trace_chunk_get_session_output_directory_handle(
406 session
->current_trace_chunk
, &session_output_directory
);
407 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
411 LTTNG_ASSERT(session_output_directory
);
412 session
->output_directory
= session_output_directory
;
413 } else if (!id_sessiond
) {
415 * Pre-2.11 peers will not announce trace chunks. An
416 * anonymous trace chunk which will remain set for the
417 * duration of the session is created.
419 ret
= session_set_anonymous_chunk(session
);
424 session
->output_directory
= session_create_output_directory_handle(session
);
425 if (!session
->output_directory
) {
430 lttng_ht_add_unique_u64(sessions_ht
, &session
->session_n
);
434 session_put(session
);
438 /* Should be called with RCU read-side lock held. */
439 bool session_get(struct relay_session
*session
)
441 return urcu_ref_get_unless_zero(&session
->ref
);
445 * Lookup a session within the session hash table using the session id
446 * as key. A session reference is taken when a session is returned.
447 * session_put() must be called on that session.
449 * Return session or NULL if not found.
451 struct relay_session
*session_get_by_id(uint64_t id
)
453 struct relay_session
*session
= nullptr;
454 struct lttng_ht_node_u64
*node
;
455 struct lttng_ht_iter iter
;
458 lttng_ht_lookup(sessions_ht
, &id
, &iter
);
459 node
= lttng_ht_iter_get_node_u64(&iter
);
461 DBG("Session find by ID %" PRIu64
" id NOT found", id
);
464 session
= lttng::utils::container_of(node
, &relay_session::session_n
);
465 DBG("Session find by ID %" PRIu64
" id found", id
);
466 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;
503 * Sample the 'ongoing_rotation' status of all relay sessions that
504 * originate from the same session daemon session.
506 cds_lfht_for_each_entry (sessions_ht
->ht
, &iter
.iter
, iterated_session
, session_n
.node
) {
507 if (!session_get(iterated_session
)) {
511 if (session
== iterated_session
) {
512 /* Skip this session. */
513 goto next_session_no_unlock
;
516 pthread_mutex_lock(&iterated_session
->lock
);
518 if (!iterated_session
->id_sessiond
.is_set
) {
520 * Session belongs to a peer that doesn't support
526 if (session
->sessiond_uuid
!= iterated_session
->sessiond_uuid
) {
527 /* Sessions do not originate from the same sessiond. */
531 if (LTTNG_OPTIONAL_GET(session
->id_sessiond
) !=
532 LTTNG_OPTIONAL_GET(iterated_session
->id_sessiond
)) {
534 * Sessions do not originate from the same sessiond
540 ongoing_rotation
= iterated_session
->ongoing_rotation
;
543 pthread_mutex_unlock(&iterated_session
->lock
);
544 next_session_no_unlock
:
545 session_put(iterated_session
);
547 if (ongoing_rotation
) {
554 return ongoing_rotation
;
557 static void rcu_destroy_session(struct rcu_head
*rcu_head
)
559 struct relay_session
*session
= caa_container_of(rcu_head
, struct relay_session
, rcu_node
);
561 * Since each trace has a reference on the session, it means
562 * that if we are at the point where we teardown the session, no
563 * trace belonging to that session exist at this point.
564 * Calling lttng_ht_destroy in call_rcu worker thread so we
565 * don't hold the RCU read-side lock while calling it.
567 lttng_ht_destroy(session
->ctf_traces_ht
);
572 * Delete session from the given hash table.
574 * Return lttng ht del error code being 0 on success and 1 on failure.
576 static int session_delete(struct relay_session
*session
)
578 struct lttng_ht_iter iter
;
580 iter
.iter
.node
= &session
->session_n
.node
;
581 return lttng_ht_del(sessions_ht
, &iter
);
584 static void destroy_session(struct relay_session
*session
)
588 ret
= session_delete(session
);
590 lttng_trace_chunk_put(session
->current_trace_chunk
);
591 session
->current_trace_chunk
= nullptr;
592 lttng_trace_chunk_put(session
->pending_closure_trace_chunk
);
593 session
->pending_closure_trace_chunk
= nullptr;
594 ret
= sessiond_trace_chunk_registry_session_destroyed(sessiond_trace_chunk_registry
,
595 session
->sessiond_uuid
);
597 lttng_directory_handle_put(session
->output_directory
);
598 session
->output_directory
= nullptr;
599 call_rcu(&session
->rcu_node
, rcu_destroy_session
);
602 static void session_release(struct urcu_ref
*ref
)
604 struct relay_session
*session
= lttng::utils::container_of(ref
, &relay_session::ref
);
606 destroy_session(session
);
609 void session_put(struct relay_session
*session
)
615 urcu_ref_put(&session
->ref
, session_release
);
619 int session_close(struct relay_session
*session
)
622 struct ctf_trace
*trace
;
623 struct lttng_ht_iter iter
;
624 struct relay_stream
*stream
;
626 pthread_mutex_lock(&session
->lock
);
627 DBG("closing session %" PRIu64
": is conn already closed %d",
629 session
->connection_closed
);
630 session
->connection_closed
= true;
631 pthread_mutex_unlock(&session
->lock
);
634 cds_lfht_for_each_entry (session
->ctf_traces_ht
->ht
, &iter
.iter
, trace
, node
.node
) {
635 ret
= ctf_trace_close(trace
);
640 cds_list_for_each_entry_rcu(stream
, &session
->recv_list
, recv_node
)
642 /* Close streams which have not been published yet. */
643 try_stream_close(stream
);
650 /* Put self-reference from create. */
651 session_put(session
);
655 int session_abort(struct relay_session
*session
)
663 pthread_mutex_lock(&session
->lock
);
664 DBG("aborting session %" PRIu64
, session
->id
);
665 session
->aborted
= true;
666 pthread_mutex_unlock(&session
->lock
);
670 void print_sessions()
672 struct lttng_ht_iter iter
;
673 struct relay_session
*session
;
680 cds_lfht_for_each_entry (sessions_ht
->ht
, &iter
.iter
, session
, session_n
.node
) {
681 if (!session_get(session
)) {
684 DBG("session %p refcount %ld session %" PRIu64
,
686 session
->ref
.refcount
,
688 session_put(session
);