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