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> |
07b86b52 | 40 | #include <common/consumer-stream.h> |
309167d2 | 41 | #include <common/index/index.h> |
0857097f | 42 | |
10a8a223 | 43 | #include "kernel-consumer.h" |
3bd1e081 MD |
44 | |
45 | extern struct lttng_consumer_global_data consumer_data; | |
46 | extern int consumer_poll_timeout; | |
47 | extern volatile int consumer_quit; | |
48 | ||
3bd1e081 MD |
49 | /* |
50 | * Take a snapshot for a specific fd | |
51 | * | |
52 | * Returns 0 on success, < 0 on error | |
53 | */ | |
ffe60014 | 54 | int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream *stream) |
3bd1e081 MD |
55 | { |
56 | int ret = 0; | |
57 | int infd = stream->wait_fd; | |
58 | ||
59 | ret = kernctl_snapshot(infd); | |
60 | if (ret != 0) { | |
3bd1e081 | 61 | perror("Getting sub-buffer snapshot."); |
56591bac | 62 | ret = -errno; |
3bd1e081 MD |
63 | } |
64 | ||
65 | return ret; | |
66 | } | |
67 | ||
68 | /* | |
69 | * Get the produced position | |
70 | * | |
71 | * Returns 0 on success, < 0 on error | |
72 | */ | |
ffe60014 | 73 | int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream *stream, |
3bd1e081 MD |
74 | unsigned long *pos) |
75 | { | |
76 | int ret; | |
77 | int infd = stream->wait_fd; | |
78 | ||
79 | ret = kernctl_snapshot_get_produced(infd, pos); | |
80 | if (ret != 0) { | |
3bd1e081 | 81 | perror("kernctl_snapshot_get_produced"); |
56591bac | 82 | ret = -errno; |
3bd1e081 MD |
83 | } |
84 | ||
85 | return ret; | |
86 | } | |
87 | ||
07b86b52 JD |
88 | /* |
89 | * Get the consumerd position | |
90 | * | |
91 | * Returns 0 on success, < 0 on error | |
92 | */ | |
93 | int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream *stream, | |
94 | unsigned long *pos) | |
95 | { | |
96 | int ret; | |
97 | int infd = stream->wait_fd; | |
98 | ||
99 | ret = kernctl_snapshot_get_consumed(infd, pos); | |
100 | if (ret != 0) { | |
07b86b52 | 101 | perror("kernctl_snapshot_get_consumed"); |
56591bac | 102 | ret = -errno; |
07b86b52 JD |
103 | } |
104 | ||
105 | return ret; | |
106 | } | |
107 | ||
07b86b52 JD |
108 | /* |
109 | * Take a snapshot of all the stream of a channel | |
110 | * | |
111 | * Returns 0 on success, < 0 on error | |
112 | */ | |
113 | int lttng_kconsumer_snapshot_channel(uint64_t key, char *path, | |
5c786ded JD |
114 | uint64_t relayd_id, uint64_t max_stream_size, |
115 | struct lttng_consumer_local_data *ctx) | |
07b86b52 JD |
116 | { |
117 | int ret; | |
118 | unsigned long consumed_pos, produced_pos; | |
119 | struct lttng_consumer_channel *channel; | |
120 | struct lttng_consumer_stream *stream; | |
121 | ||
6a00837f | 122 | DBG("Kernel consumer snapshot channel %" PRIu64, key); |
07b86b52 JD |
123 | |
124 | rcu_read_lock(); | |
125 | ||
126 | channel = consumer_find_channel(key); | |
127 | if (!channel) { | |
6a00837f | 128 | ERR("No channel found for key %" PRIu64, key); |
07b86b52 JD |
129 | ret = -1; |
130 | goto end; | |
131 | } | |
132 | ||
133 | /* Splice is not supported yet for channel snapshot. */ | |
134 | if (channel->output != CONSUMER_CHANNEL_MMAP) { | |
135 | ERR("Unsupported output %d", channel->output); | |
136 | ret = -1; | |
137 | goto end; | |
138 | } | |
139 | ||
10a50311 | 140 | cds_list_for_each_entry(stream, &channel->streams.head, send_node) { |
07b86b52 JD |
141 | /* |
142 | * Lock stream because we are about to change its state. | |
143 | */ | |
144 | pthread_mutex_lock(&stream->lock); | |
145 | ||
29decac3 DG |
146 | /* |
147 | * Assign the received relayd ID so we can use it for streaming. The streams | |
148 | * are not visible to anyone so this is OK to change it. | |
149 | */ | |
07b86b52 JD |
150 | stream->net_seq_idx = relayd_id; |
151 | channel->relayd_id = relayd_id; | |
152 | if (relayd_id != (uint64_t) -1ULL) { | |
10a50311 | 153 | ret = consumer_send_relayd_stream(stream, path); |
07b86b52 JD |
154 | if (ret < 0) { |
155 | ERR("sending stream to relayd"); | |
156 | goto end_unlock; | |
157 | } | |
07b86b52 JD |
158 | } else { |
159 | ret = utils_create_stream_file(path, stream->name, | |
10a50311 JD |
160 | stream->chan->tracefile_size, |
161 | stream->tracefile_count_current, | |
309167d2 | 162 | stream->uid, stream->gid, NULL); |
07b86b52 JD |
163 | if (ret < 0) { |
164 | ERR("utils_create_stream_file"); | |
165 | goto end_unlock; | |
166 | } | |
167 | ||
168 | stream->out_fd = ret; | |
169 | stream->tracefile_size_current = 0; | |
170 | ||
81ea21bf MD |
171 | DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64 ")", |
172 | path, stream->name, stream->key); | |
07b86b52 JD |
173 | } |
174 | ||
175 | ret = kernctl_buffer_flush(stream->wait_fd); | |
176 | if (ret < 0) { | |
10a50311 | 177 | ERR("Failed to flush kernel stream"); |
56591bac | 178 | ret = -errno; |
07b86b52 JD |
179 | goto end_unlock; |
180 | } | |
181 | ||
182 | ret = lttng_kconsumer_take_snapshot(stream); | |
183 | if (ret < 0) { | |
184 | ERR("Taking kernel snapshot"); | |
185 | goto end_unlock; | |
186 | } | |
187 | ||
188 | ret = lttng_kconsumer_get_produced_snapshot(stream, &produced_pos); | |
189 | if (ret < 0) { | |
190 | ERR("Produced kernel snapshot position"); | |
191 | goto end_unlock; | |
192 | } | |
193 | ||
194 | ret = lttng_kconsumer_get_consumed_snapshot(stream, &consumed_pos); | |
195 | if (ret < 0) { | |
196 | ERR("Consumerd kernel snapshot position"); | |
197 | goto end_unlock; | |
198 | } | |
199 | ||
200 | if (stream->max_sb_size == 0) { | |
201 | ret = kernctl_get_max_subbuf_size(stream->wait_fd, | |
202 | &stream->max_sb_size); | |
203 | if (ret < 0) { | |
204 | ERR("Getting kernel max_sb_size"); | |
56591bac | 205 | ret = -errno; |
07b86b52 JD |
206 | goto end_unlock; |
207 | } | |
208 | } | |
209 | ||
5c786ded JD |
210 | /* |
211 | * The original value is sent back if max stream size is larger than | |
212 | * the possible size of the snapshot. Also, we asume that the session | |
213 | * daemon should never send a maximum stream size that is lower than | |
214 | * subbuffer size. | |
215 | */ | |
216 | consumed_pos = consumer_get_consumed_maxsize(consumed_pos, | |
217 | produced_pos, max_stream_size); | |
218 | ||
07b86b52 JD |
219 | while (consumed_pos < produced_pos) { |
220 | ssize_t read_len; | |
221 | unsigned long len, padded_len; | |
222 | ||
223 | DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos); | |
224 | ||
225 | ret = kernctl_get_subbuf(stream->wait_fd, &consumed_pos); | |
226 | if (ret < 0) { | |
227 | if (errno != EAGAIN) { | |
228 | PERROR("kernctl_get_subbuf snapshot"); | |
56591bac | 229 | ret = -errno; |
07b86b52 JD |
230 | goto end_unlock; |
231 | } | |
232 | DBG("Kernel consumer get subbuf failed. Skipping it."); | |
233 | consumed_pos += stream->max_sb_size; | |
234 | continue; | |
235 | } | |
236 | ||
237 | ret = kernctl_get_subbuf_size(stream->wait_fd, &len); | |
238 | if (ret < 0) { | |
239 | ERR("Snapshot kernctl_get_subbuf_size"); | |
56591bac | 240 | ret = -errno; |
29decac3 | 241 | goto error_put_subbuf; |
07b86b52 JD |
242 | } |
243 | ||
244 | ret = kernctl_get_padded_subbuf_size(stream->wait_fd, &padded_len); | |
245 | if (ret < 0) { | |
246 | ERR("Snapshot kernctl_get_padded_subbuf_size"); | |
56591bac | 247 | ret = -errno; |
29decac3 | 248 | goto error_put_subbuf; |
07b86b52 JD |
249 | } |
250 | ||
251 | read_len = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len, | |
309167d2 | 252 | padded_len - len, NULL); |
07b86b52 | 253 | /* |
29decac3 DG |
254 | * We write the padded len in local tracefiles but the data len |
255 | * when using a relay. Display the error but continue processing | |
256 | * to try to release the subbuffer. | |
07b86b52 JD |
257 | */ |
258 | if (relayd_id != (uint64_t) -1ULL) { | |
259 | if (read_len != len) { | |
260 | ERR("Error sending to the relay (ret: %zd != len: %lu)", | |
261 | read_len, len); | |
262 | } | |
263 | } else { | |
264 | if (read_len != padded_len) { | |
265 | ERR("Error writing to tracefile (ret: %zd != len: %lu)", | |
266 | read_len, padded_len); | |
267 | } | |
268 | } | |
269 | ||
270 | ret = kernctl_put_subbuf(stream->wait_fd); | |
271 | if (ret < 0) { | |
272 | ERR("Snapshot kernctl_put_subbuf"); | |
56591bac | 273 | ret = -errno; |
07b86b52 JD |
274 | goto end_unlock; |
275 | } | |
276 | consumed_pos += stream->max_sb_size; | |
277 | } | |
278 | ||
279 | if (relayd_id == (uint64_t) -1ULL) { | |
fdf9986c MD |
280 | if (stream->out_fd >= 0) { |
281 | ret = close(stream->out_fd); | |
282 | if (ret < 0) { | |
283 | PERROR("Kernel consumer snapshot close out_fd"); | |
284 | goto end_unlock; | |
285 | } | |
286 | stream->out_fd = -1; | |
07b86b52 | 287 | } |
07b86b52 JD |
288 | } else { |
289 | close_relayd_stream(stream); | |
290 | stream->net_seq_idx = (uint64_t) -1ULL; | |
291 | } | |
292 | pthread_mutex_unlock(&stream->lock); | |
293 | } | |
294 | ||
295 | /* All good! */ | |
296 | ret = 0; | |
297 | goto end; | |
298 | ||
29decac3 DG |
299 | error_put_subbuf: |
300 | ret = kernctl_put_subbuf(stream->wait_fd); | |
301 | if (ret < 0) { | |
56591bac | 302 | ret = -errno; |
29decac3 DG |
303 | ERR("Snapshot kernctl_put_subbuf error path"); |
304 | } | |
07b86b52 JD |
305 | end_unlock: |
306 | pthread_mutex_unlock(&stream->lock); | |
307 | end: | |
308 | rcu_read_unlock(); | |
309 | return ret; | |
310 | } | |
311 | ||
312 | /* | |
313 | * Read the whole metadata available for a snapshot. | |
314 | * | |
315 | * Returns 0 on success, < 0 on error | |
316 | */ | |
317 | int lttng_kconsumer_snapshot_metadata(uint64_t key, char *path, | |
e2039c7a | 318 | uint64_t relayd_id, struct lttng_consumer_local_data *ctx) |
07b86b52 | 319 | { |
d771f832 DG |
320 | int ret, use_relayd = 0; |
321 | ssize_t ret_read; | |
07b86b52 JD |
322 | struct lttng_consumer_channel *metadata_channel; |
323 | struct lttng_consumer_stream *metadata_stream; | |
d771f832 DG |
324 | |
325 | assert(ctx); | |
07b86b52 JD |
326 | |
327 | DBG("Kernel consumer snapshot metadata with key %" PRIu64 " at path %s", | |
328 | key, path); | |
329 | ||
330 | rcu_read_lock(); | |
331 | ||
332 | metadata_channel = consumer_find_channel(key); | |
333 | if (!metadata_channel) { | |
d771f832 | 334 | ERR("Kernel snapshot metadata not found for key %" PRIu64, key); |
07b86b52 | 335 | ret = -1; |
d771f832 | 336 | goto error; |
07b86b52 JD |
337 | } |
338 | ||
339 | metadata_stream = metadata_channel->metadata_stream; | |
340 | assert(metadata_stream); | |
341 | ||
d771f832 | 342 | /* Flag once that we have a valid relayd for the stream. */ |
e2039c7a | 343 | if (relayd_id != (uint64_t) -1ULL) { |
d771f832 DG |
344 | use_relayd = 1; |
345 | } | |
346 | ||
347 | if (use_relayd) { | |
10a50311 | 348 | ret = consumer_send_relayd_stream(metadata_stream, path); |
e2039c7a | 349 | if (ret < 0) { |
d771f832 | 350 | goto error; |
e2039c7a | 351 | } |
e2039c7a JD |
352 | } else { |
353 | ret = utils_create_stream_file(path, metadata_stream->name, | |
354 | metadata_stream->chan->tracefile_size, | |
355 | metadata_stream->tracefile_count_current, | |
309167d2 | 356 | metadata_stream->uid, metadata_stream->gid, NULL); |
e2039c7a | 357 | if (ret < 0) { |
d771f832 | 358 | goto error; |
e2039c7a JD |
359 | } |
360 | metadata_stream->out_fd = ret; | |
07b86b52 | 361 | } |
07b86b52 | 362 | |
d771f832 DG |
363 | do { |
364 | ret_read = lttng_kconsumer_read_subbuffer(metadata_stream, ctx); | |
365 | if (ret_read < 0) { | |
56591bac | 366 | if (ret_read != -EAGAIN) { |
6a00837f | 367 | ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)", |
d771f832 DG |
368 | ret_read); |
369 | goto error; | |
07b86b52 | 370 | } |
d771f832 | 371 | /* ret_read is negative at this point so we will exit the loop. */ |
07b86b52 JD |
372 | continue; |
373 | } | |
d771f832 | 374 | } while (ret_read >= 0); |
07b86b52 | 375 | |
d771f832 DG |
376 | if (use_relayd) { |
377 | close_relayd_stream(metadata_stream); | |
378 | metadata_stream->net_seq_idx = (uint64_t) -1ULL; | |
379 | } else { | |
fdf9986c MD |
380 | if (metadata_stream->out_fd >= 0) { |
381 | ret = close(metadata_stream->out_fd); | |
382 | if (ret < 0) { | |
383 | PERROR("Kernel consumer snapshot metadata close out_fd"); | |
384 | /* | |
385 | * Don't go on error here since the snapshot was successful at this | |
386 | * point but somehow the close failed. | |
387 | */ | |
388 | } | |
389 | metadata_stream->out_fd = -1; | |
e2039c7a | 390 | } |
e2039c7a JD |
391 | } |
392 | ||
07b86b52 | 393 | ret = 0; |
d771f832 | 394 | |
cf53a8a6 JD |
395 | cds_list_del(&metadata_stream->send_node); |
396 | consumer_stream_destroy(metadata_stream, NULL); | |
397 | metadata_channel->metadata_stream = NULL; | |
d771f832 | 398 | error: |
07b86b52 JD |
399 | rcu_read_unlock(); |
400 | return ret; | |
401 | } | |
402 | ||
1803a064 MD |
403 | /* |
404 | * Receive command from session daemon and process it. | |
405 | * | |
406 | * Return 1 on success else a negative value or 0. | |
407 | */ | |
3bd1e081 MD |
408 | int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx, |
409 | int sock, struct pollfd *consumer_sockpoll) | |
410 | { | |
411 | ssize_t ret; | |
f50f23d9 | 412 | enum lttng_error_code ret_code = LTTNG_OK; |
3bd1e081 MD |
413 | struct lttcomm_consumer_msg msg; |
414 | ||
415 | ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg)); | |
416 | if (ret != sizeof(msg)) { | |
1803a064 | 417 | if (ret > 0) { |
c6857fcf | 418 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD); |
1803a064 MD |
419 | ret = -1; |
420 | } | |
3bd1e081 MD |
421 | return ret; |
422 | } | |
423 | if (msg.cmd_type == LTTNG_CONSUMER_STOP) { | |
f50f23d9 DG |
424 | /* |
425 | * Notify the session daemon that the command is completed. | |
426 | * | |
427 | * On transport layer error, the function call will print an error | |
428 | * message so handling the returned code is a bit useless since we | |
429 | * return an error code anyway. | |
430 | */ | |
431 | (void) consumer_send_status_msg(sock, ret_code); | |
3bd1e081 MD |
432 | return -ENOENT; |
433 | } | |
434 | ||
b0b335c8 MD |
435 | /* relayd needs RCU read-side protection */ |
436 | rcu_read_lock(); | |
437 | ||
3bd1e081 | 438 | switch (msg.cmd_type) { |
00e2e675 DG |
439 | case LTTNG_CONSUMER_ADD_RELAYD_SOCKET: |
440 | { | |
f50f23d9 | 441 | /* Session daemon status message are handled in the following call. */ |
7735ef9e DG |
442 | ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index, |
443 | msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll, | |
46e6455f | 444 | &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id); |
00e2e675 DG |
445 | goto end_nosignal; |
446 | } | |
3bd1e081 MD |
447 | case LTTNG_CONSUMER_ADD_CHANNEL: |
448 | { | |
449 | struct lttng_consumer_channel *new_channel; | |
e43c41c5 | 450 | int ret_recv; |
3bd1e081 | 451 | |
f50f23d9 DG |
452 | /* First send a status message before receiving the fds. */ |
453 | ret = consumer_send_status_msg(sock, ret_code); | |
454 | if (ret < 0) { | |
455 | /* Somehow, the session daemon is not responding anymore. */ | |
1803a064 | 456 | goto error_fatal; |
f50f23d9 | 457 | } |
d88aee68 | 458 | DBG("consumer_add_channel %" PRIu64, msg.u.channel.channel_key); |
3bd1e081 | 459 | new_channel = consumer_allocate_channel(msg.u.channel.channel_key, |
ffe60014 DG |
460 | msg.u.channel.session_id, msg.u.channel.pathname, |
461 | msg.u.channel.name, msg.u.channel.uid, msg.u.channel.gid, | |
1624d5b7 JD |
462 | msg.u.channel.relayd_id, msg.u.channel.output, |
463 | msg.u.channel.tracefile_size, | |
1950109e | 464 | msg.u.channel.tracefile_count, 0, |
ecc48a90 JD |
465 | msg.u.channel.monitor, |
466 | msg.u.channel.live_timer_interval); | |
3bd1e081 | 467 | if (new_channel == NULL) { |
f73fabfd | 468 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR); |
3bd1e081 MD |
469 | goto end_nosignal; |
470 | } | |
ffe60014 | 471 | new_channel->nb_init_stream_left = msg.u.channel.nb_init_streams; |
95a1109b JD |
472 | switch (msg.u.channel.output) { |
473 | case LTTNG_EVENT_SPLICE: | |
474 | new_channel->output = CONSUMER_CHANNEL_SPLICE; | |
475 | break; | |
476 | case LTTNG_EVENT_MMAP: | |
477 | new_channel->output = CONSUMER_CHANNEL_MMAP; | |
478 | break; | |
479 | default: | |
480 | ERR("Channel output unknown %d", msg.u.channel.output); | |
481 | goto end_nosignal; | |
482 | } | |
ffe60014 DG |
483 | |
484 | /* Translate and save channel type. */ | |
485 | switch (msg.u.channel.type) { | |
486 | case CONSUMER_CHANNEL_TYPE_DATA: | |
487 | case CONSUMER_CHANNEL_TYPE_METADATA: | |
488 | new_channel->type = msg.u.channel.type; | |
489 | break; | |
490 | default: | |
491 | assert(0); | |
492 | goto end_nosignal; | |
493 | }; | |
494 | ||
3bd1e081 | 495 | if (ctx->on_recv_channel != NULL) { |
e43c41c5 JD |
496 | ret_recv = ctx->on_recv_channel(new_channel); |
497 | if (ret_recv == 0) { | |
498 | ret = consumer_add_channel(new_channel, ctx); | |
499 | } else if (ret_recv < 0) { | |
3bd1e081 MD |
500 | goto end_nosignal; |
501 | } | |
502 | } else { | |
e43c41c5 | 503 | ret = consumer_add_channel(new_channel, ctx); |
3bd1e081 | 504 | } |
e43c41c5 JD |
505 | |
506 | /* If we received an error in add_channel, we need to report it. */ | |
821fffb2 | 507 | if (ret < 0) { |
1803a064 MD |
508 | ret = consumer_send_status_msg(sock, ret); |
509 | if (ret < 0) { | |
510 | goto error_fatal; | |
511 | } | |
e43c41c5 JD |
512 | goto end_nosignal; |
513 | } | |
514 | ||
3bd1e081 MD |
515 | goto end_nosignal; |
516 | } | |
517 | case LTTNG_CONSUMER_ADD_STREAM: | |
518 | { | |
dae10966 DG |
519 | int fd; |
520 | struct lttng_pipe *stream_pipe; | |
00e2e675 | 521 | struct lttng_consumer_stream *new_stream; |
ffe60014 | 522 | struct lttng_consumer_channel *channel; |
c80048c6 | 523 | int alloc_ret = 0; |
3bd1e081 | 524 | |
ffe60014 DG |
525 | /* |
526 | * Get stream's channel reference. Needed when adding the stream to the | |
527 | * global hash table. | |
528 | */ | |
529 | channel = consumer_find_channel(msg.u.stream.channel_key); | |
530 | if (!channel) { | |
531 | /* | |
532 | * We could not find the channel. Can happen if cpu hotplug | |
533 | * happens while tearing down. | |
534 | */ | |
d88aee68 | 535 | ERR("Unable to find channel key %" PRIu64, msg.u.stream.channel_key); |
ffe60014 DG |
536 | ret_code = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
537 | } | |
538 | ||
f50f23d9 DG |
539 | /* First send a status message before receiving the fds. */ |
540 | ret = consumer_send_status_msg(sock, ret_code); | |
1803a064 | 541 | if (ret < 0) { |
d771f832 | 542 | /* Somehow, the session daemon is not responding anymore. */ |
1803a064 MD |
543 | goto error_fatal; |
544 | } | |
545 | if (ret_code != LTTNG_OK) { | |
d771f832 | 546 | /* Channel was not found. */ |
f50f23d9 DG |
547 | goto end_nosignal; |
548 | } | |
549 | ||
d771f832 | 550 | /* Blocking call */ |
3bd1e081 | 551 | if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) { |
3f8e211f | 552 | rcu_read_unlock(); |
3bd1e081 MD |
553 | return -EINTR; |
554 | } | |
00e2e675 DG |
555 | |
556 | /* Get stream file descriptor from socket */ | |
f2fc6720 MD |
557 | ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1); |
558 | if (ret != sizeof(fd)) { | |
f73fabfd | 559 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD); |
3f8e211f | 560 | rcu_read_unlock(); |
3bd1e081 MD |
561 | return ret; |
562 | } | |
3bd1e081 | 563 | |
f50f23d9 DG |
564 | /* |
565 | * Send status code to session daemon only if the recv works. If the | |
566 | * above recv() failed, the session daemon is notified through the | |
567 | * error socket and the teardown is eventually done. | |
568 | */ | |
569 | ret = consumer_send_status_msg(sock, ret_code); | |
570 | if (ret < 0) { | |
571 | /* Somehow, the session daemon is not responding anymore. */ | |
572 | goto end_nosignal; | |
573 | } | |
574 | ||
ffe60014 DG |
575 | new_stream = consumer_allocate_stream(channel->key, |
576 | fd, | |
577 | LTTNG_CONSUMER_ACTIVE_STREAM, | |
578 | channel->name, | |
579 | channel->uid, | |
580 | channel->gid, | |
581 | channel->relayd_id, | |
582 | channel->session_id, | |
583 | msg.u.stream.cpu, | |
584 | &alloc_ret, | |
4891ece8 DG |
585 | channel->type, |
586 | channel->monitor); | |
3bd1e081 | 587 | if (new_stream == NULL) { |
c80048c6 MD |
588 | switch (alloc_ret) { |
589 | case -ENOMEM: | |
590 | case -EINVAL: | |
591 | default: | |
592 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR); | |
593 | break; | |
c80048c6 | 594 | } |
3f8e211f | 595 | goto end_nosignal; |
3bd1e081 | 596 | } |
d771f832 | 597 | |
ffe60014 DG |
598 | new_stream->chan = channel; |
599 | new_stream->wait_fd = fd; | |
07b86b52 JD |
600 | switch (channel->output) { |
601 | case CONSUMER_CHANNEL_SPLICE: | |
602 | new_stream->output = LTTNG_EVENT_SPLICE; | |
603 | break; | |
604 | case CONSUMER_CHANNEL_MMAP: | |
605 | new_stream->output = LTTNG_EVENT_MMAP; | |
606 | break; | |
607 | default: | |
608 | ERR("Stream output unknown %d", channel->output); | |
609 | goto end_nosignal; | |
610 | } | |
00e2e675 | 611 | |
a0c83db9 DG |
612 | /* |
613 | * We've just assigned the channel to the stream so increment the | |
07b86b52 JD |
614 | * refcount right now. We don't need to increment the refcount for |
615 | * streams in no monitor because we handle manually the cleanup of | |
616 | * those. It is very important to make sure there is NO prior | |
617 | * consumer_del_stream() calls or else the refcount will be unbalanced. | |
a0c83db9 | 618 | */ |
07b86b52 JD |
619 | if (channel->monitor) { |
620 | uatomic_inc(&new_stream->chan->refcount); | |
621 | } | |
9d9353f9 | 622 | |
fb3a43a9 DG |
623 | /* |
624 | * The buffer flush is done on the session daemon side for the kernel | |
625 | * so no need for the stream "hangup_flush_done" variable to be | |
626 | * tracked. This is important for a kernel stream since we don't rely | |
627 | * on the flush state of the stream to read data. It's not the case for | |
628 | * user space tracing. | |
629 | */ | |
630 | new_stream->hangup_flush_done = 0; | |
631 | ||
633d0084 DG |
632 | if (ctx->on_recv_stream) { |
633 | ret = ctx->on_recv_stream(new_stream); | |
634 | if (ret < 0) { | |
d771f832 | 635 | consumer_stream_free(new_stream); |
633d0084 | 636 | goto end_nosignal; |
fb3a43a9 | 637 | } |
633d0084 | 638 | } |
fb3a43a9 | 639 | |
07b86b52 JD |
640 | if (new_stream->metadata_flag) { |
641 | channel->metadata_stream = new_stream; | |
642 | } | |
643 | ||
2bba9e53 DG |
644 | /* Do not monitor this stream. */ |
645 | if (!channel->monitor) { | |
5eecee74 | 646 | DBG("Kernel consumer add stream %s in no monitor mode with " |
6dc3064a | 647 | "relayd id %" PRIu64, new_stream->name, |
5eecee74 | 648 | new_stream->net_seq_idx); |
10a50311 | 649 | cds_list_add(&new_stream->send_node, &channel->streams.head); |
6dc3064a DG |
650 | break; |
651 | } | |
652 | ||
e1b71bdc DG |
653 | /* Send stream to relayd if the stream has an ID. */ |
654 | if (new_stream->net_seq_idx != (uint64_t) -1ULL) { | |
194ee077 DG |
655 | ret = consumer_send_relayd_stream(new_stream, |
656 | new_stream->chan->pathname); | |
e1b71bdc DG |
657 | if (ret < 0) { |
658 | consumer_stream_free(new_stream); | |
659 | goto end_nosignal; | |
660 | } | |
e2039c7a JD |
661 | } |
662 | ||
50f8ae69 | 663 | /* Get the right pipe where the stream will be sent. */ |
633d0084 | 664 | if (new_stream->metadata_flag) { |
5ab66908 MD |
665 | ret = consumer_add_metadata_stream(new_stream); |
666 | if (ret) { | |
667 | ERR("Consumer add metadata stream %" PRIu64 " failed. Continuing", | |
668 | new_stream->key); | |
669 | consumer_stream_free(new_stream); | |
670 | goto end_nosignal; | |
671 | } | |
dae10966 | 672 | stream_pipe = ctx->consumer_metadata_pipe; |
3bd1e081 | 673 | } else { |
5ab66908 MD |
674 | ret = consumer_add_data_stream(new_stream); |
675 | if (ret) { | |
676 | ERR("Consumer add stream %" PRIu64 " failed. Continuing", | |
677 | new_stream->key); | |
678 | consumer_stream_free(new_stream); | |
679 | goto end_nosignal; | |
680 | } | |
dae10966 | 681 | stream_pipe = ctx->consumer_data_pipe; |
50f8ae69 DG |
682 | } |
683 | ||
5ab66908 MD |
684 | /* Vitible to other threads */ |
685 | new_stream->globally_visible = 1; | |
686 | ||
dae10966 | 687 | ret = lttng_pipe_write(stream_pipe, &new_stream, sizeof(new_stream)); |
50f8ae69 | 688 | if (ret < 0) { |
dae10966 | 689 | ERR("Consumer write %s stream to pipe %d", |
50f8ae69 | 690 | new_stream->metadata_flag ? "metadata" : "data", |
dae10966 | 691 | lttng_pipe_get_writefd(stream_pipe)); |
5ab66908 MD |
692 | if (new_stream->metadata_flag) { |
693 | consumer_del_stream_for_metadata(new_stream); | |
694 | } else { | |
695 | consumer_del_stream_for_data(new_stream); | |
696 | } | |
50f8ae69 | 697 | goto end_nosignal; |
3bd1e081 | 698 | } |
00e2e675 | 699 | |
50f8ae69 | 700 | DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64, |
ffe60014 | 701 | new_stream->name, fd, new_stream->relayd_stream_id); |
3bd1e081 MD |
702 | break; |
703 | } | |
704 | case LTTNG_CONSUMER_UPDATE_STREAM: | |
705 | { | |
3f8e211f DG |
706 | rcu_read_unlock(); |
707 | return -ENOSYS; | |
708 | } | |
709 | case LTTNG_CONSUMER_DESTROY_RELAYD: | |
710 | { | |
a6ba4fe1 | 711 | uint64_t index = msg.u.destroy_relayd.net_seq_idx; |
3f8e211f DG |
712 | struct consumer_relayd_sock_pair *relayd; |
713 | ||
a6ba4fe1 | 714 | DBG("Kernel consumer destroying relayd %" PRIu64, index); |
3f8e211f DG |
715 | |
716 | /* Get relayd reference if exists. */ | |
a6ba4fe1 | 717 | relayd = consumer_find_relayd(index); |
3f8e211f | 718 | if (relayd == NULL) { |
3448e266 | 719 | DBG("Unable to find relayd %" PRIu64, index); |
f50f23d9 | 720 | ret_code = LTTNG_ERR_NO_CONSUMER; |
3bd1e081 | 721 | } |
3f8e211f | 722 | |
a6ba4fe1 DG |
723 | /* |
724 | * Each relayd socket pair has a refcount of stream attached to it | |
725 | * which tells if the relayd is still active or not depending on the | |
726 | * refcount value. | |
727 | * | |
728 | * This will set the destroy flag of the relayd object and destroy it | |
729 | * if the refcount reaches zero when called. | |
730 | * | |
731 | * The destroy can happen either here or when a stream fd hangs up. | |
732 | */ | |
f50f23d9 DG |
733 | if (relayd) { |
734 | consumer_flag_relayd_for_destroy(relayd); | |
735 | } | |
736 | ||
737 | ret = consumer_send_status_msg(sock, ret_code); | |
738 | if (ret < 0) { | |
739 | /* Somehow, the session daemon is not responding anymore. */ | |
1803a064 | 740 | goto error_fatal; |
f50f23d9 | 741 | } |
3f8e211f | 742 | |
3f8e211f | 743 | goto end_nosignal; |
3bd1e081 | 744 | } |
6d805429 | 745 | case LTTNG_CONSUMER_DATA_PENDING: |
53632229 | 746 | { |
c8f59ee5 | 747 | int32_t ret; |
6d805429 | 748 | uint64_t id = msg.u.data_pending.session_id; |
c8f59ee5 | 749 | |
6d805429 | 750 | DBG("Kernel consumer data pending command for id %" PRIu64, id); |
c8f59ee5 | 751 | |
6d805429 | 752 | ret = consumer_data_pending(id); |
c8f59ee5 DG |
753 | |
754 | /* Send back returned value to session daemon */ | |
755 | ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret)); | |
756 | if (ret < 0) { | |
6d805429 | 757 | PERROR("send data pending ret code"); |
1803a064 | 758 | goto error_fatal; |
c8f59ee5 | 759 | } |
f50f23d9 DG |
760 | |
761 | /* | |
762 | * No need to send back a status message since the data pending | |
763 | * returned value is the response. | |
764 | */ | |
c8f59ee5 | 765 | break; |
53632229 | 766 | } |
6dc3064a DG |
767 | case LTTNG_CONSUMER_SNAPSHOT_CHANNEL: |
768 | { | |
07b86b52 JD |
769 | if (msg.u.snapshot_channel.metadata == 1) { |
770 | ret = lttng_kconsumer_snapshot_metadata(msg.u.snapshot_channel.key, | |
e2039c7a JD |
771 | msg.u.snapshot_channel.pathname, |
772 | msg.u.snapshot_channel.relayd_id, ctx); | |
07b86b52 JD |
773 | if (ret < 0) { |
774 | ERR("Snapshot metadata failed"); | |
775 | ret_code = LTTNG_ERR_KERN_META_FAIL; | |
776 | } | |
777 | } else { | |
778 | ret = lttng_kconsumer_snapshot_channel(msg.u.snapshot_channel.key, | |
779 | msg.u.snapshot_channel.pathname, | |
5c786ded JD |
780 | msg.u.snapshot_channel.relayd_id, |
781 | msg.u.snapshot_channel.max_stream_size, | |
782 | ctx); | |
07b86b52 JD |
783 | if (ret < 0) { |
784 | ERR("Snapshot channel failed"); | |
785 | ret_code = LTTNG_ERR_KERN_CHAN_FAIL; | |
786 | } | |
787 | } | |
788 | ||
6dc3064a DG |
789 | ret = consumer_send_status_msg(sock, ret_code); |
790 | if (ret < 0) { | |
791 | /* Somehow, the session daemon is not responding anymore. */ | |
792 | goto end_nosignal; | |
793 | } | |
794 | break; | |
795 | } | |
07b86b52 JD |
796 | case LTTNG_CONSUMER_DESTROY_CHANNEL: |
797 | { | |
798 | uint64_t key = msg.u.destroy_channel.key; | |
799 | struct lttng_consumer_channel *channel; | |
800 | ||
801 | channel = consumer_find_channel(key); | |
802 | if (!channel) { | |
803 | ERR("Kernel consumer destroy channel %" PRIu64 " not found", key); | |
804 | ret_code = LTTNG_ERR_KERN_CHAN_NOT_FOUND; | |
805 | } | |
806 | ||
807 | ret = consumer_send_status_msg(sock, ret_code); | |
808 | if (ret < 0) { | |
809 | /* Somehow, the session daemon is not responding anymore. */ | |
810 | goto end_nosignal; | |
811 | } | |
812 | ||
813 | /* | |
814 | * This command should ONLY be issued for channel with streams set in | |
815 | * no monitor mode. | |
816 | */ | |
817 | assert(!channel->monitor); | |
818 | ||
819 | /* | |
820 | * The refcount should ALWAYS be 0 in the case of a channel in no | |
821 | * monitor mode. | |
822 | */ | |
823 | assert(!uatomic_sub_return(&channel->refcount, 1)); | |
824 | ||
825 | consumer_del_channel(channel); | |
826 | ||
827 | goto end_nosignal; | |
828 | } | |
3bd1e081 | 829 | default: |
3f8e211f | 830 | goto end_nosignal; |
3bd1e081 | 831 | } |
3f8e211f | 832 | |
3bd1e081 | 833 | end_nosignal: |
b0b335c8 | 834 | rcu_read_unlock(); |
4cbc1a04 DG |
835 | |
836 | /* | |
837 | * Return 1 to indicate success since the 0 value can be a socket | |
838 | * shutdown during the recv() or send() call. | |
839 | */ | |
840 | return 1; | |
1803a064 MD |
841 | |
842 | error_fatal: | |
843 | rcu_read_unlock(); | |
844 | /* This will issue a consumer stop. */ | |
845 | return -1; | |
3bd1e081 | 846 | } |
d41f73b7 | 847 | |
309167d2 JD |
848 | /* |
849 | * Populate index values of a kernel stream. Values are set in big endian order. | |
850 | * | |
851 | * Return 0 on success or else a negative value. | |
852 | */ | |
853 | static int get_index_values(struct lttng_packet_index *index, int infd) | |
854 | { | |
855 | int ret; | |
856 | ||
857 | ret = kernctl_get_timestamp_begin(infd, &index->timestamp_begin); | |
858 | if (ret < 0) { | |
859 | PERROR("kernctl_get_timestamp_begin"); | |
860 | goto error; | |
861 | } | |
862 | index->timestamp_begin = htobe64(index->timestamp_begin); | |
863 | ||
864 | ret = kernctl_get_timestamp_end(infd, &index->timestamp_end); | |
865 | if (ret < 0) { | |
866 | PERROR("kernctl_get_timestamp_end"); | |
867 | goto error; | |
868 | } | |
869 | index->timestamp_end = htobe64(index->timestamp_end); | |
870 | ||
871 | ret = kernctl_get_events_discarded(infd, &index->events_discarded); | |
872 | if (ret < 0) { | |
873 | PERROR("kernctl_get_events_discarded"); | |
874 | goto error; | |
875 | } | |
876 | index->events_discarded = htobe64(index->events_discarded); | |
877 | ||
878 | ret = kernctl_get_content_size(infd, &index->content_size); | |
879 | if (ret < 0) { | |
880 | PERROR("kernctl_get_content_size"); | |
881 | goto error; | |
882 | } | |
883 | index->content_size = htobe64(index->content_size); | |
884 | ||
885 | ret = kernctl_get_packet_size(infd, &index->packet_size); | |
886 | if (ret < 0) { | |
887 | PERROR("kernctl_get_packet_size"); | |
888 | goto error; | |
889 | } | |
890 | index->packet_size = htobe64(index->packet_size); | |
891 | ||
892 | ret = kernctl_get_stream_id(infd, &index->stream_id); | |
893 | if (ret < 0) { | |
894 | PERROR("kernctl_get_stream_id"); | |
895 | goto error; | |
896 | } | |
897 | index->stream_id = htobe64(index->stream_id); | |
898 | ||
899 | error: | |
900 | return ret; | |
901 | } | |
902 | ||
d41f73b7 MD |
903 | /* |
904 | * Consume data on a file descriptor and write it on a trace file. | |
905 | */ | |
4078b776 | 906 | ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream, |
d41f73b7 MD |
907 | struct lttng_consumer_local_data *ctx) |
908 | { | |
1d4dfdef | 909 | unsigned long len, subbuf_size, padding; |
309167d2 | 910 | int err, write_index = 0; |
4078b776 | 911 | ssize_t ret = 0; |
d41f73b7 | 912 | int infd = stream->wait_fd; |
309167d2 | 913 | struct lttng_packet_index index; |
d41f73b7 MD |
914 | |
915 | DBG("In read_subbuffer (infd : %d)", infd); | |
309167d2 JD |
916 | |
917 | /* Indicate that for this stream we have to write the index. */ | |
918 | if (stream->index_fd >= 0) { | |
919 | write_index = 1; | |
920 | } | |
921 | ||
d41f73b7 MD |
922 | /* Get the next subbuffer */ |
923 | err = kernctl_get_next_subbuf(infd); | |
924 | if (err != 0) { | |
d41f73b7 MD |
925 | /* |
926 | * This is a debug message even for single-threaded consumer, | |
927 | * because poll() have more relaxed criterions than get subbuf, | |
928 | * so get_subbuf may fail for short race windows where poll() | |
929 | * would issue wakeups. | |
930 | */ | |
931 | DBG("Reserving sub buffer failed (everything is normal, " | |
932 | "it is due to concurrency)"); | |
56591bac | 933 | ret = -errno; |
d41f73b7 MD |
934 | goto end; |
935 | } | |
936 | ||
1d4dfdef DG |
937 | /* Get the full subbuffer size including padding */ |
938 | err = kernctl_get_padded_subbuf_size(infd, &len); | |
939 | if (err != 0) { | |
1d4dfdef | 940 | perror("Getting sub-buffer len failed."); |
56591bac | 941 | ret = -errno; |
1d4dfdef DG |
942 | goto end; |
943 | } | |
944 | ||
309167d2 JD |
945 | if (!stream->metadata_flag && write_index) { |
946 | ret = get_index_values(&index, infd); | |
947 | if (ret < 0) { | |
948 | goto end; | |
949 | } | |
950 | } | |
951 | ||
ffe60014 | 952 | switch (stream->chan->output) { |
07b86b52 | 953 | case CONSUMER_CHANNEL_SPLICE: |
1d4dfdef DG |
954 | /* |
955 | * XXX: The lttng-modules splice "actor" does not handle copying | |
956 | * partial pages hence only using the subbuffer size without the | |
957 | * padding makes the splice fail. | |
958 | */ | |
959 | subbuf_size = len; | |
960 | padding = 0; | |
961 | ||
962 | /* splice the subbuffer to the tracefile */ | |
91dfef6e | 963 | ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, subbuf_size, |
309167d2 | 964 | padding, &index); |
91dfef6e DG |
965 | /* |
966 | * XXX: Splice does not support network streaming so the return value | |
967 | * is simply checked against subbuf_size and not like the mmap() op. | |
968 | */ | |
1d4dfdef DG |
969 | if (ret != subbuf_size) { |
970 | /* | |
971 | * display the error but continue processing to try | |
972 | * to release the subbuffer | |
973 | */ | |
974 | ERR("Error splicing to tracefile (ret: %zd != len: %lu)", | |
975 | ret, subbuf_size); | |
309167d2 | 976 | write_index = 0; |
1d4dfdef DG |
977 | } |
978 | break; | |
07b86b52 | 979 | case CONSUMER_CHANNEL_MMAP: |
1d4dfdef DG |
980 | /* Get subbuffer size without padding */ |
981 | err = kernctl_get_subbuf_size(infd, &subbuf_size); | |
982 | if (err != 0) { | |
1d4dfdef | 983 | perror("Getting sub-buffer len failed."); |
56591bac | 984 | ret = -errno; |
1d4dfdef DG |
985 | goto end; |
986 | } | |
47e81c02 | 987 | |
1d4dfdef DG |
988 | /* Make sure the tracer is not gone mad on us! */ |
989 | assert(len >= subbuf_size); | |
990 | ||
991 | padding = len - subbuf_size; | |
992 | ||
993 | /* write the subbuffer to the tracefile */ | |
91dfef6e | 994 | ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size, |
309167d2 | 995 | padding, &index); |
91dfef6e DG |
996 | /* |
997 | * The mmap operation should write subbuf_size amount of data when | |
998 | * network streaming or the full padding (len) size when we are _not_ | |
999 | * streaming. | |
1000 | */ | |
d88aee68 DG |
1001 | if ((ret != subbuf_size && stream->net_seq_idx != (uint64_t) -1ULL) || |
1002 | (ret != len && stream->net_seq_idx == (uint64_t) -1ULL)) { | |
1d4dfdef | 1003 | /* |
91dfef6e DG |
1004 | * Display the error but continue processing to try to release the |
1005 | * subbuffer | |
1d4dfdef | 1006 | */ |
91dfef6e DG |
1007 | ERR("Error writing to tracefile " |
1008 | "(ret: %zd != len: %lu != subbuf_size: %lu)", | |
1009 | ret, len, subbuf_size); | |
309167d2 | 1010 | write_index = 0; |
1d4dfdef DG |
1011 | } |
1012 | break; | |
1013 | default: | |
1014 | ERR("Unknown output method"); | |
56591bac | 1015 | ret = -EPERM; |
d41f73b7 MD |
1016 | } |
1017 | ||
1018 | err = kernctl_put_next_subbuf(infd); | |
1019 | if (err != 0) { | |
d41f73b7 MD |
1020 | if (errno == EFAULT) { |
1021 | perror("Error in unreserving sub buffer\n"); | |
1022 | } else if (errno == EIO) { | |
1023 | /* Should never happen with newer LTTng versions */ | |
1024 | perror("Reader has been pushed by the writer, last sub-buffer corrupted."); | |
1025 | } | |
56591bac | 1026 | ret = -errno; |
d41f73b7 MD |
1027 | goto end; |
1028 | } | |
1029 | ||
309167d2 JD |
1030 | /* Write index if needed. */ |
1031 | if (write_index) { | |
1032 | err = index_write(stream->index_fd, &index, sizeof(index)); | |
1033 | if (err < 0) { | |
1034 | ret = -1; | |
1035 | goto end; | |
1036 | } | |
1037 | } | |
1038 | ||
d41f73b7 MD |
1039 | end: |
1040 | return ret; | |
1041 | } | |
1042 | ||
1043 | int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream) | |
1044 | { | |
1045 | int ret; | |
ffe60014 DG |
1046 | |
1047 | assert(stream); | |
1048 | ||
2bba9e53 DG |
1049 | /* |
1050 | * Don't create anything if this is set for streaming or should not be | |
1051 | * monitored. | |
1052 | */ | |
1053 | if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor) { | |
fe4477ee JD |
1054 | ret = utils_create_stream_file(stream->chan->pathname, stream->name, |
1055 | stream->chan->tracefile_size, stream->tracefile_count_current, | |
309167d2 | 1056 | stream->uid, stream->gid, NULL); |
fe4477ee JD |
1057 | if (ret < 0) { |
1058 | goto error; | |
1059 | } | |
1060 | stream->out_fd = ret; | |
1061 | stream->tracefile_size_current = 0; | |
309167d2 JD |
1062 | |
1063 | if (!stream->metadata_flag) { | |
1064 | ret = index_create_file(stream->chan->pathname, | |
1065 | stream->name, stream->uid, stream->gid, | |
1066 | stream->chan->tracefile_size, | |
1067 | stream->tracefile_count_current); | |
1068 | if (ret < 0) { | |
1069 | goto error; | |
1070 | } | |
1071 | stream->index_fd = ret; | |
1072 | } | |
ffe60014 | 1073 | } |
d41f73b7 | 1074 | |
d41f73b7 MD |
1075 | if (stream->output == LTTNG_EVENT_MMAP) { |
1076 | /* get the len of the mmap region */ | |
1077 | unsigned long mmap_len; | |
1078 | ||
1079 | ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len); | |
1080 | if (ret != 0) { | |
ffe60014 | 1081 | PERROR("kernctl_get_mmap_len"); |
56591bac | 1082 | ret = -errno; |
d41f73b7 MD |
1083 | goto error_close_fd; |
1084 | } | |
1085 | stream->mmap_len = (size_t) mmap_len; | |
1086 | ||
ffe60014 DG |
1087 | stream->mmap_base = mmap(NULL, stream->mmap_len, PROT_READ, |
1088 | MAP_PRIVATE, stream->wait_fd, 0); | |
d41f73b7 | 1089 | if (stream->mmap_base == MAP_FAILED) { |
ffe60014 | 1090 | PERROR("Error mmaping"); |
d41f73b7 MD |
1091 | ret = -1; |
1092 | goto error_close_fd; | |
1093 | } | |
1094 | } | |
1095 | ||
1096 | /* we return 0 to let the library handle the FD internally */ | |
1097 | return 0; | |
1098 | ||
1099 | error_close_fd: | |
2f225ce2 | 1100 | if (stream->out_fd >= 0) { |
d41f73b7 MD |
1101 | int err; |
1102 | ||
1103 | err = close(stream->out_fd); | |
1104 | assert(!err); | |
2f225ce2 | 1105 | stream->out_fd = -1; |
d41f73b7 MD |
1106 | } |
1107 | error: | |
1108 | return ret; | |
1109 | } | |
1110 | ||
ca22feea DG |
1111 | /* |
1112 | * Check if data is still being extracted from the buffers for a specific | |
4e9a4686 DG |
1113 | * stream. Consumer data lock MUST be acquired before calling this function |
1114 | * and the stream lock. | |
ca22feea | 1115 | * |
6d805429 | 1116 | * Return 1 if the traced data are still getting read else 0 meaning that the |
ca22feea DG |
1117 | * data is available for trace viewer reading. |
1118 | */ | |
6d805429 | 1119 | int lttng_kconsumer_data_pending(struct lttng_consumer_stream *stream) |
ca22feea DG |
1120 | { |
1121 | int ret; | |
1122 | ||
1123 | assert(stream); | |
1124 | ||
873b9e9a MD |
1125 | if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) { |
1126 | ret = 0; | |
1127 | goto end; | |
1128 | } | |
1129 | ||
ca22feea DG |
1130 | ret = kernctl_get_next_subbuf(stream->wait_fd); |
1131 | if (ret == 0) { | |
1132 | /* There is still data so let's put back this subbuffer. */ | |
1133 | ret = kernctl_put_subbuf(stream->wait_fd); | |
1134 | assert(ret == 0); | |
6d805429 | 1135 | ret = 1; /* Data is pending */ |
4e9a4686 | 1136 | goto end; |
ca22feea DG |
1137 | } |
1138 | ||
6d805429 DG |
1139 | /* Data is NOT pending and ready to be read. */ |
1140 | ret = 0; | |
ca22feea | 1141 | |
6efae65e DG |
1142 | end: |
1143 | return ret; | |
ca22feea | 1144 | } |