From da63dcc00a2b9db442cdacb3f36046e661894561 Mon Sep 17 00:00:00 2001 From: krw Date: Tue, 6 Feb 2018 23:45:15 +0000 Subject: [PATCH] Abstract "no link ..." and "no lease ..." message generation into a function tick_msg(). Now both work the same way, dot dot dotting as time passes until they 'sleep'. Tested by tb@ as part of a larger diff. --- sbin/dhclient/dhclient.c | 109 ++++++++++++++++++++++++--------------- 1 file changed, 67 insertions(+), 42 deletions(-) diff --git a/sbin/dhclient/dhclient.c b/sbin/dhclient/dhclient.c index 82221d6e318..0dbcfd75947 100644 --- a/sbin/dhclient/dhclient.c +++ b/sbin/dhclient/dhclient.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dhclient.c,v 1.558 2018/02/06 21:09:10 krw Exp $ */ +/* $OpenBSD: dhclient.c,v 1.559 2018/02/06 23:45:15 krw Exp $ */ /* * Copyright 2004 Henning Brauer @@ -137,6 +137,7 @@ int get_ifa_family(char *, int); void interface_link_forceup(char *, int); int interface_status(char *); void get_hw_address(struct interface_info *); +void tick_msg(const char *, int, time_t, time_t); struct client_lease *apply_defaults(struct client_lease *); struct client_lease *clone_lease(struct client_lease *); @@ -689,42 +690,27 @@ usage(void) void state_preboot(struct interface_info *ifi) { - static int preamble; - time_t cur_time; - int interval; + time_t cur_time, tickstart, tickstop; time(&cur_time); - interval = cur_time - ifi->startup_time; + tickstart = ifi->startup_time + 3; + tickstop = ifi->startup_time + config->link_timeout; ifi->linkstat = interface_status(ifi->name); - if (isatty(STDERR_FILENO) != 0 && interval > 3) { - if (preamble == 0 && ifi->linkstat == 0) { - fprintf(stderr, "%s: no link ....", ifi->name); - preamble = 1; - } - if (preamble != 0) { - if (ifi->linkstat != 0) - fprintf(stderr, " got link\n"); - else if (interval > config->link_timeout) - fprintf(stderr, " sleeping\n"); - else - fprintf(stderr, "."); - fflush(stderr); - } - } - if (ifi->linkstat != 0) { + tick_msg("link", 1, tickstart, tickstop); if ((ifi->flags & IFI_VALID_LLADDR) == 0) get_hw_address(ifi); ifi->state = S_REBOOTING; state_reboot(ifi); - } else if (interval > config->link_timeout) { - go_daemon(); - cancel_timeout(ifi); /* Wait for RTM_IFINFO. */ } else { - set_timeout(ifi, 1, state_preboot); + tick_msg("link", 0, tickstart, tickstop); + if (cur_time > tickstop) + cancel_timeout(ifi); /* Wait for RTM_IFINFO. */ + else + set_timeout(ifi, 1, state_preboot); } } @@ -963,10 +949,15 @@ bind_lease(struct interface_info *ifi) struct proposal *offered_proposal = NULL; struct proposal *effective_proposal = NULL; char *msg; - time_t cur_time, renewal; + time_t cur_time, renewal, tickstart, tickstop; int rslt, seen; time(&cur_time); + tickstart = ifi->first_sending + 3; + tickstop = ifi->startup_time + config->link_timeout; + if ((cmd_opts & OPT_VERBOSE) == 0) + tick_msg("lease", 1, tickstart, tickstop); + lease = apply_defaults(ifi->offer); /* @@ -1295,15 +1286,17 @@ void send_discover(struct interface_info *ifi) { struct dhcp_packet *packet = &ifi->sent_packet; - time_t cur_time; + time_t cur_time, interval, tickstart, tickstop; ssize_t rslt; - int interval; time(&cur_time); /* Figure out how long it's been since we started transmitting. */ interval = cur_time - ifi->first_sending; + tickstart = ifi->first_sending + 3; + tickstop = ifi->startup_time + config->link_timeout; + if (interval > config->timeout) { state_panic(ifi); return; @@ -1339,13 +1332,12 @@ send_discover(struct interface_info *ifi) * link_timeout we just go daemon and finish things up in the * background. */ - if (cur_time - ifi->startup_time < config->link_timeout) + if (cur_time < tickstop) { + if ((cmd_opts & OPT_VERBOSE) == 0) + tick_msg("lease", 0, tickstart, tickstop); ifi->interval = 1; - else { - if (isatty(STDERR_FILENO) != 0) - fprintf(stderr, "%s: no lease .... sleeping\n", - log_procname); - go_daemon(); + } else { + tick_msg("lease", 0, tickstart, tickstop); } /* Record the number of seconds since we started sending. */ @@ -1397,14 +1389,16 @@ send_request(struct interface_info *ifi) struct in_addr from; struct dhcp_packet *packet = &ifi->sent_packet; ssize_t rslt; - time_t cur_time; - int interval; + time_t cur_time, interval, tickstart, tickstop; time(&cur_time); /* Figure out how long it's been since we started transmitting. */ interval = cur_time - ifi->first_sending; + tickstart = ifi->first_sending + 3; + tickstop = ifi->startup_time + config->link_timeout; + /* * If we're in the INIT-REBOOT state and we've been trying longer * than reboot_timeout, go to INIT state and DISCOVER an address. @@ -1461,13 +1455,12 @@ send_request(struct interface_info *ifi) * link_timeout we just go daemon and finish things up in the * background. */ - if (cur_time - ifi->startup_time < config->link_timeout) + if (cur_time < tickstop) { + if ((cmd_opts & OPT_VERBOSE) == 0) + tick_msg("lease", 0, tickstart, tickstop); ifi->interval = 1; - else { - if (isatty(STDERR_FILENO) != 0) - fprintf(stderr, "%s: no lease .... sleeping\n", - log_procname); - go_daemon(); + } else { + tick_msg("lease", 0, tickstart, tickstop); } /* @@ -2658,3 +2651,35 @@ lease_rebind(struct client_lease *lease) return lease->epoch + rebind; } + +void +tick_msg(const char *preamble, int success, time_t start, time_t stop) +{ + static int preamble_sent; + time_t cur_time; + + time(&cur_time); + + if (isatty(STDERR_FILENO) == 0 || cur_time < start) + return; + + if (preamble_sent == 0) { + fprintf(stderr, "%s: no %s ...", log_procname, preamble); + fflush(stderr); + preamble_sent = 1; + } + + if (success == 1) { + fprintf(stderr, " got %s\n", preamble); + fflush(stderr); + preamble_sent = 0; + } else if (cur_time < stop) { + fprintf(stderr, "."); + fflush(stderr); + } else { + fprintf(stderr, " sleeping\n"); + fflush(stderr); + go_daemon(); + preamble_sent = 0; + } +} -- 2.20.1