| 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 _GNU_SOURCE |
| 19 | #include <assert.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <unistd.h> |
| 26 | #include <inttypes.h> |
| 27 | |
| 28 | #include <common/common.h> |
| 29 | #include <common/defaults.h> |
| 30 | #include <common/uri.h> |
| 31 | #include <common/utils.h> |
| 32 | |
| 33 | #include "consumer.h" |
| 34 | #include "health.h" |
| 35 | #include "ust-app.h" |
| 36 | #include "utils.h" |
| 37 | |
| 38 | /* |
| 39 | * Receive a reply command status message from the consumer. Consumer socket |
| 40 | * lock MUST be acquired before calling this function. |
| 41 | * |
| 42 | * Return 0 on success, -1 on recv error or a negative lttng error code which |
| 43 | * was possibly returned by the consumer. |
| 44 | */ |
| 45 | int consumer_recv_status_reply(struct consumer_socket *sock) |
| 46 | { |
| 47 | int ret; |
| 48 | struct lttcomm_consumer_status_msg reply; |
| 49 | |
| 50 | assert(sock); |
| 51 | |
| 52 | ret = lttcomm_recv_unix_sock(sock->fd, &reply, sizeof(reply)); |
| 53 | if (ret <= 0) { |
| 54 | if (ret == 0) { |
| 55 | /* Orderly shutdown. Don't return 0 which means success. */ |
| 56 | ret = -1; |
| 57 | } |
| 58 | /* The above call will print a PERROR on error. */ |
| 59 | DBG("Fail to receive status reply on sock %d", sock->fd); |
| 60 | goto end; |
| 61 | } |
| 62 | |
| 63 | if (reply.ret_code == LTTNG_OK) { |
| 64 | /* All good. */ |
| 65 | ret = 0; |
| 66 | } else { |
| 67 | ret = -reply.ret_code; |
| 68 | DBG("Consumer ret code %d", ret); |
| 69 | } |
| 70 | |
| 71 | end: |
| 72 | return ret; |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * Once the ASK_CHANNEL command is sent to the consumer, the channel |
| 77 | * information are sent back. This call receives that data and populates key |
| 78 | * and stream_count. |
| 79 | * |
| 80 | * On success return 0 and both key and stream_count are set. On error, a |
| 81 | * negative value is sent back and both parameters are untouched. |
| 82 | */ |
| 83 | int consumer_recv_status_channel(struct consumer_socket *sock, |
| 84 | uint64_t *key, unsigned int *stream_count) |
| 85 | { |
| 86 | int ret; |
| 87 | struct lttcomm_consumer_status_channel reply; |
| 88 | |
| 89 | assert(sock); |
| 90 | assert(stream_count); |
| 91 | assert(key); |
| 92 | |
| 93 | ret = lttcomm_recv_unix_sock(sock->fd, &reply, sizeof(reply)); |
| 94 | if (ret <= 0) { |
| 95 | if (ret == 0) { |
| 96 | /* Orderly shutdown. Don't return 0 which means success. */ |
| 97 | ret = -1; |
| 98 | } |
| 99 | /* The above call will print a PERROR on error. */ |
| 100 | DBG("Fail to receive status reply on sock %d", sock->fd); |
| 101 | goto end; |
| 102 | } |
| 103 | |
| 104 | /* An error is possible so don't touch the key and stream_count. */ |
| 105 | if (reply.ret_code != LTTNG_OK) { |
| 106 | ret = -1; |
| 107 | goto end; |
| 108 | } |
| 109 | |
| 110 | *key = reply.key; |
| 111 | *stream_count = reply.stream_count; |
| 112 | |
| 113 | end: |
| 114 | return ret; |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * Send destroy relayd command to consumer. |
| 119 | * |
| 120 | * On success return positive value. On error, negative value. |
| 121 | */ |
| 122 | int consumer_send_destroy_relayd(struct consumer_socket *sock, |
| 123 | struct consumer_output *consumer) |
| 124 | { |
| 125 | int ret; |
| 126 | struct lttcomm_consumer_msg msg; |
| 127 | |
| 128 | assert(consumer); |
| 129 | assert(sock); |
| 130 | |
| 131 | DBG2("Sending destroy relayd command to consumer sock %d", sock->fd); |
| 132 | |
| 133 | /* Bail out if consumer is disabled */ |
| 134 | if (!consumer->enabled) { |
| 135 | ret = LTTNG_OK; |
| 136 | DBG3("Consumer is disabled"); |
| 137 | goto error; |
| 138 | } |
| 139 | |
| 140 | msg.cmd_type = LTTNG_CONSUMER_DESTROY_RELAYD; |
| 141 | msg.u.destroy_relayd.net_seq_idx = consumer->net_seq_index; |
| 142 | |
| 143 | pthread_mutex_lock(sock->lock); |
| 144 | ret = lttcomm_send_unix_sock(sock->fd, &msg, sizeof(msg)); |
| 145 | if (ret < 0) { |
| 146 | /* Indicate that the consumer is probably closing at this point. */ |
| 147 | DBG("send consumer destroy relayd command"); |
| 148 | goto error_send; |
| 149 | } |
| 150 | |
| 151 | /* Don't check the return value. The caller will do it. */ |
| 152 | ret = consumer_recv_status_reply(sock); |
| 153 | |
| 154 | DBG2("Consumer send destroy relayd command done"); |
| 155 | |
| 156 | error_send: |
| 157 | pthread_mutex_unlock(sock->lock); |
| 158 | error: |
| 159 | return ret; |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * For each consumer socket in the consumer output object, send a destroy |
| 164 | * relayd command. |
| 165 | */ |
| 166 | void consumer_output_send_destroy_relayd(struct consumer_output *consumer) |
| 167 | { |
| 168 | struct lttng_ht_iter iter; |
| 169 | struct consumer_socket *socket; |
| 170 | |
| 171 | assert(consumer); |
| 172 | |
| 173 | /* Destroy any relayd connection */ |
| 174 | if (consumer->type == CONSUMER_DST_NET) { |
| 175 | rcu_read_lock(); |
| 176 | cds_lfht_for_each_entry(consumer->socks->ht, &iter.iter, socket, |
| 177 | node.node) { |
| 178 | int ret; |
| 179 | |
| 180 | /* Send destroy relayd command */ |
| 181 | ret = consumer_send_destroy_relayd(socket, consumer); |
| 182 | if (ret < 0) { |
| 183 | DBG("Unable to send destroy relayd command to consumer"); |
| 184 | /* Continue since we MUST delete everything at this point. */ |
| 185 | } |
| 186 | } |
| 187 | rcu_read_unlock(); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * From a consumer_data structure, allocate and add a consumer socket to the |
| 193 | * consumer output. |
| 194 | * |
| 195 | * Return 0 on success, else negative value on error |
| 196 | */ |
| 197 | int consumer_create_socket(struct consumer_data *data, |
| 198 | struct consumer_output *output) |
| 199 | { |
| 200 | int ret = 0; |
| 201 | struct consumer_socket *socket; |
| 202 | |
| 203 | assert(data); |
| 204 | |
| 205 | if (output == NULL || data->cmd_sock < 0) { |
| 206 | /* |
| 207 | * Not an error. Possible there is simply not spawned consumer or it's |
| 208 | * disabled for the tracing session asking the socket. |
| 209 | */ |
| 210 | goto error; |
| 211 | } |
| 212 | |
| 213 | rcu_read_lock(); |
| 214 | socket = consumer_find_socket(data->cmd_sock, output); |
| 215 | rcu_read_unlock(); |
| 216 | if (socket == NULL) { |
| 217 | socket = consumer_allocate_socket(data->cmd_sock); |
| 218 | if (socket == NULL) { |
| 219 | ret = -1; |
| 220 | goto error; |
| 221 | } |
| 222 | |
| 223 | socket->registered = 0; |
| 224 | socket->lock = &data->lock; |
| 225 | rcu_read_lock(); |
| 226 | consumer_add_socket(socket, output); |
| 227 | rcu_read_unlock(); |
| 228 | } |
| 229 | |
| 230 | socket->type = data->type; |
| 231 | |
| 232 | DBG3("Consumer socket created (fd: %d) and added to output", |
| 233 | data->cmd_sock); |
| 234 | |
| 235 | error: |
| 236 | return ret; |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * Return the consumer socket from the given consumer output with the right |
| 241 | * bitness. On error, returns NULL. |
| 242 | * |
| 243 | * The caller MUST acquire a rcu read side lock and keep it until the socket |
| 244 | * object reference is not needed anymore. |
| 245 | */ |
| 246 | struct consumer_socket *consumer_find_socket_by_bitness(int bits, |
| 247 | struct consumer_output *consumer) |
| 248 | { |
| 249 | int consumer_fd; |
| 250 | struct consumer_socket *socket = NULL; |
| 251 | |
| 252 | switch (bits) { |
| 253 | case 64: |
| 254 | consumer_fd = uatomic_read(&ust_consumerd64_fd); |
| 255 | break; |
| 256 | case 32: |
| 257 | consumer_fd = uatomic_read(&ust_consumerd32_fd); |
| 258 | break; |
| 259 | default: |
| 260 | assert(0); |
| 261 | goto end; |
| 262 | } |
| 263 | |
| 264 | socket = consumer_find_socket(consumer_fd, consumer); |
| 265 | if (!socket) { |
| 266 | ERR("Consumer socket fd %d not found in consumer obj %p", |
| 267 | consumer_fd, consumer); |
| 268 | } |
| 269 | |
| 270 | end: |
| 271 | return socket; |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | * Find a consumer_socket in a consumer_output hashtable. Read side lock must |
| 276 | * be acquired before calling this function and across use of the |
| 277 | * returned consumer_socket. |
| 278 | */ |
| 279 | struct consumer_socket *consumer_find_socket(int key, |
| 280 | struct consumer_output *consumer) |
| 281 | { |
| 282 | struct lttng_ht_iter iter; |
| 283 | struct lttng_ht_node_ulong *node; |
| 284 | struct consumer_socket *socket = NULL; |
| 285 | |
| 286 | /* Negative keys are lookup failures */ |
| 287 | if (key < 0 || consumer == NULL) { |
| 288 | return NULL; |
| 289 | } |
| 290 | |
| 291 | lttng_ht_lookup(consumer->socks, (void *)((unsigned long) key), |
| 292 | &iter); |
| 293 | node = lttng_ht_iter_get_node_ulong(&iter); |
| 294 | if (node != NULL) { |
| 295 | socket = caa_container_of(node, struct consumer_socket, node); |
| 296 | } |
| 297 | |
| 298 | return socket; |
| 299 | } |
| 300 | |
| 301 | /* |
| 302 | * Allocate a new consumer_socket and return the pointer. |
| 303 | */ |
| 304 | struct consumer_socket *consumer_allocate_socket(int fd) |
| 305 | { |
| 306 | struct consumer_socket *socket = NULL; |
| 307 | |
| 308 | socket = zmalloc(sizeof(struct consumer_socket)); |
| 309 | if (socket == NULL) { |
| 310 | PERROR("zmalloc consumer socket"); |
| 311 | goto error; |
| 312 | } |
| 313 | |
| 314 | socket->fd = fd; |
| 315 | lttng_ht_node_init_ulong(&socket->node, fd); |
| 316 | |
| 317 | error: |
| 318 | return socket; |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | * Add consumer socket to consumer output object. Read side lock must be |
| 323 | * acquired before calling this function. |
| 324 | */ |
| 325 | void consumer_add_socket(struct consumer_socket *sock, |
| 326 | struct consumer_output *consumer) |
| 327 | { |
| 328 | assert(sock); |
| 329 | assert(consumer); |
| 330 | |
| 331 | lttng_ht_add_unique_ulong(consumer->socks, &sock->node); |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | * Delte consumer socket to consumer output object. Read side lock must be |
| 336 | * acquired before calling this function. |
| 337 | */ |
| 338 | void consumer_del_socket(struct consumer_socket *sock, |
| 339 | struct consumer_output *consumer) |
| 340 | { |
| 341 | int ret; |
| 342 | struct lttng_ht_iter iter; |
| 343 | |
| 344 | assert(sock); |
| 345 | assert(consumer); |
| 346 | |
| 347 | iter.iter.node = &sock->node.node; |
| 348 | ret = lttng_ht_del(consumer->socks, &iter); |
| 349 | assert(!ret); |
| 350 | } |
| 351 | |
| 352 | /* |
| 353 | * RCU destroy call function. |
| 354 | */ |
| 355 | static void destroy_socket_rcu(struct rcu_head *head) |
| 356 | { |
| 357 | struct lttng_ht_node_ulong *node = |
| 358 | caa_container_of(head, struct lttng_ht_node_ulong, head); |
| 359 | struct consumer_socket *socket = |
| 360 | caa_container_of(node, struct consumer_socket, node); |
| 361 | |
| 362 | free(socket); |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | * Destroy and free socket pointer in a call RCU. Read side lock must be |
| 367 | * acquired before calling this function. |
| 368 | */ |
| 369 | void consumer_destroy_socket(struct consumer_socket *sock) |
| 370 | { |
| 371 | assert(sock); |
| 372 | |
| 373 | /* |
| 374 | * We DO NOT close the file descriptor here since it is global to the |
| 375 | * session daemon and is closed only if the consumer dies or a custom |
| 376 | * consumer was registered, |
| 377 | */ |
| 378 | if (sock->registered) { |
| 379 | DBG3("Consumer socket was registered. Closing fd %d", sock->fd); |
| 380 | lttcomm_close_unix_sock(sock->fd); |
| 381 | } |
| 382 | |
| 383 | call_rcu(&sock->node.head, destroy_socket_rcu); |
| 384 | } |
| 385 | |
| 386 | /* |
| 387 | * Allocate and assign data to a consumer_output object. |
| 388 | * |
| 389 | * Return pointer to structure. |
| 390 | */ |
| 391 | struct consumer_output *consumer_create_output(enum consumer_dst_type type) |
| 392 | { |
| 393 | struct consumer_output *output = NULL; |
| 394 | |
| 395 | output = zmalloc(sizeof(struct consumer_output)); |
| 396 | if (output == NULL) { |
| 397 | PERROR("zmalloc consumer_output"); |
| 398 | goto error; |
| 399 | } |
| 400 | |
| 401 | /* By default, consumer output is enabled */ |
| 402 | output->enabled = 1; |
| 403 | output->type = type; |
| 404 | output->net_seq_index = (uint64_t) -1ULL; |
| 405 | |
| 406 | output->socks = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 407 | |
| 408 | error: |
| 409 | return output; |
| 410 | } |
| 411 | |
| 412 | /* |
| 413 | * Delete the consumer_output object from the list and free the ptr. |
| 414 | * |
| 415 | * Should *NOT* be called with RCU read-side lock held. |
| 416 | */ |
| 417 | void consumer_destroy_output(struct consumer_output *obj) |
| 418 | { |
| 419 | if (obj == NULL) { |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | if (obj->socks) { |
| 424 | struct lttng_ht_iter iter; |
| 425 | struct consumer_socket *socket; |
| 426 | |
| 427 | rcu_read_lock(); |
| 428 | cds_lfht_for_each_entry(obj->socks->ht, &iter.iter, socket, node.node) { |
| 429 | consumer_del_socket(socket, obj); |
| 430 | consumer_destroy_socket(socket); |
| 431 | } |
| 432 | rcu_read_unlock(); |
| 433 | |
| 434 | /* Finally destroy HT */ |
| 435 | ht_cleanup_push(obj->socks); |
| 436 | } |
| 437 | |
| 438 | free(obj); |
| 439 | } |
| 440 | |
| 441 | /* |
| 442 | * Copy consumer output and returned the newly allocated copy. |
| 443 | * |
| 444 | * Should *NOT* be called with RCU read-side lock held. |
| 445 | */ |
| 446 | struct consumer_output *consumer_copy_output(struct consumer_output *obj) |
| 447 | { |
| 448 | int ret; |
| 449 | struct lttng_ht *tmp_ht_ptr; |
| 450 | struct consumer_output *output; |
| 451 | |
| 452 | assert(obj); |
| 453 | |
| 454 | output = consumer_create_output(obj->type); |
| 455 | if (output == NULL) { |
| 456 | goto error; |
| 457 | } |
| 458 | /* Avoid losing the HT reference after the memcpy() */ |
| 459 | tmp_ht_ptr = output->socks; |
| 460 | |
| 461 | memcpy(output, obj, sizeof(struct consumer_output)); |
| 462 | |
| 463 | /* Putting back the HT pointer and start copying socket(s). */ |
| 464 | output->socks = tmp_ht_ptr; |
| 465 | |
| 466 | ret = consumer_copy_sockets(output, obj); |
| 467 | if (ret < 0) { |
| 468 | goto malloc_error; |
| 469 | } |
| 470 | |
| 471 | error: |
| 472 | return output; |
| 473 | |
| 474 | malloc_error: |
| 475 | consumer_destroy_output(output); |
| 476 | return NULL; |
| 477 | } |
| 478 | |
| 479 | /* |
| 480 | * Copy consumer sockets from src to dst. |
| 481 | * |
| 482 | * Return 0 on success or else a negative value. |
| 483 | */ |
| 484 | int consumer_copy_sockets(struct consumer_output *dst, |
| 485 | struct consumer_output *src) |
| 486 | { |
| 487 | int ret = 0; |
| 488 | struct lttng_ht_iter iter; |
| 489 | struct consumer_socket *socket, *copy_sock; |
| 490 | |
| 491 | assert(dst); |
| 492 | assert(src); |
| 493 | |
| 494 | rcu_read_lock(); |
| 495 | cds_lfht_for_each_entry(src->socks->ht, &iter.iter, socket, node.node) { |
| 496 | /* Ignore socket that are already there. */ |
| 497 | copy_sock = consumer_find_socket(socket->fd, dst); |
| 498 | if (copy_sock) { |
| 499 | continue; |
| 500 | } |
| 501 | |
| 502 | /* Create new socket object. */ |
| 503 | copy_sock = consumer_allocate_socket(socket->fd); |
| 504 | if (copy_sock == NULL) { |
| 505 | rcu_read_unlock(); |
| 506 | ret = -ENOMEM; |
| 507 | goto error; |
| 508 | } |
| 509 | |
| 510 | copy_sock->registered = socket->registered; |
| 511 | /* |
| 512 | * This is valid because this lock is shared accross all consumer |
| 513 | * object being the global lock of the consumer data structure of the |
| 514 | * session daemon. |
| 515 | */ |
| 516 | copy_sock->lock = socket->lock; |
| 517 | consumer_add_socket(copy_sock, dst); |
| 518 | } |
| 519 | rcu_read_unlock(); |
| 520 | |
| 521 | error: |
| 522 | return ret; |
| 523 | } |
| 524 | |
| 525 | /* |
| 526 | * Set network URI to the consumer output object. |
| 527 | * |
| 528 | * Return 0 on success. Return 1 if the URI were equal. Else, negative value on |
| 529 | * error. |
| 530 | */ |
| 531 | int consumer_set_network_uri(struct consumer_output *obj, |
| 532 | struct lttng_uri *uri) |
| 533 | { |
| 534 | int ret; |
| 535 | char tmp_path[PATH_MAX]; |
| 536 | char hostname[HOST_NAME_MAX]; |
| 537 | struct lttng_uri *dst_uri = NULL; |
| 538 | |
| 539 | /* Code flow error safety net. */ |
| 540 | assert(obj); |
| 541 | assert(uri); |
| 542 | |
| 543 | switch (uri->stype) { |
| 544 | case LTTNG_STREAM_CONTROL: |
| 545 | dst_uri = &obj->dst.net.control; |
| 546 | obj->dst.net.control_isset = 1; |
| 547 | if (uri->port == 0) { |
| 548 | /* Assign default port. */ |
| 549 | uri->port = DEFAULT_NETWORK_CONTROL_PORT; |
| 550 | } else { |
| 551 | if (obj->dst.net.data_isset && uri->port == |
| 552 | obj->dst.net.data.port) { |
| 553 | ret = -LTTNG_ERR_INVALID; |
| 554 | goto error; |
| 555 | } |
| 556 | } |
| 557 | DBG3("Consumer control URI set with port %d", uri->port); |
| 558 | break; |
| 559 | case LTTNG_STREAM_DATA: |
| 560 | dst_uri = &obj->dst.net.data; |
| 561 | obj->dst.net.data_isset = 1; |
| 562 | if (uri->port == 0) { |
| 563 | /* Assign default port. */ |
| 564 | uri->port = DEFAULT_NETWORK_DATA_PORT; |
| 565 | } else { |
| 566 | if (obj->dst.net.control_isset && uri->port == |
| 567 | obj->dst.net.control.port) { |
| 568 | ret = -LTTNG_ERR_INVALID; |
| 569 | goto error; |
| 570 | } |
| 571 | } |
| 572 | DBG3("Consumer data URI set with port %d", uri->port); |
| 573 | break; |
| 574 | default: |
| 575 | ERR("Set network uri type unknown %d", uri->stype); |
| 576 | ret = -LTTNG_ERR_INVALID; |
| 577 | goto error; |
| 578 | } |
| 579 | |
| 580 | ret = uri_compare(dst_uri, uri); |
| 581 | if (!ret) { |
| 582 | /* Same URI, don't touch it and return success. */ |
| 583 | DBG3("URI network compare are the same"); |
| 584 | goto equal; |
| 585 | } |
| 586 | |
| 587 | /* URIs were not equal, replacing it. */ |
| 588 | memset(dst_uri, 0, sizeof(struct lttng_uri)); |
| 589 | memcpy(dst_uri, uri, sizeof(struct lttng_uri)); |
| 590 | obj->type = CONSUMER_DST_NET; |
| 591 | |
| 592 | /* Handle subdir and add hostname in front. */ |
| 593 | if (dst_uri->stype == LTTNG_STREAM_CONTROL) { |
| 594 | /* Get hostname to append it in the pathname */ |
| 595 | ret = gethostname(hostname, sizeof(hostname)); |
| 596 | if (ret < 0) { |
| 597 | PERROR("gethostname. Fallback on default localhost"); |
| 598 | strncpy(hostname, "localhost", sizeof(hostname)); |
| 599 | } |
| 600 | hostname[sizeof(hostname) - 1] = '\0'; |
| 601 | |
| 602 | /* Setup consumer subdir if none present in the control URI */ |
| 603 | if (strlen(dst_uri->subdir) == 0) { |
| 604 | ret = snprintf(tmp_path, sizeof(tmp_path), "%s/%s", |
| 605 | hostname, obj->subdir); |
| 606 | } else { |
| 607 | ret = snprintf(tmp_path, sizeof(tmp_path), "%s/%s", |
| 608 | hostname, dst_uri->subdir); |
| 609 | } |
| 610 | if (ret < 0) { |
| 611 | PERROR("snprintf set consumer uri subdir"); |
| 612 | ret = -LTTNG_ERR_NOMEM; |
| 613 | goto error; |
| 614 | } |
| 615 | |
| 616 | strncpy(obj->subdir, tmp_path, sizeof(obj->subdir)); |
| 617 | DBG3("Consumer set network uri subdir path %s", tmp_path); |
| 618 | } |
| 619 | |
| 620 | return 0; |
| 621 | equal: |
| 622 | return 1; |
| 623 | error: |
| 624 | return ret; |
| 625 | } |
| 626 | |
| 627 | /* |
| 628 | * Send file descriptor to consumer via sock. |
| 629 | */ |
| 630 | int consumer_send_fds(struct consumer_socket *sock, int *fds, size_t nb_fd) |
| 631 | { |
| 632 | int ret; |
| 633 | |
| 634 | assert(fds); |
| 635 | assert(sock); |
| 636 | assert(nb_fd > 0); |
| 637 | |
| 638 | ret = lttcomm_send_fds_unix_sock(sock->fd, fds, nb_fd); |
| 639 | if (ret < 0) { |
| 640 | /* The above call will print a PERROR on error. */ |
| 641 | DBG("Error when sending consumer fds on sock %d", sock->fd); |
| 642 | goto error; |
| 643 | } |
| 644 | |
| 645 | ret = consumer_recv_status_reply(sock); |
| 646 | |
| 647 | error: |
| 648 | return ret; |
| 649 | } |
| 650 | |
| 651 | /* |
| 652 | * Consumer send communication message structure to consumer. |
| 653 | */ |
| 654 | int consumer_send_msg(struct consumer_socket *sock, |
| 655 | struct lttcomm_consumer_msg *msg) |
| 656 | { |
| 657 | int ret; |
| 658 | |
| 659 | assert(msg); |
| 660 | assert(sock); |
| 661 | assert(sock->fd >= 0); |
| 662 | |
| 663 | ret = lttcomm_send_unix_sock(sock->fd, msg, |
| 664 | sizeof(struct lttcomm_consumer_msg)); |
| 665 | if (ret < 0) { |
| 666 | /* The above call will print a PERROR on error. */ |
| 667 | DBG("Error when sending consumer channel on sock %d", sock->fd); |
| 668 | goto error; |
| 669 | } |
| 670 | |
| 671 | ret = consumer_recv_status_reply(sock); |
| 672 | |
| 673 | error: |
| 674 | return ret; |
| 675 | } |
| 676 | |
| 677 | /* |
| 678 | * Consumer send channel communication message structure to consumer. |
| 679 | */ |
| 680 | int consumer_send_channel(struct consumer_socket *sock, |
| 681 | struct lttcomm_consumer_msg *msg) |
| 682 | { |
| 683 | int ret; |
| 684 | |
| 685 | assert(msg); |
| 686 | assert(sock); |
| 687 | assert(sock->fd >= 0); |
| 688 | |
| 689 | ret = lttcomm_send_unix_sock(sock->fd, msg, |
| 690 | sizeof(struct lttcomm_consumer_msg)); |
| 691 | if (ret < 0) { |
| 692 | /* The above call will print a PERROR on error. */ |
| 693 | DBG("Error when sending consumer channel on sock %d", sock->fd); |
| 694 | goto error; |
| 695 | } |
| 696 | |
| 697 | ret = consumer_recv_status_reply(sock); |
| 698 | |
| 699 | error: |
| 700 | return ret; |
| 701 | } |
| 702 | |
| 703 | /* |
| 704 | * Populate the given consumer msg structure with the ask_channel command |
| 705 | * information. |
| 706 | */ |
| 707 | void consumer_init_ask_channel_comm_msg(struct lttcomm_consumer_msg *msg, |
| 708 | uint64_t subbuf_size, |
| 709 | uint64_t num_subbuf, |
| 710 | int overwrite, |
| 711 | unsigned int switch_timer_interval, |
| 712 | unsigned int read_timer_interval, |
| 713 | int output, |
| 714 | int type, |
| 715 | uint64_t session_id, |
| 716 | const char *pathname, |
| 717 | const char *name, |
| 718 | uid_t uid, |
| 719 | gid_t gid, |
| 720 | uint64_t relayd_id, |
| 721 | uint64_t key, |
| 722 | unsigned char *uuid, |
| 723 | uint32_t chan_id, |
| 724 | uint64_t tracefile_size, |
| 725 | uint64_t tracefile_count, |
| 726 | unsigned int monitor) |
| 727 | { |
| 728 | assert(msg); |
| 729 | |
| 730 | /* Zeroed structure */ |
| 731 | memset(msg, 0, sizeof(struct lttcomm_consumer_msg)); |
| 732 | |
| 733 | msg->cmd_type = LTTNG_CONSUMER_ASK_CHANNEL_CREATION; |
| 734 | msg->u.ask_channel.subbuf_size = subbuf_size; |
| 735 | msg->u.ask_channel.num_subbuf = num_subbuf ; |
| 736 | msg->u.ask_channel.overwrite = overwrite; |
| 737 | msg->u.ask_channel.switch_timer_interval = switch_timer_interval; |
| 738 | msg->u.ask_channel.read_timer_interval = read_timer_interval; |
| 739 | msg->u.ask_channel.output = output; |
| 740 | msg->u.ask_channel.type = type; |
| 741 | msg->u.ask_channel.session_id = session_id; |
| 742 | msg->u.ask_channel.uid = uid; |
| 743 | msg->u.ask_channel.gid = gid; |
| 744 | msg->u.ask_channel.relayd_id = relayd_id; |
| 745 | msg->u.ask_channel.key = key; |
| 746 | msg->u.ask_channel.chan_id = chan_id; |
| 747 | msg->u.ask_channel.tracefile_size = tracefile_size; |
| 748 | msg->u.ask_channel.tracefile_count = tracefile_count; |
| 749 | msg->u.ask_channel.monitor = monitor; |
| 750 | |
| 751 | memcpy(msg->u.ask_channel.uuid, uuid, sizeof(msg->u.ask_channel.uuid)); |
| 752 | |
| 753 | strncpy(msg->u.ask_channel.pathname, pathname, |
| 754 | sizeof(msg->u.ask_channel.pathname)); |
| 755 | msg->u.ask_channel.pathname[sizeof(msg->u.ask_channel.pathname)-1] = '\0'; |
| 756 | |
| 757 | strncpy(msg->u.ask_channel.name, name, sizeof(msg->u.ask_channel.name)); |
| 758 | msg->u.ask_channel.name[sizeof(msg->u.ask_channel.name) - 1] = '\0'; |
| 759 | } |
| 760 | |
| 761 | /* |
| 762 | * Init channel communication message structure. |
| 763 | */ |
| 764 | void consumer_init_channel_comm_msg(struct lttcomm_consumer_msg *msg, |
| 765 | enum lttng_consumer_command cmd, |
| 766 | uint64_t channel_key, |
| 767 | uint64_t session_id, |
| 768 | const char *pathname, |
| 769 | uid_t uid, |
| 770 | gid_t gid, |
| 771 | uint64_t relayd_id, |
| 772 | const char *name, |
| 773 | unsigned int nb_init_streams, |
| 774 | enum lttng_event_output output, |
| 775 | int type, |
| 776 | uint64_t tracefile_size, |
| 777 | uint64_t tracefile_count, |
| 778 | unsigned int monitor) |
| 779 | { |
| 780 | assert(msg); |
| 781 | |
| 782 | /* Zeroed structure */ |
| 783 | memset(msg, 0, sizeof(struct lttcomm_consumer_msg)); |
| 784 | |
| 785 | /* Send channel */ |
| 786 | msg->cmd_type = cmd; |
| 787 | msg->u.channel.channel_key = channel_key; |
| 788 | msg->u.channel.session_id = session_id; |
| 789 | msg->u.channel.uid = uid; |
| 790 | msg->u.channel.gid = gid; |
| 791 | msg->u.channel.relayd_id = relayd_id; |
| 792 | msg->u.channel.nb_init_streams = nb_init_streams; |
| 793 | msg->u.channel.output = output; |
| 794 | msg->u.channel.type = type; |
| 795 | msg->u.channel.tracefile_size = tracefile_size; |
| 796 | msg->u.channel.tracefile_count = tracefile_count; |
| 797 | msg->u.channel.monitor = monitor; |
| 798 | |
| 799 | strncpy(msg->u.channel.pathname, pathname, |
| 800 | sizeof(msg->u.channel.pathname)); |
| 801 | msg->u.channel.pathname[sizeof(msg->u.channel.pathname) - 1] = '\0'; |
| 802 | |
| 803 | strncpy(msg->u.channel.name, name, sizeof(msg->u.channel.name)); |
| 804 | msg->u.channel.name[sizeof(msg->u.channel.name) - 1] = '\0'; |
| 805 | } |
| 806 | |
| 807 | /* |
| 808 | * Init stream communication message structure. |
| 809 | */ |
| 810 | void consumer_init_stream_comm_msg(struct lttcomm_consumer_msg *msg, |
| 811 | enum lttng_consumer_command cmd, |
| 812 | uint64_t channel_key, |
| 813 | uint64_t stream_key, |
| 814 | int cpu) |
| 815 | { |
| 816 | assert(msg); |
| 817 | |
| 818 | memset(msg, 0, sizeof(struct lttcomm_consumer_msg)); |
| 819 | |
| 820 | msg->cmd_type = cmd; |
| 821 | msg->u.stream.channel_key = channel_key; |
| 822 | msg->u.stream.stream_key = stream_key; |
| 823 | msg->u.stream.cpu = cpu; |
| 824 | } |
| 825 | |
| 826 | /* |
| 827 | * Send stream communication structure to the consumer. |
| 828 | */ |
| 829 | int consumer_send_stream(struct consumer_socket *sock, |
| 830 | struct consumer_output *dst, struct lttcomm_consumer_msg *msg, |
| 831 | int *fds, size_t nb_fd) |
| 832 | { |
| 833 | int ret; |
| 834 | |
| 835 | assert(msg); |
| 836 | assert(dst); |
| 837 | assert(sock); |
| 838 | assert(fds); |
| 839 | |
| 840 | /* Send on socket */ |
| 841 | ret = lttcomm_send_unix_sock(sock->fd, msg, |
| 842 | sizeof(struct lttcomm_consumer_msg)); |
| 843 | if (ret < 0) { |
| 844 | /* The above call will print a PERROR on error. */ |
| 845 | DBG("Error when sending consumer stream on sock %d", sock->fd); |
| 846 | goto error; |
| 847 | } |
| 848 | |
| 849 | ret = consumer_recv_status_reply(sock); |
| 850 | if (ret < 0) { |
| 851 | goto error; |
| 852 | } |
| 853 | |
| 854 | ret = consumer_send_fds(sock, fds, nb_fd); |
| 855 | if (ret < 0) { |
| 856 | goto error; |
| 857 | } |
| 858 | |
| 859 | error: |
| 860 | return ret; |
| 861 | } |
| 862 | |
| 863 | /* |
| 864 | * Send relayd socket to consumer associated with a session name. |
| 865 | * |
| 866 | * On success return positive value. On error, negative value. |
| 867 | */ |
| 868 | int consumer_send_relayd_socket(struct consumer_socket *consumer_sock, |
| 869 | struct lttcomm_relayd_sock *rsock, struct consumer_output *consumer, |
| 870 | enum lttng_stream_type type, uint64_t session_id) |
| 871 | { |
| 872 | int ret; |
| 873 | struct lttcomm_consumer_msg msg; |
| 874 | |
| 875 | /* Code flow error. Safety net. */ |
| 876 | assert(rsock); |
| 877 | assert(consumer); |
| 878 | assert(consumer_sock); |
| 879 | |
| 880 | /* Bail out if consumer is disabled */ |
| 881 | if (!consumer->enabled) { |
| 882 | ret = LTTNG_OK; |
| 883 | goto error; |
| 884 | } |
| 885 | |
| 886 | msg.cmd_type = LTTNG_CONSUMER_ADD_RELAYD_SOCKET; |
| 887 | /* |
| 888 | * Assign network consumer output index using the temporary consumer since |
| 889 | * this call should only be made from within a set_consumer_uri() function |
| 890 | * call in the session daemon. |
| 891 | */ |
| 892 | msg.u.relayd_sock.net_index = consumer->net_seq_index; |
| 893 | msg.u.relayd_sock.type = type; |
| 894 | msg.u.relayd_sock.session_id = session_id; |
| 895 | memcpy(&msg.u.relayd_sock.sock, rsock, sizeof(msg.u.relayd_sock.sock)); |
| 896 | |
| 897 | DBG3("Sending relayd sock info to consumer on %d", consumer_sock->fd); |
| 898 | ret = lttcomm_send_unix_sock(consumer_sock->fd, &msg, sizeof(msg)); |
| 899 | if (ret < 0) { |
| 900 | /* The above call will print a PERROR on error. */ |
| 901 | DBG("Error when sending relayd sockets on sock %d", rsock->sock.fd); |
| 902 | goto error; |
| 903 | } |
| 904 | |
| 905 | ret = consumer_recv_status_reply(consumer_sock); |
| 906 | if (ret < 0) { |
| 907 | goto error; |
| 908 | } |
| 909 | |
| 910 | DBG3("Sending relayd socket file descriptor to consumer"); |
| 911 | ret = consumer_send_fds(consumer_sock, &rsock->sock.fd, 1); |
| 912 | if (ret < 0) { |
| 913 | goto error; |
| 914 | } |
| 915 | |
| 916 | DBG2("Consumer relayd socket sent"); |
| 917 | |
| 918 | error: |
| 919 | return ret; |
| 920 | } |
| 921 | |
| 922 | /* |
| 923 | * Set consumer subdirectory using the session name and a generated datetime if |
| 924 | * needed. This is appended to the current subdirectory. |
| 925 | */ |
| 926 | int consumer_set_subdir(struct consumer_output *consumer, |
| 927 | const char *session_name) |
| 928 | { |
| 929 | int ret = 0; |
| 930 | unsigned int have_default_name = 0; |
| 931 | char datetime[16], tmp_path[PATH_MAX]; |
| 932 | time_t rawtime; |
| 933 | struct tm *timeinfo; |
| 934 | |
| 935 | assert(consumer); |
| 936 | assert(session_name); |
| 937 | |
| 938 | memset(tmp_path, 0, sizeof(tmp_path)); |
| 939 | |
| 940 | /* Flag if we have a default session. */ |
| 941 | if (strncmp(session_name, DEFAULT_SESSION_NAME "-", |
| 942 | strlen(DEFAULT_SESSION_NAME) + 1) == 0) { |
| 943 | have_default_name = 1; |
| 944 | } else { |
| 945 | /* Get date and time for session path */ |
| 946 | time(&rawtime); |
| 947 | timeinfo = localtime(&rawtime); |
| 948 | strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo); |
| 949 | } |
| 950 | |
| 951 | if (have_default_name) { |
| 952 | ret = snprintf(tmp_path, sizeof(tmp_path), |
| 953 | "%s/%s", consumer->subdir, session_name); |
| 954 | } else { |
| 955 | ret = snprintf(tmp_path, sizeof(tmp_path), |
| 956 | "%s/%s-%s/", consumer->subdir, session_name, datetime); |
| 957 | } |
| 958 | if (ret < 0) { |
| 959 | PERROR("snprintf session name date"); |
| 960 | goto error; |
| 961 | } |
| 962 | |
| 963 | strncpy(consumer->subdir, tmp_path, sizeof(consumer->subdir)); |
| 964 | DBG2("Consumer subdir set to %s", consumer->subdir); |
| 965 | |
| 966 | error: |
| 967 | return ret; |
| 968 | } |
| 969 | |
| 970 | /* |
| 971 | * Ask the consumer if the data is ready to read (NOT pending) for the specific |
| 972 | * session id. |
| 973 | * |
| 974 | * This function has a different behavior with the consumer i.e. that it waits |
| 975 | * for a reply from the consumer if yes or no the data is pending. |
| 976 | */ |
| 977 | int consumer_is_data_pending(uint64_t session_id, |
| 978 | struct consumer_output *consumer) |
| 979 | { |
| 980 | int ret; |
| 981 | int32_t ret_code = 0; /* Default is that the data is NOT pending */ |
| 982 | struct consumer_socket *socket; |
| 983 | struct lttng_ht_iter iter; |
| 984 | struct lttcomm_consumer_msg msg; |
| 985 | |
| 986 | assert(consumer); |
| 987 | |
| 988 | msg.cmd_type = LTTNG_CONSUMER_DATA_PENDING; |
| 989 | |
| 990 | msg.u.data_pending.session_id = session_id; |
| 991 | |
| 992 | DBG3("Consumer data pending for id %" PRIu64, session_id); |
| 993 | |
| 994 | /* Send command for each consumer */ |
| 995 | rcu_read_lock(); |
| 996 | cds_lfht_for_each_entry(consumer->socks->ht, &iter.iter, socket, |
| 997 | node.node) { |
| 998 | /* Code flow error */ |
| 999 | assert(socket->fd >= 0); |
| 1000 | |
| 1001 | pthread_mutex_lock(socket->lock); |
| 1002 | |
| 1003 | ret = lttcomm_send_unix_sock(socket->fd, &msg, sizeof(msg)); |
| 1004 | if (ret < 0) { |
| 1005 | /* The above call will print a PERROR on error. */ |
| 1006 | DBG("Error on consumer is data pending on sock %d", socket->fd); |
| 1007 | pthread_mutex_unlock(socket->lock); |
| 1008 | goto error_unlock; |
| 1009 | } |
| 1010 | |
| 1011 | /* |
| 1012 | * No need for a recv reply status because the answer to the command is |
| 1013 | * the reply status message. |
| 1014 | */ |
| 1015 | |
| 1016 | ret = lttcomm_recv_unix_sock(socket->fd, &ret_code, sizeof(ret_code)); |
| 1017 | if (ret <= 0) { |
| 1018 | if (ret == 0) { |
| 1019 | /* Orderly shutdown. Don't return 0 which means success. */ |
| 1020 | ret = -1; |
| 1021 | } |
| 1022 | /* The above call will print a PERROR on error. */ |
| 1023 | DBG("Error on recv consumer is data pending on sock %d", socket->fd); |
| 1024 | pthread_mutex_unlock(socket->lock); |
| 1025 | goto error_unlock; |
| 1026 | } |
| 1027 | |
| 1028 | pthread_mutex_unlock(socket->lock); |
| 1029 | |
| 1030 | if (ret_code == 1) { |
| 1031 | break; |
| 1032 | } |
| 1033 | } |
| 1034 | rcu_read_unlock(); |
| 1035 | |
| 1036 | DBG("Consumer data is %s pending for session id %" PRIu64, |
| 1037 | ret_code == 1 ? "" : "NOT", session_id); |
| 1038 | return ret_code; |
| 1039 | |
| 1040 | error_unlock: |
| 1041 | rcu_read_unlock(); |
| 1042 | return -1; |
| 1043 | } |
| 1044 | |
| 1045 | /* |
| 1046 | * Send a flush command to consumer using the given channel key. |
| 1047 | * |
| 1048 | * Return 0 on success else a negative value. |
| 1049 | */ |
| 1050 | int consumer_flush_channel(struct consumer_socket *socket, uint64_t key) |
| 1051 | { |
| 1052 | int ret; |
| 1053 | struct lttcomm_consumer_msg msg; |
| 1054 | |
| 1055 | assert(socket); |
| 1056 | assert(socket->fd >= 0); |
| 1057 | |
| 1058 | DBG2("Consumer flush channel key %" PRIu64, key); |
| 1059 | |
| 1060 | msg.cmd_type = LTTNG_CONSUMER_FLUSH_CHANNEL; |
| 1061 | msg.u.flush_channel.key = key; |
| 1062 | |
| 1063 | pthread_mutex_lock(socket->lock); |
| 1064 | health_code_update(); |
| 1065 | |
| 1066 | ret = consumer_send_msg(socket, &msg); |
| 1067 | if (ret < 0) { |
| 1068 | goto end; |
| 1069 | } |
| 1070 | |
| 1071 | end: |
| 1072 | health_code_update(); |
| 1073 | pthread_mutex_unlock(socket->lock); |
| 1074 | return ret; |
| 1075 | } |
| 1076 | |
| 1077 | /* |
| 1078 | * Send a close metdata command to consumer using the given channel key. |
| 1079 | * |
| 1080 | * Return 0 on success else a negative value. |
| 1081 | */ |
| 1082 | int consumer_close_metadata(struct consumer_socket *socket, |
| 1083 | uint64_t metadata_key) |
| 1084 | { |
| 1085 | int ret; |
| 1086 | struct lttcomm_consumer_msg msg; |
| 1087 | |
| 1088 | assert(socket); |
| 1089 | assert(socket->fd >= 0); |
| 1090 | |
| 1091 | DBG2("Consumer close metadata channel key %" PRIu64, metadata_key); |
| 1092 | |
| 1093 | msg.cmd_type = LTTNG_CONSUMER_CLOSE_METADATA; |
| 1094 | msg.u.close_metadata.key = metadata_key; |
| 1095 | |
| 1096 | pthread_mutex_lock(socket->lock); |
| 1097 | health_code_update(); |
| 1098 | |
| 1099 | ret = consumer_send_msg(socket, &msg); |
| 1100 | if (ret < 0) { |
| 1101 | goto end; |
| 1102 | } |
| 1103 | |
| 1104 | end: |
| 1105 | health_code_update(); |
| 1106 | pthread_mutex_unlock(socket->lock); |
| 1107 | return ret; |
| 1108 | } |
| 1109 | |
| 1110 | /* |
| 1111 | * Send a setup metdata command to consumer using the given channel key. |
| 1112 | * |
| 1113 | * Return 0 on success else a negative value. |
| 1114 | */ |
| 1115 | int consumer_setup_metadata(struct consumer_socket *socket, |
| 1116 | uint64_t metadata_key) |
| 1117 | { |
| 1118 | int ret; |
| 1119 | struct lttcomm_consumer_msg msg; |
| 1120 | |
| 1121 | assert(socket); |
| 1122 | assert(socket->fd >= 0); |
| 1123 | |
| 1124 | DBG2("Consumer setup metadata channel key %" PRIu64, metadata_key); |
| 1125 | |
| 1126 | msg.cmd_type = LTTNG_CONSUMER_SETUP_METADATA; |
| 1127 | msg.u.setup_metadata.key = metadata_key; |
| 1128 | |
| 1129 | pthread_mutex_lock(socket->lock); |
| 1130 | health_code_update(); |
| 1131 | |
| 1132 | ret = consumer_send_msg(socket, &msg); |
| 1133 | if (ret < 0) { |
| 1134 | goto end; |
| 1135 | } |
| 1136 | |
| 1137 | end: |
| 1138 | health_code_update(); |
| 1139 | pthread_mutex_unlock(socket->lock); |
| 1140 | return ret; |
| 1141 | } |
| 1142 | |
| 1143 | /* |
| 1144 | * Send metadata string to consumer. Socket lock MUST be acquired. |
| 1145 | * |
| 1146 | * Return 0 on success else a negative value. |
| 1147 | */ |
| 1148 | int consumer_push_metadata(struct consumer_socket *socket, |
| 1149 | uint64_t metadata_key, char *metadata_str, size_t len, |
| 1150 | size_t target_offset) |
| 1151 | { |
| 1152 | int ret; |
| 1153 | struct lttcomm_consumer_msg msg; |
| 1154 | |
| 1155 | assert(socket); |
| 1156 | assert(socket->fd >= 0); |
| 1157 | |
| 1158 | DBG2("Consumer push metadata to consumer socket %d", socket->fd); |
| 1159 | |
| 1160 | msg.cmd_type = LTTNG_CONSUMER_PUSH_METADATA; |
| 1161 | msg.u.push_metadata.key = metadata_key; |
| 1162 | msg.u.push_metadata.target_offset = target_offset; |
| 1163 | msg.u.push_metadata.len = len; |
| 1164 | |
| 1165 | health_code_update(); |
| 1166 | ret = consumer_send_msg(socket, &msg); |
| 1167 | if (ret < 0 || len == 0) { |
| 1168 | goto end; |
| 1169 | } |
| 1170 | |
| 1171 | DBG3("Consumer pushing metadata on sock %d of len %zu", socket->fd, len); |
| 1172 | |
| 1173 | ret = lttcomm_send_unix_sock(socket->fd, metadata_str, len); |
| 1174 | if (ret < 0) { |
| 1175 | goto end; |
| 1176 | } |
| 1177 | |
| 1178 | health_code_update(); |
| 1179 | ret = consumer_recv_status_reply(socket); |
| 1180 | if (ret < 0) { |
| 1181 | goto end; |
| 1182 | } |
| 1183 | |
| 1184 | end: |
| 1185 | health_code_update(); |
| 1186 | return ret; |
| 1187 | } |
| 1188 | |
| 1189 | /* |
| 1190 | * Ask the consumer to snapshot a specific channel using the key. |
| 1191 | * |
| 1192 | * Return 0 on success or else a negative error. |
| 1193 | */ |
| 1194 | int consumer_snapshot_channel(struct consumer_socket *socket, uint64_t key, |
| 1195 | struct snapshot_output *output, int metadata, uid_t uid, gid_t gid, |
| 1196 | const char *session_path, int wait) |
| 1197 | { |
| 1198 | int ret; |
| 1199 | char datetime[16]; |
| 1200 | struct lttcomm_consumer_msg msg; |
| 1201 | |
| 1202 | assert(socket); |
| 1203 | assert(socket->fd >= 0); |
| 1204 | assert(output); |
| 1205 | assert(output->consumer); |
| 1206 | |
| 1207 | DBG("Consumer snapshot channel key %" PRIu64, key); |
| 1208 | |
| 1209 | ret = utils_get_current_time_str("%Y%m%d-%H%M%S", datetime, |
| 1210 | sizeof(datetime)); |
| 1211 | if (!ret) { |
| 1212 | ret = -EINVAL; |
| 1213 | goto error; |
| 1214 | } |
| 1215 | |
| 1216 | memset(&msg, 0, sizeof(msg)); |
| 1217 | msg.cmd_type = LTTNG_CONSUMER_SNAPSHOT_CHANNEL; |
| 1218 | msg.u.snapshot_channel.key = key; |
| 1219 | msg.u.snapshot_channel.max_size = output->max_size; |
| 1220 | msg.u.snapshot_channel.metadata = metadata; |
| 1221 | |
| 1222 | if (output->consumer->type == CONSUMER_DST_NET) { |
| 1223 | msg.u.snapshot_channel.relayd_id = output->consumer->net_seq_index; |
| 1224 | msg.u.snapshot_channel.use_relayd = 1; |
| 1225 | ret = snprintf(msg.u.snapshot_channel.pathname, |
| 1226 | sizeof(msg.u.snapshot_channel.pathname), "%s/%s-%s%s", |
| 1227 | output->consumer->subdir, output->name, datetime, |
| 1228 | session_path); |
| 1229 | if (ret < 0) { |
| 1230 | ret = -LTTNG_ERR_NOMEM; |
| 1231 | goto error; |
| 1232 | } |
| 1233 | } else { |
| 1234 | ret = snprintf(msg.u.snapshot_channel.pathname, |
| 1235 | sizeof(msg.u.snapshot_channel.pathname), "%s/%s-%s%s", |
| 1236 | output->consumer->dst.trace_path, output->name, datetime, |
| 1237 | session_path); |
| 1238 | if (ret < 0) { |
| 1239 | ret = -LTTNG_ERR_NOMEM; |
| 1240 | goto error; |
| 1241 | } |
| 1242 | msg.u.snapshot_channel.relayd_id = (uint64_t) -1ULL; |
| 1243 | |
| 1244 | /* Create directory. Ignore if exist. */ |
| 1245 | ret = run_as_mkdir_recursive(msg.u.snapshot_channel.pathname, |
| 1246 | S_IRWXU | S_IRWXG, uid, gid); |
| 1247 | if (ret < 0) { |
| 1248 | if (ret != -EEXIST) { |
| 1249 | ERR("Trace directory creation error"); |
| 1250 | goto error; |
| 1251 | } |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | health_code_update(); |
| 1256 | ret = consumer_send_msg(socket, &msg); |
| 1257 | if (ret < 0) { |
| 1258 | goto error; |
| 1259 | } |
| 1260 | |
| 1261 | error: |
| 1262 | health_code_update(); |
| 1263 | return ret; |
| 1264 | } |