Commit | Line | Data |
---|---|---|
3bd1e081 MD |
1 | /* |
2 | * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca> | |
3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or | |
6 | * modify it under the terms of the GNU General Public License | |
7 | * as published by the Free Software Foundation; only version 2 | |
8 | * of the License. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
18 | */ | |
19 | ||
20 | #define _GNU_SOURCE | |
21 | #include <assert.h> | |
ca4537d3 | 22 | #include <fcntl.h> |
3bd1e081 MD |
23 | #include <poll.h> |
24 | #include <pthread.h> | |
25 | #include <stdlib.h> | |
26 | #include <string.h> | |
27 | #include <sys/mman.h> | |
28 | #include <sys/socket.h> | |
29 | #include <sys/types.h> | |
30 | #include <unistd.h> | |
31 | ||
990570ed | 32 | #include <common/common.h> |
10a8a223 DG |
33 | #include <common/kernel-ctl/kernel-ctl.h> |
34 | #include <common/sessiond-comm/sessiond-comm.h> | |
35 | #include <common/kernel-consumer/kernel-consumer.h> | |
36 | #include <common/ust-consumer/ust-consumer.h> | |
37 | ||
38 | #include "consumer.h" | |
3bd1e081 MD |
39 | |
40 | struct lttng_consumer_global_data consumer_data = { | |
3bd1e081 MD |
41 | .stream_count = 0, |
42 | .need_update = 1, | |
43 | .type = LTTNG_CONSUMER_UNKNOWN, | |
44 | }; | |
45 | ||
46 | /* timeout parameter, to control the polling thread grace period. */ | |
47 | int consumer_poll_timeout = -1; | |
48 | ||
49 | /* | |
50 | * Flag to inform the polling thread to quit when all fd hung up. Updated by | |
51 | * the consumer_thread_receive_fds when it notices that all fds has hung up. | |
52 | * Also updated by the signal handler (consumer_should_exit()). Read by the | |
53 | * polling threads. | |
54 | */ | |
55 | volatile int consumer_quit = 0; | |
56 | ||
57 | /* | |
58 | * Find a stream. The consumer_data.lock must be locked during this | |
59 | * call. | |
60 | */ | |
61 | static struct lttng_consumer_stream *consumer_find_stream(int key) | |
62 | { | |
e4421fec DG |
63 | struct lttng_ht_iter iter; |
64 | struct lttng_ht_node_ulong *node; | |
65 | struct lttng_consumer_stream *stream = NULL; | |
3bd1e081 | 66 | |
7ad0a0cb MD |
67 | /* Negative keys are lookup failures */ |
68 | if (key < 0) | |
69 | return NULL; | |
e4421fec | 70 | |
6065ceec DG |
71 | rcu_read_lock(); |
72 | ||
e4421fec DG |
73 | lttng_ht_lookup(consumer_data.stream_ht, (void *)((unsigned long) key), |
74 | &iter); | |
75 | node = lttng_ht_iter_get_node_ulong(&iter); | |
76 | if (node != NULL) { | |
77 | stream = caa_container_of(node, struct lttng_consumer_stream, node); | |
3bd1e081 | 78 | } |
e4421fec | 79 | |
6065ceec DG |
80 | rcu_read_unlock(); |
81 | ||
e4421fec | 82 | return stream; |
3bd1e081 MD |
83 | } |
84 | ||
7ad0a0cb MD |
85 | static void consumer_steal_stream_key(int key) |
86 | { | |
87 | struct lttng_consumer_stream *stream; | |
88 | ||
89 | stream = consumer_find_stream(key); | |
90 | if (stream) | |
91 | stream->key = -1; | |
92 | } | |
93 | ||
3bd1e081 MD |
94 | static struct lttng_consumer_channel *consumer_find_channel(int key) |
95 | { | |
e4421fec DG |
96 | struct lttng_ht_iter iter; |
97 | struct lttng_ht_node_ulong *node; | |
98 | struct lttng_consumer_channel *channel = NULL; | |
3bd1e081 | 99 | |
7ad0a0cb MD |
100 | /* Negative keys are lookup failures */ |
101 | if (key < 0) | |
102 | return NULL; | |
e4421fec | 103 | |
6065ceec DG |
104 | rcu_read_lock(); |
105 | ||
e4421fec DG |
106 | lttng_ht_lookup(consumer_data.channel_ht, (void *)((unsigned long) key), |
107 | &iter); | |
108 | node = lttng_ht_iter_get_node_ulong(&iter); | |
109 | if (node != NULL) { | |
110 | channel = caa_container_of(node, struct lttng_consumer_channel, node); | |
3bd1e081 | 111 | } |
e4421fec | 112 | |
6065ceec DG |
113 | rcu_read_unlock(); |
114 | ||
e4421fec | 115 | return channel; |
3bd1e081 MD |
116 | } |
117 | ||
7ad0a0cb MD |
118 | static void consumer_steal_channel_key(int key) |
119 | { | |
120 | struct lttng_consumer_channel *channel; | |
121 | ||
122 | channel = consumer_find_channel(key); | |
123 | if (channel) | |
124 | channel->key = -1; | |
125 | } | |
126 | ||
3bd1e081 MD |
127 | /* |
128 | * Remove a stream from the global list protected by a mutex. This | |
129 | * function is also responsible for freeing its data structures. | |
130 | */ | |
131 | void consumer_del_stream(struct lttng_consumer_stream *stream) | |
132 | { | |
133 | int ret; | |
e4421fec | 134 | struct lttng_ht_iter iter; |
3bd1e081 MD |
135 | struct lttng_consumer_channel *free_chan = NULL; |
136 | ||
137 | pthread_mutex_lock(&consumer_data.lock); | |
138 | ||
139 | switch (consumer_data.type) { | |
140 | case LTTNG_CONSUMER_KERNEL: | |
141 | if (stream->mmap_base != NULL) { | |
142 | ret = munmap(stream->mmap_base, stream->mmap_len); | |
143 | if (ret != 0) { | |
144 | perror("munmap"); | |
145 | } | |
146 | } | |
147 | break; | |
7753dea8 MD |
148 | case LTTNG_CONSUMER32_UST: |
149 | case LTTNG_CONSUMER64_UST: | |
3bd1e081 MD |
150 | lttng_ustconsumer_del_stream(stream); |
151 | break; | |
152 | default: | |
153 | ERR("Unknown consumer_data type"); | |
154 | assert(0); | |
155 | goto end; | |
156 | } | |
157 | ||
6065ceec DG |
158 | rcu_read_lock(); |
159 | ||
e4421fec DG |
160 | /* Get stream node from hash table */ |
161 | lttng_ht_lookup(consumer_data.stream_ht, | |
162 | (void *)((unsigned long) stream->key), &iter); | |
163 | /* Remove stream node from hash table */ | |
164 | ret = lttng_ht_del(consumer_data.stream_ht, &iter); | |
165 | assert(!ret); | |
166 | ||
6065ceec DG |
167 | rcu_read_unlock(); |
168 | ||
3bd1e081 MD |
169 | if (consumer_data.stream_count <= 0) { |
170 | goto end; | |
171 | } | |
172 | consumer_data.stream_count--; | |
173 | if (!stream) { | |
174 | goto end; | |
175 | } | |
176 | if (stream->out_fd >= 0) { | |
4c462e79 MD |
177 | ret = close(stream->out_fd); |
178 | if (ret) { | |
179 | PERROR("close"); | |
180 | } | |
3bd1e081 | 181 | } |
b5c5fc29 | 182 | if (stream->wait_fd >= 0 && !stream->wait_fd_is_copy) { |
4c462e79 MD |
183 | ret = close(stream->wait_fd); |
184 | if (ret) { | |
185 | PERROR("close"); | |
186 | } | |
3bd1e081 | 187 | } |
2c1dd183 | 188 | if (stream->shm_fd >= 0 && stream->wait_fd != stream->shm_fd) { |
4c462e79 MD |
189 | ret = close(stream->shm_fd); |
190 | if (ret) { | |
191 | PERROR("close"); | |
192 | } | |
3bd1e081 MD |
193 | } |
194 | if (!--stream->chan->refcount) | |
195 | free_chan = stream->chan; | |
196 | free(stream); | |
197 | end: | |
198 | consumer_data.need_update = 1; | |
199 | pthread_mutex_unlock(&consumer_data.lock); | |
200 | ||
201 | if (free_chan) | |
202 | consumer_del_channel(free_chan); | |
203 | } | |
204 | ||
6065ceec DG |
205 | static void consumer_del_stream_rcu(struct rcu_head *head) |
206 | { | |
207 | struct lttng_ht_node_ulong *node = | |
208 | caa_container_of(head, struct lttng_ht_node_ulong, head); | |
209 | struct lttng_consumer_stream *stream = | |
210 | caa_container_of(node, struct lttng_consumer_stream, node); | |
211 | ||
212 | consumer_del_stream(stream); | |
213 | } | |
214 | ||
3bd1e081 MD |
215 | struct lttng_consumer_stream *consumer_allocate_stream( |
216 | int channel_key, int stream_key, | |
217 | int shm_fd, int wait_fd, | |
218 | enum lttng_consumer_stream_state state, | |
219 | uint64_t mmap_len, | |
220 | enum lttng_event_output output, | |
6df2e2c9 MD |
221 | const char *path_name, |
222 | uid_t uid, | |
223 | gid_t gid) | |
3bd1e081 MD |
224 | { |
225 | struct lttng_consumer_stream *stream; | |
226 | int ret; | |
227 | ||
effcf122 | 228 | stream = zmalloc(sizeof(*stream)); |
3bd1e081 MD |
229 | if (stream == NULL) { |
230 | perror("malloc struct lttng_consumer_stream"); | |
231 | goto end; | |
232 | } | |
233 | stream->chan = consumer_find_channel(channel_key); | |
234 | if (!stream->chan) { | |
235 | perror("Unable to find channel key"); | |
236 | goto end; | |
237 | } | |
238 | stream->chan->refcount++; | |
239 | stream->key = stream_key; | |
240 | stream->shm_fd = shm_fd; | |
241 | stream->wait_fd = wait_fd; | |
242 | stream->out_fd = -1; | |
243 | stream->out_fd_offset = 0; | |
244 | stream->state = state; | |
245 | stream->mmap_len = mmap_len; | |
246 | stream->mmap_base = NULL; | |
247 | stream->output = output; | |
6df2e2c9 MD |
248 | stream->uid = uid; |
249 | stream->gid = gid; | |
3bd1e081 MD |
250 | strncpy(stream->path_name, path_name, PATH_MAX - 1); |
251 | stream->path_name[PATH_MAX - 1] = '\0'; | |
e4421fec | 252 | lttng_ht_node_init_ulong(&stream->node, stream->key); |
3bd1e081 MD |
253 | |
254 | switch (consumer_data.type) { | |
255 | case LTTNG_CONSUMER_KERNEL: | |
256 | break; | |
7753dea8 MD |
257 | case LTTNG_CONSUMER32_UST: |
258 | case LTTNG_CONSUMER64_UST: | |
5af2f756 | 259 | stream->cpu = stream->chan->cpucount++; |
3bd1e081 MD |
260 | ret = lttng_ustconsumer_allocate_stream(stream); |
261 | if (ret) { | |
262 | free(stream); | |
263 | return NULL; | |
264 | } | |
265 | break; | |
266 | default: | |
267 | ERR("Unknown consumer_data type"); | |
268 | assert(0); | |
269 | goto end; | |
270 | } | |
271 | DBG("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, out_fd %d)", | |
272 | stream->path_name, stream->key, | |
273 | stream->shm_fd, | |
274 | stream->wait_fd, | |
275 | (unsigned long long) stream->mmap_len, | |
276 | stream->out_fd); | |
277 | end: | |
278 | return stream; | |
279 | } | |
280 | ||
281 | /* | |
282 | * Add a stream to the global list protected by a mutex. | |
283 | */ | |
284 | int consumer_add_stream(struct lttng_consumer_stream *stream) | |
285 | { | |
286 | int ret = 0; | |
287 | ||
288 | pthread_mutex_lock(&consumer_data.lock); | |
7ad0a0cb MD |
289 | /* Steal stream identifier, for UST */ |
290 | consumer_steal_stream_key(stream->key); | |
6065ceec | 291 | rcu_read_lock(); |
e4421fec | 292 | lttng_ht_add_unique_ulong(consumer_data.stream_ht, &stream->node); |
6065ceec | 293 | rcu_read_unlock(); |
3bd1e081 MD |
294 | consumer_data.stream_count++; |
295 | consumer_data.need_update = 1; | |
296 | ||
297 | switch (consumer_data.type) { | |
298 | case LTTNG_CONSUMER_KERNEL: | |
299 | break; | |
7753dea8 MD |
300 | case LTTNG_CONSUMER32_UST: |
301 | case LTTNG_CONSUMER64_UST: | |
3bd1e081 MD |
302 | /* Streams are in CPU number order (we rely on this) */ |
303 | stream->cpu = stream->chan->nr_streams++; | |
304 | break; | |
305 | default: | |
306 | ERR("Unknown consumer_data type"); | |
307 | assert(0); | |
308 | goto end; | |
309 | } | |
310 | ||
311 | end: | |
312 | pthread_mutex_unlock(&consumer_data.lock); | |
313 | return ret; | |
314 | } | |
315 | ||
316 | /* | |
317 | * Update a stream according to what we just received. | |
318 | */ | |
319 | void consumer_change_stream_state(int stream_key, | |
320 | enum lttng_consumer_stream_state state) | |
321 | { | |
322 | struct lttng_consumer_stream *stream; | |
323 | ||
324 | pthread_mutex_lock(&consumer_data.lock); | |
325 | stream = consumer_find_stream(stream_key); | |
326 | if (stream) { | |
327 | stream->state = state; | |
328 | } | |
329 | consumer_data.need_update = 1; | |
330 | pthread_mutex_unlock(&consumer_data.lock); | |
331 | } | |
332 | ||
333 | /* | |
334 | * Remove a channel from the global list protected by a mutex. This | |
335 | * function is also responsible for freeing its data structures. | |
336 | */ | |
337 | void consumer_del_channel(struct lttng_consumer_channel *channel) | |
338 | { | |
339 | int ret; | |
e4421fec | 340 | struct lttng_ht_iter iter; |
3bd1e081 MD |
341 | |
342 | pthread_mutex_lock(&consumer_data.lock); | |
343 | ||
344 | switch (consumer_data.type) { | |
345 | case LTTNG_CONSUMER_KERNEL: | |
346 | break; | |
7753dea8 MD |
347 | case LTTNG_CONSUMER32_UST: |
348 | case LTTNG_CONSUMER64_UST: | |
3bd1e081 MD |
349 | lttng_ustconsumer_del_channel(channel); |
350 | break; | |
351 | default: | |
352 | ERR("Unknown consumer_data type"); | |
353 | assert(0); | |
354 | goto end; | |
355 | } | |
356 | ||
6065ceec DG |
357 | rcu_read_lock(); |
358 | ||
e4421fec DG |
359 | lttng_ht_lookup(consumer_data.channel_ht, |
360 | (void *)((unsigned long) channel->key), &iter); | |
361 | ret = lttng_ht_del(consumer_data.channel_ht, &iter); | |
362 | assert(!ret); | |
363 | ||
6065ceec DG |
364 | rcu_read_unlock(); |
365 | ||
3bd1e081 MD |
366 | if (channel->mmap_base != NULL) { |
367 | ret = munmap(channel->mmap_base, channel->mmap_len); | |
368 | if (ret != 0) { | |
369 | perror("munmap"); | |
370 | } | |
371 | } | |
b5c5fc29 | 372 | if (channel->wait_fd >= 0 && !channel->wait_fd_is_copy) { |
4c462e79 MD |
373 | ret = close(channel->wait_fd); |
374 | if (ret) { | |
375 | PERROR("close"); | |
376 | } | |
3bd1e081 | 377 | } |
2c1dd183 | 378 | if (channel->shm_fd >= 0 && channel->wait_fd != channel->shm_fd) { |
4c462e79 MD |
379 | ret = close(channel->shm_fd); |
380 | if (ret) { | |
381 | PERROR("close"); | |
382 | } | |
3bd1e081 MD |
383 | } |
384 | free(channel); | |
385 | end: | |
386 | pthread_mutex_unlock(&consumer_data.lock); | |
387 | } | |
388 | ||
6065ceec DG |
389 | static void consumer_del_channel_rcu(struct rcu_head *head) |
390 | { | |
391 | struct lttng_ht_node_ulong *node = | |
392 | caa_container_of(head, struct lttng_ht_node_ulong, head); | |
393 | struct lttng_consumer_channel *channel= | |
394 | caa_container_of(node, struct lttng_consumer_channel, node); | |
395 | ||
396 | consumer_del_channel(channel); | |
397 | } | |
398 | ||
3bd1e081 MD |
399 | struct lttng_consumer_channel *consumer_allocate_channel( |
400 | int channel_key, | |
401 | int shm_fd, int wait_fd, | |
402 | uint64_t mmap_len, | |
403 | uint64_t max_sb_size) | |
404 | { | |
405 | struct lttng_consumer_channel *channel; | |
406 | int ret; | |
407 | ||
276b26d1 | 408 | channel = zmalloc(sizeof(*channel)); |
3bd1e081 MD |
409 | if (channel == NULL) { |
410 | perror("malloc struct lttng_consumer_channel"); | |
411 | goto end; | |
412 | } | |
413 | channel->key = channel_key; | |
414 | channel->shm_fd = shm_fd; | |
415 | channel->wait_fd = wait_fd; | |
416 | channel->mmap_len = mmap_len; | |
417 | channel->max_sb_size = max_sb_size; | |
418 | channel->refcount = 0; | |
419 | channel->nr_streams = 0; | |
e4421fec | 420 | lttng_ht_node_init_ulong(&channel->node, channel->key); |
3bd1e081 MD |
421 | |
422 | switch (consumer_data.type) { | |
423 | case LTTNG_CONSUMER_KERNEL: | |
424 | channel->mmap_base = NULL; | |
425 | channel->mmap_len = 0; | |
426 | break; | |
7753dea8 MD |
427 | case LTTNG_CONSUMER32_UST: |
428 | case LTTNG_CONSUMER64_UST: | |
3bd1e081 MD |
429 | ret = lttng_ustconsumer_allocate_channel(channel); |
430 | if (ret) { | |
431 | free(channel); | |
432 | return NULL; | |
433 | } | |
434 | break; | |
435 | default: | |
436 | ERR("Unknown consumer_data type"); | |
437 | assert(0); | |
438 | goto end; | |
439 | } | |
440 | DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)", | |
441 | channel->key, | |
442 | channel->shm_fd, | |
443 | channel->wait_fd, | |
444 | (unsigned long long) channel->mmap_len, | |
445 | (unsigned long long) channel->max_sb_size); | |
446 | end: | |
447 | return channel; | |
448 | } | |
449 | ||
450 | /* | |
451 | * Add a channel to the global list protected by a mutex. | |
452 | */ | |
453 | int consumer_add_channel(struct lttng_consumer_channel *channel) | |
454 | { | |
3bd1e081 | 455 | pthread_mutex_lock(&consumer_data.lock); |
7ad0a0cb MD |
456 | /* Steal channel identifier, for UST */ |
457 | consumer_steal_channel_key(channel->key); | |
6065ceec | 458 | rcu_read_lock(); |
e4421fec | 459 | lttng_ht_add_unique_ulong(consumer_data.channel_ht, &channel->node); |
6065ceec | 460 | rcu_read_unlock(); |
3bd1e081 | 461 | pthread_mutex_unlock(&consumer_data.lock); |
7ad0a0cb | 462 | return 0; |
3bd1e081 MD |
463 | } |
464 | ||
465 | /* | |
466 | * Allocate the pollfd structure and the local view of the out fds to avoid | |
467 | * doing a lookup in the linked list and concurrency issues when writing is | |
468 | * needed. Called with consumer_data.lock held. | |
469 | * | |
470 | * Returns the number of fds in the structures. | |
471 | */ | |
472 | int consumer_update_poll_array( | |
473 | struct lttng_consumer_local_data *ctx, struct pollfd **pollfd, | |
474 | struct lttng_consumer_stream **local_stream) | |
475 | { | |
3bd1e081 | 476 | int i = 0; |
e4421fec DG |
477 | struct lttng_ht_iter iter; |
478 | struct lttng_consumer_stream *stream; | |
3bd1e081 MD |
479 | |
480 | DBG("Updating poll fd array"); | |
e4421fec DG |
481 | cds_lfht_for_each_entry(consumer_data.stream_ht->ht, &iter.iter, stream, |
482 | node.node) { | |
483 | if (stream->state != LTTNG_CONSUMER_ACTIVE_STREAM) { | |
3bd1e081 MD |
484 | continue; |
485 | } | |
e4421fec DG |
486 | DBG("Active FD %d", stream->wait_fd); |
487 | (*pollfd)[i].fd = stream->wait_fd; | |
3bd1e081 | 488 | (*pollfd)[i].events = POLLIN | POLLPRI; |
e4421fec | 489 | local_stream[i] = stream; |
3bd1e081 MD |
490 | i++; |
491 | } | |
492 | ||
493 | /* | |
494 | * Insert the consumer_poll_pipe at the end of the array and don't | |
495 | * increment i so nb_fd is the number of real FD. | |
496 | */ | |
497 | (*pollfd)[i].fd = ctx->consumer_poll_pipe[0]; | |
498 | (*pollfd)[i].events = POLLIN; | |
499 | return i; | |
500 | } | |
501 | ||
502 | /* | |
503 | * Poll on the should_quit pipe and the command socket return -1 on error and | |
504 | * should exit, 0 if data is available on the command socket | |
505 | */ | |
506 | int lttng_consumer_poll_socket(struct pollfd *consumer_sockpoll) | |
507 | { | |
508 | int num_rdy; | |
509 | ||
a8b0b341 | 510 | restart: |
3bd1e081 MD |
511 | num_rdy = poll(consumer_sockpoll, 2, -1); |
512 | if (num_rdy == -1) { | |
a8b0b341 MD |
513 | /* |
514 | * Restart interrupted system call. | |
515 | */ | |
516 | if (errno == EINTR) { | |
517 | goto restart; | |
518 | } | |
3bd1e081 MD |
519 | perror("Poll error"); |
520 | goto exit; | |
521 | } | |
522 | if (consumer_sockpoll[0].revents == POLLIN) { | |
523 | DBG("consumer_should_quit wake up"); | |
524 | goto exit; | |
525 | } | |
526 | return 0; | |
527 | ||
528 | exit: | |
529 | return -1; | |
530 | } | |
531 | ||
532 | /* | |
533 | * Set the error socket. | |
534 | */ | |
535 | void lttng_consumer_set_error_sock( | |
536 | struct lttng_consumer_local_data *ctx, int sock) | |
537 | { | |
538 | ctx->consumer_error_socket = sock; | |
539 | } | |
540 | ||
541 | /* | |
542 | * Set the command socket path. | |
543 | */ | |
544 | ||
545 | void lttng_consumer_set_command_sock_path( | |
546 | struct lttng_consumer_local_data *ctx, char *sock) | |
547 | { | |
548 | ctx->consumer_command_sock_path = sock; | |
549 | } | |
550 | ||
551 | /* | |
552 | * Send return code to the session daemon. | |
553 | * If the socket is not defined, we return 0, it is not a fatal error | |
554 | */ | |
555 | int lttng_consumer_send_error( | |
556 | struct lttng_consumer_local_data *ctx, int cmd) | |
557 | { | |
558 | if (ctx->consumer_error_socket > 0) { | |
559 | return lttcomm_send_unix_sock(ctx->consumer_error_socket, &cmd, | |
560 | sizeof(enum lttcomm_sessiond_command)); | |
561 | } | |
562 | ||
563 | return 0; | |
564 | } | |
565 | ||
566 | /* | |
567 | * Close all the tracefiles and stream fds, should be called when all instances | |
568 | * are destroyed. | |
569 | */ | |
570 | void lttng_consumer_cleanup(void) | |
571 | { | |
e4421fec DG |
572 | int ret; |
573 | struct lttng_ht_iter iter; | |
6065ceec DG |
574 | struct lttng_ht_node_ulong *node; |
575 | ||
576 | rcu_read_lock(); | |
3bd1e081 MD |
577 | |
578 | /* | |
6065ceec DG |
579 | * close all outfd. Called when there are no more threads running (after |
580 | * joining on the threads), no need to protect list iteration with mutex. | |
3bd1e081 | 581 | */ |
6065ceec DG |
582 | cds_lfht_for_each_entry(consumer_data.stream_ht->ht, &iter.iter, node, |
583 | node) { | |
e4421fec DG |
584 | ret = lttng_ht_del(consumer_data.stream_ht, &iter); |
585 | assert(!ret); | |
6065ceec | 586 | call_rcu(&node->head, consumer_del_stream_rcu); |
3bd1e081 | 587 | } |
e4421fec | 588 | |
6065ceec DG |
589 | cds_lfht_for_each_entry(consumer_data.channel_ht->ht, &iter.iter, node, |
590 | node) { | |
e4421fec DG |
591 | ret = lttng_ht_del(consumer_data.channel_ht, &iter); |
592 | assert(!ret); | |
6065ceec | 593 | call_rcu(&node->head, consumer_del_channel_rcu); |
3bd1e081 | 594 | } |
6065ceec DG |
595 | |
596 | rcu_read_unlock(); | |
3bd1e081 MD |
597 | } |
598 | ||
599 | /* | |
600 | * Called from signal handler. | |
601 | */ | |
602 | void lttng_consumer_should_exit(struct lttng_consumer_local_data *ctx) | |
603 | { | |
604 | int ret; | |
605 | consumer_quit = 1; | |
606 | ret = write(ctx->consumer_should_quit[1], "4", 1); | |
607 | if (ret < 0) { | |
608 | perror("write consumer quit"); | |
609 | } | |
610 | } | |
611 | ||
612 | void lttng_consumer_sync_trace_file( | |
613 | struct lttng_consumer_stream *stream, off_t orig_offset) | |
614 | { | |
615 | int outfd = stream->out_fd; | |
616 | ||
617 | /* | |
618 | * This does a blocking write-and-wait on any page that belongs to the | |
619 | * subbuffer prior to the one we just wrote. | |
620 | * Don't care about error values, as these are just hints and ways to | |
621 | * limit the amount of page cache used. | |
622 | */ | |
623 | if (orig_offset < stream->chan->max_sb_size) { | |
624 | return; | |
625 | } | |
ca4537d3 | 626 | sync_file_range(outfd, orig_offset - stream->chan->max_sb_size, |
3bd1e081 MD |
627 | stream->chan->max_sb_size, |
628 | SYNC_FILE_RANGE_WAIT_BEFORE | |
629 | | SYNC_FILE_RANGE_WRITE | |
630 | | SYNC_FILE_RANGE_WAIT_AFTER); | |
631 | /* | |
632 | * Give hints to the kernel about how we access the file: | |
633 | * POSIX_FADV_DONTNEED : we won't re-access data in a near future after | |
634 | * we write it. | |
635 | * | |
636 | * We need to call fadvise again after the file grows because the | |
637 | * kernel does not seem to apply fadvise to non-existing parts of the | |
638 | * file. | |
639 | * | |
640 | * Call fadvise _after_ having waited for the page writeback to | |
641 | * complete because the dirty page writeback semantic is not well | |
642 | * defined. So it can be expected to lead to lower throughput in | |
643 | * streaming. | |
644 | */ | |
645 | posix_fadvise(outfd, orig_offset - stream->chan->max_sb_size, | |
646 | stream->chan->max_sb_size, POSIX_FADV_DONTNEED); | |
647 | } | |
648 | ||
649 | /* | |
650 | * Initialise the necessary environnement : | |
651 | * - create a new context | |
652 | * - create the poll_pipe | |
653 | * - create the should_quit pipe (for signal handler) | |
654 | * - create the thread pipe (for splice) | |
655 | * | |
656 | * Takes a function pointer as argument, this function is called when data is | |
657 | * available on a buffer. This function is responsible to do the | |
658 | * kernctl_get_next_subbuf, read the data with mmap or splice depending on the | |
659 | * buffer configuration and then kernctl_put_next_subbuf at the end. | |
660 | * | |
661 | * Returns a pointer to the new context or NULL on error. | |
662 | */ | |
663 | struct lttng_consumer_local_data *lttng_consumer_create( | |
664 | enum lttng_consumer_type type, | |
f5204c94 | 665 | ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream, |
d41f73b7 | 666 | struct lttng_consumer_local_data *ctx), |
3bd1e081 MD |
667 | int (*recv_channel)(struct lttng_consumer_channel *channel), |
668 | int (*recv_stream)(struct lttng_consumer_stream *stream), | |
669 | int (*update_stream)(int stream_key, uint32_t state)) | |
670 | { | |
671 | int ret, i; | |
672 | struct lttng_consumer_local_data *ctx; | |
673 | ||
674 | assert(consumer_data.type == LTTNG_CONSUMER_UNKNOWN || | |
675 | consumer_data.type == type); | |
676 | consumer_data.type = type; | |
677 | ||
effcf122 | 678 | ctx = zmalloc(sizeof(struct lttng_consumer_local_data)); |
3bd1e081 MD |
679 | if (ctx == NULL) { |
680 | perror("allocating context"); | |
681 | goto error; | |
682 | } | |
683 | ||
684 | ctx->consumer_error_socket = -1; | |
685 | /* assign the callbacks */ | |
686 | ctx->on_buffer_ready = buffer_ready; | |
687 | ctx->on_recv_channel = recv_channel; | |
688 | ctx->on_recv_stream = recv_stream; | |
689 | ctx->on_update_stream = update_stream; | |
690 | ||
691 | ret = pipe(ctx->consumer_poll_pipe); | |
692 | if (ret < 0) { | |
693 | perror("Error creating poll pipe"); | |
694 | goto error_poll_pipe; | |
695 | } | |
696 | ||
697 | ret = pipe(ctx->consumer_should_quit); | |
698 | if (ret < 0) { | |
699 | perror("Error creating recv pipe"); | |
700 | goto error_quit_pipe; | |
701 | } | |
702 | ||
703 | ret = pipe(ctx->consumer_thread_pipe); | |
704 | if (ret < 0) { | |
705 | perror("Error creating thread pipe"); | |
706 | goto error_thread_pipe; | |
707 | } | |
708 | ||
709 | return ctx; | |
710 | ||
711 | ||
712 | error_thread_pipe: | |
713 | for (i = 0; i < 2; i++) { | |
714 | int err; | |
715 | ||
716 | err = close(ctx->consumer_should_quit[i]); | |
4c462e79 MD |
717 | if (err) { |
718 | PERROR("close"); | |
719 | } | |
3bd1e081 MD |
720 | } |
721 | error_quit_pipe: | |
722 | for (i = 0; i < 2; i++) { | |
723 | int err; | |
724 | ||
725 | err = close(ctx->consumer_poll_pipe[i]); | |
4c462e79 MD |
726 | if (err) { |
727 | PERROR("close"); | |
728 | } | |
3bd1e081 MD |
729 | } |
730 | error_poll_pipe: | |
731 | free(ctx); | |
732 | error: | |
733 | return NULL; | |
734 | } | |
735 | ||
736 | /* | |
737 | * Close all fds associated with the instance and free the context. | |
738 | */ | |
739 | void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx) | |
740 | { | |
4c462e79 MD |
741 | int ret; |
742 | ||
743 | ret = close(ctx->consumer_error_socket); | |
744 | if (ret) { | |
745 | PERROR("close"); | |
746 | } | |
747 | ret = close(ctx->consumer_thread_pipe[0]); | |
748 | if (ret) { | |
749 | PERROR("close"); | |
750 | } | |
751 | ret = close(ctx->consumer_thread_pipe[1]); | |
752 | if (ret) { | |
753 | PERROR("close"); | |
754 | } | |
755 | ret = close(ctx->consumer_poll_pipe[0]); | |
756 | if (ret) { | |
757 | PERROR("close"); | |
758 | } | |
759 | ret = close(ctx->consumer_poll_pipe[1]); | |
760 | if (ret) { | |
761 | PERROR("close"); | |
762 | } | |
763 | ret = close(ctx->consumer_should_quit[0]); | |
764 | if (ret) { | |
765 | PERROR("close"); | |
766 | } | |
767 | ret = close(ctx->consumer_should_quit[1]); | |
768 | if (ret) { | |
769 | PERROR("close"); | |
770 | } | |
3bd1e081 MD |
771 | unlink(ctx->consumer_command_sock_path); |
772 | free(ctx); | |
773 | } | |
774 | ||
775 | /* | |
776 | * Mmap the ring buffer, read it and write the data to the tracefile. | |
777 | * | |
778 | * Returns the number of bytes written | |
779 | */ | |
f5204c94 | 780 | ssize_t lttng_consumer_on_read_subbuffer_mmap( |
3bd1e081 MD |
781 | struct lttng_consumer_local_data *ctx, |
782 | struct lttng_consumer_stream *stream, unsigned long len) | |
783 | { | |
784 | switch (consumer_data.type) { | |
785 | case LTTNG_CONSUMER_KERNEL: | |
786 | return lttng_kconsumer_on_read_subbuffer_mmap(ctx, stream, len); | |
7753dea8 MD |
787 | case LTTNG_CONSUMER32_UST: |
788 | case LTTNG_CONSUMER64_UST: | |
3bd1e081 MD |
789 | return lttng_ustconsumer_on_read_subbuffer_mmap(ctx, stream, len); |
790 | default: | |
791 | ERR("Unknown consumer_data type"); | |
792 | assert(0); | |
793 | } | |
794 | } | |
795 | ||
796 | /* | |
797 | * Splice the data from the ring buffer to the tracefile. | |
798 | * | |
799 | * Returns the number of bytes spliced. | |
800 | */ | |
f5204c94 | 801 | ssize_t lttng_consumer_on_read_subbuffer_splice( |
3bd1e081 MD |
802 | struct lttng_consumer_local_data *ctx, |
803 | struct lttng_consumer_stream *stream, unsigned long len) | |
804 | { | |
805 | switch (consumer_data.type) { | |
806 | case LTTNG_CONSUMER_KERNEL: | |
807 | return lttng_kconsumer_on_read_subbuffer_splice(ctx, stream, len); | |
7753dea8 MD |
808 | case LTTNG_CONSUMER32_UST: |
809 | case LTTNG_CONSUMER64_UST: | |
3bd1e081 MD |
810 | return -ENOSYS; |
811 | default: | |
812 | ERR("Unknown consumer_data type"); | |
813 | assert(0); | |
814 | return -ENOSYS; | |
815 | } | |
816 | ||
817 | } | |
818 | ||
819 | /* | |
820 | * Take a snapshot for a specific fd | |
821 | * | |
822 | * Returns 0 on success, < 0 on error | |
823 | */ | |
824 | int lttng_consumer_take_snapshot(struct lttng_consumer_local_data *ctx, | |
825 | struct lttng_consumer_stream *stream) | |
826 | { | |
827 | switch (consumer_data.type) { | |
828 | case LTTNG_CONSUMER_KERNEL: | |
829 | return lttng_kconsumer_take_snapshot(ctx, stream); | |
7753dea8 MD |
830 | case LTTNG_CONSUMER32_UST: |
831 | case LTTNG_CONSUMER64_UST: | |
3bd1e081 MD |
832 | return lttng_ustconsumer_take_snapshot(ctx, stream); |
833 | default: | |
834 | ERR("Unknown consumer_data type"); | |
835 | assert(0); | |
836 | return -ENOSYS; | |
837 | } | |
838 | ||
839 | } | |
840 | ||
841 | /* | |
842 | * Get the produced position | |
843 | * | |
844 | * Returns 0 on success, < 0 on error | |
845 | */ | |
846 | int lttng_consumer_get_produced_snapshot( | |
847 | struct lttng_consumer_local_data *ctx, | |
848 | struct lttng_consumer_stream *stream, | |
849 | unsigned long *pos) | |
850 | { | |
851 | switch (consumer_data.type) { | |
852 | case LTTNG_CONSUMER_KERNEL: | |
853 | return lttng_kconsumer_get_produced_snapshot(ctx, stream, pos); | |
7753dea8 MD |
854 | case LTTNG_CONSUMER32_UST: |
855 | case LTTNG_CONSUMER64_UST: | |
3bd1e081 MD |
856 | return lttng_ustconsumer_get_produced_snapshot(ctx, stream, pos); |
857 | default: | |
858 | ERR("Unknown consumer_data type"); | |
859 | assert(0); | |
860 | return -ENOSYS; | |
861 | } | |
862 | } | |
863 | ||
864 | int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx, | |
865 | int sock, struct pollfd *consumer_sockpoll) | |
866 | { | |
867 | switch (consumer_data.type) { | |
868 | case LTTNG_CONSUMER_KERNEL: | |
869 | return lttng_kconsumer_recv_cmd(ctx, sock, consumer_sockpoll); | |
7753dea8 MD |
870 | case LTTNG_CONSUMER32_UST: |
871 | case LTTNG_CONSUMER64_UST: | |
3bd1e081 MD |
872 | return lttng_ustconsumer_recv_cmd(ctx, sock, consumer_sockpoll); |
873 | default: | |
874 | ERR("Unknown consumer_data type"); | |
875 | assert(0); | |
876 | return -ENOSYS; | |
877 | } | |
878 | } | |
879 | ||
880 | /* | |
e4421fec | 881 | * This thread polls the fds in the set to consume the data and write |
3bd1e081 MD |
882 | * it to tracefile if necessary. |
883 | */ | |
884 | void *lttng_consumer_thread_poll_fds(void *data) | |
885 | { | |
886 | int num_rdy, num_hup, high_prio, ret, i; | |
887 | struct pollfd *pollfd = NULL; | |
888 | /* local view of the streams */ | |
889 | struct lttng_consumer_stream **local_stream = NULL; | |
890 | /* local view of consumer_data.fds_count */ | |
891 | int nb_fd = 0; | |
892 | char tmp; | |
893 | int tmp2; | |
894 | struct lttng_consumer_local_data *ctx = data; | |
895 | ||
e7b994a3 DG |
896 | rcu_register_thread(); |
897 | ||
effcf122 | 898 | local_stream = zmalloc(sizeof(struct lttng_consumer_stream)); |
3bd1e081 MD |
899 | |
900 | while (1) { | |
901 | high_prio = 0; | |
902 | num_hup = 0; | |
903 | ||
904 | /* | |
e4421fec | 905 | * the fds set has been updated, we need to update our |
3bd1e081 MD |
906 | * local array as well |
907 | */ | |
908 | pthread_mutex_lock(&consumer_data.lock); | |
909 | if (consumer_data.need_update) { | |
910 | if (pollfd != NULL) { | |
911 | free(pollfd); | |
912 | pollfd = NULL; | |
913 | } | |
914 | if (local_stream != NULL) { | |
915 | free(local_stream); | |
916 | local_stream = NULL; | |
917 | } | |
918 | ||
919 | /* allocate for all fds + 1 for the consumer_poll_pipe */ | |
effcf122 | 920 | pollfd = zmalloc((consumer_data.stream_count + 1) * sizeof(struct pollfd)); |
3bd1e081 MD |
921 | if (pollfd == NULL) { |
922 | perror("pollfd malloc"); | |
923 | pthread_mutex_unlock(&consumer_data.lock); | |
924 | goto end; | |
925 | } | |
926 | ||
927 | /* allocate for all fds + 1 for the consumer_poll_pipe */ | |
effcf122 | 928 | local_stream = zmalloc((consumer_data.stream_count + 1) * |
3bd1e081 MD |
929 | sizeof(struct lttng_consumer_stream)); |
930 | if (local_stream == NULL) { | |
931 | perror("local_stream malloc"); | |
932 | pthread_mutex_unlock(&consumer_data.lock); | |
933 | goto end; | |
934 | } | |
935 | ret = consumer_update_poll_array(ctx, &pollfd, local_stream); | |
936 | if (ret < 0) { | |
937 | ERR("Error in allocating pollfd or local_outfds"); | |
938 | lttng_consumer_send_error(ctx, CONSUMERD_POLL_ERROR); | |
939 | pthread_mutex_unlock(&consumer_data.lock); | |
940 | goto end; | |
941 | } | |
942 | nb_fd = ret; | |
943 | consumer_data.need_update = 0; | |
944 | } | |
945 | pthread_mutex_unlock(&consumer_data.lock); | |
946 | ||
f5204c94 MD |
947 | /* No FDs and consumer_quit, consumer_cleanup the thread */ |
948 | if (nb_fd == 0 && consumer_quit == 1) { | |
949 | goto end; | |
950 | } | |
3bd1e081 | 951 | /* poll on the array of fds */ |
a8b0b341 | 952 | restart: |
3bd1e081 MD |
953 | DBG("polling on %d fd", nb_fd + 1); |
954 | num_rdy = poll(pollfd, nb_fd + 1, consumer_poll_timeout); | |
955 | DBG("poll num_rdy : %d", num_rdy); | |
956 | if (num_rdy == -1) { | |
a8b0b341 MD |
957 | /* |
958 | * Restart interrupted system call. | |
959 | */ | |
960 | if (errno == EINTR) { | |
961 | goto restart; | |
962 | } | |
3bd1e081 MD |
963 | perror("Poll error"); |
964 | lttng_consumer_send_error(ctx, CONSUMERD_POLL_ERROR); | |
965 | goto end; | |
966 | } else if (num_rdy == 0) { | |
967 | DBG("Polling thread timed out"); | |
968 | goto end; | |
969 | } | |
970 | ||
3bd1e081 MD |
971 | /* |
972 | * If the consumer_poll_pipe triggered poll go | |
973 | * directly to the beginning of the loop to update the | |
974 | * array. We want to prioritize array update over | |
975 | * low-priority reads. | |
976 | */ | |
d41f73b7 | 977 | if (pollfd[nb_fd].revents & POLLIN) { |
3bd1e081 MD |
978 | DBG("consumer_poll_pipe wake up"); |
979 | tmp2 = read(ctx->consumer_poll_pipe[0], &tmp, 1); | |
980 | if (tmp2 < 0) { | |
d41f73b7 | 981 | perror("read consumer poll"); |
3bd1e081 MD |
982 | } |
983 | continue; | |
984 | } | |
985 | ||
986 | /* Take care of high priority channels first. */ | |
987 | for (i = 0; i < nb_fd; i++) { | |
d41f73b7 | 988 | if (pollfd[i].revents & POLLPRI) { |
f5204c94 MD |
989 | ssize_t len; |
990 | ||
d41f73b7 MD |
991 | DBG("Urgent read on fd %d", pollfd[i].fd); |
992 | high_prio = 1; | |
f5204c94 | 993 | len = ctx->on_buffer_ready(local_stream[i], ctx); |
d41f73b7 | 994 | /* it's ok to have an unavailable sub-buffer */ |
f5204c94 MD |
995 | if (len < 0 && len != -EAGAIN) { |
996 | goto end; | |
997 | } else if (len > 0) { | |
998 | local_stream[i]->data_read = 1; | |
d41f73b7 | 999 | } |
3bd1e081 MD |
1000 | } |
1001 | } | |
1002 | ||
f5204c94 MD |
1003 | /* |
1004 | * If we read high prio channel in this loop, try again | |
1005 | * for more high prio data. | |
1006 | */ | |
1007 | if (high_prio) { | |
3bd1e081 MD |
1008 | continue; |
1009 | } | |
1010 | ||
1011 | /* Take care of low priority channels. */ | |
f5204c94 MD |
1012 | for (i = 0; i < nb_fd; i++) { |
1013 | if ((pollfd[i].revents & POLLIN) || | |
1014 | local_stream[i]->hangup_flush_done) { | |
1015 | ssize_t len; | |
1016 | ||
1017 | assert(!(pollfd[i].revents & POLLERR)); | |
1018 | assert(!(pollfd[i].revents & POLLNVAL)); | |
1019 | DBG("Normal read on fd %d", pollfd[i].fd); | |
1020 | len = ctx->on_buffer_ready(local_stream[i], ctx); | |
1021 | /* it's ok to have an unavailable sub-buffer */ | |
1022 | if (len < 0 && len != -EAGAIN) { | |
1023 | goto end; | |
1024 | } else if (len > 0) { | |
1025 | local_stream[i]->data_read = 1; | |
1026 | } | |
1027 | } | |
1028 | } | |
1029 | ||
1030 | /* Handle hangup and errors */ | |
1031 | for (i = 0; i < nb_fd; i++) { | |
1032 | if (!local_stream[i]->hangup_flush_done | |
1033 | && (pollfd[i].revents & (POLLHUP | POLLERR | POLLNVAL)) | |
1034 | && (consumer_data.type == LTTNG_CONSUMER32_UST | |
1035 | || consumer_data.type == LTTNG_CONSUMER64_UST)) { | |
1036 | DBG("fd %d is hup|err|nval. Attempting flush and read.", | |
1037 | pollfd[i].fd); | |
1038 | lttng_ustconsumer_on_stream_hangup(local_stream[i]); | |
1039 | /* Attempt read again, for the data we just flushed. */ | |
1040 | local_stream[i]->data_read = 1; | |
1041 | } | |
1042 | /* | |
1043 | * If the poll flag is HUP/ERR/NVAL and we have | |
1044 | * read no data in this pass, we can remove the | |
1045 | * stream from its hash table. | |
1046 | */ | |
1047 | if ((pollfd[i].revents & POLLHUP)) { | |
1048 | DBG("Polling fd %d tells it has hung up.", pollfd[i].fd); | |
1049 | if (!local_stream[i]->data_read) { | |
1050 | rcu_read_lock(); | |
1051 | consumer_del_stream_rcu(&local_stream[i]->node.head); | |
1052 | rcu_read_unlock(); | |
1053 | num_hup++; | |
1054 | } | |
1055 | } else if (pollfd[i].revents & POLLERR) { | |
1056 | ERR("Error returned in polling fd %d.", pollfd[i].fd); | |
1057 | if (!local_stream[i]->data_read) { | |
1058 | rcu_read_lock(); | |
1059 | consumer_del_stream_rcu(&local_stream[i]->node.head); | |
1060 | rcu_read_unlock(); | |
1061 | num_hup++; | |
1062 | } | |
1063 | } else if (pollfd[i].revents & POLLNVAL) { | |
1064 | ERR("Polling fd %d tells fd is not open.", pollfd[i].fd); | |
1065 | if (!local_stream[i]->data_read) { | |
1066 | rcu_read_lock(); | |
1067 | consumer_del_stream_rcu(&local_stream[i]->node.head); | |
1068 | rcu_read_unlock(); | |
1069 | num_hup++; | |
3bd1e081 MD |
1070 | } |
1071 | } | |
f5204c94 | 1072 | local_stream[i]->data_read = 0; |
3bd1e081 MD |
1073 | } |
1074 | } | |
1075 | end: | |
1076 | DBG("polling thread exiting"); | |
1077 | if (pollfd != NULL) { | |
1078 | free(pollfd); | |
1079 | pollfd = NULL; | |
1080 | } | |
1081 | if (local_stream != NULL) { | |
1082 | free(local_stream); | |
1083 | local_stream = NULL; | |
1084 | } | |
e7b994a3 | 1085 | rcu_unregister_thread(); |
3bd1e081 MD |
1086 | return NULL; |
1087 | } | |
1088 | ||
1089 | /* | |
1090 | * This thread listens on the consumerd socket and receives the file | |
1091 | * descriptors from the session daemon. | |
1092 | */ | |
1093 | void *lttng_consumer_thread_receive_fds(void *data) | |
1094 | { | |
1095 | int sock, client_socket, ret; | |
1096 | /* | |
1097 | * structure to poll for incoming data on communication socket avoids | |
1098 | * making blocking sockets. | |
1099 | */ | |
1100 | struct pollfd consumer_sockpoll[2]; | |
1101 | struct lttng_consumer_local_data *ctx = data; | |
1102 | ||
e7b994a3 DG |
1103 | rcu_register_thread(); |
1104 | ||
3bd1e081 MD |
1105 | DBG("Creating command socket %s", ctx->consumer_command_sock_path); |
1106 | unlink(ctx->consumer_command_sock_path); | |
1107 | client_socket = lttcomm_create_unix_sock(ctx->consumer_command_sock_path); | |
1108 | if (client_socket < 0) { | |
1109 | ERR("Cannot create command socket"); | |
1110 | goto end; | |
1111 | } | |
1112 | ||
1113 | ret = lttcomm_listen_unix_sock(client_socket); | |
1114 | if (ret < 0) { | |
1115 | goto end; | |
1116 | } | |
1117 | ||
32258573 | 1118 | DBG("Sending ready command to lttng-sessiond"); |
3bd1e081 MD |
1119 | ret = lttng_consumer_send_error(ctx, CONSUMERD_COMMAND_SOCK_READY); |
1120 | /* return < 0 on error, but == 0 is not fatal */ | |
1121 | if (ret < 0) { | |
32258573 | 1122 | ERR("Error sending ready command to lttng-sessiond"); |
3bd1e081 MD |
1123 | goto end; |
1124 | } | |
1125 | ||
1126 | ret = fcntl(client_socket, F_SETFL, O_NONBLOCK); | |
1127 | if (ret < 0) { | |
1128 | perror("fcntl O_NONBLOCK"); | |
1129 | goto end; | |
1130 | } | |
1131 | ||
1132 | /* prepare the FDs to poll : to client socket and the should_quit pipe */ | |
1133 | consumer_sockpoll[0].fd = ctx->consumer_should_quit[0]; | |
1134 | consumer_sockpoll[0].events = POLLIN | POLLPRI; | |
1135 | consumer_sockpoll[1].fd = client_socket; | |
1136 | consumer_sockpoll[1].events = POLLIN | POLLPRI; | |
1137 | ||
1138 | if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) { | |
1139 | goto end; | |
1140 | } | |
1141 | DBG("Connection on client_socket"); | |
1142 | ||
1143 | /* Blocking call, waiting for transmission */ | |
1144 | sock = lttcomm_accept_unix_sock(client_socket); | |
1145 | if (sock <= 0) { | |
1146 | WARN("On accept"); | |
1147 | goto end; | |
1148 | } | |
1149 | ret = fcntl(sock, F_SETFL, O_NONBLOCK); | |
1150 | if (ret < 0) { | |
1151 | perror("fcntl O_NONBLOCK"); | |
1152 | goto end; | |
1153 | } | |
1154 | ||
1155 | /* update the polling structure to poll on the established socket */ | |
1156 | consumer_sockpoll[1].fd = sock; | |
1157 | consumer_sockpoll[1].events = POLLIN | POLLPRI; | |
1158 | ||
1159 | while (1) { | |
1160 | if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) { | |
1161 | goto end; | |
1162 | } | |
1163 | DBG("Incoming command on sock"); | |
1164 | ret = lttng_consumer_recv_cmd(ctx, sock, consumer_sockpoll); | |
1165 | if (ret == -ENOENT) { | |
1166 | DBG("Received STOP command"); | |
1167 | goto end; | |
1168 | } | |
1169 | if (ret < 0) { | |
1170 | ERR("Communication interrupted on command socket"); | |
1171 | goto end; | |
1172 | } | |
1173 | if (consumer_quit) { | |
1174 | DBG("consumer_thread_receive_fds received quit from signal"); | |
1175 | goto end; | |
1176 | } | |
1177 | DBG("received fds on sock"); | |
1178 | } | |
1179 | end: | |
1180 | DBG("consumer_thread_receive_fds exiting"); | |
1181 | ||
1182 | /* | |
1183 | * when all fds have hung up, the polling thread | |
1184 | * can exit cleanly | |
1185 | */ | |
1186 | consumer_quit = 1; | |
1187 | ||
1188 | /* | |
1189 | * 2s of grace period, if no polling events occur during | |
1190 | * this period, the polling thread will exit even if there | |
1191 | * are still open FDs (should not happen, but safety mechanism). | |
1192 | */ | |
1193 | consumer_poll_timeout = LTTNG_CONSUMER_POLL_TIMEOUT; | |
1194 | ||
1195 | /* wake up the polling thread */ | |
1196 | ret = write(ctx->consumer_poll_pipe[1], "4", 1); | |
1197 | if (ret < 0) { | |
1198 | perror("poll pipe write"); | |
1199 | } | |
e7b994a3 | 1200 | rcu_unregister_thread(); |
3bd1e081 MD |
1201 | return NULL; |
1202 | } | |
d41f73b7 | 1203 | |
f5204c94 | 1204 | ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream, |
d41f73b7 MD |
1205 | struct lttng_consumer_local_data *ctx) |
1206 | { | |
1207 | switch (consumer_data.type) { | |
1208 | case LTTNG_CONSUMER_KERNEL: | |
1209 | return lttng_kconsumer_read_subbuffer(stream, ctx); | |
7753dea8 MD |
1210 | case LTTNG_CONSUMER32_UST: |
1211 | case LTTNG_CONSUMER64_UST: | |
d41f73b7 MD |
1212 | return lttng_ustconsumer_read_subbuffer(stream, ctx); |
1213 | default: | |
1214 | ERR("Unknown consumer_data type"); | |
1215 | assert(0); | |
1216 | return -ENOSYS; | |
1217 | } | |
1218 | } | |
1219 | ||
1220 | int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream) | |
1221 | { | |
1222 | switch (consumer_data.type) { | |
1223 | case LTTNG_CONSUMER_KERNEL: | |
1224 | return lttng_kconsumer_on_recv_stream(stream); | |
7753dea8 MD |
1225 | case LTTNG_CONSUMER32_UST: |
1226 | case LTTNG_CONSUMER64_UST: | |
d41f73b7 MD |
1227 | return lttng_ustconsumer_on_recv_stream(stream); |
1228 | default: | |
1229 | ERR("Unknown consumer_data type"); | |
1230 | assert(0); | |
1231 | return -ENOSYS; | |
1232 | } | |
1233 | } | |
e4421fec DG |
1234 | |
1235 | /* | |
1236 | * Allocate and set consumer data hash tables. | |
1237 | */ | |
1238 | void lttng_consumer_init(void) | |
1239 | { | |
1240 | consumer_data.stream_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); | |
1241 | consumer_data.channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); | |
1242 | } | |
1243 |