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 <common/common.hpp>
12 #include <common/compat/path.hpp>
13 #include <common/fd-tracker/utils.hpp>
14 #include <common/time.hpp>
15 #include <common/utils.hpp>
16 #include <common/uuid.hpp>
17 #include <urcu/rculist.h>
21 #include "ctf-trace.hpp"
22 #include "lttng-relayd.hpp"
23 #include "session.hpp"
24 #include "sessiond-trace-chunks.hpp"
26 #include <common/defaults.hpp>
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
= NULL
;
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
,
63 } else if (session
->session_name_contains_creation_time
) {
64 ret
= asprintf(&session_directory
, "%s/%s", session
->hostname
,
65 session
->session_name
);
67 char session_creation_datetime
[DATETIME_STR_LEN
];
69 ret
= time_to_datetime_str(
70 LTTNG_OPTIONAL_GET(session
->creation_time
),
71 session_creation_datetime
,
72 sizeof(session_creation_datetime
));
74 ERR("Failed to format session creation timestamp while initializing session output directory handle");
79 ret
= asprintf(&session_directory
, "%s/%s-%s",
80 session
->hostname
, 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(
102 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
= NULL
;
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
),
123 creation_datetime
, sizeof(creation_datetime
));
125 ERR("Failed to format session creation timestamp while initializing session output directory handle");
130 ret
= asprintf(&session_directory
, "%s/%s-%s%s%s",
131 session
->session_name
, session
->hostname
,
133 session
->base_path
[0] != '\0' ? "/" : "",
136 PERROR("Failed to format session directory name");
140 if (strlen(session_directory
) >= LTTNG_PATH_MAX
) {
141 ERR("Session output directory exceeds maximal length");
146 strcpy(session
->output_path
, session_directory
);
150 free(session_directory
);
154 static int init_session_output_path(struct relay_session
*session
)
158 switch (opt_group_output_by
) {
159 case RELAYD_GROUP_OUTPUT_BY_HOST
:
160 ret
= init_session_output_path_group_by_host(session
);
162 case RELAYD_GROUP_OUTPUT_BY_SESSION
:
163 ret
= init_session_output_path_group_by_session(session
);
165 case RELAYD_GROUP_OUTPUT_BY_UNKNOWN
:
174 static struct lttng_directory_handle
*session_create_output_directory_handle(
175 struct relay_session
*session
)
179 * relayd_output_path/session_directory
180 * e.g. /home/user/lttng-traces/hostname/session_name
182 char *full_session_path
= NULL
;
183 struct lttng_directory_handle
*handle
= NULL
;
185 pthread_mutex_lock(&session
->lock
);
186 full_session_path
= create_output_path(session
->output_path
);
187 if (!full_session_path
) {
191 ret
= utils_mkdir_recursive(
192 full_session_path
, S_IRWXU
| S_IRWXG
, -1, -1);
194 ERR("Failed to create session output path \"%s\"",
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
= NULL
;
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 '.'", name
);
266 /* Does not contain a path-separator. */
267 if (strchr(name
, LTTNG_PATH_SEPARATOR
)) {
268 WARN("Name \"%s\" is not allowed to be used in a path since it contains a path separator", name
);
276 * Create a new session by assigning a new session ID.
278 * Return allocated session or else NULL.
280 struct relay_session
*session_create(const char *session_name
,
281 const char *hostname
, const char *base_path
,
284 const lttng_uuid sessiond_uuid
,
285 const uint64_t *id_sessiond
,
286 const uint64_t *current_chunk_id
,
287 const time_t *creation_time
,
290 bool session_name_contains_creation_time
)
293 struct relay_session
*session
= NULL
;
295 LTTNG_ASSERT(session_name
);
296 LTTNG_ASSERT(hostname
);
297 LTTNG_ASSERT(base_path
);
299 if (!is_name_path_safe(session_name
)) {
300 ERR("Refusing to create session as the provided session name is not path-safe");
303 if (!is_name_path_safe(hostname
)) {
304 ERR("Refusing to create session as the provided hostname is not path-safe");
307 if (strstr(base_path
, "../")) {
308 ERR("Invalid session base path walks up the path hierarchy: \"%s\"",
313 session
= (relay_session
*) zmalloc(sizeof(*session
));
315 PERROR("Failed to allocate session");
319 pthread_mutex_lock(&last_relay_session_id_lock
);
320 session
->id
= ++last_relay_session_id
;
321 pthread_mutex_unlock(&last_relay_session_id_lock
);
323 lttng_ht_node_init_u64(&session
->session_n
, session
->id
);
324 urcu_ref_init(&session
->ref
);
325 CDS_INIT_LIST_HEAD(&session
->recv_list
);
326 pthread_mutex_init(&session
->lock
, NULL
);
327 pthread_mutex_init(&session
->recv_list_lock
, NULL
);
329 if (lttng_strncpy(session
->session_name
, session_name
,
330 sizeof(session
->session_name
))) {
331 WARN("Session name exceeds maximal allowed length");
334 if (lttng_strncpy(session
->hostname
, hostname
,
335 sizeof(session
->hostname
))) {
336 WARN("Hostname exceeds maximal allowed length");
339 if (lttng_strncpy(session
->base_path
, base_path
,
340 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(NULL
));
348 if (session
->creation_time
.value
== (time_t) -1) {
349 PERROR("Failed to sample session creation time");
353 session
->session_name_contains_creation_time
=
354 session_name_contains_creation_time
;
356 session
->ctf_traces_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
357 if (!session
->ctf_traces_ht
) {
361 session
->major
= major
;
362 session
->minor
= minor
;
364 session
->live_timer
= live_timer
;
365 session
->snapshot
= snapshot
;
366 lttng_uuid_copy(session
->sessiond_uuid
, sessiond_uuid
);
369 LTTNG_OPTIONAL_SET(&session
->id_sessiond
, *id_sessiond
);
372 if (major
== 2 && minor
>= 11) {
373 /* Only applies for 2.11+ peers using trace chunks. */
374 ret
= init_session_output_path(session
);
380 ret
= sessiond_trace_chunk_registry_session_created(
381 sessiond_trace_chunk_registry
, sessiond_uuid
);
386 if (id_sessiond
&& current_chunk_id
) {
387 enum lttng_trace_chunk_status chunk_status
;
388 struct lttng_directory_handle
*session_output_directory
;
390 session
->current_trace_chunk
=
391 sessiond_trace_chunk_registry_get_chunk(
392 sessiond_trace_chunk_registry
,
393 session
->sessiond_uuid
,
394 session
->id_sessiond
.value
,
396 if (!session
->current_trace_chunk
) {
397 char uuid_str
[LTTNG_UUID_STR_LEN
];
399 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
400 ERR("Could not find trace chunk: sessiond = {%s}, sessiond session id = %" PRIu64
", trace chunk id = %" PRIu64
,
401 uuid_str
, *id_sessiond
,
406 chunk_status
= lttng_trace_chunk_get_session_output_directory_handle(
407 session
->current_trace_chunk
,
408 &session_output_directory
);
409 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
413 LTTNG_ASSERT(session_output_directory
);
414 session
->output_directory
= session_output_directory
;
415 } else if (!id_sessiond
) {
417 * Pre-2.11 peers will not announce trace chunks. An
418 * anonymous trace chunk which will remain set for the
419 * duration of the session is created.
421 ret
= session_set_anonymous_chunk(session
);
426 session
->output_directory
=
427 session_create_output_directory_handle(session
);
428 if (!session
->output_directory
) {
433 lttng_ht_add_unique_u64(sessions_ht
, &session
->session_n
);
437 session_put(session
);
441 /* Should be called with RCU read-side lock held. */
442 bool session_get(struct relay_session
*session
)
444 return urcu_ref_get_unless_zero(&session
->ref
);
448 * Lookup a session within the session hash table using the session id
449 * as key. A session reference is taken when a session is returned.
450 * session_put() must be called on that session.
452 * Return session or NULL if not found.
454 struct relay_session
*session_get_by_id(uint64_t id
)
456 struct relay_session
*session
= NULL
;
457 struct lttng_ht_node_u64
*node
;
458 struct lttng_ht_iter iter
;
461 lttng_ht_lookup(sessions_ht
, &id
, &iter
);
462 node
= lttng_ht_iter_get_node_u64(&iter
);
464 DBG("Session find by ID %" PRIu64
" id NOT found", id
);
467 session
= caa_container_of(node
, struct relay_session
, session_n
);
468 DBG("Session find by ID %" PRIu64
" id found", id
);
469 if (!session_get(session
)) {
478 * Check if any of the relay sessions originating from the same
479 * session daemon session have the 'ongoing_rotation' state set.
481 * The caller must hold the lock of session.
483 bool session_has_ongoing_rotation(const struct relay_session
*session
)
485 bool ongoing_rotation
= false;
486 struct lttng_ht_iter iter
;
487 struct relay_session
*iterated_session
;
489 ASSERT_LOCKED(session
->lock
);
491 if (!session
->id_sessiond
.is_set
) {
493 * The peer that created this session is too old to
494 * support rotations; we can assume no rotations are ongoing.
499 if (session
->ongoing_rotation
) {
500 ongoing_rotation
= true;
506 * Sample the 'ongoing_rotation' status of all relay sessions that
507 * originate from the same session daemon session.
509 cds_lfht_for_each_entry(sessions_ht
->ht
, &iter
.iter
, iterated_session
,
511 if (!session_get(iterated_session
)) {
515 if (session
== iterated_session
) {
516 /* Skip this session. */
517 goto next_session_no_unlock
;
520 pthread_mutex_lock(&iterated_session
->lock
);
522 if (!iterated_session
->id_sessiond
.is_set
) {
524 * Session belongs to a peer that doesn't support
530 if (!lttng_uuid_is_equal(session
->sessiond_uuid
,
531 iterated_session
->sessiond_uuid
)) {
532 /* Sessions do not originate from the same sessiond. */
536 if (LTTNG_OPTIONAL_GET(session
->id_sessiond
) !=
537 LTTNG_OPTIONAL_GET(iterated_session
->id_sessiond
)) {
539 * Sessions do not originate from the same sessiond
545 ongoing_rotation
= iterated_session
->ongoing_rotation
;
548 pthread_mutex_unlock(&iterated_session
->lock
);
549 next_session_no_unlock
:
550 session_put(iterated_session
);
552 if (ongoing_rotation
) {
559 return ongoing_rotation
;
562 static void rcu_destroy_session(struct rcu_head
*rcu_head
)
564 struct relay_session
*session
=
565 caa_container_of(rcu_head
, struct relay_session
,
568 * Since each trace has a reference on the session, it means
569 * that if we are at the point where we teardown the session, no
570 * trace belonging to that session exist at this point.
571 * Calling lttng_ht_destroy in call_rcu worker thread so we
572 * don't hold the RCU read-side lock while calling it.
574 lttng_ht_destroy(session
->ctf_traces_ht
);
579 * Delete session from the given hash table.
581 * Return lttng ht del error code being 0 on success and 1 on failure.
583 static int session_delete(struct relay_session
*session
)
585 struct lttng_ht_iter iter
;
587 iter
.iter
.node
= &session
->session_n
.node
;
588 return lttng_ht_del(sessions_ht
, &iter
);
592 static void destroy_session(struct relay_session
*session
)
596 ret
= session_delete(session
);
598 lttng_trace_chunk_put(session
->current_trace_chunk
);
599 session
->current_trace_chunk
= NULL
;
600 lttng_trace_chunk_put(session
->pending_closure_trace_chunk
);
601 session
->pending_closure_trace_chunk
= NULL
;
602 ret
= sessiond_trace_chunk_registry_session_destroyed(
603 sessiond_trace_chunk_registry
, session
->sessiond_uuid
);
605 lttng_directory_handle_put(session
->output_directory
);
606 session
->output_directory
= NULL
;
607 call_rcu(&session
->rcu_node
, rcu_destroy_session
);
610 static void session_release(struct urcu_ref
*ref
)
612 struct relay_session
*session
=
613 caa_container_of(ref
, struct relay_session
, ref
);
615 destroy_session(session
);
618 void session_put(struct relay_session
*session
)
624 urcu_ref_put(&session
->ref
, session_release
);
628 int session_close(struct relay_session
*session
)
631 struct ctf_trace
*trace
;
632 struct lttng_ht_iter iter
;
633 struct relay_stream
*stream
;
635 pthread_mutex_lock(&session
->lock
);
636 DBG("closing session %" PRIu64
": is conn already closed %d",
637 session
->id
, session
->connection_closed
);
638 session
->connection_closed
= true;
639 pthread_mutex_unlock(&session
->lock
);
642 cds_lfht_for_each_entry(session
->ctf_traces_ht
->ht
,
643 &iter
.iter
, trace
, node
.node
) {
644 ret
= ctf_trace_close(trace
);
649 cds_list_for_each_entry_rcu(stream
, &session
->recv_list
,
651 /* Close streams which have not been published yet. */
652 try_stream_close(stream
);
659 /* Put self-reference from create. */
660 session_put(session
);
664 int session_abort(struct relay_session
*session
)
672 pthread_mutex_lock(&session
->lock
);
673 DBG("aborting session %" PRIu64
, session
->id
);
674 session
->aborted
= true;
675 pthread_mutex_unlock(&session
->lock
);
679 void print_sessions(void)
681 struct lttng_ht_iter iter
;
682 struct relay_session
*session
;
689 cds_lfht_for_each_entry(sessions_ht
->ht
, &iter
.iter
, session
,
691 if (!session_get(session
)) {
694 DBG("session %p refcount %ld session %" PRIu64
,
696 session
->ref
.refcount
,
698 session_put(session
);