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