Fix: sessiond: rotation trigger leak
[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);
c08136a3 1037 lttng_trigger_put(session->rotate_trigger);
e32d7f27 1038 free(session);
7fdbed1c
JG
1039 if (session_published) {
1040 /*
1041 * Broadcast after free-ing to ensure the memory is
1042 * reclaimed before the main thread exits.
1043 */
f1494934
JG
1044 ASSERT_LOCKED(the_session_list.lock);
1045 pthread_cond_broadcast(&the_session_list.removal_cond);
7fdbed1c 1046 }
e32d7f27
JG
1047}
1048
1049/*
1050 * Acquire a reference to a session.
1051 * This function may fail (return false); its return value must be checked.
1052 */
1053bool session_get(struct ltt_session *session)
1054{
1055 return urcu_ref_get_unless_zero(&session->ref);
1056}
1057
1058/*
1059 * Release a reference to a session.
1060 */
1061void session_put(struct ltt_session *session)
1062{
b178f53e
JG
1063 if (!session) {
1064 return;
1065 }
e32d7f27
JG
1066 /*
1067 * The session list lock must be held as any session_put()
1068 * may cause the removal of the session from the session_list.
1069 */
f1494934 1070 ASSERT_LOCKED(the_session_list.lock);
a0377dfe 1071 LTTNG_ASSERT(session->ref.refcount);
e32d7f27
JG
1072 urcu_ref_put(&session->ref, session_release);
1073}
1074
1075/*
1076 * Destroy a session.
1077 *
1078 * This method does not immediately release/free the session as other
1079 * components may still hold a reference to the session. However,
1080 * the session should no longer be presented to the user.
1081 *
1082 * Releases the session list's reference to the session
1083 * and marks it as destroyed. Iterations on the session list should be
1084 * mindful of the "destroyed" flag.
1085 */
1086void session_destroy(struct ltt_session *session)
1087{
ed41e570
JR
1088 int ret;
1089 struct lttng_ht_iter iter;
1090
a0377dfe 1091 LTTNG_ASSERT(!session->destroyed);
e32d7f27 1092 session->destroyed = true;
ed41e570
JR
1093
1094 /*
1095 * Remove immediately from the "session by name" hash table. Only one
1096 * session is expected to exist with a given name for at any given time.
1097 *
1098 * Even if a session still technically exists for a little while longer,
1099 * there is no point in performing action on a "destroyed" session.
1100 */
1101 iter.iter.node = &session->node_by_name.node;
1102 ret = lttng_ht_del(ltt_sessions_ht_by_name, &iter);
1103 LTTNG_ASSERT(!ret);
1104
e32d7f27
JG
1105 session_put(session);
1106}
1107
3e3665b8
JG
1108int session_add_destroy_notifier(struct ltt_session *session,
1109 ltt_session_destroy_notifier notifier, void *user_data)
1110{
1111 const struct ltt_session_destroy_notifier_element element = {
1112 .notifier = notifier,
1113 .user_data = user_data
1114 };
1115
1116 return lttng_dynamic_array_add_element(&session->destroy_notifiers,
1117 &element);
1118}
1119
ccbdaca4
MD
1120int session_add_clear_notifier(struct ltt_session *session,
1121 ltt_session_clear_notifier notifier, void *user_data)
1122{
1123 const struct ltt_session_clear_notifier_element element = {
1124 .notifier = notifier,
1125 .user_data = user_data
1126 };
1127
1128 return lttng_dynamic_array_add_element(&session->clear_notifiers,
1129 &element);
1130}
1131
5b74c7b1 1132/*
74babd95 1133 * Return a ltt_session structure ptr that matches name. If no session found,
23324029 1134 * NULL is returned. This must be called with the session list lock held using
74babd95 1135 * session_lock_list and session_unlock_list.
e32d7f27 1136 * A reference to the session is implicitly acquired by this function.
5b74c7b1 1137 */
58a1a227 1138struct ltt_session *session_find_by_name(const char *name)
5b74c7b1 1139{
5b74c7b1
DG
1140 struct ltt_session *iter;
1141
a0377dfe 1142 LTTNG_ASSERT(name);
f1494934 1143 ASSERT_LOCKED(the_session_list.lock);
0525e9ae 1144
5f822d0a
DG
1145 DBG2("Trying to find session by name %s", name);
1146
f1494934 1147 cds_list_for_each_entry(iter, &the_session_list.head, list) {
e32d7f27
JG
1148 if (!strncmp(iter->name, name, NAME_MAX) &&
1149 !iter->destroyed) {
74babd95 1150 goto found;
5b74c7b1
DG
1151 }
1152 }
1153
e32d7f27 1154 return NULL;
74babd95 1155found:
e32d7f27 1156 return session_get(iter) ? iter : NULL;
5b74c7b1
DG
1157}
1158
23324029
JD
1159/*
1160 * Return an ltt_session that matches the id. If no session is found,
1161 * NULL is returned. This must be called with rcu_read_lock and
1162 * session list lock held (to guarantee the lifetime of the session).
1163 */
1164struct ltt_session *session_find_by_id(uint64_t id)
1165{
1166 struct lttng_ht_node_u64 *node;
1167 struct lttng_ht_iter iter;
1168 struct ltt_session *ls;
1169
48b7cdc2 1170 ASSERT_RCU_READ_LOCKED();
f1494934 1171 ASSERT_LOCKED(the_session_list.lock);
e32d7f27 1172
d68ec974
JG
1173 if (!ltt_sessions_ht_by_id) {
1174 goto end;
1175 }
1176
23324029
JD
1177 lttng_ht_lookup(ltt_sessions_ht_by_id, &id, &iter);
1178 node = lttng_ht_iter_get_node_u64(&iter);
1179 if (node == NULL) {
d68ec974 1180 goto end;
23324029
JD
1181 }
1182 ls = caa_container_of(node, struct ltt_session, node);
1183
1184 DBG3("Session %" PRIu64 " found by id.", id);
e32d7f27 1185 return session_get(ls) ? ls : NULL;
23324029 1186
d68ec974 1187end:
23324029
JD
1188 DBG3("Session %" PRIu64 " NOT found by id", id);
1189 return NULL;
1190}
1191
5b74c7b1 1192/*
b178f53e
JG
1193 * Create a new session and add it to the session list.
1194 * Session list lock must be held by the caller.
5b74c7b1 1195 */
b178f53e 1196enum lttng_error_code session_create(const char *name, uid_t uid, gid_t gid,
e3876bf0 1197 struct ltt_session **out_session)
5b74c7b1 1198{
f3ed775e 1199 int ret;
b178f53e
JG
1200 enum lttng_error_code ret_code;
1201 struct ltt_session *new_session = NULL;
e07ae692 1202
f1494934 1203 ASSERT_LOCKED(the_session_list.lock);
b178f53e
JG
1204 if (name) {
1205 struct ltt_session *clashing_session;
1206
1207 clashing_session = session_find_by_name(name);
1208 if (clashing_session) {
1209 session_put(clashing_session);
1210 ret_code = LTTNG_ERR_EXIST_SESS;
1211 goto error;
1212 }
1213 }
64803277 1214 new_session = zmalloc<ltt_session>();
b178f53e
JG
1215 if (!new_session) {
1216 PERROR("Failed to allocate an ltt_session structure");
1217 ret_code = LTTNG_ERR_NOMEM;
1218 goto error;
5b74c7b1
DG
1219 }
1220
3e3665b8 1221 lttng_dynamic_array_init(&new_session->destroy_notifiers,
93bed9fe
JG
1222 sizeof(struct ltt_session_destroy_notifier_element),
1223 NULL);
ccbdaca4
MD
1224 lttng_dynamic_array_init(&new_session->clear_notifiers,
1225 sizeof(struct ltt_session_clear_notifier_element),
1226 NULL);
e32d7f27 1227 urcu_ref_init(&new_session->ref);
b178f53e 1228 pthread_mutex_init(&new_session->lock, NULL);
e32d7f27 1229
b178f53e
JG
1230 new_session->creation_time = time(NULL);
1231 if (new_session->creation_time == (time_t) -1) {
1232 PERROR("Failed to sample session creation time");
1233 ret_code = LTTNG_ERR_SESSION_FAIL;
f3ed775e
DG
1234 goto error;
1235 }
1236
b178f53e
JG
1237 /* Create default consumer output. */
1238 new_session->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
1239 if (new_session->consumer == NULL) {
1240 ret_code = LTTNG_ERR_NOMEM;
1c1c3634
DG
1241 goto error;
1242 }
1243
b178f53e
JG
1244 if (name) {
1245 ret = lttng_strncpy(new_session->name, name, sizeof(new_session->name));
1246 if (ret) {
1247 ret_code = LTTNG_ERR_SESSION_INVALID_CHAR;
1248 goto error;
1249 }
1250 ret = validate_name(name);
1251 if (ret < 0) {
1252 ret_code = LTTNG_ERR_SESSION_INVALID_CHAR;
1253 goto error;
1254 }
1255 } else {
1256 int i = 0;
1257 bool found_name = false;
1258 char datetime[16];
1259 struct tm *timeinfo;
1260
1261 timeinfo = localtime(&new_session->creation_time);
1262 if (!timeinfo) {
1263 ret_code = LTTNG_ERR_SESSION_FAIL;
1264 goto error;
1265 }
1266 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
1267 for (i = 0; i < INT_MAX; i++) {
1268 struct ltt_session *clashing_session;
1269
1270 if (i == 0) {
1271 ret = snprintf(new_session->name,
1272 sizeof(new_session->name),
1273 "%s-%s",
1274 DEFAULT_SESSION_NAME,
1275 datetime);
1276 } else {
1277 ret = snprintf(new_session->name,
1278 sizeof(new_session->name),
1279 "%s%d-%s",
1280 DEFAULT_SESSION_NAME, i,
1281 datetime);
1282 }
46ef2188 1283 new_session->name_contains_creation_time = true;
b178f53e
JG
1284 if (ret == -1 || ret >= sizeof(new_session->name)) {
1285 /*
1286 * Null-terminate in case the name is used
1287 * in logging statements.
1288 */
1289 new_session->name[sizeof(new_session->name) - 1] = '\0';
1290 ret_code = LTTNG_ERR_SESSION_FAIL;
1291 goto error;
1292 }
1293
1294 clashing_session =
1295 session_find_by_name(new_session->name);
1296 session_put(clashing_session);
1297 if (!clashing_session) {
1298 found_name = true;
1299 break;
1300 }
1301 }
1302 if (found_name) {
1303 DBG("Generated session name \"%s\"", new_session->name);
1304 new_session->has_auto_generated_name = true;
1305 } else {
1306 ERR("Failed to auto-generate a session name");
1307 ret_code = LTTNG_ERR_SESSION_FAIL;
1308 goto error;
1309 }
1310 }
1311
d3e2ba59 1312 ret = gethostname(new_session->hostname, sizeof(new_session->hostname));
73184835
DG
1313 if (ret < 0) {
1314 if (errno == ENAMETOOLONG) {
1315 new_session->hostname[sizeof(new_session->hostname) - 1] = '\0';
b178f53e
JG
1316 ERR("Hostname exceeds the maximal permitted length and has been truncated to %s",
1317 new_session->hostname);
73184835 1318 } else {
b178f53e 1319 ret_code = LTTNG_ERR_SESSION_FAIL;
73184835
DG
1320 goto error;
1321 }
d3e2ba59
JD
1322 }
1323
6df2e2c9
MD
1324 new_session->uid = uid;
1325 new_session->gid = gid;
1326
6dc3064a
DG
1327 ret = snapshot_init(&new_session->snapshot);
1328 if (ret < 0) {
b178f53e 1329 ret_code = LTTNG_ERR_NOMEM;
6dc3064a
DG
1330 goto error;
1331 }
1332
4f23c583 1333 new_session->rotation_state = LTTNG_ROTATION_STATE_NO_ROTATION;
92816cc3 1334
b178f53e 1335 /* Add new session to the session list. */
a991f516 1336 new_session->id = add_session_list(new_session);
b178f53e 1337
23324029
JD
1338 /*
1339 * Add the new session to the ltt_sessions_ht_by_id.
1340 * No ownership is taken by the hash table; it is merely
1341 * a wrapper around the session list used for faster access
1342 * by session id.
1343 */
1344 add_session_ht(new_session);
f4cc5e83 1345 new_session->published = true;
b5541356 1346
a4b92340 1347 /*
b178f53e
JG
1348 * Consumer is left to NULL since the create_session_uri command will
1349 * set it up and, if valid, assign it to the session.
a4b92340 1350 */
b178f53e
JG
1351 DBG("Tracing session %s created with ID %" PRIu64 " by uid = %d, gid = %d",
1352 new_session->name, new_session->id, new_session->uid,
1353 new_session->gid);
1354 ret_code = LTTNG_OK;
1355end:
1356 if (new_session) {
1357 (void) session_get(new_session);
1358 *out_session = new_session;
1359 }
1360 return ret_code;
5b74c7b1 1361error:
f4cc5e83 1362 session_put(new_session);
b178f53e
JG
1363 new_session = NULL;
1364 goto end;
5b74c7b1 1365}
2f77fc4b
DG
1366
1367/*
d7b377ed 1368 * Check if the UID matches the session. Root user has access to all
2f77fc4b
DG
1369 * sessions.
1370 */
d7b377ed 1371bool session_access_ok(struct ltt_session *session, uid_t uid)
2f77fc4b 1372{
a0377dfe 1373 LTTNG_ASSERT(session);
d7b377ed 1374 return (uid == session->uid) || uid == 0;
2f77fc4b 1375}
2961f09e
JG
1376
1377/*
1378 * Set a session's rotation state and reset all associated state.
1379 *
1380 * This function resets the rotation state (check timers, pending
1381 * flags, etc.) and sets the result of the last rotation. The result
1382 * can be queries by a liblttng-ctl client.
1383 *
1384 * Be careful of the result passed to this function. For instance,
1385 * on failure to launch a rotation, a client will expect the rotation
83ed9e90 1386 * state to be set to "NO_ROTATION". If an error occurred while the
2961f09e
JG
1387 * rotation was "ONGOING", result should be set to "ERROR", which will
1388 * allow a client to report it.
1389 *
1390 * Must be called with the session and session_list locks held.
1391 */
1392int session_reset_rotation_state(struct ltt_session *session,
1393 enum lttng_rotation_state result)
1394{
1395 int ret = 0;
1396
f1494934 1397 ASSERT_LOCKED(the_session_list.lock);
2961f09e
JG
1398 ASSERT_LOCKED(session->lock);
1399
2961f09e
JG
1400 session->rotation_state = result;
1401 if (session->rotation_pending_check_timer_enabled) {
1402 ret = timer_session_rotation_pending_check_stop(session);
1403 }
d2956687
JG
1404 if (session->chunk_being_archived) {
1405 uint64_t chunk_id;
1406 enum lttng_trace_chunk_status chunk_status;
1407
1408 chunk_status = lttng_trace_chunk_get_id(
1409 session->chunk_being_archived,
1410 &chunk_id);
a0377dfe 1411 LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
d2956687
JG
1412 LTTNG_OPTIONAL_SET(&session->last_archived_chunk_id,
1413 chunk_id);
1414 lttng_trace_chunk_put(session->chunk_being_archived);
1415 session->chunk_being_archived = NULL;
ccbdaca4
MD
1416 /*
1417 * Fire the clear reply notifiers if we are completing a clear
1418 * rotation.
1419 */
1420 session_notify_clear(session);
d2956687 1421 }
2961f09e
JG
1422 return ret;
1423}
e1bbf989
JR
1424
1425/*
1426 * Sample the id of a session looked up via its name.
1427 * Here the term "sampling" hint the caller that this return the id at a given
1428 * point in time with no guarantee that the session for which the id was
1429 * sampled still exist at that point.
1430 *
1431 * Return 0 when the session is not found,
1432 * Return 1 when the session is found and set `id`.
1433 */
1434bool sample_session_id_by_name(const char *name, uint64_t *id)
1435{
1436 bool found = false;
1437 struct lttng_ht_node_str *node;
1438 struct lttng_ht_iter iter;
1439 struct ltt_session *ls;
1440
1441 rcu_read_lock();
1442
1443 if (!ltt_sessions_ht_by_name) {
1444 found = false;
1445 goto end;
1446 }
1447
1448 lttng_ht_lookup(ltt_sessions_ht_by_name, name, &iter);
1449 node = lttng_ht_iter_get_node_str(&iter);
1450 if (node == NULL) {
1451 found = false;
1452 goto end;
1453 }
1454
1455 ls = caa_container_of(node, struct ltt_session, node_by_name);
1456 *id = ls->id;
1457 found = true;
1458
1459 DBG3("Session id `%" PRIu64 "` sampled for session `%s", *id, name);
1460end:
1461 rcu_read_unlock();
1462 return found;
1463}
This page took 0.153203 seconds and 4 git commands to generate.