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