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