non-busybox dhcp clients (was: PATCH: ifupdown.c, udhcpc, and standalone shell)

Gabriel L. Somlo somlo at cmu.edu
Tue Oct 3 15:32:23 PDT 2006


> But the patch is _technically incorrect_, that's the point.
> [...]
> What if I have pump in /usr/local/bin? It will not work.
> 
> I _hate_ systems with scripts riddled with "/usr/bin/something" instead
> of just "something". $PATH is there for a reason, guys.

OK, how about this (still against svn 16294) ? It searches for each
client in $PATH, and executes it if it is found...

Thanks,
Gabriel


diff -NarU5 busybox-svn-16294.orig/networking/ifupdown.c busybox-svn-16294/networking/ifupdown.c
--- busybox-svn-16294.orig/networking/ifupdown.c	2006-10-03 13:01:04.000000000 -0400
+++ busybox-svn-16294/networking/ifupdown.c	2006-10-03 18:25:59.000000000 -0400
@@ -448,47 +448,85 @@
 	result += execute("ifconfig %iface% down", ifd, exec);
 #endif
 	return ((result == 2) ? 2 : 0);
 }
 
-static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
+#ifndef CONFIG_APP_UDHCPC
+struct dhcp_client_t
 {
-	if (execute("udhcpc -R -n -p /var/run/udhcpc.%iface%.pid -i %iface% "
-			"[[-H %hostname%]] [[-c %clientid%]] [[-s %script%]]", ifd, exec))
-		return 1;
+	char *name;
+	char *startcmd;
+	char *stopcmd;
+};
+
+static const struct dhcp_client_t ext_dhcp_clients[] = {
+	{ "udhcpc",
+		"udhcpc -R -n -p /var/run/udhcpc.%iface%.pid -i %iface% [[-H %hostname%]] [[-c %clientid%]] [[-s %script%]]",
+		"kill -TERM `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
+	},
+	{ "pump",
+		"pump -i %iface% [[-h %hostname%]] [[-l %leasehours%]]",
+		"pump -i %iface% -k",
+	},
+	{ "dhclient",
+		"dhclient -pf /var/run/dhclient.%iface%.pid %iface%",
+		"kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null",
+	},
+	{ "dhcpcd",
+		"dhcpcd [[-h %hostname%]] [[-i %vendor%]] [[-I %clientid%]] [[-l %leasetime%]] %iface%",
+		"dhcpcd -k %iface%",
+	},
+};
 
-	/* 2006-09-30: The following are deprecated, and should eventually be
-	 * removed. For non-busybox (i.e., other than udhcpc) clients, use
-	 * 'iface foo inet manual' in /etc/network/interfaces, and supply
-	 * start/stop commands explicitly via up/down. */
-
-	if (execute("pump -i %iface% [[-h %hostname%]] [[-l %leasehours%]]",
-			ifd, exec)) return 1;
-	if (execute("dhclient -pf /var/run/dhclient.%iface%.pid %iface%",
-			ifd, exec)) return 1;
-	if (execute("dhcpcd [[-h %hostname%]] [[-i %vendor%]] [[-I %clientid%]] "
-			"[[-l %leasetime%]] %iface%", ifd, exec)) return 1;
+static int execable(char *program)
+{
+    struct stat buf;
+	char *path = xstrdup(getenv("PATH"));
+	char *p;
+	int found = 0;
+	for (p = strtok(path, ":"); p && !found; p = strtok(NULL, ":")) {
+		p = xasprintf("%s/%s", p, program);
+		found = (stat(p, &buf) == 0) &&
+				S_ISREG(buf.st_mode) && (S_IXUSR & buf.st_mode);
+		free(p);
+	}
+	free(path);
+    return found;
+}
+#endif
 
+static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
+{
+#ifdef CONFIG_APP_UDHCPC
+	return execute("udhcpc -R -n -p /var/run/udhcpc.%iface%.pid "
+			"-i %iface% [[-H %hostname%]] [[-c %clientid%]] [[-s %script%]]",
+			ifd, exec);
+#else
+	int i, nclients = sizeof(ext_dhcp_clients) / sizeof(struct dhcp_client_t);
+	for (i = 0; i < nclients; i++) {
+		if (execable(ext_dhcp_clients[i].name))
+			return execute(ext_dhcp_clients[i].startcmd, ifd, exec);
+	}
+	bb_error_msg("No dhcp clients found.");
 	return 0;
+#endif
 }
 
 static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
 {
-	if (execute("kill -TERM `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
-			ifd, exec)) return 1;
-
-	/* 2006-09-30: The following are deprecated, and should eventually be
-	 * removed. For non-busybox (i.e., other than udhcpc) clients, use
-	 * 'iface foo inet manual' in /etc/network/interfaces, and supply
-	 * start/stop commands explicitly via up/down. */
-
-	if (execute("pump -i %iface% -k", ifd, exec)) return 1;
-	if (execute("kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null",
-			ifd, exec)) return 1;
-	if (execute("dhcpcd -k %iface%", ifd, exec)) return 1;
-
+#ifdef CONFIG_APP_UDHCPC
+	return execute("kill -TERM "
+			"`cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
+#else
+	int i, nclients = sizeof(ext_dhcp_clients) / sizeof(struct dhcp_client_t);
+	for (i = 0; i < nclients; i++) {
+		if (execable(ext_dhcp_clients[i].name))
+			return execute(ext_dhcp_clients[i].stopcmd, ifd, exec);
+	}
+	bb_error_msg("No dhcp clients found. Using static interface shutdown.");
 	return static_down(ifd, exec);
+#endif
 }
 
 static int manual_up_down(struct interface_defn_t *ifd, execfn *exec)
 {
 	return 1;


More information about the busybox mailing list