Commit | Line | Data |
---|---|---|
00e2e675 DG |
1 | /* |
2 | * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms of the GNU General Public License, version 2 only, as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
11 | * more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along with | |
14 | * this program; if not, write to the Free Software Foundation, Inc., 51 | |
15 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
16 | */ | |
17 | ||
18 | #define _GNU_SOURCE | |
19 | #include <assert.h> | |
20 | #include <stdio.h> | |
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
23 | #include <sys/stat.h> | |
24 | #include <sys/types.h> | |
25 | #include <unistd.h> | |
d88aee68 | 26 | #include <inttypes.h> |
00e2e675 DG |
27 | |
28 | #include <common/common.h> | |
29 | #include <common/defaults.h> | |
30 | #include <common/uri.h> | |
31 | ||
32 | #include "consumer.h" | |
7972aab2 DG |
33 | #include "health.h" |
34 | #include "ust-app.h" | |
0b2dc8df | 35 | #include "utils.h" |
00e2e675 | 36 | |
f50f23d9 DG |
37 | /* |
38 | * Receive a reply command status message from the consumer. Consumer socket | |
39 | * lock MUST be acquired before calling this function. | |
40 | * | |
41 | * Return 0 on success, -1 on recv error or a negative lttng error code which | |
42 | * was possibly returned by the consumer. | |
43 | */ | |
44 | int consumer_recv_status_reply(struct consumer_socket *sock) | |
45 | { | |
46 | int ret; | |
47 | struct lttcomm_consumer_status_msg reply; | |
48 | ||
49 | assert(sock); | |
50 | ||
51 | ret = lttcomm_recv_unix_sock(sock->fd, &reply, sizeof(reply)); | |
a6cd2b97 DG |
52 | if (ret <= 0) { |
53 | if (ret == 0) { | |
54 | /* Orderly shutdown. Don't return 0 which means success. */ | |
55 | ret = -1; | |
56 | } | |
3448e266 DG |
57 | /* The above call will print a PERROR on error. */ |
58 | DBG("Fail to receive status reply on sock %d", sock->fd); | |
f50f23d9 DG |
59 | goto end; |
60 | } | |
61 | ||
62 | if (reply.ret_code == LTTNG_OK) { | |
63 | /* All good. */ | |
64 | ret = 0; | |
65 | } else { | |
66 | ret = -reply.ret_code; | |
ffe60014 | 67 | DBG("Consumer ret code %d", ret); |
f50f23d9 DG |
68 | } |
69 | ||
70 | end: | |
71 | return ret; | |
72 | } | |
73 | ||
ffe60014 DG |
74 | /* |
75 | * Once the ASK_CHANNEL command is sent to the consumer, the channel | |
76 | * information are sent back. This call receives that data and populates key | |
77 | * and stream_count. | |
78 | * | |
79 | * On success return 0 and both key and stream_count are set. On error, a | |
80 | * negative value is sent back and both parameters are untouched. | |
81 | */ | |
82 | int consumer_recv_status_channel(struct consumer_socket *sock, | |
d88aee68 | 83 | uint64_t *key, unsigned int *stream_count) |
ffe60014 DG |
84 | { |
85 | int ret; | |
86 | struct lttcomm_consumer_status_channel reply; | |
87 | ||
88 | assert(sock); | |
89 | assert(stream_count); | |
90 | assert(key); | |
91 | ||
92 | ret = lttcomm_recv_unix_sock(sock->fd, &reply, sizeof(reply)); | |
93 | if (ret <= 0) { | |
94 | if (ret == 0) { | |
95 | /* Orderly shutdown. Don't return 0 which means success. */ | |
96 | ret = -1; | |
97 | } | |
98 | /* The above call will print a PERROR on error. */ | |
99 | DBG("Fail to receive status reply on sock %d", sock->fd); | |
100 | goto end; | |
101 | } | |
102 | ||
103 | /* An error is possible so don't touch the key and stream_count. */ | |
104 | if (reply.ret_code != LTTNG_OK) { | |
105 | ret = -1; | |
106 | goto end; | |
107 | } | |
108 | ||
109 | *key = reply.key; | |
110 | *stream_count = reply.stream_count; | |
111 | ||
112 | end: | |
113 | return ret; | |
114 | } | |
115 | ||
2f77fc4b DG |
116 | /* |
117 | * Send destroy relayd command to consumer. | |
118 | * | |
119 | * On success return positive value. On error, negative value. | |
120 | */ | |
121 | int consumer_send_destroy_relayd(struct consumer_socket *sock, | |
122 | struct consumer_output *consumer) | |
123 | { | |
124 | int ret; | |
125 | struct lttcomm_consumer_msg msg; | |
126 | ||
127 | assert(consumer); | |
128 | assert(sock); | |
129 | ||
ffe60014 | 130 | DBG2("Sending destroy relayd command to consumer sock %d", sock->fd); |
2f77fc4b DG |
131 | |
132 | /* Bail out if consumer is disabled */ | |
133 | if (!consumer->enabled) { | |
f73fabfd | 134 | ret = LTTNG_OK; |
2f77fc4b DG |
135 | DBG3("Consumer is disabled"); |
136 | goto error; | |
137 | } | |
138 | ||
139 | msg.cmd_type = LTTNG_CONSUMER_DESTROY_RELAYD; | |
140 | msg.u.destroy_relayd.net_seq_idx = consumer->net_seq_index; | |
141 | ||
142 | pthread_mutex_lock(sock->lock); | |
143 | ret = lttcomm_send_unix_sock(sock->fd, &msg, sizeof(msg)); | |
2f77fc4b | 144 | if (ret < 0) { |
c5c45efa DG |
145 | /* Indicate that the consumer is probably closing at this point. */ |
146 | DBG("send consumer destroy relayd command"); | |
f50f23d9 | 147 | goto error_send; |
2f77fc4b DG |
148 | } |
149 | ||
f50f23d9 DG |
150 | /* Don't check the return value. The caller will do it. */ |
151 | ret = consumer_recv_status_reply(sock); | |
152 | ||
2f77fc4b DG |
153 | DBG2("Consumer send destroy relayd command done"); |
154 | ||
f50f23d9 DG |
155 | error_send: |
156 | pthread_mutex_unlock(sock->lock); | |
2f77fc4b DG |
157 | error: |
158 | return ret; | |
159 | } | |
160 | ||
161 | /* | |
162 | * For each consumer socket in the consumer output object, send a destroy | |
163 | * relayd command. | |
164 | */ | |
165 | void consumer_output_send_destroy_relayd(struct consumer_output *consumer) | |
166 | { | |
2f77fc4b DG |
167 | struct lttng_ht_iter iter; |
168 | struct consumer_socket *socket; | |
169 | ||
170 | assert(consumer); | |
171 | ||
172 | /* Destroy any relayd connection */ | |
6dc3064a | 173 | if (consumer->type == CONSUMER_DST_NET) { |
2f77fc4b DG |
174 | rcu_read_lock(); |
175 | cds_lfht_for_each_entry(consumer->socks->ht, &iter.iter, socket, | |
176 | node.node) { | |
c617c0c6 MD |
177 | int ret; |
178 | ||
2f77fc4b DG |
179 | /* Send destroy relayd command */ |
180 | ret = consumer_send_destroy_relayd(socket, consumer); | |
181 | if (ret < 0) { | |
c5c45efa | 182 | DBG("Unable to send destroy relayd command to consumer"); |
2f77fc4b DG |
183 | /* Continue since we MUST delete everything at this point. */ |
184 | } | |
185 | } | |
186 | rcu_read_unlock(); | |
187 | } | |
188 | } | |
189 | ||
a4b92340 DG |
190 | /* |
191 | * From a consumer_data structure, allocate and add a consumer socket to the | |
192 | * consumer output. | |
193 | * | |
194 | * Return 0 on success, else negative value on error | |
195 | */ | |
196 | int consumer_create_socket(struct consumer_data *data, | |
197 | struct consumer_output *output) | |
198 | { | |
199 | int ret = 0; | |
200 | struct consumer_socket *socket; | |
201 | ||
202 | assert(data); | |
203 | ||
204 | if (output == NULL || data->cmd_sock < 0) { | |
205 | /* | |
206 | * Not an error. Possible there is simply not spawned consumer or it's | |
207 | * disabled for the tracing session asking the socket. | |
208 | */ | |
209 | goto error; | |
210 | } | |
211 | ||
212 | rcu_read_lock(); | |
213 | socket = consumer_find_socket(data->cmd_sock, output); | |
214 | rcu_read_unlock(); | |
215 | if (socket == NULL) { | |
216 | socket = consumer_allocate_socket(data->cmd_sock); | |
217 | if (socket == NULL) { | |
218 | ret = -1; | |
219 | goto error; | |
220 | } | |
221 | ||
2f77fc4b | 222 | socket->registered = 0; |
a4b92340 DG |
223 | socket->lock = &data->lock; |
224 | rcu_read_lock(); | |
225 | consumer_add_socket(socket, output); | |
226 | rcu_read_unlock(); | |
227 | } | |
228 | ||
6dc3064a DG |
229 | socket->type = data->type; |
230 | ||
a4b92340 DG |
231 | DBG3("Consumer socket created (fd: %d) and added to output", |
232 | data->cmd_sock); | |
233 | ||
234 | error: | |
235 | return ret; | |
236 | } | |
237 | ||
7972aab2 DG |
238 | /* |
239 | * Return the consumer socket from the given consumer output with the right | |
240 | * bitness. On error, returns NULL. | |
241 | * | |
242 | * The caller MUST acquire a rcu read side lock and keep it until the socket | |
243 | * object reference is not needed anymore. | |
244 | */ | |
245 | struct consumer_socket *consumer_find_socket_by_bitness(int bits, | |
246 | struct consumer_output *consumer) | |
247 | { | |
248 | int consumer_fd; | |
249 | struct consumer_socket *socket = NULL; | |
250 | ||
251 | switch (bits) { | |
252 | case 64: | |
253 | consumer_fd = uatomic_read(&ust_consumerd64_fd); | |
254 | break; | |
255 | case 32: | |
256 | consumer_fd = uatomic_read(&ust_consumerd32_fd); | |
257 | break; | |
258 | default: | |
259 | assert(0); | |
260 | goto end; | |
261 | } | |
262 | ||
263 | socket = consumer_find_socket(consumer_fd, consumer); | |
264 | if (!socket) { | |
265 | ERR("Consumer socket fd %d not found in consumer obj %p", | |
266 | consumer_fd, consumer); | |
267 | } | |
268 | ||
269 | end: | |
270 | return socket; | |
271 | } | |
272 | ||
173af62f DG |
273 | /* |
274 | * Find a consumer_socket in a consumer_output hashtable. Read side lock must | |
275 | * be acquired before calling this function and across use of the | |
276 | * returned consumer_socket. | |
277 | */ | |
278 | struct consumer_socket *consumer_find_socket(int key, | |
279 | struct consumer_output *consumer) | |
280 | { | |
281 | struct lttng_ht_iter iter; | |
282 | struct lttng_ht_node_ulong *node; | |
283 | struct consumer_socket *socket = NULL; | |
284 | ||
285 | /* Negative keys are lookup failures */ | |
a4b92340 | 286 | if (key < 0 || consumer == NULL) { |
173af62f DG |
287 | return NULL; |
288 | } | |
289 | ||
290 | lttng_ht_lookup(consumer->socks, (void *)((unsigned long) key), | |
291 | &iter); | |
292 | node = lttng_ht_iter_get_node_ulong(&iter); | |
293 | if (node != NULL) { | |
294 | socket = caa_container_of(node, struct consumer_socket, node); | |
295 | } | |
296 | ||
297 | return socket; | |
298 | } | |
299 | ||
300 | /* | |
301 | * Allocate a new consumer_socket and return the pointer. | |
302 | */ | |
303 | struct consumer_socket *consumer_allocate_socket(int fd) | |
304 | { | |
305 | struct consumer_socket *socket = NULL; | |
306 | ||
307 | socket = zmalloc(sizeof(struct consumer_socket)); | |
308 | if (socket == NULL) { | |
309 | PERROR("zmalloc consumer socket"); | |
310 | goto error; | |
311 | } | |
312 | ||
313 | socket->fd = fd; | |
314 | lttng_ht_node_init_ulong(&socket->node, fd); | |
315 | ||
316 | error: | |
317 | return socket; | |
318 | } | |
319 | ||
320 | /* | |
321 | * Add consumer socket to consumer output object. Read side lock must be | |
322 | * acquired before calling this function. | |
323 | */ | |
324 | void consumer_add_socket(struct consumer_socket *sock, | |
325 | struct consumer_output *consumer) | |
326 | { | |
327 | assert(sock); | |
328 | assert(consumer); | |
329 | ||
330 | lttng_ht_add_unique_ulong(consumer->socks, &sock->node); | |
331 | } | |
332 | ||
333 | /* | |
334 | * Delte consumer socket to consumer output object. Read side lock must be | |
335 | * acquired before calling this function. | |
336 | */ | |
337 | void consumer_del_socket(struct consumer_socket *sock, | |
338 | struct consumer_output *consumer) | |
339 | { | |
340 | int ret; | |
341 | struct lttng_ht_iter iter; | |
342 | ||
343 | assert(sock); | |
344 | assert(consumer); | |
345 | ||
346 | iter.iter.node = &sock->node.node; | |
347 | ret = lttng_ht_del(consumer->socks, &iter); | |
348 | assert(!ret); | |
349 | } | |
350 | ||
351 | /* | |
352 | * RCU destroy call function. | |
353 | */ | |
354 | static void destroy_socket_rcu(struct rcu_head *head) | |
355 | { | |
356 | struct lttng_ht_node_ulong *node = | |
357 | caa_container_of(head, struct lttng_ht_node_ulong, head); | |
358 | struct consumer_socket *socket = | |
359 | caa_container_of(node, struct consumer_socket, node); | |
360 | ||
361 | free(socket); | |
362 | } | |
363 | ||
364 | /* | |
365 | * Destroy and free socket pointer in a call RCU. Read side lock must be | |
366 | * acquired before calling this function. | |
367 | */ | |
368 | void consumer_destroy_socket(struct consumer_socket *sock) | |
369 | { | |
370 | assert(sock); | |
371 | ||
372 | /* | |
373 | * We DO NOT close the file descriptor here since it is global to the | |
2f77fc4b DG |
374 | * session daemon and is closed only if the consumer dies or a custom |
375 | * consumer was registered, | |
173af62f | 376 | */ |
2f77fc4b DG |
377 | if (sock->registered) { |
378 | DBG3("Consumer socket was registered. Closing fd %d", sock->fd); | |
379 | lttcomm_close_unix_sock(sock->fd); | |
380 | } | |
173af62f DG |
381 | |
382 | call_rcu(&sock->node.head, destroy_socket_rcu); | |
383 | } | |
384 | ||
00e2e675 DG |
385 | /* |
386 | * Allocate and assign data to a consumer_output object. | |
387 | * | |
388 | * Return pointer to structure. | |
389 | */ | |
390 | struct consumer_output *consumer_create_output(enum consumer_dst_type type) | |
391 | { | |
392 | struct consumer_output *output = NULL; | |
393 | ||
394 | output = zmalloc(sizeof(struct consumer_output)); | |
395 | if (output == NULL) { | |
396 | PERROR("zmalloc consumer_output"); | |
397 | goto error; | |
398 | } | |
399 | ||
400 | /* By default, consumer output is enabled */ | |
401 | output->enabled = 1; | |
402 | output->type = type; | |
d88aee68 | 403 | output->net_seq_index = (uint64_t) -1ULL; |
173af62f DG |
404 | |
405 | output->socks = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); | |
00e2e675 DG |
406 | |
407 | error: | |
408 | return output; | |
409 | } | |
410 | ||
411 | /* | |
412 | * Delete the consumer_output object from the list and free the ptr. | |
36b588ed MD |
413 | * |
414 | * Should *NOT* be called with RCU read-side lock held. | |
00e2e675 DG |
415 | */ |
416 | void consumer_destroy_output(struct consumer_output *obj) | |
417 | { | |
418 | if (obj == NULL) { | |
419 | return; | |
420 | } | |
421 | ||
173af62f DG |
422 | if (obj->socks) { |
423 | struct lttng_ht_iter iter; | |
424 | struct consumer_socket *socket; | |
425 | ||
2f77fc4b | 426 | rcu_read_lock(); |
173af62f | 427 | cds_lfht_for_each_entry(obj->socks->ht, &iter.iter, socket, node.node) { |
2f77fc4b | 428 | consumer_del_socket(socket, obj); |
173af62f DG |
429 | consumer_destroy_socket(socket); |
430 | } | |
2f77fc4b DG |
431 | rcu_read_unlock(); |
432 | ||
433 | /* Finally destroy HT */ | |
0b2dc8df | 434 | ht_cleanup_push(obj->socks); |
00e2e675 | 435 | } |
173af62f | 436 | |
00e2e675 DG |
437 | free(obj); |
438 | } | |
439 | ||
440 | /* | |
441 | * Copy consumer output and returned the newly allocated copy. | |
36b588ed MD |
442 | * |
443 | * Should *NOT* be called with RCU read-side lock held. | |
00e2e675 DG |
444 | */ |
445 | struct consumer_output *consumer_copy_output(struct consumer_output *obj) | |
446 | { | |
6dc3064a | 447 | int ret; |
09a90bcd | 448 | struct lttng_ht *tmp_ht_ptr; |
00e2e675 DG |
449 | struct consumer_output *output; |
450 | ||
451 | assert(obj); | |
452 | ||
453 | output = consumer_create_output(obj->type); | |
454 | if (output == NULL) { | |
455 | goto error; | |
456 | } | |
09a90bcd DG |
457 | /* Avoid losing the HT reference after the memcpy() */ |
458 | tmp_ht_ptr = output->socks; | |
00e2e675 DG |
459 | |
460 | memcpy(output, obj, sizeof(struct consumer_output)); | |
461 | ||
09a90bcd DG |
462 | /* Putting back the HT pointer and start copying socket(s). */ |
463 | output->socks = tmp_ht_ptr; | |
173af62f | 464 | |
6dc3064a DG |
465 | ret = consumer_copy_sockets(output, obj); |
466 | if (ret < 0) { | |
467 | goto malloc_error; | |
468 | } | |
469 | ||
470 | error: | |
471 | return output; | |
472 | ||
473 | malloc_error: | |
474 | consumer_destroy_output(output); | |
475 | return NULL; | |
476 | } | |
477 | ||
478 | /* | |
479 | * Copy consumer sockets from src to dst. | |
480 | * | |
481 | * Return 0 on success or else a negative value. | |
482 | */ | |
483 | int consumer_copy_sockets(struct consumer_output *dst, | |
484 | struct consumer_output *src) | |
485 | { | |
486 | int ret = 0; | |
487 | struct lttng_ht_iter iter; | |
488 | struct consumer_socket *socket, *copy_sock; | |
489 | ||
490 | assert(dst); | |
491 | assert(src); | |
492 | ||
b82c5c4d | 493 | rcu_read_lock(); |
6dc3064a DG |
494 | cds_lfht_for_each_entry(src->socks->ht, &iter.iter, socket, node.node) { |
495 | /* Ignore socket that are already there. */ | |
496 | copy_sock = consumer_find_socket(socket->fd, dst); | |
497 | if (copy_sock) { | |
498 | continue; | |
499 | } | |
500 | ||
173af62f DG |
501 | /* Create new socket object. */ |
502 | copy_sock = consumer_allocate_socket(socket->fd); | |
503 | if (copy_sock == NULL) { | |
b82c5c4d | 504 | rcu_read_unlock(); |
6dc3064a DG |
505 | ret = -ENOMEM; |
506 | goto error; | |
173af62f DG |
507 | } |
508 | ||
09a90bcd | 509 | copy_sock->registered = socket->registered; |
6dc3064a DG |
510 | /* |
511 | * This is valid because this lock is shared accross all consumer | |
512 | * object being the global lock of the consumer data structure of the | |
513 | * session daemon. | |
514 | */ | |
173af62f | 515 | copy_sock->lock = socket->lock; |
6dc3064a | 516 | consumer_add_socket(copy_sock, dst); |
173af62f | 517 | } |
b82c5c4d | 518 | rcu_read_unlock(); |
173af62f | 519 | |
00e2e675 | 520 | error: |
6dc3064a | 521 | return ret; |
00e2e675 DG |
522 | } |
523 | ||
524 | /* | |
525 | * Set network URI to the consumer output object. | |
526 | * | |
ad20f474 DG |
527 | * Return 0 on success. Return 1 if the URI were equal. Else, negative value on |
528 | * error. | |
00e2e675 DG |
529 | */ |
530 | int consumer_set_network_uri(struct consumer_output *obj, | |
531 | struct lttng_uri *uri) | |
532 | { | |
533 | int ret; | |
534 | char tmp_path[PATH_MAX]; | |
535 | char hostname[HOST_NAME_MAX]; | |
536 | struct lttng_uri *dst_uri = NULL; | |
537 | ||
538 | /* Code flow error safety net. */ | |
539 | assert(obj); | |
540 | assert(uri); | |
541 | ||
542 | switch (uri->stype) { | |
543 | case LTTNG_STREAM_CONTROL: | |
544 | dst_uri = &obj->dst.net.control; | |
545 | obj->dst.net.control_isset = 1; | |
546 | if (uri->port == 0) { | |
547 | /* Assign default port. */ | |
548 | uri->port = DEFAULT_NETWORK_CONTROL_PORT; | |
a74934ba DG |
549 | } else { |
550 | if (obj->dst.net.data_isset && uri->port == | |
551 | obj->dst.net.data.port) { | |
552 | ret = -LTTNG_ERR_INVALID; | |
553 | goto error; | |
554 | } | |
00e2e675 | 555 | } |
ad20f474 | 556 | DBG3("Consumer control URI set with port %d", uri->port); |
00e2e675 DG |
557 | break; |
558 | case LTTNG_STREAM_DATA: | |
559 | dst_uri = &obj->dst.net.data; | |
560 | obj->dst.net.data_isset = 1; | |
561 | if (uri->port == 0) { | |
562 | /* Assign default port. */ | |
563 | uri->port = DEFAULT_NETWORK_DATA_PORT; | |
a74934ba DG |
564 | } else { |
565 | if (obj->dst.net.control_isset && uri->port == | |
566 | obj->dst.net.control.port) { | |
567 | ret = -LTTNG_ERR_INVALID; | |
568 | goto error; | |
569 | } | |
00e2e675 | 570 | } |
ad20f474 | 571 | DBG3("Consumer data URI set with port %d", uri->port); |
00e2e675 DG |
572 | break; |
573 | default: | |
574 | ERR("Set network uri type unknown %d", uri->stype); | |
a74934ba | 575 | ret = -LTTNG_ERR_INVALID; |
00e2e675 DG |
576 | goto error; |
577 | } | |
578 | ||
579 | ret = uri_compare(dst_uri, uri); | |
580 | if (!ret) { | |
581 | /* Same URI, don't touch it and return success. */ | |
582 | DBG3("URI network compare are the same"); | |
ad20f474 | 583 | goto equal; |
00e2e675 DG |
584 | } |
585 | ||
586 | /* URIs were not equal, replacing it. */ | |
587 | memset(dst_uri, 0, sizeof(struct lttng_uri)); | |
588 | memcpy(dst_uri, uri, sizeof(struct lttng_uri)); | |
589 | obj->type = CONSUMER_DST_NET; | |
590 | ||
591 | /* Handle subdir and add hostname in front. */ | |
592 | if (dst_uri->stype == LTTNG_STREAM_CONTROL) { | |
593 | /* Get hostname to append it in the pathname */ | |
594 | ret = gethostname(hostname, sizeof(hostname)); | |
595 | if (ret < 0) { | |
596 | PERROR("gethostname. Fallback on default localhost"); | |
597 | strncpy(hostname, "localhost", sizeof(hostname)); | |
598 | } | |
599 | hostname[sizeof(hostname) - 1] = '\0'; | |
600 | ||
601 | /* Setup consumer subdir if none present in the control URI */ | |
602 | if (strlen(dst_uri->subdir) == 0) { | |
603 | ret = snprintf(tmp_path, sizeof(tmp_path), "%s/%s", | |
604 | hostname, obj->subdir); | |
605 | } else { | |
606 | ret = snprintf(tmp_path, sizeof(tmp_path), "%s/%s", | |
607 | hostname, dst_uri->subdir); | |
608 | } | |
609 | if (ret < 0) { | |
610 | PERROR("snprintf set consumer uri subdir"); | |
a74934ba | 611 | ret = -LTTNG_ERR_NOMEM; |
00e2e675 DG |
612 | goto error; |
613 | } | |
614 | ||
615 | strncpy(obj->subdir, tmp_path, sizeof(obj->subdir)); | |
616 | DBG3("Consumer set network uri subdir path %s", tmp_path); | |
617 | } | |
618 | ||
00e2e675 | 619 | return 0; |
ad20f474 DG |
620 | equal: |
621 | return 1; | |
00e2e675 | 622 | error: |
a74934ba | 623 | return ret; |
00e2e675 DG |
624 | } |
625 | ||
626 | /* | |
627 | * Send file descriptor to consumer via sock. | |
628 | */ | |
f50f23d9 | 629 | int consumer_send_fds(struct consumer_socket *sock, int *fds, size_t nb_fd) |
00e2e675 DG |
630 | { |
631 | int ret; | |
632 | ||
633 | assert(fds); | |
f50f23d9 | 634 | assert(sock); |
00e2e675 DG |
635 | assert(nb_fd > 0); |
636 | ||
f50f23d9 | 637 | ret = lttcomm_send_fds_unix_sock(sock->fd, fds, nb_fd); |
00e2e675 | 638 | if (ret < 0) { |
3448e266 DG |
639 | /* The above call will print a PERROR on error. */ |
640 | DBG("Error when sending consumer fds on sock %d", sock->fd); | |
00e2e675 DG |
641 | goto error; |
642 | } | |
643 | ||
f50f23d9 DG |
644 | ret = consumer_recv_status_reply(sock); |
645 | ||
00e2e675 DG |
646 | error: |
647 | return ret; | |
648 | } | |
649 | ||
ffe60014 DG |
650 | /* |
651 | * Consumer send communication message structure to consumer. | |
652 | */ | |
653 | int consumer_send_msg(struct consumer_socket *sock, | |
654 | struct lttcomm_consumer_msg *msg) | |
655 | { | |
656 | int ret; | |
657 | ||
658 | assert(msg); | |
659 | assert(sock); | |
660 | assert(sock->fd >= 0); | |
661 | ||
662 | ret = lttcomm_send_unix_sock(sock->fd, msg, | |
663 | sizeof(struct lttcomm_consumer_msg)); | |
664 | if (ret < 0) { | |
665 | /* The above call will print a PERROR on error. */ | |
666 | DBG("Error when sending consumer channel on sock %d", sock->fd); | |
667 | goto error; | |
668 | } | |
669 | ||
670 | ret = consumer_recv_status_reply(sock); | |
671 | ||
672 | error: | |
673 | return ret; | |
674 | } | |
675 | ||
00e2e675 DG |
676 | /* |
677 | * Consumer send channel communication message structure to consumer. | |
678 | */ | |
f50f23d9 DG |
679 | int consumer_send_channel(struct consumer_socket *sock, |
680 | struct lttcomm_consumer_msg *msg) | |
00e2e675 DG |
681 | { |
682 | int ret; | |
683 | ||
684 | assert(msg); | |
f50f23d9 DG |
685 | assert(sock); |
686 | assert(sock->fd >= 0); | |
00e2e675 | 687 | |
f50f23d9 | 688 | ret = lttcomm_send_unix_sock(sock->fd, msg, |
00e2e675 DG |
689 | sizeof(struct lttcomm_consumer_msg)); |
690 | if (ret < 0) { | |
3448e266 DG |
691 | /* The above call will print a PERROR on error. */ |
692 | DBG("Error when sending consumer channel on sock %d", sock->fd); | |
00e2e675 DG |
693 | goto error; |
694 | } | |
695 | ||
f50f23d9 DG |
696 | ret = consumer_recv_status_reply(sock); |
697 | ||
00e2e675 DG |
698 | error: |
699 | return ret; | |
700 | } | |
701 | ||
ffe60014 DG |
702 | /* |
703 | * Populate the given consumer msg structure with the ask_channel command | |
704 | * information. | |
705 | */ | |
706 | void consumer_init_ask_channel_comm_msg(struct lttcomm_consumer_msg *msg, | |
707 | uint64_t subbuf_size, | |
708 | uint64_t num_subbuf, | |
709 | int overwrite, | |
710 | unsigned int switch_timer_interval, | |
711 | unsigned int read_timer_interval, | |
712 | int output, | |
713 | int type, | |
714 | uint64_t session_id, | |
715 | const char *pathname, | |
716 | const char *name, | |
717 | uid_t uid, | |
718 | gid_t gid, | |
d88aee68 DG |
719 | uint64_t relayd_id, |
720 | uint64_t key, | |
7972aab2 | 721 | unsigned char *uuid, |
1624d5b7 JD |
722 | uint32_t chan_id, |
723 | uint64_t tracefile_size, | |
2bba9e53 DG |
724 | uint64_t tracefile_count, |
725 | unsigned int monitor) | |
ffe60014 DG |
726 | { |
727 | assert(msg); | |
728 | ||
729 | /* Zeroed structure */ | |
730 | memset(msg, 0, sizeof(struct lttcomm_consumer_msg)); | |
731 | ||
732 | msg->cmd_type = LTTNG_CONSUMER_ASK_CHANNEL_CREATION; | |
733 | msg->u.ask_channel.subbuf_size = subbuf_size; | |
734 | msg->u.ask_channel.num_subbuf = num_subbuf ; | |
735 | msg->u.ask_channel.overwrite = overwrite; | |
736 | msg->u.ask_channel.switch_timer_interval = switch_timer_interval; | |
737 | msg->u.ask_channel.read_timer_interval = read_timer_interval; | |
738 | msg->u.ask_channel.output = output; | |
739 | msg->u.ask_channel.type = type; | |
740 | msg->u.ask_channel.session_id = session_id; | |
741 | msg->u.ask_channel.uid = uid; | |
742 | msg->u.ask_channel.gid = gid; | |
743 | msg->u.ask_channel.relayd_id = relayd_id; | |
744 | msg->u.ask_channel.key = key; | |
7972aab2 | 745 | msg->u.ask_channel.chan_id = chan_id; |
1624d5b7 JD |
746 | msg->u.ask_channel.tracefile_size = tracefile_size; |
747 | msg->u.ask_channel.tracefile_count = tracefile_count; | |
2bba9e53 | 748 | msg->u.ask_channel.monitor = monitor; |
ffe60014 DG |
749 | |
750 | memcpy(msg->u.ask_channel.uuid, uuid, sizeof(msg->u.ask_channel.uuid)); | |
751 | ||
752 | strncpy(msg->u.ask_channel.pathname, pathname, | |
753 | sizeof(msg->u.ask_channel.pathname)); | |
754 | msg->u.ask_channel.pathname[sizeof(msg->u.ask_channel.pathname)-1] = '\0'; | |
755 | ||
756 | strncpy(msg->u.ask_channel.name, name, sizeof(msg->u.ask_channel.name)); | |
757 | msg->u.ask_channel.name[sizeof(msg->u.ask_channel.name) - 1] = '\0'; | |
758 | } | |
759 | ||
00e2e675 DG |
760 | /* |
761 | * Init channel communication message structure. | |
762 | */ | |
763 | void consumer_init_channel_comm_msg(struct lttcomm_consumer_msg *msg, | |
764 | enum lttng_consumer_command cmd, | |
d88aee68 | 765 | uint64_t channel_key, |
ffe60014 DG |
766 | uint64_t session_id, |
767 | const char *pathname, | |
768 | uid_t uid, | |
769 | gid_t gid, | |
d88aee68 | 770 | uint64_t relayd_id, |
c30aaa51 | 771 | const char *name, |
ffe60014 DG |
772 | unsigned int nb_init_streams, |
773 | enum lttng_event_output output, | |
1624d5b7 JD |
774 | int type, |
775 | uint64_t tracefile_size, | |
2bba9e53 DG |
776 | uint64_t tracefile_count, |
777 | unsigned int monitor) | |
00e2e675 DG |
778 | { |
779 | assert(msg); | |
780 | ||
00e2e675 DG |
781 | /* Zeroed structure */ |
782 | memset(msg, 0, sizeof(struct lttcomm_consumer_msg)); | |
783 | ||
784 | /* Send channel */ | |
785 | msg->cmd_type = cmd; | |
786 | msg->u.channel.channel_key = channel_key; | |
ffe60014 DG |
787 | msg->u.channel.session_id = session_id; |
788 | msg->u.channel.uid = uid; | |
789 | msg->u.channel.gid = gid; | |
790 | msg->u.channel.relayd_id = relayd_id; | |
c30aaa51 | 791 | msg->u.channel.nb_init_streams = nb_init_streams; |
ffe60014 DG |
792 | msg->u.channel.output = output; |
793 | msg->u.channel.type = type; | |
1624d5b7 JD |
794 | msg->u.channel.tracefile_size = tracefile_size; |
795 | msg->u.channel.tracefile_count = tracefile_count; | |
2bba9e53 | 796 | msg->u.channel.monitor = monitor; |
ffe60014 DG |
797 | |
798 | strncpy(msg->u.channel.pathname, pathname, | |
799 | sizeof(msg->u.channel.pathname)); | |
800 | msg->u.channel.pathname[sizeof(msg->u.channel.pathname) - 1] = '\0'; | |
801 | ||
802 | strncpy(msg->u.channel.name, name, sizeof(msg->u.channel.name)); | |
803 | msg->u.channel.name[sizeof(msg->u.channel.name) - 1] = '\0'; | |
00e2e675 DG |
804 | } |
805 | ||
806 | /* | |
807 | * Init stream communication message structure. | |
808 | */ | |
809 | void consumer_init_stream_comm_msg(struct lttcomm_consumer_msg *msg, | |
810 | enum lttng_consumer_command cmd, | |
d88aee68 DG |
811 | uint64_t channel_key, |
812 | uint64_t stream_key, | |
ffe60014 | 813 | int cpu) |
00e2e675 DG |
814 | { |
815 | assert(msg); | |
816 | ||
817 | memset(msg, 0, sizeof(struct lttcomm_consumer_msg)); | |
818 | ||
00e2e675 DG |
819 | msg->cmd_type = cmd; |
820 | msg->u.stream.channel_key = channel_key; | |
821 | msg->u.stream.stream_key = stream_key; | |
ffe60014 | 822 | msg->u.stream.cpu = cpu; |
00e2e675 DG |
823 | } |
824 | ||
825 | /* | |
826 | * Send stream communication structure to the consumer. | |
827 | */ | |
f50f23d9 DG |
828 | int consumer_send_stream(struct consumer_socket *sock, |
829 | struct consumer_output *dst, struct lttcomm_consumer_msg *msg, | |
830 | int *fds, size_t nb_fd) | |
00e2e675 DG |
831 | { |
832 | int ret; | |
833 | ||
834 | assert(msg); | |
835 | assert(dst); | |
f50f23d9 | 836 | assert(sock); |
ffe60014 | 837 | assert(fds); |
00e2e675 DG |
838 | |
839 | /* Send on socket */ | |
f50f23d9 | 840 | ret = lttcomm_send_unix_sock(sock->fd, msg, |
00e2e675 DG |
841 | sizeof(struct lttcomm_consumer_msg)); |
842 | if (ret < 0) { | |
3448e266 DG |
843 | /* The above call will print a PERROR on error. */ |
844 | DBG("Error when sending consumer stream on sock %d", sock->fd); | |
00e2e675 DG |
845 | goto error; |
846 | } | |
847 | ||
f50f23d9 DG |
848 | ret = consumer_recv_status_reply(sock); |
849 | if (ret < 0) { | |
850 | goto error; | |
851 | } | |
852 | ||
00e2e675 DG |
853 | ret = consumer_send_fds(sock, fds, nb_fd); |
854 | if (ret < 0) { | |
855 | goto error; | |
856 | } | |
857 | ||
858 | error: | |
859 | return ret; | |
860 | } | |
37278a1e DG |
861 | |
862 | /* | |
863 | * Send relayd socket to consumer associated with a session name. | |
864 | * | |
865 | * On success return positive value. On error, negative value. | |
866 | */ | |
f50f23d9 | 867 | int consumer_send_relayd_socket(struct consumer_socket *consumer_sock, |
6151a90f | 868 | struct lttcomm_relayd_sock *rsock, struct consumer_output *consumer, |
d88aee68 | 869 | enum lttng_stream_type type, uint64_t session_id) |
37278a1e DG |
870 | { |
871 | int ret; | |
872 | struct lttcomm_consumer_msg msg; | |
873 | ||
874 | /* Code flow error. Safety net. */ | |
6151a90f | 875 | assert(rsock); |
37278a1e | 876 | assert(consumer); |
f50f23d9 | 877 | assert(consumer_sock); |
37278a1e DG |
878 | |
879 | /* Bail out if consumer is disabled */ | |
880 | if (!consumer->enabled) { | |
f73fabfd | 881 | ret = LTTNG_OK; |
37278a1e DG |
882 | goto error; |
883 | } | |
884 | ||
885 | msg.cmd_type = LTTNG_CONSUMER_ADD_RELAYD_SOCKET; | |
886 | /* | |
887 | * Assign network consumer output index using the temporary consumer since | |
888 | * this call should only be made from within a set_consumer_uri() function | |
889 | * call in the session daemon. | |
890 | */ | |
891 | msg.u.relayd_sock.net_index = consumer->net_seq_index; | |
892 | msg.u.relayd_sock.type = type; | |
46e6455f | 893 | msg.u.relayd_sock.session_id = session_id; |
6151a90f | 894 | memcpy(&msg.u.relayd_sock.sock, rsock, sizeof(msg.u.relayd_sock.sock)); |
37278a1e | 895 | |
f50f23d9 DG |
896 | DBG3("Sending relayd sock info to consumer on %d", consumer_sock->fd); |
897 | ret = lttcomm_send_unix_sock(consumer_sock->fd, &msg, sizeof(msg)); | |
37278a1e | 898 | if (ret < 0) { |
3448e266 | 899 | /* The above call will print a PERROR on error. */ |
6151a90f | 900 | DBG("Error when sending relayd sockets on sock %d", rsock->sock.fd); |
37278a1e DG |
901 | goto error; |
902 | } | |
903 | ||
f50f23d9 DG |
904 | ret = consumer_recv_status_reply(consumer_sock); |
905 | if (ret < 0) { | |
906 | goto error; | |
907 | } | |
908 | ||
37278a1e | 909 | DBG3("Sending relayd socket file descriptor to consumer"); |
6151a90f | 910 | ret = consumer_send_fds(consumer_sock, &rsock->sock.fd, 1); |
37278a1e DG |
911 | if (ret < 0) { |
912 | goto error; | |
913 | } | |
914 | ||
915 | DBG2("Consumer relayd socket sent"); | |
916 | ||
917 | error: | |
918 | return ret; | |
919 | } | |
173af62f DG |
920 | |
921 | /* | |
2f77fc4b DG |
922 | * Set consumer subdirectory using the session name and a generated datetime if |
923 | * needed. This is appended to the current subdirectory. | |
173af62f | 924 | */ |
2f77fc4b DG |
925 | int consumer_set_subdir(struct consumer_output *consumer, |
926 | const char *session_name) | |
173af62f | 927 | { |
2f77fc4b DG |
928 | int ret = 0; |
929 | unsigned int have_default_name = 0; | |
930 | char datetime[16], tmp_path[PATH_MAX]; | |
931 | time_t rawtime; | |
932 | struct tm *timeinfo; | |
173af62f DG |
933 | |
934 | assert(consumer); | |
2f77fc4b DG |
935 | assert(session_name); |
936 | ||
937 | memset(tmp_path, 0, sizeof(tmp_path)); | |
938 | ||
939 | /* Flag if we have a default session. */ | |
940 | if (strncmp(session_name, DEFAULT_SESSION_NAME "-", | |
941 | strlen(DEFAULT_SESSION_NAME) + 1) == 0) { | |
942 | have_default_name = 1; | |
943 | } else { | |
944 | /* Get date and time for session path */ | |
945 | time(&rawtime); | |
946 | timeinfo = localtime(&rawtime); | |
947 | strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo); | |
173af62f DG |
948 | } |
949 | ||
2f77fc4b DG |
950 | if (have_default_name) { |
951 | ret = snprintf(tmp_path, sizeof(tmp_path), | |
952 | "%s/%s", consumer->subdir, session_name); | |
953 | } else { | |
954 | ret = snprintf(tmp_path, sizeof(tmp_path), | |
955 | "%s/%s-%s/", consumer->subdir, session_name, datetime); | |
956 | } | |
173af62f | 957 | if (ret < 0) { |
2f77fc4b | 958 | PERROR("snprintf session name date"); |
173af62f DG |
959 | goto error; |
960 | } | |
961 | ||
2f77fc4b DG |
962 | strncpy(consumer->subdir, tmp_path, sizeof(consumer->subdir)); |
963 | DBG2("Consumer subdir set to %s", consumer->subdir); | |
173af62f DG |
964 | |
965 | error: | |
966 | return ret; | |
967 | } | |
806e2684 DG |
968 | |
969 | /* | |
6d805429 | 970 | * Ask the consumer if the data is ready to read (NOT pending) for the specific |
806e2684 DG |
971 | * session id. |
972 | * | |
973 | * This function has a different behavior with the consumer i.e. that it waits | |
6d805429 | 974 | * for a reply from the consumer if yes or no the data is pending. |
806e2684 | 975 | */ |
d88aee68 | 976 | int consumer_is_data_pending(uint64_t session_id, |
806e2684 DG |
977 | struct consumer_output *consumer) |
978 | { | |
979 | int ret; | |
6d805429 | 980 | int32_t ret_code = 0; /* Default is that the data is NOT pending */ |
806e2684 DG |
981 | struct consumer_socket *socket; |
982 | struct lttng_ht_iter iter; | |
983 | struct lttcomm_consumer_msg msg; | |
984 | ||
985 | assert(consumer); | |
986 | ||
6d805429 | 987 | msg.cmd_type = LTTNG_CONSUMER_DATA_PENDING; |
806e2684 | 988 | |
d88aee68 | 989 | msg.u.data_pending.session_id = session_id; |
806e2684 | 990 | |
d88aee68 | 991 | DBG3("Consumer data pending for id %" PRIu64, session_id); |
806e2684 | 992 | |
c8f59ee5 | 993 | /* Send command for each consumer */ |
b82c5c4d | 994 | rcu_read_lock(); |
806e2684 DG |
995 | cds_lfht_for_each_entry(consumer->socks->ht, &iter.iter, socket, |
996 | node.node) { | |
997 | /* Code flow error */ | |
998 | assert(socket->fd >= 0); | |
999 | ||
1000 | pthread_mutex_lock(socket->lock); | |
1001 | ||
1002 | ret = lttcomm_send_unix_sock(socket->fd, &msg, sizeof(msg)); | |
1003 | if (ret < 0) { | |
3448e266 DG |
1004 | /* The above call will print a PERROR on error. */ |
1005 | DBG("Error on consumer is data pending on sock %d", socket->fd); | |
806e2684 | 1006 | pthread_mutex_unlock(socket->lock); |
b82c5c4d | 1007 | goto error_unlock; |
806e2684 DG |
1008 | } |
1009 | ||
f50f23d9 DG |
1010 | /* |
1011 | * No need for a recv reply status because the answer to the command is | |
1012 | * the reply status message. | |
1013 | */ | |
1014 | ||
806e2684 | 1015 | ret = lttcomm_recv_unix_sock(socket->fd, &ret_code, sizeof(ret_code)); |
a6cd2b97 DG |
1016 | if (ret <= 0) { |
1017 | if (ret == 0) { | |
1018 | /* Orderly shutdown. Don't return 0 which means success. */ | |
1019 | ret = -1; | |
1020 | } | |
3448e266 DG |
1021 | /* The above call will print a PERROR on error. */ |
1022 | DBG("Error on recv consumer is data pending on sock %d", socket->fd); | |
806e2684 | 1023 | pthread_mutex_unlock(socket->lock); |
b82c5c4d | 1024 | goto error_unlock; |
806e2684 DG |
1025 | } |
1026 | ||
1027 | pthread_mutex_unlock(socket->lock); | |
1028 | ||
6d805429 | 1029 | if (ret_code == 1) { |
806e2684 DG |
1030 | break; |
1031 | } | |
1032 | } | |
b82c5c4d | 1033 | rcu_read_unlock(); |
806e2684 | 1034 | |
d88aee68 DG |
1035 | DBG("Consumer data is %s pending for session id %" PRIu64, |
1036 | ret_code == 1 ? "" : "NOT", session_id); | |
806e2684 DG |
1037 | return ret_code; |
1038 | ||
b82c5c4d DG |
1039 | error_unlock: |
1040 | rcu_read_unlock(); | |
806e2684 DG |
1041 | return -1; |
1042 | } | |
7972aab2 DG |
1043 | |
1044 | /* | |
1045 | * Send a flush command to consumer using the given channel key. | |
1046 | * | |
1047 | * Return 0 on success else a negative value. | |
1048 | */ | |
1049 | int consumer_flush_channel(struct consumer_socket *socket, uint64_t key) | |
1050 | { | |
1051 | int ret; | |
1052 | struct lttcomm_consumer_msg msg; | |
1053 | ||
1054 | assert(socket); | |
1055 | assert(socket->fd >= 0); | |
1056 | ||
1057 | DBG2("Consumer flush channel key %" PRIu64, key); | |
1058 | ||
1059 | msg.cmd_type = LTTNG_CONSUMER_FLUSH_CHANNEL; | |
1060 | msg.u.flush_channel.key = key; | |
1061 | ||
1062 | pthread_mutex_lock(socket->lock); | |
1063 | health_code_update(); | |
1064 | ||
1065 | ret = consumer_send_msg(socket, &msg); | |
1066 | if (ret < 0) { | |
1067 | goto end; | |
1068 | } | |
1069 | ||
1070 | end: | |
1071 | health_code_update(); | |
1072 | pthread_mutex_unlock(socket->lock); | |
1073 | return ret; | |
1074 | } | |
1075 | ||
1076 | /* | |
1077 | * Send a close metdata command to consumer using the given channel key. | |
1078 | * | |
1079 | * Return 0 on success else a negative value. | |
1080 | */ | |
1081 | int consumer_close_metadata(struct consumer_socket *socket, | |
1082 | uint64_t metadata_key) | |
1083 | { | |
1084 | int ret; | |
1085 | struct lttcomm_consumer_msg msg; | |
1086 | ||
1087 | assert(socket); | |
1088 | assert(socket->fd >= 0); | |
1089 | ||
1090 | DBG2("Consumer close metadata channel key %" PRIu64, metadata_key); | |
1091 | ||
1092 | msg.cmd_type = LTTNG_CONSUMER_CLOSE_METADATA; | |
1093 | msg.u.close_metadata.key = metadata_key; | |
1094 | ||
1095 | pthread_mutex_lock(socket->lock); | |
1096 | health_code_update(); | |
1097 | ||
1098 | ret = consumer_send_msg(socket, &msg); | |
1099 | if (ret < 0) { | |
1100 | goto end; | |
1101 | } | |
1102 | ||
1103 | end: | |
1104 | health_code_update(); | |
1105 | pthread_mutex_unlock(socket->lock); | |
1106 | return ret; | |
1107 | } | |
1108 | ||
1109 | /* | |
1110 | * Send a setup metdata command to consumer using the given channel key. | |
1111 | * | |
1112 | * Return 0 on success else a negative value. | |
1113 | */ | |
1114 | int consumer_setup_metadata(struct consumer_socket *socket, | |
1115 | uint64_t metadata_key) | |
1116 | { | |
1117 | int ret; | |
1118 | struct lttcomm_consumer_msg msg; | |
1119 | ||
1120 | assert(socket); | |
1121 | assert(socket->fd >= 0); | |
1122 | ||
1123 | DBG2("Consumer setup metadata channel key %" PRIu64, metadata_key); | |
1124 | ||
1125 | msg.cmd_type = LTTNG_CONSUMER_SETUP_METADATA; | |
1126 | msg.u.setup_metadata.key = metadata_key; | |
1127 | ||
1128 | pthread_mutex_lock(socket->lock); | |
1129 | health_code_update(); | |
1130 | ||
1131 | ret = consumer_send_msg(socket, &msg); | |
1132 | if (ret < 0) { | |
1133 | goto end; | |
1134 | } | |
1135 | ||
1136 | end: | |
1137 | health_code_update(); | |
1138 | pthread_mutex_unlock(socket->lock); | |
1139 | return ret; | |
1140 | } | |
1141 | ||
1142 | /* | |
331744e3 | 1143 | * Send metadata string to consumer. Socket lock MUST be acquired. |
7972aab2 DG |
1144 | * |
1145 | * Return 0 on success else a negative value. | |
1146 | */ | |
1147 | int consumer_push_metadata(struct consumer_socket *socket, | |
1148 | uint64_t metadata_key, char *metadata_str, size_t len, | |
1149 | size_t target_offset) | |
1150 | { | |
1151 | int ret; | |
1152 | struct lttcomm_consumer_msg msg; | |
1153 | ||
1154 | assert(socket); | |
1155 | assert(socket->fd >= 0); | |
1156 | ||
1157 | DBG2("Consumer push metadata to consumer socket %d", socket->fd); | |
1158 | ||
1159 | msg.cmd_type = LTTNG_CONSUMER_PUSH_METADATA; | |
1160 | msg.u.push_metadata.key = metadata_key; | |
1161 | msg.u.push_metadata.target_offset = target_offset; | |
1162 | msg.u.push_metadata.len = len; | |
1163 | ||
7972aab2 DG |
1164 | health_code_update(); |
1165 | ret = consumer_send_msg(socket, &msg); | |
331744e3 | 1166 | if (ret < 0 || len == 0) { |
7972aab2 DG |
1167 | goto end; |
1168 | } | |
1169 | ||
8fd623e0 | 1170 | DBG3("Consumer pushing metadata on sock %d of len %zu", socket->fd, len); |
7972aab2 DG |
1171 | |
1172 | ret = lttcomm_send_unix_sock(socket->fd, metadata_str, len); | |
1173 | if (ret < 0) { | |
1174 | goto end; | |
1175 | } | |
1176 | ||
1177 | health_code_update(); | |
1178 | ret = consumer_recv_status_reply(socket); | |
1179 | if (ret < 0) { | |
1180 | goto end; | |
1181 | } | |
1182 | ||
1183 | end: | |
1184 | health_code_update(); | |
7972aab2 DG |
1185 | return ret; |
1186 | } | |
6dc3064a DG |
1187 | |
1188 | /* | |
1189 | * Ask the consumer to snapshot a specific channel using the key. | |
1190 | * | |
1191 | * Return 0 on success or else a negative error. | |
1192 | */ | |
1193 | int consumer_snapshot_channel(struct consumer_socket *socket, uint64_t key, | |
1194 | struct snapshot_output *output, int metadata, uid_t uid, gid_t gid, | |
1195 | int wait) | |
1196 | { | |
1197 | int ret; | |
1198 | struct lttcomm_consumer_msg msg; | |
1199 | ||
1200 | assert(socket); | |
1201 | assert(socket->fd >= 0); | |
1202 | assert(output); | |
1203 | assert(output->consumer); | |
1204 | ||
1205 | DBG("Consumer snapshot channel key %" PRIu64, key); | |
1206 | ||
1207 | memset(&msg, 0, sizeof(msg)); | |
1208 | ||
1209 | msg.cmd_type = LTTNG_CONSUMER_SNAPSHOT_CHANNEL; | |
1210 | msg.u.snapshot_channel.key = key; | |
1211 | msg.u.snapshot_channel.max_size = output->max_size; | |
1212 | msg.u.snapshot_channel.metadata = metadata; | |
1213 | ||
1214 | if (output->consumer->type == CONSUMER_DST_NET) { | |
1215 | msg.u.snapshot_channel.relayd_id = output->consumer->net_seq_index; | |
1216 | msg.u.snapshot_channel.use_relayd = 1; | |
1217 | ret = snprintf(msg.u.snapshot_channel.pathname, | |
1218 | sizeof(msg.u.snapshot_channel.pathname), "%s/%s", | |
1219 | output->consumer->subdir, DEFAULT_SNAPSHOT_NAME); | |
1220 | if (ret < 0) { | |
1221 | ret = -LTTNG_ERR_NOMEM; | |
1222 | goto error; | |
1223 | } | |
1224 | } else { | |
1225 | ret = snprintf(msg.u.snapshot_channel.pathname, | |
1226 | sizeof(msg.u.snapshot_channel.pathname), "%s/%s", | |
1227 | output->consumer->dst.trace_path, DEFAULT_SNAPSHOT_NAME); | |
1228 | if (ret < 0) { | |
1229 | ret = -LTTNG_ERR_NOMEM; | |
1230 | goto error; | |
1231 | } | |
07b86b52 | 1232 | msg.u.snapshot_channel.relayd_id = (uint64_t) -1ULL; |
6dc3064a DG |
1233 | |
1234 | /* Create directory. Ignore if exist. */ | |
1235 | ret = run_as_mkdir_recursive(msg.u.snapshot_channel.pathname, | |
1236 | S_IRWXU | S_IRWXG, uid, gid); | |
1237 | if (ret < 0) { | |
1238 | if (ret != -EEXIST) { | |
1239 | ERR("Trace directory creation error"); | |
1240 | goto error; | |
1241 | } | |
1242 | } | |
1243 | } | |
1244 | ||
1245 | health_code_update(); | |
1246 | ret = consumer_send_msg(socket, &msg); | |
1247 | if (ret < 0) { | |
1248 | goto error; | |
1249 | } | |
1250 | ||
1251 | error: | |
1252 | health_code_update(); | |
1253 | return ret; | |
1254 | } |