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