| 1 | /* |
| 2 | * Copyright (C) 2011 EfficiOS Inc. |
| 3 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 4 | * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0-only |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | #define _LGPL_SOURCE |
| 11 | #include "ust-consumer.hpp" |
| 12 | |
| 13 | #include <common/common.hpp> |
| 14 | #include <common/compat/endian.hpp> |
| 15 | #include <common/consumer/consumer-metadata-cache.hpp> |
| 16 | #include <common/consumer/consumer-stream.hpp> |
| 17 | #include <common/consumer/consumer-timer.hpp> |
| 18 | #include <common/consumer/consumer.hpp> |
| 19 | #include <common/index/index.hpp> |
| 20 | #include <common/optional.hpp> |
| 21 | #include <common/relayd/relayd.hpp> |
| 22 | #include <common/sessiond-comm/sessiond-comm.hpp> |
| 23 | #include <common/shm.hpp> |
| 24 | #include <common/urcu.hpp> |
| 25 | #include <common/utils.hpp> |
| 26 | |
| 27 | #include <lttng/ust-ctl.h> |
| 28 | #include <lttng/ust-sigbus.h> |
| 29 | |
| 30 | #include <bin/lttng-consumerd/health-consumerd.hpp> |
| 31 | #include <fcntl.h> |
| 32 | #include <inttypes.h> |
| 33 | #include <poll.h> |
| 34 | #include <pthread.h> |
| 35 | #include <signal.h> |
| 36 | #include <stdbool.h> |
| 37 | #include <stdint.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <string.h> |
| 40 | #include <sys/mman.h> |
| 41 | #include <sys/socket.h> |
| 42 | #include <sys/stat.h> |
| 43 | #include <sys/types.h> |
| 44 | #include <unistd.h> |
| 45 | #include <urcu/list.h> |
| 46 | |
| 47 | #define INT_MAX_STR_LEN 12 /* includes \0 */ |
| 48 | |
| 49 | extern struct lttng_consumer_global_data the_consumer_data; |
| 50 | extern int consumer_poll_timeout; |
| 51 | |
| 52 | LTTNG_EXPORT DEFINE_LTTNG_UST_SIGBUS_STATE(); |
| 53 | |
| 54 | /* |
| 55 | * Add channel to internal consumer state. |
| 56 | * |
| 57 | * Returns 0 on success or else a negative value. |
| 58 | */ |
| 59 | static int add_channel(struct lttng_consumer_channel *channel, |
| 60 | struct lttng_consumer_local_data *ctx) |
| 61 | { |
| 62 | int ret = 0; |
| 63 | |
| 64 | LTTNG_ASSERT(channel); |
| 65 | LTTNG_ASSERT(ctx); |
| 66 | |
| 67 | if (ctx->on_recv_channel != nullptr) { |
| 68 | ret = ctx->on_recv_channel(channel); |
| 69 | if (ret == 0) { |
| 70 | ret = consumer_add_channel(channel, ctx); |
| 71 | } else if (ret < 0) { |
| 72 | /* Most likely an ENOMEM. */ |
| 73 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR); |
| 74 | goto error; |
| 75 | } |
| 76 | } else { |
| 77 | ret = consumer_add_channel(channel, ctx); |
| 78 | } |
| 79 | |
| 80 | DBG("UST consumer channel added (key: %" PRIu64 ")", channel->key); |
| 81 | |
| 82 | error: |
| 83 | return ret; |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Allocate and return a consumer stream object. If _alloc_ret is not NULL, the |
| 88 | * error value if applicable is set in it else it is kept untouched. |
| 89 | * |
| 90 | * Return NULL on error else the newly allocated stream object. |
| 91 | */ |
| 92 | static struct lttng_consumer_stream *allocate_stream(int cpu, |
| 93 | int key, |
| 94 | struct lttng_consumer_channel *channel, |
| 95 | struct lttng_consumer_local_data *ctx, |
| 96 | int *_alloc_ret) |
| 97 | { |
| 98 | int alloc_ret; |
| 99 | struct lttng_consumer_stream *stream = nullptr; |
| 100 | |
| 101 | LTTNG_ASSERT(channel); |
| 102 | LTTNG_ASSERT(ctx); |
| 103 | |
| 104 | stream = consumer_stream_create(channel, |
| 105 | channel->key, |
| 106 | key, |
| 107 | channel->name, |
| 108 | channel->relayd_id, |
| 109 | channel->session_id, |
| 110 | channel->trace_chunk, |
| 111 | cpu, |
| 112 | &alloc_ret, |
| 113 | channel->type, |
| 114 | channel->monitor); |
| 115 | if (stream == nullptr) { |
| 116 | switch (alloc_ret) { |
| 117 | case -ENOENT: |
| 118 | /* |
| 119 | * We could not find the channel. Can happen if cpu hotplug |
| 120 | * happens while tearing down. |
| 121 | */ |
| 122 | DBG3("Could not find channel"); |
| 123 | break; |
| 124 | case -ENOMEM: |
| 125 | case -EINVAL: |
| 126 | default: |
| 127 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR); |
| 128 | break; |
| 129 | } |
| 130 | goto error; |
| 131 | } |
| 132 | |
| 133 | consumer_stream_update_channel_attributes(stream, channel); |
| 134 | |
| 135 | error: |
| 136 | if (_alloc_ret) { |
| 137 | *_alloc_ret = alloc_ret; |
| 138 | } |
| 139 | return stream; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * Send the given stream pointer to the corresponding thread. |
| 144 | * |
| 145 | * Returns 0 on success else a negative value. |
| 146 | */ |
| 147 | static int send_stream_to_thread(struct lttng_consumer_stream *stream, |
| 148 | struct lttng_consumer_local_data *ctx) |
| 149 | { |
| 150 | int ret; |
| 151 | struct lttng_pipe *stream_pipe; |
| 152 | |
| 153 | /* Get the right pipe where the stream will be sent. */ |
| 154 | if (stream->metadata_flag) { |
| 155 | consumer_add_metadata_stream(stream); |
| 156 | stream_pipe = ctx->consumer_metadata_pipe; |
| 157 | } else { |
| 158 | consumer_add_data_stream(stream); |
| 159 | stream_pipe = ctx->consumer_data_pipe; |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * From this point on, the stream's ownership has been moved away from |
| 164 | * the channel and it becomes globally visible. Hence, remove it from |
| 165 | * the local stream list to prevent the stream from being both local and |
| 166 | * global. |
| 167 | */ |
| 168 | stream->globally_visible = 1; |
| 169 | cds_list_del_init(&stream->send_node); |
| 170 | |
| 171 | ret = lttng_pipe_write(stream_pipe, &stream, sizeof(stream)); /* NOLINT sizeof used on a |
| 172 | pointer. */ |
| 173 | if (ret < 0) { |
| 174 | ERR("Consumer write %s stream to pipe %d", |
| 175 | stream->metadata_flag ? "metadata" : "data", |
| 176 | lttng_pipe_get_writefd(stream_pipe)); |
| 177 | if (stream->metadata_flag) { |
| 178 | consumer_del_stream_for_metadata(stream); |
| 179 | } else { |
| 180 | consumer_del_stream_for_data(stream); |
| 181 | } |
| 182 | goto error; |
| 183 | } |
| 184 | |
| 185 | error: |
| 186 | return ret; |
| 187 | } |
| 188 | |
| 189 | static int get_stream_shm_path(char *stream_shm_path, const char *shm_path, int cpu) |
| 190 | { |
| 191 | char cpu_nr[INT_MAX_STR_LEN]; /* int max len */ |
| 192 | int ret; |
| 193 | |
| 194 | strncpy(stream_shm_path, shm_path, PATH_MAX); |
| 195 | stream_shm_path[PATH_MAX - 1] = '\0'; |
| 196 | ret = snprintf(cpu_nr, INT_MAX_STR_LEN, "%i", cpu); |
| 197 | if (ret < 0) { |
| 198 | PERROR("snprintf"); |
| 199 | goto end; |
| 200 | } |
| 201 | strncat(stream_shm_path, cpu_nr, PATH_MAX - strlen(stream_shm_path) - 1); |
| 202 | ret = 0; |
| 203 | end: |
| 204 | return ret; |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * Create streams for the given channel using liblttng-ust-ctl. |
| 209 | * The channel lock must be acquired by the caller. |
| 210 | * |
| 211 | * Return 0 on success else a negative value. |
| 212 | */ |
| 213 | static int create_ust_streams(struct lttng_consumer_channel *channel, |
| 214 | struct lttng_consumer_local_data *ctx) |
| 215 | { |
| 216 | int ret, cpu = 0; |
| 217 | struct lttng_ust_ctl_consumer_stream *ustream; |
| 218 | struct lttng_consumer_stream *stream; |
| 219 | pthread_mutex_t *current_stream_lock = nullptr; |
| 220 | |
| 221 | LTTNG_ASSERT(channel); |
| 222 | LTTNG_ASSERT(ctx); |
| 223 | |
| 224 | /* |
| 225 | * While a stream is available from ustctl. When NULL is returned, we've |
| 226 | * reached the end of the possible stream for the channel. |
| 227 | */ |
| 228 | while ((ustream = lttng_ust_ctl_create_stream(channel->uchan, cpu))) { |
| 229 | int wait_fd; |
| 230 | int ust_metadata_pipe[2]; |
| 231 | |
| 232 | health_code_update(); |
| 233 | |
| 234 | if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA && channel->monitor) { |
| 235 | ret = utils_create_pipe_cloexec_nonblock(ust_metadata_pipe); |
| 236 | if (ret < 0) { |
| 237 | ERR("Create ust metadata poll pipe"); |
| 238 | goto error; |
| 239 | } |
| 240 | wait_fd = ust_metadata_pipe[0]; |
| 241 | } else { |
| 242 | wait_fd = lttng_ust_ctl_stream_get_wait_fd(ustream); |
| 243 | } |
| 244 | |
| 245 | /* Allocate consumer stream object. */ |
| 246 | stream = allocate_stream(cpu, wait_fd, channel, ctx, &ret); |
| 247 | if (!stream) { |
| 248 | goto error_alloc; |
| 249 | } |
| 250 | stream->ustream = ustream; |
| 251 | /* |
| 252 | * Store it so we can save multiple function calls afterwards since |
| 253 | * this value is used heavily in the stream threads. This is UST |
| 254 | * specific so this is why it's done after allocation. |
| 255 | */ |
| 256 | stream->wait_fd = wait_fd; |
| 257 | |
| 258 | /* |
| 259 | * Increment channel refcount since the channel reference has now been |
| 260 | * assigned in the allocation process above. |
| 261 | */ |
| 262 | if (stream->chan->monitor) { |
| 263 | uatomic_inc(&stream->chan->refcount); |
| 264 | } |
| 265 | |
| 266 | pthread_mutex_lock(&stream->lock); |
| 267 | current_stream_lock = &stream->lock; |
| 268 | /* |
| 269 | * Order is important this is why a list is used. On error, the caller |
| 270 | * should clean this list. |
| 271 | */ |
| 272 | cds_list_add_tail(&stream->send_node, &channel->streams.head); |
| 273 | |
| 274 | ret = lttng_ust_ctl_get_max_subbuf_size(stream->ustream, &stream->max_sb_size); |
| 275 | if (ret < 0) { |
| 276 | ERR("lttng_ust_ctl_get_max_subbuf_size failed for stream %s", stream->name); |
| 277 | goto error; |
| 278 | } |
| 279 | |
| 280 | /* Do actions once stream has been received. */ |
| 281 | if (ctx->on_recv_stream) { |
| 282 | ret = ctx->on_recv_stream(stream); |
| 283 | if (ret < 0) { |
| 284 | goto error; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | DBG("UST consumer add stream %s (key: %" PRIu64 ") with relayd id %" PRIu64, |
| 289 | stream->name, |
| 290 | stream->key, |
| 291 | stream->relayd_stream_id); |
| 292 | |
| 293 | /* Set next CPU stream. */ |
| 294 | channel->streams.count = ++cpu; |
| 295 | |
| 296 | /* Keep stream reference when creating metadata. */ |
| 297 | if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA) { |
| 298 | channel->metadata_stream = stream; |
| 299 | if (channel->monitor) { |
| 300 | /* Set metadata poll pipe if we created one */ |
| 301 | memcpy(stream->ust_metadata_poll_pipe, |
| 302 | ust_metadata_pipe, |
| 303 | sizeof(ust_metadata_pipe)); |
| 304 | } |
| 305 | } |
| 306 | pthread_mutex_unlock(&stream->lock); |
| 307 | current_stream_lock = nullptr; |
| 308 | } |
| 309 | |
| 310 | return 0; |
| 311 | |
| 312 | error: |
| 313 | error_alloc: |
| 314 | if (current_stream_lock) { |
| 315 | pthread_mutex_unlock(current_stream_lock); |
| 316 | } |
| 317 | return ret; |
| 318 | } |
| 319 | |
| 320 | static int open_ust_stream_fd(struct lttng_consumer_channel *channel, |
| 321 | int cpu, |
| 322 | const struct lttng_credentials *session_credentials) |
| 323 | { |
| 324 | char shm_path[PATH_MAX]; |
| 325 | int ret; |
| 326 | |
| 327 | if (!channel->shm_path[0]) { |
| 328 | return shm_create_anonymous("ust-consumer"); |
| 329 | } |
| 330 | ret = get_stream_shm_path(shm_path, channel->shm_path, cpu); |
| 331 | if (ret) { |
| 332 | goto error_shm_path; |
| 333 | } |
| 334 | return run_as_open(shm_path, |
| 335 | O_RDWR | O_CREAT | O_EXCL, |
| 336 | S_IRUSR | S_IWUSR, |
| 337 | lttng_credentials_get_uid(session_credentials), |
| 338 | lttng_credentials_get_gid(session_credentials)); |
| 339 | |
| 340 | error_shm_path: |
| 341 | return -1; |
| 342 | } |
| 343 | |
| 344 | /* |
| 345 | * Create an UST channel with the given attributes and send it to the session |
| 346 | * daemon using the ust ctl API. |
| 347 | * |
| 348 | * Return 0 on success or else a negative value. |
| 349 | */ |
| 350 | static int create_ust_channel(struct lttng_consumer_channel *channel, |
| 351 | struct lttng_ust_ctl_consumer_channel_attr *attr, |
| 352 | struct lttng_ust_ctl_consumer_channel **ust_chanp) |
| 353 | { |
| 354 | int ret, nr_stream_fds, i, j; |
| 355 | int *stream_fds; |
| 356 | struct lttng_ust_ctl_consumer_channel *ust_channel; |
| 357 | |
| 358 | LTTNG_ASSERT(channel); |
| 359 | LTTNG_ASSERT(attr); |
| 360 | LTTNG_ASSERT(ust_chanp); |
| 361 | LTTNG_ASSERT(channel->buffer_credentials.is_set); |
| 362 | |
| 363 | DBG3("Creating channel to ustctl with attr: [overwrite: %d, " |
| 364 | "subbuf_size: %" PRIu64 ", num_subbuf: %" PRIu64 ", " |
| 365 | "switch_timer_interval: %u, read_timer_interval: %u, " |
| 366 | "output: %d, type: %d", |
| 367 | attr->overwrite, |
| 368 | attr->subbuf_size, |
| 369 | attr->num_subbuf, |
| 370 | attr->switch_timer_interval, |
| 371 | attr->read_timer_interval, |
| 372 | attr->output, |
| 373 | attr->type); |
| 374 | |
| 375 | if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA) |
| 376 | nr_stream_fds = 1; |
| 377 | else |
| 378 | nr_stream_fds = lttng_ust_ctl_get_nr_stream_per_channel(); |
| 379 | stream_fds = calloc<int>(nr_stream_fds); |
| 380 | if (!stream_fds) { |
| 381 | ret = -1; |
| 382 | goto error_alloc; |
| 383 | } |
| 384 | for (i = 0; i < nr_stream_fds; i++) { |
| 385 | stream_fds[i] = open_ust_stream_fd(channel, i, &channel->buffer_credentials.value); |
| 386 | if (stream_fds[i] < 0) { |
| 387 | ret = -1; |
| 388 | goto error_open; |
| 389 | } |
| 390 | } |
| 391 | ust_channel = lttng_ust_ctl_create_channel(attr, stream_fds, nr_stream_fds); |
| 392 | if (!ust_channel) { |
| 393 | ret = -1; |
| 394 | goto error_create; |
| 395 | } |
| 396 | channel->nr_stream_fds = nr_stream_fds; |
| 397 | channel->stream_fds = stream_fds; |
| 398 | *ust_chanp = ust_channel; |
| 399 | |
| 400 | return 0; |
| 401 | |
| 402 | error_create: |
| 403 | error_open: |
| 404 | for (j = i - 1; j >= 0; j--) { |
| 405 | int closeret; |
| 406 | |
| 407 | closeret = close(stream_fds[j]); |
| 408 | if (closeret) { |
| 409 | PERROR("close"); |
| 410 | } |
| 411 | if (channel->shm_path[0]) { |
| 412 | char shm_path[PATH_MAX]; |
| 413 | |
| 414 | closeret = get_stream_shm_path(shm_path, channel->shm_path, j); |
| 415 | if (closeret) { |
| 416 | ERR("Cannot get stream shm path"); |
| 417 | } |
| 418 | closeret = run_as_unlink(shm_path, |
| 419 | lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR( |
| 420 | channel->buffer_credentials)), |
| 421 | lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR( |
| 422 | channel->buffer_credentials))); |
| 423 | if (closeret) { |
| 424 | PERROR("unlink %s", shm_path); |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | /* Try to rmdir all directories under shm_path root. */ |
| 429 | if (channel->root_shm_path[0]) { |
| 430 | (void) run_as_rmdir_recursive(channel->root_shm_path, |
| 431 | lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR( |
| 432 | channel->buffer_credentials)), |
| 433 | lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR( |
| 434 | channel->buffer_credentials)), |
| 435 | LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG); |
| 436 | } |
| 437 | free(stream_fds); |
| 438 | error_alloc: |
| 439 | return ret; |
| 440 | } |
| 441 | |
| 442 | /* |
| 443 | * Send a single given stream to the session daemon using the sock. |
| 444 | * |
| 445 | * Return 0 on success else a negative value. |
| 446 | */ |
| 447 | static int send_sessiond_stream(int sock, struct lttng_consumer_stream *stream) |
| 448 | { |
| 449 | int ret; |
| 450 | |
| 451 | LTTNG_ASSERT(stream); |
| 452 | LTTNG_ASSERT(sock >= 0); |
| 453 | |
| 454 | DBG("UST consumer sending stream %" PRIu64 " to sessiond", stream->key); |
| 455 | |
| 456 | /* Send stream to session daemon. */ |
| 457 | ret = lttng_ust_ctl_send_stream_to_sessiond(sock, stream->ustream); |
| 458 | if (ret < 0) { |
| 459 | goto error; |
| 460 | } |
| 461 | |
| 462 | error: |
| 463 | return ret; |
| 464 | } |
| 465 | |
| 466 | /* |
| 467 | * Send channel to sessiond and relayd if applicable. |
| 468 | * |
| 469 | * Return 0 on success or else a negative value. |
| 470 | */ |
| 471 | static int send_channel_to_sessiond_and_relayd(int sock, |
| 472 | struct lttng_consumer_channel *channel, |
| 473 | struct lttng_consumer_local_data *ctx, |
| 474 | int *relayd_error) |
| 475 | { |
| 476 | int ret, ret_code = LTTCOMM_CONSUMERD_SUCCESS; |
| 477 | struct lttng_consumer_stream *stream; |
| 478 | uint64_t net_seq_idx = -1ULL; |
| 479 | |
| 480 | LTTNG_ASSERT(channel); |
| 481 | LTTNG_ASSERT(ctx); |
| 482 | LTTNG_ASSERT(sock >= 0); |
| 483 | |
| 484 | DBG("UST consumer sending channel %s to sessiond", channel->name); |
| 485 | |
| 486 | if (channel->relayd_id != (uint64_t) -1ULL) { |
| 487 | cds_list_for_each_entry (stream, &channel->streams.head, send_node) { |
| 488 | health_code_update(); |
| 489 | |
| 490 | /* Try to send the stream to the relayd if one is available. */ |
| 491 | DBG("Sending stream %" PRIu64 " of channel \"%s\" to relayd", |
| 492 | stream->key, |
| 493 | channel->name); |
| 494 | ret = consumer_send_relayd_stream(stream, stream->chan->pathname); |
| 495 | if (ret < 0) { |
| 496 | /* |
| 497 | * Flag that the relayd was the problem here probably due to a |
| 498 | * communicaton error on the socket. |
| 499 | */ |
| 500 | if (relayd_error) { |
| 501 | *relayd_error = 1; |
| 502 | } |
| 503 | ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL; |
| 504 | } |
| 505 | if (net_seq_idx == -1ULL) { |
| 506 | net_seq_idx = stream->net_seq_idx; |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | /* Inform sessiond that we are about to send channel and streams. */ |
| 512 | ret = consumer_send_status_msg(sock, ret_code); |
| 513 | if (ret < 0 || ret_code != LTTCOMM_CONSUMERD_SUCCESS) { |
| 514 | /* |
| 515 | * Either the session daemon is not responding or the relayd died so we |
| 516 | * stop now. |
| 517 | */ |
| 518 | goto error; |
| 519 | } |
| 520 | |
| 521 | /* Send channel to sessiond. */ |
| 522 | ret = lttng_ust_ctl_send_channel_to_sessiond(sock, channel->uchan); |
| 523 | if (ret < 0) { |
| 524 | goto error; |
| 525 | } |
| 526 | |
| 527 | ret = lttng_ust_ctl_channel_close_wakeup_fd(channel->uchan); |
| 528 | if (ret < 0) { |
| 529 | goto error; |
| 530 | } |
| 531 | |
| 532 | /* The channel was sent successfully to the sessiond at this point. */ |
| 533 | cds_list_for_each_entry (stream, &channel->streams.head, send_node) { |
| 534 | health_code_update(); |
| 535 | |
| 536 | /* Send stream to session daemon. */ |
| 537 | ret = send_sessiond_stream(sock, stream); |
| 538 | if (ret < 0) { |
| 539 | goto error; |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | /* Tell sessiond there is no more stream. */ |
| 544 | ret = lttng_ust_ctl_send_stream_to_sessiond(sock, nullptr); |
| 545 | if (ret < 0) { |
| 546 | goto error; |
| 547 | } |
| 548 | |
| 549 | DBG("UST consumer NULL stream sent to sessiond"); |
| 550 | |
| 551 | return 0; |
| 552 | |
| 553 | error: |
| 554 | if (ret_code != LTTCOMM_CONSUMERD_SUCCESS) { |
| 555 | ret = -1; |
| 556 | } |
| 557 | return ret; |
| 558 | } |
| 559 | |
| 560 | /* |
| 561 | * Creates a channel and streams and add the channel it to the channel internal |
| 562 | * state. The created stream must ONLY be sent once the GET_CHANNEL command is |
| 563 | * received. |
| 564 | * |
| 565 | * Return 0 on success or else, a negative value is returned and the channel |
| 566 | * MUST be destroyed by consumer_del_channel(). |
| 567 | */ |
| 568 | static int ask_channel(struct lttng_consumer_local_data *ctx, |
| 569 | struct lttng_consumer_channel *channel, |
| 570 | struct lttng_ust_ctl_consumer_channel_attr *attr) |
| 571 | { |
| 572 | int ret; |
| 573 | |
| 574 | LTTNG_ASSERT(ctx); |
| 575 | LTTNG_ASSERT(channel); |
| 576 | LTTNG_ASSERT(attr); |
| 577 | |
| 578 | /* |
| 579 | * This value is still used by the kernel consumer since for the kernel, |
| 580 | * the stream ownership is not IN the consumer so we need to have the |
| 581 | * number of left stream that needs to be initialized so we can know when |
| 582 | * to delete the channel (see consumer.c). |
| 583 | * |
| 584 | * As for the user space tracer now, the consumer creates and sends the |
| 585 | * stream to the session daemon which only sends them to the application |
| 586 | * once every stream of a channel is received making this value useless |
| 587 | * because we they will be added to the poll thread before the application |
| 588 | * receives them. This ensures that a stream can not hang up during |
| 589 | * initilization of a channel. |
| 590 | */ |
| 591 | channel->nb_init_stream_left = 0; |
| 592 | |
| 593 | /* The reply msg status is handled in the following call. */ |
| 594 | ret = create_ust_channel(channel, attr, &channel->uchan); |
| 595 | if (ret < 0) { |
| 596 | goto end; |
| 597 | } |
| 598 | |
| 599 | channel->wait_fd = lttng_ust_ctl_channel_get_wait_fd(channel->uchan); |
| 600 | |
| 601 | /* |
| 602 | * For the snapshots (no monitor), we create the metadata streams |
| 603 | * on demand, not during the channel creation. |
| 604 | */ |
| 605 | if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA && !channel->monitor) { |
| 606 | ret = 0; |
| 607 | goto end; |
| 608 | } |
| 609 | |
| 610 | /* Open all streams for this channel. */ |
| 611 | pthread_mutex_lock(&channel->lock); |
| 612 | ret = create_ust_streams(channel, ctx); |
| 613 | pthread_mutex_unlock(&channel->lock); |
| 614 | if (ret < 0) { |
| 615 | goto end; |
| 616 | } |
| 617 | |
| 618 | end: |
| 619 | return ret; |
| 620 | } |
| 621 | |
| 622 | /* |
| 623 | * Send all stream of a channel to the right thread handling it. |
| 624 | * |
| 625 | * On error, return a negative value else 0 on success. |
| 626 | */ |
| 627 | static int send_streams_to_thread(struct lttng_consumer_channel *channel, |
| 628 | struct lttng_consumer_local_data *ctx) |
| 629 | { |
| 630 | int ret = 0; |
| 631 | struct lttng_consumer_stream *stream, *stmp; |
| 632 | |
| 633 | LTTNG_ASSERT(channel); |
| 634 | LTTNG_ASSERT(ctx); |
| 635 | |
| 636 | /* Send streams to the corresponding thread. */ |
| 637 | cds_list_for_each_entry_safe (stream, stmp, &channel->streams.head, send_node) { |
| 638 | health_code_update(); |
| 639 | |
| 640 | /* Sending the stream to the thread. */ |
| 641 | ret = send_stream_to_thread(stream, ctx); |
| 642 | if (ret < 0) { |
| 643 | /* |
| 644 | * If we are unable to send the stream to the thread, there is |
| 645 | * a big problem so just stop everything. |
| 646 | */ |
| 647 | goto error; |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | error: |
| 652 | return ret; |
| 653 | } |
| 654 | |
| 655 | /* |
| 656 | * Flush channel's streams using the given key to retrieve the channel. |
| 657 | * |
| 658 | * Return 0 on success else an LTTng error code. |
| 659 | */ |
| 660 | static int flush_channel(uint64_t chan_key) |
| 661 | { |
| 662 | int ret = 0; |
| 663 | struct lttng_consumer_channel *channel; |
| 664 | struct lttng_consumer_stream *stream; |
| 665 | struct lttng_ht *ht; |
| 666 | struct lttng_ht_iter iter; |
| 667 | |
| 668 | DBG("UST consumer flush channel key %" PRIu64, chan_key); |
| 669 | |
| 670 | lttng::urcu::read_lock_guard read_lock; |
| 671 | channel = consumer_find_channel(chan_key); |
| 672 | if (!channel) { |
| 673 | ERR("UST consumer flush channel %" PRIu64 " not found", chan_key); |
| 674 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
| 675 | goto error; |
| 676 | } |
| 677 | |
| 678 | ht = the_consumer_data.stream_per_chan_id_ht; |
| 679 | |
| 680 | /* For each stream of the channel id, flush it. */ |
| 681 | cds_lfht_for_each_entry_duplicate(ht->ht, |
| 682 | ht->hash_fct(&channel->key, lttng_ht_seed), |
| 683 | ht->match_fct, |
| 684 | &channel->key, |
| 685 | &iter.iter, |
| 686 | stream, |
| 687 | node_channel_id.node) |
| 688 | { |
| 689 | health_code_update(); |
| 690 | |
| 691 | pthread_mutex_lock(&stream->lock); |
| 692 | |
| 693 | /* |
| 694 | * Protect against concurrent teardown of a stream. |
| 695 | */ |
| 696 | if (cds_lfht_is_node_deleted(&stream->node.node)) { |
| 697 | goto next; |
| 698 | } |
| 699 | |
| 700 | if (!stream->quiescent) { |
| 701 | ret = lttng_ust_ctl_flush_buffer(stream->ustream, 0); |
| 702 | if (ret) { |
| 703 | ERR("Failed to flush buffer while flushing channel: channel key = %" PRIu64 |
| 704 | ", channel name = '%s'", |
| 705 | chan_key, |
| 706 | channel->name); |
| 707 | ret = LTTNG_ERR_BUFFER_FLUSH_FAILED; |
| 708 | pthread_mutex_unlock(&stream->lock); |
| 709 | goto error; |
| 710 | } |
| 711 | stream->quiescent = true; |
| 712 | } |
| 713 | next: |
| 714 | pthread_mutex_unlock(&stream->lock); |
| 715 | } |
| 716 | |
| 717 | /* |
| 718 | * Send one last buffer statistics update to the session daemon. This |
| 719 | * ensures that the session daemon gets at least one statistics update |
| 720 | * per channel even in the case of short-lived channels, such as when a |
| 721 | * short-lived app is traced in per-pid mode. |
| 722 | */ |
| 723 | sample_and_send_channel_buffer_stats(channel); |
| 724 | error: |
| 725 | return ret; |
| 726 | } |
| 727 | |
| 728 | /* |
| 729 | * Clear quiescent state from channel's streams using the given key to |
| 730 | * retrieve the channel. |
| 731 | * |
| 732 | * Return 0 on success else an LTTng error code. |
| 733 | */ |
| 734 | static int clear_quiescent_channel(uint64_t chan_key) |
| 735 | { |
| 736 | int ret = 0; |
| 737 | struct lttng_consumer_channel *channel; |
| 738 | struct lttng_consumer_stream *stream; |
| 739 | struct lttng_ht *ht; |
| 740 | struct lttng_ht_iter iter; |
| 741 | |
| 742 | DBG("UST consumer clear quiescent channel key %" PRIu64, chan_key); |
| 743 | |
| 744 | lttng::urcu::read_lock_guard read_lock; |
| 745 | channel = consumer_find_channel(chan_key); |
| 746 | if (!channel) { |
| 747 | ERR("UST consumer clear quiescent channel %" PRIu64 " not found", chan_key); |
| 748 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
| 749 | goto error; |
| 750 | } |
| 751 | |
| 752 | ht = the_consumer_data.stream_per_chan_id_ht; |
| 753 | |
| 754 | /* For each stream of the channel id, clear quiescent state. */ |
| 755 | cds_lfht_for_each_entry_duplicate(ht->ht, |
| 756 | ht->hash_fct(&channel->key, lttng_ht_seed), |
| 757 | ht->match_fct, |
| 758 | &channel->key, |
| 759 | &iter.iter, |
| 760 | stream, |
| 761 | node_channel_id.node) |
| 762 | { |
| 763 | health_code_update(); |
| 764 | |
| 765 | pthread_mutex_lock(&stream->lock); |
| 766 | stream->quiescent = false; |
| 767 | pthread_mutex_unlock(&stream->lock); |
| 768 | } |
| 769 | error: |
| 770 | return ret; |
| 771 | } |
| 772 | |
| 773 | /* |
| 774 | * Close metadata stream wakeup_fd using the given key to retrieve the channel. |
| 775 | * |
| 776 | * Return 0 on success else an LTTng error code. |
| 777 | */ |
| 778 | static int close_metadata(uint64_t chan_key) |
| 779 | { |
| 780 | int ret = 0; |
| 781 | struct lttng_consumer_channel *channel; |
| 782 | unsigned int channel_monitor; |
| 783 | |
| 784 | DBG("UST consumer close metadata key %" PRIu64, chan_key); |
| 785 | |
| 786 | channel = consumer_find_channel(chan_key); |
| 787 | if (!channel) { |
| 788 | /* |
| 789 | * This is possible if the metadata thread has issue a delete because |
| 790 | * the endpoint point of the stream hung up. There is no way the |
| 791 | * session daemon can know about it thus use a DBG instead of an actual |
| 792 | * error. |
| 793 | */ |
| 794 | DBG("UST consumer close metadata %" PRIu64 " not found", chan_key); |
| 795 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
| 796 | goto error; |
| 797 | } |
| 798 | |
| 799 | pthread_mutex_lock(&the_consumer_data.lock); |
| 800 | pthread_mutex_lock(&channel->lock); |
| 801 | channel_monitor = channel->monitor; |
| 802 | if (cds_lfht_is_node_deleted(&channel->node.node)) { |
| 803 | goto error_unlock; |
| 804 | } |
| 805 | |
| 806 | lttng_ustconsumer_close_metadata(channel); |
| 807 | pthread_mutex_unlock(&channel->lock); |
| 808 | pthread_mutex_unlock(&the_consumer_data.lock); |
| 809 | |
| 810 | /* |
| 811 | * The ownership of a metadata channel depends on the type of |
| 812 | * session to which it belongs. In effect, the monitor flag is checked |
| 813 | * to determine if this metadata channel is in "snapshot" mode or not. |
| 814 | * |
| 815 | * In the non-snapshot case, the metadata channel is created along with |
| 816 | * a single stream which will remain present until the metadata channel |
| 817 | * is destroyed (on the destruction of its session). In this case, the |
| 818 | * metadata stream in "monitored" by the metadata poll thread and holds |
| 819 | * the ownership of its channel. |
| 820 | * |
| 821 | * Closing the metadata will cause the metadata stream's "metadata poll |
| 822 | * pipe" to be closed. Closing this pipe will wake-up the metadata poll |
| 823 | * thread which will teardown the metadata stream which, in return, |
| 824 | * deletes the metadata channel. |
| 825 | * |
| 826 | * In the snapshot case, the metadata stream is created and destroyed |
| 827 | * on every snapshot record. Since the channel doesn't have an owner |
| 828 | * other than the session daemon, it is safe to destroy it immediately |
| 829 | * on reception of the CLOSE_METADATA command. |
| 830 | */ |
| 831 | if (!channel_monitor) { |
| 832 | /* |
| 833 | * The channel and consumer_data locks must be |
| 834 | * released before this call since consumer_del_channel |
| 835 | * re-acquires the channel and consumer_data locks to teardown |
| 836 | * the channel and queue its reclamation by the "call_rcu" |
| 837 | * worker thread. |
| 838 | */ |
| 839 | consumer_del_channel(channel); |
| 840 | } |
| 841 | |
| 842 | return ret; |
| 843 | error_unlock: |
| 844 | pthread_mutex_unlock(&channel->lock); |
| 845 | pthread_mutex_unlock(&the_consumer_data.lock); |
| 846 | error: |
| 847 | return ret; |
| 848 | } |
| 849 | |
| 850 | /* |
| 851 | * RCU read side lock MUST be acquired before calling this function. |
| 852 | * |
| 853 | * Return 0 on success else an LTTng error code. |
| 854 | */ |
| 855 | static int setup_metadata(struct lttng_consumer_local_data *ctx, uint64_t key) |
| 856 | { |
| 857 | int ret; |
| 858 | struct lttng_consumer_channel *metadata; |
| 859 | |
| 860 | ASSERT_RCU_READ_LOCKED(); |
| 861 | |
| 862 | DBG("UST consumer setup metadata key %" PRIu64, key); |
| 863 | |
| 864 | metadata = consumer_find_channel(key); |
| 865 | if (!metadata) { |
| 866 | ERR("UST consumer push metadata %" PRIu64 " not found", key); |
| 867 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
| 868 | goto end; |
| 869 | } |
| 870 | |
| 871 | /* |
| 872 | * In no monitor mode, the metadata channel has no stream(s) so skip the |
| 873 | * ownership transfer to the metadata thread. |
| 874 | */ |
| 875 | if (!metadata->monitor) { |
| 876 | DBG("Metadata channel in no monitor"); |
| 877 | ret = 0; |
| 878 | goto end; |
| 879 | } |
| 880 | |
| 881 | /* |
| 882 | * Send metadata stream to relayd if one available. Availability is |
| 883 | * known if the stream is still in the list of the channel. |
| 884 | */ |
| 885 | if (cds_list_empty(&metadata->streams.head)) { |
| 886 | ERR("Metadata channel key %" PRIu64 ", no stream available.", key); |
| 887 | ret = LTTCOMM_CONSUMERD_ERROR_METADATA; |
| 888 | goto error_no_stream; |
| 889 | } |
| 890 | |
| 891 | /* Send metadata stream to relayd if needed. */ |
| 892 | if (metadata->metadata_stream->net_seq_idx != (uint64_t) -1ULL) { |
| 893 | ret = consumer_send_relayd_stream(metadata->metadata_stream, metadata->pathname); |
| 894 | if (ret < 0) { |
| 895 | ret = LTTCOMM_CONSUMERD_ERROR_METADATA; |
| 896 | goto error; |
| 897 | } |
| 898 | ret = consumer_send_relayd_streams_sent(metadata->metadata_stream->net_seq_idx); |
| 899 | if (ret < 0) { |
| 900 | ret = LTTCOMM_CONSUMERD_RELAYD_FAIL; |
| 901 | goto error; |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | /* |
| 906 | * Ownership of metadata stream is passed along. Freeing is handled by |
| 907 | * the callee. |
| 908 | */ |
| 909 | ret = send_streams_to_thread(metadata, ctx); |
| 910 | if (ret < 0) { |
| 911 | /* |
| 912 | * If we are unable to send the stream to the thread, there is |
| 913 | * a big problem so just stop everything. |
| 914 | */ |
| 915 | ret = LTTCOMM_CONSUMERD_FATAL; |
| 916 | goto send_streams_error; |
| 917 | } |
| 918 | /* List MUST be empty after or else it could be reused. */ |
| 919 | LTTNG_ASSERT(cds_list_empty(&metadata->streams.head)); |
| 920 | |
| 921 | ret = 0; |
| 922 | goto end; |
| 923 | |
| 924 | error: |
| 925 | /* |
| 926 | * Delete metadata channel on error. At this point, the metadata stream can |
| 927 | * NOT be monitored by the metadata thread thus having the guarantee that |
| 928 | * the stream is still in the local stream list of the channel. This call |
| 929 | * will make sure to clean that list. |
| 930 | */ |
| 931 | consumer_stream_destroy(metadata->metadata_stream, nullptr); |
| 932 | metadata->metadata_stream = nullptr; |
| 933 | metadata->metadata_pushed_wait_queue.wake_all(); |
| 934 | |
| 935 | send_streams_error: |
| 936 | error_no_stream: |
| 937 | end: |
| 938 | return ret; |
| 939 | } |
| 940 | |
| 941 | /* |
| 942 | * Snapshot the whole metadata. |
| 943 | * RCU read-side lock must be held by the caller. |
| 944 | * |
| 945 | * Returns 0 on success, < 0 on error |
| 946 | */ |
| 947 | static int snapshot_metadata(struct lttng_consumer_channel *metadata_channel, |
| 948 | uint64_t key, |
| 949 | char *path, |
| 950 | uint64_t relayd_id, |
| 951 | struct lttng_consumer_local_data *ctx) |
| 952 | { |
| 953 | int ret = 0; |
| 954 | struct lttng_consumer_stream *metadata_stream; |
| 955 | |
| 956 | LTTNG_ASSERT(path); |
| 957 | LTTNG_ASSERT(ctx); |
| 958 | ASSERT_RCU_READ_LOCKED(); |
| 959 | |
| 960 | DBG("UST consumer snapshot metadata with key %" PRIu64 " at path %s", key, path); |
| 961 | |
| 962 | lttng::urcu::read_lock_guard read_lock; |
| 963 | |
| 964 | LTTNG_ASSERT(!metadata_channel->monitor); |
| 965 | |
| 966 | health_code_update(); |
| 967 | |
| 968 | /* |
| 969 | * Ask the sessiond if we have new metadata waiting and update the |
| 970 | * consumer metadata cache. |
| 971 | */ |
| 972 | ret = lttng_ustconsumer_request_metadata(ctx, metadata_channel, false, 1); |
| 973 | if (ret < 0) { |
| 974 | goto error; |
| 975 | } |
| 976 | |
| 977 | health_code_update(); |
| 978 | |
| 979 | /* |
| 980 | * The metadata stream is NOT created in no monitor mode when the channel |
| 981 | * is created on a sessiond ask channel command. |
| 982 | */ |
| 983 | ret = create_ust_streams(metadata_channel, ctx); |
| 984 | if (ret < 0) { |
| 985 | goto error; |
| 986 | } |
| 987 | |
| 988 | metadata_stream = metadata_channel->metadata_stream; |
| 989 | LTTNG_ASSERT(metadata_stream); |
| 990 | |
| 991 | metadata_stream->read_subbuffer_ops.lock(metadata_stream); |
| 992 | if (relayd_id != (uint64_t) -1ULL) { |
| 993 | metadata_stream->net_seq_idx = relayd_id; |
| 994 | ret = consumer_send_relayd_stream(metadata_stream, path); |
| 995 | } else { |
| 996 | ret = consumer_stream_create_output_files(metadata_stream, false); |
| 997 | } |
| 998 | if (ret < 0) { |
| 999 | goto error_stream; |
| 1000 | } |
| 1001 | |
| 1002 | do { |
| 1003 | health_code_update(); |
| 1004 | ret = lttng_consumer_read_subbuffer(metadata_stream, ctx, true); |
| 1005 | if (ret < 0) { |
| 1006 | goto error_stream; |
| 1007 | } |
| 1008 | } while (ret > 0); |
| 1009 | |
| 1010 | error_stream: |
| 1011 | metadata_stream->read_subbuffer_ops.unlock(metadata_stream); |
| 1012 | /* |
| 1013 | * Clean up the stream completely because the next snapshot will use a |
| 1014 | * new metadata stream. |
| 1015 | */ |
| 1016 | consumer_stream_destroy(metadata_stream, nullptr); |
| 1017 | metadata_channel->metadata_stream = nullptr; |
| 1018 | metadata_channel->metadata_pushed_wait_queue.wake_all(); |
| 1019 | |
| 1020 | error: |
| 1021 | return ret; |
| 1022 | } |
| 1023 | |
| 1024 | static int get_current_subbuf_addr(struct lttng_consumer_stream *stream, const char **addr) |
| 1025 | { |
| 1026 | int ret; |
| 1027 | unsigned long mmap_offset; |
| 1028 | const char *mmap_base; |
| 1029 | |
| 1030 | mmap_base = (const char *) lttng_ust_ctl_get_mmap_base(stream->ustream); |
| 1031 | if (!mmap_base) { |
| 1032 | ERR("Failed to get mmap base for stream `%s`", stream->name); |
| 1033 | ret = -EPERM; |
| 1034 | goto error; |
| 1035 | } |
| 1036 | |
| 1037 | ret = lttng_ust_ctl_get_mmap_read_offset(stream->ustream, &mmap_offset); |
| 1038 | if (ret != 0) { |
| 1039 | ERR("Failed to get mmap offset for stream `%s`", stream->name); |
| 1040 | ret = -EINVAL; |
| 1041 | goto error; |
| 1042 | } |
| 1043 | |
| 1044 | *addr = mmap_base + mmap_offset; |
| 1045 | error: |
| 1046 | return ret; |
| 1047 | } |
| 1048 | |
| 1049 | /* |
| 1050 | * Take a snapshot of all the stream of a channel. |
| 1051 | * RCU read-side lock and the channel lock must be held by the caller. |
| 1052 | * |
| 1053 | * Returns 0 on success, < 0 on error |
| 1054 | */ |
| 1055 | static int snapshot_channel(struct lttng_consumer_channel *channel, |
| 1056 | uint64_t key, |
| 1057 | char *path, |
| 1058 | uint64_t relayd_id, |
| 1059 | uint64_t nb_packets_per_stream, |
| 1060 | struct lttng_consumer_local_data *ctx) |
| 1061 | { |
| 1062 | int ret; |
| 1063 | unsigned use_relayd = 0; |
| 1064 | unsigned long consumed_pos, produced_pos; |
| 1065 | struct lttng_consumer_stream *stream; |
| 1066 | |
| 1067 | LTTNG_ASSERT(path); |
| 1068 | LTTNG_ASSERT(ctx); |
| 1069 | ASSERT_RCU_READ_LOCKED(); |
| 1070 | |
| 1071 | lttng::urcu::read_lock_guard read_lock; |
| 1072 | |
| 1073 | if (relayd_id != (uint64_t) -1ULL) { |
| 1074 | use_relayd = 1; |
| 1075 | } |
| 1076 | |
| 1077 | LTTNG_ASSERT(!channel->monitor); |
| 1078 | DBG("UST consumer snapshot channel %" PRIu64, key); |
| 1079 | |
| 1080 | cds_list_for_each_entry (stream, &channel->streams.head, send_node) { |
| 1081 | health_code_update(); |
| 1082 | |
| 1083 | /* Lock stream because we are about to change its state. */ |
| 1084 | pthread_mutex_lock(&stream->lock); |
| 1085 | LTTNG_ASSERT(channel->trace_chunk); |
| 1086 | if (!lttng_trace_chunk_get(channel->trace_chunk)) { |
| 1087 | /* |
| 1088 | * Can't happen barring an internal error as the channel |
| 1089 | * holds a reference to the trace chunk. |
| 1090 | */ |
| 1091 | ERR("Failed to acquire reference to channel's trace chunk"); |
| 1092 | ret = -1; |
| 1093 | goto error_unlock; |
| 1094 | } |
| 1095 | LTTNG_ASSERT(!stream->trace_chunk); |
| 1096 | stream->trace_chunk = channel->trace_chunk; |
| 1097 | |
| 1098 | stream->net_seq_idx = relayd_id; |
| 1099 | |
| 1100 | if (use_relayd) { |
| 1101 | ret = consumer_send_relayd_stream(stream, path); |
| 1102 | if (ret < 0) { |
| 1103 | goto error_close_stream; |
| 1104 | } |
| 1105 | } else { |
| 1106 | ret = consumer_stream_create_output_files(stream, false); |
| 1107 | if (ret < 0) { |
| 1108 | goto error_close_stream; |
| 1109 | } |
| 1110 | DBG("UST consumer snapshot stream (%" PRIu64 ")", stream->key); |
| 1111 | } |
| 1112 | |
| 1113 | /* |
| 1114 | * If tracing is active, we want to perform a "full" buffer flush. |
| 1115 | * Else, if quiescent, it has already been done by the prior stop. |
| 1116 | */ |
| 1117 | if (!stream->quiescent) { |
| 1118 | ret = lttng_ust_ctl_flush_buffer(stream->ustream, 0); |
| 1119 | if (ret < 0) { |
| 1120 | ERR("Failed to flush buffer during snapshot of channel: channel key = %" PRIu64 |
| 1121 | ", channel name = '%s'", |
| 1122 | channel->key, |
| 1123 | channel->name); |
| 1124 | goto error_unlock; |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | ret = lttng_ustconsumer_take_snapshot(stream); |
| 1129 | if (ret < 0) { |
| 1130 | ERR("Taking UST snapshot"); |
| 1131 | goto error_close_stream; |
| 1132 | } |
| 1133 | |
| 1134 | ret = lttng_ustconsumer_get_produced_snapshot(stream, &produced_pos); |
| 1135 | if (ret < 0) { |
| 1136 | ERR("Produced UST snapshot position"); |
| 1137 | goto error_close_stream; |
| 1138 | } |
| 1139 | |
| 1140 | ret = lttng_ustconsumer_get_consumed_snapshot(stream, &consumed_pos); |
| 1141 | if (ret < 0) { |
| 1142 | ERR("Consumerd UST snapshot position"); |
| 1143 | goto error_close_stream; |
| 1144 | } |
| 1145 | |
| 1146 | /* |
| 1147 | * The original value is sent back if max stream size is larger than |
| 1148 | * the possible size of the snapshot. Also, we assume that the session |
| 1149 | * daemon should never send a maximum stream size that is lower than |
| 1150 | * subbuffer size. |
| 1151 | */ |
| 1152 | consumed_pos = consumer_get_consume_start_pos( |
| 1153 | consumed_pos, produced_pos, nb_packets_per_stream, stream->max_sb_size); |
| 1154 | |
| 1155 | while ((long) (consumed_pos - produced_pos) < 0) { |
| 1156 | ssize_t read_len; |
| 1157 | unsigned long len, padded_len; |
| 1158 | const char *subbuf_addr; |
| 1159 | struct lttng_buffer_view subbuf_view; |
| 1160 | |
| 1161 | health_code_update(); |
| 1162 | |
| 1163 | DBG("UST consumer taking snapshot at pos %lu", consumed_pos); |
| 1164 | |
| 1165 | ret = lttng_ust_ctl_get_subbuf(stream->ustream, &consumed_pos); |
| 1166 | if (ret < 0) { |
| 1167 | if (ret != -EAGAIN) { |
| 1168 | PERROR("lttng_ust_ctl_get_subbuf snapshot"); |
| 1169 | goto error_close_stream; |
| 1170 | } |
| 1171 | DBG("UST consumer get subbuf failed. Skipping it."); |
| 1172 | consumed_pos += stream->max_sb_size; |
| 1173 | stream->chan->lost_packets++; |
| 1174 | continue; |
| 1175 | } |
| 1176 | |
| 1177 | ret = lttng_ust_ctl_get_subbuf_size(stream->ustream, &len); |
| 1178 | if (ret < 0) { |
| 1179 | ERR("Snapshot lttng_ust_ctl_get_subbuf_size"); |
| 1180 | goto error_put_subbuf; |
| 1181 | } |
| 1182 | |
| 1183 | ret = lttng_ust_ctl_get_padded_subbuf_size(stream->ustream, &padded_len); |
| 1184 | if (ret < 0) { |
| 1185 | ERR("Snapshot lttng_ust_ctl_get_padded_subbuf_size"); |
| 1186 | goto error_put_subbuf; |
| 1187 | } |
| 1188 | |
| 1189 | ret = get_current_subbuf_addr(stream, &subbuf_addr); |
| 1190 | if (ret) { |
| 1191 | goto error_put_subbuf; |
| 1192 | } |
| 1193 | |
| 1194 | subbuf_view = lttng_buffer_view_init(subbuf_addr, 0, padded_len); |
| 1195 | read_len = lttng_consumer_on_read_subbuffer_mmap( |
| 1196 | stream, &subbuf_view, padded_len - len); |
| 1197 | if (use_relayd) { |
| 1198 | if (read_len != len) { |
| 1199 | ret = -EPERM; |
| 1200 | goto error_put_subbuf; |
| 1201 | } |
| 1202 | } else { |
| 1203 | if (read_len != padded_len) { |
| 1204 | ret = -EPERM; |
| 1205 | goto error_put_subbuf; |
| 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | ret = lttng_ust_ctl_put_subbuf(stream->ustream); |
| 1210 | if (ret < 0) { |
| 1211 | ERR("Snapshot lttng_ust_ctl_put_subbuf"); |
| 1212 | goto error_close_stream; |
| 1213 | } |
| 1214 | consumed_pos += stream->max_sb_size; |
| 1215 | } |
| 1216 | |
| 1217 | /* Simply close the stream so we can use it on the next snapshot. */ |
| 1218 | consumer_stream_close_output(stream); |
| 1219 | pthread_mutex_unlock(&stream->lock); |
| 1220 | } |
| 1221 | |
| 1222 | return 0; |
| 1223 | |
| 1224 | error_put_subbuf: |
| 1225 | if (lttng_ust_ctl_put_subbuf(stream->ustream) < 0) { |
| 1226 | ERR("Snapshot lttng_ust_ctl_put_subbuf"); |
| 1227 | } |
| 1228 | error_close_stream: |
| 1229 | consumer_stream_close_output(stream); |
| 1230 | error_unlock: |
| 1231 | pthread_mutex_unlock(&stream->lock); |
| 1232 | return ret; |
| 1233 | } |
| 1234 | |
| 1235 | static void metadata_stream_reset_cache_consumed_position(struct lttng_consumer_stream *stream) |
| 1236 | { |
| 1237 | ASSERT_LOCKED(stream->lock); |
| 1238 | |
| 1239 | DBG("Reset metadata cache of session %" PRIu64, stream->chan->session_id); |
| 1240 | stream->ust_metadata_pushed = 0; |
| 1241 | } |
| 1242 | |
| 1243 | /* |
| 1244 | * Receive the metadata updates from the sessiond. Supports receiving |
| 1245 | * overlapping metadata, but is needs to always belong to a contiguous |
| 1246 | * range starting from 0. |
| 1247 | * Be careful about the locks held when calling this function: it needs |
| 1248 | * the metadata cache flush to concurrently progress in order to |
| 1249 | * complete. |
| 1250 | */ |
| 1251 | int lttng_ustconsumer_recv_metadata(int sock, |
| 1252 | uint64_t key, |
| 1253 | uint64_t offset, |
| 1254 | uint64_t len, |
| 1255 | uint64_t version, |
| 1256 | struct lttng_consumer_channel *channel, |
| 1257 | bool invoked_by_timer, |
| 1258 | int wait) |
| 1259 | { |
| 1260 | int ret, ret_code = LTTCOMM_CONSUMERD_SUCCESS; |
| 1261 | char *metadata_str; |
| 1262 | enum consumer_metadata_cache_write_status cache_write_status; |
| 1263 | |
| 1264 | DBG("UST consumer push metadata key %" PRIu64 " of len %" PRIu64, key, len); |
| 1265 | |
| 1266 | metadata_str = calloc<char>(len); |
| 1267 | if (!metadata_str) { |
| 1268 | PERROR("zmalloc metadata string"); |
| 1269 | ret_code = LTTCOMM_CONSUMERD_ENOMEM; |
| 1270 | goto end; |
| 1271 | } |
| 1272 | |
| 1273 | health_code_update(); |
| 1274 | |
| 1275 | /* Receive metadata string. */ |
| 1276 | ret = lttcomm_recv_unix_sock(sock, metadata_str, len); |
| 1277 | if (ret < 0) { |
| 1278 | /* Session daemon is dead so return gracefully. */ |
| 1279 | ret_code = ret; |
| 1280 | goto end_free; |
| 1281 | } |
| 1282 | |
| 1283 | health_code_update(); |
| 1284 | |
| 1285 | pthread_mutex_lock(&channel->metadata_cache->lock); |
| 1286 | cache_write_status = consumer_metadata_cache_write( |
| 1287 | channel->metadata_cache, offset, len, version, metadata_str); |
| 1288 | pthread_mutex_unlock(&channel->metadata_cache->lock); |
| 1289 | switch (cache_write_status) { |
| 1290 | case CONSUMER_METADATA_CACHE_WRITE_STATUS_NO_CHANGE: |
| 1291 | /* |
| 1292 | * The write entirely overlapped with existing contents of the |
| 1293 | * same metadata version (same content); there is nothing to do. |
| 1294 | */ |
| 1295 | break; |
| 1296 | case CONSUMER_METADATA_CACHE_WRITE_STATUS_INVALIDATED: |
| 1297 | /* |
| 1298 | * The metadata cache was invalidated (previously pushed |
| 1299 | * content has been overwritten). Reset the stream's consumed |
| 1300 | * metadata position to ensure the metadata poll thread consumes |
| 1301 | * the whole cache. |
| 1302 | */ |
| 1303 | |
| 1304 | /* |
| 1305 | * channel::metadata_stream can be null when the metadata |
| 1306 | * channel is under a snapshot session type. No need to update |
| 1307 | * the stream position in that scenario. |
| 1308 | */ |
| 1309 | if (channel->metadata_stream != nullptr) { |
| 1310 | pthread_mutex_lock(&channel->metadata_stream->lock); |
| 1311 | metadata_stream_reset_cache_consumed_position(channel->metadata_stream); |
| 1312 | pthread_mutex_unlock(&channel->metadata_stream->lock); |
| 1313 | } else { |
| 1314 | /* Validate we are in snapshot mode. */ |
| 1315 | LTTNG_ASSERT(!channel->monitor); |
| 1316 | } |
| 1317 | /* Fall-through. */ |
| 1318 | case CONSUMER_METADATA_CACHE_WRITE_STATUS_APPENDED_CONTENT: |
| 1319 | /* |
| 1320 | * In both cases, the metadata poll thread has new data to |
| 1321 | * consume. |
| 1322 | */ |
| 1323 | ret = consumer_metadata_wakeup_pipe(channel); |
| 1324 | if (ret) { |
| 1325 | ret_code = LTTCOMM_CONSUMERD_ERROR_METADATA; |
| 1326 | goto end_free; |
| 1327 | } |
| 1328 | break; |
| 1329 | case CONSUMER_METADATA_CACHE_WRITE_STATUS_ERROR: |
| 1330 | /* Unable to handle metadata. Notify session daemon. */ |
| 1331 | ret_code = LTTCOMM_CONSUMERD_ERROR_METADATA; |
| 1332 | /* |
| 1333 | * Skip metadata flush on write error since the offset and len might |
| 1334 | * not have been updated which could create an infinite loop below when |
| 1335 | * waiting for the metadata cache to be flushed. |
| 1336 | */ |
| 1337 | goto end_free; |
| 1338 | default: |
| 1339 | abort(); |
| 1340 | } |
| 1341 | |
| 1342 | if (!wait) { |
| 1343 | goto end_free; |
| 1344 | } |
| 1345 | |
| 1346 | consumer_wait_metadata_cache_flushed(channel, offset + len, invoked_by_timer); |
| 1347 | |
| 1348 | end_free: |
| 1349 | free(metadata_str); |
| 1350 | end: |
| 1351 | return ret_code; |
| 1352 | } |
| 1353 | |
| 1354 | /* |
| 1355 | * Receive command from session daemon and process it. |
| 1356 | * |
| 1357 | * Return 1 on success else a negative value or 0. |
| 1358 | */ |
| 1359 | int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data *ctx, |
| 1360 | int sock, |
| 1361 | struct pollfd *consumer_sockpoll) |
| 1362 | { |
| 1363 | int ret_func; |
| 1364 | enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS; |
| 1365 | struct lttcomm_consumer_msg msg; |
| 1366 | struct lttng_consumer_channel *channel = nullptr; |
| 1367 | |
| 1368 | health_code_update(); |
| 1369 | |
| 1370 | { |
| 1371 | ssize_t ret_recv; |
| 1372 | |
| 1373 | ret_recv = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg)); |
| 1374 | if (ret_recv != sizeof(msg)) { |
| 1375 | DBG("Consumer received unexpected message size %zd (expects %zu)", |
| 1376 | ret_recv, |
| 1377 | sizeof(msg)); |
| 1378 | /* |
| 1379 | * The ret value might 0 meaning an orderly shutdown but this is ok |
| 1380 | * since the caller handles this. |
| 1381 | */ |
| 1382 | if (ret_recv > 0) { |
| 1383 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD); |
| 1384 | ret_recv = -1; |
| 1385 | } |
| 1386 | return ret_recv; |
| 1387 | } |
| 1388 | } |
| 1389 | |
| 1390 | health_code_update(); |
| 1391 | |
| 1392 | /* deprecated */ |
| 1393 | LTTNG_ASSERT(msg.cmd_type != LTTNG_CONSUMER_STOP); |
| 1394 | |
| 1395 | health_code_update(); |
| 1396 | |
| 1397 | /* relayd needs RCU read-side lock */ |
| 1398 | lttng::urcu::read_lock_guard read_lock; |
| 1399 | |
| 1400 | switch (msg.cmd_type) { |
| 1401 | case LTTNG_CONSUMER_ADD_RELAYD_SOCKET: |
| 1402 | { |
| 1403 | uint32_t major = msg.u.relayd_sock.major; |
| 1404 | uint32_t minor = msg.u.relayd_sock.minor; |
| 1405 | enum lttcomm_sock_proto protocol = |
| 1406 | (enum lttcomm_sock_proto) msg.u.relayd_sock.relayd_socket_protocol; |
| 1407 | |
| 1408 | /* Session daemon status message are handled in the following call. */ |
| 1409 | consumer_add_relayd_socket(msg.u.relayd_sock.net_index, |
| 1410 | msg.u.relayd_sock.type, |
| 1411 | ctx, |
| 1412 | sock, |
| 1413 | consumer_sockpoll, |
| 1414 | msg.u.relayd_sock.session_id, |
| 1415 | msg.u.relayd_sock.relayd_session_id, |
| 1416 | major, |
| 1417 | minor, |
| 1418 | protocol); |
| 1419 | goto end_nosignal; |
| 1420 | } |
| 1421 | case LTTNG_CONSUMER_DESTROY_RELAYD: |
| 1422 | { |
| 1423 | uint64_t index = msg.u.destroy_relayd.net_seq_idx; |
| 1424 | struct consumer_relayd_sock_pair *relayd; |
| 1425 | |
| 1426 | DBG("UST consumer destroying relayd %" PRIu64, index); |
| 1427 | |
| 1428 | /* Get relayd reference if exists. */ |
| 1429 | relayd = consumer_find_relayd(index); |
| 1430 | if (relayd == nullptr) { |
| 1431 | DBG("Unable to find relayd %" PRIu64, index); |
| 1432 | ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL; |
| 1433 | } |
| 1434 | |
| 1435 | /* |
| 1436 | * Each relayd socket pair has a refcount of stream attached to it |
| 1437 | * which tells if the relayd is still active or not depending on the |
| 1438 | * refcount value. |
| 1439 | * |
| 1440 | * This will set the destroy flag of the relayd object and destroy it |
| 1441 | * if the refcount reaches zero when called. |
| 1442 | * |
| 1443 | * The destroy can happen either here or when a stream fd hangs up. |
| 1444 | */ |
| 1445 | if (relayd) { |
| 1446 | consumer_flag_relayd_for_destroy(relayd); |
| 1447 | } |
| 1448 | |
| 1449 | goto end_msg_sessiond; |
| 1450 | } |
| 1451 | case LTTNG_CONSUMER_UPDATE_STREAM: |
| 1452 | { |
| 1453 | return -ENOSYS; |
| 1454 | } |
| 1455 | case LTTNG_CONSUMER_DATA_PENDING: |
| 1456 | { |
| 1457 | int is_data_pending; |
| 1458 | ssize_t ret_send; |
| 1459 | uint64_t id = msg.u.data_pending.session_id; |
| 1460 | |
| 1461 | DBG("UST consumer data pending command for id %" PRIu64, id); |
| 1462 | |
| 1463 | is_data_pending = consumer_data_pending(id); |
| 1464 | |
| 1465 | /* Send back returned value to session daemon */ |
| 1466 | ret_send = lttcomm_send_unix_sock(sock, &is_data_pending, sizeof(is_data_pending)); |
| 1467 | if (ret_send < 0) { |
| 1468 | DBG("Error when sending the data pending ret code: %zd", ret_send); |
| 1469 | goto error_fatal; |
| 1470 | } |
| 1471 | |
| 1472 | /* |
| 1473 | * No need to send back a status message since the data pending |
| 1474 | * returned value is the response. |
| 1475 | */ |
| 1476 | break; |
| 1477 | } |
| 1478 | case LTTNG_CONSUMER_ASK_CHANNEL_CREATION: |
| 1479 | { |
| 1480 | int ret_ask_channel, ret_add_channel, ret_send; |
| 1481 | struct lttng_ust_ctl_consumer_channel_attr attr; |
| 1482 | const uint64_t chunk_id = msg.u.ask_channel.chunk_id.value; |
| 1483 | const struct lttng_credentials buffer_credentials = { |
| 1484 | .uid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.ask_channel.buffer_credentials.uid), |
| 1485 | .gid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.ask_channel.buffer_credentials.gid), |
| 1486 | }; |
| 1487 | |
| 1488 | /* Create a plain object and reserve a channel key. */ |
| 1489 | channel = consumer_allocate_channel( |
| 1490 | msg.u.ask_channel.key, |
| 1491 | msg.u.ask_channel.session_id, |
| 1492 | msg.u.ask_channel.chunk_id.is_set ? &chunk_id : nullptr, |
| 1493 | msg.u.ask_channel.pathname, |
| 1494 | msg.u.ask_channel.name, |
| 1495 | msg.u.ask_channel.relayd_id, |
| 1496 | (enum lttng_event_output) msg.u.ask_channel.output, |
| 1497 | msg.u.ask_channel.tracefile_size, |
| 1498 | msg.u.ask_channel.tracefile_count, |
| 1499 | msg.u.ask_channel.session_id_per_pid, |
| 1500 | msg.u.ask_channel.monitor, |
| 1501 | msg.u.ask_channel.live_timer_interval, |
| 1502 | msg.u.ask_channel.is_live, |
| 1503 | msg.u.ask_channel.root_shm_path, |
| 1504 | msg.u.ask_channel.shm_path); |
| 1505 | if (!channel) { |
| 1506 | goto end_channel_error; |
| 1507 | } |
| 1508 | |
| 1509 | LTTNG_OPTIONAL_SET(&channel->buffer_credentials, buffer_credentials); |
| 1510 | |
| 1511 | /* |
| 1512 | * Assign UST application UID to the channel. This value is ignored for |
| 1513 | * per PID buffers. This is specific to UST thus setting this after the |
| 1514 | * allocation. |
| 1515 | */ |
| 1516 | channel->ust_app_uid = msg.u.ask_channel.ust_app_uid; |
| 1517 | |
| 1518 | /* Build channel attributes from received message. */ |
| 1519 | attr.subbuf_size = msg.u.ask_channel.subbuf_size; |
| 1520 | attr.num_subbuf = msg.u.ask_channel.num_subbuf; |
| 1521 | attr.overwrite = msg.u.ask_channel.overwrite; |
| 1522 | attr.switch_timer_interval = msg.u.ask_channel.switch_timer_interval; |
| 1523 | attr.read_timer_interval = msg.u.ask_channel.read_timer_interval; |
| 1524 | attr.chan_id = msg.u.ask_channel.chan_id; |
| 1525 | memcpy(attr.uuid, msg.u.ask_channel.uuid, sizeof(attr.uuid)); |
| 1526 | attr.blocking_timeout = msg.u.ask_channel.blocking_timeout; |
| 1527 | |
| 1528 | /* Match channel buffer type to the UST abi. */ |
| 1529 | switch (msg.u.ask_channel.output) { |
| 1530 | case LTTNG_EVENT_MMAP: |
| 1531 | default: |
| 1532 | attr.output = LTTNG_UST_ABI_MMAP; |
| 1533 | break; |
| 1534 | } |
| 1535 | |
| 1536 | /* Translate and save channel type. */ |
| 1537 | switch (msg.u.ask_channel.type) { |
| 1538 | case LTTNG_UST_ABI_CHAN_PER_CPU: |
| 1539 | channel->type = CONSUMER_CHANNEL_TYPE_DATA; |
| 1540 | attr.type = LTTNG_UST_ABI_CHAN_PER_CPU; |
| 1541 | /* |
| 1542 | * Set refcount to 1 for owner. Below, we will |
| 1543 | * pass ownership to the |
| 1544 | * consumer_thread_channel_poll() thread. |
| 1545 | */ |
| 1546 | channel->refcount = 1; |
| 1547 | break; |
| 1548 | case LTTNG_UST_ABI_CHAN_METADATA: |
| 1549 | channel->type = CONSUMER_CHANNEL_TYPE_METADATA; |
| 1550 | attr.type = LTTNG_UST_ABI_CHAN_METADATA; |
| 1551 | break; |
| 1552 | default: |
| 1553 | abort(); |
| 1554 | goto error_fatal; |
| 1555 | }; |
| 1556 | |
| 1557 | health_code_update(); |
| 1558 | |
| 1559 | ret_ask_channel = ask_channel(ctx, channel, &attr); |
| 1560 | if (ret_ask_channel < 0) { |
| 1561 | goto end_channel_error; |
| 1562 | } |
| 1563 | |
| 1564 | if (msg.u.ask_channel.type == LTTNG_UST_ABI_CHAN_METADATA) { |
| 1565 | int ret_allocate; |
| 1566 | |
| 1567 | ret_allocate = consumer_metadata_cache_allocate(channel); |
| 1568 | if (ret_allocate < 0) { |
| 1569 | ERR("Allocating metadata cache"); |
| 1570 | goto end_channel_error; |
| 1571 | } |
| 1572 | consumer_timer_switch_start(channel, attr.switch_timer_interval); |
| 1573 | attr.switch_timer_interval = 0; |
| 1574 | } else { |
| 1575 | int monitor_start_ret; |
| 1576 | |
| 1577 | consumer_timer_live_start(channel, msg.u.ask_channel.live_timer_interval); |
| 1578 | monitor_start_ret = consumer_timer_monitor_start( |
| 1579 | channel, msg.u.ask_channel.monitor_timer_interval); |
| 1580 | if (monitor_start_ret < 0) { |
| 1581 | ERR("Starting channel monitoring timer failed"); |
| 1582 | goto end_channel_error; |
| 1583 | } |
| 1584 | } |
| 1585 | |
| 1586 | health_code_update(); |
| 1587 | |
| 1588 | /* |
| 1589 | * Add the channel to the internal state AFTER all streams were created |
| 1590 | * and successfully sent to session daemon. This way, all streams must |
| 1591 | * be ready before this channel is visible to the threads. |
| 1592 | * If add_channel succeeds, ownership of the channel is |
| 1593 | * passed to consumer_thread_channel_poll(). |
| 1594 | */ |
| 1595 | ret_add_channel = add_channel(channel, ctx); |
| 1596 | if (ret_add_channel < 0) { |
| 1597 | if (msg.u.ask_channel.type == LTTNG_UST_ABI_CHAN_METADATA) { |
| 1598 | if (channel->switch_timer_enabled == 1) { |
| 1599 | consumer_timer_switch_stop(channel); |
| 1600 | } |
| 1601 | consumer_metadata_cache_destroy(channel); |
| 1602 | } |
| 1603 | if (channel->live_timer_enabled == 1) { |
| 1604 | consumer_timer_live_stop(channel); |
| 1605 | } |
| 1606 | if (channel->monitor_timer_enabled == 1) { |
| 1607 | consumer_timer_monitor_stop(channel); |
| 1608 | } |
| 1609 | goto end_channel_error; |
| 1610 | } |
| 1611 | |
| 1612 | health_code_update(); |
| 1613 | |
| 1614 | /* |
| 1615 | * Channel and streams are now created. Inform the session daemon that |
| 1616 | * everything went well and should wait to receive the channel and |
| 1617 | * streams with ustctl API. |
| 1618 | */ |
| 1619 | ret_send = consumer_send_status_channel(sock, channel); |
| 1620 | if (ret_send < 0) { |
| 1621 | /* |
| 1622 | * There is probably a problem on the socket. |
| 1623 | */ |
| 1624 | goto error_fatal; |
| 1625 | } |
| 1626 | |
| 1627 | break; |
| 1628 | } |
| 1629 | case LTTNG_CONSUMER_GET_CHANNEL: |
| 1630 | { |
| 1631 | int ret, relayd_err = 0; |
| 1632 | uint64_t key = msg.u.get_channel.key; |
| 1633 | struct lttng_consumer_channel *found_channel; |
| 1634 | |
| 1635 | found_channel = consumer_find_channel(key); |
| 1636 | if (!found_channel) { |
| 1637 | ERR("UST consumer get channel key %" PRIu64 " not found", key); |
| 1638 | ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND; |
| 1639 | goto end_get_channel; |
| 1640 | } |
| 1641 | |
| 1642 | health_code_update(); |
| 1643 | |
| 1644 | /* Send the channel to sessiond (and relayd, if applicable). */ |
| 1645 | ret = send_channel_to_sessiond_and_relayd(sock, found_channel, ctx, &relayd_err); |
| 1646 | if (ret < 0) { |
| 1647 | if (relayd_err) { |
| 1648 | /* |
| 1649 | * We were unable to send to the relayd the stream so avoid |
| 1650 | * sending back a fatal error to the thread since this is OK |
| 1651 | * and the consumer can continue its work. The above call |
| 1652 | * has sent the error status message to the sessiond. |
| 1653 | */ |
| 1654 | goto end_get_channel_nosignal; |
| 1655 | } |
| 1656 | /* |
| 1657 | * The communicaton was broken hence there is a bad state between |
| 1658 | * the consumer and sessiond so stop everything. |
| 1659 | */ |
| 1660 | goto error_get_channel_fatal; |
| 1661 | } |
| 1662 | |
| 1663 | health_code_update(); |
| 1664 | |
| 1665 | /* |
| 1666 | * In no monitor mode, the streams ownership is kept inside the channel |
| 1667 | * so don't send them to the data thread. |
| 1668 | */ |
| 1669 | if (!found_channel->monitor) { |
| 1670 | goto end_get_channel; |
| 1671 | } |
| 1672 | |
| 1673 | ret = send_streams_to_thread(found_channel, ctx); |
| 1674 | if (ret < 0) { |
| 1675 | /* |
| 1676 | * If we are unable to send the stream to the thread, there is |
| 1677 | * a big problem so just stop everything. |
| 1678 | */ |
| 1679 | goto error_get_channel_fatal; |
| 1680 | } |
| 1681 | /* List MUST be empty after or else it could be reused. */ |
| 1682 | LTTNG_ASSERT(cds_list_empty(&found_channel->streams.head)); |
| 1683 | end_get_channel: |
| 1684 | goto end_msg_sessiond; |
| 1685 | error_get_channel_fatal: |
| 1686 | goto error_fatal; |
| 1687 | end_get_channel_nosignal: |
| 1688 | goto end_nosignal; |
| 1689 | } |
| 1690 | case LTTNG_CONSUMER_DESTROY_CHANNEL: |
| 1691 | { |
| 1692 | uint64_t key = msg.u.destroy_channel.key; |
| 1693 | |
| 1694 | /* |
| 1695 | * Only called if streams have not been sent to stream |
| 1696 | * manager thread. However, channel has been sent to |
| 1697 | * channel manager thread. |
| 1698 | */ |
| 1699 | notify_thread_del_channel(ctx, key); |
| 1700 | goto end_msg_sessiond; |
| 1701 | } |
| 1702 | case LTTNG_CONSUMER_CLOSE_METADATA: |
| 1703 | { |
| 1704 | int ret; |
| 1705 | |
| 1706 | ret = close_metadata(msg.u.close_metadata.key); |
| 1707 | if (ret != 0) { |
| 1708 | ret_code = (lttcomm_return_code) ret; |
| 1709 | } |
| 1710 | |
| 1711 | goto end_msg_sessiond; |
| 1712 | } |
| 1713 | case LTTNG_CONSUMER_FLUSH_CHANNEL: |
| 1714 | { |
| 1715 | int ret; |
| 1716 | |
| 1717 | ret = flush_channel(msg.u.flush_channel.key); |
| 1718 | if (ret != 0) { |
| 1719 | ret_code = (lttcomm_return_code) ret; |
| 1720 | } |
| 1721 | |
| 1722 | goto end_msg_sessiond; |
| 1723 | } |
| 1724 | case LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL: |
| 1725 | { |
| 1726 | int ret; |
| 1727 | |
| 1728 | ret = clear_quiescent_channel(msg.u.clear_quiescent_channel.key); |
| 1729 | if (ret != 0) { |
| 1730 | ret_code = (lttcomm_return_code) ret; |
| 1731 | } |
| 1732 | |
| 1733 | goto end_msg_sessiond; |
| 1734 | } |
| 1735 | case LTTNG_CONSUMER_PUSH_METADATA: |
| 1736 | { |
| 1737 | int ret; |
| 1738 | uint64_t len = msg.u.push_metadata.len; |
| 1739 | uint64_t key = msg.u.push_metadata.key; |
| 1740 | uint64_t offset = msg.u.push_metadata.target_offset; |
| 1741 | uint64_t version = msg.u.push_metadata.version; |
| 1742 | struct lttng_consumer_channel *found_channel; |
| 1743 | |
| 1744 | DBG("UST consumer push metadata key %" PRIu64 " of len %" PRIu64, key, len); |
| 1745 | |
| 1746 | found_channel = consumer_find_channel(key); |
| 1747 | if (!found_channel) { |
| 1748 | /* |
| 1749 | * This is possible if the metadata creation on the consumer side |
| 1750 | * is in flight vis-a-vis a concurrent push metadata from the |
| 1751 | * session daemon. Simply return that the channel failed and the |
| 1752 | * session daemon will handle that message correctly considering |
| 1753 | * that this race is acceptable thus the DBG() statement here. |
| 1754 | */ |
| 1755 | DBG("UST consumer push metadata %" PRIu64 " not found", key); |
| 1756 | ret_code = LTTCOMM_CONSUMERD_CHANNEL_FAIL; |
| 1757 | goto end_push_metadata_msg_sessiond; |
| 1758 | } |
| 1759 | |
| 1760 | health_code_update(); |
| 1761 | |
| 1762 | if (!len) { |
| 1763 | /* |
| 1764 | * There is nothing to receive. We have simply |
| 1765 | * checked whether the channel can be found. |
| 1766 | */ |
| 1767 | ret_code = LTTCOMM_CONSUMERD_SUCCESS; |
| 1768 | goto end_push_metadata_msg_sessiond; |
| 1769 | } |
| 1770 | |
| 1771 | /* Tell session daemon we are ready to receive the metadata. */ |
| 1772 | ret = consumer_send_status_msg(sock, LTTCOMM_CONSUMERD_SUCCESS); |
| 1773 | if (ret < 0) { |
| 1774 | /* Somehow, the session daemon is not responding anymore. */ |
| 1775 | goto error_push_metadata_fatal; |
| 1776 | } |
| 1777 | |
| 1778 | health_code_update(); |
| 1779 | |
| 1780 | /* Wait for more data. */ |
| 1781 | health_poll_entry(); |
| 1782 | ret = lttng_consumer_poll_socket(consumer_sockpoll); |
| 1783 | health_poll_exit(); |
| 1784 | if (ret) { |
| 1785 | goto error_push_metadata_fatal; |
| 1786 | } |
| 1787 | |
| 1788 | health_code_update(); |
| 1789 | |
| 1790 | ret = lttng_ustconsumer_recv_metadata( |
| 1791 | sock, key, offset, len, version, found_channel, false, 1); |
| 1792 | if (ret < 0) { |
| 1793 | /* error receiving from sessiond */ |
| 1794 | goto error_push_metadata_fatal; |
| 1795 | } else { |
| 1796 | ret_code = (lttcomm_return_code) ret; |
| 1797 | goto end_push_metadata_msg_sessiond; |
| 1798 | } |
| 1799 | end_push_metadata_msg_sessiond: |
| 1800 | goto end_msg_sessiond; |
| 1801 | error_push_metadata_fatal: |
| 1802 | goto error_fatal; |
| 1803 | } |
| 1804 | case LTTNG_CONSUMER_SETUP_METADATA: |
| 1805 | { |
| 1806 | int ret; |
| 1807 | |
| 1808 | ret = setup_metadata(ctx, msg.u.setup_metadata.key); |
| 1809 | if (ret) { |
| 1810 | ret_code = (lttcomm_return_code) ret; |
| 1811 | } |
| 1812 | goto end_msg_sessiond; |
| 1813 | } |
| 1814 | case LTTNG_CONSUMER_SNAPSHOT_CHANNEL: |
| 1815 | { |
| 1816 | struct lttng_consumer_channel *found_channel; |
| 1817 | uint64_t key = msg.u.snapshot_channel.key; |
| 1818 | int ret_send; |
| 1819 | |
| 1820 | found_channel = consumer_find_channel(key); |
| 1821 | if (!found_channel) { |
| 1822 | DBG("UST snapshot channel not found for key %" PRIu64, key); |
| 1823 | ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND; |
| 1824 | } else { |
| 1825 | if (msg.u.snapshot_channel.metadata) { |
| 1826 | int ret_snapshot; |
| 1827 | |
| 1828 | ret_snapshot = snapshot_metadata(found_channel, |
| 1829 | key, |
| 1830 | msg.u.snapshot_channel.pathname, |
| 1831 | msg.u.snapshot_channel.relayd_id, |
| 1832 | ctx); |
| 1833 | if (ret_snapshot < 0) { |
| 1834 | ERR("Snapshot metadata failed"); |
| 1835 | ret_code = LTTCOMM_CONSUMERD_SNAPSHOT_FAILED; |
| 1836 | } |
| 1837 | } else { |
| 1838 | int ret_snapshot; |
| 1839 | |
| 1840 | ret_snapshot = snapshot_channel( |
| 1841 | found_channel, |
| 1842 | key, |
| 1843 | msg.u.snapshot_channel.pathname, |
| 1844 | msg.u.snapshot_channel.relayd_id, |
| 1845 | msg.u.snapshot_channel.nb_packets_per_stream, |
| 1846 | ctx); |
| 1847 | if (ret_snapshot < 0) { |
| 1848 | ERR("Snapshot channel failed"); |
| 1849 | ret_code = LTTCOMM_CONSUMERD_SNAPSHOT_FAILED; |
| 1850 | } |
| 1851 | } |
| 1852 | } |
| 1853 | health_code_update(); |
| 1854 | ret_send = consumer_send_status_msg(sock, ret_code); |
| 1855 | if (ret_send < 0) { |
| 1856 | /* Somehow, the session daemon is not responding anymore. */ |
| 1857 | goto end_nosignal; |
| 1858 | } |
| 1859 | health_code_update(); |
| 1860 | break; |
| 1861 | } |
| 1862 | case LTTNG_CONSUMER_DISCARDED_EVENTS: |
| 1863 | { |
| 1864 | int ret = 0; |
| 1865 | uint64_t discarded_events; |
| 1866 | struct lttng_ht_iter iter; |
| 1867 | struct lttng_ht *ht; |
| 1868 | struct lttng_consumer_stream *stream; |
| 1869 | uint64_t id = msg.u.discarded_events.session_id; |
| 1870 | uint64_t key = msg.u.discarded_events.channel_key; |
| 1871 | |
| 1872 | DBG("UST consumer discarded events command for session id %" PRIu64, id); |
| 1873 | pthread_mutex_lock(&the_consumer_data.lock); |
| 1874 | |
| 1875 | ht = the_consumer_data.stream_list_ht; |
| 1876 | |
| 1877 | /* |
| 1878 | * We only need a reference to the channel, but they are not |
| 1879 | * directly indexed, so we just use the first matching stream |
| 1880 | * to extract the information we need, we default to 0 if not |
| 1881 | * found (no events are dropped if the channel is not yet in |
| 1882 | * use). |
| 1883 | */ |
| 1884 | discarded_events = 0; |
| 1885 | cds_lfht_for_each_entry_duplicate(ht->ht, |
| 1886 | ht->hash_fct(&id, lttng_ht_seed), |
| 1887 | ht->match_fct, |
| 1888 | &id, |
| 1889 | &iter.iter, |
| 1890 | stream, |
| 1891 | node_session_id.node) |
| 1892 | { |
| 1893 | if (stream->chan->key == key) { |
| 1894 | discarded_events = stream->chan->discarded_events; |
| 1895 | break; |
| 1896 | } |
| 1897 | } |
| 1898 | pthread_mutex_unlock(&the_consumer_data.lock); |
| 1899 | |
| 1900 | DBG("UST consumer discarded events command for session id %" PRIu64 |
| 1901 | ", channel key %" PRIu64, |
| 1902 | id, |
| 1903 | key); |
| 1904 | |
| 1905 | health_code_update(); |
| 1906 | |
| 1907 | /* Send back returned value to session daemon */ |
| 1908 | ret = lttcomm_send_unix_sock(sock, &discarded_events, sizeof(discarded_events)); |
| 1909 | if (ret < 0) { |
| 1910 | PERROR("send discarded events"); |
| 1911 | goto error_fatal; |
| 1912 | } |
| 1913 | |
| 1914 | break; |
| 1915 | } |
| 1916 | case LTTNG_CONSUMER_LOST_PACKETS: |
| 1917 | { |
| 1918 | int ret; |
| 1919 | uint64_t lost_packets; |
| 1920 | struct lttng_ht_iter iter; |
| 1921 | struct lttng_ht *ht; |
| 1922 | struct lttng_consumer_stream *stream; |
| 1923 | uint64_t id = msg.u.lost_packets.session_id; |
| 1924 | uint64_t key = msg.u.lost_packets.channel_key; |
| 1925 | |
| 1926 | DBG("UST consumer lost packets command for session id %" PRIu64, id); |
| 1927 | pthread_mutex_lock(&the_consumer_data.lock); |
| 1928 | |
| 1929 | ht = the_consumer_data.stream_list_ht; |
| 1930 | |
| 1931 | /* |
| 1932 | * We only need a reference to the channel, but they are not |
| 1933 | * directly indexed, so we just use the first matching stream |
| 1934 | * to extract the information we need, we default to 0 if not |
| 1935 | * found (no packets lost if the channel is not yet in use). |
| 1936 | */ |
| 1937 | lost_packets = 0; |
| 1938 | cds_lfht_for_each_entry_duplicate(ht->ht, |
| 1939 | ht->hash_fct(&id, lttng_ht_seed), |
| 1940 | ht->match_fct, |
| 1941 | &id, |
| 1942 | &iter.iter, |
| 1943 | stream, |
| 1944 | node_session_id.node) |
| 1945 | { |
| 1946 | if (stream->chan->key == key) { |
| 1947 | lost_packets = stream->chan->lost_packets; |
| 1948 | break; |
| 1949 | } |
| 1950 | } |
| 1951 | pthread_mutex_unlock(&the_consumer_data.lock); |
| 1952 | |
| 1953 | DBG("UST consumer lost packets command for session id %" PRIu64 |
| 1954 | ", channel key %" PRIu64, |
| 1955 | id, |
| 1956 | key); |
| 1957 | |
| 1958 | health_code_update(); |
| 1959 | |
| 1960 | /* Send back returned value to session daemon */ |
| 1961 | ret = lttcomm_send_unix_sock(sock, &lost_packets, sizeof(lost_packets)); |
| 1962 | if (ret < 0) { |
| 1963 | PERROR("send lost packets"); |
| 1964 | goto error_fatal; |
| 1965 | } |
| 1966 | |
| 1967 | break; |
| 1968 | } |
| 1969 | case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE: |
| 1970 | { |
| 1971 | int channel_monitor_pipe, ret_send, ret_set_channel_monitor_pipe; |
| 1972 | ssize_t ret_recv; |
| 1973 | |
| 1974 | ret_code = LTTCOMM_CONSUMERD_SUCCESS; |
| 1975 | /* Successfully received the command's type. */ |
| 1976 | ret_send = consumer_send_status_msg(sock, ret_code); |
| 1977 | if (ret_send < 0) { |
| 1978 | goto error_fatal; |
| 1979 | } |
| 1980 | |
| 1981 | ret_recv = lttcomm_recv_fds_unix_sock(sock, &channel_monitor_pipe, 1); |
| 1982 | if (ret_recv != sizeof(channel_monitor_pipe)) { |
| 1983 | ERR("Failed to receive channel monitor pipe"); |
| 1984 | goto error_fatal; |
| 1985 | } |
| 1986 | |
| 1987 | DBG("Received channel monitor pipe (%d)", channel_monitor_pipe); |
| 1988 | ret_set_channel_monitor_pipe = |
| 1989 | consumer_timer_thread_set_channel_monitor_pipe(channel_monitor_pipe); |
| 1990 | if (!ret_set_channel_monitor_pipe) { |
| 1991 | int flags; |
| 1992 | int ret_fcntl; |
| 1993 | |
| 1994 | ret_code = LTTCOMM_CONSUMERD_SUCCESS; |
| 1995 | /* Set the pipe as non-blocking. */ |
| 1996 | ret_fcntl = fcntl(channel_monitor_pipe, F_GETFL, 0); |
| 1997 | if (ret_fcntl == -1) { |
| 1998 | PERROR("fcntl get flags of the channel monitoring pipe"); |
| 1999 | goto error_fatal; |
| 2000 | } |
| 2001 | flags = ret_fcntl; |
| 2002 | |
| 2003 | ret_fcntl = fcntl(channel_monitor_pipe, F_SETFL, flags | O_NONBLOCK); |
| 2004 | if (ret_fcntl == -1) { |
| 2005 | PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe"); |
| 2006 | goto error_fatal; |
| 2007 | } |
| 2008 | DBG("Channel monitor pipe set as non-blocking"); |
| 2009 | } else { |
| 2010 | ret_code = LTTCOMM_CONSUMERD_ALREADY_SET; |
| 2011 | } |
| 2012 | goto end_msg_sessiond; |
| 2013 | } |
| 2014 | case LTTNG_CONSUMER_ROTATE_CHANNEL: |
| 2015 | { |
| 2016 | struct lttng_consumer_channel *found_channel; |
| 2017 | uint64_t key = msg.u.rotate_channel.key; |
| 2018 | int ret_send_status; |
| 2019 | |
| 2020 | found_channel = consumer_find_channel(key); |
| 2021 | if (!found_channel) { |
| 2022 | DBG("Channel %" PRIu64 " not found", key); |
| 2023 | ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND; |
| 2024 | } else { |
| 2025 | int rotate_channel; |
| 2026 | |
| 2027 | /* |
| 2028 | * Sample the rotate position of all the streams in |
| 2029 | * this channel. |
| 2030 | */ |
| 2031 | rotate_channel = lttng_consumer_rotate_channel( |
| 2032 | found_channel, key, msg.u.rotate_channel.relayd_id); |
| 2033 | if (rotate_channel < 0) { |
| 2034 | ERR("Rotate channel failed"); |
| 2035 | ret_code = LTTCOMM_CONSUMERD_ROTATION_FAIL; |
| 2036 | } |
| 2037 | |
| 2038 | health_code_update(); |
| 2039 | } |
| 2040 | |
| 2041 | ret_send_status = consumer_send_status_msg(sock, ret_code); |
| 2042 | if (ret_send_status < 0) { |
| 2043 | /* Somehow, the session daemon is not responding anymore. */ |
| 2044 | goto end_rotate_channel_nosignal; |
| 2045 | } |
| 2046 | |
| 2047 | /* |
| 2048 | * Rotate the streams that are ready right now. |
| 2049 | * FIXME: this is a second consecutive iteration over the |
| 2050 | * streams in a channel, there is probably a better way to |
| 2051 | * handle this, but it needs to be after the |
| 2052 | * consumer_send_status_msg() call. |
| 2053 | */ |
| 2054 | if (found_channel) { |
| 2055 | int ret_rotate_read_streams; |
| 2056 | |
| 2057 | ret_rotate_read_streams = |
| 2058 | lttng_consumer_rotate_ready_streams(found_channel, key); |
| 2059 | if (ret_rotate_read_streams < 0) { |
| 2060 | ERR("Rotate channel failed"); |
| 2061 | } |
| 2062 | } |
| 2063 | break; |
| 2064 | end_rotate_channel_nosignal: |
| 2065 | goto end_nosignal; |
| 2066 | } |
| 2067 | case LTTNG_CONSUMER_CLEAR_CHANNEL: |
| 2068 | { |
| 2069 | struct lttng_consumer_channel *found_channel; |
| 2070 | uint64_t key = msg.u.clear_channel.key; |
| 2071 | int ret_send_status; |
| 2072 | |
| 2073 | found_channel = consumer_find_channel(key); |
| 2074 | if (!found_channel) { |
| 2075 | DBG("Channel %" PRIu64 " not found", key); |
| 2076 | ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND; |
| 2077 | } else { |
| 2078 | int ret_clear_channel; |
| 2079 | |
| 2080 | ret_clear_channel = lttng_consumer_clear_channel(found_channel); |
| 2081 | if (ret_clear_channel) { |
| 2082 | ERR("Clear channel failed key %" PRIu64, key); |
| 2083 | ret_code = (lttcomm_return_code) ret_clear_channel; |
| 2084 | } |
| 2085 | |
| 2086 | health_code_update(); |
| 2087 | } |
| 2088 | ret_send_status = consumer_send_status_msg(sock, ret_code); |
| 2089 | if (ret_send_status < 0) { |
| 2090 | /* Somehow, the session daemon is not responding anymore. */ |
| 2091 | goto end_nosignal; |
| 2092 | } |
| 2093 | break; |
| 2094 | } |
| 2095 | case LTTNG_CONSUMER_INIT: |
| 2096 | { |
| 2097 | int ret_send_status; |
| 2098 | lttng_uuid sessiond_uuid; |
| 2099 | |
| 2100 | std::copy(std::begin(msg.u.init.sessiond_uuid), |
| 2101 | std::end(msg.u.init.sessiond_uuid), |
| 2102 | sessiond_uuid.begin()); |
| 2103 | ret_code = lttng_consumer_init_command(ctx, sessiond_uuid); |
| 2104 | health_code_update(); |
| 2105 | ret_send_status = consumer_send_status_msg(sock, ret_code); |
| 2106 | if (ret_send_status < 0) { |
| 2107 | /* Somehow, the session daemon is not responding anymore. */ |
| 2108 | goto end_nosignal; |
| 2109 | } |
| 2110 | break; |
| 2111 | } |
| 2112 | case LTTNG_CONSUMER_CREATE_TRACE_CHUNK: |
| 2113 | { |
| 2114 | const struct lttng_credentials credentials = { |
| 2115 | .uid = LTTNG_OPTIONAL_INIT_VALUE( |
| 2116 | msg.u.create_trace_chunk.credentials.value.uid), |
| 2117 | .gid = LTTNG_OPTIONAL_INIT_VALUE( |
| 2118 | msg.u.create_trace_chunk.credentials.value.gid), |
| 2119 | }; |
| 2120 | const bool is_local_trace = !msg.u.create_trace_chunk.relayd_id.is_set; |
| 2121 | const uint64_t relayd_id = msg.u.create_trace_chunk.relayd_id.value; |
| 2122 | const char *chunk_override_name = *msg.u.create_trace_chunk.override_name ? |
| 2123 | msg.u.create_trace_chunk.override_name : |
| 2124 | nullptr; |
| 2125 | struct lttng_directory_handle *chunk_directory_handle = nullptr; |
| 2126 | |
| 2127 | /* |
| 2128 | * The session daemon will only provide a chunk directory file |
| 2129 | * descriptor for local traces. |
| 2130 | */ |
| 2131 | if (is_local_trace) { |
| 2132 | int chunk_dirfd; |
| 2133 | int ret_send_status; |
| 2134 | ssize_t ret_recv; |
| 2135 | |
| 2136 | /* Acnowledge the reception of the command. */ |
| 2137 | ret_send_status = consumer_send_status_msg(sock, LTTCOMM_CONSUMERD_SUCCESS); |
| 2138 | if (ret_send_status < 0) { |
| 2139 | /* Somehow, the session daemon is not responding anymore. */ |
| 2140 | goto end_nosignal; |
| 2141 | } |
| 2142 | |
| 2143 | /* |
| 2144 | * Receive trace chunk domain dirfd. |
| 2145 | */ |
| 2146 | ret_recv = lttcomm_recv_fds_unix_sock(sock, &chunk_dirfd, 1); |
| 2147 | if (ret_recv != sizeof(chunk_dirfd)) { |
| 2148 | ERR("Failed to receive trace chunk domain directory file descriptor"); |
| 2149 | goto error_fatal; |
| 2150 | } |
| 2151 | |
| 2152 | DBG("Received trace chunk domain directory fd (%d)", chunk_dirfd); |
| 2153 | chunk_directory_handle = |
| 2154 | lttng_directory_handle_create_from_dirfd(chunk_dirfd); |
| 2155 | if (!chunk_directory_handle) { |
| 2156 | ERR("Failed to initialize chunk domain directory handle from directory file descriptor"); |
| 2157 | if (close(chunk_dirfd)) { |
| 2158 | PERROR("Failed to close chunk directory file descriptor"); |
| 2159 | } |
| 2160 | goto error_fatal; |
| 2161 | } |
| 2162 | } |
| 2163 | |
| 2164 | ret_code = lttng_consumer_create_trace_chunk( |
| 2165 | !is_local_trace ? &relayd_id : nullptr, |
| 2166 | msg.u.create_trace_chunk.session_id, |
| 2167 | msg.u.create_trace_chunk.chunk_id, |
| 2168 | (time_t) msg.u.create_trace_chunk.creation_timestamp, |
| 2169 | chunk_override_name, |
| 2170 | msg.u.create_trace_chunk.credentials.is_set ? &credentials : nullptr, |
| 2171 | chunk_directory_handle); |
| 2172 | lttng_directory_handle_put(chunk_directory_handle); |
| 2173 | goto end_msg_sessiond; |
| 2174 | } |
| 2175 | case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK: |
| 2176 | { |
| 2177 | enum lttng_trace_chunk_command_type close_command = |
| 2178 | (lttng_trace_chunk_command_type) msg.u.close_trace_chunk.close_command.value; |
| 2179 | const uint64_t relayd_id = msg.u.close_trace_chunk.relayd_id.value; |
| 2180 | struct lttcomm_consumer_close_trace_chunk_reply reply; |
| 2181 | char closed_trace_chunk_path[LTTNG_PATH_MAX] = {}; |
| 2182 | int ret; |
| 2183 | |
| 2184 | ret_code = lttng_consumer_close_trace_chunk( |
| 2185 | msg.u.close_trace_chunk.relayd_id.is_set ? &relayd_id : nullptr, |
| 2186 | msg.u.close_trace_chunk.session_id, |
| 2187 | msg.u.close_trace_chunk.chunk_id, |
| 2188 | (time_t) msg.u.close_trace_chunk.close_timestamp, |
| 2189 | msg.u.close_trace_chunk.close_command.is_set ? &close_command : nullptr, |
| 2190 | closed_trace_chunk_path); |
| 2191 | reply.ret_code = ret_code; |
| 2192 | reply.path_length = strlen(closed_trace_chunk_path) + 1; |
| 2193 | ret = lttcomm_send_unix_sock(sock, &reply, sizeof(reply)); |
| 2194 | if (ret != sizeof(reply)) { |
| 2195 | goto error_fatal; |
| 2196 | } |
| 2197 | ret = lttcomm_send_unix_sock(sock, closed_trace_chunk_path, reply.path_length); |
| 2198 | if (ret != reply.path_length) { |
| 2199 | goto error_fatal; |
| 2200 | } |
| 2201 | goto end_nosignal; |
| 2202 | } |
| 2203 | case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS: |
| 2204 | { |
| 2205 | const uint64_t relayd_id = msg.u.trace_chunk_exists.relayd_id.value; |
| 2206 | |
| 2207 | ret_code = lttng_consumer_trace_chunk_exists( |
| 2208 | msg.u.trace_chunk_exists.relayd_id.is_set ? &relayd_id : nullptr, |
| 2209 | msg.u.trace_chunk_exists.session_id, |
| 2210 | msg.u.trace_chunk_exists.chunk_id); |
| 2211 | goto end_msg_sessiond; |
| 2212 | } |
| 2213 | case LTTNG_CONSUMER_OPEN_CHANNEL_PACKETS: |
| 2214 | { |
| 2215 | const uint64_t key = msg.u.open_channel_packets.key; |
| 2216 | struct lttng_consumer_channel *found_channel = consumer_find_channel(key); |
| 2217 | |
| 2218 | if (found_channel) { |
| 2219 | pthread_mutex_lock(&found_channel->lock); |
| 2220 | ret_code = lttng_consumer_open_channel_packets(found_channel); |
| 2221 | pthread_mutex_unlock(&found_channel->lock); |
| 2222 | } else { |
| 2223 | /* |
| 2224 | * The channel could have disappeared in per-pid |
| 2225 | * buffering mode. |
| 2226 | */ |
| 2227 | DBG("Channel %" PRIu64 " not found", key); |
| 2228 | ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND; |
| 2229 | } |
| 2230 | |
| 2231 | health_code_update(); |
| 2232 | goto end_msg_sessiond; |
| 2233 | } |
| 2234 | default: |
| 2235 | break; |
| 2236 | } |
| 2237 | |
| 2238 | end_nosignal: |
| 2239 | /* |
| 2240 | * Return 1 to indicate success since the 0 value can be a socket |
| 2241 | * shutdown during the recv() or send() call. |
| 2242 | */ |
| 2243 | ret_func = 1; |
| 2244 | goto end; |
| 2245 | |
| 2246 | end_msg_sessiond: |
| 2247 | /* |
| 2248 | * The returned value here is not useful since either way we'll return 1 to |
| 2249 | * the caller because the session daemon socket management is done |
| 2250 | * elsewhere. Returning a negative code or 0 will shutdown the consumer. |
| 2251 | */ |
| 2252 | { |
| 2253 | int ret_send_status; |
| 2254 | |
| 2255 | ret_send_status = consumer_send_status_msg(sock, ret_code); |
| 2256 | if (ret_send_status < 0) { |
| 2257 | goto error_fatal; |
| 2258 | } |
| 2259 | } |
| 2260 | |
| 2261 | ret_func = 1; |
| 2262 | goto end; |
| 2263 | |
| 2264 | end_channel_error: |
| 2265 | if (channel) { |
| 2266 | consumer_del_channel(channel); |
| 2267 | } |
| 2268 | /* We have to send a status channel message indicating an error. */ |
| 2269 | { |
| 2270 | int ret_send_status; |
| 2271 | |
| 2272 | ret_send_status = consumer_send_status_channel(sock, nullptr); |
| 2273 | if (ret_send_status < 0) { |
| 2274 | /* Stop everything if session daemon can not be notified. */ |
| 2275 | goto error_fatal; |
| 2276 | } |
| 2277 | } |
| 2278 | |
| 2279 | ret_func = 1; |
| 2280 | goto end; |
| 2281 | |
| 2282 | error_fatal: |
| 2283 | /* This will issue a consumer stop. */ |
| 2284 | ret_func = -1; |
| 2285 | goto end; |
| 2286 | |
| 2287 | end: |
| 2288 | health_code_update(); |
| 2289 | return ret_func; |
| 2290 | } |
| 2291 | |
| 2292 | int lttng_ust_flush_buffer(struct lttng_consumer_stream *stream, int producer_active) |
| 2293 | { |
| 2294 | LTTNG_ASSERT(stream); |
| 2295 | LTTNG_ASSERT(stream->ustream); |
| 2296 | |
| 2297 | return lttng_ust_ctl_flush_buffer(stream->ustream, producer_active); |
| 2298 | } |
| 2299 | |
| 2300 | /* |
| 2301 | * Take a snapshot for a specific stream. |
| 2302 | * |
| 2303 | * Returns 0 on success, < 0 on error |
| 2304 | */ |
| 2305 | int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream *stream) |
| 2306 | { |
| 2307 | LTTNG_ASSERT(stream); |
| 2308 | LTTNG_ASSERT(stream->ustream); |
| 2309 | |
| 2310 | return lttng_ust_ctl_snapshot(stream->ustream); |
| 2311 | } |
| 2312 | |
| 2313 | /* |
| 2314 | * Sample consumed and produced positions for a specific stream. |
| 2315 | * |
| 2316 | * Returns 0 on success, < 0 on error. |
| 2317 | */ |
| 2318 | int lttng_ustconsumer_sample_snapshot_positions(struct lttng_consumer_stream *stream) |
| 2319 | { |
| 2320 | LTTNG_ASSERT(stream); |
| 2321 | LTTNG_ASSERT(stream->ustream); |
| 2322 | |
| 2323 | return lttng_ust_ctl_snapshot_sample_positions(stream->ustream); |
| 2324 | } |
| 2325 | |
| 2326 | /* |
| 2327 | * Get the produced position |
| 2328 | * |
| 2329 | * Returns 0 on success, < 0 on error |
| 2330 | */ |
| 2331 | int lttng_ustconsumer_get_produced_snapshot(struct lttng_consumer_stream *stream, |
| 2332 | unsigned long *pos) |
| 2333 | { |
| 2334 | LTTNG_ASSERT(stream); |
| 2335 | LTTNG_ASSERT(stream->ustream); |
| 2336 | LTTNG_ASSERT(pos); |
| 2337 | |
| 2338 | return lttng_ust_ctl_snapshot_get_produced(stream->ustream, pos); |
| 2339 | } |
| 2340 | |
| 2341 | /* |
| 2342 | * Get the consumed position |
| 2343 | * |
| 2344 | * Returns 0 on success, < 0 on error |
| 2345 | */ |
| 2346 | int lttng_ustconsumer_get_consumed_snapshot(struct lttng_consumer_stream *stream, |
| 2347 | unsigned long *pos) |
| 2348 | { |
| 2349 | LTTNG_ASSERT(stream); |
| 2350 | LTTNG_ASSERT(stream->ustream); |
| 2351 | LTTNG_ASSERT(pos); |
| 2352 | |
| 2353 | return lttng_ust_ctl_snapshot_get_consumed(stream->ustream, pos); |
| 2354 | } |
| 2355 | |
| 2356 | int lttng_ustconsumer_flush_buffer(struct lttng_consumer_stream *stream, int producer) |
| 2357 | { |
| 2358 | LTTNG_ASSERT(stream); |
| 2359 | LTTNG_ASSERT(stream->ustream); |
| 2360 | |
| 2361 | return lttng_ust_ctl_flush_buffer(stream->ustream, producer); |
| 2362 | } |
| 2363 | |
| 2364 | int lttng_ustconsumer_clear_buffer(struct lttng_consumer_stream *stream) |
| 2365 | { |
| 2366 | LTTNG_ASSERT(stream); |
| 2367 | LTTNG_ASSERT(stream->ustream); |
| 2368 | |
| 2369 | return lttng_ust_ctl_clear_buffer(stream->ustream); |
| 2370 | } |
| 2371 | |
| 2372 | int lttng_ustconsumer_get_current_timestamp(struct lttng_consumer_stream *stream, uint64_t *ts) |
| 2373 | { |
| 2374 | LTTNG_ASSERT(stream); |
| 2375 | LTTNG_ASSERT(stream->ustream); |
| 2376 | LTTNG_ASSERT(ts); |
| 2377 | |
| 2378 | return lttng_ust_ctl_get_current_timestamp(stream->ustream, ts); |
| 2379 | } |
| 2380 | |
| 2381 | int lttng_ustconsumer_get_sequence_number(struct lttng_consumer_stream *stream, uint64_t *seq) |
| 2382 | { |
| 2383 | LTTNG_ASSERT(stream); |
| 2384 | LTTNG_ASSERT(stream->ustream); |
| 2385 | LTTNG_ASSERT(seq); |
| 2386 | |
| 2387 | return lttng_ust_ctl_get_sequence_number(stream->ustream, seq); |
| 2388 | } |
| 2389 | |
| 2390 | /* |
| 2391 | * Called when the stream signals the consumer that it has hung up. |
| 2392 | */ |
| 2393 | void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream *stream) |
| 2394 | { |
| 2395 | LTTNG_ASSERT(stream); |
| 2396 | LTTNG_ASSERT(stream->ustream); |
| 2397 | |
| 2398 | pthread_mutex_lock(&stream->lock); |
| 2399 | if (!stream->quiescent) { |
| 2400 | if (lttng_ust_ctl_flush_buffer(stream->ustream, 0) < 0) { |
| 2401 | ERR("Failed to flush buffer on stream hang-up"); |
| 2402 | } else { |
| 2403 | stream->quiescent = true; |
| 2404 | } |
| 2405 | } |
| 2406 | |
| 2407 | stream->hangup_flush_done = 1; |
| 2408 | pthread_mutex_unlock(&stream->lock); |
| 2409 | } |
| 2410 | |
| 2411 | void lttng_ustconsumer_del_channel(struct lttng_consumer_channel *chan) |
| 2412 | { |
| 2413 | int i; |
| 2414 | |
| 2415 | LTTNG_ASSERT(chan); |
| 2416 | LTTNG_ASSERT(chan->uchan); |
| 2417 | LTTNG_ASSERT(chan->buffer_credentials.is_set); |
| 2418 | |
| 2419 | if (chan->switch_timer_enabled == 1) { |
| 2420 | consumer_timer_switch_stop(chan); |
| 2421 | } |
| 2422 | for (i = 0; i < chan->nr_stream_fds; i++) { |
| 2423 | int ret; |
| 2424 | |
| 2425 | ret = close(chan->stream_fds[i]); |
| 2426 | if (ret) { |
| 2427 | PERROR("close"); |
| 2428 | } |
| 2429 | if (chan->shm_path[0]) { |
| 2430 | char shm_path[PATH_MAX]; |
| 2431 | |
| 2432 | ret = get_stream_shm_path(shm_path, chan->shm_path, i); |
| 2433 | if (ret) { |
| 2434 | ERR("Cannot get stream shm path"); |
| 2435 | } |
| 2436 | ret = run_as_unlink(shm_path, |
| 2437 | lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR( |
| 2438 | chan->buffer_credentials)), |
| 2439 | lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR( |
| 2440 | chan->buffer_credentials))); |
| 2441 | if (ret) { |
| 2442 | PERROR("unlink %s", shm_path); |
| 2443 | } |
| 2444 | } |
| 2445 | } |
| 2446 | } |
| 2447 | |
| 2448 | void lttng_ustconsumer_free_channel(struct lttng_consumer_channel *chan) |
| 2449 | { |
| 2450 | LTTNG_ASSERT(chan); |
| 2451 | LTTNG_ASSERT(chan->uchan); |
| 2452 | LTTNG_ASSERT(chan->buffer_credentials.is_set); |
| 2453 | |
| 2454 | consumer_metadata_cache_destroy(chan); |
| 2455 | lttng_ust_ctl_destroy_channel(chan->uchan); |
| 2456 | /* Try to rmdir all directories under shm_path root. */ |
| 2457 | if (chan->root_shm_path[0]) { |
| 2458 | (void) run_as_rmdir_recursive( |
| 2459 | chan->root_shm_path, |
| 2460 | lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(chan->buffer_credentials)), |
| 2461 | lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(chan->buffer_credentials)), |
| 2462 | LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG); |
| 2463 | } |
| 2464 | free(chan->stream_fds); |
| 2465 | } |
| 2466 | |
| 2467 | void lttng_ustconsumer_del_stream(struct lttng_consumer_stream *stream) |
| 2468 | { |
| 2469 | LTTNG_ASSERT(stream); |
| 2470 | LTTNG_ASSERT(stream->ustream); |
| 2471 | |
| 2472 | if (stream->chan->switch_timer_enabled == 1) { |
| 2473 | consumer_timer_switch_stop(stream->chan); |
| 2474 | } |
| 2475 | lttng_ust_ctl_destroy_stream(stream->ustream); |
| 2476 | } |
| 2477 | |
| 2478 | int lttng_ustconsumer_get_wakeup_fd(struct lttng_consumer_stream *stream) |
| 2479 | { |
| 2480 | LTTNG_ASSERT(stream); |
| 2481 | LTTNG_ASSERT(stream->ustream); |
| 2482 | |
| 2483 | return lttng_ust_ctl_stream_get_wakeup_fd(stream->ustream); |
| 2484 | } |
| 2485 | |
| 2486 | int lttng_ustconsumer_close_wakeup_fd(struct lttng_consumer_stream *stream) |
| 2487 | { |
| 2488 | LTTNG_ASSERT(stream); |
| 2489 | LTTNG_ASSERT(stream->ustream); |
| 2490 | |
| 2491 | return lttng_ust_ctl_stream_close_wakeup_fd(stream->ustream); |
| 2492 | } |
| 2493 | |
| 2494 | /* |
| 2495 | * Write up to one packet from the metadata cache to the channel. |
| 2496 | * |
| 2497 | * Returns the number of bytes pushed from the cache into the ring buffer, or a |
| 2498 | * negative value on error. |
| 2499 | */ |
| 2500 | static int commit_one_metadata_packet(struct lttng_consumer_stream *stream) |
| 2501 | { |
| 2502 | ssize_t write_len; |
| 2503 | int ret; |
| 2504 | |
| 2505 | pthread_mutex_lock(&stream->chan->metadata_cache->lock); |
| 2506 | if (stream->chan->metadata_cache->contents.size == stream->ust_metadata_pushed) { |
| 2507 | /* |
| 2508 | * In the context of a user space metadata channel, a |
| 2509 | * change in version can be detected in two ways: |
| 2510 | * 1) During the pre-consume of the `read_subbuffer` loop, |
| 2511 | * 2) When populating the metadata ring buffer (i.e. here). |
| 2512 | * |
| 2513 | * This function is invoked when there is no metadata |
| 2514 | * available in the ring-buffer. If all data was consumed |
| 2515 | * up to the size of the metadata cache, there is no metadata |
| 2516 | * to insert in the ring-buffer. |
| 2517 | * |
| 2518 | * However, the metadata version could still have changed (a |
| 2519 | * regeneration without any new data will yield the same cache |
| 2520 | * size). |
| 2521 | * |
| 2522 | * The cache's version is checked for a version change and the |
| 2523 | * consumed position is reset if one occurred. |
| 2524 | * |
| 2525 | * This check is only necessary for the user space domain as |
| 2526 | * it has to manage the cache explicitly. If this reset was not |
| 2527 | * performed, no metadata would be consumed (and no reset would |
| 2528 | * occur as part of the pre-consume) until the metadata size |
| 2529 | * exceeded the cache size. |
| 2530 | */ |
| 2531 | if (stream->metadata_version != stream->chan->metadata_cache->version) { |
| 2532 | metadata_stream_reset_cache_consumed_position(stream); |
| 2533 | consumer_stream_metadata_set_version(stream, |
| 2534 | stream->chan->metadata_cache->version); |
| 2535 | } else { |
| 2536 | ret = 0; |
| 2537 | goto end; |
| 2538 | } |
| 2539 | } |
| 2540 | |
| 2541 | write_len = lttng_ust_ctl_write_one_packet_to_channel( |
| 2542 | stream->chan->uchan, |
| 2543 | &stream->chan->metadata_cache->contents.data[stream->ust_metadata_pushed], |
| 2544 | stream->chan->metadata_cache->contents.size - stream->ust_metadata_pushed); |
| 2545 | LTTNG_ASSERT(write_len != 0); |
| 2546 | if (write_len < 0) { |
| 2547 | ERR("Writing one metadata packet"); |
| 2548 | ret = write_len; |
| 2549 | goto end; |
| 2550 | } |
| 2551 | |
| 2552 | stream->ust_metadata_pushed += write_len; |
| 2553 | stream->chan->metadata_pushed_wait_queue.wake_all(); |
| 2554 | |
| 2555 | LTTNG_ASSERT(stream->chan->metadata_cache->contents.size >= stream->ust_metadata_pushed); |
| 2556 | ret = write_len; |
| 2557 | |
| 2558 | /* |
| 2559 | * Switch packet (but don't open the next one) on every commit of |
| 2560 | * a metadata packet. Since the subbuffer is fully filled (with padding, |
| 2561 | * if needed), the stream is "quiescent" after this commit. |
| 2562 | */ |
| 2563 | if (lttng_ust_ctl_flush_buffer(stream->ustream, 1)) { |
| 2564 | ERR("Failed to flush buffer while committing one metadata packet"); |
| 2565 | ret = -EIO; |
| 2566 | } else { |
| 2567 | stream->quiescent = true; |
| 2568 | } |
| 2569 | end: |
| 2570 | pthread_mutex_unlock(&stream->chan->metadata_cache->lock); |
| 2571 | return ret; |
| 2572 | } |
| 2573 | |
| 2574 | /* |
| 2575 | * Sync metadata meaning request them to the session daemon and snapshot to the |
| 2576 | * metadata thread can consumer them. |
| 2577 | * |
| 2578 | * Metadata stream lock is held here, but we need to release it when |
| 2579 | * interacting with sessiond, else we cause a deadlock with live |
| 2580 | * awaiting on metadata to be pushed out. |
| 2581 | * |
| 2582 | * The RCU read side lock must be held by the caller. |
| 2583 | */ |
| 2584 | enum sync_metadata_status |
| 2585 | lttng_ustconsumer_sync_metadata(struct lttng_consumer_local_data *ctx, |
| 2586 | struct lttng_consumer_stream *metadata_stream) |
| 2587 | { |
| 2588 | int ret; |
| 2589 | enum sync_metadata_status status; |
| 2590 | struct lttng_consumer_channel *metadata_channel; |
| 2591 | |
| 2592 | LTTNG_ASSERT(ctx); |
| 2593 | LTTNG_ASSERT(metadata_stream); |
| 2594 | ASSERT_RCU_READ_LOCKED(); |
| 2595 | |
| 2596 | metadata_channel = metadata_stream->chan; |
| 2597 | pthread_mutex_unlock(&metadata_stream->lock); |
| 2598 | /* |
| 2599 | * Request metadata from the sessiond, but don't wait for the flush |
| 2600 | * because we locked the metadata thread. |
| 2601 | */ |
| 2602 | ret = lttng_ustconsumer_request_metadata(ctx, metadata_channel, false, 0); |
| 2603 | pthread_mutex_lock(&metadata_stream->lock); |
| 2604 | if (ret < 0) { |
| 2605 | status = SYNC_METADATA_STATUS_ERROR; |
| 2606 | goto end; |
| 2607 | } |
| 2608 | |
| 2609 | /* |
| 2610 | * The metadata stream and channel can be deleted while the |
| 2611 | * metadata stream lock was released. The streamed is checked |
| 2612 | * for deletion before we use it further. |
| 2613 | * |
| 2614 | * Note that it is safe to access a logically-deleted stream since its |
| 2615 | * existence is still guaranteed by the RCU read side lock. However, |
| 2616 | * it should no longer be used. The close/deletion of the metadata |
| 2617 | * channel and stream already guarantees that all metadata has been |
| 2618 | * consumed. Therefore, there is nothing left to do in this function. |
| 2619 | */ |
| 2620 | if (consumer_stream_is_deleted(metadata_stream)) { |
| 2621 | DBG("Metadata stream %" PRIu64 " was deleted during the metadata synchronization", |
| 2622 | metadata_stream->key); |
| 2623 | status = SYNC_METADATA_STATUS_NO_DATA; |
| 2624 | goto end; |
| 2625 | } |
| 2626 | |
| 2627 | ret = commit_one_metadata_packet(metadata_stream); |
| 2628 | if (ret < 0) { |
| 2629 | status = SYNC_METADATA_STATUS_ERROR; |
| 2630 | goto end; |
| 2631 | } else if (ret > 0) { |
| 2632 | status = SYNC_METADATA_STATUS_NEW_DATA; |
| 2633 | } else /* ret == 0 */ { |
| 2634 | status = SYNC_METADATA_STATUS_NO_DATA; |
| 2635 | goto end; |
| 2636 | } |
| 2637 | |
| 2638 | ret = lttng_ust_ctl_snapshot(metadata_stream->ustream); |
| 2639 | if (ret < 0) { |
| 2640 | ERR("Failed to take a snapshot of the metadata ring-buffer positions, ret = %d", |
| 2641 | ret); |
| 2642 | status = SYNC_METADATA_STATUS_ERROR; |
| 2643 | goto end; |
| 2644 | } |
| 2645 | |
| 2646 | end: |
| 2647 | return status; |
| 2648 | } |
| 2649 | |
| 2650 | /* |
| 2651 | * Return 0 on success else a negative value. |
| 2652 | */ |
| 2653 | static int notify_if_more_data(struct lttng_consumer_stream *stream, |
| 2654 | struct lttng_consumer_local_data *ctx) |
| 2655 | { |
| 2656 | int ret; |
| 2657 | struct lttng_ust_ctl_consumer_stream *ustream; |
| 2658 | |
| 2659 | LTTNG_ASSERT(stream); |
| 2660 | LTTNG_ASSERT(ctx); |
| 2661 | |
| 2662 | ustream = stream->ustream; |
| 2663 | |
| 2664 | /* |
| 2665 | * First, we are going to check if there is a new subbuffer available |
| 2666 | * before reading the stream wait_fd. |
| 2667 | */ |
| 2668 | /* Get the next subbuffer */ |
| 2669 | ret = lttng_ust_ctl_get_next_subbuf(ustream); |
| 2670 | if (ret) { |
| 2671 | /* No more data found, flag the stream. */ |
| 2672 | stream->has_data = 0; |
| 2673 | ret = 0; |
| 2674 | goto end; |
| 2675 | } |
| 2676 | |
| 2677 | ret = lttng_ust_ctl_put_subbuf(ustream); |
| 2678 | LTTNG_ASSERT(!ret); |
| 2679 | |
| 2680 | /* This stream still has data. Flag it and wake up the data thread. */ |
| 2681 | stream->has_data = 1; |
| 2682 | |
| 2683 | if (stream->monitor && !stream->hangup_flush_done && !ctx->has_wakeup) { |
| 2684 | ssize_t writelen; |
| 2685 | |
| 2686 | writelen = lttng_pipe_write(ctx->consumer_wakeup_pipe, "!", 1); |
| 2687 | if (writelen < 0 && errno != EAGAIN && errno != EWOULDBLOCK) { |
| 2688 | ret = writelen; |
| 2689 | goto end; |
| 2690 | } |
| 2691 | |
| 2692 | /* The wake up pipe has been notified. */ |
| 2693 | ctx->has_wakeup = 1; |
| 2694 | } |
| 2695 | ret = 0; |
| 2696 | |
| 2697 | end: |
| 2698 | return ret; |
| 2699 | } |
| 2700 | |
| 2701 | static int consumer_stream_ust_on_wake_up(struct lttng_consumer_stream *stream) |
| 2702 | { |
| 2703 | int ret = 0; |
| 2704 | |
| 2705 | /* |
| 2706 | * We can consume the 1 byte written into the wait_fd by |
| 2707 | * UST. Don't trigger error if we cannot read this one byte |
| 2708 | * (read returns 0), or if the error is EAGAIN or EWOULDBLOCK. |
| 2709 | * |
| 2710 | * This is only done when the stream is monitored by a thread, |
| 2711 | * before the flush is done after a hangup and if the stream |
| 2712 | * is not flagged with data since there might be nothing to |
| 2713 | * consume in the wait fd but still have data available |
| 2714 | * flagged by the consumer wake up pipe. |
| 2715 | */ |
| 2716 | if (stream->monitor && !stream->hangup_flush_done && !stream->has_data) { |
| 2717 | char dummy; |
| 2718 | ssize_t readlen; |
| 2719 | |
| 2720 | readlen = lttng_read(stream->wait_fd, &dummy, 1); |
| 2721 | if (readlen < 0 && errno != EAGAIN && errno != EWOULDBLOCK) { |
| 2722 | ret = readlen; |
| 2723 | } |
| 2724 | } |
| 2725 | |
| 2726 | return ret; |
| 2727 | } |
| 2728 | |
| 2729 | static int extract_common_subbuffer_info(struct lttng_consumer_stream *stream, |
| 2730 | struct stream_subbuffer *subbuf) |
| 2731 | { |
| 2732 | int ret; |
| 2733 | |
| 2734 | ret = lttng_ust_ctl_get_subbuf_size(stream->ustream, &subbuf->info.data.subbuf_size); |
| 2735 | if (ret) { |
| 2736 | goto end; |
| 2737 | } |
| 2738 | |
| 2739 | ret = lttng_ust_ctl_get_padded_subbuf_size(stream->ustream, |
| 2740 | &subbuf->info.data.padded_subbuf_size); |
| 2741 | if (ret) { |
| 2742 | goto end; |
| 2743 | } |
| 2744 | |
| 2745 | end: |
| 2746 | return ret; |
| 2747 | } |
| 2748 | |
| 2749 | static int extract_metadata_subbuffer_info(struct lttng_consumer_stream *stream, |
| 2750 | struct stream_subbuffer *subbuf) |
| 2751 | { |
| 2752 | int ret; |
| 2753 | |
| 2754 | ret = extract_common_subbuffer_info(stream, subbuf); |
| 2755 | if (ret) { |
| 2756 | goto end; |
| 2757 | } |
| 2758 | |
| 2759 | subbuf->info.metadata.version = stream->metadata_version; |
| 2760 | |
| 2761 | end: |
| 2762 | return ret; |
| 2763 | } |
| 2764 | |
| 2765 | static int extract_data_subbuffer_info(struct lttng_consumer_stream *stream, |
| 2766 | struct stream_subbuffer *subbuf) |
| 2767 | { |
| 2768 | int ret; |
| 2769 | |
| 2770 | ret = extract_common_subbuffer_info(stream, subbuf); |
| 2771 | if (ret) { |
| 2772 | goto end; |
| 2773 | } |
| 2774 | |
| 2775 | ret = lttng_ust_ctl_get_packet_size(stream->ustream, &subbuf->info.data.packet_size); |
| 2776 | if (ret < 0) { |
| 2777 | PERROR("Failed to get sub-buffer packet size"); |
| 2778 | goto end; |
| 2779 | } |
| 2780 | |
| 2781 | ret = lttng_ust_ctl_get_content_size(stream->ustream, &subbuf->info.data.content_size); |
| 2782 | if (ret < 0) { |
| 2783 | PERROR("Failed to get sub-buffer content size"); |
| 2784 | goto end; |
| 2785 | } |
| 2786 | |
| 2787 | ret = lttng_ust_ctl_get_timestamp_begin(stream->ustream, |
| 2788 | &subbuf->info.data.timestamp_begin); |
| 2789 | if (ret < 0) { |
| 2790 | PERROR("Failed to get sub-buffer begin timestamp"); |
| 2791 | goto end; |
| 2792 | } |
| 2793 | |
| 2794 | ret = lttng_ust_ctl_get_timestamp_end(stream->ustream, &subbuf->info.data.timestamp_end); |
| 2795 | if (ret < 0) { |
| 2796 | PERROR("Failed to get sub-buffer end timestamp"); |
| 2797 | goto end; |
| 2798 | } |
| 2799 | |
| 2800 | ret = lttng_ust_ctl_get_events_discarded(stream->ustream, |
| 2801 | &subbuf->info.data.events_discarded); |
| 2802 | if (ret) { |
| 2803 | PERROR("Failed to get sub-buffer events discarded count"); |
| 2804 | goto end; |
| 2805 | } |
| 2806 | |
| 2807 | ret = lttng_ust_ctl_get_sequence_number(stream->ustream, |
| 2808 | &subbuf->info.data.sequence_number.value); |
| 2809 | if (ret) { |
| 2810 | /* May not be supported by older LTTng-modules. */ |
| 2811 | if (ret != -ENOTTY) { |
| 2812 | PERROR("Failed to get sub-buffer sequence number"); |
| 2813 | goto end; |
| 2814 | } |
| 2815 | } else { |
| 2816 | subbuf->info.data.sequence_number.is_set = true; |
| 2817 | } |
| 2818 | |
| 2819 | ret = lttng_ust_ctl_get_stream_id(stream->ustream, &subbuf->info.data.stream_id); |
| 2820 | if (ret < 0) { |
| 2821 | PERROR("Failed to get stream id"); |
| 2822 | goto end; |
| 2823 | } |
| 2824 | |
| 2825 | ret = lttng_ust_ctl_get_instance_id(stream->ustream, |
| 2826 | &subbuf->info.data.stream_instance_id.value); |
| 2827 | if (ret) { |
| 2828 | /* May not be supported by older LTTng-modules. */ |
| 2829 | if (ret != -ENOTTY) { |
| 2830 | PERROR("Failed to get stream instance id"); |
| 2831 | goto end; |
| 2832 | } |
| 2833 | } else { |
| 2834 | subbuf->info.data.stream_instance_id.is_set = true; |
| 2835 | } |
| 2836 | end: |
| 2837 | return ret; |
| 2838 | } |
| 2839 | |
| 2840 | static int get_next_subbuffer_common(struct lttng_consumer_stream *stream, |
| 2841 | struct stream_subbuffer *subbuffer) |
| 2842 | { |
| 2843 | int ret; |
| 2844 | const char *addr; |
| 2845 | |
| 2846 | ret = stream->read_subbuffer_ops.extract_subbuffer_info(stream, subbuffer); |
| 2847 | if (ret) { |
| 2848 | goto end; |
| 2849 | } |
| 2850 | |
| 2851 | ret = get_current_subbuf_addr(stream, &addr); |
| 2852 | if (ret) { |
| 2853 | goto end; |
| 2854 | } |
| 2855 | |
| 2856 | subbuffer->buffer.buffer = |
| 2857 | lttng_buffer_view_init(addr, 0, subbuffer->info.data.padded_subbuf_size); |
| 2858 | LTTNG_ASSERT(subbuffer->buffer.buffer.data != nullptr); |
| 2859 | end: |
| 2860 | return ret; |
| 2861 | } |
| 2862 | |
| 2863 | static enum get_next_subbuffer_status get_next_subbuffer(struct lttng_consumer_stream *stream, |
| 2864 | struct stream_subbuffer *subbuffer) |
| 2865 | { |
| 2866 | int ret; |
| 2867 | enum get_next_subbuffer_status status; |
| 2868 | |
| 2869 | ret = lttng_ust_ctl_get_next_subbuf(stream->ustream); |
| 2870 | switch (ret) { |
| 2871 | case 0: |
| 2872 | status = GET_NEXT_SUBBUFFER_STATUS_OK; |
| 2873 | break; |
| 2874 | case -ENODATA: |
| 2875 | case -EAGAIN: |
| 2876 | /* |
| 2877 | * The caller only expects -ENODATA when there is no data to |
| 2878 | * read, but the kernel tracer returns -EAGAIN when there is |
| 2879 | * currently no data for a non-finalized stream, and -ENODATA |
| 2880 | * when there is no data for a finalized stream. Those can be |
| 2881 | * combined into a -ENODATA return value. |
| 2882 | */ |
| 2883 | status = GET_NEXT_SUBBUFFER_STATUS_NO_DATA; |
| 2884 | goto end; |
| 2885 | default: |
| 2886 | status = GET_NEXT_SUBBUFFER_STATUS_ERROR; |
| 2887 | goto end; |
| 2888 | } |
| 2889 | |
| 2890 | ret = get_next_subbuffer_common(stream, subbuffer); |
| 2891 | if (ret) { |
| 2892 | status = GET_NEXT_SUBBUFFER_STATUS_ERROR; |
| 2893 | goto end; |
| 2894 | } |
| 2895 | end: |
| 2896 | return status; |
| 2897 | } |
| 2898 | |
| 2899 | static enum get_next_subbuffer_status |
| 2900 | get_next_subbuffer_metadata(struct lttng_consumer_stream *stream, |
| 2901 | struct stream_subbuffer *subbuffer) |
| 2902 | { |
| 2903 | int ret; |
| 2904 | bool cache_empty; |
| 2905 | bool got_subbuffer; |
| 2906 | bool coherent; |
| 2907 | bool buffer_empty; |
| 2908 | unsigned long consumed_pos, produced_pos; |
| 2909 | enum get_next_subbuffer_status status; |
| 2910 | |
| 2911 | do { |
| 2912 | ret = lttng_ust_ctl_get_next_subbuf(stream->ustream); |
| 2913 | if (ret == 0) { |
| 2914 | got_subbuffer = true; |
| 2915 | } else { |
| 2916 | got_subbuffer = false; |
| 2917 | if (ret != -EAGAIN) { |
| 2918 | /* Fatal error. */ |
| 2919 | status = GET_NEXT_SUBBUFFER_STATUS_ERROR; |
| 2920 | goto end; |
| 2921 | } |
| 2922 | } |
| 2923 | |
| 2924 | /* |
| 2925 | * Determine if the cache is empty and ensure that a sub-buffer |
| 2926 | * is made available if the cache is not empty. |
| 2927 | */ |
| 2928 | if (!got_subbuffer) { |
| 2929 | ret = commit_one_metadata_packet(stream); |
| 2930 | if (ret < 0 && ret != -ENOBUFS) { |
| 2931 | status = GET_NEXT_SUBBUFFER_STATUS_ERROR; |
| 2932 | goto end; |
| 2933 | } else if (ret == 0) { |
| 2934 | /* Not an error, the cache is empty. */ |
| 2935 | cache_empty = true; |
| 2936 | status = GET_NEXT_SUBBUFFER_STATUS_NO_DATA; |
| 2937 | goto end; |
| 2938 | } else { |
| 2939 | cache_empty = false; |
| 2940 | } |
| 2941 | } else { |
| 2942 | pthread_mutex_lock(&stream->chan->metadata_cache->lock); |
| 2943 | cache_empty = stream->chan->metadata_cache->contents.size == |
| 2944 | stream->ust_metadata_pushed; |
| 2945 | pthread_mutex_unlock(&stream->chan->metadata_cache->lock); |
| 2946 | } |
| 2947 | } while (!got_subbuffer); |
| 2948 | |
| 2949 | /* Populate sub-buffer infos and view. */ |
| 2950 | ret = get_next_subbuffer_common(stream, subbuffer); |
| 2951 | if (ret) { |
| 2952 | status = GET_NEXT_SUBBUFFER_STATUS_ERROR; |
| 2953 | goto end; |
| 2954 | } |
| 2955 | |
| 2956 | ret = lttng_ustconsumer_sample_snapshot_positions(stream); |
| 2957 | if (ret < 0) { |
| 2958 | /* |
| 2959 | * -EAGAIN is not expected since we got a sub-buffer and haven't |
| 2960 | * pushed the consumption position yet (on put_next). |
| 2961 | */ |
| 2962 | PERROR("Failed to take a snapshot of metadata buffer positions"); |
| 2963 | status = GET_NEXT_SUBBUFFER_STATUS_ERROR; |
| 2964 | goto end; |
| 2965 | } |
| 2966 | |
| 2967 | ret = lttng_ustconsumer_get_consumed_snapshot(stream, &consumed_pos); |
| 2968 | if (ret) { |
| 2969 | PERROR("Failed to get metadata consumed position"); |
| 2970 | status = GET_NEXT_SUBBUFFER_STATUS_ERROR; |
| 2971 | goto end; |
| 2972 | } |
| 2973 | |
| 2974 | ret = lttng_ustconsumer_get_produced_snapshot(stream, &produced_pos); |
| 2975 | if (ret) { |
| 2976 | PERROR("Failed to get metadata produced position"); |
| 2977 | status = GET_NEXT_SUBBUFFER_STATUS_ERROR; |
| 2978 | goto end; |
| 2979 | } |
| 2980 | |
| 2981 | /* Last sub-buffer of the ring buffer ? */ |
| 2982 | buffer_empty = (consumed_pos + stream->max_sb_size) == produced_pos; |
| 2983 | |
| 2984 | /* |
| 2985 | * The sessiond registry lock ensures that coherent units of metadata |
| 2986 | * are pushed to the consumer daemon at once. Hence, if a sub-buffer is |
| 2987 | * acquired, the cache is empty, and it is the only available sub-buffer |
| 2988 | * available, it is safe to assume that it is "coherent". |
| 2989 | */ |
| 2990 | coherent = got_subbuffer && cache_empty && buffer_empty; |
| 2991 | |
| 2992 | LTTNG_OPTIONAL_SET(&subbuffer->info.metadata.coherent, coherent); |
| 2993 | status = GET_NEXT_SUBBUFFER_STATUS_OK; |
| 2994 | end: |
| 2995 | return status; |
| 2996 | } |
| 2997 | |
| 2998 | static int put_next_subbuffer(struct lttng_consumer_stream *stream, |
| 2999 | struct stream_subbuffer *subbuffer __attribute__((unused))) |
| 3000 | { |
| 3001 | const int ret = lttng_ust_ctl_put_next_subbuf(stream->ustream); |
| 3002 | |
| 3003 | LTTNG_ASSERT(ret == 0); |
| 3004 | return ret; |
| 3005 | } |
| 3006 | |
| 3007 | static int signal_metadata(struct lttng_consumer_stream *stream, |
| 3008 | struct lttng_consumer_local_data *ctx __attribute__((unused))) |
| 3009 | { |
| 3010 | ASSERT_LOCKED(stream->metadata_rdv_lock); |
| 3011 | return pthread_cond_broadcast(&stream->metadata_rdv) ? -errno : 0; |
| 3012 | } |
| 3013 | |
| 3014 | static int lttng_ustconsumer_set_stream_ops(struct lttng_consumer_stream *stream) |
| 3015 | { |
| 3016 | int ret = 0; |
| 3017 | |
| 3018 | stream->read_subbuffer_ops.on_wake_up = consumer_stream_ust_on_wake_up; |
| 3019 | if (stream->metadata_flag) { |
| 3020 | stream->read_subbuffer_ops.get_next_subbuffer = get_next_subbuffer_metadata; |
| 3021 | stream->read_subbuffer_ops.extract_subbuffer_info = extract_metadata_subbuffer_info; |
| 3022 | stream->read_subbuffer_ops.reset_metadata = |
| 3023 | metadata_stream_reset_cache_consumed_position; |
| 3024 | if (stream->chan->is_live) { |
| 3025 | stream->read_subbuffer_ops.on_sleep = signal_metadata; |
| 3026 | ret = consumer_stream_enable_metadata_bucketization(stream); |
| 3027 | if (ret) { |
| 3028 | goto end; |
| 3029 | } |
| 3030 | } |
| 3031 | } else { |
| 3032 | stream->read_subbuffer_ops.get_next_subbuffer = get_next_subbuffer; |
| 3033 | stream->read_subbuffer_ops.extract_subbuffer_info = extract_data_subbuffer_info; |
| 3034 | stream->read_subbuffer_ops.on_sleep = notify_if_more_data; |
| 3035 | if (stream->chan->is_live) { |
| 3036 | stream->read_subbuffer_ops.send_live_beacon = consumer_flush_ust_index; |
| 3037 | } |
| 3038 | } |
| 3039 | |
| 3040 | stream->read_subbuffer_ops.put_next_subbuffer = put_next_subbuffer; |
| 3041 | end: |
| 3042 | return ret; |
| 3043 | } |
| 3044 | |
| 3045 | /* |
| 3046 | * Called when a stream is created. |
| 3047 | * |
| 3048 | * Return 0 on success or else a negative value. |
| 3049 | */ |
| 3050 | int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream *stream) |
| 3051 | { |
| 3052 | int ret; |
| 3053 | |
| 3054 | LTTNG_ASSERT(stream); |
| 3055 | |
| 3056 | /* |
| 3057 | * Don't create anything if this is set for streaming or if there is |
| 3058 | * no current trace chunk on the parent channel. |
| 3059 | */ |
| 3060 | if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor && |
| 3061 | stream->chan->trace_chunk) { |
| 3062 | ret = consumer_stream_create_output_files(stream, true); |
| 3063 | if (ret) { |
| 3064 | goto error; |
| 3065 | } |
| 3066 | } |
| 3067 | |
| 3068 | lttng_ustconsumer_set_stream_ops(stream); |
| 3069 | ret = 0; |
| 3070 | |
| 3071 | error: |
| 3072 | return ret; |
| 3073 | } |
| 3074 | |
| 3075 | /* |
| 3076 | * Check if data is still being extracted from the buffers for a specific |
| 3077 | * stream. Consumer data lock MUST be acquired before calling this function |
| 3078 | * and the stream lock. |
| 3079 | * |
| 3080 | * Return 1 if the traced data are still getting read else 0 meaning that the |
| 3081 | * data is available for trace viewer reading. |
| 3082 | */ |
| 3083 | int lttng_ustconsumer_data_pending(struct lttng_consumer_stream *stream) |
| 3084 | { |
| 3085 | int ret; |
| 3086 | |
| 3087 | LTTNG_ASSERT(stream); |
| 3088 | LTTNG_ASSERT(stream->ustream); |
| 3089 | ASSERT_LOCKED(stream->lock); |
| 3090 | |
| 3091 | DBG("UST consumer checking data pending"); |
| 3092 | |
| 3093 | if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) { |
| 3094 | ret = 0; |
| 3095 | goto end; |
| 3096 | } |
| 3097 | |
| 3098 | if (stream->chan->type == CONSUMER_CHANNEL_TYPE_METADATA) { |
| 3099 | uint64_t contiguous, pushed; |
| 3100 | |
| 3101 | /* Ease our life a bit. */ |
| 3102 | pthread_mutex_lock(&stream->chan->metadata_cache->lock); |
| 3103 | contiguous = stream->chan->metadata_cache->contents.size; |
| 3104 | pthread_mutex_unlock(&stream->chan->metadata_cache->lock); |
| 3105 | pushed = stream->ust_metadata_pushed; |
| 3106 | |
| 3107 | /* |
| 3108 | * We can simply check whether all contiguously available data |
| 3109 | * has been pushed to the ring buffer, since the push operation |
| 3110 | * is performed within get_next_subbuf(), and because both |
| 3111 | * get_next_subbuf() and put_next_subbuf() are issued atomically |
| 3112 | * thanks to the stream lock within |
| 3113 | * lttng_ustconsumer_read_subbuffer(). This basically means that |
| 3114 | * whetnever ust_metadata_pushed is incremented, the associated |
| 3115 | * metadata has been consumed from the metadata stream. |
| 3116 | */ |
| 3117 | DBG("UST consumer metadata pending check: contiguous %" PRIu64 |
| 3118 | " vs pushed %" PRIu64, |
| 3119 | contiguous, |
| 3120 | pushed); |
| 3121 | LTTNG_ASSERT(((int64_t) (contiguous - pushed)) >= 0); |
| 3122 | if ((contiguous != pushed) || |
| 3123 | (((int64_t) contiguous - pushed) > 0 || contiguous == 0)) { |
| 3124 | ret = 1; /* Data is pending */ |
| 3125 | goto end; |
| 3126 | } |
| 3127 | } else { |
| 3128 | ret = lttng_ust_ctl_get_next_subbuf(stream->ustream); |
| 3129 | if (ret == 0) { |
| 3130 | /* |
| 3131 | * There is still data so let's put back this |
| 3132 | * subbuffer. |
| 3133 | */ |
| 3134 | ret = lttng_ust_ctl_put_subbuf(stream->ustream); |
| 3135 | LTTNG_ASSERT(ret == 0); |
| 3136 | ret = 1; /* Data is pending */ |
| 3137 | goto end; |
| 3138 | } |
| 3139 | } |
| 3140 | |
| 3141 | /* Data is NOT pending so ready to be read. */ |
| 3142 | ret = 0; |
| 3143 | |
| 3144 | end: |
| 3145 | return ret; |
| 3146 | } |
| 3147 | |
| 3148 | /* |
| 3149 | * Stop a given metadata channel timer if enabled and close the wait fd which |
| 3150 | * is the poll pipe of the metadata stream. |
| 3151 | * |
| 3152 | * This MUST be called with the metadata channel lock acquired. |
| 3153 | */ |
| 3154 | void lttng_ustconsumer_close_metadata(struct lttng_consumer_channel *metadata) |
| 3155 | { |
| 3156 | int ret; |
| 3157 | |
| 3158 | LTTNG_ASSERT(metadata); |
| 3159 | LTTNG_ASSERT(metadata->type == CONSUMER_CHANNEL_TYPE_METADATA); |
| 3160 | |
| 3161 | DBG("Closing metadata channel key %" PRIu64, metadata->key); |
| 3162 | |
| 3163 | if (metadata->switch_timer_enabled == 1) { |
| 3164 | consumer_timer_switch_stop(metadata); |
| 3165 | } |
| 3166 | |
| 3167 | if (!metadata->metadata_stream) { |
| 3168 | goto end; |
| 3169 | } |
| 3170 | |
| 3171 | /* |
| 3172 | * Closing write side so the thread monitoring the stream wakes up if any |
| 3173 | * and clean the metadata stream. |
| 3174 | */ |
| 3175 | if (metadata->metadata_stream->ust_metadata_poll_pipe[1] >= 0) { |
| 3176 | ret = close(metadata->metadata_stream->ust_metadata_poll_pipe[1]); |
| 3177 | if (ret < 0) { |
| 3178 | PERROR("closing metadata pipe write side"); |
| 3179 | } |
| 3180 | metadata->metadata_stream->ust_metadata_poll_pipe[1] = -1; |
| 3181 | } |
| 3182 | |
| 3183 | end: |
| 3184 | return; |
| 3185 | } |
| 3186 | |
| 3187 | /* |
| 3188 | * Close every metadata stream wait fd of the metadata hash table. This |
| 3189 | * function MUST be used very carefully so not to run into a race between the |
| 3190 | * metadata thread handling streams and this function closing their wait fd. |
| 3191 | * |
| 3192 | * For UST, this is used when the session daemon hangs up. Its the metadata |
| 3193 | * producer so calling this is safe because we are assured that no state change |
| 3194 | * can occur in the metadata thread for the streams in the hash table. |
| 3195 | */ |
| 3196 | void lttng_ustconsumer_close_all_metadata(struct lttng_ht *metadata_ht) |
| 3197 | { |
| 3198 | struct lttng_ht_iter iter; |
| 3199 | struct lttng_consumer_stream *stream; |
| 3200 | |
| 3201 | LTTNG_ASSERT(metadata_ht); |
| 3202 | LTTNG_ASSERT(metadata_ht->ht); |
| 3203 | |
| 3204 | DBG("UST consumer closing all metadata streams"); |
| 3205 | |
| 3206 | { |
| 3207 | lttng::urcu::read_lock_guard read_lock; |
| 3208 | |
| 3209 | cds_lfht_for_each_entry (metadata_ht->ht, &iter.iter, stream, node.node) { |
| 3210 | health_code_update(); |
| 3211 | |
| 3212 | pthread_mutex_lock(&stream->chan->lock); |
| 3213 | lttng_ustconsumer_close_metadata(stream->chan); |
| 3214 | pthread_mutex_unlock(&stream->chan->lock); |
| 3215 | } |
| 3216 | } |
| 3217 | } |
| 3218 | |
| 3219 | void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream *stream) |
| 3220 | { |
| 3221 | int ret; |
| 3222 | |
| 3223 | ret = lttng_ust_ctl_stream_close_wakeup_fd(stream->ustream); |
| 3224 | if (ret < 0) { |
| 3225 | ERR("Unable to close wakeup fd"); |
| 3226 | } |
| 3227 | } |
| 3228 | |
| 3229 | /* |
| 3230 | * Please refer to consumer-timer.c before adding any lock within this |
| 3231 | * function or any of its callees. Timers have a very strict locking |
| 3232 | * semantic with respect to teardown. Failure to respect this semantic |
| 3233 | * introduces deadlocks. |
| 3234 | * |
| 3235 | * DON'T hold the metadata lock when calling this function, else this |
| 3236 | * can cause deadlock involving consumer awaiting for metadata to be |
| 3237 | * pushed out due to concurrent interaction with the session daemon. |
| 3238 | */ |
| 3239 | int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data *ctx, |
| 3240 | struct lttng_consumer_channel *channel, |
| 3241 | bool invoked_by_timer, |
| 3242 | int wait) |
| 3243 | { |
| 3244 | struct lttcomm_metadata_request_msg request; |
| 3245 | struct lttcomm_consumer_msg msg; |
| 3246 | enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS; |
| 3247 | uint64_t len, key, offset, version; |
| 3248 | int ret; |
| 3249 | |
| 3250 | LTTNG_ASSERT(channel); |
| 3251 | LTTNG_ASSERT(channel->metadata_cache); |
| 3252 | |
| 3253 | memset(&request, 0, sizeof(request)); |
| 3254 | |
| 3255 | /* send the metadata request to sessiond */ |
| 3256 | switch (the_consumer_data.type) { |
| 3257 | case LTTNG_CONSUMER64_UST: |
| 3258 | request.bits_per_long = 64; |
| 3259 | break; |
| 3260 | case LTTNG_CONSUMER32_UST: |
| 3261 | request.bits_per_long = 32; |
| 3262 | break; |
| 3263 | default: |
| 3264 | request.bits_per_long = 0; |
| 3265 | break; |
| 3266 | } |
| 3267 | |
| 3268 | request.session_id = channel->session_id; |
| 3269 | request.session_id_per_pid = channel->session_id_per_pid; |
| 3270 | /* |
| 3271 | * Request the application UID here so the metadata of that application can |
| 3272 | * be sent back. The channel UID corresponds to the user UID of the session |
| 3273 | * used for the rights on the stream file(s). |
| 3274 | */ |
| 3275 | request.uid = channel->ust_app_uid; |
| 3276 | request.key = channel->key; |
| 3277 | |
| 3278 | DBG("Sending metadata request to sessiond, session id %" PRIu64 ", per-pid %" PRIu64 |
| 3279 | ", app UID %u and channel key %" PRIu64, |
| 3280 | request.session_id, |
| 3281 | request.session_id_per_pid, |
| 3282 | request.uid, |
| 3283 | request.key); |
| 3284 | |
| 3285 | pthread_mutex_lock(&ctx->metadata_socket_lock); |
| 3286 | |
| 3287 | health_code_update(); |
| 3288 | |
| 3289 | ret = lttcomm_send_unix_sock(ctx->consumer_metadata_socket, &request, sizeof(request)); |
| 3290 | if (ret < 0) { |
| 3291 | ERR("Asking metadata to sessiond"); |
| 3292 | goto end; |
| 3293 | } |
| 3294 | |
| 3295 | health_code_update(); |
| 3296 | |
| 3297 | /* Receive the metadata from sessiond */ |
| 3298 | ret = lttcomm_recv_unix_sock(ctx->consumer_metadata_socket, &msg, sizeof(msg)); |
| 3299 | if (ret != sizeof(msg)) { |
| 3300 | DBG("Consumer received unexpected message size %d (expects %zu)", ret, sizeof(msg)); |
| 3301 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD); |
| 3302 | /* |
| 3303 | * The ret value might 0 meaning an orderly shutdown but this is ok |
| 3304 | * since the caller handles this. |
| 3305 | */ |
| 3306 | goto end; |
| 3307 | } |
| 3308 | |
| 3309 | health_code_update(); |
| 3310 | |
| 3311 | if (msg.cmd_type == LTTNG_ERR_UND) { |
| 3312 | /* No registry found */ |
| 3313 | (void) consumer_send_status_msg(ctx->consumer_metadata_socket, ret_code); |
| 3314 | ret = 0; |
| 3315 | goto end; |
| 3316 | } else if (msg.cmd_type != LTTNG_CONSUMER_PUSH_METADATA) { |
| 3317 | ERR("Unexpected cmd_type received %d", msg.cmd_type); |
| 3318 | ret = -1; |
| 3319 | goto end; |
| 3320 | } |
| 3321 | |
| 3322 | len = msg.u.push_metadata.len; |
| 3323 | key = msg.u.push_metadata.key; |
| 3324 | offset = msg.u.push_metadata.target_offset; |
| 3325 | version = msg.u.push_metadata.version; |
| 3326 | |
| 3327 | LTTNG_ASSERT(key == channel->key); |
| 3328 | if (len == 0) { |
| 3329 | DBG("No new metadata to receive for key %" PRIu64, key); |
| 3330 | } |
| 3331 | |
| 3332 | health_code_update(); |
| 3333 | |
| 3334 | /* Tell session daemon we are ready to receive the metadata. */ |
| 3335 | ret = consumer_send_status_msg(ctx->consumer_metadata_socket, LTTCOMM_CONSUMERD_SUCCESS); |
| 3336 | if (ret < 0 || len == 0) { |
| 3337 | /* |
| 3338 | * Somehow, the session daemon is not responding anymore or there is |
| 3339 | * nothing to receive. |
| 3340 | */ |
| 3341 | goto end; |
| 3342 | } |
| 3343 | |
| 3344 | health_code_update(); |
| 3345 | |
| 3346 | ret = lttng_ustconsumer_recv_metadata(ctx->consumer_metadata_socket, |
| 3347 | key, |
| 3348 | offset, |
| 3349 | len, |
| 3350 | version, |
| 3351 | channel, |
| 3352 | invoked_by_timer, |
| 3353 | wait); |
| 3354 | if (ret >= 0) { |
| 3355 | /* |
| 3356 | * Only send the status msg if the sessiond is alive meaning a positive |
| 3357 | * ret code. |
| 3358 | */ |
| 3359 | (void) consumer_send_status_msg(ctx->consumer_metadata_socket, ret); |
| 3360 | } |
| 3361 | ret = 0; |
| 3362 | |
| 3363 | end: |
| 3364 | health_code_update(); |
| 3365 | |
| 3366 | pthread_mutex_unlock(&ctx->metadata_socket_lock); |
| 3367 | return ret; |
| 3368 | } |
| 3369 | |
| 3370 | /* |
| 3371 | * Return the ustctl call for the get stream id. |
| 3372 | */ |
| 3373 | int lttng_ustconsumer_get_stream_id(struct lttng_consumer_stream *stream, uint64_t *stream_id) |
| 3374 | { |
| 3375 | LTTNG_ASSERT(stream); |
| 3376 | LTTNG_ASSERT(stream_id); |
| 3377 | |
| 3378 | return lttng_ust_ctl_get_stream_id(stream->ustream, stream_id); |
| 3379 | } |
| 3380 | |
| 3381 | void lttng_ustconsumer_sigbus_handle(void *addr) |
| 3382 | { |
| 3383 | lttng_ust_ctl_sigbus_handle(addr); |
| 3384 | } |