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