From: Lennart Poettering Date: Sat, 22 Oct 2005 23:47:48 +0000 (+0000) Subject: set O_NONBLOCK for expoted libdns_sd sockets X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=e559962871b8564afb6199f3b5fe24cd47607745;p=catta set O_NONBLOCK for expoted libdns_sd sockets git-svn-id: file:///home/lennart/svn/public/avahi/trunk@841 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- diff --git a/avahi-compat-libdns_sd/compat.c b/avahi-compat-libdns_sd/compat.c index 984e288..15166d4 100644 --- a/avahi-compat-libdns_sd/compat.c +++ b/avahi-compat-libdns_sd/compat.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include @@ -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;