[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[MiNT] hostfs bug with unlink() when an open handle exists



On 12/12/2013 01:03, Vincent Rivière wrote :
I'm going to post a simple testcase.

See the attached program.
It uses the following sequence, on the same file:
- open()
- unlink()
- fstat()

On a normal filesystem, unlink() removes the file reference from the filesystem, but the handle got with open() is still valid, so fstat() succeeds.

On hostfs, unlink() incorrectly destroys the handle obtained via open(), then fstat() fails with "No such file or directory".

This bug prevents using the "here document" syntax in bash when TMPDIR is set to a hostfs directory.

--
Vincent Rivière
/* Demonstration of hostfs bug with unlink() when an open handle exists */

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

int main(void)
{
    int fd, ret;
    //const char* filename = "/ram/mytemp"; // ramfs: ok
    const char* filename = "/tmp/mytemp"; // hostfs: bad
    struct stat st;

    // Open
    errno = 0;
    fd = open (filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
    printf("open(\"%s\") fd==%d\n", filename, fd);
    if (fd < 0) {
        printf("errno=%d %s\n", errno, strerror(errno));
        return 1;
    }
    
    // Unlink
    errno = 0;
    ret = unlink(filename);
    printf("unlink(\"%s\") ret==%d\n", filename, ret);
    if (ret < 0) {
        printf("errno=%d %s\n", errno, strerror(errno));
        return 1;
    }

    // Stat
    errno = 0;
    ret = fstat(fd, &st);
    printf("fstat(%d) ret=%d\n", fd, ret);
    if (ret < 0) {
        printf("errno=%d %s\n", errno, strerror(errno));
        return 1;
    }

    return 0;
}