Hi Denis,
attached is a patch for ifplugd. Please review and consider usage.
Max.
diff --git a/networking/ifplugd.c b/networking/ifplugd.c
index eb74428..d977a67 100644
--- a/networking/ifplugd.c
+++ b/networking/ifplugd.c
@@ -71,32 +71,21 @@ enum {
# define OPTION_STR "+ansfFi:r:It:u:d:m:pqlx:M"
#endif
-enum { // api mode
- API_AUTO = 'a',
- API_ETHTOOL = 'e',
- API_MII = 'm',
- API_PRIVATE = 'p',
- API_WLAN = 'w',
- API_IFF = 'i',
-};
-static const char api_modes[] ALIGN1 = "aempwi";
-
-enum { // interface status
+enum {
+ // interface status
IFSTATUS_ERR = -1,
IFSTATUS_DOWN = 0,
IFSTATUS_UP = 1,
-};
-enum { // constant fds
- ioctl_fd = 3,
- netlink_fd = 4,
+ // constant fds
+ IOCTL_FD = 3,
+ NETLINK_FD = 4,
};
struct globals {
smallint iface_last_status;
smallint iface_prev_status;
smallint iface_exists;
- smallint api_method_num;
/* Used in getopt32, must have sizeof == sizeof(int) */
unsigned poll_time;
@@ -122,6 +111,25 @@ struct globals {
G.script_name = "/etc/ifplugd/ifplugd.action"; \
} while (0)
+static smallint detect_link_ethtool(void);
+static smallint detect_link_mii(void);
+static smallint detect_link_priv(void);
+static smallint detect_link_wlan(void);
+static smallint detect_link_iff(void);
+
+static const struct {
+ const char *name;
+ smallint (*func)(void);
+} api_methods[] = {
+ { "SIOCETHTOOL" , &detect_link_ethtool },
+ { "SIOCGMIIPHY" , &detect_link_mii },
+ { "SIOCDEVPRIVATE" , &detect_link_priv },
+ { "wireless extension", &detect_link_wlan },
+ { "IFF_RUNNING" , &detect_link_iff },
+ { "" , NULL },
+};
+
+static const char api_modes[] ALIGN1 = "empwia";
static const char *strstatus(int status)
{
@@ -163,16 +171,21 @@ static int run_script(const char *action)
static int network_ioctl(int request, void* data, const char *errmsg)
{
- int r = ioctl(ioctl_fd, request, data);
+ int r = ioctl(IOCTL_FD, request, data);
if (r < 0 && errmsg)
bb_perror_msg("%s failed", errmsg);
return r;
}
-static void set_ifreq_to_ifname(struct ifreq *ifreq)
+static void set_request_to_ifname(void *req, size_t sz)
{
- memset(ifreq, 0, sizeof(struct ifreq));
- strncpy_IFNAMSIZ(ifreq->ifr_name, G.iface);
+ struct req {
+ union {
+ char ifname[IFNAMSIZ];
+ } u;
+ };
+ memset(req, 0, sz);
+ strncpy_IFNAMSIZ(((struct req *)req)->u.ifname, G.iface);
}
static void up_iface(void)
@@ -182,7 +195,7 @@ static void up_iface(void)
if (!G.iface_exists)
return;
- set_ifreq_to_ifname(&ifrequest);
+ set_request_to_ifname(&ifrequest, sizeof(struct ifreq));
if (network_ioctl(SIOCGIFFLAGS, &ifrequest, "getting interface flags") < 0) {
G.iface_exists = 0;
return;
@@ -240,7 +253,7 @@ static void maybe_up_new_iface(void)
G.iface, buf, driver_info.driver, driver_info.version);
}
#endif
- if (G.api_method_num == 0)
+ if (G.api_mode[0] == 'a')
G.detect_link_func = NULL;
}
@@ -249,7 +262,7 @@ static smallint detect_link_mii(void)
struct ifreq ifreq;
struct mii_ioctl_data *mii = (void *)&ifreq.ifr_data;
- set_ifreq_to_ifname(&ifreq);
+ set_request_to_ifname(&ifreq, sizeof(struct ifreq));
if (network_ioctl(SIOCGMIIPHY, &ifreq, "SIOCGMIIPHY") < 0) {
return IFSTATUS_ERR;
@@ -269,7 +282,7 @@ static smallint detect_link_priv(void)
struct ifreq ifreq;
struct mii_ioctl_data *mii = (void *)&ifreq.ifr_data;
- set_ifreq_to_ifname(&ifreq);
+ set_request_to_ifname(&ifreq, sizeof(struct ifreq));
if (network_ioctl(SIOCDEVPRIVATE, &ifreq, "SIOCDEVPRIVATE") < 0) {
return IFSTATUS_ERR;
@@ -289,7 +302,7 @@ static smallint detect_link_ethtool(void)
struct ifreq ifreq;
struct ethtool_value edata;
- set_ifreq_to_ifname(&ifreq);
+ set_request_to_ifname(&ifreq, sizeof(struct ifreq));
edata.cmd = ETHTOOL_GLINK;
ifreq.ifr_data = (void*) &edata;
@@ -305,7 +318,7 @@ static smallint detect_link_iff(void)
{
struct ifreq ifreq;
- set_ifreq_to_ifname(&ifreq);
+ set_request_to_ifname(&ifreq, sizeof(struct ifreq));
if (network_ioctl(SIOCGIFFLAGS, &ifreq, "SIOCGIFFLAGS") < 0) {
return IFSTATUS_ERR;
@@ -326,17 +339,16 @@ static smallint detect_link_iff(void)
static smallint detect_link_wlan(void)
{
int i;
- struct iwreq iwrequest;
+ struct iwreq iwreq;
uint8_t mac[ETH_ALEN];
- memset(&iwrequest, 0, sizeof(iwrequest));
- strncpy_IFNAMSIZ(iwrequest.ifr_ifrn.ifrn_name, G.iface);
+ set_request_to_ifname(&iwreq, sizeof(struct iwreq));
- if (network_ioctl(SIOCGIWAP, &iwrequest, "SIOCGIWAP") < 0) {
+ if (network_ioctl(SIOCGIWAP, &iwreq, "SIOCGIWAP") < 0) {
return IFSTATUS_ERR;
}
- memcpy(mac, &iwrequest.u.ap_addr.sa_data, ETH_ALEN);
+ memcpy(mac, &iwreq.u.ap_addr.sa_data, ETH_ALEN);
if (mac[0] == 0xFF || mac[0] == 0x44 || mac[0] == 0x00) {
for (i = 1; i < ETH_ALEN; ++i) {
@@ -351,16 +363,6 @@ static smallint detect_link_wlan(void)
static smallint detect_link(void)
{
- static const struct {
- const char *name;
- smallint (*func)(void);
- } method[] = {
- { "SIOCETHTOOL" , &detect_link_ethtool },
- { "SIOCGMIIPHY" , &detect_link_mii },
- { "SIOCDEVPRIVATE" , &detect_link_priv },
- { "wireless extension", &detect_link_wlan },
- { "IFF_RUNNING" , &detect_link_iff },
- };
smallint status;
if (!G.iface_exists)
@@ -374,37 +376,34 @@ static smallint detect_link(void)
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;
- }
+ smallint sv_logmode, i;
+
+ sv_logmode = logmode;
+ for (i = 0; api_methods[i].func; i++) {
+ logmode = LOGMODE_NONE;
+ status = api_methods[i].func();
+ logmode = sv_logmode;
+ if (status != IFSTATUS_ERR) {
+ G.detect_link_func = api_methods[i].func;
+ bb_error_msg("using %s detection mode", api_methods[i].name);
+ goto _2;
}
- goto _1;
}
- G.detect_link_func = method[G.api_method_num - 1].func;
+ goto _1;
}
status = G.detect_link_func();
- _1:
+
if (status == IFSTATUS_ERR) {
+ _1:
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:
+ _2:
if (status != G.iface_last_status) {
G.iface_prev_status = G.iface_last_status;
G.iface_last_status = status;
@@ -423,7 +422,7 @@ static NOINLINE int check_existence_through_netlink(void)
struct nlmsghdr *mhdr;
ssize_t bytes;
- bytes = recv(netlink_fd, &replybuf, sizeof(replybuf), MSG_DONTWAIT);
+ bytes = recv(NETLINK_FD, &replybuf, sizeof(replybuf), MSG_DONTWAIT);
if (bytes < 0) {
if (errno == EAGAIN)
return G.iface_exists;
@@ -548,14 +547,14 @@ int ifplugd_main(int argc UNUSED_PARAM, char **argv)
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);
- G.api_method_num = api_mode_found - api_modes;
+ G.detect_link_func = api_methods[api_mode_found - api_modes].func;
if (!(opts & FLAG_NO_DAEMON))
bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
- xmove_fd(xsocket(AF_INET, SOCK_DGRAM, 0), ioctl_fd);
+ xmove_fd(xsocket(AF_INET, SOCK_DGRAM, 0), IOCTL_FD);
if (opts & FLAG_MONITOR) {
- xmove_fd(netlink_open(), netlink_fd);
+ xmove_fd(netlink_open(), NETLINK_FD);
}
write_pidfile(pidfile_name);
@@ -578,7 +577,7 @@ int ifplugd_main(int argc UNUSED_PARAM, char **argv)
if (opts & FLAG_MONITOR) {
struct ifreq ifrequest;
- set_ifreq_to_ifname(&ifrequest);
+ set_request_to_ifname(&ifrequest, sizeof(struct ifreq));
G.iface_exists = (network_ioctl(SIOCGIFINDEX, &ifrequest, NULL) == 0);
}
@@ -612,7 +611,7 @@ int ifplugd_main(int argc UNUSED_PARAM, char **argv)
}
/* Main loop */
- netlink_pollfd[0].fd = netlink_fd;
+ netlink_pollfd[0].fd = NETLINK_FD;
netlink_pollfd[0].events = POLLIN;
delay_time = 0;
while (1) {
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox