PATCH: ifupdown and /var/run/ifstate file

Gabriel L. Somlo somlo at cmu.edu
Mon Mar 5 21:58:58 UTC 2007


Hi List,

ifupdown.c records the status of the interfaces it works on in the
/var/run/ifstate file.

Until a while ago, it used to read this file at startup and build a
list (state_list) based on its contents, before actually trying to
make changes to interfaces. When an interface was marked as 'up' in
the state file, ifup would cowardly refuse to bring it up *again*.
Similar situation with ifdown and an interface marked as already
unconfigured.

While ifupdown still writes the /var/run/ifstate before exiting, it no
longer reads it on startup. I wonder whether that's intentional, or
was removed accidentally as part of some cleanup operation.

If unintentional, please apply the enclosed patch to put the
functionality back in place.

Thanks much,
Gabriel


diff -NarU5 busybox-svn-17982.orig/networking/ifupdown.c busybox-svn-17982/networking/ifupdown.c
--- busybox-svn-17982.orig/networking/ifupdown.c	2007-02-27 22:45:19.000000000 -0500
+++ busybox-svn-17982/networking/ifupdown.c	2007-03-05 13:53:52.000000000 -0500
@@ -1089,10 +1089,12 @@
 	int (*cmds)(struct interface_defn_t *) = NULL;
 	struct interfaces_file_t *defn;
 	llist_t *state_list = NULL;
 	llist_t *target_list = NULL;
 	const char *interfaces = "/etc/network/interfaces";
+	const char *statefile = "/var/run/ifstate";
+	FILE *state_fp;
 	int any_failures = 0;
 
 	cmds = iface_down;
 	if (applet_name[2] == 'u') {
 		/* ifup command */
@@ -1115,10 +1117,23 @@
 	}
 
 	startup_PATH = getenv("PATH");
 	if (!startup_PATH) startup_PATH = "";
 
+	/* Read the previous state from the state file */
+	state_fp = fopen_or_warn(statefile, "r");
+	if (state_fp) {
+		char *start, *end_ptr;
+		while ((start = xmalloc_fgets(state_fp)) != NULL) {
+			/* We should only need to check for a single character */
+			end_ptr = start + strcspn(start, " \t\n");
+			*end_ptr = '\0';
+			llist_add_to(&state_list, start);
+		}
+		fclose(state_fp);
+	}
+
 	/* Create a list of interfaces to work on */
 	if (DO_ALL) {
 		if (cmds == iface_up) {
 			target_list = defn->autointerfaces;
 		} else {
@@ -1164,11 +1179,11 @@
 					bb_error_msg("interface %s already configured", iface);
 					continue;
 				}
 			} else {
 				/* ifdown */
-				if (iface_state) {
+				if (!iface_state) {
 					bb_error_msg("interface %s not configured", iface);
 					continue;
 				}
 			}
 		}
@@ -1242,13 +1257,11 @@
 		}
 	}
 
 	/* Actually write the new state */
 	if (!NO_ACT) {
-		FILE *state_fp;
-
-		state_fp = xfopen("/var/run/ifstate", "w");
+		state_fp = xfopen(statefile, "w");
 		while (state_list) {
 			if (state_list->data) {
 				fprintf(state_fp, "%s\n", state_list->data);
 			}
 			state_list = state_list->link;



More information about the busybox mailing list