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