From: Richard Kettlewell Date: Mon, 11 Jul 2011 18:44:18 +0000 (+0100) Subject: Log write failures on tun device. X-Git-Tag: v0.2.0~44 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=secnet.git;a=commitdiff_plain;h=090dbeef60c7e578950c3dbe807a9e2ea7e24875;hp=baab3a63ada033b2b9eed46807b2e88536612bbc Log write failures on tun device. This is useful when debugging. Short writes count as failures The error reporting is rate limited to ensure that the logs are not flooded. Signed-off-by: Richard Kettlewell --- diff --git a/tun.c b/tun.c index 7d1053a..3db998f 100644 --- a/tun.c +++ b/tun.c @@ -3,6 +3,7 @@ #include "netlink.h" #include #include +#include #include #include #include @@ -134,11 +135,27 @@ static void tun_afterpoll(void *sst, struct pollfd *fds, int nfds) static void tun_deliver_to_kernel(void *sst, struct buffer_if *buf) { struct tun *st=sst; + ssize_t rc; BUF_ASSERT_USED(buf); - /* No error checking, because we'd just throw the packet away - anyway if it didn't work. */ - write(st->fd,buf->start,buf->size); + + /* Log errors, so we can tell what's going on, but only once a + minute, so we don't flood the logs. Short writes count as + errors. */ + rc = write(st->fd,buf->start,buf->size); + if(rc != buf->size) { + static struct timeval last_report; + if(tv_now_global.tv_sec >= last_report.tv_sec + 60) { + if(rc < 0) + Message(M_WARNING, + "failed to deliver packet to tun device: %s\n", + strerror(errno)); + else + Message(M_WARNING, + "truncated packet delivered to tun device\n"); + last_report = tv_now_global; + } + } BUF_FREE(buf); }