]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Don't overrwite .info files when dropping if they already exist
authorRoy Marples <roy@marples.name>
Mon, 14 May 2007 10:01:56 +0000 (10:01 +0000)
committerRoy Marples <roy@marples.name>
Mon, 14 May 2007 10:01:56 +0000 (10:01 +0000)
client.c
configure.c
configure.h
info.c
info.h

index cf51ff4eaed0b5f2ab22035b946b280eeb486e75..f210554e1474736fedd5137dfb30cdcd01378f3f 100644 (file)
--- a/client.c
+++ b/client.c
@@ -95,9 +95,8 @@
 }
 
 #define DROP_CONFIG { \
-       memset (&dhcp->address, 0, sizeof (struct in_addr)); \
        if (! options->persistent) \
-       configure (options, iface, dhcp); \
+       configure (options, iface, dhcp, false); \
        free_dhcp (dhcp); \
        memset (dhcp, 0, sizeof (dhcp_t)); \
 }
@@ -375,7 +374,7 @@ int dhcp_run (const options_t *options, int *pidfd)
                                                        } else {
                                                                logger (LOG_INFO, "using last known IP address %s",
                                                                                inet_ntoa (dhcp->address));
-                                                               if (configure (options, iface, dhcp)) {
+                                                               if (configure (options, iface, dhcp, true)) {
                                                                        retval = EXIT_FAILURE;
                                                                        goto eexit;
                                                                }
@@ -651,7 +650,7 @@ int dhcp_run (const options_t *options, int *pidfd)
 
                                                xid = 0;
 
-                                               if (configure (options, iface, dhcp) < 0 && ! daemonised) {
+                                               if (configure (options, iface, dhcp, true) < 0 && ! daemonised) {
                                                        retval = EXIT_FAILURE;
                                                        goto eexit;
                                                }
index 8a178dd58b20c58cf120b2f62d4239349b01a074..16093b8875a830225ff00d09c0f252528f6839b4 100644 (file)
@@ -65,7 +65,7 @@ static int exec_cmd (const char *cmd, const char *args, ...)
        argv = alloca ((n + 1) * sizeof (*argv));
        if (argv == NULL) {
                errno = ENOMEM;
-               return -1;
+               return (-1);
        }
 
        va_start (va, args);
@@ -84,7 +84,7 @@ static int exec_cmd (const char *cmd, const char *args, ...)
        } else if (pid == -1)
                logger (LOG_ERR, "vfork: %s", strerror (errno));
 
-       return 0;
+       return (0);
 }
 
 static void exec_script (const char *script, const char *infofile,
@@ -128,27 +128,27 @@ static int make_resolv (const char *ifname, const dhcp_t *dhcp)
                        logger (LOG_ERR, "fopen `%s': %s", RESOLVFILE, strerror (errno));
        }
 
-       if (f) {
-               fprintf (f, "# Generated by dhcpcd for interface %s\n", ifname);
-               if (dhcp->dnssearch)
-                       fprintf (f, "search %s\n", dhcp->dnssearch);
-               else if (dhcp->dnsdomain) {
-                       fprintf (f, "search %s\n", dhcp->dnsdomain);
-               }
+       if (! f)
+               return (-1);
 
-               for (address = dhcp->dnsservers; address; address = address->next)
-                       fprintf (f, "nameserver %s\n", inet_ntoa (address->address));
+       fprintf (f, "# Generated by dhcpcd for interface %s\n", ifname);
+       if (dhcp->dnssearch)
+               fprintf (f, "search %s\n", dhcp->dnssearch);
+       else if (dhcp->dnsdomain) {
+               fprintf (f, "search %s\n", dhcp->dnsdomain);
+       }
 
-               if (*resolvconf)
-                       pclose (f);
-               else
-                       fclose (f);
-       } else
-               return -1;
+       for (address = dhcp->dnsservers; address; address = address->next)
+               fprintf (f, "nameserver %s\n", inet_ntoa (address->address));
+
+       if (*resolvconf)
+               pclose (f);
+       else
+               fclose (f);
 
        /* Refresh the local resolver */
        res_init ();
-       return 0;
+       return (0);
 }
 
 static void restore_resolv(const char *ifname)
@@ -319,7 +319,7 @@ static int make_nis (const char *ifname, const dhcp_t *dhcp)
 #endif
 
 int configure (const options_t *options, interface_t *iface,
-                          const dhcp_t *dhcp)
+                          const dhcp_t *dhcp, bool up)
 {
        route_t *route = NULL;
        route_t *new_route = NULL;
@@ -328,7 +328,10 @@ int configure (const options_t *options, interface_t *iface,
        char curhostname[MAXHOSTNAMELEN] = {0};
 
        if (! options || ! iface || ! dhcp)
-               return -1;
+               return (-1);
+
+       if (dhcp->address.s_addr == 0)
+               up = 0;
 
        /* Remove old routes
           Always do this as the interface may have >1 address not added by us
@@ -337,7 +340,7 @@ int configure (const options_t *options, interface_t *iface,
                for (route = iface->previous_routes; route; route = route->next)
                        if (route->destination.s_addr || options->dogateway) {
                                int have = 0;
-                               if (dhcp->address.s_addr != 0)
+                               if (up)
                                        for (new_route = dhcp->routes; new_route; new_route = new_route->next)
                                                if (new_route->destination.s_addr == route->destination.s_addr
                                                        && new_route->netmask.s_addr == route->netmask.s_addr
@@ -352,8 +355,8 @@ int configure (const options_t *options, interface_t *iface,
                        }
        }
 
-       /* If we don't have an address, then return */
-       if (dhcp->address.s_addr == 0) {
+       /* If we aren't up, then reset the interface as much as we can */
+       if (! up) {
                if (iface->previous_routes) {
                        free_route (iface->previous_routes);
                        iface->previous_routes = NULL;
@@ -367,8 +370,8 @@ int configure (const options_t *options, interface_t *iface,
 
 #ifdef ENABLE_INFO
                /* If we haven't created an info file, do so now */
-               if (! dhcp->frominfo && iface->previous_address.s_addr == 0)
-                       write_info (iface, dhcp, options);
+               if (! dhcp->frominfo)
+                       write_info (iface, dhcp, options, false);
 #endif
 
                /* Only reset things if we had set them before */
@@ -386,7 +389,7 @@ int configure (const options_t *options, interface_t *iface,
 
                exec_script (options->script, iface->infofile, "down");
 
-               return 0;
+               return (0);
        }
 
        /* Set the MTU requested.
@@ -406,7 +409,7 @@ int configure (const options_t *options, interface_t *iface,
        if (! options->doinform || ! has_address (iface->name, dhcp->address))
                if (add_address (iface->name, dhcp->address, dhcp->netmask,
                                                 dhcp->broadcast) < 0 && errno != EEXIST)
-                       return -1;
+                       return (false);
 
        /* Now delete the old address if different */
        if (iface->previous_address.s_addr != dhcp->address.s_addr &&
@@ -569,7 +572,7 @@ int configure (const options_t *options, interface_t *iface,
 
 #ifdef ENABLE_INFO
        if (! dhcp->frominfo)
-               write_info (iface, dhcp, options);
+               write_info (iface, dhcp, options, true);
 #endif
 
        if (iface->previous_address.s_addr != dhcp->address.s_addr ||
@@ -583,6 +586,6 @@ int configure (const options_t *options, interface_t *iface,
        } else
                exec_script (options->script, iface->infofile, "up");
 
-       return 0;
+       return (0);
 }
 
index ddc709f9820242893fa0542d07f029dd040b406b..e14abc0e043d21a9e4e8866ec13961948f541151 100644 (file)
@@ -27,6 +27,6 @@
 #include "dhcp.h"
 
 int configure (const options_t *options, interface_t *iface,
-                          const dhcp_t *dhcp);
+                          const dhcp_t *dhcp, bool up);
 
 #endif
diff --git a/info.c b/info.c
index 014be53550452492b9072cec13f11cc5436ff3ba..27cfe326c7017e3d15e5dbf02f99da20802bd8f9 100644 (file)
--- a/info.c
+++ b/info.c
@@ -65,11 +65,15 @@ static char *cleanmetas (const char *cstr)
 }
 
 bool write_info(const interface_t *iface, const dhcp_t *dhcp,
-                               const options_t *options)
+                               const options_t *options, bool overwrite)
 {
        FILE *f;
        route_t *route;
        address_t *address;
+       struct stat sb;
+
+       if (! overwrite && stat (iface->infofile, &sb) == 0)
+               return (true);
 
        logger (LOG_DEBUG, "writing %s", iface->infofile);
        if ((f = fopen (iface->infofile, "w")) == NULL) {
diff --git a/info.h b/info.h
index 129e740af8560c363398fda8c8417a829b944dfa..3c9650b9238e57b6344f41f0052cd6aa702bbcf3 100644 (file)
--- a/info.h
+++ b/info.h
@@ -28,7 +28,7 @@
 
 #ifdef ENABLE_INFO
 bool write_info (const interface_t *iface, const dhcp_t *dhcp,
-                                const options_t *options);
+                                const options_t *options, bool overwrite);
 
 bool read_info (const interface_t *iface, dhcp_t *dhcp);
 #endif