changeset 2467:56b4420b0d71 draft

Check secondaries value before writing it as /proc could be read-only.
author Roy Marples <roy@marples.name>
date Sat, 03 May 2014 10:17:48 +0000
parents b02e207610c3
children ec51834c005f
files if-linux.c
diffstat 1 files changed, 39 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/if-linux.c	Sat May 03 00:12:19 2014 +0000
+++ b/if-linux.c	Sat May 03 10:17:48 2014 +0000
@@ -160,11 +160,39 @@
 	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 @@
 	 * 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 @@
 	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