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