| 1 | /* |
| 2 | * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca> |
| 3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 4 | * 2012 - David Goulet <dgoulet@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, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 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 _GNU_SOURCE |
| 21 | #include <assert.h> |
| 22 | #include <poll.h> |
| 23 | #include <pthread.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/mman.h> |
| 27 | #include <sys/socket.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <unistd.h> |
| 30 | #include <inttypes.h> |
| 31 | |
| 32 | #include <common/common.h> |
| 33 | #include <common/utils.h> |
| 34 | #include <common/compat/poll.h> |
| 35 | #include <common/kernel-ctl/kernel-ctl.h> |
| 36 | #include <common/sessiond-comm/relayd.h> |
| 37 | #include <common/sessiond-comm/sessiond-comm.h> |
| 38 | #include <common/kernel-consumer/kernel-consumer.h> |
| 39 | #include <common/relayd/relayd.h> |
| 40 | #include <common/ust-consumer/ust-consumer.h> |
| 41 | |
| 42 | #include "consumer.h" |
| 43 | |
| 44 | struct lttng_consumer_global_data consumer_data = { |
| 45 | .stream_count = 0, |
| 46 | .need_update = 1, |
| 47 | .type = LTTNG_CONSUMER_UNKNOWN, |
| 48 | }; |
| 49 | |
| 50 | /* timeout parameter, to control the polling thread grace period. */ |
| 51 | int consumer_poll_timeout = -1; |
| 52 | |
| 53 | /* |
| 54 | * Flag to inform the polling thread to quit when all fd hung up. Updated by |
| 55 | * the consumer_thread_receive_fds when it notices that all fds has hung up. |
| 56 | * Also updated by the signal handler (consumer_should_exit()). Read by the |
| 57 | * polling threads. |
| 58 | */ |
| 59 | volatile int consumer_quit = 0; |
| 60 | |
| 61 | /* |
| 62 | * Find a stream. The consumer_data.lock must be locked during this |
| 63 | * call. |
| 64 | */ |
| 65 | static struct lttng_consumer_stream *consumer_find_stream(int key, |
| 66 | struct lttng_ht *ht) |
| 67 | { |
| 68 | struct lttng_ht_iter iter; |
| 69 | struct lttng_ht_node_ulong *node; |
| 70 | struct lttng_consumer_stream *stream = NULL; |
| 71 | |
| 72 | assert(ht); |
| 73 | |
| 74 | /* Negative keys are lookup failures */ |
| 75 | if (key < 0) { |
| 76 | return NULL; |
| 77 | } |
| 78 | |
| 79 | rcu_read_lock(); |
| 80 | |
| 81 | lttng_ht_lookup(ht, (void *)((unsigned long) key), &iter); |
| 82 | node = lttng_ht_iter_get_node_ulong(&iter); |
| 83 | if (node != NULL) { |
| 84 | stream = caa_container_of(node, struct lttng_consumer_stream, node); |
| 85 | } |
| 86 | |
| 87 | rcu_read_unlock(); |
| 88 | |
| 89 | return stream; |
| 90 | } |
| 91 | |
| 92 | static void consumer_steal_stream_key(int key, struct lttng_ht *ht) |
| 93 | { |
| 94 | struct lttng_consumer_stream *stream; |
| 95 | |
| 96 | rcu_read_lock(); |
| 97 | stream = consumer_find_stream(key, ht); |
| 98 | if (stream) { |
| 99 | stream->key = -1; |
| 100 | /* |
| 101 | * We don't want the lookup to match, but we still need |
| 102 | * to iterate on this stream when iterating over the hash table. Just |
| 103 | * change the node key. |
| 104 | */ |
| 105 | stream->node.key = -1; |
| 106 | } |
| 107 | rcu_read_unlock(); |
| 108 | } |
| 109 | |
| 110 | static struct lttng_consumer_channel *consumer_find_channel(int key) |
| 111 | { |
| 112 | struct lttng_ht_iter iter; |
| 113 | struct lttng_ht_node_ulong *node; |
| 114 | struct lttng_consumer_channel *channel = NULL; |
| 115 | |
| 116 | /* Negative keys are lookup failures */ |
| 117 | if (key < 0) { |
| 118 | return NULL; |
| 119 | } |
| 120 | |
| 121 | rcu_read_lock(); |
| 122 | |
| 123 | lttng_ht_lookup(consumer_data.channel_ht, (void *)((unsigned long) key), |
| 124 | &iter); |
| 125 | node = lttng_ht_iter_get_node_ulong(&iter); |
| 126 | if (node != NULL) { |
| 127 | channel = caa_container_of(node, struct lttng_consumer_channel, node); |
| 128 | } |
| 129 | |
| 130 | rcu_read_unlock(); |
| 131 | |
| 132 | return channel; |
| 133 | } |
| 134 | |
| 135 | static void consumer_steal_channel_key(int key) |
| 136 | { |
| 137 | struct lttng_consumer_channel *channel; |
| 138 | |
| 139 | rcu_read_lock(); |
| 140 | channel = consumer_find_channel(key); |
| 141 | if (channel) { |
| 142 | channel->key = -1; |
| 143 | /* |
| 144 | * We don't want the lookup to match, but we still need |
| 145 | * to iterate on this channel when iterating over the hash table. Just |
| 146 | * change the node key. |
| 147 | */ |
| 148 | channel->node.key = -1; |
| 149 | } |
| 150 | rcu_read_unlock(); |
| 151 | } |
| 152 | |
| 153 | static |
| 154 | void consumer_free_stream(struct rcu_head *head) |
| 155 | { |
| 156 | struct lttng_ht_node_ulong *node = |
| 157 | caa_container_of(head, struct lttng_ht_node_ulong, head); |
| 158 | struct lttng_consumer_stream *stream = |
| 159 | caa_container_of(node, struct lttng_consumer_stream, node); |
| 160 | |
| 161 | free(stream); |
| 162 | } |
| 163 | |
| 164 | static |
| 165 | void consumer_free_metadata_stream(struct rcu_head *head) |
| 166 | { |
| 167 | struct lttng_ht_node_ulong *node = |
| 168 | caa_container_of(head, struct lttng_ht_node_ulong, head); |
| 169 | struct lttng_consumer_stream *stream = |
| 170 | caa_container_of(node, struct lttng_consumer_stream, waitfd_node); |
| 171 | |
| 172 | free(stream); |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * RCU protected relayd socket pair free. |
| 177 | */ |
| 178 | static void consumer_rcu_free_relayd(struct rcu_head *head) |
| 179 | { |
| 180 | struct lttng_ht_node_ulong *node = |
| 181 | caa_container_of(head, struct lttng_ht_node_ulong, head); |
| 182 | struct consumer_relayd_sock_pair *relayd = |
| 183 | caa_container_of(node, struct consumer_relayd_sock_pair, node); |
| 184 | |
| 185 | free(relayd); |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * Destroy and free relayd socket pair object. |
| 190 | * |
| 191 | * This function MUST be called with the consumer_data lock acquired. |
| 192 | */ |
| 193 | static void destroy_relayd(struct consumer_relayd_sock_pair *relayd) |
| 194 | { |
| 195 | int ret; |
| 196 | struct lttng_ht_iter iter; |
| 197 | |
| 198 | if (relayd == NULL) { |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | DBG("Consumer destroy and close relayd socket pair"); |
| 203 | |
| 204 | iter.iter.node = &relayd->node.node; |
| 205 | ret = lttng_ht_del(consumer_data.relayd_ht, &iter); |
| 206 | if (ret != 0) { |
| 207 | /* We assume the relayd was already destroyed */ |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | /* Close all sockets */ |
| 212 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); |
| 213 | (void) relayd_close(&relayd->control_sock); |
| 214 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
| 215 | (void) relayd_close(&relayd->data_sock); |
| 216 | |
| 217 | /* RCU free() call */ |
| 218 | call_rcu(&relayd->node.head, consumer_rcu_free_relayd); |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * Flag a relayd socket pair for destruction. Destroy it if the refcount |
| 223 | * reaches zero. |
| 224 | * |
| 225 | * RCU read side lock MUST be aquired before calling this function. |
| 226 | */ |
| 227 | void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair *relayd) |
| 228 | { |
| 229 | assert(relayd); |
| 230 | |
| 231 | /* Set destroy flag for this object */ |
| 232 | uatomic_set(&relayd->destroy_flag, 1); |
| 233 | |
| 234 | /* Destroy the relayd if refcount is 0 */ |
| 235 | if (uatomic_read(&relayd->refcount) == 0) { |
| 236 | destroy_relayd(relayd); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | * Remove a stream from the global list protected by a mutex. This |
| 242 | * function is also responsible for freeing its data structures. |
| 243 | */ |
| 244 | void consumer_del_stream(struct lttng_consumer_stream *stream, |
| 245 | struct lttng_ht *ht) |
| 246 | { |
| 247 | int ret; |
| 248 | struct lttng_ht_iter iter; |
| 249 | struct lttng_consumer_channel *free_chan = NULL; |
| 250 | struct consumer_relayd_sock_pair *relayd; |
| 251 | |
| 252 | assert(stream); |
| 253 | |
| 254 | if (ht == NULL) { |
| 255 | /* Means the stream was allocated but not successfully added */ |
| 256 | goto free_stream; |
| 257 | } |
| 258 | |
| 259 | pthread_mutex_lock(&consumer_data.lock); |
| 260 | |
| 261 | switch (consumer_data.type) { |
| 262 | case LTTNG_CONSUMER_KERNEL: |
| 263 | if (stream->mmap_base != NULL) { |
| 264 | ret = munmap(stream->mmap_base, stream->mmap_len); |
| 265 | if (ret != 0) { |
| 266 | PERROR("munmap"); |
| 267 | } |
| 268 | } |
| 269 | break; |
| 270 | case LTTNG_CONSUMER32_UST: |
| 271 | case LTTNG_CONSUMER64_UST: |
| 272 | lttng_ustconsumer_del_stream(stream); |
| 273 | break; |
| 274 | default: |
| 275 | ERR("Unknown consumer_data type"); |
| 276 | assert(0); |
| 277 | goto end; |
| 278 | } |
| 279 | |
| 280 | rcu_read_lock(); |
| 281 | iter.iter.node = &stream->node.node; |
| 282 | ret = lttng_ht_del(ht, &iter); |
| 283 | assert(!ret); |
| 284 | |
| 285 | rcu_read_unlock(); |
| 286 | |
| 287 | if (consumer_data.stream_count <= 0) { |
| 288 | goto end; |
| 289 | } |
| 290 | consumer_data.stream_count--; |
| 291 | if (!stream) { |
| 292 | goto end; |
| 293 | } |
| 294 | if (stream->out_fd >= 0) { |
| 295 | ret = close(stream->out_fd); |
| 296 | if (ret) { |
| 297 | PERROR("close"); |
| 298 | } |
| 299 | } |
| 300 | if (stream->wait_fd >= 0 && !stream->wait_fd_is_copy) { |
| 301 | ret = close(stream->wait_fd); |
| 302 | if (ret) { |
| 303 | PERROR("close"); |
| 304 | } |
| 305 | } |
| 306 | if (stream->shm_fd >= 0 && stream->wait_fd != stream->shm_fd) { |
| 307 | ret = close(stream->shm_fd); |
| 308 | if (ret) { |
| 309 | PERROR("close"); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | /* Check and cleanup relayd */ |
| 314 | rcu_read_lock(); |
| 315 | relayd = consumer_find_relayd(stream->net_seq_idx); |
| 316 | if (relayd != NULL) { |
| 317 | uatomic_dec(&relayd->refcount); |
| 318 | assert(uatomic_read(&relayd->refcount) >= 0); |
| 319 | |
| 320 | /* Closing streams requires to lock the control socket. */ |
| 321 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); |
| 322 | ret = relayd_send_close_stream(&relayd->control_sock, |
| 323 | stream->relayd_stream_id, |
| 324 | stream->next_net_seq_num - 1); |
| 325 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
| 326 | if (ret < 0) { |
| 327 | DBG("Unable to close stream on the relayd. Continuing"); |
| 328 | /* |
| 329 | * Continue here. There is nothing we can do for the relayd. |
| 330 | * Chances are that the relayd has closed the socket so we just |
| 331 | * continue cleaning up. |
| 332 | */ |
| 333 | } |
| 334 | |
| 335 | /* Both conditions are met, we destroy the relayd. */ |
| 336 | if (uatomic_read(&relayd->refcount) == 0 && |
| 337 | uatomic_read(&relayd->destroy_flag)) { |
| 338 | destroy_relayd(relayd); |
| 339 | } |
| 340 | } |
| 341 | rcu_read_unlock(); |
| 342 | |
| 343 | uatomic_dec(&stream->chan->refcount); |
| 344 | if (!uatomic_read(&stream->chan->refcount) |
| 345 | && !uatomic_read(&stream->chan->nb_init_streams)) { |
| 346 | free_chan = stream->chan; |
| 347 | } |
| 348 | |
| 349 | end: |
| 350 | consumer_data.need_update = 1; |
| 351 | pthread_mutex_unlock(&consumer_data.lock); |
| 352 | |
| 353 | if (free_chan) { |
| 354 | consumer_del_channel(free_chan); |
| 355 | } |
| 356 | |
| 357 | free_stream: |
| 358 | call_rcu(&stream->node.head, consumer_free_stream); |
| 359 | } |
| 360 | |
| 361 | struct lttng_consumer_stream *consumer_allocate_stream( |
| 362 | int channel_key, int stream_key, |
| 363 | int shm_fd, int wait_fd, |
| 364 | enum lttng_consumer_stream_state state, |
| 365 | uint64_t mmap_len, |
| 366 | enum lttng_event_output output, |
| 367 | const char *path_name, |
| 368 | uid_t uid, |
| 369 | gid_t gid, |
| 370 | int net_index, |
| 371 | int metadata_flag, |
| 372 | int *alloc_ret) |
| 373 | { |
| 374 | struct lttng_consumer_stream *stream; |
| 375 | |
| 376 | stream = zmalloc(sizeof(*stream)); |
| 377 | if (stream == NULL) { |
| 378 | PERROR("malloc struct lttng_consumer_stream"); |
| 379 | *alloc_ret = -ENOMEM; |
| 380 | goto end; |
| 381 | } |
| 382 | |
| 383 | /* |
| 384 | * Get stream's channel reference. Needed when adding the stream to the |
| 385 | * global hash table. |
| 386 | */ |
| 387 | stream->chan = consumer_find_channel(channel_key); |
| 388 | if (!stream->chan) { |
| 389 | *alloc_ret = -ENOENT; |
| 390 | ERR("Unable to find channel for stream %d", stream_key); |
| 391 | goto error; |
| 392 | } |
| 393 | |
| 394 | stream->key = stream_key; |
| 395 | stream->shm_fd = shm_fd; |
| 396 | stream->wait_fd = wait_fd; |
| 397 | stream->out_fd = -1; |
| 398 | stream->out_fd_offset = 0; |
| 399 | stream->state = state; |
| 400 | stream->mmap_len = mmap_len; |
| 401 | stream->mmap_base = NULL; |
| 402 | stream->output = output; |
| 403 | stream->uid = uid; |
| 404 | stream->gid = gid; |
| 405 | stream->net_seq_idx = net_index; |
| 406 | stream->metadata_flag = metadata_flag; |
| 407 | strncpy(stream->path_name, path_name, sizeof(stream->path_name)); |
| 408 | stream->path_name[sizeof(stream->path_name) - 1] = '\0'; |
| 409 | lttng_ht_node_init_ulong(&stream->waitfd_node, stream->wait_fd); |
| 410 | lttng_ht_node_init_ulong(&stream->node, stream->key); |
| 411 | |
| 412 | DBG3("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu," |
| 413 | " out_fd %d, net_seq_idx %d)", stream->path_name, stream->key, |
| 414 | stream->shm_fd, stream->wait_fd, |
| 415 | (unsigned long long) stream->mmap_len, stream->out_fd, |
| 416 | stream->net_seq_idx); |
| 417 | return stream; |
| 418 | |
| 419 | error: |
| 420 | free(stream); |
| 421 | end: |
| 422 | return NULL; |
| 423 | } |
| 424 | |
| 425 | /* |
| 426 | * Add a stream to the global list protected by a mutex. |
| 427 | */ |
| 428 | int consumer_add_stream(struct lttng_consumer_stream *stream) |
| 429 | { |
| 430 | int ret = 0; |
| 431 | struct consumer_relayd_sock_pair *relayd; |
| 432 | |
| 433 | assert(stream); |
| 434 | |
| 435 | DBG3("Adding consumer stream %d", stream->key); |
| 436 | |
| 437 | pthread_mutex_lock(&consumer_data.lock); |
| 438 | rcu_read_lock(); |
| 439 | |
| 440 | switch (consumer_data.type) { |
| 441 | case LTTNG_CONSUMER_KERNEL: |
| 442 | break; |
| 443 | case LTTNG_CONSUMER32_UST: |
| 444 | case LTTNG_CONSUMER64_UST: |
| 445 | stream->cpu = stream->chan->cpucount++; |
| 446 | ret = lttng_ustconsumer_add_stream(stream); |
| 447 | if (ret) { |
| 448 | ret = -EINVAL; |
| 449 | goto error; |
| 450 | } |
| 451 | |
| 452 | /* Steal stream identifier only for UST */ |
| 453 | consumer_steal_stream_key(stream->key, consumer_data.stream_ht); |
| 454 | break; |
| 455 | default: |
| 456 | ERR("Unknown consumer_data type"); |
| 457 | assert(0); |
| 458 | ret = -ENOSYS; |
| 459 | goto error; |
| 460 | } |
| 461 | |
| 462 | lttng_ht_add_unique_ulong(consumer_data.stream_ht, &stream->node); |
| 463 | |
| 464 | /* Check and cleanup relayd */ |
| 465 | relayd = consumer_find_relayd(stream->net_seq_idx); |
| 466 | if (relayd != NULL) { |
| 467 | uatomic_inc(&relayd->refcount); |
| 468 | } |
| 469 | |
| 470 | /* Update channel refcount once added without error(s). */ |
| 471 | uatomic_inc(&stream->chan->refcount); |
| 472 | |
| 473 | /* |
| 474 | * When nb_init_streams reaches 0, we don't need to trigger any action in |
| 475 | * terms of destroying the associated channel, because the action that |
| 476 | * causes the count to become 0 also causes a stream to be added. The |
| 477 | * channel deletion will thus be triggered by the following removal of this |
| 478 | * stream. |
| 479 | */ |
| 480 | if (uatomic_read(&stream->chan->nb_init_streams) > 0) { |
| 481 | uatomic_dec(&stream->chan->nb_init_streams); |
| 482 | } |
| 483 | |
| 484 | /* Update consumer data once the node is inserted. */ |
| 485 | consumer_data.stream_count++; |
| 486 | consumer_data.need_update = 1; |
| 487 | |
| 488 | error: |
| 489 | rcu_read_unlock(); |
| 490 | pthread_mutex_unlock(&consumer_data.lock); |
| 491 | |
| 492 | return ret; |
| 493 | } |
| 494 | |
| 495 | /* |
| 496 | * Add relayd socket to global consumer data hashtable. RCU read side lock MUST |
| 497 | * be acquired before calling this. |
| 498 | */ |
| 499 | static int add_relayd(struct consumer_relayd_sock_pair *relayd) |
| 500 | { |
| 501 | int ret = 0; |
| 502 | struct lttng_ht_node_ulong *node; |
| 503 | struct lttng_ht_iter iter; |
| 504 | |
| 505 | if (relayd == NULL) { |
| 506 | ret = -1; |
| 507 | goto end; |
| 508 | } |
| 509 | |
| 510 | lttng_ht_lookup(consumer_data.relayd_ht, |
| 511 | (void *)((unsigned long) relayd->net_seq_idx), &iter); |
| 512 | node = lttng_ht_iter_get_node_ulong(&iter); |
| 513 | if (node != NULL) { |
| 514 | /* Relayd already exist. Ignore the insertion */ |
| 515 | goto end; |
| 516 | } |
| 517 | lttng_ht_add_unique_ulong(consumer_data.relayd_ht, &relayd->node); |
| 518 | |
| 519 | end: |
| 520 | return ret; |
| 521 | } |
| 522 | |
| 523 | /* |
| 524 | * Allocate and return a consumer relayd socket. |
| 525 | */ |
| 526 | struct consumer_relayd_sock_pair *consumer_allocate_relayd_sock_pair( |
| 527 | int net_seq_idx) |
| 528 | { |
| 529 | struct consumer_relayd_sock_pair *obj = NULL; |
| 530 | |
| 531 | /* Negative net sequence index is a failure */ |
| 532 | if (net_seq_idx < 0) { |
| 533 | goto error; |
| 534 | } |
| 535 | |
| 536 | obj = zmalloc(sizeof(struct consumer_relayd_sock_pair)); |
| 537 | if (obj == NULL) { |
| 538 | PERROR("zmalloc relayd sock"); |
| 539 | goto error; |
| 540 | } |
| 541 | |
| 542 | obj->net_seq_idx = net_seq_idx; |
| 543 | obj->refcount = 0; |
| 544 | obj->destroy_flag = 0; |
| 545 | lttng_ht_node_init_ulong(&obj->node, obj->net_seq_idx); |
| 546 | pthread_mutex_init(&obj->ctrl_sock_mutex, NULL); |
| 547 | |
| 548 | error: |
| 549 | return obj; |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | * Find a relayd socket pair in the global consumer data. |
| 554 | * |
| 555 | * Return the object if found else NULL. |
| 556 | * RCU read-side lock must be held across this call and while using the |
| 557 | * returned object. |
| 558 | */ |
| 559 | struct consumer_relayd_sock_pair *consumer_find_relayd(int key) |
| 560 | { |
| 561 | struct lttng_ht_iter iter; |
| 562 | struct lttng_ht_node_ulong *node; |
| 563 | struct consumer_relayd_sock_pair *relayd = NULL; |
| 564 | |
| 565 | /* Negative keys are lookup failures */ |
| 566 | if (key < 0) { |
| 567 | goto error; |
| 568 | } |
| 569 | |
| 570 | lttng_ht_lookup(consumer_data.relayd_ht, (void *)((unsigned long) key), |
| 571 | &iter); |
| 572 | node = lttng_ht_iter_get_node_ulong(&iter); |
| 573 | if (node != NULL) { |
| 574 | relayd = caa_container_of(node, struct consumer_relayd_sock_pair, node); |
| 575 | } |
| 576 | |
| 577 | error: |
| 578 | return relayd; |
| 579 | } |
| 580 | |
| 581 | /* |
| 582 | * Handle stream for relayd transmission if the stream applies for network |
| 583 | * streaming where the net sequence index is set. |
| 584 | * |
| 585 | * Return destination file descriptor or negative value on error. |
| 586 | */ |
| 587 | static int write_relayd_stream_header(struct lttng_consumer_stream *stream, |
| 588 | size_t data_size, unsigned long padding, |
| 589 | struct consumer_relayd_sock_pair *relayd) |
| 590 | { |
| 591 | int outfd = -1, ret; |
| 592 | struct lttcomm_relayd_data_hdr data_hdr; |
| 593 | |
| 594 | /* Safety net */ |
| 595 | assert(stream); |
| 596 | assert(relayd); |
| 597 | |
| 598 | /* Reset data header */ |
| 599 | memset(&data_hdr, 0, sizeof(data_hdr)); |
| 600 | |
| 601 | if (stream->metadata_flag) { |
| 602 | /* Caller MUST acquire the relayd control socket lock */ |
| 603 | ret = relayd_send_metadata(&relayd->control_sock, data_size); |
| 604 | if (ret < 0) { |
| 605 | goto error; |
| 606 | } |
| 607 | |
| 608 | /* Metadata are always sent on the control socket. */ |
| 609 | outfd = relayd->control_sock.fd; |
| 610 | } else { |
| 611 | /* Set header with stream information */ |
| 612 | data_hdr.stream_id = htobe64(stream->relayd_stream_id); |
| 613 | data_hdr.data_size = htobe32(data_size); |
| 614 | data_hdr.padding_size = htobe32(padding); |
| 615 | data_hdr.net_seq_num = htobe64(stream->next_net_seq_num++); |
| 616 | /* Other fields are zeroed previously */ |
| 617 | |
| 618 | ret = relayd_send_data_hdr(&relayd->data_sock, &data_hdr, |
| 619 | sizeof(data_hdr)); |
| 620 | if (ret < 0) { |
| 621 | goto error; |
| 622 | } |
| 623 | |
| 624 | /* Set to go on data socket */ |
| 625 | outfd = relayd->data_sock.fd; |
| 626 | } |
| 627 | |
| 628 | error: |
| 629 | return outfd; |
| 630 | } |
| 631 | |
| 632 | /* |
| 633 | * Update a stream according to what we just received. |
| 634 | */ |
| 635 | void consumer_change_stream_state(int stream_key, |
| 636 | enum lttng_consumer_stream_state state) |
| 637 | { |
| 638 | struct lttng_consumer_stream *stream; |
| 639 | |
| 640 | pthread_mutex_lock(&consumer_data.lock); |
| 641 | stream = consumer_find_stream(stream_key, consumer_data.stream_ht); |
| 642 | if (stream) { |
| 643 | stream->state = state; |
| 644 | } |
| 645 | consumer_data.need_update = 1; |
| 646 | pthread_mutex_unlock(&consumer_data.lock); |
| 647 | } |
| 648 | |
| 649 | static |
| 650 | void consumer_free_channel(struct rcu_head *head) |
| 651 | { |
| 652 | struct lttng_ht_node_ulong *node = |
| 653 | caa_container_of(head, struct lttng_ht_node_ulong, head); |
| 654 | struct lttng_consumer_channel *channel = |
| 655 | caa_container_of(node, struct lttng_consumer_channel, node); |
| 656 | |
| 657 | free(channel); |
| 658 | } |
| 659 | |
| 660 | /* |
| 661 | * Remove a channel from the global list protected by a mutex. This |
| 662 | * function is also responsible for freeing its data structures. |
| 663 | */ |
| 664 | void consumer_del_channel(struct lttng_consumer_channel *channel) |
| 665 | { |
| 666 | int ret; |
| 667 | struct lttng_ht_iter iter; |
| 668 | |
| 669 | pthread_mutex_lock(&consumer_data.lock); |
| 670 | |
| 671 | switch (consumer_data.type) { |
| 672 | case LTTNG_CONSUMER_KERNEL: |
| 673 | break; |
| 674 | case LTTNG_CONSUMER32_UST: |
| 675 | case LTTNG_CONSUMER64_UST: |
| 676 | lttng_ustconsumer_del_channel(channel); |
| 677 | break; |
| 678 | default: |
| 679 | ERR("Unknown consumer_data type"); |
| 680 | assert(0); |
| 681 | goto end; |
| 682 | } |
| 683 | |
| 684 | rcu_read_lock(); |
| 685 | iter.iter.node = &channel->node.node; |
| 686 | ret = lttng_ht_del(consumer_data.channel_ht, &iter); |
| 687 | assert(!ret); |
| 688 | rcu_read_unlock(); |
| 689 | |
| 690 | if (channel->mmap_base != NULL) { |
| 691 | ret = munmap(channel->mmap_base, channel->mmap_len); |
| 692 | if (ret != 0) { |
| 693 | PERROR("munmap"); |
| 694 | } |
| 695 | } |
| 696 | if (channel->wait_fd >= 0 && !channel->wait_fd_is_copy) { |
| 697 | ret = close(channel->wait_fd); |
| 698 | if (ret) { |
| 699 | PERROR("close"); |
| 700 | } |
| 701 | } |
| 702 | if (channel->shm_fd >= 0 && channel->wait_fd != channel->shm_fd) { |
| 703 | ret = close(channel->shm_fd); |
| 704 | if (ret) { |
| 705 | PERROR("close"); |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | call_rcu(&channel->node.head, consumer_free_channel); |
| 710 | end: |
| 711 | pthread_mutex_unlock(&consumer_data.lock); |
| 712 | } |
| 713 | |
| 714 | struct lttng_consumer_channel *consumer_allocate_channel( |
| 715 | int channel_key, |
| 716 | int shm_fd, int wait_fd, |
| 717 | uint64_t mmap_len, |
| 718 | uint64_t max_sb_size, |
| 719 | unsigned int nb_init_streams) |
| 720 | { |
| 721 | struct lttng_consumer_channel *channel; |
| 722 | int ret; |
| 723 | |
| 724 | channel = zmalloc(sizeof(*channel)); |
| 725 | if (channel == NULL) { |
| 726 | PERROR("malloc struct lttng_consumer_channel"); |
| 727 | goto end; |
| 728 | } |
| 729 | channel->key = channel_key; |
| 730 | channel->shm_fd = shm_fd; |
| 731 | channel->wait_fd = wait_fd; |
| 732 | channel->mmap_len = mmap_len; |
| 733 | channel->max_sb_size = max_sb_size; |
| 734 | channel->refcount = 0; |
| 735 | channel->nb_init_streams = nb_init_streams; |
| 736 | lttng_ht_node_init_ulong(&channel->node, channel->key); |
| 737 | |
| 738 | switch (consumer_data.type) { |
| 739 | case LTTNG_CONSUMER_KERNEL: |
| 740 | channel->mmap_base = NULL; |
| 741 | channel->mmap_len = 0; |
| 742 | break; |
| 743 | case LTTNG_CONSUMER32_UST: |
| 744 | case LTTNG_CONSUMER64_UST: |
| 745 | ret = lttng_ustconsumer_allocate_channel(channel); |
| 746 | if (ret) { |
| 747 | free(channel); |
| 748 | return NULL; |
| 749 | } |
| 750 | break; |
| 751 | default: |
| 752 | ERR("Unknown consumer_data type"); |
| 753 | assert(0); |
| 754 | goto end; |
| 755 | } |
| 756 | DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)", |
| 757 | channel->key, channel->shm_fd, channel->wait_fd, |
| 758 | (unsigned long long) channel->mmap_len, |
| 759 | (unsigned long long) channel->max_sb_size); |
| 760 | end: |
| 761 | return channel; |
| 762 | } |
| 763 | |
| 764 | /* |
| 765 | * Add a channel to the global list protected by a mutex. |
| 766 | */ |
| 767 | int consumer_add_channel(struct lttng_consumer_channel *channel) |
| 768 | { |
| 769 | struct lttng_ht_node_ulong *node; |
| 770 | struct lttng_ht_iter iter; |
| 771 | |
| 772 | pthread_mutex_lock(&consumer_data.lock); |
| 773 | /* Steal channel identifier, for UST */ |
| 774 | consumer_steal_channel_key(channel->key); |
| 775 | rcu_read_lock(); |
| 776 | |
| 777 | lttng_ht_lookup(consumer_data.channel_ht, |
| 778 | (void *)((unsigned long) channel->key), &iter); |
| 779 | node = lttng_ht_iter_get_node_ulong(&iter); |
| 780 | if (node != NULL) { |
| 781 | /* Channel already exist. Ignore the insertion */ |
| 782 | goto end; |
| 783 | } |
| 784 | |
| 785 | lttng_ht_add_unique_ulong(consumer_data.channel_ht, &channel->node); |
| 786 | |
| 787 | end: |
| 788 | rcu_read_unlock(); |
| 789 | pthread_mutex_unlock(&consumer_data.lock); |
| 790 | |
| 791 | return 0; |
| 792 | } |
| 793 | |
| 794 | /* |
| 795 | * Allocate the pollfd structure and the local view of the out fds to avoid |
| 796 | * doing a lookup in the linked list and concurrency issues when writing is |
| 797 | * needed. Called with consumer_data.lock held. |
| 798 | * |
| 799 | * Returns the number of fds in the structures. |
| 800 | */ |
| 801 | int consumer_update_poll_array( |
| 802 | struct lttng_consumer_local_data *ctx, struct pollfd **pollfd, |
| 803 | struct lttng_consumer_stream **local_stream) |
| 804 | { |
| 805 | int i = 0; |
| 806 | struct lttng_ht_iter iter; |
| 807 | struct lttng_consumer_stream *stream; |
| 808 | |
| 809 | DBG("Updating poll fd array"); |
| 810 | rcu_read_lock(); |
| 811 | cds_lfht_for_each_entry(consumer_data.stream_ht->ht, &iter.iter, stream, |
| 812 | node.node) { |
| 813 | if (stream->state != LTTNG_CONSUMER_ACTIVE_STREAM) { |
| 814 | continue; |
| 815 | } |
| 816 | DBG("Active FD %d", stream->wait_fd); |
| 817 | (*pollfd)[i].fd = stream->wait_fd; |
| 818 | (*pollfd)[i].events = POLLIN | POLLPRI; |
| 819 | local_stream[i] = stream; |
| 820 | i++; |
| 821 | } |
| 822 | rcu_read_unlock(); |
| 823 | |
| 824 | /* |
| 825 | * Insert the consumer_poll_pipe at the end of the array and don't |
| 826 | * increment i so nb_fd is the number of real FD. |
| 827 | */ |
| 828 | (*pollfd)[i].fd = ctx->consumer_poll_pipe[0]; |
| 829 | (*pollfd)[i].events = POLLIN | POLLPRI; |
| 830 | return i; |
| 831 | } |
| 832 | |
| 833 | /* |
| 834 | * Poll on the should_quit pipe and the command socket return -1 on error and |
| 835 | * should exit, 0 if data is available on the command socket |
| 836 | */ |
| 837 | int lttng_consumer_poll_socket(struct pollfd *consumer_sockpoll) |
| 838 | { |
| 839 | int num_rdy; |
| 840 | |
| 841 | restart: |
| 842 | num_rdy = poll(consumer_sockpoll, 2, -1); |
| 843 | if (num_rdy == -1) { |
| 844 | /* |
| 845 | * Restart interrupted system call. |
| 846 | */ |
| 847 | if (errno == EINTR) { |
| 848 | goto restart; |
| 849 | } |
| 850 | PERROR("Poll error"); |
| 851 | goto exit; |
| 852 | } |
| 853 | if (consumer_sockpoll[0].revents & (POLLIN | POLLPRI)) { |
| 854 | DBG("consumer_should_quit wake up"); |
| 855 | goto exit; |
| 856 | } |
| 857 | return 0; |
| 858 | |
| 859 | exit: |
| 860 | return -1; |
| 861 | } |
| 862 | |
| 863 | /* |
| 864 | * Set the error socket. |
| 865 | */ |
| 866 | void lttng_consumer_set_error_sock( |
| 867 | struct lttng_consumer_local_data *ctx, int sock) |
| 868 | { |
| 869 | ctx->consumer_error_socket = sock; |
| 870 | } |
| 871 | |
| 872 | /* |
| 873 | * Set the command socket path. |
| 874 | */ |
| 875 | void lttng_consumer_set_command_sock_path( |
| 876 | struct lttng_consumer_local_data *ctx, char *sock) |
| 877 | { |
| 878 | ctx->consumer_command_sock_path = sock; |
| 879 | } |
| 880 | |
| 881 | /* |
| 882 | * Send return code to the session daemon. |
| 883 | * If the socket is not defined, we return 0, it is not a fatal error |
| 884 | */ |
| 885 | int lttng_consumer_send_error( |
| 886 | struct lttng_consumer_local_data *ctx, int cmd) |
| 887 | { |
| 888 | if (ctx->consumer_error_socket > 0) { |
| 889 | return lttcomm_send_unix_sock(ctx->consumer_error_socket, &cmd, |
| 890 | sizeof(enum lttcomm_sessiond_command)); |
| 891 | } |
| 892 | |
| 893 | return 0; |
| 894 | } |
| 895 | |
| 896 | /* |
| 897 | * Close all the tracefiles and stream fds, should be called when all instances |
| 898 | * are destroyed. |
| 899 | */ |
| 900 | void lttng_consumer_cleanup(void) |
| 901 | { |
| 902 | struct lttng_ht_iter iter; |
| 903 | struct lttng_ht_node_ulong *node; |
| 904 | |
| 905 | rcu_read_lock(); |
| 906 | |
| 907 | /* |
| 908 | * close all outfd. Called when there are no more threads running (after |
| 909 | * joining on the threads), no need to protect list iteration with mutex. |
| 910 | */ |
| 911 | cds_lfht_for_each_entry(consumer_data.stream_ht->ht, &iter.iter, node, |
| 912 | node) { |
| 913 | struct lttng_consumer_stream *stream = |
| 914 | caa_container_of(node, struct lttng_consumer_stream, node); |
| 915 | consumer_del_stream(stream, consumer_data.stream_ht); |
| 916 | } |
| 917 | |
| 918 | cds_lfht_for_each_entry(consumer_data.channel_ht->ht, &iter.iter, node, |
| 919 | node) { |
| 920 | struct lttng_consumer_channel *channel = |
| 921 | caa_container_of(node, struct lttng_consumer_channel, node); |
| 922 | consumer_del_channel(channel); |
| 923 | } |
| 924 | |
| 925 | rcu_read_unlock(); |
| 926 | |
| 927 | lttng_ht_destroy(consumer_data.stream_ht); |
| 928 | lttng_ht_destroy(consumer_data.channel_ht); |
| 929 | } |
| 930 | |
| 931 | /* |
| 932 | * Called from signal handler. |
| 933 | */ |
| 934 | void lttng_consumer_should_exit(struct lttng_consumer_local_data *ctx) |
| 935 | { |
| 936 | int ret; |
| 937 | consumer_quit = 1; |
| 938 | do { |
| 939 | ret = write(ctx->consumer_should_quit[1], "4", 1); |
| 940 | } while (ret < 0 && errno == EINTR); |
| 941 | if (ret < 0) { |
| 942 | PERROR("write consumer quit"); |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | void lttng_consumer_sync_trace_file(struct lttng_consumer_stream *stream, |
| 947 | off_t orig_offset) |
| 948 | { |
| 949 | int outfd = stream->out_fd; |
| 950 | |
| 951 | /* |
| 952 | * This does a blocking write-and-wait on any page that belongs to the |
| 953 | * subbuffer prior to the one we just wrote. |
| 954 | * Don't care about error values, as these are just hints and ways to |
| 955 | * limit the amount of page cache used. |
| 956 | */ |
| 957 | if (orig_offset < stream->chan->max_sb_size) { |
| 958 | return; |
| 959 | } |
| 960 | lttng_sync_file_range(outfd, orig_offset - stream->chan->max_sb_size, |
| 961 | stream->chan->max_sb_size, |
| 962 | SYNC_FILE_RANGE_WAIT_BEFORE |
| 963 | | SYNC_FILE_RANGE_WRITE |
| 964 | | SYNC_FILE_RANGE_WAIT_AFTER); |
| 965 | /* |
| 966 | * Give hints to the kernel about how we access the file: |
| 967 | * POSIX_FADV_DONTNEED : we won't re-access data in a near future after |
| 968 | * we write it. |
| 969 | * |
| 970 | * We need to call fadvise again after the file grows because the |
| 971 | * kernel does not seem to apply fadvise to non-existing parts of the |
| 972 | * file. |
| 973 | * |
| 974 | * Call fadvise _after_ having waited for the page writeback to |
| 975 | * complete because the dirty page writeback semantic is not well |
| 976 | * defined. So it can be expected to lead to lower throughput in |
| 977 | * streaming. |
| 978 | */ |
| 979 | posix_fadvise(outfd, orig_offset - stream->chan->max_sb_size, |
| 980 | stream->chan->max_sb_size, POSIX_FADV_DONTNEED); |
| 981 | } |
| 982 | |
| 983 | /* |
| 984 | * Initialise the necessary environnement : |
| 985 | * - create a new context |
| 986 | * - create the poll_pipe |
| 987 | * - create the should_quit pipe (for signal handler) |
| 988 | * - create the thread pipe (for splice) |
| 989 | * |
| 990 | * Takes a function pointer as argument, this function is called when data is |
| 991 | * available on a buffer. This function is responsible to do the |
| 992 | * kernctl_get_next_subbuf, read the data with mmap or splice depending on the |
| 993 | * buffer configuration and then kernctl_put_next_subbuf at the end. |
| 994 | * |
| 995 | * Returns a pointer to the new context or NULL on error. |
| 996 | */ |
| 997 | struct lttng_consumer_local_data *lttng_consumer_create( |
| 998 | enum lttng_consumer_type type, |
| 999 | ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream, |
| 1000 | struct lttng_consumer_local_data *ctx), |
| 1001 | int (*recv_channel)(struct lttng_consumer_channel *channel), |
| 1002 | int (*recv_stream)(struct lttng_consumer_stream *stream), |
| 1003 | int (*update_stream)(int stream_key, uint32_t state)) |
| 1004 | { |
| 1005 | int ret, i; |
| 1006 | struct lttng_consumer_local_data *ctx; |
| 1007 | |
| 1008 | assert(consumer_data.type == LTTNG_CONSUMER_UNKNOWN || |
| 1009 | consumer_data.type == type); |
| 1010 | consumer_data.type = type; |
| 1011 | |
| 1012 | ctx = zmalloc(sizeof(struct lttng_consumer_local_data)); |
| 1013 | if (ctx == NULL) { |
| 1014 | PERROR("allocating context"); |
| 1015 | goto error; |
| 1016 | } |
| 1017 | |
| 1018 | ctx->consumer_error_socket = -1; |
| 1019 | /* assign the callbacks */ |
| 1020 | ctx->on_buffer_ready = buffer_ready; |
| 1021 | ctx->on_recv_channel = recv_channel; |
| 1022 | ctx->on_recv_stream = recv_stream; |
| 1023 | ctx->on_update_stream = update_stream; |
| 1024 | |
| 1025 | ret = pipe(ctx->consumer_poll_pipe); |
| 1026 | if (ret < 0) { |
| 1027 | PERROR("Error creating poll pipe"); |
| 1028 | goto error_poll_pipe; |
| 1029 | } |
| 1030 | |
| 1031 | /* set read end of the pipe to non-blocking */ |
| 1032 | ret = fcntl(ctx->consumer_poll_pipe[0], F_SETFL, O_NONBLOCK); |
| 1033 | if (ret < 0) { |
| 1034 | PERROR("fcntl O_NONBLOCK"); |
| 1035 | goto error_poll_fcntl; |
| 1036 | } |
| 1037 | |
| 1038 | /* set write end of the pipe to non-blocking */ |
| 1039 | ret = fcntl(ctx->consumer_poll_pipe[1], F_SETFL, O_NONBLOCK); |
| 1040 | if (ret < 0) { |
| 1041 | PERROR("fcntl O_NONBLOCK"); |
| 1042 | goto error_poll_fcntl; |
| 1043 | } |
| 1044 | |
| 1045 | ret = pipe(ctx->consumer_should_quit); |
| 1046 | if (ret < 0) { |
| 1047 | PERROR("Error creating recv pipe"); |
| 1048 | goto error_quit_pipe; |
| 1049 | } |
| 1050 | |
| 1051 | ret = pipe(ctx->consumer_thread_pipe); |
| 1052 | if (ret < 0) { |
| 1053 | PERROR("Error creating thread pipe"); |
| 1054 | goto error_thread_pipe; |
| 1055 | } |
| 1056 | |
| 1057 | ret = utils_create_pipe(ctx->consumer_metadata_pipe); |
| 1058 | if (ret < 0) { |
| 1059 | goto error_metadata_pipe; |
| 1060 | } |
| 1061 | |
| 1062 | ret = utils_create_pipe(ctx->consumer_splice_metadata_pipe); |
| 1063 | if (ret < 0) { |
| 1064 | goto error_splice_pipe; |
| 1065 | } |
| 1066 | |
| 1067 | return ctx; |
| 1068 | |
| 1069 | error_splice_pipe: |
| 1070 | utils_close_pipe(ctx->consumer_metadata_pipe); |
| 1071 | error_metadata_pipe: |
| 1072 | utils_close_pipe(ctx->consumer_thread_pipe); |
| 1073 | error_thread_pipe: |
| 1074 | for (i = 0; i < 2; i++) { |
| 1075 | int err; |
| 1076 | |
| 1077 | err = close(ctx->consumer_should_quit[i]); |
| 1078 | if (err) { |
| 1079 | PERROR("close"); |
| 1080 | } |
| 1081 | } |
| 1082 | error_poll_fcntl: |
| 1083 | error_quit_pipe: |
| 1084 | for (i = 0; i < 2; i++) { |
| 1085 | int err; |
| 1086 | |
| 1087 | err = close(ctx->consumer_poll_pipe[i]); |
| 1088 | if (err) { |
| 1089 | PERROR("close"); |
| 1090 | } |
| 1091 | } |
| 1092 | error_poll_pipe: |
| 1093 | free(ctx); |
| 1094 | error: |
| 1095 | return NULL; |
| 1096 | } |
| 1097 | |
| 1098 | /* |
| 1099 | * Close all fds associated with the instance and free the context. |
| 1100 | */ |
| 1101 | void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx) |
| 1102 | { |
| 1103 | int ret; |
| 1104 | |
| 1105 | ret = close(ctx->consumer_error_socket); |
| 1106 | if (ret) { |
| 1107 | PERROR("close"); |
| 1108 | } |
| 1109 | ret = close(ctx->consumer_thread_pipe[0]); |
| 1110 | if (ret) { |
| 1111 | PERROR("close"); |
| 1112 | } |
| 1113 | ret = close(ctx->consumer_thread_pipe[1]); |
| 1114 | if (ret) { |
| 1115 | PERROR("close"); |
| 1116 | } |
| 1117 | ret = close(ctx->consumer_poll_pipe[0]); |
| 1118 | if (ret) { |
| 1119 | PERROR("close"); |
| 1120 | } |
| 1121 | ret = close(ctx->consumer_poll_pipe[1]); |
| 1122 | if (ret) { |
| 1123 | PERROR("close"); |
| 1124 | } |
| 1125 | ret = close(ctx->consumer_should_quit[0]); |
| 1126 | if (ret) { |
| 1127 | PERROR("close"); |
| 1128 | } |
| 1129 | ret = close(ctx->consumer_should_quit[1]); |
| 1130 | if (ret) { |
| 1131 | PERROR("close"); |
| 1132 | } |
| 1133 | utils_close_pipe(ctx->consumer_splice_metadata_pipe); |
| 1134 | |
| 1135 | unlink(ctx->consumer_command_sock_path); |
| 1136 | free(ctx); |
| 1137 | } |
| 1138 | |
| 1139 | /* |
| 1140 | * Write the metadata stream id on the specified file descriptor. |
| 1141 | */ |
| 1142 | static int write_relayd_metadata_id(int fd, |
| 1143 | struct lttng_consumer_stream *stream, |
| 1144 | struct consumer_relayd_sock_pair *relayd, |
| 1145 | unsigned long padding) |
| 1146 | { |
| 1147 | int ret; |
| 1148 | struct lttcomm_relayd_metadata_payload hdr; |
| 1149 | |
| 1150 | hdr.stream_id = htobe64(stream->relayd_stream_id); |
| 1151 | hdr.padding_size = htobe32(padding); |
| 1152 | do { |
| 1153 | ret = write(fd, (void *) &hdr, sizeof(hdr)); |
| 1154 | } while (ret < 0 && errno == EINTR); |
| 1155 | if (ret < 0) { |
| 1156 | PERROR("write metadata stream id"); |
| 1157 | goto end; |
| 1158 | } |
| 1159 | DBG("Metadata stream id %" PRIu64 " with padding %lu written before data", |
| 1160 | stream->relayd_stream_id, padding); |
| 1161 | |
| 1162 | end: |
| 1163 | return ret; |
| 1164 | } |
| 1165 | |
| 1166 | /* |
| 1167 | * Mmap the ring buffer, read it and write the data to the tracefile. This is a |
| 1168 | * core function for writing trace buffers to either the local filesystem or |
| 1169 | * the network. |
| 1170 | * |
| 1171 | * Careful review MUST be put if any changes occur! |
| 1172 | * |
| 1173 | * Returns the number of bytes written |
| 1174 | */ |
| 1175 | ssize_t lttng_consumer_on_read_subbuffer_mmap( |
| 1176 | struct lttng_consumer_local_data *ctx, |
| 1177 | struct lttng_consumer_stream *stream, unsigned long len, |
| 1178 | unsigned long padding) |
| 1179 | { |
| 1180 | unsigned long mmap_offset; |
| 1181 | ssize_t ret = 0, written = 0; |
| 1182 | off_t orig_offset = stream->out_fd_offset; |
| 1183 | /* Default is on the disk */ |
| 1184 | int outfd = stream->out_fd; |
| 1185 | struct consumer_relayd_sock_pair *relayd = NULL; |
| 1186 | |
| 1187 | /* RCU lock for the relayd pointer */ |
| 1188 | rcu_read_lock(); |
| 1189 | |
| 1190 | /* Flag that the current stream if set for network streaming. */ |
| 1191 | if (stream->net_seq_idx != -1) { |
| 1192 | relayd = consumer_find_relayd(stream->net_seq_idx); |
| 1193 | if (relayd == NULL) { |
| 1194 | goto end; |
| 1195 | } |
| 1196 | } |
| 1197 | |
| 1198 | /* get the offset inside the fd to mmap */ |
| 1199 | switch (consumer_data.type) { |
| 1200 | case LTTNG_CONSUMER_KERNEL: |
| 1201 | ret = kernctl_get_mmap_read_offset(stream->wait_fd, &mmap_offset); |
| 1202 | break; |
| 1203 | case LTTNG_CONSUMER32_UST: |
| 1204 | case LTTNG_CONSUMER64_UST: |
| 1205 | ret = lttng_ustctl_get_mmap_read_offset(stream->chan->handle, |
| 1206 | stream->buf, &mmap_offset); |
| 1207 | break; |
| 1208 | default: |
| 1209 | ERR("Unknown consumer_data type"); |
| 1210 | assert(0); |
| 1211 | } |
| 1212 | if (ret != 0) { |
| 1213 | errno = -ret; |
| 1214 | PERROR("tracer ctl get_mmap_read_offset"); |
| 1215 | written = ret; |
| 1216 | goto end; |
| 1217 | } |
| 1218 | |
| 1219 | /* Handle stream on the relayd if the output is on the network */ |
| 1220 | if (relayd) { |
| 1221 | unsigned long netlen = len; |
| 1222 | |
| 1223 | /* |
| 1224 | * Lock the control socket for the complete duration of the function |
| 1225 | * since from this point on we will use the socket. |
| 1226 | */ |
| 1227 | if (stream->metadata_flag) { |
| 1228 | /* Metadata requires the control socket. */ |
| 1229 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); |
| 1230 | netlen += sizeof(struct lttcomm_relayd_metadata_payload); |
| 1231 | } |
| 1232 | |
| 1233 | ret = write_relayd_stream_header(stream, netlen, padding, relayd); |
| 1234 | if (ret >= 0) { |
| 1235 | /* Use the returned socket. */ |
| 1236 | outfd = ret; |
| 1237 | |
| 1238 | /* Write metadata stream id before payload */ |
| 1239 | if (stream->metadata_flag) { |
| 1240 | ret = write_relayd_metadata_id(outfd, stream, relayd, padding); |
| 1241 | if (ret < 0) { |
| 1242 | written = ret; |
| 1243 | goto end; |
| 1244 | } |
| 1245 | } |
| 1246 | } |
| 1247 | /* Else, use the default set before which is the filesystem. */ |
| 1248 | } else { |
| 1249 | /* No streaming, we have to set the len with the full padding */ |
| 1250 | len += padding; |
| 1251 | } |
| 1252 | |
| 1253 | while (len > 0) { |
| 1254 | do { |
| 1255 | ret = write(outfd, stream->mmap_base + mmap_offset, len); |
| 1256 | } while (ret < 0 && errno == EINTR); |
| 1257 | DBG("Consumer mmap write() ret %zd (len %lu)", ret, len); |
| 1258 | if (ret < 0) { |
| 1259 | PERROR("Error in file write"); |
| 1260 | if (written == 0) { |
| 1261 | written = ret; |
| 1262 | } |
| 1263 | goto end; |
| 1264 | } else if (ret > len) { |
| 1265 | PERROR("Error in file write (ret %zd > len %lu)", ret, len); |
| 1266 | written += ret; |
| 1267 | goto end; |
| 1268 | } else { |
| 1269 | len -= ret; |
| 1270 | mmap_offset += ret; |
| 1271 | } |
| 1272 | |
| 1273 | /* This call is useless on a socket so better save a syscall. */ |
| 1274 | if (!relayd) { |
| 1275 | /* This won't block, but will start writeout asynchronously */ |
| 1276 | lttng_sync_file_range(outfd, stream->out_fd_offset, ret, |
| 1277 | SYNC_FILE_RANGE_WRITE); |
| 1278 | stream->out_fd_offset += ret; |
| 1279 | } |
| 1280 | written += ret; |
| 1281 | } |
| 1282 | lttng_consumer_sync_trace_file(stream, orig_offset); |
| 1283 | |
| 1284 | end: |
| 1285 | /* Unlock only if ctrl socket used */ |
| 1286 | if (relayd && stream->metadata_flag) { |
| 1287 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
| 1288 | } |
| 1289 | |
| 1290 | rcu_read_unlock(); |
| 1291 | return written; |
| 1292 | } |
| 1293 | |
| 1294 | /* |
| 1295 | * Splice the data from the ring buffer to the tracefile. |
| 1296 | * |
| 1297 | * Returns the number of bytes spliced. |
| 1298 | */ |
| 1299 | ssize_t lttng_consumer_on_read_subbuffer_splice( |
| 1300 | struct lttng_consumer_local_data *ctx, |
| 1301 | struct lttng_consumer_stream *stream, unsigned long len, |
| 1302 | unsigned long padding) |
| 1303 | { |
| 1304 | ssize_t ret = 0, written = 0, ret_splice = 0; |
| 1305 | loff_t offset = 0; |
| 1306 | off_t orig_offset = stream->out_fd_offset; |
| 1307 | int fd = stream->wait_fd; |
| 1308 | /* Default is on the disk */ |
| 1309 | int outfd = stream->out_fd; |
| 1310 | struct consumer_relayd_sock_pair *relayd = NULL; |
| 1311 | int *splice_pipe; |
| 1312 | |
| 1313 | switch (consumer_data.type) { |
| 1314 | case LTTNG_CONSUMER_KERNEL: |
| 1315 | break; |
| 1316 | case LTTNG_CONSUMER32_UST: |
| 1317 | case LTTNG_CONSUMER64_UST: |
| 1318 | /* Not supported for user space tracing */ |
| 1319 | return -ENOSYS; |
| 1320 | default: |
| 1321 | ERR("Unknown consumer_data type"); |
| 1322 | assert(0); |
| 1323 | } |
| 1324 | |
| 1325 | /* RCU lock for the relayd pointer */ |
| 1326 | rcu_read_lock(); |
| 1327 | |
| 1328 | /* Flag that the current stream if set for network streaming. */ |
| 1329 | if (stream->net_seq_idx != -1) { |
| 1330 | relayd = consumer_find_relayd(stream->net_seq_idx); |
| 1331 | if (relayd == NULL) { |
| 1332 | goto end; |
| 1333 | } |
| 1334 | } |
| 1335 | |
| 1336 | /* |
| 1337 | * Choose right pipe for splice. Metadata and trace data are handled by |
| 1338 | * different threads hence the use of two pipes in order not to race or |
| 1339 | * corrupt the written data. |
| 1340 | */ |
| 1341 | if (stream->metadata_flag) { |
| 1342 | splice_pipe = ctx->consumer_splice_metadata_pipe; |
| 1343 | } else { |
| 1344 | splice_pipe = ctx->consumer_thread_pipe; |
| 1345 | } |
| 1346 | |
| 1347 | /* Write metadata stream id before payload */ |
| 1348 | if (relayd) { |
| 1349 | int total_len = len; |
| 1350 | |
| 1351 | if (stream->metadata_flag) { |
| 1352 | /* |
| 1353 | * Lock the control socket for the complete duration of the function |
| 1354 | * since from this point on we will use the socket. |
| 1355 | */ |
| 1356 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); |
| 1357 | |
| 1358 | ret = write_relayd_metadata_id(splice_pipe[1], stream, relayd, |
| 1359 | padding); |
| 1360 | if (ret < 0) { |
| 1361 | written = ret; |
| 1362 | goto end; |
| 1363 | } |
| 1364 | |
| 1365 | total_len += sizeof(struct lttcomm_relayd_metadata_payload); |
| 1366 | } |
| 1367 | |
| 1368 | ret = write_relayd_stream_header(stream, total_len, padding, relayd); |
| 1369 | if (ret >= 0) { |
| 1370 | /* Use the returned socket. */ |
| 1371 | outfd = ret; |
| 1372 | } else { |
| 1373 | ERR("Remote relayd disconnected. Stopping"); |
| 1374 | goto end; |
| 1375 | } |
| 1376 | } else { |
| 1377 | /* No streaming, we have to set the len with the full padding */ |
| 1378 | len += padding; |
| 1379 | } |
| 1380 | |
| 1381 | while (len > 0) { |
| 1382 | DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)", |
| 1383 | (unsigned long)offset, len, fd, splice_pipe[1]); |
| 1384 | ret_splice = splice(fd, &offset, splice_pipe[1], NULL, len, |
| 1385 | SPLICE_F_MOVE | SPLICE_F_MORE); |
| 1386 | DBG("splice chan to pipe, ret %zd", ret_splice); |
| 1387 | if (ret_splice < 0) { |
| 1388 | PERROR("Error in relay splice"); |
| 1389 | if (written == 0) { |
| 1390 | written = ret_splice; |
| 1391 | } |
| 1392 | ret = errno; |
| 1393 | goto splice_error; |
| 1394 | } |
| 1395 | |
| 1396 | /* Handle stream on the relayd if the output is on the network */ |
| 1397 | if (relayd) { |
| 1398 | if (stream->metadata_flag) { |
| 1399 | size_t metadata_payload_size = |
| 1400 | sizeof(struct lttcomm_relayd_metadata_payload); |
| 1401 | |
| 1402 | /* Update counter to fit the spliced data */ |
| 1403 | ret_splice += metadata_payload_size; |
| 1404 | len += metadata_payload_size; |
| 1405 | /* |
| 1406 | * We do this so the return value can match the len passed as |
| 1407 | * argument to this function. |
| 1408 | */ |
| 1409 | written -= metadata_payload_size; |
| 1410 | } |
| 1411 | } |
| 1412 | |
| 1413 | /* Splice data out */ |
| 1414 | ret_splice = splice(splice_pipe[0], NULL, outfd, NULL, |
| 1415 | ret_splice, SPLICE_F_MOVE | SPLICE_F_MORE); |
| 1416 | DBG("Consumer splice pipe to file, ret %zd", ret_splice); |
| 1417 | if (ret_splice < 0) { |
| 1418 | PERROR("Error in file splice"); |
| 1419 | if (written == 0) { |
| 1420 | written = ret_splice; |
| 1421 | } |
| 1422 | ret = errno; |
| 1423 | goto splice_error; |
| 1424 | } else if (ret_splice > len) { |
| 1425 | errno = EINVAL; |
| 1426 | PERROR("Wrote more data than requested %zd (len: %lu)", |
| 1427 | ret_splice, len); |
| 1428 | written += ret_splice; |
| 1429 | ret = errno; |
| 1430 | goto splice_error; |
| 1431 | } |
| 1432 | len -= ret_splice; |
| 1433 | |
| 1434 | /* This call is useless on a socket so better save a syscall. */ |
| 1435 | if (!relayd) { |
| 1436 | /* This won't block, but will start writeout asynchronously */ |
| 1437 | lttng_sync_file_range(outfd, stream->out_fd_offset, ret_splice, |
| 1438 | SYNC_FILE_RANGE_WRITE); |
| 1439 | stream->out_fd_offset += ret_splice; |
| 1440 | } |
| 1441 | written += ret_splice; |
| 1442 | } |
| 1443 | lttng_consumer_sync_trace_file(stream, orig_offset); |
| 1444 | |
| 1445 | ret = ret_splice; |
| 1446 | |
| 1447 | goto end; |
| 1448 | |
| 1449 | splice_error: |
| 1450 | /* send the appropriate error description to sessiond */ |
| 1451 | switch (ret) { |
| 1452 | case EBADF: |
| 1453 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_EBADF); |
| 1454 | break; |
| 1455 | case EINVAL: |
| 1456 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_EINVAL); |
| 1457 | break; |
| 1458 | case ENOMEM: |
| 1459 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ENOMEM); |
| 1460 | break; |
| 1461 | case ESPIPE: |
| 1462 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ESPIPE); |
| 1463 | break; |
| 1464 | } |
| 1465 | |
| 1466 | end: |
| 1467 | if (relayd && stream->metadata_flag) { |
| 1468 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
| 1469 | } |
| 1470 | |
| 1471 | rcu_read_unlock(); |
| 1472 | return written; |
| 1473 | } |
| 1474 | |
| 1475 | /* |
| 1476 | * Take a snapshot for a specific fd |
| 1477 | * |
| 1478 | * Returns 0 on success, < 0 on error |
| 1479 | */ |
| 1480 | int lttng_consumer_take_snapshot(struct lttng_consumer_local_data *ctx, |
| 1481 | struct lttng_consumer_stream *stream) |
| 1482 | { |
| 1483 | switch (consumer_data.type) { |
| 1484 | case LTTNG_CONSUMER_KERNEL: |
| 1485 | return lttng_kconsumer_take_snapshot(ctx, stream); |
| 1486 | case LTTNG_CONSUMER32_UST: |
| 1487 | case LTTNG_CONSUMER64_UST: |
| 1488 | return lttng_ustconsumer_take_snapshot(ctx, stream); |
| 1489 | default: |
| 1490 | ERR("Unknown consumer_data type"); |
| 1491 | assert(0); |
| 1492 | return -ENOSYS; |
| 1493 | } |
| 1494 | |
| 1495 | } |
| 1496 | |
| 1497 | /* |
| 1498 | * Get the produced position |
| 1499 | * |
| 1500 | * Returns 0 on success, < 0 on error |
| 1501 | */ |
| 1502 | int lttng_consumer_get_produced_snapshot( |
| 1503 | struct lttng_consumer_local_data *ctx, |
| 1504 | struct lttng_consumer_stream *stream, |
| 1505 | unsigned long *pos) |
| 1506 | { |
| 1507 | switch (consumer_data.type) { |
| 1508 | case LTTNG_CONSUMER_KERNEL: |
| 1509 | return lttng_kconsumer_get_produced_snapshot(ctx, stream, pos); |
| 1510 | case LTTNG_CONSUMER32_UST: |
| 1511 | case LTTNG_CONSUMER64_UST: |
| 1512 | return lttng_ustconsumer_get_produced_snapshot(ctx, stream, pos); |
| 1513 | default: |
| 1514 | ERR("Unknown consumer_data type"); |
| 1515 | assert(0); |
| 1516 | return -ENOSYS; |
| 1517 | } |
| 1518 | } |
| 1519 | |
| 1520 | int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx, |
| 1521 | int sock, struct pollfd *consumer_sockpoll) |
| 1522 | { |
| 1523 | switch (consumer_data.type) { |
| 1524 | case LTTNG_CONSUMER_KERNEL: |
| 1525 | return lttng_kconsumer_recv_cmd(ctx, sock, consumer_sockpoll); |
| 1526 | case LTTNG_CONSUMER32_UST: |
| 1527 | case LTTNG_CONSUMER64_UST: |
| 1528 | return lttng_ustconsumer_recv_cmd(ctx, sock, consumer_sockpoll); |
| 1529 | default: |
| 1530 | ERR("Unknown consumer_data type"); |
| 1531 | assert(0); |
| 1532 | return -ENOSYS; |
| 1533 | } |
| 1534 | } |
| 1535 | |
| 1536 | /* |
| 1537 | * Iterate over all streams of the hashtable and free them properly. |
| 1538 | * |
| 1539 | * XXX: Should not be only for metadata stream or else use an other name. |
| 1540 | */ |
| 1541 | static void destroy_stream_ht(struct lttng_ht *ht) |
| 1542 | { |
| 1543 | int ret; |
| 1544 | struct lttng_ht_iter iter; |
| 1545 | struct lttng_consumer_stream *stream; |
| 1546 | |
| 1547 | if (ht == NULL) { |
| 1548 | return; |
| 1549 | } |
| 1550 | |
| 1551 | rcu_read_lock(); |
| 1552 | cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, waitfd_node.node) { |
| 1553 | ret = lttng_ht_del(ht, &iter); |
| 1554 | assert(!ret); |
| 1555 | |
| 1556 | call_rcu(&stream->waitfd_node.head, consumer_free_metadata_stream); |
| 1557 | } |
| 1558 | rcu_read_unlock(); |
| 1559 | |
| 1560 | lttng_ht_destroy(ht); |
| 1561 | } |
| 1562 | |
| 1563 | /* |
| 1564 | * Clean up a metadata stream and free its memory. |
| 1565 | */ |
| 1566 | void consumer_del_metadata_stream(struct lttng_consumer_stream *stream, |
| 1567 | struct lttng_ht *ht) |
| 1568 | { |
| 1569 | int ret; |
| 1570 | struct lttng_ht_iter iter; |
| 1571 | struct lttng_consumer_channel *free_chan = NULL; |
| 1572 | struct consumer_relayd_sock_pair *relayd; |
| 1573 | |
| 1574 | assert(stream); |
| 1575 | /* |
| 1576 | * This call should NEVER receive regular stream. It must always be |
| 1577 | * metadata stream and this is crucial for data structure synchronization. |
| 1578 | */ |
| 1579 | assert(stream->metadata_flag); |
| 1580 | |
| 1581 | DBG3("Consumer delete metadata stream %d", stream->wait_fd); |
| 1582 | |
| 1583 | if (ht == NULL) { |
| 1584 | /* Means the stream was allocated but not successfully added */ |
| 1585 | goto free_stream; |
| 1586 | } |
| 1587 | |
| 1588 | rcu_read_lock(); |
| 1589 | iter.iter.node = &stream->waitfd_node.node; |
| 1590 | ret = lttng_ht_del(ht, &iter); |
| 1591 | assert(!ret); |
| 1592 | rcu_read_unlock(); |
| 1593 | |
| 1594 | pthread_mutex_lock(&consumer_data.lock); |
| 1595 | switch (consumer_data.type) { |
| 1596 | case LTTNG_CONSUMER_KERNEL: |
| 1597 | if (stream->mmap_base != NULL) { |
| 1598 | ret = munmap(stream->mmap_base, stream->mmap_len); |
| 1599 | if (ret != 0) { |
| 1600 | PERROR("munmap metadata stream"); |
| 1601 | } |
| 1602 | } |
| 1603 | break; |
| 1604 | case LTTNG_CONSUMER32_UST: |
| 1605 | case LTTNG_CONSUMER64_UST: |
| 1606 | lttng_ustconsumer_del_stream(stream); |
| 1607 | break; |
| 1608 | default: |
| 1609 | ERR("Unknown consumer_data type"); |
| 1610 | assert(0); |
| 1611 | goto end; |
| 1612 | } |
| 1613 | |
| 1614 | if (stream->out_fd >= 0) { |
| 1615 | ret = close(stream->out_fd); |
| 1616 | if (ret) { |
| 1617 | PERROR("close"); |
| 1618 | } |
| 1619 | } |
| 1620 | |
| 1621 | if (stream->wait_fd >= 0 && !stream->wait_fd_is_copy) { |
| 1622 | ret = close(stream->wait_fd); |
| 1623 | if (ret) { |
| 1624 | PERROR("close"); |
| 1625 | } |
| 1626 | } |
| 1627 | |
| 1628 | if (stream->shm_fd >= 0 && stream->wait_fd != stream->shm_fd) { |
| 1629 | ret = close(stream->shm_fd); |
| 1630 | if (ret) { |
| 1631 | PERROR("close"); |
| 1632 | } |
| 1633 | } |
| 1634 | |
| 1635 | /* Check and cleanup relayd */ |
| 1636 | rcu_read_lock(); |
| 1637 | relayd = consumer_find_relayd(stream->net_seq_idx); |
| 1638 | if (relayd != NULL) { |
| 1639 | uatomic_dec(&relayd->refcount); |
| 1640 | assert(uatomic_read(&relayd->refcount) >= 0); |
| 1641 | |
| 1642 | /* Closing streams requires to lock the control socket. */ |
| 1643 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); |
| 1644 | ret = relayd_send_close_stream(&relayd->control_sock, |
| 1645 | stream->relayd_stream_id, stream->next_net_seq_num - 1); |
| 1646 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
| 1647 | if (ret < 0) { |
| 1648 | DBG("Unable to close stream on the relayd. Continuing"); |
| 1649 | /* |
| 1650 | * Continue here. There is nothing we can do for the relayd. |
| 1651 | * Chances are that the relayd has closed the socket so we just |
| 1652 | * continue cleaning up. |
| 1653 | */ |
| 1654 | } |
| 1655 | |
| 1656 | /* Both conditions are met, we destroy the relayd. */ |
| 1657 | if (uatomic_read(&relayd->refcount) == 0 && |
| 1658 | uatomic_read(&relayd->destroy_flag)) { |
| 1659 | destroy_relayd(relayd); |
| 1660 | } |
| 1661 | } |
| 1662 | rcu_read_unlock(); |
| 1663 | |
| 1664 | /* Atomically decrement channel refcount since other threads can use it. */ |
| 1665 | uatomic_dec(&stream->chan->refcount); |
| 1666 | if (!uatomic_read(&stream->chan->refcount) |
| 1667 | && !uatomic_read(&stream->chan->nb_init_streams)) { |
| 1668 | /* Go for channel deletion! */ |
| 1669 | free_chan = stream->chan; |
| 1670 | } |
| 1671 | |
| 1672 | end: |
| 1673 | pthread_mutex_unlock(&consumer_data.lock); |
| 1674 | |
| 1675 | if (free_chan) { |
| 1676 | consumer_del_channel(free_chan); |
| 1677 | } |
| 1678 | |
| 1679 | free_stream: |
| 1680 | call_rcu(&stream->waitfd_node.head, consumer_free_metadata_stream); |
| 1681 | } |
| 1682 | |
| 1683 | /* |
| 1684 | * Action done with the metadata stream when adding it to the consumer internal |
| 1685 | * data structures to handle it. |
| 1686 | */ |
| 1687 | static int consumer_add_metadata_stream(struct lttng_consumer_stream *stream, |
| 1688 | struct lttng_ht *ht) |
| 1689 | { |
| 1690 | int ret = 0; |
| 1691 | struct consumer_relayd_sock_pair *relayd; |
| 1692 | |
| 1693 | assert(stream); |
| 1694 | assert(ht); |
| 1695 | |
| 1696 | DBG3("Adding metadata stream %d to hash table", stream->wait_fd); |
| 1697 | |
| 1698 | pthread_mutex_lock(&consumer_data.lock); |
| 1699 | |
| 1700 | switch (consumer_data.type) { |
| 1701 | case LTTNG_CONSUMER_KERNEL: |
| 1702 | break; |
| 1703 | case LTTNG_CONSUMER32_UST: |
| 1704 | case LTTNG_CONSUMER64_UST: |
| 1705 | ret = lttng_ustconsumer_add_stream(stream); |
| 1706 | if (ret) { |
| 1707 | ret = -EINVAL; |
| 1708 | goto error; |
| 1709 | } |
| 1710 | |
| 1711 | /* Steal stream identifier only for UST */ |
| 1712 | consumer_steal_stream_key(stream->wait_fd, ht); |
| 1713 | break; |
| 1714 | default: |
| 1715 | ERR("Unknown consumer_data type"); |
| 1716 | assert(0); |
| 1717 | ret = -ENOSYS; |
| 1718 | goto error; |
| 1719 | } |
| 1720 | |
| 1721 | /* |
| 1722 | * From here, refcounts are updated so be _careful_ when returning an error |
| 1723 | * after this point. |
| 1724 | */ |
| 1725 | |
| 1726 | rcu_read_lock(); |
| 1727 | /* Find relayd and, if one is found, increment refcount. */ |
| 1728 | relayd = consumer_find_relayd(stream->net_seq_idx); |
| 1729 | if (relayd != NULL) { |
| 1730 | uatomic_inc(&relayd->refcount); |
| 1731 | } |
| 1732 | |
| 1733 | /* Update channel refcount once added without error(s). */ |
| 1734 | uatomic_inc(&stream->chan->refcount); |
| 1735 | |
| 1736 | /* |
| 1737 | * When nb_init_streams reaches 0, we don't need to trigger any action in |
| 1738 | * terms of destroying the associated channel, because the action that |
| 1739 | * causes the count to become 0 also causes a stream to be added. The |
| 1740 | * channel deletion will thus be triggered by the following removal of this |
| 1741 | * stream. |
| 1742 | */ |
| 1743 | if (uatomic_read(&stream->chan->nb_init_streams) > 0) { |
| 1744 | uatomic_dec(&stream->chan->nb_init_streams); |
| 1745 | } |
| 1746 | |
| 1747 | lttng_ht_add_unique_ulong(ht, &stream->waitfd_node); |
| 1748 | rcu_read_unlock(); |
| 1749 | |
| 1750 | error: |
| 1751 | pthread_mutex_unlock(&consumer_data.lock); |
| 1752 | return ret; |
| 1753 | } |
| 1754 | |
| 1755 | /* |
| 1756 | * Thread polls on metadata file descriptor and write them on disk or on the |
| 1757 | * network. |
| 1758 | */ |
| 1759 | void *consumer_thread_metadata_poll(void *data) |
| 1760 | { |
| 1761 | int ret, i, pollfd; |
| 1762 | uint32_t revents, nb_fd; |
| 1763 | struct lttng_consumer_stream *stream = NULL; |
| 1764 | struct lttng_ht_iter iter; |
| 1765 | struct lttng_ht_node_ulong *node; |
| 1766 | struct lttng_ht *metadata_ht = NULL; |
| 1767 | struct lttng_poll_event events; |
| 1768 | struct lttng_consumer_local_data *ctx = data; |
| 1769 | ssize_t len; |
| 1770 | |
| 1771 | rcu_register_thread(); |
| 1772 | |
| 1773 | DBG("Thread metadata poll started"); |
| 1774 | |
| 1775 | metadata_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 1776 | if (metadata_ht == NULL) { |
| 1777 | goto end; |
| 1778 | } |
| 1779 | |
| 1780 | /* Size is set to 1 for the consumer_metadata pipe */ |
| 1781 | ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC); |
| 1782 | if (ret < 0) { |
| 1783 | ERR("Poll set creation failed"); |
| 1784 | goto end; |
| 1785 | } |
| 1786 | |
| 1787 | ret = lttng_poll_add(&events, ctx->consumer_metadata_pipe[0], LPOLLIN); |
| 1788 | if (ret < 0) { |
| 1789 | goto end; |
| 1790 | } |
| 1791 | |
| 1792 | /* Main loop */ |
| 1793 | DBG("Metadata main loop started"); |
| 1794 | |
| 1795 | while (1) { |
| 1796 | lttng_poll_reset(&events); |
| 1797 | |
| 1798 | nb_fd = LTTNG_POLL_GETNB(&events); |
| 1799 | |
| 1800 | /* Only the metadata pipe is set */ |
| 1801 | if (nb_fd == 0 && consumer_quit == 1) { |
| 1802 | goto end; |
| 1803 | } |
| 1804 | |
| 1805 | restart: |
| 1806 | DBG("Metadata poll wait with %d fd(s)", nb_fd); |
| 1807 | ret = lttng_poll_wait(&events, -1); |
| 1808 | DBG("Metadata event catched in thread"); |
| 1809 | if (ret < 0) { |
| 1810 | if (errno == EINTR) { |
| 1811 | ERR("Poll EINTR catched"); |
| 1812 | goto restart; |
| 1813 | } |
| 1814 | goto error; |
| 1815 | } |
| 1816 | |
| 1817 | /* From here, the event is a metadata wait fd */ |
| 1818 | for (i = 0; i < nb_fd; i++) { |
| 1819 | revents = LTTNG_POLL_GETEV(&events, i); |
| 1820 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 1821 | |
| 1822 | /* Just don't waste time if no returned events for the fd */ |
| 1823 | if (!revents) { |
| 1824 | continue; |
| 1825 | } |
| 1826 | |
| 1827 | if (pollfd == ctx->consumer_metadata_pipe[0]) { |
| 1828 | if (revents & (LPOLLERR | LPOLLHUP )) { |
| 1829 | DBG("Metadata thread pipe hung up"); |
| 1830 | /* |
| 1831 | * Remove the pipe from the poll set and continue the loop |
| 1832 | * since their might be data to consume. |
| 1833 | */ |
| 1834 | lttng_poll_del(&events, ctx->consumer_metadata_pipe[0]); |
| 1835 | close(ctx->consumer_metadata_pipe[0]); |
| 1836 | continue; |
| 1837 | } else if (revents & LPOLLIN) { |
| 1838 | do { |
| 1839 | /* Get the stream pointer received */ |
| 1840 | ret = read(pollfd, &stream, sizeof(stream)); |
| 1841 | } while (ret < 0 && errno == EINTR); |
| 1842 | if (ret < 0 || |
| 1843 | ret < sizeof(struct lttng_consumer_stream *)) { |
| 1844 | PERROR("read metadata stream"); |
| 1845 | /* |
| 1846 | * Let's continue here and hope we can still work |
| 1847 | * without stopping the consumer. XXX: Should we? |
| 1848 | */ |
| 1849 | continue; |
| 1850 | } |
| 1851 | |
| 1852 | DBG("Adding metadata stream %d to poll set", |
| 1853 | stream->wait_fd); |
| 1854 | |
| 1855 | ret = consumer_add_metadata_stream(stream, metadata_ht); |
| 1856 | if (ret) { |
| 1857 | ERR("Unable to add metadata stream"); |
| 1858 | /* Stream was not setup properly. Continuing. */ |
| 1859 | consumer_del_metadata_stream(stream, NULL); |
| 1860 | continue; |
| 1861 | } |
| 1862 | |
| 1863 | /* Add metadata stream to the global poll events list */ |
| 1864 | lttng_poll_add(&events, stream->wait_fd, |
| 1865 | LPOLLIN | LPOLLPRI); |
| 1866 | } |
| 1867 | |
| 1868 | /* Handle other stream */ |
| 1869 | continue; |
| 1870 | } |
| 1871 | |
| 1872 | rcu_read_lock(); |
| 1873 | lttng_ht_lookup(metadata_ht, (void *)((unsigned long) pollfd), |
| 1874 | &iter); |
| 1875 | node = lttng_ht_iter_get_node_ulong(&iter); |
| 1876 | assert(node); |
| 1877 | |
| 1878 | stream = caa_container_of(node, struct lttng_consumer_stream, |
| 1879 | waitfd_node); |
| 1880 | |
| 1881 | /* Check for error event */ |
| 1882 | if (revents & (LPOLLERR | LPOLLHUP)) { |
| 1883 | DBG("Metadata fd %d is hup|err.", pollfd); |
| 1884 | if (!stream->hangup_flush_done |
| 1885 | && (consumer_data.type == LTTNG_CONSUMER32_UST |
| 1886 | || consumer_data.type == LTTNG_CONSUMER64_UST)) { |
| 1887 | DBG("Attempting to flush and consume the UST buffers"); |
| 1888 | lttng_ustconsumer_on_stream_hangup(stream); |
| 1889 | |
| 1890 | /* We just flushed the stream now read it. */ |
| 1891 | do { |
| 1892 | len = ctx->on_buffer_ready(stream, ctx); |
| 1893 | /* |
| 1894 | * We don't check the return value here since if we get |
| 1895 | * a negative len, it means an error occured thus we |
| 1896 | * simply remove it from the poll set and free the |
| 1897 | * stream. |
| 1898 | */ |
| 1899 | } while (len > 0); |
| 1900 | } |
| 1901 | |
| 1902 | lttng_poll_del(&events, stream->wait_fd); |
| 1903 | /* |
| 1904 | * This call update the channel states, closes file descriptors |
| 1905 | * and securely free the stream. |
| 1906 | */ |
| 1907 | consumer_del_metadata_stream(stream, metadata_ht); |
| 1908 | } else if (revents & (LPOLLIN | LPOLLPRI)) { |
| 1909 | /* Get the data out of the metadata file descriptor */ |
| 1910 | DBG("Metadata available on fd %d", pollfd); |
| 1911 | assert(stream->wait_fd == pollfd); |
| 1912 | |
| 1913 | len = ctx->on_buffer_ready(stream, ctx); |
| 1914 | /* It's ok to have an unavailable sub-buffer */ |
| 1915 | if (len < 0 && len != -EAGAIN && len != -ENODATA) { |
| 1916 | rcu_read_unlock(); |
| 1917 | goto end; |
| 1918 | } else if (len > 0) { |
| 1919 | stream->data_read = 1; |
| 1920 | } |
| 1921 | } |
| 1922 | |
| 1923 | /* Release RCU lock for the stream looked up */ |
| 1924 | rcu_read_unlock(); |
| 1925 | } |
| 1926 | } |
| 1927 | |
| 1928 | error: |
| 1929 | end: |
| 1930 | DBG("Metadata poll thread exiting"); |
| 1931 | lttng_poll_clean(&events); |
| 1932 | |
| 1933 | if (metadata_ht) { |
| 1934 | destroy_stream_ht(metadata_ht); |
| 1935 | } |
| 1936 | |
| 1937 | rcu_unregister_thread(); |
| 1938 | return NULL; |
| 1939 | } |
| 1940 | |
| 1941 | /* |
| 1942 | * This thread polls the fds in the set to consume the data and write |
| 1943 | * it to tracefile if necessary. |
| 1944 | */ |
| 1945 | void *consumer_thread_data_poll(void *data) |
| 1946 | { |
| 1947 | int num_rdy, num_hup, high_prio, ret, i; |
| 1948 | struct pollfd *pollfd = NULL; |
| 1949 | /* local view of the streams */ |
| 1950 | struct lttng_consumer_stream **local_stream = NULL; |
| 1951 | /* local view of consumer_data.fds_count */ |
| 1952 | int nb_fd = 0; |
| 1953 | struct lttng_consumer_local_data *ctx = data; |
| 1954 | ssize_t len; |
| 1955 | |
| 1956 | rcu_register_thread(); |
| 1957 | |
| 1958 | local_stream = zmalloc(sizeof(struct lttng_consumer_stream)); |
| 1959 | |
| 1960 | while (1) { |
| 1961 | high_prio = 0; |
| 1962 | num_hup = 0; |
| 1963 | |
| 1964 | /* |
| 1965 | * the fds set has been updated, we need to update our |
| 1966 | * local array as well |
| 1967 | */ |
| 1968 | pthread_mutex_lock(&consumer_data.lock); |
| 1969 | if (consumer_data.need_update) { |
| 1970 | if (pollfd != NULL) { |
| 1971 | free(pollfd); |
| 1972 | pollfd = NULL; |
| 1973 | } |
| 1974 | if (local_stream != NULL) { |
| 1975 | free(local_stream); |
| 1976 | local_stream = NULL; |
| 1977 | } |
| 1978 | |
| 1979 | /* allocate for all fds + 1 for the consumer_poll_pipe */ |
| 1980 | pollfd = zmalloc((consumer_data.stream_count + 1) * sizeof(struct pollfd)); |
| 1981 | if (pollfd == NULL) { |
| 1982 | PERROR("pollfd malloc"); |
| 1983 | pthread_mutex_unlock(&consumer_data.lock); |
| 1984 | goto end; |
| 1985 | } |
| 1986 | |
| 1987 | /* allocate for all fds + 1 for the consumer_poll_pipe */ |
| 1988 | local_stream = zmalloc((consumer_data.stream_count + 1) * |
| 1989 | sizeof(struct lttng_consumer_stream)); |
| 1990 | if (local_stream == NULL) { |
| 1991 | PERROR("local_stream malloc"); |
| 1992 | pthread_mutex_unlock(&consumer_data.lock); |
| 1993 | goto end; |
| 1994 | } |
| 1995 | ret = consumer_update_poll_array(ctx, &pollfd, local_stream); |
| 1996 | if (ret < 0) { |
| 1997 | ERR("Error in allocating pollfd or local_outfds"); |
| 1998 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR); |
| 1999 | pthread_mutex_unlock(&consumer_data.lock); |
| 2000 | goto end; |
| 2001 | } |
| 2002 | nb_fd = ret; |
| 2003 | consumer_data.need_update = 0; |
| 2004 | } |
| 2005 | pthread_mutex_unlock(&consumer_data.lock); |
| 2006 | |
| 2007 | /* No FDs and consumer_quit, consumer_cleanup the thread */ |
| 2008 | if (nb_fd == 0 && consumer_quit == 1) { |
| 2009 | goto end; |
| 2010 | } |
| 2011 | /* poll on the array of fds */ |
| 2012 | restart: |
| 2013 | DBG("polling on %d fd", nb_fd + 1); |
| 2014 | num_rdy = poll(pollfd, nb_fd + 1, consumer_poll_timeout); |
| 2015 | DBG("poll num_rdy : %d", num_rdy); |
| 2016 | if (num_rdy == -1) { |
| 2017 | /* |
| 2018 | * Restart interrupted system call. |
| 2019 | */ |
| 2020 | if (errno == EINTR) { |
| 2021 | goto restart; |
| 2022 | } |
| 2023 | PERROR("Poll error"); |
| 2024 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR); |
| 2025 | goto end; |
| 2026 | } else if (num_rdy == 0) { |
| 2027 | DBG("Polling thread timed out"); |
| 2028 | goto end; |
| 2029 | } |
| 2030 | |
| 2031 | /* |
| 2032 | * If the consumer_poll_pipe triggered poll go directly to the |
| 2033 | * beginning of the loop to update the array. We want to prioritize |
| 2034 | * array update over low-priority reads. |
| 2035 | */ |
| 2036 | if (pollfd[nb_fd].revents & (POLLIN | POLLPRI)) { |
| 2037 | size_t pipe_readlen; |
| 2038 | char tmp; |
| 2039 | |
| 2040 | DBG("consumer_poll_pipe wake up"); |
| 2041 | /* Consume 1 byte of pipe data */ |
| 2042 | do { |
| 2043 | pipe_readlen = read(ctx->consumer_poll_pipe[0], &tmp, 1); |
| 2044 | } while (pipe_readlen == -1 && errno == EINTR); |
| 2045 | continue; |
| 2046 | } |
| 2047 | |
| 2048 | /* Take care of high priority channels first. */ |
| 2049 | for (i = 0; i < nb_fd; i++) { |
| 2050 | if (pollfd[i].revents & POLLPRI) { |
| 2051 | DBG("Urgent read on fd %d", pollfd[i].fd); |
| 2052 | high_prio = 1; |
| 2053 | len = ctx->on_buffer_ready(local_stream[i], ctx); |
| 2054 | /* it's ok to have an unavailable sub-buffer */ |
| 2055 | if (len < 0 && len != -EAGAIN && len != -ENODATA) { |
| 2056 | goto end; |
| 2057 | } else if (len > 0) { |
| 2058 | local_stream[i]->data_read = 1; |
| 2059 | } |
| 2060 | } |
| 2061 | } |
| 2062 | |
| 2063 | /* |
| 2064 | * If we read high prio channel in this loop, try again |
| 2065 | * for more high prio data. |
| 2066 | */ |
| 2067 | if (high_prio) { |
| 2068 | continue; |
| 2069 | } |
| 2070 | |
| 2071 | /* Take care of low priority channels. */ |
| 2072 | for (i = 0; i < nb_fd; i++) { |
| 2073 | if ((pollfd[i].revents & POLLIN) || |
| 2074 | local_stream[i]->hangup_flush_done) { |
| 2075 | DBG("Normal read on fd %d", pollfd[i].fd); |
| 2076 | len = ctx->on_buffer_ready(local_stream[i], ctx); |
| 2077 | /* it's ok to have an unavailable sub-buffer */ |
| 2078 | if (len < 0 && len != -EAGAIN && len != -ENODATA) { |
| 2079 | goto end; |
| 2080 | } else if (len > 0) { |
| 2081 | local_stream[i]->data_read = 1; |
| 2082 | } |
| 2083 | } |
| 2084 | } |
| 2085 | |
| 2086 | /* Handle hangup and errors */ |
| 2087 | for (i = 0; i < nb_fd; i++) { |
| 2088 | if (!local_stream[i]->hangup_flush_done |
| 2089 | && (pollfd[i].revents & (POLLHUP | POLLERR | POLLNVAL)) |
| 2090 | && (consumer_data.type == LTTNG_CONSUMER32_UST |
| 2091 | || consumer_data.type == LTTNG_CONSUMER64_UST)) { |
| 2092 | DBG("fd %d is hup|err|nval. Attempting flush and read.", |
| 2093 | pollfd[i].fd); |
| 2094 | lttng_ustconsumer_on_stream_hangup(local_stream[i]); |
| 2095 | /* Attempt read again, for the data we just flushed. */ |
| 2096 | local_stream[i]->data_read = 1; |
| 2097 | } |
| 2098 | /* |
| 2099 | * If the poll flag is HUP/ERR/NVAL and we have |
| 2100 | * read no data in this pass, we can remove the |
| 2101 | * stream from its hash table. |
| 2102 | */ |
| 2103 | if ((pollfd[i].revents & POLLHUP)) { |
| 2104 | DBG("Polling fd %d tells it has hung up.", pollfd[i].fd); |
| 2105 | if (!local_stream[i]->data_read) { |
| 2106 | consumer_del_stream(local_stream[i], |
| 2107 | consumer_data.stream_ht); |
| 2108 | num_hup++; |
| 2109 | } |
| 2110 | } else if (pollfd[i].revents & POLLERR) { |
| 2111 | ERR("Error returned in polling fd %d.", pollfd[i].fd); |
| 2112 | if (!local_stream[i]->data_read) { |
| 2113 | consumer_del_stream(local_stream[i], |
| 2114 | consumer_data.stream_ht); |
| 2115 | num_hup++; |
| 2116 | } |
| 2117 | } else if (pollfd[i].revents & POLLNVAL) { |
| 2118 | ERR("Polling fd %d tells fd is not open.", pollfd[i].fd); |
| 2119 | if (!local_stream[i]->data_read) { |
| 2120 | consumer_del_stream(local_stream[i], |
| 2121 | consumer_data.stream_ht); |
| 2122 | num_hup++; |
| 2123 | } |
| 2124 | } |
| 2125 | local_stream[i]->data_read = 0; |
| 2126 | } |
| 2127 | } |
| 2128 | end: |
| 2129 | DBG("polling thread exiting"); |
| 2130 | if (pollfd != NULL) { |
| 2131 | free(pollfd); |
| 2132 | pollfd = NULL; |
| 2133 | } |
| 2134 | if (local_stream != NULL) { |
| 2135 | free(local_stream); |
| 2136 | local_stream = NULL; |
| 2137 | } |
| 2138 | |
| 2139 | /* |
| 2140 | * Close the write side of the pipe so epoll_wait() in |
| 2141 | * consumer_thread_metadata_poll can catch it. The thread is monitoring the |
| 2142 | * read side of the pipe. If we close them both, epoll_wait strangely does |
| 2143 | * not return and could create a endless wait period if the pipe is the |
| 2144 | * only tracked fd in the poll set. The thread will take care of closing |
| 2145 | * the read side. |
| 2146 | */ |
| 2147 | close(ctx->consumer_metadata_pipe[1]); |
| 2148 | |
| 2149 | rcu_unregister_thread(); |
| 2150 | return NULL; |
| 2151 | } |
| 2152 | |
| 2153 | /* |
| 2154 | * This thread listens on the consumerd socket and receives the file |
| 2155 | * descriptors from the session daemon. |
| 2156 | */ |
| 2157 | void *consumer_thread_sessiond_poll(void *data) |
| 2158 | { |
| 2159 | int sock, client_socket, ret; |
| 2160 | /* |
| 2161 | * structure to poll for incoming data on communication socket avoids |
| 2162 | * making blocking sockets. |
| 2163 | */ |
| 2164 | struct pollfd consumer_sockpoll[2]; |
| 2165 | struct lttng_consumer_local_data *ctx = data; |
| 2166 | |
| 2167 | rcu_register_thread(); |
| 2168 | |
| 2169 | DBG("Creating command socket %s", ctx->consumer_command_sock_path); |
| 2170 | unlink(ctx->consumer_command_sock_path); |
| 2171 | client_socket = lttcomm_create_unix_sock(ctx->consumer_command_sock_path); |
| 2172 | if (client_socket < 0) { |
| 2173 | ERR("Cannot create command socket"); |
| 2174 | goto end; |
| 2175 | } |
| 2176 | |
| 2177 | ret = lttcomm_listen_unix_sock(client_socket); |
| 2178 | if (ret < 0) { |
| 2179 | goto end; |
| 2180 | } |
| 2181 | |
| 2182 | DBG("Sending ready command to lttng-sessiond"); |
| 2183 | ret = lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY); |
| 2184 | /* return < 0 on error, but == 0 is not fatal */ |
| 2185 | if (ret < 0) { |
| 2186 | ERR("Error sending ready command to lttng-sessiond"); |
| 2187 | goto end; |
| 2188 | } |
| 2189 | |
| 2190 | ret = fcntl(client_socket, F_SETFL, O_NONBLOCK); |
| 2191 | if (ret < 0) { |
| 2192 | PERROR("fcntl O_NONBLOCK"); |
| 2193 | goto end; |
| 2194 | } |
| 2195 | |
| 2196 | /* prepare the FDs to poll : to client socket and the should_quit pipe */ |
| 2197 | consumer_sockpoll[0].fd = ctx->consumer_should_quit[0]; |
| 2198 | consumer_sockpoll[0].events = POLLIN | POLLPRI; |
| 2199 | consumer_sockpoll[1].fd = client_socket; |
| 2200 | consumer_sockpoll[1].events = POLLIN | POLLPRI; |
| 2201 | |
| 2202 | if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) { |
| 2203 | goto end; |
| 2204 | } |
| 2205 | DBG("Connection on client_socket"); |
| 2206 | |
| 2207 | /* Blocking call, waiting for transmission */ |
| 2208 | sock = lttcomm_accept_unix_sock(client_socket); |
| 2209 | if (sock <= 0) { |
| 2210 | WARN("On accept"); |
| 2211 | goto end; |
| 2212 | } |
| 2213 | ret = fcntl(sock, F_SETFL, O_NONBLOCK); |
| 2214 | if (ret < 0) { |
| 2215 | PERROR("fcntl O_NONBLOCK"); |
| 2216 | goto end; |
| 2217 | } |
| 2218 | |
| 2219 | /* update the polling structure to poll on the established socket */ |
| 2220 | consumer_sockpoll[1].fd = sock; |
| 2221 | consumer_sockpoll[1].events = POLLIN | POLLPRI; |
| 2222 | |
| 2223 | while (1) { |
| 2224 | if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) { |
| 2225 | goto end; |
| 2226 | } |
| 2227 | DBG("Incoming command on sock"); |
| 2228 | ret = lttng_consumer_recv_cmd(ctx, sock, consumer_sockpoll); |
| 2229 | if (ret == -ENOENT) { |
| 2230 | DBG("Received STOP command"); |
| 2231 | goto end; |
| 2232 | } |
| 2233 | if (ret <= 0) { |
| 2234 | /* |
| 2235 | * This could simply be a session daemon quitting. Don't output |
| 2236 | * ERR() here. |
| 2237 | */ |
| 2238 | DBG("Communication interrupted on command socket"); |
| 2239 | goto end; |
| 2240 | } |
| 2241 | if (consumer_quit) { |
| 2242 | DBG("consumer_thread_receive_fds received quit from signal"); |
| 2243 | goto end; |
| 2244 | } |
| 2245 | DBG("received fds on sock"); |
| 2246 | } |
| 2247 | end: |
| 2248 | DBG("consumer_thread_receive_fds exiting"); |
| 2249 | |
| 2250 | /* |
| 2251 | * when all fds have hung up, the polling thread |
| 2252 | * can exit cleanly |
| 2253 | */ |
| 2254 | consumer_quit = 1; |
| 2255 | |
| 2256 | /* |
| 2257 | * 2s of grace period, if no polling events occur during |
| 2258 | * this period, the polling thread will exit even if there |
| 2259 | * are still open FDs (should not happen, but safety mechanism). |
| 2260 | */ |
| 2261 | consumer_poll_timeout = LTTNG_CONSUMER_POLL_TIMEOUT; |
| 2262 | |
| 2263 | /* |
| 2264 | * Wake-up the other end by writing a null byte in the pipe |
| 2265 | * (non-blocking). Important note: Because writing into the |
| 2266 | * pipe is non-blocking (and therefore we allow dropping wakeup |
| 2267 | * data, as long as there is wakeup data present in the pipe |
| 2268 | * buffer to wake up the other end), the other end should |
| 2269 | * perform the following sequence for waiting: |
| 2270 | * 1) empty the pipe (reads). |
| 2271 | * 2) perform update operation. |
| 2272 | * 3) wait on the pipe (poll). |
| 2273 | */ |
| 2274 | do { |
| 2275 | ret = write(ctx->consumer_poll_pipe[1], "", 1); |
| 2276 | } while (ret < 0 && errno == EINTR); |
| 2277 | rcu_unregister_thread(); |
| 2278 | return NULL; |
| 2279 | } |
| 2280 | |
| 2281 | ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream, |
| 2282 | struct lttng_consumer_local_data *ctx) |
| 2283 | { |
| 2284 | switch (consumer_data.type) { |
| 2285 | case LTTNG_CONSUMER_KERNEL: |
| 2286 | return lttng_kconsumer_read_subbuffer(stream, ctx); |
| 2287 | case LTTNG_CONSUMER32_UST: |
| 2288 | case LTTNG_CONSUMER64_UST: |
| 2289 | return lttng_ustconsumer_read_subbuffer(stream, ctx); |
| 2290 | default: |
| 2291 | ERR("Unknown consumer_data type"); |
| 2292 | assert(0); |
| 2293 | return -ENOSYS; |
| 2294 | } |
| 2295 | } |
| 2296 | |
| 2297 | int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream) |
| 2298 | { |
| 2299 | switch (consumer_data.type) { |
| 2300 | case LTTNG_CONSUMER_KERNEL: |
| 2301 | return lttng_kconsumer_on_recv_stream(stream); |
| 2302 | case LTTNG_CONSUMER32_UST: |
| 2303 | case LTTNG_CONSUMER64_UST: |
| 2304 | return lttng_ustconsumer_on_recv_stream(stream); |
| 2305 | default: |
| 2306 | ERR("Unknown consumer_data type"); |
| 2307 | assert(0); |
| 2308 | return -ENOSYS; |
| 2309 | } |
| 2310 | } |
| 2311 | |
| 2312 | /* |
| 2313 | * Allocate and set consumer data hash tables. |
| 2314 | */ |
| 2315 | void lttng_consumer_init(void) |
| 2316 | { |
| 2317 | consumer_data.stream_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 2318 | consumer_data.channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 2319 | consumer_data.relayd_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 2320 | } |
| 2321 | |
| 2322 | /* |
| 2323 | * Process the ADD_RELAYD command receive by a consumer. |
| 2324 | * |
| 2325 | * This will create a relayd socket pair and add it to the relayd hash table. |
| 2326 | * The caller MUST acquire a RCU read side lock before calling it. |
| 2327 | */ |
| 2328 | int consumer_add_relayd_socket(int net_seq_idx, int sock_type, |
| 2329 | struct lttng_consumer_local_data *ctx, int sock, |
| 2330 | struct pollfd *consumer_sockpoll, struct lttcomm_sock *relayd_sock) |
| 2331 | { |
| 2332 | int fd, ret = -1; |
| 2333 | struct consumer_relayd_sock_pair *relayd; |
| 2334 | |
| 2335 | DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx); |
| 2336 | |
| 2337 | /* Get relayd reference if exists. */ |
| 2338 | relayd = consumer_find_relayd(net_seq_idx); |
| 2339 | if (relayd == NULL) { |
| 2340 | /* Not found. Allocate one. */ |
| 2341 | relayd = consumer_allocate_relayd_sock_pair(net_seq_idx); |
| 2342 | if (relayd == NULL) { |
| 2343 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR); |
| 2344 | goto error; |
| 2345 | } |
| 2346 | } |
| 2347 | |
| 2348 | /* Poll on consumer socket. */ |
| 2349 | if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) { |
| 2350 | ret = -EINTR; |
| 2351 | goto error; |
| 2352 | } |
| 2353 | |
| 2354 | /* Get relayd socket from session daemon */ |
| 2355 | ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1); |
| 2356 | if (ret != sizeof(fd)) { |
| 2357 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD); |
| 2358 | ret = -1; |
| 2359 | goto error; |
| 2360 | } |
| 2361 | |
| 2362 | /* Copy socket information and received FD */ |
| 2363 | switch (sock_type) { |
| 2364 | case LTTNG_STREAM_CONTROL: |
| 2365 | /* Copy received lttcomm socket */ |
| 2366 | lttcomm_copy_sock(&relayd->control_sock, relayd_sock); |
| 2367 | ret = lttcomm_create_sock(&relayd->control_sock); |
| 2368 | if (ret < 0) { |
| 2369 | goto error; |
| 2370 | } |
| 2371 | |
| 2372 | /* Close the created socket fd which is useless */ |
| 2373 | close(relayd->control_sock.fd); |
| 2374 | |
| 2375 | /* Assign new file descriptor */ |
| 2376 | relayd->control_sock.fd = fd; |
| 2377 | break; |
| 2378 | case LTTNG_STREAM_DATA: |
| 2379 | /* Copy received lttcomm socket */ |
| 2380 | lttcomm_copy_sock(&relayd->data_sock, relayd_sock); |
| 2381 | ret = lttcomm_create_sock(&relayd->data_sock); |
| 2382 | if (ret < 0) { |
| 2383 | goto error; |
| 2384 | } |
| 2385 | |
| 2386 | /* Close the created socket fd which is useless */ |
| 2387 | close(relayd->data_sock.fd); |
| 2388 | |
| 2389 | /* Assign new file descriptor */ |
| 2390 | relayd->data_sock.fd = fd; |
| 2391 | break; |
| 2392 | default: |
| 2393 | ERR("Unknown relayd socket type (%d)", sock_type); |
| 2394 | goto error; |
| 2395 | } |
| 2396 | |
| 2397 | DBG("Consumer %s socket created successfully with net idx %d (fd: %d)", |
| 2398 | sock_type == LTTNG_STREAM_CONTROL ? "control" : "data", |
| 2399 | relayd->net_seq_idx, fd); |
| 2400 | |
| 2401 | /* |
| 2402 | * Add relayd socket pair to consumer data hashtable. If object already |
| 2403 | * exists or on error, the function gracefully returns. |
| 2404 | */ |
| 2405 | add_relayd(relayd); |
| 2406 | |
| 2407 | /* All good! */ |
| 2408 | ret = 0; |
| 2409 | |
| 2410 | error: |
| 2411 | return ret; |
| 2412 | } |