Fix: audit all close/fclose and check returned code
[lttng-tools.git] / src / common / consumer.c
CommitLineData
3bd1e081
MD
1/*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
00e2e675 4 * 2012 - David Goulet <dgoulet@efficios.com>
3bd1e081 5 *
d14d33bf
AM
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
3bd1e081 9 *
d14d33bf
AM
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
3bd1e081 14 *
d14d33bf
AM
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
3bd1e081
MD
18 */
19
20#define _GNU_SOURCE
21#include <assert.h>
3bd1e081
MD
22#include <poll.h>
23#include <pthread.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/mman.h>
27#include <sys/socket.h>
28#include <sys/types.h>
29#include <unistd.h>
77c7c900 30#include <inttypes.h>
3bd1e081 31
990570ed 32#include <common/common.h>
fb3a43a9
DG
33#include <common/utils.h>
34#include <common/compat/poll.h>
10a8a223 35#include <common/kernel-ctl/kernel-ctl.h>
00e2e675 36#include <common/sessiond-comm/relayd.h>
10a8a223
DG
37#include <common/sessiond-comm/sessiond-comm.h>
38#include <common/kernel-consumer/kernel-consumer.h>
00e2e675 39#include <common/relayd/relayd.h>
10a8a223
DG
40#include <common/ust-consumer/ust-consumer.h>
41
42#include "consumer.h"
3bd1e081
MD
43
44struct lttng_consumer_global_data consumer_data = {
3bd1e081
MD
45 .stream_count = 0,
46 .need_update = 1,
47 .type = LTTNG_CONSUMER_UNKNOWN,
48};
49
3bd1e081
MD
50/*
51 * Flag to inform the polling thread to quit when all fd hung up. Updated by
52 * the consumer_thread_receive_fds when it notices that all fds has hung up.
53 * Also updated by the signal handler (consumer_should_exit()). Read by the
54 * polling threads.
55 */
a98dae5f 56volatile int consumer_quit;
3bd1e081 57
43c34bc3 58/*
43c34bc3
DG
59 * Global hash table containing respectively metadata and data streams. The
60 * stream element in this ht should only be updated by the metadata poll thread
61 * for the metadata and the data poll thread for the data.
62 */
40dc48e0
DG
63static struct lttng_ht *metadata_ht;
64static struct lttng_ht *data_ht;
43c34bc3 65
8994307f
DG
66/*
67 * Notify a thread pipe to poll back again. This usually means that some global
68 * state has changed so we just send back the thread in a poll wait call.
69 */
70static void notify_thread_pipe(int wpipe)
71{
72 int ret;
73
74 do {
75 struct lttng_consumer_stream *null_stream = NULL;
76
77 ret = write(wpipe, &null_stream, sizeof(null_stream));
78 } while (ret < 0 && errno == EINTR);
79}
80
3bd1e081
MD
81/*
82 * Find a stream. The consumer_data.lock must be locked during this
83 * call.
84 */
8389e4f8
DG
85static struct lttng_consumer_stream *consumer_find_stream(int key,
86 struct lttng_ht *ht)
3bd1e081 87{
e4421fec
DG
88 struct lttng_ht_iter iter;
89 struct lttng_ht_node_ulong *node;
90 struct lttng_consumer_stream *stream = NULL;
3bd1e081 91
8389e4f8
DG
92 assert(ht);
93
7ad0a0cb 94 /* Negative keys are lookup failures */
7a57cf92 95 if (key < 0) {
7ad0a0cb 96 return NULL;
7a57cf92 97 }
e4421fec 98
6065ceec
DG
99 rcu_read_lock();
100
8389e4f8 101 lttng_ht_lookup(ht, (void *)((unsigned long) key), &iter);
e4421fec
DG
102 node = lttng_ht_iter_get_node_ulong(&iter);
103 if (node != NULL) {
104 stream = caa_container_of(node, struct lttng_consumer_stream, node);
3bd1e081 105 }
e4421fec 106
6065ceec
DG
107 rcu_read_unlock();
108
e4421fec 109 return stream;
3bd1e081
MD
110}
111
c869f647 112void consumer_steal_stream_key(int key, struct lttng_ht *ht)
7ad0a0cb
MD
113{
114 struct lttng_consumer_stream *stream;
115
04253271 116 rcu_read_lock();
8389e4f8 117 stream = consumer_find_stream(key, ht);
04253271 118 if (stream) {
7ad0a0cb 119 stream->key = -1;
04253271
MD
120 /*
121 * We don't want the lookup to match, but we still need
122 * to iterate on this stream when iterating over the hash table. Just
123 * change the node key.
124 */
125 stream->node.key = -1;
126 }
127 rcu_read_unlock();
7ad0a0cb
MD
128}
129
3bd1e081
MD
130static struct lttng_consumer_channel *consumer_find_channel(int key)
131{
e4421fec
DG
132 struct lttng_ht_iter iter;
133 struct lttng_ht_node_ulong *node;
134 struct lttng_consumer_channel *channel = NULL;
3bd1e081 135
7ad0a0cb 136 /* Negative keys are lookup failures */
7a57cf92 137 if (key < 0) {
7ad0a0cb 138 return NULL;
7a57cf92 139 }
e4421fec 140
6065ceec
DG
141 rcu_read_lock();
142
e4421fec
DG
143 lttng_ht_lookup(consumer_data.channel_ht, (void *)((unsigned long) key),
144 &iter);
145 node = lttng_ht_iter_get_node_ulong(&iter);
146 if (node != NULL) {
147 channel = caa_container_of(node, struct lttng_consumer_channel, node);
3bd1e081 148 }
e4421fec 149
6065ceec
DG
150 rcu_read_unlock();
151
e4421fec 152 return channel;
3bd1e081
MD
153}
154
7ad0a0cb
MD
155static void consumer_steal_channel_key(int key)
156{
157 struct lttng_consumer_channel *channel;
158
04253271 159 rcu_read_lock();
7ad0a0cb 160 channel = consumer_find_channel(key);
04253271 161 if (channel) {
7ad0a0cb 162 channel->key = -1;
04253271
MD
163 /*
164 * We don't want the lookup to match, but we still need
165 * to iterate on this channel when iterating over the hash table. Just
166 * change the node key.
167 */
168 channel->node.key = -1;
169 }
170 rcu_read_unlock();
7ad0a0cb
MD
171}
172
702b1ea4
MD
173static
174void consumer_free_stream(struct rcu_head *head)
175{
176 struct lttng_ht_node_ulong *node =
177 caa_container_of(head, struct lttng_ht_node_ulong, head);
178 struct lttng_consumer_stream *stream =
179 caa_container_of(node, struct lttng_consumer_stream, node);
180
181 free(stream);
182}
183
00e2e675
DG
184/*
185 * RCU protected relayd socket pair free.
186 */
187static void consumer_rcu_free_relayd(struct rcu_head *head)
188{
189 struct lttng_ht_node_ulong *node =
190 caa_container_of(head, struct lttng_ht_node_ulong, head);
191 struct consumer_relayd_sock_pair *relayd =
192 caa_container_of(node, struct consumer_relayd_sock_pair, node);
193
8994307f
DG
194 /*
195 * Close all sockets. This is done in the call RCU since we don't want the
196 * socket fds to be reassigned thus potentially creating bad state of the
197 * relayd object.
198 *
199 * We do not have to lock the control socket mutex here since at this stage
200 * there is no one referencing to this relayd object.
201 */
202 (void) relayd_close(&relayd->control_sock);
203 (void) relayd_close(&relayd->data_sock);
204
00e2e675
DG
205 free(relayd);
206}
207
208/*
209 * Destroy and free relayd socket pair object.
210 *
211 * This function MUST be called with the consumer_data lock acquired.
212 */
d09e1200 213static void destroy_relayd(struct consumer_relayd_sock_pair *relayd)
00e2e675
DG
214{
215 int ret;
216 struct lttng_ht_iter iter;
217
173af62f
DG
218 if (relayd == NULL) {
219 return;
220 }
221
00e2e675
DG
222 DBG("Consumer destroy and close relayd socket pair");
223
224 iter.iter.node = &relayd->node.node;
225 ret = lttng_ht_del(consumer_data.relayd_ht, &iter);
173af62f 226 if (ret != 0) {
8994307f 227 /* We assume the relayd is being or is destroyed */
173af62f
DG
228 return;
229 }
00e2e675 230
00e2e675
DG
231 /* RCU free() call */
232 call_rcu(&relayd->node.head, consumer_rcu_free_relayd);
233}
234
8994307f
DG
235/*
236 * Update the end point status of all streams having the given network sequence
237 * index (relayd index).
238 *
239 * It's atomically set without having the stream mutex locked which is fine
240 * because we handle the write/read race with a pipe wakeup for each thread.
241 */
242static void update_endpoint_status_by_netidx(int net_seq_idx,
243 enum consumer_endpoint_status status)
244{
245 struct lttng_ht_iter iter;
246 struct lttng_consumer_stream *stream;
247
248 DBG("Consumer set delete flag on stream by idx %d", net_seq_idx);
249
250 rcu_read_lock();
251
252 /* Let's begin with metadata */
253 cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream, node.node) {
254 if (stream->net_seq_idx == net_seq_idx) {
255 uatomic_set(&stream->endpoint_status, status);
256 DBG("Delete flag set to metadata stream %d", stream->wait_fd);
257 }
258 }
259
260 /* Follow up by the data streams */
261 cds_lfht_for_each_entry(data_ht->ht, &iter.iter, stream, node.node) {
262 if (stream->net_seq_idx == net_seq_idx) {
263 uatomic_set(&stream->endpoint_status, status);
264 DBG("Delete flag set to data stream %d", stream->wait_fd);
265 }
266 }
267 rcu_read_unlock();
268}
269
270/*
271 * Cleanup a relayd object by flagging every associated streams for deletion,
272 * destroying the object meaning removing it from the relayd hash table,
273 * closing the sockets and freeing the memory in a RCU call.
274 *
275 * If a local data context is available, notify the threads that the streams'
276 * state have changed.
277 */
278static void cleanup_relayd(struct consumer_relayd_sock_pair *relayd,
279 struct lttng_consumer_local_data *ctx)
280{
281 int netidx;
282
283 assert(relayd);
284
9617607b
DG
285 DBG("Cleaning up relayd sockets");
286
8994307f
DG
287 /* Save the net sequence index before destroying the object */
288 netidx = relayd->net_seq_idx;
289
290 /*
291 * Delete the relayd from the relayd hash table, close the sockets and free
292 * the object in a RCU call.
293 */
294 destroy_relayd(relayd);
295
296 /* Set inactive endpoint to all streams */
297 update_endpoint_status_by_netidx(netidx, CONSUMER_ENDPOINT_INACTIVE);
298
299 /*
300 * With a local data context, notify the threads that the streams' state
301 * have changed. The write() action on the pipe acts as an "implicit"
302 * memory barrier ordering the updates of the end point status from the
303 * read of this status which happens AFTER receiving this notify.
304 */
305 if (ctx) {
306 notify_thread_pipe(ctx->consumer_data_pipe[1]);
307 notify_thread_pipe(ctx->consumer_metadata_pipe[1]);
308 }
309}
310
a6ba4fe1
DG
311/*
312 * Flag a relayd socket pair for destruction. Destroy it if the refcount
313 * reaches zero.
314 *
315 * RCU read side lock MUST be aquired before calling this function.
316 */
317void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair *relayd)
318{
319 assert(relayd);
320
321 /* Set destroy flag for this object */
322 uatomic_set(&relayd->destroy_flag, 1);
323
324 /* Destroy the relayd if refcount is 0 */
325 if (uatomic_read(&relayd->refcount) == 0) {
d09e1200 326 destroy_relayd(relayd);
a6ba4fe1
DG
327 }
328}
329
3bd1e081
MD
330/*
331 * Remove a stream from the global list protected by a mutex. This
332 * function is also responsible for freeing its data structures.
333 */
e316aad5
DG
334void consumer_del_stream(struct lttng_consumer_stream *stream,
335 struct lttng_ht *ht)
3bd1e081
MD
336{
337 int ret;
e4421fec 338 struct lttng_ht_iter iter;
3bd1e081 339 struct lttng_consumer_channel *free_chan = NULL;
00e2e675
DG
340 struct consumer_relayd_sock_pair *relayd;
341
342 assert(stream);
3bd1e081 343
8994307f
DG
344 DBG("Consumer del stream %d", stream->wait_fd);
345
e316aad5
DG
346 if (ht == NULL) {
347 /* Means the stream was allocated but not successfully added */
348 goto free_stream;
349 }
350
8994307f 351 pthread_mutex_lock(&stream->lock);
3bd1e081
MD
352 pthread_mutex_lock(&consumer_data.lock);
353
354 switch (consumer_data.type) {
355 case LTTNG_CONSUMER_KERNEL:
356 if (stream->mmap_base != NULL) {
357 ret = munmap(stream->mmap_base, stream->mmap_len);
358 if (ret != 0) {
7a57cf92 359 PERROR("munmap");
3bd1e081
MD
360 }
361 }
362 break;
7753dea8
MD
363 case LTTNG_CONSUMER32_UST:
364 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
365 lttng_ustconsumer_del_stream(stream);
366 break;
367 default:
368 ERR("Unknown consumer_data type");
369 assert(0);
370 goto end;
371 }
372
6065ceec 373 rcu_read_lock();
04253271 374 iter.iter.node = &stream->node.node;
e316aad5 375 ret = lttng_ht_del(ht, &iter);
04253271 376 assert(!ret);
ca22feea
DG
377
378 /* Remove node session id from the consumer_data stream ht */
379 iter.iter.node = &stream->node_session_id.node;
380 ret = lttng_ht_del(consumer_data.stream_list_ht, &iter);
381 assert(!ret);
6065ceec
DG
382 rcu_read_unlock();
383
50f8ae69 384 assert(consumer_data.stream_count > 0);
3bd1e081 385 consumer_data.stream_count--;
50f8ae69 386
3bd1e081 387 if (stream->out_fd >= 0) {
4c462e79
MD
388 ret = close(stream->out_fd);
389 if (ret) {
390 PERROR("close");
391 }
3bd1e081 392 }
b5c5fc29 393 if (stream->wait_fd >= 0 && !stream->wait_fd_is_copy) {
4c462e79
MD
394 ret = close(stream->wait_fd);
395 if (ret) {
396 PERROR("close");
397 }
3bd1e081 398 }
2c1dd183 399 if (stream->shm_fd >= 0 && stream->wait_fd != stream->shm_fd) {
4c462e79
MD
400 ret = close(stream->shm_fd);
401 if (ret) {
402 PERROR("close");
403 }
3bd1e081 404 }
00e2e675
DG
405
406 /* Check and cleanup relayd */
b0b335c8 407 rcu_read_lock();
00e2e675
DG
408 relayd = consumer_find_relayd(stream->net_seq_idx);
409 if (relayd != NULL) {
b0b335c8
MD
410 uatomic_dec(&relayd->refcount);
411 assert(uatomic_read(&relayd->refcount) >= 0);
173af62f 412
3f8e211f
DG
413 /* Closing streams requires to lock the control socket. */
414 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
173af62f
DG
415 ret = relayd_send_close_stream(&relayd->control_sock,
416 stream->relayd_stream_id,
417 stream->next_net_seq_num - 1);
3f8e211f 418 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
173af62f 419 if (ret < 0) {
a4b92340
DG
420 DBG("Unable to close stream on the relayd. Continuing");
421 /*
422 * Continue here. There is nothing we can do for the relayd.
423 * Chances are that the relayd has closed the socket so we just
424 * continue cleaning up.
425 */
173af62f
DG
426 }
427
428 /* Both conditions are met, we destroy the relayd. */
429 if (uatomic_read(&relayd->refcount) == 0 &&
430 uatomic_read(&relayd->destroy_flag)) {
d09e1200 431 destroy_relayd(relayd);
00e2e675 432 }
00e2e675 433 }
b0b335c8 434 rcu_read_unlock();
00e2e675 435
c30aaa51
MD
436 uatomic_dec(&stream->chan->refcount);
437 if (!uatomic_read(&stream->chan->refcount)
438 && !uatomic_read(&stream->chan->nb_init_streams)) {
3bd1e081 439 free_chan = stream->chan;
00e2e675
DG
440 }
441
3bd1e081
MD
442end:
443 consumer_data.need_update = 1;
444 pthread_mutex_unlock(&consumer_data.lock);
8994307f 445 pthread_mutex_unlock(&stream->lock);
3bd1e081 446
c30aaa51 447 if (free_chan) {
3bd1e081 448 consumer_del_channel(free_chan);
c30aaa51 449 }
e316aad5
DG
450
451free_stream:
452 call_rcu(&stream->node.head, consumer_free_stream);
3bd1e081
MD
453}
454
455struct lttng_consumer_stream *consumer_allocate_stream(
456 int channel_key, int stream_key,
457 int shm_fd, int wait_fd,
458 enum lttng_consumer_stream_state state,
459 uint64_t mmap_len,
460 enum lttng_event_output output,
6df2e2c9
MD
461 const char *path_name,
462 uid_t uid,
00e2e675
DG
463 gid_t gid,
464 int net_index,
c80048c6 465 int metadata_flag,
53632229 466 uint64_t session_id,
c80048c6 467 int *alloc_ret)
3bd1e081
MD
468{
469 struct lttng_consumer_stream *stream;
3bd1e081 470
effcf122 471 stream = zmalloc(sizeof(*stream));
3bd1e081 472 if (stream == NULL) {
7a57cf92 473 PERROR("malloc struct lttng_consumer_stream");
c80048c6 474 *alloc_ret = -ENOMEM;
7a57cf92 475 goto end;
3bd1e081 476 }
7a57cf92
DG
477
478 /*
479 * Get stream's channel reference. Needed when adding the stream to the
480 * global hash table.
481 */
3bd1e081
MD
482 stream->chan = consumer_find_channel(channel_key);
483 if (!stream->chan) {
c80048c6 484 *alloc_ret = -ENOENT;
7a57cf92 485 ERR("Unable to find channel for stream %d", stream_key);
c80048c6 486 goto error;
3bd1e081 487 }
e316aad5 488
3bd1e081
MD
489 stream->key = stream_key;
490 stream->shm_fd = shm_fd;
491 stream->wait_fd = wait_fd;
492 stream->out_fd = -1;
493 stream->out_fd_offset = 0;
494 stream->state = state;
495 stream->mmap_len = mmap_len;
496 stream->mmap_base = NULL;
497 stream->output = output;
6df2e2c9
MD
498 stream->uid = uid;
499 stream->gid = gid;
00e2e675
DG
500 stream->net_seq_idx = net_index;
501 stream->metadata_flag = metadata_flag;
53632229 502 stream->session_id = session_id;
00e2e675
DG
503 strncpy(stream->path_name, path_name, sizeof(stream->path_name));
504 stream->path_name[sizeof(stream->path_name) - 1] = '\0';
53632229 505 pthread_mutex_init(&stream->lock, NULL);
58b1f425
DG
506
507 /*
508 * Index differently the metadata node because the thread is using an
509 * internal hash table to match streams in the metadata_ht to the epoll set
510 * file descriptor.
511 */
512 if (metadata_flag) {
513 lttng_ht_node_init_ulong(&stream->node, stream->wait_fd);
514 } else {
515 lttng_ht_node_init_ulong(&stream->node, stream->key);
516 }
c30aaa51 517
53632229
DG
518 /* Init session id node with the stream session id */
519 lttng_ht_node_init_ulong(&stream->node_session_id, stream->session_id);
520
c869f647
DG
521 /*
522 * The cpu number is needed before using any ustctl_* actions. Ignored for
523 * the kernel so the value does not matter.
524 */
525 pthread_mutex_lock(&consumer_data.lock);
526 stream->cpu = stream->chan->cpucount++;
527 pthread_mutex_unlock(&consumer_data.lock);
528
c30aaa51 529 DBG3("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu,"
53632229
DG
530 " out_fd %d, net_seq_idx %d, session_id %" PRIu64,
531 stream->path_name, stream->key, stream->shm_fd, stream->wait_fd,
c30aaa51 532 (unsigned long long) stream->mmap_len, stream->out_fd,
53632229 533 stream->net_seq_idx, stream->session_id);
3bd1e081 534 return stream;
c80048c6
MD
535
536error:
537 free(stream);
7a57cf92 538end:
c80048c6 539 return NULL;
3bd1e081
MD
540}
541
542/*
543 * Add a stream to the global list protected by a mutex.
544 */
43c34bc3
DG
545static int consumer_add_stream(struct lttng_consumer_stream *stream,
546 struct lttng_ht *ht)
3bd1e081
MD
547{
548 int ret = 0;
00e2e675 549 struct consumer_relayd_sock_pair *relayd;
3bd1e081 550
e316aad5 551 assert(stream);
43c34bc3 552 assert(ht);
c77fc10a 553
e316aad5
DG
554 DBG3("Adding consumer stream %d", stream->key);
555
556 pthread_mutex_lock(&consumer_data.lock);
b0b335c8 557 rcu_read_lock();
e316aad5 558
43c34bc3
DG
559 /* Steal stream identifier to avoid having streams with the same key */
560 consumer_steal_stream_key(stream->key, ht);
561
562 lttng_ht_add_unique_ulong(ht, &stream->node);
00e2e675 563
ca22feea
DG
564 /*
565 * Add stream to the stream_list_ht of the consumer data. No need to steal
566 * the key since the HT does not use it and we allow to add redundant keys
567 * into this table.
568 */
569 lttng_ht_add_ulong(consumer_data.stream_list_ht, &stream->node_session_id);
570
00e2e675
DG
571 /* Check and cleanup relayd */
572 relayd = consumer_find_relayd(stream->net_seq_idx);
573 if (relayd != NULL) {
b0b335c8 574 uatomic_inc(&relayd->refcount);
00e2e675
DG
575 }
576
e316aad5
DG
577 /* Update channel refcount once added without error(s). */
578 uatomic_inc(&stream->chan->refcount);
579
580 /*
581 * When nb_init_streams reaches 0, we don't need to trigger any action in
582 * terms of destroying the associated channel, because the action that
583 * causes the count to become 0 also causes a stream to be added. The
584 * channel deletion will thus be triggered by the following removal of this
585 * stream.
586 */
587 if (uatomic_read(&stream->chan->nb_init_streams) > 0) {
588 uatomic_dec(&stream->chan->nb_init_streams);
589 }
590
591 /* Update consumer data once the node is inserted. */
3bd1e081
MD
592 consumer_data.stream_count++;
593 consumer_data.need_update = 1;
594
e316aad5 595 rcu_read_unlock();
3bd1e081 596 pthread_mutex_unlock(&consumer_data.lock);
702b1ea4 597
3bd1e081
MD
598 return ret;
599}
600
00e2e675 601/*
3f8e211f
DG
602 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
603 * be acquired before calling this.
00e2e675 604 */
d09e1200 605static int add_relayd(struct consumer_relayd_sock_pair *relayd)
00e2e675
DG
606{
607 int ret = 0;
608 struct lttng_ht_node_ulong *node;
609 struct lttng_ht_iter iter;
610
611 if (relayd == NULL) {
612 ret = -1;
613 goto end;
614 }
615
00e2e675
DG
616 lttng_ht_lookup(consumer_data.relayd_ht,
617 (void *)((unsigned long) relayd->net_seq_idx), &iter);
618 node = lttng_ht_iter_get_node_ulong(&iter);
619 if (node != NULL) {
00e2e675
DG
620 /* Relayd already exist. Ignore the insertion */
621 goto end;
622 }
623 lttng_ht_add_unique_ulong(consumer_data.relayd_ht, &relayd->node);
624
00e2e675
DG
625end:
626 return ret;
627}
628
629/*
630 * Allocate and return a consumer relayd socket.
631 */
632struct consumer_relayd_sock_pair *consumer_allocate_relayd_sock_pair(
633 int net_seq_idx)
634{
635 struct consumer_relayd_sock_pair *obj = NULL;
636
637 /* Negative net sequence index is a failure */
638 if (net_seq_idx < 0) {
639 goto error;
640 }
641
642 obj = zmalloc(sizeof(struct consumer_relayd_sock_pair));
643 if (obj == NULL) {
644 PERROR("zmalloc relayd sock");
645 goto error;
646 }
647
648 obj->net_seq_idx = net_seq_idx;
649 obj->refcount = 0;
173af62f 650 obj->destroy_flag = 0;
00e2e675
DG
651 lttng_ht_node_init_ulong(&obj->node, obj->net_seq_idx);
652 pthread_mutex_init(&obj->ctrl_sock_mutex, NULL);
653
654error:
655 return obj;
656}
657
658/*
659 * Find a relayd socket pair in the global consumer data.
660 *
661 * Return the object if found else NULL.
b0b335c8
MD
662 * RCU read-side lock must be held across this call and while using the
663 * returned object.
00e2e675
DG
664 */
665struct consumer_relayd_sock_pair *consumer_find_relayd(int key)
666{
667 struct lttng_ht_iter iter;
668 struct lttng_ht_node_ulong *node;
669 struct consumer_relayd_sock_pair *relayd = NULL;
670
671 /* Negative keys are lookup failures */
672 if (key < 0) {
673 goto error;
674 }
675
00e2e675
DG
676 lttng_ht_lookup(consumer_data.relayd_ht, (void *)((unsigned long) key),
677 &iter);
678 node = lttng_ht_iter_get_node_ulong(&iter);
679 if (node != NULL) {
680 relayd = caa_container_of(node, struct consumer_relayd_sock_pair, node);
681 }
682
00e2e675
DG
683error:
684 return relayd;
685}
686
687/*
688 * Handle stream for relayd transmission if the stream applies for network
689 * streaming where the net sequence index is set.
690 *
691 * Return destination file descriptor or negative value on error.
692 */
6197aea7 693static int write_relayd_stream_header(struct lttng_consumer_stream *stream,
1d4dfdef
DG
694 size_t data_size, unsigned long padding,
695 struct consumer_relayd_sock_pair *relayd)
00e2e675
DG
696{
697 int outfd = -1, ret;
00e2e675
DG
698 struct lttcomm_relayd_data_hdr data_hdr;
699
700 /* Safety net */
701 assert(stream);
6197aea7 702 assert(relayd);
00e2e675
DG
703
704 /* Reset data header */
705 memset(&data_hdr, 0, sizeof(data_hdr));
706
00e2e675
DG
707 if (stream->metadata_flag) {
708 /* Caller MUST acquire the relayd control socket lock */
709 ret = relayd_send_metadata(&relayd->control_sock, data_size);
710 if (ret < 0) {
711 goto error;
712 }
713
714 /* Metadata are always sent on the control socket. */
715 outfd = relayd->control_sock.fd;
716 } else {
717 /* Set header with stream information */
718 data_hdr.stream_id = htobe64(stream->relayd_stream_id);
719 data_hdr.data_size = htobe32(data_size);
1d4dfdef 720 data_hdr.padding_size = htobe32(padding);
173af62f 721 data_hdr.net_seq_num = htobe64(stream->next_net_seq_num++);
00e2e675
DG
722 /* Other fields are zeroed previously */
723
724 ret = relayd_send_data_hdr(&relayd->data_sock, &data_hdr,
725 sizeof(data_hdr));
726 if (ret < 0) {
727 goto error;
728 }
729
730 /* Set to go on data socket */
731 outfd = relayd->data_sock.fd;
732 }
733
734error:
735 return outfd;
736}
737
702b1ea4
MD
738static
739void consumer_free_channel(struct rcu_head *head)
740{
741 struct lttng_ht_node_ulong *node =
742 caa_container_of(head, struct lttng_ht_node_ulong, head);
743 struct lttng_consumer_channel *channel =
744 caa_container_of(node, struct lttng_consumer_channel, node);
745
746 free(channel);
747}
748
3bd1e081
MD
749/*
750 * Remove a channel from the global list protected by a mutex. This
751 * function is also responsible for freeing its data structures.
752 */
753void consumer_del_channel(struct lttng_consumer_channel *channel)
754{
755 int ret;
e4421fec 756 struct lttng_ht_iter iter;
3bd1e081
MD
757
758 pthread_mutex_lock(&consumer_data.lock);
759
760 switch (consumer_data.type) {
761 case LTTNG_CONSUMER_KERNEL:
762 break;
7753dea8
MD
763 case LTTNG_CONSUMER32_UST:
764 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
765 lttng_ustconsumer_del_channel(channel);
766 break;
767 default:
768 ERR("Unknown consumer_data type");
769 assert(0);
770 goto end;
771 }
772
6065ceec 773 rcu_read_lock();
04253271
MD
774 iter.iter.node = &channel->node.node;
775 ret = lttng_ht_del(consumer_data.channel_ht, &iter);
776 assert(!ret);
6065ceec
DG
777 rcu_read_unlock();
778
3bd1e081
MD
779 if (channel->mmap_base != NULL) {
780 ret = munmap(channel->mmap_base, channel->mmap_len);
781 if (ret != 0) {
7a57cf92 782 PERROR("munmap");
3bd1e081
MD
783 }
784 }
b5c5fc29 785 if (channel->wait_fd >= 0 && !channel->wait_fd_is_copy) {
4c462e79
MD
786 ret = close(channel->wait_fd);
787 if (ret) {
788 PERROR("close");
789 }
3bd1e081 790 }
2c1dd183 791 if (channel->shm_fd >= 0 && channel->wait_fd != channel->shm_fd) {
4c462e79
MD
792 ret = close(channel->shm_fd);
793 if (ret) {
794 PERROR("close");
795 }
3bd1e081 796 }
702b1ea4
MD
797
798 call_rcu(&channel->node.head, consumer_free_channel);
3bd1e081
MD
799end:
800 pthread_mutex_unlock(&consumer_data.lock);
801}
802
803struct lttng_consumer_channel *consumer_allocate_channel(
804 int channel_key,
805 int shm_fd, int wait_fd,
806 uint64_t mmap_len,
c30aaa51
MD
807 uint64_t max_sb_size,
808 unsigned int nb_init_streams)
3bd1e081
MD
809{
810 struct lttng_consumer_channel *channel;
811 int ret;
812
276b26d1 813 channel = zmalloc(sizeof(*channel));
3bd1e081 814 if (channel == NULL) {
7a57cf92 815 PERROR("malloc struct lttng_consumer_channel");
3bd1e081
MD
816 goto end;
817 }
818 channel->key = channel_key;
819 channel->shm_fd = shm_fd;
820 channel->wait_fd = wait_fd;
821 channel->mmap_len = mmap_len;
822 channel->max_sb_size = max_sb_size;
823 channel->refcount = 0;
c30aaa51 824 channel->nb_init_streams = nb_init_streams;
e4421fec 825 lttng_ht_node_init_ulong(&channel->node, channel->key);
3bd1e081
MD
826
827 switch (consumer_data.type) {
828 case LTTNG_CONSUMER_KERNEL:
829 channel->mmap_base = NULL;
830 channel->mmap_len = 0;
831 break;
7753dea8
MD
832 case LTTNG_CONSUMER32_UST:
833 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
834 ret = lttng_ustconsumer_allocate_channel(channel);
835 if (ret) {
836 free(channel);
837 return NULL;
838 }
839 break;
840 default:
841 ERR("Unknown consumer_data type");
842 assert(0);
843 goto end;
844 }
845 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
00e2e675 846 channel->key, channel->shm_fd, channel->wait_fd,
3bd1e081
MD
847 (unsigned long long) channel->mmap_len,
848 (unsigned long long) channel->max_sb_size);
849end:
850 return channel;
851}
852
853/*
854 * Add a channel to the global list protected by a mutex.
855 */
856int consumer_add_channel(struct lttng_consumer_channel *channel)
857{
c77fc10a
DG
858 struct lttng_ht_node_ulong *node;
859 struct lttng_ht_iter iter;
860
3bd1e081 861 pthread_mutex_lock(&consumer_data.lock);
7ad0a0cb
MD
862 /* Steal channel identifier, for UST */
863 consumer_steal_channel_key(channel->key);
6065ceec 864 rcu_read_lock();
c77fc10a
DG
865
866 lttng_ht_lookup(consumer_data.channel_ht,
867 (void *)((unsigned long) channel->key), &iter);
868 node = lttng_ht_iter_get_node_ulong(&iter);
869 if (node != NULL) {
870 /* Channel already exist. Ignore the insertion */
871 goto end;
872 }
873
04253271 874 lttng_ht_add_unique_ulong(consumer_data.channel_ht, &channel->node);
c77fc10a
DG
875
876end:
6065ceec 877 rcu_read_unlock();
3bd1e081 878 pthread_mutex_unlock(&consumer_data.lock);
702b1ea4 879
7ad0a0cb 880 return 0;
3bd1e081
MD
881}
882
883/*
884 * Allocate the pollfd structure and the local view of the out fds to avoid
885 * doing a lookup in the linked list and concurrency issues when writing is
886 * needed. Called with consumer_data.lock held.
887 *
888 * Returns the number of fds in the structures.
889 */
43c34bc3 890static int consumer_update_poll_array(
3bd1e081 891 struct lttng_consumer_local_data *ctx, struct pollfd **pollfd,
43c34bc3 892 struct lttng_consumer_stream **local_stream, struct lttng_ht *ht)
3bd1e081 893{
3bd1e081 894 int i = 0;
e4421fec
DG
895 struct lttng_ht_iter iter;
896 struct lttng_consumer_stream *stream;
3bd1e081
MD
897
898 DBG("Updating poll fd array");
481d6c57 899 rcu_read_lock();
43c34bc3 900 cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) {
8994307f
DG
901 /*
902 * Only active streams with an active end point can be added to the
903 * poll set and local stream storage of the thread.
904 *
905 * There is a potential race here for endpoint_status to be updated
906 * just after the check. However, this is OK since the stream(s) will
907 * be deleted once the thread is notified that the end point state has
908 * changed where this function will be called back again.
909 */
910 if (stream->state != LTTNG_CONSUMER_ACTIVE_STREAM ||
911 stream->endpoint_status) {
3bd1e081
MD
912 continue;
913 }
e4421fec
DG
914 DBG("Active FD %d", stream->wait_fd);
915 (*pollfd)[i].fd = stream->wait_fd;
3bd1e081 916 (*pollfd)[i].events = POLLIN | POLLPRI;
e4421fec 917 local_stream[i] = stream;
3bd1e081
MD
918 i++;
919 }
481d6c57 920 rcu_read_unlock();
3bd1e081
MD
921
922 /*
50f8ae69 923 * Insert the consumer_data_pipe at the end of the array and don't
3bd1e081
MD
924 * increment i so nb_fd is the number of real FD.
925 */
50f8ae69 926 (*pollfd)[i].fd = ctx->consumer_data_pipe[0];
509bb1cf 927 (*pollfd)[i].events = POLLIN | POLLPRI;
3bd1e081
MD
928 return i;
929}
930
931/*
932 * Poll on the should_quit pipe and the command socket return -1 on error and
933 * should exit, 0 if data is available on the command socket
934 */
935int lttng_consumer_poll_socket(struct pollfd *consumer_sockpoll)
936{
937 int num_rdy;
938
88f2b785 939restart:
3bd1e081
MD
940 num_rdy = poll(consumer_sockpoll, 2, -1);
941 if (num_rdy == -1) {
88f2b785
MD
942 /*
943 * Restart interrupted system call.
944 */
945 if (errno == EINTR) {
946 goto restart;
947 }
7a57cf92 948 PERROR("Poll error");
3bd1e081
MD
949 goto exit;
950 }
509bb1cf 951 if (consumer_sockpoll[0].revents & (POLLIN | POLLPRI)) {
3bd1e081
MD
952 DBG("consumer_should_quit wake up");
953 goto exit;
954 }
955 return 0;
956
957exit:
958 return -1;
959}
960
961/*
962 * Set the error socket.
963 */
964void lttng_consumer_set_error_sock(
965 struct lttng_consumer_local_data *ctx, int sock)
966{
967 ctx->consumer_error_socket = sock;
968}
969
970/*
971 * Set the command socket path.
972 */
3bd1e081
MD
973void lttng_consumer_set_command_sock_path(
974 struct lttng_consumer_local_data *ctx, char *sock)
975{
976 ctx->consumer_command_sock_path = sock;
977}
978
979/*
980 * Send return code to the session daemon.
981 * If the socket is not defined, we return 0, it is not a fatal error
982 */
983int lttng_consumer_send_error(
984 struct lttng_consumer_local_data *ctx, int cmd)
985{
986 if (ctx->consumer_error_socket > 0) {
987 return lttcomm_send_unix_sock(ctx->consumer_error_socket, &cmd,
988 sizeof(enum lttcomm_sessiond_command));
989 }
990
991 return 0;
992}
993
994/*
995 * Close all the tracefiles and stream fds, should be called when all instances
996 * are destroyed.
997 */
998void lttng_consumer_cleanup(void)
999{
e4421fec 1000 struct lttng_ht_iter iter;
6065ceec
DG
1001 struct lttng_ht_node_ulong *node;
1002
1003 rcu_read_lock();
3bd1e081 1004
6065ceec
DG
1005 cds_lfht_for_each_entry(consumer_data.channel_ht->ht, &iter.iter, node,
1006 node) {
702b1ea4
MD
1007 struct lttng_consumer_channel *channel =
1008 caa_container_of(node, struct lttng_consumer_channel, node);
1009 consumer_del_channel(channel);
3bd1e081 1010 }
6065ceec
DG
1011
1012 rcu_read_unlock();
d6ce1df2 1013
d6ce1df2 1014 lttng_ht_destroy(consumer_data.channel_ht);
3bd1e081
MD
1015}
1016
1017/*
1018 * Called from signal handler.
1019 */
1020void lttng_consumer_should_exit(struct lttng_consumer_local_data *ctx)
1021{
1022 int ret;
1023 consumer_quit = 1;
6f94560a
MD
1024 do {
1025 ret = write(ctx->consumer_should_quit[1], "4", 1);
1026 } while (ret < 0 && errno == EINTR);
3bd1e081 1027 if (ret < 0) {
7a57cf92 1028 PERROR("write consumer quit");
3bd1e081 1029 }
ab1027f4
DG
1030
1031 DBG("Consumer flag that it should quit");
3bd1e081
MD
1032}
1033
00e2e675
DG
1034void lttng_consumer_sync_trace_file(struct lttng_consumer_stream *stream,
1035 off_t orig_offset)
3bd1e081
MD
1036{
1037 int outfd = stream->out_fd;
1038
1039 /*
1040 * This does a blocking write-and-wait on any page that belongs to the
1041 * subbuffer prior to the one we just wrote.
1042 * Don't care about error values, as these are just hints and ways to
1043 * limit the amount of page cache used.
1044 */
1045 if (orig_offset < stream->chan->max_sb_size) {
1046 return;
1047 }
b9182dd9 1048 lttng_sync_file_range(outfd, orig_offset - stream->chan->max_sb_size,
3bd1e081
MD
1049 stream->chan->max_sb_size,
1050 SYNC_FILE_RANGE_WAIT_BEFORE
1051 | SYNC_FILE_RANGE_WRITE
1052 | SYNC_FILE_RANGE_WAIT_AFTER);
1053 /*
1054 * Give hints to the kernel about how we access the file:
1055 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1056 * we write it.
1057 *
1058 * We need to call fadvise again after the file grows because the
1059 * kernel does not seem to apply fadvise to non-existing parts of the
1060 * file.
1061 *
1062 * Call fadvise _after_ having waited for the page writeback to
1063 * complete because the dirty page writeback semantic is not well
1064 * defined. So it can be expected to lead to lower throughput in
1065 * streaming.
1066 */
1067 posix_fadvise(outfd, orig_offset - stream->chan->max_sb_size,
1068 stream->chan->max_sb_size, POSIX_FADV_DONTNEED);
1069}
1070
1071/*
1072 * Initialise the necessary environnement :
1073 * - create a new context
1074 * - create the poll_pipe
1075 * - create the should_quit pipe (for signal handler)
1076 * - create the thread pipe (for splice)
1077 *
1078 * Takes a function pointer as argument, this function is called when data is
1079 * available on a buffer. This function is responsible to do the
1080 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1081 * buffer configuration and then kernctl_put_next_subbuf at the end.
1082 *
1083 * Returns a pointer to the new context or NULL on error.
1084 */
1085struct lttng_consumer_local_data *lttng_consumer_create(
1086 enum lttng_consumer_type type,
4078b776 1087 ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream,
d41f73b7 1088 struct lttng_consumer_local_data *ctx),
3bd1e081
MD
1089 int (*recv_channel)(struct lttng_consumer_channel *channel),
1090 int (*recv_stream)(struct lttng_consumer_stream *stream),
1091 int (*update_stream)(int stream_key, uint32_t state))
1092{
1093 int ret, i;
1094 struct lttng_consumer_local_data *ctx;
1095
1096 assert(consumer_data.type == LTTNG_CONSUMER_UNKNOWN ||
1097 consumer_data.type == type);
1098 consumer_data.type = type;
1099
effcf122 1100 ctx = zmalloc(sizeof(struct lttng_consumer_local_data));
3bd1e081 1101 if (ctx == NULL) {
7a57cf92 1102 PERROR("allocating context");
3bd1e081
MD
1103 goto error;
1104 }
1105
1106 ctx->consumer_error_socket = -1;
1107 /* assign the callbacks */
1108 ctx->on_buffer_ready = buffer_ready;
1109 ctx->on_recv_channel = recv_channel;
1110 ctx->on_recv_stream = recv_stream;
1111 ctx->on_update_stream = update_stream;
1112
50f8ae69 1113 ret = pipe(ctx->consumer_data_pipe);
3bd1e081 1114 if (ret < 0) {
7a57cf92 1115 PERROR("Error creating poll pipe");
3bd1e081
MD
1116 goto error_poll_pipe;
1117 }
1118
04fdd819 1119 /* set read end of the pipe to non-blocking */
50f8ae69 1120 ret = fcntl(ctx->consumer_data_pipe[0], F_SETFL, O_NONBLOCK);
04fdd819 1121 if (ret < 0) {
7a57cf92 1122 PERROR("fcntl O_NONBLOCK");
04fdd819
MD
1123 goto error_poll_fcntl;
1124 }
1125
1126 /* set write end of the pipe to non-blocking */
50f8ae69 1127 ret = fcntl(ctx->consumer_data_pipe[1], F_SETFL, O_NONBLOCK);
04fdd819 1128 if (ret < 0) {
7a57cf92 1129 PERROR("fcntl O_NONBLOCK");
04fdd819
MD
1130 goto error_poll_fcntl;
1131 }
1132
3bd1e081
MD
1133 ret = pipe(ctx->consumer_should_quit);
1134 if (ret < 0) {
7a57cf92 1135 PERROR("Error creating recv pipe");
3bd1e081
MD
1136 goto error_quit_pipe;
1137 }
1138
1139 ret = pipe(ctx->consumer_thread_pipe);
1140 if (ret < 0) {
7a57cf92 1141 PERROR("Error creating thread pipe");
3bd1e081
MD
1142 goto error_thread_pipe;
1143 }
1144
fb3a43a9
DG
1145 ret = utils_create_pipe(ctx->consumer_metadata_pipe);
1146 if (ret < 0) {
1147 goto error_metadata_pipe;
1148 }
3bd1e081 1149
fb3a43a9
DG
1150 ret = utils_create_pipe(ctx->consumer_splice_metadata_pipe);
1151 if (ret < 0) {
1152 goto error_splice_pipe;
1153 }
1154
1155 return ctx;
3bd1e081 1156
fb3a43a9
DG
1157error_splice_pipe:
1158 utils_close_pipe(ctx->consumer_metadata_pipe);
1159error_metadata_pipe:
1160 utils_close_pipe(ctx->consumer_thread_pipe);
3bd1e081
MD
1161error_thread_pipe:
1162 for (i = 0; i < 2; i++) {
1163 int err;
1164
1165 err = close(ctx->consumer_should_quit[i]);
4c462e79
MD
1166 if (err) {
1167 PERROR("close");
1168 }
3bd1e081 1169 }
04fdd819 1170error_poll_fcntl:
3bd1e081
MD
1171error_quit_pipe:
1172 for (i = 0; i < 2; i++) {
1173 int err;
1174
50f8ae69 1175 err = close(ctx->consumer_data_pipe[i]);
4c462e79
MD
1176 if (err) {
1177 PERROR("close");
1178 }
3bd1e081
MD
1179 }
1180error_poll_pipe:
1181 free(ctx);
1182error:
1183 return NULL;
1184}
1185
1186/*
1187 * Close all fds associated with the instance and free the context.
1188 */
1189void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx)
1190{
4c462e79
MD
1191 int ret;
1192
ab1027f4
DG
1193 DBG("Consumer destroying it. Closing everything.");
1194
4c462e79
MD
1195 ret = close(ctx->consumer_error_socket);
1196 if (ret) {
1197 PERROR("close");
1198 }
1199 ret = close(ctx->consumer_thread_pipe[0]);
1200 if (ret) {
1201 PERROR("close");
1202 }
1203 ret = close(ctx->consumer_thread_pipe[1]);
1204 if (ret) {
1205 PERROR("close");
1206 }
50f8ae69 1207 ret = close(ctx->consumer_data_pipe[0]);
4c462e79
MD
1208 if (ret) {
1209 PERROR("close");
1210 }
50f8ae69 1211 ret = close(ctx->consumer_data_pipe[1]);
4c462e79
MD
1212 if (ret) {
1213 PERROR("close");
1214 }
1215 ret = close(ctx->consumer_should_quit[0]);
1216 if (ret) {
1217 PERROR("close");
1218 }
1219 ret = close(ctx->consumer_should_quit[1]);
1220 if (ret) {
1221 PERROR("close");
1222 }
fb3a43a9
DG
1223 utils_close_pipe(ctx->consumer_splice_metadata_pipe);
1224
3bd1e081
MD
1225 unlink(ctx->consumer_command_sock_path);
1226 free(ctx);
1227}
1228
6197aea7
DG
1229/*
1230 * Write the metadata stream id on the specified file descriptor.
1231 */
1232static int write_relayd_metadata_id(int fd,
1233 struct lttng_consumer_stream *stream,
1d4dfdef
DG
1234 struct consumer_relayd_sock_pair *relayd,
1235 unsigned long padding)
6197aea7
DG
1236{
1237 int ret;
1d4dfdef 1238 struct lttcomm_relayd_metadata_payload hdr;
6197aea7 1239
1d4dfdef
DG
1240 hdr.stream_id = htobe64(stream->relayd_stream_id);
1241 hdr.padding_size = htobe32(padding);
6197aea7 1242 do {
1d4dfdef 1243 ret = write(fd, (void *) &hdr, sizeof(hdr));
6197aea7
DG
1244 } while (ret < 0 && errno == EINTR);
1245 if (ret < 0) {
1246 PERROR("write metadata stream id");
1247 goto end;
1248 }
1d4dfdef
DG
1249 DBG("Metadata stream id %" PRIu64 " with padding %lu written before data",
1250 stream->relayd_stream_id, padding);
6197aea7
DG
1251
1252end:
1253 return ret;
1254}
1255
3bd1e081 1256/*
09e26845
DG
1257 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1258 * core function for writing trace buffers to either the local filesystem or
1259 * the network.
1260 *
1261 * Careful review MUST be put if any changes occur!
3bd1e081
MD
1262 *
1263 * Returns the number of bytes written
1264 */
4078b776 1265ssize_t lttng_consumer_on_read_subbuffer_mmap(
3bd1e081 1266 struct lttng_consumer_local_data *ctx,
1d4dfdef
DG
1267 struct lttng_consumer_stream *stream, unsigned long len,
1268 unsigned long padding)
3bd1e081 1269{
f02e1e8a
DG
1270 unsigned long mmap_offset;
1271 ssize_t ret = 0, written = 0;
1272 off_t orig_offset = stream->out_fd_offset;
1273 /* Default is on the disk */
1274 int outfd = stream->out_fd;
f02e1e8a 1275 struct consumer_relayd_sock_pair *relayd = NULL;
8994307f 1276 unsigned int relayd_hang_up = 0;
f02e1e8a
DG
1277
1278 /* RCU lock for the relayd pointer */
1279 rcu_read_lock();
1280
c8f59ee5
DG
1281 pthread_mutex_lock(&stream->lock);
1282
f02e1e8a
DG
1283 /* Flag that the current stream if set for network streaming. */
1284 if (stream->net_seq_idx != -1) {
1285 relayd = consumer_find_relayd(stream->net_seq_idx);
1286 if (relayd == NULL) {
1287 goto end;
1288 }
1289 }
1290
1291 /* get the offset inside the fd to mmap */
3bd1e081
MD
1292 switch (consumer_data.type) {
1293 case LTTNG_CONSUMER_KERNEL:
f02e1e8a
DG
1294 ret = kernctl_get_mmap_read_offset(stream->wait_fd, &mmap_offset);
1295 break;
7753dea8
MD
1296 case LTTNG_CONSUMER32_UST:
1297 case LTTNG_CONSUMER64_UST:
f02e1e8a
DG
1298 ret = lttng_ustctl_get_mmap_read_offset(stream->chan->handle,
1299 stream->buf, &mmap_offset);
1300 break;
3bd1e081
MD
1301 default:
1302 ERR("Unknown consumer_data type");
1303 assert(0);
1304 }
f02e1e8a
DG
1305 if (ret != 0) {
1306 errno = -ret;
1307 PERROR("tracer ctl get_mmap_read_offset");
1308 written = ret;
1309 goto end;
1310 }
b9182dd9 1311
f02e1e8a
DG
1312 /* Handle stream on the relayd if the output is on the network */
1313 if (relayd) {
1314 unsigned long netlen = len;
1315
1316 /*
1317 * Lock the control socket for the complete duration of the function
1318 * since from this point on we will use the socket.
1319 */
1320 if (stream->metadata_flag) {
1321 /* Metadata requires the control socket. */
1322 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1d4dfdef 1323 netlen += sizeof(struct lttcomm_relayd_metadata_payload);
f02e1e8a
DG
1324 }
1325
1d4dfdef 1326 ret = write_relayd_stream_header(stream, netlen, padding, relayd);
f02e1e8a
DG
1327 if (ret >= 0) {
1328 /* Use the returned socket. */
1329 outfd = ret;
1330
1331 /* Write metadata stream id before payload */
1332 if (stream->metadata_flag) {
1d4dfdef 1333 ret = write_relayd_metadata_id(outfd, stream, relayd, padding);
f02e1e8a 1334 if (ret < 0) {
f02e1e8a 1335 written = ret;
8994307f
DG
1336 /* Socket operation failed. We consider the relayd dead */
1337 if (ret == -EPIPE || ret == -EINVAL) {
1338 relayd_hang_up = 1;
1339 goto write_error;
1340 }
f02e1e8a
DG
1341 goto end;
1342 }
f02e1e8a 1343 }
8994307f
DG
1344 } else {
1345 /* Socket operation failed. We consider the relayd dead */
1346 if (ret == -EPIPE || ret == -EINVAL) {
1347 relayd_hang_up = 1;
1348 goto write_error;
1349 }
1350 /* Else, use the default set before which is the filesystem. */
f02e1e8a 1351 }
1d4dfdef
DG
1352 } else {
1353 /* No streaming, we have to set the len with the full padding */
1354 len += padding;
f02e1e8a
DG
1355 }
1356
1357 while (len > 0) {
1358 do {
1359 ret = write(outfd, stream->mmap_base + mmap_offset, len);
1360 } while (ret < 0 && errno == EINTR);
1d4dfdef 1361 DBG("Consumer mmap write() ret %zd (len %lu)", ret, len);
f02e1e8a
DG
1362 if (ret < 0) {
1363 PERROR("Error in file write");
1364 if (written == 0) {
1365 written = ret;
1366 }
8994307f
DG
1367 /* Socket operation failed. We consider the relayd dead */
1368 if (errno == EPIPE || errno == EINVAL) {
1369 relayd_hang_up = 1;
1370 goto write_error;
1371 }
f02e1e8a
DG
1372 goto end;
1373 } else if (ret > len) {
77c7c900 1374 PERROR("Error in file write (ret %zd > len %lu)", ret, len);
f02e1e8a
DG
1375 written += ret;
1376 goto end;
1377 } else {
1378 len -= ret;
1379 mmap_offset += ret;
1380 }
f02e1e8a
DG
1381
1382 /* This call is useless on a socket so better save a syscall. */
1383 if (!relayd) {
1384 /* This won't block, but will start writeout asynchronously */
1385 lttng_sync_file_range(outfd, stream->out_fd_offset, ret,
1386 SYNC_FILE_RANGE_WRITE);
1387 stream->out_fd_offset += ret;
1388 }
1389 written += ret;
1390 }
1391 lttng_consumer_sync_trace_file(stream, orig_offset);
1392
8994307f
DG
1393write_error:
1394 /*
1395 * This is a special case that the relayd has closed its socket. Let's
1396 * cleanup the relayd object and all associated streams.
1397 */
1398 if (relayd && relayd_hang_up) {
1399 cleanup_relayd(relayd, ctx);
1400 }
1401
f02e1e8a
DG
1402end:
1403 /* Unlock only if ctrl socket used */
1404 if (relayd && stream->metadata_flag) {
1405 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1406 }
6f6eda74 1407 pthread_mutex_unlock(&stream->lock);
f02e1e8a
DG
1408
1409 rcu_read_unlock();
1410 return written;
3bd1e081
MD
1411}
1412
1413/*
1414 * Splice the data from the ring buffer to the tracefile.
1415 *
1416 * Returns the number of bytes spliced.
1417 */
4078b776 1418ssize_t lttng_consumer_on_read_subbuffer_splice(
3bd1e081 1419 struct lttng_consumer_local_data *ctx,
1d4dfdef
DG
1420 struct lttng_consumer_stream *stream, unsigned long len,
1421 unsigned long padding)
3bd1e081 1422{
f02e1e8a
DG
1423 ssize_t ret = 0, written = 0, ret_splice = 0;
1424 loff_t offset = 0;
1425 off_t orig_offset = stream->out_fd_offset;
1426 int fd = stream->wait_fd;
1427 /* Default is on the disk */
1428 int outfd = stream->out_fd;
f02e1e8a 1429 struct consumer_relayd_sock_pair *relayd = NULL;
fb3a43a9 1430 int *splice_pipe;
8994307f 1431 unsigned int relayd_hang_up = 0;
f02e1e8a 1432
3bd1e081
MD
1433 switch (consumer_data.type) {
1434 case LTTNG_CONSUMER_KERNEL:
f02e1e8a 1435 break;
7753dea8
MD
1436 case LTTNG_CONSUMER32_UST:
1437 case LTTNG_CONSUMER64_UST:
f02e1e8a 1438 /* Not supported for user space tracing */
3bd1e081
MD
1439 return -ENOSYS;
1440 default:
1441 ERR("Unknown consumer_data type");
1442 assert(0);
3bd1e081
MD
1443 }
1444
f02e1e8a
DG
1445 /* RCU lock for the relayd pointer */
1446 rcu_read_lock();
1447
c8f59ee5
DG
1448 pthread_mutex_lock(&stream->lock);
1449
f02e1e8a
DG
1450 /* Flag that the current stream if set for network streaming. */
1451 if (stream->net_seq_idx != -1) {
1452 relayd = consumer_find_relayd(stream->net_seq_idx);
1453 if (relayd == NULL) {
1454 goto end;
1455 }
1456 }
1457
fb3a43a9
DG
1458 /*
1459 * Choose right pipe for splice. Metadata and trace data are handled by
1460 * different threads hence the use of two pipes in order not to race or
1461 * corrupt the written data.
1462 */
1463 if (stream->metadata_flag) {
1464 splice_pipe = ctx->consumer_splice_metadata_pipe;
1465 } else {
1466 splice_pipe = ctx->consumer_thread_pipe;
1467 }
1468
f02e1e8a 1469 /* Write metadata stream id before payload */
1d4dfdef
DG
1470 if (relayd) {
1471 int total_len = len;
f02e1e8a 1472
1d4dfdef
DG
1473 if (stream->metadata_flag) {
1474 /*
1475 * Lock the control socket for the complete duration of the function
1476 * since from this point on we will use the socket.
1477 */
1478 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1479
1480 ret = write_relayd_metadata_id(splice_pipe[1], stream, relayd,
1481 padding);
1482 if (ret < 0) {
1483 written = ret;
8994307f
DG
1484 /* Socket operation failed. We consider the relayd dead */
1485 if (ret == -EBADF) {
1486 WARN("Remote relayd disconnected. Stopping");
1487 relayd_hang_up = 1;
1488 goto write_error;
1489 }
1d4dfdef
DG
1490 goto end;
1491 }
1492
1493 total_len += sizeof(struct lttcomm_relayd_metadata_payload);
1494 }
1495
1496 ret = write_relayd_stream_header(stream, total_len, padding, relayd);
1497 if (ret >= 0) {
1498 /* Use the returned socket. */
1499 outfd = ret;
1500 } else {
8994307f
DG
1501 /* Socket operation failed. We consider the relayd dead */
1502 if (ret == -EBADF) {
1503 WARN("Remote relayd disconnected. Stopping");
1504 relayd_hang_up = 1;
1505 goto write_error;
1506 }
f02e1e8a
DG
1507 goto end;
1508 }
1d4dfdef
DG
1509 } else {
1510 /* No streaming, we have to set the len with the full padding */
1511 len += padding;
f02e1e8a
DG
1512 }
1513
1514 while (len > 0) {
1d4dfdef
DG
1515 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1516 (unsigned long)offset, len, fd, splice_pipe[1]);
fb3a43a9 1517 ret_splice = splice(fd, &offset, splice_pipe[1], NULL, len,
f02e1e8a
DG
1518 SPLICE_F_MOVE | SPLICE_F_MORE);
1519 DBG("splice chan to pipe, ret %zd", ret_splice);
1520 if (ret_splice < 0) {
1521 PERROR("Error in relay splice");
1522 if (written == 0) {
1523 written = ret_splice;
1524 }
1525 ret = errno;
1526 goto splice_error;
1527 }
1528
1529 /* Handle stream on the relayd if the output is on the network */
1530 if (relayd) {
1531 if (stream->metadata_flag) {
1d4dfdef
DG
1532 size_t metadata_payload_size =
1533 sizeof(struct lttcomm_relayd_metadata_payload);
1534
f02e1e8a 1535 /* Update counter to fit the spliced data */
1d4dfdef
DG
1536 ret_splice += metadata_payload_size;
1537 len += metadata_payload_size;
f02e1e8a
DG
1538 /*
1539 * We do this so the return value can match the len passed as
1540 * argument to this function.
1541 */
1d4dfdef 1542 written -= metadata_payload_size;
f02e1e8a
DG
1543 }
1544 }
1545
1546 /* Splice data out */
fb3a43a9 1547 ret_splice = splice(splice_pipe[0], NULL, outfd, NULL,
f02e1e8a 1548 ret_splice, SPLICE_F_MOVE | SPLICE_F_MORE);
1d4dfdef 1549 DBG("Consumer splice pipe to file, ret %zd", ret_splice);
f02e1e8a
DG
1550 if (ret_splice < 0) {
1551 PERROR("Error in file splice");
1552 if (written == 0) {
1553 written = ret_splice;
1554 }
8994307f 1555 /* Socket operation failed. We consider the relayd dead */
00c8752b 1556 if (errno == EBADF || errno == EPIPE) {
8994307f
DG
1557 WARN("Remote relayd disconnected. Stopping");
1558 relayd_hang_up = 1;
1559 goto write_error;
1560 }
f02e1e8a
DG
1561 ret = errno;
1562 goto splice_error;
1563 } else if (ret_splice > len) {
1564 errno = EINVAL;
1565 PERROR("Wrote more data than requested %zd (len: %lu)",
1566 ret_splice, len);
1567 written += ret_splice;
1568 ret = errno;
1569 goto splice_error;
1570 }
1571 len -= ret_splice;
1572
1573 /* This call is useless on a socket so better save a syscall. */
1574 if (!relayd) {
1575 /* This won't block, but will start writeout asynchronously */
1576 lttng_sync_file_range(outfd, stream->out_fd_offset, ret_splice,
1577 SYNC_FILE_RANGE_WRITE);
1578 stream->out_fd_offset += ret_splice;
1579 }
1580 written += ret_splice;
1581 }
1582 lttng_consumer_sync_trace_file(stream, orig_offset);
1583
1584 ret = ret_splice;
1585
1586 goto end;
1587
8994307f
DG
1588write_error:
1589 /*
1590 * This is a special case that the relayd has closed its socket. Let's
1591 * cleanup the relayd object and all associated streams.
1592 */
1593 if (relayd && relayd_hang_up) {
1594 cleanup_relayd(relayd, ctx);
1595 /* Skip splice error so the consumer does not fail */
1596 goto end;
1597 }
1598
f02e1e8a
DG
1599splice_error:
1600 /* send the appropriate error description to sessiond */
1601 switch (ret) {
f02e1e8a 1602 case EINVAL:
f73fabfd 1603 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_EINVAL);
f02e1e8a
DG
1604 break;
1605 case ENOMEM:
f73fabfd 1606 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ENOMEM);
f02e1e8a
DG
1607 break;
1608 case ESPIPE:
f73fabfd 1609 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ESPIPE);
f02e1e8a
DG
1610 break;
1611 }
1612
1613end:
1614 if (relayd && stream->metadata_flag) {
1615 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1616 }
6f6eda74 1617 pthread_mutex_unlock(&stream->lock);
f02e1e8a
DG
1618
1619 rcu_read_unlock();
1620 return written;
3bd1e081
MD
1621}
1622
1623/*
1624 * Take a snapshot for a specific fd
1625 *
1626 * Returns 0 on success, < 0 on error
1627 */
1628int lttng_consumer_take_snapshot(struct lttng_consumer_local_data *ctx,
1629 struct lttng_consumer_stream *stream)
1630{
1631 switch (consumer_data.type) {
1632 case LTTNG_CONSUMER_KERNEL:
1633 return lttng_kconsumer_take_snapshot(ctx, stream);
7753dea8
MD
1634 case LTTNG_CONSUMER32_UST:
1635 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
1636 return lttng_ustconsumer_take_snapshot(ctx, stream);
1637 default:
1638 ERR("Unknown consumer_data type");
1639 assert(0);
1640 return -ENOSYS;
1641 }
1642
1643}
1644
1645/*
1646 * Get the produced position
1647 *
1648 * Returns 0 on success, < 0 on error
1649 */
1650int lttng_consumer_get_produced_snapshot(
1651 struct lttng_consumer_local_data *ctx,
1652 struct lttng_consumer_stream *stream,
1653 unsigned long *pos)
1654{
1655 switch (consumer_data.type) {
1656 case LTTNG_CONSUMER_KERNEL:
1657 return lttng_kconsumer_get_produced_snapshot(ctx, stream, pos);
7753dea8
MD
1658 case LTTNG_CONSUMER32_UST:
1659 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
1660 return lttng_ustconsumer_get_produced_snapshot(ctx, stream, pos);
1661 default:
1662 ERR("Unknown consumer_data type");
1663 assert(0);
1664 return -ENOSYS;
1665 }
1666}
1667
1668int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx,
1669 int sock, struct pollfd *consumer_sockpoll)
1670{
1671 switch (consumer_data.type) {
1672 case LTTNG_CONSUMER_KERNEL:
1673 return lttng_kconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
7753dea8
MD
1674 case LTTNG_CONSUMER32_UST:
1675 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
1676 return lttng_ustconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
1677 default:
1678 ERR("Unknown consumer_data type");
1679 assert(0);
1680 return -ENOSYS;
1681 }
1682}
1683
43c34bc3
DG
1684/*
1685 * Iterate over all streams of the hashtable and free them properly.
1686 *
1687 * WARNING: *MUST* be used with data stream only.
1688 */
1689static void destroy_data_stream_ht(struct lttng_ht *ht)
1690{
1691 int ret;
1692 struct lttng_ht_iter iter;
1693 struct lttng_consumer_stream *stream;
1694
1695 if (ht == NULL) {
1696 return;
1697 }
1698
1699 rcu_read_lock();
1700 cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) {
1701 ret = lttng_ht_del(ht, &iter);
1702 assert(!ret);
1703
1704 call_rcu(&stream->node.head, consumer_free_stream);
1705 }
1706 rcu_read_unlock();
1707
1708 lttng_ht_destroy(ht);
1709}
1710
fb3a43a9 1711/*
f724d81e 1712 * Iterate over all streams of the hashtable and free them properly.
e316aad5
DG
1713 *
1714 * XXX: Should not be only for metadata stream or else use an other name.
fb3a43a9
DG
1715 */
1716static void destroy_stream_ht(struct lttng_ht *ht)
1717{
1718 int ret;
1719 struct lttng_ht_iter iter;
1720 struct lttng_consumer_stream *stream;
1721
1722 if (ht == NULL) {
1723 return;
1724 }
1725
d09e1200 1726 rcu_read_lock();
58b1f425 1727 cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) {
fb3a43a9
DG
1728 ret = lttng_ht_del(ht, &iter);
1729 assert(!ret);
1730
58b1f425 1731 call_rcu(&stream->node.head, consumer_free_stream);
fb3a43a9 1732 }
d09e1200 1733 rcu_read_unlock();
fb3a43a9
DG
1734
1735 lttng_ht_destroy(ht);
1736}
1737
1738/*
1739 * Clean up a metadata stream and free its memory.
1740 */
e316aad5
DG
1741void consumer_del_metadata_stream(struct lttng_consumer_stream *stream,
1742 struct lttng_ht *ht)
fb3a43a9
DG
1743{
1744 int ret;
e316aad5
DG
1745 struct lttng_ht_iter iter;
1746 struct lttng_consumer_channel *free_chan = NULL;
fb3a43a9
DG
1747 struct consumer_relayd_sock_pair *relayd;
1748
1749 assert(stream);
1750 /*
1751 * This call should NEVER receive regular stream. It must always be
1752 * metadata stream and this is crucial for data structure synchronization.
1753 */
1754 assert(stream->metadata_flag);
1755
e316aad5
DG
1756 DBG3("Consumer delete metadata stream %d", stream->wait_fd);
1757
1758 if (ht == NULL) {
1759 /* Means the stream was allocated but not successfully added */
1760 goto free_stream;
1761 }
1762
8994307f
DG
1763 pthread_mutex_lock(&stream->lock);
1764
fb3a43a9
DG
1765 pthread_mutex_lock(&consumer_data.lock);
1766 switch (consumer_data.type) {
1767 case LTTNG_CONSUMER_KERNEL:
1768 if (stream->mmap_base != NULL) {
1769 ret = munmap(stream->mmap_base, stream->mmap_len);
1770 if (ret != 0) {
1771 PERROR("munmap metadata stream");
1772 }
1773 }
1774 break;
1775 case LTTNG_CONSUMER32_UST:
1776 case LTTNG_CONSUMER64_UST:
1777 lttng_ustconsumer_del_stream(stream);
1778 break;
1779 default:
1780 ERR("Unknown consumer_data type");
1781 assert(0);
e316aad5 1782 goto end;
fb3a43a9 1783 }
fb3a43a9 1784
c869f647 1785 rcu_read_lock();
58b1f425 1786 iter.iter.node = &stream->node.node;
c869f647
DG
1787 ret = lttng_ht_del(ht, &iter);
1788 assert(!ret);
ca22feea
DG
1789
1790 /* Remove node session id from the consumer_data stream ht */
1791 iter.iter.node = &stream->node_session_id.node;
1792 ret = lttng_ht_del(consumer_data.stream_list_ht, &iter);
1793 assert(!ret);
c869f647
DG
1794 rcu_read_unlock();
1795
fb3a43a9
DG
1796 if (stream->out_fd >= 0) {
1797 ret = close(stream->out_fd);
1798 if (ret) {
1799 PERROR("close");
1800 }
1801 }
1802
1803 if (stream->wait_fd >= 0 && !stream->wait_fd_is_copy) {
1804 ret = close(stream->wait_fd);
1805 if (ret) {
1806 PERROR("close");
1807 }
1808 }
1809
1810 if (stream->shm_fd >= 0 && stream->wait_fd != stream->shm_fd) {
1811 ret = close(stream->shm_fd);
1812 if (ret) {
1813 PERROR("close");
1814 }
1815 }
1816
1817 /* Check and cleanup relayd */
1818 rcu_read_lock();
1819 relayd = consumer_find_relayd(stream->net_seq_idx);
1820 if (relayd != NULL) {
1821 uatomic_dec(&relayd->refcount);
1822 assert(uatomic_read(&relayd->refcount) >= 0);
1823
1824 /* Closing streams requires to lock the control socket. */
1825 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1826 ret = relayd_send_close_stream(&relayd->control_sock,
1827 stream->relayd_stream_id, stream->next_net_seq_num - 1);
1828 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1829 if (ret < 0) {
1830 DBG("Unable to close stream on the relayd. Continuing");
1831 /*
1832 * Continue here. There is nothing we can do for the relayd.
1833 * Chances are that the relayd has closed the socket so we just
1834 * continue cleaning up.
1835 */
1836 }
1837
1838 /* Both conditions are met, we destroy the relayd. */
1839 if (uatomic_read(&relayd->refcount) == 0 &&
1840 uatomic_read(&relayd->destroy_flag)) {
d09e1200 1841 destroy_relayd(relayd);
fb3a43a9
DG
1842 }
1843 }
1844 rcu_read_unlock();
1845
1846 /* Atomically decrement channel refcount since other threads can use it. */
1847 uatomic_dec(&stream->chan->refcount);
c30aaa51
MD
1848 if (!uatomic_read(&stream->chan->refcount)
1849 && !uatomic_read(&stream->chan->nb_init_streams)) {
1850 /* Go for channel deletion! */
e316aad5 1851 free_chan = stream->chan;
fb3a43a9
DG
1852 }
1853
e316aad5
DG
1854end:
1855 pthread_mutex_unlock(&consumer_data.lock);
8994307f 1856 pthread_mutex_unlock(&stream->lock);
e316aad5
DG
1857
1858 if (free_chan) {
1859 consumer_del_channel(free_chan);
1860 }
1861
1862free_stream:
58b1f425 1863 call_rcu(&stream->node.head, consumer_free_stream);
fb3a43a9
DG
1864}
1865
1866/*
1867 * Action done with the metadata stream when adding it to the consumer internal
1868 * data structures to handle it.
1869 */
e316aad5
DG
1870static int consumer_add_metadata_stream(struct lttng_consumer_stream *stream,
1871 struct lttng_ht *ht)
fb3a43a9 1872{
e316aad5 1873 int ret = 0;
fb3a43a9
DG
1874 struct consumer_relayd_sock_pair *relayd;
1875
e316aad5
DG
1876 assert(stream);
1877 assert(ht);
1878
1879 DBG3("Adding metadata stream %d to hash table", stream->wait_fd);
1880
1881 pthread_mutex_lock(&consumer_data.lock);
1882
e316aad5
DG
1883 /*
1884 * From here, refcounts are updated so be _careful_ when returning an error
1885 * after this point.
1886 */
1887
fb3a43a9 1888 rcu_read_lock();
e316aad5 1889 /* Find relayd and, if one is found, increment refcount. */
fb3a43a9
DG
1890 relayd = consumer_find_relayd(stream->net_seq_idx);
1891 if (relayd != NULL) {
1892 uatomic_inc(&relayd->refcount);
1893 }
e316aad5
DG
1894
1895 /* Update channel refcount once added without error(s). */
1896 uatomic_inc(&stream->chan->refcount);
1897
1898 /*
1899 * When nb_init_streams reaches 0, we don't need to trigger any action in
1900 * terms of destroying the associated channel, because the action that
1901 * causes the count to become 0 also causes a stream to be added. The
1902 * channel deletion will thus be triggered by the following removal of this
1903 * stream.
1904 */
1905 if (uatomic_read(&stream->chan->nb_init_streams) > 0) {
1906 uatomic_dec(&stream->chan->nb_init_streams);
1907 }
1908
43c34bc3
DG
1909 /* Steal stream identifier to avoid having streams with the same key */
1910 consumer_steal_stream_key(stream->key, ht);
1911
58b1f425 1912 lttng_ht_add_unique_ulong(ht, &stream->node);
ca22feea
DG
1913
1914 /*
1915 * Add stream to the stream_list_ht of the consumer data. No need to steal
1916 * the key since the HT does not use it and we allow to add redundant keys
1917 * into this table.
1918 */
1919 lttng_ht_add_ulong(consumer_data.stream_list_ht, &stream->node_session_id);
1920
fb3a43a9 1921 rcu_read_unlock();
e316aad5 1922
e316aad5
DG
1923 pthread_mutex_unlock(&consumer_data.lock);
1924 return ret;
fb3a43a9
DG
1925}
1926
8994307f
DG
1927/*
1928 * Delete data stream that are flagged for deletion (endpoint_status).
1929 */
1930static void validate_endpoint_status_data_stream(void)
1931{
1932 struct lttng_ht_iter iter;
1933 struct lttng_consumer_stream *stream;
1934
1935 DBG("Consumer delete flagged data stream");
1936
1937 rcu_read_lock();
1938 cds_lfht_for_each_entry(data_ht->ht, &iter.iter, stream, node.node) {
1939 /* Validate delete flag of the stream */
9617607b 1940 if (stream->endpoint_status != CONSUMER_ENDPOINT_INACTIVE) {
8994307f
DG
1941 continue;
1942 }
1943 /* Delete it right now */
1944 consumer_del_stream(stream, data_ht);
1945 }
1946 rcu_read_unlock();
1947}
1948
1949/*
1950 * Delete metadata stream that are flagged for deletion (endpoint_status).
1951 */
1952static void validate_endpoint_status_metadata_stream(
1953 struct lttng_poll_event *pollset)
1954{
1955 struct lttng_ht_iter iter;
1956 struct lttng_consumer_stream *stream;
1957
1958 DBG("Consumer delete flagged metadata stream");
1959
1960 assert(pollset);
1961
1962 rcu_read_lock();
1963 cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream, node.node) {
1964 /* Validate delete flag of the stream */
1965 if (!stream->endpoint_status) {
1966 continue;
1967 }
1968 /*
1969 * Remove from pollset so the metadata thread can continue without
1970 * blocking on a deleted stream.
1971 */
1972 lttng_poll_del(pollset, stream->wait_fd);
1973
1974 /* Delete it right now */
1975 consumer_del_metadata_stream(stream, metadata_ht);
1976 }
1977 rcu_read_unlock();
1978}
1979
fb3a43a9
DG
1980/*
1981 * Thread polls on metadata file descriptor and write them on disk or on the
1982 * network.
1983 */
7d980def 1984void *consumer_thread_metadata_poll(void *data)
fb3a43a9
DG
1985{
1986 int ret, i, pollfd;
1987 uint32_t revents, nb_fd;
e316aad5 1988 struct lttng_consumer_stream *stream = NULL;
fb3a43a9
DG
1989 struct lttng_ht_iter iter;
1990 struct lttng_ht_node_ulong *node;
fb3a43a9
DG
1991 struct lttng_poll_event events;
1992 struct lttng_consumer_local_data *ctx = data;
1993 ssize_t len;
1994
1995 rcu_register_thread();
1996
1997 DBG("Thread metadata poll started");
1998
fb3a43a9
DG
1999 /* Size is set to 1 for the consumer_metadata pipe */
2000 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
2001 if (ret < 0) {
2002 ERR("Poll set creation failed");
2003 goto end;
2004 }
2005
2006 ret = lttng_poll_add(&events, ctx->consumer_metadata_pipe[0], LPOLLIN);
2007 if (ret < 0) {
2008 goto end;
2009 }
2010
2011 /* Main loop */
2012 DBG("Metadata main loop started");
2013
2014 while (1) {
2015 lttng_poll_reset(&events);
2016
2017 nb_fd = LTTNG_POLL_GETNB(&events);
2018
2019 /* Only the metadata pipe is set */
2020 if (nb_fd == 0 && consumer_quit == 1) {
2021 goto end;
2022 }
2023
2024restart:
2025 DBG("Metadata poll wait with %d fd(s)", nb_fd);
2026 ret = lttng_poll_wait(&events, -1);
2027 DBG("Metadata event catched in thread");
2028 if (ret < 0) {
2029 if (errno == EINTR) {
e316aad5 2030 ERR("Poll EINTR catched");
fb3a43a9
DG
2031 goto restart;
2032 }
2033 goto error;
2034 }
2035
e316aad5 2036 /* From here, the event is a metadata wait fd */
fb3a43a9
DG
2037 for (i = 0; i < nb_fd; i++) {
2038 revents = LTTNG_POLL_GETEV(&events, i);
2039 pollfd = LTTNG_POLL_GETFD(&events, i);
2040
e316aad5
DG
2041 /* Just don't waste time if no returned events for the fd */
2042 if (!revents) {
2043 continue;
2044 }
2045
fb3a43a9 2046 if (pollfd == ctx->consumer_metadata_pipe[0]) {
4adabd61 2047 if (revents & (LPOLLERR | LPOLLHUP )) {
fb3a43a9
DG
2048 DBG("Metadata thread pipe hung up");
2049 /*
2050 * Remove the pipe from the poll set and continue the loop
2051 * since their might be data to consume.
2052 */
2053 lttng_poll_del(&events, ctx->consumer_metadata_pipe[0]);
f66c074c
DG
2054 ret = close(ctx->consumer_metadata_pipe[0]);
2055 if (ret < 0) {
2056 PERROR("close metadata pipe");
2057 }
fb3a43a9
DG
2058 continue;
2059 } else if (revents & LPOLLIN) {
fb3a43a9 2060 do {
633d0084
DG
2061 /* Get the stream pointer received */
2062 ret = read(pollfd, &stream, sizeof(stream));
fb3a43a9 2063 } while (ret < 0 && errno == EINTR);
633d0084
DG
2064 if (ret < 0 ||
2065 ret < sizeof(struct lttng_consumer_stream *)) {
fb3a43a9 2066 PERROR("read metadata stream");
fb3a43a9
DG
2067 /*
2068 * Let's continue here and hope we can still work
2069 * without stopping the consumer. XXX: Should we?
2070 */
2071 continue;
2072 }
2073
8994307f
DG
2074 /* A NULL stream means that the state has changed. */
2075 if (stream == NULL) {
2076 /* Check for deleted streams. */
2077 validate_endpoint_status_metadata_stream(&events);
2078 continue;
2079 }
2080
fb3a43a9
DG
2081 DBG("Adding metadata stream %d to poll set",
2082 stream->wait_fd);
2083
e316aad5
DG
2084 ret = consumer_add_metadata_stream(stream, metadata_ht);
2085 if (ret) {
2086 ERR("Unable to add metadata stream");
2087 /* Stream was not setup properly. Continuing. */
2088 consumer_del_metadata_stream(stream, NULL);
2089 continue;
2090 }
fb3a43a9
DG
2091
2092 /* Add metadata stream to the global poll events list */
2093 lttng_poll_add(&events, stream->wait_fd,
2094 LPOLLIN | LPOLLPRI);
fb3a43a9
DG
2095 }
2096
e316aad5 2097 /* Handle other stream */
fb3a43a9
DG
2098 continue;
2099 }
2100
d09e1200 2101 rcu_read_lock();
fb3a43a9
DG
2102 lttng_ht_lookup(metadata_ht, (void *)((unsigned long) pollfd),
2103 &iter);
2104 node = lttng_ht_iter_get_node_ulong(&iter);
e316aad5 2105 assert(node);
fb3a43a9
DG
2106
2107 stream = caa_container_of(node, struct lttng_consumer_stream,
58b1f425 2108 node);
fb3a43a9 2109
e316aad5 2110 /* Check for error event */
4adabd61 2111 if (revents & (LPOLLERR | LPOLLHUP)) {
e316aad5 2112 DBG("Metadata fd %d is hup|err.", pollfd);
fb3a43a9
DG
2113 if (!stream->hangup_flush_done
2114 && (consumer_data.type == LTTNG_CONSUMER32_UST
2115 || consumer_data.type == LTTNG_CONSUMER64_UST)) {
2116 DBG("Attempting to flush and consume the UST buffers");
2117 lttng_ustconsumer_on_stream_hangup(stream);
2118
2119 /* We just flushed the stream now read it. */
4bb94b75
DG
2120 do {
2121 len = ctx->on_buffer_ready(stream, ctx);
2122 /*
2123 * We don't check the return value here since if we get
2124 * a negative len, it means an error occured thus we
2125 * simply remove it from the poll set and free the
2126 * stream.
2127 */
2128 } while (len > 0);
fb3a43a9
DG
2129 }
2130
fb3a43a9 2131 lttng_poll_del(&events, stream->wait_fd);
e316aad5
DG
2132 /*
2133 * This call update the channel states, closes file descriptors
2134 * and securely free the stream.
2135 */
2136 consumer_del_metadata_stream(stream, metadata_ht);
2137 } else if (revents & (LPOLLIN | LPOLLPRI)) {
2138 /* Get the data out of the metadata file descriptor */
2139 DBG("Metadata available on fd %d", pollfd);
2140 assert(stream->wait_fd == pollfd);
2141
2142 len = ctx->on_buffer_ready(stream, ctx);
2143 /* It's ok to have an unavailable sub-buffer */
b64403e3 2144 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
ab1027f4
DG
2145 /* Clean up stream from consumer and free it. */
2146 lttng_poll_del(&events, stream->wait_fd);
2147 consumer_del_metadata_stream(stream, metadata_ht);
e316aad5
DG
2148 } else if (len > 0) {
2149 stream->data_read = 1;
2150 }
fb3a43a9 2151 }
e316aad5
DG
2152
2153 /* Release RCU lock for the stream looked up */
d09e1200 2154 rcu_read_unlock();
fb3a43a9
DG
2155 }
2156 }
2157
2158error:
2159end:
2160 DBG("Metadata poll thread exiting");
2161 lttng_poll_clean(&events);
2162
2163 if (metadata_ht) {
2164 destroy_stream_ht(metadata_ht);
2165 }
2166
2167 rcu_unregister_thread();
2168 return NULL;
2169}
2170
3bd1e081 2171/*
e4421fec 2172 * This thread polls the fds in the set to consume the data and write
3bd1e081
MD
2173 * it to tracefile if necessary.
2174 */
7d980def 2175void *consumer_thread_data_poll(void *data)
3bd1e081
MD
2176{
2177 int num_rdy, num_hup, high_prio, ret, i;
2178 struct pollfd *pollfd = NULL;
2179 /* local view of the streams */
c869f647 2180 struct lttng_consumer_stream **local_stream = NULL, *new_stream = NULL;
3bd1e081
MD
2181 /* local view of consumer_data.fds_count */
2182 int nb_fd = 0;
3bd1e081 2183 struct lttng_consumer_local_data *ctx = data;
00e2e675 2184 ssize_t len;
3bd1e081 2185
e7b994a3
DG
2186 rcu_register_thread();
2187
43c34bc3
DG
2188 data_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
2189 if (data_ht == NULL) {
2190 goto end;
2191 }
2192
effcf122 2193 local_stream = zmalloc(sizeof(struct lttng_consumer_stream));
3bd1e081
MD
2194
2195 while (1) {
2196 high_prio = 0;
2197 num_hup = 0;
2198
2199 /*
e4421fec 2200 * the fds set has been updated, we need to update our
3bd1e081
MD
2201 * local array as well
2202 */
2203 pthread_mutex_lock(&consumer_data.lock);
2204 if (consumer_data.need_update) {
2205 if (pollfd != NULL) {
2206 free(pollfd);
2207 pollfd = NULL;
2208 }
2209 if (local_stream != NULL) {
2210 free(local_stream);
2211 local_stream = NULL;
2212 }
2213
50f8ae69 2214 /* allocate for all fds + 1 for the consumer_data_pipe */
effcf122 2215 pollfd = zmalloc((consumer_data.stream_count + 1) * sizeof(struct pollfd));
3bd1e081 2216 if (pollfd == NULL) {
7a57cf92 2217 PERROR("pollfd malloc");
3bd1e081
MD
2218 pthread_mutex_unlock(&consumer_data.lock);
2219 goto end;
2220 }
2221
50f8ae69 2222 /* allocate for all fds + 1 for the consumer_data_pipe */
effcf122 2223 local_stream = zmalloc((consumer_data.stream_count + 1) *
3bd1e081
MD
2224 sizeof(struct lttng_consumer_stream));
2225 if (local_stream == NULL) {
7a57cf92 2226 PERROR("local_stream malloc");
3bd1e081
MD
2227 pthread_mutex_unlock(&consumer_data.lock);
2228 goto end;
2229 }
43c34bc3
DG
2230 ret = consumer_update_poll_array(ctx, &pollfd, local_stream,
2231 data_ht);
3bd1e081
MD
2232 if (ret < 0) {
2233 ERR("Error in allocating pollfd or local_outfds");
f73fabfd 2234 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
3bd1e081
MD
2235 pthread_mutex_unlock(&consumer_data.lock);
2236 goto end;
2237 }
2238 nb_fd = ret;
2239 consumer_data.need_update = 0;
2240 }
2241 pthread_mutex_unlock(&consumer_data.lock);
2242
4078b776
MD
2243 /* No FDs and consumer_quit, consumer_cleanup the thread */
2244 if (nb_fd == 0 && consumer_quit == 1) {
2245 goto end;
2246 }
3bd1e081 2247 /* poll on the array of fds */
88f2b785 2248 restart:
3bd1e081 2249 DBG("polling on %d fd", nb_fd + 1);
cb365c03 2250 num_rdy = poll(pollfd, nb_fd + 1, -1);
3bd1e081
MD
2251 DBG("poll num_rdy : %d", num_rdy);
2252 if (num_rdy == -1) {
88f2b785
MD
2253 /*
2254 * Restart interrupted system call.
2255 */
2256 if (errno == EINTR) {
2257 goto restart;
2258 }
7a57cf92 2259 PERROR("Poll error");
f73fabfd 2260 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
3bd1e081
MD
2261 goto end;
2262 } else if (num_rdy == 0) {
2263 DBG("Polling thread timed out");
2264 goto end;
2265 }
2266
3bd1e081 2267 /*
50f8ae69 2268 * If the consumer_data_pipe triggered poll go directly to the
00e2e675
DG
2269 * beginning of the loop to update the array. We want to prioritize
2270 * array update over low-priority reads.
3bd1e081 2271 */
509bb1cf 2272 if (pollfd[nb_fd].revents & (POLLIN | POLLPRI)) {
04fdd819 2273 size_t pipe_readlen;
04fdd819 2274
50f8ae69 2275 DBG("consumer_data_pipe wake up");
04fdd819
MD
2276 /* Consume 1 byte of pipe data */
2277 do {
50f8ae69 2278 pipe_readlen = read(ctx->consumer_data_pipe[0], &new_stream,
c869f647 2279 sizeof(new_stream));
04fdd819 2280 } while (pipe_readlen == -1 && errno == EINTR);
c869f647
DG
2281
2282 /*
2283 * If the stream is NULL, just ignore it. It's also possible that
2284 * the sessiond poll thread changed the consumer_quit state and is
2285 * waking us up to test it.
2286 */
2287 if (new_stream == NULL) {
8994307f 2288 validate_endpoint_status_data_stream();
c869f647
DG
2289 continue;
2290 }
2291
43c34bc3 2292 ret = consumer_add_stream(new_stream, data_ht);
c869f647
DG
2293 if (ret) {
2294 ERR("Consumer add stream %d failed. Continuing",
2295 new_stream->key);
2296 /*
2297 * At this point, if the add_stream fails, it is not in the
2298 * hash table thus passing the NULL value here.
2299 */
2300 consumer_del_stream(new_stream, NULL);
2301 }
2302
2303 /* Continue to update the local streams and handle prio ones */
3bd1e081
MD
2304 continue;
2305 }
2306
2307 /* Take care of high priority channels first. */
2308 for (i = 0; i < nb_fd; i++) {
9617607b
DG
2309 if (local_stream[i] == NULL) {
2310 continue;
2311 }
fb3a43a9 2312 if (pollfd[i].revents & POLLPRI) {
d41f73b7
MD
2313 DBG("Urgent read on fd %d", pollfd[i].fd);
2314 high_prio = 1;
4078b776 2315 len = ctx->on_buffer_ready(local_stream[i], ctx);
d41f73b7 2316 /* it's ok to have an unavailable sub-buffer */
b64403e3 2317 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
ab1027f4
DG
2318 /* Clean the stream and free it. */
2319 consumer_del_stream(local_stream[i], data_ht);
9617607b 2320 local_stream[i] = NULL;
4078b776
MD
2321 } else if (len > 0) {
2322 local_stream[i]->data_read = 1;
d41f73b7 2323 }
3bd1e081
MD
2324 }
2325 }
2326
4078b776
MD
2327 /*
2328 * If we read high prio channel in this loop, try again
2329 * for more high prio data.
2330 */
2331 if (high_prio) {
3bd1e081
MD
2332 continue;
2333 }
2334
2335 /* Take care of low priority channels. */
4078b776 2336 for (i = 0; i < nb_fd; i++) {
9617607b
DG
2337 if (local_stream[i] == NULL) {
2338 continue;
2339 }
4078b776
MD
2340 if ((pollfd[i].revents & POLLIN) ||
2341 local_stream[i]->hangup_flush_done) {
4078b776
MD
2342 DBG("Normal read on fd %d", pollfd[i].fd);
2343 len = ctx->on_buffer_ready(local_stream[i], ctx);
2344 /* it's ok to have an unavailable sub-buffer */
b64403e3 2345 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
ab1027f4
DG
2346 /* Clean the stream and free it. */
2347 consumer_del_stream(local_stream[i], data_ht);
9617607b 2348 local_stream[i] = NULL;
4078b776
MD
2349 } else if (len > 0) {
2350 local_stream[i]->data_read = 1;
2351 }
2352 }
2353 }
2354
2355 /* Handle hangup and errors */
2356 for (i = 0; i < nb_fd; i++) {
9617607b
DG
2357 if (local_stream[i] == NULL) {
2358 continue;
2359 }
4078b776
MD
2360 if (!local_stream[i]->hangup_flush_done
2361 && (pollfd[i].revents & (POLLHUP | POLLERR | POLLNVAL))
2362 && (consumer_data.type == LTTNG_CONSUMER32_UST
2363 || consumer_data.type == LTTNG_CONSUMER64_UST)) {
2364 DBG("fd %d is hup|err|nval. Attempting flush and read.",
9617607b 2365 pollfd[i].fd);
4078b776
MD
2366 lttng_ustconsumer_on_stream_hangup(local_stream[i]);
2367 /* Attempt read again, for the data we just flushed. */
2368 local_stream[i]->data_read = 1;
2369 }
2370 /*
2371 * If the poll flag is HUP/ERR/NVAL and we have
2372 * read no data in this pass, we can remove the
2373 * stream from its hash table.
2374 */
2375 if ((pollfd[i].revents & POLLHUP)) {
2376 DBG("Polling fd %d tells it has hung up.", pollfd[i].fd);
2377 if (!local_stream[i]->data_read) {
43c34bc3 2378 consumer_del_stream(local_stream[i], data_ht);
9617607b 2379 local_stream[i] = NULL;
4078b776
MD
2380 num_hup++;
2381 }
2382 } else if (pollfd[i].revents & POLLERR) {
2383 ERR("Error returned in polling fd %d.", pollfd[i].fd);
2384 if (!local_stream[i]->data_read) {
43c34bc3 2385 consumer_del_stream(local_stream[i], data_ht);
9617607b 2386 local_stream[i] = NULL;
4078b776
MD
2387 num_hup++;
2388 }
2389 } else if (pollfd[i].revents & POLLNVAL) {
2390 ERR("Polling fd %d tells fd is not open.", pollfd[i].fd);
2391 if (!local_stream[i]->data_read) {
43c34bc3 2392 consumer_del_stream(local_stream[i], data_ht);
9617607b 2393 local_stream[i] = NULL;
4078b776 2394 num_hup++;
3bd1e081
MD
2395 }
2396 }
9617607b
DG
2397 if (local_stream[i] != NULL) {
2398 local_stream[i]->data_read = 0;
2399 }
3bd1e081
MD
2400 }
2401 }
2402end:
2403 DBG("polling thread exiting");
2404 if (pollfd != NULL) {
2405 free(pollfd);
2406 pollfd = NULL;
2407 }
2408 if (local_stream != NULL) {
2409 free(local_stream);
2410 local_stream = NULL;
2411 }
fb3a43a9
DG
2412
2413 /*
2414 * Close the write side of the pipe so epoll_wait() in
7d980def
DG
2415 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2416 * read side of the pipe. If we close them both, epoll_wait strangely does
2417 * not return and could create a endless wait period if the pipe is the
2418 * only tracked fd in the poll set. The thread will take care of closing
2419 * the read side.
fb3a43a9 2420 */
f66c074c
DG
2421 ret = close(ctx->consumer_metadata_pipe[1]);
2422 if (ret < 0) {
2423 PERROR("close data pipe");
2424 }
fb3a43a9 2425
43c34bc3
DG
2426 if (data_ht) {
2427 destroy_data_stream_ht(data_ht);
2428 }
2429
e7b994a3 2430 rcu_unregister_thread();
3bd1e081
MD
2431 return NULL;
2432}
2433
2434/*
2435 * This thread listens on the consumerd socket and receives the file
2436 * descriptors from the session daemon.
2437 */
7d980def 2438void *consumer_thread_sessiond_poll(void *data)
3bd1e081
MD
2439{
2440 int sock, client_socket, ret;
2441 /*
2442 * structure to poll for incoming data on communication socket avoids
2443 * making blocking sockets.
2444 */
2445 struct pollfd consumer_sockpoll[2];
2446 struct lttng_consumer_local_data *ctx = data;
2447
e7b994a3
DG
2448 rcu_register_thread();
2449
3bd1e081
MD
2450 DBG("Creating command socket %s", ctx->consumer_command_sock_path);
2451 unlink(ctx->consumer_command_sock_path);
2452 client_socket = lttcomm_create_unix_sock(ctx->consumer_command_sock_path);
2453 if (client_socket < 0) {
2454 ERR("Cannot create command socket");
2455 goto end;
2456 }
2457
2458 ret = lttcomm_listen_unix_sock(client_socket);
2459 if (ret < 0) {
2460 goto end;
2461 }
2462
32258573 2463 DBG("Sending ready command to lttng-sessiond");
f73fabfd 2464 ret = lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY);
3bd1e081
MD
2465 /* return < 0 on error, but == 0 is not fatal */
2466 if (ret < 0) {
32258573 2467 ERR("Error sending ready command to lttng-sessiond");
3bd1e081
MD
2468 goto end;
2469 }
2470
2471 ret = fcntl(client_socket, F_SETFL, O_NONBLOCK);
2472 if (ret < 0) {
7a57cf92 2473 PERROR("fcntl O_NONBLOCK");
3bd1e081
MD
2474 goto end;
2475 }
2476
2477 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2478 consumer_sockpoll[0].fd = ctx->consumer_should_quit[0];
2479 consumer_sockpoll[0].events = POLLIN | POLLPRI;
2480 consumer_sockpoll[1].fd = client_socket;
2481 consumer_sockpoll[1].events = POLLIN | POLLPRI;
2482
2483 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
2484 goto end;
2485 }
2486 DBG("Connection on client_socket");
2487
2488 /* Blocking call, waiting for transmission */
2489 sock = lttcomm_accept_unix_sock(client_socket);
2490 if (sock <= 0) {
2491 WARN("On accept");
2492 goto end;
2493 }
2494 ret = fcntl(sock, F_SETFL, O_NONBLOCK);
2495 if (ret < 0) {
7a57cf92 2496 PERROR("fcntl O_NONBLOCK");
3bd1e081
MD
2497 goto end;
2498 }
2499
2500 /* update the polling structure to poll on the established socket */
2501 consumer_sockpoll[1].fd = sock;
2502 consumer_sockpoll[1].events = POLLIN | POLLPRI;
2503
2504 while (1) {
2505 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
2506 goto end;
2507 }
2508 DBG("Incoming command on sock");
2509 ret = lttng_consumer_recv_cmd(ctx, sock, consumer_sockpoll);
2510 if (ret == -ENOENT) {
2511 DBG("Received STOP command");
2512 goto end;
2513 }
4cbc1a04
DG
2514 if (ret <= 0) {
2515 /*
2516 * This could simply be a session daemon quitting. Don't output
2517 * ERR() here.
2518 */
2519 DBG("Communication interrupted on command socket");
3bd1e081
MD
2520 goto end;
2521 }
2522 if (consumer_quit) {
2523 DBG("consumer_thread_receive_fds received quit from signal");
2524 goto end;
2525 }
2526 DBG("received fds on sock");
2527 }
2528end:
2529 DBG("consumer_thread_receive_fds exiting");
2530
2531 /*
2532 * when all fds have hung up, the polling thread
2533 * can exit cleanly
2534 */
2535 consumer_quit = 1;
2536
04fdd819 2537 /*
c869f647 2538 * Notify the data poll thread to poll back again and test the
8994307f 2539 * consumer_quit state that we just set so to quit gracefully.
04fdd819 2540 */
8994307f 2541 notify_thread_pipe(ctx->consumer_data_pipe[1]);
c869f647 2542
e7b994a3 2543 rcu_unregister_thread();
3bd1e081
MD
2544 return NULL;
2545}
d41f73b7 2546
4078b776 2547ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream,
d41f73b7
MD
2548 struct lttng_consumer_local_data *ctx)
2549{
2550 switch (consumer_data.type) {
2551 case LTTNG_CONSUMER_KERNEL:
2552 return lttng_kconsumer_read_subbuffer(stream, ctx);
7753dea8
MD
2553 case LTTNG_CONSUMER32_UST:
2554 case LTTNG_CONSUMER64_UST:
d41f73b7
MD
2555 return lttng_ustconsumer_read_subbuffer(stream, ctx);
2556 default:
2557 ERR("Unknown consumer_data type");
2558 assert(0);
2559 return -ENOSYS;
2560 }
2561}
2562
2563int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream)
2564{
2565 switch (consumer_data.type) {
2566 case LTTNG_CONSUMER_KERNEL:
2567 return lttng_kconsumer_on_recv_stream(stream);
7753dea8
MD
2568 case LTTNG_CONSUMER32_UST:
2569 case LTTNG_CONSUMER64_UST:
d41f73b7
MD
2570 return lttng_ustconsumer_on_recv_stream(stream);
2571 default:
2572 ERR("Unknown consumer_data type");
2573 assert(0);
2574 return -ENOSYS;
2575 }
2576}
e4421fec
DG
2577
2578/*
2579 * Allocate and set consumer data hash tables.
2580 */
2581void lttng_consumer_init(void)
2582{
e4421fec 2583 consumer_data.channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
00e2e675 2584 consumer_data.relayd_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
53632229 2585 consumer_data.stream_list_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
43c34bc3
DG
2586
2587 metadata_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
2588 assert(metadata_ht);
2589 data_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
2590 assert(data_ht);
e4421fec 2591}
7735ef9e
DG
2592
2593/*
2594 * Process the ADD_RELAYD command receive by a consumer.
2595 *
2596 * This will create a relayd socket pair and add it to the relayd hash table.
2597 * The caller MUST acquire a RCU read side lock before calling it.
2598 */
2599int consumer_add_relayd_socket(int net_seq_idx, int sock_type,
2600 struct lttng_consumer_local_data *ctx, int sock,
2601 struct pollfd *consumer_sockpoll, struct lttcomm_sock *relayd_sock)
2602{
2603 int fd, ret = -1;
2604 struct consumer_relayd_sock_pair *relayd;
2605
2606 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx);
2607
2608 /* Get relayd reference if exists. */
2609 relayd = consumer_find_relayd(net_seq_idx);
2610 if (relayd == NULL) {
2611 /* Not found. Allocate one. */
2612 relayd = consumer_allocate_relayd_sock_pair(net_seq_idx);
2613 if (relayd == NULL) {
f73fabfd 2614 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
7735ef9e
DG
2615 goto error;
2616 }
2617 }
2618
2619 /* Poll on consumer socket. */
2620 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
2621 ret = -EINTR;
2622 goto error;
2623 }
2624
2625 /* Get relayd socket from session daemon */
2626 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
2627 if (ret != sizeof(fd)) {
f73fabfd 2628 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
7735ef9e
DG
2629 ret = -1;
2630 goto error;
2631 }
2632
2633 /* Copy socket information and received FD */
2634 switch (sock_type) {
2635 case LTTNG_STREAM_CONTROL:
2636 /* Copy received lttcomm socket */
2637 lttcomm_copy_sock(&relayd->control_sock, relayd_sock);
2638 ret = lttcomm_create_sock(&relayd->control_sock);
2639 if (ret < 0) {
2640 goto error;
2641 }
2642
2643 /* Close the created socket fd which is useless */
f66c074c
DG
2644 ret = close(relayd->control_sock.fd);
2645 if (ret < 0) {
2646 PERROR("close relayd control socket");
2647 }
7735ef9e
DG
2648
2649 /* Assign new file descriptor */
2650 relayd->control_sock.fd = fd;
2651 break;
2652 case LTTNG_STREAM_DATA:
2653 /* Copy received lttcomm socket */
2654 lttcomm_copy_sock(&relayd->data_sock, relayd_sock);
2655 ret = lttcomm_create_sock(&relayd->data_sock);
2656 if (ret < 0) {
2657 goto error;
2658 }
2659
2660 /* Close the created socket fd which is useless */
f66c074c
DG
2661 ret = close(relayd->data_sock.fd);
2662 if (ret < 0) {
2663 PERROR("close relayd control socket");
2664 }
7735ef9e
DG
2665
2666 /* Assign new file descriptor */
2667 relayd->data_sock.fd = fd;
2668 break;
2669 default:
2670 ERR("Unknown relayd socket type (%d)", sock_type);
2671 goto error;
2672 }
2673
2674 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
2675 sock_type == LTTNG_STREAM_CONTROL ? "control" : "data",
2676 relayd->net_seq_idx, fd);
2677
2678 /*
2679 * Add relayd socket pair to consumer data hashtable. If object already
2680 * exists or on error, the function gracefully returns.
2681 */
d09e1200 2682 add_relayd(relayd);
7735ef9e
DG
2683
2684 /* All good! */
2685 ret = 0;
2686
2687error:
2688 return ret;
2689}
ca22feea 2690
4e9a4686
DG
2691/*
2692 * Try to lock the stream mutex.
2693 *
2694 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
2695 */
2696static int stream_try_lock(struct lttng_consumer_stream *stream)
2697{
2698 int ret;
2699
2700 assert(stream);
2701
2702 /*
2703 * Try to lock the stream mutex. On failure, we know that the stream is
2704 * being used else where hence there is data still being extracted.
2705 */
2706 ret = pthread_mutex_trylock(&stream->lock);
2707 if (ret) {
2708 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
2709 ret = 0;
2710 goto end;
2711 }
2712
2713 ret = 1;
2714
2715end:
2716 return ret;
2717}
2718
ca22feea
DG
2719/*
2720 * Check if for a given session id there is still data needed to be extract
2721 * from the buffers.
2722 *
6d805429 2723 * Return 1 if data is pending or else 0 meaning ready to be read.
ca22feea 2724 */
6d805429 2725int consumer_data_pending(uint64_t id)
ca22feea
DG
2726{
2727 int ret;
2728 struct lttng_ht_iter iter;
2729 struct lttng_ht *ht;
2730 struct lttng_consumer_stream *stream;
c8f59ee5 2731 struct consumer_relayd_sock_pair *relayd;
6d805429 2732 int (*data_pending)(struct lttng_consumer_stream *);
ca22feea 2733
6d805429 2734 DBG("Consumer data pending command on session id %" PRIu64, id);
ca22feea 2735
6f6eda74 2736 rcu_read_lock();
ca22feea
DG
2737 pthread_mutex_lock(&consumer_data.lock);
2738
2739 switch (consumer_data.type) {
2740 case LTTNG_CONSUMER_KERNEL:
6d805429 2741 data_pending = lttng_kconsumer_data_pending;
ca22feea
DG
2742 break;
2743 case LTTNG_CONSUMER32_UST:
2744 case LTTNG_CONSUMER64_UST:
6d805429 2745 data_pending = lttng_ustconsumer_data_pending;
ca22feea
DG
2746 break;
2747 default:
2748 ERR("Unknown consumer data type");
2749 assert(0);
2750 }
2751
2752 /* Ease our life a bit */
2753 ht = consumer_data.stream_list_ht;
2754
c8f59ee5 2755 cds_lfht_for_each_entry_duplicate(ht->ht,
b6314938 2756 ht->hash_fct((void *)((unsigned long) id), lttng_ht_seed),
ca22feea
DG
2757 ht->match_fct, (void *)((unsigned long) id),
2758 &iter.iter, stream, node_session_id.node) {
4e9a4686
DG
2759 /* If this call fails, the stream is being used hence data pending. */
2760 ret = stream_try_lock(stream);
2761 if (!ret) {
6d805429 2762 goto data_not_pending;
ca22feea 2763 }
ca22feea 2764
4e9a4686
DG
2765 /*
2766 * A removed node from the hash table indicates that the stream has
2767 * been deleted thus having a guarantee that the buffers are closed
2768 * on the consumer side. However, data can still be transmitted
2769 * over the network so don't skip the relayd check.
2770 */
2771 ret = cds_lfht_is_node_deleted(&stream->node.node);
2772 if (!ret) {
2773 /* Check the stream if there is data in the buffers. */
6d805429
DG
2774 ret = data_pending(stream);
2775 if (ret == 1) {
4e9a4686 2776 pthread_mutex_unlock(&stream->lock);
6d805429 2777 goto data_not_pending;
4e9a4686
DG
2778 }
2779 }
2780
2781 /* Relayd check */
c8f59ee5
DG
2782 if (stream->net_seq_idx != -1) {
2783 relayd = consumer_find_relayd(stream->net_seq_idx);
4e9a4686
DG
2784 if (!relayd) {
2785 /*
2786 * At this point, if the relayd object is not available for the
2787 * given stream, it is because the relayd is being cleaned up
2788 * so every stream associated with it (for a session id value)
2789 * are or will be marked for deletion hence no data pending.
2790 */
2791 pthread_mutex_unlock(&stream->lock);
6d805429 2792 goto data_not_pending;
4e9a4686 2793 }
c8f59ee5 2794
c8f59ee5
DG
2795 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
2796 if (stream->metadata_flag) {
2797 ret = relayd_quiescent_control(&relayd->control_sock);
2798 } else {
6d805429 2799 ret = relayd_data_pending(&relayd->control_sock,
c8f59ee5
DG
2800 stream->relayd_stream_id, stream->next_net_seq_num);
2801 }
2802 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
6d805429 2803 if (ret == 1) {
4e9a4686 2804 pthread_mutex_unlock(&stream->lock);
6d805429 2805 goto data_not_pending;
c8f59ee5
DG
2806 }
2807 }
4e9a4686 2808 pthread_mutex_unlock(&stream->lock);
c8f59ee5 2809 }
ca22feea
DG
2810
2811 /*
2812 * Finding _no_ node in the hash table means that the stream(s) have been
2813 * removed thus data is guaranteed to be available for analysis from the
2814 * trace files. This is *only* true for local consumer and not network
2815 * streaming.
2816 */
2817
2818 /* Data is available to be read by a viewer. */
2819 pthread_mutex_unlock(&consumer_data.lock);
c8f59ee5 2820 rcu_read_unlock();
6d805429 2821 return 0;
ca22feea 2822
6d805429 2823data_not_pending:
ca22feea
DG
2824 /* Data is still being extracted from buffers. */
2825 pthread_mutex_unlock(&consumer_data.lock);
c8f59ee5 2826 rcu_read_unlock();
6d805429 2827 return 1;
ca22feea 2828}
This page took 0.172113 seconds and 5 git commands to generate.