From c142ca63c9bef7af3a6ef9d0b7565730f2d02014 Mon Sep 17 00:00:00 2001 From: Stefan Herbrechtsmeier Date: Thu, 27 Jan 2022 14:31:24 +0100 Subject: [PATCH 1/4] cangen: exit program with failure in case of poll timeout The poll function returns 0 to indicate a call timed out. Signed-off-by: Stefan Herbrechtsmeier --- cangen.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cangen.c b/cangen.c index 549c8e3..916bbb8 100644 --- a/cangen.c +++ b/cangen.c @@ -504,7 +504,7 @@ resend: } if (polltimeout) { /* wait for the write socket (with timeout) */ - if (poll(&fds, 1, polltimeout) < 0) { + if (poll(&fds, 1, polltimeout) <= 0) { perror("poll"); return 1; } From 3342eb42e7960db496f2295c480df60c977a61fc Mon Sep 17 00:00:00 2001 From: Stefan Herbrechtsmeier Date: Thu, 27 Jan 2022 15:50:20 +0100 Subject: [PATCH 2/4] cangen: Do not treat -EINTR as error --- cangen.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cangen.c b/cangen.c index 916bbb8..2327064 100644 --- a/cangen.c +++ b/cangen.c @@ -503,8 +503,11 @@ resend: return 1; } if (polltimeout) { + int ret; + /* wait for the write socket (with timeout) */ - if (poll(&fds, 1, polltimeout) <= 0) { + ret = poll(&fds, 1, polltimeout); + if (ret == 0 || (ret == -1 && errno != -EINTR)) { perror("poll"); return 1; } From 7af4264a13c6c100db845f88e2a356861f3eb825 Mon Sep 17 00:00:00 2001 From: Stefan Herbrechtsmeier Date: Thu, 27 Jan 2022 14:35:14 +0100 Subject: [PATCH 3/4] cansequence: exit program with failure in case of poll timeout The poll function returns 0 to indicate a call timed out. Signed-off-by: Stefan Herbrechtsmeier --- cansequence.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cansequence.c b/cansequence.c index 3f22415..78cf5a5 100644 --- a/cansequence.c +++ b/cansequence.c @@ -216,7 +216,7 @@ static void do_send() } err = poll(fds, 1, 1000); - if (err == -1 && errno != -EINTR) { + if (err == 0 || (err == -1 && errno != -EINTR)) { perror("poll()"); exit(EXIT_FAILURE); } From 25de6276e86c459e94ae69c4b95a6e9f1cd8cdc4 Mon Sep 17 00:00:00 2001 From: Stefan Herbrechtsmeier Date: Thu, 27 Jan 2022 14:45:15 +0100 Subject: [PATCH 4/4] j1939cat: fix error detection of poll function call The poll function return -1 on error and set errno to indicate the error. Signed-off-by: Stefan Herbrechtsmeier --- j1939cat.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/j1939cat.c b/j1939cat.c index aced6b9..34a8c03 100644 --- a/j1939cat.c +++ b/j1939cat.c @@ -368,10 +368,12 @@ static int j1939cat_send_loop(struct j1939cat_priv *priv, int out_fd, char *buf, int ret; ret = poll(&fds, 1, priv->polltimeout); - if (ret == -EINTR) - continue; - if (ret < 0) - return -errno; + if (ret == -1) { + if (errno == -EINTR) + continue; + else + return -errno; + } if (!ret) return -ETIME; if (!(fds.revents & events)) { @@ -572,10 +574,12 @@ static int j1939cat_recv(struct j1939cat_priv *priv) int ret; ret = poll(&fds, 1, priv->polltimeout); - if (ret == -EINTR) - continue; - if (ret < 0) - return -errno; + if (ret == -1) { + if (errno == -EINTR) + continue; + else + return -errno; + } if (!ret) continue; if (!(fds.revents & events)) {