select 仅检查 fds 直到 255,而不检查 FD_SETSIZE

问题描述 投票:0回答:1

选择高于 255 的 fd,不检查 fd 是否打开。这是我的示例代码:

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <sys/select.h>

int main()
{
    fd_set set;
    for(int i = 5;i<FD_SETSIZE;i++)
    {
        printf("--> i is %d\n", i);
        FD_ZERO(&set);
        FD_SET(i, &set);
        close(i);

        int retval = select(FD_SETSIZE, &set, NULL, NULL, NULL);
        if(-1 == retval)
        {
            perror("select");
        }
    }
}

这会导致:

--> i is 5
select: Bad file descriptor
...
--> i is 255
select: Bad file descriptor
--> i is 256

然后应用程序就会阻塞。 为什么这不会在 256 上创建

EBADF
直到 FD_SETSIZE?

从评论中请求信息:

prlimit
的结果是:

NOFILE     max number of open files                1024   1048576

这是

strace ./test_select
的结果:

select(1024, [127], NULL, NULL, NULL)   = -1 EBADF (Bad file descriptor)
dup(2)                                  = 3
fcntl(3, F_GETFL)                       = 0x8402 (flags O_RDWR|O_APPEND|O_LARGEFILE)
fstat(3, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 2), ...}) = 0
write(3, "select: Bad file descriptor\n", 28select: Bad file descriptor
) = 28
close(3)                                = 0
write(1, "--> i is 128\n", 13--> i is 128
)          = 13
close(128)                              = -1 EBADF (Bad file descriptor)
select(1024, [128], NULL, NULL, NULL

从评论中揭穿想法:

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <sys/select.h>
#include <fcntl.h>

int main()
{
    char filename[80];
    int fd;
    for(int i = 5;i<500;i++)
    {
        snprintf(filename, 80, "/tmp/file%d", i);
        fd = open(filename, O_RDWR | O_APPEND | O_CREAT);
    }
    printf("--> fd is %d, FD_SETSIZE is %d\n", fd, FD_SETSIZE);
    fd_set set;
    FD_ZERO(&set);
    FD_SET(fd, &set);
    int retval = select(FD_SETSIZE, NULL, &set, NULL, NULL);
    if(-1 == retval)
    {
        perror("select");
    }
}

结果:

$ ./test_select
--> fd is 523, FD_SETSIZE is 1024

进程正常退出,无阻塞。

c linux posix-select
1个回答
4
投票

这里发生了一些非常奇怪的事情。您可能发现了 Linux 内核中的一个错误。

我修改了你的测试程序,使其更加精确,并且在遇到问题时不会卡住:

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/time.h>

int main(void)
{
    fd_set set;
    struct timeval tv;
    int i;

    for(i = 5; i < FD_SETSIZE; i++)
    {
        FD_ZERO(&set);
        FD_SET(i, &set);

        tv.tv_sec = 0;
        tv.tv_usec = 1000;

        close(i);
        int retval = select(FD_SETSIZE, &set, 0, 0, &tv);
        if (retval == -1 && errno == EBADF)
          ;
        else
        {
            if (retval > 0)
                printf("fd %d: select returned success (%d)\n", i, retval);
            else if (retval == 0)
                printf("fd %d: select timed out\n", i);
            else
                printf("fd %d: select failed (%d; %s)\n", i, retval, strerror(errno));
            return 1;
        }
    }
    return 0;
}

我对 POSIX 的理解是,无论

FD_SETSIZE
是什么,这个程序都应该不产生任何输出并成功退出。这就是它在 FreeBSD 11.1 和 NetBSD 7.1 上所做的事情(两者都运行在某种描述的 x86 处理器上)。但在 Linux(x86-64,内核 4.13)上,它会打印

fd 256: select timed out

并且退出失败。更奇怪的是,如果我在

strace
下运行相同的二进制文件,会改变输出

$ strace -o /dev/null ./a.out
fd 64: select timed out

如果我在

gdb
下运行它,即使我不告诉
gdb
do 除了运行程序之外的任何事情,也会发生同样的事情。

Reading symbols from ./a.out...done.
(gdb) r
Starting program: /tmp/a.out 
fd 64: select timed out
[Inferior 1 (process 8209) exited with code 01]

所以某些事情正在发生变化,只是因为该过程受到

ptrace
监控。这只能是内核造成的。

我已经提交了一份关于 Linux 内核的 bug 报告,并将报告他们对此的看法。

© www.soinside.com 2019 - 2024. All rights reserved.