This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

fcntl (locking) bug?


This is 1.5.25 but also happens on earlier versions.
It would appear that placing a shared read lock
on a file open for input/output is preventing the
the process itself from writing.
Consider -

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int
main ()
{
        int             fd;
        struct flock    lock;

        errno = 0;
        fd = open ("TESTFILE", O_CREAT | O_TRUNC | O_WRONLY, 0777);
        printf("Open for write - Errno %d\n", errno);
        if (fd >= 0) {
                errno = 0;
                if (write (fd, "TESTtest", 8) != 8) {
                        printf ("Write error - %d\n", errno);
                        close (fd);
                        return 1;
                }
                errno = 0;
                close (fd);
                printf("Close after write - Errno %d\n", errno);
        }
        fd = open ("TESTFILE", O_RDWR, 0);
        printf("Open for I/O - Errno %d\n", errno);
        if (fd < 0) {
                return 1;
        }
        memset (&lock, 0, sizeof (struct flock));
        lock.l_type = F_RDLCK;
        lock.l_whence = SEEK_SET;
        lock.l_start = 0;
        lock.l_len = 0;
        errno = 0;
        if (fcntl (fd, F_SETLK, &lock) < 0) {
                printf ("Lock error - %d\n", errno);
                close (fd);
                return 1;
        }
        errno = 0;
        if (write (fd, "test", 4) != 4) {
                printf ("Write I/O error - %d\n", errno);
        }
        memset (&lock, 0, sizeof (struct flock));
        lock.l_type = F_UNLCK;
        lock.l_whence = SEEK_SET;
        lock.l_start = 0;
        lock.l_len = 0;
        errno = 0;
        if (fcntl (fd, F_SETLK, &lock) < 0) {
                printf ("Unlock error - %d\n", errno);
                close (fd);
                return 1;
        }
        close (fd);
        return 0;
}

This always prodcues -
Write I/O error - 13
ie. EACCES

Margit


-- Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple Problem reports: http://cygwin.com/problems.html Documentation: http://cygwin.com/docs.html FAQ: http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]