给人的ioctl参数无效 [英] ioctl giving Invalid Argument

查看:350
本文介绍了给人的ioctl参数无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想给两个不同的程序之间的打开文件描述符。所以我使用的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);

}

该文件附件.H只包含一些常见的包括文件没有别的。
首先我发送一个简单的消息,然后调用 send_fd 这是先发送2个字节的消息,然后在使用的ioctl发送文件描述符。但事实并非如此。

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内核并没有对物流的任何支持。在维基百科条目确实点到了几个可能被用来添加流支持的第三方软件包,但的李氏还没有被移植到2.6内核,并 OpenSS7 不是招'T有任何积极的发展4年。

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.

但是,Linux不支持类似的东西。这种机制使用一个特殊的消息类型 SCM_RIGHTS 提供了一个UNIX域套接字文件描述符与 SENDMSG 并获得 recvmsg 。例子可以用一个简单的网络搜索发现,一个完整的例子似乎是从书的 Linux的编程接口的,有源的发送和的接收

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.

这篇关于给人的ioctl参数无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆