Bryan O'Sullivan
2003-Apr-29 13:06 UTC
[klibc] [PATCH] Add configurable timeout to ipconfig
Knocking a small item off Russell's feature list. This applies on top of my earlier bugfix+options patch. README | 15 ++++----------- main.c | 28 +++++++++++++++++++++++----- 2 files changed, 27 insertions(+), 16 deletions(-) diff -Nru a/ipconfig/README b/ipconfig/README --- a/ipconfig/README Tue Apr 29 13:03:14 2003 +++ b/ipconfig/README Tue Apr 29 13:03:14 2003 @@ -10,22 +10,15 @@ It will wait for all listed interfaces to complete bootp/dhcp configuration. -2. We should have a timeout parameter: - - ipconfig -t 30 - - so we can optionally bound the time we allow for the configuration - to complete. - -3. We should let the user specify the configuration protocol +2. We should let the user specify the configuration protocol (maybe per-interface?) The code internally supports per-interface. -4. Static IP configuration. +3. Static IP configuration. -5. Command line arguments. Maybe we should accept an ip= string on the +4. Command line arguments. Maybe we should accept an ip= string on the command line to remain compatible with the kernel? -6. The code in main.c is yucky imho. Needs cleaning. +5. The code in main.c is yucky imho. Needs cleaning. -- Russell King (22/10/2002) diff -Nru a/ipconfig/main.c b/ipconfig/main.c --- a/ipconfig/main.c Tue Apr 29 13:03:14 2003 +++ b/ipconfig/main.c Tue Apr 29 13:03:14 2003 @@ -18,6 +18,7 @@ static const char *progname; static char do_not_config; +static int loop_timeout = -1; struct state { int state; @@ -270,6 +271,7 @@ int pkt_fd; int nr = 0; struct timeval now, prev; + time_t start; pkt_fd = packet_open(); if (pkt_fd == -1) { @@ -281,6 +283,7 @@ fds[0].events = POLLRDNORM; gettimeofday(&now, NULL); + start = now.tv_sec; while (1) { int timeout = 60; int pending = 0; @@ -308,8 +311,8 @@ for (x = 0; x < 2; x++) { int delta_ms; - if (timeout_ms <= 0) - timeout_ms = 1; + if (timeout_ms <= 0 || loop_timeout == 0) + timeout_ms = 100; nr = poll(fds, NR_FDS, timeout_ms); prev = now; @@ -319,6 +322,14 @@ do_pkt_recv(pkt_fd, now.tv_sec) == 1) { break; } + + if (loop_timeout >= 0 && + now.tv_sec - start >= loop_timeout) { + printf("IP-Config: no response after %d " + "secs - giving up\n", loop_timeout); + goto bail; + } + packet_discard(); delta_ms = (now.tv_sec - prev.tv_sec) * 1000; @@ -329,7 +340,7 @@ timeout_ms -= delta_ms; } } - + bail: packet_close(); return 0; @@ -404,7 +415,7 @@ progname = argv[0]; do { - c = getopt(argc, argv, "d:np:t"); + c = getopt(argc, argv, "d:np:t:"); if (c == EOF) break; @@ -413,7 +424,7 @@ port = atoi(optarg); if (port <= 0 || port > USHRT_MAX) { fprintf(stderr, - "%s: port number %d out of range\n", + "%s: invalid port number %d\n", progname, port); exit(1); } @@ -423,6 +434,13 @@ "dest to %d\n", local_port, remote_port); break; case 't': + loop_timeout = atoi(optarg); + if (loop_timeout < 0) { + fprintf(stderr, + "%s: invalid timeout %d\n", + progname, loop_timeout); + exit(1); + } break; case 'n': do_not_config = 1;