2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License, version 2 only, as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <common/common.h>
22 #include <common/uuid.h>
23 #include <common/time.h>
24 #include <common/utils.h>
25 #include <common/uuid.h>
26 #include <common/compat/path.h>
27 #include <urcu/rculist.h>
31 #include "ctf-trace.h"
32 #include "lttng-relayd.h"
34 #include "sessiond-trace-chunks.h"
36 #include <common/defaults.h>
39 /* Global session id used in the session creation. */
40 static uint64_t last_relay_session_id
;
41 static pthread_mutex_t last_relay_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
43 static int init_session_output_path_group_by_host(struct relay_session
*session
)
49 * hostname/session_name
53 char *session_directory
= NULL
;
56 if (session
->output_path
[0] != '\0') {
60 * If base path is set, it overrides the session name for the
61 * session relative base path. No timestamp is appended if the
62 * base path is overridden.
64 * If the session name already contains the creation time (e.g.
65 * auto-<timestamp>, don't append yet another timestamp after
66 * the session name in the generated path.
68 * Otherwise, generate the path with session_name-<timestamp>.
70 if (session
->base_path
[0] != '\0') {
71 ret
= asprintf(&session_directory
, "%s/%s", session
->hostname
,
73 } else if (session
->session_name_contains_creation_time
) {
74 ret
= asprintf(&session_directory
, "%s/%s", session
->hostname
,
75 session
->session_name
);
77 char session_creation_datetime
[DATETIME_STR_LEN
];
79 ret
= time_to_datetime_str(
80 LTTNG_OPTIONAL_GET(session
->creation_time
),
81 session_creation_datetime
,
82 sizeof(session_creation_datetime
));
84 ERR("Failed to format session creation timestamp while initializing session output directory handle");
89 ret
= asprintf(&session_directory
, "%s/%s-%s",
90 session
->hostname
, session
->session_name
,
91 session_creation_datetime
);
94 PERROR("Failed to format session directory name");
98 if (strlen(session_directory
) >= LTTNG_PATH_MAX
) {
99 ERR("Session output directory exceeds maximal length");
103 strcpy(session
->output_path
, session_directory
);
107 free(session_directory
);
111 static int init_session_output_path_group_by_session(
112 struct relay_session
*session
)
117 * session_name/hostname-creation_time/base_path
119 * For session name including the datetime, use it as the complete name
120 * since. Do not perform modification on it since the datetime is an
121 * integral part of the name and how a user identify a session.
124 char *session_directory
= NULL
;
125 char creation_datetime
[DATETIME_STR_LEN
];
127 if (session
->output_path
[0] != '\0') {
128 /* output_path as been generated already */
132 ret
= time_to_datetime_str(LTTNG_OPTIONAL_GET(session
->creation_time
),
133 creation_datetime
, sizeof(creation_datetime
));
135 ERR("Failed to format session creation timestamp while initializing session output directory handle");
140 ret
= asprintf(&session_directory
, "%s/%s-%s%s%s",
141 session
->session_name
, session
->hostname
,
143 session
->base_path
[0] != '\0' ? "/" : "",
146 PERROR("Failed to format session directory name");
150 if (strlen(session_directory
) >= LTTNG_PATH_MAX
) {
151 ERR("Session output directory exceeds maximal length");
156 strcpy(session
->output_path
, session_directory
);
160 free(session_directory
);
164 static int init_session_output_path(struct relay_session
*session
)
168 switch (opt_group_output_by
) {
169 case RELAYD_GROUP_OUTPUT_BY_HOST
:
170 ret
= init_session_output_path_group_by_host(session
);
172 case RELAYD_GROUP_OUTPUT_BY_SESSION
:
173 ret
= init_session_output_path_group_by_session(session
);
175 case RELAYD_GROUP_OUTPUT_BY_UNKNOWN
:
184 static struct lttng_directory_handle
*session_create_output_directory_handle(
185 struct relay_session
*session
)
189 * relayd_output_path/session_directory
190 * e.g. /home/user/lttng-traces/hostname/session_name
192 char *full_session_path
= NULL
;
193 struct lttng_directory_handle
*handle
= NULL
;
195 pthread_mutex_lock(&session
->lock
);
196 full_session_path
= create_output_path(session
->output_path
);
197 if (!full_session_path
) {
201 ret
= utils_mkdir_recursive(
202 full_session_path
, S_IRWXU
| S_IRWXG
, -1, -1);
204 ERR("Failed to create session output path \"%s\"",
209 handle
= lttng_directory_handle_create(full_session_path
);
211 pthread_mutex_unlock(&session
->lock
);
212 free(full_session_path
);
216 static int session_set_anonymous_chunk(struct relay_session
*session
)
219 struct lttng_trace_chunk
*chunk
= NULL
;
220 enum lttng_trace_chunk_status status
;
221 struct lttng_directory_handle
*output_directory
;
223 output_directory
= session_create_output_directory_handle(session
);
224 if (!output_directory
) {
228 chunk
= lttng_trace_chunk_create_anonymous();
233 status
= lttng_trace_chunk_set_credentials_current_user(chunk
);
234 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
239 status
= lttng_trace_chunk_set_as_owner(chunk
, output_directory
);
240 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
244 output_directory
= NULL
;
245 session
->current_trace_chunk
= chunk
;
248 lttng_trace_chunk_put(chunk
);
249 lttng_directory_handle_put(output_directory
);
254 * Check if a name is safe to use in a path.
256 * A name that is deemed "path-safe":
257 * - Does not contains a path separator (/ or \, platform dependant),
258 * - Does not start with a '.' (hidden file/folder),
261 static bool is_name_path_safe(const char *name
)
263 const size_t name_len
= strlen(name
);
267 WARN("An empty name is not allowed to be used in a path");
270 /* Does not start with '.'. */
271 if (name
[0] == '.') {
272 WARN("Name \"%s\" is not allowed to be used in a path since it starts with '.'", name
);
275 /* Does not contain a path-separator. */
276 if (strchr(name
, LTTNG_PATH_SEPARATOR
)) {
277 WARN("Name \"%s\" is not allowed to be used in a path since it contains a path separator", name
);
285 * Create a new session by assigning a new session ID.
287 * Return allocated session or else NULL.
289 struct relay_session
*session_create(const char *session_name
,
290 const char *hostname
, const char *base_path
,
293 const lttng_uuid sessiond_uuid
,
294 const uint64_t *id_sessiond
,
295 const uint64_t *current_chunk_id
,
296 const time_t *creation_time
,
299 bool session_name_contains_creation_time
)
302 struct relay_session
*session
= NULL
;
304 assert(session_name
);
308 if (!is_name_path_safe(session_name
)) {
309 ERR("Refusing to create session as the provided session name is not path-safe");
312 if (!is_name_path_safe(hostname
)) {
313 ERR("Refusing to create session as the provided hostname is not path-safe");
316 if (strstr(base_path
, "../")) {
317 ERR("Invalid session base path walks up the path hierarchy: \"%s\"",
322 session
= zmalloc(sizeof(*session
));
324 PERROR("Failed to allocate session");
327 if (lttng_strncpy(session
->session_name
, session_name
,
328 sizeof(session
->session_name
))) {
329 WARN("Session name exceeds maximal allowed length");
332 if (lttng_strncpy(session
->hostname
, hostname
,
333 sizeof(session
->hostname
))) {
334 WARN("Hostname exceeds maximal allowed length");
337 if (lttng_strncpy(session
->base_path
, base_path
,
338 sizeof(session
->base_path
))) {
339 WARN("Base path exceeds maximal allowed length");
343 LTTNG_OPTIONAL_SET(&session
->creation_time
, *creation_time
);
345 session
->session_name_contains_creation_time
=
346 session_name_contains_creation_time
;
348 session
->ctf_traces_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
349 if (!session
->ctf_traces_ht
) {
353 pthread_mutex_lock(&last_relay_session_id_lock
);
354 session
->id
= ++last_relay_session_id
;
355 pthread_mutex_unlock(&last_relay_session_id_lock
);
357 session
->major
= major
;
358 session
->minor
= minor
;
359 lttng_ht_node_init_u64(&session
->session_n
, session
->id
);
360 urcu_ref_init(&session
->ref
);
361 CDS_INIT_LIST_HEAD(&session
->recv_list
);
362 pthread_mutex_init(&session
->lock
, NULL
);
363 pthread_mutex_init(&session
->recv_list_lock
, NULL
);
365 session
->live_timer
= live_timer
;
366 session
->snapshot
= snapshot
;
367 lttng_uuid_copy(session
->sessiond_uuid
, sessiond_uuid
);
370 LTTNG_OPTIONAL_SET(&session
->id_sessiond
, *id_sessiond
);
373 if (major
== 2 && minor
>= 11) {
374 /* Only applies for 2.11+ peers using trace chunks. */
375 ret
= init_session_output_path(session
);
381 ret
= sessiond_trace_chunk_registry_session_created(
382 sessiond_trace_chunk_registry
, sessiond_uuid
);
387 if (id_sessiond
&& current_chunk_id
) {
388 enum lttng_trace_chunk_status chunk_status
;
389 struct lttng_directory_handle
*session_output_directory
;
391 session
->current_trace_chunk
=
392 sessiond_trace_chunk_registry_get_chunk(
393 sessiond_trace_chunk_registry
,
394 session
->sessiond_uuid
,
395 session
->id_sessiond
.value
,
397 if (!session
->current_trace_chunk
) {
398 char uuid_str
[LTTNG_UUID_STR_LEN
];
400 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
401 ERR("Could not find trace chunk: sessiond = {%s}, sessiond session id = %" PRIu64
", trace chunk id = %" PRIu64
,
402 uuid_str
, *id_sessiond
,
407 chunk_status
= lttng_trace_chunk_get_session_output_directory_handle(
408 session
->current_trace_chunk
,
409 &session_output_directory
);
410 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
414 assert(session_output_directory
);
415 session
->output_directory
= session_output_directory
;
416 } else if (!id_sessiond
) {
418 * Pre-2.11 peers will not announce trace chunks. An
419 * anonymous trace chunk which will remain set for the
420 * duration of the session is created.
422 ret
= session_set_anonymous_chunk(session
);
427 session
->output_directory
=
428 session_create_output_directory_handle(session
);
429 if (!session
->output_directory
) {
434 lttng_ht_add_unique_u64(sessions_ht
, &session
->session_n
);
438 session_put(session
);
442 /* Should be called with RCU read-side lock held. */
443 bool session_get(struct relay_session
*session
)
445 return urcu_ref_get_unless_zero(&session
->ref
);
449 * Lookup a session within the session hash table using the session id
450 * as key. A session reference is taken when a session is returned.
451 * session_put() must be called on that session.
453 * Return session or NULL if not found.
455 struct relay_session
*session_get_by_id(uint64_t id
)
457 struct relay_session
*session
= NULL
;
458 struct lttng_ht_node_u64
*node
;
459 struct lttng_ht_iter iter
;
462 lttng_ht_lookup(sessions_ht
, &id
, &iter
);
463 node
= lttng_ht_iter_get_node_u64(&iter
);
465 DBG("Session find by ID %" PRIu64
" id NOT found", id
);
468 session
= caa_container_of(node
, struct relay_session
, session_n
);
469 DBG("Session find by ID %" PRIu64
" id found", id
);
470 if (!session_get(session
)) {
478 static void rcu_destroy_session(struct rcu_head
*rcu_head
)
480 struct relay_session
*session
=
481 caa_container_of(rcu_head
, struct relay_session
,
484 * Since each trace has a reference on the session, it means
485 * that if we are at the point where we teardown the session, no
486 * trace belonging to that session exist at this point.
487 * Calling lttng_ht_destroy in call_rcu worker thread so we
488 * don't hold the RCU read-side lock while calling it.
490 lttng_ht_destroy(session
->ctf_traces_ht
);
495 * Delete session from the given hash table.
497 * Return lttng ht del error code being 0 on success and 1 on failure.
499 static int session_delete(struct relay_session
*session
)
501 struct lttng_ht_iter iter
;
503 iter
.iter
.node
= &session
->session_n
.node
;
504 return lttng_ht_del(sessions_ht
, &iter
);
508 static void destroy_session(struct relay_session
*session
)
512 ret
= session_delete(session
);
514 lttng_trace_chunk_put(session
->current_trace_chunk
);
515 session
->current_trace_chunk
= NULL
;
516 lttng_trace_chunk_put(session
->pending_closure_trace_chunk
);
517 session
->pending_closure_trace_chunk
= NULL
;
518 ret
= sessiond_trace_chunk_registry_session_destroyed(
519 sessiond_trace_chunk_registry
, session
->sessiond_uuid
);
521 lttng_directory_handle_put(session
->output_directory
);
522 session
->output_directory
= NULL
;
523 call_rcu(&session
->rcu_node
, rcu_destroy_session
);
526 static void session_release(struct urcu_ref
*ref
)
528 struct relay_session
*session
=
529 caa_container_of(ref
, struct relay_session
, ref
);
531 destroy_session(session
);
534 void session_put(struct relay_session
*session
)
540 urcu_ref_put(&session
->ref
, session_release
);
544 int session_close(struct relay_session
*session
)
547 struct ctf_trace
*trace
;
548 struct lttng_ht_iter iter
;
549 struct relay_stream
*stream
;
551 pthread_mutex_lock(&session
->lock
);
552 DBG("closing session %" PRIu64
": is conn already closed %d",
553 session
->id
, session
->connection_closed
);
554 session
->connection_closed
= true;
555 pthread_mutex_unlock(&session
->lock
);
558 cds_lfht_for_each_entry(session
->ctf_traces_ht
->ht
,
559 &iter
.iter
, trace
, node
.node
) {
560 ret
= ctf_trace_close(trace
);
565 cds_list_for_each_entry_rcu(stream
, &session
->recv_list
,
567 /* Close streams which have not been published yet. */
568 try_stream_close(stream
);
575 /* Put self-reference from create. */
576 session_put(session
);
580 int session_abort(struct relay_session
*session
)
588 pthread_mutex_lock(&session
->lock
);
589 DBG("aborting session %" PRIu64
, session
->id
);
590 session
->aborted
= true;
591 pthread_mutex_unlock(&session
->lock
);
595 void print_sessions(void)
597 struct lttng_ht_iter iter
;
598 struct relay_session
*session
;
605 cds_lfht_for_each_entry(sessions_ht
->ht
, &iter
.iter
, session
,
607 if (!session_get(session
)) {
610 DBG("session %p refcount %ld session %" PRIu64
,
612 session
->ref
.refcount
,
614 session_put(session
);