Update copyrights
[dhcpcd-ui] / src / dhcpcd-online / dhcpcd-online.c
index 334960ca4ba98b1464cf49972ebdddeb254a9ac9..a345be9826cfd3195664c8ba813612418de67433 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * dhcpcd-online
- * Copyright 2014 Roy Marples <roy@marples.name>
+ * Copyright 2014-2015 Roy Marples <roy@marples.name>
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
 # endif
 #endif
 
+#ifndef timespeccmp
+#define        timespeccmp(tsp, usp, cmp)                                      \
+       (((tsp)->tv_sec == (usp)->tv_sec) ?                             \
+           ((tsp)->tv_nsec cmp (usp)->tv_nsec) :                       \
+           ((tsp)->tv_sec cmp (usp)->tv_sec))
+#endif
+#ifndef timespecsub
+#define        timespecsub(tsp, usp, vsp)                                      \
+       do {                                                            \
+               (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec;          \
+               (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec;       \
+               if ((vsp)->tv_nsec < 0) {                               \
+                       (vsp)->tv_sec--;                                \
+                       (vsp)->tv_nsec += 1000000000L;                  \
+               }                                                       \
+       } while (/* CONSTCOND */ 0)
+#endif
+
 static void __dead
 do_exit(DHCPCD_CONNECTION *con, int code)
 {
 
+       /* Unregister the status callback so that close doesn't spam. */
+       dhcpcd_set_status_callback(con, NULL, NULL);
+
        dhcpcd_close(con);
        dhcpcd_free(con);
        exit(code);
 }
 
 static void
-do_status_cb(DHCPCD_CONNECTION *con, const char *status, void __unused *arg)
+do_status_cb(DHCPCD_CONNECTION *con, const char *status, void *arg)
 {
+       struct pollfd *pfd;
 
        syslog(LOG_INFO, "%s", status);
        if (strcmp(status, "connected") == 0)
                do_exit(con, EXIT_SUCCESS);
+       if (strcmp(status, "down") == 0) {
+               pfd = arg;
+               pfd->fd = -1;
+       }
 }
 
 int
@@ -122,9 +148,9 @@ main(int argc, char **argv)
                return EXIT_FAILURE;
        }
 
-       dhcpcd_set_status_callback(con, do_status_cb, NULL);
+       dhcpcd_set_status_callback(con, do_status_cb, &pfd);
 
-       if ((pfd.fd = dhcpcd_open(con)) == -1) {
+       if ((pfd.fd = dhcpcd_open(con, false)) == -1) {
                lerrno = errno;
                syslog(LOG_WARNING, "dhcpcd_open: %m");
                if (xflag)
@@ -160,8 +186,8 @@ main(int argc, char **argv)
                            (t.tv_nsec + 999999) / 1000000 > INT_MAX % 1000000))
                                timeout = INT_MAX;
                        else
-                               timeout = (int)t.tv_sec * 1000 +
-                                   (t.tv_nsec + 999999) / 1000000;
+                               timeout = (int)(t.tv_sec * 1000 +
+                                   (t.tv_nsec + 999999) / 1000000);
                        n = poll(&pfd, 1, timeout);
                }
                if (n == -1) {
@@ -169,7 +195,7 @@ main(int argc, char **argv)
                        do_exit(con, EXIT_FAILURE);
                }
                if (pfd.fd == -1) {
-                       if ((pfd.fd = dhcpcd_open(con)) == -1) {
+                       if ((pfd.fd = dhcpcd_open(con, false)) == -1) {
                                if (lerrno != errno) {
                                        lerrno = errno;
                                        syslog(LOG_WARNING, "dhcpcd_open: %m");