sessiond: propagate the use of ltt_session::locked_ref
[lttng-tools.git] / src / bin / lttng-sessiond / session.cpp
CommitLineData
5b74c7b1 1/*
21cf9b6b 2 * Copyright (C) 2011 EfficiOS Inc.
5b74c7b1 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
91d76f53 5 *
5b74c7b1
DG
6 */
7
6c1c0768 8#define _LGPL_SOURCE
28ab034a
JG
9#include "cmd.hpp"
10#include "kernel.hpp"
11#include "lttng-sessiond.hpp"
12#include "session.hpp"
13#include "timer.hpp"
14#include "trace-ust.hpp"
15#include "utils.hpp"
16
17#include <common/common.hpp>
18#include <common/sessiond-comm/sessiond-comm.hpp>
19#include <common/trace-chunk.hpp>
20#include <common/urcu.hpp>
21#include <common/utils.hpp>
22
23#include <lttng/location-internal.hpp>
24
e99e3664 25#include <dirent.h>
d022620a 26#include <inttypes.h>
e99e3664
JG
27#include <limits.h>
28#include <pthread.h>
5b74c7b1
DG
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
a304c14c 32#include <sys/stat.h>
3d071855 33#include <sys/types.h>
e99e3664 34#include <urcu.h>
5b74c7b1 35
f1494934 36namespace {
3e3665b8
JG
37struct ltt_session_destroy_notifier_element {
38 ltt_session_destroy_notifier notifier;
39 void *user_data;
40};
41
ccbdaca4
MD
42struct ltt_session_clear_notifier_element {
43 ltt_session_clear_notifier notifier;
44 void *user_data;
45};
46
e99e3664
JG
47namespace ls = lttng::sessiond;
48
8c0faa1d 49/*
b5541356 50 * NOTES:
8c0faa1d 51 *
b5541356 52 * No ltt_session.lock is taken here because those data structure are widely
e1bbf989 53 * spread across the lttng-tools code base so before calling functions below
b5541356 54 * that can read/write a session, the caller MUST acquire the session lock
54d01ffb 55 * using session_lock() and session_unlock().
8c0faa1d 56 */
8c0faa1d 57
f1494934
JG
58/* These characters are forbidden in a session name. Used by validate_name. */
59const char *forbidden_name_chars = "/";
60
61/* Global hash table to keep the sessions, indexed by id. */
cd9adb8b 62struct lttng_ht *ltt_sessions_ht_by_id = nullptr;
f1494934 63/* Global hash table to keep the sessions, indexed by name. */
cd9adb8b 64struct lttng_ht *ltt_sessions_ht_by_name = nullptr;
f1494934 65
5b74c7b1 66/*
b5541356 67 * Init tracing session list.
5b74c7b1 68 *
b5541356 69 * Please see session.h for more explanation and correct usage of the list.
5b74c7b1 70 */
1a496780 71struct ltt_session_list the_session_list;
d9a970b7
JG
72
73/*
74 * Return a ltt_session structure ptr that matches name. If no session found,
75 * NULL is returned. This must be called with the session list lock held using
76 * session_lock_list and session_unlock_list.
77 * A reference to the session is implicitly acquired by this function.
78 */
79struct ltt_session *session_find_by_name(const char *name)
80{
81 struct ltt_session *iter;
82
83 LTTNG_ASSERT(name);
84 ASSERT_SESSION_LIST_LOCKED();
85
86 DBG2("Trying to find session by name %s", name);
87
88 cds_list_for_each_entry (iter, &the_session_list.head, list) {
89 if (!strncmp(iter->name, name, NAME_MAX) && !iter->destroyed) {
90 goto found;
91 }
92 }
93
94 return nullptr;
95found:
96 return session_get(iter) ? iter : nullptr;
97}
98
99/*
100 * Return an ltt_session that matches the id. If no session is found,
101 * NULL is returned. This must be called with rcu_read_lock and
102 * session list lock held (to guarantee the lifetime of the session).
103 */
104struct ltt_session *session_find_by_id(uint64_t id)
105{
106 struct lttng_ht_node_u64 *node;
107 struct lttng_ht_iter iter;
108 struct ltt_session *ls;
109
110 ASSERT_RCU_READ_LOCKED();
111 ASSERT_SESSION_LIST_LOCKED();
112
113 if (!ltt_sessions_ht_by_id) {
114 goto end;
115 }
116
117 lttng_ht_lookup(ltt_sessions_ht_by_id, &id, &iter);
118 node = lttng_ht_iter_get_node_u64(&iter);
119 if (node == nullptr) {
120 goto end;
121 }
122 ls = lttng::utils::container_of(node, &ltt_session::node);
123
124 DBG3("Session %" PRIu64 " found by id.", id);
125 return session_get(ls) ? ls : nullptr;
126
127end:
128 DBG3("Session %" PRIu64 " NOT found by id", id);
129 return nullptr;
130}
f1494934 131} /* namespace */
23324029 132
1c1c3634
DG
133/*
134 * Validate the session name for forbidden characters.
135 *
136 * Return 0 on success else -1 meaning a forbidden char. has been found.
137 */
138static int validate_name(const char *name)
139{
140 int ret;
141 char *tok, *tmp_name;
142
a0377dfe 143 LTTNG_ASSERT(name);
1c1c3634
DG
144
145 tmp_name = strdup(name);
146 if (!tmp_name) {
147 /* ENOMEM here. */
148 ret = -1;
149 goto error;
150 }
151
152 tok = strpbrk(tmp_name, forbidden_name_chars);
153 if (tok) {
154 DBG("Session name %s contains a forbidden character", name);
155 /* Forbidden character has been found. */
156 ret = -1;
157 goto error;
158 }
159 ret = 0;
160
161error:
162 free(tmp_name);
163 return ret;
164}
165
5b74c7b1 166/*
050349bb 167 * Add a ltt_session structure to the global list.
5b74c7b1 168 *
050349bb 169 * The caller MUST acquire the session list lock before.
44e96653 170 * Returns the unique identifier for the session.
5b74c7b1 171 */
d022620a 172static uint64_t add_session_list(struct ltt_session *ls)
5b74c7b1 173{
a0377dfe 174 LTTNG_ASSERT(ls);
0525e9ae 175
f1494934
JG
176 cds_list_add(&ls->list, &the_session_list.head);
177 return the_session_list.next_uuid++;
5b74c7b1
DG
178}
179
180/*
050349bb 181 * Delete a ltt_session structure to the global list.
b5541356 182 *
050349bb 183 * The caller MUST acquire the session list lock before.
5b74c7b1
DG
184 */
185static void del_session_list(struct ltt_session *ls)
186{
a0377dfe 187 LTTNG_ASSERT(ls);
0525e9ae 188
5b74c7b1 189 cds_list_del(&ls->list);
5b74c7b1
DG
190}
191
b5541356 192/*
050349bb 193 * Return a pointer to the session list.
b5541356 194 */
cd9adb8b 195struct ltt_session_list *session_get_list()
b5541356 196{
f1494934 197 return &the_session_list;
b5541356
DG
198}
199
99d688f2
JG
200/*
201 * Returns once the session list is empty.
202 */
d9a970b7 203void session_list_wait_empty(std::unique_lock<std::mutex> list_lock)
99d688f2 204{
1a496780
JG
205 /* Keep waiting until the session list is empty. */
206 the_session_list.removal_cond.wait(list_lock,
207 [] { return cds_list_empty(&the_session_list.head); });
99d688f2
JG
208}
209
71e0a100
JG
210/*
211 * Try to acquire session list lock
212 */
0038180d 213int session_trylock_list() noexcept
71e0a100 214{
1a496780
JG
215 /* Return 0 if successfully acquired. */
216 return the_session_list.lock.try_lock() ? 0 : 1;
71e0a100
JG
217}
218
dd73d57b
JG
219/*
220 * Get the session's consumer destination type.
dd73d57b 221 */
a0a4f314 222enum consumer_dst_type session_get_consumer_destination_type(const ltt_session::locked_ref& session)
dd73d57b
JG
223{
224 /*
225 * The output information is duplicated in both of those session types.
226 * Hence, it doesn't matter from which it is retrieved. However, it is
227 * possible for only one of them to be set.
228 */
28ab034a
JG
229 return session->kernel_session ? session->kernel_session->consumer->type :
230 session->ust_session->consumer->type;
dd73d57b
JG
231}
232
233/*
234 * Get the session's consumer network hostname.
235 * The caller must ensure that the destination is of type "net".
dd73d57b 236 */
a0a4f314 237const char *session_get_net_consumer_hostname(const ltt_session::locked_ref& session)
dd73d57b 238{
cd9adb8b 239 const char *hostname = nullptr;
dd73d57b
JG
240 const struct consumer_output *output;
241
28ab034a
JG
242 output = session->kernel_session ? session->kernel_session->consumer :
243 session->ust_session->consumer;
dd73d57b
JG
244
245 /*
246 * hostname is assumed to be the same for both control and data
247 * connections.
248 */
249 switch (output->dst.net.control.dtype) {
250 case LTTNG_DST_IPV4:
251 hostname = output->dst.net.control.dst.ipv4;
252 break;
253 case LTTNG_DST_IPV6:
254 hostname = output->dst.net.control.dst.ipv6;
255 break;
256 default:
257 abort();
258 }
259 return hostname;
260}
261
262/*
263 * Get the session's consumer network control and data ports.
264 * The caller must ensure that the destination is of type "net".
dd73d57b 265 */
a0a4f314 266void session_get_net_consumer_ports(const ltt_session::locked_ref& session,
28ab034a
JG
267 uint16_t *control_port,
268 uint16_t *data_port)
dd73d57b
JG
269{
270 const struct consumer_output *output;
271
28ab034a
JG
272 output = session->kernel_session ? session->kernel_session->consumer :
273 session->ust_session->consumer;
dd73d57b
JG
274 *control_port = output->dst.net.control.port;
275 *data_port = output->dst.net.data.port;
276}
277
5d65beab
JG
278/*
279 * Get the location of the latest trace archive produced by a rotation.
5d65beab 280 */
28ab034a 281struct lttng_trace_archive_location *
a0a4f314 282session_get_trace_archive_location(const ltt_session::locked_ref& session)
5d65beab 283{
d2956687 284 int ret;
cd9adb8b
JG
285 struct lttng_trace_archive_location *location = nullptr;
286 char *chunk_path = nullptr;
d2956687
JG
287
288 if (session->rotation_state != LTTNG_ROTATION_STATE_COMPLETED ||
28ab034a 289 !session->last_archived_chunk_name) {
d2956687
JG
290 goto end;
291 }
5d65beab 292
5d65beab
JG
293 switch (session_get_consumer_destination_type(session)) {
294 case CONSUMER_DST_LOCAL:
ecd1a12f 295 ret = asprintf(&chunk_path,
28ab034a
JG
296 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY "/%s",
297 session_get_base_path(session),
298 session->last_archived_chunk_name);
ecd1a12f
MD
299 if (ret == -1) {
300 goto end;
301 }
28ab034a 302 location = lttng_trace_archive_location_local_create(chunk_path);
5d65beab
JG
303 break;
304 case CONSUMER_DST_NET:
305 {
306 const char *hostname;
307 uint16_t control_port, data_port;
308
309 hostname = session_get_net_consumer_hostname(session);
28ab034a 310 session_get_net_consumer_ports(session, &control_port, &data_port);
5d65beab 311 location = lttng_trace_archive_location_relay_create(
28ab034a
JG
312 hostname,
313 LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP,
314 control_port,
315 data_port,
316 session->last_chunk_path);
5d65beab
JG
317 break;
318 }
319 default:
320 abort();
321 }
322end:
d2956687 323 free(chunk_path);
5d65beab
JG
324 return location;
325}
326
23324029 327/*
e1bbf989 328 * Allocate the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name HT.
9c6518bc
JG
329 *
330 * The session list lock must be held.
23324029 331 */
cd9adb8b 332static int ltt_sessions_ht_alloc()
23324029
JD
333{
334 int ret = 0;
335
336 DBG("Allocating ltt_sessions_ht_by_id");
337 ltt_sessions_ht_by_id = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
338 if (!ltt_sessions_ht_by_id) {
339 ret = -1;
340 ERR("Failed to allocate ltt_sessions_ht_by_id");
341 goto end;
342 }
e1bbf989
JR
343
344 DBG("Allocating ltt_sessions_ht_by_name");
345 ltt_sessions_ht_by_name = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
346 if (!ltt_sessions_ht_by_name) {
347 ret = -1;
348 ERR("Failed to allocate ltt_sessions_ht_by_name");
349 goto end;
350 }
351
23324029
JD
352end:
353 return ret;
354}
355
356/*
357 * Destroy the ltt_sessions_ht_by_id HT.
9c6518bc
JG
358 *
359 * The session list lock must be held.
23324029 360 */
cd9adb8b 361static void ltt_sessions_ht_destroy()
23324029 362{
e1bbf989 363 if (ltt_sessions_ht_by_id) {
3c339053 364 lttng_ht_destroy(ltt_sessions_ht_by_id);
cd9adb8b 365 ltt_sessions_ht_by_id = nullptr;
e1bbf989
JR
366 }
367
368 if (ltt_sessions_ht_by_name) {
3c339053 369 lttng_ht_destroy(ltt_sessions_ht_by_name);
cd9adb8b 370 ltt_sessions_ht_by_name = nullptr;
23324029 371 }
e1bbf989
JR
372
373 return;
23324029
JD
374}
375
376/*
e1bbf989
JR
377 * Add a ltt_session to the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name.
378 * If unallocated, the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name. HTs
379 * are allocated. The session list lock must be held.
23324029
JD
380 */
381static void add_session_ht(struct ltt_session *ls)
382{
383 int ret;
384
a0377dfe 385 LTTNG_ASSERT(ls);
23324029
JD
386
387 if (!ltt_sessions_ht_by_id) {
388 ret = ltt_sessions_ht_alloc();
389 if (ret) {
390 ERR("Error allocating the sessions HT");
391 goto end;
392 }
393 }
e1bbf989
JR
394
395 /* Should always be present with ltt_sessions_ht_by_id. */
a0377dfe 396 LTTNG_ASSERT(ltt_sessions_ht_by_name);
e1bbf989 397
23324029
JD
398 lttng_ht_node_init_u64(&ls->node, ls->id);
399 lttng_ht_add_unique_u64(ltt_sessions_ht_by_id, &ls->node);
400
e1bbf989
JR
401 lttng_ht_node_init_str(&ls->node_by_name, ls->name);
402 lttng_ht_add_unique_str(ltt_sessions_ht_by_name, &ls->node_by_name);
403
23324029
JD
404end:
405 return;
406}
407
408/*
e1bbf989 409 * Test if ltt_sessions_ht_by_id/name are empty.
2a6ebf6b 410 * Return `false` if hash table objects are null.
23324029
JD
411 * The session list lock must be held.
412 */
cd9adb8b 413static bool ltt_sessions_ht_empty()
23324029 414{
2a6ebf6b 415 bool empty = false;
23324029
JD
416
417 if (!ltt_sessions_ht_by_id) {
2a6ebf6b 418 /* The hash tables do not exist yet. */
23324029
JD
419 goto end;
420 }
421
a0377dfe 422 LTTNG_ASSERT(ltt_sessions_ht_by_name);
e1bbf989 423
6f729565 424 if (lttng_ht_get_count(ltt_sessions_ht_by_id) != 0) {
2a6ebf6b
JR
425 /* Not empty.*/
426 goto end;
427 }
428
429 /*
430 * At this point it is expected that the `ltt_sessions_ht_by_name` ht is
431 * empty.
432 *
433 * The removal from both hash tables is done in two different
434 * places:
435 * - removal from `ltt_sessions_ht_by_name` is done during
436 * `session_destroy()`
437 * - removal from `ltt_sessions_ht_by_id` is done later
438 * in `session_release()` on the last reference put.
439 *
440 * This means that it is possible for `ltt_sessions_ht_by_name` to be
441 * empty but for `ltt_sessions_ht_by_id` to still contain objects when
442 * multiple sessions exists. The reverse is false, hence this sanity
443 * check.
444 */
445 LTTNG_ASSERT(lttng_ht_get_count(ltt_sessions_ht_by_name) == 0);
446 empty = true;
23324029 447end:
2a6ebf6b 448 return empty;
23324029
JD
449}
450
451/*
ed41e570 452 * Remove a ltt_session from the ltt_sessions_ht_by_id.
e1bbf989 453 * If empty, the ltt_sessions_ht_by_id/name HTs are freed.
23324029
JD
454 * The session list lock must be held.
455 */
456static void del_session_ht(struct ltt_session *ls)
457{
458 struct lttng_ht_iter iter;
459 int ret;
460
a0377dfe
FD
461 LTTNG_ASSERT(ls);
462 LTTNG_ASSERT(ltt_sessions_ht_by_id);
463 LTTNG_ASSERT(ltt_sessions_ht_by_name);
23324029
JD
464
465 iter.iter.node = &ls->node.node;
466 ret = lttng_ht_del(ltt_sessions_ht_by_id, &iter);
a0377dfe 467 LTTNG_ASSERT(!ret);
23324029
JD
468
469 if (ltt_sessions_ht_empty()) {
e1bbf989 470 DBG("Empty ltt_sessions_ht_by_id/name, destroying hast tables");
23324029
JD
471 ltt_sessions_ht_destroy();
472 }
473}
474
b5541356 475/*
6c9cc2ab 476 * Acquire session lock
b5541356 477 */
54d01ffb 478void session_lock(struct ltt_session *session)
b5541356 479{
a0377dfe 480 LTTNG_ASSERT(session);
d9a970b7
JG
481 session->lock();
482}
0525e9ae 483
d9a970b7
JG
484void ltt_session::lock() const noexcept
485{
486 pthread_mutex_lock(&_lock);
487}
488
489void ltt_session::unlock() const noexcept
490{
491 ltt_session::_const_session_unlock(*this);
6c9cc2ab 492}
b5541356 493
6c9cc2ab
DG
494/*
495 * Release session lock
496 */
54d01ffb 497void session_unlock(struct ltt_session *session)
6c9cc2ab 498{
a0377dfe 499 LTTNG_ASSERT(session);
d9a970b7
JG
500 session->unlock();
501}
0525e9ae 502
a0a4f314 503void ltt_session::_const_session_unlock(const ltt_session& session)
d9a970b7
JG
504{
505 pthread_mutex_unlock(&session._lock);
b5541356
DG
506}
507
a0a4f314 508static int _session_set_trace_chunk_no_lock_check(const ltt_session::locked_ref& session,
28ab034a
JG
509 struct lttng_trace_chunk *new_trace_chunk,
510 struct lttng_trace_chunk **_current_trace_chunk)
82b69413 511{
78fc586b 512 int ret = 0;
82b69413 513 unsigned int i, refs_to_acquire = 0, refs_acquired = 0, refs_to_release = 0;
82b69413
JG
514 struct cds_lfht_iter iter;
515 struct consumer_socket *socket;
d2956687
JG
516 struct lttng_trace_chunk *current_trace_chunk;
517 uint64_t chunk_id;
518 enum lttng_trace_chunk_status chunk_status;
82b69413 519
56047f5a 520 lttng::urcu::read_lock_guard read_lock;
82b69413 521 /*
d2956687
JG
522 * Ownership of current trace chunk is transferred to
523 * `current_trace_chunk`.
82b69413 524 */
d2956687 525 current_trace_chunk = session->current_trace_chunk;
cd9adb8b 526 session->current_trace_chunk = nullptr;
82b69413 527 if (session->ust_session) {
28ab034a 528 lttng_trace_chunk_put(session->ust_session->current_trace_chunk);
cd9adb8b 529 session->ust_session->current_trace_chunk = nullptr;
82b69413
JG
530 }
531 if (session->kernel_session) {
28ab034a 532 lttng_trace_chunk_put(session->kernel_session->current_trace_chunk);
cd9adb8b 533 session->kernel_session->current_trace_chunk = nullptr;
82b69413 534 }
d2956687
JG
535 if (!new_trace_chunk) {
536 ret = 0;
537 goto end;
82b69413 538 }
d2956687 539 chunk_status = lttng_trace_chunk_get_id(new_trace_chunk, &chunk_id);
a0377dfe 540 LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
82b69413 541
d2956687
JG
542 refs_to_acquire = 1;
543 refs_to_acquire += !!session->ust_session;
544 refs_to_acquire += !!session->kernel_session;
545
28ab034a 546 for (refs_acquired = 0; refs_acquired < refs_to_acquire; refs_acquired++) {
d2956687
JG
547 if (!lttng_trace_chunk_get(new_trace_chunk)) {
548 ERR("Failed to acquire reference to new trace chunk of session \"%s\"",
28ab034a 549 session->name);
d2956687 550 goto error;
82b69413
JG
551 }
552 }
553
d2956687 554 if (session->ust_session) {
28ab034a
JG
555 const uint64_t relayd_id = session->ust_session->consumer->net_seq_index;
556 const bool is_local_trace = session->ust_session->consumer->type ==
557 CONSUMER_DST_LOCAL;
e5add6d0 558
d2956687 559 session->ust_session->current_trace_chunk = new_trace_chunk;
f8a37411 560 if (is_local_trace) {
d2956687 561 enum lttng_error_code ret_error_code;
82b69413 562
28ab034a
JG
563 ret_error_code =
564 ust_app_create_channel_subdirectories(session->ust_session);
d2956687 565 if (ret_error_code != LTTNG_OK) {
82b69413
JG
566 goto error;
567 }
f8a37411 568 }
28ab034a
JG
569 cds_lfht_for_each_entry (
570 session->ust_session->consumer->socks->ht, &iter, socket, node.node) {
d2956687
JG
571 pthread_mutex_lock(socket->lock);
572 ret = consumer_create_trace_chunk(socket,
28ab034a
JG
573 relayd_id,
574 session->id,
575 new_trace_chunk,
576 DEFAULT_UST_TRACE_DIR);
d2956687 577 pthread_mutex_unlock(socket->lock);
f8a37411 578 if (ret) {
d2956687 579 goto error;
f8a37411
JG
580 }
581 }
582 }
82b69413 583 if (session->kernel_session) {
28ab034a
JG
584 const uint64_t relayd_id = session->kernel_session->consumer->net_seq_index;
585 const bool is_local_trace = session->kernel_session->consumer->type ==
586 CONSUMER_DST_LOCAL;
e5add6d0 587
d2956687
JG
588 session->kernel_session->current_trace_chunk = new_trace_chunk;
589 if (is_local_trace) {
590 enum lttng_error_code ret_error_code;
591
28ab034a
JG
592 ret_error_code =
593 kernel_create_channel_subdirectories(session->kernel_session);
d2956687 594 if (ret_error_code != LTTNG_OK) {
d2956687
JG
595 goto error;
596 }
f8a37411 597 }
28ab034a
JG
598 cds_lfht_for_each_entry (
599 session->kernel_session->consumer->socks->ht, &iter, socket, node.node) {
d2956687
JG
600 pthread_mutex_lock(socket->lock);
601 ret = consumer_create_trace_chunk(socket,
28ab034a
JG
602 relayd_id,
603 session->id,
604 new_trace_chunk,
605 DEFAULT_KERNEL_TRACE_DIR);
d2956687 606 pthread_mutex_unlock(socket->lock);
f8a37411 607 if (ret) {
d2956687 608 goto error;
f8a37411
JG
609 }
610 }
611 }
82b69413
JG
612
613 /*
614 * Update local current trace chunk state last, only if all remote
d2956687 615 * creations succeeded.
82b69413
JG
616 */
617 session->current_trace_chunk = new_trace_chunk;
d2956687
JG
618 LTTNG_OPTIONAL_SET(&session->most_recent_chunk_id, chunk_id);
619end:
620 if (_current_trace_chunk) {
621 *_current_trace_chunk = current_trace_chunk;
cd9adb8b 622 current_trace_chunk = nullptr;
d2956687
JG
623 }
624end_no_move:
d2956687
JG
625 lttng_trace_chunk_put(current_trace_chunk);
626 return ret;
627error:
82b69413 628 if (session->ust_session) {
cd9adb8b 629 session->ust_session->current_trace_chunk = nullptr;
82b69413
JG
630 }
631 if (session->kernel_session) {
cd9adb8b 632 session->kernel_session->current_trace_chunk = nullptr;
82b69413 633 }
f8a37411 634 /*
82b69413
JG
635 * Release references taken in the case where all references could not
636 * be acquired.
637 */
638 refs_to_release = refs_to_acquire - refs_acquired;
639 for (i = 0; i < refs_to_release; i++) {
640 lttng_trace_chunk_put(new_trace_chunk);
641 }
d2956687
JG
642 ret = -1;
643 goto end_no_move;
82b69413
JG
644}
645
28ab034a 646struct lttng_trace_chunk *
a0a4f314 647session_create_new_trace_chunk(const ltt_session::locked_ref& session,
28ab034a
JG
648 const struct consumer_output *consumer_output_override,
649 const char *session_base_path_override,
650 const char *chunk_name_override)
82b69413
JG
651{
652 int ret;
cd9adb8b 653 struct lttng_trace_chunk *trace_chunk = nullptr;
82b69413 654 enum lttng_trace_chunk_status chunk_status;
cd9adb8b 655 const time_t chunk_creation_ts = time(nullptr);
348a81dc
JG
656 bool is_local_trace;
657 const char *base_path;
cd9adb8b 658 struct lttng_directory_handle *session_output_directory = nullptr;
82b69413 659 const struct lttng_credentials session_credentials = {
ff588497
JR
660 .uid = LTTNG_OPTIONAL_INIT_VALUE(session->uid),
661 .gid = LTTNG_OPTIONAL_INIT_VALUE(session->gid),
82b69413
JG
662 };
663 uint64_t next_chunk_id;
348a81dc 664 const struct consumer_output *output;
a7ceb342 665 const char *new_path;
348a81dc
JG
666
667 if (consumer_output_override) {
668 output = consumer_output_override;
669 } else {
a0377dfe 670 LTTNG_ASSERT(session->ust_session || session->kernel_session);
28ab034a
JG
671 output = session->ust_session ? session->ust_session->consumer :
672 session->kernel_session->consumer;
348a81dc
JG
673 }
674
675 is_local_trace = output->type == CONSUMER_DST_LOCAL;
28ab034a 676 base_path = session_base_path_override ?: consumer_output_get_base_path(output);
82b69413 677
d2956687
JG
678 if (chunk_creation_ts == (time_t) -1) {
679 PERROR("Failed to sample time while creation session \"%s\" trace chunk",
28ab034a 680 session->name);
82b69413
JG
681 goto error;
682 }
82b69413 683
28ab034a
JG
684 next_chunk_id =
685 session->most_recent_chunk_id.is_set ? session->most_recent_chunk_id.value + 1 : 0;
82b69413 686
a7ceb342 687 if (session->current_trace_chunk &&
28ab034a 688 !lttng_trace_chunk_get_name_overridden(session->current_trace_chunk)) {
a7ceb342 689 chunk_status = lttng_trace_chunk_rename_path(session->current_trace_chunk,
28ab034a 690 DEFAULT_CHUNK_TMP_OLD_DIRECTORY);
a7ceb342
MD
691 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
692 goto error;
693 }
694 }
695 if (!session->current_trace_chunk) {
696 if (!session->rotated) {
697 new_path = "";
698 } else {
cd9adb8b 699 new_path = nullptr;
a7ceb342
MD
700 }
701 } else {
702 new_path = DEFAULT_CHUNK_TMP_NEW_DIRECTORY;
703 }
704
28ab034a 705 trace_chunk = lttng_trace_chunk_create(next_chunk_id, chunk_creation_ts, new_path);
82b69413 706 if (!trace_chunk) {
82b69413
JG
707 goto error;
708 }
709
710 if (chunk_name_override) {
28ab034a 711 chunk_status = lttng_trace_chunk_override_name(trace_chunk, chunk_name_override);
d2956687 712 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
82b69413
JG
713 goto error;
714 }
715 }
716
717 if (!is_local_trace) {
718 /*
719 * No need to set crendentials and output directory
720 * for remote trace chunks.
721 */
d2956687 722 goto end;
82b69413
JG
723 }
724
28ab034a 725 chunk_status = lttng_trace_chunk_set_credentials(trace_chunk, &session_credentials);
82b69413 726 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
82b69413
JG
727 goto error;
728 }
729
28ab034a
JG
730 DBG("Creating base output directory of session \"%s\" at %s", session->name, base_path);
731 ret = utils_mkdir_recursive(base_path, S_IRWXU | S_IRWXG, session->uid, session->gid);
82b69413 732 if (ret) {
82b69413
JG
733 goto error;
734 }
cbf53d23
JG
735 session_output_directory = lttng_directory_handle_create(base_path);
736 if (!session_output_directory) {
82b69413
JG
737 goto error;
738 }
28ab034a 739 chunk_status = lttng_trace_chunk_set_as_owner(trace_chunk, session_output_directory);
cbf53d23 740 lttng_directory_handle_put(session_output_directory);
cd9adb8b 741 session_output_directory = nullptr;
82b69413 742 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
82b69413
JG
743 goto error;
744 }
d2956687
JG
745end:
746 return trace_chunk;
82b69413 747error:
cbf53d23 748 lttng_directory_handle_put(session_output_directory);
82b69413 749 lttng_trace_chunk_put(trace_chunk);
cd9adb8b 750 trace_chunk = nullptr;
d2956687
JG
751 goto end;
752}
753
a0a4f314 754int session_close_trace_chunk(const ltt_session::locked_ref& session,
28ab034a
JG
755 struct lttng_trace_chunk *trace_chunk,
756 enum lttng_trace_chunk_command_type close_command,
757 char *closed_trace_chunk_path)
d2956687
JG
758{
759 int ret = 0;
760 bool error_occurred = false;
761 struct cds_lfht_iter iter;
762 struct consumer_socket *socket;
763 enum lttng_trace_chunk_status chunk_status;
cd9adb8b 764 const time_t chunk_close_timestamp = time(nullptr);
a7ceb342 765 const char *new_path;
d2956687 766
28ab034a 767 chunk_status = lttng_trace_chunk_set_close_command(trace_chunk, close_command);
343defc2
MD
768 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
769 ret = -1;
770 goto end;
bbc4768c
JG
771 }
772
d2956687
JG
773 if (chunk_close_timestamp == (time_t) -1) {
774 ERR("Failed to sample the close timestamp of the current trace chunk of session \"%s\"",
28ab034a 775 session->name);
d2956687
JG
776 ret = -1;
777 goto end;
778 }
a7ceb342
MD
779
780 if (close_command == LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE && !session->rotated) {
781 /* New chunk stays in session output directory. */
782 new_path = "";
783 } else {
784 /* Use chunk name for new chunk. */
cd9adb8b 785 new_path = nullptr;
a7ceb342
MD
786 }
787 if (session->current_trace_chunk &&
28ab034a 788 !lttng_trace_chunk_get_name_overridden(session->current_trace_chunk)) {
a7ceb342 789 /* Rename new chunk path. */
28ab034a
JG
790 chunk_status =
791 lttng_trace_chunk_rename_path(session->current_trace_chunk, new_path);
a7ceb342
MD
792 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
793 ret = -1;
794 goto end;
795 }
796 }
797 if (!lttng_trace_chunk_get_name_overridden(trace_chunk) &&
28ab034a 798 close_command == LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION) {
a7ceb342
MD
799 const char *old_path;
800
801 if (!session->rotated) {
802 old_path = "";
803 } else {
cd9adb8b 804 old_path = nullptr;
a7ceb342
MD
805 }
806 /* We need to move back the .tmp_old_chunk to its rightful place. */
28ab034a 807 chunk_status = lttng_trace_chunk_rename_path(trace_chunk, old_path);
a7ceb342
MD
808 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
809 ret = -1;
810 goto end;
811 }
812 }
813 if (close_command == LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED) {
814 session->rotated = true;
815 }
28ab034a 816 chunk_status = lttng_trace_chunk_set_close_timestamp(trace_chunk, chunk_close_timestamp);
d2956687
JG
817 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
818 ERR("Failed to set the close timestamp of the current trace chunk of session \"%s\"",
28ab034a 819 session->name);
d2956687
JG
820 ret = -1;
821 goto end;
822 }
823
824 if (session->ust_session) {
28ab034a 825 const uint64_t relayd_id = session->ust_session->consumer->net_seq_index;
0a184d4e 826
28ab034a
JG
827 cds_lfht_for_each_entry (
828 session->ust_session->consumer->socks->ht, &iter, socket, node.node) {
d2956687
JG
829 pthread_mutex_lock(socket->lock);
830 ret = consumer_close_trace_chunk(socket,
28ab034a
JG
831 relayd_id,
832 session->id,
833 trace_chunk,
834 closed_trace_chunk_path);
d2956687
JG
835 pthread_mutex_unlock(socket->lock);
836 if (ret) {
837 ERR("Failed to close trace chunk on user space consumer");
838 error_occurred = true;
839 }
840 }
841 }
842 if (session->kernel_session) {
28ab034a 843 const uint64_t relayd_id = session->kernel_session->consumer->net_seq_index;
0a184d4e 844
28ab034a
JG
845 cds_lfht_for_each_entry (
846 session->kernel_session->consumer->socks->ht, &iter, socket, node.node) {
d2956687
JG
847 pthread_mutex_lock(socket->lock);
848 ret = consumer_close_trace_chunk(socket,
28ab034a
JG
849 relayd_id,
850 session->id,
851 trace_chunk,
852 closed_trace_chunk_path);
d2956687
JG
853 pthread_mutex_unlock(socket->lock);
854 if (ret) {
855 ERR("Failed to close trace chunk on kernel consumer");
856 error_occurred = true;
857 }
858 }
859 }
860 ret = error_occurred ? -1 : 0;
82b69413 861end:
d2956687 862 return ret;
82b69413
JG
863}
864
04ed9e10
JG
865/*
866 * This function skips the metadata channel as the begin/end timestamps of a
867 * metadata packet are useless.
868 *
869 * Moreover, opening a packet after a "clear" will cause problems for live
870 * sessions as it will introduce padding that was not part of the first trace
871 * chunk. The relay daemon expects the content of the metadata stream of
872 * successive metadata trace chunks to be strict supersets of one another.
873 *
874 * For example, flushing a packet at the beginning of the metadata stream of
875 * a trace chunk resulting from a "clear" session command will cause the
876 * size of the metadata stream of the new trace chunk to not match the size of
877 * the metadata stream of the original chunk. This will confuse the relay
878 * daemon as the same "offset" in a metadata stream will no longer point
879 * to the same content.
880 */
a0a4f314 881static enum lttng_error_code session_kernel_open_packets(const ltt_session::locked_ref& session)
04ed9e10
JG
882{
883 enum lttng_error_code ret = LTTNG_OK;
884 struct consumer_socket *socket;
885 struct lttng_ht_iter iter;
886 struct cds_lfht_node *node;
887 struct ltt_kernel_channel *chan;
888
56047f5a 889 lttng::urcu::read_lock_guard read_lock;
04ed9e10
JG
890
891 cds_lfht_first(session->kernel_session->consumer->socks->ht, &iter.iter);
892 node = cds_lfht_iter_get_node(&iter.iter);
0114db0e 893 socket = caa_container_of(node, typeof(*socket), node.node);
04ed9e10 894
28ab034a 895 cds_list_for_each_entry (chan, &session->kernel_session->channel_list.head, list) {
04ed9e10
JG
896 int open_ret;
897
898 DBG("Open packet of kernel channel: channel key = %" PRIu64
28ab034a
JG
899 ", session name = %s, session_id = %" PRIu64,
900 chan->key,
901 session->name,
902 session->id);
04ed9e10
JG
903
904 open_ret = consumer_open_channel_packets(socket, chan->key);
905 if (open_ret < 0) {
906 /* General error (no known error expected). */
907 ret = LTTNG_ERR_UNK;
908 goto end;
909 }
910 }
911
912end:
04ed9e10
JG
913 return ret;
914}
915
a0a4f314 916enum lttng_error_code session_open_packets(const ltt_session::locked_ref& session)
04ed9e10
JG
917{
918 enum lttng_error_code ret = LTTNG_OK;
919
920 DBG("Opening packets of session channels: session name = %s, session id = %" PRIu64,
28ab034a
JG
921 session->name,
922 session->id);
04ed9e10
JG
923
924 if (session->ust_session) {
925 ret = ust_app_open_packets(session);
926 if (ret != LTTNG_OK) {
927 goto end;
928 }
929 }
930
931 if (session->kernel_session) {
932 ret = session_kernel_open_packets(session);
933 if (ret != LTTNG_OK) {
934 goto end;
935 }
936 }
937
938end:
939 return ret;
940}
941
82b69413
JG
942/*
943 * Set a session's current trace chunk.
944 *
945 * Must be called with the session lock held.
946 */
a0a4f314 947int session_set_trace_chunk(const ltt_session::locked_ref& session,
28ab034a
JG
948 struct lttng_trace_chunk *new_trace_chunk,
949 struct lttng_trace_chunk **current_trace_chunk)
82b69413 950{
d9a970b7 951 ASSERT_LOCKED(session->_lock);
28ab034a
JG
952 return _session_set_trace_chunk_no_lock_check(
953 session, new_trace_chunk, current_trace_chunk);
82b69413
JG
954}
955
a0a4f314 956static void session_notify_destruction(const ltt_session::locked_ref& session)
3e3665b8
JG
957{
958 size_t i;
a0a4f314 959 const auto count = lttng_dynamic_array_get_count(&session->destroy_notifiers);
3e3665b8
JG
960
961 for (i = 0; i < count; i++) {
962 const struct ltt_session_destroy_notifier_element *element =
7966af57 963 (ltt_session_destroy_notifier_element *) lttng_dynamic_array_get_element(
28ab034a 964 &session->destroy_notifiers, i);
3e3665b8
JG
965
966 element->notifier(session, element->user_data);
967 }
968}
969
ccbdaca4
MD
970/*
971 * Fire each clear notifier once, and remove them from the array.
972 */
a0a4f314 973void session_notify_clear(const ltt_session::locked_ref& session)
ccbdaca4
MD
974{
975 size_t i;
a0a4f314 976 const auto count = lttng_dynamic_array_get_count(&session->clear_notifiers);
ccbdaca4
MD
977
978 for (i = 0; i < count; i++) {
979 const struct ltt_session_clear_notifier_element *element =
7966af57 980 (ltt_session_clear_notifier_element *) lttng_dynamic_array_get_element(
a0a4f314 981 &session->clear_notifiers, i);
ccbdaca4 982
a0a4f314 983 element->notifier(session, element->user_data);
ccbdaca4 984 }
a0a4f314 985 lttng_dynamic_array_clear(&session->clear_notifiers);
ccbdaca4
MD
986}
987
28ab034a 988static void session_release(struct urcu_ref *ref)
e32d7f27
JG
989{
990 int ret;
991 struct ltt_ust_session *usess;
992 struct ltt_kernel_session *ksess;
d9a970b7 993 struct ltt_session *session = lttng::utils::container_of(ref, &ltt_session::ref_count);
7fdbed1c 994 const bool session_published = session->published;
e32d7f27 995
a0377dfe 996 LTTNG_ASSERT(!session->chunk_being_archived);
d2956687 997
e32d7f27
JG
998 usess = session->ust_session;
999 ksess = session->kernel_session;
3e3665b8 1000
f8a37411 1001 /* Clean kernel session teardown, keeping data for destroy notifier. */
e32d7f27
JG
1002 kernel_destroy_session(ksess);
1003
d070c424 1004 /* UST session teardown, keeping data for destroy notifier. */
e32d7f27
JG
1005 if (usess) {
1006 /* Close any relayd session */
1007 consumer_output_send_destroy_relayd(usess->consumer);
1008
1009 /* Destroy every UST application related to this session. */
1010 ret = ust_app_destroy_trace_all(usess);
1011 if (ret) {
1012 ERR("Error in ust_app_destroy_trace_all");
1013 }
1014
d070c424 1015 /* Clean up the rest, keeping destroy notifier data. */
e32d7f27
JG
1016 trace_ust_destroy_session(usess);
1017 }
1018
1019 /*
1020 * Must notify the kernel thread here to update it's poll set in order to
1021 * remove the channel(s)' fd just destroyed.
1022 */
412d7227 1023 ret = notify_thread_pipe(the_kernel_poll_pipe[1]);
e32d7f27
JG
1024 if (ret < 0) {
1025 PERROR("write kernel poll pipe");
1026 }
1027
1028 DBG("Destroying session %s (id %" PRIu64 ")", session->name, session->id);
e32d7f27 1029
e32d7f27 1030 snapshot_destroy(&session->snapshot);
99d688f2 1031
7fdbed1c 1032 if (session_published) {
1a496780 1033 ASSERT_SESSION_LIST_LOCKED();
f4cc5e83
JG
1034 del_session_list(session);
1035 del_session_ht(session);
f4cc5e83 1036 }
a0a4f314
JG
1037
1038 /*
1039 * The notifiers make use of free-functions that expect a locked reference to a session.
1040 * To create such a reference, we need to acquire the lock and acquire a reference (increase
1041 * the ref-count). To ensure the release of the reference does not re-cross the zero value,
1042 * set the refcount to a tombstone value.
1043 */
1044 session->ref_count.refcount = 0xDEAD5E55;
1045 session_notify_destruction([session]() {
1046 session_lock(session);
1047 session_get(session);
1048 return ltt_session::locked_ref(*session);
1049 }());
1050
1051 pthread_mutex_destroy(&session->_lock);
d070c424 1052
1ac9cb73 1053 consumer_output_put(session->consumer);
d070c424 1054 kernel_free_session(ksess);
cd9adb8b 1055 session->kernel_session = nullptr;
d070c424
MD
1056 if (usess) {
1057 trace_ust_free_session(usess);
cd9adb8b 1058 session->ust_session = nullptr;
d070c424 1059 }
7fdbed1c 1060 lttng_dynamic_array_reset(&session->destroy_notifiers);
ccbdaca4 1061 lttng_dynamic_array_reset(&session->clear_notifiers);
d2956687 1062 free(session->last_archived_chunk_name);
6fa5fe7c 1063 free(session->base_path);
c08136a3 1064 lttng_trigger_put(session->rotate_trigger);
e32d7f27 1065 free(session);
7fdbed1c
JG
1066 if (session_published) {
1067 /*
1a496780
JG
1068 * Notify after free-ing to ensure the memory is
1069 * reclaimed before the main thread exits (and prevent memory leak
1070 * reports).
7fdbed1c 1071 */
1a496780
JG
1072 ASSERT_SESSION_LIST_LOCKED();
1073 the_session_list.removal_cond.notify_all();
7fdbed1c 1074 }
e32d7f27
JG
1075}
1076
1077/*
1078 * Acquire a reference to a session.
1079 * This function may fail (return false); its return value must be checked.
1080 */
1081bool session_get(struct ltt_session *session)
1082{
d9a970b7 1083 return urcu_ref_get_unless_zero(&session->ref_count);
e32d7f27
JG
1084}
1085
1086/*
1087 * Release a reference to a session.
1088 */
1089void session_put(struct ltt_session *session)
1090{
b178f53e
JG
1091 if (!session) {
1092 return;
1093 }
e32d7f27
JG
1094 /*
1095 * The session list lock must be held as any session_put()
1096 * may cause the removal of the session from the session_list.
1097 */
1a496780 1098 ASSERT_SESSION_LIST_LOCKED();
d9a970b7
JG
1099 LTTNG_ASSERT(session->ref_count.refcount);
1100 urcu_ref_put(&session->ref_count, session_release);
e32d7f27
JG
1101}
1102
1103/*
1104 * Destroy a session.
1105 *
1106 * This method does not immediately release/free the session as other
1107 * components may still hold a reference to the session. However,
1108 * the session should no longer be presented to the user.
1109 *
1110 * Releases the session list's reference to the session
1111 * and marks it as destroyed. Iterations on the session list should be
1112 * mindful of the "destroyed" flag.
1113 */
1114void session_destroy(struct ltt_session *session)
1115{
ed41e570
JR
1116 int ret;
1117 struct lttng_ht_iter iter;
1118
a0377dfe 1119 LTTNG_ASSERT(!session->destroyed);
e32d7f27 1120 session->destroyed = true;
ed41e570
JR
1121
1122 /*
1123 * Remove immediately from the "session by name" hash table. Only one
1124 * session is expected to exist with a given name for at any given time.
1125 *
1126 * Even if a session still technically exists for a little while longer,
1127 * there is no point in performing action on a "destroyed" session.
1128 */
1129 iter.iter.node = &session->node_by_name.node;
1130 ret = lttng_ht_del(ltt_sessions_ht_by_name, &iter);
1131 LTTNG_ASSERT(!ret);
1132
e32d7f27
JG
1133 session_put(session);
1134}
1135
a0a4f314 1136int session_add_destroy_notifier(const ltt_session::locked_ref& session,
28ab034a
JG
1137 ltt_session_destroy_notifier notifier,
1138 void *user_data)
3e3665b8 1139{
28ab034a
JG
1140 const struct ltt_session_destroy_notifier_element element = { .notifier = notifier,
1141 .user_data = user_data };
3e3665b8 1142
28ab034a 1143 return lttng_dynamic_array_add_element(&session->destroy_notifiers, &element);
3e3665b8
JG
1144}
1145
a0a4f314 1146int session_add_clear_notifier(const ltt_session::locked_ref& session,
28ab034a
JG
1147 ltt_session_clear_notifier notifier,
1148 void *user_data)
ccbdaca4 1149{
28ab034a
JG
1150 const struct ltt_session_clear_notifier_element element = { .notifier = notifier,
1151 .user_data = user_data };
ccbdaca4 1152
28ab034a 1153 return lttng_dynamic_array_add_element(&session->clear_notifiers, &element);
ccbdaca4
MD
1154}
1155
5b74c7b1 1156/*
b178f53e
JG
1157 * Create a new session and add it to the session list.
1158 * Session list lock must be held by the caller.
5b74c7b1 1159 */
28ab034a
JG
1160enum lttng_error_code
1161session_create(const char *name, uid_t uid, gid_t gid, struct ltt_session **out_session)
5b74c7b1 1162{
f3ed775e 1163 int ret;
b178f53e 1164 enum lttng_error_code ret_code;
cd9adb8b 1165 struct ltt_session *new_session = nullptr;
e07ae692 1166
1a496780 1167 ASSERT_SESSION_LIST_LOCKED();
b178f53e
JG
1168 if (name) {
1169 struct ltt_session *clashing_session;
1170
1171 clashing_session = session_find_by_name(name);
1172 if (clashing_session) {
1173 session_put(clashing_session);
1174 ret_code = LTTNG_ERR_EXIST_SESS;
1175 goto error;
1176 }
1177 }
64803277 1178 new_session = zmalloc<ltt_session>();
b178f53e
JG
1179 if (!new_session) {
1180 PERROR("Failed to allocate an ltt_session structure");
1181 ret_code = LTTNG_ERR_NOMEM;
1182 goto error;
5b74c7b1
DG
1183 }
1184
3e3665b8 1185 lttng_dynamic_array_init(&new_session->destroy_notifiers,
28ab034a 1186 sizeof(struct ltt_session_destroy_notifier_element),
cd9adb8b 1187 nullptr);
ccbdaca4 1188 lttng_dynamic_array_init(&new_session->clear_notifiers,
28ab034a 1189 sizeof(struct ltt_session_clear_notifier_element),
cd9adb8b 1190 nullptr);
d9a970b7
JG
1191 urcu_ref_init(&new_session->ref_count);
1192 pthread_mutex_init(&new_session->_lock, nullptr);
e32d7f27 1193
cd9adb8b 1194 new_session->creation_time = time(nullptr);
b178f53e
JG
1195 if (new_session->creation_time == (time_t) -1) {
1196 PERROR("Failed to sample session creation time");
1197 ret_code = LTTNG_ERR_SESSION_FAIL;
f3ed775e
DG
1198 goto error;
1199 }
1200
b178f53e
JG
1201 /* Create default consumer output. */
1202 new_session->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
cd9adb8b 1203 if (new_session->consumer == nullptr) {
b178f53e 1204 ret_code = LTTNG_ERR_NOMEM;
1c1c3634
DG
1205 goto error;
1206 }
1207
b178f53e
JG
1208 if (name) {
1209 ret = lttng_strncpy(new_session->name, name, sizeof(new_session->name));
1210 if (ret) {
1211 ret_code = LTTNG_ERR_SESSION_INVALID_CHAR;
1212 goto error;
1213 }
1214 ret = validate_name(name);
1215 if (ret < 0) {
1216 ret_code = LTTNG_ERR_SESSION_INVALID_CHAR;
1217 goto error;
1218 }
1219 } else {
1220 int i = 0;
1221 bool found_name = false;
1222 char datetime[16];
1223 struct tm *timeinfo;
1224
1225 timeinfo = localtime(&new_session->creation_time);
1226 if (!timeinfo) {
1227 ret_code = LTTNG_ERR_SESSION_FAIL;
1228 goto error;
1229 }
1230 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
1231 for (i = 0; i < INT_MAX; i++) {
1232 struct ltt_session *clashing_session;
1233
1234 if (i == 0) {
1235 ret = snprintf(new_session->name,
28ab034a
JG
1236 sizeof(new_session->name),
1237 "%s-%s",
1238 DEFAULT_SESSION_NAME,
1239 datetime);
b178f53e
JG
1240 } else {
1241 ret = snprintf(new_session->name,
28ab034a
JG
1242 sizeof(new_session->name),
1243 "%s%d-%s",
1244 DEFAULT_SESSION_NAME,
1245 i,
1246 datetime);
b178f53e 1247 }
46ef2188 1248 new_session->name_contains_creation_time = true;
b178f53e
JG
1249 if (ret == -1 || ret >= sizeof(new_session->name)) {
1250 /*
1251 * Null-terminate in case the name is used
1252 * in logging statements.
1253 */
1254 new_session->name[sizeof(new_session->name) - 1] = '\0';
1255 ret_code = LTTNG_ERR_SESSION_FAIL;
1256 goto error;
1257 }
1258
28ab034a 1259 clashing_session = session_find_by_name(new_session->name);
b178f53e
JG
1260 session_put(clashing_session);
1261 if (!clashing_session) {
1262 found_name = true;
1263 break;
1264 }
1265 }
1266 if (found_name) {
1267 DBG("Generated session name \"%s\"", new_session->name);
1268 new_session->has_auto_generated_name = true;
1269 } else {
1270 ERR("Failed to auto-generate a session name");
1271 ret_code = LTTNG_ERR_SESSION_FAIL;
1272 goto error;
1273 }
1274 }
1275
d3e2ba59 1276 ret = gethostname(new_session->hostname, sizeof(new_session->hostname));
73184835
DG
1277 if (ret < 0) {
1278 if (errno == ENAMETOOLONG) {
1279 new_session->hostname[sizeof(new_session->hostname) - 1] = '\0';
b178f53e 1280 ERR("Hostname exceeds the maximal permitted length and has been truncated to %s",
28ab034a 1281 new_session->hostname);
73184835 1282 } else {
b178f53e 1283 ret_code = LTTNG_ERR_SESSION_FAIL;
73184835
DG
1284 goto error;
1285 }
d3e2ba59
JD
1286 }
1287
6df2e2c9
MD
1288 new_session->uid = uid;
1289 new_session->gid = gid;
1290
6dc3064a
DG
1291 ret = snapshot_init(&new_session->snapshot);
1292 if (ret < 0) {
b178f53e 1293 ret_code = LTTNG_ERR_NOMEM;
6dc3064a
DG
1294 goto error;
1295 }
1296
4f23c583 1297 new_session->rotation_state = LTTNG_ROTATION_STATE_NO_ROTATION;
92816cc3 1298
b178f53e 1299 /* Add new session to the session list. */
a991f516 1300 new_session->id = add_session_list(new_session);
b178f53e 1301
23324029
JD
1302 /*
1303 * Add the new session to the ltt_sessions_ht_by_id.
1304 * No ownership is taken by the hash table; it is merely
1305 * a wrapper around the session list used for faster access
1306 * by session id.
1307 */
1308 add_session_ht(new_session);
f4cc5e83 1309 new_session->published = true;
b5541356 1310
a4b92340 1311 /*
b178f53e
JG
1312 * Consumer is left to NULL since the create_session_uri command will
1313 * set it up and, if valid, assign it to the session.
a4b92340 1314 */
b178f53e 1315 DBG("Tracing session %s created with ID %" PRIu64 " by uid = %d, gid = %d",
28ab034a
JG
1316 new_session->name,
1317 new_session->id,
1318 new_session->uid,
1319 new_session->gid);
b178f53e
JG
1320 ret_code = LTTNG_OK;
1321end:
1322 if (new_session) {
1323 (void) session_get(new_session);
1324 *out_session = new_session;
1325 }
1326 return ret_code;
5b74c7b1 1327error:
f4cc5e83 1328 session_put(new_session);
cd9adb8b 1329 new_session = nullptr;
b178f53e 1330 goto end;
5b74c7b1 1331}
2f77fc4b
DG
1332
1333/*
d7b377ed 1334 * Check if the UID matches the session. Root user has access to all
2f77fc4b
DG
1335 * sessions.
1336 */
a0a4f314 1337bool session_access_ok(const ltt_session::locked_ref& session, uid_t uid)
2f77fc4b 1338{
d7b377ed 1339 return (uid == session->uid) || uid == 0;
2f77fc4b 1340}
2961f09e
JG
1341
1342/*
1343 * Set a session's rotation state and reset all associated state.
1344 *
1345 * This function resets the rotation state (check timers, pending
1346 * flags, etc.) and sets the result of the last rotation. The result
1347 * can be queries by a liblttng-ctl client.
1348 *
1349 * Be careful of the result passed to this function. For instance,
1350 * on failure to launch a rotation, a client will expect the rotation
83ed9e90 1351 * state to be set to "NO_ROTATION". If an error occurred while the
2961f09e
JG
1352 * rotation was "ONGOING", result should be set to "ERROR", which will
1353 * allow a client to report it.
1354 *
1355 * Must be called with the session and session_list locks held.
1356 */
a0a4f314
JG
1357int session_reset_rotation_state(const ltt_session::locked_ref& session,
1358 enum lttng_rotation_state result)
2961f09e
JG
1359{
1360 int ret = 0;
1361
1a496780 1362 ASSERT_SESSION_LIST_LOCKED();
2961f09e 1363
a0a4f314
JG
1364 session->rotation_state = result;
1365 if (session->rotation_pending_check_timer_enabled) {
2961f09e
JG
1366 ret = timer_session_rotation_pending_check_stop(session);
1367 }
a0a4f314 1368 if (session->chunk_being_archived) {
d2956687
JG
1369 uint64_t chunk_id;
1370 enum lttng_trace_chunk_status chunk_status;
1371
a0a4f314 1372 chunk_status = lttng_trace_chunk_get_id(session->chunk_being_archived, &chunk_id);
a0377dfe 1373 LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
a0a4f314
JG
1374 LTTNG_OPTIONAL_SET(&session->last_archived_chunk_id, chunk_id);
1375 lttng_trace_chunk_put(session->chunk_being_archived);
1376 session->chunk_being_archived = nullptr;
ccbdaca4
MD
1377 /*
1378 * Fire the clear reply notifiers if we are completing a clear
1379 * rotation.
1380 */
1381 session_notify_clear(session);
d2956687 1382 }
2961f09e
JG
1383 return ret;
1384}
e1bbf989
JR
1385
1386/*
1387 * Sample the id of a session looked up via its name.
1388 * Here the term "sampling" hint the caller that this return the id at a given
1389 * point in time with no guarantee that the session for which the id was
1390 * sampled still exist at that point.
1391 *
1392 * Return 0 when the session is not found,
1393 * Return 1 when the session is found and set `id`.
1394 */
1395bool sample_session_id_by_name(const char *name, uint64_t *id)
1396{
1397 bool found = false;
1398 struct lttng_ht_node_str *node;
1399 struct lttng_ht_iter iter;
1400 struct ltt_session *ls;
1401
56047f5a 1402 lttng::urcu::read_lock_guard read_lock;
e1bbf989
JR
1403
1404 if (!ltt_sessions_ht_by_name) {
1405 found = false;
1406 goto end;
1407 }
1408
1409 lttng_ht_lookup(ltt_sessions_ht_by_name, name, &iter);
1410 node = lttng_ht_iter_get_node_str(&iter);
cd9adb8b 1411 if (node == nullptr) {
e1bbf989
JR
1412 found = false;
1413 goto end;
1414 }
1415
0114db0e 1416 ls = lttng::utils::container_of(node, &ltt_session::node_by_name);
e1bbf989
JR
1417 *id = ls->id;
1418 found = true;
1419
1420 DBG3("Session id `%" PRIu64 "` sampled for session `%s", *id, name);
1421end:
e1bbf989
JR
1422 return found;
1423}
e99e3664 1424
d9a970b7 1425void ltt_session::_locked_session_release(ltt_session *session)
e99e3664 1426{
0038180d
JG
1427 if (!session) {
1428 return;
1429 }
1430
e99e3664
JG
1431 session_unlock(session);
1432 session_put(session);
1433}
1434
d9a970b7
JG
1435void ltt_session::_locked_const_session_release(const ltt_session *session)
1436{
1437 if (!session) {
1438 return;
1439 }
1440
1441 ltt_session::_const_session_unlock(*session);
1442 ltt_session::_const_session_put(session);
1443}
1444
1445ltt_session::locked_ref ltt_session::find_locked_session(ltt_session::id_t id)
e99e3664
JG
1446{
1447 lttng::urcu::read_lock_guard rcu_lock;
1448 auto session = session_find_by_id(id);
1449
1450 if (!session) {
d9a970b7 1451 LTTNG_THROW_SESSION_NOT_FOUND_BY_ID_ERROR(id);
e99e3664
JG
1452 }
1453
1454 /*
1455 * The pointer falling out of scope will unlock and release the reference to the
1456 * session.
1457 */
1458 session_lock(session);
a0a4f314 1459 return ltt_session::locked_ref(*session);
e99e3664
JG
1460}
1461
d9a970b7
JG
1462ltt_session::locked_ref ltt_session::find_locked_session(lttng::c_string_view name)
1463{
1464 lttng::urcu::read_lock_guard rcu_lock;
1465 auto session = session_find_by_name(name.data());
1466
1467 if (!session) {
1468 LTTNG_THROW_SESSION_NOT_FOUND_BY_NAME_ERROR(name.data());
1469 }
1470
1471 session_lock(session);
a0a4f314 1472 return ltt_session::locked_ref(*session);
d9a970b7
JG
1473}
1474
1475ltt_session::const_locked_ref ltt_session::find_locked_const_session(ltt_session::id_t id)
1476{
1477 lttng::urcu::read_lock_guard rcu_lock;
1478 auto session = session_find_by_id(id);
1479
1480 if (!session) {
1481 LTTNG_THROW_SESSION_NOT_FOUND_BY_ID_ERROR(id);
1482 }
1483
1484 session_lock(session);
a0a4f314 1485 return ltt_session::const_locked_ref(*session);
d9a970b7
JG
1486}
1487
1488ltt_session::const_locked_ref ltt_session::find_locked_const_session(lttng::c_string_view name)
1489{
1490 lttng::urcu::read_lock_guard rcu_lock;
1491 auto session = session_find_by_name(name.data());
1492
1493 if (!session) {
1494 LTTNG_THROW_SESSION_NOT_FOUND_BY_NAME_ERROR(name.data());
1495 }
1496
1497 session_lock(session);
a0a4f314 1498 return ltt_session::const_locked_ref(*session);
d9a970b7
JG
1499}
1500
1501ltt_session::ref ltt_session::find_session(ltt_session::id_t id)
1502{
1503 lttng::urcu::read_lock_guard rcu_lock;
1504 auto session = session_find_by_id(id);
1505
1506 if (!session) {
1507 LTTNG_THROW_SESSION_NOT_FOUND_BY_ID_ERROR(id);
1508 }
1509
a0a4f314 1510 return ltt_session::ref(*session);
d9a970b7
JG
1511}
1512
1513ltt_session::ref ltt_session::find_session(lttng::c_string_view name)
1514{
1515 lttng::urcu::read_lock_guard rcu_lock;
1516 auto session = session_find_by_name(name.data());
1517
1518 if (!session) {
1519 LTTNG_THROW_SESSION_NOT_FOUND_BY_NAME_ERROR(name.data());
1520 }
1521
a0a4f314 1522 return ltt_session::ref(*session);
d9a970b7
JG
1523}
1524
1525ltt_session::const_ref ltt_session::find_const_session(ltt_session::id_t id)
e99e3664
JG
1526{
1527 lttng::urcu::read_lock_guard rcu_lock;
1528 auto session = session_find_by_id(id);
1529
1530 if (!session) {
d9a970b7
JG
1531 LTTNG_THROW_SESSION_NOT_FOUND_BY_ID_ERROR(id);
1532 }
1533
a0a4f314 1534 return ltt_session::const_ref(*session);
d9a970b7
JG
1535}
1536
1537ltt_session::const_ref ltt_session::find_const_session(lttng::c_string_view name)
1538{
1539 lttng::urcu::read_lock_guard rcu_lock;
1540 auto session = session_find_by_name(name.data());
1541
1542 if (!session) {
1543 LTTNG_THROW_SESSION_NOT_FOUND_BY_NAME_ERROR(name.data());
e99e3664
JG
1544 }
1545
a0a4f314 1546 return ltt_session::const_ref(*session);
d9a970b7
JG
1547}
1548
1549void ltt_session::_const_session_put(const ltt_session *session)
1550{
1551 /*
1552 * The session list lock must be held as any session_put()
1553 * may cause the removal of the session from the session_list.
1554 */
1555 ASSERT_SESSION_LIST_LOCKED();
1556 LTTNG_ASSERT(session->ref_count.refcount);
1557 urcu_ref_put(&session->ref_count, session_release);
1558}
1559
1560std::unique_lock<std::mutex> ls::lock_session_list()
1561{
1562 return std::unique_lock<std::mutex>(the_session_list.lock);
e99e3664 1563}
This page took 0.178552 seconds and 4 git commands to generate.