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