Teardown the notification thread after the sessiond clean-up
[lttng-tools.git] / src / bin / lttng-sessiond / consumer.c
CommitLineData
00e2e675
DG
1/*
2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
92816cc3 3 * 2018 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
00e2e675
DG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License, version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 51
16 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
6c1c0768 19#define _LGPL_SOURCE
00e2e675
DG
20#include <assert.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <unistd.h>
d88aee68 27#include <inttypes.h>
00e2e675
DG
28
29#include <common/common.h>
30#include <common/defaults.h>
31#include <common/uri.h>
d3e2ba59 32#include <common/relayd/relayd.h>
00e2e675
DG
33
34#include "consumer.h"
8782cc74 35#include "health-sessiond.h"
7972aab2 36#include "ust-app.h"
0b2dc8df 37#include "utils.h"
00e2e675 38
52898cb1
DG
39/*
40 * Send a data payload using a given consumer socket of size len.
41 *
42 * The consumer socket lock MUST be acquired before calling this since this
43 * function can change the fd value.
44 *
45 * Return 0 on success else a negative value on error.
46 */
47int consumer_socket_send(struct consumer_socket *socket, void *msg, size_t len)
48{
49 int fd;
50 ssize_t size;
51
52 assert(socket);
9363801e 53 assert(socket->fd_ptr);
52898cb1
DG
54 assert(msg);
55
56 /* Consumer socket is invalid. Stopping. */
9363801e 57 fd = *socket->fd_ptr;
52898cb1
DG
58 if (fd < 0) {
59 goto error;
60 }
61
62 size = lttcomm_send_unix_sock(fd, msg, len);
63 if (size < 0) {
64 /* The above call will print a PERROR on error. */
65 DBG("Error when sending data to consumer on sock %d", fd);
66 /*
92db7cdc
DG
67 * At this point, the socket is not usable anymore thus closing it and
68 * setting the file descriptor to -1 so it is not reused.
52898cb1
DG
69 */
70
71 /* This call will PERROR on error. */
72 (void) lttcomm_close_unix_sock(fd);
9363801e 73 *socket->fd_ptr = -1;
52898cb1
DG
74 goto error;
75 }
76
77 return 0;
78
79error:
80 return -1;
81}
82
83/*
84 * Receive a data payload using a given consumer socket of size len.
85 *
86 * The consumer socket lock MUST be acquired before calling this since this
87 * function can change the fd value.
88 *
89 * Return 0 on success else a negative value on error.
90 */
91int consumer_socket_recv(struct consumer_socket *socket, void *msg, size_t len)
92{
93 int fd;
94 ssize_t size;
95
96 assert(socket);
9363801e 97 assert(socket->fd_ptr);
52898cb1
DG
98 assert(msg);
99
100 /* Consumer socket is invalid. Stopping. */
9363801e 101 fd = *socket->fd_ptr;
52898cb1
DG
102 if (fd < 0) {
103 goto error;
104 }
105
106 size = lttcomm_recv_unix_sock(fd, msg, len);
107 if (size <= 0) {
108 /* The above call will print a PERROR on error. */
109 DBG("Error when receiving data from the consumer socket %d", fd);
110 /*
92db7cdc
DG
111 * At this point, the socket is not usable anymore thus closing it and
112 * setting the file descriptor to -1 so it is not reused.
52898cb1
DG
113 */
114
115 /* This call will PERROR on error. */
116 (void) lttcomm_close_unix_sock(fd);
9363801e 117 *socket->fd_ptr = -1;
52898cb1
DG
118 goto error;
119 }
120
121 return 0;
122
123error:
124 return -1;
125}
126
f50f23d9
DG
127/*
128 * Receive a reply command status message from the consumer. Consumer socket
129 * lock MUST be acquired before calling this function.
130 *
131 * Return 0 on success, -1 on recv error or a negative lttng error code which
132 * was possibly returned by the consumer.
133 */
134int consumer_recv_status_reply(struct consumer_socket *sock)
135{
136 int ret;
137 struct lttcomm_consumer_status_msg reply;
138
139 assert(sock);
140
52898cb1
DG
141 ret = consumer_socket_recv(sock, &reply, sizeof(reply));
142 if (ret < 0) {
f50f23d9
DG
143 goto end;
144 }
145
0c759fc9 146 if (reply.ret_code == LTTCOMM_CONSUMERD_SUCCESS) {
f50f23d9
DG
147 /* All good. */
148 ret = 0;
149 } else {
150 ret = -reply.ret_code;
ffe60014 151 DBG("Consumer ret code %d", ret);
f50f23d9
DG
152 }
153
154end:
155 return ret;
156}
157
ffe60014
DG
158/*
159 * Once the ASK_CHANNEL command is sent to the consumer, the channel
160 * information are sent back. This call receives that data and populates key
161 * and stream_count.
162 *
163 * On success return 0 and both key and stream_count are set. On error, a
164 * negative value is sent back and both parameters are untouched.
165 */
166int consumer_recv_status_channel(struct consumer_socket *sock,
d88aee68 167 uint64_t *key, unsigned int *stream_count)
ffe60014
DG
168{
169 int ret;
170 struct lttcomm_consumer_status_channel reply;
171
172 assert(sock);
173 assert(stream_count);
174 assert(key);
175
52898cb1
DG
176 ret = consumer_socket_recv(sock, &reply, sizeof(reply));
177 if (ret < 0) {
ffe60014
DG
178 goto end;
179 }
180
181 /* An error is possible so don't touch the key and stream_count. */
0c759fc9 182 if (reply.ret_code != LTTCOMM_CONSUMERD_SUCCESS) {
ffe60014
DG
183 ret = -1;
184 goto end;
185 }
186
187 *key = reply.key;
188 *stream_count = reply.stream_count;
0c759fc9 189 ret = 0;
ffe60014
DG
190
191end:
192 return ret;
193}
194
2f77fc4b
DG
195/*
196 * Send destroy relayd command to consumer.
197 *
198 * On success return positive value. On error, negative value.
199 */
200int consumer_send_destroy_relayd(struct consumer_socket *sock,
201 struct consumer_output *consumer)
202{
203 int ret;
204 struct lttcomm_consumer_msg msg;
205
206 assert(consumer);
207 assert(sock);
208
9363801e 209 DBG2("Sending destroy relayd command to consumer sock %d", *sock->fd_ptr);
2f77fc4b 210
53efb85a 211 memset(&msg, 0, sizeof(msg));
2f77fc4b
DG
212 msg.cmd_type = LTTNG_CONSUMER_DESTROY_RELAYD;
213 msg.u.destroy_relayd.net_seq_idx = consumer->net_seq_index;
214
215 pthread_mutex_lock(sock->lock);
52898cb1 216 ret = consumer_socket_send(sock, &msg, sizeof(msg));
2f77fc4b 217 if (ret < 0) {
52898cb1 218 goto error;
2f77fc4b
DG
219 }
220
f50f23d9
DG
221 /* Don't check the return value. The caller will do it. */
222 ret = consumer_recv_status_reply(sock);
223
2f77fc4b
DG
224 DBG2("Consumer send destroy relayd command done");
225
226error:
52898cb1 227 pthread_mutex_unlock(sock->lock);
2f77fc4b
DG
228 return ret;
229}
230
231/*
232 * For each consumer socket in the consumer output object, send a destroy
233 * relayd command.
234 */
235void consumer_output_send_destroy_relayd(struct consumer_output *consumer)
236{
2f77fc4b
DG
237 struct lttng_ht_iter iter;
238 struct consumer_socket *socket;
239
240 assert(consumer);
241
242 /* Destroy any relayd connection */
6dc3064a 243 if (consumer->type == CONSUMER_DST_NET) {
2f77fc4b
DG
244 rcu_read_lock();
245 cds_lfht_for_each_entry(consumer->socks->ht, &iter.iter, socket,
246 node.node) {
c617c0c6
MD
247 int ret;
248
2f77fc4b
DG
249 /* Send destroy relayd command */
250 ret = consumer_send_destroy_relayd(socket, consumer);
251 if (ret < 0) {
c5c45efa 252 DBG("Unable to send destroy relayd command to consumer");
2f77fc4b
DG
253 /* Continue since we MUST delete everything at this point. */
254 }
255 }
256 rcu_read_unlock();
257 }
258}
259
a4b92340
DG
260/*
261 * From a consumer_data structure, allocate and add a consumer socket to the
262 * consumer output.
263 *
264 * Return 0 on success, else negative value on error
265 */
266int consumer_create_socket(struct consumer_data *data,
267 struct consumer_output *output)
268{
269 int ret = 0;
270 struct consumer_socket *socket;
271
272 assert(data);
273
274 if (output == NULL || data->cmd_sock < 0) {
275 /*
276 * Not an error. Possible there is simply not spawned consumer or it's
277 * disabled for the tracing session asking the socket.
278 */
279 goto error;
280 }
281
282 rcu_read_lock();
283 socket = consumer_find_socket(data->cmd_sock, output);
284 rcu_read_unlock();
285 if (socket == NULL) {
4ce514c4 286 socket = consumer_allocate_socket(&data->cmd_sock);
a4b92340
DG
287 if (socket == NULL) {
288 ret = -1;
289 goto error;
290 }
291
2f77fc4b 292 socket->registered = 0;
a4b92340
DG
293 socket->lock = &data->lock;
294 rcu_read_lock();
295 consumer_add_socket(socket, output);
296 rcu_read_unlock();
297 }
298
6dc3064a
DG
299 socket->type = data->type;
300
a4b92340
DG
301 DBG3("Consumer socket created (fd: %d) and added to output",
302 data->cmd_sock);
303
304error:
305 return ret;
306}
307
7972aab2
DG
308/*
309 * Return the consumer socket from the given consumer output with the right
310 * bitness. On error, returns NULL.
311 *
312 * The caller MUST acquire a rcu read side lock and keep it until the socket
313 * object reference is not needed anymore.
314 */
315struct consumer_socket *consumer_find_socket_by_bitness(int bits,
316 struct consumer_output *consumer)
317{
318 int consumer_fd;
319 struct consumer_socket *socket = NULL;
320
321 switch (bits) {
322 case 64:
323 consumer_fd = uatomic_read(&ust_consumerd64_fd);
324 break;
325 case 32:
326 consumer_fd = uatomic_read(&ust_consumerd32_fd);
327 break;
328 default:
329 assert(0);
330 goto end;
331 }
332
333 socket = consumer_find_socket(consumer_fd, consumer);
334 if (!socket) {
335 ERR("Consumer socket fd %d not found in consumer obj %p",
336 consumer_fd, consumer);
337 }
338
339end:
340 return socket;
341}
342
173af62f
DG
343/*
344 * Find a consumer_socket in a consumer_output hashtable. Read side lock must
345 * be acquired before calling this function and across use of the
346 * returned consumer_socket.
347 */
348struct consumer_socket *consumer_find_socket(int key,
349 struct consumer_output *consumer)
350{
351 struct lttng_ht_iter iter;
352 struct lttng_ht_node_ulong *node;
353 struct consumer_socket *socket = NULL;
354
355 /* Negative keys are lookup failures */
a4b92340 356 if (key < 0 || consumer == NULL) {
173af62f
DG
357 return NULL;
358 }
359
360 lttng_ht_lookup(consumer->socks, (void *)((unsigned long) key),
361 &iter);
362 node = lttng_ht_iter_get_node_ulong(&iter);
363 if (node != NULL) {
364 socket = caa_container_of(node, struct consumer_socket, node);
365 }
366
367 return socket;
368}
369
370/*
371 * Allocate a new consumer_socket and return the pointer.
372 */
4ce514c4 373struct consumer_socket *consumer_allocate_socket(int *fd)
173af62f
DG
374{
375 struct consumer_socket *socket = NULL;
376
4ce514c4
DG
377 assert(fd);
378
173af62f
DG
379 socket = zmalloc(sizeof(struct consumer_socket));
380 if (socket == NULL) {
381 PERROR("zmalloc consumer socket");
382 goto error;
383 }
384
9363801e 385 socket->fd_ptr = fd;
4ce514c4 386 lttng_ht_node_init_ulong(&socket->node, *fd);
173af62f
DG
387
388error:
389 return socket;
390}
391
392/*
393 * Add consumer socket to consumer output object. Read side lock must be
394 * acquired before calling this function.
395 */
396void consumer_add_socket(struct consumer_socket *sock,
397 struct consumer_output *consumer)
398{
399 assert(sock);
400 assert(consumer);
401
402 lttng_ht_add_unique_ulong(consumer->socks, &sock->node);
403}
404
405/*
406 * Delte consumer socket to consumer output object. Read side lock must be
407 * acquired before calling this function.
408 */
409void consumer_del_socket(struct consumer_socket *sock,
410 struct consumer_output *consumer)
411{
412 int ret;
413 struct lttng_ht_iter iter;
414
415 assert(sock);
416 assert(consumer);
417
418 iter.iter.node = &sock->node.node;
419 ret = lttng_ht_del(consumer->socks, &iter);
420 assert(!ret);
421}
422
423/*
424 * RCU destroy call function.
425 */
426static void destroy_socket_rcu(struct rcu_head *head)
427{
428 struct lttng_ht_node_ulong *node =
429 caa_container_of(head, struct lttng_ht_node_ulong, head);
430 struct consumer_socket *socket =
431 caa_container_of(node, struct consumer_socket, node);
432
433 free(socket);
434}
435
436/*
437 * Destroy and free socket pointer in a call RCU. Read side lock must be
438 * acquired before calling this function.
439 */
440void consumer_destroy_socket(struct consumer_socket *sock)
441{
442 assert(sock);
443
444 /*
445 * We DO NOT close the file descriptor here since it is global to the
2f77fc4b
DG
446 * session daemon and is closed only if the consumer dies or a custom
447 * consumer was registered,
173af62f 448 */
2f77fc4b 449 if (sock->registered) {
9363801e
DG
450 DBG3("Consumer socket was registered. Closing fd %d", *sock->fd_ptr);
451 lttcomm_close_unix_sock(*sock->fd_ptr);
2f77fc4b 452 }
173af62f
DG
453
454 call_rcu(&sock->node.head, destroy_socket_rcu);
455}
456
00e2e675
DG
457/*
458 * Allocate and assign data to a consumer_output object.
459 *
460 * Return pointer to structure.
461 */
462struct consumer_output *consumer_create_output(enum consumer_dst_type type)
463{
464 struct consumer_output *output = NULL;
465
466 output = zmalloc(sizeof(struct consumer_output));
467 if (output == NULL) {
468 PERROR("zmalloc consumer_output");
469 goto error;
470 }
471
472 /* By default, consumer output is enabled */
473 output->enabled = 1;
474 output->type = type;
d88aee68 475 output->net_seq_index = (uint64_t) -1ULL;
6addfa37 476 urcu_ref_init(&output->ref);
173af62f
DG
477
478 output->socks = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
00e2e675
DG
479
480error:
481 return output;
482}
483
af706bb7
DG
484/*
485 * Iterate over the consumer output socket hash table and destroy them. The
486 * socket file descriptor are only closed if the consumer output was
487 * registered meaning it's an external consumer.
488 */
489void consumer_destroy_output_sockets(struct consumer_output *obj)
490{
491 struct lttng_ht_iter iter;
492 struct consumer_socket *socket;
493
494 if (!obj->socks) {
495 return;
496 }
497
498 rcu_read_lock();
499 cds_lfht_for_each_entry(obj->socks->ht, &iter.iter, socket, node.node) {
500 consumer_del_socket(socket, obj);
501 consumer_destroy_socket(socket);
502 }
503 rcu_read_unlock();
504}
505
00e2e675
DG
506/*
507 * Delete the consumer_output object from the list and free the ptr.
36b588ed
MD
508 *
509 * Should *NOT* be called with RCU read-side lock held.
00e2e675 510 */
6addfa37 511static void consumer_release_output(struct urcu_ref *ref)
00e2e675 512{
6addfa37
MD
513 struct consumer_output *obj =
514 caa_container_of(ref, struct consumer_output, ref);
00e2e675 515
af706bb7 516 consumer_destroy_output_sockets(obj);
2f77fc4b 517
af706bb7 518 if (obj->socks) {
2f77fc4b 519 /* Finally destroy HT */
0b2dc8df 520 ht_cleanup_push(obj->socks);
00e2e675 521 }
173af62f 522
00e2e675
DG
523 free(obj);
524}
525
6addfa37
MD
526/*
527 * Get the consumer_output object.
528 */
529void consumer_output_get(struct consumer_output *obj)
530{
531 urcu_ref_get(&obj->ref);
532}
533
534/*
535 * Put the consumer_output object.
536 *
537 * Should *NOT* be called with RCU read-side lock held.
538 */
539void consumer_output_put(struct consumer_output *obj)
540{
541 if (!obj) {
542 return;
543 }
544 urcu_ref_put(&obj->ref, consumer_release_output);
545}
546
00e2e675
DG
547/*
548 * Copy consumer output and returned the newly allocated copy.
36b588ed
MD
549 *
550 * Should *NOT* be called with RCU read-side lock held.
00e2e675
DG
551 */
552struct consumer_output *consumer_copy_output(struct consumer_output *obj)
553{
6dc3064a 554 int ret;
00e2e675
DG
555 struct consumer_output *output;
556
557 assert(obj);
558
559 output = consumer_create_output(obj->type);
560 if (output == NULL) {
6addfa37 561 goto end;
00e2e675 562 }
6addfa37
MD
563 output->enabled = obj->enabled;
564 output->net_seq_index = obj->net_seq_index;
61ba6b6d 565 memcpy(output->subdir, obj->subdir, sizeof(output->subdir));
6addfa37 566 output->snapshot = obj->snapshot;
b31610f2
JD
567 output->relay_major_version = obj->relay_major_version;
568 output->relay_minor_version = obj->relay_minor_version;
6addfa37 569 memcpy(&output->dst, &obj->dst, sizeof(output->dst));
6dc3064a
DG
570 ret = consumer_copy_sockets(output, obj);
571 if (ret < 0) {
6addfa37 572 goto error_put;
6dc3064a 573 }
6addfa37 574end:
6dc3064a
DG
575 return output;
576
6addfa37
MD
577error_put:
578 consumer_output_put(output);
6dc3064a
DG
579 return NULL;
580}
581
582/*
583 * Copy consumer sockets from src to dst.
584 *
585 * Return 0 on success or else a negative value.
586 */
587int consumer_copy_sockets(struct consumer_output *dst,
588 struct consumer_output *src)
589{
590 int ret = 0;
591 struct lttng_ht_iter iter;
592 struct consumer_socket *socket, *copy_sock;
593
594 assert(dst);
595 assert(src);
596
b82c5c4d 597 rcu_read_lock();
6dc3064a
DG
598 cds_lfht_for_each_entry(src->socks->ht, &iter.iter, socket, node.node) {
599 /* Ignore socket that are already there. */
9363801e 600 copy_sock = consumer_find_socket(*socket->fd_ptr, dst);
6dc3064a
DG
601 if (copy_sock) {
602 continue;
603 }
604
173af62f 605 /* Create new socket object. */
9363801e 606 copy_sock = consumer_allocate_socket(socket->fd_ptr);
173af62f 607 if (copy_sock == NULL) {
b82c5c4d 608 rcu_read_unlock();
6dc3064a
DG
609 ret = -ENOMEM;
610 goto error;
173af62f
DG
611 }
612
09a90bcd 613 copy_sock->registered = socket->registered;
6dc3064a
DG
614 /*
615 * This is valid because this lock is shared accross all consumer
616 * object being the global lock of the consumer data structure of the
617 * session daemon.
618 */
173af62f 619 copy_sock->lock = socket->lock;
6dc3064a 620 consumer_add_socket(copy_sock, dst);
173af62f 621 }
b82c5c4d 622 rcu_read_unlock();
173af62f 623
00e2e675 624error:
6dc3064a 625 return ret;
00e2e675
DG
626}
627
628/*
629 * Set network URI to the consumer output object.
630 *
ad20f474
DG
631 * Return 0 on success. Return 1 if the URI were equal. Else, negative value on
632 * error.
00e2e675
DG
633 */
634int consumer_set_network_uri(struct consumer_output *obj,
635 struct lttng_uri *uri)
636{
637 int ret;
638 char tmp_path[PATH_MAX];
639 char hostname[HOST_NAME_MAX];
640 struct lttng_uri *dst_uri = NULL;
641
642 /* Code flow error safety net. */
643 assert(obj);
644 assert(uri);
645
646 switch (uri->stype) {
647 case LTTNG_STREAM_CONTROL:
648 dst_uri = &obj->dst.net.control;
649 obj->dst.net.control_isset = 1;
650 if (uri->port == 0) {
651 /* Assign default port. */
652 uri->port = DEFAULT_NETWORK_CONTROL_PORT;
a74934ba
DG
653 } else {
654 if (obj->dst.net.data_isset && uri->port ==
655 obj->dst.net.data.port) {
656 ret = -LTTNG_ERR_INVALID;
657 goto error;
658 }
00e2e675 659 }
ad20f474 660 DBG3("Consumer control URI set with port %d", uri->port);
00e2e675
DG
661 break;
662 case LTTNG_STREAM_DATA:
663 dst_uri = &obj->dst.net.data;
664 obj->dst.net.data_isset = 1;
665 if (uri->port == 0) {
666 /* Assign default port. */
667 uri->port = DEFAULT_NETWORK_DATA_PORT;
a74934ba
DG
668 } else {
669 if (obj->dst.net.control_isset && uri->port ==
670 obj->dst.net.control.port) {
671 ret = -LTTNG_ERR_INVALID;
672 goto error;
673 }
00e2e675 674 }
ad20f474 675 DBG3("Consumer data URI set with port %d", uri->port);
00e2e675
DG
676 break;
677 default:
678 ERR("Set network uri type unknown %d", uri->stype);
a74934ba 679 ret = -LTTNG_ERR_INVALID;
00e2e675
DG
680 goto error;
681 }
682
683 ret = uri_compare(dst_uri, uri);
684 if (!ret) {
685 /* Same URI, don't touch it and return success. */
686 DBG3("URI network compare are the same");
ad20f474 687 goto equal;
00e2e675
DG
688 }
689
690 /* URIs were not equal, replacing it. */
691 memset(dst_uri, 0, sizeof(struct lttng_uri));
692 memcpy(dst_uri, uri, sizeof(struct lttng_uri));
693 obj->type = CONSUMER_DST_NET;
694
695 /* Handle subdir and add hostname in front. */
696 if (dst_uri->stype == LTTNG_STREAM_CONTROL) {
697 /* Get hostname to append it in the pathname */
698 ret = gethostname(hostname, sizeof(hostname));
699 if (ret < 0) {
700 PERROR("gethostname. Fallback on default localhost");
701 strncpy(hostname, "localhost", sizeof(hostname));
702 }
703 hostname[sizeof(hostname) - 1] = '\0';
704
705 /* Setup consumer subdir if none present in the control URI */
706 if (strlen(dst_uri->subdir) == 0) {
707 ret = snprintf(tmp_path, sizeof(tmp_path), "%s/%s",
708 hostname, obj->subdir);
709 } else {
710 ret = snprintf(tmp_path, sizeof(tmp_path), "%s/%s",
711 hostname, dst_uri->subdir);
712 }
713 if (ret < 0) {
714 PERROR("snprintf set consumer uri subdir");
a74934ba 715 ret = -LTTNG_ERR_NOMEM;
00e2e675
DG
716 goto error;
717 }
718
2b29c638
JD
719 if (lttng_strncpy(obj->dst.net.base_dir, tmp_path,
720 sizeof(obj->dst.net.base_dir))) {
bfc6eff0
MD
721 ret = -LTTNG_ERR_INVALID;
722 goto error;
723 }
2b29c638 724 DBG3("Consumer set network uri base_dir path %s", tmp_path);
00e2e675
DG
725 }
726
00e2e675 727 return 0;
ad20f474
DG
728equal:
729 return 1;
00e2e675 730error:
a74934ba 731 return ret;
00e2e675
DG
732}
733
734/*
735 * Send file descriptor to consumer via sock.
9a318688
JG
736 *
737 * The consumer socket lock must be held by the caller.
00e2e675 738 */
ac2f30af
JG
739int consumer_send_fds(struct consumer_socket *sock, const int *fds,
740 size_t nb_fd)
00e2e675
DG
741{
742 int ret;
743
744 assert(fds);
f50f23d9 745 assert(sock);
00e2e675 746 assert(nb_fd > 0);
05dac598 747 assert(pthread_mutex_trylock(sock->lock) == EBUSY);
00e2e675 748
9363801e 749 ret = lttcomm_send_fds_unix_sock(*sock->fd_ptr, fds, nb_fd);
00e2e675 750 if (ret < 0) {
3448e266 751 /* The above call will print a PERROR on error. */
9363801e 752 DBG("Error when sending consumer fds on sock %d", *sock->fd_ptr);
00e2e675
DG
753 goto error;
754 }
755
f50f23d9 756 ret = consumer_recv_status_reply(sock);
00e2e675
DG
757error:
758 return ret;
759}
760
ffe60014
DG
761/*
762 * Consumer send communication message structure to consumer.
9a318688
JG
763 *
764 * The consumer socket lock must be held by the caller.
ffe60014
DG
765 */
766int consumer_send_msg(struct consumer_socket *sock,
767 struct lttcomm_consumer_msg *msg)
768{
769 int ret;
770
771 assert(msg);
772 assert(sock);
211b734b 773 assert(pthread_mutex_trylock(sock->lock) == EBUSY);
ffe60014 774
52898cb1 775 ret = consumer_socket_send(sock, msg, sizeof(struct lttcomm_consumer_msg));
ffe60014 776 if (ret < 0) {
ffe60014
DG
777 goto error;
778 }
779
780 ret = consumer_recv_status_reply(sock);
781
782error:
783 return ret;
784}
785
00e2e675
DG
786/*
787 * Consumer send channel communication message structure to consumer.
9a318688
JG
788 *
789 * The consumer socket lock must be held by the caller.
00e2e675 790 */
f50f23d9
DG
791int consumer_send_channel(struct consumer_socket *sock,
792 struct lttcomm_consumer_msg *msg)
00e2e675
DG
793{
794 int ret;
795
796 assert(msg);
f50f23d9 797 assert(sock);
00e2e675 798
52898cb1 799 ret = consumer_send_msg(sock, msg);
00e2e675 800 if (ret < 0) {
00e2e675
DG
801 goto error;
802 }
803
804error:
805 return ret;
806}
807
ffe60014
DG
808/*
809 * Populate the given consumer msg structure with the ask_channel command
810 * information.
811 */
812void consumer_init_ask_channel_comm_msg(struct lttcomm_consumer_msg *msg,
813 uint64_t subbuf_size,
814 uint64_t num_subbuf,
815 int overwrite,
816 unsigned int switch_timer_interval,
817 unsigned int read_timer_interval,
ecc48a90 818 unsigned int live_timer_interval,
e9404c27 819 unsigned int monitor_timer_interval,
ffe60014
DG
820 int output,
821 int type,
822 uint64_t session_id,
823 const char *pathname,
824 const char *name,
825 uid_t uid,
826 gid_t gid,
d88aee68
DG
827 uint64_t relayd_id,
828 uint64_t key,
7972aab2 829 unsigned char *uuid,
1624d5b7
JD
830 uint32_t chan_id,
831 uint64_t tracefile_size,
2bba9e53 832 uint64_t tracefile_count,
1950109e 833 uint64_t session_id_per_pid,
567eb353 834 unsigned int monitor,
d7ba1388 835 uint32_t ust_app_uid,
491d1539 836 int64_t blocking_timeout,
3d071855 837 const char *root_shm_path,
e098433c
JG
838 const char *shm_path,
839 uint64_t trace_archive_id)
ffe60014
DG
840{
841 assert(msg);
842
843 /* Zeroed structure */
844 memset(msg, 0, sizeof(struct lttcomm_consumer_msg));
845
846 msg->cmd_type = LTTNG_CONSUMER_ASK_CHANNEL_CREATION;
847 msg->u.ask_channel.subbuf_size = subbuf_size;
848 msg->u.ask_channel.num_subbuf = num_subbuf ;
849 msg->u.ask_channel.overwrite = overwrite;
850 msg->u.ask_channel.switch_timer_interval = switch_timer_interval;
851 msg->u.ask_channel.read_timer_interval = read_timer_interval;
ecc48a90 852 msg->u.ask_channel.live_timer_interval = live_timer_interval;
e9404c27 853 msg->u.ask_channel.monitor_timer_interval = monitor_timer_interval;
ffe60014
DG
854 msg->u.ask_channel.output = output;
855 msg->u.ask_channel.type = type;
856 msg->u.ask_channel.session_id = session_id;
1950109e 857 msg->u.ask_channel.session_id_per_pid = session_id_per_pid;
ffe60014
DG
858 msg->u.ask_channel.uid = uid;
859 msg->u.ask_channel.gid = gid;
860 msg->u.ask_channel.relayd_id = relayd_id;
861 msg->u.ask_channel.key = key;
7972aab2 862 msg->u.ask_channel.chan_id = chan_id;
1624d5b7
JD
863 msg->u.ask_channel.tracefile_size = tracefile_size;
864 msg->u.ask_channel.tracefile_count = tracefile_count;
2bba9e53 865 msg->u.ask_channel.monitor = monitor;
567eb353 866 msg->u.ask_channel.ust_app_uid = ust_app_uid;
491d1539 867 msg->u.ask_channel.blocking_timeout = blocking_timeout;
e098433c 868 msg->u.ask_channel.trace_archive_id = trace_archive_id;
ffe60014
DG
869
870 memcpy(msg->u.ask_channel.uuid, uuid, sizeof(msg->u.ask_channel.uuid));
871
10a50311
JD
872 if (pathname) {
873 strncpy(msg->u.ask_channel.pathname, pathname,
874 sizeof(msg->u.ask_channel.pathname));
875 msg->u.ask_channel.pathname[sizeof(msg->u.ask_channel.pathname)-1] = '\0';
876 }
ffe60014
DG
877
878 strncpy(msg->u.ask_channel.name, name, sizeof(msg->u.ask_channel.name));
879 msg->u.ask_channel.name[sizeof(msg->u.ask_channel.name) - 1] = '\0';
d7ba1388 880
3d071855
MD
881 if (root_shm_path) {
882 strncpy(msg->u.ask_channel.root_shm_path, root_shm_path,
883 sizeof(msg->u.ask_channel.root_shm_path));
884 msg->u.ask_channel.root_shm_path[sizeof(msg->u.ask_channel.root_shm_path) - 1] = '\0';
885 }
d7ba1388
MD
886 if (shm_path) {
887 strncpy(msg->u.ask_channel.shm_path, shm_path,
888 sizeof(msg->u.ask_channel.shm_path));
889 msg->u.ask_channel.shm_path[sizeof(msg->u.ask_channel.shm_path) - 1] = '\0';
890 }
ffe60014
DG
891}
892
00e2e675
DG
893/*
894 * Init channel communication message structure.
895 */
638e7b4e 896void consumer_init_add_channel_comm_msg(struct lttcomm_consumer_msg *msg,
d88aee68 897 uint64_t channel_key,
ffe60014
DG
898 uint64_t session_id,
899 const char *pathname,
900 uid_t uid,
901 gid_t gid,
d88aee68 902 uint64_t relayd_id,
c30aaa51 903 const char *name,
ffe60014
DG
904 unsigned int nb_init_streams,
905 enum lttng_event_output output,
1624d5b7
JD
906 int type,
907 uint64_t tracefile_size,
2bba9e53 908 uint64_t tracefile_count,
ecc48a90 909 unsigned int monitor,
e9404c27
JG
910 unsigned int live_timer_interval,
911 unsigned int monitor_timer_interval)
00e2e675
DG
912{
913 assert(msg);
914
00e2e675
DG
915 /* Zeroed structure */
916 memset(msg, 0, sizeof(struct lttcomm_consumer_msg));
917
918 /* Send channel */
638e7b4e 919 msg->cmd_type = LTTNG_CONSUMER_ADD_CHANNEL;
00e2e675 920 msg->u.channel.channel_key = channel_key;
ffe60014
DG
921 msg->u.channel.session_id = session_id;
922 msg->u.channel.uid = uid;
923 msg->u.channel.gid = gid;
924 msg->u.channel.relayd_id = relayd_id;
c30aaa51 925 msg->u.channel.nb_init_streams = nb_init_streams;
ffe60014
DG
926 msg->u.channel.output = output;
927 msg->u.channel.type = type;
1624d5b7
JD
928 msg->u.channel.tracefile_size = tracefile_size;
929 msg->u.channel.tracefile_count = tracefile_count;
2bba9e53 930 msg->u.channel.monitor = monitor;
ecc48a90 931 msg->u.channel.live_timer_interval = live_timer_interval;
e9404c27 932 msg->u.channel.monitor_timer_interval = monitor_timer_interval;
ffe60014
DG
933
934 strncpy(msg->u.channel.pathname, pathname,
935 sizeof(msg->u.channel.pathname));
936 msg->u.channel.pathname[sizeof(msg->u.channel.pathname) - 1] = '\0';
937
938 strncpy(msg->u.channel.name, name, sizeof(msg->u.channel.name));
939 msg->u.channel.name[sizeof(msg->u.channel.name) - 1] = '\0';
00e2e675
DG
940}
941
942/*
943 * Init stream communication message structure.
944 */
e098433c 945void consumer_init_add_stream_comm_msg(struct lttcomm_consumer_msg *msg,
d88aee68
DG
946 uint64_t channel_key,
947 uint64_t stream_key,
e098433c
JG
948 int32_t cpu,
949 uint64_t trace_archive_id)
00e2e675
DG
950{
951 assert(msg);
952
953 memset(msg, 0, sizeof(struct lttcomm_consumer_msg));
954
e098433c 955 msg->cmd_type = LTTNG_CONSUMER_ADD_STREAM;
00e2e675
DG
956 msg->u.stream.channel_key = channel_key;
957 msg->u.stream.stream_key = stream_key;
ffe60014 958 msg->u.stream.cpu = cpu;
e098433c 959 msg->u.stream.trace_archive_id = trace_archive_id;
00e2e675
DG
960}
961
a4baae1b
JD
962void consumer_init_streams_sent_comm_msg(struct lttcomm_consumer_msg *msg,
963 enum lttng_consumer_command cmd,
964 uint64_t channel_key, uint64_t net_seq_idx)
965{
966 assert(msg);
967
968 memset(msg, 0, sizeof(struct lttcomm_consumer_msg));
969
970 msg->cmd_type = cmd;
971 msg->u.sent_streams.channel_key = channel_key;
972 msg->u.sent_streams.net_seq_idx = net_seq_idx;
973}
974
00e2e675
DG
975/*
976 * Send stream communication structure to the consumer.
977 */
f50f23d9
DG
978int consumer_send_stream(struct consumer_socket *sock,
979 struct consumer_output *dst, struct lttcomm_consumer_msg *msg,
ac2f30af 980 const int *fds, size_t nb_fd)
00e2e675
DG
981{
982 int ret;
983
984 assert(msg);
985 assert(dst);
f50f23d9 986 assert(sock);
ffe60014 987 assert(fds);
00e2e675 988
52898cb1 989 ret = consumer_send_msg(sock, msg);
f50f23d9
DG
990 if (ret < 0) {
991 goto error;
992 }
993
00e2e675
DG
994 ret = consumer_send_fds(sock, fds, nb_fd);
995 if (ret < 0) {
996 goto error;
997 }
998
999error:
1000 return ret;
1001}
37278a1e
DG
1002
1003/*
1004 * Send relayd socket to consumer associated with a session name.
1005 *
43fade62
JG
1006 * The consumer socket lock must be held by the caller.
1007 *
37278a1e
DG
1008 * On success return positive value. On error, negative value.
1009 */
f50f23d9 1010int consumer_send_relayd_socket(struct consumer_socket *consumer_sock,
6151a90f 1011 struct lttcomm_relayd_sock *rsock, struct consumer_output *consumer,
d3e2ba59
JD
1012 enum lttng_stream_type type, uint64_t session_id,
1013 char *session_name, char *hostname, int session_live_timer)
37278a1e
DG
1014{
1015 int ret;
1016 struct lttcomm_consumer_msg msg;
1017
1018 /* Code flow error. Safety net. */
6151a90f 1019 assert(rsock);
37278a1e 1020 assert(consumer);
f50f23d9 1021 assert(consumer_sock);
37278a1e 1022
53efb85a 1023 memset(&msg, 0, sizeof(msg));
37278a1e
DG
1024 /* Bail out if consumer is disabled */
1025 if (!consumer->enabled) {
f73fabfd 1026 ret = LTTNG_OK;
37278a1e
DG
1027 goto error;
1028 }
1029
d3e2ba59
JD
1030 if (type == LTTNG_STREAM_CONTROL) {
1031 ret = relayd_create_session(rsock,
1032 &msg.u.relayd_sock.relayd_session_id,
7d2f7452
DG
1033 session_name, hostname, session_live_timer,
1034 consumer->snapshot);
d3e2ba59
JD
1035 if (ret < 0) {
1036 /* Close the control socket. */
1037 (void) relayd_close(rsock);
1038 goto error;
1039 }
1040 }
1041
37278a1e
DG
1042 msg.cmd_type = LTTNG_CONSUMER_ADD_RELAYD_SOCKET;
1043 /*
1044 * Assign network consumer output index using the temporary consumer since
1045 * this call should only be made from within a set_consumer_uri() function
1046 * call in the session daemon.
1047 */
1048 msg.u.relayd_sock.net_index = consumer->net_seq_index;
1049 msg.u.relayd_sock.type = type;
46e6455f 1050 msg.u.relayd_sock.session_id = session_id;
6151a90f 1051 memcpy(&msg.u.relayd_sock.sock, rsock, sizeof(msg.u.relayd_sock.sock));
37278a1e 1052
9363801e 1053 DBG3("Sending relayd sock info to consumer on %d", *consumer_sock->fd_ptr);
52898cb1 1054 ret = consumer_send_msg(consumer_sock, &msg);
f50f23d9
DG
1055 if (ret < 0) {
1056 goto error;
1057 }
1058
37278a1e 1059 DBG3("Sending relayd socket file descriptor to consumer");
6151a90f 1060 ret = consumer_send_fds(consumer_sock, &rsock->sock.fd, 1);
37278a1e
DG
1061 if (ret < 0) {
1062 goto error;
1063 }
1064
1065 DBG2("Consumer relayd socket sent");
1066
1067error:
1068 return ret;
1069}
173af62f 1070
62c43103
JD
1071static
1072int consumer_send_pipe(struct consumer_socket *consumer_sock,
1073 enum lttng_consumer_command cmd, int pipe)
e9404c27
JG
1074{
1075 int ret;
1076 struct lttcomm_consumer_msg msg;
62c43103
JD
1077 const char *pipe_name;
1078 const char *command_name;
1079
1080 switch (cmd) {
1081 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE:
1082 pipe_name = "channel monitor";
1083 command_name = "SET_CHANNEL_MONITOR_PIPE";
1084 break;
62c43103
JD
1085 default:
1086 ERR("Unexpected command received in %s (cmd = %d)", __func__,
1087 (int) cmd);
1088 abort();
1089 }
e9404c27
JG
1090
1091 /* Code flow error. Safety net. */
1092
1093 memset(&msg, 0, sizeof(msg));
62c43103 1094 msg.cmd_type = cmd;
e9404c27 1095
3e4dc117 1096 pthread_mutex_lock(consumer_sock->lock);
62c43103 1097 DBG3("Sending %s command to consumer", command_name);
e9404c27
JG
1098 ret = consumer_send_msg(consumer_sock, &msg);
1099 if (ret < 0) {
1100 goto error;
1101 }
1102
62c43103
JD
1103 DBG3("Sending %s pipe %d to consumer on socket %d",
1104 pipe_name,
e9404c27
JG
1105 pipe, *consumer_sock->fd_ptr);
1106 ret = consumer_send_fds(consumer_sock, &pipe, 1);
1107 if (ret < 0) {
1108 goto error;
1109 }
1110
62c43103 1111 DBG2("%s pipe successfully sent", pipe_name);
e9404c27 1112error:
3e4dc117 1113 pthread_mutex_unlock(consumer_sock->lock);
e9404c27
JG
1114 return ret;
1115}
1116
62c43103
JD
1117int consumer_send_channel_monitor_pipe(struct consumer_socket *consumer_sock,
1118 int pipe)
1119{
1120 return consumer_send_pipe(consumer_sock,
1121 LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE, pipe);
1122}
1123
173af62f 1124/*
2f77fc4b
DG
1125 * Set consumer subdirectory using the session name and a generated datetime if
1126 * needed. This is appended to the current subdirectory.
173af62f 1127 */
2f77fc4b
DG
1128int consumer_set_subdir(struct consumer_output *consumer,
1129 const char *session_name)
173af62f 1130{
2f77fc4b
DG
1131 int ret = 0;
1132 unsigned int have_default_name = 0;
1133 char datetime[16], tmp_path[PATH_MAX];
1134 time_t rawtime;
1135 struct tm *timeinfo;
173af62f
DG
1136
1137 assert(consumer);
2f77fc4b
DG
1138 assert(session_name);
1139
1140 memset(tmp_path, 0, sizeof(tmp_path));
1141
1142 /* Flag if we have a default session. */
1143 if (strncmp(session_name, DEFAULT_SESSION_NAME "-",
1144 strlen(DEFAULT_SESSION_NAME) + 1) == 0) {
1145 have_default_name = 1;
1146 } else {
1147 /* Get date and time for session path */
1148 time(&rawtime);
1149 timeinfo = localtime(&rawtime);
1150 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
173af62f
DG
1151 }
1152
2f77fc4b
DG
1153 if (have_default_name) {
1154 ret = snprintf(tmp_path, sizeof(tmp_path),
1155 "%s/%s", consumer->subdir, session_name);
1156 } else {
1157 ret = snprintf(tmp_path, sizeof(tmp_path),
1158 "%s/%s-%s/", consumer->subdir, session_name, datetime);
1159 }
173af62f 1160 if (ret < 0) {
2f77fc4b 1161 PERROR("snprintf session name date");
173af62f
DG
1162 goto error;
1163 }
1164
2edf492f
MD
1165 if (lttng_strncpy(consumer->subdir, tmp_path,
1166 sizeof(consumer->subdir))) {
1167 ret = -EINVAL;
1168 goto error;
1169 }
2f77fc4b 1170 DBG2("Consumer subdir set to %s", consumer->subdir);
173af62f
DG
1171
1172error:
1173 return ret;
1174}
806e2684
DG
1175
1176/*
5e280d77
MD
1177 * Ask the consumer if the data is pending for the specific session id.
1178 * Returns 1 if data is pending, 0 otherwise, or < 0 on error.
806e2684 1179 */
d88aee68 1180int consumer_is_data_pending(uint64_t session_id,
806e2684
DG
1181 struct consumer_output *consumer)
1182{
1183 int ret;
6d805429 1184 int32_t ret_code = 0; /* Default is that the data is NOT pending */
806e2684
DG
1185 struct consumer_socket *socket;
1186 struct lttng_ht_iter iter;
1187 struct lttcomm_consumer_msg msg;
1188
1189 assert(consumer);
1190
53efb85a 1191 DBG3("Consumer data pending for id %" PRIu64, session_id);
806e2684 1192
53efb85a
MD
1193 memset(&msg, 0, sizeof(msg));
1194 msg.cmd_type = LTTNG_CONSUMER_DATA_PENDING;
d88aee68 1195 msg.u.data_pending.session_id = session_id;
806e2684 1196
c8f59ee5 1197 /* Send command for each consumer */
b82c5c4d 1198 rcu_read_lock();
806e2684
DG
1199 cds_lfht_for_each_entry(consumer->socks->ht, &iter.iter, socket,
1200 node.node) {
806e2684 1201 pthread_mutex_lock(socket->lock);
52898cb1 1202 ret = consumer_socket_send(socket, &msg, sizeof(msg));
806e2684 1203 if (ret < 0) {
806e2684 1204 pthread_mutex_unlock(socket->lock);
b82c5c4d 1205 goto error_unlock;
806e2684
DG
1206 }
1207
f50f23d9
DG
1208 /*
1209 * No need for a recv reply status because the answer to the command is
1210 * the reply status message.
1211 */
1212
52898cb1
DG
1213 ret = consumer_socket_recv(socket, &ret_code, sizeof(ret_code));
1214 if (ret < 0) {
806e2684 1215 pthread_mutex_unlock(socket->lock);
b82c5c4d 1216 goto error_unlock;
806e2684 1217 }
806e2684
DG
1218 pthread_mutex_unlock(socket->lock);
1219
6d805429 1220 if (ret_code == 1) {
806e2684
DG
1221 break;
1222 }
1223 }
b82c5c4d 1224 rcu_read_unlock();
806e2684 1225
d88aee68
DG
1226 DBG("Consumer data is %s pending for session id %" PRIu64,
1227 ret_code == 1 ? "" : "NOT", session_id);
806e2684
DG
1228 return ret_code;
1229
b82c5c4d
DG
1230error_unlock:
1231 rcu_read_unlock();
806e2684
DG
1232 return -1;
1233}
7972aab2
DG
1234
1235/*
1236 * Send a flush command to consumer using the given channel key.
1237 *
1238 * Return 0 on success else a negative value.
1239 */
1240int consumer_flush_channel(struct consumer_socket *socket, uint64_t key)
1241{
1242 int ret;
1243 struct lttcomm_consumer_msg msg;
1244
1245 assert(socket);
7972aab2
DG
1246
1247 DBG2("Consumer flush channel key %" PRIu64, key);
1248
53efb85a 1249 memset(&msg, 0, sizeof(msg));
7972aab2
DG
1250 msg.cmd_type = LTTNG_CONSUMER_FLUSH_CHANNEL;
1251 msg.u.flush_channel.key = key;
1252
1253 pthread_mutex_lock(socket->lock);
1254 health_code_update();
1255
1256 ret = consumer_send_msg(socket, &msg);
1257 if (ret < 0) {
1258 goto end;
1259 }
1260
1261end:
1262 health_code_update();
1263 pthread_mutex_unlock(socket->lock);
1264 return ret;
1265}
1266
0dd01979
MD
1267/*
1268 * Send a clear quiescent command to consumer using the given channel key.
1269 *
1270 * Return 0 on success else a negative value.
1271 */
1272int consumer_clear_quiescent_channel(struct consumer_socket *socket, uint64_t key)
1273{
1274 int ret;
1275 struct lttcomm_consumer_msg msg;
1276
1277 assert(socket);
1278
1279 DBG2("Consumer clear quiescent channel key %" PRIu64, key);
1280
1281 memset(&msg, 0, sizeof(msg));
1282 msg.cmd_type = LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL;
1283 msg.u.clear_quiescent_channel.key = key;
1284
1285 pthread_mutex_lock(socket->lock);
1286 health_code_update();
1287
1288 ret = consumer_send_msg(socket, &msg);
1289 if (ret < 0) {
1290 goto end;
1291 }
1292
1293end:
1294 health_code_update();
1295 pthread_mutex_unlock(socket->lock);
1296 return ret;
1297}
1298
7972aab2 1299/*
dc2bbdae
MD
1300 * Send a close metadata command to consumer using the given channel key.
1301 * Called with registry lock held.
7972aab2
DG
1302 *
1303 * Return 0 on success else a negative value.
1304 */
1305int consumer_close_metadata(struct consumer_socket *socket,
1306 uint64_t metadata_key)
1307{
1308 int ret;
1309 struct lttcomm_consumer_msg msg;
1310
1311 assert(socket);
7972aab2
DG
1312
1313 DBG2("Consumer close metadata channel key %" PRIu64, metadata_key);
1314
53efb85a 1315 memset(&msg, 0, sizeof(msg));
7972aab2
DG
1316 msg.cmd_type = LTTNG_CONSUMER_CLOSE_METADATA;
1317 msg.u.close_metadata.key = metadata_key;
1318
1319 pthread_mutex_lock(socket->lock);
1320 health_code_update();
1321
1322 ret = consumer_send_msg(socket, &msg);
1323 if (ret < 0) {
1324 goto end;
1325 }
1326
1327end:
1328 health_code_update();
1329 pthread_mutex_unlock(socket->lock);
1330 return ret;
1331}
1332
1333/*
1334 * Send a setup metdata command to consumer using the given channel key.
1335 *
1336 * Return 0 on success else a negative value.
1337 */
1338int consumer_setup_metadata(struct consumer_socket *socket,
1339 uint64_t metadata_key)
1340{
1341 int ret;
1342 struct lttcomm_consumer_msg msg;
1343
1344 assert(socket);
7972aab2
DG
1345
1346 DBG2("Consumer setup metadata channel key %" PRIu64, metadata_key);
1347
53efb85a 1348 memset(&msg, 0, sizeof(msg));
7972aab2
DG
1349 msg.cmd_type = LTTNG_CONSUMER_SETUP_METADATA;
1350 msg.u.setup_metadata.key = metadata_key;
1351
1352 pthread_mutex_lock(socket->lock);
1353 health_code_update();
1354
1355 ret = consumer_send_msg(socket, &msg);
1356 if (ret < 0) {
1357 goto end;
1358 }
1359
1360end:
1361 health_code_update();
1362 pthread_mutex_unlock(socket->lock);
1363 return ret;
1364}
1365
1366/*
dc2bbdae
MD
1367 * Send metadata string to consumer.
1368 * RCU read-side lock must be held to guarantee existence of socket.
7972aab2
DG
1369 *
1370 * Return 0 on success else a negative value.
1371 */
1372int consumer_push_metadata(struct consumer_socket *socket,
1373 uint64_t metadata_key, char *metadata_str, size_t len,
93ec662e 1374 size_t target_offset, uint64_t version)
7972aab2
DG
1375{
1376 int ret;
1377 struct lttcomm_consumer_msg msg;
1378
1379 assert(socket);
7972aab2 1380
9363801e 1381 DBG2("Consumer push metadata to consumer socket %d", *socket->fd_ptr);
7972aab2 1382
dc2bbdae
MD
1383 pthread_mutex_lock(socket->lock);
1384
53efb85a 1385 memset(&msg, 0, sizeof(msg));
7972aab2
DG
1386 msg.cmd_type = LTTNG_CONSUMER_PUSH_METADATA;
1387 msg.u.push_metadata.key = metadata_key;
1388 msg.u.push_metadata.target_offset = target_offset;
1389 msg.u.push_metadata.len = len;
93ec662e 1390 msg.u.push_metadata.version = version;
7972aab2 1391
7972aab2
DG
1392 health_code_update();
1393 ret = consumer_send_msg(socket, &msg);
331744e3 1394 if (ret < 0 || len == 0) {
7972aab2
DG
1395 goto end;
1396 }
1397
9363801e
DG
1398 DBG3("Consumer pushing metadata on sock %d of len %zu", *socket->fd_ptr,
1399 len);
7972aab2 1400
52898cb1 1401 ret = consumer_socket_send(socket, metadata_str, len);
7972aab2
DG
1402 if (ret < 0) {
1403 goto end;
1404 }
1405
1406 health_code_update();
1407 ret = consumer_recv_status_reply(socket);
1408 if (ret < 0) {
1409 goto end;
1410 }
1411
1412end:
dc2bbdae 1413 pthread_mutex_unlock(socket->lock);
7972aab2 1414 health_code_update();
7972aab2
DG
1415 return ret;
1416}
6dc3064a
DG
1417
1418/*
1419 * Ask the consumer to snapshot a specific channel using the key.
1420 *
9a654598 1421 * Returns LTTNG_OK on success or else an LTTng error code.
6dc3064a 1422 */
9a654598
JG
1423enum lttng_error_code consumer_snapshot_channel(struct consumer_socket *socket,
1424 uint64_t key, struct snapshot_output *output, int metadata,
1425 uid_t uid, gid_t gid, const char *session_path, int wait,
1426 uint64_t nb_packets_per_stream, uint64_t trace_archive_id)
6dc3064a
DG
1427{
1428 int ret;
9a654598 1429 enum lttng_error_code status = LTTNG_OK;
6dc3064a
DG
1430 struct lttcomm_consumer_msg msg;
1431
1432 assert(socket);
6dc3064a
DG
1433 assert(output);
1434 assert(output->consumer);
1435
1436 DBG("Consumer snapshot channel key %" PRIu64, key);
1437
ee91bab2 1438 memset(&msg, 0, sizeof(msg));
6dc3064a
DG
1439 msg.cmd_type = LTTNG_CONSUMER_SNAPSHOT_CHANNEL;
1440 msg.u.snapshot_channel.key = key;
d07ceecd 1441 msg.u.snapshot_channel.nb_packets_per_stream = nb_packets_per_stream;
6dc3064a 1442 msg.u.snapshot_channel.metadata = metadata;
e098433c 1443 msg.u.snapshot_channel.trace_archive_id = trace_archive_id;
6dc3064a
DG
1444
1445 if (output->consumer->type == CONSUMER_DST_NET) {
1446 msg.u.snapshot_channel.relayd_id = output->consumer->net_seq_index;
1447 msg.u.snapshot_channel.use_relayd = 1;
1448 ret = snprintf(msg.u.snapshot_channel.pathname,
1bfe7328 1449 sizeof(msg.u.snapshot_channel.pathname),
2b29c638
JD
1450 "%s/%s/%s-%s-%" PRIu64 "%s",
1451 output->consumer->dst.net.base_dir,
1452 output->consumer->subdir,
1453 output->name, output->datetime,
1454 output->nb_snapshot,
ee91bab2 1455 session_path);
6dc3064a 1456 if (ret < 0) {
9a654598 1457 status = LTTNG_ERR_INVALID;
6dc3064a 1458 goto error;
f0218c66
JG
1459 } else if (ret >= sizeof(msg.u.snapshot_channel.pathname)) {
1460 ERR("Snapshot path exceeds the maximal allowed length of %zu bytes (%i bytes required) with path \"%s/%s/%s-%s-%" PRIu64 "%s\"",
1461 sizeof(msg.u.snapshot_channel.pathname),
1462 ret, output->consumer->dst.net.base_dir,
1463 output->consumer->subdir,
1464 output->name, output->datetime,
1465 output->nb_snapshot,
1466 session_path);
9a654598 1467 status = LTTNG_ERR_SNAPSHOT_FAIL;
f0218c66 1468 goto error;
6dc3064a
DG
1469 }
1470 } else {
1471 ret = snprintf(msg.u.snapshot_channel.pathname,
1bfe7328 1472 sizeof(msg.u.snapshot_channel.pathname),
2b29c638
JD
1473 "%s/%s-%s-%" PRIu64 "%s",
1474 output->consumer->dst.session_root_path,
1475 output->name, output->datetime,
1476 output->nb_snapshot,
1bfe7328 1477 session_path);
6dc3064a 1478 if (ret < 0) {
9a654598 1479 status = LTTNG_ERR_NOMEM;
6dc3064a 1480 goto error;
f0218c66
JG
1481 } else if (ret >= sizeof(msg.u.snapshot_channel.pathname)) {
1482 ERR("Snapshot path exceeds the maximal allowed length of %zu bytes (%i bytes required) with path \"%s/%s-%s-%" PRIu64 "%s\"",
1483 sizeof(msg.u.snapshot_channel.pathname),
1484 ret, output->consumer->dst.session_root_path,
1485 output->name, output->datetime, output->nb_snapshot,
1486 session_path);
9a654598 1487 status = LTTNG_ERR_SNAPSHOT_FAIL;
f0218c66 1488 goto error;
6dc3064a 1489 }
f0218c66 1490
07b86b52 1491 msg.u.snapshot_channel.relayd_id = (uint64_t) -1ULL;
6dc3064a
DG
1492
1493 /* Create directory. Ignore if exist. */
1494 ret = run_as_mkdir_recursive(msg.u.snapshot_channel.pathname,
1495 S_IRWXU | S_IRWXG, uid, gid);
1496 if (ret < 0) {
df5b86c8 1497 if (errno != EEXIST) {
9a654598
JG
1498 status = LTTNG_ERR_CREATE_DIR_FAIL;
1499 PERROR("Trace directory creation error");
6dc3064a
DG
1500 goto error;
1501 }
1502 }
1503 }
1504
1505 health_code_update();
9d1103e6 1506 pthread_mutex_lock(socket->lock);
6dc3064a 1507 ret = consumer_send_msg(socket, &msg);
9d1103e6 1508 pthread_mutex_unlock(socket->lock);
6dc3064a 1509 if (ret < 0) {
9bbfb88c
MD
1510 switch (-ret) {
1511 case LTTCOMM_CONSUMERD_CHAN_NOT_FOUND:
9a654598 1512 status = LTTNG_ERR_CHAN_NOT_FOUND;
9bbfb88c
MD
1513 break;
1514 default:
9a654598 1515 status = LTTNG_ERR_SNAPSHOT_FAIL;
9bbfb88c
MD
1516 break;
1517 }
6dc3064a
DG
1518 goto error;
1519 }
1520
1521error:
1522 health_code_update();
9a654598 1523 return status;
6dc3064a 1524}
fb83fe64
JD
1525
1526/*
1527 * Ask the consumer the number of discarded events for a channel.
1528 */
1529int consumer_get_discarded_events(uint64_t session_id, uint64_t channel_key,
1530 struct consumer_output *consumer, uint64_t *discarded)
1531{
1532 int ret;
1533 struct consumer_socket *socket;
1534 struct lttng_ht_iter iter;
1535 struct lttcomm_consumer_msg msg;
1536
1537 assert(consumer);
1538
1539 DBG3("Consumer discarded events id %" PRIu64, session_id);
1540
1541 memset(&msg, 0, sizeof(msg));
1542 msg.cmd_type = LTTNG_CONSUMER_DISCARDED_EVENTS;
1543 msg.u.discarded_events.session_id = session_id;
1544 msg.u.discarded_events.channel_key = channel_key;
1545
1546 *discarded = 0;
1547
1548 /* Send command for each consumer */
1549 rcu_read_lock();
1550 cds_lfht_for_each_entry(consumer->socks->ht, &iter.iter, socket,
1551 node.node) {
1552 uint64_t consumer_discarded = 0;
1553 pthread_mutex_lock(socket->lock);
1554 ret = consumer_socket_send(socket, &msg, sizeof(msg));
1555 if (ret < 0) {
1556 pthread_mutex_unlock(socket->lock);
1557 goto end;
1558 }
1559
1560 /*
1561 * No need for a recv reply status because the answer to the
1562 * command is the reply status message.
1563 */
1564 ret = consumer_socket_recv(socket, &consumer_discarded,
1565 sizeof(consumer_discarded));
1566 if (ret < 0) {
1567 ERR("get discarded events");
1568 pthread_mutex_unlock(socket->lock);
1569 goto end;
1570 }
1571 pthread_mutex_unlock(socket->lock);
1572 *discarded += consumer_discarded;
1573 }
1574 ret = 0;
1575 DBG("Consumer discarded %" PRIu64 " events in session id %" PRIu64,
1576 *discarded, session_id);
1577
1578end:
1579 rcu_read_unlock();
1580 return ret;
1581}
1582
1583/*
1584 * Ask the consumer the number of lost packets for a channel.
1585 */
1586int consumer_get_lost_packets(uint64_t session_id, uint64_t channel_key,
1587 struct consumer_output *consumer, uint64_t *lost)
1588{
1589 int ret;
1590 struct consumer_socket *socket;
1591 struct lttng_ht_iter iter;
1592 struct lttcomm_consumer_msg msg;
1593
1594 assert(consumer);
1595
1596 DBG3("Consumer lost packets id %" PRIu64, session_id);
1597
1598 memset(&msg, 0, sizeof(msg));
1599 msg.cmd_type = LTTNG_CONSUMER_LOST_PACKETS;
1600 msg.u.lost_packets.session_id = session_id;
1601 msg.u.lost_packets.channel_key = channel_key;
1602
1603 *lost = 0;
1604
1605 /* Send command for each consumer */
1606 rcu_read_lock();
1607 cds_lfht_for_each_entry(consumer->socks->ht, &iter.iter, socket,
1608 node.node) {
1609 uint64_t consumer_lost = 0;
1610 pthread_mutex_lock(socket->lock);
1611 ret = consumer_socket_send(socket, &msg, sizeof(msg));
1612 if (ret < 0) {
1613 pthread_mutex_unlock(socket->lock);
1614 goto end;
1615 }
1616
1617 /*
1618 * No need for a recv reply status because the answer to the
1619 * command is the reply status message.
1620 */
1621 ret = consumer_socket_recv(socket, &consumer_lost,
1622 sizeof(consumer_lost));
1623 if (ret < 0) {
1624 ERR("get lost packets");
1625 pthread_mutex_unlock(socket->lock);
1626 goto end;
1627 }
1628 pthread_mutex_unlock(socket->lock);
1629 *lost += consumer_lost;
1630 }
1631 ret = 0;
1632 DBG("Consumer lost %" PRIu64 " packets in session id %" PRIu64,
1633 *lost, session_id);
1634
1635end:
1636 rcu_read_unlock();
1637 return ret;
1638}
a1ae2ea5 1639
5c408ad8
JD
1640/*
1641 * Ask the consumer to rotate a channel.
1642 * domain_path contains "/kernel" for kernel or the complete path for UST
1643 * (ex: /ust/uid/1000/64-bit);
1644 *
1645 * The new_chunk_id is the session->rotate_count that has been incremented
1646 * when the rotation started. On the relay, this allows to keep track in which
1647 * chunk each stream is currently writing to (for the rotate_pending operation).
1648 */
1649int consumer_rotate_channel(struct consumer_socket *socket, uint64_t key,
1650 uid_t uid, gid_t gid, struct consumer_output *output,
1651 char *domain_path, bool is_metadata_channel,
92816cc3 1652 uint64_t new_chunk_id)
5c408ad8
JD
1653{
1654 int ret;
1655 struct lttcomm_consumer_msg msg;
1656
1657 assert(socket);
1658
1659 DBG("Consumer rotate channel key %" PRIu64, key);
1660
1661 pthread_mutex_lock(socket->lock);
1662 memset(&msg, 0, sizeof(msg));
1663 msg.cmd_type = LTTNG_CONSUMER_ROTATE_CHANNEL;
1664 msg.u.rotate_channel.key = key;
1665 msg.u.rotate_channel.metadata = !!is_metadata_channel;
1666 msg.u.rotate_channel.new_chunk_id = new_chunk_id;
1667
1668 if (output->type == CONSUMER_DST_NET) {
1669 msg.u.rotate_channel.relayd_id = output->net_seq_index;
1670 ret = snprintf(msg.u.rotate_channel.pathname,
1671 sizeof(msg.u.rotate_channel.pathname), "%s%s%s",
1672 output->dst.net.base_dir,
1673 output->chunk_path, domain_path);
1674 if (ret < 0 || ret == sizeof(msg.u.rotate_channel.pathname)) {
1675 ERR("Failed to format channel path name when asking consumer to rotate channel");
20f37cb4 1676 ret = -LTTNG_ERR_INVALID;
5c408ad8
JD
1677 goto error;
1678 }
5c408ad8
JD
1679 } else {
1680 msg.u.rotate_channel.relayd_id = (uint64_t) -1ULL;
1681 ret = snprintf(msg.u.rotate_channel.pathname,
1682 sizeof(msg.u.rotate_channel.pathname), "%s%s%s",
1683 output->dst.session_root_path,
1684 output->chunk_path, domain_path);
1685 if (ret < 0 || ret == sizeof(msg.u.rotate_channel.pathname)) {
1686 ERR("Failed to format channel path name when asking consumer to rotate channel");
20f37cb4 1687 ret = -LTTNG_ERR_INVALID;
5c408ad8
JD
1688 goto error;
1689 }
1690 }
1691
1692 health_code_update();
1693 ret = consumer_send_msg(socket, &msg);
1694 if (ret < 0) {
20f37cb4
MD
1695 switch (-ret) {
1696 case LTTCOMM_CONSUMERD_CHAN_NOT_FOUND:
1697 ret = -LTTNG_ERR_CHAN_NOT_FOUND;
1698 break;
1699 default:
1700 ret = -LTTNG_ERR_ROTATION_FAIL_CONSUMER;
1701 break;
1702 }
5c408ad8
JD
1703 goto error;
1704 }
5c408ad8
JD
1705error:
1706 pthread_mutex_unlock(socket->lock);
1707 health_code_update();
1708 return ret;
1709}
1710
00fb02ac
JD
1711int consumer_rotate_rename(struct consumer_socket *socket, uint64_t session_id,
1712 const struct consumer_output *output, const char *old_path,
1713 const char *new_path, uid_t uid, gid_t gid)
1714{
1715 int ret;
1716 struct lttcomm_consumer_msg msg;
1717 size_t old_path_length, new_path_length;
1718
1719 assert(socket);
1720 assert(old_path);
1721 assert(new_path);
1722
1723 DBG("Consumer rotate rename session %" PRIu64 ", old path = \"%s\", new_path = \"%s\"",
1724 session_id, old_path, new_path);
1725
1726 old_path_length = strlen(old_path);
1727 if (old_path_length >= sizeof(msg.u.rotate_rename.old_path)) {
1728 ERR("consumer_rotate_rename: old path length (%zu bytes) exceeds the maximal length allowed by the consumer protocol (%zu bytes)",
1729 old_path_length + 1, sizeof(msg.u.rotate_rename.old_path));
20f37cb4 1730 ret = -LTTNG_ERR_INVALID;
00fb02ac
JD
1731 goto error;
1732 }
1733
1734 new_path_length = strlen(new_path);
1735 if (new_path_length >= sizeof(msg.u.rotate_rename.new_path)) {
1736 ERR("consumer_rotate_rename: new path length (%zu bytes) exceeds the maximal length allowed by the consumer protocol (%zu bytes)",
1737 new_path_length + 1, sizeof(msg.u.rotate_rename.new_path));
20f37cb4 1738 ret = -LTTNG_ERR_INVALID;
00fb02ac
JD
1739 goto error;
1740 }
1741
1742 memset(&msg, 0, sizeof(msg));
1743 msg.cmd_type = LTTNG_CONSUMER_ROTATE_RENAME;
1744 msg.u.rotate_rename.session_id = session_id;
1745 msg.u.rotate_rename.uid = uid;
1746 msg.u.rotate_rename.gid = gid;
1747 strcpy(msg.u.rotate_rename.old_path, old_path);
1748 strcpy(msg.u.rotate_rename.new_path, new_path);
1749
1750 if (output->type == CONSUMER_DST_NET) {
1751 msg.u.rotate_rename.relayd_id = output->net_seq_index;
1752 } else {
1753 msg.u.rotate_rename.relayd_id = -1ULL;
1754 }
1755
1756 health_code_update();
1757 ret = consumer_send_msg(socket, &msg);
1758 if (ret < 0) {
20f37cb4 1759 ret = -LTTNG_ERR_ROTATE_RENAME_FAIL_CONSUMER;
00fb02ac
JD
1760 goto error;
1761 }
1762
d88744a4
JD
1763error:
1764 health_code_update();
1765 return ret;
1766}
1767
1768/*
92816cc3
JG
1769 * Ask the consumer if a rotation is locally pending. Must be called with the
1770 * socket lock held.
1771 *
1772 * Return 1 if the rotation is still pending, 0 if finished, a negative value
1773 * on error.
1774 */
1775int consumer_check_rotation_pending_local(struct consumer_socket *socket,
1776 uint64_t session_id, uint64_t chunk_id)
1777{
1778 int ret;
1779 struct lttcomm_consumer_msg msg;
1780 uint32_t pending = 0;
1781
1782 assert(socket);
1783
1784 DBG("Asking consumer to locally check for pending rotation for session %" PRIu64 ", chunk id %" PRIu64,
1785 session_id, chunk_id);
1786
1787 memset(&msg, 0, sizeof(msg));
1788 msg.cmd_type = LTTNG_CONSUMER_CHECK_ROTATION_PENDING_LOCAL;
1789 msg.u.check_rotation_pending_local.session_id = session_id;
1790 msg.u.check_rotation_pending_local.chunk_id = chunk_id;
1791
1792 health_code_update();
1793 ret = consumer_send_msg(socket, &msg);
1794 if (ret < 0) {
20f37cb4 1795 ret = -LTTNG_ERR_ROTATION_PENDING_LOCAL_FAIL_CONSUMER;
92816cc3
JG
1796 goto error;
1797 }
1798
1799 ret = consumer_socket_recv(socket, &pending, sizeof(pending));
1800 if (ret < 0) {
1801 goto error;
1802 }
1803
1804 ret = pending;
1805
1806error:
1807 health_code_update();
1808 return ret;
1809}
1810
1811/*
1812 * Ask the consumer if a rotation is pending on the relayd. Must be called with
1813 * the socket lock held.
d88744a4
JD
1814 *
1815 * Return 1 if the rotation is still pending, 0 if finished, a negative value
1816 * on error.
1817 */
92816cc3
JG
1818int consumer_check_rotation_pending_relay(struct consumer_socket *socket,
1819 const struct consumer_output *output, uint64_t session_id,
d88744a4
JD
1820 uint64_t chunk_id)
1821{
1822 int ret;
1823 struct lttcomm_consumer_msg msg;
1824 uint32_t pending = 0;
1825
1826 assert(socket);
1827
92816cc3 1828 DBG("Asking consumer to check for pending rotation on relay for session %" PRIu64 ", chunk id %" PRIu64,
d88744a4
JD
1829 session_id, chunk_id);
1830 assert(output->type == CONSUMER_DST_NET);
1831
1832 memset(&msg, 0, sizeof(msg));
92816cc3
JG
1833 msg.cmd_type = LTTNG_CONSUMER_CHECK_ROTATION_PENDING_RELAY;
1834 msg.u.check_rotation_pending_relay.session_id = session_id;
1835 msg.u.check_rotation_pending_relay.relayd_id = output->net_seq_index;
1836 msg.u.check_rotation_pending_relay.chunk_id = chunk_id;
d88744a4
JD
1837
1838 health_code_update();
1839 ret = consumer_send_msg(socket, &msg);
1840 if (ret < 0) {
20f37cb4 1841 ret = -LTTNG_ERR_ROTATION_PENDING_RELAY_FAIL_CONSUMER;
d88744a4
JD
1842 goto error;
1843 }
1844
1845 ret = consumer_socket_recv(socket, &pending, sizeof(pending));
1846 if (ret < 0) {
1847 goto error;
1848 }
1849
1850 ret = pending;
1851
00fb02ac
JD
1852error:
1853 health_code_update();
1854 return ret;
1855}
1856
a1ae2ea5
JD
1857/*
1858 * Ask the consumer to create a directory.
1859 *
1860 * Called with the consumer socket lock held.
1861 */
1862int consumer_mkdir(struct consumer_socket *socket, uint64_t session_id,
1863 const struct consumer_output *output, const char *path,
1864 uid_t uid, gid_t gid)
1865{
1866 int ret;
1867 struct lttcomm_consumer_msg msg;
1868
1869 assert(socket);
1870
1871 DBG("Consumer mkdir %s in session %" PRIu64, path, session_id);
1872
1873 memset(&msg, 0, sizeof(msg));
1874 msg.cmd_type = LTTNG_CONSUMER_MKDIR;
1875 msg.u.mkdir.session_id = session_id;
1876 msg.u.mkdir.uid = uid;
1877 msg.u.mkdir.gid = gid;
1878 ret = snprintf(msg.u.mkdir.path, sizeof(msg.u.mkdir.path), "%s", path);
1879 if (ret < 0 || ret >= sizeof(msg.u.mkdir.path)) {
1880 ERR("Format path");
20f37cb4 1881 ret = -LTTNG_ERR_INVALID;
a1ae2ea5
JD
1882 goto error;
1883 }
1884
1885 if (output->type == CONSUMER_DST_NET) {
1886 msg.u.mkdir.relayd_id = output->net_seq_index;
1887 } else {
86f90fb1 1888 msg.u.mkdir.relayd_id = -1ULL;
a1ae2ea5
JD
1889 }
1890
1891 health_code_update();
1892 ret = consumer_send_msg(socket, &msg);
1893 if (ret < 0) {
20f37cb4 1894 ret = -LTTNG_ERR_MKDIR_FAIL_CONSUMER;
a1ae2ea5
JD
1895 goto error;
1896 }
1897
1898error:
1899 health_code_update();
1900 return ret;
1901}
This page took 0.13751 seconds and 4 git commands to generate.