| 1 | /* |
| 2 | * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca> |
| 3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License, version 2 only, |
| 7 | * as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along |
| 15 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | */ |
| 18 | |
| 19 | #define _GNU_SOURCE |
| 20 | #include <assert.h> |
| 21 | #include <poll.h> |
| 22 | #include <pthread.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/mman.h> |
| 26 | #include <sys/socket.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <inttypes.h> |
| 29 | #include <unistd.h> |
| 30 | #include <sys/stat.h> |
| 31 | |
| 32 | #include <bin/lttng-consumerd/health-consumerd.h> |
| 33 | #include <common/common.h> |
| 34 | #include <common/kernel-ctl/kernel-ctl.h> |
| 35 | #include <common/sessiond-comm/sessiond-comm.h> |
| 36 | #include <common/sessiond-comm/relayd.h> |
| 37 | #include <common/compat/fcntl.h> |
| 38 | #include <common/pipe.h> |
| 39 | #include <common/relayd/relayd.h> |
| 40 | #include <common/utils.h> |
| 41 | #include <common/consumer-stream.h> |
| 42 | #include <common/index/index.h> |
| 43 | #include <common/consumer-timer.h> |
| 44 | |
| 45 | #include "kernel-consumer.h" |
| 46 | |
| 47 | extern struct lttng_consumer_global_data consumer_data; |
| 48 | extern int consumer_poll_timeout; |
| 49 | extern volatile int consumer_quit; |
| 50 | |
| 51 | /* |
| 52 | * Take a snapshot for a specific fd |
| 53 | * |
| 54 | * Returns 0 on success, < 0 on error |
| 55 | */ |
| 56 | int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream *stream) |
| 57 | { |
| 58 | int ret = 0; |
| 59 | int infd = stream->wait_fd; |
| 60 | |
| 61 | ret = kernctl_snapshot(infd); |
| 62 | if (ret != 0) { |
| 63 | perror("Getting sub-buffer snapshot."); |
| 64 | ret = -errno; |
| 65 | } |
| 66 | |
| 67 | return ret; |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * Get the produced position |
| 72 | * |
| 73 | * Returns 0 on success, < 0 on error |
| 74 | */ |
| 75 | int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream *stream, |
| 76 | unsigned long *pos) |
| 77 | { |
| 78 | int ret; |
| 79 | int infd = stream->wait_fd; |
| 80 | |
| 81 | ret = kernctl_snapshot_get_produced(infd, pos); |
| 82 | if (ret != 0) { |
| 83 | perror("kernctl_snapshot_get_produced"); |
| 84 | ret = -errno; |
| 85 | } |
| 86 | |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * Get the consumerd position |
| 92 | * |
| 93 | * Returns 0 on success, < 0 on error |
| 94 | */ |
| 95 | int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream *stream, |
| 96 | unsigned long *pos) |
| 97 | { |
| 98 | int ret; |
| 99 | int infd = stream->wait_fd; |
| 100 | |
| 101 | ret = kernctl_snapshot_get_consumed(infd, pos); |
| 102 | if (ret != 0) { |
| 103 | perror("kernctl_snapshot_get_consumed"); |
| 104 | ret = -errno; |
| 105 | } |
| 106 | |
| 107 | return ret; |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * Take a snapshot of all the stream of a channel |
| 112 | * |
| 113 | * Returns 0 on success, < 0 on error |
| 114 | */ |
| 115 | int lttng_kconsumer_snapshot_channel(uint64_t key, char *path, |
| 116 | uint64_t relayd_id, uint64_t max_stream_size, |
| 117 | struct lttng_consumer_local_data *ctx) |
| 118 | { |
| 119 | int ret; |
| 120 | unsigned long consumed_pos, produced_pos; |
| 121 | struct lttng_consumer_channel *channel; |
| 122 | struct lttng_consumer_stream *stream; |
| 123 | |
| 124 | DBG("Kernel consumer snapshot channel %" PRIu64, key); |
| 125 | |
| 126 | rcu_read_lock(); |
| 127 | |
| 128 | channel = consumer_find_channel(key); |
| 129 | if (!channel) { |
| 130 | ERR("No channel found for key %" PRIu64, key); |
| 131 | ret = -1; |
| 132 | goto end; |
| 133 | } |
| 134 | |
| 135 | /* Splice is not supported yet for channel snapshot. */ |
| 136 | if (channel->output != CONSUMER_CHANNEL_MMAP) { |
| 137 | ERR("Unsupported output %d", channel->output); |
| 138 | ret = -1; |
| 139 | goto end; |
| 140 | } |
| 141 | |
| 142 | cds_list_for_each_entry(stream, &channel->streams.head, send_node) { |
| 143 | |
| 144 | health_code_update(); |
| 145 | |
| 146 | /* |
| 147 | * Lock stream because we are about to change its state. |
| 148 | */ |
| 149 | pthread_mutex_lock(&stream->lock); |
| 150 | |
| 151 | /* |
| 152 | * Assign the received relayd ID so we can use it for streaming. The streams |
| 153 | * are not visible to anyone so this is OK to change it. |
| 154 | */ |
| 155 | stream->net_seq_idx = relayd_id; |
| 156 | channel->relayd_id = relayd_id; |
| 157 | if (relayd_id != (uint64_t) -1ULL) { |
| 158 | ret = consumer_send_relayd_stream(stream, path); |
| 159 | if (ret < 0) { |
| 160 | ERR("sending stream to relayd"); |
| 161 | goto end_unlock; |
| 162 | } |
| 163 | } else { |
| 164 | ret = utils_create_stream_file(path, stream->name, |
| 165 | stream->chan->tracefile_size, |
| 166 | stream->tracefile_count_current, |
| 167 | stream->uid, stream->gid, NULL); |
| 168 | if (ret < 0) { |
| 169 | ERR("utils_create_stream_file"); |
| 170 | goto end_unlock; |
| 171 | } |
| 172 | |
| 173 | stream->out_fd = ret; |
| 174 | stream->tracefile_size_current = 0; |
| 175 | |
| 176 | DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64 ")", |
| 177 | path, stream->name, stream->key); |
| 178 | } |
| 179 | if (relayd_id != -1ULL) { |
| 180 | ret = consumer_send_relayd_streams_sent(relayd_id); |
| 181 | if (ret < 0) { |
| 182 | ERR("sending streams sent to relayd"); |
| 183 | goto end_unlock; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | ret = kernctl_buffer_flush(stream->wait_fd); |
| 188 | if (ret < 0) { |
| 189 | ERR("Failed to flush kernel stream"); |
| 190 | ret = -errno; |
| 191 | goto end_unlock; |
| 192 | } |
| 193 | |
| 194 | ret = lttng_kconsumer_take_snapshot(stream); |
| 195 | if (ret < 0) { |
| 196 | ERR("Taking kernel snapshot"); |
| 197 | goto end_unlock; |
| 198 | } |
| 199 | |
| 200 | ret = lttng_kconsumer_get_produced_snapshot(stream, &produced_pos); |
| 201 | if (ret < 0) { |
| 202 | ERR("Produced kernel snapshot position"); |
| 203 | goto end_unlock; |
| 204 | } |
| 205 | |
| 206 | ret = lttng_kconsumer_get_consumed_snapshot(stream, &consumed_pos); |
| 207 | if (ret < 0) { |
| 208 | ERR("Consumerd kernel snapshot position"); |
| 209 | goto end_unlock; |
| 210 | } |
| 211 | |
| 212 | if (stream->max_sb_size == 0) { |
| 213 | ret = kernctl_get_max_subbuf_size(stream->wait_fd, |
| 214 | &stream->max_sb_size); |
| 215 | if (ret < 0) { |
| 216 | ERR("Getting kernel max_sb_size"); |
| 217 | ret = -errno; |
| 218 | goto end_unlock; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /* |
| 223 | * The original value is sent back if max stream size is larger than |
| 224 | * the possible size of the snapshot. Also, we asume that the session |
| 225 | * daemon should never send a maximum stream size that is lower than |
| 226 | * subbuffer size. |
| 227 | */ |
| 228 | consumed_pos = consumer_get_consumed_maxsize(consumed_pos, |
| 229 | produced_pos, max_stream_size); |
| 230 | |
| 231 | while (consumed_pos < produced_pos) { |
| 232 | ssize_t read_len; |
| 233 | unsigned long len, padded_len; |
| 234 | |
| 235 | health_code_update(); |
| 236 | |
| 237 | DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos); |
| 238 | |
| 239 | ret = kernctl_get_subbuf(stream->wait_fd, &consumed_pos); |
| 240 | if (ret < 0) { |
| 241 | if (errno != EAGAIN) { |
| 242 | PERROR("kernctl_get_subbuf snapshot"); |
| 243 | ret = -errno; |
| 244 | goto end_unlock; |
| 245 | } |
| 246 | DBG("Kernel consumer get subbuf failed. Skipping it."); |
| 247 | consumed_pos += stream->max_sb_size; |
| 248 | continue; |
| 249 | } |
| 250 | |
| 251 | ret = kernctl_get_subbuf_size(stream->wait_fd, &len); |
| 252 | if (ret < 0) { |
| 253 | ERR("Snapshot kernctl_get_subbuf_size"); |
| 254 | ret = -errno; |
| 255 | goto error_put_subbuf; |
| 256 | } |
| 257 | |
| 258 | ret = kernctl_get_padded_subbuf_size(stream->wait_fd, &padded_len); |
| 259 | if (ret < 0) { |
| 260 | ERR("Snapshot kernctl_get_padded_subbuf_size"); |
| 261 | ret = -errno; |
| 262 | goto error_put_subbuf; |
| 263 | } |
| 264 | |
| 265 | read_len = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len, |
| 266 | padded_len - len, NULL); |
| 267 | /* |
| 268 | * We write the padded len in local tracefiles but the data len |
| 269 | * when using a relay. Display the error but continue processing |
| 270 | * to try to release the subbuffer. |
| 271 | */ |
| 272 | if (relayd_id != (uint64_t) -1ULL) { |
| 273 | if (read_len != len) { |
| 274 | ERR("Error sending to the relay (ret: %zd != len: %lu)", |
| 275 | read_len, len); |
| 276 | } |
| 277 | } else { |
| 278 | if (read_len != padded_len) { |
| 279 | ERR("Error writing to tracefile (ret: %zd != len: %lu)", |
| 280 | read_len, padded_len); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | ret = kernctl_put_subbuf(stream->wait_fd); |
| 285 | if (ret < 0) { |
| 286 | ERR("Snapshot kernctl_put_subbuf"); |
| 287 | ret = -errno; |
| 288 | goto end_unlock; |
| 289 | } |
| 290 | consumed_pos += stream->max_sb_size; |
| 291 | } |
| 292 | |
| 293 | if (relayd_id == (uint64_t) -1ULL) { |
| 294 | if (stream->out_fd >= 0) { |
| 295 | ret = close(stream->out_fd); |
| 296 | if (ret < 0) { |
| 297 | PERROR("Kernel consumer snapshot close out_fd"); |
| 298 | goto end_unlock; |
| 299 | } |
| 300 | stream->out_fd = -1; |
| 301 | } |
| 302 | } else { |
| 303 | close_relayd_stream(stream); |
| 304 | stream->net_seq_idx = (uint64_t) -1ULL; |
| 305 | } |
| 306 | pthread_mutex_unlock(&stream->lock); |
| 307 | } |
| 308 | |
| 309 | /* All good! */ |
| 310 | ret = 0; |
| 311 | goto end; |
| 312 | |
| 313 | error_put_subbuf: |
| 314 | ret = kernctl_put_subbuf(stream->wait_fd); |
| 315 | if (ret < 0) { |
| 316 | ret = -errno; |
| 317 | ERR("Snapshot kernctl_put_subbuf error path"); |
| 318 | } |
| 319 | end_unlock: |
| 320 | pthread_mutex_unlock(&stream->lock); |
| 321 | end: |
| 322 | rcu_read_unlock(); |
| 323 | return ret; |
| 324 | } |
| 325 | |
| 326 | /* |
| 327 | * Read the whole metadata available for a snapshot. |
| 328 | * |
| 329 | * Returns 0 on success, < 0 on error |
| 330 | */ |
| 331 | int lttng_kconsumer_snapshot_metadata(uint64_t key, char *path, |
| 332 | uint64_t relayd_id, struct lttng_consumer_local_data *ctx) |
| 333 | { |
| 334 | int ret, use_relayd = 0; |
| 335 | ssize_t ret_read; |
| 336 | struct lttng_consumer_channel *metadata_channel; |
| 337 | struct lttng_consumer_stream *metadata_stream; |
| 338 | |
| 339 | assert(ctx); |
| 340 | |
| 341 | DBG("Kernel consumer snapshot metadata with key %" PRIu64 " at path %s", |
| 342 | key, path); |
| 343 | |
| 344 | rcu_read_lock(); |
| 345 | |
| 346 | metadata_channel = consumer_find_channel(key); |
| 347 | if (!metadata_channel) { |
| 348 | ERR("Kernel snapshot metadata not found for key %" PRIu64, key); |
| 349 | ret = -1; |
| 350 | goto error; |
| 351 | } |
| 352 | |
| 353 | metadata_stream = metadata_channel->metadata_stream; |
| 354 | assert(metadata_stream); |
| 355 | |
| 356 | /* Flag once that we have a valid relayd for the stream. */ |
| 357 | if (relayd_id != (uint64_t) -1ULL) { |
| 358 | use_relayd = 1; |
| 359 | } |
| 360 | |
| 361 | if (use_relayd) { |
| 362 | ret = consumer_send_relayd_stream(metadata_stream, path); |
| 363 | if (ret < 0) { |
| 364 | goto error; |
| 365 | } |
| 366 | } else { |
| 367 | ret = utils_create_stream_file(path, metadata_stream->name, |
| 368 | metadata_stream->chan->tracefile_size, |
| 369 | metadata_stream->tracefile_count_current, |
| 370 | metadata_stream->uid, metadata_stream->gid, NULL); |
| 371 | if (ret < 0) { |
| 372 | goto error; |
| 373 | } |
| 374 | metadata_stream->out_fd = ret; |
| 375 | } |
| 376 | |
| 377 | do { |
| 378 | health_code_update(); |
| 379 | |
| 380 | ret_read = lttng_kconsumer_read_subbuffer(metadata_stream, ctx); |
| 381 | if (ret_read < 0) { |
| 382 | if (ret_read != -EAGAIN) { |
| 383 | ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)", |
| 384 | ret_read); |
| 385 | goto error; |
| 386 | } |
| 387 | /* ret_read is negative at this point so we will exit the loop. */ |
| 388 | continue; |
| 389 | } |
| 390 | } while (ret_read >= 0); |
| 391 | |
| 392 | if (use_relayd) { |
| 393 | close_relayd_stream(metadata_stream); |
| 394 | metadata_stream->net_seq_idx = (uint64_t) -1ULL; |
| 395 | } else { |
| 396 | if (metadata_stream->out_fd >= 0) { |
| 397 | ret = close(metadata_stream->out_fd); |
| 398 | if (ret < 0) { |
| 399 | PERROR("Kernel consumer snapshot metadata close out_fd"); |
| 400 | /* |
| 401 | * Don't go on error here since the snapshot was successful at this |
| 402 | * point but somehow the close failed. |
| 403 | */ |
| 404 | } |
| 405 | metadata_stream->out_fd = -1; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | ret = 0; |
| 410 | |
| 411 | cds_list_del(&metadata_stream->send_node); |
| 412 | consumer_stream_destroy(metadata_stream, NULL); |
| 413 | metadata_channel->metadata_stream = NULL; |
| 414 | error: |
| 415 | rcu_read_unlock(); |
| 416 | return ret; |
| 417 | } |
| 418 | |
| 419 | /* |
| 420 | * Receive command from session daemon and process it. |
| 421 | * |
| 422 | * Return 1 on success else a negative value or 0. |
| 423 | */ |
| 424 | int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx, |
| 425 | int sock, struct pollfd *consumer_sockpoll) |
| 426 | { |
| 427 | ssize_t ret; |
| 428 | enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS; |
| 429 | struct lttcomm_consumer_msg msg; |
| 430 | |
| 431 | health_code_update(); |
| 432 | |
| 433 | ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg)); |
| 434 | if (ret != sizeof(msg)) { |
| 435 | if (ret > 0) { |
| 436 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD); |
| 437 | ret = -1; |
| 438 | } |
| 439 | return ret; |
| 440 | } |
| 441 | |
| 442 | health_code_update(); |
| 443 | |
| 444 | /* Deprecated command */ |
| 445 | assert(msg.cmd_type != LTTNG_CONSUMER_STOP); |
| 446 | |
| 447 | health_code_update(); |
| 448 | |
| 449 | /* relayd needs RCU read-side protection */ |
| 450 | rcu_read_lock(); |
| 451 | |
| 452 | switch (msg.cmd_type) { |
| 453 | case LTTNG_CONSUMER_ADD_RELAYD_SOCKET: |
| 454 | { |
| 455 | /* Session daemon status message are handled in the following call. */ |
| 456 | ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index, |
| 457 | msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll, |
| 458 | &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id, |
| 459 | msg.u.relayd_sock.relayd_session_id); |
| 460 | goto end_nosignal; |
| 461 | } |
| 462 | case LTTNG_CONSUMER_ADD_CHANNEL: |
| 463 | { |
| 464 | struct lttng_consumer_channel *new_channel; |
| 465 | int ret_recv; |
| 466 | |
| 467 | health_code_update(); |
| 468 | |
| 469 | /* First send a status message before receiving the fds. */ |
| 470 | ret = consumer_send_status_msg(sock, ret_code); |
| 471 | if (ret < 0) { |
| 472 | /* Somehow, the session daemon is not responding anymore. */ |
| 473 | goto error_fatal; |
| 474 | } |
| 475 | |
| 476 | health_code_update(); |
| 477 | |
| 478 | DBG("consumer_add_channel %" PRIu64, msg.u.channel.channel_key); |
| 479 | new_channel = consumer_allocate_channel(msg.u.channel.channel_key, |
| 480 | msg.u.channel.session_id, msg.u.channel.pathname, |
| 481 | msg.u.channel.name, msg.u.channel.uid, msg.u.channel.gid, |
| 482 | msg.u.channel.relayd_id, msg.u.channel.output, |
| 483 | msg.u.channel.tracefile_size, |
| 484 | msg.u.channel.tracefile_count, 0, |
| 485 | msg.u.channel.monitor, |
| 486 | msg.u.channel.live_timer_interval); |
| 487 | if (new_channel == NULL) { |
| 488 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR); |
| 489 | goto end_nosignal; |
| 490 | } |
| 491 | new_channel->nb_init_stream_left = msg.u.channel.nb_init_streams; |
| 492 | switch (msg.u.channel.output) { |
| 493 | case LTTNG_EVENT_SPLICE: |
| 494 | new_channel->output = CONSUMER_CHANNEL_SPLICE; |
| 495 | break; |
| 496 | case LTTNG_EVENT_MMAP: |
| 497 | new_channel->output = CONSUMER_CHANNEL_MMAP; |
| 498 | break; |
| 499 | default: |
| 500 | ERR("Channel output unknown %d", msg.u.channel.output); |
| 501 | goto end_nosignal; |
| 502 | } |
| 503 | |
| 504 | /* Translate and save channel type. */ |
| 505 | switch (msg.u.channel.type) { |
| 506 | case CONSUMER_CHANNEL_TYPE_DATA: |
| 507 | case CONSUMER_CHANNEL_TYPE_METADATA: |
| 508 | new_channel->type = msg.u.channel.type; |
| 509 | break; |
| 510 | default: |
| 511 | assert(0); |
| 512 | goto end_nosignal; |
| 513 | }; |
| 514 | |
| 515 | health_code_update(); |
| 516 | |
| 517 | if (ctx->on_recv_channel != NULL) { |
| 518 | ret_recv = ctx->on_recv_channel(new_channel); |
| 519 | if (ret_recv == 0) { |
| 520 | ret = consumer_add_channel(new_channel, ctx); |
| 521 | } else if (ret_recv < 0) { |
| 522 | goto end_nosignal; |
| 523 | } |
| 524 | } else { |
| 525 | ret = consumer_add_channel(new_channel, ctx); |
| 526 | } |
| 527 | if (CONSUMER_CHANNEL_TYPE_DATA) { |
| 528 | consumer_timer_live_start(new_channel, |
| 529 | msg.u.channel.live_timer_interval); |
| 530 | } |
| 531 | |
| 532 | health_code_update(); |
| 533 | |
| 534 | /* If we received an error in add_channel, we need to report it. */ |
| 535 | if (ret < 0) { |
| 536 | ret = consumer_send_status_msg(sock, ret); |
| 537 | if (ret < 0) { |
| 538 | goto error_fatal; |
| 539 | } |
| 540 | goto end_nosignal; |
| 541 | } |
| 542 | |
| 543 | goto end_nosignal; |
| 544 | } |
| 545 | case LTTNG_CONSUMER_ADD_STREAM: |
| 546 | { |
| 547 | int fd; |
| 548 | struct lttng_pipe *stream_pipe; |
| 549 | struct lttng_consumer_stream *new_stream; |
| 550 | struct lttng_consumer_channel *channel; |
| 551 | int alloc_ret = 0; |
| 552 | |
| 553 | /* |
| 554 | * Get stream's channel reference. Needed when adding the stream to the |
| 555 | * global hash table. |
| 556 | */ |
| 557 | channel = consumer_find_channel(msg.u.stream.channel_key); |
| 558 | if (!channel) { |
| 559 | /* |
| 560 | * We could not find the channel. Can happen if cpu hotplug |
| 561 | * happens while tearing down. |
| 562 | */ |
| 563 | ERR("Unable to find channel key %" PRIu64, msg.u.stream.channel_key); |
| 564 | ret_code = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
| 565 | } |
| 566 | |
| 567 | health_code_update(); |
| 568 | |
| 569 | /* First send a status message before receiving the fds. */ |
| 570 | ret = consumer_send_status_msg(sock, ret_code); |
| 571 | if (ret < 0) { |
| 572 | /* Somehow, the session daemon is not responding anymore. */ |
| 573 | goto error_fatal; |
| 574 | } |
| 575 | |
| 576 | health_code_update(); |
| 577 | |
| 578 | if (ret_code != LTTCOMM_CONSUMERD_SUCCESS) { |
| 579 | /* Channel was not found. */ |
| 580 | goto end_nosignal; |
| 581 | } |
| 582 | |
| 583 | /* Blocking call */ |
| 584 | health_poll_entry(); |
| 585 | ret = lttng_consumer_poll_socket(consumer_sockpoll); |
| 586 | health_poll_exit(); |
| 587 | if (ret) { |
| 588 | goto error_fatal; |
| 589 | } |
| 590 | |
| 591 | health_code_update(); |
| 592 | |
| 593 | /* Get stream file descriptor from socket */ |
| 594 | ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1); |
| 595 | if (ret != sizeof(fd)) { |
| 596 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD); |
| 597 | rcu_read_unlock(); |
| 598 | return ret; |
| 599 | } |
| 600 | |
| 601 | health_code_update(); |
| 602 | |
| 603 | /* |
| 604 | * Send status code to session daemon only if the recv works. If the |
| 605 | * above recv() failed, the session daemon is notified through the |
| 606 | * error socket and the teardown is eventually done. |
| 607 | */ |
| 608 | ret = consumer_send_status_msg(sock, ret_code); |
| 609 | if (ret < 0) { |
| 610 | /* Somehow, the session daemon is not responding anymore. */ |
| 611 | goto end_nosignal; |
| 612 | } |
| 613 | |
| 614 | health_code_update(); |
| 615 | |
| 616 | new_stream = consumer_allocate_stream(channel->key, |
| 617 | fd, |
| 618 | LTTNG_CONSUMER_ACTIVE_STREAM, |
| 619 | channel->name, |
| 620 | channel->uid, |
| 621 | channel->gid, |
| 622 | channel->relayd_id, |
| 623 | channel->session_id, |
| 624 | msg.u.stream.cpu, |
| 625 | &alloc_ret, |
| 626 | channel->type, |
| 627 | channel->monitor); |
| 628 | if (new_stream == NULL) { |
| 629 | switch (alloc_ret) { |
| 630 | case -ENOMEM: |
| 631 | case -EINVAL: |
| 632 | default: |
| 633 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR); |
| 634 | break; |
| 635 | } |
| 636 | goto end_nosignal; |
| 637 | } |
| 638 | |
| 639 | new_stream->chan = channel; |
| 640 | new_stream->wait_fd = fd; |
| 641 | switch (channel->output) { |
| 642 | case CONSUMER_CHANNEL_SPLICE: |
| 643 | new_stream->output = LTTNG_EVENT_SPLICE; |
| 644 | break; |
| 645 | case CONSUMER_CHANNEL_MMAP: |
| 646 | new_stream->output = LTTNG_EVENT_MMAP; |
| 647 | break; |
| 648 | default: |
| 649 | ERR("Stream output unknown %d", channel->output); |
| 650 | goto end_nosignal; |
| 651 | } |
| 652 | |
| 653 | /* |
| 654 | * We've just assigned the channel to the stream so increment the |
| 655 | * refcount right now. We don't need to increment the refcount for |
| 656 | * streams in no monitor because we handle manually the cleanup of |
| 657 | * those. It is very important to make sure there is NO prior |
| 658 | * consumer_del_stream() calls or else the refcount will be unbalanced. |
| 659 | */ |
| 660 | if (channel->monitor) { |
| 661 | uatomic_inc(&new_stream->chan->refcount); |
| 662 | } |
| 663 | |
| 664 | /* |
| 665 | * The buffer flush is done on the session daemon side for the kernel |
| 666 | * so no need for the stream "hangup_flush_done" variable to be |
| 667 | * tracked. This is important for a kernel stream since we don't rely |
| 668 | * on the flush state of the stream to read data. It's not the case for |
| 669 | * user space tracing. |
| 670 | */ |
| 671 | new_stream->hangup_flush_done = 0; |
| 672 | |
| 673 | health_code_update(); |
| 674 | |
| 675 | if (ctx->on_recv_stream) { |
| 676 | ret = ctx->on_recv_stream(new_stream); |
| 677 | if (ret < 0) { |
| 678 | consumer_stream_free(new_stream); |
| 679 | goto end_nosignal; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | health_code_update(); |
| 684 | |
| 685 | if (new_stream->metadata_flag) { |
| 686 | channel->metadata_stream = new_stream; |
| 687 | } |
| 688 | |
| 689 | /* Do not monitor this stream. */ |
| 690 | if (!channel->monitor) { |
| 691 | DBG("Kernel consumer add stream %s in no monitor mode with " |
| 692 | "relayd id %" PRIu64, new_stream->name, |
| 693 | new_stream->net_seq_idx); |
| 694 | cds_list_add(&new_stream->send_node, &channel->streams.head); |
| 695 | break; |
| 696 | } |
| 697 | |
| 698 | /* Send stream to relayd if the stream has an ID. */ |
| 699 | if (new_stream->net_seq_idx != (uint64_t) -1ULL) { |
| 700 | ret = consumer_send_relayd_stream(new_stream, |
| 701 | new_stream->chan->pathname); |
| 702 | if (ret < 0) { |
| 703 | consumer_stream_free(new_stream); |
| 704 | goto end_nosignal; |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | /* Get the right pipe where the stream will be sent. */ |
| 709 | if (new_stream->metadata_flag) { |
| 710 | ret = consumer_add_metadata_stream(new_stream); |
| 711 | if (ret) { |
| 712 | ERR("Consumer add metadata stream %" PRIu64 " failed. Continuing", |
| 713 | new_stream->key); |
| 714 | consumer_stream_free(new_stream); |
| 715 | goto end_nosignal; |
| 716 | } |
| 717 | stream_pipe = ctx->consumer_metadata_pipe; |
| 718 | } else { |
| 719 | ret = consumer_add_data_stream(new_stream); |
| 720 | if (ret) { |
| 721 | ERR("Consumer add stream %" PRIu64 " failed. Continuing", |
| 722 | new_stream->key); |
| 723 | consumer_stream_free(new_stream); |
| 724 | goto end_nosignal; |
| 725 | } |
| 726 | stream_pipe = ctx->consumer_data_pipe; |
| 727 | } |
| 728 | |
| 729 | /* Vitible to other threads */ |
| 730 | new_stream->globally_visible = 1; |
| 731 | |
| 732 | health_code_update(); |
| 733 | |
| 734 | ret = lttng_pipe_write(stream_pipe, &new_stream, sizeof(new_stream)); |
| 735 | if (ret < 0) { |
| 736 | ERR("Consumer write %s stream to pipe %d", |
| 737 | new_stream->metadata_flag ? "metadata" : "data", |
| 738 | lttng_pipe_get_writefd(stream_pipe)); |
| 739 | if (new_stream->metadata_flag) { |
| 740 | consumer_del_stream_for_metadata(new_stream); |
| 741 | } else { |
| 742 | consumer_del_stream_for_data(new_stream); |
| 743 | } |
| 744 | goto end_nosignal; |
| 745 | } |
| 746 | |
| 747 | DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64, |
| 748 | new_stream->name, fd, new_stream->relayd_stream_id); |
| 749 | break; |
| 750 | } |
| 751 | case LTTNG_CONSUMER_STREAMS_SENT: |
| 752 | { |
| 753 | struct lttng_consumer_channel *channel; |
| 754 | |
| 755 | /* |
| 756 | * Get stream's channel reference. Needed when adding the stream to the |
| 757 | * global hash table. |
| 758 | */ |
| 759 | channel = consumer_find_channel(msg.u.sent_streams.channel_key); |
| 760 | if (!channel) { |
| 761 | /* |
| 762 | * We could not find the channel. Can happen if cpu hotplug |
| 763 | * happens while tearing down. |
| 764 | */ |
| 765 | ERR("Unable to find channel key %" PRIu64, |
| 766 | msg.u.sent_streams.channel_key); |
| 767 | ret_code = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
| 768 | } |
| 769 | |
| 770 | health_code_update(); |
| 771 | |
| 772 | /* |
| 773 | * Send status code to session daemon. |
| 774 | */ |
| 775 | ret = consumer_send_status_msg(sock, ret_code); |
| 776 | if (ret < 0) { |
| 777 | /* Somehow, the session daemon is not responding anymore. */ |
| 778 | goto end_nosignal; |
| 779 | } |
| 780 | |
| 781 | health_code_update(); |
| 782 | |
| 783 | /* |
| 784 | * We should not send this message if we don't monitor the |
| 785 | * streams in this channel. |
| 786 | */ |
| 787 | if (!channel->monitor) { |
| 788 | break; |
| 789 | } |
| 790 | |
| 791 | health_code_update(); |
| 792 | /* Send stream to relayd if the stream has an ID. */ |
| 793 | if (msg.u.sent_streams.net_seq_idx != (uint64_t) -1ULL) { |
| 794 | ret = consumer_send_relayd_streams_sent( |
| 795 | msg.u.sent_streams.net_seq_idx); |
| 796 | if (ret < 0) { |
| 797 | goto end_nosignal; |
| 798 | } |
| 799 | } |
| 800 | break; |
| 801 | } |
| 802 | case LTTNG_CONSUMER_UPDATE_STREAM: |
| 803 | { |
| 804 | rcu_read_unlock(); |
| 805 | return -ENOSYS; |
| 806 | } |
| 807 | case LTTNG_CONSUMER_DESTROY_RELAYD: |
| 808 | { |
| 809 | uint64_t index = msg.u.destroy_relayd.net_seq_idx; |
| 810 | struct consumer_relayd_sock_pair *relayd; |
| 811 | |
| 812 | DBG("Kernel consumer destroying relayd %" PRIu64, index); |
| 813 | |
| 814 | /* Get relayd reference if exists. */ |
| 815 | relayd = consumer_find_relayd(index); |
| 816 | if (relayd == NULL) { |
| 817 | DBG("Unable to find relayd %" PRIu64, index); |
| 818 | ret_code = LTTNG_ERR_NO_CONSUMER; |
| 819 | } |
| 820 | |
| 821 | /* |
| 822 | * Each relayd socket pair has a refcount of stream attached to it |
| 823 | * which tells if the relayd is still active or not depending on the |
| 824 | * refcount value. |
| 825 | * |
| 826 | * This will set the destroy flag of the relayd object and destroy it |
| 827 | * if the refcount reaches zero when called. |
| 828 | * |
| 829 | * The destroy can happen either here or when a stream fd hangs up. |
| 830 | */ |
| 831 | if (relayd) { |
| 832 | consumer_flag_relayd_for_destroy(relayd); |
| 833 | } |
| 834 | |
| 835 | health_code_update(); |
| 836 | |
| 837 | ret = consumer_send_status_msg(sock, ret_code); |
| 838 | if (ret < 0) { |
| 839 | /* Somehow, the session daemon is not responding anymore. */ |
| 840 | goto error_fatal; |
| 841 | } |
| 842 | |
| 843 | goto end_nosignal; |
| 844 | } |
| 845 | case LTTNG_CONSUMER_DATA_PENDING: |
| 846 | { |
| 847 | int32_t ret; |
| 848 | uint64_t id = msg.u.data_pending.session_id; |
| 849 | |
| 850 | DBG("Kernel consumer data pending command for id %" PRIu64, id); |
| 851 | |
| 852 | ret = consumer_data_pending(id); |
| 853 | |
| 854 | health_code_update(); |
| 855 | |
| 856 | /* Send back returned value to session daemon */ |
| 857 | ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret)); |
| 858 | if (ret < 0) { |
| 859 | PERROR("send data pending ret code"); |
| 860 | goto error_fatal; |
| 861 | } |
| 862 | |
| 863 | /* |
| 864 | * No need to send back a status message since the data pending |
| 865 | * returned value is the response. |
| 866 | */ |
| 867 | break; |
| 868 | } |
| 869 | case LTTNG_CONSUMER_SNAPSHOT_CHANNEL: |
| 870 | { |
| 871 | if (msg.u.snapshot_channel.metadata == 1) { |
| 872 | ret = lttng_kconsumer_snapshot_metadata(msg.u.snapshot_channel.key, |
| 873 | msg.u.snapshot_channel.pathname, |
| 874 | msg.u.snapshot_channel.relayd_id, ctx); |
| 875 | if (ret < 0) { |
| 876 | ERR("Snapshot metadata failed"); |
| 877 | ret_code = LTTNG_ERR_KERN_META_FAIL; |
| 878 | } |
| 879 | } else { |
| 880 | ret = lttng_kconsumer_snapshot_channel(msg.u.snapshot_channel.key, |
| 881 | msg.u.snapshot_channel.pathname, |
| 882 | msg.u.snapshot_channel.relayd_id, |
| 883 | msg.u.snapshot_channel.max_stream_size, |
| 884 | ctx); |
| 885 | if (ret < 0) { |
| 886 | ERR("Snapshot channel failed"); |
| 887 | ret_code = LTTNG_ERR_KERN_CHAN_FAIL; |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | health_code_update(); |
| 892 | |
| 893 | ret = consumer_send_status_msg(sock, ret_code); |
| 894 | if (ret < 0) { |
| 895 | /* Somehow, the session daemon is not responding anymore. */ |
| 896 | goto end_nosignal; |
| 897 | } |
| 898 | break; |
| 899 | } |
| 900 | case LTTNG_CONSUMER_DESTROY_CHANNEL: |
| 901 | { |
| 902 | uint64_t key = msg.u.destroy_channel.key; |
| 903 | struct lttng_consumer_channel *channel; |
| 904 | |
| 905 | channel = consumer_find_channel(key); |
| 906 | if (!channel) { |
| 907 | ERR("Kernel consumer destroy channel %" PRIu64 " not found", key); |
| 908 | ret_code = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
| 909 | } |
| 910 | |
| 911 | health_code_update(); |
| 912 | |
| 913 | ret = consumer_send_status_msg(sock, ret_code); |
| 914 | if (ret < 0) { |
| 915 | /* Somehow, the session daemon is not responding anymore. */ |
| 916 | goto end_nosignal; |
| 917 | } |
| 918 | |
| 919 | health_code_update(); |
| 920 | |
| 921 | /* |
| 922 | * This command should ONLY be issued for channel with streams set in |
| 923 | * no monitor mode. |
| 924 | */ |
| 925 | assert(!channel->monitor); |
| 926 | |
| 927 | /* |
| 928 | * The refcount should ALWAYS be 0 in the case of a channel in no |
| 929 | * monitor mode. |
| 930 | */ |
| 931 | assert(!uatomic_sub_return(&channel->refcount, 1)); |
| 932 | |
| 933 | consumer_del_channel(channel); |
| 934 | |
| 935 | goto end_nosignal; |
| 936 | } |
| 937 | default: |
| 938 | goto end_nosignal; |
| 939 | } |
| 940 | |
| 941 | end_nosignal: |
| 942 | rcu_read_unlock(); |
| 943 | |
| 944 | /* |
| 945 | * Return 1 to indicate success since the 0 value can be a socket |
| 946 | * shutdown during the recv() or send() call. |
| 947 | */ |
| 948 | health_code_update(); |
| 949 | return 1; |
| 950 | |
| 951 | error_fatal: |
| 952 | rcu_read_unlock(); |
| 953 | /* This will issue a consumer stop. */ |
| 954 | return -1; |
| 955 | } |
| 956 | |
| 957 | /* |
| 958 | * Populate index values of a kernel stream. Values are set in big endian order. |
| 959 | * |
| 960 | * Return 0 on success or else a negative value. |
| 961 | */ |
| 962 | static int get_index_values(struct ctf_packet_index *index, int infd) |
| 963 | { |
| 964 | int ret; |
| 965 | |
| 966 | ret = kernctl_get_timestamp_begin(infd, &index->timestamp_begin); |
| 967 | if (ret < 0) { |
| 968 | PERROR("kernctl_get_timestamp_begin"); |
| 969 | goto error; |
| 970 | } |
| 971 | index->timestamp_begin = htobe64(index->timestamp_begin); |
| 972 | |
| 973 | ret = kernctl_get_timestamp_end(infd, &index->timestamp_end); |
| 974 | if (ret < 0) { |
| 975 | PERROR("kernctl_get_timestamp_end"); |
| 976 | goto error; |
| 977 | } |
| 978 | index->timestamp_end = htobe64(index->timestamp_end); |
| 979 | |
| 980 | ret = kernctl_get_events_discarded(infd, &index->events_discarded); |
| 981 | if (ret < 0) { |
| 982 | PERROR("kernctl_get_events_discarded"); |
| 983 | goto error; |
| 984 | } |
| 985 | index->events_discarded = htobe64(index->events_discarded); |
| 986 | |
| 987 | ret = kernctl_get_content_size(infd, &index->content_size); |
| 988 | if (ret < 0) { |
| 989 | PERROR("kernctl_get_content_size"); |
| 990 | goto error; |
| 991 | } |
| 992 | index->content_size = htobe64(index->content_size); |
| 993 | |
| 994 | ret = kernctl_get_packet_size(infd, &index->packet_size); |
| 995 | if (ret < 0) { |
| 996 | PERROR("kernctl_get_packet_size"); |
| 997 | goto error; |
| 998 | } |
| 999 | index->packet_size = htobe64(index->packet_size); |
| 1000 | |
| 1001 | ret = kernctl_get_stream_id(infd, &index->stream_id); |
| 1002 | if (ret < 0) { |
| 1003 | PERROR("kernctl_get_stream_id"); |
| 1004 | goto error; |
| 1005 | } |
| 1006 | index->stream_id = htobe64(index->stream_id); |
| 1007 | |
| 1008 | error: |
| 1009 | return ret; |
| 1010 | } |
| 1011 | /* |
| 1012 | * Sync metadata meaning request them to the session daemon and snapshot to the |
| 1013 | * metadata thread can consumer them. |
| 1014 | * |
| 1015 | * Metadata stream lock MUST be acquired. |
| 1016 | * |
| 1017 | * Return 0 if new metadatda is available, EAGAIN if the metadata stream |
| 1018 | * is empty or a negative value on error. |
| 1019 | */ |
| 1020 | int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream *metadata) |
| 1021 | { |
| 1022 | int ret; |
| 1023 | |
| 1024 | assert(metadata); |
| 1025 | |
| 1026 | ret = kernctl_buffer_flush(metadata->wait_fd); |
| 1027 | if (ret < 0) { |
| 1028 | ERR("Failed to flush kernel stream"); |
| 1029 | goto end; |
| 1030 | } |
| 1031 | |
| 1032 | ret = kernctl_snapshot(metadata->wait_fd); |
| 1033 | if (ret < 0) { |
| 1034 | if (errno != EAGAIN) { |
| 1035 | ERR("Sync metadata, taking kernel snapshot failed."); |
| 1036 | goto end; |
| 1037 | } |
| 1038 | DBG("Sync metadata, no new kernel metadata"); |
| 1039 | /* No new metadata, exit. */ |
| 1040 | ret = ENODATA; |
| 1041 | goto end; |
| 1042 | } |
| 1043 | |
| 1044 | end: |
| 1045 | return ret; |
| 1046 | } |
| 1047 | |
| 1048 | /* |
| 1049 | * Consume data on a file descriptor and write it on a trace file. |
| 1050 | */ |
| 1051 | ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream, |
| 1052 | struct lttng_consumer_local_data *ctx) |
| 1053 | { |
| 1054 | unsigned long len, subbuf_size, padding; |
| 1055 | int err, write_index = 1; |
| 1056 | ssize_t ret = 0; |
| 1057 | int infd = stream->wait_fd; |
| 1058 | struct ctf_packet_index index; |
| 1059 | |
| 1060 | DBG("In read_subbuffer (infd : %d)", infd); |
| 1061 | |
| 1062 | /* Get the next subbuffer */ |
| 1063 | err = kernctl_get_next_subbuf(infd); |
| 1064 | if (err != 0) { |
| 1065 | /* |
| 1066 | * This is a debug message even for single-threaded consumer, |
| 1067 | * because poll() have more relaxed criterions than get subbuf, |
| 1068 | * so get_subbuf may fail for short race windows where poll() |
| 1069 | * would issue wakeups. |
| 1070 | */ |
| 1071 | DBG("Reserving sub buffer failed (everything is normal, " |
| 1072 | "it is due to concurrency)"); |
| 1073 | ret = -errno; |
| 1074 | goto end; |
| 1075 | } |
| 1076 | |
| 1077 | /* Get the full subbuffer size including padding */ |
| 1078 | err = kernctl_get_padded_subbuf_size(infd, &len); |
| 1079 | if (err != 0) { |
| 1080 | perror("Getting sub-buffer len failed."); |
| 1081 | ret = -errno; |
| 1082 | goto end; |
| 1083 | } |
| 1084 | |
| 1085 | if (!stream->metadata_flag) { |
| 1086 | ret = get_index_values(&index, infd); |
| 1087 | if (ret < 0) { |
| 1088 | goto end; |
| 1089 | } |
| 1090 | } else { |
| 1091 | write_index = 0; |
| 1092 | } |
| 1093 | |
| 1094 | switch (stream->chan->output) { |
| 1095 | case CONSUMER_CHANNEL_SPLICE: |
| 1096 | /* |
| 1097 | * XXX: The lttng-modules splice "actor" does not handle copying |
| 1098 | * partial pages hence only using the subbuffer size without the |
| 1099 | * padding makes the splice fail. |
| 1100 | */ |
| 1101 | subbuf_size = len; |
| 1102 | padding = 0; |
| 1103 | |
| 1104 | /* splice the subbuffer to the tracefile */ |
| 1105 | ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, subbuf_size, |
| 1106 | padding, &index); |
| 1107 | /* |
| 1108 | * XXX: Splice does not support network streaming so the return value |
| 1109 | * is simply checked against subbuf_size and not like the mmap() op. |
| 1110 | */ |
| 1111 | if (ret != subbuf_size) { |
| 1112 | /* |
| 1113 | * display the error but continue processing to try |
| 1114 | * to release the subbuffer |
| 1115 | */ |
| 1116 | ERR("Error splicing to tracefile (ret: %zd != len: %lu)", |
| 1117 | ret, subbuf_size); |
| 1118 | write_index = 0; |
| 1119 | } |
| 1120 | break; |
| 1121 | case CONSUMER_CHANNEL_MMAP: |
| 1122 | /* Get subbuffer size without padding */ |
| 1123 | err = kernctl_get_subbuf_size(infd, &subbuf_size); |
| 1124 | if (err != 0) { |
| 1125 | perror("Getting sub-buffer len failed."); |
| 1126 | ret = -errno; |
| 1127 | goto end; |
| 1128 | } |
| 1129 | |
| 1130 | /* Make sure the tracer is not gone mad on us! */ |
| 1131 | assert(len >= subbuf_size); |
| 1132 | |
| 1133 | padding = len - subbuf_size; |
| 1134 | |
| 1135 | /* write the subbuffer to the tracefile */ |
| 1136 | ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size, |
| 1137 | padding, &index); |
| 1138 | /* |
| 1139 | * The mmap operation should write subbuf_size amount of data when |
| 1140 | * network streaming or the full padding (len) size when we are _not_ |
| 1141 | * streaming. |
| 1142 | */ |
| 1143 | if ((ret != subbuf_size && stream->net_seq_idx != (uint64_t) -1ULL) || |
| 1144 | (ret != len && stream->net_seq_idx == (uint64_t) -1ULL)) { |
| 1145 | /* |
| 1146 | * Display the error but continue processing to try to release the |
| 1147 | * subbuffer. This is a DBG statement since this is possible to |
| 1148 | * happen without being a critical error. |
| 1149 | */ |
| 1150 | DBG("Error writing to tracefile " |
| 1151 | "(ret: %zd != len: %lu != subbuf_size: %lu)", |
| 1152 | ret, len, subbuf_size); |
| 1153 | write_index = 0; |
| 1154 | } |
| 1155 | break; |
| 1156 | default: |
| 1157 | ERR("Unknown output method"); |
| 1158 | ret = -EPERM; |
| 1159 | } |
| 1160 | |
| 1161 | err = kernctl_put_next_subbuf(infd); |
| 1162 | if (err != 0) { |
| 1163 | if (errno == EFAULT) { |
| 1164 | perror("Error in unreserving sub buffer\n"); |
| 1165 | } else if (errno == EIO) { |
| 1166 | /* Should never happen with newer LTTng versions */ |
| 1167 | perror("Reader has been pushed by the writer, last sub-buffer corrupted."); |
| 1168 | } |
| 1169 | ret = -errno; |
| 1170 | goto end; |
| 1171 | } |
| 1172 | |
| 1173 | /* Write index if needed. */ |
| 1174 | if (!write_index) { |
| 1175 | goto end; |
| 1176 | } |
| 1177 | |
| 1178 | if (stream->chan->live_timer_interval && !stream->metadata_flag) { |
| 1179 | /* |
| 1180 | * In live, block until all the metadata is sent. |
| 1181 | */ |
| 1182 | err = consumer_stream_sync_metadata(ctx, stream->session_id); |
| 1183 | if (err < 0) { |
| 1184 | goto end; |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | err = consumer_stream_write_index(stream, &index); |
| 1189 | if (err < 0) { |
| 1190 | goto end; |
| 1191 | } |
| 1192 | |
| 1193 | end: |
| 1194 | return ret; |
| 1195 | } |
| 1196 | |
| 1197 | int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream) |
| 1198 | { |
| 1199 | int ret; |
| 1200 | |
| 1201 | assert(stream); |
| 1202 | |
| 1203 | /* |
| 1204 | * Don't create anything if this is set for streaming or should not be |
| 1205 | * monitored. |
| 1206 | */ |
| 1207 | if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor) { |
| 1208 | ret = utils_create_stream_file(stream->chan->pathname, stream->name, |
| 1209 | stream->chan->tracefile_size, stream->tracefile_count_current, |
| 1210 | stream->uid, stream->gid, NULL); |
| 1211 | if (ret < 0) { |
| 1212 | goto error; |
| 1213 | } |
| 1214 | stream->out_fd = ret; |
| 1215 | stream->tracefile_size_current = 0; |
| 1216 | |
| 1217 | if (!stream->metadata_flag) { |
| 1218 | ret = index_create_file(stream->chan->pathname, |
| 1219 | stream->name, stream->uid, stream->gid, |
| 1220 | stream->chan->tracefile_size, |
| 1221 | stream->tracefile_count_current); |
| 1222 | if (ret < 0) { |
| 1223 | goto error; |
| 1224 | } |
| 1225 | stream->index_fd = ret; |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | if (stream->output == LTTNG_EVENT_MMAP) { |
| 1230 | /* get the len of the mmap region */ |
| 1231 | unsigned long mmap_len; |
| 1232 | |
| 1233 | ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len); |
| 1234 | if (ret != 0) { |
| 1235 | PERROR("kernctl_get_mmap_len"); |
| 1236 | ret = -errno; |
| 1237 | goto error_close_fd; |
| 1238 | } |
| 1239 | stream->mmap_len = (size_t) mmap_len; |
| 1240 | |
| 1241 | stream->mmap_base = mmap(NULL, stream->mmap_len, PROT_READ, |
| 1242 | MAP_PRIVATE, stream->wait_fd, 0); |
| 1243 | if (stream->mmap_base == MAP_FAILED) { |
| 1244 | PERROR("Error mmaping"); |
| 1245 | ret = -1; |
| 1246 | goto error_close_fd; |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | /* we return 0 to let the library handle the FD internally */ |
| 1251 | return 0; |
| 1252 | |
| 1253 | error_close_fd: |
| 1254 | if (stream->out_fd >= 0) { |
| 1255 | int err; |
| 1256 | |
| 1257 | err = close(stream->out_fd); |
| 1258 | assert(!err); |
| 1259 | stream->out_fd = -1; |
| 1260 | } |
| 1261 | error: |
| 1262 | return ret; |
| 1263 | } |
| 1264 | |
| 1265 | /* |
| 1266 | * Check if data is still being extracted from the buffers for a specific |
| 1267 | * stream. Consumer data lock MUST be acquired before calling this function |
| 1268 | * and the stream lock. |
| 1269 | * |
| 1270 | * Return 1 if the traced data are still getting read else 0 meaning that the |
| 1271 | * data is available for trace viewer reading. |
| 1272 | */ |
| 1273 | int lttng_kconsumer_data_pending(struct lttng_consumer_stream *stream) |
| 1274 | { |
| 1275 | int ret; |
| 1276 | |
| 1277 | assert(stream); |
| 1278 | |
| 1279 | if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) { |
| 1280 | ret = 0; |
| 1281 | goto end; |
| 1282 | } |
| 1283 | |
| 1284 | ret = kernctl_get_next_subbuf(stream->wait_fd); |
| 1285 | if (ret == 0) { |
| 1286 | /* There is still data so let's put back this subbuffer. */ |
| 1287 | ret = kernctl_put_subbuf(stream->wait_fd); |
| 1288 | assert(ret == 0); |
| 1289 | ret = 1; /* Data is pending */ |
| 1290 | goto end; |
| 1291 | } |
| 1292 | |
| 1293 | /* Data is NOT pending and ready to be read. */ |
| 1294 | ret = 0; |
| 1295 | |
| 1296 | end: |
| 1297 | return ret; |
| 1298 | } |