Fix: error handling in kernel add_channel
[lttng-tools.git] / src / common / kernel-consumer / kernel-consumer.c
1 /*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
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.
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 *
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.
17 */
18
19 #define _GNU_SOURCE
20 #include <assert.h>
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>
28 #include <inttypes.h>
29 #include <unistd.h>
30 #include <sys/stat.h>
31
32 #include <common/common.h>
33 #include <common/kernel-ctl/kernel-ctl.h>
34 #include <common/sessiond-comm/sessiond-comm.h>
35 #include <common/sessiond-comm/relayd.h>
36 #include <common/compat/fcntl.h>
37 #include <common/pipe.h>
38 #include <common/relayd/relayd.h>
39 #include <common/utils.h>
40
41 #include "kernel-consumer.h"
42
43 extern struct lttng_consumer_global_data consumer_data;
44 extern int consumer_poll_timeout;
45 extern volatile int consumer_quit;
46
47 /*
48 * Take a snapshot for a specific fd
49 *
50 * Returns 0 on success, < 0 on error
51 */
52 int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream *stream)
53 {
54 int ret = 0;
55 int infd = stream->wait_fd;
56
57 ret = kernctl_snapshot(infd);
58 if (ret != 0) {
59 errno = -ret;
60 perror("Getting sub-buffer snapshot.");
61 }
62
63 return ret;
64 }
65
66 /*
67 * Get the produced position
68 *
69 * Returns 0 on success, < 0 on error
70 */
71 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream *stream,
72 unsigned long *pos)
73 {
74 int ret;
75 int infd = stream->wait_fd;
76
77 ret = kernctl_snapshot_get_produced(infd, pos);
78 if (ret != 0) {
79 errno = -ret;
80 perror("kernctl_snapshot_get_produced");
81 }
82
83 return ret;
84 }
85
86 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
87 int sock, struct pollfd *consumer_sockpoll)
88 {
89 ssize_t ret;
90 enum lttng_error_code ret_code = LTTNG_OK;
91 struct lttcomm_consumer_msg msg;
92
93 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
94 if (ret != sizeof(msg)) {
95 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
96 return ret;
97 }
98 if (msg.cmd_type == LTTNG_CONSUMER_STOP) {
99 /*
100 * Notify the session daemon that the command is completed.
101 *
102 * On transport layer error, the function call will print an error
103 * message so handling the returned code is a bit useless since we
104 * return an error code anyway.
105 */
106 (void) consumer_send_status_msg(sock, ret_code);
107 return -ENOENT;
108 }
109
110 /* relayd needs RCU read-side protection */
111 rcu_read_lock();
112
113 switch (msg.cmd_type) {
114 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
115 {
116 /* Session daemon status message are handled in the following call. */
117 ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
118 msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll,
119 &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id);
120 goto end_nosignal;
121 }
122 case LTTNG_CONSUMER_ADD_CHANNEL:
123 {
124 struct lttng_consumer_channel *new_channel;
125 int ret_recv;
126
127 /* First send a status message before receiving the fds. */
128 ret = consumer_send_status_msg(sock, ret_code);
129 if (ret < 0) {
130 /* Somehow, the session daemon is not responding anymore. */
131 goto end_nosignal;
132 }
133 DBG("consumer_add_channel %" PRIu64, msg.u.channel.channel_key);
134 new_channel = consumer_allocate_channel(msg.u.channel.channel_key,
135 msg.u.channel.session_id, msg.u.channel.pathname,
136 msg.u.channel.name, msg.u.channel.uid, msg.u.channel.gid,
137 msg.u.channel.relayd_id, msg.u.channel.output,
138 msg.u.channel.tracefile_size,
139 msg.u.channel.tracefile_count);
140 if (new_channel == NULL) {
141 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
142 goto end_nosignal;
143 }
144 new_channel->nb_init_stream_left = msg.u.channel.nb_init_streams;
145
146 /* Translate and save channel type. */
147 switch (msg.u.channel.type) {
148 case CONSUMER_CHANNEL_TYPE_DATA:
149 case CONSUMER_CHANNEL_TYPE_METADATA:
150 new_channel->type = msg.u.channel.type;
151 break;
152 default:
153 assert(0);
154 goto end_nosignal;
155 };
156
157 if (ctx->on_recv_channel != NULL) {
158 ret_recv = ctx->on_recv_channel(new_channel);
159 if (ret_recv == 0) {
160 ret = consumer_add_channel(new_channel, ctx);
161 } else if (ret_recv < 0) {
162 goto end_nosignal;
163 }
164 } else {
165 ret = consumer_add_channel(new_channel, ctx);
166 }
167
168 /* If we received an error in add_channel, we need to report it. */
169 if (ret != 0) {
170 consumer_send_status_msg(sock, ret);
171 goto end_nosignal;
172 }
173
174 goto end_nosignal;
175 }
176 case LTTNG_CONSUMER_ADD_STREAM:
177 {
178 int fd;
179 struct lttng_pipe *stream_pipe;
180 struct consumer_relayd_sock_pair *relayd = NULL;
181 struct lttng_consumer_stream *new_stream;
182 struct lttng_consumer_channel *channel;
183 int alloc_ret = 0;
184
185 /*
186 * Get stream's channel reference. Needed when adding the stream to the
187 * global hash table.
188 */
189 channel = consumer_find_channel(msg.u.stream.channel_key);
190 if (!channel) {
191 /*
192 * We could not find the channel. Can happen if cpu hotplug
193 * happens while tearing down.
194 */
195 ERR("Unable to find channel key %" PRIu64, msg.u.stream.channel_key);
196 ret_code = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
197 }
198
199 /* First send a status message before receiving the fds. */
200 ret = consumer_send_status_msg(sock, ret_code);
201 if (ret < 0 || ret_code != LTTNG_OK) {
202 /*
203 * Somehow, the session daemon is not responding anymore or the
204 * channel was not found.
205 */
206 goto end_nosignal;
207 }
208
209 /* block */
210 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
211 rcu_read_unlock();
212 return -EINTR;
213 }
214
215 /* Get stream file descriptor from socket */
216 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
217 if (ret != sizeof(fd)) {
218 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
219 rcu_read_unlock();
220 return ret;
221 }
222
223 /*
224 * Send status code to session daemon only if the recv works. If the
225 * above recv() failed, the session daemon is notified through the
226 * error socket and the teardown is eventually done.
227 */
228 ret = consumer_send_status_msg(sock, ret_code);
229 if (ret < 0) {
230 /* Somehow, the session daemon is not responding anymore. */
231 goto end_nosignal;
232 }
233
234 new_stream = consumer_allocate_stream(channel->key,
235 fd,
236 LTTNG_CONSUMER_ACTIVE_STREAM,
237 channel->name,
238 channel->uid,
239 channel->gid,
240 channel->relayd_id,
241 channel->session_id,
242 msg.u.stream.cpu,
243 &alloc_ret,
244 channel->type);
245 if (new_stream == NULL) {
246 switch (alloc_ret) {
247 case -ENOMEM:
248 case -EINVAL:
249 default:
250 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
251 break;
252 }
253 goto end_nosignal;
254 }
255 new_stream->chan = channel;
256 new_stream->wait_fd = fd;
257
258 /*
259 * The buffer flush is done on the session daemon side for the kernel
260 * so no need for the stream "hangup_flush_done" variable to be
261 * tracked. This is important for a kernel stream since we don't rely
262 * on the flush state of the stream to read data. It's not the case for
263 * user space tracing.
264 */
265 new_stream->hangup_flush_done = 0;
266
267 /* The stream is not metadata. Get relayd reference if exists. */
268 relayd = consumer_find_relayd(new_stream->net_seq_idx);
269 if (relayd != NULL) {
270 /* Add stream on the relayd */
271 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
272 ret = relayd_add_stream(&relayd->control_sock,
273 new_stream->name, new_stream->chan->pathname,
274 &new_stream->relayd_stream_id,
275 new_stream->chan->tracefile_size,
276 new_stream->chan->tracefile_count);
277 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
278 if (ret < 0) {
279 consumer_del_stream(new_stream, NULL);
280 goto end_nosignal;
281 }
282 } else if (new_stream->net_seq_idx != (uint64_t) -1ULL) {
283 ERR("Network sequence index %" PRIu64 " unknown. Not adding stream.",
284 new_stream->net_seq_idx);
285 consumer_del_stream(new_stream, NULL);
286 goto end_nosignal;
287 }
288
289 if (ctx->on_recv_stream) {
290 ret = ctx->on_recv_stream(new_stream);
291 if (ret < 0) {
292 consumer_del_stream(new_stream, NULL);
293 goto end_nosignal;
294 }
295 }
296
297 /* Get the right pipe where the stream will be sent. */
298 if (new_stream->metadata_flag) {
299 stream_pipe = ctx->consumer_metadata_pipe;
300 } else {
301 stream_pipe = ctx->consumer_data_pipe;
302 }
303
304 ret = lttng_pipe_write(stream_pipe, &new_stream, sizeof(new_stream));
305 if (ret < 0) {
306 ERR("Consumer write %s stream to pipe %d",
307 new_stream->metadata_flag ? "metadata" : "data",
308 lttng_pipe_get_writefd(stream_pipe));
309 consumer_del_stream(new_stream, NULL);
310 goto end_nosignal;
311 }
312
313 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64,
314 new_stream->name, fd, new_stream->relayd_stream_id);
315 break;
316 }
317 case LTTNG_CONSUMER_UPDATE_STREAM:
318 {
319 rcu_read_unlock();
320 return -ENOSYS;
321 }
322 case LTTNG_CONSUMER_DESTROY_RELAYD:
323 {
324 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
325 struct consumer_relayd_sock_pair *relayd;
326
327 DBG("Kernel consumer destroying relayd %" PRIu64, index);
328
329 /* Get relayd reference if exists. */
330 relayd = consumer_find_relayd(index);
331 if (relayd == NULL) {
332 DBG("Unable to find relayd %" PRIu64, index);
333 ret_code = LTTNG_ERR_NO_CONSUMER;
334 }
335
336 /*
337 * Each relayd socket pair has a refcount of stream attached to it
338 * which tells if the relayd is still active or not depending on the
339 * refcount value.
340 *
341 * This will set the destroy flag of the relayd object and destroy it
342 * if the refcount reaches zero when called.
343 *
344 * The destroy can happen either here or when a stream fd hangs up.
345 */
346 if (relayd) {
347 consumer_flag_relayd_for_destroy(relayd);
348 }
349
350 ret = consumer_send_status_msg(sock, ret_code);
351 if (ret < 0) {
352 /* Somehow, the session daemon is not responding anymore. */
353 goto end_nosignal;
354 }
355
356 goto end_nosignal;
357 }
358 case LTTNG_CONSUMER_DATA_PENDING:
359 {
360 int32_t ret;
361 uint64_t id = msg.u.data_pending.session_id;
362
363 DBG("Kernel consumer data pending command for id %" PRIu64, id);
364
365 ret = consumer_data_pending(id);
366
367 /* Send back returned value to session daemon */
368 ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret));
369 if (ret < 0) {
370 PERROR("send data pending ret code");
371 }
372
373 /*
374 * No need to send back a status message since the data pending
375 * returned value is the response.
376 */
377 break;
378 }
379 default:
380 goto end_nosignal;
381 }
382
383 end_nosignal:
384 rcu_read_unlock();
385
386 /*
387 * Return 1 to indicate success since the 0 value can be a socket
388 * shutdown during the recv() or send() call.
389 */
390 return 1;
391 }
392
393 /*
394 * Consume data on a file descriptor and write it on a trace file.
395 */
396 ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
397 struct lttng_consumer_local_data *ctx)
398 {
399 unsigned long len, subbuf_size, padding;
400 int err;
401 ssize_t ret = 0;
402 int infd = stream->wait_fd;
403
404 DBG("In read_subbuffer (infd : %d)", infd);
405 /* Get the next subbuffer */
406 err = kernctl_get_next_subbuf(infd);
407 if (err != 0) {
408 ret = err;
409 /*
410 * This is a debug message even for single-threaded consumer,
411 * because poll() have more relaxed criterions than get subbuf,
412 * so get_subbuf may fail for short race windows where poll()
413 * would issue wakeups.
414 */
415 DBG("Reserving sub buffer failed (everything is normal, "
416 "it is due to concurrency)");
417 goto end;
418 }
419
420 /* Get the full subbuffer size including padding */
421 err = kernctl_get_padded_subbuf_size(infd, &len);
422 if (err != 0) {
423 errno = -err;
424 perror("Getting sub-buffer len failed.");
425 ret = err;
426 goto end;
427 }
428
429 switch (stream->chan->output) {
430 case LTTNG_EVENT_SPLICE:
431
432 /*
433 * XXX: The lttng-modules splice "actor" does not handle copying
434 * partial pages hence only using the subbuffer size without the
435 * padding makes the splice fail.
436 */
437 subbuf_size = len;
438 padding = 0;
439
440 /* splice the subbuffer to the tracefile */
441 ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, subbuf_size,
442 padding);
443 /*
444 * XXX: Splice does not support network streaming so the return value
445 * is simply checked against subbuf_size and not like the mmap() op.
446 */
447 if (ret != subbuf_size) {
448 /*
449 * display the error but continue processing to try
450 * to release the subbuffer
451 */
452 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
453 ret, subbuf_size);
454 }
455 break;
456 case LTTNG_EVENT_MMAP:
457 /* Get subbuffer size without padding */
458 err = kernctl_get_subbuf_size(infd, &subbuf_size);
459 if (err != 0) {
460 errno = -err;
461 perror("Getting sub-buffer len failed.");
462 ret = err;
463 goto end;
464 }
465
466 /* Make sure the tracer is not gone mad on us! */
467 assert(len >= subbuf_size);
468
469 padding = len - subbuf_size;
470
471 /* write the subbuffer to the tracefile */
472 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size,
473 padding);
474 /*
475 * The mmap operation should write subbuf_size amount of data when
476 * network streaming or the full padding (len) size when we are _not_
477 * streaming.
478 */
479 if ((ret != subbuf_size && stream->net_seq_idx != (uint64_t) -1ULL) ||
480 (ret != len && stream->net_seq_idx == (uint64_t) -1ULL)) {
481 /*
482 * Display the error but continue processing to try to release the
483 * subbuffer
484 */
485 ERR("Error writing to tracefile "
486 "(ret: %zd != len: %lu != subbuf_size: %lu)",
487 ret, len, subbuf_size);
488 }
489 break;
490 default:
491 ERR("Unknown output method");
492 ret = -1;
493 }
494
495 err = kernctl_put_next_subbuf(infd);
496 if (err != 0) {
497 errno = -err;
498 if (errno == EFAULT) {
499 perror("Error in unreserving sub buffer\n");
500 } else if (errno == EIO) {
501 /* Should never happen with newer LTTng versions */
502 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
503 }
504
505 ret = -err;
506 goto end;
507 }
508
509 end:
510 return ret;
511 }
512
513 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
514 {
515 int ret;
516
517 assert(stream);
518
519 /* Don't create anything if this is set for streaming. */
520 if (stream->net_seq_idx == (uint64_t) -1ULL) {
521 ret = utils_create_stream_file(stream->chan->pathname, stream->name,
522 stream->chan->tracefile_size, stream->tracefile_count_current,
523 stream->uid, stream->gid);
524 if (ret < 0) {
525 goto error;
526 }
527 stream->out_fd = ret;
528 stream->tracefile_size_current = 0;
529 }
530
531 if (stream->output == LTTNG_EVENT_MMAP) {
532 /* get the len of the mmap region */
533 unsigned long mmap_len;
534
535 ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len);
536 if (ret != 0) {
537 errno = -ret;
538 PERROR("kernctl_get_mmap_len");
539 goto error_close_fd;
540 }
541 stream->mmap_len = (size_t) mmap_len;
542
543 stream->mmap_base = mmap(NULL, stream->mmap_len, PROT_READ,
544 MAP_PRIVATE, stream->wait_fd, 0);
545 if (stream->mmap_base == MAP_FAILED) {
546 PERROR("Error mmaping");
547 ret = -1;
548 goto error_close_fd;
549 }
550 }
551
552 /* we return 0 to let the library handle the FD internally */
553 return 0;
554
555 error_close_fd:
556 {
557 int err;
558
559 err = close(stream->out_fd);
560 assert(!err);
561 }
562 error:
563 return ret;
564 }
565
566 /*
567 * Check if data is still being extracted from the buffers for a specific
568 * stream. Consumer data lock MUST be acquired before calling this function
569 * and the stream lock.
570 *
571 * Return 1 if the traced data are still getting read else 0 meaning that the
572 * data is available for trace viewer reading.
573 */
574 int lttng_kconsumer_data_pending(struct lttng_consumer_stream *stream)
575 {
576 int ret;
577
578 assert(stream);
579
580 ret = kernctl_get_next_subbuf(stream->wait_fd);
581 if (ret == 0) {
582 /* There is still data so let's put back this subbuffer. */
583 ret = kernctl_put_subbuf(stream->wait_fd);
584 assert(ret == 0);
585 ret = 1; /* Data is pending */
586 goto end;
587 }
588
589 /* Data is NOT pending and ready to be read. */
590 ret = 0;
591
592 end:
593 return ret;
594 }
This page took 0.040818 seconds and 4 git commands to generate.