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