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