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