首页 \ 问答 \ ioctl给出无效的参数(ioctl giving Invalid Argument)

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

我想在两个不同的程序之间发送一个打开的文件描述符。 所以我使用ioctlnamed 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.


原文:https://stackoverflow.com/questions/11355552
更新时间:2023-03-21 10:03

最满意答案

为什么不在每个if语句中设置查询

switch (selectedIndex)
{
   case 0:
     dataGridView1.DataSource = db.users.Where(u => u.Id.Contains(...));
     break;
   ....
 }

Why not just set the query in each if statement

switch (selectedIndex)
{
   case 0:
     dataGridView1.DataSource = db.users.Where(u => u.Id.Contains(...));
     break;
   ....
 }

相关问答

更多
  • 为什么不在每个if语句中设置查询 switch (selectedIndex) { case 0: dataGridView1.DataSource = db.users.Where(u => u.Id.Contains(...)); break; .... } Why not just set the query in each if statement switch (selectedIndex) { case 0: dataGridView1.Dat ...
  • NHibernate 3中新的集成提供程序允许扩展它以支持您想要的任何构造。 有关完整示例,请访问http://fabiomaulo.blogspot.com/2010/07/nhibernate-linq-provider-extension.html 。 当然,您需要考虑该表达式的合理HQL表示。 The new integrated provider in NHibernate 3 allows extending it to support pretty much any construct you ...
  • 您可以使用Where来过滤集合。 像这样: var filteredIssues = issues.Where(x => x.Contains(project)).ToArray() Select用于将一个IEnumerbale投影到另一个IEnumerbale 。 把它想象成映射,你可以选择映射到目标类型的东西。 You can use Where to filter a collection. Like so: var filteredIssues = issues. ...
  • 看来你正在编译C程序的程序。 如果是这样,那么你必须写 void load_image(FILE*, struct Image*); 另一种方法是对结构使用typedef。 例如 typedef struct Image { struct FileHeader file_header; struct InfoHeader info_header; struct RGBQuads rgbquads; struct Pixel** pixel; struct Pixel ...
  • 你在找什么就像System.Linq.Dynamic 这将为您提供翻译如下查询的可能性: var query = from p in northwind.Products where p.CategoryID == 3 && p.UnitPrice > 3 orderby p.SupplierID select p; 成: var query = northwind.Products ...
  • 尝试使用喜欢 Try using like