dhclient - Tweak leases file handling.
authorAntonio Huete Jimenez <tuxillo@quantumachine.net>
Sat, 4 Aug 2012 18:36:23 +0000 (20:36 +0200)
committerAntonio Huete Jimenez <tuxillo@quantumachine.net>
Sun, 19 Aug 2012 14:30:48 +0000 (16:30 +0200)
- Write out new leases file every time a lease is obtained. i.e. don't
  append 20 leases before cleaning it up.
- Write new leases file after calling dhclient-script to implement
  new info. Gets interface configured first, and makes it more likely
  the leases file reflects most current configuration.

Taken-from: OpenBSD

sbin/dhclient/dhclient.c
sbin/dhclient/dhcpd.h

index 2f5b3ca..816ba2c 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: src/sbin/dhclient/dhclient.c,v 1.141 2011/05/11 14:38:36 krw Exp $    */
+/*     $OpenBSD: src/sbin/dhclient/dhclient.c,v 1.142 2011/12/10 15:55:43 krw Exp $    */
 
 /*
  * Copyright 2004 Henning Brauer <henning@openbsd.org>
 
 /*
  * Copyright 2004 Henning Brauer <henning@openbsd.org>
@@ -643,9 +643,6 @@ dhcpack(struct iaddr client_addr, struct option_data *options)
 void
 bind_lease(void)
 {
 void
 bind_lease(void)
 {
-       /* Write out the new lease. */
-       write_client_lease(client->new, 0);
-
        /* Run the client script with the new parameters. */
        script_init((client->state == S_REQUESTING ? "BOUND" :
            (client->state == S_RENEWING ? "RENEW" :
        /* Run the client script with the new parameters. */
        script_init((client->state == S_REQUESTING ? "BOUND" :
            (client->state == S_RENEWING ? "RENEW" :
@@ -661,6 +658,9 @@ bind_lease(void)
        client->active = client->new;
        client->new = NULL;
 
        client->active = client->new;
        client->new = NULL;
 
+       /* Write out new leases file. */
+       rewrite_client_leases();
+
        /* Set up a timeout to start the renewal process. */
        add_timeout(client->active->renewal, state_bound);
 
        /* Set up a timeout to start the renewal process. */
        add_timeout(client->active->renewal, state_bound);
 
@@ -1371,10 +1371,15 @@ rewrite_client_leases(void)
        fflush(leaseFile);
        rewind(leaseFile);
 
        fflush(leaseFile);
        rewind(leaseFile);
 
-       for (lp = client->leases; lp; lp = lp->next)
-               write_client_lease(lp, 1);
+       for (lp = client->leases; lp; lp = lp->next) {
+               if (client->active && addr_eq(lp->address,
+                       client->active->address))
+                       continue;
+               write_client_lease(lp);
+       }
+
        if (client->active)
        if (client->active)
-               write_client_lease(client->active, 1);
+               write_client_lease(client->active);
 
        fflush(leaseFile);
        ftruncate(fileno(leaseFile), ftello(leaseFile));
 
        fflush(leaseFile);
        ftruncate(fileno(leaseFile), ftello(leaseFile));
@@ -1382,19 +1387,11 @@ rewrite_client_leases(void)
 }
 
 void
 }
 
 void
-write_client_lease(struct client_lease *lease, int rewrite)
+write_client_lease(struct client_lease *lease)
 {
 {
-       static int leases_written;
        struct tm *t;
        int i;
 
        struct tm *t;
        int i;
 
-       if (!rewrite) {
-               if (leases_written++ > 20) {
-                       rewrite_client_leases();
-                       leases_written = 0;
-               }
-       }
-
        /* If the lease came from the config file, we don't need to stash
           a copy in the lease database. */
        if (lease->is_static)
        /* If the lease came from the config file, we don't need to stash
           a copy in the lease database. */
        if (lease->is_static)
index 75f4332..2397f33 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: src/sbin/dhclient/dhcpd.h,v 1.73 2011/05/11 14:38:36 krw Exp $        */
+/*     $OpenBSD: src/sbin/dhclient/dhcpd.h,v 1.74 2011/12/10 15:55:43 krw Exp $        */
 
 /*
  * Copyright (c) 2004 Henning Brauer <henning@openbsd.org>
 
 /*
  * Copyright (c) 2004 Henning Brauer <henning@openbsd.org>
@@ -303,7 +303,7 @@ void make_decline(struct client_lease *);
 
 void free_client_lease(struct client_lease *);
 void rewrite_client_leases(void);
 
 void free_client_lease(struct client_lease *);
 void rewrite_client_leases(void);
-void write_client_lease(struct client_lease *, int);
+void write_client_lease(struct client_lease *);
 
 void    priv_script_init(char *);
 void    priv_script_write_params(char *, struct client_lease *);
 
 void    priv_script_init(char *);
 void    priv_script_write_params(char *, struct client_lease *);