| 1 | /* |
| 2 | * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com> |
| 3 | * David Goulet <dgoulet@efficios.com> |
| 4 | * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License, version 2 only, as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along with |
| 16 | * this program; if not, write to the Free Software Foundation, Inc., 51 |
| 17 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | */ |
| 19 | |
| 20 | #define _LGPL_SOURCE |
| 21 | #include <common/common.h> |
| 22 | #include <common/utils.h> |
| 23 | #include <common/compat/uuid.h> |
| 24 | #include <urcu/rculist.h> |
| 25 | |
| 26 | #include <sys/stat.h> |
| 27 | |
| 28 | #include "ctf-trace.h" |
| 29 | #include "lttng-relayd.h" |
| 30 | #include "session.h" |
| 31 | #include "sessiond-trace-chunks.h" |
| 32 | #include "stream.h" |
| 33 | #include <common/defaults.h> |
| 34 | #include "utils.h" |
| 35 | |
| 36 | /* Global session id used in the session creation. */ |
| 37 | static uint64_t last_relay_session_id; |
| 38 | static pthread_mutex_t last_relay_session_id_lock = PTHREAD_MUTEX_INITIALIZER; |
| 39 | |
| 40 | static int init_session_output_path(struct relay_session *session) |
| 41 | { |
| 42 | /* |
| 43 | * session_directory: |
| 44 | * |
| 45 | * if base_path is \0' |
| 46 | * hostname/session_name |
| 47 | * else |
| 48 | * hostname/base_path |
| 49 | */ |
| 50 | char *session_directory = NULL; |
| 51 | int ret = 0; |
| 52 | |
| 53 | if (session->output_path[0] != '\0') { |
| 54 | goto end; |
| 55 | } |
| 56 | /* |
| 57 | * If base path is set, it overrides the session name for the |
| 58 | * session relative base path. No timestamp is appended if the |
| 59 | * base path is overridden. |
| 60 | * |
| 61 | * If the session name already contains the creation time (e.g. |
| 62 | * auto-<timestamp>, don't append yet another timestamp after |
| 63 | * the session name in the generated path. |
| 64 | * |
| 65 | * Otherwise, generate the path with session_name-<timestamp>. |
| 66 | */ |
| 67 | if (session->base_path[0] != '\0') { |
| 68 | ret = asprintf(&session_directory, "%s/%s", session->hostname, |
| 69 | session->base_path); |
| 70 | } else if (session->session_name_contains_creation_time) { |
| 71 | ret = asprintf(&session_directory, "%s/%s", session->hostname, |
| 72 | session->session_name); |
| 73 | } else { |
| 74 | char session_creation_datetime[16]; |
| 75 | size_t strftime_ret; |
| 76 | struct tm *timeinfo; |
| 77 | time_t creation_time; |
| 78 | |
| 79 | /* |
| 80 | * The 2.11+ protocol guarantees that a creation time |
| 81 | * is provided for a session. This would indicate a |
| 82 | * protocol error or an improper use of this util. |
| 83 | */ |
| 84 | if (!session->creation_time.is_set) { |
| 85 | ERR("Creation time missing for session \"%s\" (protocol error)", |
| 86 | session->session_name); |
| 87 | ret = -1; |
| 88 | goto end; |
| 89 | } |
| 90 | creation_time = LTTNG_OPTIONAL_GET(session->creation_time); |
| 91 | |
| 92 | timeinfo = localtime(&creation_time); |
| 93 | if (!timeinfo) { |
| 94 | ERR("Failed to get timeinfo while initializing session output directory handle"); |
| 95 | ret = -1; |
| 96 | goto end; |
| 97 | } |
| 98 | strftime_ret = strftime(session_creation_datetime, |
| 99 | sizeof(session_creation_datetime), |
| 100 | "%Y%m%d-%H%M%S", timeinfo); |
| 101 | if (strftime_ret == 0) { |
| 102 | ERR("Failed to format session creation timestamp while initializing session output directory handle"); |
| 103 | ret = -1; |
| 104 | goto end; |
| 105 | } |
| 106 | ret = asprintf(&session_directory, "%s/%s-%s", |
| 107 | session->hostname, session->session_name, |
| 108 | session_creation_datetime); |
| 109 | } |
| 110 | if (ret < 0) { |
| 111 | PERROR("Failed to format session directory name"); |
| 112 | goto end; |
| 113 | } |
| 114 | |
| 115 | if (strlen(session_directory) >= LTTNG_PATH_MAX) { |
| 116 | ERR("Session output directory exceeds maximal length"); |
| 117 | ret = -1; |
| 118 | goto end; |
| 119 | } |
| 120 | strcpy(session->output_path, session_directory); |
| 121 | ret = 0; |
| 122 | |
| 123 | end: |
| 124 | free(session_directory); |
| 125 | return ret; |
| 126 | } |
| 127 | |
| 128 | static int session_set_anonymous_chunk(struct relay_session *session) |
| 129 | { |
| 130 | int ret = 0; |
| 131 | struct lttng_trace_chunk *chunk = NULL; |
| 132 | enum lttng_trace_chunk_status status; |
| 133 | struct lttng_directory_handle output_directory; |
| 134 | |
| 135 | ret = session_init_output_directory_handle(session, &output_directory); |
| 136 | if (ret) { |
| 137 | goto end; |
| 138 | } |
| 139 | |
| 140 | chunk = lttng_trace_chunk_create_anonymous(); |
| 141 | if (!chunk) { |
| 142 | goto end; |
| 143 | } |
| 144 | |
| 145 | status = lttng_trace_chunk_set_credentials_current_user(chunk); |
| 146 | if (status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 147 | ret = -1; |
| 148 | goto end; |
| 149 | } |
| 150 | |
| 151 | status = lttng_trace_chunk_set_as_owner(chunk, &output_directory); |
| 152 | if (status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 153 | ret = -1; |
| 154 | goto end; |
| 155 | } |
| 156 | session->current_trace_chunk = chunk; |
| 157 | chunk = NULL; |
| 158 | end: |
| 159 | lttng_trace_chunk_put(chunk); |
| 160 | lttng_directory_handle_fini(&output_directory); |
| 161 | return ret; |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | * Create a new session by assigning a new session ID. |
| 166 | * |
| 167 | * Return allocated session or else NULL. |
| 168 | */ |
| 169 | struct relay_session *session_create(const char *session_name, |
| 170 | const char *hostname, const char *base_path, |
| 171 | uint32_t live_timer, |
| 172 | bool snapshot, |
| 173 | const lttng_uuid sessiond_uuid, |
| 174 | const uint64_t *id_sessiond, |
| 175 | const uint64_t *current_chunk_id, |
| 176 | const time_t *creation_time, |
| 177 | uint32_t major, |
| 178 | uint32_t minor, |
| 179 | bool session_name_contains_creation_time) |
| 180 | { |
| 181 | int ret; |
| 182 | struct relay_session *session = NULL; |
| 183 | |
| 184 | assert(session_name); |
| 185 | assert(hostname); |
| 186 | assert(base_path); |
| 187 | |
| 188 | if (strstr(session_name, ".")) { |
| 189 | ERR("Illegal character in session name: \"%s\"", |
| 190 | session_name); |
| 191 | goto error; |
| 192 | } |
| 193 | if (strstr(base_path, "../")) { |
| 194 | ERR("Invalid session base path walks up the path hierarchy: \"%s\"", |
| 195 | base_path); |
| 196 | goto error; |
| 197 | } |
| 198 | if (strstr(hostname, ".")) { |
| 199 | ERR("Invalid character in hostname: \"%s\"", |
| 200 | hostname); |
| 201 | goto error; |
| 202 | } |
| 203 | |
| 204 | session = zmalloc(sizeof(*session)); |
| 205 | if (!session) { |
| 206 | PERROR("Failed to allocate session"); |
| 207 | goto error; |
| 208 | } |
| 209 | if (lttng_strncpy(session->session_name, session_name, |
| 210 | sizeof(session->session_name))) { |
| 211 | WARN("Session name exceeds maximal allowed length"); |
| 212 | goto error; |
| 213 | } |
| 214 | if (lttng_strncpy(session->hostname, hostname, |
| 215 | sizeof(session->hostname))) { |
| 216 | WARN("Hostname exceeds maximal allowed length"); |
| 217 | goto error; |
| 218 | } |
| 219 | if (lttng_strncpy(session->base_path, base_path, |
| 220 | sizeof(session->base_path))) { |
| 221 | WARN("Base path exceeds maximal allowed length"); |
| 222 | goto error; |
| 223 | } |
| 224 | if (creation_time) { |
| 225 | LTTNG_OPTIONAL_SET(&session->creation_time, *creation_time); |
| 226 | } |
| 227 | session->session_name_contains_creation_time = |
| 228 | session_name_contains_creation_time; |
| 229 | |
| 230 | session->ctf_traces_ht = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
| 231 | if (!session->ctf_traces_ht) { |
| 232 | goto error; |
| 233 | } |
| 234 | |
| 235 | pthread_mutex_lock(&last_relay_session_id_lock); |
| 236 | session->id = ++last_relay_session_id; |
| 237 | pthread_mutex_unlock(&last_relay_session_id_lock); |
| 238 | |
| 239 | session->major = major; |
| 240 | session->minor = minor; |
| 241 | lttng_ht_node_init_u64(&session->session_n, session->id); |
| 242 | urcu_ref_init(&session->ref); |
| 243 | CDS_INIT_LIST_HEAD(&session->recv_list); |
| 244 | pthread_mutex_init(&session->lock, NULL); |
| 245 | pthread_mutex_init(&session->recv_list_lock, NULL); |
| 246 | |
| 247 | session->live_timer = live_timer; |
| 248 | session->snapshot = snapshot; |
| 249 | lttng_uuid_copy(session->sessiond_uuid, sessiond_uuid); |
| 250 | |
| 251 | if (id_sessiond) { |
| 252 | LTTNG_OPTIONAL_SET(&session->id_sessiond, *id_sessiond); |
| 253 | } |
| 254 | |
| 255 | if (major == 2 && minor >= 11) { |
| 256 | /* Only applies for 2.11+ peers using trace chunks. */ |
| 257 | ret = init_session_output_path(session); |
| 258 | if (ret) { |
| 259 | goto error; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | ret = sessiond_trace_chunk_registry_session_created( |
| 264 | sessiond_trace_chunk_registry, sessiond_uuid); |
| 265 | if (ret) { |
| 266 | goto error; |
| 267 | } |
| 268 | |
| 269 | if (id_sessiond && current_chunk_id) { |
| 270 | session->current_trace_chunk = |
| 271 | sessiond_trace_chunk_registry_get_chunk( |
| 272 | sessiond_trace_chunk_registry, |
| 273 | session->sessiond_uuid, |
| 274 | session->id_sessiond.value, |
| 275 | *current_chunk_id); |
| 276 | if (!session->current_trace_chunk) { |
| 277 | char uuid_str[UUID_STR_LEN]; |
| 278 | |
| 279 | lttng_uuid_to_str(sessiond_uuid, uuid_str); |
| 280 | ERR("Could not find trace chunk: sessiond = {%s}, sessiond session id = %" PRIu64 ", trace chunk id = %" PRIu64, |
| 281 | uuid_str, *id_sessiond, |
| 282 | *current_chunk_id); |
| 283 | } |
| 284 | } else if (!id_sessiond) { |
| 285 | /* |
| 286 | * Pre-2.11 peers will not announce trace chunks. An |
| 287 | * anonymous trace chunk which will remain set for the |
| 288 | * duration of the session is created. |
| 289 | */ |
| 290 | ret = session_set_anonymous_chunk(session); |
| 291 | if (ret) { |
| 292 | goto error; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | lttng_ht_add_unique_u64(sessions_ht, &session->session_n); |
| 297 | return session; |
| 298 | |
| 299 | error: |
| 300 | session_put(session); |
| 301 | return NULL; |
| 302 | } |
| 303 | |
| 304 | /* Should be called with RCU read-side lock held. */ |
| 305 | bool session_get(struct relay_session *session) |
| 306 | { |
| 307 | return urcu_ref_get_unless_zero(&session->ref); |
| 308 | } |
| 309 | |
| 310 | /* |
| 311 | * Lookup a session within the session hash table using the session id |
| 312 | * as key. A session reference is taken when a session is returned. |
| 313 | * session_put() must be called on that session. |
| 314 | * |
| 315 | * Return session or NULL if not found. |
| 316 | */ |
| 317 | struct relay_session *session_get_by_id(uint64_t id) |
| 318 | { |
| 319 | struct relay_session *session = NULL; |
| 320 | struct lttng_ht_node_u64 *node; |
| 321 | struct lttng_ht_iter iter; |
| 322 | |
| 323 | rcu_read_lock(); |
| 324 | lttng_ht_lookup(sessions_ht, &id, &iter); |
| 325 | node = lttng_ht_iter_get_node_u64(&iter); |
| 326 | if (!node) { |
| 327 | DBG("Session find by ID %" PRIu64 " id NOT found", id); |
| 328 | goto end; |
| 329 | } |
| 330 | session = caa_container_of(node, struct relay_session, session_n); |
| 331 | DBG("Session find by ID %" PRIu64 " id found", id); |
| 332 | if (!session_get(session)) { |
| 333 | session = NULL; |
| 334 | } |
| 335 | end: |
| 336 | rcu_read_unlock(); |
| 337 | return session; |
| 338 | } |
| 339 | |
| 340 | static void rcu_destroy_session(struct rcu_head *rcu_head) |
| 341 | { |
| 342 | struct relay_session *session = |
| 343 | caa_container_of(rcu_head, struct relay_session, |
| 344 | rcu_node); |
| 345 | /* |
| 346 | * Since each trace has a reference on the session, it means |
| 347 | * that if we are at the point where we teardown the session, no |
| 348 | * trace belonging to that session exist at this point. |
| 349 | * Calling lttng_ht_destroy in call_rcu worker thread so we |
| 350 | * don't hold the RCU read-side lock while calling it. |
| 351 | */ |
| 352 | lttng_ht_destroy(session->ctf_traces_ht); |
| 353 | free(session); |
| 354 | } |
| 355 | |
| 356 | /* |
| 357 | * Delete session from the given hash table. |
| 358 | * |
| 359 | * Return lttng ht del error code being 0 on success and 1 on failure. |
| 360 | */ |
| 361 | static int session_delete(struct relay_session *session) |
| 362 | { |
| 363 | struct lttng_ht_iter iter; |
| 364 | |
| 365 | iter.iter.node = &session->session_n.node; |
| 366 | return lttng_ht_del(sessions_ht, &iter); |
| 367 | } |
| 368 | |
| 369 | |
| 370 | static void destroy_session(struct relay_session *session) |
| 371 | { |
| 372 | int ret; |
| 373 | |
| 374 | ret = session_delete(session); |
| 375 | assert(!ret); |
| 376 | lttng_trace_chunk_put(session->current_trace_chunk); |
| 377 | session->current_trace_chunk = NULL; |
| 378 | lttng_trace_chunk_put(session->pending_closure_trace_chunk); |
| 379 | session->pending_closure_trace_chunk = NULL; |
| 380 | ret = sessiond_trace_chunk_registry_session_destroyed( |
| 381 | sessiond_trace_chunk_registry, session->sessiond_uuid); |
| 382 | assert(!ret); |
| 383 | call_rcu(&session->rcu_node, rcu_destroy_session); |
| 384 | } |
| 385 | |
| 386 | void session_release(struct urcu_ref *ref) |
| 387 | { |
| 388 | struct relay_session *session = |
| 389 | caa_container_of(ref, struct relay_session, ref); |
| 390 | |
| 391 | destroy_session(session); |
| 392 | } |
| 393 | |
| 394 | void session_put(struct relay_session *session) |
| 395 | { |
| 396 | if (!session) { |
| 397 | return; |
| 398 | } |
| 399 | rcu_read_lock(); |
| 400 | urcu_ref_put(&session->ref, session_release); |
| 401 | rcu_read_unlock(); |
| 402 | } |
| 403 | |
| 404 | int session_close(struct relay_session *session) |
| 405 | { |
| 406 | int ret = 0; |
| 407 | struct ctf_trace *trace; |
| 408 | struct lttng_ht_iter iter; |
| 409 | struct relay_stream *stream; |
| 410 | |
| 411 | pthread_mutex_lock(&session->lock); |
| 412 | DBG("closing session %" PRIu64 ": is conn already closed %d", |
| 413 | session->id, session->connection_closed); |
| 414 | session->connection_closed = true; |
| 415 | pthread_mutex_unlock(&session->lock); |
| 416 | |
| 417 | rcu_read_lock(); |
| 418 | cds_lfht_for_each_entry(session->ctf_traces_ht->ht, |
| 419 | &iter.iter, trace, node.node) { |
| 420 | ret = ctf_trace_close(trace); |
| 421 | if (ret) { |
| 422 | goto rcu_unlock; |
| 423 | } |
| 424 | } |
| 425 | cds_list_for_each_entry_rcu(stream, &session->recv_list, |
| 426 | recv_node) { |
| 427 | /* Close streams which have not been published yet. */ |
| 428 | try_stream_close(stream); |
| 429 | } |
| 430 | rcu_unlock: |
| 431 | rcu_read_unlock(); |
| 432 | if (ret) { |
| 433 | return ret; |
| 434 | } |
| 435 | /* Put self-reference from create. */ |
| 436 | session_put(session); |
| 437 | return ret; |
| 438 | } |
| 439 | |
| 440 | int session_abort(struct relay_session *session) |
| 441 | { |
| 442 | int ret = 0; |
| 443 | |
| 444 | if (!session) { |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | pthread_mutex_lock(&session->lock); |
| 449 | DBG("aborting session %" PRIu64, session->id); |
| 450 | session->aborted = true; |
| 451 | pthread_mutex_unlock(&session->lock); |
| 452 | return ret; |
| 453 | } |
| 454 | |
| 455 | void print_sessions(void) |
| 456 | { |
| 457 | struct lttng_ht_iter iter; |
| 458 | struct relay_session *session; |
| 459 | |
| 460 | if (!sessions_ht) { |
| 461 | return; |
| 462 | } |
| 463 | |
| 464 | rcu_read_lock(); |
| 465 | cds_lfht_for_each_entry(sessions_ht->ht, &iter.iter, session, |
| 466 | session_n.node) { |
| 467 | if (!session_get(session)) { |
| 468 | continue; |
| 469 | } |
| 470 | DBG("session %p refcount %ld session %" PRIu64, |
| 471 | session, |
| 472 | session->ref.refcount, |
| 473 | session->id); |
| 474 | session_put(session); |
| 475 | } |
| 476 | rcu_read_unlock(); |
| 477 | } |
| 478 | |
| 479 | int session_init_output_directory_handle(struct relay_session *session, |
| 480 | struct lttng_directory_handle *handle) |
| 481 | { |
| 482 | int ret; |
| 483 | /* |
| 484 | * relayd_output_path/session_directory |
| 485 | * e.g. /home/user/lttng-traces/hostname/session_name |
| 486 | */ |
| 487 | char *full_session_path = NULL; |
| 488 | |
| 489 | pthread_mutex_lock(&session->lock); |
| 490 | full_session_path = create_output_path(session->output_path); |
| 491 | if (!full_session_path) { |
| 492 | ret = -1; |
| 493 | goto end; |
| 494 | } |
| 495 | |
| 496 | ret = utils_mkdir_recursive( |
| 497 | full_session_path, S_IRWXU | S_IRWXG, -1, -1); |
| 498 | if (ret) { |
| 499 | ERR("Failed to create session output path \"%s\"", |
| 500 | full_session_path); |
| 501 | goto end; |
| 502 | } |
| 503 | |
| 504 | ret = lttng_directory_handle_init(handle, full_session_path); |
| 505 | if (ret) { |
| 506 | goto end; |
| 507 | } |
| 508 | end: |
| 509 | pthread_mutex_unlock(&session->lock); |
| 510 | free(full_session_path); |
| 511 | return ret; |
| 512 | } |