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