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