| 1 | /* |
| 2 | * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License, version 2 only, as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along with |
| 14 | * this program; if not, write to the Free Software Foundation, Inc., 51 |
| 15 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | |
| 18 | #define _LGPL_SOURCE |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <unistd.h> |
| 23 | #include <inttypes.h> |
| 24 | |
| 25 | #include <common/common.h> |
| 26 | #include <common/defaults.h> |
| 27 | #include <common/compat/string.h> |
| 28 | |
| 29 | #include "consumer.h" |
| 30 | #include "health-sessiond.h" |
| 31 | #include "kernel-consumer.h" |
| 32 | #include "notification-thread-commands.h" |
| 33 | #include "session.h" |
| 34 | #include "lttng-sessiond.h" |
| 35 | |
| 36 | static char *create_channel_path(struct consumer_output *consumer, |
| 37 | uid_t uid, gid_t gid) |
| 38 | { |
| 39 | int ret; |
| 40 | char tmp_path[PATH_MAX]; |
| 41 | char *pathname = NULL; |
| 42 | |
| 43 | assert(consumer); |
| 44 | |
| 45 | /* Get the right path name destination */ |
| 46 | if (consumer->type == CONSUMER_DST_LOCAL) { |
| 47 | /* Set application path to the destination path */ |
| 48 | ret = snprintf(tmp_path, sizeof(tmp_path), "%s%s%s", |
| 49 | consumer->dst.session_root_path, |
| 50 | consumer->chunk_path, |
| 51 | consumer->subdir); |
| 52 | if (ret < 0) { |
| 53 | PERROR("snprintf kernel channel path"); |
| 54 | goto error; |
| 55 | } else if (ret >= sizeof(tmp_path)) { |
| 56 | ERR("Kernel channel path exceeds the maximal allowed length of of %zu bytes (%i bytes required) with path \"%s%s%s\"", |
| 57 | sizeof(tmp_path), ret, |
| 58 | consumer->dst.session_root_path, |
| 59 | consumer->chunk_path, |
| 60 | consumer->subdir); |
| 61 | goto error; |
| 62 | } |
| 63 | pathname = lttng_strndup(tmp_path, sizeof(tmp_path)); |
| 64 | if (!pathname) { |
| 65 | PERROR("lttng_strndup"); |
| 66 | goto error; |
| 67 | } |
| 68 | |
| 69 | /* Create directory */ |
| 70 | ret = run_as_mkdir_recursive(pathname, S_IRWXU | S_IRWXG, uid, gid); |
| 71 | if (ret < 0) { |
| 72 | if (errno != EEXIST) { |
| 73 | ERR("Trace directory creation error"); |
| 74 | goto error; |
| 75 | } |
| 76 | } |
| 77 | DBG3("Kernel local consumer tracefile path: %s", pathname); |
| 78 | } else { |
| 79 | ret = snprintf(tmp_path, sizeof(tmp_path), "%s%s", |
| 80 | consumer->dst.net.base_dir, |
| 81 | consumer->subdir); |
| 82 | if (ret < 0) { |
| 83 | PERROR("snprintf kernel metadata path"); |
| 84 | goto error; |
| 85 | } else if (ret >= sizeof(tmp_path)) { |
| 86 | ERR("Kernel channel path exceeds the maximal allowed length of of %zu bytes (%i bytes required) with path \"%s%s\"", |
| 87 | sizeof(tmp_path), ret, |
| 88 | consumer->dst.net.base_dir, |
| 89 | consumer->subdir); |
| 90 | goto error; |
| 91 | } |
| 92 | pathname = lttng_strndup(tmp_path, sizeof(tmp_path)); |
| 93 | if (!pathname) { |
| 94 | PERROR("lttng_strndup"); |
| 95 | goto error; |
| 96 | } |
| 97 | DBG3("Kernel network consumer subdir path: %s", pathname); |
| 98 | } |
| 99 | |
| 100 | return pathname; |
| 101 | |
| 102 | error: |
| 103 | free(pathname); |
| 104 | return NULL; |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Sending a single channel to the consumer with command ADD_CHANNEL. |
| 109 | */ |
| 110 | int kernel_consumer_add_channel(struct consumer_socket *sock, |
| 111 | struct ltt_kernel_channel *channel, |
| 112 | struct ltt_kernel_session *ksession, |
| 113 | unsigned int monitor) |
| 114 | { |
| 115 | int ret; |
| 116 | char *pathname; |
| 117 | struct lttcomm_consumer_msg lkm; |
| 118 | struct consumer_output *consumer; |
| 119 | enum lttng_error_code status; |
| 120 | struct ltt_session *session; |
| 121 | struct lttng_channel_extended *channel_attr_extended; |
| 122 | |
| 123 | /* Safety net */ |
| 124 | assert(channel); |
| 125 | assert(ksession); |
| 126 | assert(ksession->consumer); |
| 127 | |
| 128 | consumer = ksession->consumer; |
| 129 | channel_attr_extended = (struct lttng_channel_extended *) |
| 130 | channel->channel->attr.extended.ptr; |
| 131 | |
| 132 | DBG("Kernel consumer adding channel %s to kernel consumer", |
| 133 | channel->channel->name); |
| 134 | |
| 135 | if (monitor) { |
| 136 | pathname = create_channel_path(consumer, ksession->uid, |
| 137 | ksession->gid); |
| 138 | } else { |
| 139 | /* Empty path. */ |
| 140 | pathname = strdup(""); |
| 141 | } |
| 142 | if (!pathname) { |
| 143 | ret = -1; |
| 144 | goto error; |
| 145 | } |
| 146 | |
| 147 | /* Prep channel message structure */ |
| 148 | consumer_init_add_channel_comm_msg(&lkm, |
| 149 | channel->key, |
| 150 | ksession->id, |
| 151 | pathname, |
| 152 | ksession->uid, |
| 153 | ksession->gid, |
| 154 | consumer->net_seq_index, |
| 155 | channel->channel->name, |
| 156 | channel->stream_count, |
| 157 | channel->channel->attr.output, |
| 158 | CONSUMER_CHANNEL_TYPE_DATA, |
| 159 | channel->channel->attr.tracefile_size, |
| 160 | channel->channel->attr.tracefile_count, |
| 161 | monitor, |
| 162 | channel->channel->attr.live_timer_interval, |
| 163 | channel_attr_extended->monitor_timer_interval); |
| 164 | |
| 165 | health_code_update(); |
| 166 | |
| 167 | ret = consumer_send_channel(sock, &lkm); |
| 168 | if (ret < 0) { |
| 169 | goto error; |
| 170 | } |
| 171 | |
| 172 | health_code_update(); |
| 173 | rcu_read_lock(); |
| 174 | session = session_find_by_id(ksession->id); |
| 175 | assert(session); |
| 176 | assert(pthread_mutex_trylock(&session->lock)); |
| 177 | assert(session_trylock_list()); |
| 178 | |
| 179 | status = notification_thread_command_add_channel( |
| 180 | notification_thread_handle, session->name, |
| 181 | ksession->uid, ksession->gid, |
| 182 | channel->channel->name, channel->key, |
| 183 | LTTNG_DOMAIN_KERNEL, |
| 184 | channel->channel->attr.subbuf_size * channel->channel->attr.num_subbuf); |
| 185 | rcu_read_unlock(); |
| 186 | if (status != LTTNG_OK) { |
| 187 | ret = -1; |
| 188 | goto error; |
| 189 | } |
| 190 | |
| 191 | channel->published_to_notification_thread = true; |
| 192 | |
| 193 | error: |
| 194 | free(pathname); |
| 195 | return ret; |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM. |
| 200 | * |
| 201 | * The consumer socket lock must be held by the caller. |
| 202 | */ |
| 203 | int kernel_consumer_add_metadata(struct consumer_socket *sock, |
| 204 | struct ltt_kernel_session *ksession, unsigned int monitor) |
| 205 | { |
| 206 | int ret; |
| 207 | char *pathname; |
| 208 | struct lttcomm_consumer_msg lkm; |
| 209 | struct consumer_output *consumer; |
| 210 | struct ltt_session *session; |
| 211 | |
| 212 | rcu_read_lock(); |
| 213 | |
| 214 | /* Safety net */ |
| 215 | assert(ksession); |
| 216 | assert(ksession->consumer); |
| 217 | assert(sock); |
| 218 | |
| 219 | DBG("Sending metadata %d to kernel consumer", |
| 220 | ksession->metadata_stream_fd); |
| 221 | |
| 222 | /* Get consumer output pointer */ |
| 223 | consumer = ksession->consumer; |
| 224 | |
| 225 | if (monitor) { |
| 226 | pathname = create_channel_path(consumer, |
| 227 | ksession->uid, ksession->gid); |
| 228 | } else { |
| 229 | /* Empty path. */ |
| 230 | pathname = strdup(""); |
| 231 | } |
| 232 | if (!pathname) { |
| 233 | ret = -1; |
| 234 | goto error; |
| 235 | } |
| 236 | |
| 237 | session = session_find_by_id(ksession->id); |
| 238 | assert(session); |
| 239 | assert(pthread_mutex_trylock(&session->lock)); |
| 240 | assert(session_trylock_list()); |
| 241 | |
| 242 | /* Prep channel message structure */ |
| 243 | consumer_init_add_channel_comm_msg(&lkm, |
| 244 | ksession->metadata->key, |
| 245 | ksession->id, |
| 246 | pathname, |
| 247 | ksession->uid, |
| 248 | ksession->gid, |
| 249 | consumer->net_seq_index, |
| 250 | DEFAULT_METADATA_NAME, |
| 251 | 1, |
| 252 | DEFAULT_KERNEL_CHANNEL_OUTPUT, |
| 253 | CONSUMER_CHANNEL_TYPE_METADATA, |
| 254 | 0, 0, |
| 255 | monitor, 0, 0); |
| 256 | |
| 257 | health_code_update(); |
| 258 | |
| 259 | ret = consumer_send_channel(sock, &lkm); |
| 260 | if (ret < 0) { |
| 261 | goto error; |
| 262 | } |
| 263 | |
| 264 | health_code_update(); |
| 265 | |
| 266 | /* Prep stream message structure */ |
| 267 | consumer_init_add_stream_comm_msg(&lkm, |
| 268 | ksession->metadata->key, |
| 269 | ksession->metadata_stream_fd, |
| 270 | 0 /* CPU: 0 for metadata. */, |
| 271 | session->current_archive_id); |
| 272 | |
| 273 | health_code_update(); |
| 274 | |
| 275 | /* Send stream and file descriptor */ |
| 276 | ret = consumer_send_stream(sock, consumer, &lkm, |
| 277 | &ksession->metadata_stream_fd, 1); |
| 278 | if (ret < 0) { |
| 279 | goto error; |
| 280 | } |
| 281 | |
| 282 | health_code_update(); |
| 283 | |
| 284 | error: |
| 285 | rcu_read_unlock(); |
| 286 | free(pathname); |
| 287 | return ret; |
| 288 | } |
| 289 | |
| 290 | /* |
| 291 | * Sending a single stream to the consumer with command ADD_STREAM. |
| 292 | */ |
| 293 | static |
| 294 | int kernel_consumer_add_stream(struct consumer_socket *sock, |
| 295 | struct ltt_kernel_channel *channel, |
| 296 | struct ltt_kernel_stream *stream, |
| 297 | struct ltt_kernel_session *session, unsigned int monitor, |
| 298 | uint64_t trace_archive_id) |
| 299 | { |
| 300 | int ret; |
| 301 | struct lttcomm_consumer_msg lkm; |
| 302 | struct consumer_output *consumer; |
| 303 | |
| 304 | assert(channel); |
| 305 | assert(stream); |
| 306 | assert(session); |
| 307 | assert(session->consumer); |
| 308 | assert(sock); |
| 309 | |
| 310 | DBG("Sending stream %d of channel %s to kernel consumer", |
| 311 | stream->fd, channel->channel->name); |
| 312 | |
| 313 | /* Get consumer output pointer */ |
| 314 | consumer = session->consumer; |
| 315 | |
| 316 | /* Prep stream consumer message */ |
| 317 | consumer_init_add_stream_comm_msg(&lkm, |
| 318 | channel->key, |
| 319 | stream->fd, |
| 320 | stream->cpu, |
| 321 | trace_archive_id); |
| 322 | |
| 323 | health_code_update(); |
| 324 | |
| 325 | /* Send stream and file descriptor */ |
| 326 | ret = consumer_send_stream(sock, consumer, &lkm, &stream->fd, 1); |
| 327 | if (ret < 0) { |
| 328 | goto error; |
| 329 | } |
| 330 | |
| 331 | health_code_update(); |
| 332 | |
| 333 | error: |
| 334 | return ret; |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | * Sending the notification that all streams were sent with STREAMS_SENT. |
| 339 | */ |
| 340 | int kernel_consumer_streams_sent(struct consumer_socket *sock, |
| 341 | struct ltt_kernel_session *session, uint64_t channel_key) |
| 342 | { |
| 343 | int ret; |
| 344 | struct lttcomm_consumer_msg lkm; |
| 345 | struct consumer_output *consumer; |
| 346 | |
| 347 | assert(sock); |
| 348 | assert(session); |
| 349 | |
| 350 | DBG("Sending streams_sent"); |
| 351 | /* Get consumer output pointer */ |
| 352 | consumer = session->consumer; |
| 353 | |
| 354 | /* Prep stream consumer message */ |
| 355 | consumer_init_streams_sent_comm_msg(&lkm, |
| 356 | LTTNG_CONSUMER_STREAMS_SENT, |
| 357 | channel_key, consumer->net_seq_index); |
| 358 | |
| 359 | health_code_update(); |
| 360 | |
| 361 | /* Send stream and file descriptor */ |
| 362 | ret = consumer_send_msg(sock, &lkm); |
| 363 | if (ret < 0) { |
| 364 | goto error; |
| 365 | } |
| 366 | |
| 367 | error: |
| 368 | return ret; |
| 369 | } |
| 370 | |
| 371 | /* |
| 372 | * Send all stream fds of kernel channel to the consumer. |
| 373 | * |
| 374 | * The consumer socket lock must be held by the caller. |
| 375 | */ |
| 376 | int kernel_consumer_send_channel_streams(struct consumer_socket *sock, |
| 377 | struct ltt_kernel_channel *channel, struct ltt_kernel_session *ksession, |
| 378 | unsigned int monitor) |
| 379 | { |
| 380 | int ret = LTTNG_OK; |
| 381 | struct ltt_kernel_stream *stream; |
| 382 | struct ltt_session *session; |
| 383 | |
| 384 | /* Safety net */ |
| 385 | assert(channel); |
| 386 | assert(ksession); |
| 387 | assert(ksession->consumer); |
| 388 | assert(sock); |
| 389 | |
| 390 | rcu_read_lock(); |
| 391 | |
| 392 | session = session_find_by_id(ksession->id); |
| 393 | assert(session); |
| 394 | assert(pthread_mutex_trylock(&session->lock)); |
| 395 | assert(session_trylock_list()); |
| 396 | |
| 397 | /* Bail out if consumer is disabled */ |
| 398 | if (!ksession->consumer->enabled) { |
| 399 | ret = LTTNG_OK; |
| 400 | goto error; |
| 401 | } |
| 402 | |
| 403 | DBG("Sending streams of channel %s to kernel consumer", |
| 404 | channel->channel->name); |
| 405 | |
| 406 | if (!channel->sent_to_consumer) { |
| 407 | ret = kernel_consumer_add_channel(sock, channel, ksession, monitor); |
| 408 | if (ret < 0) { |
| 409 | goto error; |
| 410 | } |
| 411 | channel->sent_to_consumer = true; |
| 412 | } |
| 413 | |
| 414 | /* Send streams */ |
| 415 | cds_list_for_each_entry(stream, &channel->stream_list.head, list) { |
| 416 | if (!stream->fd || stream->sent_to_consumer) { |
| 417 | continue; |
| 418 | } |
| 419 | |
| 420 | /* Add stream on the kernel consumer side. */ |
| 421 | ret = kernel_consumer_add_stream(sock, channel, stream, |
| 422 | ksession, monitor, session->current_archive_id); |
| 423 | if (ret < 0) { |
| 424 | goto error; |
| 425 | } |
| 426 | stream->sent_to_consumer = true; |
| 427 | } |
| 428 | |
| 429 | error: |
| 430 | rcu_read_unlock(); |
| 431 | return ret; |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * Send all stream fds of the kernel session to the consumer. |
| 436 | * |
| 437 | * The consumer socket lock must be held by the caller. |
| 438 | */ |
| 439 | int kernel_consumer_send_session(struct consumer_socket *sock, |
| 440 | struct ltt_kernel_session *session) |
| 441 | { |
| 442 | int ret, monitor = 0; |
| 443 | struct ltt_kernel_channel *chan; |
| 444 | |
| 445 | /* Safety net */ |
| 446 | assert(session); |
| 447 | assert(session->consumer); |
| 448 | assert(sock); |
| 449 | |
| 450 | /* Bail out if consumer is disabled */ |
| 451 | if (!session->consumer->enabled) { |
| 452 | ret = LTTNG_OK; |
| 453 | goto error; |
| 454 | } |
| 455 | |
| 456 | /* Don't monitor the streams on the consumer if in flight recorder. */ |
| 457 | if (session->output_traces) { |
| 458 | monitor = 1; |
| 459 | } |
| 460 | |
| 461 | DBG("Sending session stream to kernel consumer"); |
| 462 | |
| 463 | if (session->metadata_stream_fd >= 0 && session->metadata) { |
| 464 | ret = kernel_consumer_add_metadata(sock, session, monitor); |
| 465 | if (ret < 0) { |
| 466 | goto error; |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | /* Send channel and streams of it */ |
| 471 | cds_list_for_each_entry(chan, &session->channel_list.head, list) { |
| 472 | ret = kernel_consumer_send_channel_streams(sock, chan, session, |
| 473 | monitor); |
| 474 | if (ret < 0) { |
| 475 | goto error; |
| 476 | } |
| 477 | if (monitor) { |
| 478 | /* |
| 479 | * Inform the relay that all the streams for the |
| 480 | * channel were sent. |
| 481 | */ |
| 482 | ret = kernel_consumer_streams_sent(sock, session, chan->key); |
| 483 | if (ret < 0) { |
| 484 | goto error; |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | DBG("Kernel consumer FDs of metadata and channel streams sent"); |
| 490 | |
| 491 | session->consumer_fds_sent = 1; |
| 492 | return 0; |
| 493 | |
| 494 | error: |
| 495 | return ret; |
| 496 | } |
| 497 | |
| 498 | int kernel_consumer_destroy_channel(struct consumer_socket *socket, |
| 499 | struct ltt_kernel_channel *channel) |
| 500 | { |
| 501 | int ret; |
| 502 | struct lttcomm_consumer_msg msg; |
| 503 | |
| 504 | assert(channel); |
| 505 | assert(socket); |
| 506 | |
| 507 | DBG("Sending kernel consumer destroy channel key %" PRIu64, channel->key); |
| 508 | |
| 509 | memset(&msg, 0, sizeof(msg)); |
| 510 | msg.cmd_type = LTTNG_CONSUMER_DESTROY_CHANNEL; |
| 511 | msg.u.destroy_channel.key = channel->key; |
| 512 | |
| 513 | pthread_mutex_lock(socket->lock); |
| 514 | health_code_update(); |
| 515 | |
| 516 | ret = consumer_send_msg(socket, &msg); |
| 517 | if (ret < 0) { |
| 518 | goto error; |
| 519 | } |
| 520 | |
| 521 | error: |
| 522 | health_code_update(); |
| 523 | pthread_mutex_unlock(socket->lock); |
| 524 | return ret; |
| 525 | } |
| 526 | |
| 527 | int kernel_consumer_destroy_metadata(struct consumer_socket *socket, |
| 528 | struct ltt_kernel_metadata *metadata) |
| 529 | { |
| 530 | int ret; |
| 531 | struct lttcomm_consumer_msg msg; |
| 532 | |
| 533 | assert(metadata); |
| 534 | assert(socket); |
| 535 | |
| 536 | DBG("Sending kernel consumer destroy channel key %" PRIu64, metadata->key); |
| 537 | |
| 538 | memset(&msg, 0, sizeof(msg)); |
| 539 | msg.cmd_type = LTTNG_CONSUMER_DESTROY_CHANNEL; |
| 540 | msg.u.destroy_channel.key = metadata->key; |
| 541 | |
| 542 | pthread_mutex_lock(socket->lock); |
| 543 | health_code_update(); |
| 544 | |
| 545 | ret = consumer_send_msg(socket, &msg); |
| 546 | if (ret < 0) { |
| 547 | goto error; |
| 548 | } |
| 549 | |
| 550 | error: |
| 551 | health_code_update(); |
| 552 | pthread_mutex_unlock(socket->lock); |
| 553 | return ret; |
| 554 | } |