tests: Move to kernel style SPDX license identifiers
[lttng-tools.git] / src / bin / lttng-relayd / session.c
CommitLineData
2f8f53af 1/*
ab5be9fa
MJ
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>
2f8f53af 5 *
ab5be9fa 6 * SPDX-License-Identifier: GPL-2.0-only
2f8f53af 7 *
2f8f53af
DG
8 */
9
6c1c0768 10#define _LGPL_SOURCE
2a174661 11#include <common/common.h>
c70636a7 12#include <common/uuid.h>
a8b66566
JR
13#include <common/time.h>
14#include <common/utils.h>
c70636a7 15#include <common/uuid.h>
6ec9dc48 16#include <common/compat/path.h>
7591bab1 17#include <urcu/rculist.h>
2a174661 18
d37856b8
JR
19#include <sys/stat.h>
20
2a174661 21#include "ctf-trace.h"
a620c891 22#include "lttng-relayd.h"
2f8f53af 23#include "session.h"
23c8ff50 24#include "sessiond-trace-chunks.h"
a620c891 25#include "stream.h"
a0409c33 26#include <common/defaults.h>
d37856b8 27#include "utils.h"
2a174661
DG
28
29/* Global session id used in the session creation. */
30static uint64_t last_relay_session_id;
7591bab1 31static pthread_mutex_t last_relay_session_id_lock = PTHREAD_MUTEX_INITIALIZER;
2a174661 32
a8b66566 33static int init_session_output_path_group_by_host(struct relay_session *session)
ecd1a12f
MD
34{
35 /*
36 * session_directory:
37 *
38 * if base_path is \0'
39 * hostname/session_name
40 * else
41 * hostname/base_path
42 */
43 char *session_directory = NULL;
44 int ret = 0;
45
46 if (session->output_path[0] != '\0') {
47 goto end;
48 }
49 /*
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.
53 *
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.
57 *
58 * Otherwise, generate the path with session_name-<timestamp>.
59 */
60 if (session->base_path[0] != '\0') {
61 ret = asprintf(&session_directory, "%s/%s", session->hostname,
62 session->base_path);
63 } else if (session->session_name_contains_creation_time) {
64 ret = asprintf(&session_directory, "%s/%s", session->hostname,
65 session->session_name);
66 } else {
a8b66566 67 char session_creation_datetime[DATETIME_STR_LEN];
ecd1a12f 68
a8b66566
JR
69 ret = time_to_datetime_str(
70 LTTNG_OPTIONAL_GET(session->creation_time),
71 session_creation_datetime,
72 sizeof(session_creation_datetime));
73 if (ret) {
ecd1a12f
MD
74 ERR("Failed to format session creation timestamp while initializing session output directory handle");
75 ret = -1;
76 goto end;
77 }
a8b66566 78
ecd1a12f
MD
79 ret = asprintf(&session_directory, "%s/%s-%s",
80 session->hostname, session->session_name,
81 session_creation_datetime);
82 }
83 if (ret < 0) {
84 PERROR("Failed to format session directory name");
85 goto end;
86 }
87
88 if (strlen(session_directory) >= LTTNG_PATH_MAX) {
89 ERR("Session output directory exceeds maximal length");
90 ret = -1;
91 goto end;
92 }
93 strcpy(session->output_path, session_directory);
94 ret = 0;
95
96end:
97 free(session_directory);
98 return ret;
99}
100
a8b66566
JR
101static int init_session_output_path_group_by_session(
102 struct relay_session *session)
103{
104 /*
105 * session_directory:
106 *
107 * session_name/hostname-creation_time/base_path
108 *
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.
112 */
113 int ret = 0;
114 char *session_directory = NULL;
115 char creation_datetime[DATETIME_STR_LEN];
116
117 if (session->output_path[0] != '\0') {
118 /* output_path as been generated already */
119 goto end;
120 }
121
122 ret = time_to_datetime_str(LTTNG_OPTIONAL_GET(session->creation_time),
123 creation_datetime, sizeof(creation_datetime));
124 if (ret) {
125 ERR("Failed to format session creation timestamp while initializing session output directory handle");
126 ret = -1;
127 goto end;
128 }
129
130 ret = asprintf(&session_directory, "%s/%s-%s%s%s",
131 session->session_name, session->hostname,
132 creation_datetime,
133 session->base_path[0] != '\0' ? "/" : "",
134 session->base_path);
135 if (ret < 0) {
136 PERROR("Failed to format session directory name");
137 goto end;
138 }
139
140 if (strlen(session_directory) >= LTTNG_PATH_MAX) {
141 ERR("Session output directory exceeds maximal length");
142 ret = -1;
143 goto end;
144 }
145
146 strcpy(session->output_path, session_directory);
147 ret = 0;
148
149end:
150 free(session_directory);
151 return ret;
152}
153
154static int init_session_output_path(struct relay_session *session)
155{
156 int ret;
157
158 switch (opt_group_output_by) {
159 case RELAYD_GROUP_OUTPUT_BY_HOST:
160 ret = init_session_output_path_group_by_host(session);
161 break;
162 case RELAYD_GROUP_OUTPUT_BY_SESSION:
163 ret = init_session_output_path_group_by_session(session);
164 break;
165 case RELAYD_GROUP_OUTPUT_BY_UNKNOWN:
166 default:
167 abort();
168 break;
169 }
170
171 return ret;
172}
173
7ceefac4
JG
174static struct lttng_directory_handle *session_create_output_directory_handle(
175 struct relay_session *session)
176{
177 int ret;
178 /*
179 * relayd_output_path/session_directory
180 * e.g. /home/user/lttng-traces/hostname/session_name
181 */
182 char *full_session_path = NULL;
183 struct lttng_directory_handle *handle = NULL;
184
185 pthread_mutex_lock(&session->lock);
186 full_session_path = create_output_path(session->output_path);
187 if (!full_session_path) {
188 goto end;
189 }
190
191 ret = utils_mkdir_recursive(
192 full_session_path, S_IRWXU | S_IRWXG, -1, -1);
193 if (ret) {
194 ERR("Failed to create session output path \"%s\"",
195 full_session_path);
196 goto end;
197 }
198
199 handle = lttng_directory_handle_create(full_session_path);
200end:
201 pthread_mutex_unlock(&session->lock);
202 free(full_session_path);
203 return handle;
204}
205
1e791a74
JG
206static int session_set_anonymous_chunk(struct relay_session *session)
207{
208 int ret = 0;
209 struct lttng_trace_chunk *chunk = NULL;
210 enum lttng_trace_chunk_status status;
cbf53d23 211 struct lttng_directory_handle *output_directory;
1e791a74 212
cbf53d23
JG
213 output_directory = session_create_output_directory_handle(session);
214 if (!output_directory) {
1e791a74
JG
215 goto end;
216 }
217
218 chunk = lttng_trace_chunk_create_anonymous();
219 if (!chunk) {
220 goto end;
221 }
222
223 status = lttng_trace_chunk_set_credentials_current_user(chunk);
224 if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
225 ret = -1;
226 goto end;
227 }
228
cbf53d23 229 status = lttng_trace_chunk_set_as_owner(chunk, output_directory);
1e791a74
JG
230 if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
231 ret = -1;
232 goto end;
233 }
7145f5e9
JG
234
235 lttng_trace_chunk_set_fd_tracker(chunk, the_fd_tracker);
cbf53d23 236 output_directory = NULL;
1e791a74
JG
237 session->current_trace_chunk = chunk;
238 chunk = NULL;
239end:
240 lttng_trace_chunk_put(chunk);
cbf53d23 241 lttng_directory_handle_put(output_directory);
1e791a74
JG
242 return ret;
243}
244
6ec9dc48
JG
245/*
246 * Check if a name is safe to use in a path.
247 *
248 * A name that is deemed "path-safe":
249 * - Does not contains a path separator (/ or \, platform dependant),
250 * - Does not start with a '.' (hidden file/folder),
251 * - Is not empty.
252 */
253static bool is_name_path_safe(const char *name)
254{
255 const size_t name_len = strlen(name);
256
257 /* Not empty. */
258 if (name_len == 0) {
259 WARN("An empty name is not allowed to be used in a path");
260 return false;
261 }
262 /* Does not start with '.'. */
263 if (name[0] == '.') {
264 WARN("Name \"%s\" is not allowed to be used in a path since it starts with '.'", name);
265 return false;
266 }
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", name);
270 return false;
271 }
272
273 return true;
274}
275
2a174661
DG
276/*
277 * Create a new session by assigning a new session ID.
278 *
279 * Return allocated session or else NULL.
280 */
7591bab1 281struct relay_session *session_create(const char *session_name,
6fa5fe7c 282 const char *hostname, const char *base_path,
db1da059
JG
283 uint32_t live_timer,
284 bool snapshot,
285 const lttng_uuid sessiond_uuid,
286 const uint64_t *id_sessiond,
287 const uint64_t *current_chunk_id,
288 const time_t *creation_time,
289 uint32_t major,
46ef2188
MD
290 uint32_t minor,
291 bool session_name_contains_creation_time)
2a174661 292{
23c8ff50 293 int ret;
590f0324
JG
294 struct relay_session *session = NULL;
295
5c956ba3
JG
296 assert(session_name);
297 assert(hostname);
298 assert(base_path);
299
6ec9dc48
JG
300 if (!is_name_path_safe(session_name)) {
301 ERR("Refusing to create session as the provided session name is not path-safe");
302 goto error;
303 }
304 if (!is_name_path_safe(hostname)) {
305 ERR("Refusing to create session as the provided hostname is not path-safe");
590f0324
JG
306 goto error;
307 }
5c956ba3 308 if (strstr(base_path, "../")) {
590f0324
JG
309 ERR("Invalid session base path walks up the path hierarchy: \"%s\"",
310 base_path);
311 goto error;
312 }
2a174661
DG
313
314 session = zmalloc(sizeof(*session));
315 if (!session) {
1e791a74 316 PERROR("Failed to allocate session");
2a174661
DG
317 goto error;
318 }
bb5d54e7
MD
319 if (lttng_strncpy(session->session_name, session_name,
320 sizeof(session->session_name))) {
1e791a74 321 WARN("Session name exceeds maximal allowed length");
bb5d54e7
MD
322 goto error;
323 }
324 if (lttng_strncpy(session->hostname, hostname,
325 sizeof(session->hostname))) {
1e791a74 326 WARN("Hostname exceeds maximal allowed length");
bb5d54e7
MD
327 goto error;
328 }
6fa5fe7c
MD
329 if (lttng_strncpy(session->base_path, base_path,
330 sizeof(session->base_path))) {
331 WARN("Base path exceeds maximal allowed length");
332 goto error;
333 }
46ef2188
MD
334 if (creation_time) {
335 LTTNG_OPTIONAL_SET(&session->creation_time, *creation_time);
336 }
337 session->session_name_contains_creation_time =
338 session_name_contains_creation_time;
6fa5fe7c 339
2a174661
DG
340 session->ctf_traces_ht = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
341 if (!session->ctf_traces_ht) {
2a174661
DG
342 goto error;
343 }
344
7591bab1 345 pthread_mutex_lock(&last_relay_session_id_lock);
2a174661 346 session->id = ++last_relay_session_id;
7591bab1
MD
347 pthread_mutex_unlock(&last_relay_session_id_lock);
348
349 session->major = major;
350 session->minor = minor;
2a174661 351 lttng_ht_node_init_u64(&session->session_n, session->id);
7591bab1
MD
352 urcu_ref_init(&session->ref);
353 CDS_INIT_LIST_HEAD(&session->recv_list);
354 pthread_mutex_init(&session->lock, NULL);
7591bab1
MD
355 pthread_mutex_init(&session->recv_list_lock, NULL);
356
7591bab1
MD
357 session->live_timer = live_timer;
358 session->snapshot = snapshot;
23c8ff50
JG
359 lttng_uuid_copy(session->sessiond_uuid, sessiond_uuid);
360
1e791a74
JG
361 if (id_sessiond) {
362 LTTNG_OPTIONAL_SET(&session->id_sessiond, *id_sessiond);
363 }
364
d519f442
JR
365 if (major == 2 && minor >= 11) {
366 /* Only applies for 2.11+ peers using trace chunks. */
367 ret = init_session_output_path(session);
368 if (ret) {
369 goto error;
370 }
ecd1a12f 371 }
d519f442 372
23c8ff50
JG
373 ret = sessiond_trace_chunk_registry_session_created(
374 sessiond_trace_chunk_registry, sessiond_uuid);
375 if (ret) {
376 goto error;
377 }
7591bab1 378
1e791a74 379 if (id_sessiond && current_chunk_id) {
7ceefac4
JG
380 enum lttng_trace_chunk_status chunk_status;
381 struct lttng_directory_handle *session_output_directory;
382
1e791a74
JG
383 session->current_trace_chunk =
384 sessiond_trace_chunk_registry_get_chunk(
385 sessiond_trace_chunk_registry,
386 session->sessiond_uuid,
387 session->id_sessiond.value,
388 *current_chunk_id);
389 if (!session->current_trace_chunk) {
c70636a7 390 char uuid_str[LTTNG_UUID_STR_LEN];
1e791a74
JG
391
392 lttng_uuid_to_str(sessiond_uuid, uuid_str);
393 ERR("Could not find trace chunk: sessiond = {%s}, sessiond session id = %" PRIu64 ", trace chunk id = %" PRIu64,
394 uuid_str, *id_sessiond,
395 *current_chunk_id);
53eb691c 396 goto error;
1e791a74 397 }
7ceefac4
JG
398
399 chunk_status = lttng_trace_chunk_get_session_output_directory_handle(
400 session->current_trace_chunk,
401 &session_output_directory);
402 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
403 goto error;
404 }
405
406 assert(session_output_directory);
407 session->output_directory = session_output_directory;
1e791a74
JG
408 } else if (!id_sessiond) {
409 /*
410 * Pre-2.11 peers will not announce trace chunks. An
411 * anonymous trace chunk which will remain set for the
412 * duration of the session is created.
413 */
414 ret = session_set_anonymous_chunk(session);
415 if (ret) {
416 goto error;
417 }
7ceefac4
JG
418 } else {
419 session->output_directory =
420 session_create_output_directory_handle(session);
421 if (!session->output_directory) {
422 goto error;
423 }
1e791a74
JG
424 }
425
7591bab1 426 lttng_ht_add_unique_u64(sessions_ht, &session->session_n);
bb5d54e7 427 return session;
2a174661
DG
428
429error:
1e791a74 430 session_put(session);
bb5d54e7 431 return NULL;
2a174661 432}
2f8f53af 433
7591bab1
MD
434/* Should be called with RCU read-side lock held. */
435bool session_get(struct relay_session *session)
436{
ce4d4083 437 return urcu_ref_get_unless_zero(&session->ref);
7591bab1
MD
438}
439
2f8f53af 440/*
7591bab1
MD
441 * Lookup a session within the session hash table using the session id
442 * as key. A session reference is taken when a session is returned.
443 * session_put() must be called on that session.
2f8f53af
DG
444 *
445 * Return session or NULL if not found.
446 */
7591bab1 447struct relay_session *session_get_by_id(uint64_t id)
2f8f53af
DG
448{
449 struct relay_session *session = NULL;
2a174661 450 struct lttng_ht_node_u64 *node;
2f8f53af
DG
451 struct lttng_ht_iter iter;
452
7591bab1
MD
453 rcu_read_lock();
454 lttng_ht_lookup(sessions_ht, &id, &iter);
2a174661 455 node = lttng_ht_iter_get_node_u64(&iter);
2f8f53af 456 if (!node) {
2a174661 457 DBG("Session find by ID %" PRIu64 " id NOT found", id);
2f8f53af
DG
458 goto end;
459 }
460 session = caa_container_of(node, struct relay_session, session_n);
2a174661 461 DBG("Session find by ID %" PRIu64 " id found", id);
7591bab1
MD
462 if (!session_get(session)) {
463 session = NULL;
464 }
2f8f53af 465end:
7591bab1 466 rcu_read_unlock();
2f8f53af
DG
467 return session;
468}
2a174661 469
7591bab1
MD
470static void rcu_destroy_session(struct rcu_head *rcu_head)
471{
472 struct relay_session *session =
473 caa_container_of(rcu_head, struct relay_session,
474 rcu_node);
49e614cb
MD
475 /*
476 * Since each trace has a reference on the session, it means
477 * that if we are at the point where we teardown the session, no
478 * trace belonging to that session exist at this point.
479 * Calling lttng_ht_destroy in call_rcu worker thread so we
480 * don't hold the RCU read-side lock while calling it.
481 */
482 lttng_ht_destroy(session->ctf_traces_ht);
7591bab1
MD
483 free(session);
484}
485
2a174661
DG
486/*
487 * Delete session from the given hash table.
488 *
489 * Return lttng ht del error code being 0 on success and 1 on failure.
490 */
7591bab1 491static int session_delete(struct relay_session *session)
2a174661
DG
492{
493 struct lttng_ht_iter iter;
494
2a174661 495 iter.iter.node = &session->session_n.node;
7591bab1 496 return lttng_ht_del(sessions_ht, &iter);
2a174661
DG
497}
498
7591bab1
MD
499
500static void destroy_session(struct relay_session *session)
501{
502 int ret;
503
504 ret = session_delete(session);
505 assert(!ret);
639ddf68 506 lttng_trace_chunk_put(session->current_trace_chunk);
c35f9726 507 session->current_trace_chunk = NULL;
62bad3bf
JG
508 lttng_trace_chunk_put(session->pending_closure_trace_chunk);
509 session->pending_closure_trace_chunk = NULL;
23c8ff50
JG
510 ret = sessiond_trace_chunk_registry_session_destroyed(
511 sessiond_trace_chunk_registry, session->sessiond_uuid);
512 assert(!ret);
7ceefac4
JG
513 lttng_directory_handle_put(session->output_directory);
514 session->output_directory = NULL;
7591bab1
MD
515 call_rcu(&session->rcu_node, rcu_destroy_session);
516}
517
feb33caa 518static void session_release(struct urcu_ref *ref)
2a174661 519{
7591bab1
MD
520 struct relay_session *session =
521 caa_container_of(ref, struct relay_session, ref);
2a174661 522
7591bab1
MD
523 destroy_session(session);
524}
2a174661 525
7591bab1
MD
526void session_put(struct relay_session *session)
527{
874ec45e
JG
528 if (!session) {
529 return;
530 }
7591bab1 531 rcu_read_lock();
7591bab1 532 urcu_ref_put(&session->ref, session_release);
7591bab1 533 rcu_read_unlock();
2a174661
DG
534}
535
7591bab1 536int session_close(struct relay_session *session)
2a174661
DG
537{
538 int ret = 0;
7591bab1
MD
539 struct ctf_trace *trace;
540 struct lttng_ht_iter iter;
541 struct relay_stream *stream;
542
543 pthread_mutex_lock(&session->lock);
544 DBG("closing session %" PRIu64 ": is conn already closed %d",
545 session->id, session->connection_closed);
7591bab1 546 session->connection_closed = true;
7591bab1 547 pthread_mutex_unlock(&session->lock);
2a174661 548
7591bab1
MD
549 rcu_read_lock();
550 cds_lfht_for_each_entry(session->ctf_traces_ht->ht,
551 &iter.iter, trace, node.node) {
552 ret = ctf_trace_close(trace);
553 if (ret) {
554 goto rcu_unlock;
2a174661
DG
555 }
556 }
7591bab1
MD
557 cds_list_for_each_entry_rcu(stream, &session->recv_list,
558 recv_node) {
bda7c7b9
JG
559 /* Close streams which have not been published yet. */
560 try_stream_close(stream);
7591bab1
MD
561 }
562rcu_unlock:
563 rcu_read_unlock();
564 if (ret) {
565 return ret;
566 }
567 /* Put self-reference from create. */
568 session_put(session);
569 return ret;
2a174661
DG
570}
571
98ba050e
JR
572int session_abort(struct relay_session *session)
573{
574 int ret = 0;
575
576 if (!session) {
577 return 0;
578 }
579
580 pthread_mutex_lock(&session->lock);
581 DBG("aborting session %" PRIu64, session->id);
98ba050e 582 session->aborted = true;
98ba050e
JR
583 pthread_mutex_unlock(&session->lock);
584 return ret;
585}
586
7591bab1 587void print_sessions(void)
2a174661 588{
2a174661 589 struct lttng_ht_iter iter;
7591bab1 590 struct relay_session *session;
2a174661 591
ce3f3ba3
JG
592 if (!sessions_ht) {
593 return;
594 }
595
2a174661 596 rcu_read_lock();
7591bab1
MD
597 cds_lfht_for_each_entry(sessions_ht->ht, &iter.iter, session,
598 session_n.node) {
599 if (!session_get(session)) {
600 continue;
601 }
602 DBG("session %p refcount %ld session %" PRIu64,
603 session,
604 session->ref.refcount,
605 session->id);
606 session_put(session);
2a174661 607 }
2a174661 608 rcu_read_unlock();
2a174661 609}
This page took 0.06793 seconds and 4 git commands to generate.