sessiond: query relayd configuration for availability of clear feature
[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;
eacb7b6f 629 output->relay_allows_clear = src->relay_allows_clear;
b178f53e
JG
630 memcpy(&output->dst, &src->dst, sizeof(output->dst));
631 ret = consumer_copy_sockets(output, src);
6dc3064a 632 if (ret < 0) {
6addfa37 633 goto error_put;
6dc3064a 634 }
6addfa37 635end:
6dc3064a
DG
636 return output;
637
6addfa37
MD
638error_put:
639 consumer_output_put(output);
6dc3064a
DG
640 return NULL;
641}
642
643/*
644 * Copy consumer sockets from src to dst.
645 *
646 * Return 0 on success or else a negative value.
647 */
648int consumer_copy_sockets(struct consumer_output *dst,
649 struct consumer_output *src)
650{
651 int ret = 0;
652 struct lttng_ht_iter iter;
653 struct consumer_socket *socket, *copy_sock;
654
655 assert(dst);
656 assert(src);
657
b82c5c4d 658 rcu_read_lock();
6dc3064a
DG
659 cds_lfht_for_each_entry(src->socks->ht, &iter.iter, socket, node.node) {
660 /* Ignore socket that are already there. */
9363801e 661 copy_sock = consumer_find_socket(*socket->fd_ptr, dst);
6dc3064a
DG
662 if (copy_sock) {
663 continue;
664 }
665
173af62f 666 /* Create new socket object. */
9363801e 667 copy_sock = consumer_allocate_socket(socket->fd_ptr);
173af62f 668 if (copy_sock == NULL) {
b82c5c4d 669 rcu_read_unlock();
6dc3064a
DG
670 ret = -ENOMEM;
671 goto error;
173af62f
DG
672 }
673
09a90bcd 674 copy_sock->registered = socket->registered;
6dc3064a
DG
675 /*
676 * This is valid because this lock is shared accross all consumer
677 * object being the global lock of the consumer data structure of the
678 * session daemon.
679 */
173af62f 680 copy_sock->lock = socket->lock;
6dc3064a 681 consumer_add_socket(copy_sock, dst);
173af62f 682 }
b82c5c4d 683 rcu_read_unlock();
173af62f 684
00e2e675 685error:
6dc3064a 686 return ret;
00e2e675
DG
687}
688
689/*
b178f53e 690 * Set network URI to the consumer output.
00e2e675 691 *
ad20f474
DG
692 * Return 0 on success. Return 1 if the URI were equal. Else, negative value on
693 * error.
00e2e675 694 */
b178f53e
JG
695int consumer_set_network_uri(const struct ltt_session *session,
696 struct consumer_output *output,
00e2e675
DG
697 struct lttng_uri *uri)
698{
699 int ret;
00e2e675
DG
700 struct lttng_uri *dst_uri = NULL;
701
702 /* Code flow error safety net. */
b178f53e 703 assert(output);
00e2e675
DG
704 assert(uri);
705
706 switch (uri->stype) {
707 case LTTNG_STREAM_CONTROL:
b178f53e
JG
708 dst_uri = &output->dst.net.control;
709 output->dst.net.control_isset = 1;
00e2e675
DG
710 if (uri->port == 0) {
711 /* Assign default port. */
712 uri->port = DEFAULT_NETWORK_CONTROL_PORT;
a74934ba 713 } else {
b178f53e
JG
714 if (output->dst.net.data_isset && uri->port ==
715 output->dst.net.data.port) {
a74934ba
DG
716 ret = -LTTNG_ERR_INVALID;
717 goto error;
718 }
00e2e675 719 }
ad20f474 720 DBG3("Consumer control URI set with port %d", uri->port);
00e2e675
DG
721 break;
722 case LTTNG_STREAM_DATA:
b178f53e
JG
723 dst_uri = &output->dst.net.data;
724 output->dst.net.data_isset = 1;
00e2e675
DG
725 if (uri->port == 0) {
726 /* Assign default port. */
727 uri->port = DEFAULT_NETWORK_DATA_PORT;
a74934ba 728 } else {
b178f53e
JG
729 if (output->dst.net.control_isset && uri->port ==
730 output->dst.net.control.port) {
a74934ba
DG
731 ret = -LTTNG_ERR_INVALID;
732 goto error;
733 }
00e2e675 734 }
ad20f474 735 DBG3("Consumer data URI set with port %d", uri->port);
00e2e675
DG
736 break;
737 default:
738 ERR("Set network uri type unknown %d", uri->stype);
a74934ba 739 ret = -LTTNG_ERR_INVALID;
00e2e675
DG
740 goto error;
741 }
742
743 ret = uri_compare(dst_uri, uri);
744 if (!ret) {
745 /* Same URI, don't touch it and return success. */
746 DBG3("URI network compare are the same");
ad20f474 747 goto equal;
00e2e675
DG
748 }
749
750 /* URIs were not equal, replacing it. */
00e2e675 751 memcpy(dst_uri, uri, sizeof(struct lttng_uri));
b178f53e
JG
752 output->type = CONSUMER_DST_NET;
753 if (dst_uri->stype != LTTNG_STREAM_CONTROL) {
754 /* Only the control uri needs to contain the path. */
755 goto end;
756 }
00e2e675 757
b178f53e
JG
758 /*
759 * If the user has specified a subdir as part of the control
760 * URL, the session's base output directory is:
761 * /RELAYD_OUTPUT_PATH/HOSTNAME/USER_SPECIFIED_DIR
762 *
763 * Hence, the "base_dir" from which all stream files and
764 * session rotation chunks are created takes the form
765 * /HOSTNAME/USER_SPECIFIED_DIR
766 *
767 * If the user has not specified an output directory as part of
768 * the control URL, the base output directory has the form:
769 * /RELAYD_OUTPUT_PATH/HOSTNAME/SESSION_NAME-CREATION_TIME
770 *
771 * Hence, the "base_dir" from which all stream files and
772 * session rotation chunks are created takes the form
773 * /HOSTNAME/SESSION_NAME-CREATION_TIME
774 *
775 * Note that automatically generated session names already
776 * contain the session's creation time. In that case, the
777 * creation time is omitted to prevent it from being duplicated
778 * in the final directory hierarchy.
779 */
780 if (*uri->subdir) {
781 if (strstr(uri->subdir, "../")) {
782 ERR("Network URI subdirs are not allowed to walk up the path hierarchy");
783 ret = -LTTNG_ERR_INVALID;
00e2e675
DG
784 goto error;
785 }
b178f53e
JG
786 ret = snprintf(output->dst.net.base_dir,
787 sizeof(output->dst.net.base_dir),
788 "/%s/%s/", session->hostname, uri->subdir);
789 } else {
790 if (session->has_auto_generated_name) {
791 ret = snprintf(output->dst.net.base_dir,
792 sizeof(output->dst.net.base_dir),
793 "/%s/%s/", session->hostname,
794 session->name);
795 } else {
796 char session_creation_datetime[16];
797 size_t strftime_ret;
798 struct tm *timeinfo;
00e2e675 799
b178f53e
JG
800 timeinfo = localtime(&session->creation_time);
801 if (!timeinfo) {
802 ret = -LTTNG_ERR_FATAL;
803 goto error;
804 }
805 strftime_ret = strftime(session_creation_datetime,
806 sizeof(session_creation_datetime),
807 "%Y%m%d-%H%M%S", timeinfo);
808 if (strftime_ret == 0) {
809 ERR("Failed to format session creation timestamp while setting network URI");
810 ret = -LTTNG_ERR_FATAL;
811 goto error;
812 }
813 ret = snprintf(output->dst.net.base_dir,
814 sizeof(output->dst.net.base_dir),
815 "/%s/%s-%s/", session->hostname,
816 session->name,
817 session_creation_datetime);
bfc6eff0 818 }
00e2e675 819 }
b178f53e
JG
820 if (ret >= sizeof(output->dst.net.base_dir)) {
821 ret = -LTTNG_ERR_INVALID;
822 ERR("Truncation occurred while setting network output base directory");
823 goto error;
824 } else if (ret == -1) {
825 ret = -LTTNG_ERR_INVALID;
826 PERROR("Error occurred while setting network output base directory");
827 goto error;
828 }
829
830 DBG3("Consumer set network uri base_dir path %s",
831 output->dst.net.base_dir);
00e2e675 832
b178f53e 833end:
00e2e675 834 return 0;
ad20f474
DG
835equal:
836 return 1;
00e2e675 837error:
a74934ba 838 return ret;
00e2e675
DG
839}
840
841/*
842 * Send file descriptor to consumer via sock.
9a318688
JG
843 *
844 * The consumer socket lock must be held by the caller.
00e2e675 845 */
ac2f30af
JG
846int consumer_send_fds(struct consumer_socket *sock, const int *fds,
847 size_t nb_fd)
00e2e675
DG
848{
849 int ret;
850
851 assert(fds);
f50f23d9 852 assert(sock);
00e2e675 853 assert(nb_fd > 0);
05dac598 854 assert(pthread_mutex_trylock(sock->lock) == EBUSY);
00e2e675 855
9363801e 856 ret = lttcomm_send_fds_unix_sock(*sock->fd_ptr, fds, nb_fd);
00e2e675 857 if (ret < 0) {
3448e266 858 /* The above call will print a PERROR on error. */
9363801e 859 DBG("Error when sending consumer fds on sock %d", *sock->fd_ptr);
00e2e675
DG
860 goto error;
861 }
862
f50f23d9 863 ret = consumer_recv_status_reply(sock);
00e2e675
DG
864error:
865 return ret;
866}
867
ffe60014
DG
868/*
869 * Consumer send communication message structure to consumer.
9a318688
JG
870 *
871 * The consumer socket lock must be held by the caller.
ffe60014
DG
872 */
873int consumer_send_msg(struct consumer_socket *sock,
874 struct lttcomm_consumer_msg *msg)
875{
876 int ret;
877
878 assert(msg);
879 assert(sock);
211b734b 880 assert(pthread_mutex_trylock(sock->lock) == EBUSY);
ffe60014 881
52898cb1 882 ret = consumer_socket_send(sock, msg, sizeof(struct lttcomm_consumer_msg));
ffe60014 883 if (ret < 0) {
ffe60014
DG
884 goto error;
885 }
886
887 ret = consumer_recv_status_reply(sock);
888
889error:
890 return ret;
891}
892
00e2e675
DG
893/*
894 * Consumer send channel communication message structure to consumer.
9a318688
JG
895 *
896 * The consumer socket lock must be held by the caller.
00e2e675 897 */
f50f23d9
DG
898int consumer_send_channel(struct consumer_socket *sock,
899 struct lttcomm_consumer_msg *msg)
00e2e675
DG
900{
901 int ret;
902
903 assert(msg);
f50f23d9 904 assert(sock);
00e2e675 905
52898cb1 906 ret = consumer_send_msg(sock, msg);
00e2e675 907 if (ret < 0) {
00e2e675
DG
908 goto error;
909 }
910
911error:
912 return ret;
913}
914
ffe60014
DG
915/*
916 * Populate the given consumer msg structure with the ask_channel command
917 * information.
918 */
919void consumer_init_ask_channel_comm_msg(struct lttcomm_consumer_msg *msg,
920 uint64_t subbuf_size,
921 uint64_t num_subbuf,
922 int overwrite,
923 unsigned int switch_timer_interval,
924 unsigned int read_timer_interval,
ecc48a90 925 unsigned int live_timer_interval,
e9404c27 926 unsigned int monitor_timer_interval,
ffe60014
DG
927 int output,
928 int type,
929 uint64_t session_id,
930 const char *pathname,
931 const char *name,
d88aee68
DG
932 uint64_t relayd_id,
933 uint64_t key,
7972aab2 934 unsigned char *uuid,
1624d5b7
JD
935 uint32_t chan_id,
936 uint64_t tracefile_size,
2bba9e53 937 uint64_t tracefile_count,
1950109e 938 uint64_t session_id_per_pid,
567eb353 939 unsigned int monitor,
d7ba1388 940 uint32_t ust_app_uid,
491d1539 941 int64_t blocking_timeout,
3d071855 942 const char *root_shm_path,
e098433c 943 const char *shm_path,
1b0bebcb
JG
944 struct lttng_trace_chunk *trace_chunk,
945 const struct lttng_credentials *buffer_credentials)
ffe60014
DG
946{
947 assert(msg);
948
d2956687 949 /* Zeroed structure */
ffe60014 950 memset(msg, 0, sizeof(struct lttcomm_consumer_msg));
d2956687
JG
951 msg->u.ask_channel.buffer_credentials.uid = UINT32_MAX;
952 msg->u.ask_channel.buffer_credentials.gid = UINT32_MAX;
953
d2956687
JG
954 if (trace_chunk) {
955 uint64_t chunk_id;
956 enum lttng_trace_chunk_status chunk_status;
d2956687
JG
957
958 chunk_status = lttng_trace_chunk_get_id(trace_chunk, &chunk_id);
959 assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
960 LTTNG_OPTIONAL_SET(&msg->u.ask_channel.chunk_id, chunk_id);
d2956687 961 }
1b0bebcb
JG
962 msg->u.ask_channel.buffer_credentials.uid = buffer_credentials->uid;
963 msg->u.ask_channel.buffer_credentials.gid = buffer_credentials->gid;
ffe60014
DG
964
965 msg->cmd_type = LTTNG_CONSUMER_ASK_CHANNEL_CREATION;
966 msg->u.ask_channel.subbuf_size = subbuf_size;
967 msg->u.ask_channel.num_subbuf = num_subbuf ;
968 msg->u.ask_channel.overwrite = overwrite;
969 msg->u.ask_channel.switch_timer_interval = switch_timer_interval;
970 msg->u.ask_channel.read_timer_interval = read_timer_interval;
ecc48a90 971 msg->u.ask_channel.live_timer_interval = live_timer_interval;
e9404c27 972 msg->u.ask_channel.monitor_timer_interval = monitor_timer_interval;
ffe60014
DG
973 msg->u.ask_channel.output = output;
974 msg->u.ask_channel.type = type;
975 msg->u.ask_channel.session_id = session_id;
1950109e 976 msg->u.ask_channel.session_id_per_pid = session_id_per_pid;
ffe60014
DG
977 msg->u.ask_channel.relayd_id = relayd_id;
978 msg->u.ask_channel.key = key;
7972aab2 979 msg->u.ask_channel.chan_id = chan_id;
1624d5b7
JD
980 msg->u.ask_channel.tracefile_size = tracefile_size;
981 msg->u.ask_channel.tracefile_count = tracefile_count;
2bba9e53 982 msg->u.ask_channel.monitor = monitor;
567eb353 983 msg->u.ask_channel.ust_app_uid = ust_app_uid;
491d1539 984 msg->u.ask_channel.blocking_timeout = blocking_timeout;
ffe60014
DG
985
986 memcpy(msg->u.ask_channel.uuid, uuid, sizeof(msg->u.ask_channel.uuid));
987
10a50311
JD
988 if (pathname) {
989 strncpy(msg->u.ask_channel.pathname, pathname,
990 sizeof(msg->u.ask_channel.pathname));
991 msg->u.ask_channel.pathname[sizeof(msg->u.ask_channel.pathname)-1] = '\0';
992 }
ffe60014
DG
993
994 strncpy(msg->u.ask_channel.name, name, sizeof(msg->u.ask_channel.name));
995 msg->u.ask_channel.name[sizeof(msg->u.ask_channel.name) - 1] = '\0';
d7ba1388 996
3d071855
MD
997 if (root_shm_path) {
998 strncpy(msg->u.ask_channel.root_shm_path, root_shm_path,
999 sizeof(msg->u.ask_channel.root_shm_path));
1000 msg->u.ask_channel.root_shm_path[sizeof(msg->u.ask_channel.root_shm_path) - 1] = '\0';
1001 }
d7ba1388
MD
1002 if (shm_path) {
1003 strncpy(msg->u.ask_channel.shm_path, shm_path,
1004 sizeof(msg->u.ask_channel.shm_path));
1005 msg->u.ask_channel.shm_path[sizeof(msg->u.ask_channel.shm_path) - 1] = '\0';
1006 }
ffe60014
DG
1007}
1008
00e2e675
DG
1009/*
1010 * Init channel communication message structure.
1011 */
638e7b4e 1012void consumer_init_add_channel_comm_msg(struct lttcomm_consumer_msg *msg,
d88aee68 1013 uint64_t channel_key,
ffe60014
DG
1014 uint64_t session_id,
1015 const char *pathname,
1016 uid_t uid,
1017 gid_t gid,
d88aee68 1018 uint64_t relayd_id,
c30aaa51 1019 const char *name,
ffe60014
DG
1020 unsigned int nb_init_streams,
1021 enum lttng_event_output output,
1624d5b7
JD
1022 int type,
1023 uint64_t tracefile_size,
2bba9e53 1024 uint64_t tracefile_count,
ecc48a90 1025 unsigned int monitor,
e9404c27 1026 unsigned int live_timer_interval,
d2956687
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
d2956687
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;
e9404c27 1056 msg->u.channel.monitor_timer_interval = monitor_timer_interval;
ffe60014
DG
1057
1058 strncpy(msg->u.channel.pathname, pathname,
1059 sizeof(msg->u.channel.pathname));
1060 msg->u.channel.pathname[sizeof(msg->u.channel.pathname) - 1] = '\0';
1061
1062 strncpy(msg->u.channel.name, name, sizeof(msg->u.channel.name));
1063 msg->u.channel.name[sizeof(msg->u.channel.name) - 1] = '\0';
00e2e675
DG
1064}
1065
1066/*
1067 * Init stream communication message structure.
1068 */
e098433c 1069void consumer_init_add_stream_comm_msg(struct lttcomm_consumer_msg *msg,
d88aee68
DG
1070 uint64_t channel_key,
1071 uint64_t stream_key,
d2956687 1072 int32_t cpu)
00e2e675
DG
1073{
1074 assert(msg);
1075
1076 memset(msg, 0, sizeof(struct lttcomm_consumer_msg));
1077
e098433c 1078 msg->cmd_type = LTTNG_CONSUMER_ADD_STREAM;
00e2e675
DG
1079 msg->u.stream.channel_key = channel_key;
1080 msg->u.stream.stream_key = stream_key;
ffe60014 1081 msg->u.stream.cpu = cpu;
00e2e675
DG
1082}
1083
a4baae1b
JD
1084void consumer_init_streams_sent_comm_msg(struct lttcomm_consumer_msg *msg,
1085 enum lttng_consumer_command cmd,
1086 uint64_t channel_key, uint64_t net_seq_idx)
1087{
1088 assert(msg);
1089
1090 memset(msg, 0, sizeof(struct lttcomm_consumer_msg));
1091
1092 msg->cmd_type = cmd;
1093 msg->u.sent_streams.channel_key = channel_key;
1094 msg->u.sent_streams.net_seq_idx = net_seq_idx;
1095}
1096
00e2e675
DG
1097/*
1098 * Send stream communication structure to the consumer.
1099 */
f50f23d9
DG
1100int consumer_send_stream(struct consumer_socket *sock,
1101 struct consumer_output *dst, struct lttcomm_consumer_msg *msg,
ac2f30af 1102 const int *fds, size_t nb_fd)
00e2e675
DG
1103{
1104 int ret;
1105
1106 assert(msg);
1107 assert(dst);
f50f23d9 1108 assert(sock);
ffe60014 1109 assert(fds);
00e2e675 1110
52898cb1 1111 ret = consumer_send_msg(sock, msg);
f50f23d9
DG
1112 if (ret < 0) {
1113 goto error;
1114 }
1115
00e2e675
DG
1116 ret = consumer_send_fds(sock, fds, nb_fd);
1117 if (ret < 0) {
1118 goto error;
1119 }
1120
1121error:
1122 return ret;
1123}
37278a1e
DG
1124
1125/*
1126 * Send relayd socket to consumer associated with a session name.
1127 *
43fade62
JG
1128 * The consumer socket lock must be held by the caller.
1129 *
37278a1e
DG
1130 * On success return positive value. On error, negative value.
1131 */
f50f23d9 1132int consumer_send_relayd_socket(struct consumer_socket *consumer_sock,
6151a90f 1133 struct lttcomm_relayd_sock *rsock, struct consumer_output *consumer,
d3e2ba59 1134 enum lttng_stream_type type, uint64_t session_id,
fb9a95c4 1135 const char *session_name, const char *hostname,
6fa5fe7c 1136 const char *base_path, int session_live_timer,
46ef2188
MD
1137 const uint64_t *current_chunk_id, time_t session_creation_time,
1138 bool session_name_contains_creation_time)
37278a1e
DG
1139{
1140 int ret;
1141 struct lttcomm_consumer_msg msg;
1142
1143 /* Code flow error. Safety net. */
6151a90f 1144 assert(rsock);
37278a1e 1145 assert(consumer);
f50f23d9 1146 assert(consumer_sock);
37278a1e 1147
53efb85a 1148 memset(&msg, 0, sizeof(msg));
37278a1e
DG
1149 /* Bail out if consumer is disabled */
1150 if (!consumer->enabled) {
f73fabfd 1151 ret = LTTNG_OK;
37278a1e
DG
1152 goto error;
1153 }
1154
d3e2ba59 1155 if (type == LTTNG_STREAM_CONTROL) {
ecd1a12f 1156 char output_path[LTTNG_PATH_MAX] = {};
07aa2e42 1157 uint64_t relayd_session_id;
ecd1a12f 1158
d3e2ba59 1159 ret = relayd_create_session(rsock,
07aa2e42 1160 &relayd_session_id,
6fa5fe7c
MD
1161 session_name, hostname, base_path,
1162 session_live_timer,
658f12fa 1163 consumer->snapshot, session_id,
db1da059 1164 sessiond_uuid, current_chunk_id,
46ef2188 1165 session_creation_time,
ecd1a12f
MD
1166 session_name_contains_creation_time,
1167 output_path);
d3e2ba59
JD
1168 if (ret < 0) {
1169 /* Close the control socket. */
1170 (void) relayd_close(rsock);
1171 goto error;
1172 }
07aa2e42 1173 msg.u.relayd_sock.relayd_session_id = relayd_session_id;
ecd1a12f
MD
1174 DBG("Created session on relay, output path reply: %s",
1175 output_path);
d3e2ba59
JD
1176 }
1177
37278a1e
DG
1178 msg.cmd_type = LTTNG_CONSUMER_ADD_RELAYD_SOCKET;
1179 /*
1180 * Assign network consumer output index using the temporary consumer since
1181 * this call should only be made from within a set_consumer_uri() function
1182 * call in the session daemon.
1183 */
1184 msg.u.relayd_sock.net_index = consumer->net_seq_index;
1185 msg.u.relayd_sock.type = type;
46e6455f 1186 msg.u.relayd_sock.session_id = session_id;
6151a90f 1187 memcpy(&msg.u.relayd_sock.sock, rsock, sizeof(msg.u.relayd_sock.sock));
37278a1e 1188
9363801e 1189 DBG3("Sending relayd sock info to consumer on %d", *consumer_sock->fd_ptr);
52898cb1 1190 ret = consumer_send_msg(consumer_sock, &msg);
f50f23d9
DG
1191 if (ret < 0) {
1192 goto error;
1193 }
1194
37278a1e 1195 DBG3("Sending relayd socket file descriptor to consumer");
f712d514 1196 ret = consumer_send_fds(consumer_sock, ALIGNED_CONST_PTR(rsock->sock.fd), 1);
37278a1e
DG
1197 if (ret < 0) {
1198 goto error;
1199 }
1200
1201 DBG2("Consumer relayd socket sent");
1202
1203error:
1204 return ret;
1205}
173af62f 1206
62c43103
JD
1207static
1208int consumer_send_pipe(struct consumer_socket *consumer_sock,
1209 enum lttng_consumer_command cmd, int pipe)
e9404c27
JG
1210{
1211 int ret;
1212 struct lttcomm_consumer_msg msg;
62c43103
JD
1213 const char *pipe_name;
1214 const char *command_name;
1215
1216 switch (cmd) {
1217 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE:
1218 pipe_name = "channel monitor";
1219 command_name = "SET_CHANNEL_MONITOR_PIPE";
1220 break;
62c43103
JD
1221 default:
1222 ERR("Unexpected command received in %s (cmd = %d)", __func__,
1223 (int) cmd);
1224 abort();
1225 }
e9404c27
JG
1226
1227 /* Code flow error. Safety net. */
1228
1229 memset(&msg, 0, sizeof(msg));
62c43103 1230 msg.cmd_type = cmd;
e9404c27 1231
3e4dc117 1232 pthread_mutex_lock(consumer_sock->lock);
62c43103 1233 DBG3("Sending %s command to consumer", command_name);
e9404c27
JG
1234 ret = consumer_send_msg(consumer_sock, &msg);
1235 if (ret < 0) {
1236 goto error;
1237 }
1238
62c43103
JD
1239 DBG3("Sending %s pipe %d to consumer on socket %d",
1240 pipe_name,
e9404c27
JG
1241 pipe, *consumer_sock->fd_ptr);
1242 ret = consumer_send_fds(consumer_sock, &pipe, 1);
1243 if (ret < 0) {
1244 goto error;
1245 }
1246
62c43103 1247 DBG2("%s pipe successfully sent", pipe_name);
e9404c27 1248error:
3e4dc117 1249 pthread_mutex_unlock(consumer_sock->lock);
e9404c27
JG
1250 return ret;
1251}
1252
62c43103
JD
1253int consumer_send_channel_monitor_pipe(struct consumer_socket *consumer_sock,
1254 int pipe)
1255{
1256 return consumer_send_pipe(consumer_sock,
1257 LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE, pipe);
1258}
1259
806e2684 1260/*
5e280d77
MD
1261 * Ask the consumer if the data is pending for the specific session id.
1262 * Returns 1 if data is pending, 0 otherwise, or < 0 on error.
806e2684 1263 */
d88aee68 1264int consumer_is_data_pending(uint64_t session_id,
806e2684
DG
1265 struct consumer_output *consumer)
1266{
1267 int ret;
6d805429 1268 int32_t ret_code = 0; /* Default is that the data is NOT pending */
806e2684
DG
1269 struct consumer_socket *socket;
1270 struct lttng_ht_iter iter;
1271 struct lttcomm_consumer_msg msg;
1272
1273 assert(consumer);
1274
53efb85a 1275 DBG3("Consumer data pending for id %" PRIu64, session_id);
806e2684 1276
53efb85a
MD
1277 memset(&msg, 0, sizeof(msg));
1278 msg.cmd_type = LTTNG_CONSUMER_DATA_PENDING;
d88aee68 1279 msg.u.data_pending.session_id = session_id;
806e2684 1280
c8f59ee5 1281 /* Send command for each consumer */
b82c5c4d 1282 rcu_read_lock();
806e2684
DG
1283 cds_lfht_for_each_entry(consumer->socks->ht, &iter.iter, socket,
1284 node.node) {
806e2684 1285 pthread_mutex_lock(socket->lock);
52898cb1 1286 ret = consumer_socket_send(socket, &msg, sizeof(msg));
806e2684 1287 if (ret < 0) {
806e2684 1288 pthread_mutex_unlock(socket->lock);
b82c5c4d 1289 goto error_unlock;
806e2684
DG
1290 }
1291
f50f23d9
DG
1292 /*
1293 * No need for a recv reply status because the answer to the command is
1294 * the reply status message.
1295 */
1296
52898cb1
DG
1297 ret = consumer_socket_recv(socket, &ret_code, sizeof(ret_code));
1298 if (ret < 0) {
806e2684 1299 pthread_mutex_unlock(socket->lock);
b82c5c4d 1300 goto error_unlock;
806e2684 1301 }
806e2684
DG
1302 pthread_mutex_unlock(socket->lock);
1303
6d805429 1304 if (ret_code == 1) {
806e2684
DG
1305 break;
1306 }
1307 }
b82c5c4d 1308 rcu_read_unlock();
806e2684 1309
d88aee68
DG
1310 DBG("Consumer data is %s pending for session id %" PRIu64,
1311 ret_code == 1 ? "" : "NOT", session_id);
806e2684
DG
1312 return ret_code;
1313
b82c5c4d
DG
1314error_unlock:
1315 rcu_read_unlock();
806e2684
DG
1316 return -1;
1317}
7972aab2
DG
1318
1319/*
1320 * Send a flush command to consumer using the given channel key.
1321 *
1322 * Return 0 on success else a negative value.
1323 */
1324int consumer_flush_channel(struct consumer_socket *socket, uint64_t key)
1325{
1326 int ret;
1327 struct lttcomm_consumer_msg msg;
1328
1329 assert(socket);
7972aab2
DG
1330
1331 DBG2("Consumer flush channel key %" PRIu64, key);
1332
53efb85a 1333 memset(&msg, 0, sizeof(msg));
7972aab2
DG
1334 msg.cmd_type = LTTNG_CONSUMER_FLUSH_CHANNEL;
1335 msg.u.flush_channel.key = key;
1336
1337 pthread_mutex_lock(socket->lock);
1338 health_code_update();
1339
1340 ret = consumer_send_msg(socket, &msg);
1341 if (ret < 0) {
1342 goto end;
1343 }
1344
1345end:
1346 health_code_update();
1347 pthread_mutex_unlock(socket->lock);
1348 return ret;
1349}
1350
0dd01979
MD
1351/*
1352 * Send a clear quiescent command to consumer using the given channel key.
1353 *
1354 * Return 0 on success else a negative value.
1355 */
1356int consumer_clear_quiescent_channel(struct consumer_socket *socket, uint64_t key)
1357{
1358 int ret;
1359 struct lttcomm_consumer_msg msg;
1360
1361 assert(socket);
1362
1363 DBG2("Consumer clear quiescent channel key %" PRIu64, key);
1364
1365 memset(&msg, 0, sizeof(msg));
1366 msg.cmd_type = LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL;
1367 msg.u.clear_quiescent_channel.key = key;
1368
1369 pthread_mutex_lock(socket->lock);
1370 health_code_update();
1371
1372 ret = consumer_send_msg(socket, &msg);
1373 if (ret < 0) {
1374 goto end;
1375 }
1376
1377end:
1378 health_code_update();
1379 pthread_mutex_unlock(socket->lock);
1380 return ret;
1381}
1382
7972aab2 1383/*
dc2bbdae
MD
1384 * Send a close metadata command to consumer using the given channel key.
1385 * Called with registry lock held.
7972aab2
DG
1386 *
1387 * Return 0 on success else a negative value.
1388 */
1389int consumer_close_metadata(struct consumer_socket *socket,
1390 uint64_t metadata_key)
1391{
1392 int ret;
1393 struct lttcomm_consumer_msg msg;
1394
1395 assert(socket);
7972aab2
DG
1396
1397 DBG2("Consumer close metadata channel key %" PRIu64, metadata_key);
1398
53efb85a 1399 memset(&msg, 0, sizeof(msg));
7972aab2
DG
1400 msg.cmd_type = LTTNG_CONSUMER_CLOSE_METADATA;
1401 msg.u.close_metadata.key = metadata_key;
1402
1403 pthread_mutex_lock(socket->lock);
1404 health_code_update();
1405
1406 ret = consumer_send_msg(socket, &msg);
1407 if (ret < 0) {
1408 goto end;
1409 }
1410
1411end:
1412 health_code_update();
1413 pthread_mutex_unlock(socket->lock);
1414 return ret;
1415}
1416
1417/*
1418 * Send a setup metdata command to consumer using the given channel key.
1419 *
1420 * Return 0 on success else a negative value.
1421 */
1422int consumer_setup_metadata(struct consumer_socket *socket,
1423 uint64_t metadata_key)
1424{
1425 int ret;
1426 struct lttcomm_consumer_msg msg;
1427
1428 assert(socket);
7972aab2
DG
1429
1430 DBG2("Consumer setup metadata channel key %" PRIu64, metadata_key);
1431
53efb85a 1432 memset(&msg, 0, sizeof(msg));
7972aab2
DG
1433 msg.cmd_type = LTTNG_CONSUMER_SETUP_METADATA;
1434 msg.u.setup_metadata.key = metadata_key;
1435
1436 pthread_mutex_lock(socket->lock);
1437 health_code_update();
1438
1439 ret = consumer_send_msg(socket, &msg);
1440 if (ret < 0) {
1441 goto end;
1442 }
1443
1444end:
1445 health_code_update();
1446 pthread_mutex_unlock(socket->lock);
1447 return ret;
1448}
1449
1450/*
dc2bbdae
MD
1451 * Send metadata string to consumer.
1452 * RCU read-side lock must be held to guarantee existence of socket.
7972aab2
DG
1453 *
1454 * Return 0 on success else a negative value.
1455 */
1456int consumer_push_metadata(struct consumer_socket *socket,
1457 uint64_t metadata_key, char *metadata_str, size_t len,
93ec662e 1458 size_t target_offset, uint64_t version)
7972aab2
DG
1459{
1460 int ret;
1461 struct lttcomm_consumer_msg msg;
1462
1463 assert(socket);
7972aab2 1464
9363801e 1465 DBG2("Consumer push metadata to consumer socket %d", *socket->fd_ptr);
7972aab2 1466
dc2bbdae
MD
1467 pthread_mutex_lock(socket->lock);
1468
53efb85a 1469 memset(&msg, 0, sizeof(msg));
7972aab2
DG
1470 msg.cmd_type = LTTNG_CONSUMER_PUSH_METADATA;
1471 msg.u.push_metadata.key = metadata_key;
1472 msg.u.push_metadata.target_offset = target_offset;
1473 msg.u.push_metadata.len = len;
93ec662e 1474 msg.u.push_metadata.version = version;
7972aab2 1475
7972aab2
DG
1476 health_code_update();
1477 ret = consumer_send_msg(socket, &msg);
331744e3 1478 if (ret < 0 || len == 0) {
7972aab2
DG
1479 goto end;
1480 }
1481
9363801e
DG
1482 DBG3("Consumer pushing metadata on sock %d of len %zu", *socket->fd_ptr,
1483 len);
7972aab2 1484
52898cb1 1485 ret = consumer_socket_send(socket, metadata_str, len);
7972aab2
DG
1486 if (ret < 0) {
1487 goto end;
1488 }
1489
1490 health_code_update();
1491 ret = consumer_recv_status_reply(socket);
1492 if (ret < 0) {
1493 goto end;
1494 }
1495
1496end:
dc2bbdae 1497 pthread_mutex_unlock(socket->lock);
7972aab2 1498 health_code_update();
7972aab2
DG
1499 return ret;
1500}
6dc3064a
DG
1501
1502/*
1503 * Ask the consumer to snapshot a specific channel using the key.
1504 *
9a654598 1505 * Returns LTTNG_OK on success or else an LTTng error code.
6dc3064a 1506 */
9a654598 1507enum lttng_error_code consumer_snapshot_channel(struct consumer_socket *socket,
348a81dc 1508 uint64_t key, const struct consumer_output *output, int metadata,
d2956687
JG
1509 uid_t uid, gid_t gid, const char *channel_path, int wait,
1510 uint64_t nb_packets_per_stream)
6dc3064a
DG
1511{
1512 int ret;
9a654598 1513 enum lttng_error_code status = LTTNG_OK;
6dc3064a
DG
1514 struct lttcomm_consumer_msg msg;
1515
1516 assert(socket);
6dc3064a 1517 assert(output);
6dc3064a
DG
1518
1519 DBG("Consumer snapshot channel key %" PRIu64, key);
1520
ee91bab2 1521 memset(&msg, 0, sizeof(msg));
6dc3064a
DG
1522 msg.cmd_type = LTTNG_CONSUMER_SNAPSHOT_CHANNEL;
1523 msg.u.snapshot_channel.key = key;
d07ceecd 1524 msg.u.snapshot_channel.nb_packets_per_stream = nb_packets_per_stream;
6dc3064a
DG
1525 msg.u.snapshot_channel.metadata = metadata;
1526
348a81dc 1527 if (output->type == CONSUMER_DST_NET) {
d2956687 1528 msg.u.snapshot_channel.relayd_id =
348a81dc 1529 output->net_seq_index;
6dc3064a 1530 msg.u.snapshot_channel.use_relayd = 1;
6dc3064a 1531 } else {
07b86b52 1532 msg.u.snapshot_channel.relayd_id = (uint64_t) -1ULL;
d2956687
JG
1533 }
1534 ret = lttng_strncpy(msg.u.snapshot_channel.pathname,
1535 channel_path,
1536 sizeof(msg.u.snapshot_channel.pathname));
1537 if (ret < 0) {
1538 ERR("Snapshot path exceeds the maximal allowed length of %zu bytes (%zu bytes required) with path \"%s\"",
1539 sizeof(msg.u.snapshot_channel.pathname),
1540 strlen(channel_path),
1541 channel_path);
1542 status = LTTNG_ERR_SNAPSHOT_FAIL;
1543 goto error;
6dc3064a
DG
1544 }
1545
1546 health_code_update();
9d1103e6 1547 pthread_mutex_lock(socket->lock);
6dc3064a 1548 ret = consumer_send_msg(socket, &msg);
9d1103e6 1549 pthread_mutex_unlock(socket->lock);
6dc3064a 1550 if (ret < 0) {
9bbfb88c
MD
1551 switch (-ret) {
1552 case LTTCOMM_CONSUMERD_CHAN_NOT_FOUND:
9a654598 1553 status = LTTNG_ERR_CHAN_NOT_FOUND;
9bbfb88c
MD
1554 break;
1555 default:
9a654598 1556 status = LTTNG_ERR_SNAPSHOT_FAIL;
9bbfb88c
MD
1557 break;
1558 }
6dc3064a
DG
1559 goto error;
1560 }
1561
1562error:
1563 health_code_update();
9a654598 1564 return status;
6dc3064a 1565}
fb83fe64
JD
1566
1567/*
1568 * Ask the consumer the number of discarded events for a channel.
1569 */
1570int consumer_get_discarded_events(uint64_t session_id, uint64_t channel_key,
1571 struct consumer_output *consumer, uint64_t *discarded)
1572{
1573 int ret;
1574 struct consumer_socket *socket;
1575 struct lttng_ht_iter iter;
1576 struct lttcomm_consumer_msg msg;
1577
1578 assert(consumer);
1579
1580 DBG3("Consumer discarded events id %" PRIu64, session_id);
1581
1582 memset(&msg, 0, sizeof(msg));
1583 msg.cmd_type = LTTNG_CONSUMER_DISCARDED_EVENTS;
1584 msg.u.discarded_events.session_id = session_id;
1585 msg.u.discarded_events.channel_key = channel_key;
1586
1587 *discarded = 0;
1588
1589 /* Send command for each consumer */
1590 rcu_read_lock();
1591 cds_lfht_for_each_entry(consumer->socks->ht, &iter.iter, socket,
1592 node.node) {
1593 uint64_t consumer_discarded = 0;
1594 pthread_mutex_lock(socket->lock);
1595 ret = consumer_socket_send(socket, &msg, sizeof(msg));
1596 if (ret < 0) {
1597 pthread_mutex_unlock(socket->lock);
1598 goto end;
1599 }
1600
1601 /*
1602 * No need for a recv reply status because the answer to the
1603 * command is the reply status message.
1604 */
1605 ret = consumer_socket_recv(socket, &consumer_discarded,
1606 sizeof(consumer_discarded));
1607 if (ret < 0) {
1608 ERR("get discarded events");
1609 pthread_mutex_unlock(socket->lock);
1610 goto end;
1611 }
1612 pthread_mutex_unlock(socket->lock);
1613 *discarded += consumer_discarded;
1614 }
1615 ret = 0;
1616 DBG("Consumer discarded %" PRIu64 " events in session id %" PRIu64,
1617 *discarded, session_id);
1618
1619end:
1620 rcu_read_unlock();
1621 return ret;
1622}
1623
1624/*
1625 * Ask the consumer the number of lost packets for a channel.
1626 */
1627int consumer_get_lost_packets(uint64_t session_id, uint64_t channel_key,
1628 struct consumer_output *consumer, uint64_t *lost)
1629{
1630 int ret;
1631 struct consumer_socket *socket;
1632 struct lttng_ht_iter iter;
1633 struct lttcomm_consumer_msg msg;
1634
1635 assert(consumer);
1636
1637 DBG3("Consumer lost packets id %" PRIu64, session_id);
1638
1639 memset(&msg, 0, sizeof(msg));
1640 msg.cmd_type = LTTNG_CONSUMER_LOST_PACKETS;
1641 msg.u.lost_packets.session_id = session_id;
1642 msg.u.lost_packets.channel_key = channel_key;
1643
1644 *lost = 0;
1645
1646 /* Send command for each consumer */
1647 rcu_read_lock();
1648 cds_lfht_for_each_entry(consumer->socks->ht, &iter.iter, socket,
1649 node.node) {
1650 uint64_t consumer_lost = 0;
1651 pthread_mutex_lock(socket->lock);
1652 ret = consumer_socket_send(socket, &msg, sizeof(msg));
1653 if (ret < 0) {
1654 pthread_mutex_unlock(socket->lock);
1655 goto end;
1656 }
1657
1658 /*
1659 * No need for a recv reply status because the answer to the
1660 * command is the reply status message.
1661 */
1662 ret = consumer_socket_recv(socket, &consumer_lost,
1663 sizeof(consumer_lost));
1664 if (ret < 0) {
1665 ERR("get lost packets");
1666 pthread_mutex_unlock(socket->lock);
1667 goto end;
1668 }
1669 pthread_mutex_unlock(socket->lock);
1670 *lost += consumer_lost;
1671 }
1672 ret = 0;
1673 DBG("Consumer lost %" PRIu64 " packets in session id %" PRIu64,
1674 *lost, session_id);
1675
1676end:
1677 rcu_read_unlock();
1678 return ret;
1679}
a1ae2ea5 1680
5c408ad8
JD
1681/*
1682 * Ask the consumer to rotate a channel.
5c408ad8
JD
1683 *
1684 * The new_chunk_id is the session->rotate_count that has been incremented
1685 * when the rotation started. On the relay, this allows to keep track in which
1686 * chunk each stream is currently writing to (for the rotate_pending operation).
1687 */
1688int consumer_rotate_channel(struct consumer_socket *socket, uint64_t key,
1689 uid_t uid, gid_t gid, struct consumer_output *output,
d2956687 1690 bool is_metadata_channel)
5c408ad8
JD
1691{
1692 int ret;
1693 struct lttcomm_consumer_msg msg;
1694
1695 assert(socket);
1696
1697 DBG("Consumer rotate channel key %" PRIu64, key);
1698
1699 pthread_mutex_lock(socket->lock);
1700 memset(&msg, 0, sizeof(msg));
1701 msg.cmd_type = LTTNG_CONSUMER_ROTATE_CHANNEL;
1702 msg.u.rotate_channel.key = key;
1703 msg.u.rotate_channel.metadata = !!is_metadata_channel;
5c408ad8
JD
1704
1705 if (output->type == CONSUMER_DST_NET) {
1706 msg.u.rotate_channel.relayd_id = output->net_seq_index;
5c408ad8
JD
1707 } else {
1708 msg.u.rotate_channel.relayd_id = (uint64_t) -1ULL;
5c408ad8
JD
1709 }
1710
1711 health_code_update();
1712 ret = consumer_send_msg(socket, &msg);
1713 if (ret < 0) {
20f37cb4
MD
1714 switch (-ret) {
1715 case LTTCOMM_CONSUMERD_CHAN_NOT_FOUND:
1716 ret = -LTTNG_ERR_CHAN_NOT_FOUND;
1717 break;
1718 default:
1719 ret = -LTTNG_ERR_ROTATION_FAIL_CONSUMER;
1720 break;
1721 }
5c408ad8
JD
1722 goto error;
1723 }
5c408ad8
JD
1724error:
1725 pthread_mutex_unlock(socket->lock);
1726 health_code_update();
1727 return ret;
1728}
1729
d2956687
JG
1730int consumer_init(struct consumer_socket *socket,
1731 const lttng_uuid sessiond_uuid)
00fb02ac
JD
1732{
1733 int ret;
d2956687
JG
1734 struct lttcomm_consumer_msg msg = {
1735 .cmd_type = LTTNG_CONSUMER_INIT,
1736 };
00fb02ac
JD
1737
1738 assert(socket);
00fb02ac 1739
d2956687
JG
1740 DBG("Sending consumer initialization command");
1741 lttng_uuid_copy(msg.u.init.sessiond_uuid, sessiond_uuid);
00fb02ac
JD
1742
1743 health_code_update();
1744 ret = consumer_send_msg(socket, &msg);
1745 if (ret < 0) {
1746 goto error;
1747 }
1748
d88744a4
JD
1749error:
1750 health_code_update();
1751 return ret;
1752}
1753
1754/*
d2956687 1755 * Ask the consumer to create a new chunk for a given session.
92816cc3 1756 *
d2956687 1757 * Called with the consumer socket lock held.
92816cc3 1758 */
d2956687
JG
1759int consumer_create_trace_chunk(struct consumer_socket *socket,
1760 uint64_t relayd_id, uint64_t session_id,
5da88b0f
MD
1761 struct lttng_trace_chunk *chunk,
1762 const char *domain_subdir)
92816cc3
JG
1763{
1764 int ret;
d2956687
JG
1765 enum lttng_trace_chunk_status chunk_status;
1766 struct lttng_credentials chunk_credentials;
5da88b0f
MD
1767 const struct lttng_directory_handle *chunk_directory_handle = NULL;
1768 struct lttng_directory_handle *domain_handle = NULL;
1769 int domain_dirfd;
d2956687 1770 const char *chunk_name;
913a542b 1771 bool chunk_name_overridden;
d2956687
JG
1772 uint64_t chunk_id;
1773 time_t creation_timestamp;
1774 char creation_timestamp_buffer[ISO8601_STR_LEN];
1775 const char *creation_timestamp_str = "(none)";
1776 const bool chunk_has_local_output = relayd_id == -1ULL;
1777 struct lttcomm_consumer_msg msg = {
1778 .cmd_type = LTTNG_CONSUMER_CREATE_TRACE_CHUNK,
1779 .u.create_trace_chunk.session_id = session_id,
1780 };
92816cc3
JG
1781
1782 assert(socket);
d2956687 1783 assert(chunk);
92816cc3 1784
d2956687
JG
1785 if (relayd_id != -1ULL) {
1786 LTTNG_OPTIONAL_SET(&msg.u.create_trace_chunk.relayd_id,
1787 relayd_id);
1788 }
92816cc3 1789
d2956687 1790 chunk_status = lttng_trace_chunk_get_name(chunk, &chunk_name,
913a542b 1791 &chunk_name_overridden);
d2956687
JG
1792 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK &&
1793 chunk_status != LTTNG_TRACE_CHUNK_STATUS_NONE) {
1794 ERR("Failed to get name of trace chunk");
1795 ret = -LTTNG_ERR_FATAL;
92816cc3
JG
1796 goto error;
1797 }
913a542b 1798 if (chunk_name_overridden) {
d2956687
JG
1799 ret = lttng_strncpy(msg.u.create_trace_chunk.override_name,
1800 chunk_name,
1801 sizeof(msg.u.create_trace_chunk.override_name));
1802 if (ret) {
1803 ERR("Trace chunk name \"%s\" exceeds the maximal length allowed by the consumer protocol",
1804 chunk_name);
1805 ret = -LTTNG_ERR_FATAL;
1806 goto error;
1807 }
1808 }
92816cc3 1809
d2956687
JG
1810 chunk_status = lttng_trace_chunk_get_creation_timestamp(chunk,
1811 &creation_timestamp);
1812 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
1813 ret = -LTTNG_ERR_FATAL;
92816cc3
JG
1814 goto error;
1815 }
d2956687
JG
1816 msg.u.create_trace_chunk.creation_timestamp =
1817 (uint64_t) creation_timestamp;
1818 /* Only used for logging purposes. */
1819 ret = time_to_iso8601_str(creation_timestamp,
1820 creation_timestamp_buffer,
1821 sizeof(creation_timestamp_buffer));
1822 creation_timestamp_str = !ret ? creation_timestamp_buffer :
1823 "(formatting error)";
1824
1825 chunk_status = lttng_trace_chunk_get_id(chunk, &chunk_id);
1826 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
1827 /*
1828 * Anonymous trace chunks should never be transmitted
1829 * to remote peers (consumerd and relayd). They are used
1830 * internally for backward-compatibility purposes.
1831 */
1832 ret = -LTTNG_ERR_FATAL;
1833 goto error;
1834 }
1835 msg.u.create_trace_chunk.chunk_id = chunk_id;
92816cc3 1836
d2956687 1837 if (chunk_has_local_output) {
cbf53d23 1838 chunk_status = lttng_trace_chunk_borrow_chunk_directory_handle(
d2956687
JG
1839 chunk, &chunk_directory_handle);
1840 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
1841 ret = -LTTNG_ERR_FATAL;
1842 goto error;
1843 }
e5add6d0
JG
1844 chunk_status = lttng_trace_chunk_get_credentials(
1845 chunk, &chunk_credentials);
1846 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
1847 /*
1848 * Not associating credentials to a sessiond chunk is a
1849 * fatal internal error.
1850 */
1851 ret = -LTTNG_ERR_FATAL;
1852 goto error;
1853 }
5da88b0f
MD
1854 ret = lttng_directory_handle_create_subdirectory_as_user(
1855 chunk_directory_handle,
1856 domain_subdir,
1857 S_IRWXU | S_IRWXG,
1858 &chunk_credentials);
1859 if (ret) {
1860 PERROR("Failed to create chunk domain output directory \"%s\"",
1861 domain_subdir);
1862 ret = -LTTNG_ERR_FATAL;
1863 goto error;
1864 }
1865 domain_handle = lttng_directory_handle_create_from_handle(
1866 domain_subdir,
1867 chunk_directory_handle);
1868 if (!domain_handle) {
1869 ret = -LTTNG_ERR_FATAL;
1870 goto error;
1871 }
1872
1873 /*
1874 * This will only compile on platforms that support
1875 * dirfd (POSIX.2008). This is fine as the session daemon
1876 * is only built for such platforms.
1877 *
1878 * The ownership of the chunk directory handle's is maintained
1879 * by the trace chunk.
1880 */
1881 domain_dirfd = lttng_directory_handle_get_dirfd(
1882 domain_handle);
1883 assert(domain_dirfd >= 0);
1884
e5add6d0
JG
1885 msg.u.create_trace_chunk.credentials.value.uid =
1886 chunk_credentials.uid;
1887 msg.u.create_trace_chunk.credentials.value.gid =
1888 chunk_credentials.gid;
1889 msg.u.create_trace_chunk.credentials.is_set = 1;
d2956687 1890 }
d88744a4 1891
d2956687
JG
1892 DBG("Sending consumer create trace chunk command: relayd_id = %" PRId64
1893 ", session_id = %" PRIu64 ", chunk_id = %" PRIu64
1894 ", creation_timestamp = %s",
1895 relayd_id, session_id, chunk_id,
1896 creation_timestamp_str);
d88744a4
JD
1897 health_code_update();
1898 ret = consumer_send_msg(socket, &msg);
d2956687 1899 health_code_update();
d88744a4 1900 if (ret < 0) {
d2956687
JG
1901 ERR("Trace chunk creation error on consumer");
1902 ret = -LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
d88744a4
JD
1903 goto error;
1904 }
1905
d2956687 1906 if (chunk_has_local_output) {
5da88b0f 1907 DBG("Sending trace chunk domain directory fd to consumer");
d2956687 1908 health_code_update();
5da88b0f 1909 ret = consumer_send_fds(socket, &domain_dirfd, 1);
d2956687
JG
1910 health_code_update();
1911 if (ret < 0) {
1912 ERR("Trace chunk creation error on consumer");
1913 ret = -LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
1914 goto error;
1915 }
d88744a4 1916 }
00fb02ac 1917error:
5da88b0f 1918 lttng_directory_handle_put(domain_handle);
00fb02ac
JD
1919 return ret;
1920}
1921
a1ae2ea5 1922/*
d2956687 1923 * Ask the consumer to close a trace chunk for a given session.
a1ae2ea5
JD
1924 *
1925 * Called with the consumer socket lock held.
1926 */
d2956687
JG
1927int consumer_close_trace_chunk(struct consumer_socket *socket,
1928 uint64_t relayd_id, uint64_t session_id,
ecd1a12f
MD
1929 struct lttng_trace_chunk *chunk,
1930 char *closed_trace_chunk_path)
a1ae2ea5
JD
1931{
1932 int ret;
d2956687
JG
1933 enum lttng_trace_chunk_status chunk_status;
1934 struct lttcomm_consumer_msg msg = {
bbc4768c
JG
1935 .cmd_type = LTTNG_CONSUMER_CLOSE_TRACE_CHUNK,
1936 .u.close_trace_chunk.session_id = session_id,
d2956687 1937 };
ecd1a12f 1938 struct lttcomm_consumer_close_trace_chunk_reply reply;
d2956687
JG
1939 uint64_t chunk_id;
1940 time_t close_timestamp;
bbc4768c
JG
1941 enum lttng_trace_chunk_command_type close_command;
1942 const char *close_command_name = "none";
ecd1a12f 1943 struct lttng_dynamic_buffer path_reception_buffer;
a1ae2ea5
JD
1944
1945 assert(socket);
ecd1a12f 1946 lttng_dynamic_buffer_init(&path_reception_buffer);
a1ae2ea5 1947
d2956687 1948 if (relayd_id != -1ULL) {
bbc4768c
JG
1949 LTTNG_OPTIONAL_SET(
1950 &msg.u.close_trace_chunk.relayd_id, relayd_id);
1951 }
1952
1953 chunk_status = lttng_trace_chunk_get_close_command(
1954 chunk, &close_command);
1955 switch (chunk_status) {
1956 case LTTNG_TRACE_CHUNK_STATUS_OK:
1957 LTTNG_OPTIONAL_SET(&msg.u.close_trace_chunk.close_command,
1958 (uint32_t) close_command);
1959 break;
1960 case LTTNG_TRACE_CHUNK_STATUS_NONE:
1961 break;
1962 default:
1963 ERR("Failed to get trace chunk close command");
1964 ret = -1;
1965 goto error;
a1ae2ea5
JD
1966 }
1967
d2956687
JG
1968 chunk_status = lttng_trace_chunk_get_id(chunk, &chunk_id);
1969 /*
1970 * Anonymous trace chunks should never be transmitted to remote peers
1971 * (consumerd and relayd). They are used internally for
1972 * backward-compatibility purposes.
1973 */
1974 assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
1975 msg.u.close_trace_chunk.chunk_id = chunk_id;
1976
1977 chunk_status = lttng_trace_chunk_get_close_timestamp(chunk,
1978 &close_timestamp);
1979 /*
1980 * A trace chunk should be closed locally before being closed remotely.
1981 * Otherwise, the close timestamp would never be transmitted to the
1982 * peers.
1983 */
1984 assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
1985 msg.u.close_trace_chunk.close_timestamp = (uint64_t) close_timestamp;
1986
bbc4768c
JG
1987 if (msg.u.close_trace_chunk.close_command.is_set) {
1988 close_command_name = lttng_trace_chunk_command_type_get_name(
1989 close_command);
1990 }
d2956687 1991 DBG("Sending consumer close trace chunk command: relayd_id = %" PRId64
bbc4768c
JG
1992 ", session_id = %" PRIu64 ", chunk_id = %" PRIu64
1993 ", close command = \"%s\"",
1994 relayd_id, session_id, chunk_id, close_command_name);
a1ae2ea5
JD
1995
1996 health_code_update();
ecd1a12f 1997 ret = consumer_socket_send(socket, &msg, sizeof(struct lttcomm_consumer_msg));
a1ae2ea5 1998 if (ret < 0) {
d2956687 1999 ret = -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER;
a1ae2ea5
JD
2000 goto error;
2001 }
ecd1a12f
MD
2002 ret = consumer_socket_recv(socket, &reply, sizeof(reply));
2003 if (ret < 0) {
2004 ret = -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER;
2005 goto error;
2006 }
2007 if (reply.path_length >= LTTNG_PATH_MAX) {
2008 ERR("Invalid path returned by relay daemon: %" PRIu32 "bytes exceeds maximal allowed length of %d bytes",
2009 reply.path_length, LTTNG_PATH_MAX);
2010 ret = -LTTNG_ERR_INVALID_PROTOCOL;
2011 goto error;
2012 }
2013 ret = lttng_dynamic_buffer_set_size(&path_reception_buffer,
2014 reply.path_length);
2015 if (ret) {
2016 ERR("Failed to allocate reception buffer of path returned by the \"close trace chunk\" command");
2017 ret = -LTTNG_ERR_NOMEM;
2018 goto error;
2019 }
2020 ret = consumer_socket_recv(socket, path_reception_buffer.data,
2021 path_reception_buffer.size);
2022 if (ret < 0) {
2023 ERR("Communication error while receiving path of closed trace chunk");
2024 ret = -LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER;
2025 goto error;
2026 }
2027 if (path_reception_buffer.data[path_reception_buffer.size - 1] != '\0') {
2028 ERR("Invalid path returned by relay daemon: not null-terminated");
2029 ret = -LTTNG_ERR_INVALID_PROTOCOL;
2030 goto error;
2031 }
2032 if (closed_trace_chunk_path) {
2033 /*
2034 * closed_trace_chunk_path is assumed to have a length >=
2035 * LTTNG_PATH_MAX
2036 */
2037 memcpy(closed_trace_chunk_path, path_reception_buffer.data,
2038 path_reception_buffer.size);
2039 }
a1ae2ea5 2040error:
ecd1a12f 2041 lttng_dynamic_buffer_reset(&path_reception_buffer);
a1ae2ea5
JD
2042 health_code_update();
2043 return ret;
2044}
3654ed19 2045
d2956687
JG
2046/*
2047 * Ask the consumer if a trace chunk exists.
2048 *
2049 * Called with the consumer socket lock held.
2050 * Returns 0 on success, or a negative value on error.
2051 */
2052int consumer_trace_chunk_exists(struct consumer_socket *socket,
2053 uint64_t relayd_id, uint64_t session_id,
2054 struct lttng_trace_chunk *chunk,
2055 enum consumer_trace_chunk_exists_status *result)
3654ed19
JG
2056{
2057 int ret;
d2956687 2058 enum lttng_trace_chunk_status chunk_status;
3654ed19 2059 struct lttcomm_consumer_msg msg = {
d2956687
JG
2060 .cmd_type = LTTNG_CONSUMER_TRACE_CHUNK_EXISTS,
2061 .u.trace_chunk_exists.session_id = session_id,
3654ed19 2062 };
d2956687
JG
2063 uint64_t chunk_id;
2064 const char *consumer_reply_str;
3654ed19
JG
2065
2066 assert(socket);
2067
d2956687
JG
2068 if (relayd_id != -1ULL) {
2069 LTTNG_OPTIONAL_SET(&msg.u.trace_chunk_exists.relayd_id,
2070 relayd_id);
2071 }
2072
2073 chunk_status = lttng_trace_chunk_get_id(chunk, &chunk_id);
2074 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
2075 /*
2076 * Anonymous trace chunks should never be transmitted
2077 * to remote peers (consumerd and relayd). They are used
2078 * internally for backward-compatibility purposes.
2079 */
2080 ret = -LTTNG_ERR_FATAL;
2081 goto error;
2082 }
2083 msg.u.trace_chunk_exists.chunk_id = chunk_id;
2084
2085 DBG("Sending consumer trace chunk exists command: relayd_id = %" PRId64
2086 ", session_id = %" PRIu64
2087 ", chunk_id = %" PRIu64, relayd_id, session_id, chunk_id);
3654ed19
JG
2088
2089 health_code_update();
2090 ret = consumer_send_msg(socket, &msg);
d2956687
JG
2091 switch (-ret) {
2092 case LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK:
2093 consumer_reply_str = "unknown trace chunk";
2094 *result = CONSUMER_TRACE_CHUNK_EXISTS_STATUS_UNKNOWN_CHUNK;
2095 break;
2096 case LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL:
2097 consumer_reply_str = "trace chunk exists locally";
2098 *result = CONSUMER_TRACE_CHUNK_EXISTS_STATUS_EXISTS_LOCAL;
2099 break;
2100 case LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE:
2101 consumer_reply_str = "trace chunk exists on remote peer";
2102 *result = CONSUMER_TRACE_CHUNK_EXISTS_STATUS_EXISTS_REMOTE;
2103 break;
2104 default:
2105 ERR("Consumer returned an error from TRACE_CHUNK_EXISTS command");
2106 ret = -1;
3654ed19
JG
2107 goto error;
2108 }
d2956687
JG
2109 DBG("Consumer reply to TRACE_CHUNK_EXISTS command: %s",
2110 consumer_reply_str);
2111 ret = 0;
3654ed19
JG
2112error:
2113 health_code_update();
2114 return ret;
2115}
This page took 0.158616 seconds and 4 git commands to generate.