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