3c9f787dd59601bc2603e5fdf4c5523f86e57125
[lttng-tools.git] / src / bin / lttng-sessiond / session.cpp
1 /*
2 * Copyright (C) 2011 EfficiOS Inc.
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #define _LGPL_SOURCE
9 #include "cmd.hpp"
10 #include "kernel.hpp"
11 #include "lttng-sessiond.hpp"
12 #include "session.hpp"
13 #include "timer.hpp"
14 #include "trace-ust.hpp"
15 #include "utils.hpp"
16
17 #include <common/common.hpp>
18 #include <common/sessiond-comm/sessiond-comm.hpp>
19 #include <common/trace-chunk.hpp>
20 #include <common/urcu.hpp>
21 #include <common/utils.hpp>
22
23 #include <lttng/location-internal.hpp>
24
25 #include <dirent.h>
26 #include <inttypes.h>
27 #include <limits.h>
28 #include <pthread.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <urcu.h>
35
36 namespace {
37 struct ltt_session_destroy_notifier_element {
38 ltt_session_destroy_notifier notifier;
39 void *user_data;
40 };
41
42 struct ltt_session_clear_notifier_element {
43 ltt_session_clear_notifier notifier;
44 void *user_data;
45 };
46
47 namespace ls = lttng::sessiond;
48
49 /*
50 * NOTES:
51 *
52 * No ltt_session.lock is taken here because those data structure are widely
53 * spread across the lttng-tools code base so before calling functions below
54 * that can read/write a session, the caller MUST acquire the session lock
55 * using session_lock() and session_unlock().
56 */
57
58 /* These characters are forbidden in a session name. Used by validate_name. */
59 const char *forbidden_name_chars = "/";
60
61 /* Global hash table to keep the sessions, indexed by id. */
62 struct lttng_ht *ltt_sessions_ht_by_id = nullptr;
63 /* Global hash table to keep the sessions, indexed by name. */
64 struct lttng_ht *ltt_sessions_ht_by_name = nullptr;
65
66 /*
67 * Init tracing session list.
68 *
69 * Please see session.h for more explanation and correct usage of the list.
70 */
71 struct ltt_session_list the_session_list;
72 } /* namespace */
73
74 /*
75 * Validate the session name for forbidden characters.
76 *
77 * Return 0 on success else -1 meaning a forbidden char. has been found.
78 */
79 static int validate_name(const char *name)
80 {
81 int ret;
82 char *tok, *tmp_name;
83
84 LTTNG_ASSERT(name);
85
86 tmp_name = strdup(name);
87 if (!tmp_name) {
88 /* ENOMEM here. */
89 ret = -1;
90 goto error;
91 }
92
93 tok = strpbrk(tmp_name, forbidden_name_chars);
94 if (tok) {
95 DBG("Session name %s contains a forbidden character", name);
96 /* Forbidden character has been found. */
97 ret = -1;
98 goto error;
99 }
100 ret = 0;
101
102 error:
103 free(tmp_name);
104 return ret;
105 }
106
107 /*
108 * Add a ltt_session structure to the global list.
109 *
110 * The caller MUST acquire the session list lock before.
111 * Returns the unique identifier for the session.
112 */
113 static uint64_t add_session_list(struct ltt_session *ls)
114 {
115 LTTNG_ASSERT(ls);
116
117 cds_list_add(&ls->list, &the_session_list.head);
118 return the_session_list.next_uuid++;
119 }
120
121 /*
122 * Delete a ltt_session structure to the global list.
123 *
124 * The caller MUST acquire the session list lock before.
125 */
126 static void del_session_list(struct ltt_session *ls)
127 {
128 LTTNG_ASSERT(ls);
129
130 cds_list_del(&ls->list);
131 }
132
133 /*
134 * Return a pointer to the session list.
135 */
136 struct ltt_session_list *session_get_list()
137 {
138 return &the_session_list;
139 }
140
141 /*
142 * Returns once the session list is empty.
143 */
144 void session_list_wait_empty()
145 {
146 std::unique_lock<std::mutex> list_lock(the_session_list.lock);
147
148 /* Keep waiting until the session list is empty. */
149 the_session_list.removal_cond.wait(list_lock,
150 [] { return cds_list_empty(&the_session_list.head); });
151 }
152
153 /*
154 * Acquire session list lock
155 */
156 void session_lock_list() noexcept
157 {
158 the_session_list.lock.lock();
159 }
160
161 /*
162 * Try to acquire session list lock
163 */
164 int session_trylock_list() noexcept
165 {
166 /* Return 0 if successfully acquired. */
167 return the_session_list.lock.try_lock() ? 0 : 1;
168 }
169
170 /*
171 * Release session list lock
172 */
173 void session_unlock_list() noexcept
174 {
175 the_session_list.lock.unlock();
176 }
177
178 /*
179 * Get the session's consumer destination type.
180 *
181 * The caller must hold the session lock.
182 */
183 enum consumer_dst_type session_get_consumer_destination_type(const struct ltt_session *session)
184 {
185 /*
186 * The output information is duplicated in both of those session types.
187 * Hence, it doesn't matter from which it is retrieved. However, it is
188 * possible for only one of them to be set.
189 */
190 return session->kernel_session ? session->kernel_session->consumer->type :
191 session->ust_session->consumer->type;
192 }
193
194 /*
195 * Get the session's consumer network hostname.
196 * The caller must ensure that the destination is of type "net".
197 *
198 * The caller must hold the session lock.
199 */
200 const char *session_get_net_consumer_hostname(const struct ltt_session *session)
201 {
202 const char *hostname = nullptr;
203 const struct consumer_output *output;
204
205 output = session->kernel_session ? session->kernel_session->consumer :
206 session->ust_session->consumer;
207
208 /*
209 * hostname is assumed to be the same for both control and data
210 * connections.
211 */
212 switch (output->dst.net.control.dtype) {
213 case LTTNG_DST_IPV4:
214 hostname = output->dst.net.control.dst.ipv4;
215 break;
216 case LTTNG_DST_IPV6:
217 hostname = output->dst.net.control.dst.ipv6;
218 break;
219 default:
220 abort();
221 }
222 return hostname;
223 }
224
225 /*
226 * Get the session's consumer network control and data ports.
227 * The caller must ensure that the destination is of type "net".
228 *
229 * The caller must hold the session lock.
230 */
231 void session_get_net_consumer_ports(const struct ltt_session *session,
232 uint16_t *control_port,
233 uint16_t *data_port)
234 {
235 const struct consumer_output *output;
236
237 output = session->kernel_session ? session->kernel_session->consumer :
238 session->ust_session->consumer;
239 *control_port = output->dst.net.control.port;
240 *data_port = output->dst.net.data.port;
241 }
242
243 /*
244 * Get the location of the latest trace archive produced by a rotation.
245 *
246 * The caller must hold the session lock.
247 */
248 struct lttng_trace_archive_location *
249 session_get_trace_archive_location(const struct ltt_session *session)
250 {
251 int ret;
252 struct lttng_trace_archive_location *location = nullptr;
253 char *chunk_path = nullptr;
254
255 if (session->rotation_state != LTTNG_ROTATION_STATE_COMPLETED ||
256 !session->last_archived_chunk_name) {
257 goto end;
258 }
259
260 switch (session_get_consumer_destination_type(session)) {
261 case CONSUMER_DST_LOCAL:
262 ret = asprintf(&chunk_path,
263 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY "/%s",
264 session_get_base_path(session),
265 session->last_archived_chunk_name);
266 if (ret == -1) {
267 goto end;
268 }
269 location = lttng_trace_archive_location_local_create(chunk_path);
270 break;
271 case CONSUMER_DST_NET:
272 {
273 const char *hostname;
274 uint16_t control_port, data_port;
275
276 hostname = session_get_net_consumer_hostname(session);
277 session_get_net_consumer_ports(session, &control_port, &data_port);
278 location = lttng_trace_archive_location_relay_create(
279 hostname,
280 LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP,
281 control_port,
282 data_port,
283 session->last_chunk_path);
284 break;
285 }
286 default:
287 abort();
288 }
289 end:
290 free(chunk_path);
291 return location;
292 }
293
294 /*
295 * Allocate the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name HT.
296 *
297 * The session list lock must be held.
298 */
299 static int ltt_sessions_ht_alloc()
300 {
301 int ret = 0;
302
303 DBG("Allocating ltt_sessions_ht_by_id");
304 ltt_sessions_ht_by_id = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
305 if (!ltt_sessions_ht_by_id) {
306 ret = -1;
307 ERR("Failed to allocate ltt_sessions_ht_by_id");
308 goto end;
309 }
310
311 DBG("Allocating ltt_sessions_ht_by_name");
312 ltt_sessions_ht_by_name = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
313 if (!ltt_sessions_ht_by_name) {
314 ret = -1;
315 ERR("Failed to allocate ltt_sessions_ht_by_name");
316 goto end;
317 }
318
319 end:
320 return ret;
321 }
322
323 /*
324 * Destroy the ltt_sessions_ht_by_id HT.
325 *
326 * The session list lock must be held.
327 */
328 static void ltt_sessions_ht_destroy()
329 {
330 if (ltt_sessions_ht_by_id) {
331 lttng_ht_destroy(ltt_sessions_ht_by_id);
332 ltt_sessions_ht_by_id = nullptr;
333 }
334
335 if (ltt_sessions_ht_by_name) {
336 lttng_ht_destroy(ltt_sessions_ht_by_name);
337 ltt_sessions_ht_by_name = nullptr;
338 }
339
340 return;
341 }
342
343 /*
344 * Add a ltt_session to the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name.
345 * If unallocated, the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name. HTs
346 * are allocated. The session list lock must be held.
347 */
348 static void add_session_ht(struct ltt_session *ls)
349 {
350 int ret;
351
352 LTTNG_ASSERT(ls);
353
354 if (!ltt_sessions_ht_by_id) {
355 ret = ltt_sessions_ht_alloc();
356 if (ret) {
357 ERR("Error allocating the sessions HT");
358 goto end;
359 }
360 }
361
362 /* Should always be present with ltt_sessions_ht_by_id. */
363 LTTNG_ASSERT(ltt_sessions_ht_by_name);
364
365 lttng_ht_node_init_u64(&ls->node, ls->id);
366 lttng_ht_add_unique_u64(ltt_sessions_ht_by_id, &ls->node);
367
368 lttng_ht_node_init_str(&ls->node_by_name, ls->name);
369 lttng_ht_add_unique_str(ltt_sessions_ht_by_name, &ls->node_by_name);
370
371 end:
372 return;
373 }
374
375 /*
376 * Test if ltt_sessions_ht_by_id/name are empty.
377 * Return `false` if hash table objects are null.
378 * The session list lock must be held.
379 */
380 static bool ltt_sessions_ht_empty()
381 {
382 bool empty = false;
383
384 if (!ltt_sessions_ht_by_id) {
385 /* The hash tables do not exist yet. */
386 goto end;
387 }
388
389 LTTNG_ASSERT(ltt_sessions_ht_by_name);
390
391 if (lttng_ht_get_count(ltt_sessions_ht_by_id) != 0) {
392 /* Not empty.*/
393 goto end;
394 }
395
396 /*
397 * At this point it is expected that the `ltt_sessions_ht_by_name` ht is
398 * empty.
399 *
400 * The removal from both hash tables is done in two different
401 * places:
402 * - removal from `ltt_sessions_ht_by_name` is done during
403 * `session_destroy()`
404 * - removal from `ltt_sessions_ht_by_id` is done later
405 * in `session_release()` on the last reference put.
406 *
407 * This means that it is possible for `ltt_sessions_ht_by_name` to be
408 * empty but for `ltt_sessions_ht_by_id` to still contain objects when
409 * multiple sessions exists. The reverse is false, hence this sanity
410 * check.
411 */
412 LTTNG_ASSERT(lttng_ht_get_count(ltt_sessions_ht_by_name) == 0);
413 empty = true;
414 end:
415 return empty;
416 }
417
418 /*
419 * Remove a ltt_session from the ltt_sessions_ht_by_id.
420 * If empty, the ltt_sessions_ht_by_id/name HTs are freed.
421 * The session list lock must be held.
422 */
423 static void del_session_ht(struct ltt_session *ls)
424 {
425 struct lttng_ht_iter iter;
426 int ret;
427
428 LTTNG_ASSERT(ls);
429 LTTNG_ASSERT(ltt_sessions_ht_by_id);
430 LTTNG_ASSERT(ltt_sessions_ht_by_name);
431
432 iter.iter.node = &ls->node.node;
433 ret = lttng_ht_del(ltt_sessions_ht_by_id, &iter);
434 LTTNG_ASSERT(!ret);
435
436 if (ltt_sessions_ht_empty()) {
437 DBG("Empty ltt_sessions_ht_by_id/name, destroying hast tables");
438 ltt_sessions_ht_destroy();
439 }
440 }
441
442 /*
443 * Acquire session lock
444 */
445 void session_lock(struct ltt_session *session)
446 {
447 LTTNG_ASSERT(session);
448
449 pthread_mutex_lock(&session->lock);
450 }
451
452 /*
453 * Release session lock
454 */
455 void session_unlock(struct ltt_session *session)
456 {
457 LTTNG_ASSERT(session);
458
459 pthread_mutex_unlock(&session->lock);
460 }
461
462 static int _session_set_trace_chunk_no_lock_check(struct ltt_session *session,
463 struct lttng_trace_chunk *new_trace_chunk,
464 struct lttng_trace_chunk **_current_trace_chunk)
465 {
466 int ret = 0;
467 unsigned int i, refs_to_acquire = 0, refs_acquired = 0, refs_to_release = 0;
468 struct cds_lfht_iter iter;
469 struct consumer_socket *socket;
470 struct lttng_trace_chunk *current_trace_chunk;
471 uint64_t chunk_id;
472 enum lttng_trace_chunk_status chunk_status;
473
474 lttng::urcu::read_lock_guard read_lock;
475 /*
476 * Ownership of current trace chunk is transferred to
477 * `current_trace_chunk`.
478 */
479 current_trace_chunk = session->current_trace_chunk;
480 session->current_trace_chunk = nullptr;
481 if (session->ust_session) {
482 lttng_trace_chunk_put(session->ust_session->current_trace_chunk);
483 session->ust_session->current_trace_chunk = nullptr;
484 }
485 if (session->kernel_session) {
486 lttng_trace_chunk_put(session->kernel_session->current_trace_chunk);
487 session->kernel_session->current_trace_chunk = nullptr;
488 }
489 if (!new_trace_chunk) {
490 ret = 0;
491 goto end;
492 }
493 chunk_status = lttng_trace_chunk_get_id(new_trace_chunk, &chunk_id);
494 LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
495
496 refs_to_acquire = 1;
497 refs_to_acquire += !!session->ust_session;
498 refs_to_acquire += !!session->kernel_session;
499
500 for (refs_acquired = 0; refs_acquired < refs_to_acquire; refs_acquired++) {
501 if (!lttng_trace_chunk_get(new_trace_chunk)) {
502 ERR("Failed to acquire reference to new trace chunk of session \"%s\"",
503 session->name);
504 goto error;
505 }
506 }
507
508 if (session->ust_session) {
509 const uint64_t relayd_id = session->ust_session->consumer->net_seq_index;
510 const bool is_local_trace = session->ust_session->consumer->type ==
511 CONSUMER_DST_LOCAL;
512
513 session->ust_session->current_trace_chunk = new_trace_chunk;
514 if (is_local_trace) {
515 enum lttng_error_code ret_error_code;
516
517 ret_error_code =
518 ust_app_create_channel_subdirectories(session->ust_session);
519 if (ret_error_code != LTTNG_OK) {
520 goto error;
521 }
522 }
523 cds_lfht_for_each_entry (
524 session->ust_session->consumer->socks->ht, &iter, socket, node.node) {
525 pthread_mutex_lock(socket->lock);
526 ret = consumer_create_trace_chunk(socket,
527 relayd_id,
528 session->id,
529 new_trace_chunk,
530 DEFAULT_UST_TRACE_DIR);
531 pthread_mutex_unlock(socket->lock);
532 if (ret) {
533 goto error;
534 }
535 }
536 }
537 if (session->kernel_session) {
538 const uint64_t relayd_id = session->kernel_session->consumer->net_seq_index;
539 const bool is_local_trace = session->kernel_session->consumer->type ==
540 CONSUMER_DST_LOCAL;
541
542 session->kernel_session->current_trace_chunk = new_trace_chunk;
543 if (is_local_trace) {
544 enum lttng_error_code ret_error_code;
545
546 ret_error_code =
547 kernel_create_channel_subdirectories(session->kernel_session);
548 if (ret_error_code != LTTNG_OK) {
549 goto error;
550 }
551 }
552 cds_lfht_for_each_entry (
553 session->kernel_session->consumer->socks->ht, &iter, socket, node.node) {
554 pthread_mutex_lock(socket->lock);
555 ret = consumer_create_trace_chunk(socket,
556 relayd_id,
557 session->id,
558 new_trace_chunk,
559 DEFAULT_KERNEL_TRACE_DIR);
560 pthread_mutex_unlock(socket->lock);
561 if (ret) {
562 goto error;
563 }
564 }
565 }
566
567 /*
568 * Update local current trace chunk state last, only if all remote
569 * creations succeeded.
570 */
571 session->current_trace_chunk = new_trace_chunk;
572 LTTNG_OPTIONAL_SET(&session->most_recent_chunk_id, chunk_id);
573 end:
574 if (_current_trace_chunk) {
575 *_current_trace_chunk = current_trace_chunk;
576 current_trace_chunk = nullptr;
577 }
578 end_no_move:
579 lttng_trace_chunk_put(current_trace_chunk);
580 return ret;
581 error:
582 if (session->ust_session) {
583 session->ust_session->current_trace_chunk = nullptr;
584 }
585 if (session->kernel_session) {
586 session->kernel_session->current_trace_chunk = nullptr;
587 }
588 /*
589 * Release references taken in the case where all references could not
590 * be acquired.
591 */
592 refs_to_release = refs_to_acquire - refs_acquired;
593 for (i = 0; i < refs_to_release; i++) {
594 lttng_trace_chunk_put(new_trace_chunk);
595 }
596 ret = -1;
597 goto end_no_move;
598 }
599
600 struct lttng_trace_chunk *
601 session_create_new_trace_chunk(const struct ltt_session *session,
602 const struct consumer_output *consumer_output_override,
603 const char *session_base_path_override,
604 const char *chunk_name_override)
605 {
606 int ret;
607 struct lttng_trace_chunk *trace_chunk = nullptr;
608 enum lttng_trace_chunk_status chunk_status;
609 const time_t chunk_creation_ts = time(nullptr);
610 bool is_local_trace;
611 const char *base_path;
612 struct lttng_directory_handle *session_output_directory = nullptr;
613 const struct lttng_credentials session_credentials = {
614 .uid = LTTNG_OPTIONAL_INIT_VALUE(session->uid),
615 .gid = LTTNG_OPTIONAL_INIT_VALUE(session->gid),
616 };
617 uint64_t next_chunk_id;
618 const struct consumer_output *output;
619 const char *new_path;
620
621 if (consumer_output_override) {
622 output = consumer_output_override;
623 } else {
624 LTTNG_ASSERT(session->ust_session || session->kernel_session);
625 output = session->ust_session ? session->ust_session->consumer :
626 session->kernel_session->consumer;
627 }
628
629 is_local_trace = output->type == CONSUMER_DST_LOCAL;
630 base_path = session_base_path_override ?: consumer_output_get_base_path(output);
631
632 if (chunk_creation_ts == (time_t) -1) {
633 PERROR("Failed to sample time while creation session \"%s\" trace chunk",
634 session->name);
635 goto error;
636 }
637
638 next_chunk_id =
639 session->most_recent_chunk_id.is_set ? session->most_recent_chunk_id.value + 1 : 0;
640
641 if (session->current_trace_chunk &&
642 !lttng_trace_chunk_get_name_overridden(session->current_trace_chunk)) {
643 chunk_status = lttng_trace_chunk_rename_path(session->current_trace_chunk,
644 DEFAULT_CHUNK_TMP_OLD_DIRECTORY);
645 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
646 goto error;
647 }
648 }
649 if (!session->current_trace_chunk) {
650 if (!session->rotated) {
651 new_path = "";
652 } else {
653 new_path = nullptr;
654 }
655 } else {
656 new_path = DEFAULT_CHUNK_TMP_NEW_DIRECTORY;
657 }
658
659 trace_chunk = lttng_trace_chunk_create(next_chunk_id, chunk_creation_ts, new_path);
660 if (!trace_chunk) {
661 goto error;
662 }
663
664 if (chunk_name_override) {
665 chunk_status = lttng_trace_chunk_override_name(trace_chunk, chunk_name_override);
666 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
667 goto error;
668 }
669 }
670
671 if (!is_local_trace) {
672 /*
673 * No need to set crendentials and output directory
674 * for remote trace chunks.
675 */
676 goto end;
677 }
678
679 chunk_status = lttng_trace_chunk_set_credentials(trace_chunk, &session_credentials);
680 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
681 goto error;
682 }
683
684 DBG("Creating base output directory of session \"%s\" at %s", session->name, base_path);
685 ret = utils_mkdir_recursive(base_path, S_IRWXU | S_IRWXG, session->uid, session->gid);
686 if (ret) {
687 goto error;
688 }
689 session_output_directory = lttng_directory_handle_create(base_path);
690 if (!session_output_directory) {
691 goto error;
692 }
693 chunk_status = lttng_trace_chunk_set_as_owner(trace_chunk, session_output_directory);
694 lttng_directory_handle_put(session_output_directory);
695 session_output_directory = nullptr;
696 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
697 goto error;
698 }
699 end:
700 return trace_chunk;
701 error:
702 lttng_directory_handle_put(session_output_directory);
703 lttng_trace_chunk_put(trace_chunk);
704 trace_chunk = nullptr;
705 goto end;
706 }
707
708 int session_close_trace_chunk(struct ltt_session *session,
709 struct lttng_trace_chunk *trace_chunk,
710 enum lttng_trace_chunk_command_type close_command,
711 char *closed_trace_chunk_path)
712 {
713 int ret = 0;
714 bool error_occurred = false;
715 struct cds_lfht_iter iter;
716 struct consumer_socket *socket;
717 enum lttng_trace_chunk_status chunk_status;
718 const time_t chunk_close_timestamp = time(nullptr);
719 const char *new_path;
720
721 chunk_status = lttng_trace_chunk_set_close_command(trace_chunk, close_command);
722 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
723 ret = -1;
724 goto end;
725 }
726
727 if (chunk_close_timestamp == (time_t) -1) {
728 ERR("Failed to sample the close timestamp of the current trace chunk of session \"%s\"",
729 session->name);
730 ret = -1;
731 goto end;
732 }
733
734 if (close_command == LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE && !session->rotated) {
735 /* New chunk stays in session output directory. */
736 new_path = "";
737 } else {
738 /* Use chunk name for new chunk. */
739 new_path = nullptr;
740 }
741 if (session->current_trace_chunk &&
742 !lttng_trace_chunk_get_name_overridden(session->current_trace_chunk)) {
743 /* Rename new chunk path. */
744 chunk_status =
745 lttng_trace_chunk_rename_path(session->current_trace_chunk, new_path);
746 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
747 ret = -1;
748 goto end;
749 }
750 }
751 if (!lttng_trace_chunk_get_name_overridden(trace_chunk) &&
752 close_command == LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION) {
753 const char *old_path;
754
755 if (!session->rotated) {
756 old_path = "";
757 } else {
758 old_path = nullptr;
759 }
760 /* We need to move back the .tmp_old_chunk to its rightful place. */
761 chunk_status = lttng_trace_chunk_rename_path(trace_chunk, old_path);
762 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
763 ret = -1;
764 goto end;
765 }
766 }
767 if (close_command == LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED) {
768 session->rotated = true;
769 }
770 chunk_status = lttng_trace_chunk_set_close_timestamp(trace_chunk, chunk_close_timestamp);
771 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
772 ERR("Failed to set the close timestamp of the current trace chunk of session \"%s\"",
773 session->name);
774 ret = -1;
775 goto end;
776 }
777
778 if (session->ust_session) {
779 const uint64_t relayd_id = session->ust_session->consumer->net_seq_index;
780
781 cds_lfht_for_each_entry (
782 session->ust_session->consumer->socks->ht, &iter, socket, node.node) {
783 pthread_mutex_lock(socket->lock);
784 ret = consumer_close_trace_chunk(socket,
785 relayd_id,
786 session->id,
787 trace_chunk,
788 closed_trace_chunk_path);
789 pthread_mutex_unlock(socket->lock);
790 if (ret) {
791 ERR("Failed to close trace chunk on user space consumer");
792 error_occurred = true;
793 }
794 }
795 }
796 if (session->kernel_session) {
797 const uint64_t relayd_id = session->kernel_session->consumer->net_seq_index;
798
799 cds_lfht_for_each_entry (
800 session->kernel_session->consumer->socks->ht, &iter, socket, node.node) {
801 pthread_mutex_lock(socket->lock);
802 ret = consumer_close_trace_chunk(socket,
803 relayd_id,
804 session->id,
805 trace_chunk,
806 closed_trace_chunk_path);
807 pthread_mutex_unlock(socket->lock);
808 if (ret) {
809 ERR("Failed to close trace chunk on kernel consumer");
810 error_occurred = true;
811 }
812 }
813 }
814 ret = error_occurred ? -1 : 0;
815 end:
816 return ret;
817 }
818
819 /*
820 * This function skips the metadata channel as the begin/end timestamps of a
821 * metadata packet are useless.
822 *
823 * Moreover, opening a packet after a "clear" will cause problems for live
824 * sessions as it will introduce padding that was not part of the first trace
825 * chunk. The relay daemon expects the content of the metadata stream of
826 * successive metadata trace chunks to be strict supersets of one another.
827 *
828 * For example, flushing a packet at the beginning of the metadata stream of
829 * a trace chunk resulting from a "clear" session command will cause the
830 * size of the metadata stream of the new trace chunk to not match the size of
831 * the metadata stream of the original chunk. This will confuse the relay
832 * daemon as the same "offset" in a metadata stream will no longer point
833 * to the same content.
834 */
835 static enum lttng_error_code session_kernel_open_packets(struct ltt_session *session)
836 {
837 enum lttng_error_code ret = LTTNG_OK;
838 struct consumer_socket *socket;
839 struct lttng_ht_iter iter;
840 struct cds_lfht_node *node;
841 struct ltt_kernel_channel *chan;
842
843 lttng::urcu::read_lock_guard read_lock;
844
845 cds_lfht_first(session->kernel_session->consumer->socks->ht, &iter.iter);
846 node = cds_lfht_iter_get_node(&iter.iter);
847 socket = caa_container_of(node, typeof(*socket), node.node);
848
849 cds_list_for_each_entry (chan, &session->kernel_session->channel_list.head, list) {
850 int open_ret;
851
852 DBG("Open packet of kernel channel: channel key = %" PRIu64
853 ", session name = %s, session_id = %" PRIu64,
854 chan->key,
855 session->name,
856 session->id);
857
858 open_ret = consumer_open_channel_packets(socket, chan->key);
859 if (open_ret < 0) {
860 /* General error (no known error expected). */
861 ret = LTTNG_ERR_UNK;
862 goto end;
863 }
864 }
865
866 end:
867 return ret;
868 }
869
870 enum lttng_error_code session_open_packets(struct ltt_session *session)
871 {
872 enum lttng_error_code ret = LTTNG_OK;
873
874 DBG("Opening packets of session channels: session name = %s, session id = %" PRIu64,
875 session->name,
876 session->id);
877
878 if (session->ust_session) {
879 ret = ust_app_open_packets(session);
880 if (ret != LTTNG_OK) {
881 goto end;
882 }
883 }
884
885 if (session->kernel_session) {
886 ret = session_kernel_open_packets(session);
887 if (ret != LTTNG_OK) {
888 goto end;
889 }
890 }
891
892 end:
893 return ret;
894 }
895
896 /*
897 * Set a session's current trace chunk.
898 *
899 * Must be called with the session lock held.
900 */
901 int session_set_trace_chunk(struct ltt_session *session,
902 struct lttng_trace_chunk *new_trace_chunk,
903 struct lttng_trace_chunk **current_trace_chunk)
904 {
905 ASSERT_LOCKED(session->lock);
906 return _session_set_trace_chunk_no_lock_check(
907 session, new_trace_chunk, current_trace_chunk);
908 }
909
910 static void session_notify_destruction(const struct ltt_session *session)
911 {
912 size_t i;
913 const size_t count = lttng_dynamic_array_get_count(&session->destroy_notifiers);
914
915 for (i = 0; i < count; i++) {
916 const struct ltt_session_destroy_notifier_element *element =
917 (ltt_session_destroy_notifier_element *) lttng_dynamic_array_get_element(
918 &session->destroy_notifiers, i);
919
920 element->notifier(session, element->user_data);
921 }
922 }
923
924 /*
925 * Fire each clear notifier once, and remove them from the array.
926 */
927 void session_notify_clear(ltt_session& session)
928 {
929 size_t i;
930 const size_t count = lttng_dynamic_array_get_count(&session.clear_notifiers);
931
932 for (i = 0; i < count; i++) {
933 const struct ltt_session_clear_notifier_element *element =
934 (ltt_session_clear_notifier_element *) lttng_dynamic_array_get_element(
935 &session.clear_notifiers, i);
936
937 element->notifier(&session, element->user_data);
938 }
939 lttng_dynamic_array_clear(&session.clear_notifiers);
940 }
941
942 static void session_release(struct urcu_ref *ref)
943 {
944 int ret;
945 struct ltt_ust_session *usess;
946 struct ltt_kernel_session *ksess;
947 struct ltt_session *session = lttng::utils::container_of(ref, &ltt_session::ref);
948 const bool session_published = session->published;
949
950 LTTNG_ASSERT(!session->chunk_being_archived);
951
952 usess = session->ust_session;
953 ksess = session->kernel_session;
954
955 /* Clean kernel session teardown, keeping data for destroy notifier. */
956 kernel_destroy_session(ksess);
957
958 /* UST session teardown, keeping data for destroy notifier. */
959 if (usess) {
960 /* Close any relayd session */
961 consumer_output_send_destroy_relayd(usess->consumer);
962
963 /* Destroy every UST application related to this session. */
964 ret = ust_app_destroy_trace_all(usess);
965 if (ret) {
966 ERR("Error in ust_app_destroy_trace_all");
967 }
968
969 /* Clean up the rest, keeping destroy notifier data. */
970 trace_ust_destroy_session(usess);
971 }
972
973 /*
974 * Must notify the kernel thread here to update it's poll set in order to
975 * remove the channel(s)' fd just destroyed.
976 */
977 ret = notify_thread_pipe(the_kernel_poll_pipe[1]);
978 if (ret < 0) {
979 PERROR("write kernel poll pipe");
980 }
981
982 DBG("Destroying session %s (id %" PRIu64 ")", session->name, session->id);
983
984 snapshot_destroy(&session->snapshot);
985
986 pthread_mutex_destroy(&session->lock);
987
988 if (session_published) {
989 ASSERT_SESSION_LIST_LOCKED();
990 del_session_list(session);
991 del_session_ht(session);
992 }
993 session_notify_destruction(session);
994
995 consumer_output_put(session->consumer);
996 kernel_free_session(ksess);
997 session->kernel_session = nullptr;
998 if (usess) {
999 trace_ust_free_session(usess);
1000 session->ust_session = nullptr;
1001 }
1002 lttng_dynamic_array_reset(&session->destroy_notifiers);
1003 lttng_dynamic_array_reset(&session->clear_notifiers);
1004 free(session->last_archived_chunk_name);
1005 free(session->base_path);
1006 lttng_trigger_put(session->rotate_trigger);
1007 free(session);
1008 if (session_published) {
1009 /*
1010 * Notify after free-ing to ensure the memory is
1011 * reclaimed before the main thread exits (and prevent memory leak
1012 * reports).
1013 */
1014 ASSERT_SESSION_LIST_LOCKED();
1015 the_session_list.removal_cond.notify_all();
1016 }
1017 }
1018
1019 /*
1020 * Acquire a reference to a session.
1021 * This function may fail (return false); its return value must be checked.
1022 */
1023 bool session_get(struct ltt_session *session)
1024 {
1025 return urcu_ref_get_unless_zero(&session->ref);
1026 }
1027
1028 /*
1029 * Release a reference to a session.
1030 */
1031 void session_put(struct ltt_session *session)
1032 {
1033 if (!session) {
1034 return;
1035 }
1036 /*
1037 * The session list lock must be held as any session_put()
1038 * may cause the removal of the session from the session_list.
1039 */
1040 ASSERT_SESSION_LIST_LOCKED();
1041 LTTNG_ASSERT(session->ref.refcount);
1042 urcu_ref_put(&session->ref, session_release);
1043 }
1044
1045 /*
1046 * Destroy a session.
1047 *
1048 * This method does not immediately release/free the session as other
1049 * components may still hold a reference to the session. However,
1050 * the session should no longer be presented to the user.
1051 *
1052 * Releases the session list's reference to the session
1053 * and marks it as destroyed. Iterations on the session list should be
1054 * mindful of the "destroyed" flag.
1055 */
1056 void session_destroy(struct ltt_session *session)
1057 {
1058 int ret;
1059 struct lttng_ht_iter iter;
1060
1061 LTTNG_ASSERT(!session->destroyed);
1062 session->destroyed = true;
1063
1064 /*
1065 * Remove immediately from the "session by name" hash table. Only one
1066 * session is expected to exist with a given name for at any given time.
1067 *
1068 * Even if a session still technically exists for a little while longer,
1069 * there is no point in performing action on a "destroyed" session.
1070 */
1071 iter.iter.node = &session->node_by_name.node;
1072 ret = lttng_ht_del(ltt_sessions_ht_by_name, &iter);
1073 LTTNG_ASSERT(!ret);
1074
1075 session_put(session);
1076 }
1077
1078 int session_add_destroy_notifier(struct ltt_session *session,
1079 ltt_session_destroy_notifier notifier,
1080 void *user_data)
1081 {
1082 const struct ltt_session_destroy_notifier_element element = { .notifier = notifier,
1083 .user_data = user_data };
1084
1085 return lttng_dynamic_array_add_element(&session->destroy_notifiers, &element);
1086 }
1087
1088 int session_add_clear_notifier(struct ltt_session *session,
1089 ltt_session_clear_notifier notifier,
1090 void *user_data)
1091 {
1092 const struct ltt_session_clear_notifier_element element = { .notifier = notifier,
1093 .user_data = user_data };
1094
1095 return lttng_dynamic_array_add_element(&session->clear_notifiers, &element);
1096 }
1097
1098 /*
1099 * Return a ltt_session structure ptr that matches name. If no session found,
1100 * NULL is returned. This must be called with the session list lock held using
1101 * session_lock_list and session_unlock_list.
1102 * A reference to the session is implicitly acquired by this function.
1103 */
1104 struct ltt_session *session_find_by_name(const char *name)
1105 {
1106 struct ltt_session *iter;
1107
1108 LTTNG_ASSERT(name);
1109 ASSERT_SESSION_LIST_LOCKED();
1110
1111 DBG2("Trying to find session by name %s", name);
1112
1113 cds_list_for_each_entry (iter, &the_session_list.head, list) {
1114 if (!strncmp(iter->name, name, NAME_MAX) && !iter->destroyed) {
1115 goto found;
1116 }
1117 }
1118
1119 return nullptr;
1120 found:
1121 return session_get(iter) ? iter : nullptr;
1122 }
1123
1124 /*
1125 * Return an ltt_session that matches the id. If no session is found,
1126 * NULL is returned. This must be called with rcu_read_lock and
1127 * session list lock held (to guarantee the lifetime of the session).
1128 */
1129 struct ltt_session *session_find_by_id(uint64_t id)
1130 {
1131 struct lttng_ht_node_u64 *node;
1132 struct lttng_ht_iter iter;
1133 struct ltt_session *ls;
1134
1135 ASSERT_RCU_READ_LOCKED();
1136 ASSERT_SESSION_LIST_LOCKED();
1137
1138 if (!ltt_sessions_ht_by_id) {
1139 goto end;
1140 }
1141
1142 lttng_ht_lookup(ltt_sessions_ht_by_id, &id, &iter);
1143 node = lttng_ht_iter_get_node_u64(&iter);
1144 if (node == nullptr) {
1145 goto end;
1146 }
1147 ls = lttng::utils::container_of(node, &ltt_session::node);
1148
1149 DBG3("Session %" PRIu64 " found by id.", id);
1150 return session_get(ls) ? ls : nullptr;
1151
1152 end:
1153 DBG3("Session %" PRIu64 " NOT found by id", id);
1154 return nullptr;
1155 }
1156
1157 /*
1158 * Create a new session and add it to the session list.
1159 * Session list lock must be held by the caller.
1160 */
1161 enum lttng_error_code
1162 session_create(const char *name, uid_t uid, gid_t gid, struct ltt_session **out_session)
1163 {
1164 int ret;
1165 enum lttng_error_code ret_code;
1166 struct ltt_session *new_session = nullptr;
1167
1168 ASSERT_SESSION_LIST_LOCKED();
1169 if (name) {
1170 struct ltt_session *clashing_session;
1171
1172 clashing_session = session_find_by_name(name);
1173 if (clashing_session) {
1174 session_put(clashing_session);
1175 ret_code = LTTNG_ERR_EXIST_SESS;
1176 goto error;
1177 }
1178 }
1179 new_session = zmalloc<ltt_session>();
1180 if (!new_session) {
1181 PERROR("Failed to allocate an ltt_session structure");
1182 ret_code = LTTNG_ERR_NOMEM;
1183 goto error;
1184 }
1185
1186 lttng_dynamic_array_init(&new_session->destroy_notifiers,
1187 sizeof(struct ltt_session_destroy_notifier_element),
1188 nullptr);
1189 lttng_dynamic_array_init(&new_session->clear_notifiers,
1190 sizeof(struct ltt_session_clear_notifier_element),
1191 nullptr);
1192 urcu_ref_init(&new_session->ref);
1193 pthread_mutex_init(&new_session->lock, nullptr);
1194
1195 new_session->creation_time = time(nullptr);
1196 if (new_session->creation_time == (time_t) -1) {
1197 PERROR("Failed to sample session creation time");
1198 ret_code = LTTNG_ERR_SESSION_FAIL;
1199 goto error;
1200 }
1201
1202 /* Create default consumer output. */
1203 new_session->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
1204 if (new_session->consumer == nullptr) {
1205 ret_code = LTTNG_ERR_NOMEM;
1206 goto error;
1207 }
1208
1209 if (name) {
1210 ret = lttng_strncpy(new_session->name, name, sizeof(new_session->name));
1211 if (ret) {
1212 ret_code = LTTNG_ERR_SESSION_INVALID_CHAR;
1213 goto error;
1214 }
1215 ret = validate_name(name);
1216 if (ret < 0) {
1217 ret_code = LTTNG_ERR_SESSION_INVALID_CHAR;
1218 goto error;
1219 }
1220 } else {
1221 int i = 0;
1222 bool found_name = false;
1223 char datetime[16];
1224 struct tm *timeinfo;
1225
1226 timeinfo = localtime(&new_session->creation_time);
1227 if (!timeinfo) {
1228 ret_code = LTTNG_ERR_SESSION_FAIL;
1229 goto error;
1230 }
1231 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
1232 for (i = 0; i < INT_MAX; i++) {
1233 struct ltt_session *clashing_session;
1234
1235 if (i == 0) {
1236 ret = snprintf(new_session->name,
1237 sizeof(new_session->name),
1238 "%s-%s",
1239 DEFAULT_SESSION_NAME,
1240 datetime);
1241 } else {
1242 ret = snprintf(new_session->name,
1243 sizeof(new_session->name),
1244 "%s%d-%s",
1245 DEFAULT_SESSION_NAME,
1246 i,
1247 datetime);
1248 }
1249 new_session->name_contains_creation_time = true;
1250 if (ret == -1 || ret >= sizeof(new_session->name)) {
1251 /*
1252 * Null-terminate in case the name is used
1253 * in logging statements.
1254 */
1255 new_session->name[sizeof(new_session->name) - 1] = '\0';
1256 ret_code = LTTNG_ERR_SESSION_FAIL;
1257 goto error;
1258 }
1259
1260 clashing_session = session_find_by_name(new_session->name);
1261 session_put(clashing_session);
1262 if (!clashing_session) {
1263 found_name = true;
1264 break;
1265 }
1266 }
1267 if (found_name) {
1268 DBG("Generated session name \"%s\"", new_session->name);
1269 new_session->has_auto_generated_name = true;
1270 } else {
1271 ERR("Failed to auto-generate a session name");
1272 ret_code = LTTNG_ERR_SESSION_FAIL;
1273 goto error;
1274 }
1275 }
1276
1277 ret = gethostname(new_session->hostname, sizeof(new_session->hostname));
1278 if (ret < 0) {
1279 if (errno == ENAMETOOLONG) {
1280 new_session->hostname[sizeof(new_session->hostname) - 1] = '\0';
1281 ERR("Hostname exceeds the maximal permitted length and has been truncated to %s",
1282 new_session->hostname);
1283 } else {
1284 ret_code = LTTNG_ERR_SESSION_FAIL;
1285 goto error;
1286 }
1287 }
1288
1289 new_session->uid = uid;
1290 new_session->gid = gid;
1291
1292 ret = snapshot_init(&new_session->snapshot);
1293 if (ret < 0) {
1294 ret_code = LTTNG_ERR_NOMEM;
1295 goto error;
1296 }
1297
1298 new_session->rotation_state = LTTNG_ROTATION_STATE_NO_ROTATION;
1299
1300 /* Add new session to the session list. */
1301 new_session->id = add_session_list(new_session);
1302
1303 /*
1304 * Add the new session to the ltt_sessions_ht_by_id.
1305 * No ownership is taken by the hash table; it is merely
1306 * a wrapper around the session list used for faster access
1307 * by session id.
1308 */
1309 add_session_ht(new_session);
1310 new_session->published = true;
1311
1312 /*
1313 * Consumer is left to NULL since the create_session_uri command will
1314 * set it up and, if valid, assign it to the session.
1315 */
1316 DBG("Tracing session %s created with ID %" PRIu64 " by uid = %d, gid = %d",
1317 new_session->name,
1318 new_session->id,
1319 new_session->uid,
1320 new_session->gid);
1321 ret_code = LTTNG_OK;
1322 end:
1323 if (new_session) {
1324 (void) session_get(new_session);
1325 *out_session = new_session;
1326 }
1327 return ret_code;
1328 error:
1329 session_put(new_session);
1330 new_session = nullptr;
1331 goto end;
1332 }
1333
1334 /*
1335 * Check if the UID matches the session. Root user has access to all
1336 * sessions.
1337 */
1338 bool session_access_ok(struct ltt_session *session, uid_t uid)
1339 {
1340 LTTNG_ASSERT(session);
1341 return (uid == session->uid) || uid == 0;
1342 }
1343
1344 /*
1345 * Set a session's rotation state and reset all associated state.
1346 *
1347 * This function resets the rotation state (check timers, pending
1348 * flags, etc.) and sets the result of the last rotation. The result
1349 * can be queries by a liblttng-ctl client.
1350 *
1351 * Be careful of the result passed to this function. For instance,
1352 * on failure to launch a rotation, a client will expect the rotation
1353 * state to be set to "NO_ROTATION". If an error occurred while the
1354 * rotation was "ONGOING", result should be set to "ERROR", which will
1355 * allow a client to report it.
1356 *
1357 * Must be called with the session and session_list locks held.
1358 */
1359 int session_reset_rotation_state(ltt_session& session, enum lttng_rotation_state result)
1360 {
1361 int ret = 0;
1362
1363 ASSERT_SESSION_LIST_LOCKED();
1364 ASSERT_LOCKED(session.lock);
1365
1366 session.rotation_state = result;
1367 if (session.rotation_pending_check_timer_enabled) {
1368 ret = timer_session_rotation_pending_check_stop(session);
1369 }
1370 if (session.chunk_being_archived) {
1371 uint64_t chunk_id;
1372 enum lttng_trace_chunk_status chunk_status;
1373
1374 chunk_status = lttng_trace_chunk_get_id(session.chunk_being_archived, &chunk_id);
1375 LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
1376 LTTNG_OPTIONAL_SET(&session.last_archived_chunk_id, chunk_id);
1377 lttng_trace_chunk_put(session.chunk_being_archived);
1378 session.chunk_being_archived = nullptr;
1379 /*
1380 * Fire the clear reply notifiers if we are completing a clear
1381 * rotation.
1382 */
1383 session_notify_clear(session);
1384 }
1385 return ret;
1386 }
1387
1388 /*
1389 * Sample the id of a session looked up via its name.
1390 * Here the term "sampling" hint the caller that this return the id at a given
1391 * point in time with no guarantee that the session for which the id was
1392 * sampled still exist at that point.
1393 *
1394 * Return 0 when the session is not found,
1395 * Return 1 when the session is found and set `id`.
1396 */
1397 bool sample_session_id_by_name(const char *name, uint64_t *id)
1398 {
1399 bool found = false;
1400 struct lttng_ht_node_str *node;
1401 struct lttng_ht_iter iter;
1402 struct ltt_session *ls;
1403
1404 lttng::urcu::read_lock_guard read_lock;
1405
1406 if (!ltt_sessions_ht_by_name) {
1407 found = false;
1408 goto end;
1409 }
1410
1411 lttng_ht_lookup(ltt_sessions_ht_by_name, name, &iter);
1412 node = lttng_ht_iter_get_node_str(&iter);
1413 if (node == nullptr) {
1414 found = false;
1415 goto end;
1416 }
1417
1418 ls = lttng::utils::container_of(node, &ltt_session::node_by_name);
1419 *id = ls->id;
1420 found = true;
1421
1422 DBG3("Session id `%" PRIu64 "` sampled for session `%s", *id, name);
1423 end:
1424 return found;
1425 }
1426
1427 void ls::details::locked_session_release(ltt_session *session)
1428 {
1429 if (!session) {
1430 return;
1431 }
1432
1433 session_unlock(session);
1434 session_put(session);
1435 }
1436
1437 ltt_session::locked_ref ls::find_locked_session_by_id(ltt_session::id_t id)
1438 {
1439 lttng::urcu::read_lock_guard rcu_lock;
1440 auto session = session_find_by_id(id);
1441
1442 if (!session) {
1443 return nullptr;
1444 }
1445
1446 /*
1447 * The pointer falling out of scope will unlock and release the reference to the
1448 * session.
1449 */
1450 session_lock(session);
1451 return ltt_session::locked_ref(session);
1452 }
1453
1454 ltt_session::sptr ls::find_session_by_id(ltt_session::id_t id)
1455 {
1456 lttng::urcu::read_lock_guard rcu_lock;
1457 auto session = session_find_by_id(id);
1458
1459 if (!session) {
1460 return nullptr;
1461 }
1462
1463 return { session, session_put };
1464 }
This page took 0.05639 seconds and 3 git commands to generate.