From 5e1f381183a01ecd58229bee88c6dc4daf27c23b Mon Sep 17 00:00:00 2001
From: Peter Korsgaard <jacmet@sunsite.dk>
Date: Tue, 20 May 2008 12:50:01 +0200
Subject: [PATCH] udhcpc: give up on repeated errors

udhcpc never time out if errors occur (E.G. if the network
interface didn't detect a link), as the file descriptor keeps
on being readable with the error condition (E.G. -ENETDOWN).

Add a max_errors and give up if it is exceeded.

Signed-off-by: Peter Korsgaard <jacmet@sunsite.dk>
---
 networking/udhcp/dhcpc.c |    9 +++++++++
 1 files changed, 9 insertions(+), 0 deletions(-)

diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c
index fb328cb..8154db3 100644
--- a/networking/udhcp/dhcpc.c
+++ b/networking/udhcp/dhcpc.c
@@ -140,6 +140,8 @@ int udhcpc_main(int argc ATTRIBUTE_UNUSED, char **argv)
 	int tryagain_timeout = 20;
 	int discover_timeout = 3;
 	int discover_retries = 3;
+	int errors = 0;
+	int max_errors = 5;
 	uint32_t xid = 0;
 	uint32_t lease_seconds = 0; /* can be given as 32-bit quantity */
 	int packet_num;
@@ -477,6 +479,13 @@ int udhcpc_main(int argc ATTRIBUTE_UNUSED, char **argv)
 			if (len == -1) { /* error is severe, reopen socket */
 				DEBUG("error on read, %s, reopening socket", strerror(errno));
 				change_listen_mode(listen_mode); /* just close and reopen */
+				if (++errors >= max_errors) {
+					bb_info_msg("Too many errors, failing");
+					retval = 2;
+					goto ret;
+				}
+			} else {
+				errors = 0;
 			}
 			if (len < 0) continue;
 
-- 
1.5.5.1

