]> git.meshlink.io Git - catta/commitdiff
set O_NONBLOCK for expoted libdns_sd sockets
authorLennart Poettering <lennart@poettering.net>
Sat, 22 Oct 2005 23:47:48 +0000 (23:47 +0000)
committerLennart Poettering <lennart@poettering.net>
Sat, 22 Oct 2005 23:47:48 +0000 (23:47 +0000)
git-svn-id: file:///home/lennart/svn/public/avahi/trunk@841 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe

avahi-compat-libdns_sd/compat.c

index 984e288b27c2776bb922a95f9a16bdf5ee2a104f..15166d4ccba11efb0e3818fd78a47d544e11095b 100644 (file)
@@ -31,6 +31,7 @@
 #include <string.h>
 #include <signal.h>
 #include <netinet/in.h>
+#include <fcntl.h>
 
 #include <avahi-common/simple-watch.h>
 #include <avahi-common/malloc.h>
@@ -46,6 +47,7 @@
 #include "dns_sd.h"
 
 enum {
+    COMMAND_COME_AGAIN = 0,
     COMMAND_POLL = 'p',
     COMMAND_QUIT = 'q',
     COMMAND_POLL_DONE = 'P',
@@ -171,6 +173,10 @@ static int read_command(int fd) {
     assert(fd >= 0);
     
     if ((r = read(fd, &command, 1)) != 1) {
+
+        if (errno == EAGAIN)
+            return COMMAND_COME_AGAIN;
+        
         fprintf(stderr, __FILE__": read() failed: %s\n", r < 0 ? strerror(errno) : "EOF");
         return -1;
     }
@@ -189,6 +195,21 @@ static int write_command(int fd, char reply) {
     return 0;
 }
 
+static int set_nonblock(int fd) {
+    int n;
+
+    assert(fd >= 0);
+
+    if ((n = fcntl(fd, F_GETFL)) < 0)
+        return -1;
+
+    if (n & O_NONBLOCK)
+        return 0;
+
+    return fcntl(fd, F_SETFL, n|O_NONBLOCK);
+}
+
+
 static int poll_func(struct pollfd *ufds, unsigned int nfds, int timeout, void *userdata) {
     DNSServiceRef sdref = userdata;
     int ret;
@@ -255,6 +276,9 @@ static void * thread_func(void *data) {
 
             case COMMAND_QUIT:
                 return NULL;
+
+            case COMMAND_COME_AGAIN:
+                break;
         }
         
     }
@@ -277,6 +301,8 @@ static DNSServiceRef sdref_new(void) {
     sdref->thread_fd = fd[0];
     sdref->main_fd = fd[1];
 
+    set_nonblock(sdref->main_fd);
+
     sdref->client = NULL;
     sdref->service_browser = NULL;
     sdref->service_resolver = NULL;
@@ -381,6 +407,7 @@ int DNSSD_API DNSServiceRefSockFD(DNSServiceRef sdref) {
 
 DNSServiceErrorType DNSSD_API DNSServiceProcessResult(DNSServiceRef sdref) {
     DNSServiceErrorType ret = kDNSServiceErr_Unknown;
+    int t;
 
     assert(sdref);
     assert(sdref->n_ref >= 1);
@@ -392,8 +419,11 @@ DNSServiceErrorType DNSSD_API DNSServiceProcessResult(DNSServiceRef sdref) {
     ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
     
     /* Cleanup notification socket */
-    if (read_command(sdref->main_fd) != COMMAND_POLL_DONE)
+    if ((t = read_command(sdref->main_fd)) != COMMAND_POLL_DONE) {
+        if (t == COMMAND_COME_AGAIN)
+            ret = kDNSServiceErr_Unknown;
         goto finish;
+    }
     
     if (avahi_simple_poll_dispatch(sdref->simple_poll) < 0)
         goto finish;