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