]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
-U, --dumplease now dumps the lease for the specified interface
authorRoy Marples <roy@marples.name>
Tue, 24 Aug 2010 09:16:07 +0000 (09:16 +0000)
committerRoy Marples <roy@marples.name>
Tue, 24 Aug 2010 09:16:07 +0000 (09:16 +0000)
dhcpcd does not have to be running for this to work

dhcp.c
dhcpcd-hooks/01-test
dhcpcd-run-hooks.8.in
dhcpcd.8.in
dhcpcd.c
if-options.c
if-options.h

diff --git a/dhcp.c b/dhcp.c
index fa740a89036ed42c8e94af0c73705b9bc32f40ff..8a61a0f857ac715455cd2984f3c31d2f52dab759 100644 (file)
--- 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;
index b46d40050bf2b2df6f760246634c4786ba503d62..b594e93f3f86b5f4e4349ed337c7972288126ae3 100644 (file)
@@ -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
index c773b5e20d0b8adec795bea630642ce048121842..a893669f8e2da084b09fddc1c39f4522e35a698c 100644 (file)
@@ -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
index ecd147de27ebe12f9de5226f875901728fd5b962..b966746b4ad4cdb79b3e164e3de2eadf8eb4e283 100644 (file)
@@ -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 .
index 3759722d00d4811bd7dc2123538ed5e52e5184c2..fe5fa1a7c097a7067b9af3b2718b42b8dd54987c 100644 (file)
--- 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,
index da7f6145c4df66b1579b00249c32ab84e0e83606..3367b4154343ab4924bd60aced11c470dc61701f 100644 (file)
@@ -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;
index 86c101e8bbe12040dfe68eb4131cd7ca3c52a19b..241cb4d2060a8da5e124e0bbf258bd357eea0d30 100644 (file)
@@ -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[];