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