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