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