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