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