Commit | Line | Data |
---|---|---|
3bd1e081 MD |
1 | /* |
2 | * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca> | |
3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
4 | * | |
d14d33bf AM |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License, version 2 only, | |
7 | * as published by the Free Software Foundation. | |
3bd1e081 MD |
8 | * |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
d14d33bf AM |
14 | * You should have received a copy of the GNU General Public License along |
15 | * with this program; if not, write to the Free Software Foundation, Inc., | |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
3bd1e081 MD |
17 | */ |
18 | ||
19 | #define _GNU_SOURCE | |
20 | #include <assert.h> | |
3bd1e081 MD |
21 | #include <poll.h> |
22 | #include <pthread.h> | |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
25 | #include <sys/mman.h> | |
26 | #include <sys/socket.h> | |
27 | #include <sys/types.h> | |
77c7c900 | 28 | #include <inttypes.h> |
3bd1e081 | 29 | #include <unistd.h> |
dbb5dfe6 | 30 | #include <sys/stat.h> |
3bd1e081 | 31 | |
990570ed | 32 | #include <common/common.h> |
10a8a223 | 33 | #include <common/kernel-ctl/kernel-ctl.h> |
10a8a223 | 34 | #include <common/sessiond-comm/sessiond-comm.h> |
00e2e675 | 35 | #include <common/sessiond-comm/relayd.h> |
dbb5dfe6 | 36 | #include <common/compat/fcntl.h> |
acdb9057 | 37 | #include <common/pipe.h> |
00e2e675 | 38 | #include <common/relayd/relayd.h> |
fe4477ee | 39 | #include <common/utils.h> |
0857097f | 40 | |
10a8a223 | 41 | #include "kernel-consumer.h" |
3bd1e081 MD |
42 | |
43 | extern struct lttng_consumer_global_data consumer_data; | |
44 | extern int consumer_poll_timeout; | |
45 | extern volatile int consumer_quit; | |
46 | ||
3bd1e081 MD |
47 | /* |
48 | * Take a snapshot for a specific fd | |
49 | * | |
50 | * Returns 0 on success, < 0 on error | |
51 | */ | |
ffe60014 | 52 | int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream *stream) |
3bd1e081 MD |
53 | { |
54 | int ret = 0; | |
55 | int infd = stream->wait_fd; | |
56 | ||
57 | ret = kernctl_snapshot(infd); | |
58 | if (ret != 0) { | |
87dc6a9c | 59 | errno = -ret; |
3bd1e081 MD |
60 | perror("Getting sub-buffer snapshot."); |
61 | } | |
62 | ||
63 | return ret; | |
64 | } | |
65 | ||
66 | /* | |
67 | * Get the produced position | |
68 | * | |
69 | * Returns 0 on success, < 0 on error | |
70 | */ | |
ffe60014 | 71 | int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream *stream, |
3bd1e081 MD |
72 | unsigned long *pos) |
73 | { | |
74 | int ret; | |
75 | int infd = stream->wait_fd; | |
76 | ||
77 | ret = kernctl_snapshot_get_produced(infd, pos); | |
78 | if (ret != 0) { | |
87dc6a9c | 79 | errno = -ret; |
3bd1e081 MD |
80 | perror("kernctl_snapshot_get_produced"); |
81 | } | |
82 | ||
83 | return ret; | |
84 | } | |
85 | ||
86 | int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx, | |
87 | int sock, struct pollfd *consumer_sockpoll) | |
88 | { | |
89 | ssize_t ret; | |
f50f23d9 | 90 | enum lttng_error_code ret_code = LTTNG_OK; |
3bd1e081 MD |
91 | struct lttcomm_consumer_msg msg; |
92 | ||
93 | ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg)); | |
94 | if (ret != sizeof(msg)) { | |
f73fabfd | 95 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD); |
3bd1e081 MD |
96 | return ret; |
97 | } | |
98 | if (msg.cmd_type == LTTNG_CONSUMER_STOP) { | |
f50f23d9 DG |
99 | /* |
100 | * Notify the session daemon that the command is completed. | |
101 | * | |
102 | * On transport layer error, the function call will print an error | |
103 | * message so handling the returned code is a bit useless since we | |
104 | * return an error code anyway. | |
105 | */ | |
106 | (void) consumer_send_status_msg(sock, ret_code); | |
3bd1e081 MD |
107 | return -ENOENT; |
108 | } | |
109 | ||
b0b335c8 MD |
110 | /* relayd needs RCU read-side protection */ |
111 | rcu_read_lock(); | |
112 | ||
3bd1e081 | 113 | switch (msg.cmd_type) { |
00e2e675 DG |
114 | case LTTNG_CONSUMER_ADD_RELAYD_SOCKET: |
115 | { | |
f50f23d9 | 116 | /* Session daemon status message are handled in the following call. */ |
7735ef9e DG |
117 | ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index, |
118 | msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll, | |
46e6455f | 119 | &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id); |
00e2e675 DG |
120 | goto end_nosignal; |
121 | } | |
3bd1e081 MD |
122 | case LTTNG_CONSUMER_ADD_CHANNEL: |
123 | { | |
124 | struct lttng_consumer_channel *new_channel; | |
125 | ||
f50f23d9 DG |
126 | /* First send a status message before receiving the fds. */ |
127 | ret = consumer_send_status_msg(sock, ret_code); | |
128 | if (ret < 0) { | |
129 | /* Somehow, the session daemon is not responding anymore. */ | |
130 | goto end_nosignal; | |
131 | } | |
132 | ||
d88aee68 | 133 | DBG("consumer_add_channel %" PRIu64, msg.u.channel.channel_key); |
3bd1e081 | 134 | new_channel = consumer_allocate_channel(msg.u.channel.channel_key, |
ffe60014 DG |
135 | msg.u.channel.session_id, msg.u.channel.pathname, |
136 | msg.u.channel.name, msg.u.channel.uid, msg.u.channel.gid, | |
1624d5b7 JD |
137 | msg.u.channel.relayd_id, msg.u.channel.output, |
138 | msg.u.channel.tracefile_size, | |
139 | msg.u.channel.tracefile_count); | |
3bd1e081 | 140 | if (new_channel == NULL) { |
f73fabfd | 141 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR); |
3bd1e081 MD |
142 | goto end_nosignal; |
143 | } | |
ffe60014 DG |
144 | new_channel->nb_init_stream_left = msg.u.channel.nb_init_streams; |
145 | ||
146 | /* Translate and save channel type. */ | |
147 | switch (msg.u.channel.type) { | |
148 | case CONSUMER_CHANNEL_TYPE_DATA: | |
149 | case CONSUMER_CHANNEL_TYPE_METADATA: | |
150 | new_channel->type = msg.u.channel.type; | |
151 | break; | |
152 | default: | |
153 | assert(0); | |
154 | goto end_nosignal; | |
155 | }; | |
156 | ||
3bd1e081 MD |
157 | if (ctx->on_recv_channel != NULL) { |
158 | ret = ctx->on_recv_channel(new_channel); | |
159 | if (ret == 0) { | |
d8ef542d | 160 | consumer_add_channel(new_channel, ctx); |
3bd1e081 MD |
161 | } else if (ret < 0) { |
162 | goto end_nosignal; | |
163 | } | |
164 | } else { | |
d8ef542d | 165 | consumer_add_channel(new_channel, ctx); |
3bd1e081 MD |
166 | } |
167 | goto end_nosignal; | |
168 | } | |
169 | case LTTNG_CONSUMER_ADD_STREAM: | |
170 | { | |
50f8ae69 | 171 | int fd, stream_pipe; |
00e2e675 DG |
172 | struct consumer_relayd_sock_pair *relayd = NULL; |
173 | struct lttng_consumer_stream *new_stream; | |
ffe60014 | 174 | struct lttng_consumer_channel *channel; |
c80048c6 | 175 | int alloc_ret = 0; |
3bd1e081 | 176 | |
ffe60014 DG |
177 | /* |
178 | * Get stream's channel reference. Needed when adding the stream to the | |
179 | * global hash table. | |
180 | */ | |
181 | channel = consumer_find_channel(msg.u.stream.channel_key); | |
182 | if (!channel) { | |
183 | /* | |
184 | * We could not find the channel. Can happen if cpu hotplug | |
185 | * happens while tearing down. | |
186 | */ | |
d88aee68 | 187 | ERR("Unable to find channel key %" PRIu64, msg.u.stream.channel_key); |
ffe60014 DG |
188 | ret_code = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
189 | } | |
190 | ||
f50f23d9 DG |
191 | /* First send a status message before receiving the fds. */ |
192 | ret = consumer_send_status_msg(sock, ret_code); | |
ffe60014 DG |
193 | if (ret < 0 || ret_code != LTTNG_OK) { |
194 | /* | |
195 | * Somehow, the session daemon is not responding anymore or the | |
196 | * channel was not found. | |
197 | */ | |
f50f23d9 DG |
198 | goto end_nosignal; |
199 | } | |
200 | ||
3bd1e081 MD |
201 | /* block */ |
202 | if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) { | |
3f8e211f | 203 | rcu_read_unlock(); |
3bd1e081 MD |
204 | return -EINTR; |
205 | } | |
00e2e675 DG |
206 | |
207 | /* Get stream file descriptor from socket */ | |
f2fc6720 MD |
208 | ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1); |
209 | if (ret != sizeof(fd)) { | |
f73fabfd | 210 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD); |
3f8e211f | 211 | rcu_read_unlock(); |
3bd1e081 MD |
212 | return ret; |
213 | } | |
3bd1e081 | 214 | |
f50f23d9 DG |
215 | /* |
216 | * Send status code to session daemon only if the recv works. If the | |
217 | * above recv() failed, the session daemon is notified through the | |
218 | * error socket and the teardown is eventually done. | |
219 | */ | |
220 | ret = consumer_send_status_msg(sock, ret_code); | |
221 | if (ret < 0) { | |
222 | /* Somehow, the session daemon is not responding anymore. */ | |
223 | goto end_nosignal; | |
224 | } | |
225 | ||
ffe60014 DG |
226 | new_stream = consumer_allocate_stream(channel->key, |
227 | fd, | |
228 | LTTNG_CONSUMER_ACTIVE_STREAM, | |
229 | channel->name, | |
230 | channel->uid, | |
231 | channel->gid, | |
232 | channel->relayd_id, | |
233 | channel->session_id, | |
234 | msg.u.stream.cpu, | |
235 | &alloc_ret, | |
236 | channel->type); | |
3bd1e081 | 237 | if (new_stream == NULL) { |
c80048c6 MD |
238 | switch (alloc_ret) { |
239 | case -ENOMEM: | |
240 | case -EINVAL: | |
241 | default: | |
242 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR); | |
243 | break; | |
c80048c6 | 244 | } |
3f8e211f | 245 | goto end_nosignal; |
3bd1e081 | 246 | } |
ffe60014 DG |
247 | new_stream->chan = channel; |
248 | new_stream->wait_fd = fd; | |
00e2e675 | 249 | |
fb3a43a9 DG |
250 | /* |
251 | * The buffer flush is done on the session daemon side for the kernel | |
252 | * so no need for the stream "hangup_flush_done" variable to be | |
253 | * tracked. This is important for a kernel stream since we don't rely | |
254 | * on the flush state of the stream to read data. It's not the case for | |
255 | * user space tracing. | |
256 | */ | |
257 | new_stream->hangup_flush_done = 0; | |
258 | ||
00e2e675 | 259 | /* The stream is not metadata. Get relayd reference if exists. */ |
ffe60014 | 260 | relayd = consumer_find_relayd(new_stream->net_seq_idx); |
00e2e675 DG |
261 | if (relayd != NULL) { |
262 | /* Add stream on the relayd */ | |
263 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); | |
264 | ret = relayd_add_stream(&relayd->control_sock, | |
ffe60014 | 265 | new_stream->name, new_stream->chan->pathname, |
0f907de1 JD |
266 | &new_stream->relayd_stream_id, |
267 | new_stream->chan->tracefile_size, | |
268 | new_stream->chan->tracefile_count); | |
00e2e675 DG |
269 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
270 | if (ret < 0) { | |
e316aad5 | 271 | consumer_del_stream(new_stream, NULL); |
3f8e211f | 272 | goto end_nosignal; |
00e2e675 | 273 | } |
d88aee68 DG |
274 | } else if (new_stream->net_seq_idx != (uint64_t) -1ULL) { |
275 | ERR("Network sequence index %" PRIu64 " unknown. Not adding stream.", | |
ffe60014 | 276 | new_stream->net_seq_idx); |
e316aad5 | 277 | consumer_del_stream(new_stream, NULL); |
3f8e211f | 278 | goto end_nosignal; |
00e2e675 DG |
279 | } |
280 | ||
633d0084 DG |
281 | if (ctx->on_recv_stream) { |
282 | ret = ctx->on_recv_stream(new_stream); | |
283 | if (ret < 0) { | |
e316aad5 | 284 | consumer_del_stream(new_stream, NULL); |
633d0084 | 285 | goto end_nosignal; |
fb3a43a9 | 286 | } |
633d0084 | 287 | } |
fb3a43a9 | 288 | |
50f8ae69 | 289 | /* Get the right pipe where the stream will be sent. */ |
633d0084 | 290 | if (new_stream->metadata_flag) { |
50f8ae69 | 291 | stream_pipe = ctx->consumer_metadata_pipe[1]; |
3bd1e081 | 292 | } else { |
acdb9057 | 293 | stream_pipe = lttng_pipe_get_writefd(ctx->consumer_data_pipe); |
50f8ae69 DG |
294 | } |
295 | ||
296 | do { | |
297 | ret = write(stream_pipe, &new_stream, sizeof(new_stream)); | |
298 | } while (ret < 0 && errno == EINTR); | |
299 | if (ret < 0) { | |
300 | PERROR("Consumer write %s stream to pipe %d", | |
301 | new_stream->metadata_flag ? "metadata" : "data", | |
302 | stream_pipe); | |
303 | consumer_del_stream(new_stream, NULL); | |
304 | goto end_nosignal; | |
3bd1e081 | 305 | } |
00e2e675 | 306 | |
50f8ae69 | 307 | DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64, |
ffe60014 | 308 | new_stream->name, fd, new_stream->relayd_stream_id); |
3bd1e081 MD |
309 | break; |
310 | } | |
311 | case LTTNG_CONSUMER_UPDATE_STREAM: | |
312 | { | |
3f8e211f DG |
313 | rcu_read_unlock(); |
314 | return -ENOSYS; | |
315 | } | |
316 | case LTTNG_CONSUMER_DESTROY_RELAYD: | |
317 | { | |
a6ba4fe1 | 318 | uint64_t index = msg.u.destroy_relayd.net_seq_idx; |
3f8e211f DG |
319 | struct consumer_relayd_sock_pair *relayd; |
320 | ||
a6ba4fe1 | 321 | DBG("Kernel consumer destroying relayd %" PRIu64, index); |
3f8e211f DG |
322 | |
323 | /* Get relayd reference if exists. */ | |
a6ba4fe1 | 324 | relayd = consumer_find_relayd(index); |
3f8e211f | 325 | if (relayd == NULL) { |
3448e266 | 326 | DBG("Unable to find relayd %" PRIu64, index); |
f50f23d9 | 327 | ret_code = LTTNG_ERR_NO_CONSUMER; |
3bd1e081 | 328 | } |
3f8e211f | 329 | |
a6ba4fe1 DG |
330 | /* |
331 | * Each relayd socket pair has a refcount of stream attached to it | |
332 | * which tells if the relayd is still active or not depending on the | |
333 | * refcount value. | |
334 | * | |
335 | * This will set the destroy flag of the relayd object and destroy it | |
336 | * if the refcount reaches zero when called. | |
337 | * | |
338 | * The destroy can happen either here or when a stream fd hangs up. | |
339 | */ | |
f50f23d9 DG |
340 | if (relayd) { |
341 | consumer_flag_relayd_for_destroy(relayd); | |
342 | } | |
343 | ||
344 | ret = consumer_send_status_msg(sock, ret_code); | |
345 | if (ret < 0) { | |
346 | /* Somehow, the session daemon is not responding anymore. */ | |
347 | goto end_nosignal; | |
348 | } | |
3f8e211f | 349 | |
3f8e211f | 350 | goto end_nosignal; |
3bd1e081 | 351 | } |
6d805429 | 352 | case LTTNG_CONSUMER_DATA_PENDING: |
53632229 | 353 | { |
c8f59ee5 | 354 | int32_t ret; |
6d805429 | 355 | uint64_t id = msg.u.data_pending.session_id; |
c8f59ee5 | 356 | |
6d805429 | 357 | DBG("Kernel consumer data pending command for id %" PRIu64, id); |
c8f59ee5 | 358 | |
6d805429 | 359 | ret = consumer_data_pending(id); |
c8f59ee5 DG |
360 | |
361 | /* Send back returned value to session daemon */ | |
362 | ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret)); | |
363 | if (ret < 0) { | |
6d805429 | 364 | PERROR("send data pending ret code"); |
c8f59ee5 | 365 | } |
f50f23d9 DG |
366 | |
367 | /* | |
368 | * No need to send back a status message since the data pending | |
369 | * returned value is the response. | |
370 | */ | |
c8f59ee5 | 371 | break; |
53632229 | 372 | } |
3bd1e081 | 373 | default: |
3f8e211f | 374 | goto end_nosignal; |
3bd1e081 | 375 | } |
3f8e211f | 376 | |
3bd1e081 | 377 | end_nosignal: |
b0b335c8 | 378 | rcu_read_unlock(); |
4cbc1a04 DG |
379 | |
380 | /* | |
381 | * Return 1 to indicate success since the 0 value can be a socket | |
382 | * shutdown during the recv() or send() call. | |
383 | */ | |
384 | return 1; | |
3bd1e081 | 385 | } |
d41f73b7 MD |
386 | |
387 | /* | |
388 | * Consume data on a file descriptor and write it on a trace file. | |
389 | */ | |
4078b776 | 390 | ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream, |
d41f73b7 MD |
391 | struct lttng_consumer_local_data *ctx) |
392 | { | |
1d4dfdef | 393 | unsigned long len, subbuf_size, padding; |
d41f73b7 | 394 | int err; |
4078b776 | 395 | ssize_t ret = 0; |
d41f73b7 MD |
396 | int infd = stream->wait_fd; |
397 | ||
398 | DBG("In read_subbuffer (infd : %d)", infd); | |
399 | /* Get the next subbuffer */ | |
400 | err = kernctl_get_next_subbuf(infd); | |
401 | if (err != 0) { | |
1d4dfdef | 402 | ret = err; |
d41f73b7 MD |
403 | /* |
404 | * This is a debug message even for single-threaded consumer, | |
405 | * because poll() have more relaxed criterions than get subbuf, | |
406 | * so get_subbuf may fail for short race windows where poll() | |
407 | * would issue wakeups. | |
408 | */ | |
409 | DBG("Reserving sub buffer failed (everything is normal, " | |
410 | "it is due to concurrency)"); | |
411 | goto end; | |
412 | } | |
413 | ||
1d4dfdef DG |
414 | /* Get the full subbuffer size including padding */ |
415 | err = kernctl_get_padded_subbuf_size(infd, &len); | |
416 | if (err != 0) { | |
417 | errno = -err; | |
418 | perror("Getting sub-buffer len failed."); | |
419 | ret = err; | |
420 | goto end; | |
421 | } | |
422 | ||
ffe60014 | 423 | switch (stream->chan->output) { |
1d4dfdef | 424 | case LTTNG_EVENT_SPLICE: |
d41f73b7 | 425 | |
1d4dfdef DG |
426 | /* |
427 | * XXX: The lttng-modules splice "actor" does not handle copying | |
428 | * partial pages hence only using the subbuffer size without the | |
429 | * padding makes the splice fail. | |
430 | */ | |
431 | subbuf_size = len; | |
432 | padding = 0; | |
433 | ||
434 | /* splice the subbuffer to the tracefile */ | |
91dfef6e DG |
435 | ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, subbuf_size, |
436 | padding); | |
437 | /* | |
438 | * XXX: Splice does not support network streaming so the return value | |
439 | * is simply checked against subbuf_size and not like the mmap() op. | |
440 | */ | |
1d4dfdef DG |
441 | if (ret != subbuf_size) { |
442 | /* | |
443 | * display the error but continue processing to try | |
444 | * to release the subbuffer | |
445 | */ | |
446 | ERR("Error splicing to tracefile (ret: %zd != len: %lu)", | |
447 | ret, subbuf_size); | |
448 | } | |
449 | break; | |
450 | case LTTNG_EVENT_MMAP: | |
451 | /* Get subbuffer size without padding */ | |
452 | err = kernctl_get_subbuf_size(infd, &subbuf_size); | |
453 | if (err != 0) { | |
454 | errno = -err; | |
455 | perror("Getting sub-buffer len failed."); | |
456 | ret = err; | |
457 | goto end; | |
458 | } | |
47e81c02 | 459 | |
1d4dfdef DG |
460 | /* Make sure the tracer is not gone mad on us! */ |
461 | assert(len >= subbuf_size); | |
462 | ||
463 | padding = len - subbuf_size; | |
464 | ||
465 | /* write the subbuffer to the tracefile */ | |
91dfef6e DG |
466 | ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size, |
467 | padding); | |
468 | /* | |
469 | * The mmap operation should write subbuf_size amount of data when | |
470 | * network streaming or the full padding (len) size when we are _not_ | |
471 | * streaming. | |
472 | */ | |
d88aee68 DG |
473 | if ((ret != subbuf_size && stream->net_seq_idx != (uint64_t) -1ULL) || |
474 | (ret != len && stream->net_seq_idx == (uint64_t) -1ULL)) { | |
1d4dfdef | 475 | /* |
91dfef6e DG |
476 | * Display the error but continue processing to try to release the |
477 | * subbuffer | |
1d4dfdef | 478 | */ |
91dfef6e DG |
479 | ERR("Error writing to tracefile " |
480 | "(ret: %zd != len: %lu != subbuf_size: %lu)", | |
481 | ret, len, subbuf_size); | |
1d4dfdef DG |
482 | } |
483 | break; | |
484 | default: | |
485 | ERR("Unknown output method"); | |
486 | ret = -1; | |
d41f73b7 MD |
487 | } |
488 | ||
489 | err = kernctl_put_next_subbuf(infd); | |
490 | if (err != 0) { | |
21073eaa | 491 | errno = -err; |
d41f73b7 MD |
492 | if (errno == EFAULT) { |
493 | perror("Error in unreserving sub buffer\n"); | |
494 | } else if (errno == EIO) { | |
495 | /* Should never happen with newer LTTng versions */ | |
496 | perror("Reader has been pushed by the writer, last sub-buffer corrupted."); | |
497 | } | |
21073eaa DG |
498 | |
499 | ret = -err; | |
d41f73b7 MD |
500 | goto end; |
501 | } | |
502 | ||
503 | end: | |
504 | return ret; | |
505 | } | |
506 | ||
507 | int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream) | |
508 | { | |
509 | int ret; | |
ffe60014 DG |
510 | |
511 | assert(stream); | |
512 | ||
fe4477ee JD |
513 | /* Don't create anything if this is set for streaming. */ |
514 | if (stream->net_seq_idx == (uint64_t) -1ULL) { | |
515 | ret = utils_create_stream_file(stream->chan->pathname, stream->name, | |
516 | stream->chan->tracefile_size, stream->tracefile_count_current, | |
517 | stream->uid, stream->gid); | |
518 | if (ret < 0) { | |
519 | goto error; | |
520 | } | |
521 | stream->out_fd = ret; | |
522 | stream->tracefile_size_current = 0; | |
ffe60014 | 523 | } |
d41f73b7 | 524 | |
d41f73b7 MD |
525 | if (stream->output == LTTNG_EVENT_MMAP) { |
526 | /* get the len of the mmap region */ | |
527 | unsigned long mmap_len; | |
528 | ||
529 | ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len); | |
530 | if (ret != 0) { | |
87dc6a9c | 531 | errno = -ret; |
ffe60014 | 532 | PERROR("kernctl_get_mmap_len"); |
d41f73b7 MD |
533 | goto error_close_fd; |
534 | } | |
535 | stream->mmap_len = (size_t) mmap_len; | |
536 | ||
ffe60014 DG |
537 | stream->mmap_base = mmap(NULL, stream->mmap_len, PROT_READ, |
538 | MAP_PRIVATE, stream->wait_fd, 0); | |
d41f73b7 | 539 | if (stream->mmap_base == MAP_FAILED) { |
ffe60014 | 540 | PERROR("Error mmaping"); |
d41f73b7 MD |
541 | ret = -1; |
542 | goto error_close_fd; | |
543 | } | |
544 | } | |
545 | ||
546 | /* we return 0 to let the library handle the FD internally */ | |
547 | return 0; | |
548 | ||
549 | error_close_fd: | |
550 | { | |
551 | int err; | |
552 | ||
553 | err = close(stream->out_fd); | |
554 | assert(!err); | |
555 | } | |
556 | error: | |
557 | return ret; | |
558 | } | |
559 | ||
ca22feea DG |
560 | /* |
561 | * Check if data is still being extracted from the buffers for a specific | |
4e9a4686 DG |
562 | * stream. Consumer data lock MUST be acquired before calling this function |
563 | * and the stream lock. | |
ca22feea | 564 | * |
6d805429 | 565 | * Return 1 if the traced data are still getting read else 0 meaning that the |
ca22feea DG |
566 | * data is available for trace viewer reading. |
567 | */ | |
6d805429 | 568 | int lttng_kconsumer_data_pending(struct lttng_consumer_stream *stream) |
ca22feea DG |
569 | { |
570 | int ret; | |
571 | ||
572 | assert(stream); | |
573 | ||
ca22feea DG |
574 | ret = kernctl_get_next_subbuf(stream->wait_fd); |
575 | if (ret == 0) { | |
576 | /* There is still data so let's put back this subbuffer. */ | |
577 | ret = kernctl_put_subbuf(stream->wait_fd); | |
578 | assert(ret == 0); | |
6d805429 | 579 | ret = 1; /* Data is pending */ |
4e9a4686 | 580 | goto end; |
ca22feea DG |
581 | } |
582 | ||
6d805429 DG |
583 | /* Data is NOT pending and ready to be read. */ |
584 | ret = 0; | |
ca22feea | 585 | |
6efae65e DG |
586 | end: |
587 | return ret; | |
ca22feea | 588 | } |