Problem with socketpair , AF_UNIX and select call - can anybody through any light on this!

Howard Wilkinson howard at cohtech.com
Fri Oct 12 08:53:52 UTC 2007


Gordon Messmer wrote:
> Howard Wilkinson wrote:
>> I wanted to add a socketpair connection from the master process to 
>> the servers which implement the actual functionality. I created the 
>> socketpair, set both ends non-blocking and added it to the select set 
>> of fd's. I get an immediate return saying that the socket is ready to 
>> read but when I issue a recv I get  -1 (EAGAIN). Nothing I have tried 
>> sets the required behaviour of only returning when there is 'real' 
>> data to read.
>
> You're probably better off asking on a Posix programming list than 
> here.  Wherever you ask, though, you should post the code in question, 
> or code that demonstrates the problem in a state that can be compiled 
> and verified by others.  While you've described what you think you've 
> done, you haven't given enough information for us to do anything but 
> guess at the problem.
>
Can you suggest such a list - I can only find lists for POSIX threads!

Code goes as follows

    int fd[2] = { -1, -1 };
    fd_set rfd;
    int rv;
    char buf[1024];
    struct timeval timeout = { 10, 0 };

    rv = socketpair(PF_UNIX, SOCK_DGRAM, 0, fd);
    if (rv < 0) Err("socketpair");

    fcntl(fd[0], F_SETFL, fcntl(fd[0], F_GETFL, 0) | O_NONBLOCK);

    FD_ZERO(rfd);
    FD_SET(fd[0], rfd);

    rv = select(fd[0]+1, &rfd, NULL, NULL, &timeout);

    if (FD_ISSET(fd[0], rfd)) {
    	printf("Socket is ready to read\n");
    	rv = recv(fd[0], buf, sizeof(buf), MSG_DONTWAIT);
    	if (rv < 0) Err("recv - %s(%d)", strerror(errno), errno);
    	printf("Socket returned %d bytes\n", rv);
    } else {
    	printf("Socket is not ready to read\n");
    }
    exit (0);

I contend that this should (if I understand the semantics correctly) 
print out "Socket not ready to read" instead I get "recv - Resource 
temporarily unavailable(11)"

Anybody?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listman.redhat.com/archives/fedora-list/attachments/20071012/c37ca9d3/attachment-0001.htm>


More information about the fedora-list mailing list