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