[Haskell-cafe] Re: Does readFile "/proc/mounts" hang for you?

Gregory Collins greg at gregorycollins.net
Thu Jan 22 13:12:17 EST 2009


Thomas DuBuisson <thomas.dubuisson at gmail.com> writes:

> open("/proc/mounts", O_RDONLY|O_NOCTTY|O_NONBLOCK) = 3
> fstat(3, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
> ioctl(3, SNDCTL_TMR_TIMEBASE or TCGETS, 0x7ffff7470b70) = -1 ENOTTY
> (Inappropriate ioctl for device)

This is to test whether the file is a terminal.

> select(4, [3], [], NULL, {0, 0})        = 0 (Timeout)
> select(4, [3], [], NULL, {134, 217727} <unfinished ...>

Here's the real issue: select() doesn't work on /proc entries (or at
least not that one). Here's a small C program to illustrate the
behaviour:


========================================================================
#include <sys/select.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>

int
main(int argc, char** argv)
{
    char*          filename;
    int            fd;
    struct timeval tv;
    fd_set         read_fds, write_fds;
    int            retval;

    if (argc < 2) return 1;

    filename = argv[1];

    fd = open(filename, O_RDONLY | O_NOCTTY | O_NONBLOCK);

    FD_ZERO(&read_fds);
    FD_SET(fd, &read_fds);
    FD_ZERO(&write_fds);

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

    retval = select(fd+1, &read_fds, &write_fds, NULL, &tv);
    
    printf("select() returned %d\n", retval);
    printf("FD_ISSET returns %d\n", FD_ISSET(fd, &read_fds));
}
========================================================================


Running this program on "/proc/mounts" shows that select() will never
report it as readable:

    ~/tmp/select $ gcc -o test_select test_select.c
    ~/tmp/select $ ./test_select /bin/ls
    select() returned 1
    FD_ISSET returns 1
    ~/tmp/select $ ./test_select /proc/mounts 
    select() returned 0
    FD_ISSET returns 0
    ~/tmp/select $ 


G.
-- 
Gregory Collins <greg at gregorycollins.net>


More information about the Haskell-Cafe mailing list