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