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