| 1 | /* |
| 2 | * Copyright (C) 2011 Julien Desfossez <julien.desfossez@polymtl.ca> |
| 3 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 4 | * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0-only |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | #define _LGPL_SOURCE |
| 11 | #include <poll.h> |
| 12 | #include <pthread.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 | #include <sys/mman.h> |
| 16 | #include <sys/socket.h> |
| 17 | #include <sys/types.h> |
| 18 | #include <inttypes.h> |
| 19 | #include <unistd.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <stdint.h> |
| 22 | |
| 23 | #include <bin/lttng-consumerd/health-consumerd.h> |
| 24 | #include <common/common.h> |
| 25 | #include <common/kernel-ctl/kernel-ctl.h> |
| 26 | #include <common/sessiond-comm/sessiond-comm.h> |
| 27 | #include <common/sessiond-comm/relayd.h> |
| 28 | #include <common/compat/fcntl.h> |
| 29 | #include <common/compat/endian.h> |
| 30 | #include <common/pipe.h> |
| 31 | #include <common/relayd/relayd.h> |
| 32 | #include <common/utils.h> |
| 33 | #include <common/consumer/consumer-stream.h> |
| 34 | #include <common/index/index.h> |
| 35 | #include <common/consumer/consumer-timer.h> |
| 36 | #include <common/optional.h> |
| 37 | #include <common/buffer-view.h> |
| 38 | #include <common/consumer/consumer.h> |
| 39 | #include <common/consumer/metadata-bucket.h> |
| 40 | |
| 41 | #include "kernel-consumer.h" |
| 42 | |
| 43 | extern struct lttng_consumer_global_data the_consumer_data; |
| 44 | extern int consumer_poll_timeout; |
| 45 | |
| 46 | /* |
| 47 | * Take a snapshot for a specific fd |
| 48 | * |
| 49 | * Returns 0 on success, < 0 on error |
| 50 | */ |
| 51 | int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream *stream) |
| 52 | { |
| 53 | int ret = 0; |
| 54 | int infd = stream->wait_fd; |
| 55 | |
| 56 | ret = kernctl_snapshot(infd); |
| 57 | /* |
| 58 | * -EAGAIN is not an error, it just means that there is no data to |
| 59 | * be read. |
| 60 | */ |
| 61 | if (ret != 0 && ret != -EAGAIN) { |
| 62 | PERROR("Getting sub-buffer snapshot."); |
| 63 | } |
| 64 | |
| 65 | return ret; |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * Sample consumed and produced positions for a specific fd. |
| 70 | * |
| 71 | * Returns 0 on success, < 0 on error. |
| 72 | */ |
| 73 | int lttng_kconsumer_sample_snapshot_positions( |
| 74 | struct lttng_consumer_stream *stream) |
| 75 | { |
| 76 | LTTNG_ASSERT(stream); |
| 77 | |
| 78 | return kernctl_snapshot_sample_positions(stream->wait_fd); |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * Get the produced position |
| 83 | * |
| 84 | * Returns 0 on success, < 0 on error |
| 85 | */ |
| 86 | int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream *stream, |
| 87 | unsigned long *pos) |
| 88 | { |
| 89 | int ret; |
| 90 | int infd = stream->wait_fd; |
| 91 | |
| 92 | ret = kernctl_snapshot_get_produced(infd, pos); |
| 93 | if (ret != 0) { |
| 94 | PERROR("kernctl_snapshot_get_produced"); |
| 95 | } |
| 96 | |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Get the consumerd position |
| 102 | * |
| 103 | * Returns 0 on success, < 0 on error |
| 104 | */ |
| 105 | int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream *stream, |
| 106 | unsigned long *pos) |
| 107 | { |
| 108 | int ret; |
| 109 | int infd = stream->wait_fd; |
| 110 | |
| 111 | ret = kernctl_snapshot_get_consumed(infd, pos); |
| 112 | if (ret != 0) { |
| 113 | PERROR("kernctl_snapshot_get_consumed"); |
| 114 | } |
| 115 | |
| 116 | return ret; |
| 117 | } |
| 118 | |
| 119 | static |
| 120 | int get_current_subbuf_addr(struct lttng_consumer_stream *stream, |
| 121 | const char **addr) |
| 122 | { |
| 123 | int ret; |
| 124 | unsigned long mmap_offset; |
| 125 | const char *mmap_base = (const char *) stream->mmap_base; |
| 126 | |
| 127 | ret = kernctl_get_mmap_read_offset(stream->wait_fd, &mmap_offset); |
| 128 | if (ret < 0) { |
| 129 | PERROR("Failed to get mmap read offset"); |
| 130 | goto error; |
| 131 | } |
| 132 | |
| 133 | *addr = mmap_base + mmap_offset; |
| 134 | error: |
| 135 | return ret; |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * Take a snapshot of all the stream of a channel |
| 140 | * RCU read-side lock must be held across this function to ensure existence of |
| 141 | * channel. |
| 142 | * |
| 143 | * Returns 0 on success, < 0 on error |
| 144 | */ |
| 145 | static int lttng_kconsumer_snapshot_channel( |
| 146 | struct lttng_consumer_channel *channel, |
| 147 | uint64_t key, char *path, uint64_t relayd_id, |
| 148 | uint64_t nb_packets_per_stream, |
| 149 | struct lttng_consumer_local_data *ctx) |
| 150 | { |
| 151 | int ret; |
| 152 | struct lttng_consumer_stream *stream; |
| 153 | |
| 154 | DBG("Kernel consumer snapshot channel %" PRIu64, key); |
| 155 | |
| 156 | /* Prevent channel modifications while we perform the snapshot.*/ |
| 157 | pthread_mutex_lock(&channel->lock); |
| 158 | |
| 159 | rcu_read_lock(); |
| 160 | |
| 161 | /* Splice is not supported yet for channel snapshot. */ |
| 162 | if (channel->output != CONSUMER_CHANNEL_MMAP) { |
| 163 | ERR("Unsupported output type for channel \"%s\": mmap output is required to record a snapshot", |
| 164 | channel->name); |
| 165 | ret = -1; |
| 166 | goto end; |
| 167 | } |
| 168 | |
| 169 | cds_list_for_each_entry(stream, &channel->streams.head, send_node) { |
| 170 | unsigned long consumed_pos, produced_pos; |
| 171 | |
| 172 | health_code_update(); |
| 173 | |
| 174 | /* |
| 175 | * Lock stream because we are about to change its state. |
| 176 | */ |
| 177 | pthread_mutex_lock(&stream->lock); |
| 178 | |
| 179 | LTTNG_ASSERT(channel->trace_chunk); |
| 180 | if (!lttng_trace_chunk_get(channel->trace_chunk)) { |
| 181 | /* |
| 182 | * Can't happen barring an internal error as the channel |
| 183 | * holds a reference to the trace chunk. |
| 184 | */ |
| 185 | ERR("Failed to acquire reference to channel's trace chunk"); |
| 186 | ret = -1; |
| 187 | goto end_unlock; |
| 188 | } |
| 189 | LTTNG_ASSERT(!stream->trace_chunk); |
| 190 | stream->trace_chunk = channel->trace_chunk; |
| 191 | |
| 192 | /* |
| 193 | * Assign the received relayd ID so we can use it for streaming. The streams |
| 194 | * are not visible to anyone so this is OK to change it. |
| 195 | */ |
| 196 | stream->net_seq_idx = relayd_id; |
| 197 | channel->relayd_id = relayd_id; |
| 198 | if (relayd_id != (uint64_t) -1ULL) { |
| 199 | ret = consumer_send_relayd_stream(stream, path); |
| 200 | if (ret < 0) { |
| 201 | ERR("sending stream to relayd"); |
| 202 | goto end_unlock; |
| 203 | } |
| 204 | } else { |
| 205 | ret = consumer_stream_create_output_files(stream, |
| 206 | false); |
| 207 | if (ret < 0) { |
| 208 | goto end_unlock; |
| 209 | } |
| 210 | DBG("Kernel consumer snapshot stream (%" PRIu64 ")", |
| 211 | stream->key); |
| 212 | } |
| 213 | |
| 214 | ret = kernctl_buffer_flush_empty(stream->wait_fd); |
| 215 | if (ret < 0) { |
| 216 | /* |
| 217 | * Doing a buffer flush which does not take into |
| 218 | * account empty packets. This is not perfect |
| 219 | * for stream intersection, but required as a |
| 220 | * fall-back when "flush_empty" is not |
| 221 | * implemented by lttng-modules. |
| 222 | */ |
| 223 | ret = kernctl_buffer_flush(stream->wait_fd); |
| 224 | if (ret < 0) { |
| 225 | ERR("Failed to flush kernel stream"); |
| 226 | goto end_unlock; |
| 227 | } |
| 228 | goto end_unlock; |
| 229 | } |
| 230 | |
| 231 | ret = lttng_kconsumer_take_snapshot(stream); |
| 232 | if (ret < 0) { |
| 233 | ERR("Taking kernel snapshot"); |
| 234 | goto end_unlock; |
| 235 | } |
| 236 | |
| 237 | ret = lttng_kconsumer_get_produced_snapshot(stream, &produced_pos); |
| 238 | if (ret < 0) { |
| 239 | ERR("Produced kernel snapshot position"); |
| 240 | goto end_unlock; |
| 241 | } |
| 242 | |
| 243 | ret = lttng_kconsumer_get_consumed_snapshot(stream, &consumed_pos); |
| 244 | if (ret < 0) { |
| 245 | ERR("Consumerd kernel snapshot position"); |
| 246 | goto end_unlock; |
| 247 | } |
| 248 | |
| 249 | consumed_pos = consumer_get_consume_start_pos(consumed_pos, |
| 250 | produced_pos, nb_packets_per_stream, |
| 251 | stream->max_sb_size); |
| 252 | |
| 253 | while ((long) (consumed_pos - produced_pos) < 0) { |
| 254 | ssize_t read_len; |
| 255 | unsigned long len, padded_len; |
| 256 | const char *subbuf_addr; |
| 257 | struct lttng_buffer_view subbuf_view; |
| 258 | |
| 259 | health_code_update(); |
| 260 | DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos); |
| 261 | |
| 262 | ret = kernctl_get_subbuf(stream->wait_fd, &consumed_pos); |
| 263 | if (ret < 0) { |
| 264 | if (ret != -EAGAIN) { |
| 265 | PERROR("kernctl_get_subbuf snapshot"); |
| 266 | goto end_unlock; |
| 267 | } |
| 268 | DBG("Kernel consumer get subbuf failed. Skipping it."); |
| 269 | consumed_pos += stream->max_sb_size; |
| 270 | stream->chan->lost_packets++; |
| 271 | continue; |
| 272 | } |
| 273 | |
| 274 | ret = kernctl_get_subbuf_size(stream->wait_fd, &len); |
| 275 | if (ret < 0) { |
| 276 | ERR("Snapshot kernctl_get_subbuf_size"); |
| 277 | goto error_put_subbuf; |
| 278 | } |
| 279 | |
| 280 | ret = kernctl_get_padded_subbuf_size(stream->wait_fd, &padded_len); |
| 281 | if (ret < 0) { |
| 282 | ERR("Snapshot kernctl_get_padded_subbuf_size"); |
| 283 | goto error_put_subbuf; |
| 284 | } |
| 285 | |
| 286 | ret = get_current_subbuf_addr(stream, &subbuf_addr); |
| 287 | if (ret) { |
| 288 | goto error_put_subbuf; |
| 289 | } |
| 290 | |
| 291 | subbuf_view = lttng_buffer_view_init( |
| 292 | subbuf_addr, 0, padded_len); |
| 293 | read_len = lttng_consumer_on_read_subbuffer_mmap( |
| 294 | stream, &subbuf_view, |
| 295 | padded_len - len); |
| 296 | /* |
| 297 | * We write the padded len in local tracefiles but the data len |
| 298 | * when using a relay. Display the error but continue processing |
| 299 | * to try to release the subbuffer. |
| 300 | */ |
| 301 | if (relayd_id != (uint64_t) -1ULL) { |
| 302 | if (read_len != len) { |
| 303 | ERR("Error sending to the relay (ret: %zd != len: %lu)", |
| 304 | read_len, len); |
| 305 | } |
| 306 | } else { |
| 307 | if (read_len != padded_len) { |
| 308 | ERR("Error writing to tracefile (ret: %zd != len: %lu)", |
| 309 | read_len, padded_len); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | ret = kernctl_put_subbuf(stream->wait_fd); |
| 314 | if (ret < 0) { |
| 315 | ERR("Snapshot kernctl_put_subbuf"); |
| 316 | goto end_unlock; |
| 317 | } |
| 318 | consumed_pos += stream->max_sb_size; |
| 319 | } |
| 320 | |
| 321 | if (relayd_id == (uint64_t) -1ULL) { |
| 322 | if (stream->out_fd >= 0) { |
| 323 | ret = close(stream->out_fd); |
| 324 | if (ret < 0) { |
| 325 | PERROR("Kernel consumer snapshot close out_fd"); |
| 326 | goto end_unlock; |
| 327 | } |
| 328 | stream->out_fd = -1; |
| 329 | } |
| 330 | } else { |
| 331 | close_relayd_stream(stream); |
| 332 | stream->net_seq_idx = (uint64_t) -1ULL; |
| 333 | } |
| 334 | lttng_trace_chunk_put(stream->trace_chunk); |
| 335 | stream->trace_chunk = NULL; |
| 336 | pthread_mutex_unlock(&stream->lock); |
| 337 | } |
| 338 | |
| 339 | /* All good! */ |
| 340 | ret = 0; |
| 341 | goto end; |
| 342 | |
| 343 | error_put_subbuf: |
| 344 | ret = kernctl_put_subbuf(stream->wait_fd); |
| 345 | if (ret < 0) { |
| 346 | ERR("Snapshot kernctl_put_subbuf error path"); |
| 347 | } |
| 348 | end_unlock: |
| 349 | pthread_mutex_unlock(&stream->lock); |
| 350 | end: |
| 351 | rcu_read_unlock(); |
| 352 | pthread_mutex_unlock(&channel->lock); |
| 353 | return ret; |
| 354 | } |
| 355 | |
| 356 | /* |
| 357 | * Read the whole metadata available for a snapshot. |
| 358 | * RCU read-side lock must be held across this function to ensure existence of |
| 359 | * metadata_channel. |
| 360 | * |
| 361 | * Returns 0 on success, < 0 on error |
| 362 | */ |
| 363 | static int lttng_kconsumer_snapshot_metadata( |
| 364 | struct lttng_consumer_channel *metadata_channel, |
| 365 | uint64_t key, char *path, uint64_t relayd_id, |
| 366 | struct lttng_consumer_local_data *ctx) |
| 367 | { |
| 368 | int ret, use_relayd = 0; |
| 369 | ssize_t ret_read; |
| 370 | struct lttng_consumer_stream *metadata_stream; |
| 371 | |
| 372 | LTTNG_ASSERT(ctx); |
| 373 | |
| 374 | DBG("Kernel consumer snapshot metadata with key %" PRIu64 " at path %s", |
| 375 | key, path); |
| 376 | |
| 377 | rcu_read_lock(); |
| 378 | |
| 379 | metadata_stream = metadata_channel->metadata_stream; |
| 380 | LTTNG_ASSERT(metadata_stream); |
| 381 | |
| 382 | metadata_stream->read_subbuffer_ops.lock(metadata_stream); |
| 383 | LTTNG_ASSERT(metadata_channel->trace_chunk); |
| 384 | LTTNG_ASSERT(metadata_stream->trace_chunk); |
| 385 | |
| 386 | /* Flag once that we have a valid relayd for the stream. */ |
| 387 | if (relayd_id != (uint64_t) -1ULL) { |
| 388 | use_relayd = 1; |
| 389 | } |
| 390 | |
| 391 | if (use_relayd) { |
| 392 | ret = consumer_send_relayd_stream(metadata_stream, path); |
| 393 | if (ret < 0) { |
| 394 | goto error_snapshot; |
| 395 | } |
| 396 | } else { |
| 397 | ret = consumer_stream_create_output_files(metadata_stream, |
| 398 | false); |
| 399 | if (ret < 0) { |
| 400 | goto error_snapshot; |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | do { |
| 405 | health_code_update(); |
| 406 | |
| 407 | ret_read = lttng_consumer_read_subbuffer(metadata_stream, ctx, true); |
| 408 | if (ret_read < 0) { |
| 409 | ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)", |
| 410 | ret_read); |
| 411 | ret = ret_read; |
| 412 | goto error_snapshot; |
| 413 | } |
| 414 | } while (ret_read > 0); |
| 415 | |
| 416 | if (use_relayd) { |
| 417 | close_relayd_stream(metadata_stream); |
| 418 | metadata_stream->net_seq_idx = (uint64_t) -1ULL; |
| 419 | } else { |
| 420 | if (metadata_stream->out_fd >= 0) { |
| 421 | ret = close(metadata_stream->out_fd); |
| 422 | if (ret < 0) { |
| 423 | PERROR("Kernel consumer snapshot metadata close out_fd"); |
| 424 | /* |
| 425 | * Don't go on error here since the snapshot was successful at this |
| 426 | * point but somehow the close failed. |
| 427 | */ |
| 428 | } |
| 429 | metadata_stream->out_fd = -1; |
| 430 | lttng_trace_chunk_put(metadata_stream->trace_chunk); |
| 431 | metadata_stream->trace_chunk = NULL; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | ret = 0; |
| 436 | error_snapshot: |
| 437 | metadata_stream->read_subbuffer_ops.unlock(metadata_stream); |
| 438 | cds_list_del(&metadata_stream->send_node); |
| 439 | consumer_stream_destroy(metadata_stream, NULL); |
| 440 | metadata_channel->metadata_stream = NULL; |
| 441 | rcu_read_unlock(); |
| 442 | return ret; |
| 443 | } |
| 444 | |
| 445 | /* |
| 446 | * Receive command from session daemon and process it. |
| 447 | * |
| 448 | * Return 1 on success else a negative value or 0. |
| 449 | */ |
| 450 | int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx, |
| 451 | int sock, struct pollfd *consumer_sockpoll) |
| 452 | { |
| 453 | int ret_func; |
| 454 | enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS; |
| 455 | struct lttcomm_consumer_msg msg; |
| 456 | |
| 457 | health_code_update(); |
| 458 | |
| 459 | { |
| 460 | ssize_t ret_recv; |
| 461 | |
| 462 | ret_recv = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg)); |
| 463 | if (ret_recv != sizeof(msg)) { |
| 464 | if (ret_recv > 0) { |
| 465 | lttng_consumer_send_error(ctx, |
| 466 | LTTCOMM_CONSUMERD_ERROR_RECV_CMD); |
| 467 | ret_recv = -1; |
| 468 | } |
| 469 | return ret_recv; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | health_code_update(); |
| 474 | |
| 475 | /* Deprecated command */ |
| 476 | LTTNG_ASSERT(msg.cmd_type != LTTNG_CONSUMER_STOP); |
| 477 | |
| 478 | health_code_update(); |
| 479 | |
| 480 | /* relayd needs RCU read-side protection */ |
| 481 | rcu_read_lock(); |
| 482 | |
| 483 | switch (msg.cmd_type) { |
| 484 | case LTTNG_CONSUMER_ADD_RELAYD_SOCKET: |
| 485 | { |
| 486 | /* Session daemon status message are handled in the following call. */ |
| 487 | consumer_add_relayd_socket(msg.u.relayd_sock.net_index, |
| 488 | msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll, |
| 489 | &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id, |
| 490 | msg.u.relayd_sock.relayd_session_id); |
| 491 | goto end_nosignal; |
| 492 | } |
| 493 | case LTTNG_CONSUMER_ADD_CHANNEL: |
| 494 | { |
| 495 | struct lttng_consumer_channel *new_channel; |
| 496 | int ret_send_status, ret_add_channel = 0; |
| 497 | const uint64_t chunk_id = msg.u.channel.chunk_id.value; |
| 498 | |
| 499 | health_code_update(); |
| 500 | |
| 501 | /* First send a status message before receiving the fds. */ |
| 502 | ret_send_status = consumer_send_status_msg(sock, ret_code); |
| 503 | if (ret_send_status < 0) { |
| 504 | /* Somehow, the session daemon is not responding anymore. */ |
| 505 | goto error_fatal; |
| 506 | } |
| 507 | |
| 508 | health_code_update(); |
| 509 | |
| 510 | DBG("consumer_add_channel %" PRIu64, msg.u.channel.channel_key); |
| 511 | new_channel = consumer_allocate_channel(msg.u.channel.channel_key, |
| 512 | msg.u.channel.session_id, |
| 513 | msg.u.channel.chunk_id.is_set ? |
| 514 | &chunk_id : NULL, |
| 515 | msg.u.channel.pathname, |
| 516 | msg.u.channel.name, |
| 517 | msg.u.channel.relayd_id, msg.u.channel.output, |
| 518 | msg.u.channel.tracefile_size, |
| 519 | msg.u.channel.tracefile_count, 0, |
| 520 | msg.u.channel.monitor, |
| 521 | msg.u.channel.live_timer_interval, |
| 522 | msg.u.channel.is_live, |
| 523 | NULL, NULL); |
| 524 | if (new_channel == NULL) { |
| 525 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR); |
| 526 | goto end_nosignal; |
| 527 | } |
| 528 | new_channel->nb_init_stream_left = msg.u.channel.nb_init_streams; |
| 529 | switch (msg.u.channel.output) { |
| 530 | case LTTNG_EVENT_SPLICE: |
| 531 | new_channel->output = CONSUMER_CHANNEL_SPLICE; |
| 532 | break; |
| 533 | case LTTNG_EVENT_MMAP: |
| 534 | new_channel->output = CONSUMER_CHANNEL_MMAP; |
| 535 | break; |
| 536 | default: |
| 537 | ERR("Channel output unknown %d", msg.u.channel.output); |
| 538 | goto end_nosignal; |
| 539 | } |
| 540 | |
| 541 | /* Translate and save channel type. */ |
| 542 | switch (msg.u.channel.type) { |
| 543 | case CONSUMER_CHANNEL_TYPE_DATA: |
| 544 | case CONSUMER_CHANNEL_TYPE_METADATA: |
| 545 | new_channel->type = (consumer_channel_type) msg.u.channel.type; |
| 546 | break; |
| 547 | default: |
| 548 | abort(); |
| 549 | goto end_nosignal; |
| 550 | }; |
| 551 | |
| 552 | health_code_update(); |
| 553 | |
| 554 | if (ctx->on_recv_channel != NULL) { |
| 555 | int ret_recv_channel = |
| 556 | ctx->on_recv_channel(new_channel); |
| 557 | if (ret_recv_channel == 0) { |
| 558 | ret_add_channel = consumer_add_channel( |
| 559 | new_channel, ctx); |
| 560 | } else if (ret_recv_channel < 0) { |
| 561 | goto end_nosignal; |
| 562 | } |
| 563 | } else { |
| 564 | ret_add_channel = |
| 565 | consumer_add_channel(new_channel, ctx); |
| 566 | } |
| 567 | if (msg.u.channel.type == CONSUMER_CHANNEL_TYPE_DATA && |
| 568 | !ret_add_channel) { |
| 569 | int monitor_start_ret; |
| 570 | |
| 571 | DBG("Consumer starting monitor timer"); |
| 572 | consumer_timer_live_start(new_channel, |
| 573 | msg.u.channel.live_timer_interval); |
| 574 | monitor_start_ret = consumer_timer_monitor_start( |
| 575 | new_channel, |
| 576 | msg.u.channel.monitor_timer_interval); |
| 577 | if (monitor_start_ret < 0) { |
| 578 | ERR("Starting channel monitoring timer failed"); |
| 579 | goto end_nosignal; |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | health_code_update(); |
| 584 | |
| 585 | /* If we received an error in add_channel, we need to report it. */ |
| 586 | if (ret_add_channel < 0) { |
| 587 | ret_send_status = consumer_send_status_msg( |
| 588 | sock, ret_add_channel); |
| 589 | if (ret_send_status < 0) { |
| 590 | goto error_fatal; |
| 591 | } |
| 592 | goto end_nosignal; |
| 593 | } |
| 594 | |
| 595 | goto end_nosignal; |
| 596 | } |
| 597 | case LTTNG_CONSUMER_ADD_STREAM: |
| 598 | { |
| 599 | int fd; |
| 600 | struct lttng_pipe *stream_pipe; |
| 601 | struct lttng_consumer_stream *new_stream; |
| 602 | struct lttng_consumer_channel *channel; |
| 603 | int alloc_ret = 0; |
| 604 | int ret_send_status, ret_poll, ret_get_max_subbuf_size; |
| 605 | ssize_t ret_pipe_write, ret_recv; |
| 606 | |
| 607 | /* |
| 608 | * Get stream's channel reference. Needed when adding the stream to the |
| 609 | * global hash table. |
| 610 | */ |
| 611 | channel = consumer_find_channel(msg.u.stream.channel_key); |
| 612 | if (!channel) { |
| 613 | /* |
| 614 | * We could not find the channel. Can happen if cpu hotplug |
| 615 | * happens while tearing down. |
| 616 | */ |
| 617 | ERR("Unable to find channel key %" PRIu64, msg.u.stream.channel_key); |
| 618 | ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND; |
| 619 | } |
| 620 | |
| 621 | health_code_update(); |
| 622 | |
| 623 | /* First send a status message before receiving the fds. */ |
| 624 | ret_send_status = consumer_send_status_msg(sock, ret_code); |
| 625 | if (ret_send_status < 0) { |
| 626 | /* Somehow, the session daemon is not responding anymore. */ |
| 627 | goto error_add_stream_fatal; |
| 628 | } |
| 629 | |
| 630 | health_code_update(); |
| 631 | |
| 632 | if (ret_code != LTTCOMM_CONSUMERD_SUCCESS) { |
| 633 | /* Channel was not found. */ |
| 634 | goto error_add_stream_nosignal; |
| 635 | } |
| 636 | |
| 637 | /* Blocking call */ |
| 638 | health_poll_entry(); |
| 639 | ret_poll = lttng_consumer_poll_socket(consumer_sockpoll); |
| 640 | health_poll_exit(); |
| 641 | if (ret_poll) { |
| 642 | goto error_add_stream_fatal; |
| 643 | } |
| 644 | |
| 645 | health_code_update(); |
| 646 | |
| 647 | /* Get stream file descriptor from socket */ |
| 648 | ret_recv = lttcomm_recv_fds_unix_sock(sock, &fd, 1); |
| 649 | if (ret_recv != sizeof(fd)) { |
| 650 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD); |
| 651 | ret_func = ret_recv; |
| 652 | goto end; |
| 653 | } |
| 654 | |
| 655 | health_code_update(); |
| 656 | |
| 657 | /* |
| 658 | * Send status code to session daemon only if the recv works. If the |
| 659 | * above recv() failed, the session daemon is notified through the |
| 660 | * error socket and the teardown is eventually done. |
| 661 | */ |
| 662 | ret_send_status = consumer_send_status_msg(sock, ret_code); |
| 663 | if (ret_send_status < 0) { |
| 664 | /* Somehow, the session daemon is not responding anymore. */ |
| 665 | goto error_add_stream_nosignal; |
| 666 | } |
| 667 | |
| 668 | health_code_update(); |
| 669 | |
| 670 | pthread_mutex_lock(&channel->lock); |
| 671 | new_stream = consumer_stream_create( |
| 672 | channel, |
| 673 | channel->key, |
| 674 | fd, |
| 675 | channel->name, |
| 676 | channel->relayd_id, |
| 677 | channel->session_id, |
| 678 | channel->trace_chunk, |
| 679 | msg.u.stream.cpu, |
| 680 | &alloc_ret, |
| 681 | channel->type, |
| 682 | channel->monitor); |
| 683 | if (new_stream == NULL) { |
| 684 | switch (alloc_ret) { |
| 685 | case -ENOMEM: |
| 686 | case -EINVAL: |
| 687 | default: |
| 688 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR); |
| 689 | break; |
| 690 | } |
| 691 | pthread_mutex_unlock(&channel->lock); |
| 692 | goto error_add_stream_nosignal; |
| 693 | } |
| 694 | |
| 695 | new_stream->wait_fd = fd; |
| 696 | ret_get_max_subbuf_size = kernctl_get_max_subbuf_size( |
| 697 | new_stream->wait_fd, &new_stream->max_sb_size); |
| 698 | if (ret_get_max_subbuf_size < 0) { |
| 699 | pthread_mutex_unlock(&channel->lock); |
| 700 | ERR("Failed to get kernel maximal subbuffer size"); |
| 701 | goto error_add_stream_nosignal; |
| 702 | } |
| 703 | |
| 704 | consumer_stream_update_channel_attributes(new_stream, |
| 705 | channel); |
| 706 | |
| 707 | /* |
| 708 | * We've just assigned the channel to the stream so increment the |
| 709 | * refcount right now. We don't need to increment the refcount for |
| 710 | * streams in no monitor because we handle manually the cleanup of |
| 711 | * those. It is very important to make sure there is NO prior |
| 712 | * consumer_del_stream() calls or else the refcount will be unbalanced. |
| 713 | */ |
| 714 | if (channel->monitor) { |
| 715 | uatomic_inc(&new_stream->chan->refcount); |
| 716 | } |
| 717 | |
| 718 | /* |
| 719 | * The buffer flush is done on the session daemon side for the kernel |
| 720 | * so no need for the stream "hangup_flush_done" variable to be |
| 721 | * tracked. This is important for a kernel stream since we don't rely |
| 722 | * on the flush state of the stream to read data. It's not the case for |
| 723 | * user space tracing. |
| 724 | */ |
| 725 | new_stream->hangup_flush_done = 0; |
| 726 | |
| 727 | health_code_update(); |
| 728 | |
| 729 | pthread_mutex_lock(&new_stream->lock); |
| 730 | if (ctx->on_recv_stream) { |
| 731 | int ret_recv_stream = ctx->on_recv_stream(new_stream); |
| 732 | if (ret_recv_stream < 0) { |
| 733 | pthread_mutex_unlock(&new_stream->lock); |
| 734 | pthread_mutex_unlock(&channel->lock); |
| 735 | consumer_stream_free(new_stream); |
| 736 | goto error_add_stream_nosignal; |
| 737 | } |
| 738 | } |
| 739 | health_code_update(); |
| 740 | |
| 741 | if (new_stream->metadata_flag) { |
| 742 | channel->metadata_stream = new_stream; |
| 743 | } |
| 744 | |
| 745 | /* Do not monitor this stream. */ |
| 746 | if (!channel->monitor) { |
| 747 | DBG("Kernel consumer add stream %s in no monitor mode with " |
| 748 | "relayd id %" PRIu64, new_stream->name, |
| 749 | new_stream->net_seq_idx); |
| 750 | cds_list_add(&new_stream->send_node, &channel->streams.head); |
| 751 | pthread_mutex_unlock(&new_stream->lock); |
| 752 | pthread_mutex_unlock(&channel->lock); |
| 753 | goto end_add_stream; |
| 754 | } |
| 755 | |
| 756 | /* Send stream to relayd if the stream has an ID. */ |
| 757 | if (new_stream->net_seq_idx != (uint64_t) -1ULL) { |
| 758 | int ret_send_relayd_stream; |
| 759 | |
| 760 | ret_send_relayd_stream = consumer_send_relayd_stream( |
| 761 | new_stream, new_stream->chan->pathname); |
| 762 | if (ret_send_relayd_stream < 0) { |
| 763 | pthread_mutex_unlock(&new_stream->lock); |
| 764 | pthread_mutex_unlock(&channel->lock); |
| 765 | consumer_stream_free(new_stream); |
| 766 | goto error_add_stream_nosignal; |
| 767 | } |
| 768 | |
| 769 | /* |
| 770 | * If adding an extra stream to an already |
| 771 | * existing channel (e.g. cpu hotplug), we need |
| 772 | * to send the "streams_sent" command to relayd. |
| 773 | */ |
| 774 | if (channel->streams_sent_to_relayd) { |
| 775 | int ret_send_relayd_streams_sent; |
| 776 | |
| 777 | ret_send_relayd_streams_sent = |
| 778 | consumer_send_relayd_streams_sent( |
| 779 | new_stream->net_seq_idx); |
| 780 | if (ret_send_relayd_streams_sent < 0) { |
| 781 | pthread_mutex_unlock(&new_stream->lock); |
| 782 | pthread_mutex_unlock(&channel->lock); |
| 783 | goto error_add_stream_nosignal; |
| 784 | } |
| 785 | } |
| 786 | } |
| 787 | pthread_mutex_unlock(&new_stream->lock); |
| 788 | pthread_mutex_unlock(&channel->lock); |
| 789 | |
| 790 | /* Get the right pipe where the stream will be sent. */ |
| 791 | if (new_stream->metadata_flag) { |
| 792 | consumer_add_metadata_stream(new_stream); |
| 793 | stream_pipe = ctx->consumer_metadata_pipe; |
| 794 | } else { |
| 795 | consumer_add_data_stream(new_stream); |
| 796 | stream_pipe = ctx->consumer_data_pipe; |
| 797 | } |
| 798 | |
| 799 | /* Visible to other threads */ |
| 800 | new_stream->globally_visible = 1; |
| 801 | |
| 802 | health_code_update(); |
| 803 | |
| 804 | ret_pipe_write = lttng_pipe_write( |
| 805 | stream_pipe, &new_stream, sizeof(new_stream)); |
| 806 | if (ret_pipe_write < 0) { |
| 807 | ERR("Consumer write %s stream to pipe %d", |
| 808 | new_stream->metadata_flag ? "metadata" : "data", |
| 809 | lttng_pipe_get_writefd(stream_pipe)); |
| 810 | if (new_stream->metadata_flag) { |
| 811 | consumer_del_stream_for_metadata(new_stream); |
| 812 | } else { |
| 813 | consumer_del_stream_for_data(new_stream); |
| 814 | } |
| 815 | goto error_add_stream_nosignal; |
| 816 | } |
| 817 | |
| 818 | DBG("Kernel consumer ADD_STREAM %s (fd: %d) %s with relayd id %" PRIu64, |
| 819 | new_stream->name, fd, new_stream->chan->pathname, new_stream->relayd_stream_id); |
| 820 | end_add_stream: |
| 821 | break; |
| 822 | error_add_stream_nosignal: |
| 823 | goto end_nosignal; |
| 824 | error_add_stream_fatal: |
| 825 | goto error_fatal; |
| 826 | } |
| 827 | case LTTNG_CONSUMER_STREAMS_SENT: |
| 828 | { |
| 829 | struct lttng_consumer_channel *channel; |
| 830 | int ret_send_status; |
| 831 | |
| 832 | /* |
| 833 | * Get stream's channel reference. Needed when adding the stream to the |
| 834 | * global hash table. |
| 835 | */ |
| 836 | channel = consumer_find_channel(msg.u.sent_streams.channel_key); |
| 837 | if (!channel) { |
| 838 | /* |
| 839 | * We could not find the channel. Can happen if cpu hotplug |
| 840 | * happens while tearing down. |
| 841 | */ |
| 842 | ERR("Unable to find channel key %" PRIu64, |
| 843 | msg.u.sent_streams.channel_key); |
| 844 | ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND; |
| 845 | } |
| 846 | |
| 847 | health_code_update(); |
| 848 | |
| 849 | /* |
| 850 | * Send status code to session daemon. |
| 851 | */ |
| 852 | ret_send_status = consumer_send_status_msg(sock, ret_code); |
| 853 | if (ret_send_status < 0 || |
| 854 | ret_code != LTTCOMM_CONSUMERD_SUCCESS) { |
| 855 | /* Somehow, the session daemon is not responding anymore. */ |
| 856 | goto error_streams_sent_nosignal; |
| 857 | } |
| 858 | |
| 859 | health_code_update(); |
| 860 | |
| 861 | /* |
| 862 | * We should not send this message if we don't monitor the |
| 863 | * streams in this channel. |
| 864 | */ |
| 865 | if (!channel->monitor) { |
| 866 | goto end_error_streams_sent; |
| 867 | } |
| 868 | |
| 869 | health_code_update(); |
| 870 | /* Send stream to relayd if the stream has an ID. */ |
| 871 | if (msg.u.sent_streams.net_seq_idx != (uint64_t) -1ULL) { |
| 872 | int ret_send_relay_streams; |
| 873 | |
| 874 | ret_send_relay_streams = consumer_send_relayd_streams_sent( |
| 875 | msg.u.sent_streams.net_seq_idx); |
| 876 | if (ret_send_relay_streams < 0) { |
| 877 | goto error_streams_sent_nosignal; |
| 878 | } |
| 879 | channel->streams_sent_to_relayd = true; |
| 880 | } |
| 881 | end_error_streams_sent: |
| 882 | break; |
| 883 | error_streams_sent_nosignal: |
| 884 | goto end_nosignal; |
| 885 | } |
| 886 | case LTTNG_CONSUMER_UPDATE_STREAM: |
| 887 | { |
| 888 | rcu_read_unlock(); |
| 889 | return -ENOSYS; |
| 890 | } |
| 891 | case LTTNG_CONSUMER_DESTROY_RELAYD: |
| 892 | { |
| 893 | uint64_t index = msg.u.destroy_relayd.net_seq_idx; |
| 894 | struct consumer_relayd_sock_pair *relayd; |
| 895 | int ret_send_status; |
| 896 | |
| 897 | DBG("Kernel consumer destroying relayd %" PRIu64, index); |
| 898 | |
| 899 | /* Get relayd reference if exists. */ |
| 900 | relayd = consumer_find_relayd(index); |
| 901 | if (relayd == NULL) { |
| 902 | DBG("Unable to find relayd %" PRIu64, index); |
| 903 | ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL; |
| 904 | } |
| 905 | |
| 906 | /* |
| 907 | * Each relayd socket pair has a refcount of stream attached to it |
| 908 | * which tells if the relayd is still active or not depending on the |
| 909 | * refcount value. |
| 910 | * |
| 911 | * This will set the destroy flag of the relayd object and destroy it |
| 912 | * if the refcount reaches zero when called. |
| 913 | * |
| 914 | * The destroy can happen either here or when a stream fd hangs up. |
| 915 | */ |
| 916 | if (relayd) { |
| 917 | consumer_flag_relayd_for_destroy(relayd); |
| 918 | } |
| 919 | |
| 920 | health_code_update(); |
| 921 | |
| 922 | ret_send_status = consumer_send_status_msg(sock, ret_code); |
| 923 | if (ret_send_status < 0) { |
| 924 | /* Somehow, the session daemon is not responding anymore. */ |
| 925 | goto error_fatal; |
| 926 | } |
| 927 | |
| 928 | goto end_nosignal; |
| 929 | } |
| 930 | case LTTNG_CONSUMER_DATA_PENDING: |
| 931 | { |
| 932 | int32_t ret_data_pending; |
| 933 | uint64_t id = msg.u.data_pending.session_id; |
| 934 | ssize_t ret_send; |
| 935 | |
| 936 | DBG("Kernel consumer data pending command for id %" PRIu64, id); |
| 937 | |
| 938 | ret_data_pending = consumer_data_pending(id); |
| 939 | |
| 940 | health_code_update(); |
| 941 | |
| 942 | /* Send back returned value to session daemon */ |
| 943 | ret_send = lttcomm_send_unix_sock(sock, &ret_data_pending, |
| 944 | sizeof(ret_data_pending)); |
| 945 | if (ret_send < 0) { |
| 946 | PERROR("send data pending ret code"); |
| 947 | goto error_fatal; |
| 948 | } |
| 949 | |
| 950 | /* |
| 951 | * No need to send back a status message since the data pending |
| 952 | * returned value is the response. |
| 953 | */ |
| 954 | break; |
| 955 | } |
| 956 | case LTTNG_CONSUMER_SNAPSHOT_CHANNEL: |
| 957 | { |
| 958 | struct lttng_consumer_channel *channel; |
| 959 | uint64_t key = msg.u.snapshot_channel.key; |
| 960 | int ret_send_status; |
| 961 | |
| 962 | channel = consumer_find_channel(key); |
| 963 | if (!channel) { |
| 964 | ERR("Channel %" PRIu64 " not found", key); |
| 965 | ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND; |
| 966 | } else { |
| 967 | if (msg.u.snapshot_channel.metadata == 1) { |
| 968 | int ret_snapshot; |
| 969 | |
| 970 | ret_snapshot = lttng_kconsumer_snapshot_metadata( |
| 971 | channel, key, |
| 972 | msg.u.snapshot_channel.pathname, |
| 973 | msg.u.snapshot_channel.relayd_id, |
| 974 | ctx); |
| 975 | if (ret_snapshot < 0) { |
| 976 | ERR("Snapshot metadata failed"); |
| 977 | ret_code = LTTCOMM_CONSUMERD_SNAPSHOT_FAILED; |
| 978 | } |
| 979 | } else { |
| 980 | int ret_snapshot; |
| 981 | |
| 982 | ret_snapshot = lttng_kconsumer_snapshot_channel( |
| 983 | channel, key, |
| 984 | msg.u.snapshot_channel.pathname, |
| 985 | msg.u.snapshot_channel.relayd_id, |
| 986 | msg.u.snapshot_channel |
| 987 | .nb_packets_per_stream, |
| 988 | ctx); |
| 989 | if (ret_snapshot < 0) { |
| 990 | ERR("Snapshot channel failed"); |
| 991 | ret_code = LTTCOMM_CONSUMERD_SNAPSHOT_FAILED; |
| 992 | } |
| 993 | } |
| 994 | } |
| 995 | health_code_update(); |
| 996 | |
| 997 | ret_send_status = consumer_send_status_msg(sock, ret_code); |
| 998 | if (ret_send_status < 0) { |
| 999 | /* Somehow, the session daemon is not responding anymore. */ |
| 1000 | goto end_nosignal; |
| 1001 | } |
| 1002 | break; |
| 1003 | } |
| 1004 | case LTTNG_CONSUMER_DESTROY_CHANNEL: |
| 1005 | { |
| 1006 | uint64_t key = msg.u.destroy_channel.key; |
| 1007 | struct lttng_consumer_channel *channel; |
| 1008 | int ret_send_status; |
| 1009 | |
| 1010 | channel = consumer_find_channel(key); |
| 1011 | if (!channel) { |
| 1012 | ERR("Kernel consumer destroy channel %" PRIu64 " not found", key); |
| 1013 | ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND; |
| 1014 | } |
| 1015 | |
| 1016 | health_code_update(); |
| 1017 | |
| 1018 | ret_send_status = consumer_send_status_msg(sock, ret_code); |
| 1019 | if (ret_send_status < 0) { |
| 1020 | /* Somehow, the session daemon is not responding anymore. */ |
| 1021 | goto end_destroy_channel; |
| 1022 | } |
| 1023 | |
| 1024 | health_code_update(); |
| 1025 | |
| 1026 | /* Stop right now if no channel was found. */ |
| 1027 | if (!channel) { |
| 1028 | goto end_destroy_channel; |
| 1029 | } |
| 1030 | |
| 1031 | /* |
| 1032 | * This command should ONLY be issued for channel with streams set in |
| 1033 | * no monitor mode. |
| 1034 | */ |
| 1035 | LTTNG_ASSERT(!channel->monitor); |
| 1036 | |
| 1037 | /* |
| 1038 | * The refcount should ALWAYS be 0 in the case of a channel in no |
| 1039 | * monitor mode. |
| 1040 | */ |
| 1041 | LTTNG_ASSERT(!uatomic_sub_return(&channel->refcount, 1)); |
| 1042 | |
| 1043 | consumer_del_channel(channel); |
| 1044 | end_destroy_channel: |
| 1045 | goto end_nosignal; |
| 1046 | } |
| 1047 | case LTTNG_CONSUMER_DISCARDED_EVENTS: |
| 1048 | { |
| 1049 | ssize_t ret; |
| 1050 | uint64_t count; |
| 1051 | struct lttng_consumer_channel *channel; |
| 1052 | uint64_t id = msg.u.discarded_events.session_id; |
| 1053 | uint64_t key = msg.u.discarded_events.channel_key; |
| 1054 | |
| 1055 | DBG("Kernel consumer discarded events command for session id %" |
| 1056 | PRIu64 ", channel key %" PRIu64, id, key); |
| 1057 | |
| 1058 | channel = consumer_find_channel(key); |
| 1059 | if (!channel) { |
| 1060 | ERR("Kernel consumer discarded events channel %" |
| 1061 | PRIu64 " not found", key); |
| 1062 | count = 0; |
| 1063 | } else { |
| 1064 | count = channel->discarded_events; |
| 1065 | } |
| 1066 | |
| 1067 | health_code_update(); |
| 1068 | |
| 1069 | /* Send back returned value to session daemon */ |
| 1070 | ret = lttcomm_send_unix_sock(sock, &count, sizeof(count)); |
| 1071 | if (ret < 0) { |
| 1072 | PERROR("send discarded events"); |
| 1073 | goto error_fatal; |
| 1074 | } |
| 1075 | |
| 1076 | break; |
| 1077 | } |
| 1078 | case LTTNG_CONSUMER_LOST_PACKETS: |
| 1079 | { |
| 1080 | ssize_t ret; |
| 1081 | uint64_t count; |
| 1082 | struct lttng_consumer_channel *channel; |
| 1083 | uint64_t id = msg.u.lost_packets.session_id; |
| 1084 | uint64_t key = msg.u.lost_packets.channel_key; |
| 1085 | |
| 1086 | DBG("Kernel consumer lost packets command for session id %" |
| 1087 | PRIu64 ", channel key %" PRIu64, id, key); |
| 1088 | |
| 1089 | channel = consumer_find_channel(key); |
| 1090 | if (!channel) { |
| 1091 | ERR("Kernel consumer lost packets channel %" |
| 1092 | PRIu64 " not found", key); |
| 1093 | count = 0; |
| 1094 | } else { |
| 1095 | count = channel->lost_packets; |
| 1096 | } |
| 1097 | |
| 1098 | health_code_update(); |
| 1099 | |
| 1100 | /* Send back returned value to session daemon */ |
| 1101 | ret = lttcomm_send_unix_sock(sock, &count, sizeof(count)); |
| 1102 | if (ret < 0) { |
| 1103 | PERROR("send lost packets"); |
| 1104 | goto error_fatal; |
| 1105 | } |
| 1106 | |
| 1107 | break; |
| 1108 | } |
| 1109 | case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE: |
| 1110 | { |
| 1111 | int channel_monitor_pipe; |
| 1112 | int ret_send_status, ret_set_channel_monitor_pipe; |
| 1113 | ssize_t ret_recv; |
| 1114 | |
| 1115 | ret_code = LTTCOMM_CONSUMERD_SUCCESS; |
| 1116 | /* Successfully received the command's type. */ |
| 1117 | ret_send_status = consumer_send_status_msg(sock, ret_code); |
| 1118 | if (ret_send_status < 0) { |
| 1119 | goto error_fatal; |
| 1120 | } |
| 1121 | |
| 1122 | ret_recv = lttcomm_recv_fds_unix_sock( |
| 1123 | sock, &channel_monitor_pipe, 1); |
| 1124 | if (ret_recv != sizeof(channel_monitor_pipe)) { |
| 1125 | ERR("Failed to receive channel monitor pipe"); |
| 1126 | goto error_fatal; |
| 1127 | } |
| 1128 | |
| 1129 | DBG("Received channel monitor pipe (%d)", channel_monitor_pipe); |
| 1130 | ret_set_channel_monitor_pipe = |
| 1131 | consumer_timer_thread_set_channel_monitor_pipe( |
| 1132 | channel_monitor_pipe); |
| 1133 | if (!ret_set_channel_monitor_pipe) { |
| 1134 | int flags; |
| 1135 | int ret_fcntl; |
| 1136 | |
| 1137 | ret_code = LTTCOMM_CONSUMERD_SUCCESS; |
| 1138 | /* Set the pipe as non-blocking. */ |
| 1139 | ret_fcntl = fcntl(channel_monitor_pipe, F_GETFL, 0); |
| 1140 | if (ret_fcntl == -1) { |
| 1141 | PERROR("fcntl get flags of the channel monitoring pipe"); |
| 1142 | goto error_fatal; |
| 1143 | } |
| 1144 | flags = ret_fcntl; |
| 1145 | |
| 1146 | ret_fcntl = fcntl(channel_monitor_pipe, F_SETFL, |
| 1147 | flags | O_NONBLOCK); |
| 1148 | if (ret_fcntl == -1) { |
| 1149 | PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe"); |
| 1150 | goto error_fatal; |
| 1151 | } |
| 1152 | DBG("Channel monitor pipe set as non-blocking"); |
| 1153 | } else { |
| 1154 | ret_code = LTTCOMM_CONSUMERD_ALREADY_SET; |
| 1155 | } |
| 1156 | ret_send_status = consumer_send_status_msg(sock, ret_code); |
| 1157 | if (ret_send_status < 0) { |
| 1158 | goto error_fatal; |
| 1159 | } |
| 1160 | break; |
| 1161 | } |
| 1162 | case LTTNG_CONSUMER_ROTATE_CHANNEL: |
| 1163 | { |
| 1164 | struct lttng_consumer_channel *channel; |
| 1165 | uint64_t key = msg.u.rotate_channel.key; |
| 1166 | int ret_send_status; |
| 1167 | |
| 1168 | DBG("Consumer rotate channel %" PRIu64, key); |
| 1169 | |
| 1170 | channel = consumer_find_channel(key); |
| 1171 | if (!channel) { |
| 1172 | ERR("Channel %" PRIu64 " not found", key); |
| 1173 | ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND; |
| 1174 | } else { |
| 1175 | /* |
| 1176 | * Sample the rotate position of all the streams in this channel. |
| 1177 | */ |
| 1178 | int ret_rotate_channel; |
| 1179 | |
| 1180 | ret_rotate_channel = lttng_consumer_rotate_channel( |
| 1181 | channel, key, |
| 1182 | msg.u.rotate_channel.relayd_id, |
| 1183 | msg.u.rotate_channel.metadata, ctx); |
| 1184 | if (ret_rotate_channel < 0) { |
| 1185 | ERR("Rotate channel failed"); |
| 1186 | ret_code = LTTCOMM_CONSUMERD_ROTATION_FAIL; |
| 1187 | } |
| 1188 | |
| 1189 | health_code_update(); |
| 1190 | } |
| 1191 | |
| 1192 | ret_send_status = consumer_send_status_msg(sock, ret_code); |
| 1193 | if (ret_send_status < 0) { |
| 1194 | /* Somehow, the session daemon is not responding anymore. */ |
| 1195 | goto error_rotate_channel; |
| 1196 | } |
| 1197 | if (channel) { |
| 1198 | /* Rotate the streams that are ready right now. */ |
| 1199 | int ret_rotate; |
| 1200 | |
| 1201 | ret_rotate = lttng_consumer_rotate_ready_streams( |
| 1202 | channel, key, ctx); |
| 1203 | if (ret_rotate < 0) { |
| 1204 | ERR("Rotate ready streams failed"); |
| 1205 | } |
| 1206 | } |
| 1207 | break; |
| 1208 | error_rotate_channel: |
| 1209 | goto end_nosignal; |
| 1210 | } |
| 1211 | case LTTNG_CONSUMER_CLEAR_CHANNEL: |
| 1212 | { |
| 1213 | struct lttng_consumer_channel *channel; |
| 1214 | uint64_t key = msg.u.clear_channel.key; |
| 1215 | int ret_send_status; |
| 1216 | |
| 1217 | channel = consumer_find_channel(key); |
| 1218 | if (!channel) { |
| 1219 | DBG("Channel %" PRIu64 " not found", key); |
| 1220 | ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND; |
| 1221 | } else { |
| 1222 | int ret_clear_channel; |
| 1223 | |
| 1224 | ret_clear_channel = |
| 1225 | lttng_consumer_clear_channel(channel); |
| 1226 | if (ret_clear_channel) { |
| 1227 | ERR("Clear channel failed"); |
| 1228 | ret_code = (lttcomm_return_code) ret_clear_channel; |
| 1229 | } |
| 1230 | |
| 1231 | health_code_update(); |
| 1232 | } |
| 1233 | |
| 1234 | ret_send_status = consumer_send_status_msg(sock, ret_code); |
| 1235 | if (ret_send_status < 0) { |
| 1236 | /* Somehow, the session daemon is not responding anymore. */ |
| 1237 | goto end_nosignal; |
| 1238 | } |
| 1239 | |
| 1240 | break; |
| 1241 | } |
| 1242 | case LTTNG_CONSUMER_INIT: |
| 1243 | { |
| 1244 | int ret_send_status; |
| 1245 | |
| 1246 | ret_code = lttng_consumer_init_command(ctx, |
| 1247 | msg.u.init.sessiond_uuid); |
| 1248 | health_code_update(); |
| 1249 | ret_send_status = consumer_send_status_msg(sock, ret_code); |
| 1250 | if (ret_send_status < 0) { |
| 1251 | /* Somehow, the session daemon is not responding anymore. */ |
| 1252 | goto end_nosignal; |
| 1253 | } |
| 1254 | break; |
| 1255 | } |
| 1256 | case LTTNG_CONSUMER_CREATE_TRACE_CHUNK: |
| 1257 | { |
| 1258 | const struct lttng_credentials credentials = { |
| 1259 | .uid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.create_trace_chunk.credentials.value.uid), |
| 1260 | .gid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.create_trace_chunk.credentials.value.gid), |
| 1261 | }; |
| 1262 | const bool is_local_trace = |
| 1263 | !msg.u.create_trace_chunk.relayd_id.is_set; |
| 1264 | const uint64_t relayd_id = |
| 1265 | msg.u.create_trace_chunk.relayd_id.value; |
| 1266 | const char *chunk_override_name = |
| 1267 | *msg.u.create_trace_chunk.override_name ? |
| 1268 | msg.u.create_trace_chunk.override_name : |
| 1269 | NULL; |
| 1270 | struct lttng_directory_handle *chunk_directory_handle = NULL; |
| 1271 | |
| 1272 | /* |
| 1273 | * The session daemon will only provide a chunk directory file |
| 1274 | * descriptor for local traces. |
| 1275 | */ |
| 1276 | if (is_local_trace) { |
| 1277 | int chunk_dirfd; |
| 1278 | int ret_send_status; |
| 1279 | ssize_t ret_recv; |
| 1280 | |
| 1281 | /* Acnowledge the reception of the command. */ |
| 1282 | ret_send_status = consumer_send_status_msg( |
| 1283 | sock, LTTCOMM_CONSUMERD_SUCCESS); |
| 1284 | if (ret_send_status < 0) { |
| 1285 | /* Somehow, the session daemon is not responding anymore. */ |
| 1286 | goto end_nosignal; |
| 1287 | } |
| 1288 | |
| 1289 | ret_recv = lttcomm_recv_fds_unix_sock( |
| 1290 | sock, &chunk_dirfd, 1); |
| 1291 | if (ret_recv != sizeof(chunk_dirfd)) { |
| 1292 | ERR("Failed to receive trace chunk directory file descriptor"); |
| 1293 | goto error_fatal; |
| 1294 | } |
| 1295 | |
| 1296 | DBG("Received trace chunk directory fd (%d)", |
| 1297 | chunk_dirfd); |
| 1298 | chunk_directory_handle = lttng_directory_handle_create_from_dirfd( |
| 1299 | chunk_dirfd); |
| 1300 | if (!chunk_directory_handle) { |
| 1301 | ERR("Failed to initialize chunk directory handle from directory file descriptor"); |
| 1302 | if (close(chunk_dirfd)) { |
| 1303 | PERROR("Failed to close chunk directory file descriptor"); |
| 1304 | } |
| 1305 | goto error_fatal; |
| 1306 | } |
| 1307 | } |
| 1308 | |
| 1309 | ret_code = lttng_consumer_create_trace_chunk( |
| 1310 | !is_local_trace ? &relayd_id : NULL, |
| 1311 | msg.u.create_trace_chunk.session_id, |
| 1312 | msg.u.create_trace_chunk.chunk_id, |
| 1313 | (time_t) msg.u.create_trace_chunk |
| 1314 | .creation_timestamp, |
| 1315 | chunk_override_name, |
| 1316 | msg.u.create_trace_chunk.credentials.is_set ? |
| 1317 | &credentials : |
| 1318 | NULL, |
| 1319 | chunk_directory_handle); |
| 1320 | lttng_directory_handle_put(chunk_directory_handle); |
| 1321 | goto end_msg_sessiond; |
| 1322 | } |
| 1323 | case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK: |
| 1324 | { |
| 1325 | enum lttng_trace_chunk_command_type close_command = |
| 1326 | (lttng_trace_chunk_command_type) msg.u.close_trace_chunk.close_command.value; |
| 1327 | const uint64_t relayd_id = |
| 1328 | msg.u.close_trace_chunk.relayd_id.value; |
| 1329 | struct lttcomm_consumer_close_trace_chunk_reply reply; |
| 1330 | char path[LTTNG_PATH_MAX]; |
| 1331 | ssize_t ret_send; |
| 1332 | |
| 1333 | ret_code = lttng_consumer_close_trace_chunk( |
| 1334 | msg.u.close_trace_chunk.relayd_id.is_set ? |
| 1335 | &relayd_id : |
| 1336 | NULL, |
| 1337 | msg.u.close_trace_chunk.session_id, |
| 1338 | msg.u.close_trace_chunk.chunk_id, |
| 1339 | (time_t) msg.u.close_trace_chunk.close_timestamp, |
| 1340 | msg.u.close_trace_chunk.close_command.is_set ? |
| 1341 | &close_command : |
| 1342 | NULL, path); |
| 1343 | reply.ret_code = ret_code; |
| 1344 | reply.path_length = strlen(path) + 1; |
| 1345 | ret_send = lttcomm_send_unix_sock(sock, &reply, sizeof(reply)); |
| 1346 | if (ret_send != sizeof(reply)) { |
| 1347 | goto error_fatal; |
| 1348 | } |
| 1349 | ret_send = lttcomm_send_unix_sock( |
| 1350 | sock, path, reply.path_length); |
| 1351 | if (ret_send != reply.path_length) { |
| 1352 | goto error_fatal; |
| 1353 | } |
| 1354 | goto end_nosignal; |
| 1355 | } |
| 1356 | case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS: |
| 1357 | { |
| 1358 | const uint64_t relayd_id = |
| 1359 | msg.u.trace_chunk_exists.relayd_id.value; |
| 1360 | |
| 1361 | ret_code = lttng_consumer_trace_chunk_exists( |
| 1362 | msg.u.trace_chunk_exists.relayd_id.is_set ? |
| 1363 | &relayd_id : NULL, |
| 1364 | msg.u.trace_chunk_exists.session_id, |
| 1365 | msg.u.trace_chunk_exists.chunk_id); |
| 1366 | goto end_msg_sessiond; |
| 1367 | } |
| 1368 | case LTTNG_CONSUMER_OPEN_CHANNEL_PACKETS: |
| 1369 | { |
| 1370 | const uint64_t key = msg.u.open_channel_packets.key; |
| 1371 | struct lttng_consumer_channel *channel = |
| 1372 | consumer_find_channel(key); |
| 1373 | |
| 1374 | if (channel) { |
| 1375 | pthread_mutex_lock(&channel->lock); |
| 1376 | ret_code = lttng_consumer_open_channel_packets(channel); |
| 1377 | pthread_mutex_unlock(&channel->lock); |
| 1378 | } else { |
| 1379 | WARN("Channel %" PRIu64 " not found", key); |
| 1380 | ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND; |
| 1381 | } |
| 1382 | |
| 1383 | health_code_update(); |
| 1384 | goto end_msg_sessiond; |
| 1385 | } |
| 1386 | default: |
| 1387 | goto end_nosignal; |
| 1388 | } |
| 1389 | |
| 1390 | end_nosignal: |
| 1391 | /* |
| 1392 | * Return 1 to indicate success since the 0 value can be a socket |
| 1393 | * shutdown during the recv() or send() call. |
| 1394 | */ |
| 1395 | ret_func = 1; |
| 1396 | goto end; |
| 1397 | error_fatal: |
| 1398 | /* This will issue a consumer stop. */ |
| 1399 | ret_func = -1; |
| 1400 | goto end; |
| 1401 | end_msg_sessiond: |
| 1402 | /* |
| 1403 | * The returned value here is not useful since either way we'll return 1 to |
| 1404 | * the caller because the session daemon socket management is done |
| 1405 | * elsewhere. Returning a negative code or 0 will shutdown the consumer. |
| 1406 | */ |
| 1407 | { |
| 1408 | int ret_send_status; |
| 1409 | |
| 1410 | ret_send_status = consumer_send_status_msg(sock, ret_code); |
| 1411 | if (ret_send_status < 0) { |
| 1412 | goto error_fatal; |
| 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | ret_func = 1; |
| 1417 | |
| 1418 | end: |
| 1419 | health_code_update(); |
| 1420 | rcu_read_unlock(); |
| 1421 | return ret_func; |
| 1422 | } |
| 1423 | |
| 1424 | /* |
| 1425 | * Sync metadata meaning request them to the session daemon and snapshot to the |
| 1426 | * metadata thread can consumer them. |
| 1427 | * |
| 1428 | * Metadata stream lock MUST be acquired. |
| 1429 | */ |
| 1430 | enum sync_metadata_status lttng_kconsumer_sync_metadata( |
| 1431 | struct lttng_consumer_stream *metadata) |
| 1432 | { |
| 1433 | int ret; |
| 1434 | enum sync_metadata_status status; |
| 1435 | |
| 1436 | LTTNG_ASSERT(metadata); |
| 1437 | |
| 1438 | ret = kernctl_buffer_flush(metadata->wait_fd); |
| 1439 | if (ret < 0) { |
| 1440 | ERR("Failed to flush kernel stream"); |
| 1441 | status = SYNC_METADATA_STATUS_ERROR; |
| 1442 | goto end; |
| 1443 | } |
| 1444 | |
| 1445 | ret = kernctl_snapshot(metadata->wait_fd); |
| 1446 | if (ret < 0) { |
| 1447 | if (errno == EAGAIN) { |
| 1448 | /* No new metadata, exit. */ |
| 1449 | DBG("Sync metadata, no new kernel metadata"); |
| 1450 | status = SYNC_METADATA_STATUS_NO_DATA; |
| 1451 | } else { |
| 1452 | ERR("Sync metadata, taking kernel snapshot failed."); |
| 1453 | status = SYNC_METADATA_STATUS_ERROR; |
| 1454 | } |
| 1455 | } else { |
| 1456 | status = SYNC_METADATA_STATUS_NEW_DATA; |
| 1457 | } |
| 1458 | |
| 1459 | end: |
| 1460 | return status; |
| 1461 | } |
| 1462 | |
| 1463 | static |
| 1464 | int extract_common_subbuffer_info(struct lttng_consumer_stream *stream, |
| 1465 | struct stream_subbuffer *subbuf) |
| 1466 | { |
| 1467 | int ret; |
| 1468 | |
| 1469 | ret = kernctl_get_subbuf_size( |
| 1470 | stream->wait_fd, &subbuf->info.data.subbuf_size); |
| 1471 | if (ret) { |
| 1472 | goto end; |
| 1473 | } |
| 1474 | |
| 1475 | ret = kernctl_get_padded_subbuf_size( |
| 1476 | stream->wait_fd, &subbuf->info.data.padded_subbuf_size); |
| 1477 | if (ret) { |
| 1478 | goto end; |
| 1479 | } |
| 1480 | |
| 1481 | end: |
| 1482 | return ret; |
| 1483 | } |
| 1484 | |
| 1485 | static |
| 1486 | int extract_metadata_subbuffer_info(struct lttng_consumer_stream *stream, |
| 1487 | struct stream_subbuffer *subbuf) |
| 1488 | { |
| 1489 | int ret; |
| 1490 | |
| 1491 | ret = extract_common_subbuffer_info(stream, subbuf); |
| 1492 | if (ret) { |
| 1493 | goto end; |
| 1494 | } |
| 1495 | |
| 1496 | ret = kernctl_get_metadata_version( |
| 1497 | stream->wait_fd, &subbuf->info.metadata.version); |
| 1498 | if (ret) { |
| 1499 | goto end; |
| 1500 | } |
| 1501 | |
| 1502 | end: |
| 1503 | return ret; |
| 1504 | } |
| 1505 | |
| 1506 | static |
| 1507 | int extract_data_subbuffer_info(struct lttng_consumer_stream *stream, |
| 1508 | struct stream_subbuffer *subbuf) |
| 1509 | { |
| 1510 | int ret; |
| 1511 | |
| 1512 | ret = extract_common_subbuffer_info(stream, subbuf); |
| 1513 | if (ret) { |
| 1514 | goto end; |
| 1515 | } |
| 1516 | |
| 1517 | ret = kernctl_get_packet_size( |
| 1518 | stream->wait_fd, &subbuf->info.data.packet_size); |
| 1519 | if (ret < 0) { |
| 1520 | PERROR("Failed to get sub-buffer packet size"); |
| 1521 | goto end; |
| 1522 | } |
| 1523 | |
| 1524 | ret = kernctl_get_content_size( |
| 1525 | stream->wait_fd, &subbuf->info.data.content_size); |
| 1526 | if (ret < 0) { |
| 1527 | PERROR("Failed to get sub-buffer content size"); |
| 1528 | goto end; |
| 1529 | } |
| 1530 | |
| 1531 | ret = kernctl_get_timestamp_begin( |
| 1532 | stream->wait_fd, &subbuf->info.data.timestamp_begin); |
| 1533 | if (ret < 0) { |
| 1534 | PERROR("Failed to get sub-buffer begin timestamp"); |
| 1535 | goto end; |
| 1536 | } |
| 1537 | |
| 1538 | ret = kernctl_get_timestamp_end( |
| 1539 | stream->wait_fd, &subbuf->info.data.timestamp_end); |
| 1540 | if (ret < 0) { |
| 1541 | PERROR("Failed to get sub-buffer end timestamp"); |
| 1542 | goto end; |
| 1543 | } |
| 1544 | |
| 1545 | ret = kernctl_get_events_discarded( |
| 1546 | stream->wait_fd, &subbuf->info.data.events_discarded); |
| 1547 | if (ret) { |
| 1548 | PERROR("Failed to get sub-buffer events discarded count"); |
| 1549 | goto end; |
| 1550 | } |
| 1551 | |
| 1552 | ret = kernctl_get_sequence_number(stream->wait_fd, |
| 1553 | &subbuf->info.data.sequence_number.value); |
| 1554 | if (ret) { |
| 1555 | /* May not be supported by older LTTng-modules. */ |
| 1556 | if (ret != -ENOTTY) { |
| 1557 | PERROR("Failed to get sub-buffer sequence number"); |
| 1558 | goto end; |
| 1559 | } |
| 1560 | } else { |
| 1561 | subbuf->info.data.sequence_number.is_set = true; |
| 1562 | } |
| 1563 | |
| 1564 | ret = kernctl_get_stream_id( |
| 1565 | stream->wait_fd, &subbuf->info.data.stream_id); |
| 1566 | if (ret < 0) { |
| 1567 | PERROR("Failed to get stream id"); |
| 1568 | goto end; |
| 1569 | } |
| 1570 | |
| 1571 | ret = kernctl_get_instance_id(stream->wait_fd, |
| 1572 | &subbuf->info.data.stream_instance_id.value); |
| 1573 | if (ret) { |
| 1574 | /* May not be supported by older LTTng-modules. */ |
| 1575 | if (ret != -ENOTTY) { |
| 1576 | PERROR("Failed to get stream instance id"); |
| 1577 | goto end; |
| 1578 | } |
| 1579 | } else { |
| 1580 | subbuf->info.data.stream_instance_id.is_set = true; |
| 1581 | } |
| 1582 | end: |
| 1583 | return ret; |
| 1584 | } |
| 1585 | |
| 1586 | static |
| 1587 | enum get_next_subbuffer_status get_subbuffer_common( |
| 1588 | struct lttng_consumer_stream *stream, |
| 1589 | struct stream_subbuffer *subbuffer) |
| 1590 | { |
| 1591 | int ret; |
| 1592 | enum get_next_subbuffer_status status; |
| 1593 | |
| 1594 | ret = kernctl_get_next_subbuf(stream->wait_fd); |
| 1595 | switch (ret) { |
| 1596 | case 0: |
| 1597 | status = GET_NEXT_SUBBUFFER_STATUS_OK; |
| 1598 | break; |
| 1599 | case -ENODATA: |
| 1600 | case -EAGAIN: |
| 1601 | /* |
| 1602 | * The caller only expects -ENODATA when there is no data to |
| 1603 | * read, but the kernel tracer returns -EAGAIN when there is |
| 1604 | * currently no data for a non-finalized stream, and -ENODATA |
| 1605 | * when there is no data for a finalized stream. Those can be |
| 1606 | * combined into a -ENODATA return value. |
| 1607 | */ |
| 1608 | status = GET_NEXT_SUBBUFFER_STATUS_NO_DATA; |
| 1609 | goto end; |
| 1610 | default: |
| 1611 | status = GET_NEXT_SUBBUFFER_STATUS_ERROR; |
| 1612 | goto end; |
| 1613 | } |
| 1614 | |
| 1615 | ret = stream->read_subbuffer_ops.extract_subbuffer_info( |
| 1616 | stream, subbuffer); |
| 1617 | if (ret) { |
| 1618 | status = GET_NEXT_SUBBUFFER_STATUS_ERROR; |
| 1619 | } |
| 1620 | end: |
| 1621 | return status; |
| 1622 | } |
| 1623 | |
| 1624 | static |
| 1625 | enum get_next_subbuffer_status get_next_subbuffer_splice( |
| 1626 | struct lttng_consumer_stream *stream, |
| 1627 | struct stream_subbuffer *subbuffer) |
| 1628 | { |
| 1629 | const enum get_next_subbuffer_status status = |
| 1630 | get_subbuffer_common(stream, subbuffer); |
| 1631 | |
| 1632 | if (status != GET_NEXT_SUBBUFFER_STATUS_OK) { |
| 1633 | goto end; |
| 1634 | } |
| 1635 | |
| 1636 | subbuffer->buffer.fd = stream->wait_fd; |
| 1637 | end: |
| 1638 | return status; |
| 1639 | } |
| 1640 | |
| 1641 | static |
| 1642 | enum get_next_subbuffer_status get_next_subbuffer_mmap( |
| 1643 | struct lttng_consumer_stream *stream, |
| 1644 | struct stream_subbuffer *subbuffer) |
| 1645 | { |
| 1646 | int ret; |
| 1647 | enum get_next_subbuffer_status status; |
| 1648 | const char *addr; |
| 1649 | |
| 1650 | status = get_subbuffer_common(stream, subbuffer); |
| 1651 | if (status != GET_NEXT_SUBBUFFER_STATUS_OK) { |
| 1652 | goto end; |
| 1653 | } |
| 1654 | |
| 1655 | ret = get_current_subbuf_addr(stream, &addr); |
| 1656 | if (ret) { |
| 1657 | status = GET_NEXT_SUBBUFFER_STATUS_ERROR; |
| 1658 | goto end; |
| 1659 | } |
| 1660 | |
| 1661 | subbuffer->buffer.buffer = lttng_buffer_view_init( |
| 1662 | addr, 0, subbuffer->info.data.padded_subbuf_size); |
| 1663 | end: |
| 1664 | return status; |
| 1665 | } |
| 1666 | |
| 1667 | static |
| 1668 | enum get_next_subbuffer_status get_next_subbuffer_metadata_check(struct lttng_consumer_stream *stream, |
| 1669 | struct stream_subbuffer *subbuffer) |
| 1670 | { |
| 1671 | int ret; |
| 1672 | const char *addr; |
| 1673 | bool coherent; |
| 1674 | enum get_next_subbuffer_status status; |
| 1675 | |
| 1676 | ret = kernctl_get_next_subbuf_metadata_check(stream->wait_fd, |
| 1677 | &coherent); |
| 1678 | if (ret) { |
| 1679 | goto end; |
| 1680 | } |
| 1681 | |
| 1682 | ret = stream->read_subbuffer_ops.extract_subbuffer_info( |
| 1683 | stream, subbuffer); |
| 1684 | if (ret) { |
| 1685 | goto end; |
| 1686 | } |
| 1687 | |
| 1688 | LTTNG_OPTIONAL_SET(&subbuffer->info.metadata.coherent, coherent); |
| 1689 | |
| 1690 | ret = get_current_subbuf_addr(stream, &addr); |
| 1691 | if (ret) { |
| 1692 | goto end; |
| 1693 | } |
| 1694 | |
| 1695 | subbuffer->buffer.buffer = lttng_buffer_view_init( |
| 1696 | addr, 0, subbuffer->info.data.padded_subbuf_size); |
| 1697 | DBG("Got metadata packet with padded_subbuf_size = %lu, coherent = %s", |
| 1698 | subbuffer->info.metadata.padded_subbuf_size, |
| 1699 | coherent ? "true" : "false"); |
| 1700 | end: |
| 1701 | /* |
| 1702 | * The caller only expects -ENODATA when there is no data to read, but |
| 1703 | * the kernel tracer returns -EAGAIN when there is currently no data |
| 1704 | * for a non-finalized stream, and -ENODATA when there is no data for a |
| 1705 | * finalized stream. Those can be combined into a -ENODATA return value. |
| 1706 | */ |
| 1707 | switch (ret) { |
| 1708 | case 0: |
| 1709 | status = GET_NEXT_SUBBUFFER_STATUS_OK; |
| 1710 | break; |
| 1711 | case -ENODATA: |
| 1712 | case -EAGAIN: |
| 1713 | /* |
| 1714 | * The caller only expects -ENODATA when there is no data to |
| 1715 | * read, but the kernel tracer returns -EAGAIN when there is |
| 1716 | * currently no data for a non-finalized stream, and -ENODATA |
| 1717 | * when there is no data for a finalized stream. Those can be |
| 1718 | * combined into a -ENODATA return value. |
| 1719 | */ |
| 1720 | status = GET_NEXT_SUBBUFFER_STATUS_NO_DATA; |
| 1721 | break; |
| 1722 | default: |
| 1723 | status = GET_NEXT_SUBBUFFER_STATUS_ERROR; |
| 1724 | break; |
| 1725 | } |
| 1726 | |
| 1727 | return status; |
| 1728 | } |
| 1729 | |
| 1730 | static |
| 1731 | int put_next_subbuffer(struct lttng_consumer_stream *stream, |
| 1732 | struct stream_subbuffer *subbuffer) |
| 1733 | { |
| 1734 | const int ret = kernctl_put_next_subbuf(stream->wait_fd); |
| 1735 | |
| 1736 | if (ret) { |
| 1737 | if (ret == -EFAULT) { |
| 1738 | PERROR("Error in unreserving sub buffer"); |
| 1739 | } else if (ret == -EIO) { |
| 1740 | /* Should never happen with newer LTTng versions */ |
| 1741 | PERROR("Reader has been pushed by the writer, last sub-buffer corrupted"); |
| 1742 | } |
| 1743 | } |
| 1744 | |
| 1745 | return ret; |
| 1746 | } |
| 1747 | |
| 1748 | static |
| 1749 | bool is_get_next_check_metadata_available(int tracer_fd) |
| 1750 | { |
| 1751 | const int ret = kernctl_get_next_subbuf_metadata_check(tracer_fd, NULL); |
| 1752 | const bool available = ret != -ENOTTY; |
| 1753 | |
| 1754 | if (ret == 0) { |
| 1755 | /* get succeeded, make sure to put the subbuffer. */ |
| 1756 | kernctl_put_subbuf(tracer_fd); |
| 1757 | } |
| 1758 | |
| 1759 | return available; |
| 1760 | } |
| 1761 | |
| 1762 | static |
| 1763 | int signal_metadata(struct lttng_consumer_stream *stream, |
| 1764 | struct lttng_consumer_local_data *ctx) |
| 1765 | { |
| 1766 | ASSERT_LOCKED(stream->metadata_rdv_lock); |
| 1767 | return pthread_cond_broadcast(&stream->metadata_rdv) ? -errno : 0; |
| 1768 | } |
| 1769 | |
| 1770 | static |
| 1771 | int lttng_kconsumer_set_stream_ops( |
| 1772 | struct lttng_consumer_stream *stream) |
| 1773 | { |
| 1774 | int ret = 0; |
| 1775 | |
| 1776 | if (stream->metadata_flag && stream->chan->is_live) { |
| 1777 | DBG("Attempting to enable metadata bucketization for live consumers"); |
| 1778 | if (is_get_next_check_metadata_available(stream->wait_fd)) { |
| 1779 | DBG("Kernel tracer supports get_next_subbuffer_metadata_check, metadata will be accumulated until a coherent state is reached"); |
| 1780 | stream->read_subbuffer_ops.get_next_subbuffer = |
| 1781 | get_next_subbuffer_metadata_check; |
| 1782 | ret = consumer_stream_enable_metadata_bucketization( |
| 1783 | stream); |
| 1784 | if (ret) { |
| 1785 | goto end; |
| 1786 | } |
| 1787 | } else { |
| 1788 | /* |
| 1789 | * The kernel tracer version is too old to indicate |
| 1790 | * when the metadata stream has reached a "coherent" |
| 1791 | * (parseable) point. |
| 1792 | * |
| 1793 | * This means that a live viewer may see an incoherent |
| 1794 | * sequence of metadata and fail to parse it. |
| 1795 | */ |
| 1796 | WARN("Kernel tracer does not support get_next_subbuffer_metadata_check which may cause live clients to fail to parse the metadata stream"); |
| 1797 | metadata_bucket_destroy(stream->metadata_bucket); |
| 1798 | stream->metadata_bucket = NULL; |
| 1799 | } |
| 1800 | |
| 1801 | stream->read_subbuffer_ops.on_sleep = signal_metadata; |
| 1802 | } |
| 1803 | |
| 1804 | if (!stream->read_subbuffer_ops.get_next_subbuffer) { |
| 1805 | if (stream->chan->output == CONSUMER_CHANNEL_MMAP) { |
| 1806 | stream->read_subbuffer_ops.get_next_subbuffer = |
| 1807 | get_next_subbuffer_mmap; |
| 1808 | } else { |
| 1809 | stream->read_subbuffer_ops.get_next_subbuffer = |
| 1810 | get_next_subbuffer_splice; |
| 1811 | } |
| 1812 | } |
| 1813 | |
| 1814 | if (stream->metadata_flag) { |
| 1815 | stream->read_subbuffer_ops.extract_subbuffer_info = |
| 1816 | extract_metadata_subbuffer_info; |
| 1817 | } else { |
| 1818 | stream->read_subbuffer_ops.extract_subbuffer_info = |
| 1819 | extract_data_subbuffer_info; |
| 1820 | if (stream->chan->is_live) { |
| 1821 | stream->read_subbuffer_ops.send_live_beacon = |
| 1822 | consumer_flush_kernel_index; |
| 1823 | } |
| 1824 | } |
| 1825 | |
| 1826 | stream->read_subbuffer_ops.put_next_subbuffer = put_next_subbuffer; |
| 1827 | end: |
| 1828 | return ret; |
| 1829 | } |
| 1830 | |
| 1831 | int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream) |
| 1832 | { |
| 1833 | int ret; |
| 1834 | |
| 1835 | LTTNG_ASSERT(stream); |
| 1836 | |
| 1837 | /* |
| 1838 | * Don't create anything if this is set for streaming or if there is |
| 1839 | * no current trace chunk on the parent channel. |
| 1840 | */ |
| 1841 | if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor && |
| 1842 | stream->chan->trace_chunk) { |
| 1843 | ret = consumer_stream_create_output_files(stream, true); |
| 1844 | if (ret) { |
| 1845 | goto error; |
| 1846 | } |
| 1847 | } |
| 1848 | |
| 1849 | if (stream->output == LTTNG_EVENT_MMAP) { |
| 1850 | /* get the len of the mmap region */ |
| 1851 | unsigned long mmap_len; |
| 1852 | |
| 1853 | ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len); |
| 1854 | if (ret != 0) { |
| 1855 | PERROR("kernctl_get_mmap_len"); |
| 1856 | goto error_close_fd; |
| 1857 | } |
| 1858 | stream->mmap_len = (size_t) mmap_len; |
| 1859 | |
| 1860 | stream->mmap_base = mmap(NULL, stream->mmap_len, PROT_READ, |
| 1861 | MAP_PRIVATE, stream->wait_fd, 0); |
| 1862 | if (stream->mmap_base == MAP_FAILED) { |
| 1863 | PERROR("Error mmaping"); |
| 1864 | ret = -1; |
| 1865 | goto error_close_fd; |
| 1866 | } |
| 1867 | } |
| 1868 | |
| 1869 | ret = lttng_kconsumer_set_stream_ops(stream); |
| 1870 | if (ret) { |
| 1871 | goto error_close_fd; |
| 1872 | } |
| 1873 | |
| 1874 | /* we return 0 to let the library handle the FD internally */ |
| 1875 | return 0; |
| 1876 | |
| 1877 | error_close_fd: |
| 1878 | if (stream->out_fd >= 0) { |
| 1879 | int err; |
| 1880 | |
| 1881 | err = close(stream->out_fd); |
| 1882 | LTTNG_ASSERT(!err); |
| 1883 | stream->out_fd = -1; |
| 1884 | } |
| 1885 | error: |
| 1886 | return ret; |
| 1887 | } |
| 1888 | |
| 1889 | /* |
| 1890 | * Check if data is still being extracted from the buffers for a specific |
| 1891 | * stream. Consumer data lock MUST be acquired before calling this function |
| 1892 | * and the stream lock. |
| 1893 | * |
| 1894 | * Return 1 if the traced data are still getting read else 0 meaning that the |
| 1895 | * data is available for trace viewer reading. |
| 1896 | */ |
| 1897 | int lttng_kconsumer_data_pending(struct lttng_consumer_stream *stream) |
| 1898 | { |
| 1899 | int ret; |
| 1900 | |
| 1901 | LTTNG_ASSERT(stream); |
| 1902 | |
| 1903 | if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) { |
| 1904 | ret = 0; |
| 1905 | goto end; |
| 1906 | } |
| 1907 | |
| 1908 | ret = kernctl_get_next_subbuf(stream->wait_fd); |
| 1909 | if (ret == 0) { |
| 1910 | /* There is still data so let's put back this subbuffer. */ |
| 1911 | ret = kernctl_put_subbuf(stream->wait_fd); |
| 1912 | LTTNG_ASSERT(ret == 0); |
| 1913 | ret = 1; /* Data is pending */ |
| 1914 | goto end; |
| 1915 | } |
| 1916 | |
| 1917 | /* Data is NOT pending and ready to be read. */ |
| 1918 | ret = 0; |
| 1919 | |
| 1920 | end: |
| 1921 | return ret; |
| 1922 | } |