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