zcip: simplify code
[oweals/busybox.git] / networking / ifplugd.c
index 86586f0fe180f14dd3e318ff94dae813c6c54de3..fef7a5ac9489e89d1e8445a4e17474c224d3a704 100644 (file)
@@ -289,8 +289,6 @@ static const struct {
        { "IFF_RUNNING"       , &detect_link_iff     },
 };
 
-
-
 static const char *strstatus(int status)
 {
        if (status == IFSTATUS_ERR)
@@ -451,20 +449,24 @@ static smallint detect_link(void)
 static NOINLINE int check_existence_through_netlink(void)
 {
        int iface_len;
-       char replybuf[1024];
+       /* Buffer was 1K, but on linux-3.9.9 it was reported to be too small.
+        * netlink.h: "limit to 8K to avoid MSG_TRUNC when PAGE_SIZE is very large".
+        * Note: on error returns (-1) we exit, no need to free replybuf.
+        */
+       enum { BUF_SIZE = 8 * 1024 };
+       char *replybuf = xmalloc(BUF_SIZE);
 
        iface_len = strlen(G.iface);
        while (1) {
                struct nlmsghdr *mhdr;
                ssize_t bytes;
 
-               bytes = recv(netlink_fd, &replybuf, sizeof(replybuf), MSG_DONTWAIT);
+               bytes = recv(netlink_fd, replybuf, BUF_SIZE, MSG_DONTWAIT);
                if (bytes < 0) {
                        if (errno == EAGAIN)
-                               return G.iface_exists;
+                               goto ret;
                        if (errno == EINTR)
                                continue;
-
                        bb_perror_msg("netlink: recv");
                        return -1;
                }
@@ -507,6 +509,8 @@ static NOINLINE int check_existence_through_netlink(void)
                }
        }
 
+ ret:
+       free(replybuf);
        return G.iface_exists;
 }
 
@@ -556,7 +560,8 @@ int ifplugd_main(int argc UNUSED_PARAM, char **argv)
 
        if (opts & FLAG_KILL) {
                if (pid_from_pidfile > 0)
-                       kill(pid_from_pidfile, SIGQUIT);
+                       /* Upstream tool use SIGINT for -k */
+                       kill(pid_from_pidfile, SIGINT);
                return EXIT_SUCCESS;
        }
 
@@ -645,7 +650,6 @@ int ifplugd_main(int argc UNUSED_PARAM, char **argv)
        delay_time = 0;
        while (1) {
                int iface_status_old;
-               int iface_exists_old;
 
                switch (bb_got_signal) {
                case SIGINT:
@@ -671,12 +675,12 @@ int ifplugd_main(int argc UNUSED_PARAM, char **argv)
                        goto exiting;
                }
 
-               iface_status_old = iface_status;
-               iface_exists_old = G.iface_exists;
-
                if ((opts & FLAG_MONITOR)
                 && (netlink_pollfd[0].revents & POLLIN)
                ) {
+                       int iface_exists_old;
+
+                       iface_exists_old = G.iface_exists;
                        G.iface_exists = check_existence_through_netlink();
                        if (G.iface_exists < 0) /* error */
                                goto exiting;
@@ -689,6 +693,7 @@ int ifplugd_main(int argc UNUSED_PARAM, char **argv)
                }
 
                /* note: if !G.iface_exists, returns DOWN */
+               iface_status_old = iface_status;
                iface_status = detect_link();
                if (iface_status == IFSTATUS_ERR) {
                        if (!(opts & FLAG_MONITOR))
@@ -702,7 +707,7 @@ int ifplugd_main(int argc UNUSED_PARAM, char **argv)
 
                        if (delay_time) {
                                /* link restored its old status before
-                                * we run script. don't run the script: */
+                                * we ran script. don't run the script: */
                                delay_time = 0;
                        } else {
                                delay_time = monotonic_sec();
@@ -710,15 +715,19 @@ int ifplugd_main(int argc UNUSED_PARAM, char **argv)
                                        delay_time += G.delay_up;
                                if (iface_status == IFSTATUS_DOWN)
                                        delay_time += G.delay_down;
-                               if (delay_time == 0)
-                                       delay_time++;
+#if 0  /* if you are back in 1970... */
+                               if (delay_time == 0) {
+                                       sleep(1);
+                                       delay_time = 1;
+                               }
+#endif
                        }
                }
 
                if (delay_time && (int)(monotonic_sec() - delay_time) >= 0) {
-                       delay_time = 0;
                        if (run_script(iface_status_str) != 0)
                                goto exiting;
+                       delay_time = 0;
                }
        } /* while (1) */