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/utils.h>
23 #include <common/compat/uuid.h>
24 #include <urcu/rculist.h>
26 #include "ctf-trace.h"
27 #include "lttng-relayd.h"
29 #include "sessiond-trace-chunks.h"
31 #include <common/defaults.h>
33 /* Global session id used in the session creation. */
34 static uint64_t last_relay_session_id
;
35 static pthread_mutex_t last_relay_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
37 static int init_session_output_path(struct relay_session
*session
)
43 * hostname/session_name
47 char *session_directory
= NULL
;
50 if (session
->output_path
[0] != '\0') {
54 * If base path is set, it overrides the session name for the
55 * session relative base path. No timestamp is appended if the
56 * base path is overridden.
58 * If the session name already contains the creation time (e.g.
59 * auto-<timestamp>, don't append yet another timestamp after
60 * the session name in the generated path.
62 * Otherwise, generate the path with session_name-<timestamp>.
64 if (session
->base_path
[0] != '\0') {
65 ret
= asprintf(&session_directory
, "%s/%s", session
->hostname
,
67 } else if (session
->session_name_contains_creation_time
) {
68 ret
= asprintf(&session_directory
, "%s/%s", session
->hostname
,
69 session
->session_name
);
71 char session_creation_datetime
[16];
77 * The 2.11+ protocol guarantees that a creation time
78 * is provided for a session. This would indicate a
79 * protocol error or an improper use of this util.
81 if (!session
->creation_time
.is_set
) {
82 ERR("Creation time missing for session \"%s\" (protocol error)",
83 session
->session_name
);
87 creation_time
= LTTNG_OPTIONAL_GET(session
->creation_time
);
89 timeinfo
= localtime(&creation_time
);
91 ERR("Failed to get timeinfo while initializing session output directory handle");
95 strftime_ret
= strftime(session_creation_datetime
,
96 sizeof(session_creation_datetime
),
97 "%Y%m%d-%H%M%S", timeinfo
);
98 if (strftime_ret
== 0) {
99 ERR("Failed to format session creation timestamp while initializing session output directory handle");
103 ret
= asprintf(&session_directory
, "%s/%s-%s",
104 session
->hostname
, session
->session_name
,
105 session_creation_datetime
);
108 PERROR("Failed to format session directory name");
112 if (strlen(session_directory
) >= LTTNG_PATH_MAX
) {
113 ERR("Session output directory exceeds maximal length");
117 strcpy(session
->output_path
, session_directory
);
121 free(session_directory
);
125 static int session_set_anonymous_chunk(struct relay_session
*session
)
128 struct lttng_trace_chunk
*chunk
= NULL
;
129 enum lttng_trace_chunk_status status
;
130 struct lttng_directory_handle output_directory
;
132 bool output_path_allocated
= false;
134 if (!opt_output_path
) {
135 /* No output path defined */
136 const char *home_dir
= utils_get_home_dir();
138 ERR("Home path not found."
139 " Please specify an output path using -o, --output PATH");
143 ret
= asprintf(&output_path
, "%s/%s", home_dir
, DEFAULT_TRACE_DIR_NAME
);
145 PERROR("asprintf trace dir name");
149 output_path_allocated
= true;
151 output_path
= opt_output_path
;
152 output_path_allocated
= false;
155 ret
= lttng_directory_handle_init(&output_directory
, output_path
);
160 chunk
= lttng_trace_chunk_create_anonymous();
165 status
= lttng_trace_chunk_set_credentials_current_user(chunk
);
166 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
171 status
= lttng_trace_chunk_set_as_owner(chunk
, &output_directory
);
172 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
176 session
->current_trace_chunk
= chunk
;
179 lttng_trace_chunk_put(chunk
);
180 lttng_directory_handle_fini(&output_directory
);
181 if (output_path_allocated
) {
188 * Create a new session by assigning a new session ID.
190 * Return allocated session or else NULL.
192 struct relay_session
*session_create(const char *session_name
,
193 const char *hostname
, const char *base_path
,
196 const lttng_uuid sessiond_uuid
,
197 const uint64_t *id_sessiond
,
198 const uint64_t *current_chunk_id
,
199 const time_t *creation_time
,
202 bool session_name_contains_creation_time
)
205 struct relay_session
*session
= NULL
;
207 if (session_name
&& strstr(session_name
, ".")) {
208 ERR("Illegal character in session name: \"%s\"",
212 if (base_path
&& strstr(base_path
, "../")) {
213 ERR("Invalid session base path walks up the path hierarchy: \"%s\"",
217 if (hostname
&& strstr(hostname
, ".")) {
218 ERR("Invalid character in hostname: \"%s\"",
223 session
= zmalloc(sizeof(*session
));
225 PERROR("Failed to allocate session");
228 if (lttng_strncpy(session
->session_name
, session_name
,
229 sizeof(session
->session_name
))) {
230 WARN("Session name exceeds maximal allowed length");
233 if (lttng_strncpy(session
->hostname
, hostname
,
234 sizeof(session
->hostname
))) {
235 WARN("Hostname exceeds maximal allowed length");
238 if (lttng_strncpy(session
->base_path
, base_path
,
239 sizeof(session
->base_path
))) {
240 WARN("Base path exceeds maximal allowed length");
244 LTTNG_OPTIONAL_SET(&session
->creation_time
, *creation_time
);
246 session
->session_name_contains_creation_time
=
247 session_name_contains_creation_time
;
249 session
->ctf_traces_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
250 if (!session
->ctf_traces_ht
) {
254 pthread_mutex_lock(&last_relay_session_id_lock
);
255 session
->id
= ++last_relay_session_id
;
256 pthread_mutex_unlock(&last_relay_session_id_lock
);
258 session
->major
= major
;
259 session
->minor
= minor
;
260 lttng_ht_node_init_u64(&session
->session_n
, session
->id
);
261 urcu_ref_init(&session
->ref
);
262 CDS_INIT_LIST_HEAD(&session
->recv_list
);
263 pthread_mutex_init(&session
->lock
, NULL
);
264 pthread_mutex_init(&session
->recv_list_lock
, NULL
);
266 session
->live_timer
= live_timer
;
267 session
->snapshot
= snapshot
;
268 lttng_uuid_copy(session
->sessiond_uuid
, sessiond_uuid
);
271 LTTNG_OPTIONAL_SET(&session
->id_sessiond
, *id_sessiond
);
274 ret
= init_session_output_path(session
);
278 ret
= sessiond_trace_chunk_registry_session_created(
279 sessiond_trace_chunk_registry
, sessiond_uuid
);
284 if (id_sessiond
&& current_chunk_id
) {
285 session
->current_trace_chunk
=
286 sessiond_trace_chunk_registry_get_chunk(
287 sessiond_trace_chunk_registry
,
288 session
->sessiond_uuid
,
289 session
->id_sessiond
.value
,
291 if (!session
->current_trace_chunk
) {
292 char uuid_str
[UUID_STR_LEN
];
294 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
295 ERR("Could not find trace chunk: sessiond = {%s}, sessiond session id = %" PRIu64
", trace chunk id = %" PRIu64
,
296 uuid_str
, *id_sessiond
,
299 } else if (!id_sessiond
) {
301 * Pre-2.11 peers will not announce trace chunks. An
302 * anonymous trace chunk which will remain set for the
303 * duration of the session is created.
305 ret
= session_set_anonymous_chunk(session
);
311 lttng_ht_add_unique_u64(sessions_ht
, &session
->session_n
);
315 session_put(session
);
319 /* Should be called with RCU read-side lock held. */
320 bool session_get(struct relay_session
*session
)
322 return urcu_ref_get_unless_zero(&session
->ref
);
326 * Lookup a session within the session hash table using the session id
327 * as key. A session reference is taken when a session is returned.
328 * session_put() must be called on that session.
330 * Return session or NULL if not found.
332 struct relay_session
*session_get_by_id(uint64_t id
)
334 struct relay_session
*session
= NULL
;
335 struct lttng_ht_node_u64
*node
;
336 struct lttng_ht_iter iter
;
339 lttng_ht_lookup(sessions_ht
, &id
, &iter
);
340 node
= lttng_ht_iter_get_node_u64(&iter
);
342 DBG("Session find by ID %" PRIu64
" id NOT found", id
);
345 session
= caa_container_of(node
, struct relay_session
, session_n
);
346 DBG("Session find by ID %" PRIu64
" id found", id
);
347 if (!session_get(session
)) {
355 static void rcu_destroy_session(struct rcu_head
*rcu_head
)
357 struct relay_session
*session
=
358 caa_container_of(rcu_head
, struct relay_session
,
361 * Since each trace has a reference on the session, it means
362 * that if we are at the point where we teardown the session, no
363 * trace belonging to that session exist at this point.
364 * Calling lttng_ht_destroy in call_rcu worker thread so we
365 * don't hold the RCU read-side lock while calling it.
367 lttng_ht_destroy(session
->ctf_traces_ht
);
372 * Delete session from the given hash table.
374 * Return lttng ht del error code being 0 on success and 1 on failure.
376 static int session_delete(struct relay_session
*session
)
378 struct lttng_ht_iter iter
;
380 iter
.iter
.node
= &session
->session_n
.node
;
381 return lttng_ht_del(sessions_ht
, &iter
);
385 static void destroy_session(struct relay_session
*session
)
389 ret
= session_delete(session
);
391 lttng_trace_chunk_put(session
->current_trace_chunk
);
392 session
->current_trace_chunk
= NULL
;
393 lttng_trace_chunk_put(session
->pending_closure_trace_chunk
);
394 session
->pending_closure_trace_chunk
= NULL
;
395 ret
= sessiond_trace_chunk_registry_session_destroyed(
396 sessiond_trace_chunk_registry
, session
->sessiond_uuid
);
398 call_rcu(&session
->rcu_node
, rcu_destroy_session
);
401 void session_release(struct urcu_ref
*ref
)
403 struct relay_session
*session
=
404 caa_container_of(ref
, struct relay_session
, ref
);
406 destroy_session(session
);
409 void session_put(struct relay_session
*session
)
412 urcu_ref_put(&session
->ref
, session_release
);
416 int session_close(struct relay_session
*session
)
419 struct ctf_trace
*trace
;
420 struct lttng_ht_iter iter
;
421 struct relay_stream
*stream
;
423 pthread_mutex_lock(&session
->lock
);
424 DBG("closing session %" PRIu64
": is conn already closed %d",
425 session
->id
, session
->connection_closed
);
426 session
->connection_closed
= true;
427 pthread_mutex_unlock(&session
->lock
);
430 cds_lfht_for_each_entry(session
->ctf_traces_ht
->ht
,
431 &iter
.iter
, trace
, node
.node
) {
432 ret
= ctf_trace_close(trace
);
437 cds_list_for_each_entry_rcu(stream
, &session
->recv_list
,
439 /* Close streams which have not been published yet. */
440 try_stream_close(stream
);
447 /* Put self-reference from create. */
448 session_put(session
);
452 int session_abort(struct relay_session
*session
)
460 pthread_mutex_lock(&session
->lock
);
461 DBG("aborting session %" PRIu64
, session
->id
);
462 session
->aborted
= true;
463 pthread_mutex_unlock(&session
->lock
);
467 void print_sessions(void)
469 struct lttng_ht_iter iter
;
470 struct relay_session
*session
;
477 cds_lfht_for_each_entry(sessions_ht
->ht
, &iter
.iter
, session
,
479 if (!session_get(session
)) {
482 DBG("session %p refcount %ld session %" PRIu64
,
484 session
->ref
.refcount
,
486 session_put(session
);