]> err.no Git - yubikey-server-c/commitdiff
Use select(2) to consume as little CPU as possible
authorTollef Fog Heen <tfheen@err.no>
Mon, 12 Oct 2009 10:20:11 +0000 (12:20 +0200)
committerTollef Fog Heen <tfheen@err.no>
Mon, 12 Oct 2009 10:20:11 +0000 (12:20 +0200)
src/main.c

index 67491e513dc7aa522c437f0251de7aa9ea86da26..6a8cf498d87355bb7ea34b0d6c9c9fed2e8ca5f8 100644 (file)
@@ -31,8 +31,9 @@
 #include <yubikey.h>
 #include <time.h>
 #include <gcrypt.h>
-#include "util.h"
+#include <sys/select.h>
 #include <arpa/inet.h>
+#include "util.h"
 
 #ifdef UNUSED
 #elif defined(__GNUC__)
@@ -465,13 +466,36 @@ int main(int UNUSED(argc), char ** UNUSED(argv))
                             NULL, /* Access policy handler */
                             NULL, /* Data to access policy handler */
                             handle_request, /* default handler for all URIs */
-                             NULL, /* Data for default handler */
-                             MHD_OPTION_END);
+                            NULL, /* Data for default handler */
+                            MHD_OPTION_END);
        if (d == NULL) {
                fprintf(stderr, "Could not start daemon\n");
                exit(1);
        }
        while (1) {
+               fd_set rs, ws, es;
+               int max_fd = 0;
+               unsigned long long timeout;
+               struct timeval tv;
+
+               FD_ZERO(&rs);
+               FD_ZERO(&ws);
+               FD_ZERO(&es);
+
+               if (MHD_get_fdset(d, &rs, &ws, &es, &max_fd) == MHD_NO) {
+                       MHD_stop_daemon(d);
+                       exit(1);
+               }
+               if (MHD_get_timeout(d, &timeout) == MHD_NO) {
+                       timeout = 0;
+               }
+               tv.tv_usec = (timeout % 1000) * 1000;
+               tv.tv_sec = timeout / 1000;
+               if (timeout == 0) {
+                       select(max_fd+1, &rs, &ws, &es, NULL);
+               } else {
+                       select(max_fd+1, &rs, &ws, &es, &tv);
+               }
                if (MHD_run(d) == MHD_NO) {
                        MHD_stop_daemon(d);
                        exit(1);