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