relayd: add remote trace chunk close command
[lttng-tools.git] / src / bin / lttng-sessiond / session.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _LGPL_SOURCE
19 #include <limits.h>
20 #include <inttypes.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <urcu.h>
26 #include <dirent.h>
27 #include <sys/types.h>
28 #include <pthread.h>
29
30 #include <common/common.h>
31 #include <common/utils.h>
32 #include <common/trace-chunk.h>
33 #include <common/sessiond-comm/sessiond-comm.h>
34 #include <lttng/location-internal.h>
35 #include "lttng-sessiond.h"
36 #include "kernel.h"
37
38 #include "session.h"
39 #include "utils.h"
40 #include "trace-ust.h"
41 #include "timer.h"
42
43 struct ltt_session_destroy_notifier_element {
44 ltt_session_destroy_notifier notifier;
45 void *user_data;
46 };
47
48 /*
49 * NOTES:
50 *
51 * No ltt_session.lock is taken here because those data structure are widely
52 * spread across the lttng-tools code base so before caling functions below
53 * that can read/write a session, the caller MUST acquire the session lock
54 * using session_lock() and session_unlock().
55 */
56
57 /*
58 * Init tracing session list.
59 *
60 * Please see session.h for more explanation and correct usage of the list.
61 */
62 static struct ltt_session_list ltt_session_list = {
63 .head = CDS_LIST_HEAD_INIT(ltt_session_list.head),
64 .lock = PTHREAD_MUTEX_INITIALIZER,
65 .removal_cond = PTHREAD_COND_INITIALIZER,
66 .next_uuid = 0,
67 };
68
69 /* These characters are forbidden in a session name. Used by validate_name. */
70 static const char *forbidden_name_chars = "/";
71
72 /* Global hash table to keep the sessions, indexed by id. */
73 static struct lttng_ht *ltt_sessions_ht_by_id = NULL;
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 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 assert(ls);
117
118 cds_list_add(&ls->list, &ltt_session_list.head);
119 return ltt_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 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 &ltt_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(&ltt_session_list.lock);
148 while (!cds_list_empty(&ltt_session_list.head)) {
149 pthread_cond_wait(&ltt_session_list.removal_cond,
150 &ltt_session_list.lock);
151 }
152 pthread_mutex_unlock(&ltt_session_list.lock);
153 }
154
155 /*
156 * Acquire session list lock
157 */
158 void session_lock_list(void)
159 {
160 pthread_mutex_lock(&ltt_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(&ltt_session_list.lock);
169 }
170
171 /*
172 * Release session list lock
173 */
174 void session_unlock_list(void)
175 {
176 pthread_mutex_unlock(&ltt_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 ret = asprintf(&chunk_path, "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY "/%s",
265 session_get_base_path(session),
266 session->last_archived_chunk_name);
267 if (ret == -1) {
268 goto end;
269 }
270
271 switch (session_get_consumer_destination_type(session)) {
272 case CONSUMER_DST_LOCAL:
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, 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 HT.
301 *
302 * The session list lock must be held.
303 */
304 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 end:
316 return ret;
317 }
318
319 /*
320 * Destroy the ltt_sessions_ht_by_id HT.
321 *
322 * The session list lock must be held.
323 */
324 static void ltt_sessions_ht_destroy(void)
325 {
326 if (!ltt_sessions_ht_by_id) {
327 return;
328 }
329 ht_cleanup_push(ltt_sessions_ht_by_id);
330 ltt_sessions_ht_by_id = NULL;
331 }
332
333 /*
334 * Add a ltt_session to the ltt_sessions_ht_by_id.
335 * If unallocated, the ltt_sessions_ht_by_id HT is allocated.
336 * The session list lock must be held.
337 */
338 static void add_session_ht(struct ltt_session *ls)
339 {
340 int ret;
341
342 assert(ls);
343
344 if (!ltt_sessions_ht_by_id) {
345 ret = ltt_sessions_ht_alloc();
346 if (ret) {
347 ERR("Error allocating the sessions HT");
348 goto end;
349 }
350 }
351 lttng_ht_node_init_u64(&ls->node, ls->id);
352 lttng_ht_add_unique_u64(ltt_sessions_ht_by_id, &ls->node);
353
354 end:
355 return;
356 }
357
358 /*
359 * Test if ltt_sessions_ht_by_id is empty.
360 * Return 1 if empty, 0 if not empty.
361 * The session list lock must be held.
362 */
363 static int ltt_sessions_ht_empty(void)
364 {
365 int ret;
366
367 if (!ltt_sessions_ht_by_id) {
368 ret = 1;
369 goto end;
370 }
371
372 ret = lttng_ht_get_count(ltt_sessions_ht_by_id) ? 0 : 1;
373 end:
374 return ret;
375 }
376
377 /*
378 * Remove a ltt_session from the ltt_sessions_ht_by_id.
379 * If empty, the ltt_sessions_ht_by_id HT is freed.
380 * The session list lock must be held.
381 */
382 static void del_session_ht(struct ltt_session *ls)
383 {
384 struct lttng_ht_iter iter;
385 int ret;
386
387 assert(ls);
388 assert(ltt_sessions_ht_by_id);
389
390 iter.iter.node = &ls->node.node;
391 ret = lttng_ht_del(ltt_sessions_ht_by_id, &iter);
392 assert(!ret);
393
394 if (ltt_sessions_ht_empty()) {
395 DBG("Empty ltt_sessions_ht_by_id, destroying it");
396 ltt_sessions_ht_destroy();
397 }
398 }
399
400 /*
401 * Acquire session lock
402 */
403 void session_lock(struct ltt_session *session)
404 {
405 assert(session);
406
407 pthread_mutex_lock(&session->lock);
408 }
409
410 /*
411 * Release session lock
412 */
413 void session_unlock(struct ltt_session *session)
414 {
415 assert(session);
416
417 pthread_mutex_unlock(&session->lock);
418 }
419
420 static
421 int _session_set_trace_chunk_no_lock_check(struct ltt_session *session,
422 struct lttng_trace_chunk *new_trace_chunk,
423 struct lttng_trace_chunk **_current_trace_chunk)
424 {
425 int ret;
426 unsigned int i, refs_to_acquire = 0, refs_acquired = 0, refs_to_release = 0;
427 struct cds_lfht_iter iter;
428 struct consumer_socket *socket;
429 struct lttng_trace_chunk *current_trace_chunk;
430 uint64_t chunk_id;
431 enum lttng_trace_chunk_status chunk_status;
432
433 rcu_read_lock();
434 /*
435 * Ownership of current trace chunk is transferred to
436 * `current_trace_chunk`.
437 */
438 current_trace_chunk = session->current_trace_chunk;
439 session->current_trace_chunk = NULL;
440 if (session->ust_session) {
441 lttng_trace_chunk_put(
442 session->ust_session->current_trace_chunk);
443 session->ust_session->current_trace_chunk = NULL;
444 }
445 if (session->kernel_session) {
446 lttng_trace_chunk_put(
447 session->kernel_session->current_trace_chunk);
448 session->kernel_session->current_trace_chunk = NULL;
449 }
450 if (!new_trace_chunk) {
451 ret = 0;
452 goto end;
453 }
454 chunk_status = lttng_trace_chunk_get_id(new_trace_chunk, &chunk_id);
455 assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
456
457 refs_to_acquire = 1;
458 refs_to_acquire += !!session->ust_session;
459 refs_to_acquire += !!session->kernel_session;
460
461 for (refs_acquired = 0; refs_acquired < refs_to_acquire;
462 refs_acquired++) {
463 if (!lttng_trace_chunk_get(new_trace_chunk)) {
464 ERR("Failed to acquire reference to new trace chunk of session \"%s\"",
465 session->name);
466 goto error;
467 }
468 }
469
470 if (session->ust_session) {
471 const uint64_t relayd_id =
472 session->ust_session->consumer->net_seq_index;
473 const bool is_local_trace =
474 session->ust_session->consumer->type ==
475 CONSUMER_DST_LOCAL;
476
477 session->ust_session->current_trace_chunk = new_trace_chunk;
478 if (is_local_trace) {
479 enum lttng_error_code ret_error_code;
480
481 ret_error_code = ust_app_create_channel_subdirectories(
482 session->ust_session);
483 if (ret_error_code != LTTNG_OK) {
484 ret = -ret_error_code;
485 goto error;
486 }
487 }
488 cds_lfht_for_each_entry(
489 session->ust_session->consumer->socks->ht,
490 &iter, socket, node.node) {
491 pthread_mutex_lock(socket->lock);
492 ret = consumer_create_trace_chunk(socket,
493 relayd_id,
494 session->id, new_trace_chunk);
495 pthread_mutex_unlock(socket->lock);
496 if (ret) {
497 goto error;
498 }
499 }
500 }
501 if (session->kernel_session) {
502 const uint64_t relayd_id =
503 session->kernel_session->consumer->net_seq_index;
504 const bool is_local_trace =
505 session->kernel_session->consumer->type ==
506 CONSUMER_DST_LOCAL;
507
508 session->kernel_session->current_trace_chunk = new_trace_chunk;
509 if (is_local_trace) {
510 enum lttng_error_code ret_error_code;
511
512 ret_error_code = kernel_create_channel_subdirectories(
513 session->kernel_session);
514 if (ret_error_code != LTTNG_OK) {
515 ret = -ret_error_code;
516 goto error;
517 }
518 }
519 cds_lfht_for_each_entry(
520 session->kernel_session->consumer->socks->ht,
521 &iter, socket, node.node) {
522 pthread_mutex_lock(socket->lock);
523 ret = consumer_create_trace_chunk(socket,
524 relayd_id,
525 session->id, new_trace_chunk);
526 pthread_mutex_unlock(socket->lock);
527 if (ret) {
528 goto error;
529 }
530 }
531 }
532
533 /*
534 * Update local current trace chunk state last, only if all remote
535 * creations succeeded.
536 */
537 session->current_trace_chunk = new_trace_chunk;
538 LTTNG_OPTIONAL_SET(&session->most_recent_chunk_id, chunk_id);
539 end:
540 if (_current_trace_chunk) {
541 *_current_trace_chunk = current_trace_chunk;
542 current_trace_chunk = NULL;
543 }
544 end_no_move:
545 rcu_read_unlock();
546 lttng_trace_chunk_put(current_trace_chunk);
547 return ret;
548 error:
549 if (session->ust_session) {
550 session->ust_session->current_trace_chunk = NULL;
551 }
552 if (session->kernel_session) {
553 session->kernel_session->current_trace_chunk = NULL;
554 }
555 /*
556 * Release references taken in the case where all references could not
557 * be acquired.
558 */
559 refs_to_release = refs_to_acquire - refs_acquired;
560 for (i = 0; i < refs_to_release; i++) {
561 lttng_trace_chunk_put(new_trace_chunk);
562 }
563 ret = -1;
564 goto end_no_move;
565 }
566
567 static
568 bool output_supports_trace_chunks(const struct ltt_session *session)
569 {
570 if (session->consumer->type == CONSUMER_DST_LOCAL) {
571 return true;
572 } else {
573 struct consumer_output *output;
574
575 if (session->ust_session) {
576 output = session->ust_session->consumer;
577 } else if (session->kernel_session) {
578 output = session->kernel_session->consumer;
579 } else {
580 abort();
581 }
582
583 if (output->relay_major_version > 2) {
584 return true;
585 } else if (output->relay_major_version == 2 &&
586 output->relay_minor_version >= 11) {
587 return true;
588 }
589 }
590 return false;
591 }
592
593 struct lttng_trace_chunk *session_create_new_trace_chunk(
594 struct ltt_session *session,
595 const char *session_base_path_override,
596 const char *chunk_name_override)
597 {
598 int ret;
599 struct lttng_trace_chunk *trace_chunk = NULL;
600 enum lttng_trace_chunk_status chunk_status;
601 const time_t chunk_creation_ts = time(NULL);
602 const bool is_local_trace =
603 session->consumer->type == CONSUMER_DST_LOCAL;
604 const char *base_path = session_base_path_override ? :
605 session_get_base_path(session);
606 struct lttng_directory_handle session_output_directory;
607 const struct lttng_credentials session_credentials = {
608 .uid = session->uid,
609 .gid = session->gid,
610 };
611 uint64_t next_chunk_id;
612
613 if (chunk_creation_ts == (time_t) -1) {
614 PERROR("Failed to sample time while creation session \"%s\" trace chunk",
615 session->name);
616 goto error;
617 }
618
619 if (!output_supports_trace_chunks(session)) {
620 goto end;
621 }
622 next_chunk_id = session->most_recent_chunk_id.is_set ?
623 session->most_recent_chunk_id.value + 1 : 0;
624
625 trace_chunk = lttng_trace_chunk_create(next_chunk_id,
626 chunk_creation_ts);
627 if (!trace_chunk) {
628 goto error;
629 }
630
631 if (chunk_name_override) {
632 chunk_status = lttng_trace_chunk_override_name(trace_chunk,
633 chunk_name_override);
634 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
635 goto error;
636 }
637 }
638
639 if (!is_local_trace) {
640 /*
641 * No need to set crendentials and output directory
642 * for remote trace chunks.
643 */
644 goto end;
645 }
646
647 chunk_status = lttng_trace_chunk_set_credentials(trace_chunk,
648 &session_credentials);
649 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
650 goto error;
651 }
652
653 DBG("Creating base output directory of session \"%s\" at %s",
654 session->name, base_path);
655 ret = utils_mkdir_recursive(base_path, S_IRWXU | S_IRWXG,
656 session->uid, session->gid);
657 if (ret) {
658 goto error;
659 }
660 ret = lttng_directory_handle_init(&session_output_directory,
661 base_path);
662 if (ret) {
663 goto error;
664 }
665 chunk_status = lttng_trace_chunk_set_as_owner(trace_chunk,
666 &session_output_directory);
667 lttng_directory_handle_fini(&session_output_directory);
668 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
669 goto error;
670 }
671 end:
672 return trace_chunk;
673 error:
674 lttng_trace_chunk_put(trace_chunk);
675 trace_chunk = NULL;
676 goto end;
677 }
678
679 int session_close_trace_chunk(const struct ltt_session *session,
680 struct lttng_trace_chunk *trace_chunk,
681 const enum lttng_trace_chunk_command_type *close_command)
682 {
683 int ret = 0;
684 bool error_occurred = false;
685 struct cds_lfht_iter iter;
686 struct consumer_socket *socket;
687 enum lttng_trace_chunk_status chunk_status;
688 const time_t chunk_close_timestamp = time(NULL);
689
690 if (close_command) {
691 chunk_status = lttng_trace_chunk_set_close_command(
692 trace_chunk, *close_command);
693 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
694 ret = -1;
695 goto end;
696 }
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 chunk_status = lttng_trace_chunk_set_close_timestamp(trace_chunk,
706 chunk_close_timestamp);
707 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
708 ERR("Failed to set the close timestamp of the current trace chunk of session \"%s\"",
709 session->name);
710 ret = -1;
711 goto end;
712 }
713
714 if (session->ust_session) {
715 cds_lfht_for_each_entry(
716 session->ust_session->consumer->socks->ht,
717 &iter, socket, node.node) {
718 pthread_mutex_lock(socket->lock);
719 ret = consumer_close_trace_chunk(socket,
720 session->consumer->net_seq_index,
721 session->id,
722 trace_chunk);
723 pthread_mutex_unlock(socket->lock);
724 if (ret) {
725 ERR("Failed to close trace chunk on user space consumer");
726 error_occurred = true;
727 }
728 }
729 }
730 if (session->kernel_session) {
731 cds_lfht_for_each_entry(
732 session->kernel_session->consumer->socks->ht,
733 &iter, socket, node.node) {
734 pthread_mutex_lock(socket->lock);
735 ret = consumer_close_trace_chunk(socket,
736 session->consumer->net_seq_index,
737 session->id,
738 trace_chunk);
739 pthread_mutex_unlock(socket->lock);
740 if (ret) {
741 ERR("Failed to close trace chunk on kernel consumer");
742 error_occurred = true;
743 }
744 }
745 }
746 ret = error_occurred ? -1 : 0;
747 end:
748 return ret;
749 }
750
751 /*
752 * Set a session's current trace chunk.
753 *
754 * Must be called with the session lock held.
755 */
756 int session_set_trace_chunk(struct ltt_session *session,
757 struct lttng_trace_chunk *new_trace_chunk,
758 struct lttng_trace_chunk **current_trace_chunk)
759 {
760 ASSERT_LOCKED(session->lock);
761 return _session_set_trace_chunk_no_lock_check(session, new_trace_chunk,
762 current_trace_chunk);
763 }
764
765 static
766 void session_notify_destruction(const struct ltt_session *session)
767 {
768 size_t i;
769 const size_t count = lttng_dynamic_array_get_count(
770 &session->destroy_notifiers);
771
772 for (i = 0; i < count; i++) {
773 const struct ltt_session_destroy_notifier_element *element =
774 lttng_dynamic_array_get_element(
775 &session->destroy_notifiers, i);
776
777 element->notifier(session, element->user_data);
778 }
779 }
780
781 static
782 void session_release(struct urcu_ref *ref)
783 {
784 int ret;
785 struct ltt_ust_session *usess;
786 struct ltt_kernel_session *ksess;
787 struct ltt_session *session = container_of(ref, typeof(*session), ref);
788
789 assert(!session->chunk_being_archived);
790
791 usess = session->ust_session;
792 ksess = session->kernel_session;
793
794 session_notify_destruction(session);
795 lttng_dynamic_array_reset(&session->destroy_notifiers);
796 if (session->current_trace_chunk) {
797 ret = session_close_trace_chunk(session, session->current_trace_chunk, NULL);
798 if (ret) {
799 ERR("Failed to close the current trace chunk of session \"%s\" during its release",
800 session->name);
801 }
802 ret = _session_set_trace_chunk_no_lock_check(session, NULL, NULL);
803 if (ret) {
804 ERR("Failed to release the current trace chunk of session \"%s\" during its release",
805 session->name);
806 }
807 }
808
809 /* Clean kernel session teardown */
810 kernel_destroy_session(ksess);
811 session->kernel_session = NULL;
812
813 /* UST session teardown */
814 if (usess) {
815 /* Close any relayd session */
816 consumer_output_send_destroy_relayd(usess->consumer);
817
818 /* Destroy every UST application related to this session. */
819 ret = ust_app_destroy_trace_all(usess);
820 if (ret) {
821 ERR("Error in ust_app_destroy_trace_all");
822 }
823
824 /* Clean up the rest. */
825 trace_ust_destroy_session(usess);
826 session->ust_session = NULL;
827 }
828
829 /*
830 * Must notify the kernel thread here to update it's poll set in order to
831 * remove the channel(s)' fd just destroyed.
832 */
833 ret = notify_thread_pipe(kernel_poll_pipe[1]);
834 if (ret < 0) {
835 PERROR("write kernel poll pipe");
836 }
837
838 DBG("Destroying session %s (id %" PRIu64 ")", session->name, session->id);
839
840 consumer_output_put(session->consumer);
841 snapshot_destroy(&session->snapshot);
842
843 pthread_mutex_destroy(&session->lock);
844
845 if (session->published) {
846 ASSERT_LOCKED(ltt_session_list.lock);
847 del_session_list(session);
848 del_session_ht(session);
849 pthread_cond_broadcast(&ltt_session_list.removal_cond);
850 }
851 free(session->last_archived_chunk_name);
852 free(session);
853 }
854
855 /*
856 * Acquire a reference to a session.
857 * This function may fail (return false); its return value must be checked.
858 */
859 bool session_get(struct ltt_session *session)
860 {
861 return urcu_ref_get_unless_zero(&session->ref);
862 }
863
864 /*
865 * Release a reference to a session.
866 */
867 void session_put(struct ltt_session *session)
868 {
869 if (!session) {
870 return;
871 }
872 /*
873 * The session list lock must be held as any session_put()
874 * may cause the removal of the session from the session_list.
875 */
876 ASSERT_LOCKED(ltt_session_list.lock);
877 assert(session->ref.refcount);
878 urcu_ref_put(&session->ref, session_release);
879 }
880
881 /*
882 * Destroy a session.
883 *
884 * This method does not immediately release/free the session as other
885 * components may still hold a reference to the session. However,
886 * the session should no longer be presented to the user.
887 *
888 * Releases the session list's reference to the session
889 * and marks it as destroyed. Iterations on the session list should be
890 * mindful of the "destroyed" flag.
891 */
892 void session_destroy(struct ltt_session *session)
893 {
894 assert(!session->destroyed);
895 session->destroyed = true;
896 session_put(session);
897 }
898
899 int session_add_destroy_notifier(struct ltt_session *session,
900 ltt_session_destroy_notifier notifier, void *user_data)
901 {
902 const struct ltt_session_destroy_notifier_element element = {
903 .notifier = notifier,
904 .user_data = user_data
905 };
906
907 return lttng_dynamic_array_add_element(&session->destroy_notifiers,
908 &element);
909 }
910
911 /*
912 * Return a ltt_session structure ptr that matches name. If no session found,
913 * NULL is returned. This must be called with the session list lock held using
914 * session_lock_list and session_unlock_list.
915 * A reference to the session is implicitly acquired by this function.
916 */
917 struct ltt_session *session_find_by_name(const char *name)
918 {
919 struct ltt_session *iter;
920
921 assert(name);
922 ASSERT_LOCKED(ltt_session_list.lock);
923
924 DBG2("Trying to find session by name %s", name);
925
926 cds_list_for_each_entry(iter, &ltt_session_list.head, list) {
927 if (!strncmp(iter->name, name, NAME_MAX) &&
928 !iter->destroyed) {
929 goto found;
930 }
931 }
932
933 return NULL;
934 found:
935 return session_get(iter) ? iter : NULL;
936 }
937
938 /*
939 * Return an ltt_session that matches the id. If no session is found,
940 * NULL is returned. This must be called with rcu_read_lock and
941 * session list lock held (to guarantee the lifetime of the session).
942 */
943 struct ltt_session *session_find_by_id(uint64_t id)
944 {
945 struct lttng_ht_node_u64 *node;
946 struct lttng_ht_iter iter;
947 struct ltt_session *ls;
948
949 ASSERT_LOCKED(ltt_session_list.lock);
950
951 if (!ltt_sessions_ht_by_id) {
952 goto end;
953 }
954
955 lttng_ht_lookup(ltt_sessions_ht_by_id, &id, &iter);
956 node = lttng_ht_iter_get_node_u64(&iter);
957 if (node == NULL) {
958 goto end;
959 }
960 ls = caa_container_of(node, struct ltt_session, node);
961
962 DBG3("Session %" PRIu64 " found by id.", id);
963 return session_get(ls) ? ls : NULL;
964
965 end:
966 DBG3("Session %" PRIu64 " NOT found by id", id);
967 return NULL;
968 }
969
970 /*
971 * Create a new session and add it to the session list.
972 * Session list lock must be held by the caller.
973 */
974 enum lttng_error_code session_create(const char *name, uid_t uid, gid_t gid,
975 struct ltt_session **out_session)
976 {
977 int ret;
978 enum lttng_error_code ret_code;
979 struct ltt_session *new_session = NULL;
980
981 ASSERT_LOCKED(ltt_session_list.lock);
982 if (name) {
983 struct ltt_session *clashing_session;
984
985 clashing_session = session_find_by_name(name);
986 if (clashing_session) {
987 session_put(clashing_session);
988 ret_code = LTTNG_ERR_EXIST_SESS;
989 goto error;
990 }
991 }
992 new_session = zmalloc(sizeof(struct ltt_session));
993 if (!new_session) {
994 PERROR("Failed to allocate an ltt_session structure");
995 ret_code = LTTNG_ERR_NOMEM;
996 goto error;
997 }
998
999 lttng_dynamic_array_init(&new_session->destroy_notifiers,
1000 sizeof(struct ltt_session_destroy_notifier_element),
1001 NULL);
1002 urcu_ref_init(&new_session->ref);
1003 pthread_mutex_init(&new_session->lock, NULL);
1004
1005 new_session->creation_time = time(NULL);
1006 if (new_session->creation_time == (time_t) -1) {
1007 PERROR("Failed to sample session creation time");
1008 ret_code = LTTNG_ERR_SESSION_FAIL;
1009 goto error;
1010 }
1011
1012 /* Create default consumer output. */
1013 new_session->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
1014 if (new_session->consumer == NULL) {
1015 ret_code = LTTNG_ERR_NOMEM;
1016 goto error;
1017 }
1018
1019 if (name) {
1020 ret = lttng_strncpy(new_session->name, name, sizeof(new_session->name));
1021 if (ret) {
1022 ret_code = LTTNG_ERR_SESSION_INVALID_CHAR;
1023 goto error;
1024 }
1025 ret = validate_name(name);
1026 if (ret < 0) {
1027 ret_code = LTTNG_ERR_SESSION_INVALID_CHAR;
1028 goto error;
1029 }
1030 } else {
1031 int i = 0;
1032 bool found_name = false;
1033 char datetime[16];
1034 struct tm *timeinfo;
1035
1036 timeinfo = localtime(&new_session->creation_time);
1037 if (!timeinfo) {
1038 ret_code = LTTNG_ERR_SESSION_FAIL;
1039 goto error;
1040 }
1041 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
1042 for (i = 0; i < INT_MAX; i++) {
1043 struct ltt_session *clashing_session;
1044
1045 if (i == 0) {
1046 ret = snprintf(new_session->name,
1047 sizeof(new_session->name),
1048 "%s-%s",
1049 DEFAULT_SESSION_NAME,
1050 datetime);
1051 } else {
1052 ret = snprintf(new_session->name,
1053 sizeof(new_session->name),
1054 "%s%d-%s",
1055 DEFAULT_SESSION_NAME, i,
1056 datetime);
1057 }
1058 if (ret == -1 || ret >= sizeof(new_session->name)) {
1059 /*
1060 * Null-terminate in case the name is used
1061 * in logging statements.
1062 */
1063 new_session->name[sizeof(new_session->name) - 1] = '\0';
1064 ret_code = LTTNG_ERR_SESSION_FAIL;
1065 goto error;
1066 }
1067
1068 clashing_session =
1069 session_find_by_name(new_session->name);
1070 session_put(clashing_session);
1071 if (!clashing_session) {
1072 found_name = true;
1073 break;
1074 }
1075 }
1076 if (found_name) {
1077 DBG("Generated session name \"%s\"", new_session->name);
1078 new_session->has_auto_generated_name = true;
1079 } else {
1080 ERR("Failed to auto-generate a session name");
1081 ret_code = LTTNG_ERR_SESSION_FAIL;
1082 goto error;
1083 }
1084 }
1085
1086 ret = gethostname(new_session->hostname, sizeof(new_session->hostname));
1087 if (ret < 0) {
1088 if (errno == ENAMETOOLONG) {
1089 new_session->hostname[sizeof(new_session->hostname) - 1] = '\0';
1090 ERR("Hostname exceeds the maximal permitted length and has been truncated to %s",
1091 new_session->hostname);
1092 } else {
1093 ret_code = LTTNG_ERR_SESSION_FAIL;
1094 goto error;
1095 }
1096 }
1097
1098 new_session->uid = uid;
1099 new_session->gid = gid;
1100
1101 ret = snapshot_init(&new_session->snapshot);
1102 if (ret < 0) {
1103 ret_code = LTTNG_ERR_NOMEM;
1104 goto error;
1105 }
1106
1107 new_session->rotation_state = LTTNG_ROTATION_STATE_NO_ROTATION;
1108
1109 /* Add new session to the session list. */
1110 new_session->id = add_session_list(new_session);
1111
1112 /*
1113 * Add the new session to the ltt_sessions_ht_by_id.
1114 * No ownership is taken by the hash table; it is merely
1115 * a wrapper around the session list used for faster access
1116 * by session id.
1117 */
1118 add_session_ht(new_session);
1119 new_session->published = true;
1120
1121 /*
1122 * Consumer is left to NULL since the create_session_uri command will
1123 * set it up and, if valid, assign it to the session.
1124 */
1125 DBG("Tracing session %s created with ID %" PRIu64 " by uid = %d, gid = %d",
1126 new_session->name, new_session->id, new_session->uid,
1127 new_session->gid);
1128 ret_code = LTTNG_OK;
1129 end:
1130 if (new_session) {
1131 (void) session_get(new_session);
1132 *out_session = new_session;
1133 }
1134 return ret_code;
1135 error:
1136 session_put(new_session);
1137 new_session = NULL;
1138 goto end;
1139 }
1140
1141 /*
1142 * Check if the UID or GID match the session. Root user has access to all
1143 * sessions.
1144 */
1145 int session_access_ok(struct ltt_session *session, uid_t uid, gid_t gid)
1146 {
1147 assert(session);
1148
1149 if (uid != session->uid && gid != session->gid && uid != 0) {
1150 return 0;
1151 } else {
1152 return 1;
1153 }
1154 }
1155
1156 /*
1157 * Set a session's rotation state and reset all associated state.
1158 *
1159 * This function resets the rotation state (check timers, pending
1160 * flags, etc.) and sets the result of the last rotation. The result
1161 * can be queries by a liblttng-ctl client.
1162 *
1163 * Be careful of the result passed to this function. For instance,
1164 * on failure to launch a rotation, a client will expect the rotation
1165 * state to be set to "NO_ROTATION". If an error occured while the
1166 * rotation was "ONGOING", result should be set to "ERROR", which will
1167 * allow a client to report it.
1168 *
1169 * Must be called with the session and session_list locks held.
1170 */
1171 int session_reset_rotation_state(struct ltt_session *session,
1172 enum lttng_rotation_state result)
1173 {
1174 int ret = 0;
1175
1176 ASSERT_LOCKED(ltt_session_list.lock);
1177 ASSERT_LOCKED(session->lock);
1178
1179 session->rotation_state = result;
1180 if (session->rotation_pending_check_timer_enabled) {
1181 ret = timer_session_rotation_pending_check_stop(session);
1182 }
1183 if (session->chunk_being_archived) {
1184 uint64_t chunk_id;
1185 enum lttng_trace_chunk_status chunk_status;
1186
1187 chunk_status = lttng_trace_chunk_get_id(
1188 session->chunk_being_archived,
1189 &chunk_id);
1190 assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
1191 LTTNG_OPTIONAL_SET(&session->last_archived_chunk_id,
1192 chunk_id);
1193 lttng_trace_chunk_put(session->chunk_being_archived);
1194 session->chunk_being_archived = NULL;
1195 }
1196 return ret;
1197 }
This page took 0.056556 seconds and 4 git commands to generate.