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