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