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