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