From: Roy Marples Date: Tue, 24 Aug 2010 09:16:07 +0000 (+0000) Subject: -U, --dumplease now dumps the lease for the specified interface X-Git-Tag: v5.2.9~35 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=dc60cba4ed8b4c63b1329f03027e0f65458fbb05;p=thirdparty%2Fdhcpcd.git -U, --dumplease now dumps the lease for the specified interface dhcpcd does not have to be running for this to work --- diff --git a/dhcp.c b/dhcp.c index fa740a89..8a61a0f8 100644 --- a/dhcp.c +++ b/dhcp.c @@ -1052,7 +1052,7 @@ write_lease(const struct interface *iface, const struct dhcp_message *dhcp) syslog(LOG_DEBUG, "%s: writing lease `%s'", iface->name, iface->leasefile); - fd = open(iface->leasefile, O_WRONLY | O_CREAT | O_TRUNC, 0400); + fd = open(iface->leasefile, O_WRONLY | O_CREAT | O_TRUNC, 0444); if (fd == -1) { syslog(LOG_ERR, "%s: open: %m", iface->name); return -1; diff --git a/dhcpcd-hooks/01-test b/dhcpcd-hooks/01-test index b46d4005..b594e93f 100644 --- a/dhcpcd-hooks/01-test +++ b/dhcpcd-hooks/01-test @@ -2,5 +2,7 @@ if [ "$reason" = "TEST" ]; then set | grep "^\(interface\|metric\|pid\|reason\|skip_hooks\)=" | sort +fi +if [ "$reason" = "TEST" -o "$reason" = "DUMP" ]; then set | grep "^\(new_\|old_\)" | sort fi diff --git a/dhcpcd-run-hooks.8.in b/dhcpcd-run-hooks.8.in index c773b5e2..a893669f 100644 --- a/dhcpcd-run-hooks.8.in +++ b/dhcpcd-run-hooks.8.in @@ -1,4 +1,4 @@ -.\" Copyright (c) 2006-2009 Roy Marples +.\" Copyright (c) 2006-2010 Roy Marples .\" All rights reserved .\" .\" Redistribution and use in source and binary forms, with or without @@ -22,7 +22,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd March 23, 2009 +.Dd August 24, 2010 .Dt DHCPCD-RUN-HOOKS 8 SMM .Os .Sh NAME @@ -110,11 +110,14 @@ means it cannot work as a DHCP or ZeroConf client. Static configuration and DHCP INFORM is still allowed. .It Dv STOP dhcpcd stopped running on the interface. +.iT Dv DUMP +dhcpcd has been asked to dump the last lease for the interface. .It Dv TEST dhcpcd received an OFFER from a DHCP server but will not configure the interface. This is primarily used to test the variables are filled correctly for the script to process them. + .El .Sh FILES When diff --git a/dhcpcd.8.in b/dhcpcd.8.in index ecd147de..b966746b 100644 --- a/dhcpcd.8.in +++ b/dhcpcd.8.in @@ -22,7 +22,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd June 9, 2010 +.Dd August 24, 2010 .Dt DHCPCD 8 SMM .Os .Sh NAME @@ -61,6 +61,9 @@ .Fl k , -release .Op interface .Nm +.Fl U, -dumplease +.Ar interface +.Nm .Fl x , -exit .Op interface .Sh DESCRIPTION @@ -488,6 +491,10 @@ files. To test INFORM the interface needs to be configured with the desired address before starting .Nm . +.It Fl U, -dumplease Ar interface +Dumps the last lease for the +.Ar interface +to stdout. .It Fl V, -variables Display a list of option codes and the associated variable for use in .Xr dhcpcd-run-hooks 8 . diff --git a/dhcpcd.c b/dhcpcd.c index 3759722d..fe5fa1a7 100644 --- a/dhcpcd.c +++ b/dhcpcd.c @@ -1707,6 +1707,9 @@ main(int argc, char **argv) case 'T': i = 1; break; + case 'U': + i = 2; + break; case 'V': print_options(); exit(EXIT_SUCCESS); @@ -1727,7 +1730,11 @@ main(int argc, char **argv) } options = ifo->options; if (i != 0) { - options |= DHCPCD_TEST | DHCPCD_PERSISTENT; + if (i == 1) + options |= DHCPCD_TEST; + else + options |= DHCPCD_DUMPLEASE; + options |= DHCPCD_PERSISTENT; options &= ~DHCPCD_DAEMONISE; } @@ -1740,7 +1747,7 @@ main(int argc, char **argv) if (options & DHCPCD_QUIET) close(STDERR_FILENO); - if (!(options & DHCPCD_TEST)) { + if (!(options & (DHCPCD_TEST | DHCPCD_DUMPLEASE))) { /* If we have any other args, we should run as a single dhcpcd * instance for that interface. */ len = strlen(PIDFILE) + IF_NAMESIZE + 2; @@ -1757,7 +1764,29 @@ main(int argc, char **argv) syslog(LOG_ERR, "chdir `/': %m"); atexit(cleanup); - if (!(options & (DHCPCD_MASTER | DHCPCD_TEST))) { + if (options & DHCPCD_DUMPLEASE) { + if (optind != argc - 1) { + syslog(LOG_ERR, "dumplease requires an interface"); + exit(EXIT_FAILURE); + } + iface = xzalloc(sizeof(*iface)); + strlcpy(iface->name, argv[optind], sizeof(iface->name)); + snprintf(iface->leasefile, sizeof(iface->leasefile), + LEASEFILE, iface->name); + iface->state = xzalloc(sizeof(*iface->state)); + select_profile(iface, NULL); + add_options(iface->state->options, argc, argv); + iface->state->new = read_lease(iface); + if (iface->state->new == NULL && errno == ENOENT) { + syslog(LOG_ERR, "%s: no lease to dump", iface->name); + exit(EXIT_FAILURE); + } + iface->state->reason = "DUMP"; + run_script(iface); + exit(EXIT_SUCCESS); + } + + if (!(options & (DHCPCD_MASTER | DHCPCD_TEST | DHCPCD_DUMPLEASE))) { control_fd = open_control(); if (control_fd != -1) { syslog(LOG_INFO, diff --git a/if-options.c b/if-options.c index da7f6145..3367b415 100644 --- a/if-options.c +++ b/if-options.c @@ -95,6 +95,7 @@ const struct option cf_options[] = { {"require", required_argument, NULL, 'Q'}, {"static", required_argument, NULL, 'S'}, {"test", no_argument, NULL, 'T'}, + {"dumplease", no_argument, NULL, 'U'}, {"variables", no_argument, NULL, 'V'}, {"whitelist", required_argument, NULL, 'W'}, {"blacklist", required_argument, NULL, 'X'}, @@ -336,7 +337,8 @@ parse_option(struct if_options *ifo, int opt, const char *arg) case 'g': /* FALLTHROUGH */ case 'n': /* FALLTHROUGH */ case 'x': /* FALLTHROUGH */ - case 'T': /* We need to handle non interface options */ + case 'T': /* FALLTHROUGH */ + case 'U': /* We need to handle non interface options */ break; case 'b': ifo->options |= DHCPCD_BACKGROUND; diff --git a/if-options.h b/if-options.h index 86c101e8..241cb4d2 100644 --- a/if-options.h +++ b/if-options.h @@ -37,7 +37,7 @@ /* Don't set any optional arguments here so we retain POSIX * compatibility with getopt */ -#define IF_OPTS "bc:de:f:gh:i:kl:m:no:pqr:s:t:u:v:wxy:z:ABC:DEF:GHI:JKLO:Q:S:TVW:X:Z:" +#define IF_OPTS "bc:de:f:gh:i:kl:m:no:pqr:s:t:u:v:wxy:z:ABC:DEF:GHI:JKLO:Q:S:TUVW:X:Z:" #define DEFAULT_TIMEOUT 30 #define DEFAULT_REBOOT 10 @@ -76,6 +76,7 @@ #define DHCPCD_CSR_WARNED (1 << 27) #define DHCPCD_XID_HWADDR (1 << 28) #define DHCPCD_BROADCAST (1 << 29) +#define DHCPCD_DUMPLEASE (1 << 30) extern const struct option cf_options[];