| 1 | /* |
| 2 | * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com> |
| 3 | * 2018 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License, version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along with |
| 15 | * this program; if not, write to the Free Software Foundation, Inc., 51 |
| 16 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | */ |
| 18 | |
| 19 | #define _LGPL_SOURCE |
| 20 | #include <assert.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <unistd.h> |
| 27 | #include <inttypes.h> |
| 28 | |
| 29 | #include <common/common.h> |
| 30 | #include <common/defaults.h> |
| 31 | #include <common/uri.h> |
| 32 | #include <common/relayd/relayd.h> |
| 33 | #include <common/string-utils/format.h> |
| 34 | |
| 35 | #include "consumer.h" |
| 36 | #include "health-sessiond.h" |
| 37 | #include "ust-app.h" |
| 38 | #include "utils.h" |
| 39 | #include "lttng-sessiond.h" |
| 40 | |
| 41 | /* |
| 42 | * Return allocated full pathname of the session using the consumer trace path |
| 43 | * and subdir if available. |
| 44 | * |
| 45 | * The caller can safely free(3) the returned value. On error, NULL is |
| 46 | * returned. |
| 47 | */ |
| 48 | char *setup_channel_trace_path(struct consumer_output *consumer, |
| 49 | const char *session_path) |
| 50 | { |
| 51 | int ret; |
| 52 | char *pathname; |
| 53 | |
| 54 | assert(consumer); |
| 55 | assert(session_path); |
| 56 | |
| 57 | health_code_update(); |
| 58 | |
| 59 | /* |
| 60 | * Allocate the string ourself to make sure we never exceed |
| 61 | * LTTNG_PATH_MAX. |
| 62 | */ |
| 63 | pathname = zmalloc(LTTNG_PATH_MAX); |
| 64 | if (!pathname) { |
| 65 | goto error; |
| 66 | } |
| 67 | |
| 68 | /* Get correct path name destination */ |
| 69 | if (consumer->type == CONSUMER_DST_NET && |
| 70 | consumer->relay_major_version == 2 && |
| 71 | consumer->relay_minor_version < 11) { |
| 72 | ret = snprintf(pathname, LTTNG_PATH_MAX, "%s%s/%s%s", |
| 73 | consumer->dst.net.base_dir, |
| 74 | consumer->chunk_path, consumer->domain_subdir, |
| 75 | session_path); |
| 76 | } else { |
| 77 | ret = snprintf(pathname, LTTNG_PATH_MAX, "%s%s", |
| 78 | consumer->domain_subdir, session_path); |
| 79 | } |
| 80 | DBG3("Consumer trace path relative to current trace chunk: \"%s\"", |
| 81 | pathname); |
| 82 | if (ret < 0) { |
| 83 | PERROR("Failed to format channel path"); |
| 84 | goto error; |
| 85 | } else if (ret >= LTTNG_PATH_MAX) { |
| 86 | ERR("Truncation occurred while formatting channel path"); |
| 87 | goto error; |
| 88 | } |
| 89 | |
| 90 | return pathname; |
| 91 | error: |
| 92 | free(pathname); |
| 93 | return NULL; |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * Send a data payload using a given consumer socket of size len. |
| 98 | * |
| 99 | * The consumer socket lock MUST be acquired before calling this since this |
| 100 | * function can change the fd value. |
| 101 | * |
| 102 | * Return 0 on success else a negative value on error. |
| 103 | */ |
| 104 | int consumer_socket_send(struct consumer_socket *socket, void *msg, size_t len) |
| 105 | { |
| 106 | int fd; |
| 107 | ssize_t size; |
| 108 | |
| 109 | assert(socket); |
| 110 | assert(socket->fd_ptr); |
| 111 | assert(msg); |
| 112 | |
| 113 | /* Consumer socket is invalid. Stopping. */ |
| 114 | fd = *socket->fd_ptr; |
| 115 | if (fd < 0) { |
| 116 | goto error; |
| 117 | } |
| 118 | |
| 119 | size = lttcomm_send_unix_sock(fd, msg, len); |
| 120 | if (size < 0) { |
| 121 | /* The above call will print a PERROR on error. */ |
| 122 | DBG("Error when sending data to consumer on sock %d", fd); |
| 123 | /* |
| 124 | * At this point, the socket is not usable anymore thus closing it and |
| 125 | * setting the file descriptor to -1 so it is not reused. |
| 126 | */ |
| 127 | |
| 128 | /* This call will PERROR on error. */ |
| 129 | (void) lttcomm_close_unix_sock(fd); |
| 130 | *socket->fd_ptr = -1; |
| 131 | goto error; |
| 132 | } |
| 133 | |
| 134 | return 0; |
| 135 | |
| 136 | error: |
| 137 | return -1; |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Receive a data payload using a given consumer socket of size len. |
| 142 | * |
| 143 | * The consumer socket lock MUST be acquired before calling this since this |
| 144 | * function can change the fd value. |
| 145 | * |
| 146 | * Return 0 on success else a negative value on error. |
| 147 | */ |
| 148 | int consumer_socket_recv(struct consumer_socket *socket, void *msg, size_t len) |
| 149 | { |
| 150 | int fd; |
| 151 | ssize_t size; |
| 152 | |
| 153 | assert(socket); |
| 154 | assert(socket->fd_ptr); |
| 155 | assert(msg); |
| 156 | |
| 157 | /* Consumer socket is invalid. Stopping. */ |
| 158 | fd = *socket->fd_ptr; |
| 159 | if (fd < 0) { |
| 160 | goto error; |
| 161 | } |
| 162 | |
| 163 | size = lttcomm_recv_unix_sock(fd, msg, len); |
| 164 | if (size <= 0) { |
| 165 | /* The above call will print a PERROR on error. */ |
| 166 | DBG("Error when receiving data from the consumer socket %d", fd); |
| 167 | /* |
| 168 | * At this point, the socket is not usable anymore thus closing it and |
| 169 | * setting the file descriptor to -1 so it is not reused. |
| 170 | */ |
| 171 | |
| 172 | /* This call will PERROR on error. */ |
| 173 | (void) lttcomm_close_unix_sock(fd); |
| 174 | *socket->fd_ptr = -1; |
| 175 | goto error; |
| 176 | } |
| 177 | |
| 178 | return 0; |
| 179 | |
| 180 | error: |
| 181 | return -1; |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * Receive a reply command status message from the consumer. Consumer socket |
| 186 | * lock MUST be acquired before calling this function. |
| 187 | * |
| 188 | * Return 0 on success, -1 on recv error or a negative lttng error code which |
| 189 | * was possibly returned by the consumer. |
| 190 | */ |
| 191 | int consumer_recv_status_reply(struct consumer_socket *sock) |
| 192 | { |
| 193 | int ret; |
| 194 | struct lttcomm_consumer_status_msg reply; |
| 195 | |
| 196 | assert(sock); |
| 197 | |
| 198 | ret = consumer_socket_recv(sock, &reply, sizeof(reply)); |
| 199 | if (ret < 0) { |
| 200 | goto end; |
| 201 | } |
| 202 | |
| 203 | if (reply.ret_code == LTTCOMM_CONSUMERD_SUCCESS) { |
| 204 | /* All good. */ |
| 205 | ret = 0; |
| 206 | } else { |
| 207 | ret = -reply.ret_code; |
| 208 | DBG("Consumer ret code %d", ret); |
| 209 | } |
| 210 | |
| 211 | end: |
| 212 | return ret; |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * Once the ASK_CHANNEL command is sent to the consumer, the channel |
| 217 | * information are sent back. This call receives that data and populates key |
| 218 | * and stream_count. |
| 219 | * |
| 220 | * On success return 0 and both key and stream_count are set. On error, a |
| 221 | * negative value is sent back and both parameters are untouched. |
| 222 | */ |
| 223 | int consumer_recv_status_channel(struct consumer_socket *sock, |
| 224 | uint64_t *key, unsigned int *stream_count) |
| 225 | { |
| 226 | int ret; |
| 227 | struct lttcomm_consumer_status_channel reply; |
| 228 | |
| 229 | assert(sock); |
| 230 | assert(stream_count); |
| 231 | assert(key); |
| 232 | |
| 233 | ret = consumer_socket_recv(sock, &reply, sizeof(reply)); |
| 234 | if (ret < 0) { |
| 235 | goto end; |
| 236 | } |
| 237 | |
| 238 | /* An error is possible so don't touch the key and stream_count. */ |
| 239 | if (reply.ret_code != LTTCOMM_CONSUMERD_SUCCESS) { |
| 240 | ret = -1; |
| 241 | goto end; |
| 242 | } |
| 243 | |
| 244 | *key = reply.key; |
| 245 | *stream_count = reply.stream_count; |
| 246 | ret = 0; |
| 247 | |
| 248 | end: |
| 249 | return ret; |
| 250 | } |
| 251 | |
| 252 | /* |
| 253 | * Send destroy relayd command to consumer. |
| 254 | * |
| 255 | * On success return positive value. On error, negative value. |
| 256 | */ |
| 257 | int consumer_send_destroy_relayd(struct consumer_socket *sock, |
| 258 | struct consumer_output *consumer) |
| 259 | { |
| 260 | int ret; |
| 261 | struct lttcomm_consumer_msg msg; |
| 262 | |
| 263 | assert(consumer); |
| 264 | assert(sock); |
| 265 | |
| 266 | DBG2("Sending destroy relayd command to consumer sock %d", *sock->fd_ptr); |
| 267 | |
| 268 | memset(&msg, 0, sizeof(msg)); |
| 269 | msg.cmd_type = LTTNG_CONSUMER_DESTROY_RELAYD; |
| 270 | msg.u.destroy_relayd.net_seq_idx = consumer->net_seq_index; |
| 271 | |
| 272 | pthread_mutex_lock(sock->lock); |
| 273 | ret = consumer_socket_send(sock, &msg, sizeof(msg)); |
| 274 | if (ret < 0) { |
| 275 | goto error; |
| 276 | } |
| 277 | |
| 278 | /* Don't check the return value. The caller will do it. */ |
| 279 | ret = consumer_recv_status_reply(sock); |
| 280 | |
| 281 | DBG2("Consumer send destroy relayd command done"); |
| 282 | |
| 283 | error: |
| 284 | pthread_mutex_unlock(sock->lock); |
| 285 | return ret; |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | * For each consumer socket in the consumer output object, send a destroy |
| 290 | * relayd command. |
| 291 | */ |
| 292 | void consumer_output_send_destroy_relayd(struct consumer_output *consumer) |
| 293 | { |
| 294 | struct lttng_ht_iter iter; |
| 295 | struct consumer_socket *socket; |
| 296 | |
| 297 | assert(consumer); |
| 298 | |
| 299 | /* Destroy any relayd connection */ |
| 300 | if (consumer->type == CONSUMER_DST_NET) { |
| 301 | rcu_read_lock(); |
| 302 | cds_lfht_for_each_entry(consumer->socks->ht, &iter.iter, socket, |
| 303 | node.node) { |
| 304 | int ret; |
| 305 | |
| 306 | /* Send destroy relayd command */ |
| 307 | ret = consumer_send_destroy_relayd(socket, consumer); |
| 308 | if (ret < 0) { |
| 309 | DBG("Unable to send destroy relayd command to consumer"); |
| 310 | /* Continue since we MUST delete everything at this point. */ |
| 311 | } |
| 312 | } |
| 313 | rcu_read_unlock(); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * From a consumer_data structure, allocate and add a consumer socket to the |
| 319 | * consumer output. |
| 320 | * |
| 321 | * Return 0 on success, else negative value on error |
| 322 | */ |
| 323 | int consumer_create_socket(struct consumer_data *data, |
| 324 | struct consumer_output *output) |
| 325 | { |
| 326 | int ret = 0; |
| 327 | struct consumer_socket *socket; |
| 328 | |
| 329 | assert(data); |
| 330 | |
| 331 | if (output == NULL || data->cmd_sock < 0) { |
| 332 | /* |
| 333 | * Not an error. Possible there is simply not spawned consumer or it's |
| 334 | * disabled for the tracing session asking the socket. |
| 335 | */ |
| 336 | goto error; |
| 337 | } |
| 338 | |
| 339 | rcu_read_lock(); |
| 340 | socket = consumer_find_socket(data->cmd_sock, output); |
| 341 | rcu_read_unlock(); |
| 342 | if (socket == NULL) { |
| 343 | socket = consumer_allocate_socket(&data->cmd_sock); |
| 344 | if (socket == NULL) { |
| 345 | ret = -1; |
| 346 | goto error; |
| 347 | } |
| 348 | |
| 349 | socket->registered = 0; |
| 350 | socket->lock = &data->lock; |
| 351 | rcu_read_lock(); |
| 352 | consumer_add_socket(socket, output); |
| 353 | rcu_read_unlock(); |
| 354 | } |
| 355 | |
| 356 | socket->type = data->type; |
| 357 | |
| 358 | DBG3("Consumer socket created (fd: %d) and added to output", |
| 359 | data->cmd_sock); |
| 360 | |
| 361 | error: |
| 362 | return ret; |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | * Return the consumer socket from the given consumer output with the right |
| 367 | * bitness. On error, returns NULL. |
| 368 | * |
| 369 | * The caller MUST acquire a rcu read side lock and keep it until the socket |
| 370 | * object reference is not needed anymore. |
| 371 | */ |
| 372 | struct consumer_socket *consumer_find_socket_by_bitness(int bits, |
| 373 | const struct consumer_output *consumer) |
| 374 | { |
| 375 | int consumer_fd; |
| 376 | struct consumer_socket *socket = NULL; |
| 377 | |
| 378 | switch (bits) { |
| 379 | case 64: |
| 380 | consumer_fd = uatomic_read(&ust_consumerd64_fd); |
| 381 | break; |
| 382 | case 32: |
| 383 | consumer_fd = uatomic_read(&ust_consumerd32_fd); |
| 384 | break; |
| 385 | default: |
| 386 | assert(0); |
| 387 | goto end; |
| 388 | } |
| 389 | |
| 390 | socket = consumer_find_socket(consumer_fd, consumer); |
| 391 | if (!socket) { |
| 392 | ERR("Consumer socket fd %d not found in consumer obj %p", |
| 393 | consumer_fd, consumer); |
| 394 | } |
| 395 | |
| 396 | end: |
| 397 | return socket; |
| 398 | } |
| 399 | |
| 400 | /* |
| 401 | * Find a consumer_socket in a consumer_output hashtable. Read side lock must |
| 402 | * be acquired before calling this function and across use of the |
| 403 | * returned consumer_socket. |
| 404 | */ |
| 405 | struct consumer_socket *consumer_find_socket(int key, |
| 406 | const struct consumer_output *consumer) |
| 407 | { |
| 408 | struct lttng_ht_iter iter; |
| 409 | struct lttng_ht_node_ulong *node; |
| 410 | struct consumer_socket *socket = NULL; |
| 411 | |
| 412 | /* Negative keys are lookup failures */ |
| 413 | if (key < 0 || consumer == NULL) { |
| 414 | return NULL; |
| 415 | } |
| 416 | |
| 417 | lttng_ht_lookup(consumer->socks, (void *)((unsigned long) key), |
| 418 | &iter); |
| 419 | node = lttng_ht_iter_get_node_ulong(&iter); |
| 420 | if (node != NULL) { |
| 421 | socket = caa_container_of(node, struct consumer_socket, node); |
| 422 | } |
| 423 | |
| 424 | return socket; |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * Allocate a new consumer_socket and return the pointer. |
| 429 | */ |
| 430 | struct consumer_socket *consumer_allocate_socket(int *fd) |
| 431 | { |
| 432 | struct consumer_socket *socket = NULL; |
| 433 | |
| 434 | assert(fd); |
| 435 | |
| 436 | socket = zmalloc(sizeof(struct consumer_socket)); |
| 437 | if (socket == NULL) { |
| 438 | PERROR("zmalloc consumer socket"); |
| 439 | goto error; |
| 440 | } |
| 441 | |
| 442 | socket->fd_ptr = fd; |
| 443 | lttng_ht_node_init_ulong(&socket->node, *fd); |
| 444 | |
| 445 | error: |
| 446 | return socket; |
| 447 | } |
| 448 | |
| 449 | /* |
| 450 | * Add consumer socket to consumer output object. Read side lock must be |
| 451 | * acquired before calling this function. |
| 452 | */ |
| 453 | void consumer_add_socket(struct consumer_socket *sock, |
| 454 | struct consumer_output *consumer) |
| 455 | { |
| 456 | assert(sock); |
| 457 | assert(consumer); |
| 458 | |
| 459 | lttng_ht_add_unique_ulong(consumer->socks, &sock->node); |
| 460 | } |
| 461 | |
| 462 | /* |
| 463 | * Delete consumer socket to consumer output object. Read side lock must be |
| 464 | * acquired before calling this function. |
| 465 | */ |
| 466 | void consumer_del_socket(struct consumer_socket *sock, |
| 467 | struct consumer_output *consumer) |
| 468 | { |
| 469 | int ret; |
| 470 | struct lttng_ht_iter iter; |
| 471 | |
| 472 | assert(sock); |
| 473 | assert(consumer); |
| 474 | |
| 475 | iter.iter.node = &sock->node.node; |
| 476 | ret = lttng_ht_del(consumer->socks, &iter); |
| 477 | assert(!ret); |
| 478 | } |
| 479 | |
| 480 | /* |
| 481 | * RCU destroy call function. |
| 482 | */ |
| 483 | static void destroy_socket_rcu(struct rcu_head *head) |
| 484 | { |
| 485 | struct lttng_ht_node_ulong *node = |
| 486 | caa_container_of(head, struct lttng_ht_node_ulong, head); |
| 487 | struct consumer_socket *socket = |
| 488 | caa_container_of(node, struct consumer_socket, node); |
| 489 | |
| 490 | free(socket); |
| 491 | } |
| 492 | |
| 493 | /* |
| 494 | * Destroy and free socket pointer in a call RCU. Read side lock must be |
| 495 | * acquired before calling this function. |
| 496 | */ |
| 497 | void consumer_destroy_socket(struct consumer_socket *sock) |
| 498 | { |
| 499 | assert(sock); |
| 500 | |
| 501 | /* |
| 502 | * We DO NOT close the file descriptor here since it is global to the |
| 503 | * session daemon and is closed only if the consumer dies or a custom |
| 504 | * consumer was registered, |
| 505 | */ |
| 506 | if (sock->registered) { |
| 507 | DBG3("Consumer socket was registered. Closing fd %d", *sock->fd_ptr); |
| 508 | lttcomm_close_unix_sock(*sock->fd_ptr); |
| 509 | } |
| 510 | |
| 511 | call_rcu(&sock->node.head, destroy_socket_rcu); |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * Allocate and assign data to a consumer_output object. |
| 516 | * |
| 517 | * Return pointer to structure. |
| 518 | */ |
| 519 | struct consumer_output *consumer_create_output(enum consumer_dst_type type) |
| 520 | { |
| 521 | struct consumer_output *output = NULL; |
| 522 | |
| 523 | output = zmalloc(sizeof(struct consumer_output)); |
| 524 | if (output == NULL) { |
| 525 | PERROR("zmalloc consumer_output"); |
| 526 | goto error; |
| 527 | } |
| 528 | |
| 529 | /* By default, consumer output is enabled */ |
| 530 | output->enabled = 1; |
| 531 | output->type = type; |
| 532 | output->net_seq_index = (uint64_t) -1ULL; |
| 533 | urcu_ref_init(&output->ref); |
| 534 | |
| 535 | output->socks = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 536 | |
| 537 | error: |
| 538 | return output; |
| 539 | } |
| 540 | |
| 541 | /* |
| 542 | * Iterate over the consumer output socket hash table and destroy them. The |
| 543 | * socket file descriptor are only closed if the consumer output was |
| 544 | * registered meaning it's an external consumer. |
| 545 | */ |
| 546 | void consumer_destroy_output_sockets(struct consumer_output *obj) |
| 547 | { |
| 548 | struct lttng_ht_iter iter; |
| 549 | struct consumer_socket *socket; |
| 550 | |
| 551 | if (!obj->socks) { |
| 552 | return; |
| 553 | } |
| 554 | |
| 555 | rcu_read_lock(); |
| 556 | cds_lfht_for_each_entry(obj->socks->ht, &iter.iter, socket, node.node) { |
| 557 | consumer_del_socket(socket, obj); |
| 558 | consumer_destroy_socket(socket); |
| 559 | } |
| 560 | rcu_read_unlock(); |
| 561 | } |
| 562 | |
| 563 | /* |
| 564 | * Delete the consumer_output object from the list and free the ptr. |
| 565 | * |
| 566 | * Should *NOT* be called with RCU read-side lock held. |
| 567 | */ |
| 568 | static void consumer_release_output(struct urcu_ref *ref) |
| 569 | { |
| 570 | struct consumer_output *obj = |
| 571 | caa_container_of(ref, struct consumer_output, ref); |
| 572 | |
| 573 | consumer_destroy_output_sockets(obj); |
| 574 | |
| 575 | if (obj->socks) { |
| 576 | /* Finally destroy HT */ |
| 577 | ht_cleanup_push(obj->socks); |
| 578 | } |
| 579 | |
| 580 | free(obj); |
| 581 | } |
| 582 | |
| 583 | /* |
| 584 | * Get the consumer_output object. |
| 585 | */ |
| 586 | void consumer_output_get(struct consumer_output *obj) |
| 587 | { |
| 588 | urcu_ref_get(&obj->ref); |
| 589 | } |
| 590 | |
| 591 | /* |
| 592 | * Put the consumer_output object. |
| 593 | * |
| 594 | * Should *NOT* be called with RCU read-side lock held. |
| 595 | */ |
| 596 | void consumer_output_put(struct consumer_output *obj) |
| 597 | { |
| 598 | if (!obj) { |
| 599 | return; |
| 600 | } |
| 601 | urcu_ref_put(&obj->ref, consumer_release_output); |
| 602 | } |
| 603 | |
| 604 | /* |
| 605 | * Copy consumer output and returned the newly allocated copy. |
| 606 | * |
| 607 | * Should *NOT* be called with RCU read-side lock held. |
| 608 | */ |
| 609 | struct consumer_output *consumer_copy_output(struct consumer_output *src) |
| 610 | { |
| 611 | int ret; |
| 612 | struct consumer_output *output; |
| 613 | |
| 614 | assert(src); |
| 615 | |
| 616 | output = consumer_create_output(src->type); |
| 617 | if (output == NULL) { |
| 618 | goto end; |
| 619 | } |
| 620 | output->enabled = src->enabled; |
| 621 | output->net_seq_index = src->net_seq_index; |
| 622 | memcpy(output->domain_subdir, src->domain_subdir, |
| 623 | sizeof(output->domain_subdir)); |
| 624 | output->snapshot = src->snapshot; |
| 625 | output->relay_major_version = src->relay_major_version; |
| 626 | output->relay_minor_version = src->relay_minor_version; |
| 627 | memcpy(&output->dst, &src->dst, sizeof(output->dst)); |
| 628 | ret = consumer_copy_sockets(output, src); |
| 629 | if (ret < 0) { |
| 630 | goto error_put; |
| 631 | } |
| 632 | end: |
| 633 | return output; |
| 634 | |
| 635 | error_put: |
| 636 | consumer_output_put(output); |
| 637 | return NULL; |
| 638 | } |
| 639 | |
| 640 | /* |
| 641 | * Copy consumer sockets from src to dst. |
| 642 | * |
| 643 | * Return 0 on success or else a negative value. |
| 644 | */ |
| 645 | int consumer_copy_sockets(struct consumer_output *dst, |
| 646 | struct consumer_output *src) |
| 647 | { |
| 648 | int ret = 0; |
| 649 | struct lttng_ht_iter iter; |
| 650 | struct consumer_socket *socket, *copy_sock; |
| 651 | |
| 652 | assert(dst); |
| 653 | assert(src); |
| 654 | |
| 655 | rcu_read_lock(); |
| 656 | cds_lfht_for_each_entry(src->socks->ht, &iter.iter, socket, node.node) { |
| 657 | /* Ignore socket that are already there. */ |
| 658 | copy_sock = consumer_find_socket(*socket->fd_ptr, dst); |
| 659 | if (copy_sock) { |
| 660 | continue; |
| 661 | } |
| 662 | |
| 663 | /* Create new socket object. */ |
| 664 | copy_sock = consumer_allocate_socket(socket->fd_ptr); |
| 665 | if (copy_sock == NULL) { |
| 666 | rcu_read_unlock(); |
| 667 | ret = -ENOMEM; |
| 668 | goto error; |
| 669 | } |
| 670 | |
| 671 | copy_sock->registered = socket->registered; |
| 672 | /* |
| 673 | * This is valid because this lock is shared accross all consumer |
| 674 | * object being the global lock of the consumer data structure of the |
| 675 | * session daemon. |
| 676 | */ |
| 677 | copy_sock->lock = socket->lock; |
| 678 | consumer_add_socket(copy_sock, dst); |
| 679 | } |
| 680 | rcu_read_unlock(); |
| 681 | |
| 682 | error: |
| 683 | return ret; |
| 684 | } |
| 685 | |
| 686 | /* |
| 687 | * Set network URI to the consumer output. |
| 688 | * |
| 689 | * Return 0 on success. Return 1 if the URI were equal. Else, negative value on |
| 690 | * error. |
| 691 | */ |
| 692 | int consumer_set_network_uri(const struct ltt_session *session, |
| 693 | struct consumer_output *output, |
| 694 | struct lttng_uri *uri) |
| 695 | { |
| 696 | int ret; |
| 697 | struct lttng_uri *dst_uri = NULL; |
| 698 | |
| 699 | /* Code flow error safety net. */ |
| 700 | assert(output); |
| 701 | assert(uri); |
| 702 | |
| 703 | switch (uri->stype) { |
| 704 | case LTTNG_STREAM_CONTROL: |
| 705 | dst_uri = &output->dst.net.control; |
| 706 | output->dst.net.control_isset = 1; |
| 707 | if (uri->port == 0) { |
| 708 | /* Assign default port. */ |
| 709 | uri->port = DEFAULT_NETWORK_CONTROL_PORT; |
| 710 | } else { |
| 711 | if (output->dst.net.data_isset && uri->port == |
| 712 | output->dst.net.data.port) { |
| 713 | ret = -LTTNG_ERR_INVALID; |
| 714 | goto error; |
| 715 | } |
| 716 | } |
| 717 | DBG3("Consumer control URI set with port %d", uri->port); |
| 718 | break; |
| 719 | case LTTNG_STREAM_DATA: |
| 720 | dst_uri = &output->dst.net.data; |
| 721 | output->dst.net.data_isset = 1; |
| 722 | if (uri->port == 0) { |
| 723 | /* Assign default port. */ |
| 724 | uri->port = DEFAULT_NETWORK_DATA_PORT; |
| 725 | } else { |
| 726 | if (output->dst.net.control_isset && uri->port == |
| 727 | output->dst.net.control.port) { |
| 728 | ret = -LTTNG_ERR_INVALID; |
| 729 | goto error; |
| 730 | } |
| 731 | } |
| 732 | DBG3("Consumer data URI set with port %d", uri->port); |
| 733 | break; |
| 734 | default: |
| 735 | ERR("Set network uri type unknown %d", uri->stype); |
| 736 | ret = -LTTNG_ERR_INVALID; |
| 737 | goto error; |
| 738 | } |
| 739 | |
| 740 | ret = uri_compare(dst_uri, uri); |
| 741 | if (!ret) { |
| 742 | /* Same URI, don't touch it and return success. */ |
| 743 | DBG3("URI network compare are the same"); |
| 744 | goto equal; |
| 745 | } |
| 746 | |
| 747 | /* URIs were not equal, replacing it. */ |
| 748 | memcpy(dst_uri, uri, sizeof(struct lttng_uri)); |
| 749 | output->type = CONSUMER_DST_NET; |
| 750 | if (dst_uri->stype != LTTNG_STREAM_CONTROL) { |
| 751 | /* Only the control uri needs to contain the path. */ |
| 752 | goto end; |
| 753 | } |
| 754 | |
| 755 | /* |
| 756 | * If the user has specified a subdir as part of the control |
| 757 | * URL, the session's base output directory is: |
| 758 | * /RELAYD_OUTPUT_PATH/HOSTNAME/USER_SPECIFIED_DIR |
| 759 | * |
| 760 | * Hence, the "base_dir" from which all stream files and |
| 761 | * session rotation chunks are created takes the form |
| 762 | * /HOSTNAME/USER_SPECIFIED_DIR |
| 763 | * |
| 764 | * If the user has not specified an output directory as part of |
| 765 | * the control URL, the base output directory has the form: |
| 766 | * /RELAYD_OUTPUT_PATH/HOSTNAME/SESSION_NAME-CREATION_TIME |
| 767 | * |
| 768 | * Hence, the "base_dir" from which all stream files and |
| 769 | * session rotation chunks are created takes the form |
| 770 | * /HOSTNAME/SESSION_NAME-CREATION_TIME |
| 771 | * |
| 772 | * Note that automatically generated session names already |
| 773 | * contain the session's creation time. In that case, the |
| 774 | * creation time is omitted to prevent it from being duplicated |
| 775 | * in the final directory hierarchy. |
| 776 | */ |
| 777 | if (*uri->subdir) { |
| 778 | if (strstr(uri->subdir, "../")) { |
| 779 | ERR("Network URI subdirs are not allowed to walk up the path hierarchy"); |
| 780 | ret = -LTTNG_ERR_INVALID; |
| 781 | goto error; |
| 782 | } |
| 783 | ret = snprintf(output->dst.net.base_dir, |
| 784 | sizeof(output->dst.net.base_dir), |
| 785 | "/%s/%s/", session->hostname, uri->subdir); |
| 786 | } else { |
| 787 | if (session->has_auto_generated_name) { |
| 788 | ret = snprintf(output->dst.net.base_dir, |
| 789 | sizeof(output->dst.net.base_dir), |
| 790 | "/%s/%s/", session->hostname, |
| 791 | session->name); |
| 792 | } else { |
| 793 | char session_creation_datetime[16]; |
| 794 | size_t strftime_ret; |
| 795 | struct tm *timeinfo; |
| 796 | |
| 797 | timeinfo = localtime(&session->creation_time); |
| 798 | if (!timeinfo) { |
| 799 | ret = -LTTNG_ERR_FATAL; |
| 800 | goto error; |
| 801 | } |
| 802 | strftime_ret = strftime(session_creation_datetime, |
| 803 | sizeof(session_creation_datetime), |
| 804 | "%Y%m%d-%H%M%S", timeinfo); |
| 805 | if (strftime_ret == 0) { |
| 806 | ERR("Failed to format session creation timestamp while setting network URI"); |
| 807 | ret = -LTTNG_ERR_FATAL; |
| 808 | goto error; |
| 809 | } |
| 810 | ret = snprintf(output->dst.net.base_dir, |
| 811 | sizeof(output->dst.net.base_dir), |
| 812 | "/%s/%s-%s/", session->hostname, |
| 813 | session->name, |
| 814 | session_creation_datetime); |
| 815 | } |
| 816 | } |
| 817 | if (ret >= sizeof(output->dst.net.base_dir)) { |
| 818 | ret = -LTTNG_ERR_INVALID; |
| 819 | ERR("Truncation occurred while setting network output base directory"); |
| 820 | goto error; |
| 821 | } else if (ret == -1) { |
| 822 | ret = -LTTNG_ERR_INVALID; |
| 823 | PERROR("Error occurred while setting network output base directory"); |
| 824 | goto error; |
| 825 | } |
| 826 | |
| 827 | DBG3("Consumer set network uri base_dir path %s", |
| 828 | output->dst.net.base_dir); |
| 829 | |
| 830 | end: |
| 831 | return 0; |
| 832 | equal: |
| 833 | return 1; |
| 834 | error: |
| 835 | return ret; |
| 836 | } |
| 837 | |
| 838 | /* |
| 839 | * Send file descriptor to consumer via sock. |
| 840 | * |
| 841 | * The consumer socket lock must be held by the caller. |
| 842 | */ |
| 843 | int consumer_send_fds(struct consumer_socket *sock, const int *fds, |
| 844 | size_t nb_fd) |
| 845 | { |
| 846 | int ret; |
| 847 | |
| 848 | assert(fds); |
| 849 | assert(sock); |
| 850 | assert(nb_fd > 0); |
| 851 | assert(pthread_mutex_trylock(sock->lock) == EBUSY); |
| 852 | |
| 853 | ret = lttcomm_send_fds_unix_sock(*sock->fd_ptr, fds, nb_fd); |
| 854 | if (ret < 0) { |
| 855 | /* The above call will print a PERROR on error. */ |
| 856 | DBG("Error when sending consumer fds on sock %d", *sock->fd_ptr); |
| 857 | goto error; |
| 858 | } |
| 859 | |
| 860 | ret = consumer_recv_status_reply(sock); |
| 861 | error: |
| 862 | return ret; |
| 863 | } |
| 864 | |
| 865 | /* |
| 866 | * Consumer send communication message structure to consumer. |
| 867 | * |
| 868 | * The consumer socket lock must be held by the caller. |
| 869 | */ |
| 870 | int consumer_send_msg(struct consumer_socket *sock, |
| 871 | struct lttcomm_consumer_msg *msg) |
| 872 | { |
| 873 | int ret; |
| 874 | |
| 875 | assert(msg); |
| 876 | assert(sock); |
| 877 | assert(pthread_mutex_trylock(sock->lock) == EBUSY); |
| 878 | |
| 879 | ret = consumer_socket_send(sock, msg, sizeof(struct lttcomm_consumer_msg)); |
| 880 | if (ret < 0) { |
| 881 | goto error; |
| 882 | } |
| 883 | |
| 884 | ret = consumer_recv_status_reply(sock); |
| 885 | |
| 886 | error: |
| 887 | return ret; |
| 888 | } |
| 889 | |
| 890 | /* |
| 891 | * Consumer send channel communication message structure to consumer. |
| 892 | * |
| 893 | * The consumer socket lock must be held by the caller. |
| 894 | */ |
| 895 | int consumer_send_channel(struct consumer_socket *sock, |
| 896 | struct lttcomm_consumer_msg *msg) |
| 897 | { |
| 898 | int ret; |
| 899 | |
| 900 | assert(msg); |
| 901 | assert(sock); |
| 902 | |
| 903 | ret = consumer_send_msg(sock, msg); |
| 904 | if (ret < 0) { |
| 905 | goto error; |
| 906 | } |
| 907 | |
| 908 | error: |
| 909 | return ret; |
| 910 | } |
| 911 | |
| 912 | /* |
| 913 | * Populate the given consumer msg structure with the ask_channel command |
| 914 | * information. |
| 915 | */ |
| 916 | void consumer_init_ask_channel_comm_msg(struct lttcomm_consumer_msg *msg, |
| 917 | uint64_t subbuf_size, |
| 918 | uint64_t num_subbuf, |
| 919 | int overwrite, |
| 920 | unsigned int switch_timer_interval, |
| 921 | unsigned int read_timer_interval, |
| 922 | unsigned int live_timer_interval, |
| 923 | unsigned int monitor_timer_interval, |
| 924 | int output, |
| 925 | int type, |
| 926 | uint64_t session_id, |
| 927 | const char *pathname, |
| 928 | const char *name, |
| 929 | uint64_t relayd_id, |
| 930 | uint64_t key, |
| 931 | unsigned char *uuid, |
| 932 | uint32_t chan_id, |
| 933 | uint64_t tracefile_size, |
| 934 | uint64_t tracefile_count, |
| 935 | uint64_t session_id_per_pid, |
| 936 | unsigned int monitor, |
| 937 | uint32_t ust_app_uid, |
| 938 | int64_t blocking_timeout, |
| 939 | const char *root_shm_path, |
| 940 | const char *shm_path, |
| 941 | struct lttng_trace_chunk *trace_chunk, |
| 942 | const struct lttng_credentials *buffer_credentials) |
| 943 | { |
| 944 | assert(msg); |
| 945 | |
| 946 | /* Zeroed structure */ |
| 947 | memset(msg, 0, sizeof(struct lttcomm_consumer_msg)); |
| 948 | msg->u.ask_channel.buffer_credentials.uid = UINT32_MAX; |
| 949 | msg->u.ask_channel.buffer_credentials.gid = UINT32_MAX; |
| 950 | |
| 951 | if (trace_chunk) { |
| 952 | uint64_t chunk_id; |
| 953 | enum lttng_trace_chunk_status chunk_status; |
| 954 | |
| 955 | chunk_status = lttng_trace_chunk_get_id(trace_chunk, &chunk_id); |
| 956 | assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK); |
| 957 | LTTNG_OPTIONAL_SET(&msg->u.ask_channel.chunk_id, chunk_id); |
| 958 | } |
| 959 | msg->u.ask_channel.buffer_credentials.uid = buffer_credentials->uid; |
| 960 | msg->u.ask_channel.buffer_credentials.gid = buffer_credentials->gid; |
| 961 | |
| 962 | msg->cmd_type = LTTNG_CONSUMER_ASK_CHANNEL_CREATION; |
| 963 | msg->u.ask_channel.subbuf_size = subbuf_size; |
| 964 | msg->u.ask_channel.num_subbuf = num_subbuf ; |
| 965 | msg->u.ask_channel.overwrite = overwrite; |
| 966 | msg->u.ask_channel.switch_timer_interval = switch_timer_interval; |
| 967 | msg->u.ask_channel.read_timer_interval = read_timer_interval; |
| 968 | msg->u.ask_channel.live_timer_interval = live_timer_interval; |
| 969 | msg->u.ask_channel.monitor_timer_interval = monitor_timer_interval; |
| 970 | msg->u.ask_channel.output = output; |
| 971 | msg->u.ask_channel.type = type; |
| 972 | msg->u.ask_channel.session_id = session_id; |
| 973 | msg->u.ask_channel.session_id_per_pid = session_id_per_pid; |
| 974 | msg->u.ask_channel.relayd_id = relayd_id; |
| 975 | msg->u.ask_channel.key = key; |
| 976 | msg->u.ask_channel.chan_id = chan_id; |
| 977 | msg->u.ask_channel.tracefile_size = tracefile_size; |
| 978 | msg->u.ask_channel.tracefile_count = tracefile_count; |
| 979 | msg->u.ask_channel.monitor = monitor; |
| 980 | msg->u.ask_channel.ust_app_uid = ust_app_uid; |
| 981 | msg->u.ask_channel.blocking_timeout = blocking_timeout; |
| 982 | |
| 983 | memcpy(msg->u.ask_channel.uuid, uuid, sizeof(msg->u.ask_channel.uuid)); |
| 984 | |
| 985 | if (pathname) { |
| 986 | strncpy(msg->u.ask_channel.pathname, pathname, |
| 987 | sizeof(msg->u.ask_channel.pathname)); |
| 988 | msg->u.ask_channel.pathname[sizeof(msg->u.ask_channel.pathname)-1] = '\0'; |
| 989 | } |
| 990 | |
| 991 | strncpy(msg->u.ask_channel.name, name, sizeof(msg->u.ask_channel.name)); |
| 992 | msg->u.ask_channel.name[sizeof(msg->u.ask_channel.name) - 1] = '\0'; |
| 993 | |
| 994 | if (root_shm_path) { |
| 995 | strncpy(msg->u.ask_channel.root_shm_path, root_shm_path, |
| 996 | sizeof(msg->u.ask_channel.root_shm_path)); |
| 997 | msg->u.ask_channel.root_shm_path[sizeof(msg->u.ask_channel.root_shm_path) - 1] = '\0'; |
| 998 | } |
| 999 | if (shm_path) { |
| 1000 | strncpy(msg->u.ask_channel.shm_path, shm_path, |
| 1001 | sizeof(msg->u.ask_channel.shm_path)); |
| 1002 | msg->u.ask_channel.shm_path[sizeof(msg->u.ask_channel.shm_path) - 1] = '\0'; |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | /* |
| 1007 | * Init channel communication message structure. |
| 1008 | */ |
| 1009 | void consumer_init_add_channel_comm_msg(struct lttcomm_consumer_msg *msg, |
| 1010 | uint64_t channel_key, |
| 1011 | uint64_t session_id, |
| 1012 | const char *pathname, |
| 1013 | uid_t uid, |
| 1014 | gid_t gid, |
| 1015 | uint64_t relayd_id, |
| 1016 | const char *name, |
| 1017 | unsigned int nb_init_streams, |
| 1018 | enum lttng_event_output output, |
| 1019 | int type, |
| 1020 | uint64_t tracefile_size, |
| 1021 | uint64_t tracefile_count, |
| 1022 | unsigned int monitor, |
| 1023 | unsigned int live_timer_interval, |
| 1024 | unsigned int monitor_timer_interval, |
| 1025 | struct lttng_trace_chunk *trace_chunk) |
| 1026 | { |
| 1027 | assert(msg); |
| 1028 | |
| 1029 | /* Zeroed structure */ |
| 1030 | memset(msg, 0, sizeof(struct lttcomm_consumer_msg)); |
| 1031 | |
| 1032 | if (trace_chunk) { |
| 1033 | uint64_t chunk_id; |
| 1034 | enum lttng_trace_chunk_status chunk_status; |
| 1035 | |
| 1036 | chunk_status = lttng_trace_chunk_get_id(trace_chunk, &chunk_id); |
| 1037 | assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK); |
| 1038 | LTTNG_OPTIONAL_SET(&msg->u.channel.chunk_id, chunk_id); |
| 1039 | } |
| 1040 | |
| 1041 | /* Send channel */ |
| 1042 | msg->cmd_type = LTTNG_CONSUMER_ADD_CHANNEL; |
| 1043 | msg->u.channel.channel_key = channel_key; |
| 1044 | msg->u.channel.session_id = session_id; |
| 1045 | msg->u.channel.relayd_id = relayd_id; |
| 1046 | msg->u.channel.nb_init_streams = nb_init_streams; |
| 1047 | msg->u.channel.output = output; |
| 1048 | msg->u.channel.type = type; |
| 1049 | msg->u.channel.tracefile_size = tracefile_size; |
| 1050 | msg->u.channel.tracefile_count = tracefile_count; |
| 1051 | msg->u.channel.monitor = monitor; |
| 1052 | msg->u.channel.live_timer_interval = live_timer_interval; |
| 1053 | msg->u.channel.monitor_timer_interval = monitor_timer_interval; |
| 1054 | |
| 1055 | strncpy(msg->u.channel.pathname, pathname, |
| 1056 | sizeof(msg->u.channel.pathname)); |
| 1057 | msg->u.channel.pathname[sizeof(msg->u.channel.pathname) - 1] = '\0'; |
| 1058 | |
| 1059 | strncpy(msg->u.channel.name, name, sizeof(msg->u.channel.name)); |
| 1060 | msg->u.channel.name[sizeof(msg->u.channel.name) - 1] = '\0'; |
| 1061 | } |
| 1062 | |
| 1063 | /* |
| 1064 | * Init stream communication message structure. |
| 1065 | */ |
| 1066 | void consumer_init_add_stream_comm_msg(struct lttcomm_consumer_msg *msg, |
| 1067 | uint64_t channel_key, |
| 1068 | uint64_t stream_key, |
| 1069 | int32_t cpu) |
| 1070 | { |
| 1071 | assert(msg); |
| 1072 | |
| 1073 | memset(msg, 0, sizeof(struct lttcomm_consumer_msg)); |
| 1074 | |
| 1075 | msg->cmd_type = LTTNG_CONSUMER_ADD_STREAM; |
| 1076 | msg->u.stream.channel_key = channel_key; |
| 1077 | msg->u.stream.stream_key = stream_key; |
| 1078 | msg->u.stream.cpu = cpu; |
| 1079 | } |
| 1080 | |
| 1081 | void consumer_init_streams_sent_comm_msg(struct lttcomm_consumer_msg *msg, |
| 1082 | enum lttng_consumer_command cmd, |
| 1083 | uint64_t channel_key, uint64_t net_seq_idx) |
| 1084 | { |
| 1085 | assert(msg); |
| 1086 | |
| 1087 | memset(msg, 0, sizeof(struct lttcomm_consumer_msg)); |
| 1088 | |
| 1089 | msg->cmd_type = cmd; |
| 1090 | msg->u.sent_streams.channel_key = channel_key; |
| 1091 | msg->u.sent_streams.net_seq_idx = net_seq_idx; |
| 1092 | } |
| 1093 | |
| 1094 | /* |
| 1095 | * Send stream communication structure to the consumer. |
| 1096 | */ |
| 1097 | int consumer_send_stream(struct consumer_socket *sock, |
| 1098 | struct consumer_output *dst, struct lttcomm_consumer_msg *msg, |
| 1099 | const int *fds, size_t nb_fd) |
| 1100 | { |
| 1101 | int ret; |
| 1102 | |
| 1103 | assert(msg); |
| 1104 | assert(dst); |
| 1105 | assert(sock); |
| 1106 | assert(fds); |
| 1107 | |
| 1108 | ret = consumer_send_msg(sock, msg); |
| 1109 | if (ret < 0) { |
| 1110 | goto error; |
| 1111 | } |
| 1112 | |
| 1113 | ret = consumer_send_fds(sock, fds, nb_fd); |
| 1114 | if (ret < 0) { |
| 1115 | goto error; |
| 1116 | } |
| 1117 | |
| 1118 | error: |
| 1119 | return ret; |
| 1120 | } |
| 1121 | |
| 1122 | /* |
| 1123 | * Send relayd socket to consumer associated with a session name. |
| 1124 | * |
| 1125 | * The consumer socket lock must be held by the caller. |
| 1126 | * |
| 1127 | * On success return positive value. On error, negative value. |
| 1128 | */ |
| 1129 | int consumer_send_relayd_socket(struct consumer_socket *consumer_sock, |
| 1130 | struct lttcomm_relayd_sock *rsock, struct consumer_output *consumer, |
| 1131 | enum lttng_stream_type type, uint64_t session_id, |
| 1132 | const char *session_name, const char *hostname, |
| 1133 | const char *base_path, int session_live_timer, |
| 1134 | const uint64_t *current_chunk_id, time_t session_creation_time, |
| 1135 | bool session_name_contains_creation_time) |
| 1136 | { |
| 1137 | int ret; |
| 1138 | struct lttcomm_consumer_msg msg; |
| 1139 | |
| 1140 | /* Code flow error. Safety net. */ |
| 1141 | assert(rsock); |
| 1142 | assert(consumer); |
| 1143 | assert(consumer_sock); |
| 1144 | |
| 1145 | memset(&msg, 0, sizeof(msg)); |
| 1146 | /* Bail out if consumer is disabled */ |
| 1147 | if (!consumer->enabled) { |
| 1148 | ret = LTTNG_OK; |
| 1149 | goto error; |
| 1150 | } |
| 1151 | |
| 1152 | if (type == LTTNG_STREAM_CONTROL) { |
| 1153 | char output_path[LTTNG_PATH_MAX] = {}; |
| 1154 | uint64_t relayd_session_id; |
| 1155 | |
| 1156 | ret = relayd_create_session(rsock, |
| 1157 | &relayd_session_id, |
| 1158 | session_name, hostname, base_path, |
| 1159 | session_live_timer, |
| 1160 | consumer->snapshot, session_id, |
| 1161 | sessiond_uuid, current_chunk_id, |
| 1162 | session_creation_time, |
| 1163 | session_name_contains_creation_time, |
| 1164 | output_path); |
| 1165 | if (ret < 0) { |
| 1166 | /* Close the control socket. */ |
| 1167 | (void) relayd_close(rsock); |
| 1168 | goto error; |
| 1169 | } |
| 1170 | msg.u.relayd_sock.relayd_session_id = relayd_session_id; |
| 1171 | DBG("Created session on relay, output path reply: %s", |
| 1172 | output_path); |
| 1173 | } |
| 1174 | |
| 1175 | msg.cmd_type = LTTNG_CONSUMER_ADD_RELAYD_SOCKET; |
| 1176 | /* |
| 1177 | * Assign network consumer output index using the temporary consumer since |
| 1178 | * this call should only be made from within a set_consumer_uri() function |
| 1179 | * call in the session daemon. |
| 1180 | */ |
| 1181 | msg.u.relayd_sock.net_index = consumer->net_seq_index; |
| 1182 | msg.u.relayd_sock.type = type; |
| 1183 | msg.u.relayd_sock.session_id = session_id; |
| 1184 | memcpy(&msg.u.relayd_sock.sock, rsock, sizeof(msg.u.relayd_sock.sock)); |
| 1185 | |
| 1186 | DBG3("Sending relayd sock info to consumer on %d", *consumer_sock->fd_ptr); |
| 1187 | ret = consumer_send_msg(consumer_sock, &msg); |
| 1188 | if (ret < 0) { |
| 1189 | goto error; |
| 1190 | } |
| 1191 | |
| 1192 | DBG3("Sending relayd socket file descriptor to consumer"); |
| 1193 | ret = consumer_send_fds(consumer_sock, ALIGNED_CONST_PTR(rsock->sock.fd), 1); |
| 1194 | if (ret < 0) { |
| 1195 | goto error; |
| 1196 | } |
| 1197 | |
| 1198 | DBG2("Consumer relayd socket sent"); |
| 1199 | |
| 1200 | error: |
| 1201 | return ret; |
| 1202 | } |
| 1203 | |
| 1204 | static |
| 1205 | int consumer_send_pipe(struct consumer_socket *consumer_sock, |
| 1206 | enum lttng_consumer_command cmd, int pipe) |
| 1207 | { |
| 1208 | int ret; |
| 1209 | struct lttcomm_consumer_msg msg; |
| 1210 | const char *pipe_name; |
| 1211 | const char *command_name; |
| 1212 | |
| 1213 | switch (cmd) { |
| 1214 | case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE: |
| 1215 | pipe_name = "channel monitor"; |
| 1216 | command_name = "SET_CHANNEL_MONITOR_PIPE"; |
| 1217 | break; |
| 1218 | default: |
| 1219 | ERR("Unexpected command received in %s (cmd = %d)", __func__, |
| 1220 | (int) cmd); |
| 1221 | abort(); |
| 1222 | } |
| 1223 | |
| 1224 | /* Code flow error. Safety net. */ |
| 1225 | |
| 1226 | memset(&msg, 0, sizeof(msg)); |
| 1227 | msg.cmd_type = cmd; |
| 1228 | |
| 1229 | pthread_mutex_lock(consumer_sock->lock); |
| 1230 | DBG3("Sending %s command to consumer", command_name); |
| 1231 | ret = consumer_send_msg(consumer_sock, &msg); |
| 1232 | if (ret < 0) { |
| 1233 | goto error; |
| 1234 | } |
| 1235 | |
| 1236 | DBG3("Sending %s pipe %d to consumer on socket %d", |
| 1237 | pipe_name, |
| 1238 | pipe, *consumer_sock->fd_ptr); |
| 1239 | ret = consumer_send_fds(consumer_sock, &pipe, 1); |
| 1240 | if (ret < 0) { |
| 1241 | goto error; |
| 1242 | } |
| 1243 | |
| 1244 | DBG2("%s pipe successfully sent", pipe_name); |
| 1245 | error: |
| 1246 | pthread_mutex_unlock(consumer_sock->lock); |
| 1247 | return ret; |
| 1248 | } |
| 1249 | |
| 1250 | int consumer_send_channel_monitor_pipe(struct consumer_socket *consumer_sock, |
| 1251 | int pipe) |
| 1252 | { |
| 1253 | return consumer_send_pipe(consumer_sock, |
| 1254 | LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE, pipe); |
| 1255 | } |
| 1256 | |
| 1257 | /* |
| 1258 | * Ask the consumer if the data is pending for the specific session id. |
| 1259 | * Returns 1 if data is pending, 0 otherwise, or < 0 on error. |
| 1260 | */ |
| 1261 | int consumer_is_data_pending(uint64_t session_id, |
| 1262 | struct consumer_output *consumer) |
| 1263 | { |
| 1264 | int ret; |
| 1265 | int32_t ret_code = 0; /* Default is that the data is NOT pending */ |
| 1266 | struct consumer_socket *socket; |
| 1267 | struct lttng_ht_iter iter; |
| 1268 | struct lttcomm_consumer_msg msg; |
| 1269 | |
| 1270 | assert(consumer); |
| 1271 | |
| 1272 | DBG3("Consumer data pending for id %" PRIu64, session_id); |
| 1273 | |
| 1274 | memset(&msg, 0, sizeof(msg)); |
| 1275 | msg.cmd_type = LTTNG_CONSUMER_DATA_PENDING; |
| 1276 | msg.u.data_pending.session_id = session_id; |
| 1277 | |
| 1278 | /* Send command for each consumer */ |
| 1279 | rcu_read_lock(); |
| 1280 | cds_lfht_for_each_entry(consumer->socks->ht, &iter.iter, socket, |
| 1281 | node.node) { |
| 1282 | pthread_mutex_lock(socket->lock); |
| 1283 | ret = consumer_socket_send(socket, &msg, sizeof(msg)); |
| 1284 | if (ret < 0) { |
| 1285 | pthread_mutex_unlock(socket->lock); |
| 1286 | goto error_unlock; |
| 1287 | } |
| 1288 | |
| 1289 | /* |
| 1290 | * No need for a recv reply status because the answer to the command is |
| 1291 | * the reply status message. |
| 1292 | */ |
| 1293 | |
| 1294 | ret = consumer_socket_recv(socket, &ret_code, sizeof(ret_code)); |
| 1295 | if (ret < 0) { |
| 1296 | pthread_mutex_unlock(socket->lock); |
| 1297 | goto error_unlock; |
| 1298 | } |
| 1299 | pthread_mutex_unlock(socket->lock); |
| 1300 | |
| 1301 | if (ret_code == 1) { |
| 1302 | break; |
| 1303 | } |
| 1304 | } |
| 1305 | rcu_read_unlock(); |
| 1306 | |
| 1307 | DBG("Consumer data is %s pending for session id %" PRIu64, |
| 1308 | ret_code == 1 ? "" : "NOT", session_id); |
| 1309 | return ret_code; |
| 1310 | |
| 1311 | error_unlock: |
| 1312 | rcu_read_unlock(); |
| 1313 | return -1; |
| 1314 | } |
| 1315 | |
| 1316 | /* |
| 1317 | * Send a flush command to consumer using the given channel key. |
| 1318 | * |
| 1319 | * Return 0 on success else a negative value. |
| 1320 | */ |
| 1321 | int consumer_flush_channel(struct consumer_socket *socket, uint64_t key) |
| 1322 | { |
| 1323 | int ret; |
| 1324 | struct lttcomm_consumer_msg msg; |
| 1325 | |
| 1326 | assert(socket); |
| 1327 | |
| 1328 | DBG2("Consumer flush channel key %" PRIu64, key); |
| 1329 | |
| 1330 | memset(&msg, 0, sizeof(msg)); |
| 1331 | msg.cmd_type = LTTNG_CONSUMER_FLUSH_CHANNEL; |
| 1332 | msg.u.flush_channel.key = key; |
| 1333 | |
| 1334 | pthread_mutex_lock(socket->lock); |
| 1335 | health_code_update(); |
| 1336 | |
| 1337 | ret = consumer_send_msg(socket, &msg); |
| 1338 | if (ret < 0) { |
| 1339 | goto end; |
| 1340 | } |
| 1341 | |
| 1342 | end: |
| 1343 | health_code_update(); |
| 1344 | pthread_mutex_unlock(socket->lock); |
| 1345 | return ret; |
| 1346 | } |
| 1347 | |
| 1348 | /* |
| 1349 | * Send a clear quiescent command to consumer using the given channel key. |
| 1350 | * |
| 1351 | * Return 0 on success else a negative value. |
| 1352 | */ |
| 1353 | int consumer_clear_quiescent_channel(struct consumer_socket *socket, uint64_t key) |
| 1354 | { |
| 1355 | int ret; |
| 1356 | struct lttcomm_consumer_msg msg; |
| 1357 | |
| 1358 | assert(socket); |
| 1359 | |
| 1360 | DBG2("Consumer clear quiescent channel key %" PRIu64, key); |
| 1361 | |
| 1362 | memset(&msg, 0, sizeof(msg)); |
| 1363 | msg.cmd_type = LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL; |
| 1364 | msg.u.clear_quiescent_channel.key = key; |
| 1365 | |
| 1366 | pthread_mutex_lock(socket->lock); |
| 1367 | health_code_update(); |
| 1368 | |
| 1369 | ret = consumer_send_msg(socket, &msg); |
| 1370 | if (ret < 0) { |
| 1371 | goto end; |
| 1372 | } |
| 1373 | |
| 1374 | end: |
| 1375 | health_code_update(); |
| 1376 | pthread_mutex_unlock(socket->lock); |
| 1377 | return ret; |
| 1378 | } |
| 1379 | |
| 1380 | /* |
| 1381 | * Send a close metadata command to consumer using the given channel key. |
| 1382 | * Called with registry lock held. |
| 1383 | * |
| 1384 | * Return 0 on success else a negative value. |
| 1385 | */ |
| 1386 | int consumer_close_metadata(struct consumer_socket *socket, |
| 1387 | uint64_t metadata_key) |
| 1388 | { |
| 1389 | int ret; |
| 1390 | struct lttcomm_consumer_msg msg; |
| 1391 | |
| 1392 | assert(socket); |
| 1393 | |
| 1394 | DBG2("Consumer close metadata channel key %" PRIu64, metadata_key); |
| 1395 | |
| 1396 | memset(&msg, 0, sizeof(msg)); |
| 1397 | msg.cmd_type = LTTNG_CONSUMER_CLOSE_METADATA; |
| 1398 | msg.u.close_metadata.key = metadata_key; |
| 1399 | |
| 1400 | pthread_mutex_lock(socket->lock); |
| 1401 | health_code_update(); |
| 1402 | |
| 1403 | ret = consumer_send_msg(socket, &msg); |
| 1404 | if (ret < 0) { |
| 1405 | goto end; |
| 1406 | } |
| 1407 | |
| 1408 | end: |
| 1409 | health_code_update(); |
| 1410 | pthread_mutex_unlock(socket->lock); |
| 1411 | return ret; |
| 1412 | } |
| 1413 | |
| 1414 | /* |
| 1415 | * Send a setup metdata command to consumer using the given channel key. |
| 1416 | * |
| 1417 | * Return 0 on success else a negative value. |
| 1418 | */ |
| 1419 | int consumer_setup_metadata(struct consumer_socket *socket, |
| 1420 | uint64_t metadata_key) |
| 1421 | { |
| 1422 | int ret; |
| 1423 | struct lttcomm_consumer_msg msg; |
| 1424 | |
| 1425 | assert(socket); |
| 1426 | |
| 1427 | DBG2("Consumer setup metadata channel key %" PRIu64, metadata_key); |
| 1428 | |
| 1429 | memset(&msg, 0, sizeof(msg)); |
| 1430 | msg.cmd_type = LTTNG_CONSUMER_SETUP_METADATA; |
| 1431 | msg.u.setup_metadata.key = metadata_key; |
| 1432 | |
| 1433 | pthread_mutex_lock(socket->lock); |
| 1434 | health_code_update(); |
| 1435 | |
| 1436 | ret = consumer_send_msg(socket, &msg); |
| 1437 | if (ret < 0) { |
| 1438 | goto end; |
| 1439 | } |
| 1440 | |
| 1441 | end: |
| 1442 | health_code_update(); |
| 1443 | pthread_mutex_unlock(socket->lock); |
| 1444 | return ret; |
| 1445 | } |
| 1446 | |
| 1447 | /* |
| 1448 | * Send metadata string to consumer. |
| 1449 | * RCU read-side lock must be held to guarantee existence of socket. |
| 1450 | * |
| 1451 | * Return 0 on success else a negative value. |
| 1452 | */ |
| 1453 | int consumer_push_metadata(struct consumer_socket *socket, |
| 1454 | uint64_t metadata_key, char *metadata_str, size_t len, |
| 1455 | size_t target_offset, uint64_t version) |
| 1456 | { |
| 1457 | int ret; |
| 1458 | struct lttcomm_consumer_msg msg; |
| 1459 | |
| 1460 | assert(socket); |
| 1461 | |
| 1462 | DBG2("Consumer push metadata to consumer socket %d", *socket->fd_ptr); |
| 1463 | |
| 1464 | pthread_mutex_lock(socket->lock); |
| 1465 | |
| 1466 | memset(&msg, 0, sizeof(msg)); |
| 1467 | msg.cmd_type = LTTNG_CONSUMER_PUSH_METADATA; |
| 1468 | msg.u.push_metadata.key = metadata_key; |
| 1469 | msg.u.push_metadata.target_offset = target_offset; |
| 1470 | msg.u.push_metadata.len = len; |
| 1471 | msg.u.push_metadata.version = version; |
| 1472 | |
| 1473 | health_code_update(); |
| 1474 | ret = consumer_send_msg(socket, &msg); |
| 1475 | if (ret < 0 || len == 0) { |
| 1476 | goto end; |
| 1477 | } |
| 1478 | |
| 1479 | DBG3("Consumer pushing metadata on sock %d of len %zu", *socket->fd_ptr, |
| 1480 | len); |
| 1481 | |
| 1482 | ret = consumer_socket_send(socket, metadata_str, len); |
| 1483 | if (ret < 0) { |
| 1484 | goto end; |
| 1485 | } |
| 1486 | |
| 1487 | health_code_update(); |
| 1488 | ret = consumer_recv_status_reply(socket); |
| 1489 | if (ret < 0) { |
| 1490 | goto end; |
| 1491 | } |
| 1492 | |
| 1493 | end: |
| 1494 | pthread_mutex_unlock(socket->lock); |
| 1495 | health_code_update(); |
| 1496 | return ret; |
| 1497 | } |
| 1498 | |
| 1499 | /* |
| 1500 | * Ask the consumer to snapshot a specific channel using the key. |
| 1501 | * |
| 1502 | * Returns LTTNG_OK on success or else an LTTng error code. |
| 1503 | */ |
| 1504 | enum lttng_error_code consumer_snapshot_channel(struct consumer_socket *socket, |
| 1505 | uint64_t key, const struct consumer_output *output, int metadata, |
| 1506 | uid_t uid, gid_t gid, const char *channel_path, int wait, |
| 1507 | uint64_t nb_packets_per_stream) |
| 1508 | { |
| 1509 | int ret; |
| 1510 | enum lttng_error_code status = LTTNG_OK; |
| 1511 | struct lttcomm_consumer_msg msg; |
| 1512 | |
| 1513 | assert(socket); |
| 1514 | assert(output); |
| 1515 | |
| 1516 | DBG("Consumer snapshot channel key %" PRIu64, key); |
| 1517 | |
| 1518 | memset(&msg, 0, sizeof(msg)); |
| 1519 | msg.cmd_type = LTTNG_CONSUMER_SNAPSHOT_CHANNEL; |
| 1520 | msg.u.snapshot_channel.key = key; |
| 1521 | msg.u.snapshot_channel.nb_packets_per_stream = nb_packets_per_stream; |
| 1522 | msg.u.snapshot_channel.metadata = metadata; |
| 1523 | |
| 1524 | if (output->type == CONSUMER_DST_NET) { |
| 1525 | msg.u.snapshot_channel.relayd_id = |
| 1526 | output->net_seq_index; |
| 1527 | msg.u.snapshot_channel.use_relayd = 1; |
| 1528 | } else { |
| 1529 | msg.u.snapshot_channel.relayd_id = (uint64_t) -1ULL; |
| 1530 | } |
| 1531 | ret = lttng_strncpy(msg.u.snapshot_channel.pathname, |
| 1532 | channel_path, |
| 1533 | sizeof(msg.u.snapshot_channel.pathname)); |
| 1534 | if (ret < 0) { |
| 1535 | ERR("Snapshot path exceeds the maximal allowed length of %zu bytes (%zu bytes required) with path \"%s\"", |
| 1536 | sizeof(msg.u.snapshot_channel.pathname), |
| 1537 | strlen(channel_path), |
| 1538 | channel_path); |
| 1539 | status = LTTNG_ERR_SNAPSHOT_FAIL; |
| 1540 | goto error; |
| 1541 | } |
| 1542 | |
| 1543 | health_code_update(); |
| 1544 | pthread_mutex_lock(socket->lock); |
| 1545 | ret = consumer_send_msg(socket, &msg); |
| 1546 | pthread_mutex_unlock(socket->lock); |
| 1547 | if (ret < 0) { |
| 1548 | switch (-ret) { |
| 1549 | case LTTCOMM_CONSUMERD_CHAN_NOT_FOUND: |
| 1550 | status = LTTNG_ERR_CHAN_NOT_FOUND; |
| 1551 | break; |
| 1552 | default: |
| 1553 | status = LTTNG_ERR_SNAPSHOT_FAIL; |
| 1554 | break; |
| 1555 | } |
| 1556 | goto error; |
| 1557 | } |
| 1558 | |
| 1559 | error: |
| 1560 | health_code_update(); |
| 1561 | return status; |
| 1562 | } |
| 1563 | |
| 1564 | /* |
| 1565 | * Ask the consumer the number of discarded events for a channel. |
| 1566 | */ |
| 1567 | int consumer_get_discarded_events(uint64_t session_id, uint64_t channel_key, |
| 1568 | struct consumer_output *consumer, uint64_t *discarded) |
| 1569 | { |
| 1570 | int ret; |
| 1571 | struct consumer_socket *socket; |
| 1572 | struct lttng_ht_iter iter; |
| 1573 | struct lttcomm_consumer_msg msg; |
| 1574 | |
| 1575 | assert(consumer); |
| 1576 | |
| 1577 | DBG3("Consumer discarded events id %" PRIu64, session_id); |
| 1578 | |
| 1579 | memset(&msg, 0, sizeof(msg)); |
| 1580 | msg.cmd_type = LTTNG_CONSUMER_DISCARDED_EVENTS; |
| 1581 | msg.u.discarded_events.session_id = session_id; |
| 1582 | msg.u.discarded_events.channel_key = channel_key; |
| 1583 | |
| 1584 | *discarded = 0; |
| 1585 | |
| 1586 | /* Send command for each consumer */ |
| 1587 | rcu_read_lock(); |
| 1588 | cds_lfht_for_each_entry(consumer->socks->ht, &iter.iter, socket, |
| 1589 | node.node) { |
| 1590 | uint64_t consumer_discarded = 0; |
| 1591 | pthread_mutex_lock(socket->lock); |
| 1592 | ret = consumer_socket_send(socket, &msg, sizeof(msg)); |
| 1593 | if (ret < 0) { |
| 1594 | pthread_mutex_unlock(socket->lock); |
| 1595 | goto end; |
| 1596 | } |
| 1597 | |
| 1598 | /* |
| 1599 | * No need for a recv reply status because the answer to the |
| 1600 | * command is the reply status message. |
| 1601 | */ |
| 1602 | ret = consumer_socket_recv(socket, &consumer_discarded, |
| 1603 | sizeof(consumer_discarded)); |
| 1604 | if (ret < 0) { |
| 1605 | ERR("get discarded events"); |
| 1606 | pthread_mutex_unlock(socket->lock); |
| 1607 | goto end; |
| 1608 | } |
| 1609 | pthread_mutex_unlock(socket->lock); |
| 1610 | *discarded += consumer_discarded; |
| 1611 | } |
| 1612 | ret = 0; |
| 1613 | DBG("Consumer discarded %" PRIu64 " events in session id %" PRIu64, |
| 1614 | *discarded, session_id); |
| 1615 | |
| 1616 | end: |
| 1617 | rcu_read_unlock(); |
| 1618 | return ret; |
| 1619 | } |
| 1620 | |
| 1621 | /* |
| 1622 | * Ask the consumer the number of lost packets for a channel. |
| 1623 | */ |
| 1624 | int consumer_get_lost_packets(uint64_t session_id, uint64_t channel_key, |
| 1625 | struct consumer_output *consumer, uint64_t *lost) |
| 1626 | { |
| 1627 | int ret; |
| 1628 | struct consumer_socket *socket; |
| 1629 | struct lttng_ht_iter iter; |
| 1630 | struct lttcomm_consumer_msg msg; |
| 1631 | |
| 1632 | assert(consumer); |
| 1633 | |
| 1634 | DBG3("Consumer lost packets id %" PRIu64, session_id); |
| 1635 | |
| 1636 | memset(&msg, 0, sizeof(msg)); |
| 1637 | msg.cmd_type = LTTNG_CONSUMER_LOST_PACKETS; |
| 1638 | msg.u.lost_packets.session_id = session_id; |
| 1639 | msg.u.lost_packets.channel_key = channel_key; |
| 1640 | |
| 1641 | *lost = 0; |
| 1642 | |
| 1643 | /* Send command for each consumer */ |
| 1644 | rcu_read_lock(); |
| 1645 | cds_lfht_for_each_entry(consumer->socks->ht, &iter.iter, socket, |
| 1646 | node.node) { |
| 1647 | uint64_t consumer_lost = 0; |
| 1648 | pthread_mutex_lock(socket->lock); |
| 1649 | ret = consumer_socket_send(socket, &msg, sizeof(msg)); |
| 1650 | if (ret < 0) { |
| 1651 | pthread_mutex_unlock(socket->lock); |
| 1652 | goto end; |
| 1653 | } |
| 1654 | |
| 1655 | /* |
| 1656 | * No need for a recv reply status because the answer to the |
| 1657 | * command is the reply status message. |
| 1658 | */ |
| 1659 | ret = consumer_socket_recv(socket, &consumer_lost, |
| 1660 | sizeof(consumer_lost)); |
| 1661 | if (ret < 0) { |
| 1662 | ERR("get lost packets"); |
| 1663 | pthread_mutex_unlock(socket->lock); |
| 1664 | goto end; |
| 1665 | } |
| 1666 | pthread_mutex_unlock(socket->lock); |
| 1667 | *lost += consumer_lost; |
| 1668 | } |
| 1669 | ret = 0; |
| 1670 | DBG("Consumer lost %" PRIu64 " packets in session id %" PRIu64, |
| 1671 | *lost, session_id); |
| 1672 | |
| 1673 | end: |
| 1674 | rcu_read_unlock(); |
| 1675 | return ret; |
| 1676 | } |
| 1677 | |
| 1678 | /* |
| 1679 | * Ask the consumer to rotate a channel. |
| 1680 | * |
| 1681 | * The new_chunk_id is the session->rotate_count that has been incremented |
| 1682 | * when the rotation started. On the relay, this allows to keep track in which |
| 1683 | * chunk each stream is currently writing to (for the rotate_pending operation). |
| 1684 | */ |
| 1685 | int consumer_rotate_channel(struct consumer_socket *socket, uint64_t key, |
| 1686 | uid_t uid, gid_t gid, struct consumer_output *output, |
| 1687 | bool is_metadata_channel) |
| 1688 | { |
| 1689 | int ret; |
| 1690 | struct lttcomm_consumer_msg msg; |
| 1691 | |
| 1692 | assert(socket); |
| 1693 | |
| 1694 | DBG("Consumer rotate channel key %" PRIu64, key); |
| 1695 | |
| 1696 | pthread_mutex_lock(socket->lock); |
| 1697 | memset(&msg, 0, sizeof(msg)); |
| 1698 | msg.cmd_type = LTTNG_CONSUMER_ROTATE_CHANNEL; |
| 1699 | msg.u.rotate_channel.key = key; |
| 1700 | msg.u.rotate_channel.metadata = !!is_metadata_channel; |
| 1701 | |
| 1702 | if (output->type == CONSUMER_DST_NET) { |
| 1703 | msg.u.rotate_channel.relayd_id = output->net_seq_index; |
| 1704 | } else { |
| 1705 | msg.u.rotate_channel.relayd_id = (uint64_t) -1ULL; |
| 1706 | } |
| 1707 | |
| 1708 | health_code_update(); |
| 1709 | ret = consumer_send_msg(socket, &msg); |
| 1710 | if (ret < 0) { |
| 1711 | switch (-ret) { |
| 1712 | case LTTCOMM_CONSUMERD_CHAN_NOT_FOUND: |
| 1713 | ret = -LTTNG_ERR_CHAN_NOT_FOUND; |
| 1714 | break; |
| 1715 | default: |
| 1716 | ret = -LTTNG_ERR_ROTATION_FAIL_CONSUMER; |
| 1717 | break; |
| 1718 | } |
| 1719 | goto error; |
| 1720 | } |
| 1721 | error: |
| 1722 | pthread_mutex_unlock(socket->lock); |
| 1723 | health_code_update(); |
| 1724 | return ret; |
| 1725 | } |
| 1726 | |
| 1727 | int consumer_init(struct consumer_socket *socket, |
| 1728 | const lttng_uuid sessiond_uuid) |
| 1729 | { |
| 1730 | int ret; |
| 1731 | struct lttcomm_consumer_msg msg = { |
| 1732 | .cmd_type = LTTNG_CONSUMER_INIT, |
| 1733 | }; |
| 1734 | |
| 1735 | assert(socket); |
| 1736 | |
| 1737 | DBG("Sending consumer initialization command"); |
| 1738 | lttng_uuid_copy(msg.u.init.sessiond_uuid, sessiond_uuid); |
| 1739 | |
| 1740 | health_code_update(); |
| 1741 | ret = consumer_send_msg(socket, &msg); |
| 1742 | if (ret < 0) { |
| 1743 | goto error; |
| 1744 | } |
| 1745 | |
| 1746 | error: |
| 1747 | health_code_update(); |
| 1748 | return ret; |
| 1749 | } |
| 1750 | |
| 1751 | /* |
| 1752 | * Ask the consumer to create a new chunk for a given session. |
| 1753 | * |
| 1754 | * Called with the consumer socket lock held. |
| 1755 | */ |
| 1756 | int consumer_create_trace_chunk(struct consumer_socket *socket, |
| 1757 | uint64_t relayd_id, uint64_t session_id, |
| 1758 | struct lttng_trace_chunk *chunk) |
| 1759 | { |
| 1760 | int ret; |
| 1761 | enum lttng_trace_chunk_status chunk_status; |
| 1762 | struct lttng_credentials chunk_credentials; |
| 1763 | const struct lttng_directory_handle *chunk_directory_handle; |
| 1764 | int chunk_dirfd; |
| 1765 | const char *chunk_name; |
| 1766 | bool chunk_name_overridden; |
| 1767 | uint64_t chunk_id; |
| 1768 | time_t creation_timestamp; |
| 1769 | char creation_timestamp_buffer[ISO8601_STR_LEN]; |
| 1770 | const char *creation_timestamp_str = "(none)"; |
| 1771 | const bool chunk_has_local_output = relayd_id == -1ULL; |
| 1772 | struct lttcomm_consumer_msg msg = { |
| 1773 | .cmd_type = LTTNG_CONSUMER_CREATE_TRACE_CHUNK, |
| 1774 | .u.create_trace_chunk.session_id = session_id, |
| 1775 | }; |
| 1776 | |
| 1777 | assert(socket); |
| 1778 | assert(chunk); |
| 1779 | |
| 1780 | if (relayd_id != -1ULL) { |
| 1781 | LTTNG_OPTIONAL_SET(&msg.u.create_trace_chunk.relayd_id, |
| 1782 | relayd_id); |
| 1783 | } |
| 1784 | |
| 1785 | chunk_status = lttng_trace_chunk_get_name(chunk, &chunk_name, |
| 1786 | &chunk_name_overridden); |
| 1787 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK && |
| 1788 | chunk_status != LTTNG_TRACE_CHUNK_STATUS_NONE) { |
| 1789 | ERR("Failed to get name of trace chunk"); |
| 1790 | ret = -LTTNG_ERR_FATAL; |
| 1791 | goto error; |
| 1792 | } |
| 1793 | if (chunk_name_overridden) { |
| 1794 | ret = lttng_strncpy(msg.u.create_trace_chunk.override_name, |
| 1795 | chunk_name, |
| 1796 | sizeof(msg.u.create_trace_chunk.override_name)); |
| 1797 | if (ret) { |
| 1798 | ERR("Trace chunk name \"%s\" exceeds the maximal length allowed by the consumer protocol", |
| 1799 | chunk_name); |
| 1800 | ret = -LTTNG_ERR_FATAL; |
| 1801 | goto error; |
| 1802 | } |
| 1803 | } |
| 1804 | |
| 1805 | chunk_status = lttng_trace_chunk_get_creation_timestamp(chunk, |
| 1806 | &creation_timestamp); |
| 1807 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 1808 | ret = -LTTNG_ERR_FATAL; |
| 1809 | goto error; |
| 1810 | } |
| 1811 | msg.u.create_trace_chunk.creation_timestamp = |
| 1812 | (uint64_t) creation_timestamp; |
| 1813 | /* Only used for logging purposes. */ |
| 1814 | ret = time_to_iso8601_str(creation_timestamp, |
| 1815 | creation_timestamp_buffer, |
| 1816 | sizeof(creation_timestamp_buffer)); |
| 1817 | creation_timestamp_str = !ret ? creation_timestamp_buffer : |
| 1818 | "(formatting error)"; |
| 1819 | |
| 1820 | chunk_status = lttng_trace_chunk_get_id(chunk, &chunk_id); |
| 1821 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 1822 | /* |
| 1823 | * Anonymous trace chunks should never be transmitted |
| 1824 | * to remote peers (consumerd and relayd). They are used |
| 1825 | * internally for backward-compatibility purposes. |
| 1826 | */ |
| 1827 | ret = -LTTNG_ERR_FATAL; |
| 1828 | goto error; |
| 1829 | } |
| 1830 | msg.u.create_trace_chunk.chunk_id = chunk_id; |
| 1831 | |
| 1832 | if (chunk_has_local_output) { |
| 1833 | chunk_status = lttng_trace_chunk_get_chunk_directory_handle( |
| 1834 | chunk, &chunk_directory_handle); |
| 1835 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 1836 | ret = -LTTNG_ERR_FATAL; |
| 1837 | goto error; |
| 1838 | } |
| 1839 | |
| 1840 | /* |
| 1841 | * This will only compile on platforms that support |
| 1842 | * dirfd (POSIX.2008). This is fine as the session daemon |
| 1843 | * is only built for such platforms. |
| 1844 | * |
| 1845 | * The ownership of the chunk directory handle's is maintained |
| 1846 | * by the trace chunk. |
| 1847 | */ |
| 1848 | chunk_dirfd = lttng_directory_handle_get_dirfd( |
| 1849 | chunk_directory_handle); |
| 1850 | assert(chunk_dirfd >= 0); |
| 1851 | |
| 1852 | chunk_status = lttng_trace_chunk_get_credentials( |
| 1853 | chunk, &chunk_credentials); |
| 1854 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 1855 | /* |
| 1856 | * Not associating credentials to a sessiond chunk is a |
| 1857 | * fatal internal error. |
| 1858 | */ |
| 1859 | ret = -LTTNG_ERR_FATAL; |
| 1860 | goto error; |
| 1861 | } |
| 1862 | msg.u.create_trace_chunk.credentials.value.uid = |
| 1863 | chunk_credentials.uid; |
| 1864 | msg.u.create_trace_chunk.credentials.value.gid = |
| 1865 | chunk_credentials.gid; |
| 1866 | msg.u.create_trace_chunk.credentials.is_set = 1; |
| 1867 | } |
| 1868 | |
| 1869 | DBG("Sending consumer create trace chunk command: relayd_id = %" PRId64 |
| 1870 | ", session_id = %" PRIu64 ", chunk_id = %" PRIu64 |
| 1871 | ", creation_timestamp = %s", |
| 1872 | relayd_id, session_id, chunk_id, |
| 1873 | creation_timestamp_str); |
| 1874 | health_code_update(); |
| 1875 | ret = consumer_send_msg(socket, &msg); |
| 1876 | health_code_update(); |
| 1877 | if (ret < 0) { |
| 1878 | ERR("Trace chunk creation error on consumer"); |
| 1879 | ret = -LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER; |
| 1880 | goto error; |
| 1881 | } |
| 1882 | |
| 1883 | if (chunk_has_local_output) { |
| 1884 | DBG("Sending trace chunk directory fd to consumer"); |
| 1885 | health_code_update(); |
| 1886 | ret = consumer_send_fds(socket, &chunk_dirfd, 1); |
| 1887 | health_code_update(); |
| 1888 | if (ret < 0) { |
| 1889 | ERR("Trace chunk creation error on consumer"); |
| 1890 | ret = -LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER; |
| 1891 | goto error; |
| 1892 | } |
| 1893 | } |
| 1894 | error: |
| 1895 | return ret; |
| 1896 | } |
| 1897 | |
| 1898 | /* |
| 1899 | * Ask the consumer to close a trace chunk for a given session. |
| 1900 | * |
| 1901 | * Called with the consumer socket lock held. |
| 1902 | */ |
| 1903 | int consumer_close_trace_chunk(struct consumer_socket *socket, |
| 1904 | uint64_t relayd_id, uint64_t session_id, |
| 1905 | struct lttng_trace_chunk *chunk, |
| 1906 | char *closed_trace_chunk_path) |
| 1907 | { |
| 1908 | int ret; |
| 1909 | enum lttng_trace_chunk_status chunk_status; |
| 1910 | struct lttcomm_consumer_msg msg = { |
| 1911 | .cmd_type = LTTNG_CONSUMER_CLOSE_TRACE_CHUNK, |
| 1912 | .u.close_trace_chunk.session_id = session_id, |
| 1913 | }; |
| 1914 | struct lttcomm_consumer_close_trace_chunk_reply reply; |
| 1915 | uint64_t chunk_id; |
| 1916 | time_t close_timestamp; |
| 1917 | enum lttng_trace_chunk_command_type close_command; |
| 1918 | const char *close_command_name = "none"; |
| 1919 | struct lttng_dynamic_buffer path_reception_buffer; |
| 1920 | |
| 1921 | assert(socket); |
| 1922 | lttng_dynamic_buffer_init(&path_reception_buffer); |
| 1923 | |
| 1924 | if (relayd_id != -1ULL) { |
| 1925 | LTTNG_OPTIONAL_SET( |
| 1926 | &msg.u.close_trace_chunk.relayd_id, relayd_id); |
| 1927 | } |
| 1928 | |
| 1929 | chunk_status = lttng_trace_chunk_get_close_command( |
| 1930 | chunk, &close_command); |
| 1931 | switch (chunk_status) { |
| 1932 | case LTTNG_TRACE_CHUNK_STATUS_OK: |
| 1933 | LTTNG_OPTIONAL_SET(&msg.u.close_trace_chunk.close_command, |
| 1934 | (uint32_t) close_command); |
| 1935 | break; |
| 1936 | case LTTNG_TRACE_CHUNK_STATUS_NONE: |
| 1937 | break; |
| 1938 | default: |
| 1939 | ERR("Failed to get trace chunk close command"); |
| 1940 | ret = -1; |
| 1941 | goto error; |
| 1942 | } |
| 1943 | |
| 1944 | chunk_status = lttng_trace_chunk_get_id(chunk, &chunk_id); |
| 1945 | /* |
| 1946 | * Anonymous trace chunks should never be transmitted to remote peers |
| 1947 | * (consumerd and relayd). They are used internally for |
| 1948 | * backward-compatibility purposes. |
| 1949 | */ |
| 1950 | assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK); |
| 1951 | msg.u.close_trace_chunk.chunk_id = chunk_id; |
| 1952 | |
| 1953 | chunk_status = lttng_trace_chunk_get_close_timestamp(chunk, |
| 1954 | &close_timestamp); |
| 1955 | /* |
| 1956 | * A trace chunk should be closed locally before being closed remotely. |
| 1957 | * Otherwise, the close timestamp would never be transmitted to the |
| 1958 | * peers. |
| 1959 | */ |
| 1960 | assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK); |
| 1961 | msg.u.close_trace_chunk.close_timestamp = (uint64_t) close_timestamp; |
| 1962 | |
| 1963 | if (msg.u.close_trace_chunk.close_command.is_set) { |
| 1964 | close_command_name = lttng_trace_chunk_command_type_get_name( |
| 1965 | close_command); |
| 1966 | } |
| 1967 | DBG("Sending consumer close trace chunk command: relayd_id = %" PRId64 |
| 1968 | ", session_id = %" PRIu64 ", chunk_id = %" PRIu64 |
| 1969 | ", close command = \"%s\"", |
| 1970 | relayd_id, session_id, chunk_id, close_command_name); |
| 1971 | |
| 1972 | health_code_update(); |
| 1973 | ret = consumer_socket_send(socket, &msg, sizeof(struct lttcomm_consumer_msg)); |
| 1974 | if (ret < 0) { |
| 1975 | ret = -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER; |
| 1976 | goto error; |
| 1977 | } |
| 1978 | ret = consumer_socket_recv(socket, &reply, sizeof(reply)); |
| 1979 | if (ret < 0) { |
| 1980 | ret = -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER; |
| 1981 | goto error; |
| 1982 | } |
| 1983 | if (reply.path_length >= LTTNG_PATH_MAX) { |
| 1984 | ERR("Invalid path returned by relay daemon: %" PRIu32 "bytes exceeds maximal allowed length of %d bytes", |
| 1985 | reply.path_length, LTTNG_PATH_MAX); |
| 1986 | ret = -LTTNG_ERR_INVALID_PROTOCOL; |
| 1987 | goto error; |
| 1988 | } |
| 1989 | ret = lttng_dynamic_buffer_set_size(&path_reception_buffer, |
| 1990 | reply.path_length); |
| 1991 | if (ret) { |
| 1992 | ERR("Failed to allocate reception buffer of path returned by the \"close trace chunk\" command"); |
| 1993 | ret = -LTTNG_ERR_NOMEM; |
| 1994 | goto error; |
| 1995 | } |
| 1996 | ret = consumer_socket_recv(socket, path_reception_buffer.data, |
| 1997 | path_reception_buffer.size); |
| 1998 | if (ret < 0) { |
| 1999 | ERR("Communication error while receiving path of closed trace chunk"); |
| 2000 | ret = -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER; |
| 2001 | goto error; |
| 2002 | } |
| 2003 | if (path_reception_buffer.data[path_reception_buffer.size - 1] != '\0') { |
| 2004 | ERR("Invalid path returned by relay daemon: not null-terminated"); |
| 2005 | ret = -LTTNG_ERR_INVALID_PROTOCOL; |
| 2006 | goto error; |
| 2007 | } |
| 2008 | if (closed_trace_chunk_path) { |
| 2009 | /* |
| 2010 | * closed_trace_chunk_path is assumed to have a length >= |
| 2011 | * LTTNG_PATH_MAX |
| 2012 | */ |
| 2013 | memcpy(closed_trace_chunk_path, path_reception_buffer.data, |
| 2014 | path_reception_buffer.size); |
| 2015 | } |
| 2016 | error: |
| 2017 | lttng_dynamic_buffer_reset(&path_reception_buffer); |
| 2018 | health_code_update(); |
| 2019 | return ret; |
| 2020 | } |
| 2021 | |
| 2022 | /* |
| 2023 | * Ask the consumer if a trace chunk exists. |
| 2024 | * |
| 2025 | * Called with the consumer socket lock held. |
| 2026 | * Returns 0 on success, or a negative value on error. |
| 2027 | */ |
| 2028 | int consumer_trace_chunk_exists(struct consumer_socket *socket, |
| 2029 | uint64_t relayd_id, uint64_t session_id, |
| 2030 | struct lttng_trace_chunk *chunk, |
| 2031 | enum consumer_trace_chunk_exists_status *result) |
| 2032 | { |
| 2033 | int ret; |
| 2034 | enum lttng_trace_chunk_status chunk_status; |
| 2035 | struct lttcomm_consumer_msg msg = { |
| 2036 | .cmd_type = LTTNG_CONSUMER_TRACE_CHUNK_EXISTS, |
| 2037 | .u.trace_chunk_exists.session_id = session_id, |
| 2038 | }; |
| 2039 | uint64_t chunk_id; |
| 2040 | const char *consumer_reply_str; |
| 2041 | |
| 2042 | assert(socket); |
| 2043 | |
| 2044 | if (relayd_id != -1ULL) { |
| 2045 | LTTNG_OPTIONAL_SET(&msg.u.trace_chunk_exists.relayd_id, |
| 2046 | relayd_id); |
| 2047 | } |
| 2048 | |
| 2049 | chunk_status = lttng_trace_chunk_get_id(chunk, &chunk_id); |
| 2050 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 2051 | /* |
| 2052 | * Anonymous trace chunks should never be transmitted |
| 2053 | * to remote peers (consumerd and relayd). They are used |
| 2054 | * internally for backward-compatibility purposes. |
| 2055 | */ |
| 2056 | ret = -LTTNG_ERR_FATAL; |
| 2057 | goto error; |
| 2058 | } |
| 2059 | msg.u.trace_chunk_exists.chunk_id = chunk_id; |
| 2060 | |
| 2061 | DBG("Sending consumer trace chunk exists command: relayd_id = %" PRId64 |
| 2062 | ", session_id = %" PRIu64 |
| 2063 | ", chunk_id = %" PRIu64, relayd_id, session_id, chunk_id); |
| 2064 | |
| 2065 | health_code_update(); |
| 2066 | ret = consumer_send_msg(socket, &msg); |
| 2067 | switch (-ret) { |
| 2068 | case LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK: |
| 2069 | consumer_reply_str = "unknown trace chunk"; |
| 2070 | *result = CONSUMER_TRACE_CHUNK_EXISTS_STATUS_UNKNOWN_CHUNK; |
| 2071 | break; |
| 2072 | case LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL: |
| 2073 | consumer_reply_str = "trace chunk exists locally"; |
| 2074 | *result = CONSUMER_TRACE_CHUNK_EXISTS_STATUS_EXISTS_LOCAL; |
| 2075 | break; |
| 2076 | case LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE: |
| 2077 | consumer_reply_str = "trace chunk exists on remote peer"; |
| 2078 | *result = CONSUMER_TRACE_CHUNK_EXISTS_STATUS_EXISTS_REMOTE; |
| 2079 | break; |
| 2080 | default: |
| 2081 | ERR("Consumer returned an error from TRACE_CHUNK_EXISTS command"); |
| 2082 | ret = -1; |
| 2083 | goto error; |
| 2084 | } |
| 2085 | DBG("Consumer reply to TRACE_CHUNK_EXISTS command: %s", |
| 2086 | consumer_reply_str); |
| 2087 | ret = 0; |
| 2088 | error: |
| 2089 | health_code_update(); |
| 2090 | return ret; |
| 2091 | } |