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