Search code examples
processpipeioctl

Why is ioctl() not blocking?


I have written code for passing file descriptors between unrelated processes using streams. The server should wait for the client to send a file descriptor. Here is the server code:

#include <sys/types.h>   
#include <sys/stat.h>  
#include <fcntl.h>  
#include <stropts.h>  
#include <stdio.h>  
#include <errno.h>  
#include <unistd.h>  

int  main(int argc, char *argv[])
{  
    int fd;  
    int pipefd[2];  
    pipe(pipefd);  
    close(pipefd[1]);  
    recvfd(pipefd[0]);  
    return 0;
}

void recvfd(int p)  
{
    struct strrecvfd rfdbuf;  
    struct stat statbuf;   
    int i;  
    i=ioctl(p, I_RECVFD, &rfdbuf);  
    printf("errno=%d\n",errno);  
    printf("recvfd=%d\n", rfdbuf.fd);  
}

But I receive the error number 9 - Bad file descriptor.


Solution

  • You don't mention what OS you are running. I_RECVFD is part of the STREAMS extensions which are normally only present in System V based Unixy operating systems (AIX and Solaris for example). Others, like Linux and BSD, do not support it and probably never will as Posix now has an alternative using sendmsg() and recvmsg().

    I'm afraid that I don't know why Linux has #defines I_RECVFD if it does not support it.