为什么 ioctl() 不阻塞?
我编写了使用流在不相关进程之间传递文件描述符的代码。 服务器应该等待客户端发送文件描述符。 这是服务器代码:
#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);
}
但我收到错误号 9 - 错误的文件描述符。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您没有提及您正在运行的操作系统。 I_RECVFD 是 STREAMS 扩展的一部分,通常仅存在于基于 System V 的 Unixy 操作系统(例如 AIX 和 Solaris)中。 其他系统,如 Linux 和 BSD,不支持它,并且可能永远不会支持它,因为 Posix 现在有使用 sendmsg() 和 recvmsg() 的替代方案。
如果Linux不支持的话,恐怕我不知道为什么会有#defines I_RECVFD。
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.
注意:自从写完这个答案以来,这个问题已经被广泛修改。
从哪里开始?
main()
返回一个int
。pipefd
未初始化。recvfd()
。main()
返回值。#define
值。#include
文件。fd
。statbuf
。i
。基本问题 - 使用未初始化的变量。
次要问题 - 有限的错误检查。
附加问题:从概念上讲,您需要一个可以传递文件描述符的服务器。 您需要该服务器从其他(不相关)进程可以创建的文件描述符中读取。 您需要相当仔细地查看手册,但可能需要服务器在 Unix 域套接字上侦听,或者可能在(命名的)FIFO 上读取。 然后其他程序可以打开套接字或 FIFO 并将自己的文件描述符发送到服务器。
NB: the question has been extensively modified since this answer was written.
Where to begin?
main()
returns anint
.pipefd
is not initialized.recvfd()
on another random file descriptor.main()
.#define
value.#include
files needed to make this compile.fd
.statbuf
.i
.Basic problem - use of uninitialized variables.
Subsidiary problem - limited error checking.
Additional problem: conceptually, you want a server that can be passed a file descriptor. You need that server to be reading from a file descriptor that other (unrelated) processes can create. You will need to look at the manuals rather carefully, but will probably need the server listening on a Unix-domain socket, or perhaps reading on a (named) FIFO. Other programs can then open the socket or FIFO and send their own file descriptor to the server.
这是实际的代码吗?
因为您无法创建或接收有效的文件描述符。
Is this the actual code ?
Cause nowhere do you create or receive a valid filedescriptor.
你永远不会初始化
pipefd
! 缺少一行...?close
调用之前You never initialize
pipefd
! Missing aline before the
close
call...?