ioctl给出无效的参数(ioctl giving Invalid Argument)

编程入门 行业动态 更新时间:2024-10-27 02:21:06
ioctl给出无效的参数(ioctl giving Invalid Argument)

我想在两个不同的程序之间发送一个打开的文件描述符。 所以我使用ioctl与named pipes这样做。 但在那里我得到了ioctl的无效参数。

#include <stropts.h> #include "accesories.c" #include <fcntl.h> #include <errno.h> #include <string.h> #include <sys/ioctl.h> #define MSGSIZ 63 char *fifo = "fifo"; int send_err(int fd, int errcode, const char *msg) { int n; if ((n = strlen(msg)) > 0) if (write(fd, msg, n) != n) /* send the error message */ return(-1); if (errcode >= 0) errcode = -1; /* must be negative */ if (send_fd(fd, errcode) < 0) return(-1); return(0); } int send_fd(int fd, int fd_to_send) { char buf[2]; /* send_fd()/recv_fd() 2-byte protocol */ buf[0] = 0; /* null byte flag to recv_fd() */ if (fd_to_send < 0) { buf[1] = -fd_to_send; /* nonzero status means error */ if (buf[1] == 0) buf[1] = 1; /* -256, etc. would screw up protocol */ } else { buf[1] = 0; /* zero status means OK */ } //printf("From the write %d\n",buf[0]); if (write(fd, buf, 2) != 2) return(-1); if (fd_to_send >= 0) if (ioctl(fd, I_SENDFD, fd_to_send) < 0) { printf("Eroor ::: %s\n",strerror(errno)); return(-1); } return(0); } int main(int argc, char const *argv[]) { int fd, j, nwrite; char msgbuf[MSGSIZ+1]; int fd_to_send; if((fd_to_send = open("vi",O_RDONLY)) < 0) printf("vi open failed"); if(argc < 2) { fprintf(stderr, "Usage: sendmessage msg ... \n"); exit(1); } /* open fifo with O_NONBLOCK set */ if((fd = open(fifo, O_WRONLY | O_NONBLOCK)) < 0) printf("fifo open failed"); /* send messages */ for (j = 1; j < argc; j++) { if(strlen(argv[j]) > MSGSIZ) { fprintf(stderr, "message too long %s\n", argv[j]); continue; } strcpy(msgbuf, argv[j]); if((nwrite = write(fd, msgbuf, 6)) == -1) printf("message write failed"); } printf("From send_fd %d \n",send_fd(fd,fd_to_send)); exit(0); }

文件附件.h只包含一些常见的包含文件。 首先,我发送一条简单的消息然后调用send_fd ,它首先发送一个2字节的消息,然后必须使用ioctl发送文件描述符。 但事实并非如此。

I want to send a opened file descriptor between two different programs. So I am using ioctl with named pipes to do so. But there I am getting Invalid argument for ioctl.

#include <stropts.h> #include "accesories.c" #include <fcntl.h> #include <errno.h> #include <string.h> #include <sys/ioctl.h> #define MSGSIZ 63 char *fifo = "fifo"; int send_err(int fd, int errcode, const char *msg) { int n; if ((n = strlen(msg)) > 0) if (write(fd, msg, n) != n) /* send the error message */ return(-1); if (errcode >= 0) errcode = -1; /* must be negative */ if (send_fd(fd, errcode) < 0) return(-1); return(0); } int send_fd(int fd, int fd_to_send) { char buf[2]; /* send_fd()/recv_fd() 2-byte protocol */ buf[0] = 0; /* null byte flag to recv_fd() */ if (fd_to_send < 0) { buf[1] = -fd_to_send; /* nonzero status means error */ if (buf[1] == 0) buf[1] = 1; /* -256, etc. would screw up protocol */ } else { buf[1] = 0; /* zero status means OK */ } //printf("From the write %d\n",buf[0]); if (write(fd, buf, 2) != 2) return(-1); if (fd_to_send >= 0) if (ioctl(fd, I_SENDFD, fd_to_send) < 0) { printf("Eroor ::: %s\n",strerror(errno)); return(-1); } return(0); } int main(int argc, char const *argv[]) { int fd, j, nwrite; char msgbuf[MSGSIZ+1]; int fd_to_send; if((fd_to_send = open("vi",O_RDONLY)) < 0) printf("vi open failed"); if(argc < 2) { fprintf(stderr, "Usage: sendmessage msg ... \n"); exit(1); } /* open fifo with O_NONBLOCK set */ if((fd = open(fifo, O_WRONLY | O_NONBLOCK)) < 0) printf("fifo open failed"); /* send messages */ for (j = 1; j < argc; j++) { if(strlen(argv[j]) > MSGSIZ) { fprintf(stderr, "message too long %s\n", argv[j]); continue; } strcpy(msgbuf, argv[j]); if((nwrite = write(fd, msgbuf, 6)) == -1) printf("message write failed"); } printf("From send_fd %d \n",send_fd(fd,fd_to_send)); exit(0); }

The file accessories .h only contain some common include files nothing else. First I am sending a simple message and then calling send_fd which is first sending a 2 byte message and then have to send file descriptor using ioctl. But it is not.

最满意答案

看起来linux不支持I_SENDFD 。 注释表明I_SENDFD在文档中,但实际上不受支持,并导致您遇到的错误消息。 STREAMS的维基百科条目声明linux内核对流没有任何支持。 维基百科条目确实指向了几个可用于添加流支持的第三方软件包,但LiS尚未移植到2.6内核,而OpenSS7在4年内没有任何活跃的开发。

但是,linux确实支持类似的东西。 此机制使用特殊消息类型SCM_RIGHTS通过sendmsg在UNIX域套接字上传递文件描述符,并从recvmsg 。 可以通过简单的Web搜索找到示例,完整的示例似乎来自“Linux编程接口 ”一书,其中包含发送和接收源。

It looks like linux doesn't support I_SENDFD. The comments indicate that I_SENDFD is in the documentation, but is not actually supported, and results in the error message you encountered. The wikipedia entry for STREAMS states the linux kernel does not have any support for streams. The wikipedia entry does point to a couple of third-party packages that could be used to add streams support, but LiS has not been ported to the 2.6 kernel, and OpenSS7 hasn't had any active development in 4 years.

However, linux does support something similar. This mechanism uses a special message type SCM_RIGHTS to deliver a file descriptor over a UNIX domain socket with sendmsg and obtained from recvmsg. Examples can be found with a simple web search, a complete example seems to be from the book The Linux Programming Interface, with source for sending and receiving.

更多推荐

本文发布于:2023-07-07 09:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1061408.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   ioctl   giving   Argument   Invalid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!