From: Roy Marples Date: Mon, 14 May 2007 10:01:56 +0000 (+0000) Subject: Don't overrwite .info files when dropping if they already exist X-Git-Tag: v3.2.3~255 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cff1ccb615d01ab1c68fc7c78d520eb49f6a47ab;p=thirdparty%2Fdhcpcd.git Don't overrwite .info files when dropping if they already exist --- diff --git a/client.c b/client.c index cf51ff4e..f210554e 100644 --- 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; } diff --git a/configure.c b/configure.c index 8a178dd5..16093b88 100644 --- a/configure.c +++ b/configure.c @@ -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); } diff --git a/configure.h b/configure.h index ddc709f9..e14abc0e 100644 --- a/configure.h +++ b/configure.h @@ -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 014be535..27cfe326 100644 --- 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 129e740a..3c9650b9 100644 --- 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