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.