Introduce the relayd socket object
[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
1624d5b7
JD
1290/*
1291 * Create the tracefile on disk.
1292 *
1293 * Return 0 on success or else a negative value.
1294 */
1295int lttng_create_output_file(struct lttng_consumer_stream *stream)
1296{
1297 int ret;
1298 char full_path[PATH_MAX];
1299 char *path_name_id = NULL;
1300 char *path;
1301
1302 assert(stream);
6151a90f
JD
1303
1304 /* Don't create anything if this is set for streaming. */
1305 if (stream->net_seq_idx != (uint64_t) -1ULL) {
1306 ret = 0;
1307 goto end;
1308 }
1624d5b7
JD
1309
1310 ret = snprintf(full_path, sizeof(full_path), "%s/%s",
1311 stream->chan->pathname, stream->name);
1312 if (ret < 0) {
1313 PERROR("snprintf create output file");
1314 goto error;
1315 }
1316
1317 /*
1318 * If we split the trace in multiple files, we have to add the tracefile
1319 * current count at the end of the tracefile name
1320 */
1321 if (stream->chan->tracefile_size > 0) {
1322 ret = asprintf(&path_name_id, "%s_%" PRIu64, full_path,
1323 stream->tracefile_count_current);
1324 if (ret < 0) {
1325 PERROR("Allocating path name ID");
1326 goto error;
1327 }
1328 path = path_name_id;
1329 } else {
1330 path = full_path;
1331 }
1332
1333 ret = run_as_open(path, O_WRONLY | O_CREAT | O_TRUNC,
1334 S_IRWXU | S_IRWXG | S_IRWXO, stream->uid, stream->gid);
1335 if (ret < 0) {
1336 PERROR("open stream path %s", path);
1337 goto error_open;
1338 }
1339 stream->out_fd = ret;
1340 stream->tracefile_size_current = 0;
1341
1342error_open:
1343 free(path_name_id);
1344error:
6151a90f 1345end:
1624d5b7
JD
1346 return ret;
1347}
1348
1349/*
1350 * Change the output tracefile according to the tracefile_size and
1351 * tracefile_count parameters. The stream lock MUST be held before calling this
1352 * function because we are modifying the stream status.
1353 *
1354 * Return 0 on success or else a negative value.
1355 */
1356static int rotate_output_file(struct lttng_consumer_stream *stream)
1357{
1358 int ret;
1359
1360 assert(stream);
1361 assert(stream->tracefile_size_current);
1362
1363 ret = close(stream->out_fd);
1364 if (ret < 0) {
1365 PERROR("Closing tracefile");
1366 goto end;
1367 }
1368
1369 if (stream->chan->tracefile_count > 0) {
1370 stream->tracefile_count_current =
1371 (stream->tracefile_count_current + 1) %
1372 stream->chan->tracefile_count;
1373 } else {
1374 stream->tracefile_count_current++;
1375 }
1376
1377 return lttng_create_output_file(stream);
1378
1379end:
1380 return ret;
1381}
1382
3bd1e081 1383/*
09e26845
DG
1384 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1385 * core function for writing trace buffers to either the local filesystem or
1386 * the network.
1387 *
79d4ffb7
DG
1388 * It must be called with the stream lock held.
1389 *
09e26845 1390 * Careful review MUST be put if any changes occur!
3bd1e081
MD
1391 *
1392 * Returns the number of bytes written
1393 */
4078b776 1394ssize_t lttng_consumer_on_read_subbuffer_mmap(
3bd1e081 1395 struct lttng_consumer_local_data *ctx,
1d4dfdef
DG
1396 struct lttng_consumer_stream *stream, unsigned long len,
1397 unsigned long padding)
3bd1e081 1398{
f02e1e8a 1399 unsigned long mmap_offset;
ffe60014 1400 void *mmap_base;
f02e1e8a
DG
1401 ssize_t ret = 0, written = 0;
1402 off_t orig_offset = stream->out_fd_offset;
1403 /* Default is on the disk */
1404 int outfd = stream->out_fd;
f02e1e8a 1405 struct consumer_relayd_sock_pair *relayd = NULL;
8994307f 1406 unsigned int relayd_hang_up = 0;
f02e1e8a
DG
1407
1408 /* RCU lock for the relayd pointer */
1409 rcu_read_lock();
1410
1411 /* Flag that the current stream if set for network streaming. */
1412 if (stream->net_seq_idx != -1) {
1413 relayd = consumer_find_relayd(stream->net_seq_idx);
1414 if (relayd == NULL) {
1415 goto end;
1416 }
1417 }
1418
1419 /* get the offset inside the fd to mmap */
3bd1e081
MD
1420 switch (consumer_data.type) {
1421 case LTTNG_CONSUMER_KERNEL:
ffe60014 1422 mmap_base = stream->mmap_base;
f02e1e8a
DG
1423 ret = kernctl_get_mmap_read_offset(stream->wait_fd, &mmap_offset);
1424 break;
7753dea8
MD
1425 case LTTNG_CONSUMER32_UST:
1426 case LTTNG_CONSUMER64_UST:
ffe60014
DG
1427 mmap_base = lttng_ustctl_get_mmap_base(stream);
1428 if (!mmap_base) {
1429 ERR("read mmap get mmap base for stream %s", stream->name);
1430 written = -1;
1431 goto end;
1432 }
1433 ret = lttng_ustctl_get_mmap_read_offset(stream, &mmap_offset);
331744e3 1434
f02e1e8a 1435 break;
3bd1e081
MD
1436 default:
1437 ERR("Unknown consumer_data type");
1438 assert(0);
1439 }
f02e1e8a
DG
1440 if (ret != 0) {
1441 errno = -ret;
1442 PERROR("tracer ctl get_mmap_read_offset");
1443 written = ret;
1444 goto end;
1445 }
b9182dd9 1446
f02e1e8a
DG
1447 /* Handle stream on the relayd if the output is on the network */
1448 if (relayd) {
1449 unsigned long netlen = len;
1450
1451 /*
1452 * Lock the control socket for the complete duration of the function
1453 * since from this point on we will use the socket.
1454 */
1455 if (stream->metadata_flag) {
1456 /* Metadata requires the control socket. */
1457 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1d4dfdef 1458 netlen += sizeof(struct lttcomm_relayd_metadata_payload);
f02e1e8a
DG
1459 }
1460
1d4dfdef 1461 ret = write_relayd_stream_header(stream, netlen, padding, relayd);
f02e1e8a
DG
1462 if (ret >= 0) {
1463 /* Use the returned socket. */
1464 outfd = ret;
1465
1466 /* Write metadata stream id before payload */
1467 if (stream->metadata_flag) {
1d4dfdef 1468 ret = write_relayd_metadata_id(outfd, stream, relayd, padding);
f02e1e8a 1469 if (ret < 0) {
f02e1e8a 1470 written = ret;
8994307f
DG
1471 /* Socket operation failed. We consider the relayd dead */
1472 if (ret == -EPIPE || ret == -EINVAL) {
1473 relayd_hang_up = 1;
1474 goto write_error;
1475 }
f02e1e8a
DG
1476 goto end;
1477 }
f02e1e8a 1478 }
8994307f
DG
1479 } else {
1480 /* Socket operation failed. We consider the relayd dead */
1481 if (ret == -EPIPE || ret == -EINVAL) {
1482 relayd_hang_up = 1;
1483 goto write_error;
1484 }
1485 /* Else, use the default set before which is the filesystem. */
f02e1e8a 1486 }
1d4dfdef
DG
1487 } else {
1488 /* No streaming, we have to set the len with the full padding */
1489 len += padding;
1624d5b7
JD
1490
1491 /*
1492 * Check if we need to change the tracefile before writing the packet.
1493 */
1494 if (stream->chan->tracefile_size > 0 &&
1495 (stream->tracefile_size_current + len) >
1496 stream->chan->tracefile_size) {
1497 ret = rotate_output_file(stream);
1498 if (ret < 0) {
1499 ERR("Rotating output file");
1500 goto end;
1501 }
1502 outfd = stream->out_fd;
1503 }
1504 stream->tracefile_size_current += len;
f02e1e8a
DG
1505 }
1506
1507 while (len > 0) {
1508 do {
ffe60014 1509 ret = write(outfd, mmap_base + mmap_offset, len);
f02e1e8a 1510 } while (ret < 0 && errno == EINTR);
1d4dfdef 1511 DBG("Consumer mmap write() ret %zd (len %lu)", ret, len);
f02e1e8a 1512 if (ret < 0) {
c5c45efa
DG
1513 /*
1514 * This is possible if the fd is closed on the other side (outfd)
1515 * or any write problem. It can be verbose a bit for a normal
1516 * execution if for instance the relayd is stopped abruptly. This
1517 * can happen so set this to a DBG statement.
1518 */
1519 DBG("Error in file write mmap");
f02e1e8a
DG
1520 if (written == 0) {
1521 written = ret;
1522 }
8994307f
DG
1523 /* Socket operation failed. We consider the relayd dead */
1524 if (errno == EPIPE || errno == EINVAL) {
1525 relayd_hang_up = 1;
1526 goto write_error;
1527 }
f02e1e8a
DG
1528 goto end;
1529 } else if (ret > len) {
77c7c900 1530 PERROR("Error in file write (ret %zd > len %lu)", ret, len);
f02e1e8a
DG
1531 written += ret;
1532 goto end;
1533 } else {
1534 len -= ret;
1535 mmap_offset += ret;
1536 }
f02e1e8a
DG
1537
1538 /* This call is useless on a socket so better save a syscall. */
1539 if (!relayd) {
1540 /* This won't block, but will start writeout asynchronously */
1541 lttng_sync_file_range(outfd, stream->out_fd_offset, ret,
1542 SYNC_FILE_RANGE_WRITE);
1543 stream->out_fd_offset += ret;
1544 }
1545 written += ret;
1546 }
1547 lttng_consumer_sync_trace_file(stream, orig_offset);
1548
8994307f
DG
1549write_error:
1550 /*
1551 * This is a special case that the relayd has closed its socket. Let's
1552 * cleanup the relayd object and all associated streams.
1553 */
1554 if (relayd && relayd_hang_up) {
1555 cleanup_relayd(relayd, ctx);
1556 }
1557
f02e1e8a
DG
1558end:
1559 /* Unlock only if ctrl socket used */
1560 if (relayd && stream->metadata_flag) {
1561 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1562 }
1563
1564 rcu_read_unlock();
1565 return written;
3bd1e081
MD
1566}
1567
1568/*
1569 * Splice the data from the ring buffer to the tracefile.
1570 *
79d4ffb7
DG
1571 * It must be called with the stream lock held.
1572 *
3bd1e081
MD
1573 * Returns the number of bytes spliced.
1574 */
4078b776 1575ssize_t lttng_consumer_on_read_subbuffer_splice(
3bd1e081 1576 struct lttng_consumer_local_data *ctx,
1d4dfdef
DG
1577 struct lttng_consumer_stream *stream, unsigned long len,
1578 unsigned long padding)
3bd1e081 1579{
f02e1e8a
DG
1580 ssize_t ret = 0, written = 0, ret_splice = 0;
1581 loff_t offset = 0;
1582 off_t orig_offset = stream->out_fd_offset;
1583 int fd = stream->wait_fd;
1584 /* Default is on the disk */
1585 int outfd = stream->out_fd;
f02e1e8a 1586 struct consumer_relayd_sock_pair *relayd = NULL;
fb3a43a9 1587 int *splice_pipe;
8994307f 1588 unsigned int relayd_hang_up = 0;
f02e1e8a 1589
3bd1e081
MD
1590 switch (consumer_data.type) {
1591 case LTTNG_CONSUMER_KERNEL:
f02e1e8a 1592 break;
7753dea8
MD
1593 case LTTNG_CONSUMER32_UST:
1594 case LTTNG_CONSUMER64_UST:
f02e1e8a 1595 /* Not supported for user space tracing */
3bd1e081
MD
1596 return -ENOSYS;
1597 default:
1598 ERR("Unknown consumer_data type");
1599 assert(0);
3bd1e081
MD
1600 }
1601
f02e1e8a
DG
1602 /* RCU lock for the relayd pointer */
1603 rcu_read_lock();
1604
1605 /* Flag that the current stream if set for network streaming. */
1606 if (stream->net_seq_idx != -1) {
1607 relayd = consumer_find_relayd(stream->net_seq_idx);
1608 if (relayd == NULL) {
1609 goto end;
1610 }
1611 }
1612
fb3a43a9
DG
1613 /*
1614 * Choose right pipe for splice. Metadata and trace data are handled by
1615 * different threads hence the use of two pipes in order not to race or
1616 * corrupt the written data.
1617 */
1618 if (stream->metadata_flag) {
1619 splice_pipe = ctx->consumer_splice_metadata_pipe;
1620 } else {
1621 splice_pipe = ctx->consumer_thread_pipe;
1622 }
1623
f02e1e8a 1624 /* Write metadata stream id before payload */
1d4dfdef
DG
1625 if (relayd) {
1626 int total_len = len;
f02e1e8a 1627
1d4dfdef
DG
1628 if (stream->metadata_flag) {
1629 /*
1630 * Lock the control socket for the complete duration of the function
1631 * since from this point on we will use the socket.
1632 */
1633 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1634
1635 ret = write_relayd_metadata_id(splice_pipe[1], stream, relayd,
1636 padding);
1637 if (ret < 0) {
1638 written = ret;
8994307f
DG
1639 /* Socket operation failed. We consider the relayd dead */
1640 if (ret == -EBADF) {
1641 WARN("Remote relayd disconnected. Stopping");
1642 relayd_hang_up = 1;
1643 goto write_error;
1644 }
1d4dfdef
DG
1645 goto end;
1646 }
1647
1648 total_len += sizeof(struct lttcomm_relayd_metadata_payload);
1649 }
1650
1651 ret = write_relayd_stream_header(stream, total_len, padding, relayd);
1652 if (ret >= 0) {
1653 /* Use the returned socket. */
1654 outfd = ret;
1655 } else {
8994307f
DG
1656 /* Socket operation failed. We consider the relayd dead */
1657 if (ret == -EBADF) {
1658 WARN("Remote relayd disconnected. Stopping");
1659 relayd_hang_up = 1;
1660 goto write_error;
1661 }
f02e1e8a
DG
1662 goto end;
1663 }
1d4dfdef
DG
1664 } else {
1665 /* No streaming, we have to set the len with the full padding */
1666 len += padding;
1624d5b7
JD
1667
1668 /*
1669 * Check if we need to change the tracefile before writing the packet.
1670 */
1671 if (stream->chan->tracefile_size > 0 &&
1672 (stream->tracefile_size_current + len) >
1673 stream->chan->tracefile_size) {
1674 ret = rotate_output_file(stream);
1675 if (ret < 0) {
1676 ERR("Rotating output file");
1677 goto end;
1678 }
1679 outfd = stream->out_fd;
1680 }
1681 stream->tracefile_size_current += len;
f02e1e8a
DG
1682 }
1683
1684 while (len > 0) {
1d4dfdef
DG
1685 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1686 (unsigned long)offset, len, fd, splice_pipe[1]);
fb3a43a9 1687 ret_splice = splice(fd, &offset, splice_pipe[1], NULL, len,
f02e1e8a
DG
1688 SPLICE_F_MOVE | SPLICE_F_MORE);
1689 DBG("splice chan to pipe, ret %zd", ret_splice);
1690 if (ret_splice < 0) {
1691 PERROR("Error in relay splice");
1692 if (written == 0) {
1693 written = ret_splice;
1694 }
1695 ret = errno;
1696 goto splice_error;
1697 }
1698
1699 /* Handle stream on the relayd if the output is on the network */
1700 if (relayd) {
1701 if (stream->metadata_flag) {
1d4dfdef
DG
1702 size_t metadata_payload_size =
1703 sizeof(struct lttcomm_relayd_metadata_payload);
1704
f02e1e8a 1705 /* Update counter to fit the spliced data */
1d4dfdef
DG
1706 ret_splice += metadata_payload_size;
1707 len += metadata_payload_size;
f02e1e8a
DG
1708 /*
1709 * We do this so the return value can match the len passed as
1710 * argument to this function.
1711 */
1d4dfdef 1712 written -= metadata_payload_size;
f02e1e8a
DG
1713 }
1714 }
1715
1716 /* Splice data out */
fb3a43a9 1717 ret_splice = splice(splice_pipe[0], NULL, outfd, NULL,
f02e1e8a 1718 ret_splice, SPLICE_F_MOVE | SPLICE_F_MORE);
1d4dfdef 1719 DBG("Consumer splice pipe to file, ret %zd", ret_splice);
f02e1e8a
DG
1720 if (ret_splice < 0) {
1721 PERROR("Error in file splice");
1722 if (written == 0) {
1723 written = ret_splice;
1724 }
8994307f 1725 /* Socket operation failed. We consider the relayd dead */
00c8752b 1726 if (errno == EBADF || errno == EPIPE) {
8994307f
DG
1727 WARN("Remote relayd disconnected. Stopping");
1728 relayd_hang_up = 1;
1729 goto write_error;
1730 }
f02e1e8a
DG
1731 ret = errno;
1732 goto splice_error;
1733 } else if (ret_splice > len) {
1734 errno = EINVAL;
1735 PERROR("Wrote more data than requested %zd (len: %lu)",
1736 ret_splice, len);
1737 written += ret_splice;
1738 ret = errno;
1739 goto splice_error;
1740 }
1741 len -= ret_splice;
1742
1743 /* This call is useless on a socket so better save a syscall. */
1744 if (!relayd) {
1745 /* This won't block, but will start writeout asynchronously */
1746 lttng_sync_file_range(outfd, stream->out_fd_offset, ret_splice,
1747 SYNC_FILE_RANGE_WRITE);
1748 stream->out_fd_offset += ret_splice;
1749 }
1750 written += ret_splice;
1751 }
1752 lttng_consumer_sync_trace_file(stream, orig_offset);
1753
1754 ret = ret_splice;
1755
1756 goto end;
1757
8994307f
DG
1758write_error:
1759 /*
1760 * This is a special case that the relayd has closed its socket. Let's
1761 * cleanup the relayd object and all associated streams.
1762 */
1763 if (relayd && relayd_hang_up) {
1764 cleanup_relayd(relayd, ctx);
1765 /* Skip splice error so the consumer does not fail */
1766 goto end;
1767 }
1768
f02e1e8a
DG
1769splice_error:
1770 /* send the appropriate error description to sessiond */
1771 switch (ret) {
f02e1e8a 1772 case EINVAL:
f73fabfd 1773 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_EINVAL);
f02e1e8a
DG
1774 break;
1775 case ENOMEM:
f73fabfd 1776 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ENOMEM);
f02e1e8a
DG
1777 break;
1778 case ESPIPE:
f73fabfd 1779 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ESPIPE);
f02e1e8a
DG
1780 break;
1781 }
1782
1783end:
1784 if (relayd && stream->metadata_flag) {
1785 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1786 }
1787
1788 rcu_read_unlock();
1789 return written;
3bd1e081
MD
1790}
1791
1792/*
1793 * Take a snapshot for a specific fd
1794 *
1795 * Returns 0 on success, < 0 on error
1796 */
ffe60014 1797int lttng_consumer_take_snapshot(struct lttng_consumer_stream *stream)
3bd1e081
MD
1798{
1799 switch (consumer_data.type) {
1800 case LTTNG_CONSUMER_KERNEL:
ffe60014 1801 return lttng_kconsumer_take_snapshot(stream);
7753dea8
MD
1802 case LTTNG_CONSUMER32_UST:
1803 case LTTNG_CONSUMER64_UST:
ffe60014 1804 return lttng_ustconsumer_take_snapshot(stream);
3bd1e081
MD
1805 default:
1806 ERR("Unknown consumer_data type");
1807 assert(0);
1808 return -ENOSYS;
1809 }
3bd1e081
MD
1810}
1811
1812/*
1813 * Get the produced position
1814 *
1815 * Returns 0 on success, < 0 on error
1816 */
ffe60014 1817int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream *stream,
3bd1e081
MD
1818 unsigned long *pos)
1819{
1820 switch (consumer_data.type) {
1821 case LTTNG_CONSUMER_KERNEL:
ffe60014 1822 return lttng_kconsumer_get_produced_snapshot(stream, pos);
7753dea8
MD
1823 case LTTNG_CONSUMER32_UST:
1824 case LTTNG_CONSUMER64_UST:
ffe60014 1825 return lttng_ustconsumer_get_produced_snapshot(stream, pos);
3bd1e081
MD
1826 default:
1827 ERR("Unknown consumer_data type");
1828 assert(0);
1829 return -ENOSYS;
1830 }
1831}
1832
1833int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx,
1834 int sock, struct pollfd *consumer_sockpoll)
1835{
1836 switch (consumer_data.type) {
1837 case LTTNG_CONSUMER_KERNEL:
1838 return lttng_kconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
7753dea8
MD
1839 case LTTNG_CONSUMER32_UST:
1840 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
1841 return lttng_ustconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
1842 default:
1843 ERR("Unknown consumer_data type");
1844 assert(0);
1845 return -ENOSYS;
1846 }
1847}
1848
43c34bc3
DG
1849/*
1850 * Iterate over all streams of the hashtable and free them properly.
1851 *
1852 * WARNING: *MUST* be used with data stream only.
1853 */
1854static void destroy_data_stream_ht(struct lttng_ht *ht)
1855{
43c34bc3
DG
1856 struct lttng_ht_iter iter;
1857 struct lttng_consumer_stream *stream;
1858
1859 if (ht == NULL) {
1860 return;
1861 }
1862
1863 rcu_read_lock();
1864 cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) {
5c540210
DG
1865 /*
1866 * Ignore return value since we are currently cleaning up so any error
1867 * can't be handled.
1868 */
1869 (void) consumer_del_stream(stream, ht);
43c34bc3
DG
1870 }
1871 rcu_read_unlock();
1872
1873 lttng_ht_destroy(ht);
1874}
1875
fb3a43a9 1876/*
f724d81e 1877 * Iterate over all streams of the hashtable and free them properly.
e316aad5
DG
1878 *
1879 * XXX: Should not be only for metadata stream or else use an other name.
fb3a43a9
DG
1880 */
1881static void destroy_stream_ht(struct lttng_ht *ht)
1882{
fb3a43a9
DG
1883 struct lttng_ht_iter iter;
1884 struct lttng_consumer_stream *stream;
1885
1886 if (ht == NULL) {
1887 return;
1888 }
1889
d09e1200 1890 rcu_read_lock();
58b1f425 1891 cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) {
5c540210
DG
1892 /*
1893 * Ignore return value since we are currently cleaning up so any error
1894 * can't be handled.
1895 */
1896 (void) consumer_del_metadata_stream(stream, ht);
fb3a43a9 1897 }
d09e1200 1898 rcu_read_unlock();
fb3a43a9
DG
1899
1900 lttng_ht_destroy(ht);
1901}
1902
d88aee68
DG
1903void lttng_consumer_close_metadata(void)
1904{
1905 switch (consumer_data.type) {
1906 case LTTNG_CONSUMER_KERNEL:
1907 /*
1908 * The Kernel consumer has a different metadata scheme so we don't
1909 * close anything because the stream will be closed by the session
1910 * daemon.
1911 */
1912 break;
1913 case LTTNG_CONSUMER32_UST:
1914 case LTTNG_CONSUMER64_UST:
1915 /*
1916 * Close all metadata streams. The metadata hash table is passed and
1917 * this call iterates over it by closing all wakeup fd. This is safe
1918 * because at this point we are sure that the metadata producer is
1919 * either dead or blocked.
1920 */
1921 lttng_ustconsumer_close_metadata(metadata_ht);
1922 break;
1923 default:
1924 ERR("Unknown consumer_data type");
1925 assert(0);
1926 }
1927}
1928
fb3a43a9
DG
1929/*
1930 * Clean up a metadata stream and free its memory.
1931 */
e316aad5
DG
1932void consumer_del_metadata_stream(struct lttng_consumer_stream *stream,
1933 struct lttng_ht *ht)
fb3a43a9
DG
1934{
1935 int ret;
e316aad5
DG
1936 struct lttng_ht_iter iter;
1937 struct lttng_consumer_channel *free_chan = NULL;
fb3a43a9
DG
1938 struct consumer_relayd_sock_pair *relayd;
1939
1940 assert(stream);
1941 /*
1942 * This call should NEVER receive regular stream. It must always be
1943 * metadata stream and this is crucial for data structure synchronization.
1944 */
1945 assert(stream->metadata_flag);
1946
e316aad5
DG
1947 DBG3("Consumer delete metadata stream %d", stream->wait_fd);
1948
1949 if (ht == NULL) {
1950 /* Means the stream was allocated but not successfully added */
ffe60014 1951 goto free_stream_rcu;
e316aad5
DG
1952 }
1953
74251bb8 1954 pthread_mutex_lock(&consumer_data.lock);
8994307f
DG
1955 pthread_mutex_lock(&stream->lock);
1956
fb3a43a9
DG
1957 switch (consumer_data.type) {
1958 case LTTNG_CONSUMER_KERNEL:
1959 if (stream->mmap_base != NULL) {
1960 ret = munmap(stream->mmap_base, stream->mmap_len);
1961 if (ret != 0) {
1962 PERROR("munmap metadata stream");
1963 }
1964 }
1965 break;
1966 case LTTNG_CONSUMER32_UST:
1967 case LTTNG_CONSUMER64_UST:
1968 lttng_ustconsumer_del_stream(stream);
1969 break;
1970 default:
1971 ERR("Unknown consumer_data type");
1972 assert(0);
e316aad5 1973 goto end;
fb3a43a9 1974 }
fb3a43a9 1975
c869f647 1976 rcu_read_lock();
58b1f425 1977 iter.iter.node = &stream->node.node;
c869f647
DG
1978 ret = lttng_ht_del(ht, &iter);
1979 assert(!ret);
ca22feea 1980
d8ef542d
MD
1981 iter.iter.node = &stream->node_channel_id.node;
1982 ret = lttng_ht_del(consumer_data.stream_per_chan_id_ht, &iter);
1983 assert(!ret);
1984
ca22feea
DG
1985 iter.iter.node = &stream->node_session_id.node;
1986 ret = lttng_ht_del(consumer_data.stream_list_ht, &iter);
1987 assert(!ret);
c869f647
DG
1988 rcu_read_unlock();
1989
fb3a43a9
DG
1990 if (stream->out_fd >= 0) {
1991 ret = close(stream->out_fd);
1992 if (ret) {
1993 PERROR("close");
1994 }
1995 }
1996
fb3a43a9
DG
1997 /* Check and cleanup relayd */
1998 rcu_read_lock();
1999 relayd = consumer_find_relayd(stream->net_seq_idx);
2000 if (relayd != NULL) {
2001 uatomic_dec(&relayd->refcount);
2002 assert(uatomic_read(&relayd->refcount) >= 0);
2003
2004 /* Closing streams requires to lock the control socket. */
2005 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
2006 ret = relayd_send_close_stream(&relayd->control_sock,
2007 stream->relayd_stream_id, stream->next_net_seq_num - 1);
2008 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
2009 if (ret < 0) {
2010 DBG("Unable to close stream on the relayd. Continuing");
2011 /*
2012 * Continue here. There is nothing we can do for the relayd.
2013 * Chances are that the relayd has closed the socket so we just
2014 * continue cleaning up.
2015 */
2016 }
2017
2018 /* Both conditions are met, we destroy the relayd. */
2019 if (uatomic_read(&relayd->refcount) == 0 &&
2020 uatomic_read(&relayd->destroy_flag)) {
d09e1200 2021 destroy_relayd(relayd);
fb3a43a9
DG
2022 }
2023 }
2024 rcu_read_unlock();
2025
2026 /* Atomically decrement channel refcount since other threads can use it. */
2027 uatomic_dec(&stream->chan->refcount);
c30aaa51 2028 if (!uatomic_read(&stream->chan->refcount)
ffe60014 2029 && !uatomic_read(&stream->chan->nb_init_stream_left)) {
c30aaa51 2030 /* Go for channel deletion! */
e316aad5 2031 free_chan = stream->chan;
fb3a43a9
DG
2032 }
2033
e316aad5 2034end:
8994307f 2035 pthread_mutex_unlock(&stream->lock);
74251bb8 2036 pthread_mutex_unlock(&consumer_data.lock);
e316aad5
DG
2037
2038 if (free_chan) {
2039 consumer_del_channel(free_chan);
2040 }
2041
ffe60014
DG
2042free_stream_rcu:
2043 call_rcu(&stream->node.head, free_stream_rcu);
fb3a43a9
DG
2044}
2045
2046/*
2047 * Action done with the metadata stream when adding it to the consumer internal
2048 * data structures to handle it.
2049 */
ffe60014 2050static int add_metadata_stream(struct lttng_consumer_stream *stream,
e316aad5 2051 struct lttng_ht *ht)
fb3a43a9 2052{
e316aad5 2053 int ret = 0;
fb3a43a9 2054 struct consumer_relayd_sock_pair *relayd;
76082088 2055 struct lttng_ht_iter iter;
d88aee68 2056 struct lttng_ht_node_u64 *node;
fb3a43a9 2057
e316aad5
DG
2058 assert(stream);
2059 assert(ht);
2060
d88aee68 2061 DBG3("Adding metadata stream %" PRIu64 " to hash table", stream->key);
e316aad5
DG
2062
2063 pthread_mutex_lock(&consumer_data.lock);
2e818a6a 2064 pthread_mutex_lock(&stream->lock);
e316aad5 2065
e316aad5
DG
2066 /*
2067 * From here, refcounts are updated so be _careful_ when returning an error
2068 * after this point.
2069 */
2070
fb3a43a9 2071 rcu_read_lock();
76082088
DG
2072
2073 /*
2074 * Lookup the stream just to make sure it does not exist in our internal
2075 * state. This should NEVER happen.
2076 */
d88aee68
DG
2077 lttng_ht_lookup(ht, &stream->key, &iter);
2078 node = lttng_ht_iter_get_node_u64(&iter);
76082088
DG
2079 assert(!node);
2080
e316aad5 2081 /* Find relayd and, if one is found, increment refcount. */
fb3a43a9
DG
2082 relayd = consumer_find_relayd(stream->net_seq_idx);
2083 if (relayd != NULL) {
2084 uatomic_inc(&relayd->refcount);
2085 }
e316aad5
DG
2086
2087 /* Update channel refcount once added without error(s). */
2088 uatomic_inc(&stream->chan->refcount);
2089
2090 /*
ffe60014
DG
2091 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2092 * in terms of destroying the associated channel, because the action that
e316aad5
DG
2093 * causes the count to become 0 also causes a stream to be added. The
2094 * channel deletion will thus be triggered by the following removal of this
2095 * stream.
2096 */
ffe60014
DG
2097 if (uatomic_read(&stream->chan->nb_init_stream_left) > 0) {
2098 uatomic_dec(&stream->chan->nb_init_stream_left);
e316aad5
DG
2099 }
2100
d88aee68 2101 lttng_ht_add_unique_u64(ht, &stream->node);
ca22feea 2102
d8ef542d
MD
2103 lttng_ht_add_unique_u64(consumer_data.stream_per_chan_id_ht,
2104 &stream->node_channel_id);
2105
ca22feea
DG
2106 /*
2107 * Add stream to the stream_list_ht of the consumer data. No need to steal
2108 * the key since the HT does not use it and we allow to add redundant keys
2109 * into this table.
2110 */
d88aee68 2111 lttng_ht_add_u64(consumer_data.stream_list_ht, &stream->node_session_id);
ca22feea 2112
fb3a43a9 2113 rcu_read_unlock();
e316aad5 2114
2e818a6a 2115 pthread_mutex_unlock(&stream->lock);
e316aad5
DG
2116 pthread_mutex_unlock(&consumer_data.lock);
2117 return ret;
fb3a43a9
DG
2118}
2119
8994307f
DG
2120/*
2121 * Delete data stream that are flagged for deletion (endpoint_status).
2122 */
2123static void validate_endpoint_status_data_stream(void)
2124{
2125 struct lttng_ht_iter iter;
2126 struct lttng_consumer_stream *stream;
2127
2128 DBG("Consumer delete flagged data stream");
2129
2130 rcu_read_lock();
2131 cds_lfht_for_each_entry(data_ht->ht, &iter.iter, stream, node.node) {
2132 /* Validate delete flag of the stream */
79d4ffb7 2133 if (stream->endpoint_status == CONSUMER_ENDPOINT_ACTIVE) {
8994307f
DG
2134 continue;
2135 }
2136 /* Delete it right now */
2137 consumer_del_stream(stream, data_ht);
2138 }
2139 rcu_read_unlock();
2140}
2141
2142/*
2143 * Delete metadata stream that are flagged for deletion (endpoint_status).
2144 */
2145static void validate_endpoint_status_metadata_stream(
2146 struct lttng_poll_event *pollset)
2147{
2148 struct lttng_ht_iter iter;
2149 struct lttng_consumer_stream *stream;
2150
2151 DBG("Consumer delete flagged metadata stream");
2152
2153 assert(pollset);
2154
2155 rcu_read_lock();
2156 cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream, node.node) {
2157 /* Validate delete flag of the stream */
79d4ffb7 2158 if (stream->endpoint_status == CONSUMER_ENDPOINT_ACTIVE) {
8994307f
DG
2159 continue;
2160 }
2161 /*
2162 * Remove from pollset so the metadata thread can continue without
2163 * blocking on a deleted stream.
2164 */
2165 lttng_poll_del(pollset, stream->wait_fd);
2166
2167 /* Delete it right now */
2168 consumer_del_metadata_stream(stream, metadata_ht);
2169 }
2170 rcu_read_unlock();
2171}
2172
fb3a43a9
DG
2173/*
2174 * Thread polls on metadata file descriptor and write them on disk or on the
2175 * network.
2176 */
7d980def 2177void *consumer_thread_metadata_poll(void *data)
fb3a43a9
DG
2178{
2179 int ret, i, pollfd;
2180 uint32_t revents, nb_fd;
e316aad5 2181 struct lttng_consumer_stream *stream = NULL;
fb3a43a9 2182 struct lttng_ht_iter iter;
d88aee68 2183 struct lttng_ht_node_u64 *node;
fb3a43a9
DG
2184 struct lttng_poll_event events;
2185 struct lttng_consumer_local_data *ctx = data;
2186 ssize_t len;
2187
2188 rcu_register_thread();
2189
d88aee68 2190 metadata_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
04bb2b64
DG
2191 if (!metadata_ht) {
2192 /* ENOMEM at this point. Better to bail out. */
d8ef542d 2193 goto end_ht;
04bb2b64
DG
2194 }
2195
fb3a43a9
DG
2196 DBG("Thread metadata poll started");
2197
fb3a43a9
DG
2198 /* Size is set to 1 for the consumer_metadata pipe */
2199 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
2200 if (ret < 0) {
2201 ERR("Poll set creation failed");
d8ef542d 2202 goto end_poll;
fb3a43a9
DG
2203 }
2204
2205 ret = lttng_poll_add(&events, ctx->consumer_metadata_pipe[0], LPOLLIN);
2206 if (ret < 0) {
2207 goto end;
2208 }
2209
2210 /* Main loop */
2211 DBG("Metadata main loop started");
2212
2213 while (1) {
fb3a43a9 2214 /* Only the metadata pipe is set */
d21b0d71 2215 if (LTTNG_POLL_GETNB(&events) == 0 && consumer_quit == 1) {
fb3a43a9
DG
2216 goto end;
2217 }
2218
2219restart:
d21b0d71 2220 DBG("Metadata poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events));
fb3a43a9
DG
2221 ret = lttng_poll_wait(&events, -1);
2222 DBG("Metadata event catched in thread");
2223 if (ret < 0) {
2224 if (errno == EINTR) {
e316aad5 2225 ERR("Poll EINTR catched");
fb3a43a9
DG
2226 goto restart;
2227 }
2228 goto error;
2229 }
2230
0d9c5d77
DG
2231 nb_fd = ret;
2232
e316aad5 2233 /* From here, the event is a metadata wait fd */
fb3a43a9
DG
2234 for (i = 0; i < nb_fd; i++) {
2235 revents = LTTNG_POLL_GETEV(&events, i);
2236 pollfd = LTTNG_POLL_GETFD(&events, i);
2237
e316aad5
DG
2238 /* Just don't waste time if no returned events for the fd */
2239 if (!revents) {
2240 continue;
2241 }
2242
fb3a43a9 2243 if (pollfd == ctx->consumer_metadata_pipe[0]) {
4adabd61 2244 if (revents & (LPOLLERR | LPOLLHUP )) {
fb3a43a9
DG
2245 DBG("Metadata thread pipe hung up");
2246 /*
2247 * Remove the pipe from the poll set and continue the loop
2248 * since their might be data to consume.
2249 */
2250 lttng_poll_del(&events, ctx->consumer_metadata_pipe[0]);
f66c074c
DG
2251 ret = close(ctx->consumer_metadata_pipe[0]);
2252 if (ret < 0) {
2253 PERROR("close metadata pipe");
2254 }
fb3a43a9
DG
2255 continue;
2256 } else if (revents & LPOLLIN) {
fb3a43a9 2257 do {
633d0084
DG
2258 /* Get the stream pointer received */
2259 ret = read(pollfd, &stream, sizeof(stream));
fb3a43a9 2260 } while (ret < 0 && errno == EINTR);
633d0084
DG
2261 if (ret < 0 ||
2262 ret < sizeof(struct lttng_consumer_stream *)) {
fb3a43a9 2263 PERROR("read metadata stream");
fb3a43a9
DG
2264 /*
2265 * Let's continue here and hope we can still work
2266 * without stopping the consumer. XXX: Should we?
2267 */
2268 continue;
2269 }
2270
8994307f
DG
2271 /* A NULL stream means that the state has changed. */
2272 if (stream == NULL) {
2273 /* Check for deleted streams. */
2274 validate_endpoint_status_metadata_stream(&events);
3714380f 2275 goto restart;
8994307f
DG
2276 }
2277
fb3a43a9
DG
2278 DBG("Adding metadata stream %d to poll set",
2279 stream->wait_fd);
2280
ffe60014 2281 ret = add_metadata_stream(stream, metadata_ht);
e316aad5
DG
2282 if (ret) {
2283 ERR("Unable to add metadata stream");
2284 /* Stream was not setup properly. Continuing. */
2285 consumer_del_metadata_stream(stream, NULL);
2286 continue;
2287 }
fb3a43a9
DG
2288
2289 /* Add metadata stream to the global poll events list */
2290 lttng_poll_add(&events, stream->wait_fd,
2291 LPOLLIN | LPOLLPRI);
fb3a43a9
DG
2292 }
2293
e316aad5 2294 /* Handle other stream */
fb3a43a9
DG
2295 continue;
2296 }
2297
d09e1200 2298 rcu_read_lock();
d88aee68
DG
2299 {
2300 uint64_t tmp_id = (uint64_t) pollfd;
2301
2302 lttng_ht_lookup(metadata_ht, &tmp_id, &iter);
2303 }
2304 node = lttng_ht_iter_get_node_u64(&iter);
e316aad5 2305 assert(node);
fb3a43a9
DG
2306
2307 stream = caa_container_of(node, struct lttng_consumer_stream,
58b1f425 2308 node);
fb3a43a9 2309
e316aad5 2310 /* Check for error event */
4adabd61 2311 if (revents & (LPOLLERR | LPOLLHUP)) {
e316aad5 2312 DBG("Metadata fd %d is hup|err.", pollfd);
fb3a43a9
DG
2313 if (!stream->hangup_flush_done
2314 && (consumer_data.type == LTTNG_CONSUMER32_UST
2315 || consumer_data.type == LTTNG_CONSUMER64_UST)) {
2316 DBG("Attempting to flush and consume the UST buffers");
2317 lttng_ustconsumer_on_stream_hangup(stream);
2318
2319 /* We just flushed the stream now read it. */
4bb94b75
DG
2320 do {
2321 len = ctx->on_buffer_ready(stream, ctx);
2322 /*
2323 * We don't check the return value here since if we get
2324 * a negative len, it means an error occured thus we
2325 * simply remove it from the poll set and free the
2326 * stream.
2327 */
2328 } while (len > 0);
fb3a43a9
DG
2329 }
2330
fb3a43a9 2331 lttng_poll_del(&events, stream->wait_fd);
e316aad5
DG
2332 /*
2333 * This call update the channel states, closes file descriptors
2334 * and securely free the stream.
2335 */
2336 consumer_del_metadata_stream(stream, metadata_ht);
2337 } else if (revents & (LPOLLIN | LPOLLPRI)) {
2338 /* Get the data out of the metadata file descriptor */
2339 DBG("Metadata available on fd %d", pollfd);
2340 assert(stream->wait_fd == pollfd);
2341
2342 len = ctx->on_buffer_ready(stream, ctx);
2343 /* It's ok to have an unavailable sub-buffer */
b64403e3 2344 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
ab1027f4
DG
2345 /* Clean up stream from consumer and free it. */
2346 lttng_poll_del(&events, stream->wait_fd);
2347 consumer_del_metadata_stream(stream, metadata_ht);
e316aad5
DG
2348 } else if (len > 0) {
2349 stream->data_read = 1;
2350 }
fb3a43a9 2351 }
e316aad5
DG
2352
2353 /* Release RCU lock for the stream looked up */
d09e1200 2354 rcu_read_unlock();
fb3a43a9
DG
2355 }
2356 }
2357
2358error:
2359end:
2360 DBG("Metadata poll thread exiting");
fb3a43a9 2361
d8ef542d
MD
2362 lttng_poll_clean(&events);
2363end_poll:
04bb2b64 2364 destroy_stream_ht(metadata_ht);
d8ef542d 2365end_ht:
fb3a43a9
DG
2366 rcu_unregister_thread();
2367 return NULL;
2368}
2369
3bd1e081 2370/*
e4421fec 2371 * This thread polls the fds in the set to consume the data and write
3bd1e081
MD
2372 * it to tracefile if necessary.
2373 */
7d980def 2374void *consumer_thread_data_poll(void *data)
3bd1e081
MD
2375{
2376 int num_rdy, num_hup, high_prio, ret, i;
2377 struct pollfd *pollfd = NULL;
2378 /* local view of the streams */
c869f647 2379 struct lttng_consumer_stream **local_stream = NULL, *new_stream = NULL;
3bd1e081
MD
2380 /* local view of consumer_data.fds_count */
2381 int nb_fd = 0;
3bd1e081 2382 struct lttng_consumer_local_data *ctx = data;
00e2e675 2383 ssize_t len;
3bd1e081 2384
e7b994a3
DG
2385 rcu_register_thread();
2386
d88aee68 2387 data_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
43c34bc3 2388 if (data_ht == NULL) {
04bb2b64 2389 /* ENOMEM at this point. Better to bail out. */
43c34bc3
DG
2390 goto end;
2391 }
2392
effcf122 2393 local_stream = zmalloc(sizeof(struct lttng_consumer_stream));
3bd1e081
MD
2394
2395 while (1) {
2396 high_prio = 0;
2397 num_hup = 0;
2398
2399 /*
e4421fec 2400 * the fds set has been updated, we need to update our
3bd1e081
MD
2401 * local array as well
2402 */
2403 pthread_mutex_lock(&consumer_data.lock);
2404 if (consumer_data.need_update) {
0e428499
DG
2405 free(pollfd);
2406 pollfd = NULL;
2407
2408 free(local_stream);
2409 local_stream = NULL;
3bd1e081 2410
50f8ae69 2411 /* allocate for all fds + 1 for the consumer_data_pipe */
effcf122 2412 pollfd = zmalloc((consumer_data.stream_count + 1) * sizeof(struct pollfd));
3bd1e081 2413 if (pollfd == NULL) {
7a57cf92 2414 PERROR("pollfd malloc");
3bd1e081
MD
2415 pthread_mutex_unlock(&consumer_data.lock);
2416 goto end;
2417 }
2418
50f8ae69 2419 /* allocate for all fds + 1 for the consumer_data_pipe */
effcf122 2420 local_stream = zmalloc((consumer_data.stream_count + 1) *
3bd1e081
MD
2421 sizeof(struct lttng_consumer_stream));
2422 if (local_stream == NULL) {
7a57cf92 2423 PERROR("local_stream malloc");
3bd1e081
MD
2424 pthread_mutex_unlock(&consumer_data.lock);
2425 goto end;
2426 }
ffe60014 2427 ret = update_poll_array(ctx, &pollfd, local_stream,
43c34bc3 2428 data_ht);
3bd1e081
MD
2429 if (ret < 0) {
2430 ERR("Error in allocating pollfd or local_outfds");
f73fabfd 2431 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
3bd1e081
MD
2432 pthread_mutex_unlock(&consumer_data.lock);
2433 goto end;
2434 }
2435 nb_fd = ret;
2436 consumer_data.need_update = 0;
2437 }
2438 pthread_mutex_unlock(&consumer_data.lock);
2439
4078b776
MD
2440 /* No FDs and consumer_quit, consumer_cleanup the thread */
2441 if (nb_fd == 0 && consumer_quit == 1) {
2442 goto end;
2443 }
3bd1e081 2444 /* poll on the array of fds */
88f2b785 2445 restart:
3bd1e081 2446 DBG("polling on %d fd", nb_fd + 1);
cb365c03 2447 num_rdy = poll(pollfd, nb_fd + 1, -1);
3bd1e081
MD
2448 DBG("poll num_rdy : %d", num_rdy);
2449 if (num_rdy == -1) {
88f2b785
MD
2450 /*
2451 * Restart interrupted system call.
2452 */
2453 if (errno == EINTR) {
2454 goto restart;
2455 }
7a57cf92 2456 PERROR("Poll error");
f73fabfd 2457 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
3bd1e081
MD
2458 goto end;
2459 } else if (num_rdy == 0) {
2460 DBG("Polling thread timed out");
2461 goto end;
2462 }
2463
3bd1e081 2464 /*
50f8ae69 2465 * If the consumer_data_pipe triggered poll go directly to the
00e2e675
DG
2466 * beginning of the loop to update the array. We want to prioritize
2467 * array update over low-priority reads.
3bd1e081 2468 */
509bb1cf 2469 if (pollfd[nb_fd].revents & (POLLIN | POLLPRI)) {
ab30f567 2470 ssize_t pipe_readlen;
04fdd819 2471
50f8ae69 2472 DBG("consumer_data_pipe wake up");
04fdd819
MD
2473 /* Consume 1 byte of pipe data */
2474 do {
50f8ae69 2475 pipe_readlen = read(ctx->consumer_data_pipe[0], &new_stream,
c869f647 2476 sizeof(new_stream));
04fdd819 2477 } while (pipe_readlen == -1 && errno == EINTR);
23f5f35d
DG
2478 if (pipe_readlen < 0) {
2479 PERROR("read consumer data pipe");
2480 /* Continue so we can at least handle the current stream(s). */
2481 continue;
2482 }
c869f647
DG
2483
2484 /*
2485 * If the stream is NULL, just ignore it. It's also possible that
2486 * the sessiond poll thread changed the consumer_quit state and is
2487 * waking us up to test it.
2488 */
2489 if (new_stream == NULL) {
8994307f 2490 validate_endpoint_status_data_stream();
c869f647
DG
2491 continue;
2492 }
2493
ffe60014 2494 ret = add_stream(new_stream, data_ht);
c869f647 2495 if (ret) {
d88aee68 2496 ERR("Consumer add stream %" PRIu64 " failed. Continuing",
c869f647
DG
2497 new_stream->key);
2498 /*
2499 * At this point, if the add_stream fails, it is not in the
2500 * hash table thus passing the NULL value here.
2501 */
2502 consumer_del_stream(new_stream, NULL);
2503 }
2504
2505 /* Continue to update the local streams and handle prio ones */
3bd1e081
MD
2506 continue;
2507 }
2508
2509 /* Take care of high priority channels first. */
2510 for (i = 0; i < nb_fd; i++) {
9617607b
DG
2511 if (local_stream[i] == NULL) {
2512 continue;
2513 }
fb3a43a9 2514 if (pollfd[i].revents & POLLPRI) {
d41f73b7
MD
2515 DBG("Urgent read on fd %d", pollfd[i].fd);
2516 high_prio = 1;
4078b776 2517 len = ctx->on_buffer_ready(local_stream[i], ctx);
d41f73b7 2518 /* it's ok to have an unavailable sub-buffer */
b64403e3 2519 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
ab1027f4
DG
2520 /* Clean the stream and free it. */
2521 consumer_del_stream(local_stream[i], data_ht);
9617607b 2522 local_stream[i] = NULL;
4078b776
MD
2523 } else if (len > 0) {
2524 local_stream[i]->data_read = 1;
d41f73b7 2525 }
3bd1e081
MD
2526 }
2527 }
2528
4078b776
MD
2529 /*
2530 * If we read high prio channel in this loop, try again
2531 * for more high prio data.
2532 */
2533 if (high_prio) {
3bd1e081
MD
2534 continue;
2535 }
2536
2537 /* Take care of low priority channels. */
4078b776 2538 for (i = 0; i < nb_fd; i++) {
9617607b
DG
2539 if (local_stream[i] == NULL) {
2540 continue;
2541 }
4078b776
MD
2542 if ((pollfd[i].revents & POLLIN) ||
2543 local_stream[i]->hangup_flush_done) {
4078b776
MD
2544 DBG("Normal read on fd %d", pollfd[i].fd);
2545 len = ctx->on_buffer_ready(local_stream[i], ctx);
2546 /* it's ok to have an unavailable sub-buffer */
b64403e3 2547 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
ab1027f4
DG
2548 /* Clean the stream and free it. */
2549 consumer_del_stream(local_stream[i], data_ht);
9617607b 2550 local_stream[i] = NULL;
4078b776
MD
2551 } else if (len > 0) {
2552 local_stream[i]->data_read = 1;
2553 }
2554 }
2555 }
2556
2557 /* Handle hangup and errors */
2558 for (i = 0; i < nb_fd; i++) {
9617607b
DG
2559 if (local_stream[i] == NULL) {
2560 continue;
2561 }
4078b776
MD
2562 if (!local_stream[i]->hangup_flush_done
2563 && (pollfd[i].revents & (POLLHUP | POLLERR | POLLNVAL))
2564 && (consumer_data.type == LTTNG_CONSUMER32_UST
2565 || consumer_data.type == LTTNG_CONSUMER64_UST)) {
2566 DBG("fd %d is hup|err|nval. Attempting flush and read.",
9617607b 2567 pollfd[i].fd);
4078b776
MD
2568 lttng_ustconsumer_on_stream_hangup(local_stream[i]);
2569 /* Attempt read again, for the data we just flushed. */
2570 local_stream[i]->data_read = 1;
2571 }
2572 /*
2573 * If the poll flag is HUP/ERR/NVAL and we have
2574 * read no data in this pass, we can remove the
2575 * stream from its hash table.
2576 */
2577 if ((pollfd[i].revents & POLLHUP)) {
2578 DBG("Polling fd %d tells it has hung up.", pollfd[i].fd);
2579 if (!local_stream[i]->data_read) {
43c34bc3 2580 consumer_del_stream(local_stream[i], data_ht);
9617607b 2581 local_stream[i] = NULL;
4078b776
MD
2582 num_hup++;
2583 }
2584 } else if (pollfd[i].revents & POLLERR) {
2585 ERR("Error returned in polling fd %d.", pollfd[i].fd);
2586 if (!local_stream[i]->data_read) {
43c34bc3 2587 consumer_del_stream(local_stream[i], data_ht);
9617607b 2588 local_stream[i] = NULL;
4078b776
MD
2589 num_hup++;
2590 }
2591 } else if (pollfd[i].revents & POLLNVAL) {
2592 ERR("Polling fd %d tells fd is not open.", pollfd[i].fd);
2593 if (!local_stream[i]->data_read) {
43c34bc3 2594 consumer_del_stream(local_stream[i], data_ht);
9617607b 2595 local_stream[i] = NULL;
4078b776 2596 num_hup++;
3bd1e081
MD
2597 }
2598 }
9617607b
DG
2599 if (local_stream[i] != NULL) {
2600 local_stream[i]->data_read = 0;
2601 }
3bd1e081
MD
2602 }
2603 }
2604end:
2605 DBG("polling thread exiting");
0e428499
DG
2606 free(pollfd);
2607 free(local_stream);
fb3a43a9
DG
2608
2609 /*
2610 * Close the write side of the pipe so epoll_wait() in
7d980def
DG
2611 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2612 * read side of the pipe. If we close them both, epoll_wait strangely does
2613 * not return and could create a endless wait period if the pipe is the
2614 * only tracked fd in the poll set. The thread will take care of closing
2615 * the read side.
fb3a43a9 2616 */
f66c074c
DG
2617 ret = close(ctx->consumer_metadata_pipe[1]);
2618 if (ret < 0) {
2619 PERROR("close data pipe");
2620 }
fb3a43a9 2621
04bb2b64 2622 destroy_data_stream_ht(data_ht);
43c34bc3 2623
e7b994a3 2624 rcu_unregister_thread();
3bd1e081
MD
2625 return NULL;
2626}
2627
d8ef542d
MD
2628/*
2629 * Close wake-up end of each stream belonging to the channel. This will
2630 * allow the poll() on the stream read-side to detect when the
2631 * write-side (application) finally closes them.
2632 */
2633static
2634void consumer_close_channel_streams(struct lttng_consumer_channel *channel)
2635{
2636 struct lttng_ht *ht;
2637 struct lttng_consumer_stream *stream;
2638 struct lttng_ht_iter iter;
2639
2640 ht = consumer_data.stream_per_chan_id_ht;
2641
2642 rcu_read_lock();
2643 cds_lfht_for_each_entry_duplicate(ht->ht,
2644 ht->hash_fct(&channel->key, lttng_ht_seed),
2645 ht->match_fct, &channel->key,
2646 &iter.iter, stream, node_channel_id.node) {
2647 switch (consumer_data.type) {
2648 case LTTNG_CONSUMER_KERNEL:
2649 break;
2650 case LTTNG_CONSUMER32_UST:
2651 case LTTNG_CONSUMER64_UST:
2652 /*
2653 * Note: a mutex is taken internally within
2654 * liblttng-ust-ctl to protect timer wakeup_fd
2655 * use from concurrent close.
2656 */
2657 lttng_ustconsumer_close_stream_wakeup(stream);
2658 break;
2659 default:
2660 ERR("Unknown consumer_data type");
2661 assert(0);
2662 }
2663 }
2664 rcu_read_unlock();
2665}
2666
2667static void destroy_channel_ht(struct lttng_ht *ht)
2668{
2669 struct lttng_ht_iter iter;
2670 struct lttng_consumer_channel *channel;
2671 int ret;
2672
2673 if (ht == NULL) {
2674 return;
2675 }
2676
2677 rcu_read_lock();
2678 cds_lfht_for_each_entry(ht->ht, &iter.iter, channel, wait_fd_node.node) {
2679 ret = lttng_ht_del(ht, &iter);
2680 assert(ret != 0);
2681 }
2682 rcu_read_unlock();
2683
2684 lttng_ht_destroy(ht);
2685}
2686
2687/*
2688 * This thread polls the channel fds to detect when they are being
2689 * closed. It closes all related streams if the channel is detected as
2690 * closed. It is currently only used as a shim layer for UST because the
2691 * consumerd needs to keep the per-stream wakeup end of pipes open for
2692 * periodical flush.
2693 */
2694void *consumer_thread_channel_poll(void *data)
2695{
2696 int ret, i, pollfd;
2697 uint32_t revents, nb_fd;
2698 struct lttng_consumer_channel *chan = NULL;
2699 struct lttng_ht_iter iter;
2700 struct lttng_ht_node_u64 *node;
2701 struct lttng_poll_event events;
2702 struct lttng_consumer_local_data *ctx = data;
2703 struct lttng_ht *channel_ht;
2704
2705 rcu_register_thread();
2706
2707 channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
2708 if (!channel_ht) {
2709 /* ENOMEM at this point. Better to bail out. */
2710 goto end_ht;
2711 }
2712
2713 DBG("Thread channel poll started");
2714
2715 /* Size is set to 1 for the consumer_channel pipe */
2716 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
2717 if (ret < 0) {
2718 ERR("Poll set creation failed");
2719 goto end_poll;
2720 }
2721
2722 ret = lttng_poll_add(&events, ctx->consumer_channel_pipe[0], LPOLLIN);
2723 if (ret < 0) {
2724 goto end;
2725 }
2726
2727 /* Main loop */
2728 DBG("Channel main loop started");
2729
2730 while (1) {
2731 /* Only the channel pipe is set */
2732 if (LTTNG_POLL_GETNB(&events) == 0 && consumer_quit == 1) {
2733 goto end;
2734 }
2735
2736restart:
2737 DBG("Channel poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events));
2738 ret = lttng_poll_wait(&events, -1);
2739 DBG("Channel event catched in thread");
2740 if (ret < 0) {
2741 if (errno == EINTR) {
2742 ERR("Poll EINTR catched");
2743 goto restart;
2744 }
2745 goto end;
2746 }
2747
2748 nb_fd = ret;
2749
2750 /* From here, the event is a channel wait fd */
2751 for (i = 0; i < nb_fd; i++) {
2752 revents = LTTNG_POLL_GETEV(&events, i);
2753 pollfd = LTTNG_POLL_GETFD(&events, i);
2754
2755 /* Just don't waste time if no returned events for the fd */
2756 if (!revents) {
2757 continue;
2758 }
2759 if (pollfd == ctx->consumer_channel_pipe[0]) {
2760 if (revents & (LPOLLERR | LPOLLHUP)) {
2761 DBG("Channel thread pipe hung up");
2762 /*
2763 * Remove the pipe from the poll set and continue the loop
2764 * since their might be data to consume.
2765 */
2766 lttng_poll_del(&events, ctx->consumer_channel_pipe[0]);
2767 continue;
2768 } else if (revents & LPOLLIN) {
2769 enum consumer_channel_action action;
2770
2771 ret = read_channel_pipe(ctx, &chan, &action);
2772 if (ret <= 0) {
2773 ERR("Error reading channel pipe");
2774 continue;
2775 }
2776
2777 switch (action) {
2778 case CONSUMER_CHANNEL_ADD:
2779 DBG("Adding channel %d to poll set",
2780 chan->wait_fd);
2781
2782 lttng_ht_node_init_u64(&chan->wait_fd_node,
2783 chan->wait_fd);
2784 lttng_ht_add_unique_u64(channel_ht,
2785 &chan->wait_fd_node);
2786 /* Add channel to the global poll events list */
2787 lttng_poll_add(&events, chan->wait_fd,
2788 LPOLLIN | LPOLLPRI);
2789 break;
2790 case CONSUMER_CHANNEL_QUIT:
2791 /*
2792 * Remove the pipe from the poll set and continue the loop
2793 * since their might be data to consume.
2794 */
2795 lttng_poll_del(&events, ctx->consumer_channel_pipe[0]);
2796 continue;
2797 default:
2798 ERR("Unknown action");
2799 break;
2800 }
2801 }
2802
2803 /* Handle other stream */
2804 continue;
2805 }
2806
2807 rcu_read_lock();
2808 {
2809 uint64_t tmp_id = (uint64_t) pollfd;
2810
2811 lttng_ht_lookup(channel_ht, &tmp_id, &iter);
2812 }
2813 node = lttng_ht_iter_get_node_u64(&iter);
2814 assert(node);
2815
2816 chan = caa_container_of(node, struct lttng_consumer_channel,
2817 wait_fd_node);
2818
2819 /* Check for error event */
2820 if (revents & (LPOLLERR | LPOLLHUP)) {
2821 DBG("Channel fd %d is hup|err.", pollfd);
2822
2823 lttng_poll_del(&events, chan->wait_fd);
2824 ret = lttng_ht_del(channel_ht, &iter);
2825 assert(ret == 0);
2826 consumer_close_channel_streams(chan);
2827 }
2828
2829 /* Release RCU lock for the channel looked up */
2830 rcu_read_unlock();
2831 }
2832 }
2833
2834end:
2835 lttng_poll_clean(&events);
2836end_poll:
2837 destroy_channel_ht(channel_ht);
2838end_ht:
2839 DBG("Channel poll thread exiting");
2840 rcu_unregister_thread();
2841 return NULL;
2842}
2843
331744e3
JD
2844static int set_metadata_socket(struct lttng_consumer_local_data *ctx,
2845 struct pollfd *sockpoll, int client_socket)
2846{
2847 int ret;
2848
2849 assert(ctx);
2850 assert(sockpoll);
2851
2852 if (lttng_consumer_poll_socket(sockpoll) < 0) {
2853 ret = -1;
2854 goto error;
2855 }
2856 DBG("Metadata connection on client_socket");
2857
2858 /* Blocking call, waiting for transmission */
2859 ctx->consumer_metadata_socket = lttcomm_accept_unix_sock(client_socket);
2860 if (ctx->consumer_metadata_socket < 0) {
2861 WARN("On accept metadata");
2862 ret = -1;
2863 goto error;
2864 }
2865 ret = 0;
2866
2867error:
2868 return ret;
2869}
2870
3bd1e081
MD
2871/*
2872 * This thread listens on the consumerd socket and receives the file
2873 * descriptors from the session daemon.
2874 */
7d980def 2875void *consumer_thread_sessiond_poll(void *data)
3bd1e081 2876{
d96f09c6 2877 int sock = -1, client_socket, ret;
3bd1e081
MD
2878 /*
2879 * structure to poll for incoming data on communication socket avoids
2880 * making blocking sockets.
2881 */
2882 struct pollfd consumer_sockpoll[2];
2883 struct lttng_consumer_local_data *ctx = data;
2884
e7b994a3
DG
2885 rcu_register_thread();
2886
3bd1e081
MD
2887 DBG("Creating command socket %s", ctx->consumer_command_sock_path);
2888 unlink(ctx->consumer_command_sock_path);
2889 client_socket = lttcomm_create_unix_sock(ctx->consumer_command_sock_path);
2890 if (client_socket < 0) {
2891 ERR("Cannot create command socket");
2892 goto end;
2893 }
2894
2895 ret = lttcomm_listen_unix_sock(client_socket);
2896 if (ret < 0) {
2897 goto end;
2898 }
2899
32258573 2900 DBG("Sending ready command to lttng-sessiond");
f73fabfd 2901 ret = lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY);
3bd1e081
MD
2902 /* return < 0 on error, but == 0 is not fatal */
2903 if (ret < 0) {
32258573 2904 ERR("Error sending ready command to lttng-sessiond");
3bd1e081
MD
2905 goto end;
2906 }
2907
2908 ret = fcntl(client_socket, F_SETFL, O_NONBLOCK);
2909 if (ret < 0) {
7a57cf92 2910 PERROR("fcntl O_NONBLOCK");
3bd1e081
MD
2911 goto end;
2912 }
2913
2914 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2915 consumer_sockpoll[0].fd = ctx->consumer_should_quit[0];
2916 consumer_sockpoll[0].events = POLLIN | POLLPRI;
2917 consumer_sockpoll[1].fd = client_socket;
2918 consumer_sockpoll[1].events = POLLIN | POLLPRI;
2919
2920 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
2921 goto end;
2922 }
2923 DBG("Connection on client_socket");
2924
2925 /* Blocking call, waiting for transmission */
2926 sock = lttcomm_accept_unix_sock(client_socket);
534d2592 2927 if (sock < 0) {
3bd1e081
MD
2928 WARN("On accept");
2929 goto end;
2930 }
2931 ret = fcntl(sock, F_SETFL, O_NONBLOCK);
2932 if (ret < 0) {
7a57cf92 2933 PERROR("fcntl O_NONBLOCK");
3bd1e081
MD
2934 goto end;
2935 }
2936
331744e3
JD
2937 /*
2938 * Setup metadata socket which is the second socket connection on the
2939 * command unix socket.
2940 */
2941 ret = set_metadata_socket(ctx, consumer_sockpoll, client_socket);
2942 if (ret < 0) {
2943 goto end;
2944 }
2945
d96f09c6
DG
2946 /* This socket is not useful anymore. */
2947 ret = close(client_socket);
2948 if (ret < 0) {
2949 PERROR("close client_socket");
2950 }
2951 client_socket = -1;
2952
3bd1e081
MD
2953 /* update the polling structure to poll on the established socket */
2954 consumer_sockpoll[1].fd = sock;
2955 consumer_sockpoll[1].events = POLLIN | POLLPRI;
2956
2957 while (1) {
2958 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
2959 goto end;
2960 }
2961 DBG("Incoming command on sock");
2962 ret = lttng_consumer_recv_cmd(ctx, sock, consumer_sockpoll);
2963 if (ret == -ENOENT) {
2964 DBG("Received STOP command");
2965 goto end;
2966 }
4cbc1a04
DG
2967 if (ret <= 0) {
2968 /*
2969 * This could simply be a session daemon quitting. Don't output
2970 * ERR() here.
2971 */
2972 DBG("Communication interrupted on command socket");
3bd1e081
MD
2973 goto end;
2974 }
2975 if (consumer_quit) {
2976 DBG("consumer_thread_receive_fds received quit from signal");
2977 goto end;
2978 }
ffe60014 2979 DBG("received command on sock");
3bd1e081
MD
2980 }
2981end:
ffe60014 2982 DBG("Consumer thread sessiond poll exiting");
3bd1e081 2983
d88aee68
DG
2984 /*
2985 * Close metadata streams since the producer is the session daemon which
2986 * just died.
2987 *
2988 * NOTE: for now, this only applies to the UST tracer.
2989 */
2990 lttng_consumer_close_metadata();
2991
3bd1e081
MD
2992 /*
2993 * when all fds have hung up, the polling thread
2994 * can exit cleanly
2995 */
2996 consumer_quit = 1;
2997
04fdd819 2998 /*
c869f647 2999 * Notify the data poll thread to poll back again and test the
8994307f 3000 * consumer_quit state that we just set so to quit gracefully.
04fdd819 3001 */
8994307f 3002 notify_thread_pipe(ctx->consumer_data_pipe[1]);
c869f647 3003
d8ef542d
MD
3004 notify_channel_pipe(ctx, NULL, CONSUMER_CHANNEL_QUIT);
3005
d96f09c6
DG
3006 /* Cleaning up possibly open sockets. */
3007 if (sock >= 0) {
3008 ret = close(sock);
3009 if (ret < 0) {
3010 PERROR("close sock sessiond poll");
3011 }
3012 }
3013 if (client_socket >= 0) {
3014 ret = close(sock);
3015 if (ret < 0) {
3016 PERROR("close client_socket sessiond poll");
3017 }
3018 }
3019
e7b994a3 3020 rcu_unregister_thread();
3bd1e081
MD
3021 return NULL;
3022}
d41f73b7 3023
4078b776 3024ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream,
d41f73b7
MD
3025 struct lttng_consumer_local_data *ctx)
3026{
74251bb8
DG
3027 ssize_t ret;
3028
3029 pthread_mutex_lock(&stream->lock);
3030
d41f73b7
MD
3031 switch (consumer_data.type) {
3032 case LTTNG_CONSUMER_KERNEL:
74251bb8
DG
3033 ret = lttng_kconsumer_read_subbuffer(stream, ctx);
3034 break;
7753dea8
MD
3035 case LTTNG_CONSUMER32_UST:
3036 case LTTNG_CONSUMER64_UST:
74251bb8
DG
3037 ret = lttng_ustconsumer_read_subbuffer(stream, ctx);
3038 break;
d41f73b7
MD
3039 default:
3040 ERR("Unknown consumer_data type");
3041 assert(0);
74251bb8
DG
3042 ret = -ENOSYS;
3043 break;
d41f73b7 3044 }
74251bb8
DG
3045
3046 pthread_mutex_unlock(&stream->lock);
3047 return ret;
d41f73b7
MD
3048}
3049
3050int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream)
3051{
3052 switch (consumer_data.type) {
3053 case LTTNG_CONSUMER_KERNEL:
3054 return lttng_kconsumer_on_recv_stream(stream);
7753dea8
MD
3055 case LTTNG_CONSUMER32_UST:
3056 case LTTNG_CONSUMER64_UST:
d41f73b7
MD
3057 return lttng_ustconsumer_on_recv_stream(stream);
3058 default:
3059 ERR("Unknown consumer_data type");
3060 assert(0);
3061 return -ENOSYS;
3062 }
3063}
e4421fec
DG
3064
3065/*
3066 * Allocate and set consumer data hash tables.
3067 */
3068void lttng_consumer_init(void)
3069{
d88aee68
DG
3070 consumer_data.channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3071 consumer_data.relayd_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3072 consumer_data.stream_list_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
d8ef542d 3073 consumer_data.stream_per_chan_id_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
e4421fec 3074}
7735ef9e
DG
3075
3076/*
3077 * Process the ADD_RELAYD command receive by a consumer.
3078 *
3079 * This will create a relayd socket pair and add it to the relayd hash table.
3080 * The caller MUST acquire a RCU read side lock before calling it.
3081 */
3082int consumer_add_relayd_socket(int net_seq_idx, int sock_type,
3083 struct lttng_consumer_local_data *ctx, int sock,
6151a90f
JD
3084 struct pollfd *consumer_sockpoll,
3085 struct lttcomm_relayd_sock *relayd_sock, unsigned int sessiond_id)
7735ef9e 3086{
cd2b09ed 3087 int fd = -1, ret = -1, relayd_created = 0;
f50f23d9 3088 enum lttng_error_code ret_code = LTTNG_OK;
7735ef9e
DG
3089 struct consumer_relayd_sock_pair *relayd;
3090
6151a90f
JD
3091 assert(ctx);
3092 assert(relayd_sock);
3093
7735ef9e
DG
3094 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx);
3095
f50f23d9
DG
3096 /* First send a status message before receiving the fds. */
3097 ret = consumer_send_status_msg(sock, ret_code);
3098 if (ret < 0) {
3099 /* Somehow, the session daemon is not responding anymore. */
3100 goto error;
3101 }
3102
7735ef9e
DG
3103 /* Get relayd reference if exists. */
3104 relayd = consumer_find_relayd(net_seq_idx);
3105 if (relayd == NULL) {
3106 /* Not found. Allocate one. */
3107 relayd = consumer_allocate_relayd_sock_pair(net_seq_idx);
3108 if (relayd == NULL) {
f73fabfd 3109 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
59e71485 3110 ret = -1;
7735ef9e
DG
3111 goto error;
3112 }
f7079f67 3113 relayd->sessiond_session_id = (uint64_t) sessiond_id;
cd2b09ed 3114 relayd_created = 1;
7735ef9e
DG
3115 }
3116
3117 /* Poll on consumer socket. */
3118 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
3119 ret = -EINTR;
3120 goto error;
3121 }
3122
3123 /* Get relayd socket from session daemon */
3124 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
3125 if (ret != sizeof(fd)) {
f73fabfd 3126 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
7735ef9e 3127 ret = -1;
4028eeb9 3128 fd = -1; /* Just in case it gets set with an invalid value. */
ffe60014 3129 goto error_close;
7735ef9e
DG
3130 }
3131
f50f23d9
DG
3132 /* We have the fds without error. Send status back. */
3133 ret = consumer_send_status_msg(sock, ret_code);
3134 if (ret < 0) {
3135 /* Somehow, the session daemon is not responding anymore. */
3136 goto error;
3137 }
3138
7735ef9e
DG
3139 /* Copy socket information and received FD */
3140 switch (sock_type) {
3141 case LTTNG_STREAM_CONTROL:
3142 /* Copy received lttcomm socket */
6151a90f
JD
3143 lttcomm_copy_sock(&relayd->control_sock.sock, &relayd_sock->sock);
3144 ret = lttcomm_create_sock(&relayd->control_sock.sock);
4028eeb9 3145 /* Immediately try to close the created socket if valid. */
6151a90f
JD
3146 if (relayd->control_sock.sock.fd >= 0) {
3147 if (close(relayd->control_sock.sock.fd)) {
4028eeb9
DG
3148 PERROR("close relayd control socket");
3149 }
7735ef9e 3150 }
4028eeb9 3151 /* Handle create_sock error. */
f66c074c 3152 if (ret < 0) {
4028eeb9 3153 goto error;
f66c074c 3154 }
7735ef9e
DG
3155
3156 /* Assign new file descriptor */
6151a90f
JD
3157 relayd->control_sock.sock.fd = fd;
3158 /* Assign version values. */
3159 relayd->control_sock.major = relayd_sock->major;
3160 relayd->control_sock.minor = relayd_sock->minor;
c5b6f4f0
DG
3161
3162 /*
59e71485
DG
3163 * Create a session on the relayd and store the returned id. Lock the
3164 * control socket mutex if the relayd was NOT created before.
c5b6f4f0 3165 */
59e71485
DG
3166 if (!relayd_created) {
3167 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
3168 }
c5b6f4f0 3169 ret = relayd_create_session(&relayd->control_sock,
f7079f67 3170 &relayd->relayd_session_id);
59e71485
DG
3171 if (!relayd_created) {
3172 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
3173 }
c5b6f4f0 3174 if (ret < 0) {
ffe60014
DG
3175 /*
3176 * Close all sockets of a relayd object. It will be freed if it was
3177 * created at the error code path or else it will be garbage
3178 * collect.
3179 */
3180 (void) relayd_close(&relayd->control_sock);
3181 (void) relayd_close(&relayd->data_sock);
c5b6f4f0
DG
3182 goto error;
3183 }
3184
7735ef9e
DG
3185 break;
3186 case LTTNG_STREAM_DATA:
3187 /* Copy received lttcomm socket */
6151a90f
JD
3188 lttcomm_copy_sock(&relayd->data_sock.sock, &relayd_sock->sock);
3189 ret = lttcomm_create_sock(&relayd->data_sock.sock);
4028eeb9 3190 /* Immediately try to close the created socket if valid. */
6151a90f
JD
3191 if (relayd->data_sock.sock.fd >= 0) {
3192 if (close(relayd->data_sock.sock.fd)) {
4028eeb9
DG
3193 PERROR("close relayd data socket");
3194 }
7735ef9e 3195 }
4028eeb9 3196 /* Handle create_sock error. */
f66c074c 3197 if (ret < 0) {
4028eeb9 3198 goto error;
f66c074c 3199 }
7735ef9e
DG
3200
3201 /* Assign new file descriptor */
6151a90f
JD
3202 relayd->data_sock.sock.fd = fd;
3203 /* Assign version values. */
3204 relayd->data_sock.major = relayd_sock->major;
3205 relayd->data_sock.minor = relayd_sock->minor;
7735ef9e
DG
3206 break;
3207 default:
3208 ERR("Unknown relayd socket type (%d)", sock_type);
59e71485 3209 ret = -1;
7735ef9e
DG
3210 goto error;
3211 }
3212
d88aee68 3213 DBG("Consumer %s socket created successfully with net idx %" PRIu64 " (fd: %d)",
7735ef9e
DG
3214 sock_type == LTTNG_STREAM_CONTROL ? "control" : "data",
3215 relayd->net_seq_idx, fd);
3216
3217 /*
3218 * Add relayd socket pair to consumer data hashtable. If object already
3219 * exists or on error, the function gracefully returns.
3220 */
d09e1200 3221 add_relayd(relayd);
7735ef9e
DG
3222
3223 /* All good! */
4028eeb9 3224 return 0;
7735ef9e
DG
3225
3226error:
4028eeb9
DG
3227 /* Close received socket if valid. */
3228 if (fd >= 0) {
3229 if (close(fd)) {
3230 PERROR("close received socket");
3231 }
3232 }
cd2b09ed 3233
ffe60014 3234error_close:
cd2b09ed 3235 if (relayd_created) {
cd2b09ed
DG
3236 free(relayd);
3237 }
3238
7735ef9e
DG
3239 return ret;
3240}
ca22feea 3241
4e9a4686
DG
3242/*
3243 * Try to lock the stream mutex.
3244 *
3245 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
3246 */
3247static int stream_try_lock(struct lttng_consumer_stream *stream)
3248{
3249 int ret;
3250
3251 assert(stream);
3252
3253 /*
3254 * Try to lock the stream mutex. On failure, we know that the stream is
3255 * being used else where hence there is data still being extracted.
3256 */
3257 ret = pthread_mutex_trylock(&stream->lock);
3258 if (ret) {
3259 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
3260 ret = 0;
3261 goto end;
3262 }
3263
3264 ret = 1;
3265
3266end:
3267 return ret;
3268}
3269
f7079f67
DG
3270/*
3271 * Search for a relayd associated to the session id and return the reference.
3272 *
3273 * A rcu read side lock MUST be acquire before calling this function and locked
3274 * until the relayd object is no longer necessary.
3275 */
3276static struct consumer_relayd_sock_pair *find_relayd_by_session_id(uint64_t id)
3277{
3278 struct lttng_ht_iter iter;
f7079f67 3279 struct consumer_relayd_sock_pair *relayd = NULL;
f7079f67
DG
3280
3281 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3282 cds_lfht_for_each_entry(consumer_data.relayd_ht->ht, &iter.iter, relayd,
3283 node.node) {
18261bd1
DG
3284 /*
3285 * Check by sessiond id which is unique here where the relayd session
3286 * id might not be when having multiple relayd.
3287 */
3288 if (relayd->sessiond_session_id == id) {
f7079f67 3289 /* Found the relayd. There can be only one per id. */
18261bd1 3290 goto found;
f7079f67
DG
3291 }
3292 }
3293
18261bd1
DG
3294 return NULL;
3295
3296found:
f7079f67
DG
3297 return relayd;
3298}
3299
ca22feea
DG
3300/*
3301 * Check if for a given session id there is still data needed to be extract
3302 * from the buffers.
3303 *
6d805429 3304 * Return 1 if data is pending or else 0 meaning ready to be read.
ca22feea 3305 */
6d805429 3306int consumer_data_pending(uint64_t id)
ca22feea
DG
3307{
3308 int ret;
3309 struct lttng_ht_iter iter;
3310 struct lttng_ht *ht;
3311 struct lttng_consumer_stream *stream;
f7079f67 3312 struct consumer_relayd_sock_pair *relayd = NULL;
6d805429 3313 int (*data_pending)(struct lttng_consumer_stream *);
ca22feea 3314
6d805429 3315 DBG("Consumer data pending command on session id %" PRIu64, id);
ca22feea 3316
6f6eda74 3317 rcu_read_lock();
ca22feea
DG
3318 pthread_mutex_lock(&consumer_data.lock);
3319
3320 switch (consumer_data.type) {
3321 case LTTNG_CONSUMER_KERNEL:
6d805429 3322 data_pending = lttng_kconsumer_data_pending;
ca22feea
DG
3323 break;
3324 case LTTNG_CONSUMER32_UST:
3325 case LTTNG_CONSUMER64_UST:
6d805429 3326 data_pending = lttng_ustconsumer_data_pending;
ca22feea
DG
3327 break;
3328 default:
3329 ERR("Unknown consumer data type");
3330 assert(0);
3331 }
3332
3333 /* Ease our life a bit */
3334 ht = consumer_data.stream_list_ht;
3335
f7079f67
DG
3336 relayd = find_relayd_by_session_id(id);
3337 if (relayd) {
3338 /* Send init command for data pending. */
3339 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
3340 ret = relayd_begin_data_pending(&relayd->control_sock,
3341 relayd->relayd_session_id);
3342 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
3343 if (ret < 0) {
3344 /* Communication error thus the relayd so no data pending. */
3345 goto data_not_pending;
3346 }
3347 }
3348
c8f59ee5 3349 cds_lfht_for_each_entry_duplicate(ht->ht,
d88aee68
DG
3350 ht->hash_fct(&id, lttng_ht_seed),
3351 ht->match_fct, &id,
ca22feea 3352 &iter.iter, stream, node_session_id.node) {
4e9a4686
DG
3353 /* If this call fails, the stream is being used hence data pending. */
3354 ret = stream_try_lock(stream);
3355 if (!ret) {
f7079f67 3356 goto data_pending;
ca22feea 3357 }
ca22feea 3358
4e9a4686
DG
3359 /*
3360 * A removed node from the hash table indicates that the stream has
3361 * been deleted thus having a guarantee that the buffers are closed
3362 * on the consumer side. However, data can still be transmitted
3363 * over the network so don't skip the relayd check.
3364 */
3365 ret = cds_lfht_is_node_deleted(&stream->node.node);
3366 if (!ret) {
3367 /* Check the stream if there is data in the buffers. */
6d805429
DG
3368 ret = data_pending(stream);
3369 if (ret == 1) {
4e9a4686 3370 pthread_mutex_unlock(&stream->lock);
f7079f67 3371 goto data_pending;
4e9a4686
DG
3372 }
3373 }
3374
3375 /* Relayd check */
f7079f67 3376 if (relayd) {
c8f59ee5
DG
3377 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
3378 if (stream->metadata_flag) {
ad7051c0
DG
3379 ret = relayd_quiescent_control(&relayd->control_sock,
3380 stream->relayd_stream_id);
c8f59ee5 3381 } else {
6d805429 3382 ret = relayd_data_pending(&relayd->control_sock,
39df6d9f
DG
3383 stream->relayd_stream_id,
3384 stream->next_net_seq_num - 1);
c8f59ee5
DG
3385 }
3386 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
6d805429 3387 if (ret == 1) {
4e9a4686 3388 pthread_mutex_unlock(&stream->lock);
f7079f67 3389 goto data_pending;
c8f59ee5
DG
3390 }
3391 }
4e9a4686 3392 pthread_mutex_unlock(&stream->lock);
c8f59ee5 3393 }
ca22feea 3394
f7079f67
DG
3395 if (relayd) {
3396 unsigned int is_data_inflight = 0;
3397
3398 /* Send init command for data pending. */
3399 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
3400 ret = relayd_end_data_pending(&relayd->control_sock,
3401 relayd->relayd_session_id, &is_data_inflight);
3402 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
bdd88757 3403 if (ret < 0) {
f7079f67
DG
3404 goto data_not_pending;
3405 }
bdd88757
DG
3406 if (is_data_inflight) {
3407 goto data_pending;
3408 }
f7079f67
DG
3409 }
3410
ca22feea 3411 /*
f7079f67
DG
3412 * Finding _no_ node in the hash table and no inflight data means that the
3413 * stream(s) have been removed thus data is guaranteed to be available for
3414 * analysis from the trace files.
ca22feea
DG
3415 */
3416
f7079f67 3417data_not_pending:
ca22feea
DG
3418 /* Data is available to be read by a viewer. */
3419 pthread_mutex_unlock(&consumer_data.lock);
c8f59ee5 3420 rcu_read_unlock();
6d805429 3421 return 0;
ca22feea 3422
f7079f67 3423data_pending:
ca22feea
DG
3424 /* Data is still being extracted from buffers. */
3425 pthread_mutex_unlock(&consumer_data.lock);
c8f59ee5 3426 rcu_read_unlock();
6d805429 3427 return 1;
ca22feea 3428}
f50f23d9
DG
3429
3430/*
3431 * Send a ret code status message to the sessiond daemon.
3432 *
3433 * Return the sendmsg() return value.
3434 */
3435int consumer_send_status_msg(int sock, int ret_code)
3436{
3437 struct lttcomm_consumer_status_msg msg;
3438
3439 msg.ret_code = ret_code;
3440
3441 return lttcomm_send_unix_sock(sock, &msg, sizeof(msg));
3442}
ffe60014
DG
3443
3444/*
3445 * Send a channel status message to the sessiond daemon.
3446 *
3447 * Return the sendmsg() return value.
3448 */
3449int consumer_send_status_channel(int sock,
3450 struct lttng_consumer_channel *channel)
3451{
3452 struct lttcomm_consumer_status_channel msg;
3453
3454 assert(sock >= 0);
3455
3456 if (!channel) {
3457 msg.ret_code = -LTTNG_ERR_UST_CHAN_FAIL;
3458 } else {
3459 msg.ret_code = LTTNG_OK;
3460 msg.key = channel->key;
3461 msg.stream_count = channel->streams.count;
3462 }
3463
3464 return lttcomm_send_unix_sock(sock, &msg, sizeof(msg));
3465}
This page took 0.214965 seconds and 4 git commands to generate.