Disconnect when same SSID selected (#1)
[dhcpcd-ui] / src / dhcpcd-online / dhcpcd-online.c
index a345be9826cfd3195664c8ba813612418de67433..6596220de16ad41e6074e8d95891166762f3da35 100644 (file)
        } while (/* CONSTCOND */ 0)
 #endif
 
        } while (/* CONSTCOND */ 0)
 #endif
 
+/* Incase we need to pass anything else in context to status cb */
+struct ctx {
+       struct pollfd pollfd;
+};
+
 static void __dead
 do_exit(DHCPCD_CONNECTION *con, int code)
 {
 static void __dead
 do_exit(DHCPCD_CONNECTION *con, int code)
 {
@@ -84,16 +89,20 @@ do_exit(DHCPCD_CONNECTION *con, int code)
 }
 
 static void
 }
 
 static void
-do_status_cb(DHCPCD_CONNECTION *con, const char *status, void *arg)
+status_cb(DHCPCD_CONNECTION *con,
+    unsigned int status, const char *status_msg, void *arg)
 {
 {
-       struct pollfd *pfd;
+       struct ctx *ctx;
 
 
-       syslog(LOG_INFO, "%s", status);
-       if (strcmp(status, "connected") == 0)
+       syslog(LOG_INFO, "%s", status_msg);
+       switch (status) {
+       case DHC_CONNECTED:
                do_exit(con, EXIT_SUCCESS);
                do_exit(con, EXIT_SUCCESS);
-       if (strcmp(status, "down") == 0) {
-               pfd = arg;
-               pfd->fd = -1;
+               /* NOT REACHED */
+       case DHC_DOWN:
+               ctx = arg;
+               ctx->pollfd.fd = -1;
+               break;
        }
 }
 
        }
 }
 
@@ -103,7 +112,7 @@ main(int argc, char **argv)
        DHCPCD_CONNECTION *con;
        bool xflag;
        struct timespec now, end, t;
        DHCPCD_CONNECTION *con;
        bool xflag;
        struct timespec now, end, t;
-       struct pollfd pfd;
+       struct ctx ctx;
        int timeout, n, lerrno;
        long lnum;
        char *lend;
        int timeout, n, lerrno;
        long lnum;
        char *lend;
@@ -112,6 +121,9 @@ main(int argc, char **argv)
        timeout = 30;
 
        xflag = false;
        timeout = 30;
 
        xflag = false;
+       ctx.pollfd.fd = -1;
+       ctx.pollfd.events = POLLIN;
+       ctx.pollfd.revents = 0;
 
        openlog("dhcpcd-online", LOG_PERROR, 0);
        setlogmask(LOG_UPTO(LOG_INFO));
 
        openlog("dhcpcd-online", LOG_PERROR, 0);
        setlogmask(LOG_UPTO(LOG_INFO));
@@ -147,18 +159,15 @@ main(int argc, char **argv)
                syslog(LOG_ERR, "dhcpcd_new: %m");
                return EXIT_FAILURE;
        }
                syslog(LOG_ERR, "dhcpcd_new: %m");
                return EXIT_FAILURE;
        }
+       dhcpcd_set_status_callback(con, status_cb, &ctx);
 
 
-       dhcpcd_set_status_callback(con, do_status_cb, &pfd);
-
-       if ((pfd.fd = dhcpcd_open(con, false)) == -1) {
+       if ((ctx.pollfd.fd = dhcpcd_open(con, false)) == -1) {
                lerrno = errno;
                syslog(LOG_WARNING, "dhcpcd_open: %m");
                if (xflag)
                        do_exit(con, EXIT_FAILURE);
        } else
                lerrno = 0;
                lerrno = errno;
                syslog(LOG_WARNING, "dhcpcd_open: %m");
                if (xflag)
                        do_exit(con, EXIT_FAILURE);
        } else
                lerrno = 0;
-       pfd.events = POLLIN;
-       pfd.revents = 0;
 
        /* Work out our timeout time */
        if (clock_gettime(CLOCK_MONOTONIC, &end) == -1) {
 
        /* Work out our timeout time */
        if (clock_gettime(CLOCK_MONOTONIC, &end) == -1) {
@@ -176,7 +185,7 @@ main(int argc, char **argv)
                        syslog(LOG_ERR, "timed out");
                        do_exit(con, EXIT_FAILURE);
                }
                        syslog(LOG_ERR, "timed out");
                        do_exit(con, EXIT_FAILURE);
                }
-               if (pfd.fd == -1) {
+               if (ctx.pollfd.fd == -1) {
                        n = poll(NULL, 0, DHCPCD_RETRYOPEN);
                } else {
                        /* poll(2) should really take a timespec */
                        n = poll(NULL, 0, DHCPCD_RETRYOPEN);
                } else {
                        /* poll(2) should really take a timespec */
@@ -188,21 +197,21 @@ main(int argc, char **argv)
                        else
                                timeout = (int)(t.tv_sec * 1000 +
                                    (t.tv_nsec + 999999) / 1000000);
                        else
                                timeout = (int)(t.tv_sec * 1000 +
                                    (t.tv_nsec + 999999) / 1000000);
-                       n = poll(&pfd, 1, timeout);
+                       n = poll(&ctx.pollfd, 1, timeout);
                }
                if (n == -1) {
                        syslog(LOG_ERR, "poll: %m");
                        do_exit(con, EXIT_FAILURE);
                }
                }
                if (n == -1) {
                        syslog(LOG_ERR, "poll: %m");
                        do_exit(con, EXIT_FAILURE);
                }
-               if (pfd.fd == -1) {
-                       if ((pfd.fd = dhcpcd_open(con, false)) == -1) {
+               if (ctx.pollfd.fd == -1) {
+                       if ((ctx.pollfd.fd = dhcpcd_open(con, false)) == -1) {
                                if (lerrno != errno) {
                                        lerrno = errno;
                                        syslog(LOG_WARNING, "dhcpcd_open: %m");
                                }
                        }
                } else {
                                if (lerrno != errno) {
                                        lerrno = errno;
                                        syslog(LOG_WARNING, "dhcpcd_open: %m");
                                }
                        }
                } else {
-                       if (n > 0 && pfd.revents)
+                       if (n > 0 && ctx.pollfd.revents)
                                dhcpcd_dispatch(con);
                }
        }
                                dhcpcd_dispatch(con);
                }
        }