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> | |
26 | ||
27 | #include <common/common.h> | |
28 | #include <common/defaults.h> | |
29 | #include <common/uri.h> | |
30 | ||
31 | #include "consumer.h" | |
32 | ||
f50f23d9 DG |
33 | /* |
34 | * Receive a reply command status message from the consumer. Consumer socket | |
35 | * lock MUST be acquired before calling this function. | |
36 | * | |
37 | * Return 0 on success, -1 on recv error or a negative lttng error code which | |
38 | * was possibly returned by the consumer. | |
39 | */ | |
40 | int consumer_recv_status_reply(struct consumer_socket *sock) | |
41 | { | |
42 | int ret; | |
43 | struct lttcomm_consumer_status_msg reply; | |
44 | ||
45 | assert(sock); | |
46 | ||
47 | ret = lttcomm_recv_unix_sock(sock->fd, &reply, sizeof(reply)); | |
a6cd2b97 DG |
48 | if (ret <= 0) { |
49 | if (ret == 0) { | |
50 | /* Orderly shutdown. Don't return 0 which means success. */ | |
51 | ret = -1; | |
52 | } | |
3448e266 DG |
53 | /* The above call will print a PERROR on error. */ |
54 | DBG("Fail to receive status reply on sock %d", sock->fd); | |
f50f23d9 DG |
55 | goto end; |
56 | } | |
57 | ||
58 | if (reply.ret_code == LTTNG_OK) { | |
59 | /* All good. */ | |
60 | ret = 0; | |
61 | } else { | |
62 | ret = -reply.ret_code; | |
3448e266 | 63 | DBG("Consumer ret code %d", reply.ret_code); |
f50f23d9 DG |
64 | } |
65 | ||
66 | end: | |
67 | return ret; | |
68 | } | |
69 | ||
2f77fc4b DG |
70 | /* |
71 | * Send destroy relayd command to consumer. | |
72 | * | |
73 | * On success return positive value. On error, negative value. | |
74 | */ | |
75 | int consumer_send_destroy_relayd(struct consumer_socket *sock, | |
76 | struct consumer_output *consumer) | |
77 | { | |
78 | int ret; | |
79 | struct lttcomm_consumer_msg msg; | |
80 | ||
81 | assert(consumer); | |
82 | assert(sock); | |
83 | ||
84 | DBG2("Sending destroy relayd command to consumer..."); | |
85 | ||
86 | /* Bail out if consumer is disabled */ | |
87 | if (!consumer->enabled) { | |
f73fabfd | 88 | ret = LTTNG_OK; |
2f77fc4b DG |
89 | DBG3("Consumer is disabled"); |
90 | goto error; | |
91 | } | |
92 | ||
93 | msg.cmd_type = LTTNG_CONSUMER_DESTROY_RELAYD; | |
94 | msg.u.destroy_relayd.net_seq_idx = consumer->net_seq_index; | |
95 | ||
96 | pthread_mutex_lock(sock->lock); | |
97 | ret = lttcomm_send_unix_sock(sock->fd, &msg, sizeof(msg)); | |
2f77fc4b | 98 | if (ret < 0) { |
c5c45efa DG |
99 | /* Indicate that the consumer is probably closing at this point. */ |
100 | DBG("send consumer destroy relayd command"); | |
f50f23d9 | 101 | goto error_send; |
2f77fc4b DG |
102 | } |
103 | ||
f50f23d9 DG |
104 | /* Don't check the return value. The caller will do it. */ |
105 | ret = consumer_recv_status_reply(sock); | |
106 | ||
2f77fc4b DG |
107 | DBG2("Consumer send destroy relayd command done"); |
108 | ||
f50f23d9 DG |
109 | error_send: |
110 | pthread_mutex_unlock(sock->lock); | |
2f77fc4b DG |
111 | error: |
112 | return ret; | |
113 | } | |
114 | ||
115 | /* | |
116 | * For each consumer socket in the consumer output object, send a destroy | |
117 | * relayd command. | |
118 | */ | |
119 | void consumer_output_send_destroy_relayd(struct consumer_output *consumer) | |
120 | { | |
2f77fc4b DG |
121 | struct lttng_ht_iter iter; |
122 | struct consumer_socket *socket; | |
123 | ||
124 | assert(consumer); | |
125 | ||
126 | /* Destroy any relayd connection */ | |
127 | if (consumer && consumer->type == CONSUMER_DST_NET) { | |
128 | rcu_read_lock(); | |
129 | cds_lfht_for_each_entry(consumer->socks->ht, &iter.iter, socket, | |
130 | node.node) { | |
c617c0c6 MD |
131 | int ret; |
132 | ||
2f77fc4b DG |
133 | /* Send destroy relayd command */ |
134 | ret = consumer_send_destroy_relayd(socket, consumer); | |
135 | if (ret < 0) { | |
c5c45efa | 136 | DBG("Unable to send destroy relayd command to consumer"); |
2f77fc4b DG |
137 | /* Continue since we MUST delete everything at this point. */ |
138 | } | |
139 | } | |
140 | rcu_read_unlock(); | |
141 | } | |
142 | } | |
143 | ||
a4b92340 DG |
144 | /* |
145 | * From a consumer_data structure, allocate and add a consumer socket to the | |
146 | * consumer output. | |
147 | * | |
148 | * Return 0 on success, else negative value on error | |
149 | */ | |
150 | int consumer_create_socket(struct consumer_data *data, | |
151 | struct consumer_output *output) | |
152 | { | |
153 | int ret = 0; | |
154 | struct consumer_socket *socket; | |
155 | ||
156 | assert(data); | |
157 | ||
158 | if (output == NULL || data->cmd_sock < 0) { | |
159 | /* | |
160 | * Not an error. Possible there is simply not spawned consumer or it's | |
161 | * disabled for the tracing session asking the socket. | |
162 | */ | |
163 | goto error; | |
164 | } | |
165 | ||
166 | rcu_read_lock(); | |
167 | socket = consumer_find_socket(data->cmd_sock, output); | |
168 | rcu_read_unlock(); | |
169 | if (socket == NULL) { | |
170 | socket = consumer_allocate_socket(data->cmd_sock); | |
171 | if (socket == NULL) { | |
172 | ret = -1; | |
173 | goto error; | |
174 | } | |
175 | ||
2f77fc4b | 176 | socket->registered = 0; |
a4b92340 DG |
177 | socket->lock = &data->lock; |
178 | rcu_read_lock(); | |
179 | consumer_add_socket(socket, output); | |
180 | rcu_read_unlock(); | |
181 | } | |
182 | ||
183 | DBG3("Consumer socket created (fd: %d) and added to output", | |
184 | data->cmd_sock); | |
185 | ||
186 | error: | |
187 | return ret; | |
188 | } | |
189 | ||
173af62f DG |
190 | /* |
191 | * Find a consumer_socket in a consumer_output hashtable. Read side lock must | |
192 | * be acquired before calling this function and across use of the | |
193 | * returned consumer_socket. | |
194 | */ | |
195 | struct consumer_socket *consumer_find_socket(int key, | |
196 | struct consumer_output *consumer) | |
197 | { | |
198 | struct lttng_ht_iter iter; | |
199 | struct lttng_ht_node_ulong *node; | |
200 | struct consumer_socket *socket = NULL; | |
201 | ||
202 | /* Negative keys are lookup failures */ | |
a4b92340 | 203 | if (key < 0 || consumer == NULL) { |
173af62f DG |
204 | return NULL; |
205 | } | |
206 | ||
207 | lttng_ht_lookup(consumer->socks, (void *)((unsigned long) key), | |
208 | &iter); | |
209 | node = lttng_ht_iter_get_node_ulong(&iter); | |
210 | if (node != NULL) { | |
211 | socket = caa_container_of(node, struct consumer_socket, node); | |
212 | } | |
213 | ||
214 | return socket; | |
215 | } | |
216 | ||
217 | /* | |
218 | * Allocate a new consumer_socket and return the pointer. | |
219 | */ | |
220 | struct consumer_socket *consumer_allocate_socket(int fd) | |
221 | { | |
222 | struct consumer_socket *socket = NULL; | |
223 | ||
224 | socket = zmalloc(sizeof(struct consumer_socket)); | |
225 | if (socket == NULL) { | |
226 | PERROR("zmalloc consumer socket"); | |
227 | goto error; | |
228 | } | |
229 | ||
230 | socket->fd = fd; | |
231 | lttng_ht_node_init_ulong(&socket->node, fd); | |
232 | ||
233 | error: | |
234 | return socket; | |
235 | } | |
236 | ||
237 | /* | |
238 | * Add consumer socket to consumer output object. Read side lock must be | |
239 | * acquired before calling this function. | |
240 | */ | |
241 | void consumer_add_socket(struct consumer_socket *sock, | |
242 | struct consumer_output *consumer) | |
243 | { | |
244 | assert(sock); | |
245 | assert(consumer); | |
246 | ||
247 | lttng_ht_add_unique_ulong(consumer->socks, &sock->node); | |
248 | } | |
249 | ||
250 | /* | |
251 | * Delte consumer socket to consumer output object. Read side lock must be | |
252 | * acquired before calling this function. | |
253 | */ | |
254 | void consumer_del_socket(struct consumer_socket *sock, | |
255 | struct consumer_output *consumer) | |
256 | { | |
257 | int ret; | |
258 | struct lttng_ht_iter iter; | |
259 | ||
260 | assert(sock); | |
261 | assert(consumer); | |
262 | ||
263 | iter.iter.node = &sock->node.node; | |
264 | ret = lttng_ht_del(consumer->socks, &iter); | |
265 | assert(!ret); | |
266 | } | |
267 | ||
268 | /* | |
269 | * RCU destroy call function. | |
270 | */ | |
271 | static void destroy_socket_rcu(struct rcu_head *head) | |
272 | { | |
273 | struct lttng_ht_node_ulong *node = | |
274 | caa_container_of(head, struct lttng_ht_node_ulong, head); | |
275 | struct consumer_socket *socket = | |
276 | caa_container_of(node, struct consumer_socket, node); | |
277 | ||
278 | free(socket); | |
279 | } | |
280 | ||
281 | /* | |
282 | * Destroy and free socket pointer in a call RCU. Read side lock must be | |
283 | * acquired before calling this function. | |
284 | */ | |
285 | void consumer_destroy_socket(struct consumer_socket *sock) | |
286 | { | |
287 | assert(sock); | |
288 | ||
289 | /* | |
290 | * We DO NOT close the file descriptor here since it is global to the | |
2f77fc4b DG |
291 | * session daemon and is closed only if the consumer dies or a custom |
292 | * consumer was registered, | |
173af62f | 293 | */ |
2f77fc4b DG |
294 | if (sock->registered) { |
295 | DBG3("Consumer socket was registered. Closing fd %d", sock->fd); | |
296 | lttcomm_close_unix_sock(sock->fd); | |
297 | } | |
173af62f DG |
298 | |
299 | call_rcu(&sock->node.head, destroy_socket_rcu); | |
300 | } | |
301 | ||
00e2e675 DG |
302 | /* |
303 | * Allocate and assign data to a consumer_output object. | |
304 | * | |
305 | * Return pointer to structure. | |
306 | */ | |
307 | struct consumer_output *consumer_create_output(enum consumer_dst_type type) | |
308 | { | |
309 | struct consumer_output *output = NULL; | |
310 | ||
311 | output = zmalloc(sizeof(struct consumer_output)); | |
312 | if (output == NULL) { | |
313 | PERROR("zmalloc consumer_output"); | |
314 | goto error; | |
315 | } | |
316 | ||
317 | /* By default, consumer output is enabled */ | |
318 | output->enabled = 1; | |
319 | output->type = type; | |
320 | output->net_seq_index = -1; | |
173af62f DG |
321 | |
322 | output->socks = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); | |
00e2e675 DG |
323 | |
324 | error: | |
325 | return output; | |
326 | } | |
327 | ||
328 | /* | |
329 | * Delete the consumer_output object from the list and free the ptr. | |
330 | */ | |
331 | void consumer_destroy_output(struct consumer_output *obj) | |
332 | { | |
333 | if (obj == NULL) { | |
334 | return; | |
335 | } | |
336 | ||
173af62f DG |
337 | if (obj->socks) { |
338 | struct lttng_ht_iter iter; | |
339 | struct consumer_socket *socket; | |
340 | ||
2f77fc4b | 341 | rcu_read_lock(); |
173af62f | 342 | cds_lfht_for_each_entry(obj->socks->ht, &iter.iter, socket, node.node) { |
2f77fc4b | 343 | consumer_del_socket(socket, obj); |
173af62f DG |
344 | consumer_destroy_socket(socket); |
345 | } | |
2f77fc4b DG |
346 | rcu_read_unlock(); |
347 | ||
348 | /* Finally destroy HT */ | |
349 | lttng_ht_destroy(obj->socks); | |
00e2e675 | 350 | } |
173af62f | 351 | |
00e2e675 DG |
352 | free(obj); |
353 | } | |
354 | ||
355 | /* | |
356 | * Copy consumer output and returned the newly allocated copy. | |
357 | */ | |
358 | struct consumer_output *consumer_copy_output(struct consumer_output *obj) | |
359 | { | |
09a90bcd | 360 | struct lttng_ht *tmp_ht_ptr; |
173af62f DG |
361 | struct lttng_ht_iter iter; |
362 | struct consumer_socket *socket, *copy_sock; | |
00e2e675 DG |
363 | struct consumer_output *output; |
364 | ||
365 | assert(obj); | |
366 | ||
367 | output = consumer_create_output(obj->type); | |
368 | if (output == NULL) { | |
369 | goto error; | |
370 | } | |
09a90bcd DG |
371 | /* Avoid losing the HT reference after the memcpy() */ |
372 | tmp_ht_ptr = output->socks; | |
00e2e675 DG |
373 | |
374 | memcpy(output, obj, sizeof(struct consumer_output)); | |
375 | ||
09a90bcd DG |
376 | /* Putting back the HT pointer and start copying socket(s). */ |
377 | output->socks = tmp_ht_ptr; | |
173af62f | 378 | |
b82c5c4d | 379 | rcu_read_lock(); |
173af62f DG |
380 | cds_lfht_for_each_entry(obj->socks->ht, &iter.iter, socket, node.node) { |
381 | /* Create new socket object. */ | |
382 | copy_sock = consumer_allocate_socket(socket->fd); | |
383 | if (copy_sock == NULL) { | |
b82c5c4d | 384 | rcu_read_unlock(); |
173af62f DG |
385 | goto malloc_error; |
386 | } | |
387 | ||
09a90bcd | 388 | copy_sock->registered = socket->registered; |
173af62f DG |
389 | copy_sock->lock = socket->lock; |
390 | consumer_add_socket(copy_sock, output); | |
391 | } | |
b82c5c4d | 392 | rcu_read_unlock(); |
173af62f | 393 | |
00e2e675 DG |
394 | error: |
395 | return output; | |
173af62f DG |
396 | |
397 | malloc_error: | |
398 | consumer_destroy_output(output); | |
399 | return NULL; | |
00e2e675 DG |
400 | } |
401 | ||
402 | /* | |
403 | * Set network URI to the consumer output object. | |
404 | * | |
ad20f474 DG |
405 | * Return 0 on success. Return 1 if the URI were equal. Else, negative value on |
406 | * error. | |
00e2e675 DG |
407 | */ |
408 | int consumer_set_network_uri(struct consumer_output *obj, | |
409 | struct lttng_uri *uri) | |
410 | { | |
411 | int ret; | |
412 | char tmp_path[PATH_MAX]; | |
413 | char hostname[HOST_NAME_MAX]; | |
414 | struct lttng_uri *dst_uri = NULL; | |
415 | ||
416 | /* Code flow error safety net. */ | |
417 | assert(obj); | |
418 | assert(uri); | |
419 | ||
420 | switch (uri->stype) { | |
421 | case LTTNG_STREAM_CONTROL: | |
422 | dst_uri = &obj->dst.net.control; | |
423 | obj->dst.net.control_isset = 1; | |
424 | if (uri->port == 0) { | |
425 | /* Assign default port. */ | |
426 | uri->port = DEFAULT_NETWORK_CONTROL_PORT; | |
427 | } | |
ad20f474 | 428 | DBG3("Consumer control URI set with port %d", uri->port); |
00e2e675 DG |
429 | break; |
430 | case LTTNG_STREAM_DATA: | |
431 | dst_uri = &obj->dst.net.data; | |
432 | obj->dst.net.data_isset = 1; | |
433 | if (uri->port == 0) { | |
434 | /* Assign default port. */ | |
435 | uri->port = DEFAULT_NETWORK_DATA_PORT; | |
436 | } | |
ad20f474 | 437 | DBG3("Consumer data URI set with port %d", uri->port); |
00e2e675 DG |
438 | break; |
439 | default: | |
440 | ERR("Set network uri type unknown %d", uri->stype); | |
441 | goto error; | |
442 | } | |
443 | ||
444 | ret = uri_compare(dst_uri, uri); | |
445 | if (!ret) { | |
446 | /* Same URI, don't touch it and return success. */ | |
447 | DBG3("URI network compare are the same"); | |
ad20f474 | 448 | goto equal; |
00e2e675 DG |
449 | } |
450 | ||
451 | /* URIs were not equal, replacing it. */ | |
452 | memset(dst_uri, 0, sizeof(struct lttng_uri)); | |
453 | memcpy(dst_uri, uri, sizeof(struct lttng_uri)); | |
454 | obj->type = CONSUMER_DST_NET; | |
455 | ||
456 | /* Handle subdir and add hostname in front. */ | |
457 | if (dst_uri->stype == LTTNG_STREAM_CONTROL) { | |
458 | /* Get hostname to append it in the pathname */ | |
459 | ret = gethostname(hostname, sizeof(hostname)); | |
460 | if (ret < 0) { | |
461 | PERROR("gethostname. Fallback on default localhost"); | |
462 | strncpy(hostname, "localhost", sizeof(hostname)); | |
463 | } | |
464 | hostname[sizeof(hostname) - 1] = '\0'; | |
465 | ||
466 | /* Setup consumer subdir if none present in the control URI */ | |
467 | if (strlen(dst_uri->subdir) == 0) { | |
468 | ret = snprintf(tmp_path, sizeof(tmp_path), "%s/%s", | |
469 | hostname, obj->subdir); | |
470 | } else { | |
471 | ret = snprintf(tmp_path, sizeof(tmp_path), "%s/%s", | |
472 | hostname, dst_uri->subdir); | |
473 | } | |
474 | if (ret < 0) { | |
475 | PERROR("snprintf set consumer uri subdir"); | |
476 | goto error; | |
477 | } | |
478 | ||
479 | strncpy(obj->subdir, tmp_path, sizeof(obj->subdir)); | |
480 | DBG3("Consumer set network uri subdir path %s", tmp_path); | |
481 | } | |
482 | ||
00e2e675 | 483 | return 0; |
ad20f474 DG |
484 | equal: |
485 | return 1; | |
00e2e675 DG |
486 | error: |
487 | return -1; | |
488 | } | |
489 | ||
490 | /* | |
491 | * Send file descriptor to consumer via sock. | |
492 | */ | |
f50f23d9 | 493 | int consumer_send_fds(struct consumer_socket *sock, int *fds, size_t nb_fd) |
00e2e675 DG |
494 | { |
495 | int ret; | |
496 | ||
497 | assert(fds); | |
f50f23d9 | 498 | assert(sock); |
00e2e675 DG |
499 | assert(nb_fd > 0); |
500 | ||
f50f23d9 | 501 | ret = lttcomm_send_fds_unix_sock(sock->fd, fds, nb_fd); |
00e2e675 | 502 | if (ret < 0) { |
3448e266 DG |
503 | /* The above call will print a PERROR on error. */ |
504 | DBG("Error when sending consumer fds on sock %d", sock->fd); | |
00e2e675 DG |
505 | goto error; |
506 | } | |
507 | ||
f50f23d9 DG |
508 | ret = consumer_recv_status_reply(sock); |
509 | ||
00e2e675 DG |
510 | error: |
511 | return ret; | |
512 | } | |
513 | ||
514 | /* | |
515 | * Consumer send channel communication message structure to consumer. | |
516 | */ | |
f50f23d9 DG |
517 | int consumer_send_channel(struct consumer_socket *sock, |
518 | struct lttcomm_consumer_msg *msg) | |
00e2e675 DG |
519 | { |
520 | int ret; | |
521 | ||
522 | assert(msg); | |
f50f23d9 DG |
523 | assert(sock); |
524 | assert(sock->fd >= 0); | |
00e2e675 | 525 | |
f50f23d9 | 526 | ret = lttcomm_send_unix_sock(sock->fd, msg, |
00e2e675 DG |
527 | sizeof(struct lttcomm_consumer_msg)); |
528 | if (ret < 0) { | |
3448e266 DG |
529 | /* The above call will print a PERROR on error. */ |
530 | DBG("Error when sending consumer channel on sock %d", sock->fd); | |
00e2e675 DG |
531 | goto error; |
532 | } | |
533 | ||
f50f23d9 DG |
534 | ret = consumer_recv_status_reply(sock); |
535 | ||
00e2e675 DG |
536 | error: |
537 | return ret; | |
538 | } | |
539 | ||
540 | /* | |
541 | * Init channel communication message structure. | |
542 | */ | |
543 | void consumer_init_channel_comm_msg(struct lttcomm_consumer_msg *msg, | |
544 | enum lttng_consumer_command cmd, | |
545 | int channel_key, | |
546 | uint64_t max_sb_size, | |
547 | uint64_t mmap_len, | |
c30aaa51 MD |
548 | const char *name, |
549 | unsigned int nb_init_streams) | |
00e2e675 DG |
550 | { |
551 | assert(msg); | |
552 | ||
553 | /* TODO: Args validation */ | |
554 | ||
555 | /* Zeroed structure */ | |
556 | memset(msg, 0, sizeof(struct lttcomm_consumer_msg)); | |
557 | ||
558 | /* Send channel */ | |
559 | msg->cmd_type = cmd; | |
560 | msg->u.channel.channel_key = channel_key; | |
561 | msg->u.channel.max_sb_size = max_sb_size; | |
562 | msg->u.channel.mmap_len = mmap_len; | |
c30aaa51 | 563 | msg->u.channel.nb_init_streams = nb_init_streams; |
00e2e675 DG |
564 | } |
565 | ||
566 | /* | |
567 | * Init stream communication message structure. | |
568 | */ | |
569 | void consumer_init_stream_comm_msg(struct lttcomm_consumer_msg *msg, | |
570 | enum lttng_consumer_command cmd, | |
571 | int channel_key, | |
572 | int stream_key, | |
573 | uint32_t state, | |
574 | enum lttng_event_output output, | |
575 | uint64_t mmap_len, | |
576 | uid_t uid, | |
577 | gid_t gid, | |
578 | int net_index, | |
579 | unsigned int metadata_flag, | |
580 | const char *name, | |
ca22feea DG |
581 | const char *pathname, |
582 | unsigned int session_id) | |
00e2e675 DG |
583 | { |
584 | assert(msg); | |
585 | ||
586 | memset(msg, 0, sizeof(struct lttcomm_consumer_msg)); | |
587 | ||
588 | /* TODO: Args validation */ | |
589 | ||
590 | msg->cmd_type = cmd; | |
591 | msg->u.stream.channel_key = channel_key; | |
592 | msg->u.stream.stream_key = stream_key; | |
593 | msg->u.stream.state = state; | |
594 | msg->u.stream.output = output; | |
595 | msg->u.stream.mmap_len = mmap_len; | |
596 | msg->u.stream.uid = uid; | |
597 | msg->u.stream.gid = gid; | |
598 | msg->u.stream.net_index = net_index; | |
599 | msg->u.stream.metadata_flag = metadata_flag; | |
ca22feea | 600 | msg->u.stream.session_id = (uint64_t) session_id; |
00e2e675 DG |
601 | strncpy(msg->u.stream.name, name, sizeof(msg->u.stream.name)); |
602 | msg->u.stream.name[sizeof(msg->u.stream.name) - 1] = '\0'; | |
603 | strncpy(msg->u.stream.path_name, pathname, | |
604 | sizeof(msg->u.stream.path_name)); | |
605 | msg->u.stream.path_name[sizeof(msg->u.stream.path_name) - 1] = '\0'; | |
606 | } | |
607 | ||
608 | /* | |
609 | * Send stream communication structure to the consumer. | |
610 | */ | |
f50f23d9 DG |
611 | int consumer_send_stream(struct consumer_socket *sock, |
612 | struct consumer_output *dst, struct lttcomm_consumer_msg *msg, | |
613 | int *fds, size_t nb_fd) | |
00e2e675 DG |
614 | { |
615 | int ret; | |
616 | ||
617 | assert(msg); | |
618 | assert(dst); | |
f50f23d9 | 619 | assert(sock); |
00e2e675 DG |
620 | |
621 | switch (dst->type) { | |
622 | case CONSUMER_DST_NET: | |
623 | /* Consumer should send the stream on the network. */ | |
624 | msg->u.stream.net_index = dst->net_seq_index; | |
625 | break; | |
626 | case CONSUMER_DST_LOCAL: | |
627 | /* Add stream file name to stream path */ | |
c30ce0b3 CB |
628 | strncat(msg->u.stream.path_name, "/", |
629 | sizeof(msg->u.stream.path_name) - | |
630 | strlen(msg->u.stream.path_name) - 1); | |
00e2e675 | 631 | strncat(msg->u.stream.path_name, msg->u.stream.name, |
c30ce0b3 CB |
632 | sizeof(msg->u.stream.path_name) - |
633 | strlen(msg->u.stream.path_name) - 1); | |
00e2e675 DG |
634 | msg->u.stream.path_name[sizeof(msg->u.stream.path_name) - 1] = '\0'; |
635 | /* Indicate that the stream is NOT network */ | |
636 | msg->u.stream.net_index = -1; | |
637 | break; | |
638 | default: | |
639 | ERR("Consumer unknown output type (%d)", dst->type); | |
640 | ret = -1; | |
641 | goto error; | |
642 | } | |
643 | ||
644 | /* Send on socket */ | |
f50f23d9 | 645 | ret = lttcomm_send_unix_sock(sock->fd, msg, |
00e2e675 DG |
646 | sizeof(struct lttcomm_consumer_msg)); |
647 | if (ret < 0) { | |
3448e266 DG |
648 | /* The above call will print a PERROR on error. */ |
649 | DBG("Error when sending consumer stream on sock %d", sock->fd); | |
00e2e675 DG |
650 | goto error; |
651 | } | |
652 | ||
f50f23d9 DG |
653 | ret = consumer_recv_status_reply(sock); |
654 | if (ret < 0) { | |
655 | goto error; | |
656 | } | |
657 | ||
00e2e675 DG |
658 | ret = consumer_send_fds(sock, fds, nb_fd); |
659 | if (ret < 0) { | |
660 | goto error; | |
661 | } | |
662 | ||
663 | error: | |
664 | return ret; | |
665 | } | |
37278a1e DG |
666 | |
667 | /* | |
668 | * Send relayd socket to consumer associated with a session name. | |
669 | * | |
670 | * On success return positive value. On error, negative value. | |
671 | */ | |
f50f23d9 | 672 | int consumer_send_relayd_socket(struct consumer_socket *consumer_sock, |
37278a1e | 673 | struct lttcomm_sock *sock, struct consumer_output *consumer, |
46e6455f | 674 | enum lttng_stream_type type, unsigned int session_id) |
37278a1e DG |
675 | { |
676 | int ret; | |
677 | struct lttcomm_consumer_msg msg; | |
678 | ||
679 | /* Code flow error. Safety net. */ | |
680 | assert(sock); | |
681 | assert(consumer); | |
f50f23d9 | 682 | assert(consumer_sock); |
37278a1e DG |
683 | |
684 | /* Bail out if consumer is disabled */ | |
685 | if (!consumer->enabled) { | |
f73fabfd | 686 | ret = LTTNG_OK; |
37278a1e DG |
687 | goto error; |
688 | } | |
689 | ||
690 | msg.cmd_type = LTTNG_CONSUMER_ADD_RELAYD_SOCKET; | |
691 | /* | |
692 | * Assign network consumer output index using the temporary consumer since | |
693 | * this call should only be made from within a set_consumer_uri() function | |
694 | * call in the session daemon. | |
695 | */ | |
696 | msg.u.relayd_sock.net_index = consumer->net_seq_index; | |
697 | msg.u.relayd_sock.type = type; | |
46e6455f | 698 | msg.u.relayd_sock.session_id = session_id; |
37278a1e DG |
699 | memcpy(&msg.u.relayd_sock.sock, sock, sizeof(msg.u.relayd_sock.sock)); |
700 | ||
f50f23d9 DG |
701 | DBG3("Sending relayd sock info to consumer on %d", consumer_sock->fd); |
702 | ret = lttcomm_send_unix_sock(consumer_sock->fd, &msg, sizeof(msg)); | |
37278a1e | 703 | if (ret < 0) { |
3448e266 DG |
704 | /* The above call will print a PERROR on error. */ |
705 | DBG("Error when sending relayd sockets on sock %d", sock->fd); | |
37278a1e DG |
706 | goto error; |
707 | } | |
708 | ||
f50f23d9 DG |
709 | ret = consumer_recv_status_reply(consumer_sock); |
710 | if (ret < 0) { | |
711 | goto error; | |
712 | } | |
713 | ||
37278a1e DG |
714 | DBG3("Sending relayd socket file descriptor to consumer"); |
715 | ret = consumer_send_fds(consumer_sock, &sock->fd, 1); | |
716 | if (ret < 0) { | |
717 | goto error; | |
718 | } | |
719 | ||
720 | DBG2("Consumer relayd socket sent"); | |
721 | ||
722 | error: | |
723 | return ret; | |
724 | } | |
173af62f DG |
725 | |
726 | /* | |
2f77fc4b DG |
727 | * Set consumer subdirectory using the session name and a generated datetime if |
728 | * needed. This is appended to the current subdirectory. | |
173af62f | 729 | */ |
2f77fc4b DG |
730 | int consumer_set_subdir(struct consumer_output *consumer, |
731 | const char *session_name) | |
173af62f | 732 | { |
2f77fc4b DG |
733 | int ret = 0; |
734 | unsigned int have_default_name = 0; | |
735 | char datetime[16], tmp_path[PATH_MAX]; | |
736 | time_t rawtime; | |
737 | struct tm *timeinfo; | |
173af62f DG |
738 | |
739 | assert(consumer); | |
2f77fc4b DG |
740 | assert(session_name); |
741 | ||
742 | memset(tmp_path, 0, sizeof(tmp_path)); | |
743 | ||
744 | /* Flag if we have a default session. */ | |
745 | if (strncmp(session_name, DEFAULT_SESSION_NAME "-", | |
746 | strlen(DEFAULT_SESSION_NAME) + 1) == 0) { | |
747 | have_default_name = 1; | |
748 | } else { | |
749 | /* Get date and time for session path */ | |
750 | time(&rawtime); | |
751 | timeinfo = localtime(&rawtime); | |
752 | strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo); | |
173af62f DG |
753 | } |
754 | ||
2f77fc4b DG |
755 | if (have_default_name) { |
756 | ret = snprintf(tmp_path, sizeof(tmp_path), | |
757 | "%s/%s", consumer->subdir, session_name); | |
758 | } else { | |
759 | ret = snprintf(tmp_path, sizeof(tmp_path), | |
760 | "%s/%s-%s/", consumer->subdir, session_name, datetime); | |
761 | } | |
173af62f | 762 | if (ret < 0) { |
2f77fc4b | 763 | PERROR("snprintf session name date"); |
173af62f DG |
764 | goto error; |
765 | } | |
766 | ||
2f77fc4b DG |
767 | strncpy(consumer->subdir, tmp_path, sizeof(consumer->subdir)); |
768 | DBG2("Consumer subdir set to %s", consumer->subdir); | |
173af62f DG |
769 | |
770 | error: | |
771 | return ret; | |
772 | } | |
806e2684 DG |
773 | |
774 | /* | |
6d805429 | 775 | * Ask the consumer if the data is ready to read (NOT pending) for the specific |
806e2684 DG |
776 | * session id. |
777 | * | |
778 | * This function has a different behavior with the consumer i.e. that it waits | |
6d805429 | 779 | * for a reply from the consumer if yes or no the data is pending. |
806e2684 | 780 | */ |
6d805429 | 781 | int consumer_is_data_pending(unsigned int id, |
806e2684 DG |
782 | struct consumer_output *consumer) |
783 | { | |
784 | int ret; | |
6d805429 | 785 | int32_t ret_code = 0; /* Default is that the data is NOT pending */ |
806e2684 DG |
786 | struct consumer_socket *socket; |
787 | struct lttng_ht_iter iter; | |
788 | struct lttcomm_consumer_msg msg; | |
789 | ||
790 | assert(consumer); | |
791 | ||
6d805429 | 792 | msg.cmd_type = LTTNG_CONSUMER_DATA_PENDING; |
806e2684 | 793 | |
6d805429 | 794 | msg.u.data_pending.session_id = (uint64_t) id; |
806e2684 | 795 | |
6d805429 | 796 | DBG3("Consumer data pending for id %u", id); |
806e2684 | 797 | |
c8f59ee5 | 798 | /* Send command for each consumer */ |
b82c5c4d | 799 | rcu_read_lock(); |
806e2684 DG |
800 | cds_lfht_for_each_entry(consumer->socks->ht, &iter.iter, socket, |
801 | node.node) { | |
802 | /* Code flow error */ | |
803 | assert(socket->fd >= 0); | |
804 | ||
805 | pthread_mutex_lock(socket->lock); | |
806 | ||
807 | ret = lttcomm_send_unix_sock(socket->fd, &msg, sizeof(msg)); | |
808 | if (ret < 0) { | |
3448e266 DG |
809 | /* The above call will print a PERROR on error. */ |
810 | DBG("Error on consumer is data pending on sock %d", socket->fd); | |
806e2684 | 811 | pthread_mutex_unlock(socket->lock); |
b82c5c4d | 812 | goto error_unlock; |
806e2684 DG |
813 | } |
814 | ||
f50f23d9 DG |
815 | /* |
816 | * No need for a recv reply status because the answer to the command is | |
817 | * the reply status message. | |
818 | */ | |
819 | ||
806e2684 | 820 | ret = lttcomm_recv_unix_sock(socket->fd, &ret_code, sizeof(ret_code)); |
a6cd2b97 DG |
821 | if (ret <= 0) { |
822 | if (ret == 0) { | |
823 | /* Orderly shutdown. Don't return 0 which means success. */ | |
824 | ret = -1; | |
825 | } | |
3448e266 DG |
826 | /* The above call will print a PERROR on error. */ |
827 | DBG("Error on recv consumer is data pending on sock %d", socket->fd); | |
806e2684 | 828 | pthread_mutex_unlock(socket->lock); |
b82c5c4d | 829 | goto error_unlock; |
806e2684 DG |
830 | } |
831 | ||
832 | pthread_mutex_unlock(socket->lock); | |
833 | ||
6d805429 | 834 | if (ret_code == 1) { |
806e2684 DG |
835 | break; |
836 | } | |
837 | } | |
b82c5c4d | 838 | rcu_read_unlock(); |
806e2684 | 839 | |
3448e266 DG |
840 | DBG("Consumer data is %s pending for session id %u", |
841 | ret_code == 1 ? "" : "NOT", id); | |
806e2684 DG |
842 | return ret_code; |
843 | ||
b82c5c4d DG |
844 | error_unlock: |
845 | rcu_read_unlock(); | |
806e2684 DG |
846 | return -1; |
847 | } |