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/compat/uuid.h>
23 #include <common/time.h>
24 #include <common/utils.h>
25 #include <urcu/rculist.h>
29 #include "ctf-trace.h"
30 #include "lttng-relayd.h"
32 #include "sessiond-trace-chunks.h"
34 #include <common/defaults.h>
37 /* Global session id used in the session creation. */
38 static uint64_t last_relay_session_id
;
39 static pthread_mutex_t last_relay_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
41 static int init_session_output_path_group_by_host(struct relay_session
*session
)
47 * hostname/session_name
51 char *session_directory
= NULL
;
54 if (session
->output_path
[0] != '\0') {
58 * If base path is set, it overrides the session name for the
59 * session relative base path. No timestamp is appended if the
60 * base path is overridden.
62 * If the session name already contains the creation time (e.g.
63 * auto-<timestamp>, don't append yet another timestamp after
64 * the session name in the generated path.
66 * Otherwise, generate the path with session_name-<timestamp>.
68 if (session
->base_path
[0] != '\0') {
69 ret
= asprintf(&session_directory
, "%s/%s", session
->hostname
,
71 } else if (session
->session_name_contains_creation_time
) {
72 ret
= asprintf(&session_directory
, "%s/%s", session
->hostname
,
73 session
->session_name
);
75 char session_creation_datetime
[DATETIME_STR_LEN
];
77 ret
= time_to_datetime_str(
78 LTTNG_OPTIONAL_GET(session
->creation_time
),
79 session_creation_datetime
,
80 sizeof(session_creation_datetime
));
82 ERR("Failed to format session creation timestamp while initializing session output directory handle");
87 ret
= asprintf(&session_directory
, "%s/%s-%s",
88 session
->hostname
, session
->session_name
,
89 session_creation_datetime
);
92 PERROR("Failed to format session directory name");
96 if (strlen(session_directory
) >= LTTNG_PATH_MAX
) {
97 ERR("Session output directory exceeds maximal length");
101 strcpy(session
->output_path
, session_directory
);
105 free(session_directory
);
109 static int init_session_output_path_group_by_session(
110 struct relay_session
*session
)
115 * session_name/hostname-creation_time/base_path
117 * For session name including the datetime, use it as the complete name
118 * since. Do not perform modification on it since the datetime is an
119 * integral part of the name and how a user identify a session.
122 char *session_directory
= NULL
;
123 char creation_datetime
[DATETIME_STR_LEN
];
125 if (session
->output_path
[0] != '\0') {
126 /* output_path as been generated already */
130 ret
= time_to_datetime_str(LTTNG_OPTIONAL_GET(session
->creation_time
),
131 creation_datetime
, sizeof(creation_datetime
));
133 ERR("Failed to format session creation timestamp while initializing session output directory handle");
138 ret
= asprintf(&session_directory
, "%s/%s-%s%s%s",
139 session
->session_name
, session
->hostname
,
141 session
->base_path
[0] != '\0' ? "/" : "",
144 PERROR("Failed to format session directory name");
148 if (strlen(session_directory
) >= LTTNG_PATH_MAX
) {
149 ERR("Session output directory exceeds maximal length");
154 strcpy(session
->output_path
, session_directory
);
158 free(session_directory
);
162 static int init_session_output_path(struct relay_session
*session
)
166 switch (opt_group_output_by
) {
167 case RELAYD_GROUP_OUTPUT_BY_HOST
:
168 ret
= init_session_output_path_group_by_host(session
);
170 case RELAYD_GROUP_OUTPUT_BY_SESSION
:
171 ret
= init_session_output_path_group_by_session(session
);
173 case RELAYD_GROUP_OUTPUT_BY_UNKNOWN
:
182 static int session_set_anonymous_chunk(struct relay_session
*session
)
185 struct lttng_trace_chunk
*chunk
= NULL
;
186 enum lttng_trace_chunk_status status
;
187 struct lttng_directory_handle output_directory
;
189 ret
= session_init_output_directory_handle(session
, &output_directory
);
194 chunk
= lttng_trace_chunk_create_anonymous();
199 status
= lttng_trace_chunk_set_credentials_current_user(chunk
);
200 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
205 status
= lttng_trace_chunk_set_as_owner(chunk
, &output_directory
);
206 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
210 session
->current_trace_chunk
= chunk
;
213 lttng_trace_chunk_put(chunk
);
214 lttng_directory_handle_fini(&output_directory
);
219 * Create a new session by assigning a new session ID.
221 * Return allocated session or else NULL.
223 struct relay_session
*session_create(const char *session_name
,
224 const char *hostname
, const char *base_path
,
227 const lttng_uuid sessiond_uuid
,
228 const uint64_t *id_sessiond
,
229 const uint64_t *current_chunk_id
,
230 const time_t *creation_time
,
233 bool session_name_contains_creation_time
)
236 struct relay_session
*session
= NULL
;
238 assert(session_name
);
242 if (strstr(session_name
, ".")) {
243 ERR("Illegal character in session name: \"%s\"",
247 if (strstr(base_path
, "../")) {
248 ERR("Invalid session base path walks up the path hierarchy: \"%s\"",
252 if (strstr(hostname
, ".")) {
253 ERR("Invalid character in hostname: \"%s\"",
258 session
= zmalloc(sizeof(*session
));
260 PERROR("Failed to allocate session");
263 if (lttng_strncpy(session
->session_name
, session_name
,
264 sizeof(session
->session_name
))) {
265 WARN("Session name exceeds maximal allowed length");
268 if (lttng_strncpy(session
->hostname
, hostname
,
269 sizeof(session
->hostname
))) {
270 WARN("Hostname exceeds maximal allowed length");
273 if (lttng_strncpy(session
->base_path
, base_path
,
274 sizeof(session
->base_path
))) {
275 WARN("Base path exceeds maximal allowed length");
279 LTTNG_OPTIONAL_SET(&session
->creation_time
, *creation_time
);
281 session
->session_name_contains_creation_time
=
282 session_name_contains_creation_time
;
284 session
->ctf_traces_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
285 if (!session
->ctf_traces_ht
) {
289 pthread_mutex_lock(&last_relay_session_id_lock
);
290 session
->id
= ++last_relay_session_id
;
291 pthread_mutex_unlock(&last_relay_session_id_lock
);
293 session
->major
= major
;
294 session
->minor
= minor
;
295 lttng_ht_node_init_u64(&session
->session_n
, session
->id
);
296 urcu_ref_init(&session
->ref
);
297 CDS_INIT_LIST_HEAD(&session
->recv_list
);
298 pthread_mutex_init(&session
->lock
, NULL
);
299 pthread_mutex_init(&session
->recv_list_lock
, NULL
);
301 session
->live_timer
= live_timer
;
302 session
->snapshot
= snapshot
;
303 lttng_uuid_copy(session
->sessiond_uuid
, sessiond_uuid
);
306 LTTNG_OPTIONAL_SET(&session
->id_sessiond
, *id_sessiond
);
309 if (major
== 2 && minor
>= 11) {
310 /* Only applies for 2.11+ peers using trace chunks. */
311 ret
= init_session_output_path(session
);
317 ret
= sessiond_trace_chunk_registry_session_created(
318 sessiond_trace_chunk_registry
, sessiond_uuid
);
323 if (id_sessiond
&& current_chunk_id
) {
324 session
->current_trace_chunk
=
325 sessiond_trace_chunk_registry_get_chunk(
326 sessiond_trace_chunk_registry
,
327 session
->sessiond_uuid
,
328 session
->id_sessiond
.value
,
330 if (!session
->current_trace_chunk
) {
331 char uuid_str
[UUID_STR_LEN
];
333 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
334 ERR("Could not find trace chunk: sessiond = {%s}, sessiond session id = %" PRIu64
", trace chunk id = %" PRIu64
,
335 uuid_str
, *id_sessiond
,
338 } else if (!id_sessiond
) {
340 * Pre-2.11 peers will not announce trace chunks. An
341 * anonymous trace chunk which will remain set for the
342 * duration of the session is created.
344 ret
= session_set_anonymous_chunk(session
);
350 lttng_ht_add_unique_u64(sessions_ht
, &session
->session_n
);
354 session_put(session
);
358 /* Should be called with RCU read-side lock held. */
359 bool session_get(struct relay_session
*session
)
361 return urcu_ref_get_unless_zero(&session
->ref
);
365 * Lookup a session within the session hash table using the session id
366 * as key. A session reference is taken when a session is returned.
367 * session_put() must be called on that session.
369 * Return session or NULL if not found.
371 struct relay_session
*session_get_by_id(uint64_t id
)
373 struct relay_session
*session
= NULL
;
374 struct lttng_ht_node_u64
*node
;
375 struct lttng_ht_iter iter
;
378 lttng_ht_lookup(sessions_ht
, &id
, &iter
);
379 node
= lttng_ht_iter_get_node_u64(&iter
);
381 DBG("Session find by ID %" PRIu64
" id NOT found", id
);
384 session
= caa_container_of(node
, struct relay_session
, session_n
);
385 DBG("Session find by ID %" PRIu64
" id found", id
);
386 if (!session_get(session
)) {
394 static void rcu_destroy_session(struct rcu_head
*rcu_head
)
396 struct relay_session
*session
=
397 caa_container_of(rcu_head
, struct relay_session
,
400 * Since each trace has a reference on the session, it means
401 * that if we are at the point where we teardown the session, no
402 * trace belonging to that session exist at this point.
403 * Calling lttng_ht_destroy in call_rcu worker thread so we
404 * don't hold the RCU read-side lock while calling it.
406 lttng_ht_destroy(session
->ctf_traces_ht
);
411 * Delete session from the given hash table.
413 * Return lttng ht del error code being 0 on success and 1 on failure.
415 static int session_delete(struct relay_session
*session
)
417 struct lttng_ht_iter iter
;
419 iter
.iter
.node
= &session
->session_n
.node
;
420 return lttng_ht_del(sessions_ht
, &iter
);
424 static void destroy_session(struct relay_session
*session
)
428 ret
= session_delete(session
);
430 lttng_trace_chunk_put(session
->current_trace_chunk
);
431 session
->current_trace_chunk
= NULL
;
432 lttng_trace_chunk_put(session
->pending_closure_trace_chunk
);
433 session
->pending_closure_trace_chunk
= NULL
;
434 ret
= sessiond_trace_chunk_registry_session_destroyed(
435 sessiond_trace_chunk_registry
, session
->sessiond_uuid
);
437 call_rcu(&session
->rcu_node
, rcu_destroy_session
);
440 void session_release(struct urcu_ref
*ref
)
442 struct relay_session
*session
=
443 caa_container_of(ref
, struct relay_session
, ref
);
445 destroy_session(session
);
448 void session_put(struct relay_session
*session
)
454 urcu_ref_put(&session
->ref
, session_release
);
458 int session_close(struct relay_session
*session
)
461 struct ctf_trace
*trace
;
462 struct lttng_ht_iter iter
;
463 struct relay_stream
*stream
;
465 pthread_mutex_lock(&session
->lock
);
466 DBG("closing session %" PRIu64
": is conn already closed %d",
467 session
->id
, session
->connection_closed
);
468 session
->connection_closed
= true;
469 pthread_mutex_unlock(&session
->lock
);
472 cds_lfht_for_each_entry(session
->ctf_traces_ht
->ht
,
473 &iter
.iter
, trace
, node
.node
) {
474 ret
= ctf_trace_close(trace
);
479 cds_list_for_each_entry_rcu(stream
, &session
->recv_list
,
481 /* Close streams which have not been published yet. */
482 try_stream_close(stream
);
489 /* Put self-reference from create. */
490 session_put(session
);
494 int session_abort(struct relay_session
*session
)
502 pthread_mutex_lock(&session
->lock
);
503 DBG("aborting session %" PRIu64
, session
->id
);
504 session
->aborted
= true;
505 pthread_mutex_unlock(&session
->lock
);
509 void print_sessions(void)
511 struct lttng_ht_iter iter
;
512 struct relay_session
*session
;
519 cds_lfht_for_each_entry(sessions_ht
->ht
, &iter
.iter
, session
,
521 if (!session_get(session
)) {
524 DBG("session %p refcount %ld session %" PRIu64
,
526 session
->ref
.refcount
,
528 session_put(session
);
533 int session_init_output_directory_handle(struct relay_session
*session
,
534 struct lttng_directory_handle
*handle
)
538 * relayd_output_path/session_directory
539 * e.g. /home/user/lttng-traces/hostname/session_name
541 char *full_session_path
= NULL
;
543 pthread_mutex_lock(&session
->lock
);
544 full_session_path
= create_output_path(session
->output_path
);
545 if (!full_session_path
) {
550 ret
= utils_mkdir_recursive(
551 full_session_path
, S_IRWXU
| S_IRWXG
, -1, -1);
553 ERR("Failed to create session output path \"%s\"",
558 ret
= lttng_directory_handle_init(handle
, full_session_path
);
563 pthread_mutex_unlock(&session
->lock
);
564 free(full_session_path
);