From: Roy Marples Date: Sat, 3 May 2014 10:17:48 +0000 (+0000) Subject: Check secondaries value before writing it as /proc could be read-only. X-Git-Tag: v6.4.0~70 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=17d810346d07744bb8ec8d5adadd31ddb87140ea;p=thirdparty%2Fdhcpcd.git Check secondaries value before writing it as /proc could be read-only. --- diff --git a/if-linux.c b/if-linux.c index 553669ab..866da57e 100644 --- a/if-linux.c +++ b/if-linux.c @@ -160,11 +160,39 @@ if_machinearch(char *str, size_t len) return -1; } +static int +check_proc_int(const char *path) +{ + FILE *fp; + int i; + + fp = fopen(path, "r"); + if (fp == NULL) + return -1; + if (fscanf(fp, "%d", &i) != 1) + i = -1; + fclose(fp); + return i; +} + +static ssize_t +write_path(const char *path, const char *val) +{ + FILE *fp; + ssize_t r; + + fp = fopen(path, "w"); + if (fp == NULL) + return -1; + r = fprintf(fp, "%s\n", val); + fclose(fp); + return r; +} + int if_init(struct interface *ifp) { char path[sizeof(PROC_PROMOTE) + IF_NAMESIZE]; - FILE *fp; int n; /* We enable promote_secondaries so that we can do this @@ -175,29 +203,27 @@ if_init(struct interface *ifp) * This matches the behaviour of BSD which makes coding dhcpcd * a little easier as there's just one behaviour. */ snprintf(path, sizeof(path), PROC_PROMOTE, ifp->name); - - fp = fopen(path, "w"); - if (fp == NULL) + n = check_proc_int(path); + if (n == -1) return errno == ENOENT ? 0 : -1; - n = fprintf(fp, "1"); - fclose(fp); - return n == -1 ? -1 : 0; + if (n == 1) + return 0; + return write_path(path, "1") == -1 ? -1 : 0; } int if_conf(struct interface *ifp) { - char path[sizeof(SYS_LAYER2) + IF_NAMESIZE], buf[1]; - FILE *fp; + char path[sizeof(SYS_LAYER2) + IF_NAMESIZE]; + int n; /* Some qeth setups require the use of the broadcast flag. */ snprintf(path, sizeof(path), SYS_LAYER2, ifp->name); - fp = fopen(path, "r"); - if (fp == NULL) + n = check_proc_int(path); + if (n == -1) return errno == ENOENT ? 0 : -1; - if (fgets(buf, sizeof(buf), fp) != NULL && buf[0] == '0') + if (n == 0) ifp->options->options |= DHCPCD_BROADCAST; - fclose(fp); return 0; } @@ -1137,35 +1163,6 @@ if_addrflags6(const char *ifname, const struct in6_addr *addr) return -1; } -static int -check_proc_int(const char *path) -{ - FILE *fp; - int i; - - fp = fopen(path, "r"); - if (fp == NULL) - return -1; - if (fscanf(fp, "%d", &i) != 1) - i = -1; - fclose(fp); - return i; -} - -static ssize_t -write_path(const char *path, const char *val) -{ - FILE *fp; - ssize_t r; - - fp = fopen(path, "w"); - if (fp == NULL) - return -1; - r = fprintf(fp, "%s\n", val); - fclose(fp); - return r; -} - static const char *prefix = "/proc/sys/net/ipv6/conf"; void