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