Hi Denis,

attached is a cosmetic (+ a bit smaller code) patch for ifplugd.
Please review and consider usage.

--
Max
diff --git a/networking/ifplugd.c b/networking/ifplugd.c
index eb74428..f5d39c3 100644
--- a/networking/ifplugd.c
+++ b/networking/ifplugd.c
@@ -72,14 +72,14 @@ enum {
 #endif
 
 enum { // api mode
-	API_AUTO	= 'a',
-	API_ETHTOOL	= 'e',
-	API_MII		= 'm',
-	API_PRIVATE	= 'p',
-	API_WLAN	= 'w',
-	API_IFF		= 'i',
+	API_ETHTOOL, // 'e'
+	API_MII,     // 'm'
+	API_PRIVATE, // 'p'
+	API_WLAN,    // 'w'
+	API_IFF,     // 'i'
+	API_AUTO,    // 'a'
 };
-static const char api_modes[] ALIGN1 = "aempwi";
+static const char api_modes[] ALIGN1 = "empwia";
 
 enum { // interface status
 	IFSTATUS_ERR = -1,
@@ -107,8 +107,6 @@ struct globals {
 	const char *api_mode;
 	const char *script_name;
 	const char *extra_arg;
-
-	smallint (*detect_link_func)(void);
 };
 #define G (*ptr_to_globals)
 #define INIT_G() do { \
@@ -240,8 +238,8 @@ static void maybe_up_new_iface(void)
 			G.iface, buf, driver_info.driver, driver_info.version);
 	}
 #endif
-	if (G.api_method_num == 0)
-		G.detect_link_func = NULL;
+	if (G.api_mode[0] == 'a')
+		G.api_method_num = API_AUTO;
 }
 
 static smallint detect_link_mii(void)
@@ -373,38 +371,34 @@ static smallint detect_link(void)
 	if (!(option_mask32 & FLAG_NO_AUTO))
 		up_iface();
 
-	if (!G.detect_link_func) {
-		if (G.api_method_num == 0) {
-			int i;
-			smallint sv_logmode;
-
-			sv_logmode = logmode;
-			for (i = 0; i < ARRAY_SIZE(method); i++) {
-				logmode = LOGMODE_NONE;
-				status = method[i].func();
-				logmode = sv_logmode;
-				if (status != IFSTATUS_ERR) {
-					G.detect_link_func = method[i].func;
-					bb_error_msg("using %s detection mode", method[i].name);
-					goto _2;
-				}
+	if (G.api_method_num == API_AUTO) {
+		int i;
+		smallint sv_logmode;
+
+		sv_logmode = logmode;
+		for (i = 0; i < ARRAY_SIZE(method); i++) {
+			logmode = LOGMODE_NONE;
+			status = method[i].func();
+			logmode = sv_logmode;
+			if (status != IFSTATUS_ERR) {
+				G.api_method_num = i;
+				bb_error_msg("using %s detection mode", method[i].name);
+				break;
 			}
-			goto _1;
 		}
-		G.detect_link_func = method[G.api_method_num - 1].func;
+	} else {
+		status = method[G.api_method_num].func();
 	}
 
-	status = G.detect_link_func();
- _1:
 	if (status == IFSTATUS_ERR) {
 		if (option_mask32 & FLAG_IGNORE_FAIL)
 			status = IFSTATUS_DOWN;
 		else if (option_mask32 & FLAG_IGNORE_FAIL_POSITIVE)
 			status = IFSTATUS_UP;
-		else if (G.api_method_num == 0)
+		else if (G.api_mode[0] == 'a')
 			bb_error_msg("can't detect link status");
 	}
- _2:
+
 	if (status != G.iface_last_status) {
 		G.iface_prev_status = G.iface_last_status;
 		G.iface_last_status = status;
@@ -475,23 +469,6 @@ static NOINLINE int check_existence_through_netlink(void)
 	return G.iface_exists;
 }
 
-static NOINLINE int netlink_open(void)
-{
-	int fd;
-	struct sockaddr_nl addr;
-
-	fd = xsocket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
-
-	memset(&addr, 0, sizeof(addr));
-	addr.nl_family = AF_NETLINK;
-	addr.nl_groups = RTMGRP_LINK;
-	addr.nl_pid = getpid();
-
-	xbind(fd, (struct sockaddr*)&addr, sizeof(addr));
-
-	return fd;
-}
-
 #if ENABLE_FEATURE_PIDFILE
 static NOINLINE pid_t read_pid(const char *filename)
 {
@@ -545,6 +522,7 @@ int ifplugd_main(int argc UNUSED_PARAM, char **argv)
 	if (pid_from_pidfile > 0 && kill(pid_from_pidfile, 0) == 0)
 		bb_error_msg_and_die("daemon already running");
 #endif
+
 	api_mode_found = strchr(api_modes, G.api_mode[0]);
 	if (!api_mode_found)
 		bb_error_msg_and_die("unknown API mode '%s'", G.api_mode);
@@ -555,7 +533,16 @@ int ifplugd_main(int argc UNUSED_PARAM, char **argv)
 
 	xmove_fd(xsocket(AF_INET, SOCK_DGRAM, 0), ioctl_fd);
 	if (opts & FLAG_MONITOR) {
-		xmove_fd(netlink_open(), netlink_fd);
+		struct sockaddr_nl addr;
+		int fd = xsocket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
+
+		memset(&addr, 0, sizeof(addr));
+		addr.nl_family = AF_NETLINK;
+		addr.nl_groups = RTMGRP_LINK;
+		addr.nl_pid = getpid();
+
+		xbind(fd, (struct sockaddr*)&addr, sizeof(addr));
+		xmove_fd(fd, netlink_fd);
 	}
 
 	write_pidfile(pidfile_name);
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to