1430feef79cb50d9f2e55665e7c5ae5ac405748d
[lttng-tools.git] / src / bin / lttng-relayd / session.cpp
1 /*
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>
5 *
6 * SPDX-License-Identifier: GPL-2.0-only
7 *
8 */
9
10 #define _LGPL_SOURCE
11 #include "ctf-trace.hpp"
12 #include "lttng-relayd.hpp"
13 #include "session.hpp"
14 #include "sessiond-trace-chunks.hpp"
15 #include "stream.hpp"
16 #include "utils.hpp"
17
18 #include <common/common.hpp>
19 #include <common/compat/path.hpp>
20 #include <common/defaults.hpp>
21 #include <common/fd-tracker/utils.hpp>
22 #include <common/time.hpp>
23 #include <common/utils.hpp>
24 #include <common/uuid.hpp>
25
26 #include <sys/stat.h>
27 #include <urcu/rculist.h>
28
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;
32
33 static int init_session_output_path_group_by_host(struct relay_session *session)
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 = nullptr;
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, session->base_path);
62 } else if (session->session_name_contains_creation_time) {
63 ret = asprintf(
64 &session_directory, "%s/%s", session->hostname, session->session_name);
65 } else {
66 char session_creation_datetime[DATETIME_STR_LEN];
67
68 ret = time_to_datetime_str(LTTNG_OPTIONAL_GET(session->creation_time),
69 session_creation_datetime,
70 sizeof(session_creation_datetime));
71 if (ret) {
72 ERR("Failed to format session creation timestamp while initializing session output directory handle");
73 ret = -1;
74 goto end;
75 }
76
77 ret = asprintf(&session_directory,
78 "%s/%s-%s",
79 session->hostname,
80 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
96 end:
97 free(session_directory);
98 return ret;
99 }
100
101 static int init_session_output_path_group_by_session(struct relay_session *session)
102 {
103 /*
104 * session_directory:
105 *
106 * session_name/hostname-creation_time/base_path
107 *
108 * For session name including the datetime, use it as the complete name
109 * since. Do not perform modification on it since the datetime is an
110 * integral part of the name and how a user identify a session.
111 */
112 int ret = 0;
113 char *session_directory = nullptr;
114 char creation_datetime[DATETIME_STR_LEN];
115
116 if (session->output_path[0] != '\0') {
117 /* output_path as been generated already */
118 goto end;
119 }
120
121 ret = time_to_datetime_str(LTTNG_OPTIONAL_GET(session->creation_time),
122 creation_datetime,
123 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,
131 "%s/%s-%s%s%s",
132 session->session_name,
133 session->hostname,
134 creation_datetime,
135 session->base_path[0] != '\0' ? "/" : "",
136 session->base_path);
137 if (ret < 0) {
138 PERROR("Failed to format session directory name");
139 goto end;
140 }
141
142 if (strlen(session_directory) >= LTTNG_PATH_MAX) {
143 ERR("Session output directory exceeds maximal length");
144 ret = -1;
145 goto end;
146 }
147
148 strcpy(session->output_path, session_directory);
149 ret = 0;
150
151 end:
152 free(session_directory);
153 return ret;
154 }
155
156 static int init_session_output_path(struct relay_session *session)
157 {
158 int ret;
159
160 switch (opt_group_output_by) {
161 case RELAYD_GROUP_OUTPUT_BY_HOST:
162 ret = init_session_output_path_group_by_host(session);
163 break;
164 case RELAYD_GROUP_OUTPUT_BY_SESSION:
165 ret = init_session_output_path_group_by_session(session);
166 break;
167 case RELAYD_GROUP_OUTPUT_BY_UNKNOWN:
168 default:
169 abort();
170 break;
171 }
172
173 return ret;
174 }
175
176 static struct lttng_directory_handle *
177 session_create_output_directory_handle(struct relay_session *session)
178 {
179 int ret;
180 /*
181 * relayd_output_path/session_directory
182 * e.g. /home/user/lttng-traces/hostname/session_name
183 */
184 char *full_session_path = nullptr;
185 struct lttng_directory_handle *handle = nullptr;
186
187 pthread_mutex_lock(&session->lock);
188 full_session_path = create_output_path(session->output_path);
189 if (!full_session_path) {
190 goto end;
191 }
192
193 ret = utils_mkdir_recursive(full_session_path, S_IRWXU | S_IRWXG, -1, -1);
194 if (ret) {
195 ERR("Failed to create session output path \"%s\"", full_session_path);
196 goto end;
197 }
198
199 handle = fd_tracker_create_directory_handle(the_fd_tracker, full_session_path);
200 end:
201 pthread_mutex_unlock(&session->lock);
202 free(full_session_path);
203 return handle;
204 }
205
206 static int session_set_anonymous_chunk(struct relay_session *session)
207 {
208 int ret = 0;
209 struct lttng_trace_chunk *chunk = nullptr;
210 enum lttng_trace_chunk_status status;
211 struct lttng_directory_handle *output_directory;
212
213 output_directory = session_create_output_directory_handle(session);
214 if (!output_directory) {
215 goto end;
216 }
217
218 chunk = lttng_trace_chunk_create_anonymous();
219 if (!chunk) {
220 goto end;
221 }
222
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) {
226 ret = -1;
227 goto end;
228 }
229
230 status = lttng_trace_chunk_set_as_owner(chunk, output_directory);
231 if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
232 ret = -1;
233 goto end;
234 }
235
236 session->current_trace_chunk = chunk;
237 chunk = nullptr;
238 end:
239 lttng_trace_chunk_put(chunk);
240 lttng_directory_handle_put(output_directory);
241 return ret;
242 }
243
244 /*
245 * Check if a name is safe to use in a path.
246 *
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),
250 * - Is not empty.
251 */
252 static bool is_name_path_safe(const char *name)
253 {
254 const size_t name_len = strlen(name);
255
256 /* Not empty. */
257 if (name_len == 0) {
258 WARN("An empty name is not allowed to be used in a path");
259 return false;
260 }
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 '.'",
264 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",
270 name);
271 return false;
272 }
273
274 return true;
275 }
276
277 /*
278 * Create a new session by assigning a new session ID.
279 *
280 * Return allocated session or else NULL.
281 */
282 struct relay_session *session_create(const char *session_name,
283 const char *hostname,
284 const char *base_path,
285 uint32_t live_timer,
286 bool snapshot,
287 const lttng_uuid& sessiond_uuid,
288 const uint64_t *id_sessiond,
289 const uint64_t *current_chunk_id,
290 const time_t *creation_time,
291 uint32_t major,
292 uint32_t minor,
293 bool session_name_contains_creation_time)
294 {
295 int ret;
296 struct relay_session *session = nullptr;
297
298 LTTNG_ASSERT(session_name);
299 LTTNG_ASSERT(hostname);
300 LTTNG_ASSERT(base_path);
301
302 if (!is_name_path_safe(session_name)) {
303 ERR("Refusing to create session as the provided session name is not path-safe");
304 goto error;
305 }
306 if (!is_name_path_safe(hostname)) {
307 ERR("Refusing to create session as the provided hostname is not path-safe");
308 goto error;
309 }
310 if (strstr(base_path, "../")) {
311 ERR("Invalid session base path walks up the path hierarchy: \"%s\"", base_path);
312 goto error;
313 }
314
315 session = zmalloc<relay_session>();
316 if (!session) {
317 PERROR("Failed to allocate session");
318 goto error;
319 }
320
321 pthread_mutex_lock(&last_relay_session_id_lock);
322 session->id = ++last_relay_session_id;
323 pthread_mutex_unlock(&last_relay_session_id_lock);
324
325 lttng_ht_node_init_u64(&session->session_n, session->id);
326 urcu_ref_init(&session->ref);
327 CDS_INIT_LIST_HEAD(&session->recv_list);
328 pthread_mutex_init(&session->lock, nullptr);
329 pthread_mutex_init(&session->recv_list_lock, nullptr);
330
331 if (lttng_strncpy(session->session_name, session_name, sizeof(session->session_name))) {
332 WARN("Session name exceeds maximal allowed length");
333 goto error;
334 }
335 if (lttng_strncpy(session->hostname, hostname, sizeof(session->hostname))) {
336 WARN("Hostname exceeds maximal allowed length");
337 goto error;
338 }
339 if (lttng_strncpy(session->base_path, base_path, sizeof(session->base_path))) {
340 WARN("Base path exceeds maximal allowed length");
341 goto error;
342 }
343 if (creation_time) {
344 LTTNG_OPTIONAL_SET(&session->creation_time, *creation_time);
345 } else {
346 LTTNG_OPTIONAL_SET(&session->creation_time, time(nullptr));
347 if (session->creation_time.value == (time_t) -1) {
348 PERROR("Failed to sample session creation time");
349 goto error;
350 }
351 }
352 session->session_name_contains_creation_time = session_name_contains_creation_time;
353
354 session->ctf_traces_ht = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
355 if (!session->ctf_traces_ht) {
356 goto error;
357 }
358
359 session->major = major;
360 session->minor = minor;
361
362 session->live_timer = live_timer;
363 session->snapshot = snapshot;
364 session->sessiond_uuid = sessiond_uuid;
365
366 if (id_sessiond) {
367 LTTNG_OPTIONAL_SET(&session->id_sessiond, *id_sessiond);
368 }
369
370 if (major == 2 && minor >= 11) {
371 /* Only applies for 2.11+ peers using trace chunks. */
372 ret = init_session_output_path(session);
373 if (ret) {
374 goto error;
375 }
376 }
377
378 ret = sessiond_trace_chunk_registry_session_created(sessiond_trace_chunk_registry,
379 sessiond_uuid);
380 if (ret) {
381 goto error;
382 }
383
384 if (id_sessiond && current_chunk_id) {
385 enum lttng_trace_chunk_status chunk_status;
386 struct lttng_directory_handle *session_output_directory;
387
388 session->current_trace_chunk =
389 sessiond_trace_chunk_registry_get_chunk(sessiond_trace_chunk_registry,
390 session->sessiond_uuid,
391 session->id_sessiond.value,
392 *current_chunk_id);
393 if (!session->current_trace_chunk) {
394 char uuid_str[LTTNG_UUID_STR_LEN];
395
396 lttng_uuid_to_str(sessiond_uuid, uuid_str);
397 ERR("Could not find trace chunk: sessiond = {%s}, sessiond session id = %" PRIu64
398 ", trace chunk id = %" PRIu64,
399 uuid_str,
400 *id_sessiond,
401 *current_chunk_id);
402 goto error;
403 }
404
405 chunk_status = lttng_trace_chunk_get_session_output_directory_handle(
406 session->current_trace_chunk, &session_output_directory);
407 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
408 goto error;
409 }
410
411 LTTNG_ASSERT(session_output_directory);
412 session->output_directory = session_output_directory;
413 } else if (!id_sessiond) {
414 /*
415 * Pre-2.11 peers will not announce trace chunks. An
416 * anonymous trace chunk which will remain set for the
417 * duration of the session is created.
418 */
419 ret = session_set_anonymous_chunk(session);
420 if (ret) {
421 goto error;
422 }
423 } else {
424 session->output_directory = session_create_output_directory_handle(session);
425 if (!session->output_directory) {
426 goto error;
427 }
428 }
429
430 lttng_ht_add_unique_u64(sessions_ht, &session->session_n);
431 return session;
432
433 error:
434 session_put(session);
435 return nullptr;
436 }
437
438 /* Should be called with RCU read-side lock held. */
439 bool session_get(struct relay_session *session)
440 {
441 return urcu_ref_get_unless_zero(&session->ref);
442 }
443
444 /*
445 * Lookup a session within the session hash table using the session id
446 * as key. A session reference is taken when a session is returned.
447 * session_put() must be called on that session.
448 *
449 * Return session or NULL if not found.
450 */
451 struct relay_session *session_get_by_id(uint64_t id)
452 {
453 struct relay_session *session = nullptr;
454 struct lttng_ht_node_u64 *node;
455 struct lttng_ht_iter iter;
456
457 rcu_read_lock();
458 lttng_ht_lookup(sessions_ht, &id, &iter);
459 node = lttng_ht_iter_get_node_u64(&iter);
460 if (!node) {
461 DBG("Session find by ID %" PRIu64 " id NOT found", id);
462 goto end;
463 }
464 session = lttng::utils::container_of(node, &relay_session::session_n);
465 DBG("Session find by ID %" PRIu64 " id found", id);
466 if (!session_get(session)) {
467 session = nullptr;
468 }
469 end:
470 rcu_read_unlock();
471 return session;
472 }
473
474 /*
475 * Check if any of the relay sessions originating from the same
476 * session daemon session have the 'ongoing_rotation' state set.
477 *
478 * The caller must hold the lock of session.
479 */
480 bool session_has_ongoing_rotation(const struct relay_session *session)
481 {
482 bool ongoing_rotation = false;
483 struct lttng_ht_iter iter;
484 struct relay_session *iterated_session;
485
486 ASSERT_LOCKED(session->lock);
487
488 if (!session->id_sessiond.is_set) {
489 /*
490 * The peer that created this session is too old to
491 * support rotations; we can assume no rotations are ongoing.
492 */
493 goto end;
494 }
495
496 if (session->ongoing_rotation) {
497 ongoing_rotation = true;
498 goto end;
499 }
500
501 rcu_read_lock();
502 /*
503 * Sample the 'ongoing_rotation' status of all relay sessions that
504 * originate from the same session daemon session.
505 */
506 cds_lfht_for_each_entry (sessions_ht->ht, &iter.iter, iterated_session, session_n.node) {
507 if (!session_get(iterated_session)) {
508 continue;
509 }
510
511 if (session == iterated_session) {
512 /* Skip this session. */
513 goto next_session_no_unlock;
514 }
515
516 pthread_mutex_lock(&iterated_session->lock);
517
518 if (!iterated_session->id_sessiond.is_set) {
519 /*
520 * Session belongs to a peer that doesn't support
521 * rotations.
522 */
523 goto next_session;
524 }
525
526 if (session->sessiond_uuid != iterated_session->sessiond_uuid) {
527 /* Sessions do not originate from the same sessiond. */
528 goto next_session;
529 }
530
531 if (LTTNG_OPTIONAL_GET(session->id_sessiond) !=
532 LTTNG_OPTIONAL_GET(iterated_session->id_sessiond)) {
533 /*
534 * Sessions do not originate from the same sessiond
535 * session.
536 */
537 goto next_session;
538 }
539
540 ongoing_rotation = iterated_session->ongoing_rotation;
541
542 next_session:
543 pthread_mutex_unlock(&iterated_session->lock);
544 next_session_no_unlock:
545 session_put(iterated_session);
546
547 if (ongoing_rotation) {
548 break;
549 }
550 }
551 rcu_read_unlock();
552
553 end:
554 return ongoing_rotation;
555 }
556
557 static void rcu_destroy_session(struct rcu_head *rcu_head)
558 {
559 struct relay_session *session = caa_container_of(rcu_head, struct relay_session, rcu_node);
560 /*
561 * Since each trace has a reference on the session, it means
562 * that if we are at the point where we teardown the session, no
563 * trace belonging to that session exist at this point.
564 * Calling lttng_ht_destroy in call_rcu worker thread so we
565 * don't hold the RCU read-side lock while calling it.
566 */
567 lttng_ht_destroy(session->ctf_traces_ht);
568 free(session);
569 }
570
571 /*
572 * Delete session from the given hash table.
573 *
574 * Return lttng ht del error code being 0 on success and 1 on failure.
575 */
576 static int session_delete(struct relay_session *session)
577 {
578 struct lttng_ht_iter iter;
579
580 iter.iter.node = &session->session_n.node;
581 return lttng_ht_del(sessions_ht, &iter);
582 }
583
584 static void destroy_session(struct relay_session *session)
585 {
586 int ret;
587
588 ret = session_delete(session);
589 LTTNG_ASSERT(!ret);
590 lttng_trace_chunk_put(session->current_trace_chunk);
591 session->current_trace_chunk = nullptr;
592 lttng_trace_chunk_put(session->pending_closure_trace_chunk);
593 session->pending_closure_trace_chunk = nullptr;
594 ret = sessiond_trace_chunk_registry_session_destroyed(sessiond_trace_chunk_registry,
595 session->sessiond_uuid);
596 LTTNG_ASSERT(!ret);
597 lttng_directory_handle_put(session->output_directory);
598 session->output_directory = nullptr;
599 call_rcu(&session->rcu_node, rcu_destroy_session);
600 }
601
602 static void session_release(struct urcu_ref *ref)
603 {
604 struct relay_session *session = lttng::utils::container_of(ref, &relay_session::ref);
605
606 destroy_session(session);
607 }
608
609 void session_put(struct relay_session *session)
610 {
611 if (!session) {
612 return;
613 }
614 rcu_read_lock();
615 urcu_ref_put(&session->ref, session_release);
616 rcu_read_unlock();
617 }
618
619 int session_close(struct relay_session *session)
620 {
621 int ret = 0;
622 struct ctf_trace *trace;
623 struct lttng_ht_iter iter;
624 struct relay_stream *stream;
625
626 pthread_mutex_lock(&session->lock);
627 DBG("closing session %" PRIu64 ": is conn already closed %d",
628 session->id,
629 session->connection_closed);
630 session->connection_closed = true;
631 pthread_mutex_unlock(&session->lock);
632
633 rcu_read_lock();
634 cds_lfht_for_each_entry (session->ctf_traces_ht->ht, &iter.iter, trace, node.node) {
635 ret = ctf_trace_close(trace);
636 if (ret) {
637 goto rcu_unlock;
638 }
639 }
640 cds_list_for_each_entry_rcu(stream, &session->recv_list, recv_node)
641 {
642 /* Close streams which have not been published yet. */
643 try_stream_close(stream);
644 }
645 rcu_unlock:
646 rcu_read_unlock();
647 if (ret) {
648 return ret;
649 }
650 /* Put self-reference from create. */
651 session_put(session);
652 return ret;
653 }
654
655 int session_abort(struct relay_session *session)
656 {
657 int ret = 0;
658
659 if (!session) {
660 return 0;
661 }
662
663 pthread_mutex_lock(&session->lock);
664 DBG("aborting session %" PRIu64, session->id);
665 session->aborted = true;
666 pthread_mutex_unlock(&session->lock);
667 return ret;
668 }
669
670 void print_sessions()
671 {
672 struct lttng_ht_iter iter;
673 struct relay_session *session;
674
675 if (!sessions_ht) {
676 return;
677 }
678
679 rcu_read_lock();
680 cds_lfht_for_each_entry (sessions_ht->ht, &iter.iter, session, session_n.node) {
681 if (!session_get(session)) {
682 continue;
683 }
684 DBG("session %p refcount %ld session %" PRIu64,
685 session,
686 session->ref.refcount,
687 session->id);
688 session_put(session);
689 }
690 rcu_read_unlock();
691 }
This page took 0.042025 seconds and 4 git commands to generate.