All of lore.kernel.org
 help / color / mirror / Atom feed
From: uescher <linux-can_ger.kernel.org@myvdr.de>
To: linux-can@vger.kernel.org
Subject: [PATCH 1/2] slcanpty: Improve the reading from pty to avoid incomplete messages in the reading buf[]
Date: Sun, 24 Feb 2013 16:30:35 +0100	[thread overview]
Message-ID: <512A321B.7090201@myvdr.de> (raw)

[-- Attachment #1: Type: text/plain, Size: 295 bytes --]

slcanpty: Improve the reading from pty to avoid incomplete messages in 
the reading buf[]

This Patch ensure that we read the maximum of complete SLCAN messages 
from pty.
Leave messages at the pty if is not possible to read it complete in the buf.

Signed-off-by: Ulrich Escher <git@myvdr.de>


[-- Attachment #2: 0001-slcanpty-Improve-the-reading-from-pty-to-avoid-incom.patch --]
[-- Type: text/x-patch, Size: 1772 bytes --]

From 13e8e6ad85e42f68816d14eb74c0f3fabcd28465 Mon Sep 17 00:00:00 2001
From: ulrich <git@myvdr.de>
Date: Sun, 24 Feb 2013 15:55:19 +0100
Subject: [PATCH 1/2] slcanpty: Improve the reading from pty to avoid
 incomplete messages in the reading buf[]

This Patch ensure that we read the maximum of complete SLCAN messages from pty.
Leave messages at the pty if is not possible to read it complete in the buf.

Signed-off-by: Ulrich Escher <git@myvdr.de>
---
 slcanpty.c |   32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)

diff --git a/slcanpty.c b/slcanpty.c
index f3628b9..037edf5 100644
--- a/slcanpty.c
+++ b/slcanpty.c
@@ -31,6 +31,7 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <termios.h>
+#include <poll.h>
 
 #include <net/if.h>
 #include <sys/socket.h>
@@ -74,12 +75,41 @@ int pty2can(int pty, int socket, struct can_filter *fi,
 	struct can_frame frame;
 	int tmp, i;
 
-	nbytes = read(pty, &buf, sizeof(buf)-1);
+	nbytes = read(pty, &buf, sizeof(buf)-SLC_MTU-1);
 	if (nbytes < 0) {
 		perror("read pty");
 		return 1;
 	}
 
+	if (nbytes == sizeof(buf)-SLC_MTU-1) {
+		struct pollfd fds;
+		fds.fd = pty;
+		fds.events = POLLIN;
+
+		while (buf[nbytes-1] != '\r' && nbytes < (sizeof(buf)-1)) {
+			tmp = poll(&fds, 1, 0);
+			if (tmp < 0) {
+				perror("poll pty");
+				return 1;
+			}
+
+			if (fds.revents & POLLIN) {
+				tmp = read(pty, &buf[nbytes++], 1);
+				if (tmp < 0) {
+					perror("read pty");
+					return 1;
+				}
+			} else {
+				/* No further Data available */
+				break;
+			}
+		}
+	}
+
+#ifdef DEBUG
+	printf("read %d bytes from pty\n",nbytes);
+#endif
+
 rx_restart:
 	/* remove trailing '\r' characters to be robust against some apps */
 	while (buf[0] == '\r' && nbytes > 0) {
-- 
1.7.9.5


                 reply	other threads:[~2013-02-24 15:37 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=512A321B.7090201@myvdr.de \
    --to=linux-can_ger.kernel.org@myvdr.de \
    --cc=linux-can@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.